blob: 00aac0837fbbdb233a37d84c3ad7b9c67f952e49 [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;
kaiyizb7bf1f22013-10-02 11:42:23 +080020import android.app.ActivityManagerNative;
luxiaolb40014b2013-07-19 10:01:43 +080021import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.ProgressDialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070025import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070027import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.content.ContentValues;
29import android.content.Context;
John Reck68234a92012-04-19 15:27:12 -070030import android.content.DialogInterface;
31import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070032import android.content.Intent;
33import android.content.pm.PackageManager;
34import android.content.pm.ResolveInfo;
35import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070036import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050037import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.database.Cursor;
39import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020040import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080042import android.graphics.BitmapFactory;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Canvas;
kaiyiza016da12013-08-26 17:50:22 +080044import android.net.ConnectivityManager;
45import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.net.Uri;
47import android.net.http.SslError;
kaiyiz6e5b3e02013-08-19 20:02:01 +080048import android.net.WebAddress;
kaiyiza016da12013-08-26 17:50:22 +080049import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080050import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.os.AsyncTask;
52import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070053import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.os.Handler;
55import android.os.Message;
56import android.os.PowerManager;
57import android.os.PowerManager.WakeLock;
kaiyiza8b6dbb2013-07-29 18:11:22 +080058import android.os.SystemProperties;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.provider.Browser;
61import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.provider.BrowserContract.Images;
63import android.provider.ContactsContract;
64import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080065import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070066import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070067import android.text.TextUtils;
68import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080069import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070070import android.view.ActionMode;
71import android.view.ContextMenu;
72import android.view.ContextMenu.ContextMenuInfo;
73import android.view.Gravity;
74import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070075import android.view.Menu;
76import android.view.MenuInflater;
77import android.view.MenuItem;
78import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070079import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070080import android.view.View;
81import android.webkit.CookieManager;
82import android.webkit.CookieSyncManager;
83import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070084import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070085import android.webkit.SslErrorHandler;
86import android.webkit.ValueCallback;
87import android.webkit.WebChromeClient;
88import android.webkit.WebIconDatabase;
89import android.webkit.WebSettings;
90import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000091import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050092import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070093
Michael Kolb4bd767d2011-05-27 11:33:55 -070094import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070095import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070096import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070097import com.android.browser.provider.SnapshotProvider.Snapshots;
kaiyiz6e5b3e02013-08-19 20:02:01 +080098import com.android.browser.mynavigation.AddMyNavigationPage;
99import com.android.browser.mynavigation.MyNavigationUtil;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700100
Michael Kolb8233fac2010-10-26 16:08:53 -0700101import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700102import java.io.File;
103import java.io.FileOutputStream;
104import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700105import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700106import java.text.DateFormat;
107import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700108import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700109import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700110import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700111import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800112import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200113import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700114import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115
116/**
117 * Controller for browser
118 */
119public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700120 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700121
122 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800123 private static final String SEND_APP_ID_EXTRA =
124 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700125 private static final String INCOGNITO_URI = "browser:incognito";
kaiyiza016da12013-08-26 17:50:22 +0800126 private static final String PROP_NETSWITCH = "persist.env.browser.netswitch";
127 private static final String INTENT_WIFI_SELECTION_DATA_CONNECTION =
128 "android.net.wifi.cmcc.WIFI_SELECTION_DATA_CONNECTION";
kaiyiz6e5b3e02013-08-19 20:02:01 +0800129 private static final String OFFLINE_PAGE =
130 "content://com.android.browser.mynavigation/websites";
kaiyizb7bf1f22013-10-02 11:42:23 +0800131 private static final String INTENT_PICK_NETWORK =
132 "android.net.wifi.cmcc.PICK_WIFI_NETWORK_AND_GPRS";
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 // public message ids
134 public final static int LOAD_URL = 1001;
135 public final static int STOP_LOAD = 1002;
136
137 // Message Ids
138 private static final int FOCUS_NODE_HREF = 102;
139 private static final int RELEASE_WAKELOCK = 107;
140
141 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
142
143 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800144 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700145
146 private static final int EMPTY_MENU = -1;
147
Michael Kolb8233fac2010-10-26 16:08:53 -0700148 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700149 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150 final static int PREFERENCES_PAGE = 3;
151 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000152 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700153 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800154 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
157
158 // As the ids are dynamically created, we can't guarantee that they will
159 // be in sequence, so this static array maps ids to a window number.
160 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
161 { R.id.window_one_menu_id, R.id.window_two_menu_id,
162 R.id.window_three_menu_id, R.id.window_four_menu_id,
163 R.id.window_five_menu_id, R.id.window_six_menu_id,
164 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
165
166 // "source" parameter for Google search through search key
167 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
168 // "source" parameter for Google search through simplily type
169 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
170
George Mount387d45d2011-10-07 15:57:53 -0700171 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700172 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
173
John Reckd7dd9b22011-08-30 09:18:29 -0700174 // A bitmap that is re-used in createScreenshot as scratch space
175 private static Bitmap sThumbnailBitmap;
176
Michael Kolb8233fac2010-10-26 16:08:53 -0700177 private Activity mActivity;
178 private UI mUi;
179 private TabControl mTabControl;
180 private BrowserSettings mSettings;
181 private WebViewFactory mFactory;
182
183 private WakeLock mWakeLock;
184
185 private UrlHandler mUrlHandler;
186 private UploadHandler mUploadHandler;
187 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700188 private PageDialogsHandler mPageDialogsHandler;
189 private NetworkStateHandler mNetworkHandler;
190
Ben Murdoch8029a772010-11-16 11:58:21 +0000191 private Message mAutoFillSetupMessage;
192
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800194 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700195
196 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
197
198 // FIXME, temp address onPrepareMenu performance problem.
199 // When we move everything out of view, we should rewrite this.
200 private int mCurrentMenuState = 0;
201 private int mMenuState = R.id.MAIN_MENU;
202 private int mOldMenuState = EMPTY_MENU;
203 private Menu mCachedMenu;
204
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 private boolean mMenuIsDown;
206
207 // For select and find, we keep track of the ActionMode so that
208 // finish() can be called as desired.
209 private ActionMode mActionMode;
210
211 /**
212 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
213 * of whether the configuration has changed. The first onMenuOpened call
214 * after a configuration change is simply a reopening of the same menu
215 * (i.e. mIconView did not change).
216 */
217 private boolean mConfigChanged;
218
219 /**
220 * Keeps track of whether the options menu is open. This is important in
221 * determining whether to show or hide the title bar overlay
222 */
223 private boolean mOptionsMenuOpen;
224
225 /**
226 * Whether or not the options menu is in its bigger, popup menu form. When
227 * true, we want the title bar overlay to be gone. When false, we do not.
228 * Only meaningful if mOptionsMenuOpen is true.
229 */
230 private boolean mExtendedMenuOpen;
231
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 private boolean mActivityPaused = true;
233 private boolean mLoadStopped;
234
235 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500236 // Checks to see when the bookmarks database has changed, and updates the
237 // Tabs' notion of whether they represent bookmarked sites.
238 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700239 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700240
Michael Kolbc3af0672011-08-09 10:24:41 -0700241 private boolean mBlockEvents;
242
Michael Kolb0b129122012-06-04 16:31:58 -0700243 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800244 private boolean mUpdateMyNavThumbnail;
245 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700246
George Mount3636d0a2011-11-21 09:08:21 -0800247 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 mActivity = browser;
249 mSettings = BrowserSettings.getInstance();
250 mTabControl = new TabControl(this);
251 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700252 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800253 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700254 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700255
256 mUrlHandler = new UrlHandler(this);
257 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
259
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500261 mBookmarksObserver = new ContentObserver(mHandler) {
262 @Override
263 public void onChange(boolean selfChange) {
264 int size = mTabControl.getTabCount();
265 for (int i = 0; i < size; i++) {
266 mTabControl.getTab(i).updateBookmarkedStatus();
267 }
268 }
269
270 };
271 browser.getContentResolver().registerContentObserver(
272 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700273
274 mNetworkHandler = new NetworkStateHandler(mActivity, this);
275 // Start watching the default geolocation permissions
276 mSystemAllowGeolocationOrigins =
277 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
278 mSystemAllowGeolocationOrigins.start();
279
John Reckaf262e72011-07-25 13:55:44 -0700280 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 }
282
John Reck9c35b9c2012-05-30 10:08:50 -0700283 @Override
284 public void start(final Intent intent) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800285 if (BrowserWebView.isClassic()) WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800286 // mCrashRecoverHandler has any previously saved state.
287 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700288 }
289
George Mount3636d0a2011-11-21 09:08:21 -0800290 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700291 // Unless the last browser usage was within 24 hours, destroy any
292 // remaining incognito tabs.
293
294 Calendar lastActiveDate = icicle != null ?
295 (Calendar) icicle.getSerializable("lastActiveDate") : null;
296 Calendar today = Calendar.getInstance();
297 Calendar yesterday = Calendar.getInstance();
298 yesterday.add(Calendar.DATE, -1);
299
Patrick Scott7d50a932011-02-04 09:27:26 -0500300 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700301 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800302 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700303
Patrick Scott7d50a932011-02-04 09:27:26 -0500304 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700305 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500306 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000307
Michael Kolbc831b632011-05-11 09:30:34 -0700308 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500309 // Not able to restore so we go ahead and clear session cookies. We
310 // must do this before trying to login the user as we don't want to
311 // clear any session cookies set during login.
312 CookieManager.getInstance().removeSessionCookie();
313 }
314
Patrick Scottd43e75a2011-03-14 14:47:23 -0400315 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500316 new Runnable() {
317 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800318 onPreloginFinished(icicle, intent, currentTabId,
319 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500320 }
321 });
322 }
323
Michael Kolbc831b632011-05-11 09:30:34 -0700324 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800325 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700326 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700327 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800328 if (intent == null) {
329 // This won't happen under common scenarios. The icicle is
330 // not null, but there aren't any tabs to restore.
331 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700332 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800333 final Bundle extra = intent.getExtras();
334 // Create an initial tab.
335 // If the intent is ACTION_VIEW and data is not null, the Browser is
336 // invoked to view the content by another application. In this case,
337 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800338 UrlData urlData = null;
339 if (intent.getData() != null
340 && Intent.ACTION_VIEW.equals(intent.getAction())
341 && intent.getData().toString().startsWith("content://")) {
342 urlData = new UrlData(intent.getData().toString());
343 } else {
344 urlData = IntentHandler.getUrlDataFromIntent(intent);
345 }
346
George Mount3636d0a2011-11-21 09:08:21 -0800347 Tab t = null;
348 if (urlData.isEmpty()) {
kaiyiz6e5b3e02013-08-19 20:02:01 +0800349 if (SystemProperties.get("persist.env.c.browser.resource", "default").equals(
350 "cmcc")) {
351 t = openTab(OFFLINE_PAGE, false, true, true);
352 } else {
353 t = openTabToHomePage();
354 }
George Mount3636d0a2011-11-21 09:08:21 -0800355 } else {
356 t = openTab(urlData);
357 }
358 if (t != null) {
359 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
360 }
361 WebView webView = t.getWebView();
362 if (extra != null) {
363 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
364 if (scale > 0 && scale <= 1000) {
365 webView.setInitialScale(scale);
366 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700367 }
368 }
John Reckd8c74522011-06-14 08:45:00 -0700369 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700370 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700371 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500372 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700373 List<Tab> tabs = mTabControl.getTabs();
374 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
375 for (Tab t : tabs) {
376 restoredTabs.add(t.getId());
377 }
378 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700379 if (tabs.size() == 0) {
380 openTabToHomePage();
381 }
John Reck1cf4b792011-07-26 10:22:22 -0700382 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700383 // TabControl.restoreState() will create a new tab even if
384 // restoring the state fails.
385 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800386 // Intent is non-null when framework thinks the browser should be
387 // launching with a new intent (icicle is null).
388 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700389 mIntentHandler.onNewIntent(intent);
390 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700391 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700392 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700393 String jsFlags = getSettings().getJsEngineFlags();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800394 if (jsFlags.trim().length() != 0 && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000395 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 }
George Mount3636d0a2011-11-21 09:08:21 -0800397 if (intent != null
398 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700399 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800400 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 }
402
John Reck1cf4b792011-07-26 10:22:22 -0700403 private static class PruneThumbnails implements Runnable {
404 private Context mContext;
405 private List<Long> mIds;
406
407 PruneThumbnails(Context context, List<Long> preserveIds) {
408 mContext = context.getApplicationContext();
409 mIds = preserveIds;
410 }
411
412 @Override
413 public void run() {
414 ContentResolver cr = mContext.getContentResolver();
415 if (mIds == null || mIds.size() == 0) {
416 cr.delete(Thumbnails.CONTENT_URI, null, null);
417 } else {
418 int length = mIds.size();
419 StringBuilder where = new StringBuilder();
420 where.append(Thumbnails._ID);
421 where.append(" not in (");
422 for (int i = 0; i < length; i++) {
423 where.append(mIds.get(i));
424 if (i < (length - 1)) {
425 where.append(",");
426 }
427 }
428 where.append(")");
429 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
430 }
431 }
432
433 }
434
Michael Kolb1514bb72010-11-22 09:11:48 -0800435 @Override
436 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700437 return mFactory;
438 }
439
440 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800441 public void onSetWebView(Tab tab, WebView view) {
442 mUi.onSetWebView(tab, view);
443 }
444
445 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800446 public void createSubWindow(Tab tab) {
447 endActionMode();
448 WebView mainView = tab.getWebView();
449 WebView subView = mFactory.createWebView((mainView == null)
450 ? false
451 : mainView.isPrivateBrowsingEnabled());
452 mUi.createSubWindow(tab, subView);
453 }
454
455 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700456 public Context getContext() {
457 return mActivity;
458 }
459
460 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700461 public Activity getActivity() {
462 return mActivity;
463 }
464
465 void setUi(UI ui) {
466 mUi = ui;
467 }
468
Michael Kolbaf63dba2012-05-16 12:58:05 -0700469 @Override
470 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700471 return mSettings;
472 }
473
474 IntentHandler getIntentHandler() {
475 return mIntentHandler;
476 }
477
478 @Override
479 public UI getUi() {
480 return mUi;
481 }
482
483 int getMaxTabs() {
484 return mActivity.getResources().getInteger(R.integer.max_tabs);
485 }
486
487 @Override
488 public TabControl getTabControl() {
489 return mTabControl;
490 }
491
Michael Kolb1bf23132010-11-19 12:55:12 -0800492 @Override
493 public List<Tab> getTabs() {
494 return mTabControl.getTabs();
495 }
496
John Reckaf262e72011-07-25 13:55:44 -0700497 // Open the icon database.
498 private void openIconDatabase() {
499 // We have to call getInstance on the UI thread
500 final WebIconDatabase instance = WebIconDatabase.getInstance();
501 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000502
John Reckaf262e72011-07-25 13:55:44 -0700503 @Override
504 public void run() {
505 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700506 }
John Reckaf262e72011-07-25 13:55:44 -0700507 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 }
509
510 private void startHandler() {
511 mHandler = new Handler() {
512
513 @Override
514 public void handleMessage(Message msg) {
515 switch (msg.what) {
516 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700517 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700518 break;
519 case FOCUS_NODE_HREF:
520 {
521 String url = (String) msg.getData().get("url");
522 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500523 String src = (String) msg.getData().get("src");
524 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 if (TextUtils.isEmpty(url)) {
526 break;
527 }
528 HashMap focusNodeMap = (HashMap) msg.obj;
529 WebView view = (WebView) focusNodeMap.get("webview");
530 // Only apply the action if the top window did not change.
531 if (getCurrentTopWebView() != view) {
532 break;
533 }
534 switch (msg.arg1) {
535 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700536 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700537 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500538 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700539 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500540 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500541 case R.id.open_newtab_context_menu_id:
542 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700543 openTab(url, parent,
544 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500545 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 case R.id.copy_link_context_menu_id:
547 copy(url);
548 break;
549 case R.id.save_link_context_menu_id:
550 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500551 DownloadHandler.onDownloadStartNoStream(
Andreas Sandblad8e4ce662012-06-11 11:02:49 +0200552 mActivity, url, view.getSettings().getUserAgentString(),
luxiaol62677b02013-07-22 07:54:49 +0800553 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 break;
555 }
556 break;
557 }
558
559 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700560 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 break;
562
563 case STOP_LOAD:
564 stopLoading();
565 break;
566
567 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700568 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700569 mWakeLock.release();
570 // if we reach here, Browser should be still in the
571 // background loading after WAKELOCK_TIMEOUT (5-min).
572 // To avoid burning the battery, stop loading.
573 mTabControl.stopAllLoading();
574 }
575 break;
576
577 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800578 Tab tab = (Tab) msg.obj;
579 if (tab != null) {
580 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 }
582 break;
kaiyiz591110b2013-08-06 17:11:06 +0800583
584 case OPEN_MENU:
585 if (!mOptionsMenuOpen && mActivity != null ) {
586 mActivity.openOptionsMenu();
587 }
588 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700589 }
590 }
591 };
592
593 }
594
John Reckef654f12011-07-12 16:42:08 -0700595 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200596 public Tab getCurrentTab() {
597 return mTabControl.getCurrentTab();
598 }
599
Michael Kolbba99c5d2010-11-29 14:57:41 -0800600 @Override
601 public void shareCurrentPage() {
602 shareCurrentPage(mTabControl.getCurrentTab());
603 }
604
605 private void shareCurrentPage(Tab tab) {
606 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800607 sharePage(mActivity, tab.getTitle(),
608 tab.getUrl(), tab.getFavicon(),
609 createScreenshot(tab.getWebView(),
610 getDesiredThumbnailWidth(mActivity),
611 getDesiredThumbnailHeight(mActivity)));
612 }
613 }
614
Michael Kolb8233fac2010-10-26 16:08:53 -0700615 /**
616 * Share a page, providing the title, url, favicon, and a screenshot. Uses
617 * an {@link Intent} to launch the Activity chooser.
618 * @param c Context used to launch a new Activity.
619 * @param title Title of the page. Stored in the Intent with
620 * {@link Intent#EXTRA_SUBJECT}
621 * @param url URL of the page. Stored in the Intent with
622 * {@link Intent#EXTRA_TEXT}
623 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
624 * with {@link Browser#EXTRA_SHARE_FAVICON}
625 * @param screenshot Bitmap of a screenshot of the page. Stored in the
626 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
627 */
628 static final void sharePage(Context c, String title, String url,
629 Bitmap favicon, Bitmap screenshot) {
630 Intent send = new Intent(Intent.ACTION_SEND);
631 send.setType("text/plain");
632 send.putExtra(Intent.EXTRA_TEXT, url);
633 send.putExtra(Intent.EXTRA_SUBJECT, title);
634 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
635 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
636 try {
637 c.startActivity(Intent.createChooser(send, c.getString(
638 R.string.choosertitle_sharevia)));
639 } catch(android.content.ActivityNotFoundException ex) {
640 // if no app handles it, do nothing
641 }
642 }
643
644 private void copy(CharSequence text) {
645 ClipboardManager cm = (ClipboardManager) mActivity
646 .getSystemService(Context.CLIPBOARD_SERVICE);
647 cm.setText(text);
648 }
649
650 // lifecycle
651
John Reck9c35b9c2012-05-30 10:08:50 -0700652 @Override
653 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700654 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800655 // update the menu in case of a locale change
656 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800657 if (mOptionsMenuOpen) {
658 mActivity.closeOptionsMenu();
659 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
660 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 if (mPageDialogsHandler != null) {
662 mPageDialogsHandler.onConfigurationChanged(config);
663 }
664 mUi.onConfigurationChanged(config);
665 }
666
667 @Override
668 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700669 if (!mUi.isWebShowing()) {
670 mUi.showWeb(false);
671 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700672 mIntentHandler.onNewIntent(intent);
673 }
674
John Reck9c35b9c2012-05-30 10:08:50 -0700675 @Override
676 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800677 if (mUi.isCustomViewShowing()) {
678 hideCustomView();
679 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700680 if (mActivityPaused) {
681 Log.e(LOGTAG, "BrowserActivity is already paused.");
682 return;
683 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800685 Tab tab = mTabControl.getCurrentTab();
686 if (tab != null) {
687 tab.pause();
688 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700689 if (mWakeLock == null) {
690 PowerManager pm = (PowerManager) mActivity
691 .getSystemService(Context.POWER_SERVICE);
692 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
693 }
Michael Kolb70976932010-11-30 11:34:01 -0800694 mWakeLock.acquire();
695 mHandler.sendMessageDelayed(mHandler
696 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
697 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 }
699 mUi.onPause();
700 mNetworkHandler.onPause();
701
702 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100703 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700704 if (sThumbnailBitmap != null) {
705 sThumbnailBitmap.recycle();
706 sThumbnailBitmap = null;
707 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700708 }
709
John Reck9c35b9c2012-05-30 10:08:50 -0700710 @Override
711 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800713 Bundle saveState = createSaveState();
714
715 // crash recovery manages all save & restore state
716 mCrashRecoveryHandler.writeState(saveState);
717 mSettings.setLastRunPaused(true);
718 }
719
720 /**
721 * Save the current state to outState. Does not write the state to
722 * disk.
723 * @return Bundle containing the current state of all tabs.
724 */
725 /* package */ Bundle createSaveState() {
726 Bundle saveState = new Bundle();
727 mTabControl.saveState(saveState);
728 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700729 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800730 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700731 }
George Mount3636d0a2011-11-21 09:08:21 -0800732 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 }
734
John Reck9c35b9c2012-05-30 10:08:50 -0700735 @Override
736 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 if (!mActivityPaused) {
738 Log.e(LOGTAG, "BrowserActivity is already resumed.");
739 return;
740 }
George Mount3636d0a2011-11-21 09:08:21 -0800741 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800743 Tab current = mTabControl.getCurrentTab();
744 if (current != null) {
745 current.resume();
746 resumeWebViewTimers(current);
747 }
John Reckf57c0292011-07-21 18:15:39 -0700748 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200749
Michael Kolb8233fac2010-10-26 16:08:53 -0700750 mUi.onResume();
751 mNetworkHandler.onResume();
752 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100753 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700754 if (mVoiceResult != null) {
755 mUi.onVoiceResult(mVoiceResult);
756 mVoiceResult = null;
757 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 }
759
John Reckf57c0292011-07-21 18:15:39 -0700760 private void releaseWakeLock() {
761 if (mWakeLock != null && mWakeLock.isHeld()) {
762 mHandler.removeMessages(RELEASE_WAKELOCK);
763 mWakeLock.release();
764 }
765 }
766
Michael Kolb70976932010-11-30 11:34:01 -0800767 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800768 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800769 * @param tab guaranteed non-null
770 */
771 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700772 boolean inLoad = tab.inPageLoad();
773 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
774 CookieSyncManager.getInstance().startSync();
775 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100776 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700777 }
778 }
779
Michael Kolb70976932010-11-30 11:34:01 -0800780 /**
781 * Pause all WebView timers using the WebView of the given tab
782 * @param tab
783 * @return true if the timers are paused or tab is null
784 */
785 private boolean pauseWebViewTimers(Tab tab) {
786 if (tab == null) {
787 return true;
788 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700789 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100790 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700791 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 }
Michael Kolb70976932010-11-30 11:34:01 -0800793 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 }
795
John Reck9c35b9c2012-05-30 10:08:50 -0700796 @Override
797 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800798 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
800 mUploadHandler = null;
801 }
802 if (mTabControl == null) return;
803 mUi.onDestroy();
804 // Remove the current tab and sub window
805 Tab t = mTabControl.getCurrentTab();
806 if (t != null) {
807 dismissSubWindow(t);
808 removeTab(t);
809 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500810 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 // Destroy all the tabs
812 mTabControl.destroy();
813 WebIconDatabase.getInstance().close();
814 // Stop watching the default geolocation permissions
815 mSystemAllowGeolocationOrigins.stop();
816 mSystemAllowGeolocationOrigins = null;
817 }
818
819 protected boolean isActivityPaused() {
820 return mActivityPaused;
821 }
822
John Reck9c35b9c2012-05-30 10:08:50 -0700823 @Override
824 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 mTabControl.freeMemory();
826 }
827
828 @Override
829 public boolean shouldShowErrorConsole() {
830 return mShouldShowErrorConsole;
831 }
832
833 protected void setShouldShowErrorConsole(boolean show) {
834 if (show == mShouldShowErrorConsole) {
835 // Nothing to do.
836 return;
837 }
838 mShouldShowErrorConsole = show;
839 Tab t = mTabControl.getCurrentTab();
840 if (t == null) {
841 // There is no current tab so we cannot toggle the error console
842 return;
843 }
844 mUi.setShouldShowErrorConsole(t, show);
845 }
846
847 @Override
848 public void stopLoading() {
849 mLoadStopped = true;
850 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700851 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700852 if (w != null) {
853 w.stopLoading();
854 mUi.onPageStopped(tab);
855 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 }
857
858 boolean didUserStopLoading() {
859 return mLoadStopped;
860 }
861
kaiyiza016da12013-08-26 17:50:22 +0800862 private void handleNetworkNotify(WebView view) {
863 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
864 Context.CONNECTIVITY_SERVICE);
865 WifiManager wifiMgr = (WifiManager) this.getContext()
866 .getSystemService(Context.WIFI_SERVICE);
867 int networkSwitchTypeOK = this.getContext().getResources()
868 .getInteger(R.integer.netswitch_type_remind);
869
870 if (wifiMgr.isWifiEnabled()) {
871 NetworkInfo mNetworkInfo = conMgr.getActiveNetworkInfo();
872 if (mNetworkInfo == null
873 || (mNetworkInfo != null && (mNetworkInfo.getType() !=
874 ConnectivityManager.TYPE_WIFI))) {
kaiyizb7bf1f22013-10-02 11:42:23 +0800875 List<ScanResult> list = wifiMgr.getScanResults();
876 if (list != null && list.size() == 0) {
877 int isReminder = Settings.System.getInt(
878 mActivity.getContentResolver(),
879 this.getContext().getResources()
880 .getString(R.string.network_switch_remind_type),
881 networkSwitchTypeOK);
882 if (isReminder == networkSwitchTypeOK) {
883 Intent intent = new Intent(
884 INTENT_WIFI_SELECTION_DATA_CONNECTION);
885 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
886 this.getContext().startActivity(intent);
887 }
888 } else {
889 if (ActivityManagerNative.isSystemReady()) {
890 try {
891 Intent intent = new Intent(INTENT_PICK_NETWORK);
892 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
893 this.getContext().startActivity(intent);
894 } catch (Exception e) {
895 String err_msg = this.getContext().getString(
896 R.string.acivity_not_found, INTENT_PICK_NETWORK);
897 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
898 }
899 } else {
900 Log.e(LOGTAG, "System is not ready!");
901 }
kaiyiza016da12013-08-26 17:50:22 +0800902 }
903 mNetworkShouldNotify = false;
904 }
905 } else {
906 if (!mNetworkHandler.isNetworkUp()) {
907 view.setNetworkAvailable(false);
908 Log.v(LOGTAG, "handleNetworkNotify() Wlan is not enabled.");
909 }
910 }
911 }
912
Michael Kolb8233fac2010-10-26 16:08:53 -0700913 // WebViewController
914
915 @Override
John Reck324d4402011-01-11 16:56:42 -0800916 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700917
918 // We've started to load a new page. If there was a pending message
919 // to save a screenshot then we will now take the new page and save
920 // an incorrect screenshot. Therefore, remove any pending thumbnail
921 // messages from the queue.
922 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800923 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700924
925 // reset sync timer to avoid sync starts during loading a page
926 CookieSyncManager.getInstance().resetSync();
927
kaiyiza016da12013-08-26 17:50:22 +0800928 if (SystemProperties.getBoolean(PROP_NETSWITCH, false)) {
929 if (!mNetworkHandler.isNetworkUp()) {
930 Log.d(LOGTAG, "onPageStarted() network unavailable");
931 if (mNetworkShouldNotify) {
932 handleNetworkNotify(view);
933 } else {
934 view.setNetworkAvailable(false);
935 }
936 mNetworkShouldNotify = false;
937 } else {
938 Log.d(LOGTAG, "onPageStarted() network available");
939 if (mNetworkShouldNotify) {
940 handleNetworkNotify(view);
941 }
942 mNetworkShouldNotify = false;
943 }
944 } else {
945 if (!mNetworkHandler.isNetworkUp()) {
946 view.setNetworkAvailable(false);
947 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 }
949
950 // when BrowserActivity just starts, onPageStarted may be called before
951 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
952 // to start the timer. As we won't switch tabs while an activity is in
953 // pause state, we can ensure calling resume and pause in pair.
954 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800955 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700956 }
957 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 endActionMode();
959
John Reck30c714c2010-12-16 17:30:34 -0800960 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700961
John Reck324d4402011-01-11 16:56:42 -0800962 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700963 // update the bookmark database for favicon
964 maybeUpdateFavicon(tab, null, url, favicon);
965
966 Performance.tracePageStart(url);
967
968 // Performance probe
969 if (false) {
970 Performance.onPageStarted();
971 }
972
973 }
974
975 @Override
John Reck324d4402011-01-11 16:56:42 -0800976 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700977 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800978 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200979
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 // Performance probe
981 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800982 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 }
984
985 Performance.tracePageFinished();
986 }
987
988 @Override
John Reck30c714c2010-12-16 17:30:34 -0800989 public void onProgressChanged(Tab tab) {
990 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700991
992 if (newProgress == 100) {
993 CookieSyncManager.getInstance().sync();
994 // onProgressChanged() may continue to be called after the main
995 // frame has finished loading, as any remaining sub frames continue
996 // to load. We'll only get called once though with newProgress as
997 // 100 when everything is loaded. (onPageFinished is called once
998 // when the main frame completes loading regardless of the state of
999 // any sub frames so calls to onProgressChanges may continue after
1000 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001001 if (tab.inPageLoad()) {
1002 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +02001003 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
1004 // pause the WebView timer and release the wake lock if it is
1005 // finished while BrowserActivity is in pause state.
1006 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001007 }
John Reckd9862372012-02-21 15:04:50 -08001008 if (!tab.isPrivateBrowsingEnabled()
1009 && !TextUtils.isEmpty(tab.getUrl())
1010 && !tab.isSnapshot()) {
1011 // Only update the bookmark screenshot if the user did not
1012 // cancel the load early and there is not already
1013 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001014 if (tab.shouldUpdateThumbnail() &&
1015 (tab.inForeground() && !didUserStopLoading()
1016 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001017 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1018 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1019 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
1020 500);
1021 }
1022 }
1023 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001025 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001027 // still loading
1028 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001029 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001030 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 }
1032 }
John Reck30c714c2010-12-16 17:30:34 -08001033 mUi.onProgressChanged(tab);
1034 }
1035
1036 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001037 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001038 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 }
1040
1041 @Override
1042 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001043 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001044 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001045 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1047 return;
1048 }
1049 // Update the title in the history database if not in private browsing mode
1050 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001051 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 }
1053 }
1054
1055 @Override
1056 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001057 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001058 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1059 }
1060
1061 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001062 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1063 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 }
1065
1066 @Override
1067 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1068 if (mMenuIsDown) {
1069 // only check shortcut key when MENU is held
1070 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1071 event);
1072 } else {
1073 return false;
1074 }
1075 }
1076
1077 @Override
John Reck997b1b72012-04-19 18:08:25 -07001078 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 if (!isActivityPaused()) {
1080 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001081 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 } else {
John Reck997b1b72012-04-19 18:08:25 -07001083 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 }
1085 }
John Reck997b1b72012-04-19 18:08:25 -07001086 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001087 }
1088
1089 @Override
John Reck324d4402011-01-11 16:56:42 -08001090 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 // Don't save anything in private browsing mode
1092 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -08001093 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001094
John Reck324d4402011-01-11 16:56:42 -08001095 if (TextUtils.isEmpty(url)
1096 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 return;
1098 }
John Reckf57c0292011-07-21 18:15:39 -07001099 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001100 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001101 }
1102
1103 @Override
1104 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1105 AsyncTask<Void, Void, String[]> task =
1106 new AsyncTask<Void, Void, String[]>() {
1107 @Override
1108 public String[] doInBackground(Void... unused) {
1109 return Browser.getVisitedHistory(mActivity.getContentResolver());
1110 }
1111 @Override
1112 public void onPostExecute(String[] result) {
1113 callback.onReceiveValue(result);
1114 }
1115 };
1116 task.execute();
1117 }
1118
1119 @Override
1120 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1121 final HttpAuthHandler handler, final String host,
1122 final String realm) {
1123 String username = null;
1124 String password = null;
1125
1126 boolean reuseHttpAuthUsernamePassword
1127 = handler.useHttpAuthUsernamePassword();
1128
1129 if (reuseHttpAuthUsernamePassword && view != null) {
1130 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1131 if (credentials != null && credentials.length == 2) {
1132 username = credentials[0];
1133 password = credentials[1];
1134 }
1135 }
1136
1137 if (username != null && password != null) {
1138 handler.proceed(username, password);
1139 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001140 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1142 } else {
1143 handler.cancel();
1144 }
1145 }
1146 }
1147
1148 @Override
1149 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001150 String contentDisposition, String mimetype, String referer,
1151 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001152 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001153 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001154 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001155 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 // This Tab was opened for the sole purpose of downloading a
1157 // file. Remove it.
1158 if (tab == mTabControl.getCurrentTab()) {
1159 // In this case, the Tab is still on top.
1160 goBackOnePageOrQuit();
1161 } else {
1162 // In this case, it is not.
1163 closeTab(tab);
1164 }
1165 }
1166 }
1167
1168 @Override
1169 public Bitmap getDefaultVideoPoster() {
1170 return mUi.getDefaultVideoPoster();
1171 }
1172
1173 @Override
1174 public View getVideoLoadingProgressView() {
1175 return mUi.getVideoLoadingProgressView();
1176 }
1177
1178 @Override
1179 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1180 SslError error) {
1181 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1182 }
1183
Patrick Scott92066772011-03-10 08:46:27 -05001184 @Override
1185 public void showAutoLogin(Tab tab) {
1186 assert tab.inForeground();
1187 // Update the title bar to show the auto-login request.
1188 mUi.showAutoLogin(tab);
1189 }
1190
1191 @Override
1192 public void hideAutoLogin(Tab tab) {
1193 assert tab.inForeground();
1194 mUi.hideAutoLogin(tab);
1195 }
1196
Michael Kolb8233fac2010-10-26 16:08:53 -07001197 // helper method
1198
1199 /*
1200 * Update the favorites icon if the private browsing isn't enabled and the
1201 * icon is valid.
1202 */
1203 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1204 final String url, Bitmap favicon) {
1205 if (favicon == null) {
1206 return;
1207 }
1208 if (!tab.isPrivateBrowsingEnabled()) {
1209 Bookmarks.updateFavicon(mActivity
1210 .getContentResolver(), originalUrl, url, favicon);
1211 }
1212 }
1213
Leon Scroggins4cd97792010-12-03 15:31:56 -05001214 @Override
1215 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001216 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001217 mUi.bookmarkedStatusHasChanged(tab);
1218 }
1219
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 // end WebViewController
1221
1222 protected void pageUp() {
1223 getCurrentTopWebView().pageUp(false);
1224 }
1225
1226 protected void pageDown() {
1227 getCurrentTopWebView().pageDown(false);
1228 }
1229
1230 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001231 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 public void editUrl() {
1233 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001234 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 }
1236
John Reck9c35b9c2012-05-30 10:08:50 -07001237 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001238 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 WebChromeClient.CustomViewCallback callback) {
1240 if (tab.inForeground()) {
1241 if (mUi.isCustomViewShowing()) {
1242 callback.onCustomViewHidden();
1243 return;
1244 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001245 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 // Save the menu state and set it to empty while the custom
1247 // view is showing.
1248 mOldMenuState = mMenuState;
1249 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001250 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 }
1252 }
1253
1254 @Override
1255 public void hideCustomView() {
1256 if (mUi.isCustomViewShowing()) {
1257 mUi.onHideCustomView();
1258 // Reset the old menu state.
1259 mMenuState = mOldMenuState;
1260 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001261 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 }
1263 }
1264
John Reck9c35b9c2012-05-30 10:08:50 -07001265 @Override
1266 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001267 Intent intent) {
1268 if (getCurrentTopWebView() == null) return;
1269 switch (requestCode) {
1270 case PREFERENCES_PAGE:
1271 if (resultCode == Activity.RESULT_OK && intent != null) {
1272 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001273 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 mTabControl.removeParentChildRelationShips();
1275 }
1276 }
1277 break;
1278 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001279 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001280 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001283 case AUTOFILL_SETUP:
1284 // Determine whether a profile was actually set up or not
1285 // and if so, send the message back to the WebTextView to
1286 // fill the form with the new profile.
1287 if (getSettings().getAutoFillProfile() != null) {
1288 mAutoFillSetupMessage.sendToTarget();
1289 mAutoFillSetupMessage = null;
1290 }
1291 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001292 case COMBO_VIEW:
1293 if (intent == null || resultCode != Activity.RESULT_OK) {
1294 break;
1295 }
John Reck3ba45532011-08-11 16:26:53 -07001296 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001297 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1298 Tab t = getCurrentTab();
1299 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001300 mUpdateMyNavThumbnail = true;
1301 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001302 loadUrl(t, uri.toString());
1303 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1304 String[] urls = intent.getStringArrayExtra(
1305 ComboViewActivity.EXTRA_OPEN_ALL);
1306 Tab parent = getCurrentTab();
1307 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001308 if (url != null) {
1309 parent = openTab(url, parent,
1310 !mSettings.openInBackground(), true);
1311 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001312 }
1313 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1314 long id = intent.getLongExtra(
1315 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1316 if (id >= 0) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08001317 if (BrowserWebView.isClassic()) {
1318 createNewSnapshotTab(id, true);
1319 } else {
1320 Toast.makeText(mActivity, "Snapshot Tab requires WebViewClassic",
1321 Toast.LENGTH_LONG).show();
1322 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001323 }
1324 }
1325 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001326 case VOICE_RESULT:
1327 if (resultCode == Activity.RESULT_OK && intent != null) {
1328 ArrayList<String> results = intent.getStringArrayListExtra(
1329 RecognizerIntent.EXTRA_RESULTS);
1330 if (results.size() >= 1) {
1331 mVoiceResult = results.get(0);
1332 }
1333 }
1334 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001335
1336 case MY_NAVIGATION:
1337 if (intent == null || resultCode != Activity.RESULT_OK) {
1338 break;
1339 }
1340
1341 if (intent.getBooleanExtra("need_refresh", false) &&
1342 getCurrentTopWebView() != null) {
1343 getCurrentTopWebView().reload();
1344 }
1345 break;
1346
Michael Kolb8233fac2010-10-26 16:08:53 -07001347 default:
1348 break;
1349 }
1350 getCurrentTopWebView().requestFocus();
1351 }
1352
1353 /**
1354 * Open the Go page.
1355 * @param startWithHistory If true, open starting on the history tab.
1356 * Otherwise, start with the bookmarks tab.
1357 */
1358 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001359 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 if (mTabControl.getCurrentWebView() == null) {
1361 return;
1362 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001363 // clear action mode
1364 if (isInCustomActionMode()) {
1365 endActionMode();
1366 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001367 Bundle extras = new Bundle();
1368 // Disable opening in a new window if we have maxed out the windows
1369 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1370 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001371 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001372 }
1373
1374 // combo view callbacks
1375
Michael Kolb8233fac2010-10-26 16:08:53 -07001376 // key handling
1377 protected void onBackKey() {
1378 if (!mUi.onBackKey()) {
1379 WebView subwindow = mTabControl.getCurrentSubWindow();
1380 if (subwindow != null) {
1381 if (subwindow.canGoBack()) {
1382 subwindow.goBack();
1383 } else {
1384 dismissSubWindow(mTabControl.getCurrentTab());
1385 }
1386 } else {
1387 goBackOnePageOrQuit();
1388 }
1389 }
1390 }
1391
Michael Kolb4bd767d2011-05-27 11:33:55 -07001392 protected boolean onMenuKey() {
1393 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001394 }
1395
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 // menu handling and state
1397 // TODO: maybe put into separate handler
1398
John Reck9c35b9c2012-05-30 10:08:50 -07001399 @Override
1400 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001401 if (mMenuState == EMPTY_MENU) {
1402 return false;
1403 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001404 MenuInflater inflater = mActivity.getMenuInflater();
1405 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 return true;
1407 }
1408
John Reck9c35b9c2012-05-30 10:08:50 -07001409 @Override
1410 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001411 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001412 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001413 return;
1414 }
1415 if (!(v instanceof WebView)) {
1416 return;
1417 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001418 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 WebView.HitTestResult result = webview.getHitTestResult();
1420 if (result == null) {
1421 return;
1422 }
1423
1424 int type = result.getType();
1425 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1426 Log.w(LOGTAG,
1427 "We should not show context menu when nothing is touched");
1428 return;
1429 }
1430 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1431 // let TextView handles context menu
1432 return;
1433 }
1434
1435 // Note, http://b/issue?id=1106666 is requesting that
1436 // an inflated menu can be used again. This is not available
1437 // yet, so inflate each time (yuk!)
1438 MenuInflater inflater = mActivity.getMenuInflater();
1439 inflater.inflate(R.menu.browsercontext, menu);
1440
1441 // Show the correct menu group
1442 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001443 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001444 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001445 menu.setGroupVisible(R.id.PHONE_MENU,
1446 type == WebView.HitTestResult.PHONE_TYPE);
1447 menu.setGroupVisible(R.id.EMAIL_MENU,
1448 type == WebView.HitTestResult.EMAIL_TYPE);
1449 menu.setGroupVisible(R.id.GEO_MENU,
1450 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001451
1452 String itemUrl = null;
1453 String url = webview.getOriginalUrl();
1454 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1455 itemUrl = Uri.decode(navigationUrl);
1456 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1457 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1458 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1459 } else {
1460 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1461 }
1462 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1463 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1464 } else {
1465 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1466
1467 menu.setGroupVisible(R.id.IMAGE_MENU,
1468 type == WebView.HitTestResult.IMAGE_TYPE
1469 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1470 menu.setGroupVisible(R.id.ANCHOR_MENU,
1471 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1472 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1473 }
1474
Michael Kolb8233fac2010-10-26 16:08:53 -07001475 // Setup custom handling depending on the type
1476 switch (type) {
1477 case WebView.HitTestResult.PHONE_TYPE:
1478 menu.setHeaderTitle(Uri.decode(extra));
1479 menu.findItem(R.id.dial_context_menu_id).setIntent(
1480 new Intent(Intent.ACTION_VIEW, Uri
1481 .parse(WebView.SCHEME_TEL + extra)));
1482 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1483 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1484 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1485 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1486 addIntent);
1487 menu.findItem(R.id.copy_phone_context_menu_id)
1488 .setOnMenuItemClickListener(
1489 new Copy(extra));
1490 break;
1491
1492 case WebView.HitTestResult.EMAIL_TYPE:
1493 menu.setHeaderTitle(extra);
1494 menu.findItem(R.id.email_context_menu_id).setIntent(
1495 new Intent(Intent.ACTION_VIEW, Uri
1496 .parse(WebView.SCHEME_MAILTO + extra)));
1497 menu.findItem(R.id.copy_mail_context_menu_id)
1498 .setOnMenuItemClickListener(
1499 new Copy(extra));
1500 break;
1501
1502 case WebView.HitTestResult.GEO_TYPE:
1503 menu.setHeaderTitle(extra);
1504 menu.findItem(R.id.map_context_menu_id).setIntent(
1505 new Intent(Intent.ACTION_VIEW, Uri
1506 .parse(WebView.SCHEME_GEO
1507 + URLEncoder.encode(extra))));
1508 menu.findItem(R.id.copy_geo_context_menu_id)
1509 .setOnMenuItemClickListener(
1510 new Copy(extra));
1511 break;
1512
1513 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1514 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001515 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001516 // decide whether to show the open link in new tab option
1517 boolean showNewTab = mTabControl.canCreateNewTab();
1518 MenuItem newTabItem
1519 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001520 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001521 ? R.string.contextmenu_openlink_newwindow_background
1522 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001523 newTabItem.setVisible(showNewTab);
1524 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001525 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1526 newTabItem.setOnMenuItemClickListener(
1527 new MenuItem.OnMenuItemClickListener() {
1528 @Override
1529 public boolean onMenuItemClick(MenuItem item) {
1530 final HashMap<String, WebView> hrefMap =
1531 new HashMap<String, WebView>();
1532 hrefMap.put("webview", webview);
1533 final Message msg = mHandler.obtainMessage(
1534 FOCUS_NODE_HREF,
1535 R.id.open_newtab_context_menu_id,
1536 0, hrefMap);
1537 webview.requestFocusNodeHref(msg);
1538 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001539 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001540 });
1541 } else {
1542 newTabItem.setOnMenuItemClickListener(
1543 new MenuItem.OnMenuItemClickListener() {
1544 @Override
1545 public boolean onMenuItemClick(MenuItem item) {
1546 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001547 openTab(extra, parent,
1548 !mSettings.openInBackground(),
1549 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001550 return true;
1551 }
1552 });
1553 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001554 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001555 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1556 menu.setHeaderTitle(navigationUrl);
1557 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1558
1559 if (itemUrl != null) {
1560 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1561 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1562 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1563 @Override
1564 public boolean onMenuItemClick(MenuItem item) {
1565 final Intent intent = new Intent(Controller.this
1566 .getContext(),
1567 AddMyNavigationPage.class);
1568 Bundle bundle = new Bundle();
1569 String url = Uri.decode(navigationUrl);
1570 bundle.putBoolean("isAdding", false);
1571 bundle.putString("url", url);
1572 bundle.putString("name", getNameFromUrl(url));
1573 intent.putExtra("websites", bundle);
1574 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1575 return false;
1576 }
1577 });
1578 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1579 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1580 @Override
1581 public boolean onMenuItemClick(MenuItem item) {
1582 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1583 return false;
1584 }
1585 });
1586 }
1587 } else {
1588 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1589 }
1590 }
1591
Michael Kolb8233fac2010-10-26 16:08:53 -07001592 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1593 break;
1594 }
1595 // otherwise fall through to handle image part
1596 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001597 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1598 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001599 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1600 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001601 shareItem.setOnMenuItemClickListener(
1602 new MenuItem.OnMenuItemClickListener() {
1603 @Override
1604 public boolean onMenuItemClick(MenuItem item) {
1605 sharePage(mActivity, null, extra, null,
1606 null);
1607 return true;
1608 }
1609 }
1610 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001611 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001612 menu.findItem(R.id.view_image_context_menu_id)
1613 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1614 @Override
1615 public boolean onMenuItemClick(MenuItem item) {
1616 openTab(extra, mTabControl.getCurrentTab(), true, true);
1617 return false;
1618 }
1619 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001620 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1621 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1622 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001623 menu.findItem(R.id.set_wallpaper_context_menu_id).
1624 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1625 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001626 break;
1627
1628 default:
1629 Log.w(LOGTAG, "We should not get here.");
1630 break;
1631 }
1632 //update the ui
1633 mUi.onContextMenuCreated(menu);
1634 }
1635
kaiyiz6e5b3e02013-08-19 20:02:01 +08001636 public void startAddMyNavigation(String url) {
1637 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1638 Bundle bundle = new Bundle();
1639 bundle.putBoolean("isAdding", true);
1640 bundle.putString("url", url);
1641 bundle.putString("name", getNameFromUrl(url));
1642 intent.putExtra("websites", bundle);
1643 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1644 }
1645
1646 private void showMyNavigationDeleteDialog(final String itemUrl) {
1647 new AlertDialog.Builder(this.getContext())
1648 .setTitle(R.string.my_navigation_delete_label)
1649 .setIcon(android.R.drawable.ic_dialog_alert)
1650 .setMessage(R.string.my_navigation_delete_msg)
1651 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1652 @Override
1653 public void onClick(DialogInterface dialog, int whichButton) {
1654 deleteMyNavigationItem(itemUrl);
1655 }
1656 })
1657 .setNegativeButton(R.string.cancel, null)
1658 .show();
1659 }
1660
1661 private void deleteMyNavigationItem(final String itemUrl) {
1662 ContentResolver cr = this.getContext().getContentResolver();
1663 Cursor cursor = null;
1664
1665 try {
1666 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1667 new String[] {
1668 MyNavigationUtil.ID
1669 }, "url = ?", new String[] {
1670 itemUrl
1671 }, null);
1672 if (null != cursor && cursor.moveToFirst()) {
1673 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1674 cursor.getLong(0));
1675
1676 ContentValues values = new ContentValues();
1677 values.put(MyNavigationUtil.TITLE, "");
1678 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1679 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1680 ByteArrayOutputStream os = new ByteArrayOutputStream();
1681 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1682 R.raw.my_navigation_add);
1683 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1684 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1685 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1686 cr.update(uri, values, null, null);
1687 } else {
1688 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1689 }
1690 } catch (IllegalStateException e) {
1691 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1692 } finally {
1693 if (null != cursor) {
1694 cursor.close();
1695 }
1696 }
1697
1698 if (getCurrentTopWebView() != null) {
1699 getCurrentTopWebView().reload();
1700 }
1701 }
1702
1703 private String getNameFromUrl(String itemUrl) {
1704 ContentResolver cr = this.getContext().getContentResolver();
1705 Cursor cursor = null;
1706 String name = null;
1707
1708 try {
1709 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1710 new String[] {
1711 MyNavigationUtil.TITLE
1712 }, "url = ?", new String[] {
1713 itemUrl
1714 }, null);
1715 if (null != cursor && cursor.moveToFirst()) {
1716 name = cursor.getString(0);
1717 } else {
1718 Log.e(LOGTAG, "this item does not exist!");
1719 }
1720 } catch (IllegalStateException e) {
1721 Log.e(LOGTAG, "getNameFromUrl", e);
1722 } finally {
1723 if (null != cursor) {
1724 cursor.close();
1725 }
1726 }
1727 return name;
1728 }
1729
1730 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1731 int width = mActivity.getResources().getDimensionPixelOffset(
1732 R.dimen.myNavigationThumbnailWidth);
1733 int height = mActivity.getResources().getDimensionPixelOffset(
1734 R.dimen.myNavigationThumbnailHeight);
1735
1736 final Bitmap bm = createScreenshot(webView, width, height);
1737
1738 if (bm == null) {
1739 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1740 return;
1741 }
1742
1743 final ContentResolver cr = mActivity.getContentResolver();
1744 new AsyncTask<Void, Void, Void>() {
1745 @Override
1746 protected Void doInBackground(Void... unused) {
1747 ContentResolver cr = mActivity.getContentResolver();
1748 Cursor cursor = null;
1749 try {
1750 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1751 new String[] {
1752 MyNavigationUtil.ID
1753 }, "url = ?", new String[] {
1754 itemUrl
1755 }, null);
1756 if (null != cursor && cursor.moveToFirst()) {
1757 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1758 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1759
1760 ContentValues values = new ContentValues();
1761 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1762 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1763 cursor.getLong(0));
1764 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1765 cr.update(uri, values, null, null);
1766 os.close();
1767 }
1768 } catch (IllegalStateException e) {
1769 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1770 } catch (IOException e) {
1771 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1772 } finally {
1773 if (null != cursor) {
1774 cursor.close();
1775 }
1776 }
1777 return null;
1778 }
1779 }.execute();
1780 }
1781
Michael Kolb8233fac2010-10-26 16:08:53 -07001782 /**
1783 * As the menu can be open when loading state changes
1784 * we must manually update the state of the stop/reload menu
1785 * item
1786 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001787 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001788 if (menu == null) {
1789 return;
1790 }
1791 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001792 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001793 menu.findItem(R.id.stop_menu_id):
1794 menu.findItem(R.id.reload_menu_id);
1795 if (src != null) {
1796 dest.setIcon(src.getIcon());
1797 dest.setTitle(src.getTitle());
1798 }
1799 }
1800
John Reck9c35b9c2012-05-30 10:08:50 -07001801 @Override
1802 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001803 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001804 // hold on to the menu reference here; it is used by the page callbacks
1805 // to update the menu based on loading state
1806 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001807 // Note: setVisible will decide whether an item is visible; while
1808 // setEnabled() will decide whether an item is enabled, which also means
1809 // whether the matching shortcut key will function.
1810 switch (mMenuState) {
1811 case EMPTY_MENU:
1812 if (mCurrentMenuState != mMenuState) {
1813 menu.setGroupVisible(R.id.MAIN_MENU, false);
1814 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1815 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1816 }
1817 break;
1818 default:
1819 if (mCurrentMenuState != mMenuState) {
1820 menu.setGroupVisible(R.id.MAIN_MENU, true);
1821 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1822 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1823 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001824 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001825 break;
1826 }
1827 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001828 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001829 }
1830
Michael Kolb4bf79712011-07-14 14:18:12 -07001831 @Override
1832 public void updateMenuState(Tab tab, Menu menu) {
1833 boolean canGoBack = false;
1834 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001835 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001836 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001837 if (tab != null) {
1838 canGoBack = tab.canGoBack();
1839 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001840 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001841 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001842 }
1843 final MenuItem back = menu.findItem(R.id.back_menu_id);
1844 back.setEnabled(canGoBack);
1845
1846 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001847
1848 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1849 forward.setEnabled(canGoForward);
1850
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001851 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1852 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001853 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001854 if (source != null && dest != null) {
1855 dest.setTitle(source.getTitle());
1856 dest.setIcon(source.getIcon());
1857 }
John Recke1a03a32011-09-14 17:04:16 -07001858 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001859
1860 // decide whether to show the share link option
1861 PackageManager pm = mActivity.getPackageManager();
1862 Intent send = new Intent(Intent.ACTION_SEND);
1863 send.setType("text/plain");
1864 ResolveInfo ri = pm.resolveActivity(send,
1865 PackageManager.MATCH_DEFAULT_ONLY);
1866 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1867
1868 boolean isNavDump = mSettings.enableNavDump();
1869 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1870 nav.setVisible(isNavDump);
1871 nav.setEnabled(isNavDump);
1872
1873 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001874 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1875 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001876 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1877 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001878
1879 // history and snapshots item are the members of COMBO menu group,
1880 // so if show history item, only make snapshots item invisible.
1881 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001882
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001883 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001884 }
1885
John Reck9c35b9c2012-05-30 10:08:50 -07001886 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001888 if (null == getCurrentTopWebView()) {
1889 return false;
1890 }
1891 if (mMenuIsDown) {
1892 // The shortcut action consumes the MENU. Even if it is still down,
1893 // it won't trigger the next shortcut action. In the case of the
1894 // shortcut action triggering a new activity, like Bookmarks, we
1895 // won't get onKeyUp for MENU. So it is important to reset it here.
1896 mMenuIsDown = false;
1897 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001898 if (mUi.onOptionsItemSelected(item)) {
1899 // ui callback handled it
1900 return true;
1901 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001902 switch (item.getItemId()) {
1903 // -- Main menu
1904 case R.id.new_tab_menu_id:
1905 openTabToHomePage();
1906 break;
1907
1908 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001909 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001910 break;
1911
Afzal Najamd4e33312012-04-26 01:54:01 -04001912 case R.id.close_other_tabs_id:
1913 closeOtherTabs();
1914 break;
1915
Michael Kolb8233fac2010-10-26 16:08:53 -07001916 case R.id.goto_menu_id:
1917 editUrl();
1918 break;
1919
1920 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001921 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1922 break;
1923
1924 case R.id.history_menu_id:
1925 bookmarksOrHistoryPicker(ComboViews.History);
1926 break;
1927
1928 case R.id.snapshots_menu_id:
1929 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001930 break;
1931
Michael Kolb8233fac2010-10-26 16:08:53 -07001932 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001933 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001934 break;
1935
1936 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001937 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 stopLoading();
1939 } else {
1940 getCurrentTopWebView().reload();
1941 }
1942 break;
1943
1944 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001945 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001946 break;
1947
1948 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001949 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001950 break;
1951
1952 case R.id.close_menu_id:
1953 // Close the subwindow if it exists.
1954 if (mTabControl.getCurrentSubWindow() != null) {
1955 dismissSubWindow(mTabControl.getCurrentTab());
1956 break;
1957 }
1958 closeCurrentTab();
1959 break;
1960
kaiyiza8b6dbb2013-07-29 18:11:22 +08001961 case R.id.exit_menu_id:
1962 String ret = SystemProperties.get("persist.debug.browsermonkeytest");
1963 if (ret != null && ret.equals("enable"))
1964 break;
luxiaolb40014b2013-07-19 10:01:43 +08001965 showExitDialog(mActivity);
1966 return true;
kaiyiza8b6dbb2013-07-29 18:11:22 +08001967
Michael Kolb8233fac2010-10-26 16:08:53 -07001968 case R.id.homepage_menu_id:
1969 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001970 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001971 break;
1972
1973 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001974 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001975 break;
1976
1977 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001978 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001979 break;
1980
John Reck2bc80422011-06-30 15:11:49 -07001981 case R.id.save_snapshot_menu_id:
1982 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001983 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001984 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001985 break;
1986
Michael Kolb8233fac2010-10-26 16:08:53 -07001987 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001988 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001989 break;
1990
John Recke1a03a32011-09-14 17:04:16 -07001991 case R.id.snapshot_go_live:
1992 goLive();
1993 return true;
1994
Michael Kolb8233fac2010-10-26 16:08:53 -07001995 case R.id.share_page_menu_id:
1996 Tab currentTab = mTabControl.getCurrentTab();
1997 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001998 return false;
1999 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002000 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002001 break;
2002
2003 case R.id.dump_nav_menu_id:
2004 getCurrentTopWebView().debugDump();
2005 break;
2006
Michael Kolb8233fac2010-10-26 16:08:53 -07002007 case R.id.zoom_in_menu_id:
2008 getCurrentTopWebView().zoomIn();
2009 break;
2010
2011 case R.id.zoom_out_menu_id:
2012 getCurrentTopWebView().zoomOut();
2013 break;
2014
2015 case R.id.view_downloads_menu_id:
2016 viewDownloads();
2017 break;
2018
John Reck42229bc2011-08-19 13:26:43 -07002019 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002020 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002021 break;
2022
Michael Kolb8233fac2010-10-26 16:08:53 -07002023 case R.id.window_one_menu_id:
2024 case R.id.window_two_menu_id:
2025 case R.id.window_three_menu_id:
2026 case R.id.window_four_menu_id:
2027 case R.id.window_five_menu_id:
2028 case R.id.window_six_menu_id:
2029 case R.id.window_seven_menu_id:
2030 case R.id.window_eight_menu_id:
2031 {
2032 int menuid = item.getItemId();
2033 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2034 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2035 Tab desiredTab = mTabControl.getTab(id);
2036 if (desiredTab != null &&
2037 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002038 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002039 }
2040 break;
2041 }
2042 }
2043 }
2044 break;
2045
2046 default:
2047 return false;
2048 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002049 return true;
2050 }
2051
John Reck68234a92012-04-19 15:27:12 -07002052 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2053 implements OnCancelListener {
2054
2055 private Tab mTab;
2056 private Dialog mProgressDialog;
2057 private ContentValues mValues;
2058
2059 private SaveSnapshotTask(Tab tab) {
2060 mTab = tab;
2061 }
2062
2063 @Override
2064 protected void onPreExecute() {
2065 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2066 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2067 true, true, this);
2068 mValues = mTab.createSnapshotValues();
2069 }
2070
2071 @Override
2072 protected Long doInBackground(Void... params) {
2073 if (!mTab.saveViewState(mValues)) {
2074 return null;
2075 }
2076 if (isCancelled()) {
2077 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2078 File file = mActivity.getFileStreamPath(path);
2079 if (!file.delete()) {
2080 file.deleteOnExit();
2081 }
2082 return null;
2083 }
2084 final ContentResolver cr = mActivity.getContentResolver();
2085 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2086 if (result == null) {
2087 return null;
2088 }
2089 long id = ContentUris.parseId(result);
2090 return id;
2091 }
2092
2093 @Override
2094 protected void onPostExecute(Long id) {
2095 if (isCancelled()) {
2096 return;
2097 }
2098 mProgressDialog.dismiss();
2099 if (id == null) {
2100 Toast.makeText(mActivity, R.string.snapshot_failed,
2101 Toast.LENGTH_SHORT).show();
2102 return;
2103 }
2104 Bundle b = new Bundle();
2105 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2106 mUi.showComboView(ComboViews.Snapshots, b);
2107 }
2108
2109 @Override
2110 public void onCancel(DialogInterface dialog) {
2111 cancel(true);
2112 }
2113 }
2114
Michael Kolb80f75082012-04-10 10:50:06 -07002115 @Override
2116 public void toggleUserAgent() {
2117 WebView web = getCurrentWebView();
2118 mSettings.toggleDesktopUseragent(web);
2119 web.loadUrl(web.getOriginalUrl());
2120 }
2121
2122 @Override
2123 public void findOnPage() {
2124 getCurrentTopWebView().showFindDialog(null, true);
2125 }
2126
2127 @Override
2128 public void openPreferences() {
2129 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2130 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2131 getCurrentTopWebView().getUrl());
2132 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2133 }
2134
2135 @Override
2136 public void bookmarkCurrentPage() {
2137 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2138 if (bookmarkIntent != null) {
2139 mActivity.startActivity(bookmarkIntent);
2140 }
2141 }
2142
John Recke1a03a32011-09-14 17:04:16 -07002143 private void goLive() {
2144 Tab t = getCurrentTab();
2145 t.loadUrl(t.getUrl(), null);
2146 }
2147
luxiaolb40014b2013-07-19 10:01:43 +08002148 private void showExitDialog(final Activity activity) {
2149 new AlertDialog.Builder(activity)
2150 .setTitle(R.string.exit_browser_title)
2151 .setIcon(android.R.drawable.ic_dialog_alert)
2152 .setMessage(R.string.exit_browser_msg)
2153 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2154 public void onClick(DialogInterface dialog, int which) {
2155 activity.moveTaskToBack(true);
2156 dialog.dismiss();
2157 }
2158 })
2159 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2160 public void onClick(DialogInterface dialog, int which) {
2161 activity.finish();
2162 mHandler.postDelayed(new Runnable() {
2163 @Override
2164 public void run() {
2165 // TODO Auto-generated method stub
2166 mCrashRecoveryHandler.clearState(true);
2167 int pid = android.os.Process.myPid();
2168 android.os.Process.killProcess(pid);
2169 }
2170 }, 300);
2171 dialog.dismiss();
2172 }
2173 })
2174 .show();
2175 }
2176
Michael Kolb315d5022011-10-13 12:47:11 -07002177 @Override
2178 public void showPageInfo() {
2179 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2180 }
2181
John Reck9c35b9c2012-05-30 10:08:50 -07002182 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002183 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002184 // Let the History and Bookmark fragments handle menus they created.
2185 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2186 return false;
2187 }
2188
Michael Kolb8233fac2010-10-26 16:08:53 -07002189 int id = item.getItemId();
2190 boolean result = true;
2191 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002192 // -- Browser context menu
2193 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002194 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002195 case R.id.copy_link_context_menu_id:
2196 final WebView webView = getCurrentTopWebView();
2197 if (null == webView) {
2198 result = false;
2199 break;
2200 }
2201 final HashMap<String, WebView> hrefMap =
2202 new HashMap<String, WebView>();
2203 hrefMap.put("webview", webView);
2204 final Message msg = mHandler.obtainMessage(
2205 FOCUS_NODE_HREF, id, 0, hrefMap);
2206 webView.requestFocusNodeHref(msg);
2207 break;
2208
2209 default:
2210 // For other context menus
2211 result = onOptionsItemSelected(item);
2212 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002213 return result;
2214 }
2215
2216 /**
2217 * support programmatically opening the context menu
2218 */
2219 public void openContextMenu(View view) {
2220 mActivity.openContextMenu(view);
2221 }
2222
2223 /**
2224 * programmatically open the options menu
2225 */
2226 public void openOptionsMenu() {
2227 mActivity.openOptionsMenu();
2228 }
2229
John Reck9c35b9c2012-05-30 10:08:50 -07002230 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002231 public boolean onMenuOpened(int featureId, Menu menu) {
2232 if (mOptionsMenuOpen) {
2233 if (mConfigChanged) {
2234 // We do not need to make any changes to the state of the
2235 // title bar, since the only thing that happened was a
2236 // change in orientation
2237 mConfigChanged = false;
2238 } else {
2239 if (!mExtendedMenuOpen) {
2240 mExtendedMenuOpen = true;
2241 mUi.onExtendedMenuOpened();
2242 } else {
2243 // Switching the menu back to icon view, so show the
2244 // title bar once again.
2245 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002246 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002247 }
2248 }
2249 } else {
2250 // The options menu is closed, so open it, and show the title
2251 mOptionsMenuOpen = true;
2252 mConfigChanged = false;
2253 mExtendedMenuOpen = false;
2254 mUi.onOptionsMenuOpened();
2255 }
2256 return true;
2257 }
2258
John Reck9c35b9c2012-05-30 10:08:50 -07002259 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002260 public void onOptionsMenuClosed(Menu menu) {
2261 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002262 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002263 }
2264
John Reck9c35b9c2012-05-30 10:08:50 -07002265 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002266 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002267 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002268 }
2269
2270 // Helper method for getting the top window.
2271 @Override
2272 public WebView getCurrentTopWebView() {
2273 return mTabControl.getCurrentTopWebView();
2274 }
2275
2276 @Override
2277 public WebView getCurrentWebView() {
2278 return mTabControl.getCurrentWebView();
2279 }
2280
2281 /*
2282 * This method is called as a result of the user selecting the options
2283 * menu to see the download window. It shows the download window on top of
2284 * the current window.
2285 */
2286 void viewDownloads() {
2287 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2288 mActivity.startActivity(intent);
2289 }
2290
John Reck30b065e2011-07-19 10:58:05 -07002291 int getActionModeHeight() {
2292 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2293 new int[] { android.R.attr.actionBarSize });
2294 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2295 actionBarSizeTypedArray.recycle();
2296 return size;
2297 }
2298
Michael Kolb8233fac2010-10-26 16:08:53 -07002299 // action mode
2300
John Reck9c35b9c2012-05-30 10:08:50 -07002301 @Override
2302 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002303 mUi.onActionModeStarted(mode);
2304 mActionMode = mode;
2305 }
2306
2307 /*
2308 * True if a custom ActionMode (i.e. find or select) is in use.
2309 */
2310 @Override
2311 public boolean isInCustomActionMode() {
2312 return mActionMode != null;
2313 }
2314
2315 /*
2316 * End the current ActionMode.
2317 */
2318 @Override
2319 public void endActionMode() {
2320 if (mActionMode != null) {
2321 mActionMode.finish();
2322 }
2323 }
2324
2325 /*
2326 * Called by find and select when they are finished. Replace title bars
2327 * as necessary.
2328 */
John Reck9c35b9c2012-05-30 10:08:50 -07002329 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002330 public void onActionModeFinished(ActionMode mode) {
2331 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002332 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002333 mActionMode = null;
2334 }
2335
2336 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002337 final Tab tab = getCurrentTab();
2338 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002339 }
2340
2341 // bookmark handling
2342
2343 /**
2344 * add the current page as a bookmark to the given folder id
2345 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002346 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002347 * bookmarked, and if it is, edit that bookmark. If false, and
2348 * the site is already bookmarked, do not attempt to edit the
2349 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002350 */
2351 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002352 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002353 WebView w = getCurrentTopWebView();
2354 if (w == null) {
2355 return null;
2356 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002357 Intent i = new Intent(mActivity,
2358 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002359 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2360 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2361 String touchIconUrl = w.getTouchIconUrl();
2362 if (touchIconUrl != null) {
2363 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2364 WebSettings settings = w.getSettings();
2365 if (settings != null) {
2366 i.putExtra(AddBookmarkPage.USER_AGENT,
2367 settings.getUserAgentString());
2368 }
2369 }
2370 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2371 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2372 getDesiredThumbnailHeight(mActivity)));
2373 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002374 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002375 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2376 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002377 // Put the dialog at the upper right of the screen, covering the
2378 // star on the title bar.
2379 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002380 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002381 }
2382
2383 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002384 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002385 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002386 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002387 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002388 }
2389
2390 // thumbnails
2391
2392 /**
2393 * Return the desired width for thumbnail screenshots, which are stored in
2394 * the database, and used on the bookmarks screen.
2395 * @param context Context for finding out the density of the screen.
2396 * @return desired width for thumbnail screenshot.
2397 */
2398 static int getDesiredThumbnailWidth(Context context) {
2399 return context.getResources().getDimensionPixelOffset(
2400 R.dimen.bookmarkThumbnailWidth);
2401 }
2402
2403 /**
2404 * Return the desired height for thumbnail screenshots, which are stored in
2405 * the database, and used on the bookmarks screen.
2406 * @param context Context for finding out the density of the screen.
2407 * @return desired height for thumbnail screenshot.
2408 */
2409 static int getDesiredThumbnailHeight(Context context) {
2410 return context.getResources().getDimensionPixelOffset(
2411 R.dimen.bookmarkThumbnailHeight);
2412 }
2413
John Reck8cc92352011-07-06 17:41:52 -07002414 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002415 if (view == null || view.getContentHeight() == 0
2416 || view.getContentWidth() == 0) {
2417 return null;
2418 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002419 // We render to a bitmap 2x the desired size so that we can then
2420 // re-scale it with filtering since canvas.scale doesn't filter
2421 // This helps reduce aliasing at the cost of being slightly blurry
2422 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002423 int scaledWidth = width * filter_scale;
2424 int scaledHeight = height * filter_scale;
2425 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2426 || sThumbnailBitmap.getHeight() != scaledHeight) {
2427 if (sThumbnailBitmap != null) {
2428 sThumbnailBitmap.recycle();
2429 sThumbnailBitmap = null;
2430 }
2431 sThumbnailBitmap =
2432 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 }
John Reckd7dd9b22011-08-30 09:18:29 -07002434 Canvas canvas = new Canvas(sThumbnailBitmap);
2435 int contentWidth = view.getContentWidth();
2436 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2437 if (view instanceof BrowserWebView) {
2438 int dy = -((BrowserWebView)view).getTitleHeight();
2439 canvas.translate(0, dy * overviewScale);
2440 }
2441
2442 canvas.scale(overviewScale, overviewScale);
2443
2444 if (view instanceof BrowserWebView) {
2445 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002446 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002447 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002448 }
John Reckd7dd9b22011-08-30 09:18:29 -07002449 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2450 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002451 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002452 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002453 }
2454
John Reck34ef2672011-02-10 11:30:55 -08002455 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002456 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002457 // FIXME: Would like to make sure there is actually something to
2458 // draw, but the API for that (WebViewCore.pictureReady()) is not
2459 // currently accessible here.
2460
John Reck34ef2672011-02-10 11:30:55 -08002461 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002462 if (view == null) {
2463 // Tab was destroyed
2464 return;
2465 }
John Reck34ef2672011-02-10 11:30:55 -08002466 final String url = tab.getUrl();
2467 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002468 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002469 if (TextUtils.isEmpty(url)) {
2470 return;
2471 }
2472
kaiyiz6e5b3e02013-08-19 20:02:01 +08002473 //update My Navigation Thumbnails
2474 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2475 if (isMyNavigationUrl) {
2476 updateMyNavigationThumbnail(url, view);
2477 }
John Reck34ef2672011-02-10 11:30:55 -08002478 // Only update thumbnails for web urls (http(s)://), not for
2479 // about:, javascript:, data:, etc...
2480 // Unless it is a bookmarked site, then always update
2481 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2482 return;
2483 }
2484
kaiyiz6e5b3e02013-08-19 20:02:01 +08002485 if (url != null && mUpdateMyNavThumbnailUrl != null
2486 && Patterns.WEB_URL.matcher(url).matches()
2487 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2488 String urlHost = (new WebAddress(url)).getHost();
2489 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2490 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2491 || bookmarkHost.length() == 0) {
2492 return;
2493 }
2494 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2495 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2496 bookmarkHost.length());
2497 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2498 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2499 if (!bookmarkDomain.equals(urlDomain)) {
2500 return;
2501 }
2502 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002503 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2504 getDesiredThumbnailHeight(mActivity));
2505 if (bm == null) {
2506 return;
2507 }
2508
2509 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002510 new AsyncTask<Void, Void, Void>() {
2511 @Override
2512 protected Void doInBackground(Void... unused) {
2513 Cursor cursor = null;
2514 try {
2515 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002516 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2517 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2518 : url);
2519 if (mUpdateMyNavThumbnail) {
2520 mUpdateMyNavThumbnail = false;
2521 mUpdateMyNavThumbnailUrl = null;
2522 }
John Reck34ef2672011-02-10 11:30:55 -08002523 if (cursor != null && cursor.moveToFirst()) {
2524 final ByteArrayOutputStream os =
2525 new ByteArrayOutputStream();
2526 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002527
John Reck34ef2672011-02-10 11:30:55 -08002528 ContentValues values = new ContentValues();
2529 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002530
John Reck34ef2672011-02-10 11:30:55 -08002531 do {
John Reck617fd832011-02-16 14:35:59 -08002532 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002533 cr.update(Images.CONTENT_URI, values, null, null);
2534 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002535 }
John Reck34ef2672011-02-10 11:30:55 -08002536 } catch (IllegalStateException e) {
2537 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002538 } catch (SQLiteException s) {
2539 // Added for possible error when user tries to remove the same bookmark
2540 // that is being updated with a screen shot
2541 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002542 } finally {
2543 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002544 }
John Reck34ef2672011-02-10 11:30:55 -08002545 return null;
2546 }
2547 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002548 }
2549
2550 private class Copy implements OnMenuItemClickListener {
2551 private CharSequence mText;
2552
John Reck9c35b9c2012-05-30 10:08:50 -07002553 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002554 public boolean onMenuItemClick(MenuItem item) {
2555 copy(mText);
2556 return true;
2557 }
2558
2559 public Copy(CharSequence toCopy) {
2560 mText = toCopy;
2561 }
2562 }
2563
Leon Scroggins63c02662010-11-18 15:16:27 -05002564 private static class Download implements OnMenuItemClickListener {
2565 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002566 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002567 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002568 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002569 private static final String FALLBACK_EXTENSION = "dat";
2570 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002571
John Reck9c35b9c2012-05-30 10:08:50 -07002572 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002573 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002574 if (DataUri.isDataUri(mText)) {
2575 saveDataUri();
2576 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002577 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002578 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002579 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 return true;
2581 }
2582
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002583 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2584 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002585 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002586 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002587 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002588 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002589 }
George Mount387d45d2011-10-07 15:57:53 -07002590
2591 /**
2592 * Treats mText as a data URI and writes its contents to a file
2593 * based on the current time.
2594 */
2595 private void saveDataUri() {
2596 FileOutputStream outputStream = null;
2597 try {
2598 DataUri uri = new DataUri(mText);
2599 File target = getTarget(uri);
2600 outputStream = new FileOutputStream(target);
2601 outputStream.write(uri.getData());
2602 final DownloadManager manager =
2603 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2604 manager.addCompletedDownload(target.getName(),
2605 mActivity.getTitle().toString(), false,
2606 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002607 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002608 } catch (IOException e) {
2609 Log.e(LOGTAG, "Could not save data URL");
2610 } finally {
2611 if (outputStream != null) {
2612 try {
2613 outputStream.close();
2614 } catch (IOException e) {
2615 // ignore close errors
2616 }
2617 }
2618 }
2619 }
2620
2621 /**
2622 * Creates a File based on the current time stamp and uses
2623 * the mime type of the DataUri to get the extension.
2624 */
2625 private File getTarget(DataUri uri) throws IOException {
2626 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002627 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002628 String nameBase = format.format(new Date());
2629 String mimeType = uri.getMimeType();
2630 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2631 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2632 if (extension == null) {
2633 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2634 extension = FALLBACK_EXTENSION;
2635 }
2636 extension = "." + extension; // createTempFile needs the '.'
2637 File targetFile = File.createTempFile(nameBase, extension, dir);
2638 return targetFile;
2639 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002640 }
2641
Cary Clark8974d282010-11-22 10:46:05 -05002642 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002643 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002644
John Reck9c35b9c2012-05-30 10:08:50 -07002645 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002646 public boolean onMenuItemClick(MenuItem item) {
2647 if (mWebView != null) {
2648 return mWebView.selectText();
2649 }
2650 return false;
2651 }
2652
2653 public SelectText(WebView webView) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002654 if (BrowserWebView.isClassic()) {
2655 mWebView = WebViewClassic.fromWebView(webView);
2656 }
Cary Clark8974d282010-11-22 10:46:05 -05002657 }
2658
2659 }
2660
Michael Kolb8233fac2010-10-26 16:08:53 -07002661 /********************** TODO: UI stuff *****************************/
2662
2663 // these methods have been copied, they still need to be cleaned up
2664
2665 /****************** tabs ***************************************************/
2666
2667 // basic tab interactions:
2668
2669 // it is assumed that tabcontrol already knows about the tab
2670 protected void addTab(Tab tab) {
2671 mUi.addTab(tab);
2672 }
2673
2674 protected void removeTab(Tab tab) {
2675 mUi.removeTab(tab);
2676 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002677 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002678 }
2679
Michael Kolbf2055602011-04-09 17:20:03 -07002680 @Override
2681 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002682 // monkey protection against delayed start
2683 if (tab != null) {
2684 mTabControl.setCurrentTab(tab);
2685 // the tab is guaranteed to have a webview after setCurrentTab
2686 mUi.setActiveTab(tab);
2687 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002688 }
2689
John Reck8bcafc12011-08-29 16:43:02 -07002690 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002691 Tab current = mTabControl.getCurrentTab();
2692 if (current != null
2693 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002694 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002695 }
2696 }
2697
John Reck26b18322011-06-21 13:08:58 -07002698 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002699 // Dismiss the subwindow if applicable.
2700 dismissSubWindow(appTab);
2701 // Since we might kill the WebView, remove it from the
2702 // content view first.
2703 mUi.detachTab(appTab);
2704 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002705 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002706 // TODO: analyze why the remove and add are necessary
2707 mUi.attachTab(appTab);
2708 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002709 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002710 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002711 } else {
2712 // If the tab was the current tab, we have to attach
2713 // it to the view system again.
2714 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002715 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002716 }
2717 }
2718
2719 // Remove the sub window if it exists. Also called by TabControl when the
2720 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002721 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002722 public void dismissSubWindow(Tab tab) {
2723 removeSubWindow(tab);
2724 // dismiss the subwindow. This will destroy the WebView.
2725 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002726 WebView wv = getCurrentTopWebView();
2727 if (wv != null) {
2728 wv.requestFocus();
2729 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 }
2731
2732 @Override
2733 public void removeSubWindow(Tab t) {
2734 if (t.getSubWebView() != null) {
2735 mUi.removeSubWindow(t.getSubViewContainer());
2736 }
2737 }
2738
2739 @Override
2740 public void attachSubWindow(Tab tab) {
2741 if (tab.getSubWebView() != null) {
2742 mUi.attachSubWindow(tab.getSubViewContainer());
2743 getCurrentTopWebView().requestFocus();
2744 }
2745 }
2746
Mathew Inwood29721c22011-06-29 17:55:24 +01002747 private Tab showPreloadedTab(final UrlData urlData) {
2748 if (!urlData.isPreloaded()) {
2749 return null;
2750 }
2751 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2752 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2753 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002754 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002755 // Could not submit query. Fallback to regular tab creation
2756 tabControl.destroy();
2757 return null;
2758 }
2759 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002760 // check tab count and make room for new tab
2761 if (!mTabControl.canCreateNewTab()) {
2762 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2763 if (leastUsed != null) {
2764 closeTab(leastUsed);
2765 }
2766 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002767 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002768 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002769 mTabControl.addPreloadedTab(t);
2770 addTab(t);
2771 setActiveTab(t);
2772 return t;
2773 }
2774
Michael Kolb7bcafde2011-05-09 13:55:59 -07002775 // open a non inconito tab with the given url data
2776 // and set as active tab
2777 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002778 Tab tab = showPreloadedTab(urlData);
2779 if (tab == null) {
2780 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002781 if ((tab != null) && !urlData.isEmpty()) {
2782 loadUrlDataIn(tab, urlData);
2783 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002784 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002785 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002786 }
2787
Michael Kolb843510f2010-12-09 10:51:49 -08002788 @Override
2789 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002790 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002791 }
2792
Michael Kolb8233fac2010-10-26 16:08:53 -07002793 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002794 public Tab openIncognitoTab() {
2795 return openTab(INCOGNITO_URI, true, true, false);
2796 }
2797
2798 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002799 public Tab openTab(String url, boolean incognito, boolean setActive,
2800 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002801 return openTab(url, incognito, setActive, useCurrent, null);
2802 }
2803
2804 @Override
2805 public Tab openTab(String url, Tab parent, boolean setActive,
2806 boolean useCurrent) {
2807 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2808 setActive, useCurrent, parent);
2809 }
2810
2811 public Tab openTab(String url, boolean incognito, boolean setActive,
2812 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002813 Tab tab = createNewTab(incognito, setActive, useCurrent);
2814 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002815 if (parent != null && parent != tab) {
2816 parent.addChildTab(tab);
2817 }
Michael Kolb519d2282011-05-09 17:03:19 -07002818 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002819 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002820 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002821 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002822 return tab;
2823 }
2824
2825 // this method will attempt to create a new tab
2826 // incognito: private browsing tab
2827 // setActive: ste tab as current tab
2828 // useCurrent: if no new tab can be created, return current tab
2829 private Tab createNewTab(boolean incognito, boolean setActive,
2830 boolean useCurrent) {
2831 Tab tab = null;
2832 if (mTabControl.canCreateNewTab()) {
2833 tab = mTabControl.createNewTab(incognito);
2834 addTab(tab);
2835 if (setActive) {
2836 setActiveTab(tab);
2837 }
2838 } else {
2839 if (useCurrent) {
2840 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002841 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002842 } else {
2843 mUi.showMaxTabsWarning();
2844 }
2845 }
2846 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002847 }
2848
John Reck2bc80422011-06-30 15:11:49 -07002849 @Override
2850 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2851 SnapshotTab tab = null;
2852 if (mTabControl.canCreateNewTab()) {
2853 tab = mTabControl.createSnapshotTab(snapshotId);
2854 addTab(tab);
2855 if (setActive) {
2856 setActiveTab(tab);
2857 }
2858 } else {
2859 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002860 }
2861 return tab;
2862 }
2863
Michael Kolb8233fac2010-10-26 16:08:53 -07002864 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002865 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002866 * @return boolean True if we successfully switched to a different tab. If
2867 * the indexth tab is null, or if that tab is the same as
2868 * the current one, return false.
2869 */
2870 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002871 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002872 Tab currentTab = mTabControl.getCurrentTab();
2873 if (tab == null || tab == currentTab) {
2874 return false;
2875 }
2876 setActiveTab(tab);
2877 return true;
2878 }
2879
2880 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002881 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002882 closeCurrentTab(false);
2883 }
2884
2885 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002886 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002887 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002888 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002889 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002890 return;
2891 }
Michael Kolbc831b632011-05-11 09:30:34 -07002892 final Tab current = mTabControl.getCurrentTab();
2893 final int pos = mTabControl.getCurrentPosition();
2894 Tab newTab = current.getParent();
2895 if (newTab == null) {
2896 newTab = mTabControl.getTab(pos + 1);
2897 if (newTab == null) {
2898 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002899 }
2900 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002901 if (andQuit) {
2902 mTabControl.setCurrentTab(newTab);
2903 closeTab(current);
2904 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002905 // Close window
2906 closeTab(current);
2907 }
2908 }
2909
2910 /**
2911 * Close the tab, remove its associated title bar, and adjust mTabControl's
2912 * current tab to a valid value.
2913 */
2914 @Override
2915 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002916 if (tab == mTabControl.getCurrentTab()) {
2917 closeCurrentTab();
2918 } else {
2919 removeTab(tab);
2920 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002921 }
2922
Afzal Najamd4e33312012-04-26 01:54:01 -04002923 /**
2924 * Close all tabs except the current one
2925 */
2926 @Override
2927 public void closeOtherTabs() {
2928 int inactiveTabs = mTabControl.getTabCount() - 1;
2929 for (int i = inactiveTabs; i >= 0; i--) {
2930 Tab tab = mTabControl.getTab(i);
2931 if (tab != mTabControl.getCurrentTab()) {
2932 removeTab(tab);
2933 }
2934 }
2935 }
2936
Michael Kolb8233fac2010-10-26 16:08:53 -07002937 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002938 protected void loadUrlFromContext(String url) {
2939 Tab tab = getCurrentTab();
2940 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002941 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002942 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002943 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002944 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002945 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002946 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002947 }
2948 }
2949 }
2950
2951 /**
2952 * Load the URL into the given WebView and update the title bar
2953 * to reflect the new load. Call this instead of WebView.loadUrl
2954 * directly.
2955 * @param view The WebView used to load url.
2956 * @param url The URL to load.
2957 */
John Reck71e51422011-07-01 16:49:28 -07002958 @Override
2959 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002960 loadUrl(tab, url, null);
2961 }
2962
2963 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2964 if (tab != null) {
2965 dismissSubWindow(tab);
2966 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002967 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002968 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002969 }
2970
2971 /**
2972 * Load UrlData into a Tab and update the title bar to reflect the new
2973 * load. Call this instead of UrlData.loadIn directly.
2974 * @param t The Tab used to load.
2975 * @param data The UrlData being loaded.
2976 */
2977 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002978 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002979 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002980 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002981 } else {
John Reck38b39652012-06-05 09:22:59 -07002982 if (t != null && data.mDisableUrlOverride) {
2983 t.disableUrlOverridingForLoad();
2984 }
John Reck26b18322011-06-21 13:08:58 -07002985 loadUrl(t, data.mUrl, data.mHeaders);
2986 }
2987 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002988 }
2989
John Reck30c714c2010-12-16 17:30:34 -08002990 @Override
2991 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002992 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002993 if (tab.canGoBack()) {
2994 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002995 } else {
John Reckef654f12011-07-12 16:42:08 -07002996 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002997 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 }
2999
3000 void goBackOnePageOrQuit() {
3001 Tab current = mTabControl.getCurrentTab();
3002 if (current == null) {
3003 /*
3004 * Instead of finishing the activity, simply push this to the back
3005 * of the stack and let ActivityManager to choose the foreground
3006 * activity. As BrowserActivity is singleTask, it will be always the
3007 * root of the task. So we can use either true or false for
3008 * moveTaskToBack().
3009 */
luxiaolb40014b2013-07-19 10:01:43 +08003010 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003011 return;
3012 }
John Reckef654f12011-07-12 16:42:08 -07003013 if (current.canGoBack()) {
3014 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003015 } else {
3016 // Check to see if we are closing a window that was created by
3017 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003018 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003019 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003020 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003021 // Now we close the other tab
3022 closeTab(current);
3023 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003024 /*
3025 * Instead of finishing the activity, simply push this to the back
3026 * of the stack and let ActivityManager to choose the foreground
3027 * activity. As BrowserActivity is singleTask, it will be always the
3028 * root of the task. So we can use either true or false for
3029 * moveTaskToBack().
3030 */
luxiaolb40014b2013-07-19 10:01:43 +08003031 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003032 }
3033 }
3034 }
3035
3036 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003037 * helper method for key handler
3038 * returns the current tab if it can't advance
3039 */
Michael Kolbc831b632011-05-11 09:30:34 -07003040 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003041 int pos = mTabControl.getCurrentPosition() + 1;
3042 if (pos >= mTabControl.getTabCount()) {
3043 pos = 0;
3044 }
3045 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003046 }
3047
3048 /**
3049 * helper method for key handler
3050 * returns the current tab if it can't advance
3051 */
Michael Kolbc831b632011-05-11 09:30:34 -07003052 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003053 int pos = mTabControl.getCurrentPosition() - 1;
3054 if ( pos < 0) {
3055 pos = mTabControl.getTabCount() - 1;
3056 }
3057 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003058 }
3059
John Reckbcef87f2012-02-03 14:58:34 -08003060 boolean isMenuOrCtrlKey(int keyCode) {
3061 return (KeyEvent.KEYCODE_MENU == keyCode)
3062 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3063 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3064 }
3065
Michael Kolb0035fad2011-03-14 13:25:28 -07003066 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003067 * handle key events in browser
3068 *
3069 * @param keyCode
3070 * @param event
3071 * @return true if handled, false to pass to super
3072 */
John Reck9c35b9c2012-05-30 10:08:50 -07003073 @Override
3074 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003075 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003076 // Even if MENU is already held down, we need to call to super to open
3077 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003078 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003079 mMenuIsDown = true;
3080 return false;
3081 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003082
Cary Clark8ff8c662010-12-29 15:03:05 -05003083 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003084 Tab tab = getCurrentTab();
3085 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003086
Cary Clark160bbb92011-01-10 11:17:07 -05003087 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3088 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003089
Michael Kolb8233fac2010-10-26 16:08:53 -07003090 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003091 case KeyEvent.KEYCODE_TAB:
3092 if (event.isCtrlPressed()) {
3093 if (event.isShiftPressed()) {
3094 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003095 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003096 } else {
3097 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003098 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003099 }
3100 return true;
3101 }
3102 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003103 case KeyEvent.KEYCODE_SPACE:
3104 // WebView/WebTextView handle the keys in the KeyDown. As
3105 // the Activity's shortcut keys are only handled when WebView
3106 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003107 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003108 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003109 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003110 pageDown();
3111 }
3112 return true;
3113 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003114 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003115 event.startTracking();
3116 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003117 case KeyEvent.KEYCODE_FORWARD:
3118 if (!noModifiers) break;
3119 tab.goForward();
3120 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003121 case KeyEvent.KEYCODE_DPAD_LEFT:
3122 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003123 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003124 return true;
3125 }
3126 break;
3127 case KeyEvent.KEYCODE_DPAD_RIGHT:
3128 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003129 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003130 return true;
3131 }
3132 break;
3133 case KeyEvent.KEYCODE_A:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003134 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003135 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003136 return true;
3137 }
3138 break;
Michael Kolba4183062011-01-16 10:43:21 -08003139// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003140 case KeyEvent.KEYCODE_C:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003141 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003142 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003143 return true;
3144 }
3145 break;
Michael Kolba4183062011-01-16 10:43:21 -08003146// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003147// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003148// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003149// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003150// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003151// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003152// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003153// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003154// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003155// case KeyEvent.KEYCODE_M: // unused
3156// case KeyEvent.KEYCODE_N: // in Chrome: new window
3157// case KeyEvent.KEYCODE_O: // in Chrome: open file
3158// case KeyEvent.KEYCODE_P: // in Chrome: print page
3159// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003160// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003161// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3162 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003163 // we can't use the ctrl/shift flags, they check for
3164 // exclusive use of a modifier
3165 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003166 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003167 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003168 } else {
3169 openTabToHomePage();
3170 }
3171 return true;
3172 }
3173 break;
3174// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3175// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003176// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003177// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3178// case KeyEvent.KEYCODE_Y: // unused
3179// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003180 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003181 // it is a regular key and webview is not null
3182 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003183 }
3184
John Reck9c35b9c2012-05-30 10:08:50 -07003185 @Override
3186 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003187 switch(keyCode) {
3188 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003189 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003190 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003191 return true;
3192 }
3193 break;
3194 }
3195 return false;
3196 }
3197
John Reck9c35b9c2012-05-30 10:08:50 -07003198 @Override
3199 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003200 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003201 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003202 if (KeyEvent.KEYCODE_MENU == keyCode
3203 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003204 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003205 }
3206 }
Cary Clark160bbb92011-01-10 11:17:07 -05003207 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003208 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003209 case KeyEvent.KEYCODE_BACK:
3210 if (event.isTracking() && !event.isCanceled()) {
3211 onBackKey();
3212 return true;
3213 }
3214 break;
3215 }
3216 return false;
3217 }
3218
3219 public boolean isMenuDown() {
3220 return mMenuIsDown;
3221 }
3222
John Reck9c35b9c2012-05-30 10:08:50 -07003223 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003224 public void setupAutoFill(Message message) {
3225 // Open the settings activity at the AutoFill profile fragment so that
3226 // the user can create a new profile. When they return, we will dispatch
3227 // the message so that we can autofill the form using their new profile.
3228 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3229 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3230 AutoFillSettingsFragment.class.getName());
3231 mAutoFillSetupMessage = message;
3232 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3233 }
John Reckb3417f02011-01-14 11:01:05 -08003234
John Reck9c35b9c2012-05-30 10:08:50 -07003235 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003236 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003237 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003238 return true;
3239 }
3240
John Reck1cf4b792011-07-26 10:22:22 -07003241 @Override
3242 public boolean shouldCaptureThumbnails() {
3243 return mUi.shouldCaptureThumbnails();
3244 }
3245
Michael Kolbc3af0672011-08-09 10:24:41 -07003246 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003247 public boolean supportsVoice() {
3248 PackageManager pm = mActivity.getPackageManager();
3249 List activities = pm.queryIntentActivities(new Intent(
3250 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3251 return activities.size() != 0;
3252 }
3253
3254 @Override
3255 public void startVoiceRecognizer() {
3256 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
3257 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
3258 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3259 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3260 mActivity.startActivityForResult(voice, VOICE_RESULT);
3261 }
3262
3263 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003264 public void setBlockEvents(boolean block) {
3265 mBlockEvents = block;
3266 }
3267
John Reck9c35b9c2012-05-30 10:08:50 -07003268 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003269 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003270 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003271 }
3272
John Reck9c35b9c2012-05-30 10:08:50 -07003273 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003274 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003275 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003276 }
3277
John Reck9c35b9c2012-05-30 10:08:50 -07003278 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003279 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003280 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003281 }
3282
John Reck9c35b9c2012-05-30 10:08:50 -07003283 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003284 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003285 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003286 }
3287
John Reck9c35b9c2012-05-30 10:08:50 -07003288 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003289 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003290 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003291 }
3292
Michael Kolb8233fac2010-10-26 16:08:53 -07003293}