blob: 27b36200260e5b1aca29b620aecfa5b080e17ba8 [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080020
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080044import android.graphics.Paint;
45import android.graphics.Rect;
kaiyiza016da12013-08-26 17:50:22 +080046import android.net.ConnectivityManager;
47import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.net.Uri;
49import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080050import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080051import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.os.AsyncTask;
53import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070054import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.os.Handler;
56import android.os.Message;
57import android.os.PowerManager;
58import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.provider.Browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070061import android.provider.ContactsContract;
62import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080063import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070064import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070065import android.text.TextUtils;
66import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080067import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070068import android.view.ActionMode;
69import android.view.ContextMenu;
70import android.view.ContextMenu.ContextMenuInfo;
71import android.view.Gravity;
72import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070073import android.view.Menu;
74import android.view.MenuInflater;
75import android.view.MenuItem;
76import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070077import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070078import android.view.View;
George Mount387d45d2011-10-07 15:57:53 -070079import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070080import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080081import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb8233fac2010-10-26 16:08:53 -070082import android.webkit.WebIconDatabase;
Leon Scrogginsac993842011-02-02 12:54:07 -050083import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070084
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080085import org.codeaurora.swe.CookieManager;
86import org.codeaurora.swe.CookieSyncManager;
87import org.codeaurora.swe.HttpAuthHandler;
88import org.codeaurora.swe.SslErrorHandler;
89import org.codeaurora.swe.WebSettings;
90import org.codeaurora.swe.WebView;
91
Bijan Amirzada41242f22014-03-21 12:12:18 -070092import com.android.browser.R;
93import com.android.browser.IntentHandler.UrlData;
94import com.android.browser.UI.ComboViews;
95import com.android.browser.mynavigation.AddMyNavigationPage;
96import com.android.browser.mynavigation.MyNavigationUtil;
97import com.android.browser.platformsupport.BrowserContract;
98import com.android.browser.platformsupport.WebAddress;
99import com.android.browser.platformsupport.BrowserContract.Images;
100import com.android.browser.provider.BrowserProvider2.Thumbnails;
101import com.android.browser.provider.SnapshotProvider.Snapshots;
102import com.android.browser.reflect.ReflectHelper;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700103
Michael Kolb8233fac2010-10-26 16:08:53 -0700104import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700105import java.io.File;
106import java.io.FileOutputStream;
107import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700108import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700109import java.text.DateFormat;
110import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700111import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700112import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700113import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800115import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200116import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700117import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700118
119/**
120 * Controller for browser
121 */
122public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700123 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700124
125 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800126 private static final String SEND_APP_ID_EXTRA =
127 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700128 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800129
130
kaiyiza016da12013-08-26 17:50:22 +0800131 private static final String PROP_NETSWITCH = "persist.env.browser.netswitch";
132 private static final String INTENT_WIFI_SELECTION_DATA_CONNECTION =
133 "android.net.wifi.cmcc.WIFI_SELECTION_DATA_CONNECTION";
kaiyiz6e5b3e02013-08-19 20:02:01 +0800134 private static final String OFFLINE_PAGE =
Bijan Amirzada41242f22014-03-21 12:12:18 -0700135 "content://com.android.browser.mynavigation/websites";
kaiyizb7bf1f22013-10-02 11:42:23 +0800136 private static final String INTENT_PICK_NETWORK =
137 "android.net.wifi.cmcc.PICK_WIFI_NETWORK_AND_GPRS";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800138
139 public final static String EXTRA_SHARE_SCREENSHOT = "share_screenshot";
140 public final static String EXTRA_SHARE_FAVICON = "share_favicon";
Michael Kolb8233fac2010-10-26 16:08:53 -0700141 // public message ids
142 public final static int LOAD_URL = 1001;
143 public final static int STOP_LOAD = 1002;
144
145 // Message Ids
146 private static final int FOCUS_NODE_HREF = 102;
147 private static final int RELEASE_WAKELOCK = 107;
148
149 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
150
151 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800152 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700153
154 private static final int EMPTY_MENU = -1;
155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700157 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 final static int PREFERENCES_PAGE = 3;
159 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700161 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800162 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000163
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
165
166 // As the ids are dynamically created, we can't guarantee that they will
167 // be in sequence, so this static array maps ids to a window number.
168 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
169 { R.id.window_one_menu_id, R.id.window_two_menu_id,
170 R.id.window_three_menu_id, R.id.window_four_menu_id,
171 R.id.window_five_menu_id, R.id.window_six_menu_id,
172 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
173
174 // "source" parameter for Google search through search key
175 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
176 // "source" parameter for Google search through simplily type
177 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
178
George Mount387d45d2011-10-07 15:57:53 -0700179 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700180 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
181
John Reckd7dd9b22011-08-30 09:18:29 -0700182 // A bitmap that is re-used in createScreenshot as scratch space
183 private static Bitmap sThumbnailBitmap;
184
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 private Activity mActivity;
186 private UI mUi;
187 private TabControl mTabControl;
188 private BrowserSettings mSettings;
189 private WebViewFactory mFactory;
190
191 private WakeLock mWakeLock;
192
193 private UrlHandler mUrlHandler;
194 private UploadHandler mUploadHandler;
195 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 private PageDialogsHandler mPageDialogsHandler;
197 private NetworkStateHandler mNetworkHandler;
198
Ben Murdoch8029a772010-11-16 11:58:21 +0000199 private Message mAutoFillSetupMessage;
200
Michael Kolb8233fac2010-10-26 16:08:53 -0700201 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800202 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700203
204 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
205
206 // FIXME, temp address onPrepareMenu performance problem.
207 // When we move everything out of view, we should rewrite this.
208 private int mCurrentMenuState = 0;
209 private int mMenuState = R.id.MAIN_MENU;
210 private int mOldMenuState = EMPTY_MENU;
211 private Menu mCachedMenu;
212
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 private boolean mMenuIsDown;
214
215 // For select and find, we keep track of the ActionMode so that
216 // finish() can be called as desired.
217 private ActionMode mActionMode;
218
219 /**
220 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
221 * of whether the configuration has changed. The first onMenuOpened call
222 * after a configuration change is simply a reopening of the same menu
223 * (i.e. mIconView did not change).
224 */
225 private boolean mConfigChanged;
226
227 /**
228 * Keeps track of whether the options menu is open. This is important in
229 * determining whether to show or hide the title bar overlay
230 */
231 private boolean mOptionsMenuOpen;
232
233 /**
234 * Whether or not the options menu is in its bigger, popup menu form. When
235 * true, we want the title bar overlay to be gone. When false, we do not.
236 * Only meaningful if mOptionsMenuOpen is true.
237 */
238 private boolean mExtendedMenuOpen;
239
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 private boolean mActivityPaused = true;
241 private boolean mLoadStopped;
242
243 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500244 // Checks to see when the bookmarks database has changed, and updates the
245 // Tabs' notion of whether they represent bookmarked sites.
246 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700247 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700248
Michael Kolbc3af0672011-08-09 10:24:41 -0700249 private boolean mBlockEvents;
250
Michael Kolb0b129122012-06-04 16:31:58 -0700251 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800252 private boolean mUpdateMyNavThumbnail;
253 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700254
George Mount3636d0a2011-11-21 09:08:21 -0800255 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700256 mActivity = browser;
257 mSettings = BrowserSettings.getInstance();
258 mTabControl = new TabControl(this);
259 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700260 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800261 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700262 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700263
264 mUrlHandler = new UrlHandler(this);
265 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700266 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
267
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800268 // Creating dummy Webview for browser to force loading of library;
269 // in order for CookieManager calls to be invoked properly and
270 // awBrowserContext to be initialized
271 (mFactory.createWebView(false)).destroy();
272
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500274 mBookmarksObserver = new ContentObserver(mHandler) {
275 @Override
276 public void onChange(boolean selfChange) {
277 int size = mTabControl.getTabCount();
278 for (int i = 0; i < size; i++) {
279 mTabControl.getTab(i).updateBookmarkedStatus();
280 }
281 }
282
283 };
284 browser.getContentResolver().registerContentObserver(
285 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700286
287 mNetworkHandler = new NetworkStateHandler(mActivity, this);
288 // Start watching the default geolocation permissions
289 mSystemAllowGeolocationOrigins =
290 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
291 mSystemAllowGeolocationOrigins.start();
292
John Reckaf262e72011-07-25 13:55:44 -0700293 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700294 }
295
John Reck9c35b9c2012-05-30 10:08:50 -0700296 @Override
297 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800298 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800299 // mCrashRecoverHandler has any previously saved state.
300 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700301 }
302
George Mount3636d0a2011-11-21 09:08:21 -0800303 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700304 // Unless the last browser usage was within 24 hours, destroy any
305 // remaining incognito tabs.
306
307 Calendar lastActiveDate = icicle != null ?
308 (Calendar) icicle.getSerializable("lastActiveDate") : null;
309 Calendar today = Calendar.getInstance();
310 Calendar yesterday = Calendar.getInstance();
311 yesterday.add(Calendar.DATE, -1);
312
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800313 // we dont want to ever recover incognito tabs
314 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700315
Patrick Scott7d50a932011-02-04 09:27:26 -0500316 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700317 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500318 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000319
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800320 mSettings.initializeCookieSettings();
Michael Kolbc831b632011-05-11 09:30:34 -0700321 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500322 // Not able to restore so we go ahead and clear session cookies. We
323 // must do this before trying to login the user as we don't want to
324 // clear any session cookies set during login.
325 CookieManager.getInstance().removeSessionCookie();
326 }
327
Patrick Scottd43e75a2011-03-14 14:47:23 -0400328 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500329 new Runnable() {
330 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800331 onPreloginFinished(icicle, intent, currentTabId,
332 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500333 }
334 });
335 }
336
Michael Kolbc831b632011-05-11 09:30:34 -0700337 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800338 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700339 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700340 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800341 if (intent == null) {
342 // This won't happen under common scenarios. The icicle is
343 // not null, but there aren't any tabs to restore.
344 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700345 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800346 final Bundle extra = intent.getExtras();
347 // Create an initial tab.
348 // If the intent is ACTION_VIEW and data is not null, the Browser is
349 // invoked to view the content by another application. In this case,
350 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800351 UrlData urlData = null;
352 if (intent.getData() != null
353 && Intent.ACTION_VIEW.equals(intent.getAction())
354 && intent.getData().toString().startsWith("content://")) {
355 urlData = new UrlData(intent.getData().toString());
356 } else {
357 urlData = IntentHandler.getUrlDataFromIntent(intent);
358 }
George Mount3636d0a2011-11-21 09:08:21 -0800359 Tab t = null;
360 if (urlData.isEmpty()) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800361 Object[] params = { new String("persist.env.c.browser.resource"),
362 new String("default")};
363 Class[] type = new Class[] {String.class, String.class};
364 String browserRes = (String)ReflectHelper.invokeStaticMethod(
365 "android.os.SystemProperties", "get",
366 type, params);
367 if (browserRes.equals(
kaiyiz6e5b3e02013-08-19 20:02:01 +0800368 "cmcc")) {
369 t = openTab(OFFLINE_PAGE, false, true, true);
370 } else {
371 t = openTabToHomePage();
372 }
George Mount3636d0a2011-11-21 09:08:21 -0800373 } else {
374 t = openTab(urlData);
375 }
376 if (t != null) {
377 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
378 }
379 WebView webView = t.getWebView();
380 if (extra != null) {
381 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
382 if (scale > 0 && scale <= 1000) {
383 webView.setInitialScale(scale);
384 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700385 }
386 }
John Reckd8c74522011-06-14 08:45:00 -0700387 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700389 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500390 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700391 List<Tab> tabs = mTabControl.getTabs();
392 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
393 for (Tab t : tabs) {
394 restoredTabs.add(t.getId());
395 }
396 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700397 if (tabs.size() == 0) {
398 openTabToHomePage();
399 }
John Reck1cf4b792011-07-26 10:22:22 -0700400 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 // TabControl.restoreState() will create a new tab even if
402 // restoring the state fails.
403 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800404 // Intent is non-null when framework thinks the browser should be
405 // launching with a new intent (icicle is null).
406 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700407 mIntentHandler.onNewIntent(intent);
408 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700409 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700410 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700411 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800412 if (jsFlags.trim().length() != 0) {
413 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700414 }
George Mount3636d0a2011-11-21 09:08:21 -0800415 if (intent != null
416 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700417 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800418 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700419 }
420
John Reck1cf4b792011-07-26 10:22:22 -0700421 private static class PruneThumbnails implements Runnable {
422 private Context mContext;
423 private List<Long> mIds;
424
425 PruneThumbnails(Context context, List<Long> preserveIds) {
426 mContext = context.getApplicationContext();
427 mIds = preserveIds;
428 }
429
430 @Override
431 public void run() {
432 ContentResolver cr = mContext.getContentResolver();
433 if (mIds == null || mIds.size() == 0) {
434 cr.delete(Thumbnails.CONTENT_URI, null, null);
435 } else {
436 int length = mIds.size();
437 StringBuilder where = new StringBuilder();
438 where.append(Thumbnails._ID);
439 where.append(" not in (");
440 for (int i = 0; i < length; i++) {
441 where.append(mIds.get(i));
442 if (i < (length - 1)) {
443 where.append(",");
444 }
445 }
446 where.append(")");
447 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
448 }
449 }
450
451 }
452
Michael Kolb1514bb72010-11-22 09:11:48 -0800453 @Override
454 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700455 return mFactory;
456 }
457
458 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800459 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800460 if (tab.hasCrashed)
461 tab.showCrashView();
462 else
463 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800464 }
465
466 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800467 public void createSubWindow(Tab tab) {
468 endActionMode();
469 WebView mainView = tab.getWebView();
470 WebView subView = mFactory.createWebView((mainView == null)
471 ? false
472 : mainView.isPrivateBrowsingEnabled());
473 mUi.createSubWindow(tab, subView);
474 }
475
476 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700477 public Context getContext() {
478 return mActivity;
479 }
480
481 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700482 public Activity getActivity() {
483 return mActivity;
484 }
485
486 void setUi(UI ui) {
487 mUi = ui;
488 }
489
Michael Kolbaf63dba2012-05-16 12:58:05 -0700490 @Override
491 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700492 return mSettings;
493 }
494
495 IntentHandler getIntentHandler() {
496 return mIntentHandler;
497 }
498
499 @Override
500 public UI getUi() {
501 return mUi;
502 }
503
504 int getMaxTabs() {
505 return mActivity.getResources().getInteger(R.integer.max_tabs);
506 }
507
508 @Override
509 public TabControl getTabControl() {
510 return mTabControl;
511 }
512
Michael Kolb1bf23132010-11-19 12:55:12 -0800513 @Override
514 public List<Tab> getTabs() {
515 return mTabControl.getTabs();
516 }
517
John Reckaf262e72011-07-25 13:55:44 -0700518 // Open the icon database.
519 private void openIconDatabase() {
520 // We have to call getInstance on the UI thread
521 final WebIconDatabase instance = WebIconDatabase.getInstance();
522 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000523
John Reckaf262e72011-07-25 13:55:44 -0700524 @Override
525 public void run() {
526 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 }
John Reckaf262e72011-07-25 13:55:44 -0700528 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700529 }
530
531 private void startHandler() {
532 mHandler = new Handler() {
533
534 @Override
535 public void handleMessage(Message msg) {
536 switch (msg.what) {
537 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700538 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 break;
540 case FOCUS_NODE_HREF:
541 {
542 String url = (String) msg.getData().get("url");
543 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500544 String src = (String) msg.getData().get("src");
545 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 if (TextUtils.isEmpty(url)) {
547 break;
548 }
549 HashMap focusNodeMap = (HashMap) msg.obj;
550 WebView view = (WebView) focusNodeMap.get("webview");
551 // Only apply the action if the top window did not change.
552 if (getCurrentTopWebView() != view) {
553 break;
554 }
555 switch (msg.arg1) {
556 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700557 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700558 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500559 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700560 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500561 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500562 case R.id.open_newtab_context_menu_id:
563 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700564 openTab(url, parent,
565 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500566 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700567 case R.id.copy_link_context_menu_id:
568 copy(url);
569 break;
570 case R.id.save_link_context_menu_id:
571 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500572 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800573 mActivity, url, view.getSettings().getUserAgentString(),
574 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700575 break;
576 }
577 break;
578 }
579
580 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700581 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700582 break;
583
584 case STOP_LOAD:
585 stopLoading();
586 break;
587
588 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700589 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 mWakeLock.release();
591 // if we reach here, Browser should be still in the
592 // background loading after WAKELOCK_TIMEOUT (5-min).
593 // To avoid burning the battery, stop loading.
594 mTabControl.stopAllLoading();
595 }
596 break;
597
598 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800599 Tab tab = (Tab) msg.obj;
600 if (tab != null) {
601 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700602 }
603 break;
kaiyiz591110b2013-08-06 17:11:06 +0800604 case OPEN_MENU:
605 if (!mOptionsMenuOpen && mActivity != null ) {
606 mActivity.openOptionsMenu();
607 }
608 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700609 }
610 }
611 };
612
613 }
614
John Reckef654f12011-07-12 16:42:08 -0700615 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200616 public Tab getCurrentTab() {
617 return mTabControl.getCurrentTab();
618 }
619
Michael Kolbba99c5d2010-11-29 14:57:41 -0800620 @Override
621 public void shareCurrentPage() {
622 shareCurrentPage(mTabControl.getCurrentTab());
623 }
624
625 private void shareCurrentPage(Tab tab) {
626 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800627 sharePage(mActivity, tab.getTitle(),
628 tab.getUrl(), tab.getFavicon(),
629 createScreenshot(tab.getWebView(),
630 getDesiredThumbnailWidth(mActivity),
631 getDesiredThumbnailHeight(mActivity)));
632 }
633 }
634
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 /**
636 * Share a page, providing the title, url, favicon, and a screenshot. Uses
637 * an {@link Intent} to launch the Activity chooser.
638 * @param c Context used to launch a new Activity.
639 * @param title Title of the page. Stored in the Intent with
640 * {@link Intent#EXTRA_SUBJECT}
641 * @param url URL of the page. Stored in the Intent with
642 * {@link Intent#EXTRA_TEXT}
643 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
644 * with {@link Browser#EXTRA_SHARE_FAVICON}
645 * @param screenshot Bitmap of a screenshot of the page. Stored in the
646 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
647 */
648 static final void sharePage(Context c, String title, String url,
649 Bitmap favicon, Bitmap screenshot) {
650 Intent send = new Intent(Intent.ACTION_SEND);
651 send.setType("text/plain");
652 send.putExtra(Intent.EXTRA_TEXT, url);
653 send.putExtra(Intent.EXTRA_SUBJECT, title);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800654 send.putExtra(EXTRA_SHARE_FAVICON, favicon);
655 send.putExtra(EXTRA_SHARE_SCREENSHOT, screenshot);
Michael Kolb8233fac2010-10-26 16:08:53 -0700656 try {
657 c.startActivity(Intent.createChooser(send, c.getString(
658 R.string.choosertitle_sharevia)));
659 } catch(android.content.ActivityNotFoundException ex) {
660 // if no app handles it, do nothing
661 }
662 }
663
664 private void copy(CharSequence text) {
665 ClipboardManager cm = (ClipboardManager) mActivity
666 .getSystemService(Context.CLIPBOARD_SERVICE);
667 cm.setText(text);
668 }
669
670 // lifecycle
671
John Reck9c35b9c2012-05-30 10:08:50 -0700672 @Override
673 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700674 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800675 // update the menu in case of a locale change
676 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800677 if (mOptionsMenuOpen) {
678 mActivity.closeOptionsMenu();
679 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
680 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700681 if (mPageDialogsHandler != null) {
682 mPageDialogsHandler.onConfigurationChanged(config);
683 }
684 mUi.onConfigurationChanged(config);
685 }
686
687 @Override
688 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700689 if (!mUi.isWebShowing()) {
690 mUi.showWeb(false);
691 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 mIntentHandler.onNewIntent(intent);
693 }
694
John Reck9c35b9c2012-05-30 10:08:50 -0700695 @Override
696 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800697 if (mUi.isCustomViewShowing()) {
698 hideCustomView();
699 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700700 if (mActivityPaused) {
701 Log.e(LOGTAG, "BrowserActivity is already paused.");
702 return;
703 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800705 Tab tab = mTabControl.getCurrentTab();
706 if (tab != null) {
707 tab.pause();
708 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700709 if (mWakeLock == null) {
710 PowerManager pm = (PowerManager) mActivity
711 .getSystemService(Context.POWER_SERVICE);
712 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
713 }
Michael Kolb70976932010-11-30 11:34:01 -0800714 mWakeLock.acquire();
715 mHandler.sendMessageDelayed(mHandler
716 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
717 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700718 }
719 mUi.onPause();
720 mNetworkHandler.onPause();
721
722 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100723 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700724 if (sThumbnailBitmap != null) {
725 sThumbnailBitmap.recycle();
726 sThumbnailBitmap = null;
727 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700728 }
729
John Reck9c35b9c2012-05-30 10:08:50 -0700730 @Override
731 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700732 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800733 Bundle saveState = createSaveState();
734
735 // crash recovery manages all save & restore state
736 mCrashRecoveryHandler.writeState(saveState);
737 mSettings.setLastRunPaused(true);
738 }
739
740 /**
741 * Save the current state to outState. Does not write the state to
742 * disk.
743 * @return Bundle containing the current state of all tabs.
744 */
745 /* package */ Bundle createSaveState() {
746 Bundle saveState = new Bundle();
747 mTabControl.saveState(saveState);
748 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700749 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800750 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700751 }
George Mount3636d0a2011-11-21 09:08:21 -0800752 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 }
754
John Reck9c35b9c2012-05-30 10:08:50 -0700755 @Override
756 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700757 if (!mActivityPaused) {
758 Log.e(LOGTAG, "BrowserActivity is already resumed.");
759 return;
760 }
George Mount3636d0a2011-11-21 09:08:21 -0800761 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800763 Tab current = mTabControl.getCurrentTab();
764 if (current != null) {
765 current.resume();
766 resumeWebViewTimers(current);
767 }
John Reckf57c0292011-07-21 18:15:39 -0700768 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200769
Michael Kolb8233fac2010-10-26 16:08:53 -0700770 mUi.onResume();
771 mNetworkHandler.onResume();
772 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100773 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700774 if (mVoiceResult != null) {
775 mUi.onVoiceResult(mVoiceResult);
776 mVoiceResult = null;
777 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800778 if (current != null && current.hasCrashed) {
779 current.showCrashView();
780 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700781 }
782
John Reckf57c0292011-07-21 18:15:39 -0700783 private void releaseWakeLock() {
784 if (mWakeLock != null && mWakeLock.isHeld()) {
785 mHandler.removeMessages(RELEASE_WAKELOCK);
786 mWakeLock.release();
787 }
788 }
789
Michael Kolb70976932010-11-30 11:34:01 -0800790 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800791 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800792 * @param tab guaranteed non-null
793 */
794 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 boolean inLoad = tab.inPageLoad();
796 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
797 CookieSyncManager.getInstance().startSync();
798 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100799 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 }
801 }
802
Michael Kolb70976932010-11-30 11:34:01 -0800803 /**
804 * Pause all WebView timers using the WebView of the given tab
805 * @param tab
806 * @return true if the timers are paused or tab is null
807 */
808 private boolean pauseWebViewTimers(Tab tab) {
809 if (tab == null) {
810 return true;
811 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700812 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100813 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700815 }
Michael Kolb70976932010-11-30 11:34:01 -0800816 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700817 }
818
John Reck9c35b9c2012-05-30 10:08:50 -0700819 @Override
820 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800821 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700822 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
823 mUploadHandler = null;
824 }
825 if (mTabControl == null) return;
826 mUi.onDestroy();
827 // Remove the current tab and sub window
828 Tab t = mTabControl.getCurrentTab();
829 if (t != null) {
830 dismissSubWindow(t);
831 removeTab(t);
832 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500833 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700834 // Destroy all the tabs
835 mTabControl.destroy();
836 WebIconDatabase.getInstance().close();
837 // Stop watching the default geolocation permissions
838 mSystemAllowGeolocationOrigins.stop();
839 mSystemAllowGeolocationOrigins = null;
840 }
841
842 protected boolean isActivityPaused() {
843 return mActivityPaused;
844 }
845
John Reck9c35b9c2012-05-30 10:08:50 -0700846 @Override
847 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700848 mTabControl.freeMemory();
849 }
850
851 @Override
852 public boolean shouldShowErrorConsole() {
853 return mShouldShowErrorConsole;
854 }
855
856 protected void setShouldShowErrorConsole(boolean show) {
857 if (show == mShouldShowErrorConsole) {
858 // Nothing to do.
859 return;
860 }
861 mShouldShowErrorConsole = show;
862 Tab t = mTabControl.getCurrentTab();
863 if (t == null) {
864 // There is no current tab so we cannot toggle the error console
865 return;
866 }
867 mUi.setShouldShowErrorConsole(t, show);
868 }
869
870 @Override
871 public void stopLoading() {
872 mLoadStopped = true;
873 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700874 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700875 if (w != null) {
876 w.stopLoading();
877 mUi.onPageStopped(tab);
878 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 }
880
881 boolean didUserStopLoading() {
882 return mLoadStopped;
883 }
884
kaiyiza016da12013-08-26 17:50:22 +0800885 private void handleNetworkNotify(WebView view) {
886 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
887 Context.CONNECTIVITY_SERVICE);
888 WifiManager wifiMgr = (WifiManager) this.getContext()
889 .getSystemService(Context.WIFI_SERVICE);
890 int networkSwitchTypeOK = this.getContext().getResources()
891 .getInteger(R.integer.netswitch_type_remind);
892
893 if (wifiMgr.isWifiEnabled()) {
894 NetworkInfo mNetworkInfo = conMgr.getActiveNetworkInfo();
895 if (mNetworkInfo == null
896 || (mNetworkInfo != null && (mNetworkInfo.getType() !=
897 ConnectivityManager.TYPE_WIFI))) {
kaiyizb7bf1f22013-10-02 11:42:23 +0800898 List<ScanResult> list = wifiMgr.getScanResults();
899 if (list != null && list.size() == 0) {
900 int isReminder = Settings.System.getInt(
901 mActivity.getContentResolver(),
902 this.getContext().getResources()
903 .getString(R.string.network_switch_remind_type),
904 networkSwitchTypeOK);
905 if (isReminder == networkSwitchTypeOK) {
906 Intent intent = new Intent(
907 INTENT_WIFI_SELECTION_DATA_CONNECTION);
908 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
909 this.getContext().startActivity(intent);
910 }
911 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800912 if ((Boolean)ReflectHelper.invokeStaticMethod(
913 "ActivityManagerNative", "isSystemReady", null, null)) {
kaiyizb7bf1f22013-10-02 11:42:23 +0800914 try {
915 Intent intent = new Intent(INTENT_PICK_NETWORK);
916 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
917 this.getContext().startActivity(intent);
918 } catch (Exception e) {
919 String err_msg = this.getContext().getString(
920 R.string.acivity_not_found, INTENT_PICK_NETWORK);
921 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
922 }
923 } else {
924 Log.e(LOGTAG, "System is not ready!");
925 }
kaiyiza016da12013-08-26 17:50:22 +0800926 }
927 mNetworkShouldNotify = false;
928 }
929 } else {
930 if (!mNetworkHandler.isNetworkUp()) {
931 view.setNetworkAvailable(false);
932 Log.v(LOGTAG, "handleNetworkNotify() Wlan is not enabled.");
933 }
934 }
935 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 // WebViewController
937
938 @Override
John Reck324d4402011-01-11 16:56:42 -0800939 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700940
941 // We've started to load a new page. If there was a pending message
942 // to save a screenshot then we will now take the new page and save
943 // an incorrect screenshot. Therefore, remove any pending thumbnail
944 // messages from the queue.
945 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800946 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700947
948 // reset sync timer to avoid sync starts during loading a page
949 CookieSyncManager.getInstance().resetSync();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800950 Object[] params = {new String(PROP_NETSWITCH),
951 new Boolean(false)};
952 Class[] type = new Class[] {String.class, boolean.class};
953 Boolean result = (Boolean) ReflectHelper.invokeStaticMethod(
954 "android.os.SystemProperties", "getBoolean",
955 type, params);
956 if (result) {
kaiyiza016da12013-08-26 17:50:22 +0800957 if (!mNetworkHandler.isNetworkUp()) {
958 Log.d(LOGTAG, "onPageStarted() network unavailable");
959 if (mNetworkShouldNotify) {
960 handleNetworkNotify(view);
961 } else {
962 view.setNetworkAvailable(false);
963 }
964 mNetworkShouldNotify = false;
965 } else {
966 Log.d(LOGTAG, "onPageStarted() network available");
967 if (mNetworkShouldNotify) {
968 handleNetworkNotify(view);
969 }
970 mNetworkShouldNotify = false;
971 }
972 } else {
973 if (!mNetworkHandler.isNetworkUp()) {
974 view.setNetworkAvailable(false);
975 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 }
977
978 // when BrowserActivity just starts, onPageStarted may be called before
979 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
980 // to start the timer. As we won't switch tabs while an activity is in
981 // pause state, we can ensure calling resume and pause in pair.
982 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800983 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700984 }
985 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 endActionMode();
987
John Reck30c714c2010-12-16 17:30:34 -0800988 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700989
John Reck324d4402011-01-11 16:56:42 -0800990 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700991 // update the bookmark database for favicon
992 maybeUpdateFavicon(tab, null, url, favicon);
993
994 Performance.tracePageStart(url);
995
996 // Performance probe
997 if (false) {
998 Performance.onPageStarted();
999 }
1000
1001 }
1002
1003 @Override
John Reck324d4402011-01-11 16:56:42 -08001004 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -07001005 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -08001006 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001007
Michael Kolb8233fac2010-10-26 16:08:53 -07001008 // Performance probe
1009 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001010 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001011 }
1012
1013 Performance.tracePageFinished();
1014 }
1015
1016 @Override
John Reck30c714c2010-12-16 17:30:34 -08001017 public void onProgressChanged(Tab tab) {
1018 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001019
1020 if (newProgress == 100) {
1021 CookieSyncManager.getInstance().sync();
1022 // onProgressChanged() may continue to be called after the main
1023 // frame has finished loading, as any remaining sub frames continue
1024 // to load. We'll only get called once though with newProgress as
1025 // 100 when everything is loaded. (onPageFinished is called once
1026 // when the main frame completes loading regardless of the state of
1027 // any sub frames so calls to onProgressChanges may continue after
1028 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001029 if (tab.inPageLoad()) {
1030 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +02001031 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
1032 // pause the WebView timer and release the wake lock if it is
1033 // finished while BrowserActivity is in pause state.
1034 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001035 }
John Reckd9862372012-02-21 15:04:50 -08001036 if (!tab.isPrivateBrowsingEnabled()
1037 && !TextUtils.isEmpty(tab.getUrl())
1038 && !tab.isSnapshot()) {
1039 // Only update the bookmark screenshot if the user did not
1040 // cancel the load early and there is not already
1041 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001042 if (tab.shouldUpdateThumbnail() &&
1043 (tab.inForeground() && !didUserStopLoading()
1044 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001045 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1046 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1047 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001048 1500);
John Reckd9862372012-02-21 15:04:50 -08001049 }
1050 }
1051 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001053 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001054 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001055 // still loading
1056 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001057 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001058 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001059 }
1060 }
John Reck30c714c2010-12-16 17:30:34 -08001061 mUi.onProgressChanged(tab);
1062 }
1063
1064 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001065 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001066 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001067 }
1068
1069 @Override
1070 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001071 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001072 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001073 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001074 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1075 return;
1076 }
1077 // Update the title in the history database if not in private browsing mode
1078 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001079 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001080 }
1081 }
1082
1083 @Override
1084 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001085 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001086 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1087 }
1088
1089 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001090 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1091 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 }
1093
1094 @Override
1095 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1096 if (mMenuIsDown) {
1097 // only check shortcut key when MENU is held
1098 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1099 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001100 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001101 int keyCode = event.getKeyCode();
1102 // We need to send almost every key to WebKit. However:
1103 // 1. We don't want to block the device on the renderer for
1104 // some keys like menu, home, call.
1105 // 2. There are no WebKit equivalents for some of these keys
1106 // (see app/keyboard_codes_win.h)
1107 // Note that these are not the same set as KeyEvent.isSystemKey:
1108 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1109 if (keyCode == KeyEvent.KEYCODE_MENU ||
1110 keyCode == KeyEvent.KEYCODE_HOME ||
1111 keyCode == KeyEvent.KEYCODE_BACK ||
1112 keyCode == KeyEvent.KEYCODE_CALL ||
1113 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1114 keyCode == KeyEvent.KEYCODE_POWER ||
1115 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1116 keyCode == KeyEvent.KEYCODE_CAMERA ||
1117 keyCode == KeyEvent.KEYCODE_FOCUS ||
1118 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1119 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1120 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1121 return true;
1122 }
1123
1124 // We also have to intercept some shortcuts before we send them to the ContentView.
1125 if (event.isCtrlPressed() && (
1126 keyCode == KeyEvent.KEYCODE_TAB ||
1127 keyCode == KeyEvent.KEYCODE_W ||
1128 keyCode == KeyEvent.KEYCODE_F4)) {
1129 return true;
1130 }
1131
1132 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001133 }
1134
1135 @Override
John Reck997b1b72012-04-19 18:08:25 -07001136 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 if (!isActivityPaused()) {
1138 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001139 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001140 } else {
John Reck997b1b72012-04-19 18:08:25 -07001141 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001142 }
1143 }
John Reck997b1b72012-04-19 18:08:25 -07001144 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 }
1146
1147 @Override
John Reck324d4402011-01-11 16:56:42 -08001148 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001149 // Don't save anything in private browsing mode
1150 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -08001151 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001152
John Reck324d4402011-01-11 16:56:42 -08001153 if (TextUtils.isEmpty(url)
1154 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 return;
1156 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001157
John Reckf57c0292011-07-21 18:15:39 -07001158 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001159 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 }
1161
1162 @Override
1163 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1164 AsyncTask<Void, Void, String[]> task =
1165 new AsyncTask<Void, Void, String[]>() {
1166 @Override
1167 public String[] doInBackground(Void... unused) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001168 Object[] params = {mActivity.getContentResolver()};
1169 Class[] type = new Class[] {ContentResolver.class};
1170 return (String[])ReflectHelper.invokeStaticMethod(
1171 "android.provider.Browser","getVisitedHistory",
1172 type, params);
Michael Kolb8233fac2010-10-26 16:08:53 -07001173 }
1174 @Override
1175 public void onPostExecute(String[] result) {
1176 callback.onReceiveValue(result);
1177 }
1178 };
1179 task.execute();
1180 }
1181
1182 @Override
1183 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1184 final HttpAuthHandler handler, final String host,
1185 final String realm) {
1186 String username = null;
1187 String password = null;
1188
1189 boolean reuseHttpAuthUsernamePassword
1190 = handler.useHttpAuthUsernamePassword();
1191
1192 if (reuseHttpAuthUsernamePassword && view != null) {
1193 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1194 if (credentials != null && credentials.length == 2) {
1195 username = credentials[0];
1196 password = credentials[1];
1197 }
1198 }
1199
1200 if (username != null && password != null) {
1201 handler.proceed(username, password);
1202 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001203 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001204 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1205 } else {
1206 handler.cancel();
1207 }
1208 }
1209 }
1210
1211 @Override
1212 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001213 String contentDisposition, String mimetype, String referer,
1214 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001215 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001216 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001217 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001218 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 // This Tab was opened for the sole purpose of downloading a
1220 // file. Remove it.
1221 if (tab == mTabControl.getCurrentTab()) {
1222 // In this case, the Tab is still on top.
1223 goBackOnePageOrQuit();
1224 } else {
1225 // In this case, it is not.
1226 closeTab(tab);
1227 }
1228 }
1229 }
1230
1231 @Override
1232 public Bitmap getDefaultVideoPoster() {
1233 return mUi.getDefaultVideoPoster();
1234 }
1235
1236 @Override
1237 public View getVideoLoadingProgressView() {
1238 return mUi.getVideoLoadingProgressView();
1239 }
1240
1241 @Override
1242 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1243 SslError error) {
1244 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1245 }
1246
Patrick Scott92066772011-03-10 08:46:27 -05001247 @Override
1248 public void showAutoLogin(Tab tab) {
1249 assert tab.inForeground();
1250 // Update the title bar to show the auto-login request.
1251 mUi.showAutoLogin(tab);
1252 }
1253
1254 @Override
1255 public void hideAutoLogin(Tab tab) {
1256 assert tab.inForeground();
1257 mUi.hideAutoLogin(tab);
1258 }
1259
Michael Kolb8233fac2010-10-26 16:08:53 -07001260 // helper method
1261
1262 /*
1263 * Update the favorites icon if the private browsing isn't enabled and the
1264 * icon is valid.
1265 */
1266 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1267 final String url, Bitmap favicon) {
1268 if (favicon == null) {
1269 return;
1270 }
1271 if (!tab.isPrivateBrowsingEnabled()) {
1272 Bookmarks.updateFavicon(mActivity
1273 .getContentResolver(), originalUrl, url, favicon);
1274 }
1275 }
1276
Leon Scroggins4cd97792010-12-03 15:31:56 -05001277 @Override
1278 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001279 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001280 mUi.bookmarkedStatusHasChanged(tab);
1281 }
1282
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 // end WebViewController
1284
1285 protected void pageUp() {
1286 getCurrentTopWebView().pageUp(false);
1287 }
1288
1289 protected void pageDown() {
1290 getCurrentTopWebView().pageDown(false);
1291 }
1292
1293 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001294 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 public void editUrl() {
1296 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001297 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001298 }
1299
John Reck9c35b9c2012-05-30 10:08:50 -07001300 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001301 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001302 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001303 if (tab.inForeground()) {
1304 if (mUi.isCustomViewShowing()) {
1305 callback.onCustomViewHidden();
1306 return;
1307 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001308 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001309 // Save the menu state and set it to empty while the custom
1310 // view is showing.
1311 mOldMenuState = mMenuState;
1312 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001313 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001314 }
1315 }
1316
1317 @Override
1318 public void hideCustomView() {
1319 if (mUi.isCustomViewShowing()) {
1320 mUi.onHideCustomView();
1321 // Reset the old menu state.
1322 mMenuState = mOldMenuState;
1323 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001324 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001325 }
1326 }
1327
John Reck9c35b9c2012-05-30 10:08:50 -07001328 @Override
1329 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001330 Intent intent) {
1331 if (getCurrentTopWebView() == null) return;
1332 switch (requestCode) {
1333 case PREFERENCES_PAGE:
1334 if (resultCode == Activity.RESULT_OK && intent != null) {
1335 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001336 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 mTabControl.removeParentChildRelationShips();
1338 }
1339 }
1340 break;
1341 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001342 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001343 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001344 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001345 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001346 case AUTOFILL_SETUP:
1347 // Determine whether a profile was actually set up or not
1348 // and if so, send the message back to the WebTextView to
1349 // fill the form with the new profile.
1350 if (getSettings().getAutoFillProfile() != null) {
1351 mAutoFillSetupMessage.sendToTarget();
1352 mAutoFillSetupMessage = null;
1353 }
1354 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001355 case COMBO_VIEW:
1356 if (intent == null || resultCode != Activity.RESULT_OK) {
1357 break;
1358 }
John Reck3ba45532011-08-11 16:26:53 -07001359 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001360 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1361 Tab t = getCurrentTab();
1362 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001363 mUpdateMyNavThumbnail = true;
1364 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001365 loadUrl(t, uri.toString());
1366 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1367 String[] urls = intent.getStringArrayExtra(
1368 ComboViewActivity.EXTRA_OPEN_ALL);
1369 Tab parent = getCurrentTab();
1370 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001371 if (url != null) {
1372 parent = openTab(url, parent,
1373 !mSettings.openInBackground(), true);
1374 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001375 }
1376 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1377 long id = intent.getLongExtra(
1378 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1379 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001380 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001381 }
1382 }
1383 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001384 case VOICE_RESULT:
1385 if (resultCode == Activity.RESULT_OK && intent != null) {
1386 ArrayList<String> results = intent.getStringArrayListExtra(
1387 RecognizerIntent.EXTRA_RESULTS);
1388 if (results.size() >= 1) {
1389 mVoiceResult = results.get(0);
1390 }
1391 }
1392 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001393 case MY_NAVIGATION:
1394 if (intent == null || resultCode != Activity.RESULT_OK) {
1395 break;
1396 }
1397
1398 if (intent.getBooleanExtra("need_refresh", false) &&
1399 getCurrentTopWebView() != null) {
1400 getCurrentTopWebView().reload();
1401 }
1402 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001403 default:
1404 break;
1405 }
1406 getCurrentTopWebView().requestFocus();
1407 }
1408
1409 /**
1410 * Open the Go page.
1411 * @param startWithHistory If true, open starting on the history tab.
1412 * Otherwise, start with the bookmarks tab.
1413 */
1414 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001415 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 if (mTabControl.getCurrentWebView() == null) {
1417 return;
1418 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001419 // clear action mode
1420 if (isInCustomActionMode()) {
1421 endActionMode();
1422 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 Bundle extras = new Bundle();
1424 // Disable opening in a new window if we have maxed out the windows
1425 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1426 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001427 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001428 }
1429
1430 // combo view callbacks
1431
Michael Kolb8233fac2010-10-26 16:08:53 -07001432 // key handling
1433 protected void onBackKey() {
1434 if (!mUi.onBackKey()) {
1435 WebView subwindow = mTabControl.getCurrentSubWindow();
1436 if (subwindow != null) {
1437 if (subwindow.canGoBack()) {
1438 subwindow.goBack();
1439 } else {
1440 dismissSubWindow(mTabControl.getCurrentTab());
1441 }
1442 } else {
1443 goBackOnePageOrQuit();
1444 }
1445 }
1446 }
1447
Michael Kolb4bd767d2011-05-27 11:33:55 -07001448 protected boolean onMenuKey() {
1449 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001450 }
1451
Michael Kolb8233fac2010-10-26 16:08:53 -07001452 // menu handling and state
1453 // TODO: maybe put into separate handler
1454
John Reck9c35b9c2012-05-30 10:08:50 -07001455 @Override
1456 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001457 if (mMenuState == EMPTY_MENU) {
1458 return false;
1459 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001460 MenuInflater inflater = mActivity.getMenuInflater();
1461 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 return true;
1463 }
1464
John Reck9c35b9c2012-05-30 10:08:50 -07001465 @Override
1466 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001467 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001468 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001469 return;
1470 }
1471 if (!(v instanceof WebView)) {
1472 return;
1473 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001474 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001475 WebView.HitTestResult result = webview.getHitTestResult();
1476 if (result == null) {
1477 return;
1478 }
1479
1480 int type = result.getType();
1481 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1482 Log.w(LOGTAG,
1483 "We should not show context menu when nothing is touched");
1484 return;
1485 }
1486 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1487 // let TextView handles context menu
1488 return;
1489 }
1490
1491 // Note, http://b/issue?id=1106666 is requesting that
1492 // an inflated menu can be used again. This is not available
1493 // yet, so inflate each time (yuk!)
1494 MenuInflater inflater = mActivity.getMenuInflater();
1495 inflater.inflate(R.menu.browsercontext, menu);
1496
1497 // Show the correct menu group
1498 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001499 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001500 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001501 menu.setGroupVisible(R.id.PHONE_MENU,
1502 type == WebView.HitTestResult.PHONE_TYPE);
1503 menu.setGroupVisible(R.id.EMAIL_MENU,
1504 type == WebView.HitTestResult.EMAIL_TYPE);
1505 menu.setGroupVisible(R.id.GEO_MENU,
1506 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001507 String itemUrl = null;
1508 String url = webview.getOriginalUrl();
1509 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1510 itemUrl = Uri.decode(navigationUrl);
1511 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1512 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1513 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1514 } else {
1515 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1516 }
1517 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1518 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1519 } else {
1520 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1521
1522 menu.setGroupVisible(R.id.IMAGE_MENU,
1523 type == WebView.HitTestResult.IMAGE_TYPE
1524 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1525 menu.setGroupVisible(R.id.ANCHOR_MENU,
1526 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1527 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1528 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001529 // Setup custom handling depending on the type
1530 switch (type) {
1531 case WebView.HitTestResult.PHONE_TYPE:
1532 menu.setHeaderTitle(Uri.decode(extra));
1533 menu.findItem(R.id.dial_context_menu_id).setIntent(
1534 new Intent(Intent.ACTION_VIEW, Uri
1535 .parse(WebView.SCHEME_TEL + extra)));
1536 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1537 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1538 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1539 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1540 addIntent);
1541 menu.findItem(R.id.copy_phone_context_menu_id)
1542 .setOnMenuItemClickListener(
1543 new Copy(extra));
1544 break;
1545
1546 case WebView.HitTestResult.EMAIL_TYPE:
1547 menu.setHeaderTitle(extra);
1548 menu.findItem(R.id.email_context_menu_id).setIntent(
1549 new Intent(Intent.ACTION_VIEW, Uri
1550 .parse(WebView.SCHEME_MAILTO + extra)));
1551 menu.findItem(R.id.copy_mail_context_menu_id)
1552 .setOnMenuItemClickListener(
1553 new Copy(extra));
1554 break;
1555
1556 case WebView.HitTestResult.GEO_TYPE:
1557 menu.setHeaderTitle(extra);
1558 menu.findItem(R.id.map_context_menu_id).setIntent(
1559 new Intent(Intent.ACTION_VIEW, Uri
1560 .parse(WebView.SCHEME_GEO
1561 + URLEncoder.encode(extra))));
1562 menu.findItem(R.id.copy_geo_context_menu_id)
1563 .setOnMenuItemClickListener(
1564 new Copy(extra));
1565 break;
1566
1567 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1568 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001569 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001570 // decide whether to show the open link in new tab option
1571 boolean showNewTab = mTabControl.canCreateNewTab();
1572 MenuItem newTabItem
1573 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001574 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001575 ? R.string.contextmenu_openlink_newwindow_background
1576 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001577 newTabItem.setVisible(showNewTab);
1578 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001579 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1580 newTabItem.setOnMenuItemClickListener(
1581 new MenuItem.OnMenuItemClickListener() {
1582 @Override
1583 public boolean onMenuItemClick(MenuItem item) {
1584 final HashMap<String, WebView> hrefMap =
1585 new HashMap<String, WebView>();
1586 hrefMap.put("webview", webview);
1587 final Message msg = mHandler.obtainMessage(
1588 FOCUS_NODE_HREF,
1589 R.id.open_newtab_context_menu_id,
1590 0, hrefMap);
1591 webview.requestFocusNodeHref(msg);
1592 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001593 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001594 });
1595 } else {
1596 newTabItem.setOnMenuItemClickListener(
1597 new MenuItem.OnMenuItemClickListener() {
1598 @Override
1599 public boolean onMenuItemClick(MenuItem item) {
1600 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001601 openTab(extra, parent,
1602 !mSettings.openInBackground(),
1603 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001604 return true;
1605 }
1606 });
1607 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001609 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1610 menu.setHeaderTitle(navigationUrl);
1611 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1612
1613 if (itemUrl != null) {
1614 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1615 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1616 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1617 @Override
1618 public boolean onMenuItemClick(MenuItem item) {
1619 final Intent intent = new Intent(Controller.this
1620 .getContext(),
1621 AddMyNavigationPage.class);
1622 Bundle bundle = new Bundle();
1623 String url = Uri.decode(navigationUrl);
1624 bundle.putBoolean("isAdding", false);
1625 bundle.putString("url", url);
1626 bundle.putString("name", getNameFromUrl(url));
1627 intent.putExtra("websites", bundle);
1628 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1629 return false;
1630 }
1631 });
1632 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1633 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1634 @Override
1635 public boolean onMenuItemClick(MenuItem item) {
1636 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1637 return false;
1638 }
1639 });
1640 }
1641 } else {
1642 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1643 }
1644 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001645 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1646 break;
1647 }
1648 // otherwise fall through to handle image part
1649 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001650 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1651 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001652 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1653 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001654 shareItem.setOnMenuItemClickListener(
1655 new MenuItem.OnMenuItemClickListener() {
1656 @Override
1657 public boolean onMenuItemClick(MenuItem item) {
1658 sharePage(mActivity, null, extra, null,
1659 null);
1660 return true;
1661 }
1662 }
1663 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001664 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001665 menu.findItem(R.id.view_image_context_menu_id)
1666 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1667 @Override
1668 public boolean onMenuItemClick(MenuItem item) {
1669 openTab(extra, mTabControl.getCurrentTab(), true, true);
1670 return false;
1671 }
1672 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001673 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1674 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1675 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001676 menu.findItem(R.id.set_wallpaper_context_menu_id).
1677 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1678 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001679 break;
1680
1681 default:
1682 Log.w(LOGTAG, "We should not get here.");
1683 break;
1684 }
1685 //update the ui
1686 mUi.onContextMenuCreated(menu);
1687 }
1688
kaiyiz6e5b3e02013-08-19 20:02:01 +08001689 public void startAddMyNavigation(String url) {
1690 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1691 Bundle bundle = new Bundle();
1692 bundle.putBoolean("isAdding", true);
1693 bundle.putString("url", url);
1694 bundle.putString("name", getNameFromUrl(url));
1695 intent.putExtra("websites", bundle);
1696 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1697 }
1698
1699 private void showMyNavigationDeleteDialog(final String itemUrl) {
1700 new AlertDialog.Builder(this.getContext())
1701 .setTitle(R.string.my_navigation_delete_label)
1702 .setIcon(android.R.drawable.ic_dialog_alert)
1703 .setMessage(R.string.my_navigation_delete_msg)
1704 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1705 @Override
1706 public void onClick(DialogInterface dialog, int whichButton) {
1707 deleteMyNavigationItem(itemUrl);
1708 }
1709 })
1710 .setNegativeButton(R.string.cancel, null)
1711 .show();
1712 }
1713
1714 private void deleteMyNavigationItem(final String itemUrl) {
1715 ContentResolver cr = this.getContext().getContentResolver();
1716 Cursor cursor = null;
1717
1718 try {
1719 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1720 new String[] {
1721 MyNavigationUtil.ID
1722 }, "url = ?", new String[] {
1723 itemUrl
1724 }, null);
1725 if (null != cursor && cursor.moveToFirst()) {
1726 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1727 cursor.getLong(0));
1728
1729 ContentValues values = new ContentValues();
1730 values.put(MyNavigationUtil.TITLE, "");
1731 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1732 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1733 ByteArrayOutputStream os = new ByteArrayOutputStream();
1734 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1735 R.raw.my_navigation_add);
1736 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1737 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1738 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1739 cr.update(uri, values, null, null);
1740 } else {
1741 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1742 }
1743 } catch (IllegalStateException e) {
1744 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1745 } finally {
1746 if (null != cursor) {
1747 cursor.close();
1748 }
1749 }
1750
1751 if (getCurrentTopWebView() != null) {
1752 getCurrentTopWebView().reload();
1753 }
1754 }
1755
1756 private String getNameFromUrl(String itemUrl) {
1757 ContentResolver cr = this.getContext().getContentResolver();
1758 Cursor cursor = null;
1759 String name = null;
1760
1761 try {
1762 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1763 new String[] {
1764 MyNavigationUtil.TITLE
1765 }, "url = ?", new String[] {
1766 itemUrl
1767 }, null);
1768 if (null != cursor && cursor.moveToFirst()) {
1769 name = cursor.getString(0);
1770 } else {
1771 Log.e(LOGTAG, "this item does not exist!");
1772 }
1773 } catch (IllegalStateException e) {
1774 Log.e(LOGTAG, "getNameFromUrl", e);
1775 } finally {
1776 if (null != cursor) {
1777 cursor.close();
1778 }
1779 }
1780 return name;
1781 }
1782
1783 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1784 int width = mActivity.getResources().getDimensionPixelOffset(
1785 R.dimen.myNavigationThumbnailWidth);
1786 int height = mActivity.getResources().getDimensionPixelOffset(
1787 R.dimen.myNavigationThumbnailHeight);
1788
1789 final Bitmap bm = createScreenshot(webView, width, height);
1790
1791 if (bm == null) {
1792 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1793 return;
1794 }
1795
1796 final ContentResolver cr = mActivity.getContentResolver();
1797 new AsyncTask<Void, Void, Void>() {
1798 @Override
1799 protected Void doInBackground(Void... unused) {
1800 ContentResolver cr = mActivity.getContentResolver();
1801 Cursor cursor = null;
1802 try {
1803 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1804 new String[] {
1805 MyNavigationUtil.ID
1806 }, "url = ?", new String[] {
1807 itemUrl
1808 }, null);
1809 if (null != cursor && cursor.moveToFirst()) {
1810 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1811 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1812
1813 ContentValues values = new ContentValues();
1814 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1815 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1816 cursor.getLong(0));
1817 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1818 cr.update(uri, values, null, null);
1819 os.close();
1820 }
1821 } catch (IllegalStateException e) {
1822 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1823 } catch (IOException e) {
1824 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1825 } finally {
1826 if (null != cursor) {
1827 cursor.close();
1828 }
1829 }
1830 return null;
1831 }
1832 }.execute();
1833 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001834 /**
1835 * As the menu can be open when loading state changes
1836 * we must manually update the state of the stop/reload menu
1837 * item
1838 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001839 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001840 if (menu == null) {
1841 return;
1842 }
1843 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001844 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001845 menu.findItem(R.id.stop_menu_id):
1846 menu.findItem(R.id.reload_menu_id);
1847 if (src != null) {
1848 dest.setIcon(src.getIcon());
1849 dest.setTitle(src.getTitle());
1850 }
1851 }
1852
John Reck9c35b9c2012-05-30 10:08:50 -07001853 @Override
1854 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001855 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001856 // hold on to the menu reference here; it is used by the page callbacks
1857 // to update the menu based on loading state
1858 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001859 // Note: setVisible will decide whether an item is visible; while
1860 // setEnabled() will decide whether an item is enabled, which also means
1861 // whether the matching shortcut key will function.
1862 switch (mMenuState) {
1863 case EMPTY_MENU:
1864 if (mCurrentMenuState != mMenuState) {
1865 menu.setGroupVisible(R.id.MAIN_MENU, false);
1866 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1867 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1868 }
1869 break;
1870 default:
1871 if (mCurrentMenuState != mMenuState) {
1872 menu.setGroupVisible(R.id.MAIN_MENU, true);
1873 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1874 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1875 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001876 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001877 break;
1878 }
1879 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001880 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001881 }
1882
Michael Kolb4bf79712011-07-14 14:18:12 -07001883 @Override
1884 public void updateMenuState(Tab tab, Menu menu) {
1885 boolean canGoBack = false;
1886 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001887 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001888 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001889 if (tab != null) {
1890 canGoBack = tab.canGoBack();
1891 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001892 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001893 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001894 }
1895 final MenuItem back = menu.findItem(R.id.back_menu_id);
1896 back.setEnabled(canGoBack);
1897
1898 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001899
1900 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1901 forward.setEnabled(canGoForward);
1902
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001903 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1904 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001905 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001906 if (source != null && dest != null) {
1907 dest.setTitle(source.getTitle());
1908 dest.setIcon(source.getIcon());
1909 }
John Recke1a03a32011-09-14 17:04:16 -07001910 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001911
1912 // decide whether to show the share link option
1913 PackageManager pm = mActivity.getPackageManager();
1914 Intent send = new Intent(Intent.ACTION_SEND);
1915 send.setType("text/plain");
1916 ResolveInfo ri = pm.resolveActivity(send,
1917 PackageManager.MATCH_DEFAULT_ONLY);
1918 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1919
1920 boolean isNavDump = mSettings.enableNavDump();
1921 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1922 nav.setVisible(isNavDump);
1923 nav.setEnabled(isNavDump);
1924
1925 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001926 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1927 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001928 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1929 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001930 // history and snapshots item are the members of COMBO menu group,
1931 // so if show history item, only make snapshots item invisible.
1932 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001933
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001934 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001935 }
1936
John Reck9c35b9c2012-05-30 10:08:50 -07001937 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001939 if (null == getCurrentTopWebView()) {
1940 return false;
1941 }
1942 if (mMenuIsDown) {
1943 // The shortcut action consumes the MENU. Even if it is still down,
1944 // it won't trigger the next shortcut action. In the case of the
1945 // shortcut action triggering a new activity, like Bookmarks, we
1946 // won't get onKeyUp for MENU. So it is important to reset it here.
1947 mMenuIsDown = false;
1948 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001949 if (mUi.onOptionsItemSelected(item)) {
1950 // ui callback handled it
1951 return true;
1952 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001953 switch (item.getItemId()) {
1954 // -- Main menu
1955 case R.id.new_tab_menu_id:
1956 openTabToHomePage();
1957 break;
1958
1959 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001960 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001961 break;
1962
Afzal Najamd4e33312012-04-26 01:54:01 -04001963 case R.id.close_other_tabs_id:
1964 closeOtherTabs();
1965 break;
1966
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 case R.id.goto_menu_id:
1968 editUrl();
1969 break;
1970
1971 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001972 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1973 break;
1974
1975 case R.id.history_menu_id:
1976 bookmarksOrHistoryPicker(ComboViews.History);
1977 break;
1978
1979 case R.id.snapshots_menu_id:
1980 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001981 break;
1982
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001984 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001985 break;
1986
1987 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001988 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001989 stopLoading();
1990 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001991 Tab currentTab = mTabControl.getCurrentTab();
1992 if (currentTab.hasCrashed) {
1993 currentTab.replaceCrashView(getCurrentTopWebView(),
1994 currentTab.getViewContainer());
1995 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001996 getCurrentTopWebView().reload();
1997 }
1998 break;
1999
2000 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002001 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002002 break;
2003
2004 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002005 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002006 break;
2007
2008 case R.id.close_menu_id:
2009 // Close the subwindow if it exists.
2010 if (mTabControl.getCurrentSubWindow() != null) {
2011 dismissSubWindow(mTabControl.getCurrentTab());
2012 break;
2013 }
2014 closeCurrentTab();
2015 break;
2016
kaiyiza8b6dbb2013-07-29 18:11:22 +08002017 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002018 Object[] params = { new String("persist.debug.browsermonkeytest")};
2019 Class[] type = new Class[] {String.class};
2020 String ret = (String)ReflectHelper.invokeStaticMethod(
2021 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002022 if (ret != null && ret.equals("enable"))
2023 break;
luxiaolb40014b2013-07-19 10:01:43 +08002024 showExitDialog(mActivity);
2025 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002026 case R.id.homepage_menu_id:
2027 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002028 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002029 break;
2030
2031 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002032 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002033 break;
2034
2035 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002036 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002037 break;
2038
John Reck2bc80422011-06-30 15:11:49 -07002039 case R.id.save_snapshot_menu_id:
2040 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002041 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07002042 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05002043 break;
2044
Michael Kolb8233fac2010-10-26 16:08:53 -07002045 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002046 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002047 break;
2048
John Recke1a03a32011-09-14 17:04:16 -07002049 case R.id.snapshot_go_live:
2050 goLive();
2051 return true;
2052
Michael Kolb8233fac2010-10-26 16:08:53 -07002053 case R.id.share_page_menu_id:
2054 Tab currentTab = mTabControl.getCurrentTab();
2055 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002056 return false;
2057 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002058 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002059 break;
2060
2061 case R.id.dump_nav_menu_id:
2062 getCurrentTopWebView().debugDump();
2063 break;
2064
Michael Kolb8233fac2010-10-26 16:08:53 -07002065 case R.id.zoom_in_menu_id:
2066 getCurrentTopWebView().zoomIn();
2067 break;
2068
2069 case R.id.zoom_out_menu_id:
2070 getCurrentTopWebView().zoomOut();
2071 break;
2072
2073 case R.id.view_downloads_menu_id:
2074 viewDownloads();
2075 break;
2076
John Reck42229bc2011-08-19 13:26:43 -07002077 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002078 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002079 break;
2080
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 case R.id.window_one_menu_id:
2082 case R.id.window_two_menu_id:
2083 case R.id.window_three_menu_id:
2084 case R.id.window_four_menu_id:
2085 case R.id.window_five_menu_id:
2086 case R.id.window_six_menu_id:
2087 case R.id.window_seven_menu_id:
2088 case R.id.window_eight_menu_id:
2089 {
2090 int menuid = item.getItemId();
2091 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2092 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2093 Tab desiredTab = mTabControl.getTab(id);
2094 if (desiredTab != null &&
2095 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002096 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002097 }
2098 break;
2099 }
2100 }
2101 }
2102 break;
2103
2104 default:
2105 return false;
2106 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002107 return true;
2108 }
2109
John Reck68234a92012-04-19 15:27:12 -07002110 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2111 implements OnCancelListener {
2112
2113 private Tab mTab;
2114 private Dialog mProgressDialog;
2115 private ContentValues mValues;
2116
2117 private SaveSnapshotTask(Tab tab) {
2118 mTab = tab;
2119 }
2120
2121 @Override
2122 protected void onPreExecute() {
2123 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2124 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2125 true, true, this);
2126 mValues = mTab.createSnapshotValues();
2127 }
2128
2129 @Override
2130 protected Long doInBackground(Void... params) {
2131 if (!mTab.saveViewState(mValues)) {
2132 return null;
2133 }
2134 if (isCancelled()) {
2135 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2136 File file = mActivity.getFileStreamPath(path);
2137 if (!file.delete()) {
2138 file.deleteOnExit();
2139 }
2140 return null;
2141 }
2142 final ContentResolver cr = mActivity.getContentResolver();
2143 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2144 if (result == null) {
2145 return null;
2146 }
2147 long id = ContentUris.parseId(result);
2148 return id;
2149 }
2150
2151 @Override
2152 protected void onPostExecute(Long id) {
2153 if (isCancelled()) {
2154 return;
2155 }
2156 mProgressDialog.dismiss();
2157 if (id == null) {
2158 Toast.makeText(mActivity, R.string.snapshot_failed,
2159 Toast.LENGTH_SHORT).show();
2160 return;
2161 }
2162 Bundle b = new Bundle();
2163 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2164 mUi.showComboView(ComboViews.Snapshots, b);
2165 }
2166
2167 @Override
2168 public void onCancel(DialogInterface dialog) {
2169 cancel(true);
2170 }
2171 }
2172
Michael Kolb80f75082012-04-10 10:50:06 -07002173 @Override
2174 public void toggleUserAgent() {
2175 WebView web = getCurrentWebView();
2176 mSettings.toggleDesktopUseragent(web);
2177 web.loadUrl(web.getOriginalUrl());
2178 }
2179
2180 @Override
2181 public void findOnPage() {
2182 getCurrentTopWebView().showFindDialog(null, true);
2183 }
2184
2185 @Override
2186 public void openPreferences() {
2187 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2188 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2189 getCurrentTopWebView().getUrl());
2190 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2191 }
2192
2193 @Override
2194 public void bookmarkCurrentPage() {
2195 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2196 if (bookmarkIntent != null) {
2197 mActivity.startActivity(bookmarkIntent);
2198 }
2199 }
2200
John Recke1a03a32011-09-14 17:04:16 -07002201 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002202 SnapshotTab t = (SnapshotTab) getCurrentTab();
2203 t.loadUrl(t.getLiveUrl(), null);
John Recke1a03a32011-09-14 17:04:16 -07002204 }
2205
luxiaolb40014b2013-07-19 10:01:43 +08002206 private void showExitDialog(final Activity activity) {
2207 new AlertDialog.Builder(activity)
2208 .setTitle(R.string.exit_browser_title)
2209 .setIcon(android.R.drawable.ic_dialog_alert)
2210 .setMessage(R.string.exit_browser_msg)
2211 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2212 public void onClick(DialogInterface dialog, int which) {
2213 activity.moveTaskToBack(true);
2214 dialog.dismiss();
2215 }
2216 })
2217 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2218 public void onClick(DialogInterface dialog, int which) {
2219 activity.finish();
2220 mHandler.postDelayed(new Runnable() {
2221 @Override
2222 public void run() {
2223 // TODO Auto-generated method stub
2224 mCrashRecoveryHandler.clearState(true);
2225 int pid = android.os.Process.myPid();
2226 android.os.Process.killProcess(pid);
2227 }
2228 }, 300);
2229 dialog.dismiss();
2230 }
2231 })
2232 .show();
2233 }
Michael Kolb315d5022011-10-13 12:47:11 -07002234 @Override
2235 public void showPageInfo() {
2236 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2237 }
2238
John Reck9c35b9c2012-05-30 10:08:50 -07002239 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002240 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002241 // Let the History and Bookmark fragments handle menus they created.
2242 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2243 return false;
2244 }
2245
Michael Kolb8233fac2010-10-26 16:08:53 -07002246 int id = item.getItemId();
2247 boolean result = true;
2248 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002249 // -- Browser context menu
2250 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002251 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002252 case R.id.copy_link_context_menu_id:
2253 final WebView webView = getCurrentTopWebView();
2254 if (null == webView) {
2255 result = false;
2256 break;
2257 }
2258 final HashMap<String, WebView> hrefMap =
2259 new HashMap<String, WebView>();
2260 hrefMap.put("webview", webView);
2261 final Message msg = mHandler.obtainMessage(
2262 FOCUS_NODE_HREF, id, 0, hrefMap);
2263 webView.requestFocusNodeHref(msg);
2264 break;
2265
2266 default:
2267 // For other context menus
2268 result = onOptionsItemSelected(item);
2269 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002270 return result;
2271 }
2272
2273 /**
2274 * support programmatically opening the context menu
2275 */
2276 public void openContextMenu(View view) {
2277 mActivity.openContextMenu(view);
2278 }
2279
2280 /**
2281 * programmatically open the options menu
2282 */
2283 public void openOptionsMenu() {
2284 mActivity.openOptionsMenu();
2285 }
2286
John Reck9c35b9c2012-05-30 10:08:50 -07002287 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002288 public boolean onMenuOpened(int featureId, Menu menu) {
2289 if (mOptionsMenuOpen) {
2290 if (mConfigChanged) {
2291 // We do not need to make any changes to the state of the
2292 // title bar, since the only thing that happened was a
2293 // change in orientation
2294 mConfigChanged = false;
2295 } else {
2296 if (!mExtendedMenuOpen) {
2297 mExtendedMenuOpen = true;
2298 mUi.onExtendedMenuOpened();
2299 } else {
2300 // Switching the menu back to icon view, so show the
2301 // title bar once again.
2302 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002303 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002304 }
2305 }
2306 } else {
2307 // The options menu is closed, so open it, and show the title
2308 mOptionsMenuOpen = true;
2309 mConfigChanged = false;
2310 mExtendedMenuOpen = false;
2311 mUi.onOptionsMenuOpened();
2312 }
2313 return true;
2314 }
2315
John Reck9c35b9c2012-05-30 10:08:50 -07002316 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002317 public void onOptionsMenuClosed(Menu menu) {
2318 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002319 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002320 }
2321
John Reck9c35b9c2012-05-30 10:08:50 -07002322 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002323 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002324 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002325 }
2326
2327 // Helper method for getting the top window.
2328 @Override
2329 public WebView getCurrentTopWebView() {
2330 return mTabControl.getCurrentTopWebView();
2331 }
2332
2333 @Override
2334 public WebView getCurrentWebView() {
2335 return mTabControl.getCurrentWebView();
2336 }
2337
2338 /*
2339 * This method is called as a result of the user selecting the options
2340 * menu to see the download window. It shows the download window on top of
2341 * the current window.
2342 */
2343 void viewDownloads() {
2344 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2345 mActivity.startActivity(intent);
2346 }
2347
John Reck30b065e2011-07-19 10:58:05 -07002348 int getActionModeHeight() {
2349 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2350 new int[] { android.R.attr.actionBarSize });
2351 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2352 actionBarSizeTypedArray.recycle();
2353 return size;
2354 }
2355
Michael Kolb8233fac2010-10-26 16:08:53 -07002356 // action mode
2357
John Reck9c35b9c2012-05-30 10:08:50 -07002358 @Override
2359 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002360 mUi.onActionModeStarted(mode);
2361 mActionMode = mode;
2362 }
2363
2364 /*
2365 * True if a custom ActionMode (i.e. find or select) is in use.
2366 */
2367 @Override
2368 public boolean isInCustomActionMode() {
2369 return mActionMode != null;
2370 }
2371
2372 /*
2373 * End the current ActionMode.
2374 */
2375 @Override
2376 public void endActionMode() {
2377 if (mActionMode != null) {
2378 mActionMode.finish();
2379 }
2380 }
2381
2382 /*
2383 * Called by find and select when they are finished. Replace title bars
2384 * as necessary.
2385 */
John Reck9c35b9c2012-05-30 10:08:50 -07002386 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002387 public void onActionModeFinished(ActionMode mode) {
2388 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002389 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002390 mActionMode = null;
2391 }
2392
2393 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002394 final Tab tab = getCurrentTab();
2395 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002396 }
2397
2398 // bookmark handling
2399
2400 /**
2401 * add the current page as a bookmark to the given folder id
2402 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002403 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002404 * bookmarked, and if it is, edit that bookmark. If false, and
2405 * the site is already bookmarked, do not attempt to edit the
2406 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002407 */
2408 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002409 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002410 WebView w = getCurrentTopWebView();
2411 if (w == null) {
2412 return null;
2413 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002414 Intent i = new Intent(mActivity,
2415 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002416 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2417 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2418 String touchIconUrl = w.getTouchIconUrl();
2419 if (touchIconUrl != null) {
2420 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2421 WebSettings settings = w.getSettings();
2422 if (settings != null) {
2423 i.putExtra(AddBookmarkPage.USER_AGENT,
2424 settings.getUserAgentString());
2425 }
2426 }
2427 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2428 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2429 getDesiredThumbnailHeight(mActivity)));
2430 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002431 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002432 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2433 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002434 // Put the dialog at the upper right of the screen, covering the
2435 // star on the title bar.
2436 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002437 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002438 }
2439
2440 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002441 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002442 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002443 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002444 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002445 }
2446
2447 // thumbnails
2448
2449 /**
2450 * Return the desired width for thumbnail screenshots, which are stored in
2451 * the database, and used on the bookmarks screen.
2452 * @param context Context for finding out the density of the screen.
2453 * @return desired width for thumbnail screenshot.
2454 */
2455 static int getDesiredThumbnailWidth(Context context) {
2456 return context.getResources().getDimensionPixelOffset(
2457 R.dimen.bookmarkThumbnailWidth);
2458 }
2459
2460 /**
2461 * Return the desired height for thumbnail screenshots, which are stored in
2462 * the database, and used on the bookmarks screen.
2463 * @param context Context for finding out the density of the screen.
2464 * @return desired height for thumbnail screenshot.
2465 */
2466 static int getDesiredThumbnailHeight(Context context) {
2467 return context.getResources().getDimensionPixelOffset(
2468 R.dimen.bookmarkThumbnailHeight);
2469 }
2470
John Reck8cc92352011-07-06 17:41:52 -07002471 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002472 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002473 return null;
2474 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002475
2476 Bitmap viewportBitmap = view.getViewportBitmap();
2477 if (viewportBitmap == null) {
2478 return null;
2479 }
2480
2481 float aspectRatio = (float) width/height;
2482 int viewportWidth = viewportBitmap.getWidth();
2483 int viewportHeight = viewportBitmap.getHeight();
2484
2485 //modify the size to attain the same aspect ratio of desired thumbnail size
2486 if (viewportHeight > viewportWidth) {
2487 viewportHeight= (int)Math.round(viewportWidth * aspectRatio);
2488 } else {
2489 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2490 }
2491
2492 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2493 Rect dstRect = new Rect(0, 0, width, height);
2494
2495 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2496 || sThumbnailBitmap.getHeight() != height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002497 if (sThumbnailBitmap != null) {
2498 sThumbnailBitmap.recycle();
2499 sThumbnailBitmap = null;
2500 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002501
2502 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002503 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002504
John Reckd7dd9b22011-08-30 09:18:29 -07002505 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002506 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
John Reckd7dd9b22011-08-30 09:18:29 -07002507
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002508 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002509 }
2510
John Reck34ef2672011-02-10 11:30:55 -08002511 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002512 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002513 // FIXME: Would like to make sure there is actually something to
2514 // draw, but the API for that (WebViewCore.pictureReady()) is not
2515 // currently accessible here.
2516
John Reck34ef2672011-02-10 11:30:55 -08002517 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002518 if (view == null) {
2519 // Tab was destroyed
2520 return;
2521 }
John Reck34ef2672011-02-10 11:30:55 -08002522 final String url = tab.getUrl();
2523 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002524 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002525 if (TextUtils.isEmpty(url)) {
2526 return;
2527 }
2528
kaiyiz6e5b3e02013-08-19 20:02:01 +08002529 //update My Navigation Thumbnails
2530 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2531 if (isMyNavigationUrl) {
2532 updateMyNavigationThumbnail(url, view);
2533 }
John Reck34ef2672011-02-10 11:30:55 -08002534 // Only update thumbnails for web urls (http(s)://), not for
2535 // about:, javascript:, data:, etc...
2536 // Unless it is a bookmarked site, then always update
2537 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2538 return;
2539 }
2540
kaiyiz6e5b3e02013-08-19 20:02:01 +08002541 if (url != null && mUpdateMyNavThumbnailUrl != null
2542 && Patterns.WEB_URL.matcher(url).matches()
2543 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2544 String urlHost = (new WebAddress(url)).getHost();
2545 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2546 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2547 || bookmarkHost.length() == 0) {
2548 return;
2549 }
2550 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2551 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2552 bookmarkHost.length());
2553 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2554 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2555 if (!bookmarkDomain.equals(urlDomain)) {
2556 return;
2557 }
2558 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002559 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2560 getDesiredThumbnailHeight(mActivity));
2561 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002562 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2563 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2564 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2565 500);
2566 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002567 return;
2568 }
2569
2570 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002571 new AsyncTask<Void, Void, Void>() {
2572 @Override
2573 protected Void doInBackground(Void... unused) {
2574 Cursor cursor = null;
2575 try {
2576 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002577 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2578 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2579 : url);
2580 if (mUpdateMyNavThumbnail) {
2581 mUpdateMyNavThumbnail = false;
2582 mUpdateMyNavThumbnailUrl = null;
2583 }
John Reck34ef2672011-02-10 11:30:55 -08002584 if (cursor != null && cursor.moveToFirst()) {
2585 final ByteArrayOutputStream os =
2586 new ByteArrayOutputStream();
2587 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002588
John Reck34ef2672011-02-10 11:30:55 -08002589 ContentValues values = new ContentValues();
2590 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002591
John Reck34ef2672011-02-10 11:30:55 -08002592 do {
John Reck617fd832011-02-16 14:35:59 -08002593 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002594 cr.update(Images.CONTENT_URI, values, null, null);
2595 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002596 }
John Reck34ef2672011-02-10 11:30:55 -08002597 } catch (IllegalStateException e) {
2598 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002599 } catch (SQLiteException s) {
2600 // Added for possible error when user tries to remove the same bookmark
2601 // that is being updated with a screen shot
2602 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002603 } finally {
2604 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 }
John Reck34ef2672011-02-10 11:30:55 -08002606 return null;
2607 }
2608 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002609 }
2610
2611 private class Copy implements OnMenuItemClickListener {
2612 private CharSequence mText;
2613
John Reck9c35b9c2012-05-30 10:08:50 -07002614 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002615 public boolean onMenuItemClick(MenuItem item) {
2616 copy(mText);
2617 return true;
2618 }
2619
2620 public Copy(CharSequence toCopy) {
2621 mText = toCopy;
2622 }
2623 }
2624
Leon Scroggins63c02662010-11-18 15:16:27 -05002625 private static class Download implements OnMenuItemClickListener {
2626 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002627 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002628 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002629 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002630 private static final String FALLBACK_EXTENSION = "dat";
2631 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002632
John Reck9c35b9c2012-05-30 10:08:50 -07002633 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002634 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002635 if (DataUri.isDataUri(mText)) {
2636 saveDataUri();
2637 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002638 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002639 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002640 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002641 return true;
2642 }
2643
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002644 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2645 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002646 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002647 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002648 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002649 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002650 }
George Mount387d45d2011-10-07 15:57:53 -07002651
2652 /**
2653 * Treats mText as a data URI and writes its contents to a file
2654 * based on the current time.
2655 */
2656 private void saveDataUri() {
2657 FileOutputStream outputStream = null;
2658 try {
2659 DataUri uri = new DataUri(mText);
2660 File target = getTarget(uri);
2661 outputStream = new FileOutputStream(target);
2662 outputStream.write(uri.getData());
2663 final DownloadManager manager =
2664 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2665 manager.addCompletedDownload(target.getName(),
2666 mActivity.getTitle().toString(), false,
2667 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002668 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002669 } catch (IOException e) {
2670 Log.e(LOGTAG, "Could not save data URL");
2671 } finally {
2672 if (outputStream != null) {
2673 try {
2674 outputStream.close();
2675 } catch (IOException e) {
2676 // ignore close errors
2677 }
2678 }
2679 }
2680 }
2681
2682 /**
2683 * Creates a File based on the current time stamp and uses
2684 * the mime type of the DataUri to get the extension.
2685 */
2686 private File getTarget(DataUri uri) throws IOException {
2687 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002688 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002689 String nameBase = format.format(new Date());
2690 String mimeType = uri.getMimeType();
2691 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2692 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2693 if (extension == null) {
2694 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2695 extension = FALLBACK_EXTENSION;
2696 }
2697 extension = "." + extension; // createTempFile needs the '.'
2698 File targetFile = File.createTempFile(nameBase, extension, dir);
2699 return targetFile;
2700 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002701 }
2702
Cary Clark8974d282010-11-22 10:46:05 -05002703 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002704 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002705
John Reck9c35b9c2012-05-30 10:08:50 -07002706 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002707 public boolean onMenuItemClick(MenuItem item) {
2708 if (mWebView != null) {
2709 return mWebView.selectText();
2710 }
2711 return false;
2712 }
2713
2714 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002715 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002716 }
2717
2718 }
2719
Michael Kolb8233fac2010-10-26 16:08:53 -07002720 /********************** TODO: UI stuff *****************************/
2721
2722 // these methods have been copied, they still need to be cleaned up
2723
2724 /****************** tabs ***************************************************/
2725
2726 // basic tab interactions:
2727
2728 // it is assumed that tabcontrol already knows about the tab
2729 protected void addTab(Tab tab) {
2730 mUi.addTab(tab);
2731 }
2732
2733 protected void removeTab(Tab tab) {
2734 mUi.removeTab(tab);
2735 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002736 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 }
2738
Michael Kolbf2055602011-04-09 17:20:03 -07002739 @Override
2740 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002741 // monkey protection against delayed start
2742 if (tab != null) {
2743 mTabControl.setCurrentTab(tab);
2744 // the tab is guaranteed to have a webview after setCurrentTab
2745 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002746 tab.setTimeStamp();
Michael Kolbcd424e92011-02-24 10:26:26 -08002747 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002748 }
2749
John Reck8bcafc12011-08-29 16:43:02 -07002750 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002751 Tab current = mTabControl.getCurrentTab();
2752 if (current != null
2753 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002754 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002755 }
2756 }
2757
John Reck26b18322011-06-21 13:08:58 -07002758 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002759 // Dismiss the subwindow if applicable.
2760 dismissSubWindow(appTab);
2761 // Since we might kill the WebView, remove it from the
2762 // content view first.
2763 mUi.detachTab(appTab);
2764 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002765 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002766 // TODO: analyze why the remove and add are necessary
2767 mUi.attachTab(appTab);
2768 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002769 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002770 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002771 } else {
2772 // If the tab was the current tab, we have to attach
2773 // it to the view system again.
2774 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002775 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002776 }
2777 }
2778
2779 // Remove the sub window if it exists. Also called by TabControl when the
2780 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002781 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002782 public void dismissSubWindow(Tab tab) {
2783 removeSubWindow(tab);
2784 // dismiss the subwindow. This will destroy the WebView.
2785 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002786 WebView wv = getCurrentTopWebView();
2787 if (wv != null) {
2788 wv.requestFocus();
2789 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002790 }
2791
2792 @Override
2793 public void removeSubWindow(Tab t) {
2794 if (t.getSubWebView() != null) {
2795 mUi.removeSubWindow(t.getSubViewContainer());
2796 }
2797 }
2798
2799 @Override
2800 public void attachSubWindow(Tab tab) {
2801 if (tab.getSubWebView() != null) {
2802 mUi.attachSubWindow(tab.getSubViewContainer());
2803 getCurrentTopWebView().requestFocus();
2804 }
2805 }
2806
Mathew Inwood29721c22011-06-29 17:55:24 +01002807 private Tab showPreloadedTab(final UrlData urlData) {
2808 if (!urlData.isPreloaded()) {
2809 return null;
2810 }
2811 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2812 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2813 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002814 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002815 // Could not submit query. Fallback to regular tab creation
2816 tabControl.destroy();
2817 return null;
2818 }
2819 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002820 // check tab count and make room for new tab
2821 if (!mTabControl.canCreateNewTab()) {
2822 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2823 if (leastUsed != null) {
2824 closeTab(leastUsed);
2825 }
2826 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002827 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002828 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002829 mTabControl.addPreloadedTab(t);
2830 addTab(t);
2831 setActiveTab(t);
2832 return t;
2833 }
2834
Michael Kolb7bcafde2011-05-09 13:55:59 -07002835 // open a non inconito tab with the given url data
2836 // and set as active tab
2837 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002838 Tab tab = showPreloadedTab(urlData);
2839 if (tab == null) {
2840 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002841 if ((tab != null) && !urlData.isEmpty()) {
2842 loadUrlDataIn(tab, urlData);
2843 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002844 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002845 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002846 }
2847
Michael Kolb843510f2010-12-09 10:51:49 -08002848 @Override
2849 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002850 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002851 }
2852
Michael Kolb8233fac2010-10-26 16:08:53 -07002853 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002854 public Tab openIncognitoTab() {
2855 return openTab(INCOGNITO_URI, true, true, false);
2856 }
2857
2858 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002859 public Tab openTab(String url, boolean incognito, boolean setActive,
2860 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002861 return openTab(url, incognito, setActive, useCurrent, null);
2862 }
2863
2864 @Override
2865 public Tab openTab(String url, Tab parent, boolean setActive,
2866 boolean useCurrent) {
2867 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2868 setActive, useCurrent, parent);
2869 }
2870
2871 public Tab openTab(String url, boolean incognito, boolean setActive,
2872 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002873 Tab tab = createNewTab(incognito, setActive, useCurrent);
2874 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002875 if (parent != null && parent != tab) {
2876 parent.addChildTab(tab);
2877 }
Michael Kolb519d2282011-05-09 17:03:19 -07002878 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002879 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002880 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002881 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002882 return tab;
2883 }
2884
2885 // this method will attempt to create a new tab
2886 // incognito: private browsing tab
2887 // setActive: ste tab as current tab
2888 // useCurrent: if no new tab can be created, return current tab
2889 private Tab createNewTab(boolean incognito, boolean setActive,
2890 boolean useCurrent) {
2891 Tab tab = null;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002892 MemoryMonitor memMonitor = null;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002893 if (mTabControl.canCreateNewTab()) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002894 if (mSettings.enableMemoryMonitor()) {
2895 Log.d(LOGTAG, " Memory Monitor Enabled .");
2896 memMonitor = MemoryMonitor.getInstance(mActivity.getApplicationContext(),this);
2897 if (memMonitor != null) {
2898 //Remove webview associated with the oldest tab
2899 memMonitor.destroyLeastRecentlyActiveTab();
2900 }
2901 } else {
2902 Log.d(LOGTAG, " Memory Monitor disabled .");
2903 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002904 tab = mTabControl.createNewTab(incognito);
2905 addTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002906 tab.setTimeStamp();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002907 if (setActive) {
2908 setActiveTab(tab);
2909 }
2910 } else {
2911 if (useCurrent) {
2912 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002913 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002914 } else {
2915 mUi.showMaxTabsWarning();
2916 }
2917 }
2918 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002919 }
2920
John Reck2bc80422011-06-30 15:11:49 -07002921 @Override
2922 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2923 SnapshotTab tab = null;
2924 if (mTabControl.canCreateNewTab()) {
2925 tab = mTabControl.createSnapshotTab(snapshotId);
2926 addTab(tab);
2927 if (setActive) {
2928 setActiveTab(tab);
2929 }
2930 } else {
2931 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002932 }
2933 return tab;
2934 }
2935
Michael Kolb8233fac2010-10-26 16:08:53 -07002936 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002937 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002938 * @return boolean True if we successfully switched to a different tab. If
2939 * the indexth tab is null, or if that tab is the same as
2940 * the current one, return false.
2941 */
2942 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002943 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002944 Tab currentTab = mTabControl.getCurrentTab();
2945 if (tab == null || tab == currentTab) {
2946 return false;
2947 }
2948 setActiveTab(tab);
2949 return true;
2950 }
2951
2952 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002953 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002954 closeCurrentTab(false);
2955 }
2956
2957 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002958 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002959 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002960 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002961 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002962 return;
2963 }
Michael Kolbc831b632011-05-11 09:30:34 -07002964 final Tab current = mTabControl.getCurrentTab();
2965 final int pos = mTabControl.getCurrentPosition();
2966 Tab newTab = current.getParent();
2967 if (newTab == null) {
2968 newTab = mTabControl.getTab(pos + 1);
2969 if (newTab == null) {
2970 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002971 }
2972 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002973 if (andQuit) {
2974 mTabControl.setCurrentTab(newTab);
2975 closeTab(current);
2976 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002977 // Close window
2978 closeTab(current);
2979 }
2980 }
2981
2982 /**
2983 * Close the tab, remove its associated title bar, and adjust mTabControl's
2984 * current tab to a valid value.
2985 */
2986 @Override
2987 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002988 if (tab == mTabControl.getCurrentTab()) {
2989 closeCurrentTab();
2990 } else {
2991 removeTab(tab);
2992 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002993 }
2994
Afzal Najamd4e33312012-04-26 01:54:01 -04002995 /**
2996 * Close all tabs except the current one
2997 */
2998 @Override
2999 public void closeOtherTabs() {
3000 int inactiveTabs = mTabControl.getTabCount() - 1;
3001 for (int i = inactiveTabs; i >= 0; i--) {
3002 Tab tab = mTabControl.getTab(i);
3003 if (tab != mTabControl.getCurrentTab()) {
3004 removeTab(tab);
3005 }
3006 }
3007 }
3008
Michael Kolb8233fac2010-10-26 16:08:53 -07003009 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003010 protected void loadUrlFromContext(String url) {
3011 Tab tab = getCurrentTab();
3012 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003013 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003014 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003015 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003016 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003017 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003018 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003019 }
3020 }
3021 }
3022
3023 /**
3024 * Load the URL into the given WebView and update the title bar
3025 * to reflect the new load. Call this instead of WebView.loadUrl
3026 * directly.
3027 * @param view The WebView used to load url.
3028 * @param url The URL to load.
3029 */
John Reck71e51422011-07-01 16:49:28 -07003030 @Override
3031 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003032 loadUrl(tab, url, null);
3033 }
3034
3035 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3036 if (tab != null) {
3037 dismissSubWindow(tab);
3038 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003039 if (tab.hasCrashed) {
3040 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3041 }
Michael Kolba53c9892011-10-05 13:31:40 -07003042 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003043 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003044 }
3045
3046 /**
3047 * Load UrlData into a Tab and update the title bar to reflect the new
3048 * load. Call this instead of UrlData.loadIn directly.
3049 * @param t The Tab used to load.
3050 * @param data The UrlData being loaded.
3051 */
3052 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003053 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003054 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003055 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003056 } else {
John Reck38b39652012-06-05 09:22:59 -07003057 if (t != null && data.mDisableUrlOverride) {
3058 t.disableUrlOverridingForLoad();
3059 }
John Reck26b18322011-06-21 13:08:58 -07003060 loadUrl(t, data.mUrl, data.mHeaders);
3061 }
3062 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003063 }
3064
John Reck30c714c2010-12-16 17:30:34 -08003065 @Override
3066 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003067 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003068 if (tab.canGoBack()) {
3069 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003070 } else {
John Reckef654f12011-07-12 16:42:08 -07003071 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003072 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003073 }
3074
3075 void goBackOnePageOrQuit() {
3076 Tab current = mTabControl.getCurrentTab();
3077 if (current == null) {
3078 /*
3079 * Instead of finishing the activity, simply push this to the back
3080 * of the stack and let ActivityManager to choose the foreground
3081 * activity. As BrowserActivity is singleTask, it will be always the
3082 * root of the task. So we can use either true or false for
3083 * moveTaskToBack().
3084 */
luxiaolb40014b2013-07-19 10:01:43 +08003085 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003086 return;
3087 }
John Reckef654f12011-07-12 16:42:08 -07003088 if (current.canGoBack()) {
3089 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003090 } else {
3091 // Check to see if we are closing a window that was created by
3092 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003093 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003094 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003095 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003096 // Now we close the other tab
3097 closeTab(current);
3098 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003099 /*
3100 * Instead of finishing the activity, simply push this to the back
3101 * of the stack and let ActivityManager to choose the foreground
3102 * activity. As BrowserActivity is singleTask, it will be always the
3103 * root of the task. So we can use either true or false for
3104 * moveTaskToBack().
3105 */
luxiaolb40014b2013-07-19 10:01:43 +08003106 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003107 }
3108 }
3109 }
3110
3111 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003112 * helper method for key handler
3113 * returns the current tab if it can't advance
3114 */
Michael Kolbc831b632011-05-11 09:30:34 -07003115 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003116 int pos = mTabControl.getCurrentPosition() + 1;
3117 if (pos >= mTabControl.getTabCount()) {
3118 pos = 0;
3119 }
3120 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003121 }
3122
3123 /**
3124 * helper method for key handler
3125 * returns the current tab if it can't advance
3126 */
Michael Kolbc831b632011-05-11 09:30:34 -07003127 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003128 int pos = mTabControl.getCurrentPosition() - 1;
3129 if ( pos < 0) {
3130 pos = mTabControl.getTabCount() - 1;
3131 }
3132 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003133 }
3134
John Reckbcef87f2012-02-03 14:58:34 -08003135 boolean isMenuOrCtrlKey(int keyCode) {
3136 return (KeyEvent.KEYCODE_MENU == keyCode)
3137 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3138 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3139 }
3140
Michael Kolb0035fad2011-03-14 13:25:28 -07003141 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003142 * handle key events in browser
3143 *
3144 * @param keyCode
3145 * @param event
3146 * @return true if handled, false to pass to super
3147 */
John Reck9c35b9c2012-05-30 10:08:50 -07003148 @Override
3149 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003150 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003151 // Even if MENU is already held down, we need to call to super to open
3152 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003153 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003154 mMenuIsDown = true;
3155 return false;
3156 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003157
Cary Clark8ff8c662010-12-29 15:03:05 -05003158 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003159 Tab tab = getCurrentTab();
3160 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003161
Cary Clark160bbb92011-01-10 11:17:07 -05003162 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3163 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003164
Michael Kolb8233fac2010-10-26 16:08:53 -07003165 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003166 case KeyEvent.KEYCODE_TAB:
3167 if (event.isCtrlPressed()) {
3168 if (event.isShiftPressed()) {
3169 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003170 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003171 } else {
3172 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003173 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003174 }
3175 return true;
3176 }
3177 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003178 case KeyEvent.KEYCODE_SPACE:
3179 // WebView/WebTextView handle the keys in the KeyDown. As
3180 // the Activity's shortcut keys are only handled when WebView
3181 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003182 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003183 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003184 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003185 pageDown();
3186 }
3187 return true;
3188 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003189 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003190 event.startTracking();
3191 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003192 case KeyEvent.KEYCODE_FORWARD:
3193 if (!noModifiers) break;
3194 tab.goForward();
3195 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003196 case KeyEvent.KEYCODE_DPAD_LEFT:
3197 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003198 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003199 return true;
3200 }
3201 break;
3202 case KeyEvent.KEYCODE_DPAD_RIGHT:
3203 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003204 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003205 return true;
3206 }
3207 break;
3208 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003209 if (ctrl) {
3210 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003211 return true;
3212 }
3213 break;
Michael Kolba4183062011-01-16 10:43:21 -08003214// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003215 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003216 if (ctrl ) {
3217 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003218 return true;
3219 }
3220 break;
Michael Kolba4183062011-01-16 10:43:21 -08003221// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003222// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003223// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003224// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003225// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003226// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003227// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003228// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003229// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003230// case KeyEvent.KEYCODE_M: // unused
3231// case KeyEvent.KEYCODE_N: // in Chrome: new window
3232// case KeyEvent.KEYCODE_O: // in Chrome: open file
3233// case KeyEvent.KEYCODE_P: // in Chrome: print page
3234// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003235// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003236// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3237 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003238 // we can't use the ctrl/shift flags, they check for
3239 // exclusive use of a modifier
3240 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003241 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003242 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003243 } else {
3244 openTabToHomePage();
3245 }
3246 return true;
3247 }
3248 break;
3249// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3250// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003251// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003252// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3253// case KeyEvent.KEYCODE_Y: // unused
3254// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003255 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003256 // it is a regular key and webview is not null
3257 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003258 }
3259
John Reck9c35b9c2012-05-30 10:08:50 -07003260 @Override
3261 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003262 switch(keyCode) {
3263 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003264 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003265 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003266 return true;
3267 }
3268 break;
3269 }
3270 return false;
3271 }
3272
John Reck9c35b9c2012-05-30 10:08:50 -07003273 @Override
3274 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003275 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003276 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003277 if (KeyEvent.KEYCODE_MENU == keyCode
3278 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003279 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003280 }
3281 }
Cary Clark160bbb92011-01-10 11:17:07 -05003282 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003283 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003284 case KeyEvent.KEYCODE_BACK:
3285 if (event.isTracking() && !event.isCanceled()) {
3286 onBackKey();
3287 return true;
3288 }
3289 break;
3290 }
3291 return false;
3292 }
3293
3294 public boolean isMenuDown() {
3295 return mMenuIsDown;
3296 }
3297
John Reck9c35b9c2012-05-30 10:08:50 -07003298 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003299 public void setupAutoFill(Message message) {
3300 // Open the settings activity at the AutoFill profile fragment so that
3301 // the user can create a new profile. When they return, we will dispatch
3302 // the message so that we can autofill the form using their new profile.
3303 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3304 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3305 AutoFillSettingsFragment.class.getName());
3306 mAutoFillSetupMessage = message;
3307 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3308 }
John Reckb3417f02011-01-14 11:01:05 -08003309
John Reck9c35b9c2012-05-30 10:08:50 -07003310 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003311 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003312 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003313 return true;
3314 }
3315
John Reck1cf4b792011-07-26 10:22:22 -07003316 @Override
3317 public boolean shouldCaptureThumbnails() {
3318 return mUi.shouldCaptureThumbnails();
3319 }
3320
Michael Kolbc3af0672011-08-09 10:24:41 -07003321 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003322 public boolean supportsVoice() {
3323 PackageManager pm = mActivity.getPackageManager();
3324 List activities = pm.queryIntentActivities(new Intent(
3325 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3326 return activities.size() != 0;
3327 }
3328
3329 @Override
3330 public void startVoiceRecognizer() {
3331 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003332 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003333 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3334 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3335 mActivity.startActivityForResult(voice, VOICE_RESULT);
3336 }
3337
3338 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003339 public void setBlockEvents(boolean block) {
3340 mBlockEvents = block;
3341 }
3342
John Reck9c35b9c2012-05-30 10:08:50 -07003343 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003344 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003345 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003346 }
3347
John Reck9c35b9c2012-05-30 10:08:50 -07003348 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003349 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003350 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003351 }
3352
John Reck9c35b9c2012-05-30 10:08:50 -07003353 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003354 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003355 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003356 }
3357
John Reck9c35b9c2012-05-30 10:08:50 -07003358 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003359 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003360 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003361 }
3362
John Reck9c35b9c2012-05-30 10:08:50 -07003363 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003364 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003365 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003366 }
3367
Michael Kolb8233fac2010-10-26 16:08:53 -07003368}