blob: caea83e7ec53d174ff3e25c4b558f7e30dad1597 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
John Reck68234a92012-04-19 15:27:12 -070020import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.ProgressDialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070024import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070025import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ContentValues;
27import android.content.Context;
John Reck68234a92012-04-19 15:27:12 -070028import android.content.DialogInterface;
29import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070030import android.content.Intent;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070034import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050035import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.database.Cursor;
37import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020038import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070039import android.graphics.Bitmap;
40import android.graphics.Canvas;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.net.Uri;
42import android.net.http.SslError;
43import android.os.AsyncTask;
44import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070045import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.os.Handler;
47import android.os.Message;
48import android.os.PowerManager;
49import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000050import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.provider.Browser;
52import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.provider.BrowserContract.Images;
54import android.provider.ContactsContract;
55import android.provider.ContactsContract.Intents.Insert;
Michael Kolb0b129122012-06-04 16:31:58 -070056import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.text.TextUtils;
58import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080059import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.view.ActionMode;
61import android.view.ContextMenu;
62import android.view.ContextMenu.ContextMenuInfo;
63import android.view.Gravity;
64import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070065import android.view.Menu;
66import android.view.MenuInflater;
67import android.view.MenuItem;
68import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070069import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070070import android.view.View;
71import android.webkit.CookieManager;
72import android.webkit.CookieSyncManager;
73import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070074import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070075import android.webkit.SslErrorHandler;
76import android.webkit.ValueCallback;
77import android.webkit.WebChromeClient;
78import android.webkit.WebIconDatabase;
79import android.webkit.WebSettings;
80import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000081import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Michael Kolb4bd767d2011-05-27 11:33:55 -070084import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070085import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070086import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070087import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070088
Michael Kolb8233fac2010-10-26 16:08:53 -070089import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -070090import java.io.File;
91import java.io.FileOutputStream;
92import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070093import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -070094import java.text.DateFormat;
95import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -070096import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -070097import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -070098import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -070099import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800100import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200101import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700102import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700103
104/**
105 * Controller for browser
106 */
107public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700108 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700109
110 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800111 private static final String SEND_APP_ID_EXTRA =
112 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700113 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800114
Michael Kolb8233fac2010-10-26 16:08:53 -0700115
116 // public message ids
117 public final static int LOAD_URL = 1001;
118 public final static int STOP_LOAD = 1002;
119
120 // Message Ids
121 private static final int FOCUS_NODE_HREF = 102;
122 private static final int RELEASE_WAKELOCK = 107;
123
124 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
125
126 private static final int OPEN_BOOKMARKS = 201;
127
128 private static final int EMPTY_MENU = -1;
129
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700131 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 final static int PREFERENCES_PAGE = 3;
133 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000134 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700135 final static int VOICE_RESULT = 6;
Ben Murdoch8029a772010-11-16 11:58:21 +0000136
Michael Kolb8233fac2010-10-26 16:08:53 -0700137 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
138
139 // As the ids are dynamically created, we can't guarantee that they will
140 // be in sequence, so this static array maps ids to a window number.
141 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
142 { R.id.window_one_menu_id, R.id.window_two_menu_id,
143 R.id.window_three_menu_id, R.id.window_four_menu_id,
144 R.id.window_five_menu_id, R.id.window_six_menu_id,
145 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
146
147 // "source" parameter for Google search through search key
148 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
149 // "source" parameter for Google search through simplily type
150 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
151
George Mount387d45d2011-10-07 15:57:53 -0700152 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700153 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
154
John Reckd7dd9b22011-08-30 09:18:29 -0700155 // A bitmap that is re-used in createScreenshot as scratch space
156 private static Bitmap sThumbnailBitmap;
157
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 private Activity mActivity;
159 private UI mUi;
160 private TabControl mTabControl;
161 private BrowserSettings mSettings;
162 private WebViewFactory mFactory;
163
164 private WakeLock mWakeLock;
165
166 private UrlHandler mUrlHandler;
167 private UploadHandler mUploadHandler;
168 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700169 private PageDialogsHandler mPageDialogsHandler;
170 private NetworkStateHandler mNetworkHandler;
171
Ben Murdoch8029a772010-11-16 11:58:21 +0000172 private Message mAutoFillSetupMessage;
173
Michael Kolb8233fac2010-10-26 16:08:53 -0700174 private boolean mShouldShowErrorConsole;
175
176 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
177
178 // FIXME, temp address onPrepareMenu performance problem.
179 // When we move everything out of view, we should rewrite this.
180 private int mCurrentMenuState = 0;
181 private int mMenuState = R.id.MAIN_MENU;
182 private int mOldMenuState = EMPTY_MENU;
183 private Menu mCachedMenu;
184
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 private boolean mMenuIsDown;
186
187 // For select and find, we keep track of the ActionMode so that
188 // finish() can be called as desired.
189 private ActionMode mActionMode;
190
191 /**
192 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
193 * of whether the configuration has changed. The first onMenuOpened call
194 * after a configuration change is simply a reopening of the same menu
195 * (i.e. mIconView did not change).
196 */
197 private boolean mConfigChanged;
198
199 /**
200 * Keeps track of whether the options menu is open. This is important in
201 * determining whether to show or hide the title bar overlay
202 */
203 private boolean mOptionsMenuOpen;
204
205 /**
206 * Whether or not the options menu is in its bigger, popup menu form. When
207 * true, we want the title bar overlay to be gone. When false, we do not.
208 * Only meaningful if mOptionsMenuOpen is true.
209 */
210 private boolean mExtendedMenuOpen;
211
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 private boolean mActivityPaused = true;
213 private boolean mLoadStopped;
214
215 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500216 // Checks to see when the bookmarks database has changed, and updates the
217 // Tabs' notion of whether they represent bookmarked sites.
218 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700219 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700220
Michael Kolbc3af0672011-08-09 10:24:41 -0700221 private boolean mBlockEvents;
222
Michael Kolb0b129122012-06-04 16:31:58 -0700223 private String mVoiceResult;
224
George Mount3636d0a2011-11-21 09:08:21 -0800225 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700226 mActivity = browser;
227 mSettings = BrowserSettings.getInstance();
228 mTabControl = new TabControl(this);
229 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700230 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800231 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700232 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700233
234 mUrlHandler = new UrlHandler(this);
235 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
237
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500239 mBookmarksObserver = new ContentObserver(mHandler) {
240 @Override
241 public void onChange(boolean selfChange) {
242 int size = mTabControl.getTabCount();
243 for (int i = 0; i < size; i++) {
244 mTabControl.getTab(i).updateBookmarkedStatus();
245 }
246 }
247
248 };
249 browser.getContentResolver().registerContentObserver(
250 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700251
252 mNetworkHandler = new NetworkStateHandler(mActivity, this);
253 // Start watching the default geolocation permissions
254 mSystemAllowGeolocationOrigins =
255 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
256 mSystemAllowGeolocationOrigins.start();
257
John Reckaf262e72011-07-25 13:55:44 -0700258 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 }
260
John Reck9c35b9c2012-05-30 10:08:50 -0700261 @Override
262 public void start(final Intent intent) {
Ben Murdochf695f6b2012-05-24 12:59:45 +0100263 WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800264 // mCrashRecoverHandler has any previously saved state.
265 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700266 }
267
George Mount3636d0a2011-11-21 09:08:21 -0800268 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 // Unless the last browser usage was within 24 hours, destroy any
270 // remaining incognito tabs.
271
272 Calendar lastActiveDate = icicle != null ?
273 (Calendar) icicle.getSerializable("lastActiveDate") : null;
274 Calendar today = Calendar.getInstance();
275 Calendar yesterday = Calendar.getInstance();
276 yesterday.add(Calendar.DATE, -1);
277
Patrick Scott7d50a932011-02-04 09:27:26 -0500278 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800280 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700281
Patrick Scott7d50a932011-02-04 09:27:26 -0500282 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700283 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500284 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000285
Michael Kolbc831b632011-05-11 09:30:34 -0700286 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500287 // Not able to restore so we go ahead and clear session cookies. We
288 // must do this before trying to login the user as we don't want to
289 // clear any session cookies set during login.
290 CookieManager.getInstance().removeSessionCookie();
291 }
292
Patrick Scottd43e75a2011-03-14 14:47:23 -0400293 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500294 new Runnable() {
295 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800296 onPreloginFinished(icicle, intent, currentTabId,
297 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500298 }
299 });
300 }
301
Michael Kolbc831b632011-05-11 09:30:34 -0700302 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800303 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700304 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700305 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800306 if (intent == null) {
307 // This won't happen under common scenarios. The icicle is
308 // not null, but there aren't any tabs to restore.
309 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700310 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800311 final Bundle extra = intent.getExtras();
312 // Create an initial tab.
313 // If the intent is ACTION_VIEW and data is not null, the Browser is
314 // invoked to view the content by another application. In this case,
315 // the tab will be close when exit.
316 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
317 Tab t = null;
318 if (urlData.isEmpty()) {
319 t = openTabToHomePage();
320 } else {
321 t = openTab(urlData);
322 }
323 if (t != null) {
324 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
325 }
326 WebView webView = t.getWebView();
327 if (extra != null) {
328 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
329 if (scale > 0 && scale <= 1000) {
330 webView.setInitialScale(scale);
331 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700332 }
333 }
John Reckd8c74522011-06-14 08:45:00 -0700334 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700336 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500337 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700338 List<Tab> tabs = mTabControl.getTabs();
339 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
340 for (Tab t : tabs) {
341 restoredTabs.add(t.getId());
342 }
343 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700344 if (tabs.size() == 0) {
345 openTabToHomePage();
346 }
John Reck1cf4b792011-07-26 10:22:22 -0700347 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700348 // TabControl.restoreState() will create a new tab even if
349 // restoring the state fails.
350 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800351 // Intent is non-null when framework thinks the browser should be
352 // launching with a new intent (icicle is null).
353 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700354 mIntentHandler.onNewIntent(intent);
355 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700358 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 if (jsFlags.trim().length() != 0) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000360 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 }
George Mount3636d0a2011-11-21 09:08:21 -0800362 if (intent != null
363 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700364 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800365 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 }
367
John Reck1cf4b792011-07-26 10:22:22 -0700368 private static class PruneThumbnails implements Runnable {
369 private Context mContext;
370 private List<Long> mIds;
371
372 PruneThumbnails(Context context, List<Long> preserveIds) {
373 mContext = context.getApplicationContext();
374 mIds = preserveIds;
375 }
376
377 @Override
378 public void run() {
379 ContentResolver cr = mContext.getContentResolver();
380 if (mIds == null || mIds.size() == 0) {
381 cr.delete(Thumbnails.CONTENT_URI, null, null);
382 } else {
383 int length = mIds.size();
384 StringBuilder where = new StringBuilder();
385 where.append(Thumbnails._ID);
386 where.append(" not in (");
387 for (int i = 0; i < length; i++) {
388 where.append(mIds.get(i));
389 if (i < (length - 1)) {
390 where.append(",");
391 }
392 }
393 where.append(")");
394 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
395 }
396 }
397
398 }
399
Michael Kolb1514bb72010-11-22 09:11:48 -0800400 @Override
401 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 return mFactory;
403 }
404
405 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800406 public void onSetWebView(Tab tab, WebView view) {
407 mUi.onSetWebView(tab, view);
408 }
409
410 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800411 public void createSubWindow(Tab tab) {
412 endActionMode();
413 WebView mainView = tab.getWebView();
414 WebView subView = mFactory.createWebView((mainView == null)
415 ? false
416 : mainView.isPrivateBrowsingEnabled());
417 mUi.createSubWindow(tab, subView);
418 }
419
420 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700421 public Context getContext() {
422 return mActivity;
423 }
424
425 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700426 public Activity getActivity() {
427 return mActivity;
428 }
429
430 void setUi(UI ui) {
431 mUi = ui;
432 }
433
Michael Kolbaf63dba2012-05-16 12:58:05 -0700434 @Override
435 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700436 return mSettings;
437 }
438
439 IntentHandler getIntentHandler() {
440 return mIntentHandler;
441 }
442
443 @Override
444 public UI getUi() {
445 return mUi;
446 }
447
448 int getMaxTabs() {
449 return mActivity.getResources().getInteger(R.integer.max_tabs);
450 }
451
452 @Override
453 public TabControl getTabControl() {
454 return mTabControl;
455 }
456
Michael Kolb1bf23132010-11-19 12:55:12 -0800457 @Override
458 public List<Tab> getTabs() {
459 return mTabControl.getTabs();
460 }
461
John Reckaf262e72011-07-25 13:55:44 -0700462 // Open the icon database.
463 private void openIconDatabase() {
464 // We have to call getInstance on the UI thread
465 final WebIconDatabase instance = WebIconDatabase.getInstance();
466 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000467
John Reckaf262e72011-07-25 13:55:44 -0700468 @Override
469 public void run() {
470 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700471 }
John Reckaf262e72011-07-25 13:55:44 -0700472 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700473 }
474
475 private void startHandler() {
476 mHandler = new Handler() {
477
478 @Override
479 public void handleMessage(Message msg) {
480 switch (msg.what) {
481 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700482 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700483 break;
484 case FOCUS_NODE_HREF:
485 {
486 String url = (String) msg.getData().get("url");
487 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500488 String src = (String) msg.getData().get("src");
489 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700490 if (TextUtils.isEmpty(url)) {
491 break;
492 }
493 HashMap focusNodeMap = (HashMap) msg.obj;
494 WebView view = (WebView) focusNodeMap.get("webview");
495 // Only apply the action if the top window did not change.
496 if (getCurrentTopWebView() != view) {
497 break;
498 }
499 switch (msg.arg1) {
500 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700501 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500503 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700504 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500505 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500506 case R.id.open_newtab_context_menu_id:
507 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700508 openTab(url, parent,
509 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500510 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 case R.id.copy_link_context_menu_id:
512 copy(url);
513 break;
514 case R.id.save_link_context_menu_id:
515 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500516 DownloadHandler.onDownloadStartNoStream(
Andreas Sandblad8e4ce662012-06-11 11:02:49 +0200517 mActivity, url, view.getSettings().getUserAgentString(),
Jean-Baptiste Queru2584c312012-09-04 14:39:07 -0700518 null, null, null, view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 break;
520 }
521 break;
522 }
523
524 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700525 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700526 break;
527
528 case STOP_LOAD:
529 stopLoading();
530 break;
531
532 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700533 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700534 mWakeLock.release();
535 // if we reach here, Browser should be still in the
536 // background loading after WAKELOCK_TIMEOUT (5-min).
537 // To avoid burning the battery, stop loading.
538 mTabControl.stopAllLoading();
539 }
540 break;
541
542 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800543 Tab tab = (Tab) msg.obj;
544 if (tab != null) {
545 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 }
547 break;
548 }
549 }
550 };
551
552 }
553
John Reckef654f12011-07-12 16:42:08 -0700554 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200555 public Tab getCurrentTab() {
556 return mTabControl.getCurrentTab();
557 }
558
Michael Kolbba99c5d2010-11-29 14:57:41 -0800559 @Override
560 public void shareCurrentPage() {
561 shareCurrentPage(mTabControl.getCurrentTab());
562 }
563
564 private void shareCurrentPage(Tab tab) {
565 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800566 sharePage(mActivity, tab.getTitle(),
567 tab.getUrl(), tab.getFavicon(),
568 createScreenshot(tab.getWebView(),
569 getDesiredThumbnailWidth(mActivity),
570 getDesiredThumbnailHeight(mActivity)));
571 }
572 }
573
Michael Kolb8233fac2010-10-26 16:08:53 -0700574 /**
575 * Share a page, providing the title, url, favicon, and a screenshot. Uses
576 * an {@link Intent} to launch the Activity chooser.
577 * @param c Context used to launch a new Activity.
578 * @param title Title of the page. Stored in the Intent with
579 * {@link Intent#EXTRA_SUBJECT}
580 * @param url URL of the page. Stored in the Intent with
581 * {@link Intent#EXTRA_TEXT}
582 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
583 * with {@link Browser#EXTRA_SHARE_FAVICON}
584 * @param screenshot Bitmap of a screenshot of the page. Stored in the
585 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
586 */
587 static final void sharePage(Context c, String title, String url,
588 Bitmap favicon, Bitmap screenshot) {
589 Intent send = new Intent(Intent.ACTION_SEND);
590 send.setType("text/plain");
591 send.putExtra(Intent.EXTRA_TEXT, url);
592 send.putExtra(Intent.EXTRA_SUBJECT, title);
593 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
594 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
595 try {
596 c.startActivity(Intent.createChooser(send, c.getString(
597 R.string.choosertitle_sharevia)));
598 } catch(android.content.ActivityNotFoundException ex) {
599 // if no app handles it, do nothing
600 }
601 }
602
603 private void copy(CharSequence text) {
604 ClipboardManager cm = (ClipboardManager) mActivity
605 .getSystemService(Context.CLIPBOARD_SERVICE);
606 cm.setText(text);
607 }
608
609 // lifecycle
610
John Reck9c35b9c2012-05-30 10:08:50 -0700611 @Override
612 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800614 // update the menu in case of a locale change
615 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 if (mPageDialogsHandler != null) {
617 mPageDialogsHandler.onConfigurationChanged(config);
618 }
619 mUi.onConfigurationChanged(config);
620 }
621
622 @Override
623 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700624 if (!mUi.isWebShowing()) {
625 mUi.showWeb(false);
626 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700627 mIntentHandler.onNewIntent(intent);
628 }
629
John Reck9c35b9c2012-05-30 10:08:50 -0700630 @Override
631 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800632 if (mUi.isCustomViewShowing()) {
633 hideCustomView();
634 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 if (mActivityPaused) {
636 Log.e(LOGTAG, "BrowserActivity is already paused.");
637 return;
638 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800640 Tab tab = mTabControl.getCurrentTab();
641 if (tab != null) {
642 tab.pause();
643 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700644 if (mWakeLock == null) {
645 PowerManager pm = (PowerManager) mActivity
646 .getSystemService(Context.POWER_SERVICE);
647 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
648 }
Michael Kolb70976932010-11-30 11:34:01 -0800649 mWakeLock.acquire();
650 mHandler.sendMessageDelayed(mHandler
651 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
652 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 }
654 mUi.onPause();
655 mNetworkHandler.onPause();
656
657 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100658 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700659 if (sThumbnailBitmap != null) {
660 sThumbnailBitmap.recycle();
661 sThumbnailBitmap = null;
662 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 }
664
John Reck9c35b9c2012-05-30 10:08:50 -0700665 @Override
666 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800668 Bundle saveState = createSaveState();
669
670 // crash recovery manages all save & restore state
671 mCrashRecoveryHandler.writeState(saveState);
672 mSettings.setLastRunPaused(true);
673 }
674
675 /**
676 * Save the current state to outState. Does not write the state to
677 * disk.
678 * @return Bundle containing the current state of all tabs.
679 */
680 /* package */ Bundle createSaveState() {
681 Bundle saveState = new Bundle();
682 mTabControl.saveState(saveState);
683 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700684 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800685 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700686 }
George Mount3636d0a2011-11-21 09:08:21 -0800687 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 }
689
John Reck9c35b9c2012-05-30 10:08:50 -0700690 @Override
691 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 if (!mActivityPaused) {
693 Log.e(LOGTAG, "BrowserActivity is already resumed.");
694 return;
695 }
George Mount3636d0a2011-11-21 09:08:21 -0800696 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800698 Tab current = mTabControl.getCurrentTab();
699 if (current != null) {
700 current.resume();
701 resumeWebViewTimers(current);
702 }
John Reckf57c0292011-07-21 18:15:39 -0700703 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200704
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 mUi.onResume();
706 mNetworkHandler.onResume();
707 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100708 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700709 if (mVoiceResult != null) {
710 mUi.onVoiceResult(mVoiceResult);
711 mVoiceResult = null;
712 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 }
714
John Reckf57c0292011-07-21 18:15:39 -0700715 private void releaseWakeLock() {
716 if (mWakeLock != null && mWakeLock.isHeld()) {
717 mHandler.removeMessages(RELEASE_WAKELOCK);
718 mWakeLock.release();
719 }
720 }
721
Michael Kolb70976932010-11-30 11:34:01 -0800722 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800723 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800724 * @param tab guaranteed non-null
725 */
726 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 boolean inLoad = tab.inPageLoad();
728 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
729 CookieSyncManager.getInstance().startSync();
730 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100731 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700732 }
733 }
734
Michael Kolb70976932010-11-30 11:34:01 -0800735 /**
736 * Pause all WebView timers using the WebView of the given tab
737 * @param tab
738 * @return true if the timers are paused or tab is null
739 */
740 private boolean pauseWebViewTimers(Tab tab) {
741 if (tab == null) {
742 return true;
743 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100745 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700746 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700747 }
Michael Kolb70976932010-11-30 11:34:01 -0800748 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700749 }
750
John Reck9c35b9c2012-05-30 10:08:50 -0700751 @Override
752 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800753 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
755 mUploadHandler = null;
756 }
757 if (mTabControl == null) return;
758 mUi.onDestroy();
759 // Remove the current tab and sub window
760 Tab t = mTabControl.getCurrentTab();
761 if (t != null) {
762 dismissSubWindow(t);
763 removeTab(t);
764 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500765 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700766 // Destroy all the tabs
767 mTabControl.destroy();
768 WebIconDatabase.getInstance().close();
769 // Stop watching the default geolocation permissions
770 mSystemAllowGeolocationOrigins.stop();
771 mSystemAllowGeolocationOrigins = null;
772 }
773
774 protected boolean isActivityPaused() {
775 return mActivityPaused;
776 }
777
John Reck9c35b9c2012-05-30 10:08:50 -0700778 @Override
779 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700780 mTabControl.freeMemory();
781 }
782
783 @Override
784 public boolean shouldShowErrorConsole() {
785 return mShouldShowErrorConsole;
786 }
787
788 protected void setShouldShowErrorConsole(boolean show) {
789 if (show == mShouldShowErrorConsole) {
790 // Nothing to do.
791 return;
792 }
793 mShouldShowErrorConsole = show;
794 Tab t = mTabControl.getCurrentTab();
795 if (t == null) {
796 // There is no current tab so we cannot toggle the error console
797 return;
798 }
799 mUi.setShouldShowErrorConsole(t, show);
800 }
801
802 @Override
803 public void stopLoading() {
804 mLoadStopped = true;
805 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700807 if (w != null) {
808 w.stopLoading();
809 mUi.onPageStopped(tab);
810 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 }
812
813 boolean didUserStopLoading() {
814 return mLoadStopped;
815 }
816
817 // WebViewController
818
819 @Override
John Reck324d4402011-01-11 16:56:42 -0800820 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700821
822 // We've started to load a new page. If there was a pending message
823 // to save a screenshot then we will now take the new page and save
824 // an incorrect screenshot. Therefore, remove any pending thumbnail
825 // messages from the queue.
826 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800827 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700828
829 // reset sync timer to avoid sync starts during loading a page
830 CookieSyncManager.getInstance().resetSync();
831
832 if (!mNetworkHandler.isNetworkUp()) {
833 view.setNetworkAvailable(false);
834 }
835
836 // when BrowserActivity just starts, onPageStarted may be called before
837 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
838 // to start the timer. As we won't switch tabs while an activity is in
839 // pause state, we can ensure calling resume and pause in pair.
840 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800841 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 }
843 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 endActionMode();
845
John Reck30c714c2010-12-16 17:30:34 -0800846 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700847
John Reck324d4402011-01-11 16:56:42 -0800848 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 // update the bookmark database for favicon
850 maybeUpdateFavicon(tab, null, url, favicon);
851
852 Performance.tracePageStart(url);
853
854 // Performance probe
855 if (false) {
856 Performance.onPageStarted();
857 }
858
859 }
860
861 @Override
John Reck324d4402011-01-11 16:56:42 -0800862 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700863 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800864 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200865
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 // Performance probe
867 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800868 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 }
870
871 Performance.tracePageFinished();
872 }
873
874 @Override
John Reck30c714c2010-12-16 17:30:34 -0800875 public void onProgressChanged(Tab tab) {
876 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700877
878 if (newProgress == 100) {
879 CookieSyncManager.getInstance().sync();
880 // onProgressChanged() may continue to be called after the main
881 // frame has finished loading, as any remaining sub frames continue
882 // to load. We'll only get called once though with newProgress as
883 // 100 when everything is loaded. (onPageFinished is called once
884 // when the main frame completes loading regardless of the state of
885 // any sub frames so calls to onProgressChanges may continue after
886 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800887 if (tab.inPageLoad()) {
888 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200889 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
890 // pause the WebView timer and release the wake lock if it is
891 // finished while BrowserActivity is in pause state.
892 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 }
John Reckd9862372012-02-21 15:04:50 -0800894 if (!tab.isPrivateBrowsingEnabled()
895 && !TextUtils.isEmpty(tab.getUrl())
896 && !tab.isSnapshot()) {
897 // Only update the bookmark screenshot if the user did not
898 // cancel the load early and there is not already
899 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700900 if (tab.shouldUpdateThumbnail() &&
901 (tab.inForeground() && !didUserStopLoading()
902 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800903 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
904 mHandler.sendMessageDelayed(mHandler.obtainMessage(
905 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
906 500);
907 }
908 }
909 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700910 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800911 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700912 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800913 // still loading
914 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700915 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800916 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700917 }
918 }
John Reck30c714c2010-12-16 17:30:34 -0800919 mUi.onProgressChanged(tab);
920 }
921
922 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100923 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800924 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700925 }
926
927 @Override
928 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800929 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800930 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800931 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700932 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
933 return;
934 }
935 // Update the title in the history database if not in private browsing mode
936 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700937 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 }
939 }
940
941 @Override
942 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800943 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700944 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
945 }
946
947 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800948 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
949 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 }
951
952 @Override
953 public boolean shouldOverrideKeyEvent(KeyEvent event) {
954 if (mMenuIsDown) {
955 // only check shortcut key when MENU is held
956 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
957 event);
958 } else {
959 return false;
960 }
961 }
962
963 @Override
John Reck997b1b72012-04-19 18:08:25 -0700964 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 if (!isActivityPaused()) {
966 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700967 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700968 } else {
John Reck997b1b72012-04-19 18:08:25 -0700969 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 }
971 }
John Reck997b1b72012-04-19 18:08:25 -0700972 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700973 }
974
975 @Override
John Reck324d4402011-01-11 16:56:42 -0800976 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 // Don't save anything in private browsing mode
978 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800979 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700980
John Reck324d4402011-01-11 16:56:42 -0800981 if (TextUtils.isEmpty(url)
982 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 return;
984 }
John Reckf57c0292011-07-21 18:15:39 -0700985 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700986 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 }
988
989 @Override
990 public void getVisitedHistory(final ValueCallback<String[]> callback) {
991 AsyncTask<Void, Void, String[]> task =
992 new AsyncTask<Void, Void, String[]>() {
993 @Override
994 public String[] doInBackground(Void... unused) {
995 return Browser.getVisitedHistory(mActivity.getContentResolver());
996 }
997 @Override
998 public void onPostExecute(String[] result) {
999 callback.onReceiveValue(result);
1000 }
1001 };
1002 task.execute();
1003 }
1004
1005 @Override
1006 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1007 final HttpAuthHandler handler, final String host,
1008 final String realm) {
1009 String username = null;
1010 String password = null;
1011
1012 boolean reuseHttpAuthUsernamePassword
1013 = handler.useHttpAuthUsernamePassword();
1014
1015 if (reuseHttpAuthUsernamePassword && view != null) {
1016 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1017 if (credentials != null && credentials.length == 2) {
1018 username = credentials[0];
1019 password = credentials[1];
1020 }
1021 }
1022
1023 if (username != null && password != null) {
1024 handler.proceed(username, password);
1025 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001026 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001027 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1028 } else {
1029 handler.cancel();
1030 }
1031 }
1032 }
1033
1034 @Override
1035 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001036 String contentDisposition, String mimetype, String referer,
1037 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001038 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -05001039 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001040 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled());
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001041 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 // This Tab was opened for the sole purpose of downloading a
1043 // file. Remove it.
1044 if (tab == mTabControl.getCurrentTab()) {
1045 // In this case, the Tab is still on top.
1046 goBackOnePageOrQuit();
1047 } else {
1048 // In this case, it is not.
1049 closeTab(tab);
1050 }
1051 }
1052 }
1053
1054 @Override
1055 public Bitmap getDefaultVideoPoster() {
1056 return mUi.getDefaultVideoPoster();
1057 }
1058
1059 @Override
1060 public View getVideoLoadingProgressView() {
1061 return mUi.getVideoLoadingProgressView();
1062 }
1063
1064 @Override
1065 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1066 SslError error) {
1067 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1068 }
1069
Patrick Scott92066772011-03-10 08:46:27 -05001070 @Override
1071 public void showAutoLogin(Tab tab) {
1072 assert tab.inForeground();
1073 // Update the title bar to show the auto-login request.
1074 mUi.showAutoLogin(tab);
1075 }
1076
1077 @Override
1078 public void hideAutoLogin(Tab tab) {
1079 assert tab.inForeground();
1080 mUi.hideAutoLogin(tab);
1081 }
1082
Michael Kolb8233fac2010-10-26 16:08:53 -07001083 // helper method
1084
1085 /*
1086 * Update the favorites icon if the private browsing isn't enabled and the
1087 * icon is valid.
1088 */
1089 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1090 final String url, Bitmap favicon) {
1091 if (favicon == null) {
1092 return;
1093 }
1094 if (!tab.isPrivateBrowsingEnabled()) {
1095 Bookmarks.updateFavicon(mActivity
1096 .getContentResolver(), originalUrl, url, favicon);
1097 }
1098 }
1099
Leon Scroggins4cd97792010-12-03 15:31:56 -05001100 @Override
1101 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001102 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001103 mUi.bookmarkedStatusHasChanged(tab);
1104 }
1105
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 // end WebViewController
1107
1108 protected void pageUp() {
1109 getCurrentTopWebView().pageUp(false);
1110 }
1111
1112 protected void pageDown() {
1113 getCurrentTopWebView().pageDown(false);
1114 }
1115
1116 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001117 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001118 public void editUrl() {
1119 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001120 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001121 }
1122
John Reck9c35b9c2012-05-30 10:08:50 -07001123 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001124 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 WebChromeClient.CustomViewCallback callback) {
1126 if (tab.inForeground()) {
1127 if (mUi.isCustomViewShowing()) {
1128 callback.onCustomViewHidden();
1129 return;
1130 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001131 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001132 // Save the menu state and set it to empty while the custom
1133 // view is showing.
1134 mOldMenuState = mMenuState;
1135 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001136 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 }
1138 }
1139
1140 @Override
1141 public void hideCustomView() {
1142 if (mUi.isCustomViewShowing()) {
1143 mUi.onHideCustomView();
1144 // Reset the old menu state.
1145 mMenuState = mOldMenuState;
1146 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001147 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 }
1149 }
1150
John Reck9c35b9c2012-05-30 10:08:50 -07001151 @Override
1152 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001153 Intent intent) {
1154 if (getCurrentTopWebView() == null) return;
1155 switch (requestCode) {
1156 case PREFERENCES_PAGE:
1157 if (resultCode == Activity.RESULT_OK && intent != null) {
1158 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001159 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 mTabControl.removeParentChildRelationShips();
1161 }
1162 }
1163 break;
1164 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001165 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001166 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001167 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001168 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001169 case AUTOFILL_SETUP:
1170 // Determine whether a profile was actually set up or not
1171 // and if so, send the message back to the WebTextView to
1172 // fill the form with the new profile.
1173 if (getSettings().getAutoFillProfile() != null) {
1174 mAutoFillSetupMessage.sendToTarget();
1175 mAutoFillSetupMessage = null;
1176 }
1177 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001178 case COMBO_VIEW:
1179 if (intent == null || resultCode != Activity.RESULT_OK) {
1180 break;
1181 }
John Reck3ba45532011-08-11 16:26:53 -07001182 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001183 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1184 Tab t = getCurrentTab();
1185 Uri uri = intent.getData();
1186 loadUrl(t, uri.toString());
1187 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1188 String[] urls = intent.getStringArrayExtra(
1189 ComboViewActivity.EXTRA_OPEN_ALL);
1190 Tab parent = getCurrentTab();
1191 for (String url : urls) {
1192 parent = openTab(url, parent,
1193 !mSettings.openInBackground(), true);
1194 }
1195 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1196 long id = intent.getLongExtra(
1197 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1198 if (id >= 0) {
1199 createNewSnapshotTab(id, true);
1200 }
1201 }
1202 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001203 case VOICE_RESULT:
1204 if (resultCode == Activity.RESULT_OK && intent != null) {
1205 ArrayList<String> results = intent.getStringArrayListExtra(
1206 RecognizerIntent.EXTRA_RESULTS);
1207 if (results.size() >= 1) {
1208 mVoiceResult = results.get(0);
1209 }
1210 }
1211 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001212 default:
1213 break;
1214 }
1215 getCurrentTopWebView().requestFocus();
1216 }
1217
1218 /**
1219 * Open the Go page.
1220 * @param startWithHistory If true, open starting on the history tab.
1221 * Otherwise, start with the bookmarks tab.
1222 */
1223 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001224 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001225 if (mTabControl.getCurrentWebView() == null) {
1226 return;
1227 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001228 // clear action mode
1229 if (isInCustomActionMode()) {
1230 endActionMode();
1231 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 Bundle extras = new Bundle();
1233 // Disable opening in a new window if we have maxed out the windows
1234 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1235 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001236 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001237 }
1238
1239 // combo view callbacks
1240
Michael Kolb8233fac2010-10-26 16:08:53 -07001241 // key handling
1242 protected void onBackKey() {
1243 if (!mUi.onBackKey()) {
1244 WebView subwindow = mTabControl.getCurrentSubWindow();
1245 if (subwindow != null) {
1246 if (subwindow.canGoBack()) {
1247 subwindow.goBack();
1248 } else {
1249 dismissSubWindow(mTabControl.getCurrentTab());
1250 }
1251 } else {
1252 goBackOnePageOrQuit();
1253 }
1254 }
1255 }
1256
Michael Kolb4bd767d2011-05-27 11:33:55 -07001257 protected boolean onMenuKey() {
1258 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001259 }
1260
Michael Kolb8233fac2010-10-26 16:08:53 -07001261 // menu handling and state
1262 // TODO: maybe put into separate handler
1263
John Reck9c35b9c2012-05-30 10:08:50 -07001264 @Override
1265 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001266 if (mMenuState == EMPTY_MENU) {
1267 return false;
1268 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001269 MenuInflater inflater = mActivity.getMenuInflater();
1270 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001271 return true;
1272 }
1273
John Reck9c35b9c2012-05-30 10:08:50 -07001274 @Override
1275 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001277 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001278 return;
1279 }
1280 if (!(v instanceof WebView)) {
1281 return;
1282 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001283 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001284 WebView.HitTestResult result = webview.getHitTestResult();
1285 if (result == null) {
1286 return;
1287 }
1288
1289 int type = result.getType();
1290 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1291 Log.w(LOGTAG,
1292 "We should not show context menu when nothing is touched");
1293 return;
1294 }
1295 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1296 // let TextView handles context menu
1297 return;
1298 }
1299
1300 // Note, http://b/issue?id=1106666 is requesting that
1301 // an inflated menu can be used again. This is not available
1302 // yet, so inflate each time (yuk!)
1303 MenuInflater inflater = mActivity.getMenuInflater();
1304 inflater.inflate(R.menu.browsercontext, menu);
1305
1306 // Show the correct menu group
1307 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001308 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001309 menu.setGroupVisible(R.id.PHONE_MENU,
1310 type == WebView.HitTestResult.PHONE_TYPE);
1311 menu.setGroupVisible(R.id.EMAIL_MENU,
1312 type == WebView.HitTestResult.EMAIL_TYPE);
1313 menu.setGroupVisible(R.id.GEO_MENU,
1314 type == WebView.HitTestResult.GEO_TYPE);
1315 menu.setGroupVisible(R.id.IMAGE_MENU,
1316 type == WebView.HitTestResult.IMAGE_TYPE
1317 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1318 menu.setGroupVisible(R.id.ANCHOR_MENU,
1319 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1320 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001321 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1322 || type == WebView.HitTestResult.PHONE_TYPE
1323 || type == WebView.HitTestResult.EMAIL_TYPE
1324 || type == WebView.HitTestResult.GEO_TYPE;
1325 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1326 if (hitText) {
1327 menu.findItem(R.id.select_text_menu_id)
1328 .setOnMenuItemClickListener(new SelectText(webview));
1329 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001330 // Setup custom handling depending on the type
1331 switch (type) {
1332 case WebView.HitTestResult.PHONE_TYPE:
1333 menu.setHeaderTitle(Uri.decode(extra));
1334 menu.findItem(R.id.dial_context_menu_id).setIntent(
1335 new Intent(Intent.ACTION_VIEW, Uri
1336 .parse(WebView.SCHEME_TEL + extra)));
1337 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1338 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1339 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1340 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1341 addIntent);
1342 menu.findItem(R.id.copy_phone_context_menu_id)
1343 .setOnMenuItemClickListener(
1344 new Copy(extra));
1345 break;
1346
1347 case WebView.HitTestResult.EMAIL_TYPE:
1348 menu.setHeaderTitle(extra);
1349 menu.findItem(R.id.email_context_menu_id).setIntent(
1350 new Intent(Intent.ACTION_VIEW, Uri
1351 .parse(WebView.SCHEME_MAILTO + extra)));
1352 menu.findItem(R.id.copy_mail_context_menu_id)
1353 .setOnMenuItemClickListener(
1354 new Copy(extra));
1355 break;
1356
1357 case WebView.HitTestResult.GEO_TYPE:
1358 menu.setHeaderTitle(extra);
1359 menu.findItem(R.id.map_context_menu_id).setIntent(
1360 new Intent(Intent.ACTION_VIEW, Uri
1361 .parse(WebView.SCHEME_GEO
1362 + URLEncoder.encode(extra))));
1363 menu.findItem(R.id.copy_geo_context_menu_id)
1364 .setOnMenuItemClickListener(
1365 new Copy(extra));
1366 break;
1367
1368 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1369 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001370 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001371 // decide whether to show the open link in new tab option
1372 boolean showNewTab = mTabControl.canCreateNewTab();
1373 MenuItem newTabItem
1374 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001375 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001376 ? R.string.contextmenu_openlink_newwindow_background
1377 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001378 newTabItem.setVisible(showNewTab);
1379 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001380 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1381 newTabItem.setOnMenuItemClickListener(
1382 new MenuItem.OnMenuItemClickListener() {
1383 @Override
1384 public boolean onMenuItemClick(MenuItem item) {
1385 final HashMap<String, WebView> hrefMap =
1386 new HashMap<String, WebView>();
1387 hrefMap.put("webview", webview);
1388 final Message msg = mHandler.obtainMessage(
1389 FOCUS_NODE_HREF,
1390 R.id.open_newtab_context_menu_id,
1391 0, hrefMap);
1392 webview.requestFocusNodeHref(msg);
1393 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001395 });
1396 } else {
1397 newTabItem.setOnMenuItemClickListener(
1398 new MenuItem.OnMenuItemClickListener() {
1399 @Override
1400 public boolean onMenuItemClick(MenuItem item) {
1401 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001402 openTab(extra, parent,
1403 !mSettings.openInBackground(),
1404 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001405 return true;
1406 }
1407 });
1408 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1411 break;
1412 }
1413 // otherwise fall through to handle image part
1414 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001415 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1416 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1418 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001419 shareItem.setOnMenuItemClickListener(
1420 new MenuItem.OnMenuItemClickListener() {
1421 @Override
1422 public boolean onMenuItemClick(MenuItem item) {
1423 sharePage(mActivity, null, extra, null,
1424 null);
1425 return true;
1426 }
1427 }
1428 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001429 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001430 menu.findItem(R.id.view_image_context_menu_id)
1431 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1432 @Override
1433 public boolean onMenuItemClick(MenuItem item) {
1434 openTab(extra, mTabControl.getCurrentTab(), true, true);
1435 return false;
1436 }
1437 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001438 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1439 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1440 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001441 menu.findItem(R.id.set_wallpaper_context_menu_id).
1442 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1443 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001444 break;
1445
1446 default:
1447 Log.w(LOGTAG, "We should not get here.");
1448 break;
1449 }
1450 //update the ui
1451 mUi.onContextMenuCreated(menu);
1452 }
1453
1454 /**
1455 * As the menu can be open when loading state changes
1456 * we must manually update the state of the stop/reload menu
1457 * item
1458 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001459 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001460 if (menu == null) {
1461 return;
1462 }
1463 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001464 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001465 menu.findItem(R.id.stop_menu_id):
1466 menu.findItem(R.id.reload_menu_id);
1467 if (src != null) {
1468 dest.setIcon(src.getIcon());
1469 dest.setTitle(src.getTitle());
1470 }
1471 }
1472
John Reck9c35b9c2012-05-30 10:08:50 -07001473 @Override
1474 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001475 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001476 // hold on to the menu reference here; it is used by the page callbacks
1477 // to update the menu based on loading state
1478 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001479 // Note: setVisible will decide whether an item is visible; while
1480 // setEnabled() will decide whether an item is enabled, which also means
1481 // whether the matching shortcut key will function.
1482 switch (mMenuState) {
1483 case EMPTY_MENU:
1484 if (mCurrentMenuState != mMenuState) {
1485 menu.setGroupVisible(R.id.MAIN_MENU, false);
1486 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1487 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1488 }
1489 break;
1490 default:
1491 if (mCurrentMenuState != mMenuState) {
1492 menu.setGroupVisible(R.id.MAIN_MENU, true);
1493 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1494 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1495 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001496 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001497 break;
1498 }
1499 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001500 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001501 }
1502
Michael Kolb4bf79712011-07-14 14:18:12 -07001503 @Override
1504 public void updateMenuState(Tab tab, Menu menu) {
1505 boolean canGoBack = false;
1506 boolean canGoForward = false;
1507 boolean isHome = false;
John Reck42229bc2011-08-19 13:26:43 -07001508 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001509 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001510 if (tab != null) {
1511 canGoBack = tab.canGoBack();
1512 canGoForward = tab.canGoForward();
1513 isHome = mSettings.getHomePage().equals(tab.getUrl());
John Reck42229bc2011-08-19 13:26:43 -07001514 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001515 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001516 }
1517 final MenuItem back = menu.findItem(R.id.back_menu_id);
1518 back.setEnabled(canGoBack);
1519
1520 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1521 home.setEnabled(!isHome);
1522
1523 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1524 forward.setEnabled(canGoForward);
1525
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001526 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1527 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001528 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001529 if (source != null && dest != null) {
1530 dest.setTitle(source.getTitle());
1531 dest.setIcon(source.getIcon());
1532 }
John Recke1a03a32011-09-14 17:04:16 -07001533 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001534
1535 // decide whether to show the share link option
1536 PackageManager pm = mActivity.getPackageManager();
1537 Intent send = new Intent(Intent.ACTION_SEND);
1538 send.setType("text/plain");
1539 ResolveInfo ri = pm.resolveActivity(send,
1540 PackageManager.MATCH_DEFAULT_ONLY);
1541 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1542
1543 boolean isNavDump = mSettings.enableNavDump();
1544 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1545 nav.setVisible(isNavDump);
1546 nav.setEnabled(isNavDump);
1547
1548 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001549 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1550 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001551 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1552 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Michael Kolb315d5022011-10-13 12:47:11 -07001553 menu.setGroupVisible(R.id.COMBO_MENU, false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001554
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001555 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001556 }
1557
John Reck9c35b9c2012-05-30 10:08:50 -07001558 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001560 if (null == getCurrentTopWebView()) {
1561 return false;
1562 }
1563 if (mMenuIsDown) {
1564 // The shortcut action consumes the MENU. Even if it is still down,
1565 // it won't trigger the next shortcut action. In the case of the
1566 // shortcut action triggering a new activity, like Bookmarks, we
1567 // won't get onKeyUp for MENU. So it is important to reset it here.
1568 mMenuIsDown = false;
1569 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001570 if (mUi.onOptionsItemSelected(item)) {
1571 // ui callback handled it
1572 return true;
1573 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001574 switch (item.getItemId()) {
1575 // -- Main menu
1576 case R.id.new_tab_menu_id:
1577 openTabToHomePage();
1578 break;
1579
1580 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001581 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001582 break;
1583
Afzal Najamd4e33312012-04-26 01:54:01 -04001584 case R.id.close_other_tabs_id:
1585 closeOtherTabs();
1586 break;
1587
Michael Kolb8233fac2010-10-26 16:08:53 -07001588 case R.id.goto_menu_id:
1589 editUrl();
1590 break;
1591
1592 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001593 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1594 break;
1595
1596 case R.id.history_menu_id:
1597 bookmarksOrHistoryPicker(ComboViews.History);
1598 break;
1599
1600 case R.id.snapshots_menu_id:
1601 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 break;
1603
Michael Kolb8233fac2010-10-26 16:08:53 -07001604 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001605 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001606 break;
1607
1608 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001609 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001610 stopLoading();
1611 } else {
1612 getCurrentTopWebView().reload();
1613 }
1614 break;
1615
1616 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001617 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 break;
1619
1620 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001621 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001622 break;
1623
1624 case R.id.close_menu_id:
1625 // Close the subwindow if it exists.
1626 if (mTabControl.getCurrentSubWindow() != null) {
1627 dismissSubWindow(mTabControl.getCurrentTab());
1628 break;
1629 }
1630 closeCurrentTab();
1631 break;
1632
1633 case R.id.homepage_menu_id:
1634 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001635 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001636 break;
1637
1638 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001639 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001640 break;
1641
1642 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001643 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001644 break;
1645
John Reck2bc80422011-06-30 15:11:49 -07001646 case R.id.save_snapshot_menu_id:
1647 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001648 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001649 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001650 break;
1651
Michael Kolb8233fac2010-10-26 16:08:53 -07001652 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001653 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001654 break;
1655
John Recke1a03a32011-09-14 17:04:16 -07001656 case R.id.snapshot_go_live:
1657 goLive();
1658 return true;
1659
Michael Kolb8233fac2010-10-26 16:08:53 -07001660 case R.id.share_page_menu_id:
1661 Tab currentTab = mTabControl.getCurrentTab();
1662 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001663 return false;
1664 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001665 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001666 break;
1667
1668 case R.id.dump_nav_menu_id:
1669 getCurrentTopWebView().debugDump();
1670 break;
1671
Michael Kolb8233fac2010-10-26 16:08:53 -07001672 case R.id.zoom_in_menu_id:
1673 getCurrentTopWebView().zoomIn();
1674 break;
1675
1676 case R.id.zoom_out_menu_id:
1677 getCurrentTopWebView().zoomOut();
1678 break;
1679
1680 case R.id.view_downloads_menu_id:
1681 viewDownloads();
1682 break;
1683
John Reck42229bc2011-08-19 13:26:43 -07001684 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001685 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001686 break;
1687
Michael Kolb8233fac2010-10-26 16:08:53 -07001688 case R.id.window_one_menu_id:
1689 case R.id.window_two_menu_id:
1690 case R.id.window_three_menu_id:
1691 case R.id.window_four_menu_id:
1692 case R.id.window_five_menu_id:
1693 case R.id.window_six_menu_id:
1694 case R.id.window_seven_menu_id:
1695 case R.id.window_eight_menu_id:
1696 {
1697 int menuid = item.getItemId();
1698 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1699 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1700 Tab desiredTab = mTabControl.getTab(id);
1701 if (desiredTab != null &&
1702 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001703 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001704 }
1705 break;
1706 }
1707 }
1708 }
1709 break;
1710
1711 default:
1712 return false;
1713 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001714 return true;
1715 }
1716
John Reck68234a92012-04-19 15:27:12 -07001717 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1718 implements OnCancelListener {
1719
1720 private Tab mTab;
1721 private Dialog mProgressDialog;
1722 private ContentValues mValues;
1723
1724 private SaveSnapshotTask(Tab tab) {
1725 mTab = tab;
1726 }
1727
1728 @Override
1729 protected void onPreExecute() {
1730 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1731 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1732 true, true, this);
1733 mValues = mTab.createSnapshotValues();
1734 }
1735
1736 @Override
1737 protected Long doInBackground(Void... params) {
1738 if (!mTab.saveViewState(mValues)) {
1739 return null;
1740 }
1741 if (isCancelled()) {
1742 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1743 File file = mActivity.getFileStreamPath(path);
1744 if (!file.delete()) {
1745 file.deleteOnExit();
1746 }
1747 return null;
1748 }
1749 final ContentResolver cr = mActivity.getContentResolver();
1750 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1751 if (result == null) {
1752 return null;
1753 }
1754 long id = ContentUris.parseId(result);
1755 return id;
1756 }
1757
1758 @Override
1759 protected void onPostExecute(Long id) {
1760 if (isCancelled()) {
1761 return;
1762 }
1763 mProgressDialog.dismiss();
1764 if (id == null) {
1765 Toast.makeText(mActivity, R.string.snapshot_failed,
1766 Toast.LENGTH_SHORT).show();
1767 return;
1768 }
1769 Bundle b = new Bundle();
1770 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1771 mUi.showComboView(ComboViews.Snapshots, b);
1772 }
1773
1774 @Override
1775 public void onCancel(DialogInterface dialog) {
1776 cancel(true);
1777 }
1778 }
1779
Michael Kolb80f75082012-04-10 10:50:06 -07001780 @Override
1781 public void toggleUserAgent() {
1782 WebView web = getCurrentWebView();
1783 mSettings.toggleDesktopUseragent(web);
1784 web.loadUrl(web.getOriginalUrl());
1785 }
1786
1787 @Override
1788 public void findOnPage() {
1789 getCurrentTopWebView().showFindDialog(null, true);
1790 }
1791
1792 @Override
1793 public void openPreferences() {
1794 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1795 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1796 getCurrentTopWebView().getUrl());
1797 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1798 }
1799
1800 @Override
1801 public void bookmarkCurrentPage() {
1802 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1803 if (bookmarkIntent != null) {
1804 mActivity.startActivity(bookmarkIntent);
1805 }
1806 }
1807
John Recke1a03a32011-09-14 17:04:16 -07001808 private void goLive() {
1809 Tab t = getCurrentTab();
1810 t.loadUrl(t.getUrl(), null);
1811 }
1812
Michael Kolb315d5022011-10-13 12:47:11 -07001813 @Override
1814 public void showPageInfo() {
1815 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1816 }
1817
John Reck9c35b9c2012-05-30 10:08:50 -07001818 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001819 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001820 // Let the History and Bookmark fragments handle menus they created.
1821 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1822 return false;
1823 }
1824
Michael Kolb8233fac2010-10-26 16:08:53 -07001825 int id = item.getItemId();
1826 boolean result = true;
1827 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001828 // -- Browser context menu
1829 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001830 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001831 case R.id.copy_link_context_menu_id:
1832 final WebView webView = getCurrentTopWebView();
1833 if (null == webView) {
1834 result = false;
1835 break;
1836 }
1837 final HashMap<String, WebView> hrefMap =
1838 new HashMap<String, WebView>();
1839 hrefMap.put("webview", webView);
1840 final Message msg = mHandler.obtainMessage(
1841 FOCUS_NODE_HREF, id, 0, hrefMap);
1842 webView.requestFocusNodeHref(msg);
1843 break;
1844
1845 default:
1846 // For other context menus
1847 result = onOptionsItemSelected(item);
1848 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001849 return result;
1850 }
1851
1852 /**
1853 * support programmatically opening the context menu
1854 */
1855 public void openContextMenu(View view) {
1856 mActivity.openContextMenu(view);
1857 }
1858
1859 /**
1860 * programmatically open the options menu
1861 */
1862 public void openOptionsMenu() {
1863 mActivity.openOptionsMenu();
1864 }
1865
John Reck9c35b9c2012-05-30 10:08:50 -07001866 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001867 public boolean onMenuOpened(int featureId, Menu menu) {
1868 if (mOptionsMenuOpen) {
1869 if (mConfigChanged) {
1870 // We do not need to make any changes to the state of the
1871 // title bar, since the only thing that happened was a
1872 // change in orientation
1873 mConfigChanged = false;
1874 } else {
1875 if (!mExtendedMenuOpen) {
1876 mExtendedMenuOpen = true;
1877 mUi.onExtendedMenuOpened();
1878 } else {
1879 // Switching the menu back to icon view, so show the
1880 // title bar once again.
1881 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001882 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001883 }
1884 }
1885 } else {
1886 // The options menu is closed, so open it, and show the title
1887 mOptionsMenuOpen = true;
1888 mConfigChanged = false;
1889 mExtendedMenuOpen = false;
1890 mUi.onOptionsMenuOpened();
1891 }
1892 return true;
1893 }
1894
John Reck9c35b9c2012-05-30 10:08:50 -07001895 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001896 public void onOptionsMenuClosed(Menu menu) {
1897 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001898 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001899 }
1900
John Reck9c35b9c2012-05-30 10:08:50 -07001901 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001902 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001903 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001904 }
1905
1906 // Helper method for getting the top window.
1907 @Override
1908 public WebView getCurrentTopWebView() {
1909 return mTabControl.getCurrentTopWebView();
1910 }
1911
1912 @Override
1913 public WebView getCurrentWebView() {
1914 return mTabControl.getCurrentWebView();
1915 }
1916
1917 /*
1918 * This method is called as a result of the user selecting the options
1919 * menu to see the download window. It shows the download window on top of
1920 * the current window.
1921 */
1922 void viewDownloads() {
1923 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1924 mActivity.startActivity(intent);
1925 }
1926
John Reck30b065e2011-07-19 10:58:05 -07001927 int getActionModeHeight() {
1928 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1929 new int[] { android.R.attr.actionBarSize });
1930 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1931 actionBarSizeTypedArray.recycle();
1932 return size;
1933 }
1934
Michael Kolb8233fac2010-10-26 16:08:53 -07001935 // action mode
1936
John Reck9c35b9c2012-05-30 10:08:50 -07001937 @Override
1938 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001939 mUi.onActionModeStarted(mode);
1940 mActionMode = mode;
1941 }
1942
1943 /*
1944 * True if a custom ActionMode (i.e. find or select) is in use.
1945 */
1946 @Override
1947 public boolean isInCustomActionMode() {
1948 return mActionMode != null;
1949 }
1950
1951 /*
1952 * End the current ActionMode.
1953 */
1954 @Override
1955 public void endActionMode() {
1956 if (mActionMode != null) {
1957 mActionMode.finish();
1958 }
1959 }
1960
1961 /*
1962 * Called by find and select when they are finished. Replace title bars
1963 * as necessary.
1964 */
John Reck9c35b9c2012-05-30 10:08:50 -07001965 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001966 public void onActionModeFinished(ActionMode mode) {
1967 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001968 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001969 mActionMode = null;
1970 }
1971
1972 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001973 final Tab tab = getCurrentTab();
1974 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07001975 }
1976
1977 // bookmark handling
1978
1979 /**
1980 * add the current page as a bookmark to the given folder id
1981 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001982 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001983 * bookmarked, and if it is, edit that bookmark. If false, and
1984 * the site is already bookmarked, do not attempt to edit the
1985 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001986 */
1987 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001988 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07001989 WebView w = getCurrentTopWebView();
1990 if (w == null) {
1991 return null;
1992 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001993 Intent i = new Intent(mActivity,
1994 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07001995 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1996 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1997 String touchIconUrl = w.getTouchIconUrl();
1998 if (touchIconUrl != null) {
1999 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2000 WebSettings settings = w.getSettings();
2001 if (settings != null) {
2002 i.putExtra(AddBookmarkPage.USER_AGENT,
2003 settings.getUserAgentString());
2004 }
2005 }
2006 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2007 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2008 getDesiredThumbnailHeight(mActivity)));
2009 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002010 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002011 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2012 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002013 // Put the dialog at the upper right of the screen, covering the
2014 // star on the title bar.
2015 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002016 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002017 }
2018
2019 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002020 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002021 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002022 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002023 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002024 }
2025
2026 // thumbnails
2027
2028 /**
2029 * Return the desired width for thumbnail screenshots, which are stored in
2030 * the database, and used on the bookmarks screen.
2031 * @param context Context for finding out the density of the screen.
2032 * @return desired width for thumbnail screenshot.
2033 */
2034 static int getDesiredThumbnailWidth(Context context) {
2035 return context.getResources().getDimensionPixelOffset(
2036 R.dimen.bookmarkThumbnailWidth);
2037 }
2038
2039 /**
2040 * Return the desired height for thumbnail screenshots, which are stored in
2041 * the database, and used on the bookmarks screen.
2042 * @param context Context for finding out the density of the screen.
2043 * @return desired height for thumbnail screenshot.
2044 */
2045 static int getDesiredThumbnailHeight(Context context) {
2046 return context.getResources().getDimensionPixelOffset(
2047 R.dimen.bookmarkThumbnailHeight);
2048 }
2049
John Reck8cc92352011-07-06 17:41:52 -07002050 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002051 if (view == null || view.getContentHeight() == 0
2052 || view.getContentWidth() == 0) {
2053 return null;
2054 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002055 // We render to a bitmap 2x the desired size so that we can then
2056 // re-scale it with filtering since canvas.scale doesn't filter
2057 // This helps reduce aliasing at the cost of being slightly blurry
2058 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002059 int scaledWidth = width * filter_scale;
2060 int scaledHeight = height * filter_scale;
2061 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2062 || sThumbnailBitmap.getHeight() != scaledHeight) {
2063 if (sThumbnailBitmap != null) {
2064 sThumbnailBitmap.recycle();
2065 sThumbnailBitmap = null;
2066 }
2067 sThumbnailBitmap =
2068 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 }
John Reckd7dd9b22011-08-30 09:18:29 -07002070 Canvas canvas = new Canvas(sThumbnailBitmap);
2071 int contentWidth = view.getContentWidth();
2072 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2073 if (view instanceof BrowserWebView) {
2074 int dy = -((BrowserWebView)view).getTitleHeight();
2075 canvas.translate(0, dy * overviewScale);
2076 }
2077
2078 canvas.scale(overviewScale, overviewScale);
2079
2080 if (view instanceof BrowserWebView) {
2081 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002082 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002083 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 }
John Reckd7dd9b22011-08-30 09:18:29 -07002085 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2086 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002087 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002088 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002089 }
2090
John Reck34ef2672011-02-10 11:30:55 -08002091 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002092 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002093 // FIXME: Would like to make sure there is actually something to
2094 // draw, but the API for that (WebViewCore.pictureReady()) is not
2095 // currently accessible here.
2096
John Reck34ef2672011-02-10 11:30:55 -08002097 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002098 if (view == null) {
2099 // Tab was destroyed
2100 return;
2101 }
John Reck34ef2672011-02-10 11:30:55 -08002102 final String url = tab.getUrl();
2103 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002104 if (TextUtils.isEmpty(url)) {
2105 return;
2106 }
2107
2108 // Only update thumbnails for web urls (http(s)://), not for
2109 // about:, javascript:, data:, etc...
2110 // Unless it is a bookmarked site, then always update
2111 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2112 return;
2113 }
2114
Michael Kolb8233fac2010-10-26 16:08:53 -07002115 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2116 getDesiredThumbnailHeight(mActivity));
2117 if (bm == null) {
2118 return;
2119 }
2120
2121 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002122 new AsyncTask<Void, Void, Void>() {
2123 @Override
2124 protected Void doInBackground(Void... unused) {
2125 Cursor cursor = null;
2126 try {
2127 // TODO: Clean this up
2128 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2129 if (cursor != null && cursor.moveToFirst()) {
2130 final ByteArrayOutputStream os =
2131 new ByteArrayOutputStream();
2132 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002133
John Reck34ef2672011-02-10 11:30:55 -08002134 ContentValues values = new ContentValues();
2135 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002136
John Reck34ef2672011-02-10 11:30:55 -08002137 do {
John Reck617fd832011-02-16 14:35:59 -08002138 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002139 cr.update(Images.CONTENT_URI, values, null, null);
2140 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002141 }
John Reck34ef2672011-02-10 11:30:55 -08002142 } catch (IllegalStateException e) {
2143 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002144 } catch (SQLiteException s) {
2145 // Added for possible error when user tries to remove the same bookmark
2146 // that is being updated with a screen shot
2147 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002148 } finally {
2149 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002150 }
John Reck34ef2672011-02-10 11:30:55 -08002151 return null;
2152 }
2153 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 }
2155
2156 private class Copy implements OnMenuItemClickListener {
2157 private CharSequence mText;
2158
John Reck9c35b9c2012-05-30 10:08:50 -07002159 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002160 public boolean onMenuItemClick(MenuItem item) {
2161 copy(mText);
2162 return true;
2163 }
2164
2165 public Copy(CharSequence toCopy) {
2166 mText = toCopy;
2167 }
2168 }
2169
Leon Scroggins63c02662010-11-18 15:16:27 -05002170 private static class Download implements OnMenuItemClickListener {
2171 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002172 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002173 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002174 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002175 private static final String FALLBACK_EXTENSION = "dat";
2176 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002177
John Reck9c35b9c2012-05-30 10:08:50 -07002178 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002179 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002180 if (DataUri.isDataUri(mText)) {
2181 saveDataUri();
2182 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002183 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07002184 null, null, null, mPrivateBrowsing);
George Mount387d45d2011-10-07 15:57:53 -07002185 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002186 return true;
2187 }
2188
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002189 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2190 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002191 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002192 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002193 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002194 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002195 }
George Mount387d45d2011-10-07 15:57:53 -07002196
2197 /**
2198 * Treats mText as a data URI and writes its contents to a file
2199 * based on the current time.
2200 */
2201 private void saveDataUri() {
2202 FileOutputStream outputStream = null;
2203 try {
2204 DataUri uri = new DataUri(mText);
2205 File target = getTarget(uri);
2206 outputStream = new FileOutputStream(target);
2207 outputStream.write(uri.getData());
2208 final DownloadManager manager =
2209 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2210 manager.addCompletedDownload(target.getName(),
2211 mActivity.getTitle().toString(), false,
2212 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002213 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002214 } catch (IOException e) {
2215 Log.e(LOGTAG, "Could not save data URL");
2216 } finally {
2217 if (outputStream != null) {
2218 try {
2219 outputStream.close();
2220 } catch (IOException e) {
2221 // ignore close errors
2222 }
2223 }
2224 }
2225 }
2226
2227 /**
2228 * Creates a File based on the current time stamp and uses
2229 * the mime type of the DataUri to get the extension.
2230 */
2231 private File getTarget(DataUri uri) throws IOException {
2232 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002233 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002234 String nameBase = format.format(new Date());
2235 String mimeType = uri.getMimeType();
2236 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2237 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2238 if (extension == null) {
2239 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2240 extension = FALLBACK_EXTENSION;
2241 }
2242 extension = "." + extension; // createTempFile needs the '.'
2243 File targetFile = File.createTempFile(nameBase, extension, dir);
2244 return targetFile;
2245 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002246 }
2247
Cary Clark8974d282010-11-22 10:46:05 -05002248 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002249 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002250
John Reck9c35b9c2012-05-30 10:08:50 -07002251 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002252 public boolean onMenuItemClick(MenuItem item) {
2253 if (mWebView != null) {
2254 return mWebView.selectText();
2255 }
2256 return false;
2257 }
2258
2259 public SelectText(WebView webView) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002260 mWebView = WebViewClassic.fromWebView(webView);
Cary Clark8974d282010-11-22 10:46:05 -05002261 }
2262
2263 }
2264
Michael Kolb8233fac2010-10-26 16:08:53 -07002265 /********************** TODO: UI stuff *****************************/
2266
2267 // these methods have been copied, they still need to be cleaned up
2268
2269 /****************** tabs ***************************************************/
2270
2271 // basic tab interactions:
2272
2273 // it is assumed that tabcontrol already knows about the tab
2274 protected void addTab(Tab tab) {
2275 mUi.addTab(tab);
2276 }
2277
2278 protected void removeTab(Tab tab) {
2279 mUi.removeTab(tab);
2280 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002281 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002282 }
2283
Michael Kolbf2055602011-04-09 17:20:03 -07002284 @Override
2285 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002286 // monkey protection against delayed start
2287 if (tab != null) {
2288 mTabControl.setCurrentTab(tab);
2289 // the tab is guaranteed to have a webview after setCurrentTab
2290 mUi.setActiveTab(tab);
2291 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002292 }
2293
John Reck8bcafc12011-08-29 16:43:02 -07002294 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002295 Tab current = mTabControl.getCurrentTab();
2296 if (current != null
2297 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002298 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002299 }
2300 }
2301
John Reck26b18322011-06-21 13:08:58 -07002302 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002303 // Dismiss the subwindow if applicable.
2304 dismissSubWindow(appTab);
2305 // Since we might kill the WebView, remove it from the
2306 // content view first.
2307 mUi.detachTab(appTab);
2308 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002309 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002310 // TODO: analyze why the remove and add are necessary
2311 mUi.attachTab(appTab);
2312 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002313 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002314 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002315 } else {
2316 // If the tab was the current tab, we have to attach
2317 // it to the view system again.
2318 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002319 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002320 }
2321 }
2322
2323 // Remove the sub window if it exists. Also called by TabControl when the
2324 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002325 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002326 public void dismissSubWindow(Tab tab) {
2327 removeSubWindow(tab);
2328 // dismiss the subwindow. This will destroy the WebView.
2329 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002330 WebView wv = getCurrentTopWebView();
2331 if (wv != null) {
2332 wv.requestFocus();
2333 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002334 }
2335
2336 @Override
2337 public void removeSubWindow(Tab t) {
2338 if (t.getSubWebView() != null) {
2339 mUi.removeSubWindow(t.getSubViewContainer());
2340 }
2341 }
2342
2343 @Override
2344 public void attachSubWindow(Tab tab) {
2345 if (tab.getSubWebView() != null) {
2346 mUi.attachSubWindow(tab.getSubViewContainer());
2347 getCurrentTopWebView().requestFocus();
2348 }
2349 }
2350
Mathew Inwood29721c22011-06-29 17:55:24 +01002351 private Tab showPreloadedTab(final UrlData urlData) {
2352 if (!urlData.isPreloaded()) {
2353 return null;
2354 }
2355 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2356 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2357 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002358 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002359 // Could not submit query. Fallback to regular tab creation
2360 tabControl.destroy();
2361 return null;
2362 }
2363 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002364 // check tab count and make room for new tab
2365 if (!mTabControl.canCreateNewTab()) {
2366 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2367 if (leastUsed != null) {
2368 closeTab(leastUsed);
2369 }
2370 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002371 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002372 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002373 mTabControl.addPreloadedTab(t);
2374 addTab(t);
2375 setActiveTab(t);
2376 return t;
2377 }
2378
Michael Kolb7bcafde2011-05-09 13:55:59 -07002379 // open a non inconito tab with the given url data
2380 // and set as active tab
2381 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002382 Tab tab = showPreloadedTab(urlData);
2383 if (tab == null) {
2384 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002385 if ((tab != null) && !urlData.isEmpty()) {
2386 loadUrlDataIn(tab, urlData);
2387 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002388 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002389 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002390 }
2391
Michael Kolb843510f2010-12-09 10:51:49 -08002392 @Override
2393 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002394 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002395 }
2396
Michael Kolb8233fac2010-10-26 16:08:53 -07002397 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002398 public Tab openIncognitoTab() {
2399 return openTab(INCOGNITO_URI, true, true, false);
2400 }
2401
2402 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002403 public Tab openTab(String url, boolean incognito, boolean setActive,
2404 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002405 return openTab(url, incognito, setActive, useCurrent, null);
2406 }
2407
2408 @Override
2409 public Tab openTab(String url, Tab parent, boolean setActive,
2410 boolean useCurrent) {
2411 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2412 setActive, useCurrent, parent);
2413 }
2414
2415 public Tab openTab(String url, boolean incognito, boolean setActive,
2416 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002417 Tab tab = createNewTab(incognito, setActive, useCurrent);
2418 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002419 if (parent != null && parent != tab) {
2420 parent.addChildTab(tab);
2421 }
Michael Kolb519d2282011-05-09 17:03:19 -07002422 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002423 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002424 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002425 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002426 return tab;
2427 }
2428
2429 // this method will attempt to create a new tab
2430 // incognito: private browsing tab
2431 // setActive: ste tab as current tab
2432 // useCurrent: if no new tab can be created, return current tab
2433 private Tab createNewTab(boolean incognito, boolean setActive,
2434 boolean useCurrent) {
2435 Tab tab = null;
2436 if (mTabControl.canCreateNewTab()) {
2437 tab = mTabControl.createNewTab(incognito);
2438 addTab(tab);
2439 if (setActive) {
2440 setActiveTab(tab);
2441 }
2442 } else {
2443 if (useCurrent) {
2444 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002445 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002446 } else {
2447 mUi.showMaxTabsWarning();
2448 }
2449 }
2450 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002451 }
2452
John Reck2bc80422011-06-30 15:11:49 -07002453 @Override
2454 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2455 SnapshotTab tab = null;
2456 if (mTabControl.canCreateNewTab()) {
2457 tab = mTabControl.createSnapshotTab(snapshotId);
2458 addTab(tab);
2459 if (setActive) {
2460 setActiveTab(tab);
2461 }
2462 } else {
2463 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002464 }
2465 return tab;
2466 }
2467
Michael Kolb8233fac2010-10-26 16:08:53 -07002468 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002469 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002470 * @return boolean True if we successfully switched to a different tab. If
2471 * the indexth tab is null, or if that tab is the same as
2472 * the current one, return false.
2473 */
2474 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002475 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002476 Tab currentTab = mTabControl.getCurrentTab();
2477 if (tab == null || tab == currentTab) {
2478 return false;
2479 }
2480 setActiveTab(tab);
2481 return true;
2482 }
2483
2484 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002485 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002486 closeCurrentTab(false);
2487 }
2488
2489 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002490 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002491 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002492 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002493 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002494 return;
2495 }
Michael Kolbc831b632011-05-11 09:30:34 -07002496 final Tab current = mTabControl.getCurrentTab();
2497 final int pos = mTabControl.getCurrentPosition();
2498 Tab newTab = current.getParent();
2499 if (newTab == null) {
2500 newTab = mTabControl.getTab(pos + 1);
2501 if (newTab == null) {
2502 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002503 }
2504 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002505 if (andQuit) {
2506 mTabControl.setCurrentTab(newTab);
2507 closeTab(current);
2508 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002509 // Close window
2510 closeTab(current);
2511 }
2512 }
2513
2514 /**
2515 * Close the tab, remove its associated title bar, and adjust mTabControl's
2516 * current tab to a valid value.
2517 */
2518 @Override
2519 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002520 if (tab == mTabControl.getCurrentTab()) {
2521 closeCurrentTab();
2522 } else {
2523 removeTab(tab);
2524 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 }
2526
Afzal Najamd4e33312012-04-26 01:54:01 -04002527 /**
2528 * Close all tabs except the current one
2529 */
2530 @Override
2531 public void closeOtherTabs() {
2532 int inactiveTabs = mTabControl.getTabCount() - 1;
2533 for (int i = inactiveTabs; i >= 0; i--) {
2534 Tab tab = mTabControl.getTab(i);
2535 if (tab != mTabControl.getCurrentTab()) {
2536 removeTab(tab);
2537 }
2538 }
2539 }
2540
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002542 protected void loadUrlFromContext(String url) {
2543 Tab tab = getCurrentTab();
2544 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002545 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002546 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002547 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002548 if (!WebViewClassic.fromWebView(view).getWebViewClient().
2549 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002550 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002551 }
2552 }
2553 }
2554
2555 /**
2556 * Load the URL into the given WebView and update the title bar
2557 * to reflect the new load. Call this instead of WebView.loadUrl
2558 * directly.
2559 * @param view The WebView used to load url.
2560 * @param url The URL to load.
2561 */
John Reck71e51422011-07-01 16:49:28 -07002562 @Override
2563 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002564 loadUrl(tab, url, null);
2565 }
2566
2567 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2568 if (tab != null) {
2569 dismissSubWindow(tab);
2570 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002571 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002572 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002573 }
2574
2575 /**
2576 * Load UrlData into a Tab and update the title bar to reflect the new
2577 * load. Call this instead of UrlData.loadIn directly.
2578 * @param t The Tab used to load.
2579 * @param data The UrlData being loaded.
2580 */
2581 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002582 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002583 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002584 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002585 } else {
John Reck38b39652012-06-05 09:22:59 -07002586 if (t != null && data.mDisableUrlOverride) {
2587 t.disableUrlOverridingForLoad();
2588 }
John Reck26b18322011-06-21 13:08:58 -07002589 loadUrl(t, data.mUrl, data.mHeaders);
2590 }
2591 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002592 }
2593
John Reck30c714c2010-12-16 17:30:34 -08002594 @Override
2595 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002596 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002597 if (tab.canGoBack()) {
2598 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002599 } else {
John Reckef654f12011-07-12 16:42:08 -07002600 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002601 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 }
2603
2604 void goBackOnePageOrQuit() {
2605 Tab current = mTabControl.getCurrentTab();
2606 if (current == null) {
2607 /*
2608 * Instead of finishing the activity, simply push this to the back
2609 * of the stack and let ActivityManager to choose the foreground
2610 * activity. As BrowserActivity is singleTask, it will be always the
2611 * root of the task. So we can use either true or false for
2612 * moveTaskToBack().
2613 */
2614 mActivity.moveTaskToBack(true);
2615 return;
2616 }
John Reckef654f12011-07-12 16:42:08 -07002617 if (current.canGoBack()) {
2618 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 } else {
2620 // Check to see if we are closing a window that was created by
2621 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002622 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002623 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002624 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002625 // Now we close the other tab
2626 closeTab(current);
2627 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002628 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002629 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002630 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002631 /*
2632 * Instead of finishing the activity, simply push this to the back
2633 * of the stack and let ActivityManager to choose the foreground
2634 * activity. As BrowserActivity is singleTask, it will be always the
2635 * root of the task. So we can use either true or false for
2636 * moveTaskToBack().
2637 */
2638 mActivity.moveTaskToBack(true);
2639 }
2640 }
2641 }
2642
2643 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002644 * helper method for key handler
2645 * returns the current tab if it can't advance
2646 */
Michael Kolbc831b632011-05-11 09:30:34 -07002647 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002648 int pos = mTabControl.getCurrentPosition() + 1;
2649 if (pos >= mTabControl.getTabCount()) {
2650 pos = 0;
2651 }
2652 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002653 }
2654
2655 /**
2656 * helper method for key handler
2657 * returns the current tab if it can't advance
2658 */
Michael Kolbc831b632011-05-11 09:30:34 -07002659 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002660 int pos = mTabControl.getCurrentPosition() - 1;
2661 if ( pos < 0) {
2662 pos = mTabControl.getTabCount() - 1;
2663 }
2664 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002665 }
2666
John Reckbcef87f2012-02-03 14:58:34 -08002667 boolean isMenuOrCtrlKey(int keyCode) {
2668 return (KeyEvent.KEYCODE_MENU == keyCode)
2669 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2670 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2671 }
2672
Michael Kolb0035fad2011-03-14 13:25:28 -07002673 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002674 * handle key events in browser
2675 *
2676 * @param keyCode
2677 * @param event
2678 * @return true if handled, false to pass to super
2679 */
John Reck9c35b9c2012-05-30 10:08:50 -07002680 @Override
2681 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002682 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002683 // Even if MENU is already held down, we need to call to super to open
2684 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002685 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002686 mMenuIsDown = true;
2687 return false;
2688 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002689
Cary Clark8ff8c662010-12-29 15:03:05 -05002690 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002691 Tab tab = getCurrentTab();
2692 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002693
Cary Clark160bbb92011-01-10 11:17:07 -05002694 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2695 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002696
Michael Kolb8233fac2010-10-26 16:08:53 -07002697 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002698 case KeyEvent.KEYCODE_TAB:
2699 if (event.isCtrlPressed()) {
2700 if (event.isShiftPressed()) {
2701 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002702 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002703 } else {
2704 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002705 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002706 }
2707 return true;
2708 }
2709 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002710 case KeyEvent.KEYCODE_SPACE:
2711 // WebView/WebTextView handle the keys in the KeyDown. As
2712 // the Activity's shortcut keys are only handled when WebView
2713 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002714 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002715 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002716 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002717 pageDown();
2718 }
2719 return true;
2720 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002721 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002722 event.startTracking();
2723 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002724 case KeyEvent.KEYCODE_FORWARD:
2725 if (!noModifiers) break;
2726 tab.goForward();
2727 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002728 case KeyEvent.KEYCODE_DPAD_LEFT:
2729 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002730 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002731 return true;
2732 }
2733 break;
2734 case KeyEvent.KEYCODE_DPAD_RIGHT:
2735 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002736 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002737 return true;
2738 }
2739 break;
2740 case KeyEvent.KEYCODE_A:
2741 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002742 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002743 return true;
2744 }
2745 break;
Michael Kolba4183062011-01-16 10:43:21 -08002746// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002747 case KeyEvent.KEYCODE_C:
2748 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002749 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002750 return true;
2751 }
2752 break;
Michael Kolba4183062011-01-16 10:43:21 -08002753// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002754// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002755// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002756// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002757// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002758// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002759// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002760// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002761// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002762// case KeyEvent.KEYCODE_M: // unused
2763// case KeyEvent.KEYCODE_N: // in Chrome: new window
2764// case KeyEvent.KEYCODE_O: // in Chrome: open file
2765// case KeyEvent.KEYCODE_P: // in Chrome: print page
2766// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002767// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002768// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2769 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002770 // we can't use the ctrl/shift flags, they check for
2771 // exclusive use of a modifier
2772 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002773 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002774 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002775 } else {
2776 openTabToHomePage();
2777 }
2778 return true;
2779 }
2780 break;
2781// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2782// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002783// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002784// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2785// case KeyEvent.KEYCODE_Y: // unused
2786// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002787 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002788 // it is a regular key and webview is not null
2789 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002790 }
2791
John Reck9c35b9c2012-05-30 10:08:50 -07002792 @Override
2793 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002794 switch(keyCode) {
2795 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002796 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002797 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002798 return true;
2799 }
2800 break;
2801 }
2802 return false;
2803 }
2804
John Reck9c35b9c2012-05-30 10:08:50 -07002805 @Override
2806 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002807 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002808 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002809 if (KeyEvent.KEYCODE_MENU == keyCode
2810 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002811 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002812 }
2813 }
Cary Clark160bbb92011-01-10 11:17:07 -05002814 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002815 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002816 case KeyEvent.KEYCODE_BACK:
2817 if (event.isTracking() && !event.isCanceled()) {
2818 onBackKey();
2819 return true;
2820 }
2821 break;
2822 }
2823 return false;
2824 }
2825
2826 public boolean isMenuDown() {
2827 return mMenuIsDown;
2828 }
2829
John Reck9c35b9c2012-05-30 10:08:50 -07002830 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002831 public void setupAutoFill(Message message) {
2832 // Open the settings activity at the AutoFill profile fragment so that
2833 // the user can create a new profile. When they return, we will dispatch
2834 // the message so that we can autofill the form using their new profile.
2835 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2836 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2837 AutoFillSettingsFragment.class.getName());
2838 mAutoFillSetupMessage = message;
2839 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2840 }
John Reckb3417f02011-01-14 11:01:05 -08002841
John Reck9c35b9c2012-05-30 10:08:50 -07002842 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002843 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002844 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002845 return true;
2846 }
2847
John Reck1cf4b792011-07-26 10:22:22 -07002848 @Override
2849 public boolean shouldCaptureThumbnails() {
2850 return mUi.shouldCaptureThumbnails();
2851 }
2852
Michael Kolbc3af0672011-08-09 10:24:41 -07002853 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002854 public boolean supportsVoice() {
2855 PackageManager pm = mActivity.getPackageManager();
2856 List activities = pm.queryIntentActivities(new Intent(
2857 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2858 return activities.size() != 0;
2859 }
2860
2861 @Override
2862 public void startVoiceRecognizer() {
2863 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2864 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2865 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2866 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2867 mActivity.startActivityForResult(voice, VOICE_RESULT);
2868 }
2869
2870 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002871 public void setBlockEvents(boolean block) {
2872 mBlockEvents = block;
2873 }
2874
John Reck9c35b9c2012-05-30 10:08:50 -07002875 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002876 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002877 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002878 }
2879
John Reck9c35b9c2012-05-30 10:08:50 -07002880 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002881 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002882 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002883 }
2884
John Reck9c35b9c2012-05-30 10:08:50 -07002885 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002886 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002887 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002888 }
2889
John Reck9c35b9c2012-05-30 10:08:50 -07002890 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002891 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002892 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002893 }
2894
John Reck9c35b9c2012-05-30 10:08:50 -07002895 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002896 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002897 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002898 }
2899
Michael Kolb8233fac2010-10-26 16:08:53 -07002900}