blob: ec7ff48c80666029ab6b436ecc5dade4af685555 [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);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200863
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 // Performance probe
865 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800866 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700867 }
868
869 Performance.tracePageFinished();
870 }
871
872 @Override
John Reck30c714c2010-12-16 17:30:34 -0800873 public void onProgressChanged(Tab tab) {
874 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700875
876 if (newProgress == 100) {
877 CookieSyncManager.getInstance().sync();
878 // onProgressChanged() may continue to be called after the main
879 // frame has finished loading, as any remaining sub frames continue
880 // to load. We'll only get called once though with newProgress as
881 // 100 when everything is loaded. (onPageFinished is called once
882 // when the main frame completes loading regardless of the state of
883 // any sub frames so calls to onProgressChanges may continue after
884 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800885 if (tab.inPageLoad()) {
886 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200887 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
888 // pause the WebView timer and release the wake lock if it is
889 // finished while BrowserActivity is in pause state.
890 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700891 }
John Reckd9862372012-02-21 15:04:50 -0800892 if (!tab.isPrivateBrowsingEnabled()
893 && !TextUtils.isEmpty(tab.getUrl())
894 && !tab.isSnapshot()) {
895 // Only update the bookmark screenshot if the user did not
896 // cancel the load early and there is not already
897 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700898 if (tab.shouldUpdateThumbnail() &&
899 (tab.inForeground() && !didUserStopLoading()
900 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800901 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
902 mHandler.sendMessageDelayed(mHandler.obtainMessage(
903 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
904 500);
905 }
906 }
907 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800909 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700910 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800911 // still loading
912 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700913 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800914 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700915 }
916 }
John Reck30c714c2010-12-16 17:30:34 -0800917 mUi.onProgressChanged(tab);
918 }
919
920 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100921 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800922 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700923 }
924
925 @Override
926 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800927 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800928 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800929 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700930 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
931 return;
932 }
933 // Update the title in the history database if not in private browsing mode
934 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700935 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 }
937 }
938
939 @Override
940 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800941 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
943 }
944
945 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800946 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
947 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 }
949
950 @Override
951 public boolean shouldOverrideKeyEvent(KeyEvent event) {
952 if (mMenuIsDown) {
953 // only check shortcut key when MENU is held
954 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
955 event);
956 } else {
957 return false;
958 }
959 }
960
961 @Override
John Reck997b1b72012-04-19 18:08:25 -0700962 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700963 if (!isActivityPaused()) {
964 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700965 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700966 } else {
John Reck997b1b72012-04-19 18:08:25 -0700967 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700968 }
969 }
John Reck997b1b72012-04-19 18:08:25 -0700970 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700971 }
972
973 @Override
John Reck324d4402011-01-11 16:56:42 -0800974 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700975 // Don't save anything in private browsing mode
976 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800977 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700978
John Reck324d4402011-01-11 16:56:42 -0800979 if (TextUtils.isEmpty(url)
980 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700981 return;
982 }
John Reckf57c0292011-07-21 18:15:39 -0700983 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700984 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 }
986
987 @Override
988 public void getVisitedHistory(final ValueCallback<String[]> callback) {
989 AsyncTask<Void, Void, String[]> task =
990 new AsyncTask<Void, Void, String[]>() {
991 @Override
992 public String[] doInBackground(Void... unused) {
993 return Browser.getVisitedHistory(mActivity.getContentResolver());
994 }
995 @Override
996 public void onPostExecute(String[] result) {
997 callback.onReceiveValue(result);
998 }
999 };
1000 task.execute();
1001 }
1002
1003 @Override
1004 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1005 final HttpAuthHandler handler, final String host,
1006 final String realm) {
1007 String username = null;
1008 String password = null;
1009
1010 boolean reuseHttpAuthUsernamePassword
1011 = handler.useHttpAuthUsernamePassword();
1012
1013 if (reuseHttpAuthUsernamePassword && view != null) {
1014 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1015 if (credentials != null && credentials.length == 2) {
1016 username = credentials[0];
1017 password = credentials[1];
1018 }
1019 }
1020
1021 if (username != null && password != null) {
1022 handler.proceed(username, password);
1023 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001024 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001025 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1026 } else {
1027 handler.cancel();
1028 }
1029 }
1030 }
1031
1032 @Override
1033 public void onDownloadStart(Tab tab, String url, String userAgent,
1034 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001035 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -05001036 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001037 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
1038 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 // This Tab was opened for the sole purpose of downloading a
1040 // file. Remove it.
1041 if (tab == mTabControl.getCurrentTab()) {
1042 // In this case, the Tab is still on top.
1043 goBackOnePageOrQuit();
1044 } else {
1045 // In this case, it is not.
1046 closeTab(tab);
1047 }
1048 }
1049 }
1050
1051 @Override
1052 public Bitmap getDefaultVideoPoster() {
1053 return mUi.getDefaultVideoPoster();
1054 }
1055
1056 @Override
1057 public View getVideoLoadingProgressView() {
1058 return mUi.getVideoLoadingProgressView();
1059 }
1060
1061 @Override
1062 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1063 SslError error) {
1064 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1065 }
1066
Patrick Scott92066772011-03-10 08:46:27 -05001067 @Override
1068 public void showAutoLogin(Tab tab) {
1069 assert tab.inForeground();
1070 // Update the title bar to show the auto-login request.
1071 mUi.showAutoLogin(tab);
1072 }
1073
1074 @Override
1075 public void hideAutoLogin(Tab tab) {
1076 assert tab.inForeground();
1077 mUi.hideAutoLogin(tab);
1078 }
1079
Michael Kolb8233fac2010-10-26 16:08:53 -07001080 // helper method
1081
1082 /*
1083 * Update the favorites icon if the private browsing isn't enabled and the
1084 * icon is valid.
1085 */
1086 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1087 final String url, Bitmap favicon) {
1088 if (favicon == null) {
1089 return;
1090 }
1091 if (!tab.isPrivateBrowsingEnabled()) {
1092 Bookmarks.updateFavicon(mActivity
1093 .getContentResolver(), originalUrl, url, favicon);
1094 }
1095 }
1096
Leon Scroggins4cd97792010-12-03 15:31:56 -05001097 @Override
1098 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001099 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001100 mUi.bookmarkedStatusHasChanged(tab);
1101 }
1102
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 // end WebViewController
1104
1105 protected void pageUp() {
1106 getCurrentTopWebView().pageUp(false);
1107 }
1108
1109 protected void pageDown() {
1110 getCurrentTopWebView().pageDown(false);
1111 }
1112
1113 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001114 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 public void editUrl() {
1116 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001117 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001118 }
1119
John Reck9c35b9c2012-05-30 10:08:50 -07001120 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001121 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 WebChromeClient.CustomViewCallback callback) {
1123 if (tab.inForeground()) {
1124 if (mUi.isCustomViewShowing()) {
1125 callback.onCustomViewHidden();
1126 return;
1127 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001128 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001129 // Save the menu state and set it to empty while the custom
1130 // view is showing.
1131 mOldMenuState = mMenuState;
1132 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001133 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001134 }
1135 }
1136
1137 @Override
1138 public void hideCustomView() {
1139 if (mUi.isCustomViewShowing()) {
1140 mUi.onHideCustomView();
1141 // Reset the old menu state.
1142 mMenuState = mOldMenuState;
1143 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001144 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 }
1146 }
1147
John Reck9c35b9c2012-05-30 10:08:50 -07001148 @Override
1149 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 Intent intent) {
1151 if (getCurrentTopWebView() == null) return;
1152 switch (requestCode) {
1153 case PREFERENCES_PAGE:
1154 if (resultCode == Activity.RESULT_OK && intent != null) {
1155 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001156 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 mTabControl.removeParentChildRelationShips();
1158 }
1159 }
1160 break;
1161 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001162 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001163 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001166 case AUTOFILL_SETUP:
1167 // Determine whether a profile was actually set up or not
1168 // and if so, send the message back to the WebTextView to
1169 // fill the form with the new profile.
1170 if (getSettings().getAutoFillProfile() != null) {
1171 mAutoFillSetupMessage.sendToTarget();
1172 mAutoFillSetupMessage = null;
1173 }
1174 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001175 case COMBO_VIEW:
1176 if (intent == null || resultCode != Activity.RESULT_OK) {
1177 break;
1178 }
John Reck3ba45532011-08-11 16:26:53 -07001179 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001180 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1181 Tab t = getCurrentTab();
1182 Uri uri = intent.getData();
1183 loadUrl(t, uri.toString());
1184 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1185 String[] urls = intent.getStringArrayExtra(
1186 ComboViewActivity.EXTRA_OPEN_ALL);
1187 Tab parent = getCurrentTab();
1188 for (String url : urls) {
1189 parent = openTab(url, parent,
1190 !mSettings.openInBackground(), true);
1191 }
1192 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1193 long id = intent.getLongExtra(
1194 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1195 if (id >= 0) {
1196 createNewSnapshotTab(id, true);
1197 }
1198 }
1199 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001200 case VOICE_RESULT:
1201 if (resultCode == Activity.RESULT_OK && intent != null) {
1202 ArrayList<String> results = intent.getStringArrayListExtra(
1203 RecognizerIntent.EXTRA_RESULTS);
1204 if (results.size() >= 1) {
1205 mVoiceResult = results.get(0);
1206 }
1207 }
1208 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 default:
1210 break;
1211 }
1212 getCurrentTopWebView().requestFocus();
1213 }
1214
1215 /**
1216 * Open the Go page.
1217 * @param startWithHistory If true, open starting on the history tab.
1218 * Otherwise, start with the bookmarks tab.
1219 */
1220 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001221 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001222 if (mTabControl.getCurrentWebView() == null) {
1223 return;
1224 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001225 // clear action mode
1226 if (isInCustomActionMode()) {
1227 endActionMode();
1228 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001229 Bundle extras = new Bundle();
1230 // Disable opening in a new window if we have maxed out the windows
1231 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1232 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001233 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001234 }
1235
1236 // combo view callbacks
1237
Michael Kolb8233fac2010-10-26 16:08:53 -07001238 // key handling
1239 protected void onBackKey() {
1240 if (!mUi.onBackKey()) {
1241 WebView subwindow = mTabControl.getCurrentSubWindow();
1242 if (subwindow != null) {
1243 if (subwindow.canGoBack()) {
1244 subwindow.goBack();
1245 } else {
1246 dismissSubWindow(mTabControl.getCurrentTab());
1247 }
1248 } else {
1249 goBackOnePageOrQuit();
1250 }
1251 }
1252 }
1253
Michael Kolb4bd767d2011-05-27 11:33:55 -07001254 protected boolean onMenuKey() {
1255 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001256 }
1257
Michael Kolb8233fac2010-10-26 16:08:53 -07001258 // menu handling and state
1259 // TODO: maybe put into separate handler
1260
John Reck9c35b9c2012-05-30 10:08:50 -07001261 @Override
1262 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001263 if (mMenuState == EMPTY_MENU) {
1264 return false;
1265 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001266 MenuInflater inflater = mActivity.getMenuInflater();
1267 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 return true;
1269 }
1270
John Reck9c35b9c2012-05-30 10:08:50 -07001271 @Override
1272 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001274 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001275 return;
1276 }
1277 if (!(v instanceof WebView)) {
1278 return;
1279 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001280 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 WebView.HitTestResult result = webview.getHitTestResult();
1282 if (result == null) {
1283 return;
1284 }
1285
1286 int type = result.getType();
1287 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1288 Log.w(LOGTAG,
1289 "We should not show context menu when nothing is touched");
1290 return;
1291 }
1292 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1293 // let TextView handles context menu
1294 return;
1295 }
1296
1297 // Note, http://b/issue?id=1106666 is requesting that
1298 // an inflated menu can be used again. This is not available
1299 // yet, so inflate each time (yuk!)
1300 MenuInflater inflater = mActivity.getMenuInflater();
1301 inflater.inflate(R.menu.browsercontext, menu);
1302
1303 // Show the correct menu group
1304 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001305 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001306 menu.setGroupVisible(R.id.PHONE_MENU,
1307 type == WebView.HitTestResult.PHONE_TYPE);
1308 menu.setGroupVisible(R.id.EMAIL_MENU,
1309 type == WebView.HitTestResult.EMAIL_TYPE);
1310 menu.setGroupVisible(R.id.GEO_MENU,
1311 type == WebView.HitTestResult.GEO_TYPE);
1312 menu.setGroupVisible(R.id.IMAGE_MENU,
1313 type == WebView.HitTestResult.IMAGE_TYPE
1314 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1315 menu.setGroupVisible(R.id.ANCHOR_MENU,
1316 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1317 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001318 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1319 || type == WebView.HitTestResult.PHONE_TYPE
1320 || type == WebView.HitTestResult.EMAIL_TYPE
1321 || type == WebView.HitTestResult.GEO_TYPE;
1322 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1323 if (hitText) {
1324 menu.findItem(R.id.select_text_menu_id)
1325 .setOnMenuItemClickListener(new SelectText(webview));
1326 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 // Setup custom handling depending on the type
1328 switch (type) {
1329 case WebView.HitTestResult.PHONE_TYPE:
1330 menu.setHeaderTitle(Uri.decode(extra));
1331 menu.findItem(R.id.dial_context_menu_id).setIntent(
1332 new Intent(Intent.ACTION_VIEW, Uri
1333 .parse(WebView.SCHEME_TEL + extra)));
1334 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1335 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1336 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1337 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1338 addIntent);
1339 menu.findItem(R.id.copy_phone_context_menu_id)
1340 .setOnMenuItemClickListener(
1341 new Copy(extra));
1342 break;
1343
1344 case WebView.HitTestResult.EMAIL_TYPE:
1345 menu.setHeaderTitle(extra);
1346 menu.findItem(R.id.email_context_menu_id).setIntent(
1347 new Intent(Intent.ACTION_VIEW, Uri
1348 .parse(WebView.SCHEME_MAILTO + extra)));
1349 menu.findItem(R.id.copy_mail_context_menu_id)
1350 .setOnMenuItemClickListener(
1351 new Copy(extra));
1352 break;
1353
1354 case WebView.HitTestResult.GEO_TYPE:
1355 menu.setHeaderTitle(extra);
1356 menu.findItem(R.id.map_context_menu_id).setIntent(
1357 new Intent(Intent.ACTION_VIEW, Uri
1358 .parse(WebView.SCHEME_GEO
1359 + URLEncoder.encode(extra))));
1360 menu.findItem(R.id.copy_geo_context_menu_id)
1361 .setOnMenuItemClickListener(
1362 new Copy(extra));
1363 break;
1364
1365 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1366 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001367 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 // decide whether to show the open link in new tab option
1369 boolean showNewTab = mTabControl.canCreateNewTab();
1370 MenuItem newTabItem
1371 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001372 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001373 ? R.string.contextmenu_openlink_newwindow_background
1374 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001375 newTabItem.setVisible(showNewTab);
1376 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001377 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1378 newTabItem.setOnMenuItemClickListener(
1379 new MenuItem.OnMenuItemClickListener() {
1380 @Override
1381 public boolean onMenuItemClick(MenuItem item) {
1382 final HashMap<String, WebView> hrefMap =
1383 new HashMap<String, WebView>();
1384 hrefMap.put("webview", webview);
1385 final Message msg = mHandler.obtainMessage(
1386 FOCUS_NODE_HREF,
1387 R.id.open_newtab_context_menu_id,
1388 0, hrefMap);
1389 webview.requestFocusNodeHref(msg);
1390 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001391 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001392 });
1393 } else {
1394 newTabItem.setOnMenuItemClickListener(
1395 new MenuItem.OnMenuItemClickListener() {
1396 @Override
1397 public boolean onMenuItemClick(MenuItem item) {
1398 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001399 openTab(extra, parent,
1400 !mSettings.openInBackground(),
1401 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001402 return true;
1403 }
1404 });
1405 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1408 break;
1409 }
1410 // otherwise fall through to handle image part
1411 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001412 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1413 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1415 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001416 shareItem.setOnMenuItemClickListener(
1417 new MenuItem.OnMenuItemClickListener() {
1418 @Override
1419 public boolean onMenuItemClick(MenuItem item) {
1420 sharePage(mActivity, null, extra, null,
1421 null);
1422 return true;
1423 }
1424 }
1425 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001426 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001427 menu.findItem(R.id.view_image_context_menu_id)
1428 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1429 @Override
1430 public boolean onMenuItemClick(MenuItem item) {
1431 openTab(extra, mTabControl.getCurrentTab(), true, true);
1432 return false;
1433 }
1434 });
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001436 setOnMenuItemClickListener(
1437 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001438 menu.findItem(R.id.set_wallpaper_context_menu_id).
1439 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1440 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001441 break;
1442
1443 default:
1444 Log.w(LOGTAG, "We should not get here.");
1445 break;
1446 }
1447 //update the ui
1448 mUi.onContextMenuCreated(menu);
1449 }
1450
1451 /**
1452 * As the menu can be open when loading state changes
1453 * we must manually update the state of the stop/reload menu
1454 * item
1455 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001456 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001457 if (menu == null) {
1458 return;
1459 }
1460 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001461 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 menu.findItem(R.id.stop_menu_id):
1463 menu.findItem(R.id.reload_menu_id);
1464 if (src != null) {
1465 dest.setIcon(src.getIcon());
1466 dest.setTitle(src.getTitle());
1467 }
1468 }
1469
John Reck9c35b9c2012-05-30 10:08:50 -07001470 @Override
1471 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001472 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001473 // hold on to the menu reference here; it is used by the page callbacks
1474 // to update the menu based on loading state
1475 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001476 // Note: setVisible will decide whether an item is visible; while
1477 // setEnabled() will decide whether an item is enabled, which also means
1478 // whether the matching shortcut key will function.
1479 switch (mMenuState) {
1480 case EMPTY_MENU:
1481 if (mCurrentMenuState != mMenuState) {
1482 menu.setGroupVisible(R.id.MAIN_MENU, false);
1483 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1484 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1485 }
1486 break;
1487 default:
1488 if (mCurrentMenuState != mMenuState) {
1489 menu.setGroupVisible(R.id.MAIN_MENU, true);
1490 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1491 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1492 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001493 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001494 break;
1495 }
1496 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001497 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001498 }
1499
Michael Kolb4bf79712011-07-14 14:18:12 -07001500 @Override
1501 public void updateMenuState(Tab tab, Menu menu) {
1502 boolean canGoBack = false;
1503 boolean canGoForward = false;
1504 boolean isHome = false;
John Reck42229bc2011-08-19 13:26:43 -07001505 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001506 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001507 if (tab != null) {
1508 canGoBack = tab.canGoBack();
1509 canGoForward = tab.canGoForward();
1510 isHome = mSettings.getHomePage().equals(tab.getUrl());
John Reck42229bc2011-08-19 13:26:43 -07001511 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001512 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001513 }
1514 final MenuItem back = menu.findItem(R.id.back_menu_id);
1515 back.setEnabled(canGoBack);
1516
1517 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1518 home.setEnabled(!isHome);
1519
1520 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1521 forward.setEnabled(canGoForward);
1522
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001523 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1524 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001525 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001526 if (source != null && dest != null) {
1527 dest.setTitle(source.getTitle());
1528 dest.setIcon(source.getIcon());
1529 }
John Recke1a03a32011-09-14 17:04:16 -07001530 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001531
1532 // decide whether to show the share link option
1533 PackageManager pm = mActivity.getPackageManager();
1534 Intent send = new Intent(Intent.ACTION_SEND);
1535 send.setType("text/plain");
1536 ResolveInfo ri = pm.resolveActivity(send,
1537 PackageManager.MATCH_DEFAULT_ONLY);
1538 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1539
1540 boolean isNavDump = mSettings.enableNavDump();
1541 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1542 nav.setVisible(isNavDump);
1543 nav.setEnabled(isNavDump);
1544
1545 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001546 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1547 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001548 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1549 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Michael Kolb315d5022011-10-13 12:47:11 -07001550 menu.setGroupVisible(R.id.COMBO_MENU, false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001551
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001552 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001553 }
1554
John Reck9c35b9c2012-05-30 10:08:50 -07001555 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001556 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001557 if (null == getCurrentTopWebView()) {
1558 return false;
1559 }
1560 if (mMenuIsDown) {
1561 // The shortcut action consumes the MENU. Even if it is still down,
1562 // it won't trigger the next shortcut action. In the case of the
1563 // shortcut action triggering a new activity, like Bookmarks, we
1564 // won't get onKeyUp for MENU. So it is important to reset it here.
1565 mMenuIsDown = false;
1566 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001567 if (mUi.onOptionsItemSelected(item)) {
1568 // ui callback handled it
1569 return true;
1570 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001571 switch (item.getItemId()) {
1572 // -- Main menu
1573 case R.id.new_tab_menu_id:
1574 openTabToHomePage();
1575 break;
1576
1577 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001578 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001579 break;
1580
Afzal Najamd4e33312012-04-26 01:54:01 -04001581 case R.id.close_other_tabs_id:
1582 closeOtherTabs();
1583 break;
1584
Michael Kolb8233fac2010-10-26 16:08:53 -07001585 case R.id.goto_menu_id:
1586 editUrl();
1587 break;
1588
1589 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001590 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1591 break;
1592
1593 case R.id.history_menu_id:
1594 bookmarksOrHistoryPicker(ComboViews.History);
1595 break;
1596
1597 case R.id.snapshots_menu_id:
1598 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001599 break;
1600
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001602 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 break;
1604
1605 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001606 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001607 stopLoading();
1608 } else {
1609 getCurrentTopWebView().reload();
1610 }
1611 break;
1612
1613 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001614 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001615 break;
1616
1617 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001618 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001619 break;
1620
1621 case R.id.close_menu_id:
1622 // Close the subwindow if it exists.
1623 if (mTabControl.getCurrentSubWindow() != null) {
1624 dismissSubWindow(mTabControl.getCurrentTab());
1625 break;
1626 }
1627 closeCurrentTab();
1628 break;
1629
1630 case R.id.homepage_menu_id:
1631 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001632 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001633 break;
1634
1635 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001636 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001637 break;
1638
1639 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001640 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001641 break;
1642
John Reck2bc80422011-06-30 15:11:49 -07001643 case R.id.save_snapshot_menu_id:
1644 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001645 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001646 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001647 break;
1648
Michael Kolb8233fac2010-10-26 16:08:53 -07001649 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001650 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001651 break;
1652
John Recke1a03a32011-09-14 17:04:16 -07001653 case R.id.snapshot_go_live:
1654 goLive();
1655 return true;
1656
Michael Kolb8233fac2010-10-26 16:08:53 -07001657 case R.id.share_page_menu_id:
1658 Tab currentTab = mTabControl.getCurrentTab();
1659 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001660 return false;
1661 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001662 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001663 break;
1664
1665 case R.id.dump_nav_menu_id:
1666 getCurrentTopWebView().debugDump();
1667 break;
1668
Michael Kolb8233fac2010-10-26 16:08:53 -07001669 case R.id.zoom_in_menu_id:
1670 getCurrentTopWebView().zoomIn();
1671 break;
1672
1673 case R.id.zoom_out_menu_id:
1674 getCurrentTopWebView().zoomOut();
1675 break;
1676
1677 case R.id.view_downloads_menu_id:
1678 viewDownloads();
1679 break;
1680
John Reck42229bc2011-08-19 13:26:43 -07001681 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001682 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001683 break;
1684
Michael Kolb8233fac2010-10-26 16:08:53 -07001685 case R.id.window_one_menu_id:
1686 case R.id.window_two_menu_id:
1687 case R.id.window_three_menu_id:
1688 case R.id.window_four_menu_id:
1689 case R.id.window_five_menu_id:
1690 case R.id.window_six_menu_id:
1691 case R.id.window_seven_menu_id:
1692 case R.id.window_eight_menu_id:
1693 {
1694 int menuid = item.getItemId();
1695 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1696 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1697 Tab desiredTab = mTabControl.getTab(id);
1698 if (desiredTab != null &&
1699 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001700 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001701 }
1702 break;
1703 }
1704 }
1705 }
1706 break;
1707
1708 default:
1709 return false;
1710 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001711 return true;
1712 }
1713
John Reck68234a92012-04-19 15:27:12 -07001714 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1715 implements OnCancelListener {
1716
1717 private Tab mTab;
1718 private Dialog mProgressDialog;
1719 private ContentValues mValues;
1720
1721 private SaveSnapshotTask(Tab tab) {
1722 mTab = tab;
1723 }
1724
1725 @Override
1726 protected void onPreExecute() {
1727 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1728 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1729 true, true, this);
1730 mValues = mTab.createSnapshotValues();
1731 }
1732
1733 @Override
1734 protected Long doInBackground(Void... params) {
1735 if (!mTab.saveViewState(mValues)) {
1736 return null;
1737 }
1738 if (isCancelled()) {
1739 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1740 File file = mActivity.getFileStreamPath(path);
1741 if (!file.delete()) {
1742 file.deleteOnExit();
1743 }
1744 return null;
1745 }
1746 final ContentResolver cr = mActivity.getContentResolver();
1747 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1748 if (result == null) {
1749 return null;
1750 }
1751 long id = ContentUris.parseId(result);
1752 return id;
1753 }
1754
1755 @Override
1756 protected void onPostExecute(Long id) {
1757 if (isCancelled()) {
1758 return;
1759 }
1760 mProgressDialog.dismiss();
1761 if (id == null) {
1762 Toast.makeText(mActivity, R.string.snapshot_failed,
1763 Toast.LENGTH_SHORT).show();
1764 return;
1765 }
1766 Bundle b = new Bundle();
1767 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1768 mUi.showComboView(ComboViews.Snapshots, b);
1769 }
1770
1771 @Override
1772 public void onCancel(DialogInterface dialog) {
1773 cancel(true);
1774 }
1775 }
1776
Michael Kolb80f75082012-04-10 10:50:06 -07001777 @Override
1778 public void toggleUserAgent() {
1779 WebView web = getCurrentWebView();
1780 mSettings.toggleDesktopUseragent(web);
1781 web.loadUrl(web.getOriginalUrl());
1782 }
1783
1784 @Override
1785 public void findOnPage() {
1786 getCurrentTopWebView().showFindDialog(null, true);
1787 }
1788
1789 @Override
1790 public void openPreferences() {
1791 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1792 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1793 getCurrentTopWebView().getUrl());
1794 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1795 }
1796
1797 @Override
1798 public void bookmarkCurrentPage() {
1799 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1800 if (bookmarkIntent != null) {
1801 mActivity.startActivity(bookmarkIntent);
1802 }
1803 }
1804
John Recke1a03a32011-09-14 17:04:16 -07001805 private void goLive() {
1806 Tab t = getCurrentTab();
1807 t.loadUrl(t.getUrl(), null);
1808 }
1809
Michael Kolb315d5022011-10-13 12:47:11 -07001810 @Override
1811 public void showPageInfo() {
1812 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1813 }
1814
John Reck9c35b9c2012-05-30 10:08:50 -07001815 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001816 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001817 // Let the History and Bookmark fragments handle menus they created.
1818 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1819 return false;
1820 }
1821
Michael Kolb8233fac2010-10-26 16:08:53 -07001822 int id = item.getItemId();
1823 boolean result = true;
1824 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001825 // -- Browser context menu
1826 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001827 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001828 case R.id.copy_link_context_menu_id:
1829 final WebView webView = getCurrentTopWebView();
1830 if (null == webView) {
1831 result = false;
1832 break;
1833 }
1834 final HashMap<String, WebView> hrefMap =
1835 new HashMap<String, WebView>();
1836 hrefMap.put("webview", webView);
1837 final Message msg = mHandler.obtainMessage(
1838 FOCUS_NODE_HREF, id, 0, hrefMap);
1839 webView.requestFocusNodeHref(msg);
1840 break;
1841
1842 default:
1843 // For other context menus
1844 result = onOptionsItemSelected(item);
1845 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001846 return result;
1847 }
1848
1849 /**
1850 * support programmatically opening the context menu
1851 */
1852 public void openContextMenu(View view) {
1853 mActivity.openContextMenu(view);
1854 }
1855
1856 /**
1857 * programmatically open the options menu
1858 */
1859 public void openOptionsMenu() {
1860 mActivity.openOptionsMenu();
1861 }
1862
John Reck9c35b9c2012-05-30 10:08:50 -07001863 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001864 public boolean onMenuOpened(int featureId, Menu menu) {
1865 if (mOptionsMenuOpen) {
1866 if (mConfigChanged) {
1867 // We do not need to make any changes to the state of the
1868 // title bar, since the only thing that happened was a
1869 // change in orientation
1870 mConfigChanged = false;
1871 } else {
1872 if (!mExtendedMenuOpen) {
1873 mExtendedMenuOpen = true;
1874 mUi.onExtendedMenuOpened();
1875 } else {
1876 // Switching the menu back to icon view, so show the
1877 // title bar once again.
1878 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001879 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001880 }
1881 }
1882 } else {
1883 // The options menu is closed, so open it, and show the title
1884 mOptionsMenuOpen = true;
1885 mConfigChanged = false;
1886 mExtendedMenuOpen = false;
1887 mUi.onOptionsMenuOpened();
1888 }
1889 return true;
1890 }
1891
John Reck9c35b9c2012-05-30 10:08:50 -07001892 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001893 public void onOptionsMenuClosed(Menu menu) {
1894 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001895 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001896 }
1897
John Reck9c35b9c2012-05-30 10:08:50 -07001898 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001899 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001900 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001901 }
1902
1903 // Helper method for getting the top window.
1904 @Override
1905 public WebView getCurrentTopWebView() {
1906 return mTabControl.getCurrentTopWebView();
1907 }
1908
1909 @Override
1910 public WebView getCurrentWebView() {
1911 return mTabControl.getCurrentWebView();
1912 }
1913
1914 /*
1915 * This method is called as a result of the user selecting the options
1916 * menu to see the download window. It shows the download window on top of
1917 * the current window.
1918 */
1919 void viewDownloads() {
1920 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1921 mActivity.startActivity(intent);
1922 }
1923
John Reck30b065e2011-07-19 10:58:05 -07001924 int getActionModeHeight() {
1925 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1926 new int[] { android.R.attr.actionBarSize });
1927 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1928 actionBarSizeTypedArray.recycle();
1929 return size;
1930 }
1931
Michael Kolb8233fac2010-10-26 16:08:53 -07001932 // action mode
1933
John Reck9c35b9c2012-05-30 10:08:50 -07001934 @Override
1935 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001936 mUi.onActionModeStarted(mode);
1937 mActionMode = mode;
1938 }
1939
1940 /*
1941 * True if a custom ActionMode (i.e. find or select) is in use.
1942 */
1943 @Override
1944 public boolean isInCustomActionMode() {
1945 return mActionMode != null;
1946 }
1947
1948 /*
1949 * End the current ActionMode.
1950 */
1951 @Override
1952 public void endActionMode() {
1953 if (mActionMode != null) {
1954 mActionMode.finish();
1955 }
1956 }
1957
1958 /*
1959 * Called by find and select when they are finished. Replace title bars
1960 * as necessary.
1961 */
John Reck9c35b9c2012-05-30 10:08:50 -07001962 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 public void onActionModeFinished(ActionMode mode) {
1964 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001965 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001966 mActionMode = null;
1967 }
1968
1969 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001970 final Tab tab = getCurrentTab();
1971 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07001972 }
1973
1974 // bookmark handling
1975
1976 /**
1977 * add the current page as a bookmark to the given folder id
1978 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001979 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001980 * bookmarked, and if it is, edit that bookmark. If false, and
1981 * the site is already bookmarked, do not attempt to edit the
1982 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 */
1984 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001985 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07001986 WebView w = getCurrentTopWebView();
1987 if (w == null) {
1988 return null;
1989 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001990 Intent i = new Intent(mActivity,
1991 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1993 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1994 String touchIconUrl = w.getTouchIconUrl();
1995 if (touchIconUrl != null) {
1996 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1997 WebSettings settings = w.getSettings();
1998 if (settings != null) {
1999 i.putExtra(AddBookmarkPage.USER_AGENT,
2000 settings.getUserAgentString());
2001 }
2002 }
2003 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2004 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2005 getDesiredThumbnailHeight(mActivity)));
2006 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002007 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002008 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2009 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002010 // Put the dialog at the upper right of the screen, covering the
2011 // star on the title bar.
2012 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002013 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002014 }
2015
2016 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002017 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002018 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002019 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002020 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002021 }
2022
2023 // thumbnails
2024
2025 /**
2026 * Return the desired width for thumbnail screenshots, which are stored in
2027 * the database, and used on the bookmarks screen.
2028 * @param context Context for finding out the density of the screen.
2029 * @return desired width for thumbnail screenshot.
2030 */
2031 static int getDesiredThumbnailWidth(Context context) {
2032 return context.getResources().getDimensionPixelOffset(
2033 R.dimen.bookmarkThumbnailWidth);
2034 }
2035
2036 /**
2037 * Return the desired height for thumbnail screenshots, which are stored in
2038 * the database, and used on the bookmarks screen.
2039 * @param context Context for finding out the density of the screen.
2040 * @return desired height for thumbnail screenshot.
2041 */
2042 static int getDesiredThumbnailHeight(Context context) {
2043 return context.getResources().getDimensionPixelOffset(
2044 R.dimen.bookmarkThumbnailHeight);
2045 }
2046
John Reck8cc92352011-07-06 17:41:52 -07002047 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002048 if (view == null || view.getContentHeight() == 0
2049 || view.getContentWidth() == 0) {
2050 return null;
2051 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002052 // We render to a bitmap 2x the desired size so that we can then
2053 // re-scale it with filtering since canvas.scale doesn't filter
2054 // This helps reduce aliasing at the cost of being slightly blurry
2055 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002056 int scaledWidth = width * filter_scale;
2057 int scaledHeight = height * filter_scale;
2058 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2059 || sThumbnailBitmap.getHeight() != scaledHeight) {
2060 if (sThumbnailBitmap != null) {
2061 sThumbnailBitmap.recycle();
2062 sThumbnailBitmap = null;
2063 }
2064 sThumbnailBitmap =
2065 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002066 }
John Reckd7dd9b22011-08-30 09:18:29 -07002067 Canvas canvas = new Canvas(sThumbnailBitmap);
2068 int contentWidth = view.getContentWidth();
2069 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2070 if (view instanceof BrowserWebView) {
2071 int dy = -((BrowserWebView)view).getTitleHeight();
2072 canvas.translate(0, dy * overviewScale);
2073 }
2074
2075 canvas.scale(overviewScale, overviewScale);
2076
2077 if (view instanceof BrowserWebView) {
2078 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002079 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002080 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 }
John Reckd7dd9b22011-08-30 09:18:29 -07002082 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2083 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002084 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002085 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002086 }
2087
John Reck34ef2672011-02-10 11:30:55 -08002088 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002089 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002090 // FIXME: Would like to make sure there is actually something to
2091 // draw, but the API for that (WebViewCore.pictureReady()) is not
2092 // currently accessible here.
2093
John Reck34ef2672011-02-10 11:30:55 -08002094 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002095 if (view == null) {
2096 // Tab was destroyed
2097 return;
2098 }
John Reck34ef2672011-02-10 11:30:55 -08002099 final String url = tab.getUrl();
2100 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002101 if (TextUtils.isEmpty(url)) {
2102 return;
2103 }
2104
2105 // Only update thumbnails for web urls (http(s)://), not for
2106 // about:, javascript:, data:, etc...
2107 // Unless it is a bookmarked site, then always update
2108 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2109 return;
2110 }
2111
Michael Kolb8233fac2010-10-26 16:08:53 -07002112 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2113 getDesiredThumbnailHeight(mActivity));
2114 if (bm == null) {
2115 return;
2116 }
2117
2118 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002119 new AsyncTask<Void, Void, Void>() {
2120 @Override
2121 protected Void doInBackground(Void... unused) {
2122 Cursor cursor = null;
2123 try {
2124 // TODO: Clean this up
2125 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2126 if (cursor != null && cursor.moveToFirst()) {
2127 final ByteArrayOutputStream os =
2128 new ByteArrayOutputStream();
2129 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002130
John Reck34ef2672011-02-10 11:30:55 -08002131 ContentValues values = new ContentValues();
2132 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002133
John Reck34ef2672011-02-10 11:30:55 -08002134 do {
John Reck617fd832011-02-16 14:35:59 -08002135 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002136 cr.update(Images.CONTENT_URI, values, null, null);
2137 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002138 }
John Reck34ef2672011-02-10 11:30:55 -08002139 } catch (IllegalStateException e) {
2140 // Ignore
2141 } finally {
2142 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002143 }
John Reck34ef2672011-02-10 11:30:55 -08002144 return null;
2145 }
2146 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002147 }
2148
2149 private class Copy implements OnMenuItemClickListener {
2150 private CharSequence mText;
2151
John Reck9c35b9c2012-05-30 10:08:50 -07002152 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002153 public boolean onMenuItemClick(MenuItem item) {
2154 copy(mText);
2155 return true;
2156 }
2157
2158 public Copy(CharSequence toCopy) {
2159 mText = toCopy;
2160 }
2161 }
2162
Leon Scroggins63c02662010-11-18 15:16:27 -05002163 private static class Download implements OnMenuItemClickListener {
2164 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002165 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002166 private boolean mPrivateBrowsing;
George Mount387d45d2011-10-07 15:57:53 -07002167 private static final String FALLBACK_EXTENSION = "dat";
2168 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002169
John Reck9c35b9c2012-05-30 10:08:50 -07002170 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002171 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002172 if (DataUri.isDataUri(mText)) {
2173 saveDataUri();
2174 } else {
2175 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
2176 null, null, mPrivateBrowsing);
2177 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002178 return true;
2179 }
2180
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002181 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002182 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002183 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002184 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002185 }
George Mount387d45d2011-10-07 15:57:53 -07002186
2187 /**
2188 * Treats mText as a data URI and writes its contents to a file
2189 * based on the current time.
2190 */
2191 private void saveDataUri() {
2192 FileOutputStream outputStream = null;
2193 try {
2194 DataUri uri = new DataUri(mText);
2195 File target = getTarget(uri);
2196 outputStream = new FileOutputStream(target);
2197 outputStream.write(uri.getData());
2198 final DownloadManager manager =
2199 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2200 manager.addCompletedDownload(target.getName(),
2201 mActivity.getTitle().toString(), false,
2202 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002203 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002204 } catch (IOException e) {
2205 Log.e(LOGTAG, "Could not save data URL");
2206 } finally {
2207 if (outputStream != null) {
2208 try {
2209 outputStream.close();
2210 } catch (IOException e) {
2211 // ignore close errors
2212 }
2213 }
2214 }
2215 }
2216
2217 /**
2218 * Creates a File based on the current time stamp and uses
2219 * the mime type of the DataUri to get the extension.
2220 */
2221 private File getTarget(DataUri uri) throws IOException {
2222 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
2223 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT);
2224 String nameBase = format.format(new Date());
2225 String mimeType = uri.getMimeType();
2226 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2227 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2228 if (extension == null) {
2229 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2230 extension = FALLBACK_EXTENSION;
2231 }
2232 extension = "." + extension; // createTempFile needs the '.'
2233 File targetFile = File.createTempFile(nameBase, extension, dir);
2234 return targetFile;
2235 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002236 }
2237
Cary Clark8974d282010-11-22 10:46:05 -05002238 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002239 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002240
John Reck9c35b9c2012-05-30 10:08:50 -07002241 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002242 public boolean onMenuItemClick(MenuItem item) {
2243 if (mWebView != null) {
2244 return mWebView.selectText();
2245 }
2246 return false;
2247 }
2248
2249 public SelectText(WebView webView) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002250 mWebView = WebViewClassic.fromWebView(webView);
Cary Clark8974d282010-11-22 10:46:05 -05002251 }
2252
2253 }
2254
Michael Kolb8233fac2010-10-26 16:08:53 -07002255 /********************** TODO: UI stuff *****************************/
2256
2257 // these methods have been copied, they still need to be cleaned up
2258
2259 /****************** tabs ***************************************************/
2260
2261 // basic tab interactions:
2262
2263 // it is assumed that tabcontrol already knows about the tab
2264 protected void addTab(Tab tab) {
2265 mUi.addTab(tab);
2266 }
2267
2268 protected void removeTab(Tab tab) {
2269 mUi.removeTab(tab);
2270 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002271 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002272 }
2273
Michael Kolbf2055602011-04-09 17:20:03 -07002274 @Override
2275 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002276 // monkey protection against delayed start
2277 if (tab != null) {
2278 mTabControl.setCurrentTab(tab);
2279 // the tab is guaranteed to have a webview after setCurrentTab
2280 mUi.setActiveTab(tab);
2281 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002282 }
2283
John Reck8bcafc12011-08-29 16:43:02 -07002284 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002285 Tab current = mTabControl.getCurrentTab();
2286 if (current != null
2287 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002288 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002289 }
2290 }
2291
John Reck26b18322011-06-21 13:08:58 -07002292 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002293 // Dismiss the subwindow if applicable.
2294 dismissSubWindow(appTab);
2295 // Since we might kill the WebView, remove it from the
2296 // content view first.
2297 mUi.detachTab(appTab);
2298 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002299 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002300 // TODO: analyze why the remove and add are necessary
2301 mUi.attachTab(appTab);
2302 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002303 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002304 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002305 } else {
2306 // If the tab was the current tab, we have to attach
2307 // it to the view system again.
2308 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002309 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002310 }
2311 }
2312
2313 // Remove the sub window if it exists. Also called by TabControl when the
2314 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002315 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002316 public void dismissSubWindow(Tab tab) {
2317 removeSubWindow(tab);
2318 // dismiss the subwindow. This will destroy the WebView.
2319 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002320 WebView wv = getCurrentTopWebView();
2321 if (wv != null) {
2322 wv.requestFocus();
2323 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002324 }
2325
2326 @Override
2327 public void removeSubWindow(Tab t) {
2328 if (t.getSubWebView() != null) {
2329 mUi.removeSubWindow(t.getSubViewContainer());
2330 }
2331 }
2332
2333 @Override
2334 public void attachSubWindow(Tab tab) {
2335 if (tab.getSubWebView() != null) {
2336 mUi.attachSubWindow(tab.getSubViewContainer());
2337 getCurrentTopWebView().requestFocus();
2338 }
2339 }
2340
Mathew Inwood29721c22011-06-29 17:55:24 +01002341 private Tab showPreloadedTab(final UrlData urlData) {
2342 if (!urlData.isPreloaded()) {
2343 return null;
2344 }
2345 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2346 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2347 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002348 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002349 // Could not submit query. Fallback to regular tab creation
2350 tabControl.destroy();
2351 return null;
2352 }
2353 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002354 // check tab count and make room for new tab
2355 if (!mTabControl.canCreateNewTab()) {
2356 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2357 if (leastUsed != null) {
2358 closeTab(leastUsed);
2359 }
2360 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002361 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002362 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002363 mTabControl.addPreloadedTab(t);
2364 addTab(t);
2365 setActiveTab(t);
2366 return t;
2367 }
2368
Michael Kolb7bcafde2011-05-09 13:55:59 -07002369 // open a non inconito tab with the given url data
2370 // and set as active tab
2371 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002372 Tab tab = showPreloadedTab(urlData);
2373 if (tab == null) {
2374 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002375 if ((tab != null) && !urlData.isEmpty()) {
2376 loadUrlDataIn(tab, urlData);
2377 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002378 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002379 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002380 }
2381
Michael Kolb843510f2010-12-09 10:51:49 -08002382 @Override
2383 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002384 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002385 }
2386
Michael Kolb8233fac2010-10-26 16:08:53 -07002387 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002388 public Tab openIncognitoTab() {
2389 return openTab(INCOGNITO_URI, true, true, false);
2390 }
2391
2392 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002393 public Tab openTab(String url, boolean incognito, boolean setActive,
2394 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002395 return openTab(url, incognito, setActive, useCurrent, null);
2396 }
2397
2398 @Override
2399 public Tab openTab(String url, Tab parent, boolean setActive,
2400 boolean useCurrent) {
2401 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2402 setActive, useCurrent, parent);
2403 }
2404
2405 public Tab openTab(String url, boolean incognito, boolean setActive,
2406 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002407 Tab tab = createNewTab(incognito, setActive, useCurrent);
2408 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002409 if (parent != null && parent != tab) {
2410 parent.addChildTab(tab);
2411 }
Michael Kolb519d2282011-05-09 17:03:19 -07002412 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002413 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002414 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002415 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002416 return tab;
2417 }
2418
2419 // this method will attempt to create a new tab
2420 // incognito: private browsing tab
2421 // setActive: ste tab as current tab
2422 // useCurrent: if no new tab can be created, return current tab
2423 private Tab createNewTab(boolean incognito, boolean setActive,
2424 boolean useCurrent) {
2425 Tab tab = null;
2426 if (mTabControl.canCreateNewTab()) {
2427 tab = mTabControl.createNewTab(incognito);
2428 addTab(tab);
2429 if (setActive) {
2430 setActiveTab(tab);
2431 }
2432 } else {
2433 if (useCurrent) {
2434 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002435 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002436 } else {
2437 mUi.showMaxTabsWarning();
2438 }
2439 }
2440 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002441 }
2442
John Reck2bc80422011-06-30 15:11:49 -07002443 @Override
2444 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2445 SnapshotTab tab = null;
2446 if (mTabControl.canCreateNewTab()) {
2447 tab = mTabControl.createSnapshotTab(snapshotId);
2448 addTab(tab);
2449 if (setActive) {
2450 setActiveTab(tab);
2451 }
2452 } else {
2453 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002454 }
2455 return tab;
2456 }
2457
Michael Kolb8233fac2010-10-26 16:08:53 -07002458 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002459 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002460 * @return boolean True if we successfully switched to a different tab. If
2461 * the indexth tab is null, or if that tab is the same as
2462 * the current one, return false.
2463 */
2464 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002465 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002466 Tab currentTab = mTabControl.getCurrentTab();
2467 if (tab == null || tab == currentTab) {
2468 return false;
2469 }
2470 setActiveTab(tab);
2471 return true;
2472 }
2473
2474 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002475 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002476 closeCurrentTab(false);
2477 }
2478
2479 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002480 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002481 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002482 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002483 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002484 return;
2485 }
Michael Kolbc831b632011-05-11 09:30:34 -07002486 final Tab current = mTabControl.getCurrentTab();
2487 final int pos = mTabControl.getCurrentPosition();
2488 Tab newTab = current.getParent();
2489 if (newTab == null) {
2490 newTab = mTabControl.getTab(pos + 1);
2491 if (newTab == null) {
2492 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002493 }
2494 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002495 if (andQuit) {
2496 mTabControl.setCurrentTab(newTab);
2497 closeTab(current);
2498 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002499 // Close window
2500 closeTab(current);
2501 }
2502 }
2503
2504 /**
2505 * Close the tab, remove its associated title bar, and adjust mTabControl's
2506 * current tab to a valid value.
2507 */
2508 @Override
2509 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002510 if (tab == mTabControl.getCurrentTab()) {
2511 closeCurrentTab();
2512 } else {
2513 removeTab(tab);
2514 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002515 }
2516
Afzal Najamd4e33312012-04-26 01:54:01 -04002517 /**
2518 * Close all tabs except the current one
2519 */
2520 @Override
2521 public void closeOtherTabs() {
2522 int inactiveTabs = mTabControl.getTabCount() - 1;
2523 for (int i = inactiveTabs; i >= 0; i--) {
2524 Tab tab = mTabControl.getTab(i);
2525 if (tab != mTabControl.getCurrentTab()) {
2526 removeTab(tab);
2527 }
2528 }
2529 }
2530
Michael Kolb8233fac2010-10-26 16:08:53 -07002531 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002532 protected void loadUrlFromContext(String url) {
2533 Tab tab = getCurrentTab();
2534 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002535 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002536 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002537 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002538 if (!WebViewClassic.fromWebView(view).getWebViewClient().
2539 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002540 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 }
2542 }
2543 }
2544
2545 /**
2546 * Load the URL into the given WebView and update the title bar
2547 * to reflect the new load. Call this instead of WebView.loadUrl
2548 * directly.
2549 * @param view The WebView used to load url.
2550 * @param url The URL to load.
2551 */
John Reck71e51422011-07-01 16:49:28 -07002552 @Override
2553 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002554 loadUrl(tab, url, null);
2555 }
2556
2557 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2558 if (tab != null) {
2559 dismissSubWindow(tab);
2560 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002561 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002562 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002563 }
2564
2565 /**
2566 * Load UrlData into a Tab and update the title bar to reflect the new
2567 * load. Call this instead of UrlData.loadIn directly.
2568 * @param t The Tab used to load.
2569 * @param data The UrlData being loaded.
2570 */
2571 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002572 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002573 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002574 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002575 } else {
2576 loadUrl(t, data.mUrl, data.mHeaders);
2577 }
2578 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002579 }
2580
John Reck30c714c2010-12-16 17:30:34 -08002581 @Override
2582 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002583 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002584 if (tab.canGoBack()) {
2585 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002586 } else {
John Reckef654f12011-07-12 16:42:08 -07002587 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002588 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002589 }
2590
2591 void goBackOnePageOrQuit() {
2592 Tab current = mTabControl.getCurrentTab();
2593 if (current == null) {
2594 /*
2595 * Instead of finishing the activity, simply push this to the back
2596 * of the stack and let ActivityManager to choose the foreground
2597 * activity. As BrowserActivity is singleTask, it will be always the
2598 * root of the task. So we can use either true or false for
2599 * moveTaskToBack().
2600 */
2601 mActivity.moveTaskToBack(true);
2602 return;
2603 }
John Reckef654f12011-07-12 16:42:08 -07002604 if (current.canGoBack()) {
2605 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 } else {
2607 // Check to see if we are closing a window that was created by
2608 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002609 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002610 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002611 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 // Now we close the other tab
2613 closeTab(current);
2614 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002615 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002616 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002617 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002618 /*
2619 * Instead of finishing the activity, simply push this to the back
2620 * of the stack and let ActivityManager to choose the foreground
2621 * activity. As BrowserActivity is singleTask, it will be always the
2622 * root of the task. So we can use either true or false for
2623 * moveTaskToBack().
2624 */
2625 mActivity.moveTaskToBack(true);
2626 }
2627 }
2628 }
2629
2630 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002631 * helper method for key handler
2632 * returns the current tab if it can't advance
2633 */
Michael Kolbc831b632011-05-11 09:30:34 -07002634 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002635 int pos = mTabControl.getCurrentPosition() + 1;
2636 if (pos >= mTabControl.getTabCount()) {
2637 pos = 0;
2638 }
2639 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002640 }
2641
2642 /**
2643 * helper method for key handler
2644 * returns the current tab if it can't advance
2645 */
Michael Kolbc831b632011-05-11 09:30:34 -07002646 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002647 int pos = mTabControl.getCurrentPosition() - 1;
2648 if ( pos < 0) {
2649 pos = mTabControl.getTabCount() - 1;
2650 }
2651 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002652 }
2653
John Reckbcef87f2012-02-03 14:58:34 -08002654 boolean isMenuOrCtrlKey(int keyCode) {
2655 return (KeyEvent.KEYCODE_MENU == keyCode)
2656 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2657 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2658 }
2659
Michael Kolb0035fad2011-03-14 13:25:28 -07002660 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002661 * handle key events in browser
2662 *
2663 * @param keyCode
2664 * @param event
2665 * @return true if handled, false to pass to super
2666 */
John Reck9c35b9c2012-05-30 10:08:50 -07002667 @Override
2668 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002669 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002670 // Even if MENU is already held down, we need to call to super to open
2671 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002672 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002673 mMenuIsDown = true;
2674 return false;
2675 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002676
Cary Clark8ff8c662010-12-29 15:03:05 -05002677 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002678 Tab tab = getCurrentTab();
2679 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002680
Cary Clark160bbb92011-01-10 11:17:07 -05002681 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2682 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002683
Michael Kolb8233fac2010-10-26 16:08:53 -07002684 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002685 case KeyEvent.KEYCODE_TAB:
2686 if (event.isCtrlPressed()) {
2687 if (event.isShiftPressed()) {
2688 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002689 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002690 } else {
2691 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002692 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002693 }
2694 return true;
2695 }
2696 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002697 case KeyEvent.KEYCODE_SPACE:
2698 // WebView/WebTextView handle the keys in the KeyDown. As
2699 // the Activity's shortcut keys are only handled when WebView
2700 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002701 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002702 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002703 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002704 pageDown();
2705 }
2706 return true;
2707 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002708 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002709 event.startTracking();
2710 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002711 case KeyEvent.KEYCODE_FORWARD:
2712 if (!noModifiers) break;
2713 tab.goForward();
2714 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002715 case KeyEvent.KEYCODE_DPAD_LEFT:
2716 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002717 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002718 return true;
2719 }
2720 break;
2721 case KeyEvent.KEYCODE_DPAD_RIGHT:
2722 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002723 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002724 return true;
2725 }
2726 break;
2727 case KeyEvent.KEYCODE_A:
2728 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002729 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002730 return true;
2731 }
2732 break;
Michael Kolba4183062011-01-16 10:43:21 -08002733// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002734 case KeyEvent.KEYCODE_C:
2735 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002736 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002737 return true;
2738 }
2739 break;
Michael Kolba4183062011-01-16 10:43:21 -08002740// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002741// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002742// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002743// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002744// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002745// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002746// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002747// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002748// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002749// case KeyEvent.KEYCODE_M: // unused
2750// case KeyEvent.KEYCODE_N: // in Chrome: new window
2751// case KeyEvent.KEYCODE_O: // in Chrome: open file
2752// case KeyEvent.KEYCODE_P: // in Chrome: print page
2753// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002754// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002755// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2756 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002757 // we can't use the ctrl/shift flags, they check for
2758 // exclusive use of a modifier
2759 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002760 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002761 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002762 } else {
2763 openTabToHomePage();
2764 }
2765 return true;
2766 }
2767 break;
2768// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2769// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002770// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002771// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2772// case KeyEvent.KEYCODE_Y: // unused
2773// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002774 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002775 // it is a regular key and webview is not null
2776 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002777 }
2778
John Reck9c35b9c2012-05-30 10:08:50 -07002779 @Override
2780 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002781 switch(keyCode) {
2782 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002783 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002784 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002785 return true;
2786 }
2787 break;
2788 }
2789 return false;
2790 }
2791
John Reck9c35b9c2012-05-30 10:08:50 -07002792 @Override
2793 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002794 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002795 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002796 if (KeyEvent.KEYCODE_MENU == keyCode
2797 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002798 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002799 }
2800 }
Cary Clark160bbb92011-01-10 11:17:07 -05002801 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002802 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002803 case KeyEvent.KEYCODE_BACK:
2804 if (event.isTracking() && !event.isCanceled()) {
2805 onBackKey();
2806 return true;
2807 }
2808 break;
2809 }
2810 return false;
2811 }
2812
2813 public boolean isMenuDown() {
2814 return mMenuIsDown;
2815 }
2816
John Reck9c35b9c2012-05-30 10:08:50 -07002817 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002818 public void setupAutoFill(Message message) {
2819 // Open the settings activity at the AutoFill profile fragment so that
2820 // the user can create a new profile. When they return, we will dispatch
2821 // the message so that we can autofill the form using their new profile.
2822 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2823 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2824 AutoFillSettingsFragment.class.getName());
2825 mAutoFillSetupMessage = message;
2826 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2827 }
John Reckb3417f02011-01-14 11:01:05 -08002828
John Reck9c35b9c2012-05-30 10:08:50 -07002829 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002830 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002831 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002832 return true;
2833 }
2834
John Reck1cf4b792011-07-26 10:22:22 -07002835 @Override
2836 public boolean shouldCaptureThumbnails() {
2837 return mUi.shouldCaptureThumbnails();
2838 }
2839
Michael Kolbc3af0672011-08-09 10:24:41 -07002840 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002841 public boolean supportsVoice() {
2842 PackageManager pm = mActivity.getPackageManager();
2843 List activities = pm.queryIntentActivities(new Intent(
2844 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2845 return activities.size() != 0;
2846 }
2847
2848 @Override
2849 public void startVoiceRecognizer() {
2850 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2851 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2852 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2853 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2854 mActivity.startActivityForResult(voice, VOICE_RESULT);
2855 }
2856
2857 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002858 public void setBlockEvents(boolean block) {
2859 mBlockEvents = block;
2860 }
2861
John Reck9c35b9c2012-05-30 10:08:50 -07002862 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002863 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002864 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002865 }
2866
John Reck9c35b9c2012-05-30 10:08:50 -07002867 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002868 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002869 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002870 }
2871
John Reck9c35b9c2012-05-30 10:08:50 -07002872 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002873 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002874 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002875 }
2876
John Reck9c35b9c2012-05-30 10:08:50 -07002877 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002878 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002879 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002880 }
2881
John Reck9c35b9c2012-05-30 10:08:50 -07002882 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002883 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002884 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002885 }
2886
Michael Kolb8233fac2010-10-26 16:08:53 -07002887}