blob: 846d41fcb845abfad563fb955b1fddc3e5624faf [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
John Reck68234a92012-04-19 15:27:12 -070020import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.ProgressDialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070024import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070025import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ContentValues;
27import android.content.Context;
John Reck68234a92012-04-19 15:27:12 -070028import android.content.DialogInterface;
29import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070030import android.content.Intent;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070034import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050035import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.database.Cursor;
37import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020038import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070039import android.graphics.Bitmap;
40import android.graphics.Canvas;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.net.Uri;
42import android.net.http.SslError;
43import android.os.AsyncTask;
44import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070045import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.os.Handler;
47import android.os.Message;
48import android.os.PowerManager;
49import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000050import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.provider.Browser;
52import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.provider.BrowserContract.Images;
54import android.provider.ContactsContract;
55import android.provider.ContactsContract.Intents.Insert;
Michael Kolb0b129122012-06-04 16:31:58 -070056import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.text.TextUtils;
58import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080059import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.view.ActionMode;
61import android.view.ContextMenu;
62import android.view.ContextMenu.ContextMenuInfo;
63import android.view.Gravity;
64import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070065import android.view.Menu;
66import android.view.MenuInflater;
67import android.view.MenuItem;
68import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070069import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070070import android.view.View;
71import android.webkit.CookieManager;
72import android.webkit.CookieSyncManager;
73import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070074import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070075import android.webkit.SslErrorHandler;
76import android.webkit.ValueCallback;
77import android.webkit.WebChromeClient;
78import android.webkit.WebIconDatabase;
79import android.webkit.WebSettings;
80import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000081import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Michael Kolb4bd767d2011-05-27 11:33:55 -070084import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070085import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070086import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070087import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070088
Michael Kolb8233fac2010-10-26 16:08:53 -070089import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -070090import java.io.File;
91import java.io.FileOutputStream;
92import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070093import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -070094import java.text.DateFormat;
95import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -070096import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -070097import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -070098import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -070099import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800100import java.util.List;
John Reck26b18322011-06-21 13:08:58 -0700101import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700102
103/**
104 * Controller for browser
105 */
106public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700107 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700108
109 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800110 private static final String SEND_APP_ID_EXTRA =
111 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700112 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800113
Michael Kolb8233fac2010-10-26 16:08:53 -0700114
115 // public message ids
116 public final static int LOAD_URL = 1001;
117 public final static int STOP_LOAD = 1002;
118
119 // Message Ids
120 private static final int FOCUS_NODE_HREF = 102;
121 private static final int RELEASE_WAKELOCK = 107;
122
123 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
124
125 private static final int OPEN_BOOKMARKS = 201;
126
127 private static final int EMPTY_MENU = -1;
128
Michael Kolb8233fac2010-10-26 16:08:53 -0700129 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700130 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700131 final static int PREFERENCES_PAGE = 3;
132 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000133 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700134 final static int VOICE_RESULT = 6;
Ben Murdoch8029a772010-11-16 11:58:21 +0000135
Michael Kolb8233fac2010-10-26 16:08:53 -0700136 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
137
138 // As the ids are dynamically created, we can't guarantee that they will
139 // be in sequence, so this static array maps ids to a window number.
140 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
141 { R.id.window_one_menu_id, R.id.window_two_menu_id,
142 R.id.window_three_menu_id, R.id.window_four_menu_id,
143 R.id.window_five_menu_id, R.id.window_six_menu_id,
144 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
145
146 // "source" parameter for Google search through search key
147 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
148 // "source" parameter for Google search through simplily type
149 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
150
George Mount387d45d2011-10-07 15:57:53 -0700151 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700152 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
153
John Reckd7dd9b22011-08-30 09:18:29 -0700154 // A bitmap that is re-used in createScreenshot as scratch space
155 private static Bitmap sThumbnailBitmap;
156
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 private Activity mActivity;
158 private UI mUi;
159 private TabControl mTabControl;
160 private BrowserSettings mSettings;
161 private WebViewFactory mFactory;
162
163 private WakeLock mWakeLock;
164
165 private UrlHandler mUrlHandler;
166 private UploadHandler mUploadHandler;
167 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700168 private PageDialogsHandler mPageDialogsHandler;
169 private NetworkStateHandler mNetworkHandler;
170
Ben Murdoch8029a772010-11-16 11:58:21 +0000171 private Message mAutoFillSetupMessage;
172
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 private boolean mShouldShowErrorConsole;
174
175 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
176
177 // FIXME, temp address onPrepareMenu performance problem.
178 // When we move everything out of view, we should rewrite this.
179 private int mCurrentMenuState = 0;
180 private int mMenuState = R.id.MAIN_MENU;
181 private int mOldMenuState = EMPTY_MENU;
182 private Menu mCachedMenu;
183
Michael Kolb8233fac2010-10-26 16:08:53 -0700184 private boolean mMenuIsDown;
185
186 // For select and find, we keep track of the ActionMode so that
187 // finish() can be called as desired.
188 private ActionMode mActionMode;
189
190 /**
191 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
192 * of whether the configuration has changed. The first onMenuOpened call
193 * after a configuration change is simply a reopening of the same menu
194 * (i.e. mIconView did not change).
195 */
196 private boolean mConfigChanged;
197
198 /**
199 * Keeps track of whether the options menu is open. This is important in
200 * determining whether to show or hide the title bar overlay
201 */
202 private boolean mOptionsMenuOpen;
203
204 /**
205 * Whether or not the options menu is in its bigger, popup menu form. When
206 * true, we want the title bar overlay to be gone. When false, we do not.
207 * Only meaningful if mOptionsMenuOpen is true.
208 */
209 private boolean mExtendedMenuOpen;
210
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 private boolean mActivityPaused = true;
212 private boolean mLoadStopped;
213
214 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500215 // Checks to see when the bookmarks database has changed, and updates the
216 // Tabs' notion of whether they represent bookmarked sites.
217 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700218 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700219
Michael Kolbc3af0672011-08-09 10:24:41 -0700220 private boolean mBlockEvents;
221
Michael Kolb0b129122012-06-04 16:31:58 -0700222 private String mVoiceResult;
223
George Mount3636d0a2011-11-21 09:08:21 -0800224 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700225 mActivity = browser;
226 mSettings = BrowserSettings.getInstance();
227 mTabControl = new TabControl(this);
228 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700229 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800230 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700231 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700232
233 mUrlHandler = new UrlHandler(this);
234 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700235 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
236
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500238 mBookmarksObserver = new ContentObserver(mHandler) {
239 @Override
240 public void onChange(boolean selfChange) {
241 int size = mTabControl.getTabCount();
242 for (int i = 0; i < size; i++) {
243 mTabControl.getTab(i).updateBookmarkedStatus();
244 }
245 }
246
247 };
248 browser.getContentResolver().registerContentObserver(
249 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700250
251 mNetworkHandler = new NetworkStateHandler(mActivity, this);
252 // Start watching the default geolocation permissions
253 mSystemAllowGeolocationOrigins =
254 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
255 mSystemAllowGeolocationOrigins.start();
256
John Reckaf262e72011-07-25 13:55:44 -0700257 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 }
259
John Reck9c35b9c2012-05-30 10:08:50 -0700260 @Override
261 public void start(final Intent intent) {
Ben Murdochf695f6b2012-05-24 12:59:45 +0100262 WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800263 // mCrashRecoverHandler has any previously saved state.
264 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700265 }
266
George Mount3636d0a2011-11-21 09:08:21 -0800267 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 // Unless the last browser usage was within 24 hours, destroy any
269 // remaining incognito tabs.
270
271 Calendar lastActiveDate = icicle != null ?
272 (Calendar) icicle.getSerializable("lastActiveDate") : null;
273 Calendar today = Calendar.getInstance();
274 Calendar yesterday = Calendar.getInstance();
275 yesterday.add(Calendar.DATE, -1);
276
Patrick Scott7d50a932011-02-04 09:27:26 -0500277 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800279 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700280
Patrick Scott7d50a932011-02-04 09:27:26 -0500281 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700282 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500283 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000284
Michael Kolbc831b632011-05-11 09:30:34 -0700285 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 // Not able to restore so we go ahead and clear session cookies. We
287 // must do this before trying to login the user as we don't want to
288 // clear any session cookies set during login.
289 CookieManager.getInstance().removeSessionCookie();
290 }
291
Patrick Scottd43e75a2011-03-14 14:47:23 -0400292 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500293 new Runnable() {
294 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800295 onPreloginFinished(icicle, intent, currentTabId,
296 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500297 }
298 });
299 }
300
Michael Kolbc831b632011-05-11 09:30:34 -0700301 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800302 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700303 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700304 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800305 if (intent == null) {
306 // This won't happen under common scenarios. The icicle is
307 // not null, but there aren't any tabs to restore.
308 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700309 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800310 final Bundle extra = intent.getExtras();
311 // Create an initial tab.
312 // If the intent is ACTION_VIEW and data is not null, the Browser is
313 // invoked to view the content by another application. In this case,
314 // the tab will be close when exit.
315 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
316 Tab t = null;
317 if (urlData.isEmpty()) {
318 t = openTabToHomePage();
319 } else {
320 t = openTab(urlData);
321 }
322 if (t != null) {
323 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
324 }
325 WebView webView = t.getWebView();
326 if (extra != null) {
327 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
328 if (scale > 0 && scale <= 1000) {
329 webView.setInitialScale(scale);
330 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700331 }
332 }
John Reckd8c74522011-06-14 08:45:00 -0700333 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700334 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700335 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500336 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700337 List<Tab> tabs = mTabControl.getTabs();
338 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
339 for (Tab t : tabs) {
340 restoredTabs.add(t.getId());
341 }
342 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700343 if (tabs.size() == 0) {
344 openTabToHomePage();
345 }
John Reck1cf4b792011-07-26 10:22:22 -0700346 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700347 // TabControl.restoreState() will create a new tab even if
348 // restoring the state fails.
349 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800350 // Intent is non-null when framework thinks the browser should be
351 // launching with a new intent (icicle is null).
352 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700353 mIntentHandler.onNewIntent(intent);
354 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700357 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 if (jsFlags.trim().length() != 0) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000359 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700360 }
George Mount3636d0a2011-11-21 09:08:21 -0800361 if (intent != null
362 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700363 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800364 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700365 }
366
John Reck1cf4b792011-07-26 10:22:22 -0700367 private static class PruneThumbnails implements Runnable {
368 private Context mContext;
369 private List<Long> mIds;
370
371 PruneThumbnails(Context context, List<Long> preserveIds) {
372 mContext = context.getApplicationContext();
373 mIds = preserveIds;
374 }
375
376 @Override
377 public void run() {
378 ContentResolver cr = mContext.getContentResolver();
379 if (mIds == null || mIds.size() == 0) {
380 cr.delete(Thumbnails.CONTENT_URI, null, null);
381 } else {
382 int length = mIds.size();
383 StringBuilder where = new StringBuilder();
384 where.append(Thumbnails._ID);
385 where.append(" not in (");
386 for (int i = 0; i < length; i++) {
387 where.append(mIds.get(i));
388 if (i < (length - 1)) {
389 where.append(",");
390 }
391 }
392 where.append(")");
393 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
394 }
395 }
396
397 }
398
Michael Kolb1514bb72010-11-22 09:11:48 -0800399 @Override
400 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 return mFactory;
402 }
403
404 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800405 public void onSetWebView(Tab tab, WebView view) {
406 mUi.onSetWebView(tab, view);
407 }
408
409 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800410 public void createSubWindow(Tab tab) {
411 endActionMode();
412 WebView mainView = tab.getWebView();
413 WebView subView = mFactory.createWebView((mainView == null)
414 ? false
415 : mainView.isPrivateBrowsingEnabled());
416 mUi.createSubWindow(tab, subView);
417 }
418
419 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700420 public Context getContext() {
421 return mActivity;
422 }
423
424 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700425 public Activity getActivity() {
426 return mActivity;
427 }
428
429 void setUi(UI ui) {
430 mUi = ui;
431 }
432
Michael Kolbaf63dba2012-05-16 12:58:05 -0700433 @Override
434 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700435 return mSettings;
436 }
437
438 IntentHandler getIntentHandler() {
439 return mIntentHandler;
440 }
441
442 @Override
443 public UI getUi() {
444 return mUi;
445 }
446
447 int getMaxTabs() {
448 return mActivity.getResources().getInteger(R.integer.max_tabs);
449 }
450
451 @Override
452 public TabControl getTabControl() {
453 return mTabControl;
454 }
455
Michael Kolb1bf23132010-11-19 12:55:12 -0800456 @Override
457 public List<Tab> getTabs() {
458 return mTabControl.getTabs();
459 }
460
John Reckaf262e72011-07-25 13:55:44 -0700461 // Open the icon database.
462 private void openIconDatabase() {
463 // We have to call getInstance on the UI thread
464 final WebIconDatabase instance = WebIconDatabase.getInstance();
465 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000466
John Reckaf262e72011-07-25 13:55:44 -0700467 @Override
468 public void run() {
469 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700470 }
John Reckaf262e72011-07-25 13:55:44 -0700471 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700472 }
473
474 private void startHandler() {
475 mHandler = new Handler() {
476
477 @Override
478 public void handleMessage(Message msg) {
479 switch (msg.what) {
480 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700481 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700482 break;
483 case FOCUS_NODE_HREF:
484 {
485 String url = (String) msg.getData().get("url");
486 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500487 String src = (String) msg.getData().get("src");
488 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700489 if (TextUtils.isEmpty(url)) {
490 break;
491 }
492 HashMap focusNodeMap = (HashMap) msg.obj;
493 WebView view = (WebView) focusNodeMap.get("webview");
494 // Only apply the action if the top window did not change.
495 if (getCurrentTopWebView() != view) {
496 break;
497 }
498 switch (msg.arg1) {
499 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700500 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700501 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500502 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700503 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500504 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500505 case R.id.open_newtab_context_menu_id:
506 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700507 openTab(url, parent,
508 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500509 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700510 case R.id.copy_link_context_menu_id:
511 copy(url);
512 break;
513 case R.id.save_link_context_menu_id:
514 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500515 DownloadHandler.onDownloadStartNoStream(
Andreas Sandblad8e4ce662012-06-11 11:02:49 +0200516 mActivity, url, view.getSettings().getUserAgentString(),
Jean-Baptiste Queru2584c312012-09-04 14:39:07 -0700517 null, null, null, view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700518 break;
519 }
520 break;
521 }
522
523 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700524 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 break;
526
527 case STOP_LOAD:
528 stopLoading();
529 break;
530
531 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700532 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700533 mWakeLock.release();
534 // if we reach here, Browser should be still in the
535 // background loading after WAKELOCK_TIMEOUT (5-min).
536 // To avoid burning the battery, stop loading.
537 mTabControl.stopAllLoading();
538 }
539 break;
540
541 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800542 Tab tab = (Tab) msg.obj;
543 if (tab != null) {
544 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700545 }
546 break;
547 }
548 }
549 };
550
551 }
552
John Reckef654f12011-07-12 16:42:08 -0700553 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200554 public Tab getCurrentTab() {
555 return mTabControl.getCurrentTab();
556 }
557
Michael Kolbba99c5d2010-11-29 14:57:41 -0800558 @Override
559 public void shareCurrentPage() {
560 shareCurrentPage(mTabControl.getCurrentTab());
561 }
562
563 private void shareCurrentPage(Tab tab) {
564 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800565 sharePage(mActivity, tab.getTitle(),
566 tab.getUrl(), tab.getFavicon(),
567 createScreenshot(tab.getWebView(),
568 getDesiredThumbnailWidth(mActivity),
569 getDesiredThumbnailHeight(mActivity)));
570 }
571 }
572
Michael Kolb8233fac2010-10-26 16:08:53 -0700573 /**
574 * Share a page, providing the title, url, favicon, and a screenshot. Uses
575 * an {@link Intent} to launch the Activity chooser.
576 * @param c Context used to launch a new Activity.
577 * @param title Title of the page. Stored in the Intent with
578 * {@link Intent#EXTRA_SUBJECT}
579 * @param url URL of the page. Stored in the Intent with
580 * {@link Intent#EXTRA_TEXT}
581 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
582 * with {@link Browser#EXTRA_SHARE_FAVICON}
583 * @param screenshot Bitmap of a screenshot of the page. Stored in the
584 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
585 */
586 static final void sharePage(Context c, String title, String url,
587 Bitmap favicon, Bitmap screenshot) {
588 Intent send = new Intent(Intent.ACTION_SEND);
589 send.setType("text/plain");
590 send.putExtra(Intent.EXTRA_TEXT, url);
591 send.putExtra(Intent.EXTRA_SUBJECT, title);
592 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
593 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
594 try {
595 c.startActivity(Intent.createChooser(send, c.getString(
596 R.string.choosertitle_sharevia)));
597 } catch(android.content.ActivityNotFoundException ex) {
598 // if no app handles it, do nothing
599 }
600 }
601
602 private void copy(CharSequence text) {
603 ClipboardManager cm = (ClipboardManager) mActivity
604 .getSystemService(Context.CLIPBOARD_SERVICE);
605 cm.setText(text);
606 }
607
608 // lifecycle
609
John Reck9c35b9c2012-05-30 10:08:50 -0700610 @Override
611 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800613 // update the menu in case of a locale change
614 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700615 if (mPageDialogsHandler != null) {
616 mPageDialogsHandler.onConfigurationChanged(config);
617 }
618 mUi.onConfigurationChanged(config);
619 }
620
621 @Override
622 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700623 if (!mUi.isWebShowing()) {
624 mUi.showWeb(false);
625 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 mIntentHandler.onNewIntent(intent);
627 }
628
John Reck9c35b9c2012-05-30 10:08:50 -0700629 @Override
630 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800631 if (mUi.isCustomViewShowing()) {
632 hideCustomView();
633 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700634 if (mActivityPaused) {
635 Log.e(LOGTAG, "BrowserActivity is already paused.");
636 return;
637 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700638 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800639 Tab tab = mTabControl.getCurrentTab();
640 if (tab != null) {
641 tab.pause();
642 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700643 if (mWakeLock == null) {
644 PowerManager pm = (PowerManager) mActivity
645 .getSystemService(Context.POWER_SERVICE);
646 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
647 }
Michael Kolb70976932010-11-30 11:34:01 -0800648 mWakeLock.acquire();
649 mHandler.sendMessageDelayed(mHandler
650 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
651 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 }
653 mUi.onPause();
654 mNetworkHandler.onPause();
655
656 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100657 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700658 if (sThumbnailBitmap != null) {
659 sThumbnailBitmap.recycle();
660 sThumbnailBitmap = null;
661 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700662 }
663
John Reck9c35b9c2012-05-30 10:08:50 -0700664 @Override
665 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800667 Bundle saveState = createSaveState();
668
669 // crash recovery manages all save & restore state
670 mCrashRecoveryHandler.writeState(saveState);
671 mSettings.setLastRunPaused(true);
672 }
673
674 /**
675 * Save the current state to outState. Does not write the state to
676 * disk.
677 * @return Bundle containing the current state of all tabs.
678 */
679 /* package */ Bundle createSaveState() {
680 Bundle saveState = new Bundle();
681 mTabControl.saveState(saveState);
682 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700683 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800684 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700685 }
George Mount3636d0a2011-11-21 09:08:21 -0800686 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 }
688
John Reck9c35b9c2012-05-30 10:08:50 -0700689 @Override
690 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700691 if (!mActivityPaused) {
692 Log.e(LOGTAG, "BrowserActivity is already resumed.");
693 return;
694 }
George Mount3636d0a2011-11-21 09:08:21 -0800695 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800697 Tab current = mTabControl.getCurrentTab();
698 if (current != null) {
699 current.resume();
700 resumeWebViewTimers(current);
701 }
John Reckf57c0292011-07-21 18:15:39 -0700702 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200703
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 mUi.onResume();
705 mNetworkHandler.onResume();
706 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100707 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700708 if (mVoiceResult != null) {
709 mUi.onVoiceResult(mVoiceResult);
710 mVoiceResult = null;
711 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 }
713
John Reckf57c0292011-07-21 18:15:39 -0700714 private void releaseWakeLock() {
715 if (mWakeLock != null && mWakeLock.isHeld()) {
716 mHandler.removeMessages(RELEASE_WAKELOCK);
717 mWakeLock.release();
718 }
719 }
720
Michael Kolb70976932010-11-30 11:34:01 -0800721 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800722 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800723 * @param tab guaranteed non-null
724 */
725 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700726 boolean inLoad = tab.inPageLoad();
727 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
728 CookieSyncManager.getInstance().startSync();
729 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100730 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700731 }
732 }
733
Michael Kolb70976932010-11-30 11:34:01 -0800734 /**
735 * Pause all WebView timers using the WebView of the given tab
736 * @param tab
737 * @return true if the timers are paused or tab is null
738 */
739 private boolean pauseWebViewTimers(Tab tab) {
740 if (tab == null) {
741 return true;
742 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700743 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100744 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700746 }
Michael Kolb70976932010-11-30 11:34:01 -0800747 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700748 }
749
John Reck9c35b9c2012-05-30 10:08:50 -0700750 @Override
751 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800752 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
754 mUploadHandler = null;
755 }
756 if (mTabControl == null) return;
757 mUi.onDestroy();
758 // Remove the current tab and sub window
759 Tab t = mTabControl.getCurrentTab();
760 if (t != null) {
761 dismissSubWindow(t);
762 removeTab(t);
763 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500764 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700765 // Destroy all the tabs
766 mTabControl.destroy();
767 WebIconDatabase.getInstance().close();
768 // Stop watching the default geolocation permissions
769 mSystemAllowGeolocationOrigins.stop();
770 mSystemAllowGeolocationOrigins = null;
771 }
772
773 protected boolean isActivityPaused() {
774 return mActivityPaused;
775 }
776
John Reck9c35b9c2012-05-30 10:08:50 -0700777 @Override
778 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700779 mTabControl.freeMemory();
780 }
781
782 @Override
783 public boolean shouldShowErrorConsole() {
784 return mShouldShowErrorConsole;
785 }
786
787 protected void setShouldShowErrorConsole(boolean show) {
788 if (show == mShouldShowErrorConsole) {
789 // Nothing to do.
790 return;
791 }
792 mShouldShowErrorConsole = show;
793 Tab t = mTabControl.getCurrentTab();
794 if (t == null) {
795 // There is no current tab so we cannot toggle the error console
796 return;
797 }
798 mUi.setShouldShowErrorConsole(t, show);
799 }
800
801 @Override
802 public void stopLoading() {
803 mLoadStopped = true;
804 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700806 if (w != null) {
807 w.stopLoading();
808 mUi.onPageStopped(tab);
809 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700810 }
811
812 boolean didUserStopLoading() {
813 return mLoadStopped;
814 }
815
816 // WebViewController
817
818 @Override
John Reck324d4402011-01-11 16:56:42 -0800819 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700820
821 // We've started to load a new page. If there was a pending message
822 // to save a screenshot then we will now take the new page and save
823 // an incorrect screenshot. Therefore, remove any pending thumbnail
824 // messages from the queue.
825 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800826 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700827
828 // reset sync timer to avoid sync starts during loading a page
829 CookieSyncManager.getInstance().resetSync();
830
831 if (!mNetworkHandler.isNetworkUp()) {
832 view.setNetworkAvailable(false);
833 }
834
835 // when BrowserActivity just starts, onPageStarted may be called before
836 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
837 // to start the timer. As we won't switch tabs while an activity is in
838 // pause state, we can ensure calling resume and pause in pair.
839 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800840 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 }
842 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700843 endActionMode();
844
John Reck30c714c2010-12-16 17:30:34 -0800845 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700846
John Reck324d4402011-01-11 16:56:42 -0800847 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700848 // update the bookmark database for favicon
849 maybeUpdateFavicon(tab, null, url, favicon);
850
851 Performance.tracePageStart(url);
852
853 // Performance probe
854 if (false) {
855 Performance.onPageStarted();
856 }
857
858 }
859
860 @Override
John Reck324d4402011-01-11 16:56:42 -0800861 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700862 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800863 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200864
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 // Performance probe
866 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800867 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700868 }
869
870 Performance.tracePageFinished();
871 }
872
873 @Override
John Reck30c714c2010-12-16 17:30:34 -0800874 public void onProgressChanged(Tab tab) {
875 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700876
877 if (newProgress == 100) {
878 CookieSyncManager.getInstance().sync();
879 // onProgressChanged() may continue to be called after the main
880 // frame has finished loading, as any remaining sub frames continue
881 // to load. We'll only get called once though with newProgress as
882 // 100 when everything is loaded. (onPageFinished is called once
883 // when the main frame completes loading regardless of the state of
884 // any sub frames so calls to onProgressChanges may continue after
885 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800886 if (tab.inPageLoad()) {
887 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200888 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
889 // pause the WebView timer and release the wake lock if it is
890 // finished while BrowserActivity is in pause state.
891 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 }
John Reckd9862372012-02-21 15:04:50 -0800893 if (!tab.isPrivateBrowsingEnabled()
894 && !TextUtils.isEmpty(tab.getUrl())
895 && !tab.isSnapshot()) {
896 // Only update the bookmark screenshot if the user did not
897 // cancel the load early and there is not already
898 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700899 if (tab.shouldUpdateThumbnail() &&
900 (tab.inForeground() && !didUserStopLoading()
901 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800902 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
903 mHandler.sendMessageDelayed(mHandler.obtainMessage(
904 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
905 500);
906 }
907 }
908 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800910 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700911 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800912 // still loading
913 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700914 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800915 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 }
917 }
John Reck30c714c2010-12-16 17:30:34 -0800918 mUi.onProgressChanged(tab);
919 }
920
921 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100922 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800923 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 }
925
926 @Override
927 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800928 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800929 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800930 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700931 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
932 return;
933 }
934 // Update the title in the history database if not in private browsing mode
935 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700936 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700937 }
938 }
939
940 @Override
941 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800942 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
944 }
945
946 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800947 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
948 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700949 }
950
951 @Override
952 public boolean shouldOverrideKeyEvent(KeyEvent event) {
953 if (mMenuIsDown) {
954 // only check shortcut key when MENU is held
955 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
956 event);
957 } else {
958 return false;
959 }
960 }
961
962 @Override
John Reck997b1b72012-04-19 18:08:25 -0700963 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 if (!isActivityPaused()) {
965 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700966 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 } else {
John Reck997b1b72012-04-19 18:08:25 -0700968 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700969 }
970 }
John Reck997b1b72012-04-19 18:08:25 -0700971 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 }
973
974 @Override
John Reck324d4402011-01-11 16:56:42 -0800975 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 // Don't save anything in private browsing mode
977 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800978 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700979
John Reck324d4402011-01-11 16:56:42 -0800980 if (TextUtils.isEmpty(url)
981 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 return;
983 }
John Reckf57c0292011-07-21 18:15:39 -0700984 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700985 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 }
987
988 @Override
989 public void getVisitedHistory(final ValueCallback<String[]> callback) {
990 AsyncTask<Void, Void, String[]> task =
991 new AsyncTask<Void, Void, String[]>() {
992 @Override
993 public String[] doInBackground(Void... unused) {
994 return Browser.getVisitedHistory(mActivity.getContentResolver());
995 }
996 @Override
997 public void onPostExecute(String[] result) {
998 callback.onReceiveValue(result);
999 }
1000 };
1001 task.execute();
1002 }
1003
1004 @Override
1005 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1006 final HttpAuthHandler handler, final String host,
1007 final String realm) {
1008 String username = null;
1009 String password = null;
1010
1011 boolean reuseHttpAuthUsernamePassword
1012 = handler.useHttpAuthUsernamePassword();
1013
1014 if (reuseHttpAuthUsernamePassword && view != null) {
1015 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1016 if (credentials != null && credentials.length == 2) {
1017 username = credentials[0];
1018 password = credentials[1];
1019 }
1020 }
1021
1022 if (username != null && password != null) {
1023 handler.proceed(username, password);
1024 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001025 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1027 } else {
1028 handler.cancel();
1029 }
1030 }
1031 }
1032
1033 @Override
1034 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001035 String contentDisposition, String mimetype, String referer,
1036 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,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001039 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled());
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001040 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,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07002183 null, null, null, mPrivateBrowsing);
George Mount387d45d2011-10-07 15:57:53 -07002184 }
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);
2232 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT);
2233 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 {
John Reck38b39652012-06-05 09:22:59 -07002585 if (t != null && data.mDisableUrlOverride) {
2586 t.disableUrlOverridingForLoad();
2587 }
John Reck26b18322011-06-21 13:08:58 -07002588 loadUrl(t, data.mUrl, data.mHeaders);
2589 }
2590 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002591 }
2592
John Reck30c714c2010-12-16 17:30:34 -08002593 @Override
2594 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002595 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002596 if (tab.canGoBack()) {
2597 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002598 } else {
John Reckef654f12011-07-12 16:42:08 -07002599 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002600 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002601 }
2602
2603 void goBackOnePageOrQuit() {
2604 Tab current = mTabControl.getCurrentTab();
2605 if (current == null) {
2606 /*
2607 * Instead of finishing the activity, simply push this to the back
2608 * of the stack and let ActivityManager to choose the foreground
2609 * activity. As BrowserActivity is singleTask, it will be always the
2610 * root of the task. So we can use either true or false for
2611 * moveTaskToBack().
2612 */
2613 mActivity.moveTaskToBack(true);
2614 return;
2615 }
John Reckef654f12011-07-12 16:42:08 -07002616 if (current.canGoBack()) {
2617 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002618 } else {
2619 // Check to see if we are closing a window that was created by
2620 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002621 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002622 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002623 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002624 // Now we close the other tab
2625 closeTab(current);
2626 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002627 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002628 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002629 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002630 /*
2631 * Instead of finishing the activity, simply push this to the back
2632 * of the stack and let ActivityManager to choose the foreground
2633 * activity. As BrowserActivity is singleTask, it will be always the
2634 * root of the task. So we can use either true or false for
2635 * moveTaskToBack().
2636 */
2637 mActivity.moveTaskToBack(true);
2638 }
2639 }
2640 }
2641
2642 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002643 * helper method for key handler
2644 * returns the current tab if it can't advance
2645 */
Michael Kolbc831b632011-05-11 09:30:34 -07002646 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002647 int pos = mTabControl.getCurrentPosition() + 1;
2648 if (pos >= mTabControl.getTabCount()) {
2649 pos = 0;
2650 }
2651 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002652 }
2653
2654 /**
2655 * helper method for key handler
2656 * returns the current tab if it can't advance
2657 */
Michael Kolbc831b632011-05-11 09:30:34 -07002658 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002659 int pos = mTabControl.getCurrentPosition() - 1;
2660 if ( pos < 0) {
2661 pos = mTabControl.getTabCount() - 1;
2662 }
2663 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002664 }
2665
John Reckbcef87f2012-02-03 14:58:34 -08002666 boolean isMenuOrCtrlKey(int keyCode) {
2667 return (KeyEvent.KEYCODE_MENU == keyCode)
2668 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2669 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2670 }
2671
Michael Kolb0035fad2011-03-14 13:25:28 -07002672 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002673 * handle key events in browser
2674 *
2675 * @param keyCode
2676 * @param event
2677 * @return true if handled, false to pass to super
2678 */
John Reck9c35b9c2012-05-30 10:08:50 -07002679 @Override
2680 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002681 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002682 // Even if MENU is already held down, we need to call to super to open
2683 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002684 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002685 mMenuIsDown = true;
2686 return false;
2687 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002688
Cary Clark8ff8c662010-12-29 15:03:05 -05002689 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002690 Tab tab = getCurrentTab();
2691 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002692
Cary Clark160bbb92011-01-10 11:17:07 -05002693 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2694 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002695
Michael Kolb8233fac2010-10-26 16:08:53 -07002696 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002697 case KeyEvent.KEYCODE_TAB:
2698 if (event.isCtrlPressed()) {
2699 if (event.isShiftPressed()) {
2700 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002701 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002702 } else {
2703 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002704 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002705 }
2706 return true;
2707 }
2708 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002709 case KeyEvent.KEYCODE_SPACE:
2710 // WebView/WebTextView handle the keys in the KeyDown. As
2711 // the Activity's shortcut keys are only handled when WebView
2712 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002713 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002714 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002715 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002716 pageDown();
2717 }
2718 return true;
2719 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002720 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002721 event.startTracking();
2722 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002723 case KeyEvent.KEYCODE_FORWARD:
2724 if (!noModifiers) break;
2725 tab.goForward();
2726 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002727 case KeyEvent.KEYCODE_DPAD_LEFT:
2728 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002729 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002730 return true;
2731 }
2732 break;
2733 case KeyEvent.KEYCODE_DPAD_RIGHT:
2734 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002735 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002736 return true;
2737 }
2738 break;
2739 case KeyEvent.KEYCODE_A:
2740 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002741 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002742 return true;
2743 }
2744 break;
Michael Kolba4183062011-01-16 10:43:21 -08002745// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002746 case KeyEvent.KEYCODE_C:
2747 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002748 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002749 return true;
2750 }
2751 break;
Michael Kolba4183062011-01-16 10:43:21 -08002752// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002753// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002754// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002755// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002756// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002757// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002758// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002759// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002760// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002761// case KeyEvent.KEYCODE_M: // unused
2762// case KeyEvent.KEYCODE_N: // in Chrome: new window
2763// case KeyEvent.KEYCODE_O: // in Chrome: open file
2764// case KeyEvent.KEYCODE_P: // in Chrome: print page
2765// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002766// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002767// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2768 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002769 // we can't use the ctrl/shift flags, they check for
2770 // exclusive use of a modifier
2771 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002772 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002773 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002774 } else {
2775 openTabToHomePage();
2776 }
2777 return true;
2778 }
2779 break;
2780// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2781// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002782// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002783// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2784// case KeyEvent.KEYCODE_Y: // unused
2785// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002786 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002787 // it is a regular key and webview is not null
2788 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002789 }
2790
John Reck9c35b9c2012-05-30 10:08:50 -07002791 @Override
2792 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002793 switch(keyCode) {
2794 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002795 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002796 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002797 return true;
2798 }
2799 break;
2800 }
2801 return false;
2802 }
2803
John Reck9c35b9c2012-05-30 10:08:50 -07002804 @Override
2805 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002806 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002807 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002808 if (KeyEvent.KEYCODE_MENU == keyCode
2809 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002810 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002811 }
2812 }
Cary Clark160bbb92011-01-10 11:17:07 -05002813 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002814 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002815 case KeyEvent.KEYCODE_BACK:
2816 if (event.isTracking() && !event.isCanceled()) {
2817 onBackKey();
2818 return true;
2819 }
2820 break;
2821 }
2822 return false;
2823 }
2824
2825 public boolean isMenuDown() {
2826 return mMenuIsDown;
2827 }
2828
John Reck9c35b9c2012-05-30 10:08:50 -07002829 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002830 public void setupAutoFill(Message message) {
2831 // Open the settings activity at the AutoFill profile fragment so that
2832 // the user can create a new profile. When they return, we will dispatch
2833 // the message so that we can autofill the form using their new profile.
2834 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2835 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2836 AutoFillSettingsFragment.class.getName());
2837 mAutoFillSetupMessage = message;
2838 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2839 }
John Reckb3417f02011-01-14 11:01:05 -08002840
John Reck9c35b9c2012-05-30 10:08:50 -07002841 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002842 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002843 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002844 return true;
2845 }
2846
John Reck1cf4b792011-07-26 10:22:22 -07002847 @Override
2848 public boolean shouldCaptureThumbnails() {
2849 return mUi.shouldCaptureThumbnails();
2850 }
2851
Michael Kolbc3af0672011-08-09 10:24:41 -07002852 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002853 public boolean supportsVoice() {
2854 PackageManager pm = mActivity.getPackageManager();
2855 List activities = pm.queryIntentActivities(new Intent(
2856 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2857 return activities.size() != 0;
2858 }
2859
2860 @Override
2861 public void startVoiceRecognizer() {
2862 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2863 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2864 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2865 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2866 mActivity.startActivityForResult(voice, VOICE_RESULT);
2867 }
2868
2869 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002870 public void setBlockEvents(boolean block) {
2871 mBlockEvents = block;
2872 }
2873
John Reck9c35b9c2012-05-30 10:08:50 -07002874 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002875 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002876 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002877 }
2878
John Reck9c35b9c2012-05-30 10:08:50 -07002879 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002880 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002881 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002882 }
2883
John Reck9c35b9c2012-05-30 10:08:50 -07002884 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002885 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002886 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002887 }
2888
John Reck9c35b9c2012-05-30 10:08:50 -07002889 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002890 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002891 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002892 }
2893
John Reck9c35b9c2012-05-30 10:08:50 -07002894 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002895 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002896 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002897 }
2898
Michael Kolb8233fac2010-10-26 16:08:53 -07002899}