blob: eefeffeed43e6f475115236fe5923d85b049d1d3 [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;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.graphics.Bitmap;
39import android.graphics.Canvas;
Michael Kolb8233fac2010-10-26 16:08:53 -070040import android.net.Uri;
41import android.net.http.SslError;
42import android.os.AsyncTask;
43import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070044import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070045import android.os.Handler;
46import android.os.Message;
47import android.os.PowerManager;
48import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000049import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.provider.Browser;
51import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.provider.BrowserContract.Images;
53import android.provider.ContactsContract;
54import android.provider.ContactsContract.Intents.Insert;
Michael Kolb0b129122012-06-04 16:31:58 -070055import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.text.TextUtils;
57import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080058import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070059import android.view.ActionMode;
60import android.view.ContextMenu;
61import android.view.ContextMenu.ContextMenuInfo;
62import android.view.Gravity;
63import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070064import android.view.Menu;
65import android.view.MenuInflater;
66import android.view.MenuItem;
67import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070068import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070069import android.view.View;
70import android.webkit.CookieManager;
71import android.webkit.CookieSyncManager;
72import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070073import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070074import android.webkit.SslErrorHandler;
75import android.webkit.ValueCallback;
76import android.webkit.WebChromeClient;
77import android.webkit.WebIconDatabase;
78import android.webkit.WebSettings;
79import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000080import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050081import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070082
Michael Kolb4bd767d2011-05-27 11:33:55 -070083import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070084import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070085import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070086import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070087
Michael Kolb8233fac2010-10-26 16:08:53 -070088import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -070089import java.io.File;
90import java.io.FileOutputStream;
91import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070092import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -070093import java.text.DateFormat;
94import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -070095import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -070096import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -070097import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -070098import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080099import java.util.List;
John Reck26b18322011-06-21 13:08:58 -0700100import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700101
102/**
103 * Controller for browser
104 */
105public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700106 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700107
108 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800109 private static final String SEND_APP_ID_EXTRA =
110 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700111 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800112
Michael Kolb8233fac2010-10-26 16:08:53 -0700113
114 // public message ids
115 public final static int LOAD_URL = 1001;
116 public final static int STOP_LOAD = 1002;
117
118 // Message Ids
119 private static final int FOCUS_NODE_HREF = 102;
120 private static final int RELEASE_WAKELOCK = 107;
121
122 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
123
124 private static final int OPEN_BOOKMARKS = 201;
125
126 private static final int EMPTY_MENU = -1;
127
Michael Kolb8233fac2010-10-26 16:08:53 -0700128 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700129 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 final static int PREFERENCES_PAGE = 3;
131 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000132 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700133 final static int VOICE_RESULT = 6;
Ben Murdoch8029a772010-11-16 11:58:21 +0000134
Michael Kolb8233fac2010-10-26 16:08:53 -0700135 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
136
137 // As the ids are dynamically created, we can't guarantee that they will
138 // be in sequence, so this static array maps ids to a window number.
139 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
140 { R.id.window_one_menu_id, R.id.window_two_menu_id,
141 R.id.window_three_menu_id, R.id.window_four_menu_id,
142 R.id.window_five_menu_id, R.id.window_six_menu_id,
143 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
144
145 // "source" parameter for Google search through search key
146 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
147 // "source" parameter for Google search through simplily type
148 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
149
George Mount387d45d2011-10-07 15:57:53 -0700150 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700151 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
152
John Reckd7dd9b22011-08-30 09:18:29 -0700153 // A bitmap that is re-used in createScreenshot as scratch space
154 private static Bitmap sThumbnailBitmap;
155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 private Activity mActivity;
157 private UI mUi;
158 private TabControl mTabControl;
159 private BrowserSettings mSettings;
160 private WebViewFactory mFactory;
161
162 private WakeLock mWakeLock;
163
164 private UrlHandler mUrlHandler;
165 private UploadHandler mUploadHandler;
166 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700167 private PageDialogsHandler mPageDialogsHandler;
168 private NetworkStateHandler mNetworkHandler;
169
Ben Murdoch8029a772010-11-16 11:58:21 +0000170 private Message mAutoFillSetupMessage;
171
Michael Kolb8233fac2010-10-26 16:08:53 -0700172 private boolean mShouldShowErrorConsole;
173
174 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
175
176 // FIXME, temp address onPrepareMenu performance problem.
177 // When we move everything out of view, we should rewrite this.
178 private int mCurrentMenuState = 0;
179 private int mMenuState = R.id.MAIN_MENU;
180 private int mOldMenuState = EMPTY_MENU;
181 private Menu mCachedMenu;
182
Michael Kolb8233fac2010-10-26 16:08:53 -0700183 private boolean mMenuIsDown;
184
185 // For select and find, we keep track of the ActionMode so that
186 // finish() can be called as desired.
187 private ActionMode mActionMode;
188
189 /**
190 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
191 * of whether the configuration has changed. The first onMenuOpened call
192 * after a configuration change is simply a reopening of the same menu
193 * (i.e. mIconView did not change).
194 */
195 private boolean mConfigChanged;
196
197 /**
198 * Keeps track of whether the options menu is open. This is important in
199 * determining whether to show or hide the title bar overlay
200 */
201 private boolean mOptionsMenuOpen;
202
203 /**
204 * Whether or not the options menu is in its bigger, popup menu form. When
205 * true, we want the title bar overlay to be gone. When false, we do not.
206 * Only meaningful if mOptionsMenuOpen is true.
207 */
208 private boolean mExtendedMenuOpen;
209
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 private boolean mActivityPaused = true;
211 private boolean mLoadStopped;
212
213 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500214 // Checks to see when the bookmarks database has changed, and updates the
215 // Tabs' notion of whether they represent bookmarked sites.
216 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700217 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700218
Michael Kolbc3af0672011-08-09 10:24:41 -0700219 private boolean mBlockEvents;
220
Michael Kolb0b129122012-06-04 16:31:58 -0700221 private String mVoiceResult;
222
George Mount3636d0a2011-11-21 09:08:21 -0800223 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700224 mActivity = browser;
225 mSettings = BrowserSettings.getInstance();
226 mTabControl = new TabControl(this);
227 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700228 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800229 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700230 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700231
232 mUrlHandler = new UrlHandler(this);
233 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700234 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
235
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500237 mBookmarksObserver = new ContentObserver(mHandler) {
238 @Override
239 public void onChange(boolean selfChange) {
240 int size = mTabControl.getTabCount();
241 for (int i = 0; i < size; i++) {
242 mTabControl.getTab(i).updateBookmarkedStatus();
243 }
244 }
245
246 };
247 browser.getContentResolver().registerContentObserver(
248 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700249
250 mNetworkHandler = new NetworkStateHandler(mActivity, this);
251 // Start watching the default geolocation permissions
252 mSystemAllowGeolocationOrigins =
253 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
254 mSystemAllowGeolocationOrigins.start();
255
John Reckaf262e72011-07-25 13:55:44 -0700256 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700257 }
258
John Reck9c35b9c2012-05-30 10:08:50 -0700259 @Override
260 public void start(final Intent intent) {
Ben Murdochf695f6b2012-05-24 12:59:45 +0100261 WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800262 // mCrashRecoverHandler has any previously saved state.
263 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700264 }
265
George Mount3636d0a2011-11-21 09:08:21 -0800266 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 // Unless the last browser usage was within 24 hours, destroy any
268 // remaining incognito tabs.
269
270 Calendar lastActiveDate = icicle != null ?
271 (Calendar) icicle.getSerializable("lastActiveDate") : null;
272 Calendar today = Calendar.getInstance();
273 Calendar yesterday = Calendar.getInstance();
274 yesterday.add(Calendar.DATE, -1);
275
Patrick Scott7d50a932011-02-04 09:27:26 -0500276 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800278 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700279
Patrick Scott7d50a932011-02-04 09:27:26 -0500280 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700281 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500282 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000283
Michael Kolbc831b632011-05-11 09:30:34 -0700284 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500285 // Not able to restore so we go ahead and clear session cookies. We
286 // must do this before trying to login the user as we don't want to
287 // clear any session cookies set during login.
288 CookieManager.getInstance().removeSessionCookie();
289 }
290
Patrick Scottd43e75a2011-03-14 14:47:23 -0400291 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 new Runnable() {
293 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800294 onPreloginFinished(icicle, intent, currentTabId,
295 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500296 }
297 });
298 }
299
Michael Kolbc831b632011-05-11 09:30:34 -0700300 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800301 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700302 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700303 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800304 if (intent == null) {
305 // This won't happen under common scenarios. The icicle is
306 // not null, but there aren't any tabs to restore.
307 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700308 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800309 final Bundle extra = intent.getExtras();
310 // Create an initial tab.
311 // If the intent is ACTION_VIEW and data is not null, the Browser is
312 // invoked to view the content by another application. In this case,
313 // the tab will be close when exit.
314 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
315 Tab t = null;
316 if (urlData.isEmpty()) {
317 t = openTabToHomePage();
318 } else {
319 t = openTab(urlData);
320 }
321 if (t != null) {
322 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
323 }
324 WebView webView = t.getWebView();
325 if (extra != null) {
326 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
327 if (scale > 0 && scale <= 1000) {
328 webView.setInitialScale(scale);
329 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700330 }
331 }
John Reckd8c74522011-06-14 08:45:00 -0700332 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700333 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700334 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500335 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700336 List<Tab> tabs = mTabControl.getTabs();
337 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
338 for (Tab t : tabs) {
339 restoredTabs.add(t.getId());
340 }
341 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700342 if (tabs.size() == 0) {
343 openTabToHomePage();
344 }
John Reck1cf4b792011-07-26 10:22:22 -0700345 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700346 // TabControl.restoreState() will create a new tab even if
347 // restoring the state fails.
348 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800349 // Intent is non-null when framework thinks the browser should be
350 // launching with a new intent (icicle is null).
351 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700352 mIntentHandler.onNewIntent(intent);
353 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700354 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700356 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 if (jsFlags.trim().length() != 0) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000358 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 }
George Mount3636d0a2011-11-21 09:08:21 -0800360 if (intent != null
361 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700362 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800363 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700364 }
365
John Reck1cf4b792011-07-26 10:22:22 -0700366 private static class PruneThumbnails implements Runnable {
367 private Context mContext;
368 private List<Long> mIds;
369
370 PruneThumbnails(Context context, List<Long> preserveIds) {
371 mContext = context.getApplicationContext();
372 mIds = preserveIds;
373 }
374
375 @Override
376 public void run() {
377 ContentResolver cr = mContext.getContentResolver();
378 if (mIds == null || mIds.size() == 0) {
379 cr.delete(Thumbnails.CONTENT_URI, null, null);
380 } else {
381 int length = mIds.size();
382 StringBuilder where = new StringBuilder();
383 where.append(Thumbnails._ID);
384 where.append(" not in (");
385 for (int i = 0; i < length; i++) {
386 where.append(mIds.get(i));
387 if (i < (length - 1)) {
388 where.append(",");
389 }
390 }
391 where.append(")");
392 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
393 }
394 }
395
396 }
397
Michael Kolb1514bb72010-11-22 09:11:48 -0800398 @Override
399 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700400 return mFactory;
401 }
402
403 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800404 public void onSetWebView(Tab tab, WebView view) {
405 mUi.onSetWebView(tab, view);
406 }
407
408 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800409 public void createSubWindow(Tab tab) {
410 endActionMode();
411 WebView mainView = tab.getWebView();
412 WebView subView = mFactory.createWebView((mainView == null)
413 ? false
414 : mainView.isPrivateBrowsingEnabled());
415 mUi.createSubWindow(tab, subView);
416 }
417
418 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700419 public Context getContext() {
420 return mActivity;
421 }
422
423 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700424 public Activity getActivity() {
425 return mActivity;
426 }
427
428 void setUi(UI ui) {
429 mUi = ui;
430 }
431
Michael Kolbaf63dba2012-05-16 12:58:05 -0700432 @Override
433 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700434 return mSettings;
435 }
436
437 IntentHandler getIntentHandler() {
438 return mIntentHandler;
439 }
440
441 @Override
442 public UI getUi() {
443 return mUi;
444 }
445
446 int getMaxTabs() {
447 return mActivity.getResources().getInteger(R.integer.max_tabs);
448 }
449
450 @Override
451 public TabControl getTabControl() {
452 return mTabControl;
453 }
454
Michael Kolb1bf23132010-11-19 12:55:12 -0800455 @Override
456 public List<Tab> getTabs() {
457 return mTabControl.getTabs();
458 }
459
John Reckaf262e72011-07-25 13:55:44 -0700460 // Open the icon database.
461 private void openIconDatabase() {
462 // We have to call getInstance on the UI thread
463 final WebIconDatabase instance = WebIconDatabase.getInstance();
464 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000465
John Reckaf262e72011-07-25 13:55:44 -0700466 @Override
467 public void run() {
468 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700469 }
John Reckaf262e72011-07-25 13:55:44 -0700470 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700471 }
472
473 private void startHandler() {
474 mHandler = new Handler() {
475
476 @Override
477 public void handleMessage(Message msg) {
478 switch (msg.what) {
479 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700480 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700481 break;
482 case FOCUS_NODE_HREF:
483 {
484 String url = (String) msg.getData().get("url");
485 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500486 String src = (String) msg.getData().get("src");
487 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700488 if (TextUtils.isEmpty(url)) {
489 break;
490 }
491 HashMap focusNodeMap = (HashMap) msg.obj;
492 WebView view = (WebView) focusNodeMap.get("webview");
493 // Only apply the action if the top window did not change.
494 if (getCurrentTopWebView() != view) {
495 break;
496 }
497 switch (msg.arg1) {
498 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700499 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700500 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500501 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700502 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500503 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500504 case R.id.open_newtab_context_menu_id:
505 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700506 openTab(url, parent,
507 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500508 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700509 case R.id.copy_link_context_menu_id:
510 copy(url);
511 break;
512 case R.id.save_link_context_menu_id:
513 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500514 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000515 mActivity, url, null, null, null,
516 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700517 break;
518 }
519 break;
520 }
521
522 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700523 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700524 break;
525
526 case STOP_LOAD:
527 stopLoading();
528 break;
529
530 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700531 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700532 mWakeLock.release();
533 // if we reach here, Browser should be still in the
534 // background loading after WAKELOCK_TIMEOUT (5-min).
535 // To avoid burning the battery, stop loading.
536 mTabControl.stopAllLoading();
537 }
538 break;
539
540 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800541 Tab tab = (Tab) msg.obj;
542 if (tab != null) {
543 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700544 }
545 break;
546 }
547 }
548 };
549
550 }
551
John Reckef654f12011-07-12 16:42:08 -0700552 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200553 public Tab getCurrentTab() {
554 return mTabControl.getCurrentTab();
555 }
556
Michael Kolbba99c5d2010-11-29 14:57:41 -0800557 @Override
558 public void shareCurrentPage() {
559 shareCurrentPage(mTabControl.getCurrentTab());
560 }
561
562 private void shareCurrentPage(Tab tab) {
563 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800564 sharePage(mActivity, tab.getTitle(),
565 tab.getUrl(), tab.getFavicon(),
566 createScreenshot(tab.getWebView(),
567 getDesiredThumbnailWidth(mActivity),
568 getDesiredThumbnailHeight(mActivity)));
569 }
570 }
571
Michael Kolb8233fac2010-10-26 16:08:53 -0700572 /**
573 * Share a page, providing the title, url, favicon, and a screenshot. Uses
574 * an {@link Intent} to launch the Activity chooser.
575 * @param c Context used to launch a new Activity.
576 * @param title Title of the page. Stored in the Intent with
577 * {@link Intent#EXTRA_SUBJECT}
578 * @param url URL of the page. Stored in the Intent with
579 * {@link Intent#EXTRA_TEXT}
580 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
581 * with {@link Browser#EXTRA_SHARE_FAVICON}
582 * @param screenshot Bitmap of a screenshot of the page. Stored in the
583 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
584 */
585 static final void sharePage(Context c, String title, String url,
586 Bitmap favicon, Bitmap screenshot) {
587 Intent send = new Intent(Intent.ACTION_SEND);
588 send.setType("text/plain");
589 send.putExtra(Intent.EXTRA_TEXT, url);
590 send.putExtra(Intent.EXTRA_SUBJECT, title);
591 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
592 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
593 try {
594 c.startActivity(Intent.createChooser(send, c.getString(
595 R.string.choosertitle_sharevia)));
596 } catch(android.content.ActivityNotFoundException ex) {
597 // if no app handles it, do nothing
598 }
599 }
600
601 private void copy(CharSequence text) {
602 ClipboardManager cm = (ClipboardManager) mActivity
603 .getSystemService(Context.CLIPBOARD_SERVICE);
604 cm.setText(text);
605 }
606
607 // lifecycle
608
John Reck9c35b9c2012-05-30 10:08:50 -0700609 @Override
610 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700611 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800612 // update the menu in case of a locale change
613 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700614 if (mPageDialogsHandler != null) {
615 mPageDialogsHandler.onConfigurationChanged(config);
616 }
617 mUi.onConfigurationChanged(config);
618 }
619
620 @Override
621 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700622 if (!mUi.isWebShowing()) {
623 mUi.showWeb(false);
624 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 mIntentHandler.onNewIntent(intent);
626 }
627
John Reck9c35b9c2012-05-30 10:08:50 -0700628 @Override
629 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800630 if (mUi.isCustomViewShowing()) {
631 hideCustomView();
632 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 if (mActivityPaused) {
634 Log.e(LOGTAG, "BrowserActivity is already paused.");
635 return;
636 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800638 Tab tab = mTabControl.getCurrentTab();
639 if (tab != null) {
640 tab.pause();
641 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700642 if (mWakeLock == null) {
643 PowerManager pm = (PowerManager) mActivity
644 .getSystemService(Context.POWER_SERVICE);
645 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
646 }
Michael Kolb70976932010-11-30 11:34:01 -0800647 mWakeLock.acquire();
648 mHandler.sendMessageDelayed(mHandler
649 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
650 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700651 }
652 mUi.onPause();
653 mNetworkHandler.onPause();
654
655 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100656 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700657 if (sThumbnailBitmap != null) {
658 sThumbnailBitmap.recycle();
659 sThumbnailBitmap = null;
660 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 }
662
John Reck9c35b9c2012-05-30 10:08:50 -0700663 @Override
664 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800666 Bundle saveState = createSaveState();
667
668 // crash recovery manages all save & restore state
669 mCrashRecoveryHandler.writeState(saveState);
670 mSettings.setLastRunPaused(true);
671 }
672
673 /**
674 * Save the current state to outState. Does not write the state to
675 * disk.
676 * @return Bundle containing the current state of all tabs.
677 */
678 /* package */ Bundle createSaveState() {
679 Bundle saveState = new Bundle();
680 mTabControl.saveState(saveState);
681 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700682 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800683 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700684 }
George Mount3636d0a2011-11-21 09:08:21 -0800685 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 }
687
John Reck9c35b9c2012-05-30 10:08:50 -0700688 @Override
689 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700690 if (!mActivityPaused) {
691 Log.e(LOGTAG, "BrowserActivity is already resumed.");
692 return;
693 }
George Mount3636d0a2011-11-21 09:08:21 -0800694 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700695 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800696 Tab current = mTabControl.getCurrentTab();
697 if (current != null) {
698 current.resume();
699 resumeWebViewTimers(current);
700 }
John Reckf57c0292011-07-21 18:15:39 -0700701 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200702
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 mUi.onResume();
704 mNetworkHandler.onResume();
705 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100706 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700707 if (mVoiceResult != null) {
708 mUi.onVoiceResult(mVoiceResult);
709 mVoiceResult = null;
710 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700711 }
712
John Reckf57c0292011-07-21 18:15:39 -0700713 private void releaseWakeLock() {
714 if (mWakeLock != null && mWakeLock.isHeld()) {
715 mHandler.removeMessages(RELEASE_WAKELOCK);
716 mWakeLock.release();
717 }
718 }
719
Michael Kolb70976932010-11-30 11:34:01 -0800720 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800721 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800722 * @param tab guaranteed non-null
723 */
724 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 boolean inLoad = tab.inPageLoad();
726 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
727 CookieSyncManager.getInstance().startSync();
728 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100729 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700730 }
731 }
732
Michael Kolb70976932010-11-30 11:34:01 -0800733 /**
734 * Pause all WebView timers using the WebView of the given tab
735 * @param tab
736 * @return true if the timers are paused or tab is null
737 */
738 private boolean pauseWebViewTimers(Tab tab) {
739 if (tab == null) {
740 return true;
741 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100743 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 }
Michael Kolb70976932010-11-30 11:34:01 -0800746 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700747 }
748
John Reck9c35b9c2012-05-30 10:08:50 -0700749 @Override
750 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800751 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700752 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
753 mUploadHandler = null;
754 }
755 if (mTabControl == null) return;
756 mUi.onDestroy();
757 // Remove the current tab and sub window
758 Tab t = mTabControl.getCurrentTab();
759 if (t != null) {
760 dismissSubWindow(t);
761 removeTab(t);
762 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500763 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 // Destroy all the tabs
765 mTabControl.destroy();
766 WebIconDatabase.getInstance().close();
767 // Stop watching the default geolocation permissions
768 mSystemAllowGeolocationOrigins.stop();
769 mSystemAllowGeolocationOrigins = null;
770 }
771
772 protected boolean isActivityPaused() {
773 return mActivityPaused;
774 }
775
John Reck9c35b9c2012-05-30 10:08:50 -0700776 @Override
777 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700778 mTabControl.freeMemory();
779 }
780
781 @Override
782 public boolean shouldShowErrorConsole() {
783 return mShouldShowErrorConsole;
784 }
785
786 protected void setShouldShowErrorConsole(boolean show) {
787 if (show == mShouldShowErrorConsole) {
788 // Nothing to do.
789 return;
790 }
791 mShouldShowErrorConsole = show;
792 Tab t = mTabControl.getCurrentTab();
793 if (t == null) {
794 // There is no current tab so we cannot toggle the error console
795 return;
796 }
797 mUi.setShouldShowErrorConsole(t, show);
798 }
799
800 @Override
801 public void stopLoading() {
802 mLoadStopped = true;
803 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700805 if (w != null) {
806 w.stopLoading();
807 mUi.onPageStopped(tab);
808 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700809 }
810
811 boolean didUserStopLoading() {
812 return mLoadStopped;
813 }
814
815 // WebViewController
816
817 @Override
John Reck324d4402011-01-11 16:56:42 -0800818 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700819
820 // We've started to load a new page. If there was a pending message
821 // to save a screenshot then we will now take the new page and save
822 // an incorrect screenshot. Therefore, remove any pending thumbnail
823 // messages from the queue.
824 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800825 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700826
827 // reset sync timer to avoid sync starts during loading a page
828 CookieSyncManager.getInstance().resetSync();
829
830 if (!mNetworkHandler.isNetworkUp()) {
831 view.setNetworkAvailable(false);
832 }
833
834 // when BrowserActivity just starts, onPageStarted may be called before
835 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
836 // to start the timer. As we won't switch tabs while an activity is in
837 // pause state, we can ensure calling resume and pause in pair.
838 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800839 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 }
841 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 endActionMode();
843
John Reck30c714c2010-12-16 17:30:34 -0800844 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700845
John Reck324d4402011-01-11 16:56:42 -0800846 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 // update the bookmark database for favicon
848 maybeUpdateFavicon(tab, null, url, favicon);
849
850 Performance.tracePageStart(url);
851
852 // Performance probe
853 if (false) {
854 Performance.onPageStarted();
855 }
856
857 }
858
859 @Override
John Reck324d4402011-01-11 16:56:42 -0800860 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700861 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800862 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 // pause the WebView timer and release the wake lock if it is finished
864 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800865 if (mActivityPaused && pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700866 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700867 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200868
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 // Performance probe
870 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800871 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 }
873
874 Performance.tracePageFinished();
875 }
876
877 @Override
John Reck30c714c2010-12-16 17:30:34 -0800878 public void onProgressChanged(Tab tab) {
879 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700880
881 if (newProgress == 100) {
882 CookieSyncManager.getInstance().sync();
883 // onProgressChanged() may continue to be called after the main
884 // frame has finished loading, as any remaining sub frames continue
885 // to load. We'll only get called once though with newProgress as
886 // 100 when everything is loaded. (onPageFinished is called once
887 // when the main frame completes loading regardless of the state of
888 // any sub frames so calls to onProgressChanges may continue after
889 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800890 if (tab.inPageLoad()) {
891 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 }
John Reckd9862372012-02-21 15:04:50 -0800893 if (!tab.isPrivateBrowsingEnabled()
894 && !TextUtils.isEmpty(tab.getUrl())
895 && !tab.isSnapshot()) {
896 // Only update the bookmark screenshot if the user did not
897 // cancel the load early and there is not already
898 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700899 if (tab.shouldUpdateThumbnail() &&
900 (tab.inForeground() && !didUserStopLoading()
901 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800902 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
903 mHandler.sendMessageDelayed(mHandler.obtainMessage(
904 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
905 500);
906 }
907 }
908 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800910 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700911 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800912 // still loading
913 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700914 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800915 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 }
917 }
John Reck30c714c2010-12-16 17:30:34 -0800918 mUi.onProgressChanged(tab);
919 }
920
921 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100922 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800923 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 }
925
926 @Override
927 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800928 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800929 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800930 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700931 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
932 return;
933 }
934 // Update the title in the history database if not in private browsing mode
935 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700936 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700937 }
938 }
939
940 @Override
941 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800942 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
944 }
945
946 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800947 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
948 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700949 }
950
951 @Override
952 public boolean shouldOverrideKeyEvent(KeyEvent event) {
953 if (mMenuIsDown) {
954 // only check shortcut key when MENU is held
955 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
956 event);
957 } else {
958 return false;
959 }
960 }
961
962 @Override
John Reck997b1b72012-04-19 18:08:25 -0700963 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 if (!isActivityPaused()) {
965 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700966 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 } else {
John Reck997b1b72012-04-19 18:08:25 -0700968 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700969 }
970 }
John Reck997b1b72012-04-19 18:08:25 -0700971 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 }
973
974 @Override
John Reck324d4402011-01-11 16:56:42 -0800975 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 // Don't save anything in private browsing mode
977 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800978 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700979
John Reck324d4402011-01-11 16:56:42 -0800980 if (TextUtils.isEmpty(url)
981 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 return;
983 }
John Reckf57c0292011-07-21 18:15:39 -0700984 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700985 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 }
987
988 @Override
989 public void getVisitedHistory(final ValueCallback<String[]> callback) {
990 AsyncTask<Void, Void, String[]> task =
991 new AsyncTask<Void, Void, String[]>() {
992 @Override
993 public String[] doInBackground(Void... unused) {
994 return Browser.getVisitedHistory(mActivity.getContentResolver());
995 }
996 @Override
997 public void onPostExecute(String[] result) {
998 callback.onReceiveValue(result);
999 }
1000 };
1001 task.execute();
1002 }
1003
1004 @Override
1005 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1006 final HttpAuthHandler handler, final String host,
1007 final String realm) {
1008 String username = null;
1009 String password = null;
1010
1011 boolean reuseHttpAuthUsernamePassword
1012 = handler.useHttpAuthUsernamePassword();
1013
1014 if (reuseHttpAuthUsernamePassword && view != null) {
1015 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1016 if (credentials != null && credentials.length == 2) {
1017 username = credentials[0];
1018 password = credentials[1];
1019 }
1020 }
1021
1022 if (username != null && password != null) {
1023 handler.proceed(username, password);
1024 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001025 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1027 } else {
1028 handler.cancel();
1029 }
1030 }
1031 }
1032
1033 @Override
1034 public void onDownloadStart(Tab tab, String url, String userAgent,
1035 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001036 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -05001037 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001038 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
1039 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 // This Tab was opened for the sole purpose of downloading a
1041 // file. Remove it.
1042 if (tab == mTabControl.getCurrentTab()) {
1043 // In this case, the Tab is still on top.
1044 goBackOnePageOrQuit();
1045 } else {
1046 // In this case, it is not.
1047 closeTab(tab);
1048 }
1049 }
1050 }
1051
1052 @Override
1053 public Bitmap getDefaultVideoPoster() {
1054 return mUi.getDefaultVideoPoster();
1055 }
1056
1057 @Override
1058 public View getVideoLoadingProgressView() {
1059 return mUi.getVideoLoadingProgressView();
1060 }
1061
1062 @Override
1063 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1064 SslError error) {
1065 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1066 }
1067
Patrick Scott92066772011-03-10 08:46:27 -05001068 @Override
1069 public void showAutoLogin(Tab tab) {
1070 assert tab.inForeground();
1071 // Update the title bar to show the auto-login request.
1072 mUi.showAutoLogin(tab);
1073 }
1074
1075 @Override
1076 public void hideAutoLogin(Tab tab) {
1077 assert tab.inForeground();
1078 mUi.hideAutoLogin(tab);
1079 }
1080
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 // helper method
1082
1083 /*
1084 * Update the favorites icon if the private browsing isn't enabled and the
1085 * icon is valid.
1086 */
1087 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1088 final String url, Bitmap favicon) {
1089 if (favicon == null) {
1090 return;
1091 }
1092 if (!tab.isPrivateBrowsingEnabled()) {
1093 Bookmarks.updateFavicon(mActivity
1094 .getContentResolver(), originalUrl, url, favicon);
1095 }
1096 }
1097
Leon Scroggins4cd97792010-12-03 15:31:56 -05001098 @Override
1099 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001100 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001101 mUi.bookmarkedStatusHasChanged(tab);
1102 }
1103
Michael Kolb8233fac2010-10-26 16:08:53 -07001104 // end WebViewController
1105
1106 protected void pageUp() {
1107 getCurrentTopWebView().pageUp(false);
1108 }
1109
1110 protected void pageDown() {
1111 getCurrentTopWebView().pageDown(false);
1112 }
1113
1114 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001115 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 public void editUrl() {
1117 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001118 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 }
1120
John Reck9c35b9c2012-05-30 10:08:50 -07001121 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001122 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001123 WebChromeClient.CustomViewCallback callback) {
1124 if (tab.inForeground()) {
1125 if (mUi.isCustomViewShowing()) {
1126 callback.onCustomViewHidden();
1127 return;
1128 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001129 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001130 // Save the menu state and set it to empty while the custom
1131 // view is showing.
1132 mOldMenuState = mMenuState;
1133 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001134 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 }
1136 }
1137
1138 @Override
1139 public void hideCustomView() {
1140 if (mUi.isCustomViewShowing()) {
1141 mUi.onHideCustomView();
1142 // Reset the old menu state.
1143 mMenuState = mOldMenuState;
1144 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001145 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 }
1147 }
1148
John Reck9c35b9c2012-05-30 10:08:50 -07001149 @Override
1150 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 Intent intent) {
1152 if (getCurrentTopWebView() == null) return;
1153 switch (requestCode) {
1154 case PREFERENCES_PAGE:
1155 if (resultCode == Activity.RESULT_OK && intent != null) {
1156 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001157 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001158 mTabControl.removeParentChildRelationShips();
1159 }
1160 }
1161 break;
1162 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001163 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001164 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001167 case AUTOFILL_SETUP:
1168 // Determine whether a profile was actually set up or not
1169 // and if so, send the message back to the WebTextView to
1170 // fill the form with the new profile.
1171 if (getSettings().getAutoFillProfile() != null) {
1172 mAutoFillSetupMessage.sendToTarget();
1173 mAutoFillSetupMessage = null;
1174 }
1175 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001176 case COMBO_VIEW:
1177 if (intent == null || resultCode != Activity.RESULT_OK) {
1178 break;
1179 }
John Reck3ba45532011-08-11 16:26:53 -07001180 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001181 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1182 Tab t = getCurrentTab();
1183 Uri uri = intent.getData();
1184 loadUrl(t, uri.toString());
1185 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1186 String[] urls = intent.getStringArrayExtra(
1187 ComboViewActivity.EXTRA_OPEN_ALL);
1188 Tab parent = getCurrentTab();
1189 for (String url : urls) {
1190 parent = openTab(url, parent,
1191 !mSettings.openInBackground(), true);
1192 }
1193 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1194 long id = intent.getLongExtra(
1195 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1196 if (id >= 0) {
1197 createNewSnapshotTab(id, true);
1198 }
1199 }
1200 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001201 case VOICE_RESULT:
1202 if (resultCode == Activity.RESULT_OK && intent != null) {
1203 ArrayList<String> results = intent.getStringArrayListExtra(
1204 RecognizerIntent.EXTRA_RESULTS);
1205 if (results.size() >= 1) {
1206 mVoiceResult = results.get(0);
1207 }
1208 }
1209 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001210 default:
1211 break;
1212 }
1213 getCurrentTopWebView().requestFocus();
1214 }
1215
1216 /**
1217 * Open the Go page.
1218 * @param startWithHistory If true, open starting on the history tab.
1219 * Otherwise, start with the bookmarks tab.
1220 */
1221 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001222 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001223 if (mTabControl.getCurrentWebView() == null) {
1224 return;
1225 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001226 // clear action mode
1227 if (isInCustomActionMode()) {
1228 endActionMode();
1229 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001230 Bundle extras = new Bundle();
1231 // Disable opening in a new window if we have maxed out the windows
1232 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1233 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001234 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 }
1236
1237 // combo view callbacks
1238
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 // key handling
1240 protected void onBackKey() {
1241 if (!mUi.onBackKey()) {
1242 WebView subwindow = mTabControl.getCurrentSubWindow();
1243 if (subwindow != null) {
1244 if (subwindow.canGoBack()) {
1245 subwindow.goBack();
1246 } else {
1247 dismissSubWindow(mTabControl.getCurrentTab());
1248 }
1249 } else {
1250 goBackOnePageOrQuit();
1251 }
1252 }
1253 }
1254
Michael Kolb4bd767d2011-05-27 11:33:55 -07001255 protected boolean onMenuKey() {
1256 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001257 }
1258
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 // menu handling and state
1260 // TODO: maybe put into separate handler
1261
John Reck9c35b9c2012-05-30 10:08:50 -07001262 @Override
1263 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001264 if (mMenuState == EMPTY_MENU) {
1265 return false;
1266 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001267 MenuInflater inflater = mActivity.getMenuInflater();
1268 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001269 return true;
1270 }
1271
John Reck9c35b9c2012-05-30 10:08:50 -07001272 @Override
1273 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001275 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 return;
1277 }
1278 if (!(v instanceof WebView)) {
1279 return;
1280 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001281 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 WebView.HitTestResult result = webview.getHitTestResult();
1283 if (result == null) {
1284 return;
1285 }
1286
1287 int type = result.getType();
1288 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1289 Log.w(LOGTAG,
1290 "We should not show context menu when nothing is touched");
1291 return;
1292 }
1293 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1294 // let TextView handles context menu
1295 return;
1296 }
1297
1298 // Note, http://b/issue?id=1106666 is requesting that
1299 // an inflated menu can be used again. This is not available
1300 // yet, so inflate each time (yuk!)
1301 MenuInflater inflater = mActivity.getMenuInflater();
1302 inflater.inflate(R.menu.browsercontext, menu);
1303
1304 // Show the correct menu group
1305 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001306 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001307 menu.setGroupVisible(R.id.PHONE_MENU,
1308 type == WebView.HitTestResult.PHONE_TYPE);
1309 menu.setGroupVisible(R.id.EMAIL_MENU,
1310 type == WebView.HitTestResult.EMAIL_TYPE);
1311 menu.setGroupVisible(R.id.GEO_MENU,
1312 type == WebView.HitTestResult.GEO_TYPE);
1313 menu.setGroupVisible(R.id.IMAGE_MENU,
1314 type == WebView.HitTestResult.IMAGE_TYPE
1315 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1316 menu.setGroupVisible(R.id.ANCHOR_MENU,
1317 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1318 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001319 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1320 || type == WebView.HitTestResult.PHONE_TYPE
1321 || type == WebView.HitTestResult.EMAIL_TYPE
1322 || type == WebView.HitTestResult.GEO_TYPE;
1323 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1324 if (hitText) {
1325 menu.findItem(R.id.select_text_menu_id)
1326 .setOnMenuItemClickListener(new SelectText(webview));
1327 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001328 // Setup custom handling depending on the type
1329 switch (type) {
1330 case WebView.HitTestResult.PHONE_TYPE:
1331 menu.setHeaderTitle(Uri.decode(extra));
1332 menu.findItem(R.id.dial_context_menu_id).setIntent(
1333 new Intent(Intent.ACTION_VIEW, Uri
1334 .parse(WebView.SCHEME_TEL + extra)));
1335 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1336 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1337 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1338 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1339 addIntent);
1340 menu.findItem(R.id.copy_phone_context_menu_id)
1341 .setOnMenuItemClickListener(
1342 new Copy(extra));
1343 break;
1344
1345 case WebView.HitTestResult.EMAIL_TYPE:
1346 menu.setHeaderTitle(extra);
1347 menu.findItem(R.id.email_context_menu_id).setIntent(
1348 new Intent(Intent.ACTION_VIEW, Uri
1349 .parse(WebView.SCHEME_MAILTO + extra)));
1350 menu.findItem(R.id.copy_mail_context_menu_id)
1351 .setOnMenuItemClickListener(
1352 new Copy(extra));
1353 break;
1354
1355 case WebView.HitTestResult.GEO_TYPE:
1356 menu.setHeaderTitle(extra);
1357 menu.findItem(R.id.map_context_menu_id).setIntent(
1358 new Intent(Intent.ACTION_VIEW, Uri
1359 .parse(WebView.SCHEME_GEO
1360 + URLEncoder.encode(extra))));
1361 menu.findItem(R.id.copy_geo_context_menu_id)
1362 .setOnMenuItemClickListener(
1363 new Copy(extra));
1364 break;
1365
1366 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1367 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001368 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 // decide whether to show the open link in new tab option
1370 boolean showNewTab = mTabControl.canCreateNewTab();
1371 MenuItem newTabItem
1372 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001373 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001374 ? R.string.contextmenu_openlink_newwindow_background
1375 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001376 newTabItem.setVisible(showNewTab);
1377 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001378 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1379 newTabItem.setOnMenuItemClickListener(
1380 new MenuItem.OnMenuItemClickListener() {
1381 @Override
1382 public boolean onMenuItemClick(MenuItem item) {
1383 final HashMap<String, WebView> hrefMap =
1384 new HashMap<String, WebView>();
1385 hrefMap.put("webview", webview);
1386 final Message msg = mHandler.obtainMessage(
1387 FOCUS_NODE_HREF,
1388 R.id.open_newtab_context_menu_id,
1389 0, hrefMap);
1390 webview.requestFocusNodeHref(msg);
1391 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001392 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001393 });
1394 } else {
1395 newTabItem.setOnMenuItemClickListener(
1396 new MenuItem.OnMenuItemClickListener() {
1397 @Override
1398 public boolean onMenuItemClick(MenuItem item) {
1399 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001400 openTab(extra, parent,
1401 !mSettings.openInBackground(),
1402 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001403 return true;
1404 }
1405 });
1406 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001408 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1409 break;
1410 }
1411 // otherwise fall through to handle image part
1412 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001413 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1414 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001415 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1416 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001417 shareItem.setOnMenuItemClickListener(
1418 new MenuItem.OnMenuItemClickListener() {
1419 @Override
1420 public boolean onMenuItemClick(MenuItem item) {
1421 sharePage(mActivity, null, extra, null,
1422 null);
1423 return true;
1424 }
1425 }
1426 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001427 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001428 menu.findItem(R.id.view_image_context_menu_id)
1429 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1430 @Override
1431 public boolean onMenuItemClick(MenuItem item) {
1432 openTab(extra, mTabControl.getCurrentTab(), true, true);
1433 return false;
1434 }
1435 });
Michael Kolb8233fac2010-10-26 16:08:53 -07001436 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001437 setOnMenuItemClickListener(
1438 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001439 menu.findItem(R.id.set_wallpaper_context_menu_id).
1440 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1441 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001442 break;
1443
1444 default:
1445 Log.w(LOGTAG, "We should not get here.");
1446 break;
1447 }
1448 //update the ui
1449 mUi.onContextMenuCreated(menu);
1450 }
1451
1452 /**
1453 * As the menu can be open when loading state changes
1454 * we must manually update the state of the stop/reload menu
1455 * item
1456 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001457 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 if (menu == null) {
1459 return;
1460 }
1461 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001462 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 menu.findItem(R.id.stop_menu_id):
1464 menu.findItem(R.id.reload_menu_id);
1465 if (src != null) {
1466 dest.setIcon(src.getIcon());
1467 dest.setTitle(src.getTitle());
1468 }
1469 }
1470
John Reck9c35b9c2012-05-30 10:08:50 -07001471 @Override
1472 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001473 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001474 // hold on to the menu reference here; it is used by the page callbacks
1475 // to update the menu based on loading state
1476 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001477 // Note: setVisible will decide whether an item is visible; while
1478 // setEnabled() will decide whether an item is enabled, which also means
1479 // whether the matching shortcut key will function.
1480 switch (mMenuState) {
1481 case EMPTY_MENU:
1482 if (mCurrentMenuState != mMenuState) {
1483 menu.setGroupVisible(R.id.MAIN_MENU, false);
1484 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1485 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1486 }
1487 break;
1488 default:
1489 if (mCurrentMenuState != mMenuState) {
1490 menu.setGroupVisible(R.id.MAIN_MENU, true);
1491 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1492 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1493 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001494 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001495 break;
1496 }
1497 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001498 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001499 }
1500
Michael Kolb4bf79712011-07-14 14:18:12 -07001501 @Override
1502 public void updateMenuState(Tab tab, Menu menu) {
1503 boolean canGoBack = false;
1504 boolean canGoForward = false;
1505 boolean isHome = false;
John Reck42229bc2011-08-19 13:26:43 -07001506 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001507 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001508 if (tab != null) {
1509 canGoBack = tab.canGoBack();
1510 canGoForward = tab.canGoForward();
1511 isHome = mSettings.getHomePage().equals(tab.getUrl());
John Reck42229bc2011-08-19 13:26:43 -07001512 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001513 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001514 }
1515 final MenuItem back = menu.findItem(R.id.back_menu_id);
1516 back.setEnabled(canGoBack);
1517
1518 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1519 home.setEnabled(!isHome);
1520
1521 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1522 forward.setEnabled(canGoForward);
1523
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001524 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1525 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001526 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001527 if (source != null && dest != null) {
1528 dest.setTitle(source.getTitle());
1529 dest.setIcon(source.getIcon());
1530 }
John Recke1a03a32011-09-14 17:04:16 -07001531 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001532
1533 // decide whether to show the share link option
1534 PackageManager pm = mActivity.getPackageManager();
1535 Intent send = new Intent(Intent.ACTION_SEND);
1536 send.setType("text/plain");
1537 ResolveInfo ri = pm.resolveActivity(send,
1538 PackageManager.MATCH_DEFAULT_ONLY);
1539 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1540
1541 boolean isNavDump = mSettings.enableNavDump();
1542 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1543 nav.setVisible(isNavDump);
1544 nav.setEnabled(isNavDump);
1545
1546 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001547 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1548 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001549 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1550 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Michael Kolb315d5022011-10-13 12:47:11 -07001551 menu.setGroupVisible(R.id.COMBO_MENU, false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001552
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001553 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001554 }
1555
John Reck9c35b9c2012-05-30 10:08:50 -07001556 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001557 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001558 if (null == getCurrentTopWebView()) {
1559 return false;
1560 }
1561 if (mMenuIsDown) {
1562 // The shortcut action consumes the MENU. Even if it is still down,
1563 // it won't trigger the next shortcut action. In the case of the
1564 // shortcut action triggering a new activity, like Bookmarks, we
1565 // won't get onKeyUp for MENU. So it is important to reset it here.
1566 mMenuIsDown = false;
1567 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001568 if (mUi.onOptionsItemSelected(item)) {
1569 // ui callback handled it
1570 return true;
1571 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001572 switch (item.getItemId()) {
1573 // -- Main menu
1574 case R.id.new_tab_menu_id:
1575 openTabToHomePage();
1576 break;
1577
1578 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001579 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001580 break;
1581
Afzal Najamd4e33312012-04-26 01:54:01 -04001582 case R.id.close_other_tabs_id:
1583 closeOtherTabs();
1584 break;
1585
Michael Kolb8233fac2010-10-26 16:08:53 -07001586 case R.id.goto_menu_id:
1587 editUrl();
1588 break;
1589
1590 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001591 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1592 break;
1593
1594 case R.id.history_menu_id:
1595 bookmarksOrHistoryPicker(ComboViews.History);
1596 break;
1597
1598 case R.id.snapshots_menu_id:
1599 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 break;
1601
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001603 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001604 break;
1605
1606 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001607 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 stopLoading();
1609 } else {
1610 getCurrentTopWebView().reload();
1611 }
1612 break;
1613
1614 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001615 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 break;
1617
1618 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001619 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001620 break;
1621
1622 case R.id.close_menu_id:
1623 // Close the subwindow if it exists.
1624 if (mTabControl.getCurrentSubWindow() != null) {
1625 dismissSubWindow(mTabControl.getCurrentTab());
1626 break;
1627 }
1628 closeCurrentTab();
1629 break;
1630
1631 case R.id.homepage_menu_id:
1632 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001633 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001634 break;
1635
1636 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001637 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001638 break;
1639
1640 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001641 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001642 break;
1643
John Reck2bc80422011-06-30 15:11:49 -07001644 case R.id.save_snapshot_menu_id:
1645 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001646 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001647 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001648 break;
1649
Michael Kolb8233fac2010-10-26 16:08:53 -07001650 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001651 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001652 break;
1653
John Recke1a03a32011-09-14 17:04:16 -07001654 case R.id.snapshot_go_live:
1655 goLive();
1656 return true;
1657
Michael Kolb8233fac2010-10-26 16:08:53 -07001658 case R.id.share_page_menu_id:
1659 Tab currentTab = mTabControl.getCurrentTab();
1660 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001661 return false;
1662 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001663 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001664 break;
1665
1666 case R.id.dump_nav_menu_id:
1667 getCurrentTopWebView().debugDump();
1668 break;
1669
Michael Kolb8233fac2010-10-26 16:08:53 -07001670 case R.id.zoom_in_menu_id:
1671 getCurrentTopWebView().zoomIn();
1672 break;
1673
1674 case R.id.zoom_out_menu_id:
1675 getCurrentTopWebView().zoomOut();
1676 break;
1677
1678 case R.id.view_downloads_menu_id:
1679 viewDownloads();
1680 break;
1681
John Reck42229bc2011-08-19 13:26:43 -07001682 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001683 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001684 break;
1685
Michael Kolb8233fac2010-10-26 16:08:53 -07001686 case R.id.window_one_menu_id:
1687 case R.id.window_two_menu_id:
1688 case R.id.window_three_menu_id:
1689 case R.id.window_four_menu_id:
1690 case R.id.window_five_menu_id:
1691 case R.id.window_six_menu_id:
1692 case R.id.window_seven_menu_id:
1693 case R.id.window_eight_menu_id:
1694 {
1695 int menuid = item.getItemId();
1696 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1697 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1698 Tab desiredTab = mTabControl.getTab(id);
1699 if (desiredTab != null &&
1700 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001701 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001702 }
1703 break;
1704 }
1705 }
1706 }
1707 break;
1708
1709 default:
1710 return false;
1711 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001712 return true;
1713 }
1714
John Reck68234a92012-04-19 15:27:12 -07001715 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1716 implements OnCancelListener {
1717
1718 private Tab mTab;
1719 private Dialog mProgressDialog;
1720 private ContentValues mValues;
1721
1722 private SaveSnapshotTask(Tab tab) {
1723 mTab = tab;
1724 }
1725
1726 @Override
1727 protected void onPreExecute() {
1728 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1729 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1730 true, true, this);
1731 mValues = mTab.createSnapshotValues();
1732 }
1733
1734 @Override
1735 protected Long doInBackground(Void... params) {
1736 if (!mTab.saveViewState(mValues)) {
1737 return null;
1738 }
1739 if (isCancelled()) {
1740 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1741 File file = mActivity.getFileStreamPath(path);
1742 if (!file.delete()) {
1743 file.deleteOnExit();
1744 }
1745 return null;
1746 }
1747 final ContentResolver cr = mActivity.getContentResolver();
1748 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1749 if (result == null) {
1750 return null;
1751 }
1752 long id = ContentUris.parseId(result);
1753 return id;
1754 }
1755
1756 @Override
1757 protected void onPostExecute(Long id) {
1758 if (isCancelled()) {
1759 return;
1760 }
1761 mProgressDialog.dismiss();
1762 if (id == null) {
1763 Toast.makeText(mActivity, R.string.snapshot_failed,
1764 Toast.LENGTH_SHORT).show();
1765 return;
1766 }
1767 Bundle b = new Bundle();
1768 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1769 mUi.showComboView(ComboViews.Snapshots, b);
1770 }
1771
1772 @Override
1773 public void onCancel(DialogInterface dialog) {
1774 cancel(true);
1775 }
1776 }
1777
Michael Kolb80f75082012-04-10 10:50:06 -07001778 @Override
1779 public void toggleUserAgent() {
1780 WebView web = getCurrentWebView();
1781 mSettings.toggleDesktopUseragent(web);
1782 web.loadUrl(web.getOriginalUrl());
1783 }
1784
1785 @Override
1786 public void findOnPage() {
1787 getCurrentTopWebView().showFindDialog(null, true);
1788 }
1789
1790 @Override
1791 public void openPreferences() {
1792 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1793 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1794 getCurrentTopWebView().getUrl());
1795 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1796 }
1797
1798 @Override
1799 public void bookmarkCurrentPage() {
1800 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1801 if (bookmarkIntent != null) {
1802 mActivity.startActivity(bookmarkIntent);
1803 }
1804 }
1805
John Recke1a03a32011-09-14 17:04:16 -07001806 private void goLive() {
1807 Tab t = getCurrentTab();
1808 t.loadUrl(t.getUrl(), null);
1809 }
1810
Michael Kolb315d5022011-10-13 12:47:11 -07001811 @Override
1812 public void showPageInfo() {
1813 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1814 }
1815
John Reck9c35b9c2012-05-30 10:08:50 -07001816 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001817 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001818 // Let the History and Bookmark fragments handle menus they created.
1819 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1820 return false;
1821 }
1822
Michael Kolb8233fac2010-10-26 16:08:53 -07001823 int id = item.getItemId();
1824 boolean result = true;
1825 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001826 // -- Browser context menu
1827 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001828 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001829 case R.id.copy_link_context_menu_id:
1830 final WebView webView = getCurrentTopWebView();
1831 if (null == webView) {
1832 result = false;
1833 break;
1834 }
1835 final HashMap<String, WebView> hrefMap =
1836 new HashMap<String, WebView>();
1837 hrefMap.put("webview", webView);
1838 final Message msg = mHandler.obtainMessage(
1839 FOCUS_NODE_HREF, id, 0, hrefMap);
1840 webView.requestFocusNodeHref(msg);
1841 break;
1842
1843 default:
1844 // For other context menus
1845 result = onOptionsItemSelected(item);
1846 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001847 return result;
1848 }
1849
1850 /**
1851 * support programmatically opening the context menu
1852 */
1853 public void openContextMenu(View view) {
1854 mActivity.openContextMenu(view);
1855 }
1856
1857 /**
1858 * programmatically open the options menu
1859 */
1860 public void openOptionsMenu() {
1861 mActivity.openOptionsMenu();
1862 }
1863
John Reck9c35b9c2012-05-30 10:08:50 -07001864 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001865 public boolean onMenuOpened(int featureId, Menu menu) {
1866 if (mOptionsMenuOpen) {
1867 if (mConfigChanged) {
1868 // We do not need to make any changes to the state of the
1869 // title bar, since the only thing that happened was a
1870 // change in orientation
1871 mConfigChanged = false;
1872 } else {
1873 if (!mExtendedMenuOpen) {
1874 mExtendedMenuOpen = true;
1875 mUi.onExtendedMenuOpened();
1876 } else {
1877 // Switching the menu back to icon view, so show the
1878 // title bar once again.
1879 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001880 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001881 }
1882 }
1883 } else {
1884 // The options menu is closed, so open it, and show the title
1885 mOptionsMenuOpen = true;
1886 mConfigChanged = false;
1887 mExtendedMenuOpen = false;
1888 mUi.onOptionsMenuOpened();
1889 }
1890 return true;
1891 }
1892
John Reck9c35b9c2012-05-30 10:08:50 -07001893 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001894 public void onOptionsMenuClosed(Menu menu) {
1895 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001896 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 }
1898
John Reck9c35b9c2012-05-30 10:08:50 -07001899 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001900 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001901 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001902 }
1903
1904 // Helper method for getting the top window.
1905 @Override
1906 public WebView getCurrentTopWebView() {
1907 return mTabControl.getCurrentTopWebView();
1908 }
1909
1910 @Override
1911 public WebView getCurrentWebView() {
1912 return mTabControl.getCurrentWebView();
1913 }
1914
1915 /*
1916 * This method is called as a result of the user selecting the options
1917 * menu to see the download window. It shows the download window on top of
1918 * the current window.
1919 */
1920 void viewDownloads() {
1921 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1922 mActivity.startActivity(intent);
1923 }
1924
John Reck30b065e2011-07-19 10:58:05 -07001925 int getActionModeHeight() {
1926 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1927 new int[] { android.R.attr.actionBarSize });
1928 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1929 actionBarSizeTypedArray.recycle();
1930 return size;
1931 }
1932
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 // action mode
1934
John Reck9c35b9c2012-05-30 10:08:50 -07001935 @Override
1936 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001937 mUi.onActionModeStarted(mode);
1938 mActionMode = mode;
1939 }
1940
1941 /*
1942 * True if a custom ActionMode (i.e. find or select) is in use.
1943 */
1944 @Override
1945 public boolean isInCustomActionMode() {
1946 return mActionMode != null;
1947 }
1948
1949 /*
1950 * End the current ActionMode.
1951 */
1952 @Override
1953 public void endActionMode() {
1954 if (mActionMode != null) {
1955 mActionMode.finish();
1956 }
1957 }
1958
1959 /*
1960 * Called by find and select when they are finished. Replace title bars
1961 * as necessary.
1962 */
John Reck9c35b9c2012-05-30 10:08:50 -07001963 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001964 public void onActionModeFinished(ActionMode mode) {
1965 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001966 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 mActionMode = null;
1968 }
1969
1970 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001971 final Tab tab = getCurrentTab();
1972 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07001973 }
1974
1975 // bookmark handling
1976
1977 /**
1978 * add the current page as a bookmark to the given folder id
1979 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001980 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001981 * bookmarked, and if it is, edit that bookmark. If false, and
1982 * the site is already bookmarked, do not attempt to edit the
1983 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001984 */
1985 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001986 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07001987 WebView w = getCurrentTopWebView();
1988 if (w == null) {
1989 return null;
1990 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001991 Intent i = new Intent(mActivity,
1992 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07001993 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1994 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1995 String touchIconUrl = w.getTouchIconUrl();
1996 if (touchIconUrl != null) {
1997 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1998 WebSettings settings = w.getSettings();
1999 if (settings != null) {
2000 i.putExtra(AddBookmarkPage.USER_AGENT,
2001 settings.getUserAgentString());
2002 }
2003 }
2004 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2005 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2006 getDesiredThumbnailHeight(mActivity)));
2007 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002008 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002009 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2010 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002011 // Put the dialog at the upper right of the screen, covering the
2012 // star on the title bar.
2013 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002014 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002015 }
2016
2017 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002018 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002019 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002020 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002021 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002022 }
2023
2024 // thumbnails
2025
2026 /**
2027 * Return the desired width for thumbnail screenshots, which are stored in
2028 * the database, and used on the bookmarks screen.
2029 * @param context Context for finding out the density of the screen.
2030 * @return desired width for thumbnail screenshot.
2031 */
2032 static int getDesiredThumbnailWidth(Context context) {
2033 return context.getResources().getDimensionPixelOffset(
2034 R.dimen.bookmarkThumbnailWidth);
2035 }
2036
2037 /**
2038 * Return the desired height for thumbnail screenshots, which are stored in
2039 * the database, and used on the bookmarks screen.
2040 * @param context Context for finding out the density of the screen.
2041 * @return desired height for thumbnail screenshot.
2042 */
2043 static int getDesiredThumbnailHeight(Context context) {
2044 return context.getResources().getDimensionPixelOffset(
2045 R.dimen.bookmarkThumbnailHeight);
2046 }
2047
John Reck8cc92352011-07-06 17:41:52 -07002048 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002049 if (view == null || view.getContentHeight() == 0
2050 || view.getContentWidth() == 0) {
2051 return null;
2052 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002053 // We render to a bitmap 2x the desired size so that we can then
2054 // re-scale it with filtering since canvas.scale doesn't filter
2055 // This helps reduce aliasing at the cost of being slightly blurry
2056 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002057 int scaledWidth = width * filter_scale;
2058 int scaledHeight = height * filter_scale;
2059 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2060 || sThumbnailBitmap.getHeight() != scaledHeight) {
2061 if (sThumbnailBitmap != null) {
2062 sThumbnailBitmap.recycle();
2063 sThumbnailBitmap = null;
2064 }
2065 sThumbnailBitmap =
2066 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 }
John Reckd7dd9b22011-08-30 09:18:29 -07002068 Canvas canvas = new Canvas(sThumbnailBitmap);
2069 int contentWidth = view.getContentWidth();
2070 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2071 if (view instanceof BrowserWebView) {
2072 int dy = -((BrowserWebView)view).getTitleHeight();
2073 canvas.translate(0, dy * overviewScale);
2074 }
2075
2076 canvas.scale(overviewScale, overviewScale);
2077
2078 if (view instanceof BrowserWebView) {
2079 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002081 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002082 }
John Reckd7dd9b22011-08-30 09:18:29 -07002083 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2084 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002085 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002086 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002087 }
2088
John Reck34ef2672011-02-10 11:30:55 -08002089 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002090 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 // FIXME: Would like to make sure there is actually something to
2092 // draw, but the API for that (WebViewCore.pictureReady()) is not
2093 // currently accessible here.
2094
John Reck34ef2672011-02-10 11:30:55 -08002095 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002096 if (view == null) {
2097 // Tab was destroyed
2098 return;
2099 }
John Reck34ef2672011-02-10 11:30:55 -08002100 final String url = tab.getUrl();
2101 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002102 if (TextUtils.isEmpty(url)) {
2103 return;
2104 }
2105
2106 // Only update thumbnails for web urls (http(s)://), not for
2107 // about:, javascript:, data:, etc...
2108 // Unless it is a bookmarked site, then always update
2109 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2110 return;
2111 }
2112
Michael Kolb8233fac2010-10-26 16:08:53 -07002113 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2114 getDesiredThumbnailHeight(mActivity));
2115 if (bm == null) {
2116 return;
2117 }
2118
2119 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002120 new AsyncTask<Void, Void, Void>() {
2121 @Override
2122 protected Void doInBackground(Void... unused) {
2123 Cursor cursor = null;
2124 try {
2125 // TODO: Clean this up
2126 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2127 if (cursor != null && cursor.moveToFirst()) {
2128 final ByteArrayOutputStream os =
2129 new ByteArrayOutputStream();
2130 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002131
John Reck34ef2672011-02-10 11:30:55 -08002132 ContentValues values = new ContentValues();
2133 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002134
John Reck34ef2672011-02-10 11:30:55 -08002135 do {
John Reck617fd832011-02-16 14:35:59 -08002136 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002137 cr.update(Images.CONTENT_URI, values, null, null);
2138 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 }
John Reck34ef2672011-02-10 11:30:55 -08002140 } catch (IllegalStateException e) {
2141 // Ignore
2142 } finally {
2143 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002144 }
John Reck34ef2672011-02-10 11:30:55 -08002145 return null;
2146 }
2147 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002148 }
2149
2150 private class Copy implements OnMenuItemClickListener {
2151 private CharSequence mText;
2152
John Reck9c35b9c2012-05-30 10:08:50 -07002153 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 public boolean onMenuItemClick(MenuItem item) {
2155 copy(mText);
2156 return true;
2157 }
2158
2159 public Copy(CharSequence toCopy) {
2160 mText = toCopy;
2161 }
2162 }
2163
Leon Scroggins63c02662010-11-18 15:16:27 -05002164 private static class Download implements OnMenuItemClickListener {
2165 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002166 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002167 private boolean mPrivateBrowsing;
George Mount387d45d2011-10-07 15:57:53 -07002168 private static final String FALLBACK_EXTENSION = "dat";
2169 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002170
John Reck9c35b9c2012-05-30 10:08:50 -07002171 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002172 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002173 if (DataUri.isDataUri(mText)) {
2174 saveDataUri();
2175 } else {
2176 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
2177 null, null, mPrivateBrowsing);
2178 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002179 return true;
2180 }
2181
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002182 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002183 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002184 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002185 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002186 }
George Mount387d45d2011-10-07 15:57:53 -07002187
2188 /**
2189 * Treats mText as a data URI and writes its contents to a file
2190 * based on the current time.
2191 */
2192 private void saveDataUri() {
2193 FileOutputStream outputStream = null;
2194 try {
2195 DataUri uri = new DataUri(mText);
2196 File target = getTarget(uri);
2197 outputStream = new FileOutputStream(target);
2198 outputStream.write(uri.getData());
2199 final DownloadManager manager =
2200 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2201 manager.addCompletedDownload(target.getName(),
2202 mActivity.getTitle().toString(), false,
2203 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002204 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002205 } catch (IOException e) {
2206 Log.e(LOGTAG, "Could not save data URL");
2207 } finally {
2208 if (outputStream != null) {
2209 try {
2210 outputStream.close();
2211 } catch (IOException e) {
2212 // ignore close errors
2213 }
2214 }
2215 }
2216 }
2217
2218 /**
2219 * Creates a File based on the current time stamp and uses
2220 * the mime type of the DataUri to get the extension.
2221 */
2222 private File getTarget(DataUri uri) throws IOException {
2223 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
2224 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT);
2225 String nameBase = format.format(new Date());
2226 String mimeType = uri.getMimeType();
2227 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2228 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2229 if (extension == null) {
2230 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2231 extension = FALLBACK_EXTENSION;
2232 }
2233 extension = "." + extension; // createTempFile needs the '.'
2234 File targetFile = File.createTempFile(nameBase, extension, dir);
2235 return targetFile;
2236 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002237 }
2238
Cary Clark8974d282010-11-22 10:46:05 -05002239 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002240 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002241
John Reck9c35b9c2012-05-30 10:08:50 -07002242 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002243 public boolean onMenuItemClick(MenuItem item) {
2244 if (mWebView != null) {
2245 return mWebView.selectText();
2246 }
2247 return false;
2248 }
2249
2250 public SelectText(WebView webView) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002251 mWebView = WebViewClassic.fromWebView(webView);
Cary Clark8974d282010-11-22 10:46:05 -05002252 }
2253
2254 }
2255
Michael Kolb8233fac2010-10-26 16:08:53 -07002256 /********************** TODO: UI stuff *****************************/
2257
2258 // these methods have been copied, they still need to be cleaned up
2259
2260 /****************** tabs ***************************************************/
2261
2262 // basic tab interactions:
2263
2264 // it is assumed that tabcontrol already knows about the tab
2265 protected void addTab(Tab tab) {
2266 mUi.addTab(tab);
2267 }
2268
2269 protected void removeTab(Tab tab) {
2270 mUi.removeTab(tab);
2271 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002272 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002273 }
2274
Michael Kolbf2055602011-04-09 17:20:03 -07002275 @Override
2276 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002277 // monkey protection against delayed start
2278 if (tab != null) {
2279 mTabControl.setCurrentTab(tab);
2280 // the tab is guaranteed to have a webview after setCurrentTab
2281 mUi.setActiveTab(tab);
2282 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002283 }
2284
John Reck8bcafc12011-08-29 16:43:02 -07002285 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002286 Tab current = mTabControl.getCurrentTab();
2287 if (current != null
2288 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002289 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002290 }
2291 }
2292
John Reck26b18322011-06-21 13:08:58 -07002293 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002294 // Dismiss the subwindow if applicable.
2295 dismissSubWindow(appTab);
2296 // Since we might kill the WebView, remove it from the
2297 // content view first.
2298 mUi.detachTab(appTab);
2299 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002300 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002301 // TODO: analyze why the remove and add are necessary
2302 mUi.attachTab(appTab);
2303 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002304 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002305 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002306 } else {
2307 // If the tab was the current tab, we have to attach
2308 // it to the view system again.
2309 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002310 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002311 }
2312 }
2313
2314 // Remove the sub window if it exists. Also called by TabControl when the
2315 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002316 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002317 public void dismissSubWindow(Tab tab) {
2318 removeSubWindow(tab);
2319 // dismiss the subwindow. This will destroy the WebView.
2320 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002321 WebView wv = getCurrentTopWebView();
2322 if (wv != null) {
2323 wv.requestFocus();
2324 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002325 }
2326
2327 @Override
2328 public void removeSubWindow(Tab t) {
2329 if (t.getSubWebView() != null) {
2330 mUi.removeSubWindow(t.getSubViewContainer());
2331 }
2332 }
2333
2334 @Override
2335 public void attachSubWindow(Tab tab) {
2336 if (tab.getSubWebView() != null) {
2337 mUi.attachSubWindow(tab.getSubViewContainer());
2338 getCurrentTopWebView().requestFocus();
2339 }
2340 }
2341
Mathew Inwood29721c22011-06-29 17:55:24 +01002342 private Tab showPreloadedTab(final UrlData urlData) {
2343 if (!urlData.isPreloaded()) {
2344 return null;
2345 }
2346 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2347 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2348 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002349 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002350 // Could not submit query. Fallback to regular tab creation
2351 tabControl.destroy();
2352 return null;
2353 }
2354 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002355 // check tab count and make room for new tab
2356 if (!mTabControl.canCreateNewTab()) {
2357 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2358 if (leastUsed != null) {
2359 closeTab(leastUsed);
2360 }
2361 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002362 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002363 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002364 mTabControl.addPreloadedTab(t);
2365 addTab(t);
2366 setActiveTab(t);
2367 return t;
2368 }
2369
Michael Kolb7bcafde2011-05-09 13:55:59 -07002370 // open a non inconito tab with the given url data
2371 // and set as active tab
2372 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002373 Tab tab = showPreloadedTab(urlData);
2374 if (tab == null) {
2375 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002376 if ((tab != null) && !urlData.isEmpty()) {
2377 loadUrlDataIn(tab, urlData);
2378 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002379 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002380 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002381 }
2382
Michael Kolb843510f2010-12-09 10:51:49 -08002383 @Override
2384 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002385 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002386 }
2387
Michael Kolb8233fac2010-10-26 16:08:53 -07002388 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002389 public Tab openIncognitoTab() {
2390 return openTab(INCOGNITO_URI, true, true, false);
2391 }
2392
2393 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002394 public Tab openTab(String url, boolean incognito, boolean setActive,
2395 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002396 return openTab(url, incognito, setActive, useCurrent, null);
2397 }
2398
2399 @Override
2400 public Tab openTab(String url, Tab parent, boolean setActive,
2401 boolean useCurrent) {
2402 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2403 setActive, useCurrent, parent);
2404 }
2405
2406 public Tab openTab(String url, boolean incognito, boolean setActive,
2407 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002408 Tab tab = createNewTab(incognito, setActive, useCurrent);
2409 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002410 if (parent != null && parent != tab) {
2411 parent.addChildTab(tab);
2412 }
Michael Kolb519d2282011-05-09 17:03:19 -07002413 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002414 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002415 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002416 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002417 return tab;
2418 }
2419
2420 // this method will attempt to create a new tab
2421 // incognito: private browsing tab
2422 // setActive: ste tab as current tab
2423 // useCurrent: if no new tab can be created, return current tab
2424 private Tab createNewTab(boolean incognito, boolean setActive,
2425 boolean useCurrent) {
2426 Tab tab = null;
2427 if (mTabControl.canCreateNewTab()) {
2428 tab = mTabControl.createNewTab(incognito);
2429 addTab(tab);
2430 if (setActive) {
2431 setActiveTab(tab);
2432 }
2433 } else {
2434 if (useCurrent) {
2435 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002436 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002437 } else {
2438 mUi.showMaxTabsWarning();
2439 }
2440 }
2441 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002442 }
2443
John Reck2bc80422011-06-30 15:11:49 -07002444 @Override
2445 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2446 SnapshotTab tab = null;
2447 if (mTabControl.canCreateNewTab()) {
2448 tab = mTabControl.createSnapshotTab(snapshotId);
2449 addTab(tab);
2450 if (setActive) {
2451 setActiveTab(tab);
2452 }
2453 } else {
2454 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002455 }
2456 return tab;
2457 }
2458
Michael Kolb8233fac2010-10-26 16:08:53 -07002459 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002460 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002461 * @return boolean True if we successfully switched to a different tab. If
2462 * the indexth tab is null, or if that tab is the same as
2463 * the current one, return false.
2464 */
2465 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002466 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002467 Tab currentTab = mTabControl.getCurrentTab();
2468 if (tab == null || tab == currentTab) {
2469 return false;
2470 }
2471 setActiveTab(tab);
2472 return true;
2473 }
2474
2475 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002476 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002477 closeCurrentTab(false);
2478 }
2479
2480 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002481 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002482 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002483 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002484 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002485 return;
2486 }
Michael Kolbc831b632011-05-11 09:30:34 -07002487 final Tab current = mTabControl.getCurrentTab();
2488 final int pos = mTabControl.getCurrentPosition();
2489 Tab newTab = current.getParent();
2490 if (newTab == null) {
2491 newTab = mTabControl.getTab(pos + 1);
2492 if (newTab == null) {
2493 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002494 }
2495 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002496 if (andQuit) {
2497 mTabControl.setCurrentTab(newTab);
2498 closeTab(current);
2499 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002500 // Close window
2501 closeTab(current);
2502 }
2503 }
2504
2505 /**
2506 * Close the tab, remove its associated title bar, and adjust mTabControl's
2507 * current tab to a valid value.
2508 */
2509 @Override
2510 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002511 if (tab == mTabControl.getCurrentTab()) {
2512 closeCurrentTab();
2513 } else {
2514 removeTab(tab);
2515 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002516 }
2517
Afzal Najamd4e33312012-04-26 01:54:01 -04002518 /**
2519 * Close all tabs except the current one
2520 */
2521 @Override
2522 public void closeOtherTabs() {
2523 int inactiveTabs = mTabControl.getTabCount() - 1;
2524 for (int i = inactiveTabs; i >= 0; i--) {
2525 Tab tab = mTabControl.getTab(i);
2526 if (tab != mTabControl.getCurrentTab()) {
2527 removeTab(tab);
2528 }
2529 }
2530 }
2531
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002533 protected void loadUrlFromContext(String url) {
2534 Tab tab = getCurrentTab();
2535 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002536 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002537 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002538 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002539 if (!WebViewClassic.fromWebView(view).getWebViewClient().
2540 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002541 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002542 }
2543 }
2544 }
2545
2546 /**
2547 * Load the URL into the given WebView and update the title bar
2548 * to reflect the new load. Call this instead of WebView.loadUrl
2549 * directly.
2550 * @param view The WebView used to load url.
2551 * @param url The URL to load.
2552 */
John Reck71e51422011-07-01 16:49:28 -07002553 @Override
2554 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002555 loadUrl(tab, url, null);
2556 }
2557
2558 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2559 if (tab != null) {
2560 dismissSubWindow(tab);
2561 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002562 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002563 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002564 }
2565
2566 /**
2567 * Load UrlData into a Tab and update the title bar to reflect the new
2568 * load. Call this instead of UrlData.loadIn directly.
2569 * @param t The Tab used to load.
2570 * @param data The UrlData being loaded.
2571 */
2572 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002573 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002574 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002575 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002576 } else {
John Reck38b39652012-06-05 09:22:59 -07002577 if (t != null && data.mDisableUrlOverride) {
2578 t.disableUrlOverridingForLoad();
2579 }
John Reck26b18322011-06-21 13:08:58 -07002580 loadUrl(t, data.mUrl, data.mHeaders);
2581 }
2582 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002583 }
2584
John Reck30c714c2010-12-16 17:30:34 -08002585 @Override
2586 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002587 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002588 if (tab.canGoBack()) {
2589 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002590 } else {
John Reckef654f12011-07-12 16:42:08 -07002591 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002592 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002593 }
2594
2595 void goBackOnePageOrQuit() {
2596 Tab current = mTabControl.getCurrentTab();
2597 if (current == null) {
2598 /*
2599 * Instead of finishing the activity, simply push this to the back
2600 * of the stack and let ActivityManager to choose the foreground
2601 * activity. As BrowserActivity is singleTask, it will be always the
2602 * root of the task. So we can use either true or false for
2603 * moveTaskToBack().
2604 */
2605 mActivity.moveTaskToBack(true);
2606 return;
2607 }
John Reckef654f12011-07-12 16:42:08 -07002608 if (current.canGoBack()) {
2609 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002610 } else {
2611 // Check to see if we are closing a window that was created by
2612 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002613 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002614 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002615 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002616 // Now we close the other tab
2617 closeTab(current);
2618 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002619 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002620 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002621 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002622 /*
2623 * Instead of finishing the activity, simply push this to the back
2624 * of the stack and let ActivityManager to choose the foreground
2625 * activity. As BrowserActivity is singleTask, it will be always the
2626 * root of the task. So we can use either true or false for
2627 * moveTaskToBack().
2628 */
2629 mActivity.moveTaskToBack(true);
2630 }
2631 }
2632 }
2633
2634 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002635 * helper method for key handler
2636 * returns the current tab if it can't advance
2637 */
Michael Kolbc831b632011-05-11 09:30:34 -07002638 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002639 int pos = mTabControl.getCurrentPosition() + 1;
2640 if (pos >= mTabControl.getTabCount()) {
2641 pos = 0;
2642 }
2643 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002644 }
2645
2646 /**
2647 * helper method for key handler
2648 * returns the current tab if it can't advance
2649 */
Michael Kolbc831b632011-05-11 09:30:34 -07002650 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002651 int pos = mTabControl.getCurrentPosition() - 1;
2652 if ( pos < 0) {
2653 pos = mTabControl.getTabCount() - 1;
2654 }
2655 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002656 }
2657
John Reckbcef87f2012-02-03 14:58:34 -08002658 boolean isMenuOrCtrlKey(int keyCode) {
2659 return (KeyEvent.KEYCODE_MENU == keyCode)
2660 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2661 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2662 }
2663
Michael Kolb0035fad2011-03-14 13:25:28 -07002664 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002665 * handle key events in browser
2666 *
2667 * @param keyCode
2668 * @param event
2669 * @return true if handled, false to pass to super
2670 */
John Reck9c35b9c2012-05-30 10:08:50 -07002671 @Override
2672 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002673 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002674 // Even if MENU is already held down, we need to call to super to open
2675 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002676 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002677 mMenuIsDown = true;
2678 return false;
2679 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002680
Cary Clark8ff8c662010-12-29 15:03:05 -05002681 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002682 Tab tab = getCurrentTab();
2683 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002684
Cary Clark160bbb92011-01-10 11:17:07 -05002685 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2686 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002687
Michael Kolb8233fac2010-10-26 16:08:53 -07002688 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002689 case KeyEvent.KEYCODE_TAB:
2690 if (event.isCtrlPressed()) {
2691 if (event.isShiftPressed()) {
2692 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002693 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002694 } else {
2695 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002696 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002697 }
2698 return true;
2699 }
2700 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002701 case KeyEvent.KEYCODE_SPACE:
2702 // WebView/WebTextView handle the keys in the KeyDown. As
2703 // the Activity's shortcut keys are only handled when WebView
2704 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002705 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002706 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002707 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002708 pageDown();
2709 }
2710 return true;
2711 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002712 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002713 event.startTracking();
2714 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002715 case KeyEvent.KEYCODE_FORWARD:
2716 if (!noModifiers) break;
2717 tab.goForward();
2718 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002719 case KeyEvent.KEYCODE_DPAD_LEFT:
2720 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002721 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002722 return true;
2723 }
2724 break;
2725 case KeyEvent.KEYCODE_DPAD_RIGHT:
2726 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002727 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002728 return true;
2729 }
2730 break;
2731 case KeyEvent.KEYCODE_A:
2732 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002733 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002734 return true;
2735 }
2736 break;
Michael Kolba4183062011-01-16 10:43:21 -08002737// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002738 case KeyEvent.KEYCODE_C:
2739 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002740 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002741 return true;
2742 }
2743 break;
Michael Kolba4183062011-01-16 10:43:21 -08002744// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002745// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002746// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002747// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002748// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002749// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002750// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002751// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002752// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002753// case KeyEvent.KEYCODE_M: // unused
2754// case KeyEvent.KEYCODE_N: // in Chrome: new window
2755// case KeyEvent.KEYCODE_O: // in Chrome: open file
2756// case KeyEvent.KEYCODE_P: // in Chrome: print page
2757// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002758// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002759// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2760 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002761 // we can't use the ctrl/shift flags, they check for
2762 // exclusive use of a modifier
2763 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002764 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002765 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002766 } else {
2767 openTabToHomePage();
2768 }
2769 return true;
2770 }
2771 break;
2772// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2773// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002774// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002775// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2776// case KeyEvent.KEYCODE_Y: // unused
2777// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002778 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002779 // it is a regular key and webview is not null
2780 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002781 }
2782
John Reck9c35b9c2012-05-30 10:08:50 -07002783 @Override
2784 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002785 switch(keyCode) {
2786 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002787 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002788 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002789 return true;
2790 }
2791 break;
2792 }
2793 return false;
2794 }
2795
John Reck9c35b9c2012-05-30 10:08:50 -07002796 @Override
2797 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002798 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002799 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002800 if (KeyEvent.KEYCODE_MENU == keyCode
2801 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002802 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002803 }
2804 }
Cary Clark160bbb92011-01-10 11:17:07 -05002805 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002806 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002807 case KeyEvent.KEYCODE_BACK:
2808 if (event.isTracking() && !event.isCanceled()) {
2809 onBackKey();
2810 return true;
2811 }
2812 break;
2813 }
2814 return false;
2815 }
2816
2817 public boolean isMenuDown() {
2818 return mMenuIsDown;
2819 }
2820
John Reck9c35b9c2012-05-30 10:08:50 -07002821 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002822 public void setupAutoFill(Message message) {
2823 // Open the settings activity at the AutoFill profile fragment so that
2824 // the user can create a new profile. When they return, we will dispatch
2825 // the message so that we can autofill the form using their new profile.
2826 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2827 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2828 AutoFillSettingsFragment.class.getName());
2829 mAutoFillSetupMessage = message;
2830 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2831 }
John Reckb3417f02011-01-14 11:01:05 -08002832
John Reck9c35b9c2012-05-30 10:08:50 -07002833 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002834 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002835 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002836 return true;
2837 }
2838
John Reck1cf4b792011-07-26 10:22:22 -07002839 @Override
2840 public boolean shouldCaptureThumbnails() {
2841 return mUi.shouldCaptureThumbnails();
2842 }
2843
Michael Kolbc3af0672011-08-09 10:24:41 -07002844 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002845 public boolean supportsVoice() {
2846 PackageManager pm = mActivity.getPackageManager();
2847 List activities = pm.queryIntentActivities(new Intent(
2848 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2849 return activities.size() != 0;
2850 }
2851
2852 @Override
2853 public void startVoiceRecognizer() {
2854 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2855 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2856 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2857 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2858 mActivity.startActivityForResult(voice, VOICE_RESULT);
2859 }
2860
2861 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002862 public void setBlockEvents(boolean block) {
2863 mBlockEvents = block;
2864 }
2865
John Reck9c35b9c2012-05-30 10:08:50 -07002866 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002867 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002868 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002869 }
2870
John Reck9c35b9c2012-05-30 10:08:50 -07002871 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002872 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002873 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002874 }
2875
John Reck9c35b9c2012-05-30 10:08:50 -07002876 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002877 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002878 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002879 }
2880
John Reck9c35b9c2012-05-30 10:08:50 -07002881 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002882 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002883 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002884 }
2885
John Reck9c35b9c2012-05-30 10:08:50 -07002886 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002887 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002888 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002889 }
2890
Michael Kolb8233fac2010-10-26 16:08:53 -07002891}