blob: c3553d49a53b4f8ad023fa9e3790fefccc21b00e [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;
Vivek Sekharce2a4832014-03-26 13:26:53 -070025import android.content.Context;
26import android.content.res.Resources;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070029import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070030import android.content.ContentValues;
31import android.content.Context;
John Reck68234a92012-04-19 15:27:12 -070032import android.content.DialogInterface;
33import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.content.Intent;
35import android.content.pm.PackageManager;
36import android.content.pm.ResolveInfo;
37import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070038import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050039import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070040import android.database.Cursor;
41import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020042import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080044import android.graphics.BitmapFactory;
Michael Kolb8233fac2010-10-26 16:08:53 -070045import android.graphics.Canvas;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080046import android.graphics.Paint;
47import android.graphics.Rect;
kaiyiza016da12013-08-26 17:50:22 +080048import android.net.ConnectivityManager;
49import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.net.Uri;
51import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080052import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080053import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.os.AsyncTask;
55import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070056import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.os.Handler;
58import android.os.Message;
59import android.os.PowerManager;
60import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000061import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.provider.Browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070063import android.provider.ContactsContract;
64import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080065import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070066import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070067import android.text.TextUtils;
68import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080069import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070070import android.view.ActionMode;
71import android.view.ContextMenu;
72import android.view.ContextMenu.ContextMenuInfo;
73import android.view.Gravity;
74import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070075import android.view.Menu;
76import android.view.MenuInflater;
77import android.view.MenuItem;
78import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070079import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070080import android.view.View;
George Mount387d45d2011-10-07 15:57:53 -070081import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070082import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080083import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb8233fac2010-10-26 16:08:53 -070084import android.webkit.WebIconDatabase;
Leon Scrogginsac993842011-02-02 12:54:07 -050085import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070086
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.CookieManager;
88import org.codeaurora.swe.CookieSyncManager;
89import org.codeaurora.swe.HttpAuthHandler;
90import org.codeaurora.swe.SslErrorHandler;
91import org.codeaurora.swe.WebSettings;
92import org.codeaurora.swe.WebView;
93
Bijan Amirzada41242f22014-03-21 12:12:18 -070094import com.android.browser.R;
95import com.android.browser.IntentHandler.UrlData;
96import com.android.browser.UI.ComboViews;
97import com.android.browser.mynavigation.AddMyNavigationPage;
98import com.android.browser.mynavigation.MyNavigationUtil;
99import com.android.browser.platformsupport.BrowserContract;
100import com.android.browser.platformsupport.WebAddress;
101import com.android.browser.platformsupport.BrowserContract.Images;
102import com.android.browser.provider.BrowserProvider2.Thumbnails;
103import com.android.browser.provider.SnapshotProvider.Snapshots;
104import com.android.browser.reflect.ReflectHelper;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700107import java.io.File;
108import java.io.FileOutputStream;
109import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700110import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700111import java.text.DateFormat;
112import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700113import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700115import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700116import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800117import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200118import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700119import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700120
121/**
122 * Controller for browser
123 */
124public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700125 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700126
127 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800128 private static final String SEND_APP_ID_EXTRA =
129 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700130 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800131
132
kaiyiza016da12013-08-26 17:50:22 +0800133 private static final String PROP_NETSWITCH = "persist.env.browser.netswitch";
134 private static final String INTENT_WIFI_SELECTION_DATA_CONNECTION =
135 "android.net.wifi.cmcc.WIFI_SELECTION_DATA_CONNECTION";
kaiyiz6e5b3e02013-08-19 20:02:01 +0800136 private static final String OFFLINE_PAGE =
Bijan Amirzada41242f22014-03-21 12:12:18 -0700137 "content://com.android.browser.mynavigation/websites";
kaiyizb7bf1f22013-10-02 11:42:23 +0800138 private static final String INTENT_PICK_NETWORK =
139 "android.net.wifi.cmcc.PICK_WIFI_NETWORK_AND_GPRS";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800140
141 public final static String EXTRA_SHARE_SCREENSHOT = "share_screenshot";
142 public final static String EXTRA_SHARE_FAVICON = "share_favicon";
Michael Kolb8233fac2010-10-26 16:08:53 -0700143 // public message ids
144 public final static int LOAD_URL = 1001;
145 public final static int STOP_LOAD = 1002;
146
147 // Message Ids
148 private static final int FOCUS_NODE_HREF = 102;
149 private static final int RELEASE_WAKELOCK = 107;
150
151 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
152
153 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800154 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700155
156 private static final int EMPTY_MENU = -1;
157
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700159 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 final static int PREFERENCES_PAGE = 3;
161 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000162 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700163 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800164 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000165
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
167
168 // As the ids are dynamically created, we can't guarantee that they will
169 // be in sequence, so this static array maps ids to a window number.
170 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
171 { R.id.window_one_menu_id, R.id.window_two_menu_id,
172 R.id.window_three_menu_id, R.id.window_four_menu_id,
173 R.id.window_five_menu_id, R.id.window_six_menu_id,
174 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
175
176 // "source" parameter for Google search through search key
177 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
178 // "source" parameter for Google search through simplily type
179 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
180
George Mount387d45d2011-10-07 15:57:53 -0700181 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700182 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
183
John Reckd7dd9b22011-08-30 09:18:29 -0700184 // A bitmap that is re-used in createScreenshot as scratch space
185 private static Bitmap sThumbnailBitmap;
186
Michael Kolb8233fac2010-10-26 16:08:53 -0700187 private Activity mActivity;
188 private UI mUi;
189 private TabControl mTabControl;
190 private BrowserSettings mSettings;
191 private WebViewFactory mFactory;
192
193 private WakeLock mWakeLock;
194
195 private UrlHandler mUrlHandler;
196 private UploadHandler mUploadHandler;
197 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700198 private PageDialogsHandler mPageDialogsHandler;
199 private NetworkStateHandler mNetworkHandler;
200
Ben Murdoch8029a772010-11-16 11:58:21 +0000201 private Message mAutoFillSetupMessage;
202
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800204 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700205
206 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
207
208 // FIXME, temp address onPrepareMenu performance problem.
209 // When we move everything out of view, we should rewrite this.
210 private int mCurrentMenuState = 0;
211 private int mMenuState = R.id.MAIN_MENU;
212 private int mOldMenuState = EMPTY_MENU;
213 private Menu mCachedMenu;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 private boolean mMenuIsDown;
216
217 // For select and find, we keep track of the ActionMode so that
218 // finish() can be called as desired.
219 private ActionMode mActionMode;
220
221 /**
222 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
223 * of whether the configuration has changed. The first onMenuOpened call
224 * after a configuration change is simply a reopening of the same menu
225 * (i.e. mIconView did not change).
226 */
227 private boolean mConfigChanged;
228
229 /**
230 * Keeps track of whether the options menu is open. This is important in
231 * determining whether to show or hide the title bar overlay
232 */
233 private boolean mOptionsMenuOpen;
234
235 /**
236 * Whether or not the options menu is in its bigger, popup menu form. When
237 * true, we want the title bar overlay to be gone. When false, we do not.
238 * Only meaningful if mOptionsMenuOpen is true.
239 */
240 private boolean mExtendedMenuOpen;
241
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 private boolean mActivityPaused = true;
243 private boolean mLoadStopped;
244
245 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500246 // Checks to see when the bookmarks database has changed, and updates the
247 // Tabs' notion of whether they represent bookmarked sites.
248 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700249 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700250
Michael Kolbc3af0672011-08-09 10:24:41 -0700251 private boolean mBlockEvents;
252
Michael Kolb0b129122012-06-04 16:31:58 -0700253 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800254 private boolean mUpdateMyNavThumbnail;
255 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700256
George Mount3636d0a2011-11-21 09:08:21 -0800257 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 mActivity = browser;
259 mSettings = BrowserSettings.getInstance();
260 mTabControl = new TabControl(this);
261 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700262 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800263 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700264 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700265
266 mUrlHandler = new UrlHandler(this);
267 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
269
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800270 // Creating dummy Webview for browser to force loading of library;
271 // in order for CookieManager calls to be invoked properly and
272 // awBrowserContext to be initialized
273 (mFactory.createWebView(false)).destroy();
274
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500276 mBookmarksObserver = new ContentObserver(mHandler) {
277 @Override
278 public void onChange(boolean selfChange) {
279 int size = mTabControl.getTabCount();
280 for (int i = 0; i < size; i++) {
281 mTabControl.getTab(i).updateBookmarkedStatus();
282 }
283 }
284
285 };
286 browser.getContentResolver().registerContentObserver(
287 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700288
289 mNetworkHandler = new NetworkStateHandler(mActivity, this);
290 // Start watching the default geolocation permissions
291 mSystemAllowGeolocationOrigins =
292 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
293 mSystemAllowGeolocationOrigins.start();
294
John Reckaf262e72011-07-25 13:55:44 -0700295 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700296 }
297
John Reck9c35b9c2012-05-30 10:08:50 -0700298 @Override
299 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800300 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800301 // mCrashRecoverHandler has any previously saved state.
302 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700303 }
304
George Mount3636d0a2011-11-21 09:08:21 -0800305 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700306 // Unless the last browser usage was within 24 hours, destroy any
307 // remaining incognito tabs.
308
309 Calendar lastActiveDate = icicle != null ?
310 (Calendar) icicle.getSerializable("lastActiveDate") : null;
311 Calendar today = Calendar.getInstance();
312 Calendar yesterday = Calendar.getInstance();
313 yesterday.add(Calendar.DATE, -1);
314
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800315 // we dont want to ever recover incognito tabs
316 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700317
Patrick Scott7d50a932011-02-04 09:27:26 -0500318 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700319 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500320 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000321
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800322 mSettings.initializeCookieSettings();
Michael Kolbc831b632011-05-11 09:30:34 -0700323 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500324 // Not able to restore so we go ahead and clear session cookies. We
325 // must do this before trying to login the user as we don't want to
326 // clear any session cookies set during login.
327 CookieManager.getInstance().removeSessionCookie();
328 }
329
Patrick Scottd43e75a2011-03-14 14:47:23 -0400330 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500331 new Runnable() {
332 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800333 onPreloginFinished(icicle, intent, currentTabId,
334 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500335 }
336 });
337 }
338
Michael Kolbc831b632011-05-11 09:30:34 -0700339 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800340 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700341 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700342 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800343 if (intent == null) {
344 // This won't happen under common scenarios. The icicle is
345 // not null, but there aren't any tabs to restore.
346 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700347 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800348 final Bundle extra = intent.getExtras();
349 // Create an initial tab.
350 // If the intent is ACTION_VIEW and data is not null, the Browser is
351 // invoked to view the content by another application. In this case,
352 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800353 UrlData urlData = null;
354 if (intent.getData() != null
355 && Intent.ACTION_VIEW.equals(intent.getAction())
356 && intent.getData().toString().startsWith("content://")) {
357 urlData = new UrlData(intent.getData().toString());
358 } else {
359 urlData = IntentHandler.getUrlDataFromIntent(intent);
360 }
George Mount3636d0a2011-11-21 09:08:21 -0800361 Tab t = null;
362 if (urlData.isEmpty()) {
Vivek Sekharce2a4832014-03-26 13:26:53 -0700363 Resources res = mActivity.getResources();
364 String browserRes = res.getString(R.string.config_carrier_resource);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800365 if (browserRes.equals(
kaiyiz6e5b3e02013-08-19 20:02:01 +0800366 "cmcc")) {
367 t = openTab(OFFLINE_PAGE, false, true, true);
368 } else {
369 t = openTabToHomePage();
370 }
George Mount3636d0a2011-11-21 09:08:21 -0800371 } else {
372 t = openTab(urlData);
373 }
374 if (t != null) {
375 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
376 }
377 WebView webView = t.getWebView();
378 if (extra != null) {
379 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
380 if (scale > 0 && scale <= 1000) {
381 webView.setInitialScale(scale);
382 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700383 }
384 }
John Reckd8c74522011-06-14 08:45:00 -0700385 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700386 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700387 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500388 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700389 List<Tab> tabs = mTabControl.getTabs();
390 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
391 for (Tab t : tabs) {
392 restoredTabs.add(t.getId());
393 }
394 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700395 if (tabs.size() == 0) {
396 openTabToHomePage();
397 }
John Reck1cf4b792011-07-26 10:22:22 -0700398 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700399 // TabControl.restoreState() will create a new tab even if
400 // restoring the state fails.
401 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800402 // Intent is non-null when framework thinks the browser should be
403 // launching with a new intent (icicle is null).
404 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700405 mIntentHandler.onNewIntent(intent);
406 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700407 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700408 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700409 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800410 if (jsFlags.trim().length() != 0) {
411 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700412 }
George Mount3636d0a2011-11-21 09:08:21 -0800413 if (intent != null
414 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700415 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800416 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700417 }
418
John Reck1cf4b792011-07-26 10:22:22 -0700419 private static class PruneThumbnails implements Runnable {
420 private Context mContext;
421 private List<Long> mIds;
422
423 PruneThumbnails(Context context, List<Long> preserveIds) {
424 mContext = context.getApplicationContext();
425 mIds = preserveIds;
426 }
427
428 @Override
429 public void run() {
430 ContentResolver cr = mContext.getContentResolver();
431 if (mIds == null || mIds.size() == 0) {
432 cr.delete(Thumbnails.CONTENT_URI, null, null);
433 } else {
434 int length = mIds.size();
435 StringBuilder where = new StringBuilder();
436 where.append(Thumbnails._ID);
437 where.append(" not in (");
438 for (int i = 0; i < length; i++) {
439 where.append(mIds.get(i));
440 if (i < (length - 1)) {
441 where.append(",");
442 }
443 }
444 where.append(")");
445 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
446 }
447 }
448
449 }
450
Michael Kolb1514bb72010-11-22 09:11:48 -0800451 @Override
452 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 return mFactory;
454 }
455
456 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800457 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800458 if (tab.hasCrashed)
459 tab.showCrashView();
460 else
461 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800462 }
463
464 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800465 public void createSubWindow(Tab tab) {
466 endActionMode();
467 WebView mainView = tab.getWebView();
468 WebView subView = mFactory.createWebView((mainView == null)
469 ? false
470 : mainView.isPrivateBrowsingEnabled());
471 mUi.createSubWindow(tab, subView);
472 }
473
474 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700475 public Context getContext() {
476 return mActivity;
477 }
478
479 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700480 public Activity getActivity() {
481 return mActivity;
482 }
483
484 void setUi(UI ui) {
485 mUi = ui;
486 }
487
Michael Kolbaf63dba2012-05-16 12:58:05 -0700488 @Override
489 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700490 return mSettings;
491 }
492
493 IntentHandler getIntentHandler() {
494 return mIntentHandler;
495 }
496
497 @Override
498 public UI getUi() {
499 return mUi;
500 }
501
502 int getMaxTabs() {
503 return mActivity.getResources().getInteger(R.integer.max_tabs);
504 }
505
506 @Override
507 public TabControl getTabControl() {
508 return mTabControl;
509 }
510
Michael Kolb1bf23132010-11-19 12:55:12 -0800511 @Override
512 public List<Tab> getTabs() {
513 return mTabControl.getTabs();
514 }
515
John Reckaf262e72011-07-25 13:55:44 -0700516 // Open the icon database.
517 private void openIconDatabase() {
518 // We have to call getInstance on the UI thread
519 final WebIconDatabase instance = WebIconDatabase.getInstance();
520 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000521
John Reckaf262e72011-07-25 13:55:44 -0700522 @Override
523 public void run() {
524 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 }
John Reckaf262e72011-07-25 13:55:44 -0700526 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 }
528
529 private void startHandler() {
530 mHandler = new Handler() {
531
532 @Override
533 public void handleMessage(Message msg) {
534 switch (msg.what) {
535 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700536 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700537 break;
538 case FOCUS_NODE_HREF:
539 {
540 String url = (String) msg.getData().get("url");
541 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500542 String src = (String) msg.getData().get("src");
543 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700544 if (TextUtils.isEmpty(url)) {
545 break;
546 }
547 HashMap focusNodeMap = (HashMap) msg.obj;
548 WebView view = (WebView) focusNodeMap.get("webview");
549 // Only apply the action if the top window did not change.
550 if (getCurrentTopWebView() != view) {
551 break;
552 }
553 switch (msg.arg1) {
554 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700555 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500557 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700558 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500559 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500560 case R.id.open_newtab_context_menu_id:
561 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700562 openTab(url, parent,
563 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500564 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700565 case R.id.copy_link_context_menu_id:
566 copy(url);
567 break;
568 case R.id.save_link_context_menu_id:
569 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500570 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800571 mActivity, url, view.getSettings().getUserAgentString(),
572 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700573 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700574 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700575 if(title == null || title == "")
576 title = url;
577
578 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
579 //SWE TODO: No thumbnail support for the url obtained via
580 //browser context menu as its not loaded in webview.
581 if (bookmarkIntent != null) {
582 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
583 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
584 mActivity.startActivity(bookmarkIntent);
585 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700586 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700587 }
588 break;
589 }
590
591 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700592 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 break;
594
595 case STOP_LOAD:
596 stopLoading();
597 break;
598
599 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700600 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700601 mWakeLock.release();
602 // if we reach here, Browser should be still in the
603 // background loading after WAKELOCK_TIMEOUT (5-min).
604 // To avoid burning the battery, stop loading.
605 mTabControl.stopAllLoading();
606 }
607 break;
608
609 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800610 Tab tab = (Tab) msg.obj;
611 if (tab != null) {
612 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 }
614 break;
kaiyiz591110b2013-08-06 17:11:06 +0800615 case OPEN_MENU:
616 if (!mOptionsMenuOpen && mActivity != null ) {
617 mActivity.openOptionsMenu();
618 }
619 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 }
621 }
622 };
623
624 }
625
John Reckef654f12011-07-12 16:42:08 -0700626 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200627 public Tab getCurrentTab() {
628 return mTabControl.getCurrentTab();
629 }
630
Michael Kolbba99c5d2010-11-29 14:57:41 -0800631 @Override
632 public void shareCurrentPage() {
633 shareCurrentPage(mTabControl.getCurrentTab());
634 }
635
636 private void shareCurrentPage(Tab tab) {
637 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800638 sharePage(mActivity, tab.getTitle(),
639 tab.getUrl(), tab.getFavicon(),
640 createScreenshot(tab.getWebView(),
641 getDesiredThumbnailWidth(mActivity),
642 getDesiredThumbnailHeight(mActivity)));
643 }
644 }
645
Michael Kolb8233fac2010-10-26 16:08:53 -0700646 /**
647 * Share a page, providing the title, url, favicon, and a screenshot. Uses
648 * an {@link Intent} to launch the Activity chooser.
649 * @param c Context used to launch a new Activity.
650 * @param title Title of the page. Stored in the Intent with
651 * {@link Intent#EXTRA_SUBJECT}
652 * @param url URL of the page. Stored in the Intent with
653 * {@link Intent#EXTRA_TEXT}
654 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
655 * with {@link Browser#EXTRA_SHARE_FAVICON}
656 * @param screenshot Bitmap of a screenshot of the page. Stored in the
657 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
658 */
659 static final void sharePage(Context c, String title, String url,
660 Bitmap favicon, Bitmap screenshot) {
661 Intent send = new Intent(Intent.ACTION_SEND);
662 send.setType("text/plain");
663 send.putExtra(Intent.EXTRA_TEXT, url);
664 send.putExtra(Intent.EXTRA_SUBJECT, title);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800665 send.putExtra(EXTRA_SHARE_FAVICON, favicon);
666 send.putExtra(EXTRA_SHARE_SCREENSHOT, screenshot);
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 try {
668 c.startActivity(Intent.createChooser(send, c.getString(
669 R.string.choosertitle_sharevia)));
670 } catch(android.content.ActivityNotFoundException ex) {
671 // if no app handles it, do nothing
672 }
673 }
674
675 private void copy(CharSequence text) {
676 ClipboardManager cm = (ClipboardManager) mActivity
677 .getSystemService(Context.CLIPBOARD_SERVICE);
678 cm.setText(text);
679 }
680
681 // lifecycle
682
John Reck9c35b9c2012-05-30 10:08:50 -0700683 @Override
684 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700685 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800686 // update the menu in case of a locale change
687 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800688 if (mOptionsMenuOpen) {
689 mActivity.closeOptionsMenu();
690 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
691 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 if (mPageDialogsHandler != null) {
693 mPageDialogsHandler.onConfigurationChanged(config);
694 }
695 mUi.onConfigurationChanged(config);
696 }
697
698 @Override
699 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700700 if (!mUi.isWebShowing()) {
701 mUi.showWeb(false);
702 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 mIntentHandler.onNewIntent(intent);
704 }
705
John Reck9c35b9c2012-05-30 10:08:50 -0700706 @Override
707 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800708 if (mUi.isCustomViewShowing()) {
709 hideCustomView();
710 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700711 if (mActivityPaused) {
712 Log.e(LOGTAG, "BrowserActivity is already paused.");
713 return;
714 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800716 Tab tab = mTabControl.getCurrentTab();
717 if (tab != null) {
718 tab.pause();
719 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700720 if (mWakeLock == null) {
721 PowerManager pm = (PowerManager) mActivity
722 .getSystemService(Context.POWER_SERVICE);
723 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
724 }
Michael Kolb70976932010-11-30 11:34:01 -0800725 mWakeLock.acquire();
726 mHandler.sendMessageDelayed(mHandler
727 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
728 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700729 }
730 mUi.onPause();
731 mNetworkHandler.onPause();
732
733 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100734 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700735 if (sThumbnailBitmap != null) {
736 sThumbnailBitmap.recycle();
737 sThumbnailBitmap = null;
738 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 }
740
John Reck9c35b9c2012-05-30 10:08:50 -0700741 @Override
742 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700743 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800744 Bundle saveState = createSaveState();
745
746 // crash recovery manages all save & restore state
747 mCrashRecoveryHandler.writeState(saveState);
748 mSettings.setLastRunPaused(true);
749 }
750
751 /**
752 * Save the current state to outState. Does not write the state to
753 * disk.
754 * @return Bundle containing the current state of all tabs.
755 */
756 /* package */ Bundle createSaveState() {
757 Bundle saveState = new Bundle();
758 mTabControl.saveState(saveState);
759 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700760 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800761 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700762 }
George Mount3636d0a2011-11-21 09:08:21 -0800763 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 }
765
John Reck9c35b9c2012-05-30 10:08:50 -0700766 @Override
767 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700768 if (!mActivityPaused) {
769 Log.e(LOGTAG, "BrowserActivity is already resumed.");
770 return;
771 }
George Mount3636d0a2011-11-21 09:08:21 -0800772 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700773 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800774 Tab current = mTabControl.getCurrentTab();
775 if (current != null) {
776 current.resume();
777 resumeWebViewTimers(current);
778 }
John Reckf57c0292011-07-21 18:15:39 -0700779 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200780
Michael Kolb8233fac2010-10-26 16:08:53 -0700781 mUi.onResume();
782 mNetworkHandler.onResume();
783 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100784 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700785 if (mVoiceResult != null) {
786 mUi.onVoiceResult(mVoiceResult);
787 mVoiceResult = null;
788 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800789 if (current != null && current.hasCrashed) {
790 current.showCrashView();
791 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 }
793
John Reckf57c0292011-07-21 18:15:39 -0700794 private void releaseWakeLock() {
795 if (mWakeLock != null && mWakeLock.isHeld()) {
796 mHandler.removeMessages(RELEASE_WAKELOCK);
797 mWakeLock.release();
798 }
799 }
800
Michael Kolb70976932010-11-30 11:34:01 -0800801 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800802 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800803 * @param tab guaranteed non-null
804 */
805 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 boolean inLoad = tab.inPageLoad();
807 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
808 CookieSyncManager.getInstance().startSync();
809 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100810 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 }
812 }
813
Michael Kolb70976932010-11-30 11:34:01 -0800814 /**
815 * Pause all WebView timers using the WebView of the given tab
816 * @param tab
817 * @return true if the timers are paused or tab is null
818 */
819 private boolean pauseWebViewTimers(Tab tab) {
820 if (tab == null) {
821 return true;
822 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700823 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100824 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700826 }
Michael Kolb70976932010-11-30 11:34:01 -0800827 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700828 }
829
John Reck9c35b9c2012-05-30 10:08:50 -0700830 @Override
831 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800832 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700833 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
834 mUploadHandler = null;
835 }
836 if (mTabControl == null) return;
837 mUi.onDestroy();
838 // Remove the current tab and sub window
839 Tab t = mTabControl.getCurrentTab();
840 if (t != null) {
841 dismissSubWindow(t);
842 removeTab(t);
843 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500844 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 // Destroy all the tabs
846 mTabControl.destroy();
847 WebIconDatabase.getInstance().close();
848 // Stop watching the default geolocation permissions
849 mSystemAllowGeolocationOrigins.stop();
850 mSystemAllowGeolocationOrigins = null;
851 }
852
853 protected boolean isActivityPaused() {
854 return mActivityPaused;
855 }
856
John Reck9c35b9c2012-05-30 10:08:50 -0700857 @Override
858 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 mTabControl.freeMemory();
860 }
861
862 @Override
863 public boolean shouldShowErrorConsole() {
864 return mShouldShowErrorConsole;
865 }
866
867 protected void setShouldShowErrorConsole(boolean show) {
868 if (show == mShouldShowErrorConsole) {
869 // Nothing to do.
870 return;
871 }
872 mShouldShowErrorConsole = show;
873 Tab t = mTabControl.getCurrentTab();
874 if (t == null) {
875 // There is no current tab so we cannot toggle the error console
876 return;
877 }
878 mUi.setShouldShowErrorConsole(t, show);
879 }
880
881 @Override
882 public void stopLoading() {
883 mLoadStopped = true;
884 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700886 if (w != null) {
887 w.stopLoading();
888 mUi.onPageStopped(tab);
889 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 }
891
892 boolean didUserStopLoading() {
893 return mLoadStopped;
894 }
895
kaiyiza016da12013-08-26 17:50:22 +0800896 private void handleNetworkNotify(WebView view) {
897 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
898 Context.CONNECTIVITY_SERVICE);
899 WifiManager wifiMgr = (WifiManager) this.getContext()
900 .getSystemService(Context.WIFI_SERVICE);
901 int networkSwitchTypeOK = this.getContext().getResources()
902 .getInteger(R.integer.netswitch_type_remind);
903
904 if (wifiMgr.isWifiEnabled()) {
905 NetworkInfo mNetworkInfo = conMgr.getActiveNetworkInfo();
906 if (mNetworkInfo == null
907 || (mNetworkInfo != null && (mNetworkInfo.getType() !=
908 ConnectivityManager.TYPE_WIFI))) {
kaiyizb7bf1f22013-10-02 11:42:23 +0800909 List<ScanResult> list = wifiMgr.getScanResults();
910 if (list != null && list.size() == 0) {
911 int isReminder = Settings.System.getInt(
912 mActivity.getContentResolver(),
913 this.getContext().getResources()
914 .getString(R.string.network_switch_remind_type),
915 networkSwitchTypeOK);
916 if (isReminder == networkSwitchTypeOK) {
917 Intent intent = new Intent(
918 INTENT_WIFI_SELECTION_DATA_CONNECTION);
919 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
920 this.getContext().startActivity(intent);
921 }
922 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800923 if ((Boolean)ReflectHelper.invokeStaticMethod(
924 "ActivityManagerNative", "isSystemReady", null, null)) {
kaiyizb7bf1f22013-10-02 11:42:23 +0800925 try {
926 Intent intent = new Intent(INTENT_PICK_NETWORK);
927 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
928 this.getContext().startActivity(intent);
929 } catch (Exception e) {
930 String err_msg = this.getContext().getString(
931 R.string.acivity_not_found, INTENT_PICK_NETWORK);
932 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
933 }
934 } else {
935 Log.e(LOGTAG, "System is not ready!");
936 }
kaiyiza016da12013-08-26 17:50:22 +0800937 }
938 mNetworkShouldNotify = false;
939 }
940 } else {
941 if (!mNetworkHandler.isNetworkUp()) {
942 view.setNetworkAvailable(false);
943 Log.v(LOGTAG, "handleNetworkNotify() Wlan is not enabled.");
944 }
945 }
946 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700947 // WebViewController
948
949 @Override
John Reck324d4402011-01-11 16:56:42 -0800950 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700951
952 // We've started to load a new page. If there was a pending message
953 // to save a screenshot then we will now take the new page and save
954 // an incorrect screenshot. Therefore, remove any pending thumbnail
955 // messages from the queue.
956 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800957 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700958
959 // reset sync timer to avoid sync starts during loading a page
960 CookieSyncManager.getInstance().resetSync();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800961 Object[] params = {new String(PROP_NETSWITCH),
962 new Boolean(false)};
963 Class[] type = new Class[] {String.class, boolean.class};
964 Boolean result = (Boolean) ReflectHelper.invokeStaticMethod(
965 "android.os.SystemProperties", "getBoolean",
966 type, params);
967 if (result) {
kaiyiza016da12013-08-26 17:50:22 +0800968 if (!mNetworkHandler.isNetworkUp()) {
969 Log.d(LOGTAG, "onPageStarted() network unavailable");
970 if (mNetworkShouldNotify) {
971 handleNetworkNotify(view);
972 } else {
973 view.setNetworkAvailable(false);
974 }
975 mNetworkShouldNotify = false;
976 } else {
977 Log.d(LOGTAG, "onPageStarted() network available");
978 if (mNetworkShouldNotify) {
979 handleNetworkNotify(view);
980 }
981 mNetworkShouldNotify = false;
982 }
983 } else {
984 if (!mNetworkHandler.isNetworkUp()) {
985 view.setNetworkAvailable(false);
986 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 }
988
989 // when BrowserActivity just starts, onPageStarted may be called before
990 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
991 // to start the timer. As we won't switch tabs while an activity is in
992 // pause state, we can ensure calling resume and pause in pair.
993 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800994 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700995 }
996 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 endActionMode();
998
John Reck30c714c2010-12-16 17:30:34 -0800999 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001000
John Reck324d4402011-01-11 16:56:42 -08001001 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001002 // update the bookmark database for favicon
1003 maybeUpdateFavicon(tab, null, url, favicon);
1004
1005 Performance.tracePageStart(url);
1006
1007 // Performance probe
1008 if (false) {
1009 Performance.onPageStarted();
1010 }
1011
1012 }
1013
1014 @Override
John Reck324d4402011-01-11 16:56:42 -08001015 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -07001016 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -08001017 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001018
Michael Kolb8233fac2010-10-26 16:08:53 -07001019 // Performance probe
1020 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001021 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001022 }
1023
1024 Performance.tracePageFinished();
1025 }
1026
1027 @Override
John Reck30c714c2010-12-16 17:30:34 -08001028 public void onProgressChanged(Tab tab) {
1029 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001030
1031 if (newProgress == 100) {
1032 CookieSyncManager.getInstance().sync();
1033 // onProgressChanged() may continue to be called after the main
1034 // frame has finished loading, as any remaining sub frames continue
1035 // to load. We'll only get called once though with newProgress as
1036 // 100 when everything is loaded. (onPageFinished is called once
1037 // when the main frame completes loading regardless of the state of
1038 // any sub frames so calls to onProgressChanges may continue after
1039 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001040 if (tab.inPageLoad()) {
1041 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +02001042 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
1043 // pause the WebView timer and release the wake lock if it is
1044 // finished while BrowserActivity is in pause state.
1045 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 }
John Reckd9862372012-02-21 15:04:50 -08001047 if (!tab.isPrivateBrowsingEnabled()
1048 && !TextUtils.isEmpty(tab.getUrl())
1049 && !tab.isSnapshot()) {
1050 // Only update the bookmark screenshot if the user did not
1051 // cancel the load early and there is not already
1052 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001053 if (tab.shouldUpdateThumbnail() &&
1054 (tab.inForeground() && !didUserStopLoading()
1055 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001056 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1057 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1058 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001059 1500);
John Reckd9862372012-02-21 15:04:50 -08001060 }
1061 }
1062 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001064 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001065 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001066 // still loading
1067 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001068 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001069 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001070 }
1071 }
John Reck30c714c2010-12-16 17:30:34 -08001072 mUi.onProgressChanged(tab);
1073 }
1074
1075 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001076 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001077 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001078 }
1079
1080 @Override
1081 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001082 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001083 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001084 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1086 return;
1087 }
1088 // Update the title in the history database if not in private browsing mode
1089 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001090 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 }
1092 }
1093
1094 @Override
1095 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001096 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1098 }
1099
1100 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001101 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1102 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 }
1104
1105 @Override
1106 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1107 if (mMenuIsDown) {
1108 // only check shortcut key when MENU is held
1109 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1110 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001111 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001112 int keyCode = event.getKeyCode();
1113 // We need to send almost every key to WebKit. However:
1114 // 1. We don't want to block the device on the renderer for
1115 // some keys like menu, home, call.
1116 // 2. There are no WebKit equivalents for some of these keys
1117 // (see app/keyboard_codes_win.h)
1118 // Note that these are not the same set as KeyEvent.isSystemKey:
1119 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1120 if (keyCode == KeyEvent.KEYCODE_MENU ||
1121 keyCode == KeyEvent.KEYCODE_HOME ||
1122 keyCode == KeyEvent.KEYCODE_BACK ||
1123 keyCode == KeyEvent.KEYCODE_CALL ||
1124 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1125 keyCode == KeyEvent.KEYCODE_POWER ||
1126 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1127 keyCode == KeyEvent.KEYCODE_CAMERA ||
1128 keyCode == KeyEvent.KEYCODE_FOCUS ||
1129 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1130 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1131 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1132 return true;
1133 }
1134
1135 // We also have to intercept some shortcuts before we send them to the ContentView.
1136 if (event.isCtrlPressed() && (
1137 keyCode == KeyEvent.KEYCODE_TAB ||
1138 keyCode == KeyEvent.KEYCODE_W ||
1139 keyCode == KeyEvent.KEYCODE_F4)) {
1140 return true;
1141 }
1142
1143 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 }
1145
1146 @Override
John Reck997b1b72012-04-19 18:08:25 -07001147 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 if (!isActivityPaused()) {
1149 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001150 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 } else {
John Reck997b1b72012-04-19 18:08:25 -07001152 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001153 }
1154 }
John Reck997b1b72012-04-19 18:08:25 -07001155 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 }
1157
1158 @Override
John Reck324d4402011-01-11 16:56:42 -08001159 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 // Don't save anything in private browsing mode
1161 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -08001162 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001163
John Reck324d4402011-01-11 16:56:42 -08001164 if (TextUtils.isEmpty(url)
1165 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 return;
1167 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001168
John Reckf57c0292011-07-21 18:15:39 -07001169 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001170 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001171 }
1172
1173 @Override
1174 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1175 AsyncTask<Void, Void, String[]> task =
1176 new AsyncTask<Void, Void, String[]>() {
1177 @Override
1178 public String[] doInBackground(Void... unused) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001179 Object[] params = {mActivity.getContentResolver()};
1180 Class[] type = new Class[] {ContentResolver.class};
1181 return (String[])ReflectHelper.invokeStaticMethod(
1182 "android.provider.Browser","getVisitedHistory",
1183 type, params);
Michael Kolb8233fac2010-10-26 16:08:53 -07001184 }
1185 @Override
1186 public void onPostExecute(String[] result) {
1187 callback.onReceiveValue(result);
1188 }
1189 };
1190 task.execute();
1191 }
1192
1193 @Override
1194 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1195 final HttpAuthHandler handler, final String host,
1196 final String realm) {
1197 String username = null;
1198 String password = null;
1199
1200 boolean reuseHttpAuthUsernamePassword
1201 = handler.useHttpAuthUsernamePassword();
1202
1203 if (reuseHttpAuthUsernamePassword && view != null) {
1204 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1205 if (credentials != null && credentials.length == 2) {
1206 username = credentials[0];
1207 password = credentials[1];
1208 }
1209 }
1210
1211 if (username != null && password != null) {
1212 handler.proceed(username, password);
1213 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001214 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001215 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1216 } else {
1217 handler.cancel();
1218 }
1219 }
1220 }
1221
1222 @Override
1223 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001224 String contentDisposition, String mimetype, String referer,
1225 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001226 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001227 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001228 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001229 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001230 // This Tab was opened for the sole purpose of downloading a
1231 // file. Remove it.
1232 if (tab == mTabControl.getCurrentTab()) {
1233 // In this case, the Tab is still on top.
1234 goBackOnePageOrQuit();
1235 } else {
1236 // In this case, it is not.
1237 closeTab(tab);
1238 }
1239 }
1240 }
1241
1242 @Override
1243 public Bitmap getDefaultVideoPoster() {
1244 return mUi.getDefaultVideoPoster();
1245 }
1246
1247 @Override
1248 public View getVideoLoadingProgressView() {
1249 return mUi.getVideoLoadingProgressView();
1250 }
1251
1252 @Override
1253 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1254 SslError error) {
1255 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1256 }
1257
Patrick Scott92066772011-03-10 08:46:27 -05001258 @Override
1259 public void showAutoLogin(Tab tab) {
1260 assert tab.inForeground();
1261 // Update the title bar to show the auto-login request.
1262 mUi.showAutoLogin(tab);
1263 }
1264
1265 @Override
1266 public void hideAutoLogin(Tab tab) {
1267 assert tab.inForeground();
1268 mUi.hideAutoLogin(tab);
1269 }
1270
Michael Kolb8233fac2010-10-26 16:08:53 -07001271 // helper method
1272
1273 /*
1274 * Update the favorites icon if the private browsing isn't enabled and the
1275 * icon is valid.
1276 */
1277 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1278 final String url, Bitmap favicon) {
1279 if (favicon == null) {
1280 return;
1281 }
1282 if (!tab.isPrivateBrowsingEnabled()) {
1283 Bookmarks.updateFavicon(mActivity
1284 .getContentResolver(), originalUrl, url, favicon);
1285 }
1286 }
1287
Leon Scroggins4cd97792010-12-03 15:31:56 -05001288 @Override
1289 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001290 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001291 mUi.bookmarkedStatusHasChanged(tab);
1292 }
1293
Michael Kolb8233fac2010-10-26 16:08:53 -07001294 // end WebViewController
1295
1296 protected void pageUp() {
1297 getCurrentTopWebView().pageUp(false);
1298 }
1299
1300 protected void pageDown() {
1301 getCurrentTopWebView().pageDown(false);
1302 }
1303
1304 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001305 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001306 public void editUrl() {
1307 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001308 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001309 }
1310
John Reck9c35b9c2012-05-30 10:08:50 -07001311 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001312 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001313 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001314 if (tab.inForeground()) {
1315 if (mUi.isCustomViewShowing()) {
1316 callback.onCustomViewHidden();
1317 return;
1318 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001319 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001320 // Save the menu state and set it to empty while the custom
1321 // view is showing.
1322 mOldMenuState = mMenuState;
1323 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001324 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001325 }
1326 }
1327
1328 @Override
1329 public void hideCustomView() {
1330 if (mUi.isCustomViewShowing()) {
1331 mUi.onHideCustomView();
1332 // Reset the old menu state.
1333 mMenuState = mOldMenuState;
1334 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001335 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001336 }
1337 }
1338
John Reck9c35b9c2012-05-30 10:08:50 -07001339 @Override
1340 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001341 Intent intent) {
1342 if (getCurrentTopWebView() == null) return;
1343 switch (requestCode) {
1344 case PREFERENCES_PAGE:
1345 if (resultCode == Activity.RESULT_OK && intent != null) {
1346 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001347 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001348 mTabControl.removeParentChildRelationShips();
1349 }
1350 }
1351 break;
1352 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001353 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001354 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001355 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001356 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001357 case AUTOFILL_SETUP:
1358 // Determine whether a profile was actually set up or not
1359 // and if so, send the message back to the WebTextView to
1360 // fill the form with the new profile.
1361 if (getSettings().getAutoFillProfile() != null) {
1362 mAutoFillSetupMessage.sendToTarget();
1363 mAutoFillSetupMessage = null;
1364 }
1365 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001366 case COMBO_VIEW:
1367 if (intent == null || resultCode != Activity.RESULT_OK) {
1368 break;
1369 }
John Reck3ba45532011-08-11 16:26:53 -07001370 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001371 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1372 Tab t = getCurrentTab();
1373 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001374 mUpdateMyNavThumbnail = true;
1375 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001376 loadUrl(t, uri.toString());
1377 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1378 String[] urls = intent.getStringArrayExtra(
1379 ComboViewActivity.EXTRA_OPEN_ALL);
1380 Tab parent = getCurrentTab();
1381 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001382 if (url != null) {
1383 parent = openTab(url, parent,
1384 !mSettings.openInBackground(), true);
1385 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001386 }
1387 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1388 long id = intent.getLongExtra(
1389 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1390 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001391 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001392 }
1393 }
1394 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001395 case VOICE_RESULT:
1396 if (resultCode == Activity.RESULT_OK && intent != null) {
1397 ArrayList<String> results = intent.getStringArrayListExtra(
1398 RecognizerIntent.EXTRA_RESULTS);
1399 if (results.size() >= 1) {
1400 mVoiceResult = results.get(0);
1401 }
1402 }
1403 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001404 case MY_NAVIGATION:
1405 if (intent == null || resultCode != Activity.RESULT_OK) {
1406 break;
1407 }
1408
1409 if (intent.getBooleanExtra("need_refresh", false) &&
1410 getCurrentTopWebView() != null) {
1411 getCurrentTopWebView().reload();
1412 }
1413 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 default:
1415 break;
1416 }
1417 getCurrentTopWebView().requestFocus();
1418 }
1419
1420 /**
1421 * Open the Go page.
1422 * @param startWithHistory If true, open starting on the history tab.
1423 * Otherwise, start with the bookmarks tab.
1424 */
1425 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001426 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001427 if (mTabControl.getCurrentWebView() == null) {
1428 return;
1429 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001430 // clear action mode
1431 if (isInCustomActionMode()) {
1432 endActionMode();
1433 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001434 Bundle extras = new Bundle();
1435 // Disable opening in a new window if we have maxed out the windows
1436 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1437 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001438 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001439 }
1440
1441 // combo view callbacks
1442
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 // key handling
1444 protected void onBackKey() {
1445 if (!mUi.onBackKey()) {
1446 WebView subwindow = mTabControl.getCurrentSubWindow();
1447 if (subwindow != null) {
1448 if (subwindow.canGoBack()) {
1449 subwindow.goBack();
1450 } else {
1451 dismissSubWindow(mTabControl.getCurrentTab());
1452 }
1453 } else {
1454 goBackOnePageOrQuit();
1455 }
1456 }
1457 }
1458
Michael Kolb4bd767d2011-05-27 11:33:55 -07001459 protected boolean onMenuKey() {
1460 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001461 }
1462
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 // menu handling and state
1464 // TODO: maybe put into separate handler
1465
John Reck9c35b9c2012-05-30 10:08:50 -07001466 @Override
1467 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001468 if (mMenuState == EMPTY_MENU) {
1469 return false;
1470 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001471 MenuInflater inflater = mActivity.getMenuInflater();
1472 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001473 return true;
1474 }
1475
John Reck9c35b9c2012-05-30 10:08:50 -07001476 @Override
1477 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001479 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001480 return;
1481 }
1482 if (!(v instanceof WebView)) {
1483 return;
1484 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001485 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001486 WebView.HitTestResult result = webview.getHitTestResult();
1487 if (result == null) {
1488 return;
1489 }
1490
1491 int type = result.getType();
1492 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1493 Log.w(LOGTAG,
1494 "We should not show context menu when nothing is touched");
1495 return;
1496 }
1497 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1498 // let TextView handles context menu
1499 return;
1500 }
1501
1502 // Note, http://b/issue?id=1106666 is requesting that
1503 // an inflated menu can be used again. This is not available
1504 // yet, so inflate each time (yuk!)
1505 MenuInflater inflater = mActivity.getMenuInflater();
1506 inflater.inflate(R.menu.browsercontext, menu);
1507
1508 // Show the correct menu group
1509 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001510 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001511 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001512 menu.setGroupVisible(R.id.PHONE_MENU,
1513 type == WebView.HitTestResult.PHONE_TYPE);
1514 menu.setGroupVisible(R.id.EMAIL_MENU,
1515 type == WebView.HitTestResult.EMAIL_TYPE);
1516 menu.setGroupVisible(R.id.GEO_MENU,
1517 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001518 String itemUrl = null;
1519 String url = webview.getOriginalUrl();
1520 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1521 itemUrl = Uri.decode(navigationUrl);
1522 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1523 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1524 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1525 } else {
1526 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1527 }
1528 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1529 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1530 } else {
1531 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1532
1533 menu.setGroupVisible(R.id.IMAGE_MENU,
1534 type == WebView.HitTestResult.IMAGE_TYPE
1535 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1536 menu.setGroupVisible(R.id.ANCHOR_MENU,
1537 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1538 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1539 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001540 // Setup custom handling depending on the type
1541 switch (type) {
1542 case WebView.HitTestResult.PHONE_TYPE:
1543 menu.setHeaderTitle(Uri.decode(extra));
1544 menu.findItem(R.id.dial_context_menu_id).setIntent(
1545 new Intent(Intent.ACTION_VIEW, Uri
1546 .parse(WebView.SCHEME_TEL + extra)));
1547 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1548 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1549 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1550 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1551 addIntent);
1552 menu.findItem(R.id.copy_phone_context_menu_id)
1553 .setOnMenuItemClickListener(
1554 new Copy(extra));
1555 break;
1556
1557 case WebView.HitTestResult.EMAIL_TYPE:
1558 menu.setHeaderTitle(extra);
1559 menu.findItem(R.id.email_context_menu_id).setIntent(
1560 new Intent(Intent.ACTION_VIEW, Uri
1561 .parse(WebView.SCHEME_MAILTO + extra)));
1562 menu.findItem(R.id.copy_mail_context_menu_id)
1563 .setOnMenuItemClickListener(
1564 new Copy(extra));
1565 break;
1566
1567 case WebView.HitTestResult.GEO_TYPE:
1568 menu.setHeaderTitle(extra);
1569 menu.findItem(R.id.map_context_menu_id).setIntent(
1570 new Intent(Intent.ACTION_VIEW, Uri
1571 .parse(WebView.SCHEME_GEO
1572 + URLEncoder.encode(extra))));
1573 menu.findItem(R.id.copy_geo_context_menu_id)
1574 .setOnMenuItemClickListener(
1575 new Copy(extra));
1576 break;
1577
1578 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1579 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001580 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 // decide whether to show the open link in new tab option
1582 boolean showNewTab = mTabControl.canCreateNewTab();
1583 MenuItem newTabItem
1584 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001585 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001586 ? R.string.contextmenu_openlink_newwindow_background
1587 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001588 newTabItem.setVisible(showNewTab);
1589 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001590 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1591 newTabItem.setOnMenuItemClickListener(
1592 new MenuItem.OnMenuItemClickListener() {
1593 @Override
1594 public boolean onMenuItemClick(MenuItem item) {
1595 final HashMap<String, WebView> hrefMap =
1596 new HashMap<String, WebView>();
1597 hrefMap.put("webview", webview);
1598 final Message msg = mHandler.obtainMessage(
1599 FOCUS_NODE_HREF,
1600 R.id.open_newtab_context_menu_id,
1601 0, hrefMap);
1602 webview.requestFocusNodeHref(msg);
1603 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001604 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001605 });
1606 } else {
1607 newTabItem.setOnMenuItemClickListener(
1608 new MenuItem.OnMenuItemClickListener() {
1609 @Override
1610 public boolean onMenuItemClick(MenuItem item) {
1611 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001612 openTab(extra, parent,
1613 !mSettings.openInBackground(),
1614 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001615 return true;
1616 }
1617 });
1618 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001619 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001620 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1621 menu.setHeaderTitle(navigationUrl);
1622 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1623
1624 if (itemUrl != null) {
1625 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1626 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1627 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1628 @Override
1629 public boolean onMenuItemClick(MenuItem item) {
1630 final Intent intent = new Intent(Controller.this
1631 .getContext(),
1632 AddMyNavigationPage.class);
1633 Bundle bundle = new Bundle();
1634 String url = Uri.decode(navigationUrl);
1635 bundle.putBoolean("isAdding", false);
1636 bundle.putString("url", url);
1637 bundle.putString("name", getNameFromUrl(url));
1638 intent.putExtra("websites", bundle);
1639 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1640 return false;
1641 }
1642 });
1643 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1644 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1645 @Override
1646 public boolean onMenuItemClick(MenuItem item) {
1647 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1648 return false;
1649 }
1650 });
1651 }
1652 } else {
1653 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1654 }
1655 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001656 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1657 break;
1658 }
1659 // otherwise fall through to handle image part
1660 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001661 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1662 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001663 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1664 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001665 shareItem.setOnMenuItemClickListener(
1666 new MenuItem.OnMenuItemClickListener() {
1667 @Override
1668 public boolean onMenuItemClick(MenuItem item) {
1669 sharePage(mActivity, null, extra, null,
1670 null);
1671 return true;
1672 }
1673 }
1674 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001675 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001676 menu.findItem(R.id.view_image_context_menu_id)
1677 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1678 @Override
1679 public boolean onMenuItemClick(MenuItem item) {
1680 openTab(extra, mTabControl.getCurrentTab(), true, true);
1681 return false;
1682 }
1683 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001684 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1685 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1686 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001687 menu.findItem(R.id.set_wallpaper_context_menu_id).
1688 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1689 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001690 break;
1691
1692 default:
1693 Log.w(LOGTAG, "We should not get here.");
1694 break;
1695 }
1696 //update the ui
1697 mUi.onContextMenuCreated(menu);
1698 }
1699
kaiyiz6e5b3e02013-08-19 20:02:01 +08001700 public void startAddMyNavigation(String url) {
1701 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1702 Bundle bundle = new Bundle();
1703 bundle.putBoolean("isAdding", true);
1704 bundle.putString("url", url);
1705 bundle.putString("name", getNameFromUrl(url));
1706 intent.putExtra("websites", bundle);
1707 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1708 }
1709
1710 private void showMyNavigationDeleteDialog(final String itemUrl) {
1711 new AlertDialog.Builder(this.getContext())
1712 .setTitle(R.string.my_navigation_delete_label)
1713 .setIcon(android.R.drawable.ic_dialog_alert)
1714 .setMessage(R.string.my_navigation_delete_msg)
1715 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1716 @Override
1717 public void onClick(DialogInterface dialog, int whichButton) {
1718 deleteMyNavigationItem(itemUrl);
1719 }
1720 })
1721 .setNegativeButton(R.string.cancel, null)
1722 .show();
1723 }
1724
1725 private void deleteMyNavigationItem(final String itemUrl) {
1726 ContentResolver cr = this.getContext().getContentResolver();
1727 Cursor cursor = null;
1728
1729 try {
1730 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1731 new String[] {
1732 MyNavigationUtil.ID
1733 }, "url = ?", new String[] {
1734 itemUrl
1735 }, null);
1736 if (null != cursor && cursor.moveToFirst()) {
1737 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1738 cursor.getLong(0));
1739
1740 ContentValues values = new ContentValues();
1741 values.put(MyNavigationUtil.TITLE, "");
1742 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1743 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1744 ByteArrayOutputStream os = new ByteArrayOutputStream();
1745 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1746 R.raw.my_navigation_add);
1747 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1748 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1749 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1750 cr.update(uri, values, null, null);
1751 } else {
1752 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1753 }
1754 } catch (IllegalStateException e) {
1755 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1756 } finally {
1757 if (null != cursor) {
1758 cursor.close();
1759 }
1760 }
1761
1762 if (getCurrentTopWebView() != null) {
1763 getCurrentTopWebView().reload();
1764 }
1765 }
1766
1767 private String getNameFromUrl(String itemUrl) {
1768 ContentResolver cr = this.getContext().getContentResolver();
1769 Cursor cursor = null;
1770 String name = null;
1771
1772 try {
1773 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1774 new String[] {
1775 MyNavigationUtil.TITLE
1776 }, "url = ?", new String[] {
1777 itemUrl
1778 }, null);
1779 if (null != cursor && cursor.moveToFirst()) {
1780 name = cursor.getString(0);
1781 } else {
1782 Log.e(LOGTAG, "this item does not exist!");
1783 }
1784 } catch (IllegalStateException e) {
1785 Log.e(LOGTAG, "getNameFromUrl", e);
1786 } finally {
1787 if (null != cursor) {
1788 cursor.close();
1789 }
1790 }
1791 return name;
1792 }
1793
1794 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1795 int width = mActivity.getResources().getDimensionPixelOffset(
1796 R.dimen.myNavigationThumbnailWidth);
1797 int height = mActivity.getResources().getDimensionPixelOffset(
1798 R.dimen.myNavigationThumbnailHeight);
1799
1800 final Bitmap bm = createScreenshot(webView, width, height);
1801
1802 if (bm == null) {
1803 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1804 return;
1805 }
1806
1807 final ContentResolver cr = mActivity.getContentResolver();
1808 new AsyncTask<Void, Void, Void>() {
1809 @Override
1810 protected Void doInBackground(Void... unused) {
1811 ContentResolver cr = mActivity.getContentResolver();
1812 Cursor cursor = null;
1813 try {
1814 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1815 new String[] {
1816 MyNavigationUtil.ID
1817 }, "url = ?", new String[] {
1818 itemUrl
1819 }, null);
1820 if (null != cursor && cursor.moveToFirst()) {
1821 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1822 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1823
1824 ContentValues values = new ContentValues();
1825 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1826 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1827 cursor.getLong(0));
1828 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1829 cr.update(uri, values, null, null);
1830 os.close();
1831 }
1832 } catch (IllegalStateException e) {
1833 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1834 } catch (IOException e) {
1835 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1836 } finally {
1837 if (null != cursor) {
1838 cursor.close();
1839 }
1840 }
1841 return null;
1842 }
1843 }.execute();
1844 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001845 /**
1846 * As the menu can be open when loading state changes
1847 * we must manually update the state of the stop/reload menu
1848 * item
1849 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001850 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001851 if (menu == null) {
1852 return;
1853 }
1854 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001855 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001856 menu.findItem(R.id.stop_menu_id):
1857 menu.findItem(R.id.reload_menu_id);
1858 if (src != null) {
1859 dest.setIcon(src.getIcon());
1860 dest.setTitle(src.getTitle());
1861 }
1862 }
1863
John Reck9c35b9c2012-05-30 10:08:50 -07001864 @Override
1865 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001866 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001867 // hold on to the menu reference here; it is used by the page callbacks
1868 // to update the menu based on loading state
1869 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001870 // Note: setVisible will decide whether an item is visible; while
1871 // setEnabled() will decide whether an item is enabled, which also means
1872 // whether the matching shortcut key will function.
1873 switch (mMenuState) {
1874 case EMPTY_MENU:
1875 if (mCurrentMenuState != mMenuState) {
1876 menu.setGroupVisible(R.id.MAIN_MENU, false);
1877 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1878 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1879 }
1880 break;
1881 default:
1882 if (mCurrentMenuState != mMenuState) {
1883 menu.setGroupVisible(R.id.MAIN_MENU, true);
1884 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1885 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1886 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001887 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001888 break;
1889 }
1890 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001891 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001892 }
1893
Michael Kolb4bf79712011-07-14 14:18:12 -07001894 @Override
1895 public void updateMenuState(Tab tab, Menu menu) {
1896 boolean canGoBack = false;
1897 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001898 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001899 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001900 if (tab != null) {
1901 canGoBack = tab.canGoBack();
1902 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001903 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001904 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001905 }
1906 final MenuItem back = menu.findItem(R.id.back_menu_id);
1907 back.setEnabled(canGoBack);
1908
1909 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001910
1911 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1912 forward.setEnabled(canGoForward);
1913
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001914 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1915 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001916 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001917 if (source != null && dest != null) {
1918 dest.setTitle(source.getTitle());
1919 dest.setIcon(source.getIcon());
1920 }
John Recke1a03a32011-09-14 17:04:16 -07001921 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001922
1923 // decide whether to show the share link option
1924 PackageManager pm = mActivity.getPackageManager();
1925 Intent send = new Intent(Intent.ACTION_SEND);
1926 send.setType("text/plain");
1927 ResolveInfo ri = pm.resolveActivity(send,
1928 PackageManager.MATCH_DEFAULT_ONLY);
1929 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1930
1931 boolean isNavDump = mSettings.enableNavDump();
1932 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1933 nav.setVisible(isNavDump);
1934 nav.setEnabled(isNavDump);
1935
1936 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001937 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1938 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001939 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1940 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001941 // history and snapshots item are the members of COMBO menu group,
1942 // so if show history item, only make snapshots item invisible.
1943 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001944
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001945 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001946 }
1947
John Reck9c35b9c2012-05-30 10:08:50 -07001948 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001949 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001950 if (null == getCurrentTopWebView()) {
1951 return false;
1952 }
1953 if (mMenuIsDown) {
1954 // The shortcut action consumes the MENU. Even if it is still down,
1955 // it won't trigger the next shortcut action. In the case of the
1956 // shortcut action triggering a new activity, like Bookmarks, we
1957 // won't get onKeyUp for MENU. So it is important to reset it here.
1958 mMenuIsDown = false;
1959 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001960 if (mUi.onOptionsItemSelected(item)) {
1961 // ui callback handled it
1962 return true;
1963 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001964 switch (item.getItemId()) {
1965 // -- Main menu
1966 case R.id.new_tab_menu_id:
1967 openTabToHomePage();
1968 break;
1969
1970 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001971 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001972 break;
1973
Afzal Najamd4e33312012-04-26 01:54:01 -04001974 case R.id.close_other_tabs_id:
1975 closeOtherTabs();
1976 break;
1977
Michael Kolb8233fac2010-10-26 16:08:53 -07001978 case R.id.goto_menu_id:
1979 editUrl();
1980 break;
1981
1982 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001983 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1984 break;
1985
1986 case R.id.history_menu_id:
1987 bookmarksOrHistoryPicker(ComboViews.History);
1988 break;
1989
1990 case R.id.snapshots_menu_id:
1991 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 break;
1993
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001995 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001996 break;
1997
1998 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001999 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002000 stopLoading();
2001 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002002 Tab currentTab = mTabControl.getCurrentTab();
2003 if (currentTab.hasCrashed) {
2004 currentTab.replaceCrashView(getCurrentTopWebView(),
2005 currentTab.getViewContainer());
2006 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002007 getCurrentTopWebView().reload();
2008 }
2009 break;
2010
2011 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002012 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002013 break;
2014
2015 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002016 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002017 break;
2018
2019 case R.id.close_menu_id:
2020 // Close the subwindow if it exists.
2021 if (mTabControl.getCurrentSubWindow() != null) {
2022 dismissSubWindow(mTabControl.getCurrentTab());
2023 break;
2024 }
2025 closeCurrentTab();
2026 break;
2027
kaiyiza8b6dbb2013-07-29 18:11:22 +08002028 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002029 Object[] params = { new String("persist.debug.browsermonkeytest")};
2030 Class[] type = new Class[] {String.class};
2031 String ret = (String)ReflectHelper.invokeStaticMethod(
2032 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002033 if (ret != null && ret.equals("enable"))
2034 break;
luxiaolb40014b2013-07-19 10:01:43 +08002035 showExitDialog(mActivity);
2036 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002037 case R.id.homepage_menu_id:
2038 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002039 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002040 break;
2041
2042 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002043 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002044 break;
2045
2046 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002047 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002048 break;
2049
John Reck2bc80422011-06-30 15:11:49 -07002050 case R.id.save_snapshot_menu_id:
2051 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002052 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07002053 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05002054 break;
2055
Michael Kolb8233fac2010-10-26 16:08:53 -07002056 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002057 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002058 break;
2059
John Recke1a03a32011-09-14 17:04:16 -07002060 case R.id.snapshot_go_live:
2061 goLive();
2062 return true;
2063
Michael Kolb8233fac2010-10-26 16:08:53 -07002064 case R.id.share_page_menu_id:
2065 Tab currentTab = mTabControl.getCurrentTab();
2066 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 return false;
2068 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002069 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002070 break;
2071
2072 case R.id.dump_nav_menu_id:
2073 getCurrentTopWebView().debugDump();
2074 break;
2075
Michael Kolb8233fac2010-10-26 16:08:53 -07002076 case R.id.zoom_in_menu_id:
2077 getCurrentTopWebView().zoomIn();
2078 break;
2079
2080 case R.id.zoom_out_menu_id:
2081 getCurrentTopWebView().zoomOut();
2082 break;
2083
2084 case R.id.view_downloads_menu_id:
2085 viewDownloads();
2086 break;
2087
John Reck42229bc2011-08-19 13:26:43 -07002088 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002089 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002090 break;
2091
Michael Kolb8233fac2010-10-26 16:08:53 -07002092 case R.id.window_one_menu_id:
2093 case R.id.window_two_menu_id:
2094 case R.id.window_three_menu_id:
2095 case R.id.window_four_menu_id:
2096 case R.id.window_five_menu_id:
2097 case R.id.window_six_menu_id:
2098 case R.id.window_seven_menu_id:
2099 case R.id.window_eight_menu_id:
2100 {
2101 int menuid = item.getItemId();
2102 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2103 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2104 Tab desiredTab = mTabControl.getTab(id);
2105 if (desiredTab != null &&
2106 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002107 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002108 }
2109 break;
2110 }
2111 }
2112 }
2113 break;
2114
2115 default:
2116 return false;
2117 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002118 return true;
2119 }
2120
John Reck68234a92012-04-19 15:27:12 -07002121 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2122 implements OnCancelListener {
2123
2124 private Tab mTab;
2125 private Dialog mProgressDialog;
2126 private ContentValues mValues;
2127
2128 private SaveSnapshotTask(Tab tab) {
2129 mTab = tab;
2130 }
2131
2132 @Override
2133 protected void onPreExecute() {
2134 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2135 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2136 true, true, this);
2137 mValues = mTab.createSnapshotValues();
2138 }
2139
2140 @Override
2141 protected Long doInBackground(Void... params) {
2142 if (!mTab.saveViewState(mValues)) {
2143 return null;
2144 }
2145 if (isCancelled()) {
2146 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2147 File file = mActivity.getFileStreamPath(path);
2148 if (!file.delete()) {
2149 file.deleteOnExit();
2150 }
2151 return null;
2152 }
2153 final ContentResolver cr = mActivity.getContentResolver();
2154 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2155 if (result == null) {
2156 return null;
2157 }
2158 long id = ContentUris.parseId(result);
2159 return id;
2160 }
2161
2162 @Override
2163 protected void onPostExecute(Long id) {
2164 if (isCancelled()) {
2165 return;
2166 }
2167 mProgressDialog.dismiss();
2168 if (id == null) {
2169 Toast.makeText(mActivity, R.string.snapshot_failed,
2170 Toast.LENGTH_SHORT).show();
2171 return;
2172 }
2173 Bundle b = new Bundle();
2174 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2175 mUi.showComboView(ComboViews.Snapshots, b);
2176 }
2177
2178 @Override
2179 public void onCancel(DialogInterface dialog) {
2180 cancel(true);
2181 }
2182 }
2183
Michael Kolb80f75082012-04-10 10:50:06 -07002184 @Override
2185 public void toggleUserAgent() {
2186 WebView web = getCurrentWebView();
2187 mSettings.toggleDesktopUseragent(web);
2188 web.loadUrl(web.getOriginalUrl());
2189 }
2190
2191 @Override
2192 public void findOnPage() {
2193 getCurrentTopWebView().showFindDialog(null, true);
2194 }
2195
2196 @Override
2197 public void openPreferences() {
2198 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2199 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2200 getCurrentTopWebView().getUrl());
2201 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2202 }
2203
2204 @Override
2205 public void bookmarkCurrentPage() {
2206 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2207 if (bookmarkIntent != null) {
2208 mActivity.startActivity(bookmarkIntent);
2209 }
2210 }
2211
John Recke1a03a32011-09-14 17:04:16 -07002212 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002213 SnapshotTab t = (SnapshotTab) getCurrentTab();
2214 t.loadUrl(t.getLiveUrl(), null);
John Recke1a03a32011-09-14 17:04:16 -07002215 }
2216
luxiaolb40014b2013-07-19 10:01:43 +08002217 private void showExitDialog(final Activity activity) {
2218 new AlertDialog.Builder(activity)
2219 .setTitle(R.string.exit_browser_title)
2220 .setIcon(android.R.drawable.ic_dialog_alert)
2221 .setMessage(R.string.exit_browser_msg)
2222 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2223 public void onClick(DialogInterface dialog, int which) {
2224 activity.moveTaskToBack(true);
2225 dialog.dismiss();
2226 }
2227 })
2228 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2229 public void onClick(DialogInterface dialog, int which) {
2230 activity.finish();
2231 mHandler.postDelayed(new Runnable() {
2232 @Override
2233 public void run() {
2234 // TODO Auto-generated method stub
2235 mCrashRecoveryHandler.clearState(true);
2236 int pid = android.os.Process.myPid();
2237 android.os.Process.killProcess(pid);
2238 }
2239 }, 300);
2240 dialog.dismiss();
2241 }
2242 })
2243 .show();
2244 }
Michael Kolb315d5022011-10-13 12:47:11 -07002245 @Override
2246 public void showPageInfo() {
2247 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2248 }
2249
John Reck9c35b9c2012-05-30 10:08:50 -07002250 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002251 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002252 // Let the History and Bookmark fragments handle menus they created.
2253 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2254 return false;
2255 }
2256
Michael Kolb8233fac2010-10-26 16:08:53 -07002257 int id = item.getItemId();
2258 boolean result = true;
2259 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002260 // -- Browser context menu
2261 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002262 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002263 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002264 case R.id.copy_link_context_menu_id:
2265 final WebView webView = getCurrentTopWebView();
2266 if (null == webView) {
2267 result = false;
2268 break;
2269 }
2270 final HashMap<String, WebView> hrefMap =
2271 new HashMap<String, WebView>();
2272 hrefMap.put("webview", webView);
2273 final Message msg = mHandler.obtainMessage(
2274 FOCUS_NODE_HREF, id, 0, hrefMap);
2275 webView.requestFocusNodeHref(msg);
2276 break;
2277
2278 default:
2279 // For other context menus
2280 result = onOptionsItemSelected(item);
2281 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002282 return result;
2283 }
2284
2285 /**
2286 * support programmatically opening the context menu
2287 */
2288 public void openContextMenu(View view) {
2289 mActivity.openContextMenu(view);
2290 }
2291
2292 /**
2293 * programmatically open the options menu
2294 */
2295 public void openOptionsMenu() {
2296 mActivity.openOptionsMenu();
2297 }
2298
John Reck9c35b9c2012-05-30 10:08:50 -07002299 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002300 public boolean onMenuOpened(int featureId, Menu menu) {
2301 if (mOptionsMenuOpen) {
2302 if (mConfigChanged) {
2303 // We do not need to make any changes to the state of the
2304 // title bar, since the only thing that happened was a
2305 // change in orientation
2306 mConfigChanged = false;
2307 } else {
2308 if (!mExtendedMenuOpen) {
2309 mExtendedMenuOpen = true;
2310 mUi.onExtendedMenuOpened();
2311 } else {
2312 // Switching the menu back to icon view, so show the
2313 // title bar once again.
2314 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002315 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002316 }
2317 }
2318 } else {
2319 // The options menu is closed, so open it, and show the title
2320 mOptionsMenuOpen = true;
2321 mConfigChanged = false;
2322 mExtendedMenuOpen = false;
2323 mUi.onOptionsMenuOpened();
2324 }
2325 return true;
2326 }
2327
John Reck9c35b9c2012-05-30 10:08:50 -07002328 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002329 public void onOptionsMenuClosed(Menu menu) {
2330 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002331 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002332 }
2333
John Reck9c35b9c2012-05-30 10:08:50 -07002334 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002335 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002336 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002337 }
2338
2339 // Helper method for getting the top window.
2340 @Override
2341 public WebView getCurrentTopWebView() {
2342 return mTabControl.getCurrentTopWebView();
2343 }
2344
2345 @Override
2346 public WebView getCurrentWebView() {
2347 return mTabControl.getCurrentWebView();
2348 }
2349
2350 /*
2351 * This method is called as a result of the user selecting the options
2352 * menu to see the download window. It shows the download window on top of
2353 * the current window.
2354 */
2355 void viewDownloads() {
2356 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2357 mActivity.startActivity(intent);
2358 }
2359
John Reck30b065e2011-07-19 10:58:05 -07002360 int getActionModeHeight() {
2361 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2362 new int[] { android.R.attr.actionBarSize });
2363 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2364 actionBarSizeTypedArray.recycle();
2365 return size;
2366 }
2367
Michael Kolb8233fac2010-10-26 16:08:53 -07002368 // action mode
2369
John Reck9c35b9c2012-05-30 10:08:50 -07002370 @Override
2371 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002372 mUi.onActionModeStarted(mode);
2373 mActionMode = mode;
2374 }
2375
2376 /*
2377 * True if a custom ActionMode (i.e. find or select) is in use.
2378 */
2379 @Override
2380 public boolean isInCustomActionMode() {
2381 return mActionMode != null;
2382 }
2383
2384 /*
2385 * End the current ActionMode.
2386 */
2387 @Override
2388 public void endActionMode() {
2389 if (mActionMode != null) {
2390 mActionMode.finish();
2391 }
2392 }
2393
2394 /*
2395 * Called by find and select when they are finished. Replace title bars
2396 * as necessary.
2397 */
John Reck9c35b9c2012-05-30 10:08:50 -07002398 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002399 public void onActionModeFinished(ActionMode mode) {
2400 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002401 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002402 mActionMode = null;
2403 }
2404
2405 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002406 final Tab tab = getCurrentTab();
2407 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002408 }
2409
2410 // bookmark handling
2411
2412 /**
2413 * add the current page as a bookmark to the given folder id
2414 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002415 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002416 * bookmarked, and if it is, edit that bookmark. If false, and
2417 * the site is already bookmarked, do not attempt to edit the
2418 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002419 */
2420 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002421 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002422 WebView w = getCurrentTopWebView();
2423 if (w == null) {
2424 return null;
2425 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002426 Intent i = new Intent(mActivity,
2427 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002428 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2429 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2430 String touchIconUrl = w.getTouchIconUrl();
2431 if (touchIconUrl != null) {
2432 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2433 WebSettings settings = w.getSettings();
2434 if (settings != null) {
2435 i.putExtra(AddBookmarkPage.USER_AGENT,
2436 settings.getUserAgentString());
2437 }
2438 }
2439 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2440 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2441 getDesiredThumbnailHeight(mActivity)));
2442 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002443 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002444 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2445 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002446 // Put the dialog at the upper right of the screen, covering the
2447 // star on the title bar.
2448 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002449 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002450 }
2451
2452 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002453 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002454 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002455 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002456 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002457 }
2458
2459 // thumbnails
2460
2461 /**
2462 * Return the desired width for thumbnail screenshots, which are stored in
2463 * the database, and used on the bookmarks screen.
2464 * @param context Context for finding out the density of the screen.
2465 * @return desired width for thumbnail screenshot.
2466 */
2467 static int getDesiredThumbnailWidth(Context context) {
2468 return context.getResources().getDimensionPixelOffset(
2469 R.dimen.bookmarkThumbnailWidth);
2470 }
2471
2472 /**
2473 * Return the desired height for thumbnail screenshots, which are stored in
2474 * the database, and used on the bookmarks screen.
2475 * @param context Context for finding out the density of the screen.
2476 * @return desired height for thumbnail screenshot.
2477 */
2478 static int getDesiredThumbnailHeight(Context context) {
2479 return context.getResources().getDimensionPixelOffset(
2480 R.dimen.bookmarkThumbnailHeight);
2481 }
2482
John Reck8cc92352011-07-06 17:41:52 -07002483 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002484 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002485 return null;
2486 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002487
2488 Bitmap viewportBitmap = view.getViewportBitmap();
2489 if (viewportBitmap == null) {
2490 return null;
2491 }
2492
2493 float aspectRatio = (float) width/height;
2494 int viewportWidth = viewportBitmap.getWidth();
2495 int viewportHeight = viewportBitmap.getHeight();
2496
2497 //modify the size to attain the same aspect ratio of desired thumbnail size
2498 if (viewportHeight > viewportWidth) {
2499 viewportHeight= (int)Math.round(viewportWidth * aspectRatio);
2500 } else {
2501 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2502 }
2503
2504 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2505 Rect dstRect = new Rect(0, 0, width, height);
2506
2507 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2508 || sThumbnailBitmap.getHeight() != height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002509 if (sThumbnailBitmap != null) {
2510 sThumbnailBitmap.recycle();
2511 sThumbnailBitmap = null;
2512 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002513
2514 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002515 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002516
John Reckd7dd9b22011-08-30 09:18:29 -07002517 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002518 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
John Reckd7dd9b22011-08-30 09:18:29 -07002519
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002520 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002521 }
2522
John Reck34ef2672011-02-10 11:30:55 -08002523 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002524 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 // FIXME: Would like to make sure there is actually something to
2526 // draw, but the API for that (WebViewCore.pictureReady()) is not
2527 // currently accessible here.
2528
John Reck34ef2672011-02-10 11:30:55 -08002529 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002530 if (view == null) {
2531 // Tab was destroyed
2532 return;
2533 }
John Reck34ef2672011-02-10 11:30:55 -08002534 final String url = tab.getUrl();
2535 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002536 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002537 if (TextUtils.isEmpty(url)) {
2538 return;
2539 }
2540
kaiyiz6e5b3e02013-08-19 20:02:01 +08002541 //update My Navigation Thumbnails
2542 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2543 if (isMyNavigationUrl) {
2544 updateMyNavigationThumbnail(url, view);
2545 }
John Reck34ef2672011-02-10 11:30:55 -08002546 // Only update thumbnails for web urls (http(s)://), not for
2547 // about:, javascript:, data:, etc...
2548 // Unless it is a bookmarked site, then always update
2549 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2550 return;
2551 }
2552
kaiyiz6e5b3e02013-08-19 20:02:01 +08002553 if (url != null && mUpdateMyNavThumbnailUrl != null
2554 && Patterns.WEB_URL.matcher(url).matches()
2555 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2556 String urlHost = (new WebAddress(url)).getHost();
2557 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2558 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2559 || bookmarkHost.length() == 0) {
2560 return;
2561 }
2562 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2563 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2564 bookmarkHost.length());
2565 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2566 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2567 if (!bookmarkDomain.equals(urlDomain)) {
2568 return;
2569 }
2570 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002571 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2572 getDesiredThumbnailHeight(mActivity));
2573 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002574 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2575 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2576 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2577 500);
2578 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002579 return;
2580 }
2581
2582 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002583 new AsyncTask<Void, Void, Void>() {
2584 @Override
2585 protected Void doInBackground(Void... unused) {
2586 Cursor cursor = null;
2587 try {
2588 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002589 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2590 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2591 : url);
2592 if (mUpdateMyNavThumbnail) {
2593 mUpdateMyNavThumbnail = false;
2594 mUpdateMyNavThumbnailUrl = null;
2595 }
John Reck34ef2672011-02-10 11:30:55 -08002596 if (cursor != null && cursor.moveToFirst()) {
2597 final ByteArrayOutputStream os =
2598 new ByteArrayOutputStream();
2599 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002600
John Reck34ef2672011-02-10 11:30:55 -08002601 ContentValues values = new ContentValues();
2602 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002603
John Reck34ef2672011-02-10 11:30:55 -08002604 do {
John Reck617fd832011-02-16 14:35:59 -08002605 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002606 cr.update(Images.CONTENT_URI, values, null, null);
2607 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 }
John Reck34ef2672011-02-10 11:30:55 -08002609 } catch (IllegalStateException e) {
2610 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002611 } catch (SQLiteException s) {
2612 // Added for possible error when user tries to remove the same bookmark
2613 // that is being updated with a screen shot
2614 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002615 } finally {
2616 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002617 }
John Reck34ef2672011-02-10 11:30:55 -08002618 return null;
2619 }
2620 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002621 }
2622
2623 private class Copy implements OnMenuItemClickListener {
2624 private CharSequence mText;
2625
John Reck9c35b9c2012-05-30 10:08:50 -07002626 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002627 public boolean onMenuItemClick(MenuItem item) {
2628 copy(mText);
2629 return true;
2630 }
2631
2632 public Copy(CharSequence toCopy) {
2633 mText = toCopy;
2634 }
2635 }
2636
Leon Scroggins63c02662010-11-18 15:16:27 -05002637 private static class Download implements OnMenuItemClickListener {
2638 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002639 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002640 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002641 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002642 private static final String FALLBACK_EXTENSION = "dat";
2643 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002644
John Reck9c35b9c2012-05-30 10:08:50 -07002645 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002646 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002647 if (DataUri.isDataUri(mText)) {
2648 saveDataUri();
2649 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002650 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002651 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002652 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002653 return true;
2654 }
2655
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002656 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2657 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002658 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002659 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002660 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002661 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002662 }
George Mount387d45d2011-10-07 15:57:53 -07002663
2664 /**
2665 * Treats mText as a data URI and writes its contents to a file
2666 * based on the current time.
2667 */
2668 private void saveDataUri() {
2669 FileOutputStream outputStream = null;
2670 try {
2671 DataUri uri = new DataUri(mText);
2672 File target = getTarget(uri);
2673 outputStream = new FileOutputStream(target);
2674 outputStream.write(uri.getData());
2675 final DownloadManager manager =
2676 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2677 manager.addCompletedDownload(target.getName(),
2678 mActivity.getTitle().toString(), false,
2679 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002680 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002681 } catch (IOException e) {
2682 Log.e(LOGTAG, "Could not save data URL");
2683 } finally {
2684 if (outputStream != null) {
2685 try {
2686 outputStream.close();
2687 } catch (IOException e) {
2688 // ignore close errors
2689 }
2690 }
2691 }
2692 }
2693
2694 /**
2695 * Creates a File based on the current time stamp and uses
2696 * the mime type of the DataUri to get the extension.
2697 */
2698 private File getTarget(DataUri uri) throws IOException {
2699 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002700 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002701 String nameBase = format.format(new Date());
2702 String mimeType = uri.getMimeType();
2703 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2704 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2705 if (extension == null) {
2706 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2707 extension = FALLBACK_EXTENSION;
2708 }
2709 extension = "." + extension; // createTempFile needs the '.'
2710 File targetFile = File.createTempFile(nameBase, extension, dir);
2711 return targetFile;
2712 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002713 }
2714
Cary Clark8974d282010-11-22 10:46:05 -05002715 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002716 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002717
John Reck9c35b9c2012-05-30 10:08:50 -07002718 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002719 public boolean onMenuItemClick(MenuItem item) {
2720 if (mWebView != null) {
2721 return mWebView.selectText();
2722 }
2723 return false;
2724 }
2725
2726 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002727 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002728 }
2729
2730 }
2731
Michael Kolb8233fac2010-10-26 16:08:53 -07002732 /********************** TODO: UI stuff *****************************/
2733
2734 // these methods have been copied, they still need to be cleaned up
2735
2736 /****************** tabs ***************************************************/
2737
2738 // basic tab interactions:
2739
2740 // it is assumed that tabcontrol already knows about the tab
2741 protected void addTab(Tab tab) {
2742 mUi.addTab(tab);
2743 }
2744
2745 protected void removeTab(Tab tab) {
2746 mUi.removeTab(tab);
2747 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002748 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002749 }
2750
Michael Kolbf2055602011-04-09 17:20:03 -07002751 @Override
2752 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002753 // monkey protection against delayed start
2754 if (tab != null) {
2755 mTabControl.setCurrentTab(tab);
2756 // the tab is guaranteed to have a webview after setCurrentTab
2757 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002758 tab.setTimeStamp();
Michael Kolbcd424e92011-02-24 10:26:26 -08002759 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002760 }
2761
John Reck8bcafc12011-08-29 16:43:02 -07002762 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002763 Tab current = mTabControl.getCurrentTab();
2764 if (current != null
2765 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002766 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002767 }
2768 }
2769
John Reck26b18322011-06-21 13:08:58 -07002770 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002771 // Dismiss the subwindow if applicable.
2772 dismissSubWindow(appTab);
2773 // Since we might kill the WebView, remove it from the
2774 // content view first.
2775 mUi.detachTab(appTab);
2776 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002777 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002778 // TODO: analyze why the remove and add are necessary
2779 mUi.attachTab(appTab);
2780 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002781 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002782 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002783 } else {
2784 // If the tab was the current tab, we have to attach
2785 // it to the view system again.
2786 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002787 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002788 }
2789 }
2790
2791 // Remove the sub window if it exists. Also called by TabControl when the
2792 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002793 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002794 public void dismissSubWindow(Tab tab) {
2795 removeSubWindow(tab);
2796 // dismiss the subwindow. This will destroy the WebView.
2797 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002798 WebView wv = getCurrentTopWebView();
2799 if (wv != null) {
2800 wv.requestFocus();
2801 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002802 }
2803
2804 @Override
2805 public void removeSubWindow(Tab t) {
2806 if (t.getSubWebView() != null) {
2807 mUi.removeSubWindow(t.getSubViewContainer());
2808 }
2809 }
2810
2811 @Override
2812 public void attachSubWindow(Tab tab) {
2813 if (tab.getSubWebView() != null) {
2814 mUi.attachSubWindow(tab.getSubViewContainer());
2815 getCurrentTopWebView().requestFocus();
2816 }
2817 }
2818
Mathew Inwood29721c22011-06-29 17:55:24 +01002819 private Tab showPreloadedTab(final UrlData urlData) {
2820 if (!urlData.isPreloaded()) {
2821 return null;
2822 }
2823 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2824 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2825 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002826 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002827 // Could not submit query. Fallback to regular tab creation
2828 tabControl.destroy();
2829 return null;
2830 }
2831 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002832 // check tab count and make room for new tab
2833 if (!mTabControl.canCreateNewTab()) {
2834 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2835 if (leastUsed != null) {
2836 closeTab(leastUsed);
2837 }
2838 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002839 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002840 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002841 mTabControl.addPreloadedTab(t);
2842 addTab(t);
2843 setActiveTab(t);
2844 return t;
2845 }
2846
Michael Kolb7bcafde2011-05-09 13:55:59 -07002847 // open a non inconito tab with the given url data
2848 // and set as active tab
2849 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002850 Tab tab = showPreloadedTab(urlData);
2851 if (tab == null) {
2852 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002853 if ((tab != null) && !urlData.isEmpty()) {
2854 loadUrlDataIn(tab, urlData);
2855 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002856 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002857 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002858 }
2859
Michael Kolb843510f2010-12-09 10:51:49 -08002860 @Override
2861 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002862 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002863 }
2864
Michael Kolb8233fac2010-10-26 16:08:53 -07002865 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002866 public Tab openIncognitoTab() {
2867 return openTab(INCOGNITO_URI, true, true, false);
2868 }
2869
2870 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002871 public Tab openTab(String url, boolean incognito, boolean setActive,
2872 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002873 return openTab(url, incognito, setActive, useCurrent, null);
2874 }
2875
2876 @Override
2877 public Tab openTab(String url, Tab parent, boolean setActive,
2878 boolean useCurrent) {
2879 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2880 setActive, useCurrent, parent);
2881 }
2882
2883 public Tab openTab(String url, boolean incognito, boolean setActive,
2884 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002885 Tab tab = createNewTab(incognito, setActive, useCurrent);
2886 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002887 if (parent != null && parent != tab) {
2888 parent.addChildTab(tab);
2889 }
Michael Kolb519d2282011-05-09 17:03:19 -07002890 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002891 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002892 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002893 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002894 return tab;
2895 }
2896
2897 // this method will attempt to create a new tab
2898 // incognito: private browsing tab
2899 // setActive: ste tab as current tab
2900 // useCurrent: if no new tab can be created, return current tab
2901 private Tab createNewTab(boolean incognito, boolean setActive,
2902 boolean useCurrent) {
2903 Tab tab = null;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002904 MemoryMonitor memMonitor = null;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002905 if (mTabControl.canCreateNewTab()) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002906 if (mSettings.enableMemoryMonitor()) {
2907 Log.d(LOGTAG, " Memory Monitor Enabled .");
2908 memMonitor = MemoryMonitor.getInstance(mActivity.getApplicationContext(),this);
2909 if (memMonitor != null) {
2910 //Remove webview associated with the oldest tab
2911 memMonitor.destroyLeastRecentlyActiveTab();
2912 }
2913 } else {
2914 Log.d(LOGTAG, " Memory Monitor disabled .");
2915 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002916 tab = mTabControl.createNewTab(incognito);
2917 addTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002918 tab.setTimeStamp();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002919 if (setActive) {
2920 setActiveTab(tab);
2921 }
2922 } else {
2923 if (useCurrent) {
2924 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002925 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002926 } else {
2927 mUi.showMaxTabsWarning();
2928 }
2929 }
2930 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002931 }
2932
John Reck2bc80422011-06-30 15:11:49 -07002933 @Override
2934 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2935 SnapshotTab tab = null;
2936 if (mTabControl.canCreateNewTab()) {
2937 tab = mTabControl.createSnapshotTab(snapshotId);
2938 addTab(tab);
2939 if (setActive) {
2940 setActiveTab(tab);
2941 }
2942 } else {
2943 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002944 }
2945 return tab;
2946 }
2947
Michael Kolb8233fac2010-10-26 16:08:53 -07002948 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002949 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002950 * @return boolean True if we successfully switched to a different tab. If
2951 * the indexth tab is null, or if that tab is the same as
2952 * the current one, return false.
2953 */
2954 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002955 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002956 Tab currentTab = mTabControl.getCurrentTab();
2957 if (tab == null || tab == currentTab) {
2958 return false;
2959 }
2960 setActiveTab(tab);
2961 return true;
2962 }
2963
2964 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002965 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002966 closeCurrentTab(false);
2967 }
2968
2969 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002970 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002971 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002972 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002973 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002974 return;
2975 }
Michael Kolbc831b632011-05-11 09:30:34 -07002976 final Tab current = mTabControl.getCurrentTab();
2977 final int pos = mTabControl.getCurrentPosition();
2978 Tab newTab = current.getParent();
2979 if (newTab == null) {
2980 newTab = mTabControl.getTab(pos + 1);
2981 if (newTab == null) {
2982 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002983 }
2984 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002985 if (andQuit) {
2986 mTabControl.setCurrentTab(newTab);
2987 closeTab(current);
2988 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002989 // Close window
2990 closeTab(current);
2991 }
2992 }
2993
2994 /**
2995 * Close the tab, remove its associated title bar, and adjust mTabControl's
2996 * current tab to a valid value.
2997 */
2998 @Override
2999 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003000 if (tab == mTabControl.getCurrentTab()) {
3001 closeCurrentTab();
3002 } else {
3003 removeTab(tab);
3004 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003005 }
3006
Afzal Najamd4e33312012-04-26 01:54:01 -04003007 /**
3008 * Close all tabs except the current one
3009 */
3010 @Override
3011 public void closeOtherTabs() {
3012 int inactiveTabs = mTabControl.getTabCount() - 1;
3013 for (int i = inactiveTabs; i >= 0; i--) {
3014 Tab tab = mTabControl.getTab(i);
3015 if (tab != mTabControl.getCurrentTab()) {
3016 removeTab(tab);
3017 }
3018 }
3019 }
3020
Michael Kolb8233fac2010-10-26 16:08:53 -07003021 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003022 protected void loadUrlFromContext(String url) {
3023 Tab tab = getCurrentTab();
3024 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003025 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003026 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003027 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003028 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003029 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003030 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003031 }
3032 }
3033 }
3034
3035 /**
3036 * Load the URL into the given WebView and update the title bar
3037 * to reflect the new load. Call this instead of WebView.loadUrl
3038 * directly.
3039 * @param view The WebView used to load url.
3040 * @param url The URL to load.
3041 */
John Reck71e51422011-07-01 16:49:28 -07003042 @Override
3043 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003044 loadUrl(tab, url, null);
3045 }
3046
3047 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3048 if (tab != null) {
3049 dismissSubWindow(tab);
3050 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003051 if (tab.hasCrashed) {
3052 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3053 }
Michael Kolba53c9892011-10-05 13:31:40 -07003054 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003055 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003056 }
3057
3058 /**
3059 * Load UrlData into a Tab and update the title bar to reflect the new
3060 * load. Call this instead of UrlData.loadIn directly.
3061 * @param t The Tab used to load.
3062 * @param data The UrlData being loaded.
3063 */
3064 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003065 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003066 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003067 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003068 } else {
John Reck38b39652012-06-05 09:22:59 -07003069 if (t != null && data.mDisableUrlOverride) {
3070 t.disableUrlOverridingForLoad();
3071 }
John Reck26b18322011-06-21 13:08:58 -07003072 loadUrl(t, data.mUrl, data.mHeaders);
3073 }
3074 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 }
3076
John Reck30c714c2010-12-16 17:30:34 -08003077 @Override
3078 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003079 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003080 if (tab.canGoBack()) {
3081 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003082 } else {
John Reckef654f12011-07-12 16:42:08 -07003083 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003084 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003085 }
3086
3087 void goBackOnePageOrQuit() {
3088 Tab current = mTabControl.getCurrentTab();
3089 if (current == null) {
3090 /*
3091 * Instead of finishing the activity, simply push this to the back
3092 * of the stack and let ActivityManager to choose the foreground
3093 * activity. As BrowserActivity is singleTask, it will be always the
3094 * root of the task. So we can use either true or false for
3095 * moveTaskToBack().
3096 */
luxiaolb40014b2013-07-19 10:01:43 +08003097 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003098 return;
3099 }
John Reckef654f12011-07-12 16:42:08 -07003100 if (current.canGoBack()) {
3101 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003102 } else {
3103 // Check to see if we are closing a window that was created by
3104 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003105 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003106 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003107 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003108 // Now we close the other tab
3109 closeTab(current);
3110 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003111 /*
3112 * Instead of finishing the activity, simply push this to the back
3113 * of the stack and let ActivityManager to choose the foreground
3114 * activity. As BrowserActivity is singleTask, it will be always the
3115 * root of the task. So we can use either true or false for
3116 * moveTaskToBack().
3117 */
luxiaolb40014b2013-07-19 10:01:43 +08003118 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003119 }
3120 }
3121 }
3122
3123 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003124 * 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 getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003128 int pos = mTabControl.getCurrentPosition() + 1;
3129 if (pos >= mTabControl.getTabCount()) {
3130 pos = 0;
3131 }
3132 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003133 }
3134
3135 /**
3136 * helper method for key handler
3137 * returns the current tab if it can't advance
3138 */
Michael Kolbc831b632011-05-11 09:30:34 -07003139 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003140 int pos = mTabControl.getCurrentPosition() - 1;
3141 if ( pos < 0) {
3142 pos = mTabControl.getTabCount() - 1;
3143 }
3144 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003145 }
3146
John Reckbcef87f2012-02-03 14:58:34 -08003147 boolean isMenuOrCtrlKey(int keyCode) {
3148 return (KeyEvent.KEYCODE_MENU == keyCode)
3149 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3150 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3151 }
3152
Michael Kolb0035fad2011-03-14 13:25:28 -07003153 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003154 * handle key events in browser
3155 *
3156 * @param keyCode
3157 * @param event
3158 * @return true if handled, false to pass to super
3159 */
John Reck9c35b9c2012-05-30 10:08:50 -07003160 @Override
3161 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003162 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003163 // Even if MENU is already held down, we need to call to super to open
3164 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003165 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003166 mMenuIsDown = true;
3167 return false;
3168 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003169
Cary Clark8ff8c662010-12-29 15:03:05 -05003170 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003171 Tab tab = getCurrentTab();
3172 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003173
Cary Clark160bbb92011-01-10 11:17:07 -05003174 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3175 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003176
Michael Kolb8233fac2010-10-26 16:08:53 -07003177 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003178 case KeyEvent.KEYCODE_TAB:
3179 if (event.isCtrlPressed()) {
3180 if (event.isShiftPressed()) {
3181 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003182 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003183 } else {
3184 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003185 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003186 }
3187 return true;
3188 }
3189 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003190 case KeyEvent.KEYCODE_SPACE:
3191 // WebView/WebTextView handle the keys in the KeyDown. As
3192 // the Activity's shortcut keys are only handled when WebView
3193 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003194 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003195 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003196 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003197 pageDown();
3198 }
3199 return true;
3200 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003201 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003202 event.startTracking();
3203 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003204 case KeyEvent.KEYCODE_FORWARD:
3205 if (!noModifiers) break;
3206 tab.goForward();
3207 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003208 case KeyEvent.KEYCODE_DPAD_LEFT:
3209 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003210 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003211 return true;
3212 }
3213 break;
3214 case KeyEvent.KEYCODE_DPAD_RIGHT:
3215 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003216 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003217 return true;
3218 }
3219 break;
3220 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003221 if (ctrl) {
3222 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003223 return true;
3224 }
3225 break;
Michael Kolba4183062011-01-16 10:43:21 -08003226// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003227 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003228 if (ctrl ) {
3229 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003230 return true;
3231 }
3232 break;
Michael Kolba4183062011-01-16 10:43:21 -08003233// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003234// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003235// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003236// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003237// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003238// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003239// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003240// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003241// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003242// case KeyEvent.KEYCODE_M: // unused
3243// case KeyEvent.KEYCODE_N: // in Chrome: new window
3244// case KeyEvent.KEYCODE_O: // in Chrome: open file
3245// case KeyEvent.KEYCODE_P: // in Chrome: print page
3246// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003247// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003248// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3249 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003250 // we can't use the ctrl/shift flags, they check for
3251 // exclusive use of a modifier
3252 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003253 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003254 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003255 } else {
3256 openTabToHomePage();
3257 }
3258 return true;
3259 }
3260 break;
3261// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3262// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003263// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003264// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3265// case KeyEvent.KEYCODE_Y: // unused
3266// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003267 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003268 // it is a regular key and webview is not null
3269 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003270 }
3271
John Reck9c35b9c2012-05-30 10:08:50 -07003272 @Override
3273 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003274 switch(keyCode) {
3275 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003276 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003277 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003278 return true;
3279 }
3280 break;
3281 }
3282 return false;
3283 }
3284
John Reck9c35b9c2012-05-30 10:08:50 -07003285 @Override
3286 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003287 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003288 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003289 if (KeyEvent.KEYCODE_MENU == keyCode
3290 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003291 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003292 }
3293 }
Cary Clark160bbb92011-01-10 11:17:07 -05003294 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003295 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003296 case KeyEvent.KEYCODE_BACK:
3297 if (event.isTracking() && !event.isCanceled()) {
3298 onBackKey();
3299 return true;
3300 }
3301 break;
3302 }
3303 return false;
3304 }
3305
3306 public boolean isMenuDown() {
3307 return mMenuIsDown;
3308 }
3309
John Reck9c35b9c2012-05-30 10:08:50 -07003310 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003311 public void setupAutoFill(Message message) {
3312 // Open the settings activity at the AutoFill profile fragment so that
3313 // the user can create a new profile. When they return, we will dispatch
3314 // the message so that we can autofill the form using their new profile.
3315 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3316 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3317 AutoFillSettingsFragment.class.getName());
3318 mAutoFillSetupMessage = message;
3319 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3320 }
John Reckb3417f02011-01-14 11:01:05 -08003321
John Reck9c35b9c2012-05-30 10:08:50 -07003322 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003323 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003324 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003325 return true;
3326 }
3327
John Reck1cf4b792011-07-26 10:22:22 -07003328 @Override
3329 public boolean shouldCaptureThumbnails() {
3330 return mUi.shouldCaptureThumbnails();
3331 }
3332
Michael Kolbc3af0672011-08-09 10:24:41 -07003333 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003334 public boolean supportsVoice() {
3335 PackageManager pm = mActivity.getPackageManager();
3336 List activities = pm.queryIntentActivities(new Intent(
3337 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3338 return activities.size() != 0;
3339 }
3340
3341 @Override
3342 public void startVoiceRecognizer() {
3343 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003344 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003345 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3346 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3347 mActivity.startActivityForResult(voice, VOICE_RESULT);
3348 }
3349
3350 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003351 public void setBlockEvents(boolean block) {
3352 mBlockEvents = block;
3353 }
3354
John Reck9c35b9c2012-05-30 10:08:50 -07003355 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003356 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003357 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003358 }
3359
John Reck9c35b9c2012-05-30 10:08:50 -07003360 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003361 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003362 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003363 }
3364
John Reck9c35b9c2012-05-30 10:08:50 -07003365 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003366 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003367 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003368 }
3369
John Reck9c35b9c2012-05-30 10:08:50 -07003370 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003371 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003372 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003373 }
3374
John Reck9c35b9c2012-05-30 10:08:50 -07003375 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003376 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003377 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003378 }
3379
Michael Kolb8233fac2010-10-26 16:08:53 -07003380}