blob: e3a7308ab3a6c43fdb77cf0de4412cd804808645 [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
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500271 mBookmarksObserver = new ContentObserver(mHandler) {
272 @Override
273 public void onChange(boolean selfChange) {
274 int size = mTabControl.getTabCount();
275 for (int i = 0; i < size; i++) {
276 mTabControl.getTab(i).updateBookmarkedStatus();
277 }
278 }
279
280 };
281 browser.getContentResolver().registerContentObserver(
282 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700283
284 mNetworkHandler = new NetworkStateHandler(mActivity, this);
285 // Start watching the default geolocation permissions
286 mSystemAllowGeolocationOrigins =
287 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
288 mSystemAllowGeolocationOrigins.start();
289
John Reckaf262e72011-07-25 13:55:44 -0700290 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700291 }
292
John Reck9c35b9c2012-05-30 10:08:50 -0700293 @Override
294 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800295 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800296 // mCrashRecoverHandler has any previously saved state.
297 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700298 }
299
George Mount3636d0a2011-11-21 09:08:21 -0800300 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700301 // Unless the last browser usage was within 24 hours, destroy any
302 // remaining incognito tabs.
303
304 Calendar lastActiveDate = icicle != null ?
305 (Calendar) icicle.getSerializable("lastActiveDate") : null;
306 Calendar today = Calendar.getInstance();
307 Calendar yesterday = Calendar.getInstance();
308 yesterday.add(Calendar.DATE, -1);
309
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800310 // we dont want to ever recover incognito tabs
311 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700312
Patrick Scott7d50a932011-02-04 09:27:26 -0500313 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700314 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500315 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000316
Michael Kolbc831b632011-05-11 09:30:34 -0700317 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500318 // Not able to restore so we go ahead and clear session cookies. We
319 // must do this before trying to login the user as we don't want to
320 // clear any session cookies set during login.
321 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700322 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800323 if (intent == null) {
324 // This won't happen under common scenarios. The icicle is
325 // not null, but there aren't any tabs to restore.
326 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700327 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800328 final Bundle extra = intent.getExtras();
329 // Create an initial tab.
330 // If the intent is ACTION_VIEW and data is not null, the Browser is
331 // invoked to view the content by another application. In this case,
332 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800333 UrlData urlData = null;
334 if (intent.getData() != null
335 && Intent.ACTION_VIEW.equals(intent.getAction())
336 && intent.getData().toString().startsWith("content://")) {
337 urlData = new UrlData(intent.getData().toString());
338 } else {
339 urlData = IntentHandler.getUrlDataFromIntent(intent);
340 }
George Mount3636d0a2011-11-21 09:08:21 -0800341 Tab t = null;
342 if (urlData.isEmpty()) {
Vivek Sekharce2a4832014-03-26 13:26:53 -0700343 Resources res = mActivity.getResources();
344 String browserRes = res.getString(R.string.config_carrier_resource);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800345 if (browserRes.equals(
kaiyiz6e5b3e02013-08-19 20:02:01 +0800346 "cmcc")) {
347 t = openTab(OFFLINE_PAGE, false, true, true);
348 } else {
349 t = openTabToHomePage();
350 }
George Mount3636d0a2011-11-21 09:08:21 -0800351 } else {
352 t = openTab(urlData);
353 }
354 if (t != null) {
355 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
356 }
357 WebView webView = t.getWebView();
358 if (extra != null) {
359 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
360 if (scale > 0 && scale <= 1000) {
361 webView.setInitialScale(scale);
362 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 }
364 }
John Reckd8c74522011-06-14 08:45:00 -0700365 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700367 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500368 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700369 List<Tab> tabs = mTabControl.getTabs();
370 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
371 for (Tab t : tabs) {
372 restoredTabs.add(t.getId());
373 }
374 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700375 if (tabs.size() == 0) {
376 openTabToHomePage();
377 }
John Reck1cf4b792011-07-26 10:22:22 -0700378 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 // TabControl.restoreState() will create a new tab even if
380 // restoring the state fails.
381 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800382 // Intent is non-null when framework thinks the browser should be
383 // launching with a new intent (icicle is null).
384 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700385 mIntentHandler.onNewIntent(intent);
386 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700387 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700389 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800390 if (jsFlags.trim().length() != 0) {
391 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700392 }
George Mount3636d0a2011-11-21 09:08:21 -0800393 if (intent != null
394 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700395 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800396 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 }
398
John Reck1cf4b792011-07-26 10:22:22 -0700399 private static class PruneThumbnails implements Runnable {
400 private Context mContext;
401 private List<Long> mIds;
402
403 PruneThumbnails(Context context, List<Long> preserveIds) {
404 mContext = context.getApplicationContext();
405 mIds = preserveIds;
406 }
407
408 @Override
409 public void run() {
410 ContentResolver cr = mContext.getContentResolver();
411 if (mIds == null || mIds.size() == 0) {
412 cr.delete(Thumbnails.CONTENT_URI, null, null);
413 } else {
414 int length = mIds.size();
415 StringBuilder where = new StringBuilder();
416 where.append(Thumbnails._ID);
417 where.append(" not in (");
418 for (int i = 0; i < length; i++) {
419 where.append(mIds.get(i));
420 if (i < (length - 1)) {
421 where.append(",");
422 }
423 }
424 where.append(")");
425 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
426 }
427 }
428
429 }
430
Michael Kolb1514bb72010-11-22 09:11:48 -0800431 @Override
432 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700433 return mFactory;
434 }
435
436 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800437 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800438 if (tab.hasCrashed)
439 tab.showCrashView();
440 else
441 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800442 }
443
444 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800445 public void createSubWindow(Tab tab) {
446 endActionMode();
447 WebView mainView = tab.getWebView();
448 WebView subView = mFactory.createWebView((mainView == null)
449 ? false
450 : mainView.isPrivateBrowsingEnabled());
451 mUi.createSubWindow(tab, subView);
452 }
453
454 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700455 public Context getContext() {
456 return mActivity;
457 }
458
459 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700460 public Activity getActivity() {
461 return mActivity;
462 }
463
464 void setUi(UI ui) {
465 mUi = ui;
466 }
467
Michael Kolbaf63dba2012-05-16 12:58:05 -0700468 @Override
469 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700470 return mSettings;
471 }
472
473 IntentHandler getIntentHandler() {
474 return mIntentHandler;
475 }
476
477 @Override
478 public UI getUi() {
479 return mUi;
480 }
481
482 int getMaxTabs() {
483 return mActivity.getResources().getInteger(R.integer.max_tabs);
484 }
485
486 @Override
487 public TabControl getTabControl() {
488 return mTabControl;
489 }
490
Michael Kolb1bf23132010-11-19 12:55:12 -0800491 @Override
492 public List<Tab> getTabs() {
493 return mTabControl.getTabs();
494 }
495
John Reckaf262e72011-07-25 13:55:44 -0700496 // Open the icon database.
497 private void openIconDatabase() {
498 // We have to call getInstance on the UI thread
499 final WebIconDatabase instance = WebIconDatabase.getInstance();
500 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000501
John Reckaf262e72011-07-25 13:55:44 -0700502 @Override
503 public void run() {
504 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700505 }
John Reckaf262e72011-07-25 13:55:44 -0700506 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700507 }
508
509 private void startHandler() {
510 mHandler = new Handler() {
511
512 @Override
513 public void handleMessage(Message msg) {
514 switch (msg.what) {
515 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700516 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700517 break;
518 case FOCUS_NODE_HREF:
519 {
520 String url = (String) msg.getData().get("url");
521 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500522 String src = (String) msg.getData().get("src");
523 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700524 if (TextUtils.isEmpty(url)) {
525 break;
526 }
527 HashMap focusNodeMap = (HashMap) msg.obj;
528 WebView view = (WebView) focusNodeMap.get("webview");
529 // Only apply the action if the top window did not change.
530 if (getCurrentTopWebView() != view) {
531 break;
532 }
533 switch (msg.arg1) {
534 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700535 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500537 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700538 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500539 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500540 case R.id.open_newtab_context_menu_id:
541 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700542 openTab(url, parent,
543 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500544 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700545 case R.id.copy_link_context_menu_id:
546 copy(url);
547 break;
548 case R.id.save_link_context_menu_id:
549 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500550 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800551 mActivity, url, view.getSettings().getUserAgentString(),
552 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700553 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700554 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700555 if(title == null || title == "")
556 title = url;
557
558 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
559 //SWE TODO: No thumbnail support for the url obtained via
560 //browser context menu as its not loaded in webview.
561 if (bookmarkIntent != null) {
562 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
563 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
564 mActivity.startActivity(bookmarkIntent);
565 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700566 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700567 }
568 break;
569 }
570
571 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700572 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700573 break;
574
575 case STOP_LOAD:
576 stopLoading();
577 break;
578
579 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700580 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 mWakeLock.release();
582 // if we reach here, Browser should be still in the
583 // background loading after WAKELOCK_TIMEOUT (5-min).
584 // To avoid burning the battery, stop loading.
585 mTabControl.stopAllLoading();
586 }
587 break;
588
589 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800590 Tab tab = (Tab) msg.obj;
591 if (tab != null) {
592 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 }
594 break;
kaiyiz591110b2013-08-06 17:11:06 +0800595 case OPEN_MENU:
596 if (!mOptionsMenuOpen && mActivity != null ) {
597 mActivity.openOptionsMenu();
598 }
599 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700600 }
601 }
602 };
603
604 }
605
John Reckef654f12011-07-12 16:42:08 -0700606 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200607 public Tab getCurrentTab() {
608 return mTabControl.getCurrentTab();
609 }
610
Michael Kolbba99c5d2010-11-29 14:57:41 -0800611 @Override
612 public void shareCurrentPage() {
613 shareCurrentPage(mTabControl.getCurrentTab());
614 }
615
616 private void shareCurrentPage(Tab tab) {
617 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800618 sharePage(mActivity, tab.getTitle(),
619 tab.getUrl(), tab.getFavicon(),
620 createScreenshot(tab.getWebView(),
621 getDesiredThumbnailWidth(mActivity),
622 getDesiredThumbnailHeight(mActivity)));
623 }
624 }
625
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 /**
627 * Share a page, providing the title, url, favicon, and a screenshot. Uses
628 * an {@link Intent} to launch the Activity chooser.
629 * @param c Context used to launch a new Activity.
630 * @param title Title of the page. Stored in the Intent with
631 * {@link Intent#EXTRA_SUBJECT}
632 * @param url URL of the page. Stored in the Intent with
633 * {@link Intent#EXTRA_TEXT}
634 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
635 * with {@link Browser#EXTRA_SHARE_FAVICON}
636 * @param screenshot Bitmap of a screenshot of the page. Stored in the
637 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
638 */
639 static final void sharePage(Context c, String title, String url,
640 Bitmap favicon, Bitmap screenshot) {
641 Intent send = new Intent(Intent.ACTION_SEND);
642 send.setType("text/plain");
643 send.putExtra(Intent.EXTRA_TEXT, url);
644 send.putExtra(Intent.EXTRA_SUBJECT, title);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800645 send.putExtra(EXTRA_SHARE_FAVICON, favicon);
646 send.putExtra(EXTRA_SHARE_SCREENSHOT, screenshot);
Michael Kolb8233fac2010-10-26 16:08:53 -0700647 try {
648 c.startActivity(Intent.createChooser(send, c.getString(
649 R.string.choosertitle_sharevia)));
650 } catch(android.content.ActivityNotFoundException ex) {
651 // if no app handles it, do nothing
652 }
653 }
654
655 private void copy(CharSequence text) {
656 ClipboardManager cm = (ClipboardManager) mActivity
657 .getSystemService(Context.CLIPBOARD_SERVICE);
658 cm.setText(text);
659 }
660
661 // lifecycle
662
John Reck9c35b9c2012-05-30 10:08:50 -0700663 @Override
664 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800666 // update the menu in case of a locale change
667 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800668 if (mOptionsMenuOpen) {
669 mActivity.closeOptionsMenu();
670 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
671 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700672 if (mPageDialogsHandler != null) {
673 mPageDialogsHandler.onConfigurationChanged(config);
674 }
675 mUi.onConfigurationChanged(config);
676 }
677
678 @Override
679 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700680 if (!mUi.isWebShowing()) {
681 mUi.showWeb(false);
682 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 mIntentHandler.onNewIntent(intent);
684 }
685
John Reck9c35b9c2012-05-30 10:08:50 -0700686 @Override
687 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800688 if (mUi.isCustomViewShowing()) {
689 hideCustomView();
690 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700691 if (mActivityPaused) {
692 Log.e(LOGTAG, "BrowserActivity is already paused.");
693 return;
694 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700695 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800696 Tab tab = mTabControl.getCurrentTab();
697 if (tab != null) {
698 tab.pause();
699 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700700 if (mWakeLock == null) {
701 PowerManager pm = (PowerManager) mActivity
702 .getSystemService(Context.POWER_SERVICE);
703 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
704 }
Michael Kolb70976932010-11-30 11:34:01 -0800705 mWakeLock.acquire();
706 mHandler.sendMessageDelayed(mHandler
707 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
708 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 }
710 mUi.onPause();
711 mNetworkHandler.onPause();
712
713 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100714 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700715 if (sThumbnailBitmap != null) {
716 sThumbnailBitmap.recycle();
717 sThumbnailBitmap = null;
718 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700719 }
720
John Reck9c35b9c2012-05-30 10:08:50 -0700721 @Override
722 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700723 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800724 Bundle saveState = createSaveState();
725
726 // crash recovery manages all save & restore state
727 mCrashRecoveryHandler.writeState(saveState);
728 mSettings.setLastRunPaused(true);
729 }
730
731 /**
732 * Save the current state to outState. Does not write the state to
733 * disk.
734 * @return Bundle containing the current state of all tabs.
735 */
736 /* package */ Bundle createSaveState() {
737 Bundle saveState = new Bundle();
738 mTabControl.saveState(saveState);
739 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700740 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800741 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700742 }
George Mount3636d0a2011-11-21 09:08:21 -0800743 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 }
745
John Reck9c35b9c2012-05-30 10:08:50 -0700746 @Override
747 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700748 if (!mActivityPaused) {
749 Log.e(LOGTAG, "BrowserActivity is already resumed.");
750 return;
751 }
George Mount3636d0a2011-11-21 09:08:21 -0800752 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800754 Tab current = mTabControl.getCurrentTab();
755 if (current != null) {
756 current.resume();
757 resumeWebViewTimers(current);
758 }
John Reckf57c0292011-07-21 18:15:39 -0700759 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200760
Michael Kolb8233fac2010-10-26 16:08:53 -0700761 mUi.onResume();
762 mNetworkHandler.onResume();
763 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100764 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700765 if (mVoiceResult != null) {
766 mUi.onVoiceResult(mVoiceResult);
767 mVoiceResult = null;
768 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800769 if (current != null && current.hasCrashed) {
770 current.showCrashView();
771 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700772 }
773
John Reckf57c0292011-07-21 18:15:39 -0700774 private void releaseWakeLock() {
775 if (mWakeLock != null && mWakeLock.isHeld()) {
776 mHandler.removeMessages(RELEASE_WAKELOCK);
777 mWakeLock.release();
778 }
779 }
780
Michael Kolb70976932010-11-30 11:34:01 -0800781 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800782 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800783 * @param tab guaranteed non-null
784 */
785 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 boolean inLoad = tab.inPageLoad();
787 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
788 CookieSyncManager.getInstance().startSync();
789 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100790 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700791 }
792 }
793
Michael Kolb70976932010-11-30 11:34:01 -0800794 /**
795 * Pause all WebView timers using the WebView of the given tab
796 * @param tab
797 * @return true if the timers are paused or tab is null
798 */
799 private boolean pauseWebViewTimers(Tab tab) {
800 if (tab == null) {
801 return true;
802 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700803 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100804 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 }
Michael Kolb70976932010-11-30 11:34:01 -0800807 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700808 }
809
John Reck9c35b9c2012-05-30 10:08:50 -0700810 @Override
811 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800812 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700813 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
814 mUploadHandler = null;
815 }
816 if (mTabControl == null) return;
817 mUi.onDestroy();
818 // Remove the current tab and sub window
819 Tab t = mTabControl.getCurrentTab();
820 if (t != null) {
821 dismissSubWindow(t);
822 removeTab(t);
823 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500824 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 // Destroy all the tabs
826 mTabControl.destroy();
827 WebIconDatabase.getInstance().close();
828 // Stop watching the default geolocation permissions
829 mSystemAllowGeolocationOrigins.stop();
830 mSystemAllowGeolocationOrigins = null;
831 }
832
833 protected boolean isActivityPaused() {
834 return mActivityPaused;
835 }
836
John Reck9c35b9c2012-05-30 10:08:50 -0700837 @Override
838 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 mTabControl.freeMemory();
840 }
841
842 @Override
843 public boolean shouldShowErrorConsole() {
844 return mShouldShowErrorConsole;
845 }
846
847 protected void setShouldShowErrorConsole(boolean show) {
848 if (show == mShouldShowErrorConsole) {
849 // Nothing to do.
850 return;
851 }
852 mShouldShowErrorConsole = show;
853 Tab t = mTabControl.getCurrentTab();
854 if (t == null) {
855 // There is no current tab so we cannot toggle the error console
856 return;
857 }
858 mUi.setShouldShowErrorConsole(t, show);
859 }
860
861 @Override
862 public void stopLoading() {
863 mLoadStopped = true;
864 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700866 if (w != null) {
867 w.stopLoading();
868 mUi.onPageStopped(tab);
869 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700870 }
871
872 boolean didUserStopLoading() {
873 return mLoadStopped;
874 }
875
kaiyiza016da12013-08-26 17:50:22 +0800876 private void handleNetworkNotify(WebView view) {
877 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
878 Context.CONNECTIVITY_SERVICE);
879 WifiManager wifiMgr = (WifiManager) this.getContext()
880 .getSystemService(Context.WIFI_SERVICE);
881 int networkSwitchTypeOK = this.getContext().getResources()
882 .getInteger(R.integer.netswitch_type_remind);
883
884 if (wifiMgr.isWifiEnabled()) {
885 NetworkInfo mNetworkInfo = conMgr.getActiveNetworkInfo();
886 if (mNetworkInfo == null
887 || (mNetworkInfo != null && (mNetworkInfo.getType() !=
888 ConnectivityManager.TYPE_WIFI))) {
kaiyizb7bf1f22013-10-02 11:42:23 +0800889 List<ScanResult> list = wifiMgr.getScanResults();
890 if (list != null && list.size() == 0) {
891 int isReminder = Settings.System.getInt(
892 mActivity.getContentResolver(),
893 this.getContext().getResources()
894 .getString(R.string.network_switch_remind_type),
895 networkSwitchTypeOK);
896 if (isReminder == networkSwitchTypeOK) {
897 Intent intent = new Intent(
898 INTENT_WIFI_SELECTION_DATA_CONNECTION);
899 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
900 this.getContext().startActivity(intent);
901 }
902 } else {
Bijan Amirzada58383e72014-04-01 14:45:22 -0700903 if ((Boolean)ReflectHelper.invokeMethod(
904 "android.app.ActivityManagerNative", "isSystemReady", null, null)) {
kaiyizb7bf1f22013-10-02 11:42:23 +0800905 try {
906 Intent intent = new Intent(INTENT_PICK_NETWORK);
907 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
908 this.getContext().startActivity(intent);
909 } catch (Exception e) {
910 String err_msg = this.getContext().getString(
911 R.string.acivity_not_found, INTENT_PICK_NETWORK);
912 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
913 }
914 } else {
915 Log.e(LOGTAG, "System is not ready!");
916 }
kaiyiza016da12013-08-26 17:50:22 +0800917 }
918 mNetworkShouldNotify = false;
919 }
920 } else {
921 if (!mNetworkHandler.isNetworkUp()) {
922 view.setNetworkAvailable(false);
923 Log.v(LOGTAG, "handleNetworkNotify() Wlan is not enabled.");
924 }
925 }
926 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700927 // WebViewController
928
929 @Override
John Reck324d4402011-01-11 16:56:42 -0800930 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700931
932 // We've started to load a new page. If there was a pending message
933 // to save a screenshot then we will now take the new page and save
934 // an incorrect screenshot. Therefore, remove any pending thumbnail
935 // messages from the queue.
936 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800937 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700938
939 // reset sync timer to avoid sync starts during loading a page
940 CookieSyncManager.getInstance().resetSync();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800941 Object[] params = {new String(PROP_NETSWITCH),
942 new Boolean(false)};
943 Class[] type = new Class[] {String.class, boolean.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -0700944 Boolean result = (Boolean) ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800945 "android.os.SystemProperties", "getBoolean",
946 type, params);
947 if (result) {
kaiyiza016da12013-08-26 17:50:22 +0800948 if (!mNetworkHandler.isNetworkUp()) {
949 Log.d(LOGTAG, "onPageStarted() network unavailable");
950 if (mNetworkShouldNotify) {
951 handleNetworkNotify(view);
952 } else {
953 view.setNetworkAvailable(false);
954 }
955 mNetworkShouldNotify = false;
956 } else {
957 Log.d(LOGTAG, "onPageStarted() network available");
958 if (mNetworkShouldNotify) {
959 handleNetworkNotify(view);
960 }
961 mNetworkShouldNotify = false;
962 }
963 } else {
964 if (!mNetworkHandler.isNetworkUp()) {
965 view.setNetworkAvailable(false);
966 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 }
968
969 // when BrowserActivity just starts, onPageStarted may be called before
970 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
971 // to start the timer. As we won't switch tabs while an activity is in
972 // pause state, we can ensure calling resume and pause in pair.
973 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800974 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700975 }
976 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 endActionMode();
978
John Reck30c714c2010-12-16 17:30:34 -0800979 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700980
John Reck324d4402011-01-11 16:56:42 -0800981 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 // update the bookmark database for favicon
983 maybeUpdateFavicon(tab, null, url, favicon);
984
985 Performance.tracePageStart(url);
986
987 // Performance probe
988 if (false) {
989 Performance.onPageStarted();
990 }
991
992 }
993
994 @Override
John Reck324d4402011-01-11 16:56:42 -0800995 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700996 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800997 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200998
Michael Kolb8233fac2010-10-26 16:08:53 -0700999 // Performance probe
1000 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001001 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001002 }
1003
1004 Performance.tracePageFinished();
1005 }
1006
1007 @Override
John Reck30c714c2010-12-16 17:30:34 -08001008 public void onProgressChanged(Tab tab) {
1009 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001010
1011 if (newProgress == 100) {
1012 CookieSyncManager.getInstance().sync();
1013 // onProgressChanged() may continue to be called after the main
1014 // frame has finished loading, as any remaining sub frames continue
1015 // to load. We'll only get called once though with newProgress as
1016 // 100 when everything is loaded. (onPageFinished is called once
1017 // when the main frame completes loading regardless of the state of
1018 // any sub frames so calls to onProgressChanges may continue after
1019 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001020 if (tab.inPageLoad()) {
1021 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +02001022 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
1023 // pause the WebView timer and release the wake lock if it is
1024 // finished while BrowserActivity is in pause state.
1025 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 }
John Reckd9862372012-02-21 15:04:50 -08001027 if (!tab.isPrivateBrowsingEnabled()
1028 && !TextUtils.isEmpty(tab.getUrl())
1029 && !tab.isSnapshot()) {
1030 // Only update the bookmark screenshot if the user did not
1031 // cancel the load early and there is not already
1032 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001033 if (tab.shouldUpdateThumbnail() &&
1034 (tab.inForeground() && !didUserStopLoading()
1035 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001036 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1037 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1038 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001039 1500);
John Reckd9862372012-02-21 15:04:50 -08001040 }
1041 }
1042 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001044 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001045 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001046 // still loading
1047 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001048 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001049 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 }
1051 }
John Reck30c714c2010-12-16 17:30:34 -08001052 mUi.onProgressChanged(tab);
1053 }
1054
1055 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001056 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001057 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001058 }
1059
1060 @Override
1061 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001062 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001063 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001064 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001065 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1066 return;
1067 }
1068 // Update the title in the history database if not in private browsing mode
1069 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001070 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001071 }
1072 }
1073
1074 @Override
1075 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001076 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1078 }
1079
1080 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001081 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1082 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001083 }
1084
1085 @Override
1086 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1087 if (mMenuIsDown) {
1088 // only check shortcut key when MENU is held
1089 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1090 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001092 int keyCode = event.getKeyCode();
1093 // We need to send almost every key to WebKit. However:
1094 // 1. We don't want to block the device on the renderer for
1095 // some keys like menu, home, call.
1096 // 2. There are no WebKit equivalents for some of these keys
1097 // (see app/keyboard_codes_win.h)
1098 // Note that these are not the same set as KeyEvent.isSystemKey:
1099 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1100 if (keyCode == KeyEvent.KEYCODE_MENU ||
1101 keyCode == KeyEvent.KEYCODE_HOME ||
1102 keyCode == KeyEvent.KEYCODE_BACK ||
1103 keyCode == KeyEvent.KEYCODE_CALL ||
1104 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1105 keyCode == KeyEvent.KEYCODE_POWER ||
1106 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1107 keyCode == KeyEvent.KEYCODE_CAMERA ||
1108 keyCode == KeyEvent.KEYCODE_FOCUS ||
1109 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1110 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1111 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1112 return true;
1113 }
1114
1115 // We also have to intercept some shortcuts before we send them to the ContentView.
1116 if (event.isCtrlPressed() && (
1117 keyCode == KeyEvent.KEYCODE_TAB ||
1118 keyCode == KeyEvent.KEYCODE_W ||
1119 keyCode == KeyEvent.KEYCODE_F4)) {
1120 return true;
1121 }
1122
1123 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001124 }
1125
1126 @Override
John Reck997b1b72012-04-19 18:08:25 -07001127 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001128 if (!isActivityPaused()) {
1129 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001130 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 } else {
John Reck997b1b72012-04-19 18:08:25 -07001132 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001133 }
1134 }
John Reck997b1b72012-04-19 18:08:25 -07001135 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 }
1137
1138 @Override
John Reck324d4402011-01-11 16:56:42 -08001139 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001140 // Don't save anything in private browsing mode
1141 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -08001142 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001143
John Reck324d4402011-01-11 16:56:42 -08001144 if (TextUtils.isEmpty(url)
1145 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 return;
1147 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001148
John Reckf57c0292011-07-21 18:15:39 -07001149 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001150 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 }
1152
1153 @Override
1154 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1155 AsyncTask<Void, Void, String[]> task =
1156 new AsyncTask<Void, Void, String[]>() {
1157 @Override
1158 public String[] doInBackground(Void... unused) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001159 Object[] params = {mActivity.getContentResolver()};
1160 Class[] type = new Class[] {ContentResolver.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001161 return (String[])ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001162 "android.provider.Browser","getVisitedHistory",
1163 type, params);
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 }
1165 @Override
1166 public void onPostExecute(String[] result) {
1167 callback.onReceiveValue(result);
1168 }
1169 };
1170 task.execute();
1171 }
1172
1173 @Override
1174 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1175 final HttpAuthHandler handler, final String host,
1176 final String realm) {
1177 String username = null;
1178 String password = null;
1179
1180 boolean reuseHttpAuthUsernamePassword
1181 = handler.useHttpAuthUsernamePassword();
1182
1183 if (reuseHttpAuthUsernamePassword && view != null) {
1184 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1185 if (credentials != null && credentials.length == 2) {
1186 username = credentials[0];
1187 password = credentials[1];
1188 }
1189 }
1190
1191 if (username != null && password != null) {
1192 handler.proceed(username, password);
1193 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001194 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001195 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1196 } else {
1197 handler.cancel();
1198 }
1199 }
1200 }
1201
1202 @Override
1203 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001204 String contentDisposition, String mimetype, String referer,
1205 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001206 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001207 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001208 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001209 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001210 // This Tab was opened for the sole purpose of downloading a
1211 // file. Remove it.
1212 if (tab == mTabControl.getCurrentTab()) {
1213 // In this case, the Tab is still on top.
1214 goBackOnePageOrQuit();
1215 } else {
1216 // In this case, it is not.
1217 closeTab(tab);
1218 }
1219 }
1220 }
1221
1222 @Override
1223 public Bitmap getDefaultVideoPoster() {
1224 return mUi.getDefaultVideoPoster();
1225 }
1226
1227 @Override
1228 public View getVideoLoadingProgressView() {
1229 return mUi.getVideoLoadingProgressView();
1230 }
1231
1232 @Override
1233 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1234 SslError error) {
1235 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1236 }
1237
1238 // helper method
1239
1240 /*
1241 * Update the favorites icon if the private browsing isn't enabled and the
1242 * icon is valid.
1243 */
1244 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1245 final String url, Bitmap favicon) {
1246 if (favicon == null) {
1247 return;
1248 }
1249 if (!tab.isPrivateBrowsingEnabled()) {
1250 Bookmarks.updateFavicon(mActivity
1251 .getContentResolver(), originalUrl, url, favicon);
1252 }
1253 }
1254
Leon Scroggins4cd97792010-12-03 15:31:56 -05001255 @Override
1256 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001257 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001258 mUi.bookmarkedStatusHasChanged(tab);
1259 }
1260
Michael Kolb8233fac2010-10-26 16:08:53 -07001261 // end WebViewController
1262
1263 protected void pageUp() {
1264 getCurrentTopWebView().pageUp(false);
1265 }
1266
1267 protected void pageDown() {
1268 getCurrentTopWebView().pageDown(false);
1269 }
1270
1271 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001272 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 public void editUrl() {
1274 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001275 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 }
1277
John Reck9c35b9c2012-05-30 10:08:50 -07001278 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001279 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001280 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 if (tab.inForeground()) {
1282 if (mUi.isCustomViewShowing()) {
1283 callback.onCustomViewHidden();
1284 return;
1285 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001286 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001287 // Save the menu state and set it to empty while the custom
1288 // view is showing.
1289 mOldMenuState = mMenuState;
1290 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001291 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 }
1293 }
1294
1295 @Override
1296 public void hideCustomView() {
1297 if (mUi.isCustomViewShowing()) {
1298 mUi.onHideCustomView();
1299 // Reset the old menu state.
1300 mMenuState = mOldMenuState;
1301 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001302 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001303 }
1304 }
1305
John Reck9c35b9c2012-05-30 10:08:50 -07001306 @Override
1307 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001308 Intent intent) {
1309 if (getCurrentTopWebView() == null) return;
1310 switch (requestCode) {
1311 case PREFERENCES_PAGE:
1312 if (resultCode == Activity.RESULT_OK && intent != null) {
1313 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001314 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001315 mTabControl.removeParentChildRelationShips();
1316 }
1317 }
1318 break;
1319 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001320 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001321 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001322 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001323 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001324 case AUTOFILL_SETUP:
1325 // Determine whether a profile was actually set up or not
1326 // and if so, send the message back to the WebTextView to
1327 // fill the form with the new profile.
1328 if (getSettings().getAutoFillProfile() != null) {
1329 mAutoFillSetupMessage.sendToTarget();
1330 mAutoFillSetupMessage = null;
1331 }
1332 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001333 case COMBO_VIEW:
1334 if (intent == null || resultCode != Activity.RESULT_OK) {
1335 break;
1336 }
John Reck3ba45532011-08-11 16:26:53 -07001337 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001338 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1339 Tab t = getCurrentTab();
1340 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001341 mUpdateMyNavThumbnail = true;
1342 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001343 loadUrl(t, uri.toString());
1344 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1345 String[] urls = intent.getStringArrayExtra(
1346 ComboViewActivity.EXTRA_OPEN_ALL);
1347 Tab parent = getCurrentTab();
1348 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001349 if (url != null) {
1350 parent = openTab(url, parent,
1351 !mSettings.openInBackground(), true);
1352 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001353 }
1354 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1355 long id = intent.getLongExtra(
1356 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1357 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001358 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001359 }
1360 }
1361 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001362 case VOICE_RESULT:
1363 if (resultCode == Activity.RESULT_OK && intent != null) {
1364 ArrayList<String> results = intent.getStringArrayListExtra(
1365 RecognizerIntent.EXTRA_RESULTS);
1366 if (results.size() >= 1) {
1367 mVoiceResult = results.get(0);
1368 }
1369 }
1370 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001371 case MY_NAVIGATION:
1372 if (intent == null || resultCode != Activity.RESULT_OK) {
1373 break;
1374 }
1375
1376 if (intent.getBooleanExtra("need_refresh", false) &&
1377 getCurrentTopWebView() != null) {
1378 getCurrentTopWebView().reload();
1379 }
1380 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001381 default:
1382 break;
1383 }
1384 getCurrentTopWebView().requestFocus();
1385 }
1386
1387 /**
1388 * Open the Go page.
1389 * @param startWithHistory If true, open starting on the history tab.
1390 * Otherwise, start with the bookmarks tab.
1391 */
1392 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001393 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 if (mTabControl.getCurrentWebView() == null) {
1395 return;
1396 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001397 // clear action mode
1398 if (isInCustomActionMode()) {
1399 endActionMode();
1400 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 Bundle extras = new Bundle();
1402 // Disable opening in a new window if we have maxed out the windows
1403 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1404 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001405 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 }
1407
1408 // combo view callbacks
1409
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 // key handling
1411 protected void onBackKey() {
1412 if (!mUi.onBackKey()) {
1413 WebView subwindow = mTabControl.getCurrentSubWindow();
1414 if (subwindow != null) {
1415 if (subwindow.canGoBack()) {
1416 subwindow.goBack();
1417 } else {
1418 dismissSubWindow(mTabControl.getCurrentTab());
1419 }
1420 } else {
1421 goBackOnePageOrQuit();
1422 }
1423 }
1424 }
1425
Michael Kolb4bd767d2011-05-27 11:33:55 -07001426 protected boolean onMenuKey() {
1427 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001428 }
1429
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 // menu handling and state
1431 // TODO: maybe put into separate handler
1432
John Reck9c35b9c2012-05-30 10:08:50 -07001433 @Override
1434 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001435 if (mMenuState == EMPTY_MENU) {
1436 return false;
1437 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001438 MenuInflater inflater = mActivity.getMenuInflater();
1439 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001440 return true;
1441 }
1442
John Reck9c35b9c2012-05-30 10:08:50 -07001443 @Override
1444 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001445 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001446 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001447 return;
1448 }
1449 if (!(v instanceof WebView)) {
1450 return;
1451 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001452 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 WebView.HitTestResult result = webview.getHitTestResult();
1454 if (result == null) {
1455 return;
1456 }
1457
1458 int type = result.getType();
1459 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1460 Log.w(LOGTAG,
1461 "We should not show context menu when nothing is touched");
1462 return;
1463 }
1464 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1465 // let TextView handles context menu
1466 return;
1467 }
1468
1469 // Note, http://b/issue?id=1106666 is requesting that
1470 // an inflated menu can be used again. This is not available
1471 // yet, so inflate each time (yuk!)
1472 MenuInflater inflater = mActivity.getMenuInflater();
1473 inflater.inflate(R.menu.browsercontext, menu);
1474
1475 // Show the correct menu group
1476 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001477 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001478 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001479 menu.setGroupVisible(R.id.PHONE_MENU,
1480 type == WebView.HitTestResult.PHONE_TYPE);
1481 menu.setGroupVisible(R.id.EMAIL_MENU,
1482 type == WebView.HitTestResult.EMAIL_TYPE);
1483 menu.setGroupVisible(R.id.GEO_MENU,
1484 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001485 String itemUrl = null;
1486 String url = webview.getOriginalUrl();
1487 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1488 itemUrl = Uri.decode(navigationUrl);
1489 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1490 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1491 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1492 } else {
1493 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1494 }
1495 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1496 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1497 } else {
1498 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1499
1500 menu.setGroupVisible(R.id.IMAGE_MENU,
1501 type == WebView.HitTestResult.IMAGE_TYPE
1502 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1503 menu.setGroupVisible(R.id.ANCHOR_MENU,
1504 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1505 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1506 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001507 // Setup custom handling depending on the type
1508 switch (type) {
1509 case WebView.HitTestResult.PHONE_TYPE:
1510 menu.setHeaderTitle(Uri.decode(extra));
1511 menu.findItem(R.id.dial_context_menu_id).setIntent(
1512 new Intent(Intent.ACTION_VIEW, Uri
1513 .parse(WebView.SCHEME_TEL + extra)));
1514 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1515 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1516 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1517 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1518 addIntent);
1519 menu.findItem(R.id.copy_phone_context_menu_id)
1520 .setOnMenuItemClickListener(
1521 new Copy(extra));
1522 break;
1523
1524 case WebView.HitTestResult.EMAIL_TYPE:
1525 menu.setHeaderTitle(extra);
1526 menu.findItem(R.id.email_context_menu_id).setIntent(
1527 new Intent(Intent.ACTION_VIEW, Uri
1528 .parse(WebView.SCHEME_MAILTO + extra)));
1529 menu.findItem(R.id.copy_mail_context_menu_id)
1530 .setOnMenuItemClickListener(
1531 new Copy(extra));
1532 break;
1533
1534 case WebView.HitTestResult.GEO_TYPE:
1535 menu.setHeaderTitle(extra);
1536 menu.findItem(R.id.map_context_menu_id).setIntent(
1537 new Intent(Intent.ACTION_VIEW, Uri
1538 .parse(WebView.SCHEME_GEO
1539 + URLEncoder.encode(extra))));
1540 menu.findItem(R.id.copy_geo_context_menu_id)
1541 .setOnMenuItemClickListener(
1542 new Copy(extra));
1543 break;
1544
1545 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1546 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001547 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001548 // decide whether to show the open link in new tab option
1549 boolean showNewTab = mTabControl.canCreateNewTab();
1550 MenuItem newTabItem
1551 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001552 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001553 ? R.string.contextmenu_openlink_newwindow_background
1554 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001555 newTabItem.setVisible(showNewTab);
1556 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001557 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1558 newTabItem.setOnMenuItemClickListener(
1559 new MenuItem.OnMenuItemClickListener() {
1560 @Override
1561 public boolean onMenuItemClick(MenuItem item) {
1562 final HashMap<String, WebView> hrefMap =
1563 new HashMap<String, WebView>();
1564 hrefMap.put("webview", webview);
1565 final Message msg = mHandler.obtainMessage(
1566 FOCUS_NODE_HREF,
1567 R.id.open_newtab_context_menu_id,
1568 0, hrefMap);
1569 webview.requestFocusNodeHref(msg);
1570 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001571 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001572 });
1573 } else {
1574 newTabItem.setOnMenuItemClickListener(
1575 new MenuItem.OnMenuItemClickListener() {
1576 @Override
1577 public boolean onMenuItemClick(MenuItem item) {
1578 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001579 openTab(extra, parent,
1580 !mSettings.openInBackground(),
1581 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001582 return true;
1583 }
1584 });
1585 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001586 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001587 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1588 menu.setHeaderTitle(navigationUrl);
1589 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1590
1591 if (itemUrl != null) {
1592 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1593 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1594 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1595 @Override
1596 public boolean onMenuItemClick(MenuItem item) {
1597 final Intent intent = new Intent(Controller.this
1598 .getContext(),
1599 AddMyNavigationPage.class);
1600 Bundle bundle = new Bundle();
1601 String url = Uri.decode(navigationUrl);
1602 bundle.putBoolean("isAdding", false);
1603 bundle.putString("url", url);
1604 bundle.putString("name", getNameFromUrl(url));
1605 intent.putExtra("websites", bundle);
1606 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1607 return false;
1608 }
1609 });
1610 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1611 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1612 @Override
1613 public boolean onMenuItemClick(MenuItem item) {
1614 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1615 return false;
1616 }
1617 });
1618 }
1619 } else {
1620 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1621 }
1622 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1624 break;
1625 }
1626 // otherwise fall through to handle image part
1627 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001628 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1629 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001630 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1631 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001632 shareItem.setOnMenuItemClickListener(
1633 new MenuItem.OnMenuItemClickListener() {
1634 @Override
1635 public boolean onMenuItemClick(MenuItem item) {
1636 sharePage(mActivity, null, extra, null,
1637 null);
1638 return true;
1639 }
1640 }
1641 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001642 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001643 menu.findItem(R.id.view_image_context_menu_id)
1644 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1645 @Override
1646 public boolean onMenuItemClick(MenuItem item) {
1647 openTab(extra, mTabControl.getCurrentTab(), true, true);
1648 return false;
1649 }
1650 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001651 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1652 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1653 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001654 menu.findItem(R.id.set_wallpaper_context_menu_id).
1655 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1656 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001657 break;
1658
1659 default:
1660 Log.w(LOGTAG, "We should not get here.");
1661 break;
1662 }
1663 //update the ui
1664 mUi.onContextMenuCreated(menu);
1665 }
1666
kaiyiz6e5b3e02013-08-19 20:02:01 +08001667 public void startAddMyNavigation(String url) {
1668 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1669 Bundle bundle = new Bundle();
1670 bundle.putBoolean("isAdding", true);
1671 bundle.putString("url", url);
1672 bundle.putString("name", getNameFromUrl(url));
1673 intent.putExtra("websites", bundle);
1674 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1675 }
1676
1677 private void showMyNavigationDeleteDialog(final String itemUrl) {
1678 new AlertDialog.Builder(this.getContext())
1679 .setTitle(R.string.my_navigation_delete_label)
1680 .setIcon(android.R.drawable.ic_dialog_alert)
1681 .setMessage(R.string.my_navigation_delete_msg)
1682 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1683 @Override
1684 public void onClick(DialogInterface dialog, int whichButton) {
1685 deleteMyNavigationItem(itemUrl);
1686 }
1687 })
1688 .setNegativeButton(R.string.cancel, null)
1689 .show();
1690 }
1691
1692 private void deleteMyNavigationItem(final String itemUrl) {
1693 ContentResolver cr = this.getContext().getContentResolver();
1694 Cursor cursor = null;
1695
1696 try {
1697 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1698 new String[] {
1699 MyNavigationUtil.ID
1700 }, "url = ?", new String[] {
1701 itemUrl
1702 }, null);
1703 if (null != cursor && cursor.moveToFirst()) {
1704 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1705 cursor.getLong(0));
1706
1707 ContentValues values = new ContentValues();
1708 values.put(MyNavigationUtil.TITLE, "");
1709 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1710 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1711 ByteArrayOutputStream os = new ByteArrayOutputStream();
1712 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1713 R.raw.my_navigation_add);
1714 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1715 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1716 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1717 cr.update(uri, values, null, null);
1718 } else {
1719 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1720 }
1721 } catch (IllegalStateException e) {
1722 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1723 } finally {
1724 if (null != cursor) {
1725 cursor.close();
1726 }
1727 }
1728
1729 if (getCurrentTopWebView() != null) {
1730 getCurrentTopWebView().reload();
1731 }
1732 }
1733
1734 private String getNameFromUrl(String itemUrl) {
1735 ContentResolver cr = this.getContext().getContentResolver();
1736 Cursor cursor = null;
1737 String name = null;
1738
1739 try {
1740 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1741 new String[] {
1742 MyNavigationUtil.TITLE
1743 }, "url = ?", new String[] {
1744 itemUrl
1745 }, null);
1746 if (null != cursor && cursor.moveToFirst()) {
1747 name = cursor.getString(0);
1748 } else {
1749 Log.e(LOGTAG, "this item does not exist!");
1750 }
1751 } catch (IllegalStateException e) {
1752 Log.e(LOGTAG, "getNameFromUrl", e);
1753 } finally {
1754 if (null != cursor) {
1755 cursor.close();
1756 }
1757 }
1758 return name;
1759 }
1760
1761 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1762 int width = mActivity.getResources().getDimensionPixelOffset(
1763 R.dimen.myNavigationThumbnailWidth);
1764 int height = mActivity.getResources().getDimensionPixelOffset(
1765 R.dimen.myNavigationThumbnailHeight);
1766
1767 final Bitmap bm = createScreenshot(webView, width, height);
1768
1769 if (bm == null) {
1770 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1771 return;
1772 }
1773
1774 final ContentResolver cr = mActivity.getContentResolver();
1775 new AsyncTask<Void, Void, Void>() {
1776 @Override
1777 protected Void doInBackground(Void... unused) {
1778 ContentResolver cr = mActivity.getContentResolver();
1779 Cursor cursor = null;
1780 try {
1781 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1782 new String[] {
1783 MyNavigationUtil.ID
1784 }, "url = ?", new String[] {
1785 itemUrl
1786 }, null);
1787 if (null != cursor && cursor.moveToFirst()) {
1788 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1789 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1790
1791 ContentValues values = new ContentValues();
1792 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1793 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1794 cursor.getLong(0));
1795 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1796 cr.update(uri, values, null, null);
1797 os.close();
1798 }
1799 } catch (IllegalStateException e) {
1800 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1801 } catch (IOException e) {
1802 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1803 } finally {
1804 if (null != cursor) {
1805 cursor.close();
1806 }
1807 }
1808 return null;
1809 }
1810 }.execute();
1811 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001812 /**
1813 * As the menu can be open when loading state changes
1814 * we must manually update the state of the stop/reload menu
1815 * item
1816 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001817 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001818 if (menu == null) {
1819 return;
1820 }
1821 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001822 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001823 menu.findItem(R.id.stop_menu_id):
1824 menu.findItem(R.id.reload_menu_id);
1825 if (src != null) {
1826 dest.setIcon(src.getIcon());
1827 dest.setTitle(src.getTitle());
1828 }
1829 }
1830
John Reck9c35b9c2012-05-30 10:08:50 -07001831 @Override
1832 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001833 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001834 // hold on to the menu reference here; it is used by the page callbacks
1835 // to update the menu based on loading state
1836 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001837 // Note: setVisible will decide whether an item is visible; while
1838 // setEnabled() will decide whether an item is enabled, which also means
1839 // whether the matching shortcut key will function.
1840 switch (mMenuState) {
1841 case EMPTY_MENU:
1842 if (mCurrentMenuState != mMenuState) {
1843 menu.setGroupVisible(R.id.MAIN_MENU, false);
1844 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1845 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1846 }
1847 break;
1848 default:
1849 if (mCurrentMenuState != mMenuState) {
1850 menu.setGroupVisible(R.id.MAIN_MENU, true);
1851 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1852 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1853 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001854 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001855 break;
1856 }
1857 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001858 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001859 }
1860
Michael Kolb4bf79712011-07-14 14:18:12 -07001861 @Override
1862 public void updateMenuState(Tab tab, Menu menu) {
1863 boolean canGoBack = false;
1864 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001865 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001866 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001867 if (tab != null) {
1868 canGoBack = tab.canGoBack();
1869 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001870 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001871 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001872 }
1873 final MenuItem back = menu.findItem(R.id.back_menu_id);
1874 back.setEnabled(canGoBack);
1875
1876 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001877
1878 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1879 forward.setEnabled(canGoForward);
1880
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001881 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1882 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001883 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001884 if (source != null && dest != null) {
1885 dest.setTitle(source.getTitle());
1886 dest.setIcon(source.getIcon());
1887 }
John Recke1a03a32011-09-14 17:04:16 -07001888 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001889
1890 // decide whether to show the share link option
1891 PackageManager pm = mActivity.getPackageManager();
1892 Intent send = new Intent(Intent.ACTION_SEND);
1893 send.setType("text/plain");
1894 ResolveInfo ri = pm.resolveActivity(send,
1895 PackageManager.MATCH_DEFAULT_ONLY);
1896 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1897
1898 boolean isNavDump = mSettings.enableNavDump();
1899 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1900 nav.setVisible(isNavDump);
1901 nav.setEnabled(isNavDump);
1902
1903 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001904 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1905 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001906 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1907 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001908 // history and snapshots item are the members of COMBO menu group,
1909 // so if show history item, only make snapshots item invisible.
1910 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001911
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001912 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001913 }
1914
John Reck9c35b9c2012-05-30 10:08:50 -07001915 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001916 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001917 if (null == getCurrentTopWebView()) {
1918 return false;
1919 }
1920 if (mMenuIsDown) {
1921 // The shortcut action consumes the MENU. Even if it is still down,
1922 // it won't trigger the next shortcut action. In the case of the
1923 // shortcut action triggering a new activity, like Bookmarks, we
1924 // won't get onKeyUp for MENU. So it is important to reset it here.
1925 mMenuIsDown = false;
1926 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001927 if (mUi.onOptionsItemSelected(item)) {
1928 // ui callback handled it
1929 return true;
1930 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001931 switch (item.getItemId()) {
1932 // -- Main menu
1933 case R.id.new_tab_menu_id:
1934 openTabToHomePage();
1935 break;
1936
1937 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001938 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001939 break;
1940
Afzal Najamd4e33312012-04-26 01:54:01 -04001941 case R.id.close_other_tabs_id:
1942 closeOtherTabs();
1943 break;
1944
Michael Kolb8233fac2010-10-26 16:08:53 -07001945 case R.id.goto_menu_id:
1946 editUrl();
1947 break;
1948
1949 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001950 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1951 break;
1952
1953 case R.id.history_menu_id:
1954 bookmarksOrHistoryPicker(ComboViews.History);
1955 break;
1956
1957 case R.id.snapshots_menu_id:
1958 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001959 break;
1960
Michael Kolb8233fac2010-10-26 16:08:53 -07001961 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001962 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 break;
1964
1965 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001966 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 stopLoading();
1968 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001969 Tab currentTab = mTabControl.getCurrentTab();
1970 if (currentTab.hasCrashed) {
1971 currentTab.replaceCrashView(getCurrentTopWebView(),
1972 currentTab.getViewContainer());
1973 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001974 getCurrentTopWebView().reload();
1975 }
1976 break;
1977
1978 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001979 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001980 break;
1981
1982 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001983 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001984 break;
1985
1986 case R.id.close_menu_id:
1987 // Close the subwindow if it exists.
1988 if (mTabControl.getCurrentSubWindow() != null) {
1989 dismissSubWindow(mTabControl.getCurrentTab());
1990 break;
1991 }
1992 closeCurrentTab();
1993 break;
1994
kaiyiza8b6dbb2013-07-29 18:11:22 +08001995 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001996 Object[] params = { new String("persist.debug.browsermonkeytest")};
1997 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001998 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001999 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002000 if (ret != null && ret.equals("enable"))
2001 break;
luxiaolb40014b2013-07-19 10:01:43 +08002002 showExitDialog(mActivity);
2003 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002004 case R.id.homepage_menu_id:
2005 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002006 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002007 break;
2008
2009 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002010 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002011 break;
2012
2013 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002014 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002015 break;
2016
John Reck2bc80422011-06-30 15:11:49 -07002017 case R.id.save_snapshot_menu_id:
2018 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002019 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07002020 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05002021 break;
2022
Michael Kolb8233fac2010-10-26 16:08:53 -07002023 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002024 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002025 break;
2026
John Recke1a03a32011-09-14 17:04:16 -07002027 case R.id.snapshot_go_live:
2028 goLive();
2029 return true;
2030
Michael Kolb8233fac2010-10-26 16:08:53 -07002031 case R.id.share_page_menu_id:
2032 Tab currentTab = mTabControl.getCurrentTab();
2033 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002034 return false;
2035 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002036 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002037 break;
2038
2039 case R.id.dump_nav_menu_id:
2040 getCurrentTopWebView().debugDump();
2041 break;
2042
Michael Kolb8233fac2010-10-26 16:08:53 -07002043 case R.id.zoom_in_menu_id:
2044 getCurrentTopWebView().zoomIn();
2045 break;
2046
2047 case R.id.zoom_out_menu_id:
2048 getCurrentTopWebView().zoomOut();
2049 break;
2050
2051 case R.id.view_downloads_menu_id:
2052 viewDownloads();
2053 break;
2054
John Reck42229bc2011-08-19 13:26:43 -07002055 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002056 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002057 break;
2058
Michael Kolb8233fac2010-10-26 16:08:53 -07002059 case R.id.window_one_menu_id:
2060 case R.id.window_two_menu_id:
2061 case R.id.window_three_menu_id:
2062 case R.id.window_four_menu_id:
2063 case R.id.window_five_menu_id:
2064 case R.id.window_six_menu_id:
2065 case R.id.window_seven_menu_id:
2066 case R.id.window_eight_menu_id:
2067 {
2068 int menuid = item.getItemId();
2069 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2070 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2071 Tab desiredTab = mTabControl.getTab(id);
2072 if (desiredTab != null &&
2073 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002074 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002075 }
2076 break;
2077 }
2078 }
2079 }
2080 break;
2081
2082 default:
2083 return false;
2084 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002085 return true;
2086 }
2087
John Reck68234a92012-04-19 15:27:12 -07002088 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2089 implements OnCancelListener {
2090
2091 private Tab mTab;
2092 private Dialog mProgressDialog;
2093 private ContentValues mValues;
2094
2095 private SaveSnapshotTask(Tab tab) {
2096 mTab = tab;
2097 }
2098
2099 @Override
2100 protected void onPreExecute() {
2101 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2102 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2103 true, true, this);
2104 mValues = mTab.createSnapshotValues();
2105 }
2106
2107 @Override
2108 protected Long doInBackground(Void... params) {
2109 if (!mTab.saveViewState(mValues)) {
2110 return null;
2111 }
2112 if (isCancelled()) {
2113 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2114 File file = mActivity.getFileStreamPath(path);
2115 if (!file.delete()) {
2116 file.deleteOnExit();
2117 }
2118 return null;
2119 }
2120 final ContentResolver cr = mActivity.getContentResolver();
2121 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2122 if (result == null) {
2123 return null;
2124 }
2125 long id = ContentUris.parseId(result);
2126 return id;
2127 }
2128
2129 @Override
2130 protected void onPostExecute(Long id) {
2131 if (isCancelled()) {
2132 return;
2133 }
2134 mProgressDialog.dismiss();
2135 if (id == null) {
2136 Toast.makeText(mActivity, R.string.snapshot_failed,
2137 Toast.LENGTH_SHORT).show();
2138 return;
2139 }
2140 Bundle b = new Bundle();
2141 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2142 mUi.showComboView(ComboViews.Snapshots, b);
2143 }
2144
2145 @Override
2146 public void onCancel(DialogInterface dialog) {
2147 cancel(true);
2148 }
2149 }
2150
Michael Kolb80f75082012-04-10 10:50:06 -07002151 @Override
2152 public void toggleUserAgent() {
2153 WebView web = getCurrentWebView();
2154 mSettings.toggleDesktopUseragent(web);
2155 web.loadUrl(web.getOriginalUrl());
2156 }
2157
2158 @Override
2159 public void findOnPage() {
2160 getCurrentTopWebView().showFindDialog(null, true);
2161 }
2162
2163 @Override
2164 public void openPreferences() {
2165 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2166 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2167 getCurrentTopWebView().getUrl());
2168 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2169 }
2170
2171 @Override
2172 public void bookmarkCurrentPage() {
2173 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2174 if (bookmarkIntent != null) {
2175 mActivity.startActivity(bookmarkIntent);
2176 }
2177 }
2178
John Recke1a03a32011-09-14 17:04:16 -07002179 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002180 SnapshotTab t = (SnapshotTab) getCurrentTab();
2181 t.loadUrl(t.getLiveUrl(), null);
John Recke1a03a32011-09-14 17:04:16 -07002182 }
2183
luxiaolb40014b2013-07-19 10:01:43 +08002184 private void showExitDialog(final Activity activity) {
2185 new AlertDialog.Builder(activity)
2186 .setTitle(R.string.exit_browser_title)
2187 .setIcon(android.R.drawable.ic_dialog_alert)
2188 .setMessage(R.string.exit_browser_msg)
2189 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2190 public void onClick(DialogInterface dialog, int which) {
2191 activity.moveTaskToBack(true);
2192 dialog.dismiss();
2193 }
2194 })
2195 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2196 public void onClick(DialogInterface dialog, int which) {
2197 activity.finish();
2198 mHandler.postDelayed(new Runnable() {
2199 @Override
2200 public void run() {
2201 // TODO Auto-generated method stub
2202 mCrashRecoveryHandler.clearState(true);
2203 int pid = android.os.Process.myPid();
2204 android.os.Process.killProcess(pid);
2205 }
2206 }, 300);
2207 dialog.dismiss();
2208 }
2209 })
2210 .show();
2211 }
Michael Kolb315d5022011-10-13 12:47:11 -07002212 @Override
2213 public void showPageInfo() {
2214 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2215 }
2216
John Reck9c35b9c2012-05-30 10:08:50 -07002217 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002218 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002219 // Let the History and Bookmark fragments handle menus they created.
2220 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2221 return false;
2222 }
2223
Michael Kolb8233fac2010-10-26 16:08:53 -07002224 int id = item.getItemId();
2225 boolean result = true;
2226 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002227 // -- Browser context menu
2228 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002229 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002230 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002231 case R.id.copy_link_context_menu_id:
2232 final WebView webView = getCurrentTopWebView();
2233 if (null == webView) {
2234 result = false;
2235 break;
2236 }
2237 final HashMap<String, WebView> hrefMap =
2238 new HashMap<String, WebView>();
2239 hrefMap.put("webview", webView);
2240 final Message msg = mHandler.obtainMessage(
2241 FOCUS_NODE_HREF, id, 0, hrefMap);
2242 webView.requestFocusNodeHref(msg);
2243 break;
2244
2245 default:
2246 // For other context menus
2247 result = onOptionsItemSelected(item);
2248 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002249 return result;
2250 }
2251
2252 /**
2253 * support programmatically opening the context menu
2254 */
2255 public void openContextMenu(View view) {
2256 mActivity.openContextMenu(view);
2257 }
2258
2259 /**
2260 * programmatically open the options menu
2261 */
2262 public void openOptionsMenu() {
2263 mActivity.openOptionsMenu();
2264 }
2265
John Reck9c35b9c2012-05-30 10:08:50 -07002266 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002267 public boolean onMenuOpened(int featureId, Menu menu) {
2268 if (mOptionsMenuOpen) {
2269 if (mConfigChanged) {
2270 // We do not need to make any changes to the state of the
2271 // title bar, since the only thing that happened was a
2272 // change in orientation
2273 mConfigChanged = false;
2274 } else {
2275 if (!mExtendedMenuOpen) {
2276 mExtendedMenuOpen = true;
2277 mUi.onExtendedMenuOpened();
2278 } else {
2279 // Switching the menu back to icon view, so show the
2280 // title bar once again.
2281 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002282 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002283 }
2284 }
2285 } else {
2286 // The options menu is closed, so open it, and show the title
2287 mOptionsMenuOpen = true;
2288 mConfigChanged = false;
2289 mExtendedMenuOpen = false;
2290 mUi.onOptionsMenuOpened();
2291 }
2292 return true;
2293 }
2294
John Reck9c35b9c2012-05-30 10:08:50 -07002295 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002296 public void onOptionsMenuClosed(Menu menu) {
2297 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002298 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002299 }
2300
John Reck9c35b9c2012-05-30 10:08:50 -07002301 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002303 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002304 }
2305
2306 // Helper method for getting the top window.
2307 @Override
2308 public WebView getCurrentTopWebView() {
2309 return mTabControl.getCurrentTopWebView();
2310 }
2311
2312 @Override
2313 public WebView getCurrentWebView() {
2314 return mTabControl.getCurrentWebView();
2315 }
2316
2317 /*
2318 * This method is called as a result of the user selecting the options
2319 * menu to see the download window. It shows the download window on top of
2320 * the current window.
2321 */
2322 void viewDownloads() {
2323 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2324 mActivity.startActivity(intent);
2325 }
2326
John Reck30b065e2011-07-19 10:58:05 -07002327 int getActionModeHeight() {
2328 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2329 new int[] { android.R.attr.actionBarSize });
2330 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2331 actionBarSizeTypedArray.recycle();
2332 return size;
2333 }
2334
Michael Kolb8233fac2010-10-26 16:08:53 -07002335 // action mode
2336
John Reck9c35b9c2012-05-30 10:08:50 -07002337 @Override
2338 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002339 mUi.onActionModeStarted(mode);
2340 mActionMode = mode;
2341 }
2342
2343 /*
2344 * True if a custom ActionMode (i.e. find or select) is in use.
2345 */
2346 @Override
2347 public boolean isInCustomActionMode() {
2348 return mActionMode != null;
2349 }
2350
2351 /*
2352 * End the current ActionMode.
2353 */
2354 @Override
2355 public void endActionMode() {
2356 if (mActionMode != null) {
2357 mActionMode.finish();
2358 }
2359 }
2360
2361 /*
2362 * Called by find and select when they are finished. Replace title bars
2363 * as necessary.
2364 */
John Reck9c35b9c2012-05-30 10:08:50 -07002365 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002366 public void onActionModeFinished(ActionMode mode) {
2367 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002368 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002369 mActionMode = null;
2370 }
2371
2372 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002373 final Tab tab = getCurrentTab();
2374 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002375 }
2376
2377 // bookmark handling
2378
2379 /**
2380 * add the current page as a bookmark to the given folder id
2381 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002382 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002383 * bookmarked, and if it is, edit that bookmark. If false, and
2384 * the site is already bookmarked, do not attempt to edit the
2385 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002386 */
2387 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002388 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002389 WebView w = getCurrentTopWebView();
2390 if (w == null) {
2391 return null;
2392 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002393 Intent i = new Intent(mActivity,
2394 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002395 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2396 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2397 String touchIconUrl = w.getTouchIconUrl();
2398 if (touchIconUrl != null) {
2399 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2400 WebSettings settings = w.getSettings();
2401 if (settings != null) {
2402 i.putExtra(AddBookmarkPage.USER_AGENT,
2403 settings.getUserAgentString());
2404 }
2405 }
2406 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2407 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2408 getDesiredThumbnailHeight(mActivity)));
2409 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002410 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002411 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2412 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002413 // Put the dialog at the upper right of the screen, covering the
2414 // star on the title bar.
2415 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002416 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002417 }
2418
2419 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002420 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002421 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002423 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002424 }
2425
2426 // thumbnails
2427
2428 /**
2429 * Return the desired width for thumbnail screenshots, which are stored in
2430 * the database, and used on the bookmarks screen.
2431 * @param context Context for finding out the density of the screen.
2432 * @return desired width for thumbnail screenshot.
2433 */
2434 static int getDesiredThumbnailWidth(Context context) {
2435 return context.getResources().getDimensionPixelOffset(
2436 R.dimen.bookmarkThumbnailWidth);
2437 }
2438
2439 /**
2440 * Return the desired height for thumbnail screenshots, which are stored in
2441 * the database, and used on the bookmarks screen.
2442 * @param context Context for finding out the density of the screen.
2443 * @return desired height for thumbnail screenshot.
2444 */
2445 static int getDesiredThumbnailHeight(Context context) {
2446 return context.getResources().getDimensionPixelOffset(
2447 R.dimen.bookmarkThumbnailHeight);
2448 }
2449
John Reck8cc92352011-07-06 17:41:52 -07002450 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002451 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002452 return null;
2453 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002454
2455 Bitmap viewportBitmap = view.getViewportBitmap();
2456 if (viewportBitmap == null) {
2457 return null;
2458 }
2459
2460 float aspectRatio = (float) width/height;
2461 int viewportWidth = viewportBitmap.getWidth();
2462 int viewportHeight = viewportBitmap.getHeight();
2463
2464 //modify the size to attain the same aspect ratio of desired thumbnail size
2465 if (viewportHeight > viewportWidth) {
2466 viewportHeight= (int)Math.round(viewportWidth * aspectRatio);
2467 } else {
2468 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2469 }
2470
2471 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2472 Rect dstRect = new Rect(0, 0, width, height);
2473
2474 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2475 || sThumbnailBitmap.getHeight() != height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002476 if (sThumbnailBitmap != null) {
2477 sThumbnailBitmap.recycle();
2478 sThumbnailBitmap = null;
2479 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002480
2481 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002482 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002483
John Reckd7dd9b22011-08-30 09:18:29 -07002484 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002485 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
John Reckd7dd9b22011-08-30 09:18:29 -07002486
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002487 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002488 }
2489
John Reck34ef2672011-02-10 11:30:55 -08002490 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002491 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002492 // FIXME: Would like to make sure there is actually something to
2493 // draw, but the API for that (WebViewCore.pictureReady()) is not
2494 // currently accessible here.
2495
John Reck34ef2672011-02-10 11:30:55 -08002496 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002497 if (view == null) {
2498 // Tab was destroyed
2499 return;
2500 }
John Reck34ef2672011-02-10 11:30:55 -08002501 final String url = tab.getUrl();
2502 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002503 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002504 if (TextUtils.isEmpty(url)) {
2505 return;
2506 }
2507
kaiyiz6e5b3e02013-08-19 20:02:01 +08002508 //update My Navigation Thumbnails
2509 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2510 if (isMyNavigationUrl) {
2511 updateMyNavigationThumbnail(url, view);
2512 }
John Reck34ef2672011-02-10 11:30:55 -08002513 // Only update thumbnails for web urls (http(s)://), not for
2514 // about:, javascript:, data:, etc...
2515 // Unless it is a bookmarked site, then always update
2516 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2517 return;
2518 }
2519
kaiyiz6e5b3e02013-08-19 20:02:01 +08002520 if (url != null && mUpdateMyNavThumbnailUrl != null
2521 && Patterns.WEB_URL.matcher(url).matches()
2522 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2523 String urlHost = (new WebAddress(url)).getHost();
2524 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2525 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2526 || bookmarkHost.length() == 0) {
2527 return;
2528 }
2529 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2530 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2531 bookmarkHost.length());
2532 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2533 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2534 if (!bookmarkDomain.equals(urlDomain)) {
2535 return;
2536 }
2537 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002538 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2539 getDesiredThumbnailHeight(mActivity));
2540 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002541 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2542 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2543 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2544 500);
2545 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002546 return;
2547 }
2548
2549 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002550 new AsyncTask<Void, Void, Void>() {
2551 @Override
2552 protected Void doInBackground(Void... unused) {
2553 Cursor cursor = null;
2554 try {
2555 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002556 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2557 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2558 : url);
2559 if (mUpdateMyNavThumbnail) {
2560 mUpdateMyNavThumbnail = false;
2561 mUpdateMyNavThumbnailUrl = null;
2562 }
John Reck34ef2672011-02-10 11:30:55 -08002563 if (cursor != null && cursor.moveToFirst()) {
2564 final ByteArrayOutputStream os =
2565 new ByteArrayOutputStream();
2566 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002567
John Reck34ef2672011-02-10 11:30:55 -08002568 ContentValues values = new ContentValues();
2569 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002570
John Reck34ef2672011-02-10 11:30:55 -08002571 do {
John Reck617fd832011-02-16 14:35:59 -08002572 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002573 cr.update(Images.CONTENT_URI, values, null, null);
2574 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002575 }
John Reck34ef2672011-02-10 11:30:55 -08002576 } catch (IllegalStateException e) {
2577 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002578 } catch (SQLiteException s) {
2579 // Added for possible error when user tries to remove the same bookmark
2580 // that is being updated with a screen shot
2581 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002582 } finally {
2583 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002584 }
John Reck34ef2672011-02-10 11:30:55 -08002585 return null;
2586 }
2587 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 }
2589
2590 private class Copy implements OnMenuItemClickListener {
2591 private CharSequence mText;
2592
John Reck9c35b9c2012-05-30 10:08:50 -07002593 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002594 public boolean onMenuItemClick(MenuItem item) {
2595 copy(mText);
2596 return true;
2597 }
2598
2599 public Copy(CharSequence toCopy) {
2600 mText = toCopy;
2601 }
2602 }
2603
Leon Scroggins63c02662010-11-18 15:16:27 -05002604 private static class Download implements OnMenuItemClickListener {
2605 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002607 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002608 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002609 private static final String FALLBACK_EXTENSION = "dat";
2610 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002611
John Reck9c35b9c2012-05-30 10:08:50 -07002612 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002613 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002614 if (DataUri.isDataUri(mText)) {
2615 saveDataUri();
2616 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002617 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002618 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002619 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002620 return true;
2621 }
2622
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002623 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2624 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002625 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002626 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002627 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002628 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002629 }
George Mount387d45d2011-10-07 15:57:53 -07002630
2631 /**
2632 * Treats mText as a data URI and writes its contents to a file
2633 * based on the current time.
2634 */
2635 private void saveDataUri() {
2636 FileOutputStream outputStream = null;
2637 try {
2638 DataUri uri = new DataUri(mText);
2639 File target = getTarget(uri);
2640 outputStream = new FileOutputStream(target);
2641 outputStream.write(uri.getData());
2642 final DownloadManager manager =
2643 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2644 manager.addCompletedDownload(target.getName(),
2645 mActivity.getTitle().toString(), false,
2646 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002647 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002648 } catch (IOException e) {
2649 Log.e(LOGTAG, "Could not save data URL");
2650 } finally {
2651 if (outputStream != null) {
2652 try {
2653 outputStream.close();
2654 } catch (IOException e) {
2655 // ignore close errors
2656 }
2657 }
2658 }
2659 }
2660
2661 /**
2662 * Creates a File based on the current time stamp and uses
2663 * the mime type of the DataUri to get the extension.
2664 */
2665 private File getTarget(DataUri uri) throws IOException {
2666 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002667 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002668 String nameBase = format.format(new Date());
2669 String mimeType = uri.getMimeType();
2670 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2671 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2672 if (extension == null) {
2673 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2674 extension = FALLBACK_EXTENSION;
2675 }
2676 extension = "." + extension; // createTempFile needs the '.'
2677 File targetFile = File.createTempFile(nameBase, extension, dir);
2678 return targetFile;
2679 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002680 }
2681
Cary Clark8974d282010-11-22 10:46:05 -05002682 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002683 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002684
John Reck9c35b9c2012-05-30 10:08:50 -07002685 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002686 public boolean onMenuItemClick(MenuItem item) {
2687 if (mWebView != null) {
2688 return mWebView.selectText();
2689 }
2690 return false;
2691 }
2692
2693 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002694 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002695 }
2696
2697 }
2698
Michael Kolb8233fac2010-10-26 16:08:53 -07002699 /********************** TODO: UI stuff *****************************/
2700
2701 // these methods have been copied, they still need to be cleaned up
2702
2703 /****************** tabs ***************************************************/
2704
2705 // basic tab interactions:
2706
2707 // it is assumed that tabcontrol already knows about the tab
2708 protected void addTab(Tab tab) {
2709 mUi.addTab(tab);
2710 }
2711
2712 protected void removeTab(Tab tab) {
2713 mUi.removeTab(tab);
2714 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002715 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002716 }
2717
Michael Kolbf2055602011-04-09 17:20:03 -07002718 @Override
2719 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002720 // monkey protection against delayed start
2721 if (tab != null) {
2722 mTabControl.setCurrentTab(tab);
2723 // the tab is guaranteed to have a webview after setCurrentTab
2724 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002725 tab.setTimeStamp();
Michael Kolbcd424e92011-02-24 10:26:26 -08002726 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002727 }
2728
John Reck8bcafc12011-08-29 16:43:02 -07002729 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 Tab current = mTabControl.getCurrentTab();
2731 if (current != null
2732 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002733 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002734 }
2735 }
2736
John Reck26b18322011-06-21 13:08:58 -07002737 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002738 // Dismiss the subwindow if applicable.
2739 dismissSubWindow(appTab);
2740 // Since we might kill the WebView, remove it from the
2741 // content view first.
2742 mUi.detachTab(appTab);
2743 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002744 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002745 // TODO: analyze why the remove and add are necessary
2746 mUi.attachTab(appTab);
2747 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002748 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002749 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002750 } else {
2751 // If the tab was the current tab, we have to attach
2752 // it to the view system again.
2753 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002754 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002755 }
2756 }
2757
2758 // Remove the sub window if it exists. Also called by TabControl when the
2759 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002760 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002761 public void dismissSubWindow(Tab tab) {
2762 removeSubWindow(tab);
2763 // dismiss the subwindow. This will destroy the WebView.
2764 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002765 WebView wv = getCurrentTopWebView();
2766 if (wv != null) {
2767 wv.requestFocus();
2768 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002769 }
2770
2771 @Override
2772 public void removeSubWindow(Tab t) {
2773 if (t.getSubWebView() != null) {
2774 mUi.removeSubWindow(t.getSubViewContainer());
2775 }
2776 }
2777
2778 @Override
2779 public void attachSubWindow(Tab tab) {
2780 if (tab.getSubWebView() != null) {
2781 mUi.attachSubWindow(tab.getSubViewContainer());
2782 getCurrentTopWebView().requestFocus();
2783 }
2784 }
2785
Mathew Inwood29721c22011-06-29 17:55:24 +01002786 private Tab showPreloadedTab(final UrlData urlData) {
2787 if (!urlData.isPreloaded()) {
2788 return null;
2789 }
2790 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2791 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2792 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002793 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002794 // Could not submit query. Fallback to regular tab creation
2795 tabControl.destroy();
2796 return null;
2797 }
2798 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002799 // check tab count and make room for new tab
2800 if (!mTabControl.canCreateNewTab()) {
2801 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2802 if (leastUsed != null) {
2803 closeTab(leastUsed);
2804 }
2805 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002806 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002807 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002808 mTabControl.addPreloadedTab(t);
2809 addTab(t);
2810 setActiveTab(t);
2811 return t;
2812 }
2813
Michael Kolb7bcafde2011-05-09 13:55:59 -07002814 // open a non inconito tab with the given url data
2815 // and set as active tab
2816 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002817 Tab tab = showPreloadedTab(urlData);
2818 if (tab == null) {
2819 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002820 if ((tab != null) && !urlData.isEmpty()) {
2821 loadUrlDataIn(tab, urlData);
2822 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002823 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002824 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002825 }
2826
Michael Kolb843510f2010-12-09 10:51:49 -08002827 @Override
2828 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002829 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002830 }
2831
Michael Kolb8233fac2010-10-26 16:08:53 -07002832 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002833 public Tab openIncognitoTab() {
2834 return openTab(INCOGNITO_URI, true, true, false);
2835 }
2836
2837 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002838 public Tab openTab(String url, boolean incognito, boolean setActive,
2839 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002840 return openTab(url, incognito, setActive, useCurrent, null);
2841 }
2842
2843 @Override
2844 public Tab openTab(String url, Tab parent, boolean setActive,
2845 boolean useCurrent) {
2846 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2847 setActive, useCurrent, parent);
2848 }
2849
2850 public Tab openTab(String url, boolean incognito, boolean setActive,
2851 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002852 Tab tab = createNewTab(incognito, setActive, useCurrent);
2853 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002854 if (parent != null && parent != tab) {
2855 parent.addChildTab(tab);
2856 }
Michael Kolb519d2282011-05-09 17:03:19 -07002857 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002858 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002859 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002860 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002861 return tab;
2862 }
2863
2864 // this method will attempt to create a new tab
2865 // incognito: private browsing tab
2866 // setActive: ste tab as current tab
2867 // useCurrent: if no new tab can be created, return current tab
2868 private Tab createNewTab(boolean incognito, boolean setActive,
2869 boolean useCurrent) {
2870 Tab tab = null;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002871 MemoryMonitor memMonitor = null;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002872 if (mTabControl.canCreateNewTab()) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002873 if (mSettings.enableMemoryMonitor()) {
2874 Log.d(LOGTAG, " Memory Monitor Enabled .");
2875 memMonitor = MemoryMonitor.getInstance(mActivity.getApplicationContext(),this);
2876 if (memMonitor != null) {
2877 //Remove webview associated with the oldest tab
2878 memMonitor.destroyLeastRecentlyActiveTab();
2879 }
2880 } else {
2881 Log.d(LOGTAG, " Memory Monitor disabled .");
2882 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002883 tab = mTabControl.createNewTab(incognito);
2884 addTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002885 tab.setTimeStamp();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002886 if (setActive) {
2887 setActiveTab(tab);
2888 }
2889 } else {
2890 if (useCurrent) {
2891 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002892 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002893 } else {
2894 mUi.showMaxTabsWarning();
2895 }
2896 }
2897 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002898 }
2899
John Reck2bc80422011-06-30 15:11:49 -07002900 @Override
2901 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2902 SnapshotTab tab = null;
2903 if (mTabControl.canCreateNewTab()) {
2904 tab = mTabControl.createSnapshotTab(snapshotId);
2905 addTab(tab);
2906 if (setActive) {
2907 setActiveTab(tab);
2908 }
2909 } else {
2910 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002911 }
2912 return tab;
2913 }
2914
Michael Kolb8233fac2010-10-26 16:08:53 -07002915 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002916 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002917 * @return boolean True if we successfully switched to a different tab. If
2918 * the indexth tab is null, or if that tab is the same as
2919 * the current one, return false.
2920 */
2921 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002922 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002923 Tab currentTab = mTabControl.getCurrentTab();
2924 if (tab == null || tab == currentTab) {
2925 return false;
2926 }
2927 setActiveTab(tab);
2928 return true;
2929 }
2930
2931 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002932 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002933 closeCurrentTab(false);
2934 }
2935
2936 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002937 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002938 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002939 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002940 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002941 return;
2942 }
Michael Kolbc831b632011-05-11 09:30:34 -07002943 final Tab current = mTabControl.getCurrentTab();
2944 final int pos = mTabControl.getCurrentPosition();
2945 Tab newTab = current.getParent();
2946 if (newTab == null) {
2947 newTab = mTabControl.getTab(pos + 1);
2948 if (newTab == null) {
2949 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002950 }
2951 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002952 if (andQuit) {
2953 mTabControl.setCurrentTab(newTab);
2954 closeTab(current);
2955 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002956 // Close window
2957 closeTab(current);
2958 }
2959 }
2960
2961 /**
2962 * Close the tab, remove its associated title bar, and adjust mTabControl's
2963 * current tab to a valid value.
2964 */
2965 @Override
2966 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002967 if (tab == mTabControl.getCurrentTab()) {
2968 closeCurrentTab();
2969 } else {
2970 removeTab(tab);
2971 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002972 }
2973
Afzal Najamd4e33312012-04-26 01:54:01 -04002974 /**
2975 * Close all tabs except the current one
2976 */
2977 @Override
2978 public void closeOtherTabs() {
2979 int inactiveTabs = mTabControl.getTabCount() - 1;
2980 for (int i = inactiveTabs; i >= 0; i--) {
2981 Tab tab = mTabControl.getTab(i);
2982 if (tab != mTabControl.getCurrentTab()) {
2983 removeTab(tab);
2984 }
2985 }
2986 }
2987
Michael Kolb8233fac2010-10-26 16:08:53 -07002988 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002989 protected void loadUrlFromContext(String url) {
2990 Tab tab = getCurrentTab();
2991 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002992 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002993 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002994 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002995 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002996 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002997 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 }
2999 }
3000 }
3001
3002 /**
3003 * Load the URL into the given WebView and update the title bar
3004 * to reflect the new load. Call this instead of WebView.loadUrl
3005 * directly.
3006 * @param view The WebView used to load url.
3007 * @param url The URL to load.
3008 */
John Reck71e51422011-07-01 16:49:28 -07003009 @Override
3010 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003011 loadUrl(tab, url, null);
3012 }
3013
3014 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3015 if (tab != null) {
3016 dismissSubWindow(tab);
3017 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003018 if (tab.hasCrashed) {
3019 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3020 }
Michael Kolba53c9892011-10-05 13:31:40 -07003021 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003022 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003023 }
3024
3025 /**
3026 * Load UrlData into a Tab and update the title bar to reflect the new
3027 * load. Call this instead of UrlData.loadIn directly.
3028 * @param t The Tab used to load.
3029 * @param data The UrlData being loaded.
3030 */
3031 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003032 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003033 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003034 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003035 } else {
John Reck38b39652012-06-05 09:22:59 -07003036 if (t != null && data.mDisableUrlOverride) {
3037 t.disableUrlOverridingForLoad();
3038 }
John Reck26b18322011-06-21 13:08:58 -07003039 loadUrl(t, data.mUrl, data.mHeaders);
3040 }
3041 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003042 }
3043
John Reck30c714c2010-12-16 17:30:34 -08003044 @Override
3045 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003046 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003047 if (tab.canGoBack()) {
3048 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003049 } else {
John Reckef654f12011-07-12 16:42:08 -07003050 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003051 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003052 }
3053
3054 void goBackOnePageOrQuit() {
3055 Tab current = mTabControl.getCurrentTab();
3056 if (current == null) {
3057 /*
3058 * Instead of finishing the activity, simply push this to the back
3059 * of the stack and let ActivityManager to choose the foreground
3060 * activity. As BrowserActivity is singleTask, it will be always the
3061 * root of the task. So we can use either true or false for
3062 * moveTaskToBack().
3063 */
luxiaolb40014b2013-07-19 10:01:43 +08003064 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003065 return;
3066 }
John Reckef654f12011-07-12 16:42:08 -07003067 if (current.canGoBack()) {
3068 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003069 } else {
3070 // Check to see if we are closing a window that was created by
3071 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003072 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003073 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003074 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 // Now we close the other tab
3076 closeTab(current);
3077 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003078 /*
3079 * Instead of finishing the activity, simply push this to the back
3080 * of the stack and let ActivityManager to choose the foreground
3081 * activity. As BrowserActivity is singleTask, it will be always the
3082 * root of the task. So we can use either true or false for
3083 * moveTaskToBack().
3084 */
luxiaolb40014b2013-07-19 10:01:43 +08003085 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003086 }
3087 }
3088 }
3089
3090 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003091 * helper method for key handler
3092 * returns the current tab if it can't advance
3093 */
Michael Kolbc831b632011-05-11 09:30:34 -07003094 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003095 int pos = mTabControl.getCurrentPosition() + 1;
3096 if (pos >= mTabControl.getTabCount()) {
3097 pos = 0;
3098 }
3099 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003100 }
3101
3102 /**
3103 * helper method for key handler
3104 * returns the current tab if it can't advance
3105 */
Michael Kolbc831b632011-05-11 09:30:34 -07003106 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003107 int pos = mTabControl.getCurrentPosition() - 1;
3108 if ( pos < 0) {
3109 pos = mTabControl.getTabCount() - 1;
3110 }
3111 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003112 }
3113
John Reckbcef87f2012-02-03 14:58:34 -08003114 boolean isMenuOrCtrlKey(int keyCode) {
3115 return (KeyEvent.KEYCODE_MENU == keyCode)
3116 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3117 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3118 }
3119
Michael Kolb0035fad2011-03-14 13:25:28 -07003120 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003121 * handle key events in browser
3122 *
3123 * @param keyCode
3124 * @param event
3125 * @return true if handled, false to pass to super
3126 */
John Reck9c35b9c2012-05-30 10:08:50 -07003127 @Override
3128 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003129 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003130 // Even if MENU is already held down, we need to call to super to open
3131 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003132 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003133 mMenuIsDown = true;
3134 return false;
3135 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003136
Cary Clark8ff8c662010-12-29 15:03:05 -05003137 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003138 Tab tab = getCurrentTab();
3139 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003140
Cary Clark160bbb92011-01-10 11:17:07 -05003141 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3142 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003143
Michael Kolb8233fac2010-10-26 16:08:53 -07003144 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003145 case KeyEvent.KEYCODE_TAB:
3146 if (event.isCtrlPressed()) {
3147 if (event.isShiftPressed()) {
3148 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003149 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003150 } else {
3151 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003152 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003153 }
3154 return true;
3155 }
3156 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 case KeyEvent.KEYCODE_SPACE:
3158 // WebView/WebTextView handle the keys in the KeyDown. As
3159 // the Activity's shortcut keys are only handled when WebView
3160 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003161 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003162 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003163 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003164 pageDown();
3165 }
3166 return true;
3167 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003168 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003169 event.startTracking();
3170 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003171 case KeyEvent.KEYCODE_FORWARD:
3172 if (!noModifiers) break;
3173 tab.goForward();
3174 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003175 case KeyEvent.KEYCODE_DPAD_LEFT:
3176 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003177 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003178 return true;
3179 }
3180 break;
3181 case KeyEvent.KEYCODE_DPAD_RIGHT:
3182 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003183 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003184 return true;
3185 }
3186 break;
3187 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003188 if (ctrl) {
3189 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003190 return true;
3191 }
3192 break;
Michael Kolba4183062011-01-16 10:43:21 -08003193// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003194 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003195 if (ctrl ) {
3196 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003197 return true;
3198 }
3199 break;
Michael Kolba4183062011-01-16 10:43:21 -08003200// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003201// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003202// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003203// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003204// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003205// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003206// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003207// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003208// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003209// case KeyEvent.KEYCODE_M: // unused
3210// case KeyEvent.KEYCODE_N: // in Chrome: new window
3211// case KeyEvent.KEYCODE_O: // in Chrome: open file
3212// case KeyEvent.KEYCODE_P: // in Chrome: print page
3213// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003214// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003215// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3216 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003217 // we can't use the ctrl/shift flags, they check for
3218 // exclusive use of a modifier
3219 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003220 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003221 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003222 } else {
3223 openTabToHomePage();
3224 }
3225 return true;
3226 }
3227 break;
3228// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3229// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003230// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003231// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3232// case KeyEvent.KEYCODE_Y: // unused
3233// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003234 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003235 // it is a regular key and webview is not null
3236 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003237 }
3238
John Reck9c35b9c2012-05-30 10:08:50 -07003239 @Override
3240 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003241 switch(keyCode) {
3242 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003243 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003244 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003245 return true;
3246 }
3247 break;
3248 }
3249 return false;
3250 }
3251
John Reck9c35b9c2012-05-30 10:08:50 -07003252 @Override
3253 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003254 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003255 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003256 if (KeyEvent.KEYCODE_MENU == keyCode
3257 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003258 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003259 }
3260 }
Cary Clark160bbb92011-01-10 11:17:07 -05003261 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003262 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003263 case KeyEvent.KEYCODE_BACK:
3264 if (event.isTracking() && !event.isCanceled()) {
3265 onBackKey();
3266 return true;
3267 }
3268 break;
3269 }
3270 return false;
3271 }
3272
3273 public boolean isMenuDown() {
3274 return mMenuIsDown;
3275 }
3276
John Reck9c35b9c2012-05-30 10:08:50 -07003277 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003278 public void setupAutoFill(Message message) {
3279 // Open the settings activity at the AutoFill profile fragment so that
3280 // the user can create a new profile. When they return, we will dispatch
3281 // the message so that we can autofill the form using their new profile.
3282 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3283 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3284 AutoFillSettingsFragment.class.getName());
3285 mAutoFillSetupMessage = message;
3286 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3287 }
John Reckb3417f02011-01-14 11:01:05 -08003288
John Reck9c35b9c2012-05-30 10:08:50 -07003289 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003290 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003291 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003292 return true;
3293 }
3294
John Reck1cf4b792011-07-26 10:22:22 -07003295 @Override
3296 public boolean shouldCaptureThumbnails() {
3297 return mUi.shouldCaptureThumbnails();
3298 }
3299
Michael Kolbc3af0672011-08-09 10:24:41 -07003300 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003301 public boolean supportsVoice() {
3302 PackageManager pm = mActivity.getPackageManager();
3303 List activities = pm.queryIntentActivities(new Intent(
3304 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3305 return activities.size() != 0;
3306 }
3307
3308 @Override
3309 public void startVoiceRecognizer() {
3310 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003311 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003312 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3313 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3314 mActivity.startActivityForResult(voice, VOICE_RESULT);
3315 }
3316
3317 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003318 public void setBlockEvents(boolean block) {
3319 mBlockEvents = block;
3320 }
3321
John Reck9c35b9c2012-05-30 10:08:50 -07003322 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003323 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003324 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003325 }
3326
John Reck9c35b9c2012-05-30 10:08:50 -07003327 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003328 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003329 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003330 }
3331
John Reck9c35b9c2012-05-30 10:08:50 -07003332 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003333 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003334 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003335 }
3336
John Reck9c35b9c2012-05-30 10:08:50 -07003337 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003338 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003339 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003340 }
3341
John Reck9c35b9c2012-05-30 10:08:50 -07003342 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003343 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003344 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003345 }
3346
Michael Kolb8233fac2010-10-26 16:08:53 -07003347}