blob: d9ad2e05228fc493520527f6347d9b682da76408 [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;
Johan Redestigaa676182012-10-03 13:33:01 +0200101import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700102import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700103
104/**
105 * Controller for browser
106 */
107public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700108 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700109
110 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800111 private static final String SEND_APP_ID_EXTRA =
112 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700113 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800114
Michael Kolb8233fac2010-10-26 16:08:53 -0700115
116 // public message ids
117 public final static int LOAD_URL = 1001;
118 public final static int STOP_LOAD = 1002;
119
120 // Message Ids
121 private static final int FOCUS_NODE_HREF = 102;
122 private static final int RELEASE_WAKELOCK = 107;
123
124 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
125
126 private static final int OPEN_BOOKMARKS = 201;
127
128 private static final int EMPTY_MENU = -1;
129
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700131 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 final static int PREFERENCES_PAGE = 3;
133 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000134 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700135 final static int VOICE_RESULT = 6;
Ben Murdoch8029a772010-11-16 11:58:21 +0000136
Michael Kolb8233fac2010-10-26 16:08:53 -0700137 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
138
139 // As the ids are dynamically created, we can't guarantee that they will
140 // be in sequence, so this static array maps ids to a window number.
141 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
142 { R.id.window_one_menu_id, R.id.window_two_menu_id,
143 R.id.window_three_menu_id, R.id.window_four_menu_id,
144 R.id.window_five_menu_id, R.id.window_six_menu_id,
145 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
146
147 // "source" parameter for Google search through search key
148 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
149 // "source" parameter for Google search through simplily type
150 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
151
George Mount387d45d2011-10-07 15:57:53 -0700152 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700153 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
154
John Reckd7dd9b22011-08-30 09:18:29 -0700155 // A bitmap that is re-used in createScreenshot as scratch space
156 private static Bitmap sThumbnailBitmap;
157
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 private Activity mActivity;
159 private UI mUi;
160 private TabControl mTabControl;
161 private BrowserSettings mSettings;
162 private WebViewFactory mFactory;
163
164 private WakeLock mWakeLock;
165
166 private UrlHandler mUrlHandler;
167 private UploadHandler mUploadHandler;
168 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700169 private PageDialogsHandler mPageDialogsHandler;
170 private NetworkStateHandler mNetworkHandler;
171
Ben Murdoch8029a772010-11-16 11:58:21 +0000172 private Message mAutoFillSetupMessage;
173
Michael Kolb8233fac2010-10-26 16:08:53 -0700174 private boolean mShouldShowErrorConsole;
175
176 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
177
178 // FIXME, temp address onPrepareMenu performance problem.
179 // When we move everything out of view, we should rewrite this.
180 private int mCurrentMenuState = 0;
181 private int mMenuState = R.id.MAIN_MENU;
182 private int mOldMenuState = EMPTY_MENU;
183 private Menu mCachedMenu;
184
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 private boolean mMenuIsDown;
186
187 // For select and find, we keep track of the ActionMode so that
188 // finish() can be called as desired.
189 private ActionMode mActionMode;
190
191 /**
192 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
193 * of whether the configuration has changed. The first onMenuOpened call
194 * after a configuration change is simply a reopening of the same menu
195 * (i.e. mIconView did not change).
196 */
197 private boolean mConfigChanged;
198
199 /**
200 * Keeps track of whether the options menu is open. This is important in
201 * determining whether to show or hide the title bar overlay
202 */
203 private boolean mOptionsMenuOpen;
204
205 /**
206 * Whether or not the options menu is in its bigger, popup menu form. When
207 * true, we want the title bar overlay to be gone. When false, we do not.
208 * Only meaningful if mOptionsMenuOpen is true.
209 */
210 private boolean mExtendedMenuOpen;
211
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 private boolean mActivityPaused = true;
213 private boolean mLoadStopped;
214
215 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500216 // Checks to see when the bookmarks database has changed, and updates the
217 // Tabs' notion of whether they represent bookmarked sites.
218 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700219 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700220
Michael Kolbc3af0672011-08-09 10:24:41 -0700221 private boolean mBlockEvents;
222
Michael Kolb0b129122012-06-04 16:31:58 -0700223 private String mVoiceResult;
224
George Mount3636d0a2011-11-21 09:08:21 -0800225 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700226 mActivity = browser;
227 mSettings = BrowserSettings.getInstance();
228 mTabControl = new TabControl(this);
229 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700230 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800231 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700232 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700233
234 mUrlHandler = new UrlHandler(this);
235 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
237
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500239 mBookmarksObserver = new ContentObserver(mHandler) {
240 @Override
241 public void onChange(boolean selfChange) {
242 int size = mTabControl.getTabCount();
243 for (int i = 0; i < size; i++) {
244 mTabControl.getTab(i).updateBookmarkedStatus();
245 }
246 }
247
248 };
249 browser.getContentResolver().registerContentObserver(
250 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700251
252 mNetworkHandler = new NetworkStateHandler(mActivity, this);
253 // Start watching the default geolocation permissions
254 mSystemAllowGeolocationOrigins =
255 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
256 mSystemAllowGeolocationOrigins.start();
257
John Reckaf262e72011-07-25 13:55:44 -0700258 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 }
260
John Reck9c35b9c2012-05-30 10:08:50 -0700261 @Override
262 public void start(final Intent intent) {
Ben Murdochf695f6b2012-05-24 12:59:45 +0100263 WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800264 // mCrashRecoverHandler has any previously saved state.
265 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700266 }
267
George Mount3636d0a2011-11-21 09:08:21 -0800268 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 // Unless the last browser usage was within 24 hours, destroy any
270 // remaining incognito tabs.
271
272 Calendar lastActiveDate = icicle != null ?
273 (Calendar) icicle.getSerializable("lastActiveDate") : null;
274 Calendar today = Calendar.getInstance();
275 Calendar yesterday = Calendar.getInstance();
276 yesterday.add(Calendar.DATE, -1);
277
Patrick Scott7d50a932011-02-04 09:27:26 -0500278 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800280 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700281
Patrick Scott7d50a932011-02-04 09:27:26 -0500282 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700283 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500284 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000285
Michael Kolbc831b632011-05-11 09:30:34 -0700286 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500287 // Not able to restore so we go ahead and clear session cookies. We
288 // must do this before trying to login the user as we don't want to
289 // clear any session cookies set during login.
290 CookieManager.getInstance().removeSessionCookie();
291 }
292
Patrick Scottd43e75a2011-03-14 14:47:23 -0400293 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500294 new Runnable() {
295 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800296 onPreloginFinished(icicle, intent, currentTabId,
297 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500298 }
299 });
300 }
301
Michael Kolbc831b632011-05-11 09:30:34 -0700302 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800303 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700304 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700305 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800306 if (intent == null) {
307 // This won't happen under common scenarios. The icicle is
308 // not null, but there aren't any tabs to restore.
309 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700310 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800311 final Bundle extra = intent.getExtras();
312 // Create an initial tab.
313 // If the intent is ACTION_VIEW and data is not null, the Browser is
314 // invoked to view the content by another application. In this case,
315 // the tab will be close when exit.
316 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
317 Tab t = null;
318 if (urlData.isEmpty()) {
319 t = openTabToHomePage();
320 } else {
321 t = openTab(urlData);
322 }
323 if (t != null) {
324 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
325 }
326 WebView webView = t.getWebView();
327 if (extra != null) {
328 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
329 if (scale > 0 && scale <= 1000) {
330 webView.setInitialScale(scale);
331 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700332 }
333 }
John Reckd8c74522011-06-14 08:45:00 -0700334 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700336 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500337 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700338 List<Tab> tabs = mTabControl.getTabs();
339 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
340 for (Tab t : tabs) {
341 restoredTabs.add(t.getId());
342 }
343 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700344 if (tabs.size() == 0) {
345 openTabToHomePage();
346 }
John Reck1cf4b792011-07-26 10:22:22 -0700347 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700348 // TabControl.restoreState() will create a new tab even if
349 // restoring the state fails.
350 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800351 // Intent is non-null when framework thinks the browser should be
352 // launching with a new intent (icicle is null).
353 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700354 mIntentHandler.onNewIntent(intent);
355 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700358 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 if (jsFlags.trim().length() != 0) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000360 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 }
George Mount3636d0a2011-11-21 09:08:21 -0800362 if (intent != null
363 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700364 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800365 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 }
367
John Reck1cf4b792011-07-26 10:22:22 -0700368 private static class PruneThumbnails implements Runnable {
369 private Context mContext;
370 private List<Long> mIds;
371
372 PruneThumbnails(Context context, List<Long> preserveIds) {
373 mContext = context.getApplicationContext();
374 mIds = preserveIds;
375 }
376
377 @Override
378 public void run() {
379 ContentResolver cr = mContext.getContentResolver();
380 if (mIds == null || mIds.size() == 0) {
381 cr.delete(Thumbnails.CONTENT_URI, null, null);
382 } else {
383 int length = mIds.size();
384 StringBuilder where = new StringBuilder();
385 where.append(Thumbnails._ID);
386 where.append(" not in (");
387 for (int i = 0; i < length; i++) {
388 where.append(mIds.get(i));
389 if (i < (length - 1)) {
390 where.append(",");
391 }
392 }
393 where.append(")");
394 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
395 }
396 }
397
398 }
399
Michael Kolb1514bb72010-11-22 09:11:48 -0800400 @Override
401 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 return mFactory;
403 }
404
405 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800406 public void onSetWebView(Tab tab, WebView view) {
407 mUi.onSetWebView(tab, view);
408 }
409
410 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800411 public void createSubWindow(Tab tab) {
412 endActionMode();
413 WebView mainView = tab.getWebView();
414 WebView subView = mFactory.createWebView((mainView == null)
415 ? false
416 : mainView.isPrivateBrowsingEnabled());
417 mUi.createSubWindow(tab, subView);
418 }
419
420 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700421 public Context getContext() {
422 return mActivity;
423 }
424
425 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700426 public Activity getActivity() {
427 return mActivity;
428 }
429
430 void setUi(UI ui) {
431 mUi = ui;
432 }
433
Michael Kolbaf63dba2012-05-16 12:58:05 -0700434 @Override
435 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700436 return mSettings;
437 }
438
439 IntentHandler getIntentHandler() {
440 return mIntentHandler;
441 }
442
443 @Override
444 public UI getUi() {
445 return mUi;
446 }
447
448 int getMaxTabs() {
449 return mActivity.getResources().getInteger(R.integer.max_tabs);
450 }
451
452 @Override
453 public TabControl getTabControl() {
454 return mTabControl;
455 }
456
Michael Kolb1bf23132010-11-19 12:55:12 -0800457 @Override
458 public List<Tab> getTabs() {
459 return mTabControl.getTabs();
460 }
461
John Reckaf262e72011-07-25 13:55:44 -0700462 // Open the icon database.
463 private void openIconDatabase() {
464 // We have to call getInstance on the UI thread
465 final WebIconDatabase instance = WebIconDatabase.getInstance();
466 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000467
John Reckaf262e72011-07-25 13:55:44 -0700468 @Override
469 public void run() {
470 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700471 }
John Reckaf262e72011-07-25 13:55:44 -0700472 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700473 }
474
475 private void startHandler() {
476 mHandler = new Handler() {
477
478 @Override
479 public void handleMessage(Message msg) {
480 switch (msg.what) {
481 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700482 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700483 break;
484 case FOCUS_NODE_HREF:
485 {
486 String url = (String) msg.getData().get("url");
487 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500488 String src = (String) msg.getData().get("src");
489 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700490 if (TextUtils.isEmpty(url)) {
491 break;
492 }
493 HashMap focusNodeMap = (HashMap) msg.obj;
494 WebView view = (WebView) focusNodeMap.get("webview");
495 // Only apply the action if the top window did not change.
496 if (getCurrentTopWebView() != view) {
497 break;
498 }
499 switch (msg.arg1) {
500 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700501 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500503 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700504 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500505 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500506 case R.id.open_newtab_context_menu_id:
507 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700508 openTab(url, parent,
509 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500510 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 case R.id.copy_link_context_menu_id:
512 copy(url);
513 break;
514 case R.id.save_link_context_menu_id:
515 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500516 DownloadHandler.onDownloadStartNoStream(
Andreas Sandblad8e4ce662012-06-11 11:02:49 +0200517 mActivity, url, view.getSettings().getUserAgentString(),
518 null, null, view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 break;
520 }
521 break;
522 }
523
524 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700525 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700526 break;
527
528 case STOP_LOAD:
529 stopLoading();
530 break;
531
532 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700533 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700534 mWakeLock.release();
535 // if we reach here, Browser should be still in the
536 // background loading after WAKELOCK_TIMEOUT (5-min).
537 // To avoid burning the battery, stop loading.
538 mTabControl.stopAllLoading();
539 }
540 break;
541
542 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800543 Tab tab = (Tab) msg.obj;
544 if (tab != null) {
545 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 }
547 break;
548 }
549 }
550 };
551
552 }
553
John Reckef654f12011-07-12 16:42:08 -0700554 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200555 public Tab getCurrentTab() {
556 return mTabControl.getCurrentTab();
557 }
558
Michael Kolbba99c5d2010-11-29 14:57:41 -0800559 @Override
560 public void shareCurrentPage() {
561 shareCurrentPage(mTabControl.getCurrentTab());
562 }
563
564 private void shareCurrentPage(Tab tab) {
565 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800566 sharePage(mActivity, tab.getTitle(),
567 tab.getUrl(), tab.getFavicon(),
568 createScreenshot(tab.getWebView(),
569 getDesiredThumbnailWidth(mActivity),
570 getDesiredThumbnailHeight(mActivity)));
571 }
572 }
573
Michael Kolb8233fac2010-10-26 16:08:53 -0700574 /**
575 * Share a page, providing the title, url, favicon, and a screenshot. Uses
576 * an {@link Intent} to launch the Activity chooser.
577 * @param c Context used to launch a new Activity.
578 * @param title Title of the page. Stored in the Intent with
579 * {@link Intent#EXTRA_SUBJECT}
580 * @param url URL of the page. Stored in the Intent with
581 * {@link Intent#EXTRA_TEXT}
582 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
583 * with {@link Browser#EXTRA_SHARE_FAVICON}
584 * @param screenshot Bitmap of a screenshot of the page. Stored in the
585 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
586 */
587 static final void sharePage(Context c, String title, String url,
588 Bitmap favicon, Bitmap screenshot) {
589 Intent send = new Intent(Intent.ACTION_SEND);
590 send.setType("text/plain");
591 send.putExtra(Intent.EXTRA_TEXT, url);
592 send.putExtra(Intent.EXTRA_SUBJECT, title);
593 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
594 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
595 try {
596 c.startActivity(Intent.createChooser(send, c.getString(
597 R.string.choosertitle_sharevia)));
598 } catch(android.content.ActivityNotFoundException ex) {
599 // if no app handles it, do nothing
600 }
601 }
602
603 private void copy(CharSequence text) {
604 ClipboardManager cm = (ClipboardManager) mActivity
605 .getSystemService(Context.CLIPBOARD_SERVICE);
606 cm.setText(text);
607 }
608
609 // lifecycle
610
John Reck9c35b9c2012-05-30 10:08:50 -0700611 @Override
612 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800614 // update the menu in case of a locale change
615 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 if (mPageDialogsHandler != null) {
617 mPageDialogsHandler.onConfigurationChanged(config);
618 }
619 mUi.onConfigurationChanged(config);
620 }
621
622 @Override
623 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700624 if (!mUi.isWebShowing()) {
625 mUi.showWeb(false);
626 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700627 mIntentHandler.onNewIntent(intent);
628 }
629
John Reck9c35b9c2012-05-30 10:08:50 -0700630 @Override
631 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800632 if (mUi.isCustomViewShowing()) {
633 hideCustomView();
634 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 if (mActivityPaused) {
636 Log.e(LOGTAG, "BrowserActivity is already paused.");
637 return;
638 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800640 Tab tab = mTabControl.getCurrentTab();
641 if (tab != null) {
642 tab.pause();
643 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700644 if (mWakeLock == null) {
645 PowerManager pm = (PowerManager) mActivity
646 .getSystemService(Context.POWER_SERVICE);
647 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
648 }
Michael Kolb70976932010-11-30 11:34:01 -0800649 mWakeLock.acquire();
650 mHandler.sendMessageDelayed(mHandler
651 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
652 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 }
654 mUi.onPause();
655 mNetworkHandler.onPause();
656
657 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100658 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700659 if (sThumbnailBitmap != null) {
660 sThumbnailBitmap.recycle();
661 sThumbnailBitmap = null;
662 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 }
664
John Reck9c35b9c2012-05-30 10:08:50 -0700665 @Override
666 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800668 Bundle saveState = createSaveState();
669
670 // crash recovery manages all save & restore state
671 mCrashRecoveryHandler.writeState(saveState);
672 mSettings.setLastRunPaused(true);
673 }
674
675 /**
676 * Save the current state to outState. Does not write the state to
677 * disk.
678 * @return Bundle containing the current state of all tabs.
679 */
680 /* package */ Bundle createSaveState() {
681 Bundle saveState = new Bundle();
682 mTabControl.saveState(saveState);
683 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700684 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800685 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700686 }
George Mount3636d0a2011-11-21 09:08:21 -0800687 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 }
689
John Reck9c35b9c2012-05-30 10:08:50 -0700690 @Override
691 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 if (!mActivityPaused) {
693 Log.e(LOGTAG, "BrowserActivity is already resumed.");
694 return;
695 }
George Mount3636d0a2011-11-21 09:08:21 -0800696 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800698 Tab current = mTabControl.getCurrentTab();
699 if (current != null) {
700 current.resume();
701 resumeWebViewTimers(current);
702 }
John Reckf57c0292011-07-21 18:15:39 -0700703 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200704
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 mUi.onResume();
706 mNetworkHandler.onResume();
707 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100708 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700709 if (mVoiceResult != null) {
710 mUi.onVoiceResult(mVoiceResult);
711 mVoiceResult = null;
712 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 }
714
John Reckf57c0292011-07-21 18:15:39 -0700715 private void releaseWakeLock() {
716 if (mWakeLock != null && mWakeLock.isHeld()) {
717 mHandler.removeMessages(RELEASE_WAKELOCK);
718 mWakeLock.release();
719 }
720 }
721
Michael Kolb70976932010-11-30 11:34:01 -0800722 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800723 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800724 * @param tab guaranteed non-null
725 */
726 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 boolean inLoad = tab.inPageLoad();
728 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
729 CookieSyncManager.getInstance().startSync();
730 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100731 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700732 }
733 }
734
Michael Kolb70976932010-11-30 11:34:01 -0800735 /**
736 * Pause all WebView timers using the WebView of the given tab
737 * @param tab
738 * @return true if the timers are paused or tab is null
739 */
740 private boolean pauseWebViewTimers(Tab tab) {
741 if (tab == null) {
742 return true;
743 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100745 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700746 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700747 }
Michael Kolb70976932010-11-30 11:34:01 -0800748 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700749 }
750
John Reck9c35b9c2012-05-30 10:08:50 -0700751 @Override
752 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800753 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
755 mUploadHandler = null;
756 }
757 if (mTabControl == null) return;
758 mUi.onDestroy();
759 // Remove the current tab and sub window
760 Tab t = mTabControl.getCurrentTab();
761 if (t != null) {
762 dismissSubWindow(t);
763 removeTab(t);
764 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500765 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700766 // Destroy all the tabs
767 mTabControl.destroy();
768 WebIconDatabase.getInstance().close();
769 // Stop watching the default geolocation permissions
770 mSystemAllowGeolocationOrigins.stop();
771 mSystemAllowGeolocationOrigins = null;
772 }
773
774 protected boolean isActivityPaused() {
775 return mActivityPaused;
776 }
777
John Reck9c35b9c2012-05-30 10:08:50 -0700778 @Override
779 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700780 mTabControl.freeMemory();
781 }
782
783 @Override
784 public boolean shouldShowErrorConsole() {
785 return mShouldShowErrorConsole;
786 }
787
788 protected void setShouldShowErrorConsole(boolean show) {
789 if (show == mShouldShowErrorConsole) {
790 // Nothing to do.
791 return;
792 }
793 mShouldShowErrorConsole = show;
794 Tab t = mTabControl.getCurrentTab();
795 if (t == null) {
796 // There is no current tab so we cannot toggle the error console
797 return;
798 }
799 mUi.setShouldShowErrorConsole(t, show);
800 }
801
802 @Override
803 public void stopLoading() {
804 mLoadStopped = true;
805 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700807 if (w != null) {
808 w.stopLoading();
809 mUi.onPageStopped(tab);
810 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 }
812
813 boolean didUserStopLoading() {
814 return mLoadStopped;
815 }
816
817 // WebViewController
818
819 @Override
John Reck324d4402011-01-11 16:56:42 -0800820 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700821
822 // We've started to load a new page. If there was a pending message
823 // to save a screenshot then we will now take the new page and save
824 // an incorrect screenshot. Therefore, remove any pending thumbnail
825 // messages from the queue.
826 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800827 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700828
829 // reset sync timer to avoid sync starts during loading a page
830 CookieSyncManager.getInstance().resetSync();
831
832 if (!mNetworkHandler.isNetworkUp()) {
833 view.setNetworkAvailable(false);
834 }
835
836 // when BrowserActivity just starts, onPageStarted may be called before
837 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
838 // to start the timer. As we won't switch tabs while an activity is in
839 // pause state, we can ensure calling resume and pause in pair.
840 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800841 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 }
843 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 endActionMode();
845
John Reck30c714c2010-12-16 17:30:34 -0800846 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700847
John Reck324d4402011-01-11 16:56:42 -0800848 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 // update the bookmark database for favicon
850 maybeUpdateFavicon(tab, null, url, favicon);
851
852 Performance.tracePageStart(url);
853
854 // Performance probe
855 if (false) {
856 Performance.onPageStarted();
857 }
858
859 }
860
861 @Override
John Reck324d4402011-01-11 16:56:42 -0800862 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700863 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800864 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200865
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 // Performance probe
867 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800868 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 }
870
871 Performance.tracePageFinished();
872 }
873
874 @Override
John Reck30c714c2010-12-16 17:30:34 -0800875 public void onProgressChanged(Tab tab) {
876 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700877
878 if (newProgress == 100) {
879 CookieSyncManager.getInstance().sync();
880 // onProgressChanged() may continue to be called after the main
881 // frame has finished loading, as any remaining sub frames continue
882 // to load. We'll only get called once though with newProgress as
883 // 100 when everything is loaded. (onPageFinished is called once
884 // when the main frame completes loading regardless of the state of
885 // any sub frames so calls to onProgressChanges may continue after
886 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800887 if (tab.inPageLoad()) {
888 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200889 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
890 // pause the WebView timer and release the wake lock if it is
891 // finished while BrowserActivity is in pause state.
892 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 }
John Reckd9862372012-02-21 15:04:50 -0800894 if (!tab.isPrivateBrowsingEnabled()
895 && !TextUtils.isEmpty(tab.getUrl())
896 && !tab.isSnapshot()) {
897 // Only update the bookmark screenshot if the user did not
898 // cancel the load early and there is not already
899 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700900 if (tab.shouldUpdateThumbnail() &&
901 (tab.inForeground() && !didUserStopLoading()
902 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800903 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
904 mHandler.sendMessageDelayed(mHandler.obtainMessage(
905 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
906 500);
907 }
908 }
909 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700910 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800911 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700912 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800913 // still loading
914 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700915 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800916 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700917 }
918 }
John Reck30c714c2010-12-16 17:30:34 -0800919 mUi.onProgressChanged(tab);
920 }
921
922 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100923 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800924 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700925 }
926
927 @Override
928 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800929 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800930 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800931 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700932 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
933 return;
934 }
935 // Update the title in the history database if not in private browsing mode
936 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700937 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 }
939 }
940
941 @Override
942 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800943 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700944 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
945 }
946
947 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800948 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
949 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 }
951
952 @Override
953 public boolean shouldOverrideKeyEvent(KeyEvent event) {
954 if (mMenuIsDown) {
955 // only check shortcut key when MENU is held
956 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
957 event);
958 } else {
959 return false;
960 }
961 }
962
963 @Override
John Reck997b1b72012-04-19 18:08:25 -0700964 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 if (!isActivityPaused()) {
966 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700967 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700968 } else {
John Reck997b1b72012-04-19 18:08:25 -0700969 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 }
971 }
John Reck997b1b72012-04-19 18:08:25 -0700972 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700973 }
974
975 @Override
John Reck324d4402011-01-11 16:56:42 -0800976 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 // Don't save anything in private browsing mode
978 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800979 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700980
John Reck324d4402011-01-11 16:56:42 -0800981 if (TextUtils.isEmpty(url)
982 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 return;
984 }
John Reckf57c0292011-07-21 18:15:39 -0700985 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700986 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 }
988
989 @Override
990 public void getVisitedHistory(final ValueCallback<String[]> callback) {
991 AsyncTask<Void, Void, String[]> task =
992 new AsyncTask<Void, Void, String[]>() {
993 @Override
994 public String[] doInBackground(Void... unused) {
995 return Browser.getVisitedHistory(mActivity.getContentResolver());
996 }
997 @Override
998 public void onPostExecute(String[] result) {
999 callback.onReceiveValue(result);
1000 }
1001 };
1002 task.execute();
1003 }
1004
1005 @Override
1006 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1007 final HttpAuthHandler handler, final String host,
1008 final String realm) {
1009 String username = null;
1010 String password = null;
1011
1012 boolean reuseHttpAuthUsernamePassword
1013 = handler.useHttpAuthUsernamePassword();
1014
1015 if (reuseHttpAuthUsernamePassword && view != null) {
1016 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1017 if (credentials != null && credentials.length == 2) {
1018 username = credentials[0];
1019 password = credentials[1];
1020 }
1021 }
1022
1023 if (username != null && password != null) {
1024 handler.proceed(username, password);
1025 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001026 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001027 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1028 } else {
1029 handler.cancel();
1030 }
1031 }
1032 }
1033
1034 @Override
1035 public void onDownloadStart(Tab tab, String url, String userAgent,
1036 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001037 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -05001038 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001039 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
1040 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 // This Tab was opened for the sole purpose of downloading a
1042 // file. Remove it.
1043 if (tab == mTabControl.getCurrentTab()) {
1044 // In this case, the Tab is still on top.
1045 goBackOnePageOrQuit();
1046 } else {
1047 // In this case, it is not.
1048 closeTab(tab);
1049 }
1050 }
1051 }
1052
1053 @Override
1054 public Bitmap getDefaultVideoPoster() {
1055 return mUi.getDefaultVideoPoster();
1056 }
1057
1058 @Override
1059 public View getVideoLoadingProgressView() {
1060 return mUi.getVideoLoadingProgressView();
1061 }
1062
1063 @Override
1064 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1065 SslError error) {
1066 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1067 }
1068
Patrick Scott92066772011-03-10 08:46:27 -05001069 @Override
1070 public void showAutoLogin(Tab tab) {
1071 assert tab.inForeground();
1072 // Update the title bar to show the auto-login request.
1073 mUi.showAutoLogin(tab);
1074 }
1075
1076 @Override
1077 public void hideAutoLogin(Tab tab) {
1078 assert tab.inForeground();
1079 mUi.hideAutoLogin(tab);
1080 }
1081
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 // helper method
1083
1084 /*
1085 * Update the favorites icon if the private browsing isn't enabled and the
1086 * icon is valid.
1087 */
1088 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1089 final String url, Bitmap favicon) {
1090 if (favicon == null) {
1091 return;
1092 }
1093 if (!tab.isPrivateBrowsingEnabled()) {
1094 Bookmarks.updateFavicon(mActivity
1095 .getContentResolver(), originalUrl, url, favicon);
1096 }
1097 }
1098
Leon Scroggins4cd97792010-12-03 15:31:56 -05001099 @Override
1100 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001101 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001102 mUi.bookmarkedStatusHasChanged(tab);
1103 }
1104
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 // end WebViewController
1106
1107 protected void pageUp() {
1108 getCurrentTopWebView().pageUp(false);
1109 }
1110
1111 protected void pageDown() {
1112 getCurrentTopWebView().pageDown(false);
1113 }
1114
1115 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001116 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 public void editUrl() {
1118 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001119 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 }
1121
John Reck9c35b9c2012-05-30 10:08:50 -07001122 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001123 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001124 WebChromeClient.CustomViewCallback callback) {
1125 if (tab.inForeground()) {
1126 if (mUi.isCustomViewShowing()) {
1127 callback.onCustomViewHidden();
1128 return;
1129 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001130 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 // Save the menu state and set it to empty while the custom
1132 // view is showing.
1133 mOldMenuState = mMenuState;
1134 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001135 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 }
1137 }
1138
1139 @Override
1140 public void hideCustomView() {
1141 if (mUi.isCustomViewShowing()) {
1142 mUi.onHideCustomView();
1143 // Reset the old menu state.
1144 mMenuState = mOldMenuState;
1145 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001146 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001147 }
1148 }
1149
John Reck9c35b9c2012-05-30 10:08:50 -07001150 @Override
1151 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 Intent intent) {
1153 if (getCurrentTopWebView() == null) return;
1154 switch (requestCode) {
1155 case PREFERENCES_PAGE:
1156 if (resultCode == Activity.RESULT_OK && intent != null) {
1157 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001158 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 mTabControl.removeParentChildRelationShips();
1160 }
1161 }
1162 break;
1163 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001164 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001165 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001167 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001168 case AUTOFILL_SETUP:
1169 // Determine whether a profile was actually set up or not
1170 // and if so, send the message back to the WebTextView to
1171 // fill the form with the new profile.
1172 if (getSettings().getAutoFillProfile() != null) {
1173 mAutoFillSetupMessage.sendToTarget();
1174 mAutoFillSetupMessage = null;
1175 }
1176 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001177 case COMBO_VIEW:
1178 if (intent == null || resultCode != Activity.RESULT_OK) {
1179 break;
1180 }
John Reck3ba45532011-08-11 16:26:53 -07001181 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001182 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1183 Tab t = getCurrentTab();
1184 Uri uri = intent.getData();
1185 loadUrl(t, uri.toString());
1186 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1187 String[] urls = intent.getStringArrayExtra(
1188 ComboViewActivity.EXTRA_OPEN_ALL);
1189 Tab parent = getCurrentTab();
1190 for (String url : urls) {
1191 parent = openTab(url, parent,
1192 !mSettings.openInBackground(), true);
1193 }
1194 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1195 long id = intent.getLongExtra(
1196 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1197 if (id >= 0) {
1198 createNewSnapshotTab(id, true);
1199 }
1200 }
1201 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001202 case VOICE_RESULT:
1203 if (resultCode == Activity.RESULT_OK && intent != null) {
1204 ArrayList<String> results = intent.getStringArrayListExtra(
1205 RecognizerIntent.EXTRA_RESULTS);
1206 if (results.size() >= 1) {
1207 mVoiceResult = results.get(0);
1208 }
1209 }
1210 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 default:
1212 break;
1213 }
1214 getCurrentTopWebView().requestFocus();
1215 }
1216
1217 /**
1218 * Open the Go page.
1219 * @param startWithHistory If true, open starting on the history tab.
1220 * Otherwise, start with the bookmarks tab.
1221 */
1222 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001223 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001224 if (mTabControl.getCurrentWebView() == null) {
1225 return;
1226 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001227 // clear action mode
1228 if (isInCustomActionMode()) {
1229 endActionMode();
1230 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 Bundle extras = new Bundle();
1232 // Disable opening in a new window if we have maxed out the windows
1233 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1234 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001235 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001236 }
1237
1238 // combo view callbacks
1239
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 // key handling
1241 protected void onBackKey() {
1242 if (!mUi.onBackKey()) {
1243 WebView subwindow = mTabControl.getCurrentSubWindow();
1244 if (subwindow != null) {
1245 if (subwindow.canGoBack()) {
1246 subwindow.goBack();
1247 } else {
1248 dismissSubWindow(mTabControl.getCurrentTab());
1249 }
1250 } else {
1251 goBackOnePageOrQuit();
1252 }
1253 }
1254 }
1255
Michael Kolb4bd767d2011-05-27 11:33:55 -07001256 protected boolean onMenuKey() {
1257 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001258 }
1259
Michael Kolb8233fac2010-10-26 16:08:53 -07001260 // menu handling and state
1261 // TODO: maybe put into separate handler
1262
John Reck9c35b9c2012-05-30 10:08:50 -07001263 @Override
1264 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001265 if (mMenuState == EMPTY_MENU) {
1266 return false;
1267 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 MenuInflater inflater = mActivity.getMenuInflater();
1269 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001270 return true;
1271 }
1272
John Reck9c35b9c2012-05-30 10:08:50 -07001273 @Override
1274 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001275 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001276 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001277 return;
1278 }
1279 if (!(v instanceof WebView)) {
1280 return;
1281 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001282 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 WebView.HitTestResult result = webview.getHitTestResult();
1284 if (result == null) {
1285 return;
1286 }
1287
1288 int type = result.getType();
1289 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1290 Log.w(LOGTAG,
1291 "We should not show context menu when nothing is touched");
1292 return;
1293 }
1294 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1295 // let TextView handles context menu
1296 return;
1297 }
1298
1299 // Note, http://b/issue?id=1106666 is requesting that
1300 // an inflated menu can be used again. This is not available
1301 // yet, so inflate each time (yuk!)
1302 MenuInflater inflater = mActivity.getMenuInflater();
1303 inflater.inflate(R.menu.browsercontext, menu);
1304
1305 // Show the correct menu group
1306 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001307 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001308 menu.setGroupVisible(R.id.PHONE_MENU,
1309 type == WebView.HitTestResult.PHONE_TYPE);
1310 menu.setGroupVisible(R.id.EMAIL_MENU,
1311 type == WebView.HitTestResult.EMAIL_TYPE);
1312 menu.setGroupVisible(R.id.GEO_MENU,
1313 type == WebView.HitTestResult.GEO_TYPE);
1314 menu.setGroupVisible(R.id.IMAGE_MENU,
1315 type == WebView.HitTestResult.IMAGE_TYPE
1316 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1317 menu.setGroupVisible(R.id.ANCHOR_MENU,
1318 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1319 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001320 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1321 || type == WebView.HitTestResult.PHONE_TYPE
1322 || type == WebView.HitTestResult.EMAIL_TYPE
1323 || type == WebView.HitTestResult.GEO_TYPE;
1324 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1325 if (hitText) {
1326 menu.findItem(R.id.select_text_menu_id)
1327 .setOnMenuItemClickListener(new SelectText(webview));
1328 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001329 // Setup custom handling depending on the type
1330 switch (type) {
1331 case WebView.HitTestResult.PHONE_TYPE:
1332 menu.setHeaderTitle(Uri.decode(extra));
1333 menu.findItem(R.id.dial_context_menu_id).setIntent(
1334 new Intent(Intent.ACTION_VIEW, Uri
1335 .parse(WebView.SCHEME_TEL + extra)));
1336 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1337 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1338 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1339 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1340 addIntent);
1341 menu.findItem(R.id.copy_phone_context_menu_id)
1342 .setOnMenuItemClickListener(
1343 new Copy(extra));
1344 break;
1345
1346 case WebView.HitTestResult.EMAIL_TYPE:
1347 menu.setHeaderTitle(extra);
1348 menu.findItem(R.id.email_context_menu_id).setIntent(
1349 new Intent(Intent.ACTION_VIEW, Uri
1350 .parse(WebView.SCHEME_MAILTO + extra)));
1351 menu.findItem(R.id.copy_mail_context_menu_id)
1352 .setOnMenuItemClickListener(
1353 new Copy(extra));
1354 break;
1355
1356 case WebView.HitTestResult.GEO_TYPE:
1357 menu.setHeaderTitle(extra);
1358 menu.findItem(R.id.map_context_menu_id).setIntent(
1359 new Intent(Intent.ACTION_VIEW, Uri
1360 .parse(WebView.SCHEME_GEO
1361 + URLEncoder.encode(extra))));
1362 menu.findItem(R.id.copy_geo_context_menu_id)
1363 .setOnMenuItemClickListener(
1364 new Copy(extra));
1365 break;
1366
1367 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1368 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001369 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001370 // decide whether to show the open link in new tab option
1371 boolean showNewTab = mTabControl.canCreateNewTab();
1372 MenuItem newTabItem
1373 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001374 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001375 ? R.string.contextmenu_openlink_newwindow_background
1376 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001377 newTabItem.setVisible(showNewTab);
1378 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001379 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1380 newTabItem.setOnMenuItemClickListener(
1381 new MenuItem.OnMenuItemClickListener() {
1382 @Override
1383 public boolean onMenuItemClick(MenuItem item) {
1384 final HashMap<String, WebView> hrefMap =
1385 new HashMap<String, WebView>();
1386 hrefMap.put("webview", webview);
1387 final Message msg = mHandler.obtainMessage(
1388 FOCUS_NODE_HREF,
1389 R.id.open_newtab_context_menu_id,
1390 0, hrefMap);
1391 webview.requestFocusNodeHref(msg);
1392 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001393 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001394 });
1395 } else {
1396 newTabItem.setOnMenuItemClickListener(
1397 new MenuItem.OnMenuItemClickListener() {
1398 @Override
1399 public boolean onMenuItemClick(MenuItem item) {
1400 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001401 openTab(extra, parent,
1402 !mSettings.openInBackground(),
1403 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001404 return true;
1405 }
1406 });
1407 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001408 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1410 break;
1411 }
1412 // otherwise fall through to handle image part
1413 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001414 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1415 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1417 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001418 shareItem.setOnMenuItemClickListener(
1419 new MenuItem.OnMenuItemClickListener() {
1420 @Override
1421 public boolean onMenuItemClick(MenuItem item) {
1422 sharePage(mActivity, null, extra, null,
1423 null);
1424 return true;
1425 }
1426 }
1427 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001428 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001429 menu.findItem(R.id.view_image_context_menu_id)
1430 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1431 @Override
1432 public boolean onMenuItemClick(MenuItem item) {
1433 openTab(extra, mTabControl.getCurrentTab(), true, true);
1434 return false;
1435 }
1436 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001437 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1438 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1439 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001440 menu.findItem(R.id.set_wallpaper_context_menu_id).
1441 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1442 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 break;
1444
1445 default:
1446 Log.w(LOGTAG, "We should not get here.");
1447 break;
1448 }
1449 //update the ui
1450 mUi.onContextMenuCreated(menu);
1451 }
1452
1453 /**
1454 * As the menu can be open when loading state changes
1455 * we must manually update the state of the stop/reload menu
1456 * item
1457 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001458 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001459 if (menu == null) {
1460 return;
1461 }
1462 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001463 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001464 menu.findItem(R.id.stop_menu_id):
1465 menu.findItem(R.id.reload_menu_id);
1466 if (src != null) {
1467 dest.setIcon(src.getIcon());
1468 dest.setTitle(src.getTitle());
1469 }
1470 }
1471
John Reck9c35b9c2012-05-30 10:08:50 -07001472 @Override
1473 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001474 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001475 // hold on to the menu reference here; it is used by the page callbacks
1476 // to update the menu based on loading state
1477 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 // Note: setVisible will decide whether an item is visible; while
1479 // setEnabled() will decide whether an item is enabled, which also means
1480 // whether the matching shortcut key will function.
1481 switch (mMenuState) {
1482 case EMPTY_MENU:
1483 if (mCurrentMenuState != mMenuState) {
1484 menu.setGroupVisible(R.id.MAIN_MENU, false);
1485 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1486 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1487 }
1488 break;
1489 default:
1490 if (mCurrentMenuState != mMenuState) {
1491 menu.setGroupVisible(R.id.MAIN_MENU, true);
1492 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1493 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1494 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001495 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001496 break;
1497 }
1498 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001499 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001500 }
1501
Michael Kolb4bf79712011-07-14 14:18:12 -07001502 @Override
1503 public void updateMenuState(Tab tab, Menu menu) {
1504 boolean canGoBack = false;
1505 boolean canGoForward = false;
1506 boolean isHome = false;
John Reck42229bc2011-08-19 13:26:43 -07001507 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001508 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001509 if (tab != null) {
1510 canGoBack = tab.canGoBack();
1511 canGoForward = tab.canGoForward();
1512 isHome = mSettings.getHomePage().equals(tab.getUrl());
John Reck42229bc2011-08-19 13:26:43 -07001513 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001514 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001515 }
1516 final MenuItem back = menu.findItem(R.id.back_menu_id);
1517 back.setEnabled(canGoBack);
1518
1519 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1520 home.setEnabled(!isHome);
1521
1522 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1523 forward.setEnabled(canGoForward);
1524
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001525 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1526 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001527 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001528 if (source != null && dest != null) {
1529 dest.setTitle(source.getTitle());
1530 dest.setIcon(source.getIcon());
1531 }
John Recke1a03a32011-09-14 17:04:16 -07001532 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001533
1534 // decide whether to show the share link option
1535 PackageManager pm = mActivity.getPackageManager();
1536 Intent send = new Intent(Intent.ACTION_SEND);
1537 send.setType("text/plain");
1538 ResolveInfo ri = pm.resolveActivity(send,
1539 PackageManager.MATCH_DEFAULT_ONLY);
1540 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1541
1542 boolean isNavDump = mSettings.enableNavDump();
1543 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1544 nav.setVisible(isNavDump);
1545 nav.setEnabled(isNavDump);
1546
1547 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001548 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1549 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001550 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1551 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Michael Kolb315d5022011-10-13 12:47:11 -07001552 menu.setGroupVisible(R.id.COMBO_MENU, false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001553
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001554 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001555 }
1556
John Reck9c35b9c2012-05-30 10:08:50 -07001557 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001558 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 if (null == getCurrentTopWebView()) {
1560 return false;
1561 }
1562 if (mMenuIsDown) {
1563 // The shortcut action consumes the MENU. Even if it is still down,
1564 // it won't trigger the next shortcut action. In the case of the
1565 // shortcut action triggering a new activity, like Bookmarks, we
1566 // won't get onKeyUp for MENU. So it is important to reset it here.
1567 mMenuIsDown = false;
1568 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001569 if (mUi.onOptionsItemSelected(item)) {
1570 // ui callback handled it
1571 return true;
1572 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001573 switch (item.getItemId()) {
1574 // -- Main menu
1575 case R.id.new_tab_menu_id:
1576 openTabToHomePage();
1577 break;
1578
1579 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001580 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 break;
1582
Afzal Najamd4e33312012-04-26 01:54:01 -04001583 case R.id.close_other_tabs_id:
1584 closeOtherTabs();
1585 break;
1586
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 case R.id.goto_menu_id:
1588 editUrl();
1589 break;
1590
1591 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001592 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1593 break;
1594
1595 case R.id.history_menu_id:
1596 bookmarksOrHistoryPicker(ComboViews.History);
1597 break;
1598
1599 case R.id.snapshots_menu_id:
1600 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 break;
1602
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001604 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001605 break;
1606
1607 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001608 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 stopLoading();
1610 } else {
1611 getCurrentTopWebView().reload();
1612 }
1613 break;
1614
1615 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001616 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 break;
1618
1619 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001620 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 break;
1622
1623 case R.id.close_menu_id:
1624 // Close the subwindow if it exists.
1625 if (mTabControl.getCurrentSubWindow() != null) {
1626 dismissSubWindow(mTabControl.getCurrentTab());
1627 break;
1628 }
1629 closeCurrentTab();
1630 break;
1631
1632 case R.id.homepage_menu_id:
1633 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001634 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001635 break;
1636
1637 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001638 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001639 break;
1640
1641 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001642 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001643 break;
1644
John Reck2bc80422011-06-30 15:11:49 -07001645 case R.id.save_snapshot_menu_id:
1646 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001647 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001648 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001649 break;
1650
Michael Kolb8233fac2010-10-26 16:08:53 -07001651 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001652 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001653 break;
1654
John Recke1a03a32011-09-14 17:04:16 -07001655 case R.id.snapshot_go_live:
1656 goLive();
1657 return true;
1658
Michael Kolb8233fac2010-10-26 16:08:53 -07001659 case R.id.share_page_menu_id:
1660 Tab currentTab = mTabControl.getCurrentTab();
1661 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001662 return false;
1663 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001664 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001665 break;
1666
1667 case R.id.dump_nav_menu_id:
1668 getCurrentTopWebView().debugDump();
1669 break;
1670
Michael Kolb8233fac2010-10-26 16:08:53 -07001671 case R.id.zoom_in_menu_id:
1672 getCurrentTopWebView().zoomIn();
1673 break;
1674
1675 case R.id.zoom_out_menu_id:
1676 getCurrentTopWebView().zoomOut();
1677 break;
1678
1679 case R.id.view_downloads_menu_id:
1680 viewDownloads();
1681 break;
1682
John Reck42229bc2011-08-19 13:26:43 -07001683 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001684 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001685 break;
1686
Michael Kolb8233fac2010-10-26 16:08:53 -07001687 case R.id.window_one_menu_id:
1688 case R.id.window_two_menu_id:
1689 case R.id.window_three_menu_id:
1690 case R.id.window_four_menu_id:
1691 case R.id.window_five_menu_id:
1692 case R.id.window_six_menu_id:
1693 case R.id.window_seven_menu_id:
1694 case R.id.window_eight_menu_id:
1695 {
1696 int menuid = item.getItemId();
1697 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1698 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1699 Tab desiredTab = mTabControl.getTab(id);
1700 if (desiredTab != null &&
1701 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001702 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001703 }
1704 break;
1705 }
1706 }
1707 }
1708 break;
1709
1710 default:
1711 return false;
1712 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001713 return true;
1714 }
1715
John Reck68234a92012-04-19 15:27:12 -07001716 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1717 implements OnCancelListener {
1718
1719 private Tab mTab;
1720 private Dialog mProgressDialog;
1721 private ContentValues mValues;
1722
1723 private SaveSnapshotTask(Tab tab) {
1724 mTab = tab;
1725 }
1726
1727 @Override
1728 protected void onPreExecute() {
1729 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1730 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1731 true, true, this);
1732 mValues = mTab.createSnapshotValues();
1733 }
1734
1735 @Override
1736 protected Long doInBackground(Void... params) {
1737 if (!mTab.saveViewState(mValues)) {
1738 return null;
1739 }
1740 if (isCancelled()) {
1741 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1742 File file = mActivity.getFileStreamPath(path);
1743 if (!file.delete()) {
1744 file.deleteOnExit();
1745 }
1746 return null;
1747 }
1748 final ContentResolver cr = mActivity.getContentResolver();
1749 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1750 if (result == null) {
1751 return null;
1752 }
1753 long id = ContentUris.parseId(result);
1754 return id;
1755 }
1756
1757 @Override
1758 protected void onPostExecute(Long id) {
1759 if (isCancelled()) {
1760 return;
1761 }
1762 mProgressDialog.dismiss();
1763 if (id == null) {
1764 Toast.makeText(mActivity, R.string.snapshot_failed,
1765 Toast.LENGTH_SHORT).show();
1766 return;
1767 }
1768 Bundle b = new Bundle();
1769 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1770 mUi.showComboView(ComboViews.Snapshots, b);
1771 }
1772
1773 @Override
1774 public void onCancel(DialogInterface dialog) {
1775 cancel(true);
1776 }
1777 }
1778
Michael Kolb80f75082012-04-10 10:50:06 -07001779 @Override
1780 public void toggleUserAgent() {
1781 WebView web = getCurrentWebView();
1782 mSettings.toggleDesktopUseragent(web);
1783 web.loadUrl(web.getOriginalUrl());
1784 }
1785
1786 @Override
1787 public void findOnPage() {
1788 getCurrentTopWebView().showFindDialog(null, true);
1789 }
1790
1791 @Override
1792 public void openPreferences() {
1793 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1794 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1795 getCurrentTopWebView().getUrl());
1796 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1797 }
1798
1799 @Override
1800 public void bookmarkCurrentPage() {
1801 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1802 if (bookmarkIntent != null) {
1803 mActivity.startActivity(bookmarkIntent);
1804 }
1805 }
1806
John Recke1a03a32011-09-14 17:04:16 -07001807 private void goLive() {
1808 Tab t = getCurrentTab();
1809 t.loadUrl(t.getUrl(), null);
1810 }
1811
Michael Kolb315d5022011-10-13 12:47:11 -07001812 @Override
1813 public void showPageInfo() {
1814 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1815 }
1816
John Reck9c35b9c2012-05-30 10:08:50 -07001817 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001818 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001819 // Let the History and Bookmark fragments handle menus they created.
1820 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1821 return false;
1822 }
1823
Michael Kolb8233fac2010-10-26 16:08:53 -07001824 int id = item.getItemId();
1825 boolean result = true;
1826 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001827 // -- Browser context menu
1828 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001829 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001830 case R.id.copy_link_context_menu_id:
1831 final WebView webView = getCurrentTopWebView();
1832 if (null == webView) {
1833 result = false;
1834 break;
1835 }
1836 final HashMap<String, WebView> hrefMap =
1837 new HashMap<String, WebView>();
1838 hrefMap.put("webview", webView);
1839 final Message msg = mHandler.obtainMessage(
1840 FOCUS_NODE_HREF, id, 0, hrefMap);
1841 webView.requestFocusNodeHref(msg);
1842 break;
1843
1844 default:
1845 // For other context menus
1846 result = onOptionsItemSelected(item);
1847 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001848 return result;
1849 }
1850
1851 /**
1852 * support programmatically opening the context menu
1853 */
1854 public void openContextMenu(View view) {
1855 mActivity.openContextMenu(view);
1856 }
1857
1858 /**
1859 * programmatically open the options menu
1860 */
1861 public void openOptionsMenu() {
1862 mActivity.openOptionsMenu();
1863 }
1864
John Reck9c35b9c2012-05-30 10:08:50 -07001865 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001866 public boolean onMenuOpened(int featureId, Menu menu) {
1867 if (mOptionsMenuOpen) {
1868 if (mConfigChanged) {
1869 // We do not need to make any changes to the state of the
1870 // title bar, since the only thing that happened was a
1871 // change in orientation
1872 mConfigChanged = false;
1873 } else {
1874 if (!mExtendedMenuOpen) {
1875 mExtendedMenuOpen = true;
1876 mUi.onExtendedMenuOpened();
1877 } else {
1878 // Switching the menu back to icon view, so show the
1879 // title bar once again.
1880 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001881 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001882 }
1883 }
1884 } else {
1885 // The options menu is closed, so open it, and show the title
1886 mOptionsMenuOpen = true;
1887 mConfigChanged = false;
1888 mExtendedMenuOpen = false;
1889 mUi.onOptionsMenuOpened();
1890 }
1891 return true;
1892 }
1893
John Reck9c35b9c2012-05-30 10:08:50 -07001894 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001895 public void onOptionsMenuClosed(Menu menu) {
1896 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001897 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001898 }
1899
John Reck9c35b9c2012-05-30 10:08:50 -07001900 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001901 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001902 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001903 }
1904
1905 // Helper method for getting the top window.
1906 @Override
1907 public WebView getCurrentTopWebView() {
1908 return mTabControl.getCurrentTopWebView();
1909 }
1910
1911 @Override
1912 public WebView getCurrentWebView() {
1913 return mTabControl.getCurrentWebView();
1914 }
1915
1916 /*
1917 * This method is called as a result of the user selecting the options
1918 * menu to see the download window. It shows the download window on top of
1919 * the current window.
1920 */
1921 void viewDownloads() {
1922 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1923 mActivity.startActivity(intent);
1924 }
1925
John Reck30b065e2011-07-19 10:58:05 -07001926 int getActionModeHeight() {
1927 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1928 new int[] { android.R.attr.actionBarSize });
1929 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1930 actionBarSizeTypedArray.recycle();
1931 return size;
1932 }
1933
Michael Kolb8233fac2010-10-26 16:08:53 -07001934 // action mode
1935
John Reck9c35b9c2012-05-30 10:08:50 -07001936 @Override
1937 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 mUi.onActionModeStarted(mode);
1939 mActionMode = mode;
1940 }
1941
1942 /*
1943 * True if a custom ActionMode (i.e. find or select) is in use.
1944 */
1945 @Override
1946 public boolean isInCustomActionMode() {
1947 return mActionMode != null;
1948 }
1949
1950 /*
1951 * End the current ActionMode.
1952 */
1953 @Override
1954 public void endActionMode() {
1955 if (mActionMode != null) {
1956 mActionMode.finish();
1957 }
1958 }
1959
1960 /*
1961 * Called by find and select when they are finished. Replace title bars
1962 * as necessary.
1963 */
John Reck9c35b9c2012-05-30 10:08:50 -07001964 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001965 public void onActionModeFinished(ActionMode mode) {
1966 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001967 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001968 mActionMode = null;
1969 }
1970
1971 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001972 final Tab tab = getCurrentTab();
1973 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07001974 }
1975
1976 // bookmark handling
1977
1978 /**
1979 * add the current page as a bookmark to the given folder id
1980 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001981 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001982 * bookmarked, and if it is, edit that bookmark. If false, and
1983 * the site is already bookmarked, do not attempt to edit the
1984 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001985 */
1986 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001987 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07001988 WebView w = getCurrentTopWebView();
1989 if (w == null) {
1990 return null;
1991 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 Intent i = new Intent(mActivity,
1993 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1995 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1996 String touchIconUrl = w.getTouchIconUrl();
1997 if (touchIconUrl != null) {
1998 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1999 WebSettings settings = w.getSettings();
2000 if (settings != null) {
2001 i.putExtra(AddBookmarkPage.USER_AGENT,
2002 settings.getUserAgentString());
2003 }
2004 }
2005 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2006 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2007 getDesiredThumbnailHeight(mActivity)));
2008 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002009 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002010 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2011 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002012 // Put the dialog at the upper right of the screen, covering the
2013 // star on the title bar.
2014 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002015 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002016 }
2017
2018 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002019 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002020 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002021 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002022 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002023 }
2024
2025 // thumbnails
2026
2027 /**
2028 * Return the desired width for thumbnail screenshots, which are stored in
2029 * the database, and used on the bookmarks screen.
2030 * @param context Context for finding out the density of the screen.
2031 * @return desired width for thumbnail screenshot.
2032 */
2033 static int getDesiredThumbnailWidth(Context context) {
2034 return context.getResources().getDimensionPixelOffset(
2035 R.dimen.bookmarkThumbnailWidth);
2036 }
2037
2038 /**
2039 * Return the desired height for thumbnail screenshots, which are stored in
2040 * the database, and used on the bookmarks screen.
2041 * @param context Context for finding out the density of the screen.
2042 * @return desired height for thumbnail screenshot.
2043 */
2044 static int getDesiredThumbnailHeight(Context context) {
2045 return context.getResources().getDimensionPixelOffset(
2046 R.dimen.bookmarkThumbnailHeight);
2047 }
2048
John Reck8cc92352011-07-06 17:41:52 -07002049 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002050 if (view == null || view.getContentHeight() == 0
2051 || view.getContentWidth() == 0) {
2052 return null;
2053 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002054 // We render to a bitmap 2x the desired size so that we can then
2055 // re-scale it with filtering since canvas.scale doesn't filter
2056 // This helps reduce aliasing at the cost of being slightly blurry
2057 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002058 int scaledWidth = width * filter_scale;
2059 int scaledHeight = height * filter_scale;
2060 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2061 || sThumbnailBitmap.getHeight() != scaledHeight) {
2062 if (sThumbnailBitmap != null) {
2063 sThumbnailBitmap.recycle();
2064 sThumbnailBitmap = null;
2065 }
2066 sThumbnailBitmap =
2067 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002068 }
John Reckd7dd9b22011-08-30 09:18:29 -07002069 Canvas canvas = new Canvas(sThumbnailBitmap);
2070 int contentWidth = view.getContentWidth();
2071 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2072 if (view instanceof BrowserWebView) {
2073 int dy = -((BrowserWebView)view).getTitleHeight();
2074 canvas.translate(0, dy * overviewScale);
2075 }
2076
2077 canvas.scale(overviewScale, overviewScale);
2078
2079 if (view instanceof BrowserWebView) {
2080 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002082 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002083 }
John Reckd7dd9b22011-08-30 09:18:29 -07002084 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2085 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002086 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002087 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002088 }
2089
John Reck34ef2672011-02-10 11:30:55 -08002090 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002092 // FIXME: Would like to make sure there is actually something to
2093 // draw, but the API for that (WebViewCore.pictureReady()) is not
2094 // currently accessible here.
2095
John Reck34ef2672011-02-10 11:30:55 -08002096 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002097 if (view == null) {
2098 // Tab was destroyed
2099 return;
2100 }
John Reck34ef2672011-02-10 11:30:55 -08002101 final String url = tab.getUrl();
2102 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002103 if (TextUtils.isEmpty(url)) {
2104 return;
2105 }
2106
2107 // Only update thumbnails for web urls (http(s)://), not for
2108 // about:, javascript:, data:, etc...
2109 // Unless it is a bookmarked site, then always update
2110 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2111 return;
2112 }
2113
Michael Kolb8233fac2010-10-26 16:08:53 -07002114 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2115 getDesiredThumbnailHeight(mActivity));
2116 if (bm == null) {
2117 return;
2118 }
2119
2120 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002121 new AsyncTask<Void, Void, Void>() {
2122 @Override
2123 protected Void doInBackground(Void... unused) {
2124 Cursor cursor = null;
2125 try {
2126 // TODO: Clean this up
2127 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2128 if (cursor != null && cursor.moveToFirst()) {
2129 final ByteArrayOutputStream os =
2130 new ByteArrayOutputStream();
2131 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002132
John Reck34ef2672011-02-10 11:30:55 -08002133 ContentValues values = new ContentValues();
2134 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002135
John Reck34ef2672011-02-10 11:30:55 -08002136 do {
John Reck617fd832011-02-16 14:35:59 -08002137 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002138 cr.update(Images.CONTENT_URI, values, null, null);
2139 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002140 }
John Reck34ef2672011-02-10 11:30:55 -08002141 } catch (IllegalStateException e) {
2142 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002143 } catch (SQLiteException s) {
2144 // Added for possible error when user tries to remove the same bookmark
2145 // that is being updated with a screen shot
2146 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002147 } finally {
2148 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 }
John Reck34ef2672011-02-10 11:30:55 -08002150 return null;
2151 }
2152 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002153 }
2154
2155 private class Copy implements OnMenuItemClickListener {
2156 private CharSequence mText;
2157
John Reck9c35b9c2012-05-30 10:08:50 -07002158 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002159 public boolean onMenuItemClick(MenuItem item) {
2160 copy(mText);
2161 return true;
2162 }
2163
2164 public Copy(CharSequence toCopy) {
2165 mText = toCopy;
2166 }
2167 }
2168
Leon Scroggins63c02662010-11-18 15:16:27 -05002169 private static class Download implements OnMenuItemClickListener {
2170 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002171 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002172 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002173 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002174 private static final String FALLBACK_EXTENSION = "dat";
2175 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002176
John Reck9c35b9c2012-05-30 10:08:50 -07002177 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002178 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002179 if (DataUri.isDataUri(mText)) {
2180 saveDataUri();
2181 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002182 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
George Mount387d45d2011-10-07 15:57:53 -07002183 null, null, mPrivateBrowsing);
2184 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002185 return true;
2186 }
2187
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002188 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2189 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002190 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002191 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002192 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002193 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002194 }
George Mount387d45d2011-10-07 15:57:53 -07002195
2196 /**
2197 * Treats mText as a data URI and writes its contents to a file
2198 * based on the current time.
2199 */
2200 private void saveDataUri() {
2201 FileOutputStream outputStream = null;
2202 try {
2203 DataUri uri = new DataUri(mText);
2204 File target = getTarget(uri);
2205 outputStream = new FileOutputStream(target);
2206 outputStream.write(uri.getData());
2207 final DownloadManager manager =
2208 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2209 manager.addCompletedDownload(target.getName(),
2210 mActivity.getTitle().toString(), false,
2211 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002212 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002213 } catch (IOException e) {
2214 Log.e(LOGTAG, "Could not save data URL");
2215 } finally {
2216 if (outputStream != null) {
2217 try {
2218 outputStream.close();
2219 } catch (IOException e) {
2220 // ignore close errors
2221 }
2222 }
2223 }
2224 }
2225
2226 /**
2227 * Creates a File based on the current time stamp and uses
2228 * the mime type of the DataUri to get the extension.
2229 */
2230 private File getTarget(DataUri uri) throws IOException {
2231 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002232 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002233 String nameBase = format.format(new Date());
2234 String mimeType = uri.getMimeType();
2235 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2236 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2237 if (extension == null) {
2238 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2239 extension = FALLBACK_EXTENSION;
2240 }
2241 extension = "." + extension; // createTempFile needs the '.'
2242 File targetFile = File.createTempFile(nameBase, extension, dir);
2243 return targetFile;
2244 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002245 }
2246
Cary Clark8974d282010-11-22 10:46:05 -05002247 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002248 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002249
John Reck9c35b9c2012-05-30 10:08:50 -07002250 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002251 public boolean onMenuItemClick(MenuItem item) {
2252 if (mWebView != null) {
2253 return mWebView.selectText();
2254 }
2255 return false;
2256 }
2257
2258 public SelectText(WebView webView) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002259 mWebView = WebViewClassic.fromWebView(webView);
Cary Clark8974d282010-11-22 10:46:05 -05002260 }
2261
2262 }
2263
Michael Kolb8233fac2010-10-26 16:08:53 -07002264 /********************** TODO: UI stuff *****************************/
2265
2266 // these methods have been copied, they still need to be cleaned up
2267
2268 /****************** tabs ***************************************************/
2269
2270 // basic tab interactions:
2271
2272 // it is assumed that tabcontrol already knows about the tab
2273 protected void addTab(Tab tab) {
2274 mUi.addTab(tab);
2275 }
2276
2277 protected void removeTab(Tab tab) {
2278 mUi.removeTab(tab);
2279 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002280 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002281 }
2282
Michael Kolbf2055602011-04-09 17:20:03 -07002283 @Override
2284 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002285 // monkey protection against delayed start
2286 if (tab != null) {
2287 mTabControl.setCurrentTab(tab);
2288 // the tab is guaranteed to have a webview after setCurrentTab
2289 mUi.setActiveTab(tab);
2290 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002291 }
2292
John Reck8bcafc12011-08-29 16:43:02 -07002293 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002294 Tab current = mTabControl.getCurrentTab();
2295 if (current != null
2296 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002297 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002298 }
2299 }
2300
John Reck26b18322011-06-21 13:08:58 -07002301 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 // Dismiss the subwindow if applicable.
2303 dismissSubWindow(appTab);
2304 // Since we might kill the WebView, remove it from the
2305 // content view first.
2306 mUi.detachTab(appTab);
2307 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002308 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002309 // TODO: analyze why the remove and add are necessary
2310 mUi.attachTab(appTab);
2311 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002312 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002313 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002314 } else {
2315 // If the tab was the current tab, we have to attach
2316 // it to the view system again.
2317 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002318 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002319 }
2320 }
2321
2322 // Remove the sub window if it exists. Also called by TabControl when the
2323 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002324 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002325 public void dismissSubWindow(Tab tab) {
2326 removeSubWindow(tab);
2327 // dismiss the subwindow. This will destroy the WebView.
2328 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002329 WebView wv = getCurrentTopWebView();
2330 if (wv != null) {
2331 wv.requestFocus();
2332 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002333 }
2334
2335 @Override
2336 public void removeSubWindow(Tab t) {
2337 if (t.getSubWebView() != null) {
2338 mUi.removeSubWindow(t.getSubViewContainer());
2339 }
2340 }
2341
2342 @Override
2343 public void attachSubWindow(Tab tab) {
2344 if (tab.getSubWebView() != null) {
2345 mUi.attachSubWindow(tab.getSubViewContainer());
2346 getCurrentTopWebView().requestFocus();
2347 }
2348 }
2349
Mathew Inwood29721c22011-06-29 17:55:24 +01002350 private Tab showPreloadedTab(final UrlData urlData) {
2351 if (!urlData.isPreloaded()) {
2352 return null;
2353 }
2354 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2355 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2356 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002357 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002358 // Could not submit query. Fallback to regular tab creation
2359 tabControl.destroy();
2360 return null;
2361 }
2362 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002363 // check tab count and make room for new tab
2364 if (!mTabControl.canCreateNewTab()) {
2365 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2366 if (leastUsed != null) {
2367 closeTab(leastUsed);
2368 }
2369 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002370 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002371 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002372 mTabControl.addPreloadedTab(t);
2373 addTab(t);
2374 setActiveTab(t);
2375 return t;
2376 }
2377
Michael Kolb7bcafde2011-05-09 13:55:59 -07002378 // open a non inconito tab with the given url data
2379 // and set as active tab
2380 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002381 Tab tab = showPreloadedTab(urlData);
2382 if (tab == null) {
2383 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002384 if ((tab != null) && !urlData.isEmpty()) {
2385 loadUrlDataIn(tab, urlData);
2386 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002387 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002388 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002389 }
2390
Michael Kolb843510f2010-12-09 10:51:49 -08002391 @Override
2392 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002393 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002394 }
2395
Michael Kolb8233fac2010-10-26 16:08:53 -07002396 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002397 public Tab openIncognitoTab() {
2398 return openTab(INCOGNITO_URI, true, true, false);
2399 }
2400
2401 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002402 public Tab openTab(String url, boolean incognito, boolean setActive,
2403 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002404 return openTab(url, incognito, setActive, useCurrent, null);
2405 }
2406
2407 @Override
2408 public Tab openTab(String url, Tab parent, boolean setActive,
2409 boolean useCurrent) {
2410 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2411 setActive, useCurrent, parent);
2412 }
2413
2414 public Tab openTab(String url, boolean incognito, boolean setActive,
2415 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002416 Tab tab = createNewTab(incognito, setActive, useCurrent);
2417 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002418 if (parent != null && parent != tab) {
2419 parent.addChildTab(tab);
2420 }
Michael Kolb519d2282011-05-09 17:03:19 -07002421 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002422 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002423 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002424 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002425 return tab;
2426 }
2427
2428 // this method will attempt to create a new tab
2429 // incognito: private browsing tab
2430 // setActive: ste tab as current tab
2431 // useCurrent: if no new tab can be created, return current tab
2432 private Tab createNewTab(boolean incognito, boolean setActive,
2433 boolean useCurrent) {
2434 Tab tab = null;
2435 if (mTabControl.canCreateNewTab()) {
2436 tab = mTabControl.createNewTab(incognito);
2437 addTab(tab);
2438 if (setActive) {
2439 setActiveTab(tab);
2440 }
2441 } else {
2442 if (useCurrent) {
2443 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002444 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002445 } else {
2446 mUi.showMaxTabsWarning();
2447 }
2448 }
2449 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002450 }
2451
John Reck2bc80422011-06-30 15:11:49 -07002452 @Override
2453 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2454 SnapshotTab tab = null;
2455 if (mTabControl.canCreateNewTab()) {
2456 tab = mTabControl.createSnapshotTab(snapshotId);
2457 addTab(tab);
2458 if (setActive) {
2459 setActiveTab(tab);
2460 }
2461 } else {
2462 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002463 }
2464 return tab;
2465 }
2466
Michael Kolb8233fac2010-10-26 16:08:53 -07002467 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002468 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002469 * @return boolean True if we successfully switched to a different tab. If
2470 * the indexth tab is null, or if that tab is the same as
2471 * the current one, return false.
2472 */
2473 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002474 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002475 Tab currentTab = mTabControl.getCurrentTab();
2476 if (tab == null || tab == currentTab) {
2477 return false;
2478 }
2479 setActiveTab(tab);
2480 return true;
2481 }
2482
2483 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002484 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002485 closeCurrentTab(false);
2486 }
2487
2488 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002489 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002490 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002491 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002492 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002493 return;
2494 }
Michael Kolbc831b632011-05-11 09:30:34 -07002495 final Tab current = mTabControl.getCurrentTab();
2496 final int pos = mTabControl.getCurrentPosition();
2497 Tab newTab = current.getParent();
2498 if (newTab == null) {
2499 newTab = mTabControl.getTab(pos + 1);
2500 if (newTab == null) {
2501 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002502 }
2503 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002504 if (andQuit) {
2505 mTabControl.setCurrentTab(newTab);
2506 closeTab(current);
2507 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002508 // Close window
2509 closeTab(current);
2510 }
2511 }
2512
2513 /**
2514 * Close the tab, remove its associated title bar, and adjust mTabControl's
2515 * current tab to a valid value.
2516 */
2517 @Override
2518 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002519 if (tab == mTabControl.getCurrentTab()) {
2520 closeCurrentTab();
2521 } else {
2522 removeTab(tab);
2523 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002524 }
2525
Afzal Najamd4e33312012-04-26 01:54:01 -04002526 /**
2527 * Close all tabs except the current one
2528 */
2529 @Override
2530 public void closeOtherTabs() {
2531 int inactiveTabs = mTabControl.getTabCount() - 1;
2532 for (int i = inactiveTabs; i >= 0; i--) {
2533 Tab tab = mTabControl.getTab(i);
2534 if (tab != mTabControl.getCurrentTab()) {
2535 removeTab(tab);
2536 }
2537 }
2538 }
2539
Michael Kolb8233fac2010-10-26 16:08:53 -07002540 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002541 protected void loadUrlFromContext(String url) {
2542 Tab tab = getCurrentTab();
2543 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002544 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002545 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002546 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002547 if (!WebViewClassic.fromWebView(view).getWebViewClient().
2548 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002549 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002550 }
2551 }
2552 }
2553
2554 /**
2555 * Load the URL into the given WebView and update the title bar
2556 * to reflect the new load. Call this instead of WebView.loadUrl
2557 * directly.
2558 * @param view The WebView used to load url.
2559 * @param url The URL to load.
2560 */
John Reck71e51422011-07-01 16:49:28 -07002561 @Override
2562 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002563 loadUrl(tab, url, null);
2564 }
2565
2566 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2567 if (tab != null) {
2568 dismissSubWindow(tab);
2569 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002570 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002571 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002572 }
2573
2574 /**
2575 * Load UrlData into a Tab and update the title bar to reflect the new
2576 * load. Call this instead of UrlData.loadIn directly.
2577 * @param t The Tab used to load.
2578 * @param data The UrlData being loaded.
2579 */
2580 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002581 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002582 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002583 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002584 } else {
2585 loadUrl(t, data.mUrl, data.mHeaders);
2586 }
2587 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 }
2589
John Reck30c714c2010-12-16 17:30:34 -08002590 @Override
2591 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002592 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002593 if (tab.canGoBack()) {
2594 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002595 } else {
John Reckef654f12011-07-12 16:42:08 -07002596 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002597 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002598 }
2599
2600 void goBackOnePageOrQuit() {
2601 Tab current = mTabControl.getCurrentTab();
2602 if (current == null) {
2603 /*
2604 * Instead of finishing the activity, simply push this to the back
2605 * of the stack and let ActivityManager to choose the foreground
2606 * activity. As BrowserActivity is singleTask, it will be always the
2607 * root of the task. So we can use either true or false for
2608 * moveTaskToBack().
2609 */
2610 mActivity.moveTaskToBack(true);
2611 return;
2612 }
John Reckef654f12011-07-12 16:42:08 -07002613 if (current.canGoBack()) {
2614 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002615 } else {
2616 // Check to see if we are closing a window that was created by
2617 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002618 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002620 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002621 // Now we close the other tab
2622 closeTab(current);
2623 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002624 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002625 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002626 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002627 /*
2628 * Instead of finishing the activity, simply push this to the back
2629 * of the stack and let ActivityManager to choose the foreground
2630 * activity. As BrowserActivity is singleTask, it will be always the
2631 * root of the task. So we can use either true or false for
2632 * moveTaskToBack().
2633 */
2634 mActivity.moveTaskToBack(true);
2635 }
2636 }
2637 }
2638
2639 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002640 * helper method for key handler
2641 * returns the current tab if it can't advance
2642 */
Michael Kolbc831b632011-05-11 09:30:34 -07002643 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002644 int pos = mTabControl.getCurrentPosition() + 1;
2645 if (pos >= mTabControl.getTabCount()) {
2646 pos = 0;
2647 }
2648 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002649 }
2650
2651 /**
2652 * helper method for key handler
2653 * returns the current tab if it can't advance
2654 */
Michael Kolbc831b632011-05-11 09:30:34 -07002655 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002656 int pos = mTabControl.getCurrentPosition() - 1;
2657 if ( pos < 0) {
2658 pos = mTabControl.getTabCount() - 1;
2659 }
2660 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002661 }
2662
John Reckbcef87f2012-02-03 14:58:34 -08002663 boolean isMenuOrCtrlKey(int keyCode) {
2664 return (KeyEvent.KEYCODE_MENU == keyCode)
2665 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2666 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2667 }
2668
Michael Kolb0035fad2011-03-14 13:25:28 -07002669 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002670 * handle key events in browser
2671 *
2672 * @param keyCode
2673 * @param event
2674 * @return true if handled, false to pass to super
2675 */
John Reck9c35b9c2012-05-30 10:08:50 -07002676 @Override
2677 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002678 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002679 // Even if MENU is already held down, we need to call to super to open
2680 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002681 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002682 mMenuIsDown = true;
2683 return false;
2684 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002685
Cary Clark8ff8c662010-12-29 15:03:05 -05002686 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002687 Tab tab = getCurrentTab();
2688 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002689
Cary Clark160bbb92011-01-10 11:17:07 -05002690 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2691 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002692
Michael Kolb8233fac2010-10-26 16:08:53 -07002693 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002694 case KeyEvent.KEYCODE_TAB:
2695 if (event.isCtrlPressed()) {
2696 if (event.isShiftPressed()) {
2697 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002698 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002699 } else {
2700 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002701 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002702 }
2703 return true;
2704 }
2705 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002706 case KeyEvent.KEYCODE_SPACE:
2707 // WebView/WebTextView handle the keys in the KeyDown. As
2708 // the Activity's shortcut keys are only handled when WebView
2709 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002710 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002711 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002712 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002713 pageDown();
2714 }
2715 return true;
2716 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002717 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002718 event.startTracking();
2719 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002720 case KeyEvent.KEYCODE_FORWARD:
2721 if (!noModifiers) break;
2722 tab.goForward();
2723 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002724 case KeyEvent.KEYCODE_DPAD_LEFT:
2725 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002726 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002727 return true;
2728 }
2729 break;
2730 case KeyEvent.KEYCODE_DPAD_RIGHT:
2731 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002732 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002733 return true;
2734 }
2735 break;
2736 case KeyEvent.KEYCODE_A:
2737 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002738 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002739 return true;
2740 }
2741 break;
Michael Kolba4183062011-01-16 10:43:21 -08002742// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002743 case KeyEvent.KEYCODE_C:
2744 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002745 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002746 return true;
2747 }
2748 break;
Michael Kolba4183062011-01-16 10:43:21 -08002749// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002750// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002751// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002752// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002753// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002754// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002755// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002756// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002757// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002758// case KeyEvent.KEYCODE_M: // unused
2759// case KeyEvent.KEYCODE_N: // in Chrome: new window
2760// case KeyEvent.KEYCODE_O: // in Chrome: open file
2761// case KeyEvent.KEYCODE_P: // in Chrome: print page
2762// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002763// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002764// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2765 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002766 // we can't use the ctrl/shift flags, they check for
2767 // exclusive use of a modifier
2768 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002769 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002770 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002771 } else {
2772 openTabToHomePage();
2773 }
2774 return true;
2775 }
2776 break;
2777// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2778// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002779// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002780// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2781// case KeyEvent.KEYCODE_Y: // unused
2782// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002783 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002784 // it is a regular key and webview is not null
2785 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002786 }
2787
John Reck9c35b9c2012-05-30 10:08:50 -07002788 @Override
2789 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002790 switch(keyCode) {
2791 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002792 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002793 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002794 return true;
2795 }
2796 break;
2797 }
2798 return false;
2799 }
2800
John Reck9c35b9c2012-05-30 10:08:50 -07002801 @Override
2802 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002803 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002804 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002805 if (KeyEvent.KEYCODE_MENU == keyCode
2806 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002807 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002808 }
2809 }
Cary Clark160bbb92011-01-10 11:17:07 -05002810 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002811 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002812 case KeyEvent.KEYCODE_BACK:
2813 if (event.isTracking() && !event.isCanceled()) {
2814 onBackKey();
2815 return true;
2816 }
2817 break;
2818 }
2819 return false;
2820 }
2821
2822 public boolean isMenuDown() {
2823 return mMenuIsDown;
2824 }
2825
John Reck9c35b9c2012-05-30 10:08:50 -07002826 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002827 public void setupAutoFill(Message message) {
2828 // Open the settings activity at the AutoFill profile fragment so that
2829 // the user can create a new profile. When they return, we will dispatch
2830 // the message so that we can autofill the form using their new profile.
2831 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2832 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2833 AutoFillSettingsFragment.class.getName());
2834 mAutoFillSetupMessage = message;
2835 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2836 }
John Reckb3417f02011-01-14 11:01:05 -08002837
John Reck9c35b9c2012-05-30 10:08:50 -07002838 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002839 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002840 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002841 return true;
2842 }
2843
John Reck1cf4b792011-07-26 10:22:22 -07002844 @Override
2845 public boolean shouldCaptureThumbnails() {
2846 return mUi.shouldCaptureThumbnails();
2847 }
2848
Michael Kolbc3af0672011-08-09 10:24:41 -07002849 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002850 public boolean supportsVoice() {
2851 PackageManager pm = mActivity.getPackageManager();
2852 List activities = pm.queryIntentActivities(new Intent(
2853 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2854 return activities.size() != 0;
2855 }
2856
2857 @Override
2858 public void startVoiceRecognizer() {
2859 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2860 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2861 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2862 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2863 mActivity.startActivityForResult(voice, VOICE_RESULT);
2864 }
2865
2866 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002867 public void setBlockEvents(boolean block) {
2868 mBlockEvents = block;
2869 }
2870
John Reck9c35b9c2012-05-30 10:08:50 -07002871 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002872 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002873 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002874 }
2875
John Reck9c35b9c2012-05-30 10:08:50 -07002876 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002877 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002878 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002879 }
2880
John Reck9c35b9c2012-05-30 10:08:50 -07002881 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002882 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002883 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002884 }
2885
John Reck9c35b9c2012-05-30 10:08:50 -07002886 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002887 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002888 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002889 }
2890
John Reck9c35b9c2012-05-30 10:08:50 -07002891 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002892 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002893 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002894 }
2895
Michael Kolb8233fac2010-10-26 16:08:53 -07002896}