blob: 385d494abcf2ab1031f0bbf1347bcc55fc49a70e [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(
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700515 mActivity, url, null, null, null, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000516 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700517 break;
518 }
519 break;
520 }
521
522 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700523 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700524 break;
525
526 case STOP_LOAD:
527 stopLoading();
528 break;
529
530 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700531 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700532 mWakeLock.release();
533 // if we reach here, Browser should be still in the
534 // background loading after WAKELOCK_TIMEOUT (5-min).
535 // To avoid burning the battery, stop loading.
536 mTabControl.stopAllLoading();
537 }
538 break;
539
540 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800541 Tab tab = (Tab) msg.obj;
542 if (tab != null) {
543 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700544 }
545 break;
546 }
547 }
548 };
549
550 }
551
John Reckef654f12011-07-12 16:42:08 -0700552 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200553 public Tab getCurrentTab() {
554 return mTabControl.getCurrentTab();
555 }
556
Michael Kolbba99c5d2010-11-29 14:57:41 -0800557 @Override
558 public void shareCurrentPage() {
559 shareCurrentPage(mTabControl.getCurrentTab());
560 }
561
562 private void shareCurrentPage(Tab tab) {
563 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800564 sharePage(mActivity, tab.getTitle(),
565 tab.getUrl(), tab.getFavicon(),
566 createScreenshot(tab.getWebView(),
567 getDesiredThumbnailWidth(mActivity),
568 getDesiredThumbnailHeight(mActivity)));
569 }
570 }
571
Michael Kolb8233fac2010-10-26 16:08:53 -0700572 /**
573 * Share a page, providing the title, url, favicon, and a screenshot. Uses
574 * an {@link Intent} to launch the Activity chooser.
575 * @param c Context used to launch a new Activity.
576 * @param title Title of the page. Stored in the Intent with
577 * {@link Intent#EXTRA_SUBJECT}
578 * @param url URL of the page. Stored in the Intent with
579 * {@link Intent#EXTRA_TEXT}
580 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
581 * with {@link Browser#EXTRA_SHARE_FAVICON}
582 * @param screenshot Bitmap of a screenshot of the page. Stored in the
583 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
584 */
585 static final void sharePage(Context c, String title, String url,
586 Bitmap favicon, Bitmap screenshot) {
587 Intent send = new Intent(Intent.ACTION_SEND);
588 send.setType("text/plain");
589 send.putExtra(Intent.EXTRA_TEXT, url);
590 send.putExtra(Intent.EXTRA_SUBJECT, title);
591 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
592 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
593 try {
594 c.startActivity(Intent.createChooser(send, c.getString(
595 R.string.choosertitle_sharevia)));
596 } catch(android.content.ActivityNotFoundException ex) {
597 // if no app handles it, do nothing
598 }
599 }
600
601 private void copy(CharSequence text) {
602 ClipboardManager cm = (ClipboardManager) mActivity
603 .getSystemService(Context.CLIPBOARD_SERVICE);
604 cm.setText(text);
605 }
606
607 // lifecycle
608
John Reck9c35b9c2012-05-30 10:08:50 -0700609 @Override
610 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700611 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800612 // update the menu in case of a locale change
613 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700614 if (mPageDialogsHandler != null) {
615 mPageDialogsHandler.onConfigurationChanged(config);
616 }
617 mUi.onConfigurationChanged(config);
618 }
619
620 @Override
621 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700622 if (!mUi.isWebShowing()) {
623 mUi.showWeb(false);
624 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 mIntentHandler.onNewIntent(intent);
626 }
627
John Reck9c35b9c2012-05-30 10:08:50 -0700628 @Override
629 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800630 if (mUi.isCustomViewShowing()) {
631 hideCustomView();
632 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 if (mActivityPaused) {
634 Log.e(LOGTAG, "BrowserActivity is already paused.");
635 return;
636 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800638 Tab tab = mTabControl.getCurrentTab();
639 if (tab != null) {
640 tab.pause();
641 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700642 if (mWakeLock == null) {
643 PowerManager pm = (PowerManager) mActivity
644 .getSystemService(Context.POWER_SERVICE);
645 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
646 }
Michael Kolb70976932010-11-30 11:34:01 -0800647 mWakeLock.acquire();
648 mHandler.sendMessageDelayed(mHandler
649 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
650 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700651 }
652 mUi.onPause();
653 mNetworkHandler.onPause();
654
655 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100656 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700657 if (sThumbnailBitmap != null) {
658 sThumbnailBitmap.recycle();
659 sThumbnailBitmap = null;
660 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 }
662
John Reck9c35b9c2012-05-30 10:08:50 -0700663 @Override
664 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800666 Bundle saveState = createSaveState();
667
668 // crash recovery manages all save & restore state
669 mCrashRecoveryHandler.writeState(saveState);
670 mSettings.setLastRunPaused(true);
671 }
672
673 /**
674 * Save the current state to outState. Does not write the state to
675 * disk.
676 * @return Bundle containing the current state of all tabs.
677 */
678 /* package */ Bundle createSaveState() {
679 Bundle saveState = new Bundle();
680 mTabControl.saveState(saveState);
681 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700682 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800683 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700684 }
George Mount3636d0a2011-11-21 09:08:21 -0800685 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 }
687
John Reck9c35b9c2012-05-30 10:08:50 -0700688 @Override
689 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700690 if (!mActivityPaused) {
691 Log.e(LOGTAG, "BrowserActivity is already resumed.");
692 return;
693 }
George Mount3636d0a2011-11-21 09:08:21 -0800694 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700695 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800696 Tab current = mTabControl.getCurrentTab();
697 if (current != null) {
698 current.resume();
699 resumeWebViewTimers(current);
700 }
John Reckf57c0292011-07-21 18:15:39 -0700701 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200702
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 mUi.onResume();
704 mNetworkHandler.onResume();
705 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100706 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700707 if (mVoiceResult != null) {
708 mUi.onVoiceResult(mVoiceResult);
709 mVoiceResult = null;
710 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700711 }
712
John Reckf57c0292011-07-21 18:15:39 -0700713 private void releaseWakeLock() {
714 if (mWakeLock != null && mWakeLock.isHeld()) {
715 mHandler.removeMessages(RELEASE_WAKELOCK);
716 mWakeLock.release();
717 }
718 }
719
Michael Kolb70976932010-11-30 11:34:01 -0800720 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800721 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800722 * @param tab guaranteed non-null
723 */
724 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 boolean inLoad = tab.inPageLoad();
726 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
727 CookieSyncManager.getInstance().startSync();
728 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100729 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700730 }
731 }
732
Michael Kolb70976932010-11-30 11:34:01 -0800733 /**
734 * Pause all WebView timers using the WebView of the given tab
735 * @param tab
736 * @return true if the timers are paused or tab is null
737 */
738 private boolean pauseWebViewTimers(Tab tab) {
739 if (tab == null) {
740 return true;
741 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100743 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 }
Michael Kolb70976932010-11-30 11:34:01 -0800746 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700747 }
748
John Reck9c35b9c2012-05-30 10:08:50 -0700749 @Override
750 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800751 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700752 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
753 mUploadHandler = null;
754 }
755 if (mTabControl == null) return;
756 mUi.onDestroy();
757 // Remove the current tab and sub window
758 Tab t = mTabControl.getCurrentTab();
759 if (t != null) {
760 dismissSubWindow(t);
761 removeTab(t);
762 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500763 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 // Destroy all the tabs
765 mTabControl.destroy();
766 WebIconDatabase.getInstance().close();
767 // Stop watching the default geolocation permissions
768 mSystemAllowGeolocationOrigins.stop();
769 mSystemAllowGeolocationOrigins = null;
770 }
771
772 protected boolean isActivityPaused() {
773 return mActivityPaused;
774 }
775
John Reck9c35b9c2012-05-30 10:08:50 -0700776 @Override
777 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700778 mTabControl.freeMemory();
779 }
780
781 @Override
782 public boolean shouldShowErrorConsole() {
783 return mShouldShowErrorConsole;
784 }
785
786 protected void setShouldShowErrorConsole(boolean show) {
787 if (show == mShouldShowErrorConsole) {
788 // Nothing to do.
789 return;
790 }
791 mShouldShowErrorConsole = show;
792 Tab t = mTabControl.getCurrentTab();
793 if (t == null) {
794 // There is no current tab so we cannot toggle the error console
795 return;
796 }
797 mUi.setShouldShowErrorConsole(t, show);
798 }
799
800 @Override
801 public void stopLoading() {
802 mLoadStopped = true;
803 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700805 if (w != null) {
806 w.stopLoading();
807 mUi.onPageStopped(tab);
808 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700809 }
810
811 boolean didUserStopLoading() {
812 return mLoadStopped;
813 }
814
815 // WebViewController
816
817 @Override
John Reck324d4402011-01-11 16:56:42 -0800818 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700819
820 // We've started to load a new page. If there was a pending message
821 // to save a screenshot then we will now take the new page and save
822 // an incorrect screenshot. Therefore, remove any pending thumbnail
823 // messages from the queue.
824 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800825 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700826
827 // reset sync timer to avoid sync starts during loading a page
828 CookieSyncManager.getInstance().resetSync();
829
830 if (!mNetworkHandler.isNetworkUp()) {
831 view.setNetworkAvailable(false);
832 }
833
834 // when BrowserActivity just starts, onPageStarted may be called before
835 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
836 // to start the timer. As we won't switch tabs while an activity is in
837 // pause state, we can ensure calling resume and pause in pair.
838 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800839 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 }
841 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 endActionMode();
843
John Reck30c714c2010-12-16 17:30:34 -0800844 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700845
John Reck324d4402011-01-11 16:56:42 -0800846 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 // update the bookmark database for favicon
848 maybeUpdateFavicon(tab, null, url, favicon);
849
850 Performance.tracePageStart(url);
851
852 // Performance probe
853 if (false) {
854 Performance.onPageStarted();
855 }
856
857 }
858
859 @Override
John Reck324d4402011-01-11 16:56:42 -0800860 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700861 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800862 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 // pause the WebView timer and release the wake lock if it is finished
864 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800865 if (mActivityPaused && pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700866 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700867 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200868
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 // Performance probe
870 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800871 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 }
873
874 Performance.tracePageFinished();
875 }
876
877 @Override
John Reck30c714c2010-12-16 17:30:34 -0800878 public void onProgressChanged(Tab tab) {
879 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700880
881 if (newProgress == 100) {
882 CookieSyncManager.getInstance().sync();
883 // onProgressChanged() may continue to be called after the main
884 // frame has finished loading, as any remaining sub frames continue
885 // to load. We'll only get called once though with newProgress as
886 // 100 when everything is loaded. (onPageFinished is called once
887 // when the main frame completes loading regardless of the state of
888 // any sub frames so calls to onProgressChanges may continue after
889 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800890 if (tab.inPageLoad()) {
891 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 }
John Reckd9862372012-02-21 15:04:50 -0800893 if (!tab.isPrivateBrowsingEnabled()
894 && !TextUtils.isEmpty(tab.getUrl())
895 && !tab.isSnapshot()) {
896 // Only update the bookmark screenshot if the user did not
897 // cancel the load early and there is not already
898 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700899 if (tab.shouldUpdateThumbnail() &&
900 (tab.inForeground() && !didUserStopLoading()
901 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800902 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
903 mHandler.sendMessageDelayed(mHandler.obtainMessage(
904 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
905 500);
906 }
907 }
908 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800910 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700911 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800912 // still loading
913 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700914 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800915 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 }
917 }
John Reck30c714c2010-12-16 17:30:34 -0800918 mUi.onProgressChanged(tab);
919 }
920
921 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100922 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800923 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 }
925
926 @Override
927 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800928 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800929 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800930 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700931 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
932 return;
933 }
934 // Update the title in the history database if not in private browsing mode
935 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700936 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700937 }
938 }
939
940 @Override
941 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800942 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
944 }
945
946 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800947 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
948 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700949 }
950
951 @Override
952 public boolean shouldOverrideKeyEvent(KeyEvent event) {
953 if (mMenuIsDown) {
954 // only check shortcut key when MENU is held
955 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
956 event);
957 } else {
958 return false;
959 }
960 }
961
962 @Override
John Reck997b1b72012-04-19 18:08:25 -0700963 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 if (!isActivityPaused()) {
965 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700966 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 } else {
John Reck997b1b72012-04-19 18:08:25 -0700968 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700969 }
970 }
John Reck997b1b72012-04-19 18:08:25 -0700971 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 }
973
974 @Override
John Reck324d4402011-01-11 16:56:42 -0800975 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 // Don't save anything in private browsing mode
977 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800978 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700979
John Reck324d4402011-01-11 16:56:42 -0800980 if (TextUtils.isEmpty(url)
981 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 return;
983 }
John Reckf57c0292011-07-21 18:15:39 -0700984 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700985 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 }
987
988 @Override
989 public void getVisitedHistory(final ValueCallback<String[]> callback) {
990 AsyncTask<Void, Void, String[]> task =
991 new AsyncTask<Void, Void, String[]>() {
992 @Override
993 public String[] doInBackground(Void... unused) {
994 return Browser.getVisitedHistory(mActivity.getContentResolver());
995 }
996 @Override
997 public void onPostExecute(String[] result) {
998 callback.onReceiveValue(result);
999 }
1000 };
1001 task.execute();
1002 }
1003
1004 @Override
1005 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1006 final HttpAuthHandler handler, final String host,
1007 final String realm) {
1008 String username = null;
1009 String password = null;
1010
1011 boolean reuseHttpAuthUsernamePassword
1012 = handler.useHttpAuthUsernamePassword();
1013
1014 if (reuseHttpAuthUsernamePassword && view != null) {
1015 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1016 if (credentials != null && credentials.length == 2) {
1017 username = credentials[0];
1018 password = credentials[1];
1019 }
1020 }
1021
1022 if (username != null && password != null) {
1023 handler.proceed(username, password);
1024 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001025 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1027 } else {
1028 handler.cancel();
1029 }
1030 }
1031 }
1032
1033 @Override
1034 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001035 String contentDisposition, String mimetype, String referer,
1036 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001037 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -05001038 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001039 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled());
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001040 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 // This Tab was opened for the sole purpose of downloading a
1042 // file. Remove it.
1043 if (tab == mTabControl.getCurrentTab()) {
1044 // In this case, the Tab is still on top.
1045 goBackOnePageOrQuit();
1046 } else {
1047 // In this case, it is not.
1048 closeTab(tab);
1049 }
1050 }
1051 }
1052
1053 @Override
1054 public Bitmap getDefaultVideoPoster() {
1055 return mUi.getDefaultVideoPoster();
1056 }
1057
1058 @Override
1059 public View getVideoLoadingProgressView() {
1060 return mUi.getVideoLoadingProgressView();
1061 }
1062
1063 @Override
1064 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1065 SslError error) {
1066 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1067 }
1068
Patrick Scott92066772011-03-10 08:46:27 -05001069 @Override
1070 public void showAutoLogin(Tab tab) {
1071 assert tab.inForeground();
1072 // Update the title bar to show the auto-login request.
1073 mUi.showAutoLogin(tab);
1074 }
1075
1076 @Override
1077 public void hideAutoLogin(Tab tab) {
1078 assert tab.inForeground();
1079 mUi.hideAutoLogin(tab);
1080 }
1081
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 // helper method
1083
1084 /*
1085 * Update the favorites icon if the private browsing isn't enabled and the
1086 * icon is valid.
1087 */
1088 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1089 final String url, Bitmap favicon) {
1090 if (favicon == null) {
1091 return;
1092 }
1093 if (!tab.isPrivateBrowsingEnabled()) {
1094 Bookmarks.updateFavicon(mActivity
1095 .getContentResolver(), originalUrl, url, favicon);
1096 }
1097 }
1098
Leon Scroggins4cd97792010-12-03 15:31:56 -05001099 @Override
1100 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001101 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001102 mUi.bookmarkedStatusHasChanged(tab);
1103 }
1104
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 // end WebViewController
1106
1107 protected void pageUp() {
1108 getCurrentTopWebView().pageUp(false);
1109 }
1110
1111 protected void pageDown() {
1112 getCurrentTopWebView().pageDown(false);
1113 }
1114
1115 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001116 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 public void editUrl() {
1118 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001119 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 }
1121
John Reck9c35b9c2012-05-30 10:08:50 -07001122 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001123 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001124 WebChromeClient.CustomViewCallback callback) {
1125 if (tab.inForeground()) {
1126 if (mUi.isCustomViewShowing()) {
1127 callback.onCustomViewHidden();
1128 return;
1129 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001130 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 // Save the menu state and set it to empty while the custom
1132 // view is showing.
1133 mOldMenuState = mMenuState;
1134 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001135 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 }
1137 }
1138
1139 @Override
1140 public void hideCustomView() {
1141 if (mUi.isCustomViewShowing()) {
1142 mUi.onHideCustomView();
1143 // Reset the old menu state.
1144 mMenuState = mOldMenuState;
1145 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001146 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001147 }
1148 }
1149
John Reck9c35b9c2012-05-30 10:08:50 -07001150 @Override
1151 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 Intent intent) {
1153 if (getCurrentTopWebView() == null) return;
1154 switch (requestCode) {
1155 case PREFERENCES_PAGE:
1156 if (resultCode == Activity.RESULT_OK && intent != null) {
1157 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001158 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 mTabControl.removeParentChildRelationShips();
1160 }
1161 }
1162 break;
1163 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001164 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001165 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001167 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001168 case AUTOFILL_SETUP:
1169 // Determine whether a profile was actually set up or not
1170 // and if so, send the message back to the WebTextView to
1171 // fill the form with the new profile.
1172 if (getSettings().getAutoFillProfile() != null) {
1173 mAutoFillSetupMessage.sendToTarget();
1174 mAutoFillSetupMessage = null;
1175 }
1176 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001177 case COMBO_VIEW:
1178 if (intent == null || resultCode != Activity.RESULT_OK) {
1179 break;
1180 }
John Reck3ba45532011-08-11 16:26:53 -07001181 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001182 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1183 Tab t = getCurrentTab();
1184 Uri uri = intent.getData();
1185 loadUrl(t, uri.toString());
1186 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1187 String[] urls = intent.getStringArrayExtra(
1188 ComboViewActivity.EXTRA_OPEN_ALL);
1189 Tab parent = getCurrentTab();
1190 for (String url : urls) {
1191 parent = openTab(url, parent,
1192 !mSettings.openInBackground(), true);
1193 }
1194 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1195 long id = intent.getLongExtra(
1196 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1197 if (id >= 0) {
1198 createNewSnapshotTab(id, true);
1199 }
1200 }
1201 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001202 case VOICE_RESULT:
1203 if (resultCode == Activity.RESULT_OK && intent != null) {
1204 ArrayList<String> results = intent.getStringArrayListExtra(
1205 RecognizerIntent.EXTRA_RESULTS);
1206 if (results.size() >= 1) {
1207 mVoiceResult = results.get(0);
1208 }
1209 }
1210 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 default:
1212 break;
1213 }
1214 getCurrentTopWebView().requestFocus();
1215 }
1216
1217 /**
1218 * Open the Go page.
1219 * @param startWithHistory If true, open starting on the history tab.
1220 * Otherwise, start with the bookmarks tab.
1221 */
1222 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001223 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001224 if (mTabControl.getCurrentWebView() == null) {
1225 return;
1226 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001227 // clear action mode
1228 if (isInCustomActionMode()) {
1229 endActionMode();
1230 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 Bundle extras = new Bundle();
1232 // Disable opening in a new window if we have maxed out the windows
1233 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1234 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001235 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001236 }
1237
1238 // combo view callbacks
1239
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 // key handling
1241 protected void onBackKey() {
1242 if (!mUi.onBackKey()) {
1243 WebView subwindow = mTabControl.getCurrentSubWindow();
1244 if (subwindow != null) {
1245 if (subwindow.canGoBack()) {
1246 subwindow.goBack();
1247 } else {
1248 dismissSubWindow(mTabControl.getCurrentTab());
1249 }
1250 } else {
1251 goBackOnePageOrQuit();
1252 }
1253 }
1254 }
1255
Michael Kolb4bd767d2011-05-27 11:33:55 -07001256 protected boolean onMenuKey() {
1257 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001258 }
1259
Michael Kolb8233fac2010-10-26 16:08:53 -07001260 // menu handling and state
1261 // TODO: maybe put into separate handler
1262
John Reck9c35b9c2012-05-30 10:08:50 -07001263 @Override
1264 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001265 if (mMenuState == EMPTY_MENU) {
1266 return false;
1267 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 MenuInflater inflater = mActivity.getMenuInflater();
1269 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001270 return true;
1271 }
1272
John Reck9c35b9c2012-05-30 10:08:50 -07001273 @Override
1274 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001275 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001276 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001277 return;
1278 }
1279 if (!(v instanceof WebView)) {
1280 return;
1281 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001282 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 WebView.HitTestResult result = webview.getHitTestResult();
1284 if (result == null) {
1285 return;
1286 }
1287
1288 int type = result.getType();
1289 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1290 Log.w(LOGTAG,
1291 "We should not show context menu when nothing is touched");
1292 return;
1293 }
1294 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1295 // let TextView handles context menu
1296 return;
1297 }
1298
1299 // Note, http://b/issue?id=1106666 is requesting that
1300 // an inflated menu can be used again. This is not available
1301 // yet, so inflate each time (yuk!)
1302 MenuInflater inflater = mActivity.getMenuInflater();
1303 inflater.inflate(R.menu.browsercontext, menu);
1304
1305 // Show the correct menu group
1306 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001307 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001308 menu.setGroupVisible(R.id.PHONE_MENU,
1309 type == WebView.HitTestResult.PHONE_TYPE);
1310 menu.setGroupVisible(R.id.EMAIL_MENU,
1311 type == WebView.HitTestResult.EMAIL_TYPE);
1312 menu.setGroupVisible(R.id.GEO_MENU,
1313 type == WebView.HitTestResult.GEO_TYPE);
1314 menu.setGroupVisible(R.id.IMAGE_MENU,
1315 type == WebView.HitTestResult.IMAGE_TYPE
1316 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1317 menu.setGroupVisible(R.id.ANCHOR_MENU,
1318 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1319 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001320 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1321 || type == WebView.HitTestResult.PHONE_TYPE
1322 || type == WebView.HitTestResult.EMAIL_TYPE
1323 || type == WebView.HitTestResult.GEO_TYPE;
1324 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1325 if (hitText) {
1326 menu.findItem(R.id.select_text_menu_id)
1327 .setOnMenuItemClickListener(new SelectText(webview));
1328 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001329 // Setup custom handling depending on the type
1330 switch (type) {
1331 case WebView.HitTestResult.PHONE_TYPE:
1332 menu.setHeaderTitle(Uri.decode(extra));
1333 menu.findItem(R.id.dial_context_menu_id).setIntent(
1334 new Intent(Intent.ACTION_VIEW, Uri
1335 .parse(WebView.SCHEME_TEL + extra)));
1336 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1337 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1338 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1339 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1340 addIntent);
1341 menu.findItem(R.id.copy_phone_context_menu_id)
1342 .setOnMenuItemClickListener(
1343 new Copy(extra));
1344 break;
1345
1346 case WebView.HitTestResult.EMAIL_TYPE:
1347 menu.setHeaderTitle(extra);
1348 menu.findItem(R.id.email_context_menu_id).setIntent(
1349 new Intent(Intent.ACTION_VIEW, Uri
1350 .parse(WebView.SCHEME_MAILTO + extra)));
1351 menu.findItem(R.id.copy_mail_context_menu_id)
1352 .setOnMenuItemClickListener(
1353 new Copy(extra));
1354 break;
1355
1356 case WebView.HitTestResult.GEO_TYPE:
1357 menu.setHeaderTitle(extra);
1358 menu.findItem(R.id.map_context_menu_id).setIntent(
1359 new Intent(Intent.ACTION_VIEW, Uri
1360 .parse(WebView.SCHEME_GEO
1361 + URLEncoder.encode(extra))));
1362 menu.findItem(R.id.copy_geo_context_menu_id)
1363 .setOnMenuItemClickListener(
1364 new Copy(extra));
1365 break;
1366
1367 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1368 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001369 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001370 // decide whether to show the open link in new tab option
1371 boolean showNewTab = mTabControl.canCreateNewTab();
1372 MenuItem newTabItem
1373 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001374 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001375 ? R.string.contextmenu_openlink_newwindow_background
1376 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001377 newTabItem.setVisible(showNewTab);
1378 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001379 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1380 newTabItem.setOnMenuItemClickListener(
1381 new MenuItem.OnMenuItemClickListener() {
1382 @Override
1383 public boolean onMenuItemClick(MenuItem item) {
1384 final HashMap<String, WebView> hrefMap =
1385 new HashMap<String, WebView>();
1386 hrefMap.put("webview", webview);
1387 final Message msg = mHandler.obtainMessage(
1388 FOCUS_NODE_HREF,
1389 R.id.open_newtab_context_menu_id,
1390 0, hrefMap);
1391 webview.requestFocusNodeHref(msg);
1392 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001393 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001394 });
1395 } else {
1396 newTabItem.setOnMenuItemClickListener(
1397 new MenuItem.OnMenuItemClickListener() {
1398 @Override
1399 public boolean onMenuItemClick(MenuItem item) {
1400 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001401 openTab(extra, parent,
1402 !mSettings.openInBackground(),
1403 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001404 return true;
1405 }
1406 });
1407 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001408 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1410 break;
1411 }
1412 // otherwise fall through to handle image part
1413 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001414 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1415 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1417 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001418 shareItem.setOnMenuItemClickListener(
1419 new MenuItem.OnMenuItemClickListener() {
1420 @Override
1421 public boolean onMenuItemClick(MenuItem item) {
1422 sharePage(mActivity, null, extra, null,
1423 null);
1424 return true;
1425 }
1426 }
1427 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001428 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001429 menu.findItem(R.id.view_image_context_menu_id)
1430 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1431 @Override
1432 public boolean onMenuItemClick(MenuItem item) {
1433 openTab(extra, mTabControl.getCurrentTab(), true, true);
1434 return false;
1435 }
1436 });
Michael Kolb8233fac2010-10-26 16:08:53 -07001437 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001438 setOnMenuItemClickListener(
1439 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001440 menu.findItem(R.id.set_wallpaper_context_menu_id).
1441 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1442 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 break;
1444
1445 default:
1446 Log.w(LOGTAG, "We should not get here.");
1447 break;
1448 }
1449 //update the ui
1450 mUi.onContextMenuCreated(menu);
1451 }
1452
1453 /**
1454 * As the menu can be open when loading state changes
1455 * we must manually update the state of the stop/reload menu
1456 * item
1457 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001458 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001459 if (menu == null) {
1460 return;
1461 }
1462 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001463 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001464 menu.findItem(R.id.stop_menu_id):
1465 menu.findItem(R.id.reload_menu_id);
1466 if (src != null) {
1467 dest.setIcon(src.getIcon());
1468 dest.setTitle(src.getTitle());
1469 }
1470 }
1471
John Reck9c35b9c2012-05-30 10:08:50 -07001472 @Override
1473 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001474 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001475 // hold on to the menu reference here; it is used by the page callbacks
1476 // to update the menu based on loading state
1477 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 // Note: setVisible will decide whether an item is visible; while
1479 // setEnabled() will decide whether an item is enabled, which also means
1480 // whether the matching shortcut key will function.
1481 switch (mMenuState) {
1482 case EMPTY_MENU:
1483 if (mCurrentMenuState != mMenuState) {
1484 menu.setGroupVisible(R.id.MAIN_MENU, false);
1485 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1486 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1487 }
1488 break;
1489 default:
1490 if (mCurrentMenuState != mMenuState) {
1491 menu.setGroupVisible(R.id.MAIN_MENU, true);
1492 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1493 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1494 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001495 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001496 break;
1497 }
1498 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001499 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001500 }
1501
Michael Kolb4bf79712011-07-14 14:18:12 -07001502 @Override
1503 public void updateMenuState(Tab tab, Menu menu) {
1504 boolean canGoBack = false;
1505 boolean canGoForward = false;
1506 boolean isHome = false;
John Reck42229bc2011-08-19 13:26:43 -07001507 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001508 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001509 if (tab != null) {
1510 canGoBack = tab.canGoBack();
1511 canGoForward = tab.canGoForward();
1512 isHome = mSettings.getHomePage().equals(tab.getUrl());
John Reck42229bc2011-08-19 13:26:43 -07001513 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001514 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001515 }
1516 final MenuItem back = menu.findItem(R.id.back_menu_id);
1517 back.setEnabled(canGoBack);
1518
1519 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1520 home.setEnabled(!isHome);
1521
1522 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1523 forward.setEnabled(canGoForward);
1524
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001525 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1526 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001527 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001528 if (source != null && dest != null) {
1529 dest.setTitle(source.getTitle());
1530 dest.setIcon(source.getIcon());
1531 }
John Recke1a03a32011-09-14 17:04:16 -07001532 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001533
1534 // decide whether to show the share link option
1535 PackageManager pm = mActivity.getPackageManager();
1536 Intent send = new Intent(Intent.ACTION_SEND);
1537 send.setType("text/plain");
1538 ResolveInfo ri = pm.resolveActivity(send,
1539 PackageManager.MATCH_DEFAULT_ONLY);
1540 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1541
1542 boolean isNavDump = mSettings.enableNavDump();
1543 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1544 nav.setVisible(isNavDump);
1545 nav.setEnabled(isNavDump);
1546
1547 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001548 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1549 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001550 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1551 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Michael Kolb315d5022011-10-13 12:47:11 -07001552 menu.setGroupVisible(R.id.COMBO_MENU, false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001553
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001554 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001555 }
1556
John Reck9c35b9c2012-05-30 10:08:50 -07001557 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001558 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 if (null == getCurrentTopWebView()) {
1560 return false;
1561 }
1562 if (mMenuIsDown) {
1563 // The shortcut action consumes the MENU. Even if it is still down,
1564 // it won't trigger the next shortcut action. In the case of the
1565 // shortcut action triggering a new activity, like Bookmarks, we
1566 // won't get onKeyUp for MENU. So it is important to reset it here.
1567 mMenuIsDown = false;
1568 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001569 if (mUi.onOptionsItemSelected(item)) {
1570 // ui callback handled it
1571 return true;
1572 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001573 switch (item.getItemId()) {
1574 // -- Main menu
1575 case R.id.new_tab_menu_id:
1576 openTabToHomePage();
1577 break;
1578
1579 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001580 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 break;
1582
Afzal Najamd4e33312012-04-26 01:54:01 -04001583 case R.id.close_other_tabs_id:
1584 closeOtherTabs();
1585 break;
1586
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 case R.id.goto_menu_id:
1588 editUrl();
1589 break;
1590
1591 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001592 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1593 break;
1594
1595 case R.id.history_menu_id:
1596 bookmarksOrHistoryPicker(ComboViews.History);
1597 break;
1598
1599 case R.id.snapshots_menu_id:
1600 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 break;
1602
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001604 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001605 break;
1606
1607 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001608 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 stopLoading();
1610 } else {
1611 getCurrentTopWebView().reload();
1612 }
1613 break;
1614
1615 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001616 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 break;
1618
1619 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001620 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 break;
1622
1623 case R.id.close_menu_id:
1624 // Close the subwindow if it exists.
1625 if (mTabControl.getCurrentSubWindow() != null) {
1626 dismissSubWindow(mTabControl.getCurrentTab());
1627 break;
1628 }
1629 closeCurrentTab();
1630 break;
1631
1632 case R.id.homepage_menu_id:
1633 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001634 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001635 break;
1636
1637 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001638 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001639 break;
1640
1641 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001642 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001643 break;
1644
John Reck2bc80422011-06-30 15:11:49 -07001645 case R.id.save_snapshot_menu_id:
1646 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001647 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001648 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001649 break;
1650
Michael Kolb8233fac2010-10-26 16:08:53 -07001651 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001652 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001653 break;
1654
John Recke1a03a32011-09-14 17:04:16 -07001655 case R.id.snapshot_go_live:
1656 goLive();
1657 return true;
1658
Michael Kolb8233fac2010-10-26 16:08:53 -07001659 case R.id.share_page_menu_id:
1660 Tab currentTab = mTabControl.getCurrentTab();
1661 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001662 return false;
1663 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001664 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001665 break;
1666
1667 case R.id.dump_nav_menu_id:
1668 getCurrentTopWebView().debugDump();
1669 break;
1670
Michael Kolb8233fac2010-10-26 16:08:53 -07001671 case R.id.zoom_in_menu_id:
1672 getCurrentTopWebView().zoomIn();
1673 break;
1674
1675 case R.id.zoom_out_menu_id:
1676 getCurrentTopWebView().zoomOut();
1677 break;
1678
1679 case R.id.view_downloads_menu_id:
1680 viewDownloads();
1681 break;
1682
John Reck42229bc2011-08-19 13:26:43 -07001683 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001684 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001685 break;
1686
Michael Kolb8233fac2010-10-26 16:08:53 -07001687 case R.id.window_one_menu_id:
1688 case R.id.window_two_menu_id:
1689 case R.id.window_three_menu_id:
1690 case R.id.window_four_menu_id:
1691 case R.id.window_five_menu_id:
1692 case R.id.window_six_menu_id:
1693 case R.id.window_seven_menu_id:
1694 case R.id.window_eight_menu_id:
1695 {
1696 int menuid = item.getItemId();
1697 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1698 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1699 Tab desiredTab = mTabControl.getTab(id);
1700 if (desiredTab != null &&
1701 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001702 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001703 }
1704 break;
1705 }
1706 }
1707 }
1708 break;
1709
1710 default:
1711 return false;
1712 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001713 return true;
1714 }
1715
John Reck68234a92012-04-19 15:27:12 -07001716 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1717 implements OnCancelListener {
1718
1719 private Tab mTab;
1720 private Dialog mProgressDialog;
1721 private ContentValues mValues;
1722
1723 private SaveSnapshotTask(Tab tab) {
1724 mTab = tab;
1725 }
1726
1727 @Override
1728 protected void onPreExecute() {
1729 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1730 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1731 true, true, this);
1732 mValues = mTab.createSnapshotValues();
1733 }
1734
1735 @Override
1736 protected Long doInBackground(Void... params) {
1737 if (!mTab.saveViewState(mValues)) {
1738 return null;
1739 }
1740 if (isCancelled()) {
1741 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1742 File file = mActivity.getFileStreamPath(path);
1743 if (!file.delete()) {
1744 file.deleteOnExit();
1745 }
1746 return null;
1747 }
1748 final ContentResolver cr = mActivity.getContentResolver();
1749 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1750 if (result == null) {
1751 return null;
1752 }
1753 long id = ContentUris.parseId(result);
1754 return id;
1755 }
1756
1757 @Override
1758 protected void onPostExecute(Long id) {
1759 if (isCancelled()) {
1760 return;
1761 }
1762 mProgressDialog.dismiss();
1763 if (id == null) {
1764 Toast.makeText(mActivity, R.string.snapshot_failed,
1765 Toast.LENGTH_SHORT).show();
1766 return;
1767 }
1768 Bundle b = new Bundle();
1769 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1770 mUi.showComboView(ComboViews.Snapshots, b);
1771 }
1772
1773 @Override
1774 public void onCancel(DialogInterface dialog) {
1775 cancel(true);
1776 }
1777 }
1778
Michael Kolb80f75082012-04-10 10:50:06 -07001779 @Override
1780 public void toggleUserAgent() {
1781 WebView web = getCurrentWebView();
1782 mSettings.toggleDesktopUseragent(web);
1783 web.loadUrl(web.getOriginalUrl());
1784 }
1785
1786 @Override
1787 public void findOnPage() {
1788 getCurrentTopWebView().showFindDialog(null, true);
1789 }
1790
1791 @Override
1792 public void openPreferences() {
1793 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1794 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1795 getCurrentTopWebView().getUrl());
1796 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1797 }
1798
1799 @Override
1800 public void bookmarkCurrentPage() {
1801 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1802 if (bookmarkIntent != null) {
1803 mActivity.startActivity(bookmarkIntent);
1804 }
1805 }
1806
John Recke1a03a32011-09-14 17:04:16 -07001807 private void goLive() {
1808 Tab t = getCurrentTab();
1809 t.loadUrl(t.getUrl(), null);
1810 }
1811
Michael Kolb315d5022011-10-13 12:47:11 -07001812 @Override
1813 public void showPageInfo() {
1814 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1815 }
1816
John Reck9c35b9c2012-05-30 10:08:50 -07001817 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001818 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001819 // Let the History and Bookmark fragments handle menus they created.
1820 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1821 return false;
1822 }
1823
Michael Kolb8233fac2010-10-26 16:08:53 -07001824 int id = item.getItemId();
1825 boolean result = true;
1826 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001827 // -- Browser context menu
1828 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001829 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001830 case R.id.copy_link_context_menu_id:
1831 final WebView webView = getCurrentTopWebView();
1832 if (null == webView) {
1833 result = false;
1834 break;
1835 }
1836 final HashMap<String, WebView> hrefMap =
1837 new HashMap<String, WebView>();
1838 hrefMap.put("webview", webView);
1839 final Message msg = mHandler.obtainMessage(
1840 FOCUS_NODE_HREF, id, 0, hrefMap);
1841 webView.requestFocusNodeHref(msg);
1842 break;
1843
1844 default:
1845 // For other context menus
1846 result = onOptionsItemSelected(item);
1847 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001848 return result;
1849 }
1850
1851 /**
1852 * support programmatically opening the context menu
1853 */
1854 public void openContextMenu(View view) {
1855 mActivity.openContextMenu(view);
1856 }
1857
1858 /**
1859 * programmatically open the options menu
1860 */
1861 public void openOptionsMenu() {
1862 mActivity.openOptionsMenu();
1863 }
1864
John Reck9c35b9c2012-05-30 10:08:50 -07001865 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001866 public boolean onMenuOpened(int featureId, Menu menu) {
1867 if (mOptionsMenuOpen) {
1868 if (mConfigChanged) {
1869 // We do not need to make any changes to the state of the
1870 // title bar, since the only thing that happened was a
1871 // change in orientation
1872 mConfigChanged = false;
1873 } else {
1874 if (!mExtendedMenuOpen) {
1875 mExtendedMenuOpen = true;
1876 mUi.onExtendedMenuOpened();
1877 } else {
1878 // Switching the menu back to icon view, so show the
1879 // title bar once again.
1880 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001881 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001882 }
1883 }
1884 } else {
1885 // The options menu is closed, so open it, and show the title
1886 mOptionsMenuOpen = true;
1887 mConfigChanged = false;
1888 mExtendedMenuOpen = false;
1889 mUi.onOptionsMenuOpened();
1890 }
1891 return true;
1892 }
1893
John Reck9c35b9c2012-05-30 10:08:50 -07001894 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001895 public void onOptionsMenuClosed(Menu menu) {
1896 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001897 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001898 }
1899
John Reck9c35b9c2012-05-30 10:08:50 -07001900 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001901 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001902 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001903 }
1904
1905 // Helper method for getting the top window.
1906 @Override
1907 public WebView getCurrentTopWebView() {
1908 return mTabControl.getCurrentTopWebView();
1909 }
1910
1911 @Override
1912 public WebView getCurrentWebView() {
1913 return mTabControl.getCurrentWebView();
1914 }
1915
1916 /*
1917 * This method is called as a result of the user selecting the options
1918 * menu to see the download window. It shows the download window on top of
1919 * the current window.
1920 */
1921 void viewDownloads() {
1922 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1923 mActivity.startActivity(intent);
1924 }
1925
John Reck30b065e2011-07-19 10:58:05 -07001926 int getActionModeHeight() {
1927 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1928 new int[] { android.R.attr.actionBarSize });
1929 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1930 actionBarSizeTypedArray.recycle();
1931 return size;
1932 }
1933
Michael Kolb8233fac2010-10-26 16:08:53 -07001934 // action mode
1935
John Reck9c35b9c2012-05-30 10:08:50 -07001936 @Override
1937 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 mUi.onActionModeStarted(mode);
1939 mActionMode = mode;
1940 }
1941
1942 /*
1943 * True if a custom ActionMode (i.e. find or select) is in use.
1944 */
1945 @Override
1946 public boolean isInCustomActionMode() {
1947 return mActionMode != null;
1948 }
1949
1950 /*
1951 * End the current ActionMode.
1952 */
1953 @Override
1954 public void endActionMode() {
1955 if (mActionMode != null) {
1956 mActionMode.finish();
1957 }
1958 }
1959
1960 /*
1961 * Called by find and select when they are finished. Replace title bars
1962 * as necessary.
1963 */
John Reck9c35b9c2012-05-30 10:08:50 -07001964 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001965 public void onActionModeFinished(ActionMode mode) {
1966 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001967 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001968 mActionMode = null;
1969 }
1970
1971 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001972 final Tab tab = getCurrentTab();
1973 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07001974 }
1975
1976 // bookmark handling
1977
1978 /**
1979 * add the current page as a bookmark to the given folder id
1980 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001981 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001982 * bookmarked, and if it is, edit that bookmark. If false, and
1983 * the site is already bookmarked, do not attempt to edit the
1984 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001985 */
1986 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001987 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07001988 WebView w = getCurrentTopWebView();
1989 if (w == null) {
1990 return null;
1991 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 Intent i = new Intent(mActivity,
1993 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1995 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1996 String touchIconUrl = w.getTouchIconUrl();
1997 if (touchIconUrl != null) {
1998 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1999 WebSettings settings = w.getSettings();
2000 if (settings != null) {
2001 i.putExtra(AddBookmarkPage.USER_AGENT,
2002 settings.getUserAgentString());
2003 }
2004 }
2005 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2006 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2007 getDesiredThumbnailHeight(mActivity)));
2008 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002009 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002010 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2011 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002012 // Put the dialog at the upper right of the screen, covering the
2013 // star on the title bar.
2014 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002015 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002016 }
2017
2018 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002019 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002020 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002021 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002022 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002023 }
2024
2025 // thumbnails
2026
2027 /**
2028 * Return the desired width for thumbnail screenshots, which are stored in
2029 * the database, and used on the bookmarks screen.
2030 * @param context Context for finding out the density of the screen.
2031 * @return desired width for thumbnail screenshot.
2032 */
2033 static int getDesiredThumbnailWidth(Context context) {
2034 return context.getResources().getDimensionPixelOffset(
2035 R.dimen.bookmarkThumbnailWidth);
2036 }
2037
2038 /**
2039 * Return the desired height for thumbnail screenshots, which are stored in
2040 * the database, and used on the bookmarks screen.
2041 * @param context Context for finding out the density of the screen.
2042 * @return desired height for thumbnail screenshot.
2043 */
2044 static int getDesiredThumbnailHeight(Context context) {
2045 return context.getResources().getDimensionPixelOffset(
2046 R.dimen.bookmarkThumbnailHeight);
2047 }
2048
John Reck8cc92352011-07-06 17:41:52 -07002049 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002050 if (view == null || view.getContentHeight() == 0
2051 || view.getContentWidth() == 0) {
2052 return null;
2053 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002054 // We render to a bitmap 2x the desired size so that we can then
2055 // re-scale it with filtering since canvas.scale doesn't filter
2056 // This helps reduce aliasing at the cost of being slightly blurry
2057 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002058 int scaledWidth = width * filter_scale;
2059 int scaledHeight = height * filter_scale;
2060 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2061 || sThumbnailBitmap.getHeight() != scaledHeight) {
2062 if (sThumbnailBitmap != null) {
2063 sThumbnailBitmap.recycle();
2064 sThumbnailBitmap = null;
2065 }
2066 sThumbnailBitmap =
2067 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002068 }
John Reckd7dd9b22011-08-30 09:18:29 -07002069 Canvas canvas = new Canvas(sThumbnailBitmap);
2070 int contentWidth = view.getContentWidth();
2071 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2072 if (view instanceof BrowserWebView) {
2073 int dy = -((BrowserWebView)view).getTitleHeight();
2074 canvas.translate(0, dy * overviewScale);
2075 }
2076
2077 canvas.scale(overviewScale, overviewScale);
2078
2079 if (view instanceof BrowserWebView) {
2080 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002082 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002083 }
John Reckd7dd9b22011-08-30 09:18:29 -07002084 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2085 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002086 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002087 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002088 }
2089
John Reck34ef2672011-02-10 11:30:55 -08002090 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002092 // FIXME: Would like to make sure there is actually something to
2093 // draw, but the API for that (WebViewCore.pictureReady()) is not
2094 // currently accessible here.
2095
John Reck34ef2672011-02-10 11:30:55 -08002096 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002097 if (view == null) {
2098 // Tab was destroyed
2099 return;
2100 }
John Reck34ef2672011-02-10 11:30:55 -08002101 final String url = tab.getUrl();
2102 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002103 if (TextUtils.isEmpty(url)) {
2104 return;
2105 }
2106
2107 // Only update thumbnails for web urls (http(s)://), not for
2108 // about:, javascript:, data:, etc...
2109 // Unless it is a bookmarked site, then always update
2110 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2111 return;
2112 }
2113
Michael Kolb8233fac2010-10-26 16:08:53 -07002114 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2115 getDesiredThumbnailHeight(mActivity));
2116 if (bm == null) {
2117 return;
2118 }
2119
2120 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002121 new AsyncTask<Void, Void, Void>() {
2122 @Override
2123 protected Void doInBackground(Void... unused) {
2124 Cursor cursor = null;
2125 try {
2126 // TODO: Clean this up
2127 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2128 if (cursor != null && cursor.moveToFirst()) {
2129 final ByteArrayOutputStream os =
2130 new ByteArrayOutputStream();
2131 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002132
John Reck34ef2672011-02-10 11:30:55 -08002133 ContentValues values = new ContentValues();
2134 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002135
John Reck34ef2672011-02-10 11:30:55 -08002136 do {
John Reck617fd832011-02-16 14:35:59 -08002137 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002138 cr.update(Images.CONTENT_URI, values, null, null);
2139 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002140 }
John Reck34ef2672011-02-10 11:30:55 -08002141 } catch (IllegalStateException e) {
2142 // Ignore
2143 } finally {
2144 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 }
John Reck34ef2672011-02-10 11:30:55 -08002146 return null;
2147 }
2148 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 }
2150
2151 private class Copy implements OnMenuItemClickListener {
2152 private CharSequence mText;
2153
John Reck9c35b9c2012-05-30 10:08:50 -07002154 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002155 public boolean onMenuItemClick(MenuItem item) {
2156 copy(mText);
2157 return true;
2158 }
2159
2160 public Copy(CharSequence toCopy) {
2161 mText = toCopy;
2162 }
2163 }
2164
Leon Scroggins63c02662010-11-18 15:16:27 -05002165 private static class Download implements OnMenuItemClickListener {
2166 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002167 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002168 private boolean mPrivateBrowsing;
George Mount387d45d2011-10-07 15:57:53 -07002169 private static final String FALLBACK_EXTENSION = "dat";
2170 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002171
John Reck9c35b9c2012-05-30 10:08:50 -07002172 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002173 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002174 if (DataUri.isDataUri(mText)) {
2175 saveDataUri();
2176 } else {
2177 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07002178 null, null, null, mPrivateBrowsing);
George Mount387d45d2011-10-07 15:57:53 -07002179 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002180 return true;
2181 }
2182
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002183 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002184 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002185 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002186 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002187 }
George Mount387d45d2011-10-07 15:57:53 -07002188
2189 /**
2190 * Treats mText as a data URI and writes its contents to a file
2191 * based on the current time.
2192 */
2193 private void saveDataUri() {
2194 FileOutputStream outputStream = null;
2195 try {
2196 DataUri uri = new DataUri(mText);
2197 File target = getTarget(uri);
2198 outputStream = new FileOutputStream(target);
2199 outputStream.write(uri.getData());
2200 final DownloadManager manager =
2201 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2202 manager.addCompletedDownload(target.getName(),
2203 mActivity.getTitle().toString(), false,
2204 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002205 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002206 } catch (IOException e) {
2207 Log.e(LOGTAG, "Could not save data URL");
2208 } finally {
2209 if (outputStream != null) {
2210 try {
2211 outputStream.close();
2212 } catch (IOException e) {
2213 // ignore close errors
2214 }
2215 }
2216 }
2217 }
2218
2219 /**
2220 * Creates a File based on the current time stamp and uses
2221 * the mime type of the DataUri to get the extension.
2222 */
2223 private File getTarget(DataUri uri) throws IOException {
2224 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
2225 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT);
2226 String nameBase = format.format(new Date());
2227 String mimeType = uri.getMimeType();
2228 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2229 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2230 if (extension == null) {
2231 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2232 extension = FALLBACK_EXTENSION;
2233 }
2234 extension = "." + extension; // createTempFile needs the '.'
2235 File targetFile = File.createTempFile(nameBase, extension, dir);
2236 return targetFile;
2237 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002238 }
2239
Cary Clark8974d282010-11-22 10:46:05 -05002240 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002241 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002242
John Reck9c35b9c2012-05-30 10:08:50 -07002243 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002244 public boolean onMenuItemClick(MenuItem item) {
2245 if (mWebView != null) {
2246 return mWebView.selectText();
2247 }
2248 return false;
2249 }
2250
2251 public SelectText(WebView webView) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002252 mWebView = WebViewClassic.fromWebView(webView);
Cary Clark8974d282010-11-22 10:46:05 -05002253 }
2254
2255 }
2256
Michael Kolb8233fac2010-10-26 16:08:53 -07002257 /********************** TODO: UI stuff *****************************/
2258
2259 // these methods have been copied, they still need to be cleaned up
2260
2261 /****************** tabs ***************************************************/
2262
2263 // basic tab interactions:
2264
2265 // it is assumed that tabcontrol already knows about the tab
2266 protected void addTab(Tab tab) {
2267 mUi.addTab(tab);
2268 }
2269
2270 protected void removeTab(Tab tab) {
2271 mUi.removeTab(tab);
2272 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002273 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002274 }
2275
Michael Kolbf2055602011-04-09 17:20:03 -07002276 @Override
2277 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002278 // monkey protection against delayed start
2279 if (tab != null) {
2280 mTabControl.setCurrentTab(tab);
2281 // the tab is guaranteed to have a webview after setCurrentTab
2282 mUi.setActiveTab(tab);
2283 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002284 }
2285
John Reck8bcafc12011-08-29 16:43:02 -07002286 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002287 Tab current = mTabControl.getCurrentTab();
2288 if (current != null
2289 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002290 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002291 }
2292 }
2293
John Reck26b18322011-06-21 13:08:58 -07002294 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002295 // Dismiss the subwindow if applicable.
2296 dismissSubWindow(appTab);
2297 // Since we might kill the WebView, remove it from the
2298 // content view first.
2299 mUi.detachTab(appTab);
2300 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002301 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 // TODO: analyze why the remove and add are necessary
2303 mUi.attachTab(appTab);
2304 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002305 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002306 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002307 } else {
2308 // If the tab was the current tab, we have to attach
2309 // it to the view system again.
2310 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002311 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002312 }
2313 }
2314
2315 // Remove the sub window if it exists. Also called by TabControl when the
2316 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002317 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002318 public void dismissSubWindow(Tab tab) {
2319 removeSubWindow(tab);
2320 // dismiss the subwindow. This will destroy the WebView.
2321 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002322 WebView wv = getCurrentTopWebView();
2323 if (wv != null) {
2324 wv.requestFocus();
2325 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002326 }
2327
2328 @Override
2329 public void removeSubWindow(Tab t) {
2330 if (t.getSubWebView() != null) {
2331 mUi.removeSubWindow(t.getSubViewContainer());
2332 }
2333 }
2334
2335 @Override
2336 public void attachSubWindow(Tab tab) {
2337 if (tab.getSubWebView() != null) {
2338 mUi.attachSubWindow(tab.getSubViewContainer());
2339 getCurrentTopWebView().requestFocus();
2340 }
2341 }
2342
Mathew Inwood29721c22011-06-29 17:55:24 +01002343 private Tab showPreloadedTab(final UrlData urlData) {
2344 if (!urlData.isPreloaded()) {
2345 return null;
2346 }
2347 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2348 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2349 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002350 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002351 // Could not submit query. Fallback to regular tab creation
2352 tabControl.destroy();
2353 return null;
2354 }
2355 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002356 // check tab count and make room for new tab
2357 if (!mTabControl.canCreateNewTab()) {
2358 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2359 if (leastUsed != null) {
2360 closeTab(leastUsed);
2361 }
2362 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002363 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002364 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002365 mTabControl.addPreloadedTab(t);
2366 addTab(t);
2367 setActiveTab(t);
2368 return t;
2369 }
2370
Michael Kolb7bcafde2011-05-09 13:55:59 -07002371 // open a non inconito tab with the given url data
2372 // and set as active tab
2373 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002374 Tab tab = showPreloadedTab(urlData);
2375 if (tab == null) {
2376 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002377 if ((tab != null) && !urlData.isEmpty()) {
2378 loadUrlDataIn(tab, urlData);
2379 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002380 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002381 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002382 }
2383
Michael Kolb843510f2010-12-09 10:51:49 -08002384 @Override
2385 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002386 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002387 }
2388
Michael Kolb8233fac2010-10-26 16:08:53 -07002389 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002390 public Tab openIncognitoTab() {
2391 return openTab(INCOGNITO_URI, true, true, false);
2392 }
2393
2394 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002395 public Tab openTab(String url, boolean incognito, boolean setActive,
2396 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002397 return openTab(url, incognito, setActive, useCurrent, null);
2398 }
2399
2400 @Override
2401 public Tab openTab(String url, Tab parent, boolean setActive,
2402 boolean useCurrent) {
2403 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2404 setActive, useCurrent, parent);
2405 }
2406
2407 public Tab openTab(String url, boolean incognito, boolean setActive,
2408 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002409 Tab tab = createNewTab(incognito, setActive, useCurrent);
2410 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002411 if (parent != null && parent != tab) {
2412 parent.addChildTab(tab);
2413 }
Michael Kolb519d2282011-05-09 17:03:19 -07002414 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002415 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002416 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002417 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002418 return tab;
2419 }
2420
2421 // this method will attempt to create a new tab
2422 // incognito: private browsing tab
2423 // setActive: ste tab as current tab
2424 // useCurrent: if no new tab can be created, return current tab
2425 private Tab createNewTab(boolean incognito, boolean setActive,
2426 boolean useCurrent) {
2427 Tab tab = null;
2428 if (mTabControl.canCreateNewTab()) {
2429 tab = mTabControl.createNewTab(incognito);
2430 addTab(tab);
2431 if (setActive) {
2432 setActiveTab(tab);
2433 }
2434 } else {
2435 if (useCurrent) {
2436 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002437 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002438 } else {
2439 mUi.showMaxTabsWarning();
2440 }
2441 }
2442 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002443 }
2444
John Reck2bc80422011-06-30 15:11:49 -07002445 @Override
2446 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2447 SnapshotTab tab = null;
2448 if (mTabControl.canCreateNewTab()) {
2449 tab = mTabControl.createSnapshotTab(snapshotId);
2450 addTab(tab);
2451 if (setActive) {
2452 setActiveTab(tab);
2453 }
2454 } else {
2455 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002456 }
2457 return tab;
2458 }
2459
Michael Kolb8233fac2010-10-26 16:08:53 -07002460 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002461 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002462 * @return boolean True if we successfully switched to a different tab. If
2463 * the indexth tab is null, or if that tab is the same as
2464 * the current one, return false.
2465 */
2466 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002467 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002468 Tab currentTab = mTabControl.getCurrentTab();
2469 if (tab == null || tab == currentTab) {
2470 return false;
2471 }
2472 setActiveTab(tab);
2473 return true;
2474 }
2475
2476 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002477 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002478 closeCurrentTab(false);
2479 }
2480
2481 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002482 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002483 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002484 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002485 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002486 return;
2487 }
Michael Kolbc831b632011-05-11 09:30:34 -07002488 final Tab current = mTabControl.getCurrentTab();
2489 final int pos = mTabControl.getCurrentPosition();
2490 Tab newTab = current.getParent();
2491 if (newTab == null) {
2492 newTab = mTabControl.getTab(pos + 1);
2493 if (newTab == null) {
2494 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002495 }
2496 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002497 if (andQuit) {
2498 mTabControl.setCurrentTab(newTab);
2499 closeTab(current);
2500 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002501 // Close window
2502 closeTab(current);
2503 }
2504 }
2505
2506 /**
2507 * Close the tab, remove its associated title bar, and adjust mTabControl's
2508 * current tab to a valid value.
2509 */
2510 @Override
2511 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002512 if (tab == mTabControl.getCurrentTab()) {
2513 closeCurrentTab();
2514 } else {
2515 removeTab(tab);
2516 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002517 }
2518
Afzal Najamd4e33312012-04-26 01:54:01 -04002519 /**
2520 * Close all tabs except the current one
2521 */
2522 @Override
2523 public void closeOtherTabs() {
2524 int inactiveTabs = mTabControl.getTabCount() - 1;
2525 for (int i = inactiveTabs; i >= 0; i--) {
2526 Tab tab = mTabControl.getTab(i);
2527 if (tab != mTabControl.getCurrentTab()) {
2528 removeTab(tab);
2529 }
2530 }
2531 }
2532
Michael Kolb8233fac2010-10-26 16:08:53 -07002533 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002534 protected void loadUrlFromContext(String url) {
2535 Tab tab = getCurrentTab();
2536 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002537 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002538 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002539 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002540 if (!WebViewClassic.fromWebView(view).getWebViewClient().
2541 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002542 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002543 }
2544 }
2545 }
2546
2547 /**
2548 * Load the URL into the given WebView and update the title bar
2549 * to reflect the new load. Call this instead of WebView.loadUrl
2550 * directly.
2551 * @param view The WebView used to load url.
2552 * @param url The URL to load.
2553 */
John Reck71e51422011-07-01 16:49:28 -07002554 @Override
2555 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002556 loadUrl(tab, url, null);
2557 }
2558
2559 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2560 if (tab != null) {
2561 dismissSubWindow(tab);
2562 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002563 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002564 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002565 }
2566
2567 /**
2568 * Load UrlData into a Tab and update the title bar to reflect the new
2569 * load. Call this instead of UrlData.loadIn directly.
2570 * @param t The Tab used to load.
2571 * @param data The UrlData being loaded.
2572 */
2573 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002574 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002575 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002576 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002577 } else {
John Reck38b39652012-06-05 09:22:59 -07002578 if (t != null && data.mDisableUrlOverride) {
2579 t.disableUrlOverridingForLoad();
2580 }
John Reck26b18322011-06-21 13:08:58 -07002581 loadUrl(t, data.mUrl, data.mHeaders);
2582 }
2583 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002584 }
2585
John Reck30c714c2010-12-16 17:30:34 -08002586 @Override
2587 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002588 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002589 if (tab.canGoBack()) {
2590 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002591 } else {
John Reckef654f12011-07-12 16:42:08 -07002592 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002593 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002594 }
2595
2596 void goBackOnePageOrQuit() {
2597 Tab current = mTabControl.getCurrentTab();
2598 if (current == null) {
2599 /*
2600 * Instead of finishing the activity, simply push this to the back
2601 * of the stack and let ActivityManager to choose the foreground
2602 * activity. As BrowserActivity is singleTask, it will be always the
2603 * root of the task. So we can use either true or false for
2604 * moveTaskToBack().
2605 */
2606 mActivity.moveTaskToBack(true);
2607 return;
2608 }
John Reckef654f12011-07-12 16:42:08 -07002609 if (current.canGoBack()) {
2610 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002611 } else {
2612 // Check to see if we are closing a window that was created by
2613 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002614 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002615 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002616 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002617 // Now we close the other tab
2618 closeTab(current);
2619 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002620 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002621 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002622 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002623 /*
2624 * Instead of finishing the activity, simply push this to the back
2625 * of the stack and let ActivityManager to choose the foreground
2626 * activity. As BrowserActivity is singleTask, it will be always the
2627 * root of the task. So we can use either true or false for
2628 * moveTaskToBack().
2629 */
2630 mActivity.moveTaskToBack(true);
2631 }
2632 }
2633 }
2634
2635 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002636 * helper method for key handler
2637 * returns the current tab if it can't advance
2638 */
Michael Kolbc831b632011-05-11 09:30:34 -07002639 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002640 int pos = mTabControl.getCurrentPosition() + 1;
2641 if (pos >= mTabControl.getTabCount()) {
2642 pos = 0;
2643 }
2644 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002645 }
2646
2647 /**
2648 * helper method for key handler
2649 * returns the current tab if it can't advance
2650 */
Michael Kolbc831b632011-05-11 09:30:34 -07002651 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002652 int pos = mTabControl.getCurrentPosition() - 1;
2653 if ( pos < 0) {
2654 pos = mTabControl.getTabCount() - 1;
2655 }
2656 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002657 }
2658
John Reckbcef87f2012-02-03 14:58:34 -08002659 boolean isMenuOrCtrlKey(int keyCode) {
2660 return (KeyEvent.KEYCODE_MENU == keyCode)
2661 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2662 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2663 }
2664
Michael Kolb0035fad2011-03-14 13:25:28 -07002665 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002666 * handle key events in browser
2667 *
2668 * @param keyCode
2669 * @param event
2670 * @return true if handled, false to pass to super
2671 */
John Reck9c35b9c2012-05-30 10:08:50 -07002672 @Override
2673 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002674 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002675 // Even if MENU is already held down, we need to call to super to open
2676 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002677 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002678 mMenuIsDown = true;
2679 return false;
2680 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002681
Cary Clark8ff8c662010-12-29 15:03:05 -05002682 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002683 Tab tab = getCurrentTab();
2684 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002685
Cary Clark160bbb92011-01-10 11:17:07 -05002686 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2687 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002688
Michael Kolb8233fac2010-10-26 16:08:53 -07002689 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002690 case KeyEvent.KEYCODE_TAB:
2691 if (event.isCtrlPressed()) {
2692 if (event.isShiftPressed()) {
2693 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002694 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002695 } else {
2696 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002697 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002698 }
2699 return true;
2700 }
2701 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002702 case KeyEvent.KEYCODE_SPACE:
2703 // WebView/WebTextView handle the keys in the KeyDown. As
2704 // the Activity's shortcut keys are only handled when WebView
2705 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002706 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002707 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002708 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002709 pageDown();
2710 }
2711 return true;
2712 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002713 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002714 event.startTracking();
2715 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002716 case KeyEvent.KEYCODE_FORWARD:
2717 if (!noModifiers) break;
2718 tab.goForward();
2719 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002720 case KeyEvent.KEYCODE_DPAD_LEFT:
2721 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002722 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002723 return true;
2724 }
2725 break;
2726 case KeyEvent.KEYCODE_DPAD_RIGHT:
2727 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002728 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002729 return true;
2730 }
2731 break;
2732 case KeyEvent.KEYCODE_A:
2733 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002734 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002735 return true;
2736 }
2737 break;
Michael Kolba4183062011-01-16 10:43:21 -08002738// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002739 case KeyEvent.KEYCODE_C:
2740 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002741 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002742 return true;
2743 }
2744 break;
Michael Kolba4183062011-01-16 10:43:21 -08002745// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002746// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002747// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002748// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002749// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002750// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002751// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002752// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002753// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002754// case KeyEvent.KEYCODE_M: // unused
2755// case KeyEvent.KEYCODE_N: // in Chrome: new window
2756// case KeyEvent.KEYCODE_O: // in Chrome: open file
2757// case KeyEvent.KEYCODE_P: // in Chrome: print page
2758// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002759// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002760// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2761 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002762 // we can't use the ctrl/shift flags, they check for
2763 // exclusive use of a modifier
2764 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002765 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002766 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002767 } else {
2768 openTabToHomePage();
2769 }
2770 return true;
2771 }
2772 break;
2773// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2774// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002775// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002776// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2777// case KeyEvent.KEYCODE_Y: // unused
2778// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002779 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002780 // it is a regular key and webview is not null
2781 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002782 }
2783
John Reck9c35b9c2012-05-30 10:08:50 -07002784 @Override
2785 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002786 switch(keyCode) {
2787 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002788 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002789 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002790 return true;
2791 }
2792 break;
2793 }
2794 return false;
2795 }
2796
John Reck9c35b9c2012-05-30 10:08:50 -07002797 @Override
2798 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002799 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002800 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002801 if (KeyEvent.KEYCODE_MENU == keyCode
2802 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002803 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002804 }
2805 }
Cary Clark160bbb92011-01-10 11:17:07 -05002806 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002807 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002808 case KeyEvent.KEYCODE_BACK:
2809 if (event.isTracking() && !event.isCanceled()) {
2810 onBackKey();
2811 return true;
2812 }
2813 break;
2814 }
2815 return false;
2816 }
2817
2818 public boolean isMenuDown() {
2819 return mMenuIsDown;
2820 }
2821
John Reck9c35b9c2012-05-30 10:08:50 -07002822 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002823 public void setupAutoFill(Message message) {
2824 // Open the settings activity at the AutoFill profile fragment so that
2825 // the user can create a new profile. When they return, we will dispatch
2826 // the message so that we can autofill the form using their new profile.
2827 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2828 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2829 AutoFillSettingsFragment.class.getName());
2830 mAutoFillSetupMessage = message;
2831 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2832 }
John Reckb3417f02011-01-14 11:01:05 -08002833
John Reck9c35b9c2012-05-30 10:08:50 -07002834 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002835 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002836 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002837 return true;
2838 }
2839
John Reck1cf4b792011-07-26 10:22:22 -07002840 @Override
2841 public boolean shouldCaptureThumbnails() {
2842 return mUi.shouldCaptureThumbnails();
2843 }
2844
Michael Kolbc3af0672011-08-09 10:24:41 -07002845 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002846 public boolean supportsVoice() {
2847 PackageManager pm = mActivity.getPackageManager();
2848 List activities = pm.queryIntentActivities(new Intent(
2849 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2850 return activities.size() != 0;
2851 }
2852
2853 @Override
2854 public void startVoiceRecognizer() {
2855 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2856 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2857 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2858 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2859 mActivity.startActivityForResult(voice, VOICE_RESULT);
2860 }
2861
2862 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002863 public void setBlockEvents(boolean block) {
2864 mBlockEvents = block;
2865 }
2866
John Reck9c35b9c2012-05-30 10:08:50 -07002867 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002868 public boolean dispatchKeyEvent(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 dispatchKeyShortcutEvent(KeyEvent event) {
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 dispatchTouchEvent(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 dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002884 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002885 }
2886
John Reck9c35b9c2012-05-30 10:08:50 -07002887 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002888 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002889 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002890 }
2891
Michael Kolb8233fac2010-10-26 16:08:53 -07002892}