blob: e901d4518a016c37a1ce1e7e4b1a514d16704869 [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
1582 case R.id.goto_menu_id:
1583 editUrl();
1584 break;
1585
1586 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001587 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1588 break;
1589
1590 case R.id.history_menu_id:
1591 bookmarksOrHistoryPicker(ComboViews.History);
1592 break;
1593
1594 case R.id.snapshots_menu_id:
1595 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001596 break;
1597
Michael Kolb8233fac2010-10-26 16:08:53 -07001598 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001599 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 break;
1601
1602 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001603 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001604 stopLoading();
1605 } else {
1606 getCurrentTopWebView().reload();
1607 }
1608 break;
1609
1610 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001611 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001612 break;
1613
1614 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001615 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 break;
1617
1618 case R.id.close_menu_id:
1619 // Close the subwindow if it exists.
1620 if (mTabControl.getCurrentSubWindow() != null) {
1621 dismissSubWindow(mTabControl.getCurrentTab());
1622 break;
1623 }
1624 closeCurrentTab();
1625 break;
1626
1627 case R.id.homepage_menu_id:
1628 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001629 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001630 break;
1631
1632 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001633 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001634 break;
1635
1636 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001637 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001638 break;
1639
John Reck2bc80422011-06-30 15:11:49 -07001640 case R.id.save_snapshot_menu_id:
1641 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001642 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001643 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001644 break;
1645
Michael Kolb8233fac2010-10-26 16:08:53 -07001646 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001647 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001648 break;
1649
John Recke1a03a32011-09-14 17:04:16 -07001650 case R.id.snapshot_go_live:
1651 goLive();
1652 return true;
1653
Michael Kolb8233fac2010-10-26 16:08:53 -07001654 case R.id.share_page_menu_id:
1655 Tab currentTab = mTabControl.getCurrentTab();
1656 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001657 return false;
1658 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001659 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001660 break;
1661
1662 case R.id.dump_nav_menu_id:
1663 getCurrentTopWebView().debugDump();
1664 break;
1665
Michael Kolb8233fac2010-10-26 16:08:53 -07001666 case R.id.zoom_in_menu_id:
1667 getCurrentTopWebView().zoomIn();
1668 break;
1669
1670 case R.id.zoom_out_menu_id:
1671 getCurrentTopWebView().zoomOut();
1672 break;
1673
1674 case R.id.view_downloads_menu_id:
1675 viewDownloads();
1676 break;
1677
John Reck42229bc2011-08-19 13:26:43 -07001678 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001679 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001680 break;
1681
Michael Kolb8233fac2010-10-26 16:08:53 -07001682 case R.id.window_one_menu_id:
1683 case R.id.window_two_menu_id:
1684 case R.id.window_three_menu_id:
1685 case R.id.window_four_menu_id:
1686 case R.id.window_five_menu_id:
1687 case R.id.window_six_menu_id:
1688 case R.id.window_seven_menu_id:
1689 case R.id.window_eight_menu_id:
1690 {
1691 int menuid = item.getItemId();
1692 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1693 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1694 Tab desiredTab = mTabControl.getTab(id);
1695 if (desiredTab != null &&
1696 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001697 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001698 }
1699 break;
1700 }
1701 }
1702 }
1703 break;
1704
1705 default:
1706 return false;
1707 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001708 return true;
1709 }
1710
John Reck68234a92012-04-19 15:27:12 -07001711 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1712 implements OnCancelListener {
1713
1714 private Tab mTab;
1715 private Dialog mProgressDialog;
1716 private ContentValues mValues;
1717
1718 private SaveSnapshotTask(Tab tab) {
1719 mTab = tab;
1720 }
1721
1722 @Override
1723 protected void onPreExecute() {
1724 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1725 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1726 true, true, this);
1727 mValues = mTab.createSnapshotValues();
1728 }
1729
1730 @Override
1731 protected Long doInBackground(Void... params) {
1732 if (!mTab.saveViewState(mValues)) {
1733 return null;
1734 }
1735 if (isCancelled()) {
1736 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1737 File file = mActivity.getFileStreamPath(path);
1738 if (!file.delete()) {
1739 file.deleteOnExit();
1740 }
1741 return null;
1742 }
1743 final ContentResolver cr = mActivity.getContentResolver();
1744 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1745 if (result == null) {
1746 return null;
1747 }
1748 long id = ContentUris.parseId(result);
1749 return id;
1750 }
1751
1752 @Override
1753 protected void onPostExecute(Long id) {
1754 if (isCancelled()) {
1755 return;
1756 }
1757 mProgressDialog.dismiss();
1758 if (id == null) {
1759 Toast.makeText(mActivity, R.string.snapshot_failed,
1760 Toast.LENGTH_SHORT).show();
1761 return;
1762 }
1763 Bundle b = new Bundle();
1764 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1765 mUi.showComboView(ComboViews.Snapshots, b);
1766 }
1767
1768 @Override
1769 public void onCancel(DialogInterface dialog) {
1770 cancel(true);
1771 }
1772 }
1773
Michael Kolb80f75082012-04-10 10:50:06 -07001774 @Override
1775 public void toggleUserAgent() {
1776 WebView web = getCurrentWebView();
1777 mSettings.toggleDesktopUseragent(web);
1778 web.loadUrl(web.getOriginalUrl());
1779 }
1780
1781 @Override
1782 public void findOnPage() {
1783 getCurrentTopWebView().showFindDialog(null, true);
1784 }
1785
1786 @Override
1787 public void openPreferences() {
1788 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1789 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1790 getCurrentTopWebView().getUrl());
1791 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1792 }
1793
1794 @Override
1795 public void bookmarkCurrentPage() {
1796 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1797 if (bookmarkIntent != null) {
1798 mActivity.startActivity(bookmarkIntent);
1799 }
1800 }
1801
John Recke1a03a32011-09-14 17:04:16 -07001802 private void goLive() {
1803 Tab t = getCurrentTab();
1804 t.loadUrl(t.getUrl(), null);
1805 }
1806
Michael Kolb315d5022011-10-13 12:47:11 -07001807 @Override
1808 public void showPageInfo() {
1809 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1810 }
1811
John Reck9c35b9c2012-05-30 10:08:50 -07001812 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001813 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001814 // Let the History and Bookmark fragments handle menus they created.
1815 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1816 return false;
1817 }
1818
Michael Kolb8233fac2010-10-26 16:08:53 -07001819 int id = item.getItemId();
1820 boolean result = true;
1821 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001822 // -- Browser context menu
1823 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001824 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001825 case R.id.copy_link_context_menu_id:
1826 final WebView webView = getCurrentTopWebView();
1827 if (null == webView) {
1828 result = false;
1829 break;
1830 }
1831 final HashMap<String, WebView> hrefMap =
1832 new HashMap<String, WebView>();
1833 hrefMap.put("webview", webView);
1834 final Message msg = mHandler.obtainMessage(
1835 FOCUS_NODE_HREF, id, 0, hrefMap);
1836 webView.requestFocusNodeHref(msg);
1837 break;
1838
1839 default:
1840 // For other context menus
1841 result = onOptionsItemSelected(item);
1842 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001843 return result;
1844 }
1845
1846 /**
1847 * support programmatically opening the context menu
1848 */
1849 public void openContextMenu(View view) {
1850 mActivity.openContextMenu(view);
1851 }
1852
1853 /**
1854 * programmatically open the options menu
1855 */
1856 public void openOptionsMenu() {
1857 mActivity.openOptionsMenu();
1858 }
1859
John Reck9c35b9c2012-05-30 10:08:50 -07001860 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001861 public boolean onMenuOpened(int featureId, Menu menu) {
1862 if (mOptionsMenuOpen) {
1863 if (mConfigChanged) {
1864 // We do not need to make any changes to the state of the
1865 // title bar, since the only thing that happened was a
1866 // change in orientation
1867 mConfigChanged = false;
1868 } else {
1869 if (!mExtendedMenuOpen) {
1870 mExtendedMenuOpen = true;
1871 mUi.onExtendedMenuOpened();
1872 } else {
1873 // Switching the menu back to icon view, so show the
1874 // title bar once again.
1875 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001876 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001877 }
1878 }
1879 } else {
1880 // The options menu is closed, so open it, and show the title
1881 mOptionsMenuOpen = true;
1882 mConfigChanged = false;
1883 mExtendedMenuOpen = false;
1884 mUi.onOptionsMenuOpened();
1885 }
1886 return true;
1887 }
1888
John Reck9c35b9c2012-05-30 10:08:50 -07001889 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001890 public void onOptionsMenuClosed(Menu menu) {
1891 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001892 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001893 }
1894
John Reck9c35b9c2012-05-30 10:08:50 -07001895 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001896 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001897 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001898 }
1899
1900 // Helper method for getting the top window.
1901 @Override
1902 public WebView getCurrentTopWebView() {
1903 return mTabControl.getCurrentTopWebView();
1904 }
1905
1906 @Override
1907 public WebView getCurrentWebView() {
1908 return mTabControl.getCurrentWebView();
1909 }
1910
1911 /*
1912 * This method is called as a result of the user selecting the options
1913 * menu to see the download window. It shows the download window on top of
1914 * the current window.
1915 */
1916 void viewDownloads() {
1917 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1918 mActivity.startActivity(intent);
1919 }
1920
John Reck30b065e2011-07-19 10:58:05 -07001921 int getActionModeHeight() {
1922 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1923 new int[] { android.R.attr.actionBarSize });
1924 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1925 actionBarSizeTypedArray.recycle();
1926 return size;
1927 }
1928
Michael Kolb8233fac2010-10-26 16:08:53 -07001929 // action mode
1930
John Reck9c35b9c2012-05-30 10:08:50 -07001931 @Override
1932 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 mUi.onActionModeStarted(mode);
1934 mActionMode = mode;
1935 }
1936
1937 /*
1938 * True if a custom ActionMode (i.e. find or select) is in use.
1939 */
1940 @Override
1941 public boolean isInCustomActionMode() {
1942 return mActionMode != null;
1943 }
1944
1945 /*
1946 * End the current ActionMode.
1947 */
1948 @Override
1949 public void endActionMode() {
1950 if (mActionMode != null) {
1951 mActionMode.finish();
1952 }
1953 }
1954
1955 /*
1956 * Called by find and select when they are finished. Replace title bars
1957 * as necessary.
1958 */
John Reck9c35b9c2012-05-30 10:08:50 -07001959 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001960 public void onActionModeFinished(ActionMode mode) {
1961 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001962 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 mActionMode = null;
1964 }
1965
1966 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001967 final Tab tab = getCurrentTab();
1968 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07001969 }
1970
1971 // bookmark handling
1972
1973 /**
1974 * add the current page as a bookmark to the given folder id
1975 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001976 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001977 * bookmarked, and if it is, edit that bookmark. If false, and
1978 * the site is already bookmarked, do not attempt to edit the
1979 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001980 */
1981 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001982 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07001983 WebView w = getCurrentTopWebView();
1984 if (w == null) {
1985 return null;
1986 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001987 Intent i = new Intent(mActivity,
1988 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07001989 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1990 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1991 String touchIconUrl = w.getTouchIconUrl();
1992 if (touchIconUrl != null) {
1993 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1994 WebSettings settings = w.getSettings();
1995 if (settings != null) {
1996 i.putExtra(AddBookmarkPage.USER_AGENT,
1997 settings.getUserAgentString());
1998 }
1999 }
2000 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2001 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2002 getDesiredThumbnailHeight(mActivity)));
2003 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002004 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002005 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2006 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002007 // Put the dialog at the upper right of the screen, covering the
2008 // star on the title bar.
2009 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002010 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002011 }
2012
2013 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002014 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002015 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002016 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002017 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002018 }
2019
2020 // thumbnails
2021
2022 /**
2023 * Return the desired width for thumbnail screenshots, which are stored in
2024 * the database, and used on the bookmarks screen.
2025 * @param context Context for finding out the density of the screen.
2026 * @return desired width for thumbnail screenshot.
2027 */
2028 static int getDesiredThumbnailWidth(Context context) {
2029 return context.getResources().getDimensionPixelOffset(
2030 R.dimen.bookmarkThumbnailWidth);
2031 }
2032
2033 /**
2034 * Return the desired height for thumbnail screenshots, which are stored in
2035 * the database, and used on the bookmarks screen.
2036 * @param context Context for finding out the density of the screen.
2037 * @return desired height for thumbnail screenshot.
2038 */
2039 static int getDesiredThumbnailHeight(Context context) {
2040 return context.getResources().getDimensionPixelOffset(
2041 R.dimen.bookmarkThumbnailHeight);
2042 }
2043
John Reck8cc92352011-07-06 17:41:52 -07002044 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002045 if (view == null || view.getContentHeight() == 0
2046 || view.getContentWidth() == 0) {
2047 return null;
2048 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002049 // We render to a bitmap 2x the desired size so that we can then
2050 // re-scale it with filtering since canvas.scale doesn't filter
2051 // This helps reduce aliasing at the cost of being slightly blurry
2052 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002053 int scaledWidth = width * filter_scale;
2054 int scaledHeight = height * filter_scale;
2055 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2056 || sThumbnailBitmap.getHeight() != scaledHeight) {
2057 if (sThumbnailBitmap != null) {
2058 sThumbnailBitmap.recycle();
2059 sThumbnailBitmap = null;
2060 }
2061 sThumbnailBitmap =
2062 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002063 }
John Reckd7dd9b22011-08-30 09:18:29 -07002064 Canvas canvas = new Canvas(sThumbnailBitmap);
2065 int contentWidth = view.getContentWidth();
2066 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2067 if (view instanceof BrowserWebView) {
2068 int dy = -((BrowserWebView)view).getTitleHeight();
2069 canvas.translate(0, dy * overviewScale);
2070 }
2071
2072 canvas.scale(overviewScale, overviewScale);
2073
2074 if (view instanceof BrowserWebView) {
2075 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002076 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002077 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002078 }
John Reckd7dd9b22011-08-30 09:18:29 -07002079 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2080 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002081 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002082 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002083 }
2084
John Reck34ef2672011-02-10 11:30:55 -08002085 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002086 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002087 // FIXME: Would like to make sure there is actually something to
2088 // draw, but the API for that (WebViewCore.pictureReady()) is not
2089 // currently accessible here.
2090
John Reck34ef2672011-02-10 11:30:55 -08002091 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002092 if (view == null) {
2093 // Tab was destroyed
2094 return;
2095 }
John Reck34ef2672011-02-10 11:30:55 -08002096 final String url = tab.getUrl();
2097 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002098 if (TextUtils.isEmpty(url)) {
2099 return;
2100 }
2101
2102 // Only update thumbnails for web urls (http(s)://), not for
2103 // about:, javascript:, data:, etc...
2104 // Unless it is a bookmarked site, then always update
2105 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2106 return;
2107 }
2108
Michael Kolb8233fac2010-10-26 16:08:53 -07002109 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2110 getDesiredThumbnailHeight(mActivity));
2111 if (bm == null) {
2112 return;
2113 }
2114
2115 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002116 new AsyncTask<Void, Void, Void>() {
2117 @Override
2118 protected Void doInBackground(Void... unused) {
2119 Cursor cursor = null;
2120 try {
2121 // TODO: Clean this up
2122 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2123 if (cursor != null && cursor.moveToFirst()) {
2124 final ByteArrayOutputStream os =
2125 new ByteArrayOutputStream();
2126 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002127
John Reck34ef2672011-02-10 11:30:55 -08002128 ContentValues values = new ContentValues();
2129 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002130
John Reck34ef2672011-02-10 11:30:55 -08002131 do {
John Reck617fd832011-02-16 14:35:59 -08002132 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002133 cr.update(Images.CONTENT_URI, values, null, null);
2134 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002135 }
John Reck34ef2672011-02-10 11:30:55 -08002136 } catch (IllegalStateException e) {
2137 // Ignore
2138 } finally {
2139 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002140 }
John Reck34ef2672011-02-10 11:30:55 -08002141 return null;
2142 }
2143 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002144 }
2145
2146 private class Copy implements OnMenuItemClickListener {
2147 private CharSequence mText;
2148
John Reck9c35b9c2012-05-30 10:08:50 -07002149 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002150 public boolean onMenuItemClick(MenuItem item) {
2151 copy(mText);
2152 return true;
2153 }
2154
2155 public Copy(CharSequence toCopy) {
2156 mText = toCopy;
2157 }
2158 }
2159
Leon Scroggins63c02662010-11-18 15:16:27 -05002160 private static class Download implements OnMenuItemClickListener {
2161 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002162 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002163 private boolean mPrivateBrowsing;
George Mount387d45d2011-10-07 15:57:53 -07002164 private static final String FALLBACK_EXTENSION = "dat";
2165 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002166
John Reck9c35b9c2012-05-30 10:08:50 -07002167 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002168 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002169 if (DataUri.isDataUri(mText)) {
2170 saveDataUri();
2171 } else {
2172 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
2173 null, null, mPrivateBrowsing);
2174 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002175 return true;
2176 }
2177
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002178 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002179 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002180 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002181 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002182 }
George Mount387d45d2011-10-07 15:57:53 -07002183
2184 /**
2185 * Treats mText as a data URI and writes its contents to a file
2186 * based on the current time.
2187 */
2188 private void saveDataUri() {
2189 FileOutputStream outputStream = null;
2190 try {
2191 DataUri uri = new DataUri(mText);
2192 File target = getTarget(uri);
2193 outputStream = new FileOutputStream(target);
2194 outputStream.write(uri.getData());
2195 final DownloadManager manager =
2196 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2197 manager.addCompletedDownload(target.getName(),
2198 mActivity.getTitle().toString(), false,
2199 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002200 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002201 } catch (IOException e) {
2202 Log.e(LOGTAG, "Could not save data URL");
2203 } finally {
2204 if (outputStream != null) {
2205 try {
2206 outputStream.close();
2207 } catch (IOException e) {
2208 // ignore close errors
2209 }
2210 }
2211 }
2212 }
2213
2214 /**
2215 * Creates a File based on the current time stamp and uses
2216 * the mime type of the DataUri to get the extension.
2217 */
2218 private File getTarget(DataUri uri) throws IOException {
2219 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
2220 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT);
2221 String nameBase = format.format(new Date());
2222 String mimeType = uri.getMimeType();
2223 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2224 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2225 if (extension == null) {
2226 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2227 extension = FALLBACK_EXTENSION;
2228 }
2229 extension = "." + extension; // createTempFile needs the '.'
2230 File targetFile = File.createTempFile(nameBase, extension, dir);
2231 return targetFile;
2232 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002233 }
2234
Cary Clark8974d282010-11-22 10:46:05 -05002235 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002236 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002237
John Reck9c35b9c2012-05-30 10:08:50 -07002238 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002239 public boolean onMenuItemClick(MenuItem item) {
2240 if (mWebView != null) {
2241 return mWebView.selectText();
2242 }
2243 return false;
2244 }
2245
2246 public SelectText(WebView webView) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002247 mWebView = WebViewClassic.fromWebView(webView);
Cary Clark8974d282010-11-22 10:46:05 -05002248 }
2249
2250 }
2251
Michael Kolb8233fac2010-10-26 16:08:53 -07002252 /********************** TODO: UI stuff *****************************/
2253
2254 // these methods have been copied, they still need to be cleaned up
2255
2256 /****************** tabs ***************************************************/
2257
2258 // basic tab interactions:
2259
2260 // it is assumed that tabcontrol already knows about the tab
2261 protected void addTab(Tab tab) {
2262 mUi.addTab(tab);
2263 }
2264
2265 protected void removeTab(Tab tab) {
2266 mUi.removeTab(tab);
2267 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002268 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002269 }
2270
Michael Kolbf2055602011-04-09 17:20:03 -07002271 @Override
2272 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002273 // monkey protection against delayed start
2274 if (tab != null) {
2275 mTabControl.setCurrentTab(tab);
2276 // the tab is guaranteed to have a webview after setCurrentTab
2277 mUi.setActiveTab(tab);
2278 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002279 }
2280
John Reck8bcafc12011-08-29 16:43:02 -07002281 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002282 Tab current = mTabControl.getCurrentTab();
2283 if (current != null
2284 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002285 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002286 }
2287 }
2288
John Reck26b18322011-06-21 13:08:58 -07002289 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002290 // Dismiss the subwindow if applicable.
2291 dismissSubWindow(appTab);
2292 // Since we might kill the WebView, remove it from the
2293 // content view first.
2294 mUi.detachTab(appTab);
2295 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002296 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002297 // TODO: analyze why the remove and add are necessary
2298 mUi.attachTab(appTab);
2299 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002300 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002301 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 } else {
2303 // If the tab was the current tab, we have to attach
2304 // it to the view system again.
2305 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002306 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002307 }
2308 }
2309
2310 // Remove the sub window if it exists. Also called by TabControl when the
2311 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002312 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002313 public void dismissSubWindow(Tab tab) {
2314 removeSubWindow(tab);
2315 // dismiss the subwindow. This will destroy the WebView.
2316 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002317 WebView wv = getCurrentTopWebView();
2318 if (wv != null) {
2319 wv.requestFocus();
2320 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002321 }
2322
2323 @Override
2324 public void removeSubWindow(Tab t) {
2325 if (t.getSubWebView() != null) {
2326 mUi.removeSubWindow(t.getSubViewContainer());
2327 }
2328 }
2329
2330 @Override
2331 public void attachSubWindow(Tab tab) {
2332 if (tab.getSubWebView() != null) {
2333 mUi.attachSubWindow(tab.getSubViewContainer());
2334 getCurrentTopWebView().requestFocus();
2335 }
2336 }
2337
Mathew Inwood29721c22011-06-29 17:55:24 +01002338 private Tab showPreloadedTab(final UrlData urlData) {
2339 if (!urlData.isPreloaded()) {
2340 return null;
2341 }
2342 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2343 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2344 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002345 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002346 // Could not submit query. Fallback to regular tab creation
2347 tabControl.destroy();
2348 return null;
2349 }
2350 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002351 // check tab count and make room for new tab
2352 if (!mTabControl.canCreateNewTab()) {
2353 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2354 if (leastUsed != null) {
2355 closeTab(leastUsed);
2356 }
2357 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002358 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002359 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002360 mTabControl.addPreloadedTab(t);
2361 addTab(t);
2362 setActiveTab(t);
2363 return t;
2364 }
2365
Michael Kolb7bcafde2011-05-09 13:55:59 -07002366 // open a non inconito tab with the given url data
2367 // and set as active tab
2368 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002369 Tab tab = showPreloadedTab(urlData);
2370 if (tab == null) {
2371 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002372 if ((tab != null) && !urlData.isEmpty()) {
2373 loadUrlDataIn(tab, urlData);
2374 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002375 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002376 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002377 }
2378
Michael Kolb843510f2010-12-09 10:51:49 -08002379 @Override
2380 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002381 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002382 }
2383
Michael Kolb8233fac2010-10-26 16:08:53 -07002384 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002385 public Tab openIncognitoTab() {
2386 return openTab(INCOGNITO_URI, true, true, false);
2387 }
2388
2389 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002390 public Tab openTab(String url, boolean incognito, boolean setActive,
2391 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002392 return openTab(url, incognito, setActive, useCurrent, null);
2393 }
2394
2395 @Override
2396 public Tab openTab(String url, Tab parent, boolean setActive,
2397 boolean useCurrent) {
2398 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2399 setActive, useCurrent, parent);
2400 }
2401
2402 public Tab openTab(String url, boolean incognito, boolean setActive,
2403 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002404 Tab tab = createNewTab(incognito, setActive, useCurrent);
2405 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002406 if (parent != null && parent != tab) {
2407 parent.addChildTab(tab);
2408 }
Michael Kolb519d2282011-05-09 17:03:19 -07002409 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002410 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002411 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002412 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002413 return tab;
2414 }
2415
2416 // this method will attempt to create a new tab
2417 // incognito: private browsing tab
2418 // setActive: ste tab as current tab
2419 // useCurrent: if no new tab can be created, return current tab
2420 private Tab createNewTab(boolean incognito, boolean setActive,
2421 boolean useCurrent) {
2422 Tab tab = null;
2423 if (mTabControl.canCreateNewTab()) {
2424 tab = mTabControl.createNewTab(incognito);
2425 addTab(tab);
2426 if (setActive) {
2427 setActiveTab(tab);
2428 }
2429 } else {
2430 if (useCurrent) {
2431 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002432 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002433 } else {
2434 mUi.showMaxTabsWarning();
2435 }
2436 }
2437 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002438 }
2439
John Reck2bc80422011-06-30 15:11:49 -07002440 @Override
2441 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2442 SnapshotTab tab = null;
2443 if (mTabControl.canCreateNewTab()) {
2444 tab = mTabControl.createSnapshotTab(snapshotId);
2445 addTab(tab);
2446 if (setActive) {
2447 setActiveTab(tab);
2448 }
2449 } else {
2450 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002451 }
2452 return tab;
2453 }
2454
Michael Kolb8233fac2010-10-26 16:08:53 -07002455 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002456 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002457 * @return boolean True if we successfully switched to a different tab. If
2458 * the indexth tab is null, or if that tab is the same as
2459 * the current one, return false.
2460 */
2461 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002462 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002463 Tab currentTab = mTabControl.getCurrentTab();
2464 if (tab == null || tab == currentTab) {
2465 return false;
2466 }
2467 setActiveTab(tab);
2468 return true;
2469 }
2470
2471 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002472 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002473 closeCurrentTab(false);
2474 }
2475
2476 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002477 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002478 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002479 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002480 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002481 return;
2482 }
Michael Kolbc831b632011-05-11 09:30:34 -07002483 final Tab current = mTabControl.getCurrentTab();
2484 final int pos = mTabControl.getCurrentPosition();
2485 Tab newTab = current.getParent();
2486 if (newTab == null) {
2487 newTab = mTabControl.getTab(pos + 1);
2488 if (newTab == null) {
2489 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002490 }
2491 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002492 if (andQuit) {
2493 mTabControl.setCurrentTab(newTab);
2494 closeTab(current);
2495 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002496 // Close window
2497 closeTab(current);
2498 }
2499 }
2500
2501 /**
2502 * Close the tab, remove its associated title bar, and adjust mTabControl's
2503 * current tab to a valid value.
2504 */
2505 @Override
2506 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002507 if (tab == mTabControl.getCurrentTab()) {
2508 closeCurrentTab();
2509 } else {
2510 removeTab(tab);
2511 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002512 }
2513
Michael Kolb8233fac2010-10-26 16:08:53 -07002514 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002515 protected void loadUrlFromContext(String url) {
2516 Tab tab = getCurrentTab();
2517 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002518 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002519 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002520 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002521 if (!WebViewClassic.fromWebView(view).getWebViewClient().
2522 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002523 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002524 }
2525 }
2526 }
2527
2528 /**
2529 * Load the URL into the given WebView and update the title bar
2530 * to reflect the new load. Call this instead of WebView.loadUrl
2531 * directly.
2532 * @param view The WebView used to load url.
2533 * @param url The URL to load.
2534 */
John Reck71e51422011-07-01 16:49:28 -07002535 @Override
2536 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002537 loadUrl(tab, url, null);
2538 }
2539
2540 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2541 if (tab != null) {
2542 dismissSubWindow(tab);
2543 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002544 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002545 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002546 }
2547
2548 /**
2549 * Load UrlData into a Tab and update the title bar to reflect the new
2550 * load. Call this instead of UrlData.loadIn directly.
2551 * @param t The Tab used to load.
2552 * @param data The UrlData being loaded.
2553 */
2554 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002555 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002556 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002557 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002558 } else {
2559 loadUrl(t, data.mUrl, data.mHeaders);
2560 }
2561 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002562 }
2563
John Reck30c714c2010-12-16 17:30:34 -08002564 @Override
2565 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002566 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002567 if (tab.canGoBack()) {
2568 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002569 } else {
John Reckef654f12011-07-12 16:42:08 -07002570 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002571 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002572 }
2573
2574 void goBackOnePageOrQuit() {
2575 Tab current = mTabControl.getCurrentTab();
2576 if (current == null) {
2577 /*
2578 * Instead of finishing the activity, simply push this to the back
2579 * of the stack and let ActivityManager to choose the foreground
2580 * activity. As BrowserActivity is singleTask, it will be always the
2581 * root of the task. So we can use either true or false for
2582 * moveTaskToBack().
2583 */
2584 mActivity.moveTaskToBack(true);
2585 return;
2586 }
John Reckef654f12011-07-12 16:42:08 -07002587 if (current.canGoBack()) {
2588 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002589 } else {
2590 // Check to see if we are closing a window that was created by
2591 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002592 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002593 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002594 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002595 // Now we close the other tab
2596 closeTab(current);
2597 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002598 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002599 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002600 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002601 /*
2602 * Instead of finishing the activity, simply push this to the back
2603 * of the stack and let ActivityManager to choose the foreground
2604 * activity. As BrowserActivity is singleTask, it will be always the
2605 * root of the task. So we can use either true or false for
2606 * moveTaskToBack().
2607 */
2608 mActivity.moveTaskToBack(true);
2609 }
2610 }
2611 }
2612
2613 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002614 * helper method for key handler
2615 * returns the current tab if it can't advance
2616 */
Michael Kolbc831b632011-05-11 09:30:34 -07002617 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002618 int pos = mTabControl.getCurrentPosition() + 1;
2619 if (pos >= mTabControl.getTabCount()) {
2620 pos = 0;
2621 }
2622 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002623 }
2624
2625 /**
2626 * helper method for key handler
2627 * returns the current tab if it can't advance
2628 */
Michael Kolbc831b632011-05-11 09:30:34 -07002629 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002630 int pos = mTabControl.getCurrentPosition() - 1;
2631 if ( pos < 0) {
2632 pos = mTabControl.getTabCount() - 1;
2633 }
2634 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002635 }
2636
John Reckbcef87f2012-02-03 14:58:34 -08002637 boolean isMenuOrCtrlKey(int keyCode) {
2638 return (KeyEvent.KEYCODE_MENU == keyCode)
2639 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2640 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2641 }
2642
Michael Kolb0035fad2011-03-14 13:25:28 -07002643 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002644 * handle key events in browser
2645 *
2646 * @param keyCode
2647 * @param event
2648 * @return true if handled, false to pass to super
2649 */
John Reck9c35b9c2012-05-30 10:08:50 -07002650 @Override
2651 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002652 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002653 // Even if MENU is already held down, we need to call to super to open
2654 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002655 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002656 mMenuIsDown = true;
2657 return false;
2658 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002659
Cary Clark8ff8c662010-12-29 15:03:05 -05002660 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002661 Tab tab = getCurrentTab();
2662 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002663
Cary Clark160bbb92011-01-10 11:17:07 -05002664 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2665 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002666
Michael Kolb8233fac2010-10-26 16:08:53 -07002667 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002668 case KeyEvent.KEYCODE_TAB:
2669 if (event.isCtrlPressed()) {
2670 if (event.isShiftPressed()) {
2671 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002672 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002673 } else {
2674 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002675 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002676 }
2677 return true;
2678 }
2679 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002680 case KeyEvent.KEYCODE_SPACE:
2681 // WebView/WebTextView handle the keys in the KeyDown. As
2682 // the Activity's shortcut keys are only handled when WebView
2683 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002684 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002685 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002686 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002687 pageDown();
2688 }
2689 return true;
2690 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002691 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002692 event.startTracking();
2693 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002694 case KeyEvent.KEYCODE_FORWARD:
2695 if (!noModifiers) break;
2696 tab.goForward();
2697 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002698 case KeyEvent.KEYCODE_DPAD_LEFT:
2699 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002700 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002701 return true;
2702 }
2703 break;
2704 case KeyEvent.KEYCODE_DPAD_RIGHT:
2705 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002706 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002707 return true;
2708 }
2709 break;
2710 case KeyEvent.KEYCODE_A:
2711 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002712 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002713 return true;
2714 }
2715 break;
Michael Kolba4183062011-01-16 10:43:21 -08002716// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002717 case KeyEvent.KEYCODE_C:
2718 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002719 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002720 return true;
2721 }
2722 break;
Michael Kolba4183062011-01-16 10:43:21 -08002723// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002724// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002725// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002726// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002727// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002728// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002729// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002730// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002731// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002732// case KeyEvent.KEYCODE_M: // unused
2733// case KeyEvent.KEYCODE_N: // in Chrome: new window
2734// case KeyEvent.KEYCODE_O: // in Chrome: open file
2735// case KeyEvent.KEYCODE_P: // in Chrome: print page
2736// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002737// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002738// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2739 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002740 // we can't use the ctrl/shift flags, they check for
2741 // exclusive use of a modifier
2742 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002743 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002744 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002745 } else {
2746 openTabToHomePage();
2747 }
2748 return true;
2749 }
2750 break;
2751// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2752// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002753// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002754// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2755// case KeyEvent.KEYCODE_Y: // unused
2756// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002757 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002758 // it is a regular key and webview is not null
2759 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002760 }
2761
John Reck9c35b9c2012-05-30 10:08:50 -07002762 @Override
2763 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002764 switch(keyCode) {
2765 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002766 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002767 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002768 return true;
2769 }
2770 break;
2771 }
2772 return false;
2773 }
2774
John Reck9c35b9c2012-05-30 10:08:50 -07002775 @Override
2776 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002777 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002778 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002779 if (KeyEvent.KEYCODE_MENU == keyCode
2780 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002781 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002782 }
2783 }
Cary Clark160bbb92011-01-10 11:17:07 -05002784 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002785 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002786 case KeyEvent.KEYCODE_BACK:
2787 if (event.isTracking() && !event.isCanceled()) {
2788 onBackKey();
2789 return true;
2790 }
2791 break;
2792 }
2793 return false;
2794 }
2795
2796 public boolean isMenuDown() {
2797 return mMenuIsDown;
2798 }
2799
John Reck9c35b9c2012-05-30 10:08:50 -07002800 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002801 public void setupAutoFill(Message message) {
2802 // Open the settings activity at the AutoFill profile fragment so that
2803 // the user can create a new profile. When they return, we will dispatch
2804 // the message so that we can autofill the form using their new profile.
2805 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2806 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2807 AutoFillSettingsFragment.class.getName());
2808 mAutoFillSetupMessage = message;
2809 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2810 }
John Reckb3417f02011-01-14 11:01:05 -08002811
John Reck9c35b9c2012-05-30 10:08:50 -07002812 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002813 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002814 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002815 return true;
2816 }
2817
John Reck1cf4b792011-07-26 10:22:22 -07002818 @Override
2819 public boolean shouldCaptureThumbnails() {
2820 return mUi.shouldCaptureThumbnails();
2821 }
2822
Michael Kolbc3af0672011-08-09 10:24:41 -07002823 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002824 public boolean supportsVoice() {
2825 PackageManager pm = mActivity.getPackageManager();
2826 List activities = pm.queryIntentActivities(new Intent(
2827 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2828 return activities.size() != 0;
2829 }
2830
2831 @Override
2832 public void startVoiceRecognizer() {
2833 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2834 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2835 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2836 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2837 mActivity.startActivityForResult(voice, VOICE_RESULT);
2838 }
2839
2840 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002841 public void setBlockEvents(boolean block) {
2842 mBlockEvents = block;
2843 }
2844
John Reck9c35b9c2012-05-30 10:08:50 -07002845 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002846 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002847 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002848 }
2849
John Reck9c35b9c2012-05-30 10:08:50 -07002850 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002851 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002852 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002853 }
2854
John Reck9c35b9c2012-05-30 10:08:50 -07002855 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002856 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002857 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002858 }
2859
John Reck9c35b9c2012-05-30 10:08:50 -07002860 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002861 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002862 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002863 }
2864
John Reck9c35b9c2012-05-30 10:08:50 -07002865 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002866 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002867 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002868 }
2869
Michael Kolb8233fac2010-10-26 16:08:53 -07002870}