blob: 4d96cf541b552fe951332c1e42e6d764d30025d5 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080020
luxiaolb40014b2013-07-19 10:01:43 +080021import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.ProgressDialog;
Vivek Sekharce2a4832014-03-26 13:26:53 -070025import android.content.Context;
26import android.content.res.Resources;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070029import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070030import android.content.ContentValues;
31import android.content.Context;
John Reck68234a92012-04-19 15:27:12 -070032import android.content.DialogInterface;
33import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.content.Intent;
35import android.content.pm.PackageManager;
36import android.content.pm.ResolveInfo;
37import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070038import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050039import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070040import android.database.Cursor;
41import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020042import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080044import android.graphics.BitmapFactory;
Michael Kolb8233fac2010-10-26 16:08:53 -070045import android.graphics.Canvas;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080046import android.graphics.Paint;
47import android.graphics.Rect;
kaiyiza016da12013-08-26 17:50:22 +080048import android.net.ConnectivityManager;
49import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.net.Uri;
51import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080052import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080053import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.os.AsyncTask;
55import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070056import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.os.Handler;
58import android.os.Message;
59import android.os.PowerManager;
60import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000061import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.provider.Browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070063import android.provider.ContactsContract;
64import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080065import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070066import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070067import android.text.TextUtils;
68import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080069import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070070import android.view.ActionMode;
71import android.view.ContextMenu;
72import android.view.ContextMenu.ContextMenuInfo;
73import android.view.Gravity;
74import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070075import android.view.Menu;
76import android.view.MenuInflater;
77import android.view.MenuItem;
78import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070079import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070080import android.view.View;
George Mount387d45d2011-10-07 15:57:53 -070081import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070082import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080083import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb8233fac2010-10-26 16:08:53 -070084import android.webkit.WebIconDatabase;
Leon Scrogginsac993842011-02-02 12:54:07 -050085import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070086
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.CookieManager;
88import org.codeaurora.swe.CookieSyncManager;
89import org.codeaurora.swe.HttpAuthHandler;
90import org.codeaurora.swe.SslErrorHandler;
91import org.codeaurora.swe.WebSettings;
92import org.codeaurora.swe.WebView;
93
Bijan Amirzada41242f22014-03-21 12:12:18 -070094import com.android.browser.R;
95import com.android.browser.IntentHandler.UrlData;
96import com.android.browser.UI.ComboViews;
97import com.android.browser.mynavigation.AddMyNavigationPage;
98import com.android.browser.mynavigation.MyNavigationUtil;
99import com.android.browser.platformsupport.BrowserContract;
100import com.android.browser.platformsupport.WebAddress;
101import com.android.browser.platformsupport.BrowserContract.Images;
102import com.android.browser.provider.BrowserProvider2.Thumbnails;
103import com.android.browser.provider.SnapshotProvider.Snapshots;
104import com.android.browser.reflect.ReflectHelper;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700107import java.io.File;
108import java.io.FileOutputStream;
109import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700110import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700111import java.text.DateFormat;
112import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700113import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700115import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700116import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800117import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200118import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700119import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700120
121/**
122 * Controller for browser
123 */
124public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700125 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700126
127 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800128 private static final String SEND_APP_ID_EXTRA =
129 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700130 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800131
132
kaiyiza016da12013-08-26 17:50:22 +0800133 private static final String PROP_NETSWITCH = "persist.env.browser.netswitch";
134 private static final String INTENT_WIFI_SELECTION_DATA_CONNECTION =
135 "android.net.wifi.cmcc.WIFI_SELECTION_DATA_CONNECTION";
kaiyiz6e5b3e02013-08-19 20:02:01 +0800136 private static final String OFFLINE_PAGE =
Bijan Amirzada41242f22014-03-21 12:12:18 -0700137 "content://com.android.browser.mynavigation/websites";
kaiyizb7bf1f22013-10-02 11:42:23 +0800138 private static final String INTENT_PICK_NETWORK =
139 "android.net.wifi.cmcc.PICK_WIFI_NETWORK_AND_GPRS";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800140
141 public final static String EXTRA_SHARE_SCREENSHOT = "share_screenshot";
142 public final static String EXTRA_SHARE_FAVICON = "share_favicon";
Michael Kolb8233fac2010-10-26 16:08:53 -0700143 // public message ids
144 public final static int LOAD_URL = 1001;
145 public final static int STOP_LOAD = 1002;
146
147 // Message Ids
148 private static final int FOCUS_NODE_HREF = 102;
149 private static final int RELEASE_WAKELOCK = 107;
150
151 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
152
153 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800154 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700155
156 private static final int EMPTY_MENU = -1;
157
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700159 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 final static int PREFERENCES_PAGE = 3;
161 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000162 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700163 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800164 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000165
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
167
168 // As the ids are dynamically created, we can't guarantee that they will
169 // be in sequence, so this static array maps ids to a window number.
170 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
171 { R.id.window_one_menu_id, R.id.window_two_menu_id,
172 R.id.window_three_menu_id, R.id.window_four_menu_id,
173 R.id.window_five_menu_id, R.id.window_six_menu_id,
174 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
175
176 // "source" parameter for Google search through search key
177 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
178 // "source" parameter for Google search through simplily type
179 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
180
George Mount387d45d2011-10-07 15:57:53 -0700181 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700182 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
183
John Reckd7dd9b22011-08-30 09:18:29 -0700184 // A bitmap that is re-used in createScreenshot as scratch space
185 private static Bitmap sThumbnailBitmap;
186
Michael Kolb8233fac2010-10-26 16:08:53 -0700187 private Activity mActivity;
188 private UI mUi;
189 private TabControl mTabControl;
190 private BrowserSettings mSettings;
191 private WebViewFactory mFactory;
192
193 private WakeLock mWakeLock;
194
195 private UrlHandler mUrlHandler;
196 private UploadHandler mUploadHandler;
197 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700198 private PageDialogsHandler mPageDialogsHandler;
199 private NetworkStateHandler mNetworkHandler;
200
Ben Murdoch8029a772010-11-16 11:58:21 +0000201 private Message mAutoFillSetupMessage;
202
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800204 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700205
206 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
207
208 // FIXME, temp address onPrepareMenu performance problem.
209 // When we move everything out of view, we should rewrite this.
210 private int mCurrentMenuState = 0;
211 private int mMenuState = R.id.MAIN_MENU;
212 private int mOldMenuState = EMPTY_MENU;
213 private Menu mCachedMenu;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 private boolean mMenuIsDown;
216
217 // For select and find, we keep track of the ActionMode so that
218 // finish() can be called as desired.
219 private ActionMode mActionMode;
220
221 /**
222 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
223 * of whether the configuration has changed. The first onMenuOpened call
224 * after a configuration change is simply a reopening of the same menu
225 * (i.e. mIconView did not change).
226 */
227 private boolean mConfigChanged;
228
229 /**
230 * Keeps track of whether the options menu is open. This is important in
231 * determining whether to show or hide the title bar overlay
232 */
233 private boolean mOptionsMenuOpen;
234
235 /**
236 * Whether or not the options menu is in its bigger, popup menu form. When
237 * true, we want the title bar overlay to be gone. When false, we do not.
238 * Only meaningful if mOptionsMenuOpen is true.
239 */
240 private boolean mExtendedMenuOpen;
241
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 private boolean mActivityPaused = true;
243 private boolean mLoadStopped;
244
245 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500246 // Checks to see when the bookmarks database has changed, and updates the
247 // Tabs' notion of whether they represent bookmarked sites.
248 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700249 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700250
Michael Kolbc3af0672011-08-09 10:24:41 -0700251 private boolean mBlockEvents;
252
Michael Kolb0b129122012-06-04 16:31:58 -0700253 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800254 private boolean mUpdateMyNavThumbnail;
255 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700256
George Mount3636d0a2011-11-21 09:08:21 -0800257 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 mActivity = browser;
259 mSettings = BrowserSettings.getInstance();
260 mTabControl = new TabControl(this);
261 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700262 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800263 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700264 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700265
266 mUrlHandler = new UrlHandler(this);
267 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
269
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800270 // Creating dummy Webview for browser to force loading of library;
271 // in order for CookieManager calls to be invoked properly and
272 // awBrowserContext to be initialized
273 (mFactory.createWebView(false)).destroy();
274
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500276 mBookmarksObserver = new ContentObserver(mHandler) {
277 @Override
278 public void onChange(boolean selfChange) {
279 int size = mTabControl.getTabCount();
280 for (int i = 0; i < size; i++) {
281 mTabControl.getTab(i).updateBookmarkedStatus();
282 }
283 }
284
285 };
286 browser.getContentResolver().registerContentObserver(
287 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700288
289 mNetworkHandler = new NetworkStateHandler(mActivity, this);
290 // Start watching the default geolocation permissions
291 mSystemAllowGeolocationOrigins =
292 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
293 mSystemAllowGeolocationOrigins.start();
294
John Reckaf262e72011-07-25 13:55:44 -0700295 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700296 }
297
John Reck9c35b9c2012-05-30 10:08:50 -0700298 @Override
299 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800300 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800301 // mCrashRecoverHandler has any previously saved state.
302 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700303 }
304
George Mount3636d0a2011-11-21 09:08:21 -0800305 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700306 // Unless the last browser usage was within 24 hours, destroy any
307 // remaining incognito tabs.
308
309 Calendar lastActiveDate = icicle != null ?
310 (Calendar) icicle.getSerializable("lastActiveDate") : null;
311 Calendar today = Calendar.getInstance();
312 Calendar yesterday = Calendar.getInstance();
313 yesterday.add(Calendar.DATE, -1);
314
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800315 // we dont want to ever recover incognito tabs
316 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700317
Patrick Scott7d50a932011-02-04 09:27:26 -0500318 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700319 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500320 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000321
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800322 mSettings.initializeCookieSettings();
Michael Kolbc831b632011-05-11 09:30:34 -0700323 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500324 // Not able to restore so we go ahead and clear session cookies. We
325 // must do this before trying to login the user as we don't want to
326 // clear any session cookies set during login.
327 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700328 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800329 if (intent == null) {
330 // This won't happen under common scenarios. The icicle is
331 // not null, but there aren't any tabs to restore.
332 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700333 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800334 final Bundle extra = intent.getExtras();
335 // Create an initial tab.
336 // If the intent is ACTION_VIEW and data is not null, the Browser is
337 // invoked to view the content by another application. In this case,
338 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800339 UrlData urlData = null;
340 if (intent.getData() != null
341 && Intent.ACTION_VIEW.equals(intent.getAction())
342 && intent.getData().toString().startsWith("content://")) {
343 urlData = new UrlData(intent.getData().toString());
344 } else {
345 urlData = IntentHandler.getUrlDataFromIntent(intent);
346 }
George Mount3636d0a2011-11-21 09:08:21 -0800347 Tab t = null;
348 if (urlData.isEmpty()) {
Vivek Sekharce2a4832014-03-26 13:26:53 -0700349 Resources res = mActivity.getResources();
350 String browserRes = res.getString(R.string.config_carrier_resource);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800351 if (browserRes.equals(
kaiyiz6e5b3e02013-08-19 20:02:01 +0800352 "cmcc")) {
353 t = openTab(OFFLINE_PAGE, false, true, true);
354 } else {
355 t = openTabToHomePage();
356 }
George Mount3636d0a2011-11-21 09:08:21 -0800357 } else {
358 t = openTab(urlData);
359 }
360 if (t != null) {
361 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
362 }
363 WebView webView = t.getWebView();
364 if (extra != null) {
365 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
366 if (scale > 0 && scale <= 1000) {
367 webView.setInitialScale(scale);
368 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700369 }
370 }
John Reckd8c74522011-06-14 08:45:00 -0700371 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700372 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700373 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500374 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700375 List<Tab> tabs = mTabControl.getTabs();
376 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
377 for (Tab t : tabs) {
378 restoredTabs.add(t.getId());
379 }
380 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700381 if (tabs.size() == 0) {
382 openTabToHomePage();
383 }
John Reck1cf4b792011-07-26 10:22:22 -0700384 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700385 // TabControl.restoreState() will create a new tab even if
386 // restoring the state fails.
387 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800388 // Intent is non-null when framework thinks the browser should be
389 // launching with a new intent (icicle is null).
390 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700391 mIntentHandler.onNewIntent(intent);
392 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700394 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700395 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800396 if (jsFlags.trim().length() != 0) {
397 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700398 }
George Mount3636d0a2011-11-21 09:08:21 -0800399 if (intent != null
400 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700401 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800402 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700403 }
404
John Reck1cf4b792011-07-26 10:22:22 -0700405 private static class PruneThumbnails implements Runnable {
406 private Context mContext;
407 private List<Long> mIds;
408
409 PruneThumbnails(Context context, List<Long> preserveIds) {
410 mContext = context.getApplicationContext();
411 mIds = preserveIds;
412 }
413
414 @Override
415 public void run() {
416 ContentResolver cr = mContext.getContentResolver();
417 if (mIds == null || mIds.size() == 0) {
418 cr.delete(Thumbnails.CONTENT_URI, null, null);
419 } else {
420 int length = mIds.size();
421 StringBuilder where = new StringBuilder();
422 where.append(Thumbnails._ID);
423 where.append(" not in (");
424 for (int i = 0; i < length; i++) {
425 where.append(mIds.get(i));
426 if (i < (length - 1)) {
427 where.append(",");
428 }
429 }
430 where.append(")");
431 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
432 }
433 }
434
435 }
436
Michael Kolb1514bb72010-11-22 09:11:48 -0800437 @Override
438 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700439 return mFactory;
440 }
441
442 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800443 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800444 if (tab.hasCrashed)
445 tab.showCrashView();
446 else
447 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800448 }
449
450 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800451 public void createSubWindow(Tab tab) {
452 endActionMode();
453 WebView mainView = tab.getWebView();
454 WebView subView = mFactory.createWebView((mainView == null)
455 ? false
456 : mainView.isPrivateBrowsingEnabled());
457 mUi.createSubWindow(tab, subView);
458 }
459
460 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700461 public Context getContext() {
462 return mActivity;
463 }
464
465 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700466 public Activity getActivity() {
467 return mActivity;
468 }
469
470 void setUi(UI ui) {
471 mUi = ui;
472 }
473
Michael Kolbaf63dba2012-05-16 12:58:05 -0700474 @Override
475 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700476 return mSettings;
477 }
478
479 IntentHandler getIntentHandler() {
480 return mIntentHandler;
481 }
482
483 @Override
484 public UI getUi() {
485 return mUi;
486 }
487
488 int getMaxTabs() {
489 return mActivity.getResources().getInteger(R.integer.max_tabs);
490 }
491
492 @Override
493 public TabControl getTabControl() {
494 return mTabControl;
495 }
496
Michael Kolb1bf23132010-11-19 12:55:12 -0800497 @Override
498 public List<Tab> getTabs() {
499 return mTabControl.getTabs();
500 }
501
John Reckaf262e72011-07-25 13:55:44 -0700502 // Open the icon database.
503 private void openIconDatabase() {
504 // We have to call getInstance on the UI thread
505 final WebIconDatabase instance = WebIconDatabase.getInstance();
506 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000507
John Reckaf262e72011-07-25 13:55:44 -0700508 @Override
509 public void run() {
510 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 }
John Reckaf262e72011-07-25 13:55:44 -0700512 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700513 }
514
515 private void startHandler() {
516 mHandler = new Handler() {
517
518 @Override
519 public void handleMessage(Message msg) {
520 switch (msg.what) {
521 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700522 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 break;
524 case FOCUS_NODE_HREF:
525 {
526 String url = (String) msg.getData().get("url");
527 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500528 String src = (String) msg.getData().get("src");
529 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 if (TextUtils.isEmpty(url)) {
531 break;
532 }
533 HashMap focusNodeMap = (HashMap) msg.obj;
534 WebView view = (WebView) focusNodeMap.get("webview");
535 // Only apply the action if the top window did not change.
536 if (getCurrentTopWebView() != view) {
537 break;
538 }
539 switch (msg.arg1) {
540 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700541 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500543 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700544 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500545 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500546 case R.id.open_newtab_context_menu_id:
547 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700548 openTab(url, parent,
549 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500550 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700551 case R.id.copy_link_context_menu_id:
552 copy(url);
553 break;
554 case R.id.save_link_context_menu_id:
555 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500556 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800557 mActivity, url, view.getSettings().getUserAgentString(),
558 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700559 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700560 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700561 if(title == null || title == "")
562 title = url;
563
564 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
565 //SWE TODO: No thumbnail support for the url obtained via
566 //browser context menu as its not loaded in webview.
567 if (bookmarkIntent != null) {
568 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
569 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
570 mActivity.startActivity(bookmarkIntent);
571 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700572 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700573 }
574 break;
575 }
576
577 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700578 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 break;
580
581 case STOP_LOAD:
582 stopLoading();
583 break;
584
585 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700586 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700587 mWakeLock.release();
588 // if we reach here, Browser should be still in the
589 // background loading after WAKELOCK_TIMEOUT (5-min).
590 // To avoid burning the battery, stop loading.
591 mTabControl.stopAllLoading();
592 }
593 break;
594
595 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800596 Tab tab = (Tab) msg.obj;
597 if (tab != null) {
598 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 }
600 break;
kaiyiz591110b2013-08-06 17:11:06 +0800601 case OPEN_MENU:
602 if (!mOptionsMenuOpen && mActivity != null ) {
603 mActivity.openOptionsMenu();
604 }
605 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700606 }
607 }
608 };
609
610 }
611
John Reckef654f12011-07-12 16:42:08 -0700612 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200613 public Tab getCurrentTab() {
614 return mTabControl.getCurrentTab();
615 }
616
Michael Kolbba99c5d2010-11-29 14:57:41 -0800617 @Override
618 public void shareCurrentPage() {
619 shareCurrentPage(mTabControl.getCurrentTab());
620 }
621
622 private void shareCurrentPage(Tab tab) {
623 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800624 sharePage(mActivity, tab.getTitle(),
625 tab.getUrl(), tab.getFavicon(),
626 createScreenshot(tab.getWebView(),
627 getDesiredThumbnailWidth(mActivity),
628 getDesiredThumbnailHeight(mActivity)));
629 }
630 }
631
Michael Kolb8233fac2010-10-26 16:08:53 -0700632 /**
633 * Share a page, providing the title, url, favicon, and a screenshot. Uses
634 * an {@link Intent} to launch the Activity chooser.
635 * @param c Context used to launch a new Activity.
636 * @param title Title of the page. Stored in the Intent with
637 * {@link Intent#EXTRA_SUBJECT}
638 * @param url URL of the page. Stored in the Intent with
639 * {@link Intent#EXTRA_TEXT}
640 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
641 * with {@link Browser#EXTRA_SHARE_FAVICON}
642 * @param screenshot Bitmap of a screenshot of the page. Stored in the
643 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
644 */
645 static final void sharePage(Context c, String title, String url,
646 Bitmap favicon, Bitmap screenshot) {
647 Intent send = new Intent(Intent.ACTION_SEND);
648 send.setType("text/plain");
649 send.putExtra(Intent.EXTRA_TEXT, url);
650 send.putExtra(Intent.EXTRA_SUBJECT, title);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800651 send.putExtra(EXTRA_SHARE_FAVICON, favicon);
652 send.putExtra(EXTRA_SHARE_SCREENSHOT, screenshot);
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 try {
654 c.startActivity(Intent.createChooser(send, c.getString(
655 R.string.choosertitle_sharevia)));
656 } catch(android.content.ActivityNotFoundException ex) {
657 // if no app handles it, do nothing
658 }
659 }
660
661 private void copy(CharSequence text) {
662 ClipboardManager cm = (ClipboardManager) mActivity
663 .getSystemService(Context.CLIPBOARD_SERVICE);
664 cm.setText(text);
665 }
666
667 // lifecycle
668
John Reck9c35b9c2012-05-30 10:08:50 -0700669 @Override
670 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700671 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800672 // update the menu in case of a locale change
673 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800674 if (mOptionsMenuOpen) {
675 mActivity.closeOptionsMenu();
676 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
677 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 if (mPageDialogsHandler != null) {
679 mPageDialogsHandler.onConfigurationChanged(config);
680 }
681 mUi.onConfigurationChanged(config);
682 }
683
684 @Override
685 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700686 if (!mUi.isWebShowing()) {
687 mUi.showWeb(false);
688 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700689 mIntentHandler.onNewIntent(intent);
690 }
691
John Reck9c35b9c2012-05-30 10:08:50 -0700692 @Override
693 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800694 if (mUi.isCustomViewShowing()) {
695 hideCustomView();
696 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 if (mActivityPaused) {
698 Log.e(LOGTAG, "BrowserActivity is already paused.");
699 return;
700 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800702 Tab tab = mTabControl.getCurrentTab();
703 if (tab != null) {
704 tab.pause();
705 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700706 if (mWakeLock == null) {
707 PowerManager pm = (PowerManager) mActivity
708 .getSystemService(Context.POWER_SERVICE);
709 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
710 }
Michael Kolb70976932010-11-30 11:34:01 -0800711 mWakeLock.acquire();
712 mHandler.sendMessageDelayed(mHandler
713 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
714 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 }
716 mUi.onPause();
717 mNetworkHandler.onPause();
718
719 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100720 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700721 if (sThumbnailBitmap != null) {
722 sThumbnailBitmap.recycle();
723 sThumbnailBitmap = null;
724 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 }
726
John Reck9c35b9c2012-05-30 10:08:50 -0700727 @Override
728 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700729 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800730 Bundle saveState = createSaveState();
731
732 // crash recovery manages all save & restore state
733 mCrashRecoveryHandler.writeState(saveState);
734 mSettings.setLastRunPaused(true);
735 }
736
737 /**
738 * Save the current state to outState. Does not write the state to
739 * disk.
740 * @return Bundle containing the current state of all tabs.
741 */
742 /* package */ Bundle createSaveState() {
743 Bundle saveState = new Bundle();
744 mTabControl.saveState(saveState);
745 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700746 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800747 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700748 }
George Mount3636d0a2011-11-21 09:08:21 -0800749 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700750 }
751
John Reck9c35b9c2012-05-30 10:08:50 -0700752 @Override
753 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 if (!mActivityPaused) {
755 Log.e(LOGTAG, "BrowserActivity is already resumed.");
756 return;
757 }
George Mount3636d0a2011-11-21 09:08:21 -0800758 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700759 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800760 Tab current = mTabControl.getCurrentTab();
761 if (current != null) {
762 current.resume();
763 resumeWebViewTimers(current);
764 }
John Reckf57c0292011-07-21 18:15:39 -0700765 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200766
Michael Kolb8233fac2010-10-26 16:08:53 -0700767 mUi.onResume();
768 mNetworkHandler.onResume();
769 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100770 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700771 if (mVoiceResult != null) {
772 mUi.onVoiceResult(mVoiceResult);
773 mVoiceResult = null;
774 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800775 if (current != null && current.hasCrashed) {
776 current.showCrashView();
777 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700778 }
779
John Reckf57c0292011-07-21 18:15:39 -0700780 private void releaseWakeLock() {
781 if (mWakeLock != null && mWakeLock.isHeld()) {
782 mHandler.removeMessages(RELEASE_WAKELOCK);
783 mWakeLock.release();
784 }
785 }
786
Michael Kolb70976932010-11-30 11:34:01 -0800787 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800788 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800789 * @param tab guaranteed non-null
790 */
791 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 boolean inLoad = tab.inPageLoad();
793 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
794 CookieSyncManager.getInstance().startSync();
795 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100796 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700797 }
798 }
799
Michael Kolb70976932010-11-30 11:34:01 -0800800 /**
801 * Pause all WebView timers using the WebView of the given tab
802 * @param tab
803 * @return true if the timers are paused or tab is null
804 */
805 private boolean pauseWebViewTimers(Tab tab) {
806 if (tab == null) {
807 return true;
808 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700809 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100810 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700812 }
Michael Kolb70976932010-11-30 11:34:01 -0800813 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 }
815
John Reck9c35b9c2012-05-30 10:08:50 -0700816 @Override
817 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800818 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
820 mUploadHandler = null;
821 }
822 if (mTabControl == null) return;
823 mUi.onDestroy();
824 // Remove the current tab and sub window
825 Tab t = mTabControl.getCurrentTab();
826 if (t != null) {
827 dismissSubWindow(t);
828 removeTab(t);
829 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500830 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 // Destroy all the tabs
832 mTabControl.destroy();
833 WebIconDatabase.getInstance().close();
834 // Stop watching the default geolocation permissions
835 mSystemAllowGeolocationOrigins.stop();
836 mSystemAllowGeolocationOrigins = null;
837 }
838
839 protected boolean isActivityPaused() {
840 return mActivityPaused;
841 }
842
John Reck9c35b9c2012-05-30 10:08:50 -0700843 @Override
844 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 mTabControl.freeMemory();
846 }
847
848 @Override
849 public boolean shouldShowErrorConsole() {
850 return mShouldShowErrorConsole;
851 }
852
853 protected void setShouldShowErrorConsole(boolean show) {
854 if (show == mShouldShowErrorConsole) {
855 // Nothing to do.
856 return;
857 }
858 mShouldShowErrorConsole = show;
859 Tab t = mTabControl.getCurrentTab();
860 if (t == null) {
861 // There is no current tab so we cannot toggle the error console
862 return;
863 }
864 mUi.setShouldShowErrorConsole(t, show);
865 }
866
867 @Override
868 public void stopLoading() {
869 mLoadStopped = true;
870 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700872 if (w != null) {
873 w.stopLoading();
874 mUi.onPageStopped(tab);
875 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 }
877
878 boolean didUserStopLoading() {
879 return mLoadStopped;
880 }
881
kaiyiza016da12013-08-26 17:50:22 +0800882 private void handleNetworkNotify(WebView view) {
883 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
884 Context.CONNECTIVITY_SERVICE);
885 WifiManager wifiMgr = (WifiManager) this.getContext()
886 .getSystemService(Context.WIFI_SERVICE);
887 int networkSwitchTypeOK = this.getContext().getResources()
888 .getInteger(R.integer.netswitch_type_remind);
889
890 if (wifiMgr.isWifiEnabled()) {
891 NetworkInfo mNetworkInfo = conMgr.getActiveNetworkInfo();
892 if (mNetworkInfo == null
893 || (mNetworkInfo != null && (mNetworkInfo.getType() !=
894 ConnectivityManager.TYPE_WIFI))) {
kaiyizb7bf1f22013-10-02 11:42:23 +0800895 List<ScanResult> list = wifiMgr.getScanResults();
896 if (list != null && list.size() == 0) {
897 int isReminder = Settings.System.getInt(
898 mActivity.getContentResolver(),
899 this.getContext().getResources()
900 .getString(R.string.network_switch_remind_type),
901 networkSwitchTypeOK);
902 if (isReminder == networkSwitchTypeOK) {
903 Intent intent = new Intent(
904 INTENT_WIFI_SELECTION_DATA_CONNECTION);
905 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
906 this.getContext().startActivity(intent);
907 }
908 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800909 if ((Boolean)ReflectHelper.invokeStaticMethod(
910 "ActivityManagerNative", "isSystemReady", null, null)) {
kaiyizb7bf1f22013-10-02 11:42:23 +0800911 try {
912 Intent intent = new Intent(INTENT_PICK_NETWORK);
913 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
914 this.getContext().startActivity(intent);
915 } catch (Exception e) {
916 String err_msg = this.getContext().getString(
917 R.string.acivity_not_found, INTENT_PICK_NETWORK);
918 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
919 }
920 } else {
921 Log.e(LOGTAG, "System is not ready!");
922 }
kaiyiza016da12013-08-26 17:50:22 +0800923 }
924 mNetworkShouldNotify = false;
925 }
926 } else {
927 if (!mNetworkHandler.isNetworkUp()) {
928 view.setNetworkAvailable(false);
929 Log.v(LOGTAG, "handleNetworkNotify() Wlan is not enabled.");
930 }
931 }
932 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700933 // WebViewController
934
935 @Override
John Reck324d4402011-01-11 16:56:42 -0800936 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700937
938 // We've started to load a new page. If there was a pending message
939 // to save a screenshot then we will now take the new page and save
940 // an incorrect screenshot. Therefore, remove any pending thumbnail
941 // messages from the queue.
942 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800943 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700944
945 // reset sync timer to avoid sync starts during loading a page
946 CookieSyncManager.getInstance().resetSync();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800947 Object[] params = {new String(PROP_NETSWITCH),
948 new Boolean(false)};
949 Class[] type = new Class[] {String.class, boolean.class};
950 Boolean result = (Boolean) ReflectHelper.invokeStaticMethod(
951 "android.os.SystemProperties", "getBoolean",
952 type, params);
953 if (result) {
kaiyiza016da12013-08-26 17:50:22 +0800954 if (!mNetworkHandler.isNetworkUp()) {
955 Log.d(LOGTAG, "onPageStarted() network unavailable");
956 if (mNetworkShouldNotify) {
957 handleNetworkNotify(view);
958 } else {
959 view.setNetworkAvailable(false);
960 }
961 mNetworkShouldNotify = false;
962 } else {
963 Log.d(LOGTAG, "onPageStarted() network available");
964 if (mNetworkShouldNotify) {
965 handleNetworkNotify(view);
966 }
967 mNetworkShouldNotify = false;
968 }
969 } else {
970 if (!mNetworkHandler.isNetworkUp()) {
971 view.setNetworkAvailable(false);
972 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700973 }
974
975 // when BrowserActivity just starts, onPageStarted may be called before
976 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
977 // to start the timer. As we won't switch tabs while an activity is in
978 // pause state, we can ensure calling resume and pause in pair.
979 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800980 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700981 }
982 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 endActionMode();
984
John Reck30c714c2010-12-16 17:30:34 -0800985 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700986
John Reck324d4402011-01-11 16:56:42 -0800987 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700988 // update the bookmark database for favicon
989 maybeUpdateFavicon(tab, null, url, favicon);
990
991 Performance.tracePageStart(url);
992
993 // Performance probe
994 if (false) {
995 Performance.onPageStarted();
996 }
997
998 }
999
1000 @Override
John Reck324d4402011-01-11 16:56:42 -08001001 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -07001002 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -08001003 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001004
Michael Kolb8233fac2010-10-26 16:08:53 -07001005 // Performance probe
1006 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001007 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001008 }
1009
1010 Performance.tracePageFinished();
1011 }
1012
1013 @Override
John Reck30c714c2010-12-16 17:30:34 -08001014 public void onProgressChanged(Tab tab) {
1015 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001016
1017 if (newProgress == 100) {
1018 CookieSyncManager.getInstance().sync();
1019 // onProgressChanged() may continue to be called after the main
1020 // frame has finished loading, as any remaining sub frames continue
1021 // to load. We'll only get called once though with newProgress as
1022 // 100 when everything is loaded. (onPageFinished is called once
1023 // when the main frame completes loading regardless of the state of
1024 // any sub frames so calls to onProgressChanges may continue after
1025 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001026 if (tab.inPageLoad()) {
1027 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +02001028 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
1029 // pause the WebView timer and release the wake lock if it is
1030 // finished while BrowserActivity is in pause state.
1031 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001032 }
John Reckd9862372012-02-21 15:04:50 -08001033 if (!tab.isPrivateBrowsingEnabled()
1034 && !TextUtils.isEmpty(tab.getUrl())
1035 && !tab.isSnapshot()) {
1036 // Only update the bookmark screenshot if the user did not
1037 // cancel the load early and there is not already
1038 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001039 if (tab.shouldUpdateThumbnail() &&
1040 (tab.inForeground() && !didUserStopLoading()
1041 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001042 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1043 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1044 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001045 1500);
John Reckd9862372012-02-21 15:04:50 -08001046 }
1047 }
1048 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001049 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001050 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001051 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001052 // still loading
1053 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001054 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001055 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 }
1057 }
John Reck30c714c2010-12-16 17:30:34 -08001058 mUi.onProgressChanged(tab);
1059 }
1060
1061 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001062 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001063 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 }
1065
1066 @Override
1067 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001068 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001069 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001070 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001071 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1072 return;
1073 }
1074 // Update the title in the history database if not in private browsing mode
1075 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001076 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 }
1078 }
1079
1080 @Override
1081 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001082 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001083 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1084 }
1085
1086 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001087 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1088 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001089 }
1090
1091 @Override
1092 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1093 if (mMenuIsDown) {
1094 // only check shortcut key when MENU is held
1095 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1096 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001098 int keyCode = event.getKeyCode();
1099 // We need to send almost every key to WebKit. However:
1100 // 1. We don't want to block the device on the renderer for
1101 // some keys like menu, home, call.
1102 // 2. There are no WebKit equivalents for some of these keys
1103 // (see app/keyboard_codes_win.h)
1104 // Note that these are not the same set as KeyEvent.isSystemKey:
1105 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1106 if (keyCode == KeyEvent.KEYCODE_MENU ||
1107 keyCode == KeyEvent.KEYCODE_HOME ||
1108 keyCode == KeyEvent.KEYCODE_BACK ||
1109 keyCode == KeyEvent.KEYCODE_CALL ||
1110 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1111 keyCode == KeyEvent.KEYCODE_POWER ||
1112 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1113 keyCode == KeyEvent.KEYCODE_CAMERA ||
1114 keyCode == KeyEvent.KEYCODE_FOCUS ||
1115 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1116 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1117 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1118 return true;
1119 }
1120
1121 // We also have to intercept some shortcuts before we send them to the ContentView.
1122 if (event.isCtrlPressed() && (
1123 keyCode == KeyEvent.KEYCODE_TAB ||
1124 keyCode == KeyEvent.KEYCODE_W ||
1125 keyCode == KeyEvent.KEYCODE_F4)) {
1126 return true;
1127 }
1128
1129 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001130 }
1131
1132 @Override
John Reck997b1b72012-04-19 18:08:25 -07001133 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001134 if (!isActivityPaused()) {
1135 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001136 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 } else {
John Reck997b1b72012-04-19 18:08:25 -07001138 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 }
1140 }
John Reck997b1b72012-04-19 18:08:25 -07001141 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001142 }
1143
1144 @Override
John Reck324d4402011-01-11 16:56:42 -08001145 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 // Don't save anything in private browsing mode
1147 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -08001148 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001149
John Reck324d4402011-01-11 16:56:42 -08001150 if (TextUtils.isEmpty(url)
1151 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 return;
1153 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001154
John Reckf57c0292011-07-21 18:15:39 -07001155 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001156 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 }
1158
1159 @Override
1160 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1161 AsyncTask<Void, Void, String[]> task =
1162 new AsyncTask<Void, Void, String[]>() {
1163 @Override
1164 public String[] doInBackground(Void... unused) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001165 Object[] params = {mActivity.getContentResolver()};
1166 Class[] type = new Class[] {ContentResolver.class};
1167 return (String[])ReflectHelper.invokeStaticMethod(
1168 "android.provider.Browser","getVisitedHistory",
1169 type, params);
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 }
1171 @Override
1172 public void onPostExecute(String[] result) {
1173 callback.onReceiveValue(result);
1174 }
1175 };
1176 task.execute();
1177 }
1178
1179 @Override
1180 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1181 final HttpAuthHandler handler, final String host,
1182 final String realm) {
1183 String username = null;
1184 String password = null;
1185
1186 boolean reuseHttpAuthUsernamePassword
1187 = handler.useHttpAuthUsernamePassword();
1188
1189 if (reuseHttpAuthUsernamePassword && view != null) {
1190 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1191 if (credentials != null && credentials.length == 2) {
1192 username = credentials[0];
1193 password = credentials[1];
1194 }
1195 }
1196
1197 if (username != null && password != null) {
1198 handler.proceed(username, password);
1199 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001200 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001201 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1202 } else {
1203 handler.cancel();
1204 }
1205 }
1206 }
1207
1208 @Override
1209 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001210 String contentDisposition, String mimetype, String referer,
1211 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001212 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001213 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001214 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001215 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001216 // This Tab was opened for the sole purpose of downloading a
1217 // file. Remove it.
1218 if (tab == mTabControl.getCurrentTab()) {
1219 // In this case, the Tab is still on top.
1220 goBackOnePageOrQuit();
1221 } else {
1222 // In this case, it is not.
1223 closeTab(tab);
1224 }
1225 }
1226 }
1227
1228 @Override
1229 public Bitmap getDefaultVideoPoster() {
1230 return mUi.getDefaultVideoPoster();
1231 }
1232
1233 @Override
1234 public View getVideoLoadingProgressView() {
1235 return mUi.getVideoLoadingProgressView();
1236 }
1237
1238 @Override
1239 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1240 SslError error) {
1241 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1242 }
1243
1244 // helper method
1245
1246 /*
1247 * Update the favorites icon if the private browsing isn't enabled and the
1248 * icon is valid.
1249 */
1250 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1251 final String url, Bitmap favicon) {
1252 if (favicon == null) {
1253 return;
1254 }
1255 if (!tab.isPrivateBrowsingEnabled()) {
1256 Bookmarks.updateFavicon(mActivity
1257 .getContentResolver(), originalUrl, url, favicon);
1258 }
1259 }
1260
Leon Scroggins4cd97792010-12-03 15:31:56 -05001261 @Override
1262 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001263 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001264 mUi.bookmarkedStatusHasChanged(tab);
1265 }
1266
Michael Kolb8233fac2010-10-26 16:08:53 -07001267 // end WebViewController
1268
1269 protected void pageUp() {
1270 getCurrentTopWebView().pageUp(false);
1271 }
1272
1273 protected void pageDown() {
1274 getCurrentTopWebView().pageDown(false);
1275 }
1276
1277 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001278 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 public void editUrl() {
1280 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001281 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 }
1283
John Reck9c35b9c2012-05-30 10:08:50 -07001284 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001285 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001286 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001287 if (tab.inForeground()) {
1288 if (mUi.isCustomViewShowing()) {
1289 callback.onCustomViewHidden();
1290 return;
1291 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001292 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 // Save the menu state and set it to empty while the custom
1294 // view is showing.
1295 mOldMenuState = mMenuState;
1296 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001297 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001298 }
1299 }
1300
1301 @Override
1302 public void hideCustomView() {
1303 if (mUi.isCustomViewShowing()) {
1304 mUi.onHideCustomView();
1305 // Reset the old menu state.
1306 mMenuState = mOldMenuState;
1307 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001308 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001309 }
1310 }
1311
John Reck9c35b9c2012-05-30 10:08:50 -07001312 @Override
1313 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001314 Intent intent) {
1315 if (getCurrentTopWebView() == null) return;
1316 switch (requestCode) {
1317 case PREFERENCES_PAGE:
1318 if (resultCode == Activity.RESULT_OK && intent != null) {
1319 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001320 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001321 mTabControl.removeParentChildRelationShips();
1322 }
1323 }
1324 break;
1325 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001326 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001327 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001328 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001329 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001330 case AUTOFILL_SETUP:
1331 // Determine whether a profile was actually set up or not
1332 // and if so, send the message back to the WebTextView to
1333 // fill the form with the new profile.
1334 if (getSettings().getAutoFillProfile() != null) {
1335 mAutoFillSetupMessage.sendToTarget();
1336 mAutoFillSetupMessage = null;
1337 }
1338 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001339 case COMBO_VIEW:
1340 if (intent == null || resultCode != Activity.RESULT_OK) {
1341 break;
1342 }
John Reck3ba45532011-08-11 16:26:53 -07001343 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001344 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1345 Tab t = getCurrentTab();
1346 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001347 mUpdateMyNavThumbnail = true;
1348 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001349 loadUrl(t, uri.toString());
1350 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1351 String[] urls = intent.getStringArrayExtra(
1352 ComboViewActivity.EXTRA_OPEN_ALL);
1353 Tab parent = getCurrentTab();
1354 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001355 if (url != null) {
1356 parent = openTab(url, parent,
1357 !mSettings.openInBackground(), true);
1358 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001359 }
1360 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1361 long id = intent.getLongExtra(
1362 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1363 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001364 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001365 }
1366 }
1367 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001368 case VOICE_RESULT:
1369 if (resultCode == Activity.RESULT_OK && intent != null) {
1370 ArrayList<String> results = intent.getStringArrayListExtra(
1371 RecognizerIntent.EXTRA_RESULTS);
1372 if (results.size() >= 1) {
1373 mVoiceResult = results.get(0);
1374 }
1375 }
1376 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001377 case MY_NAVIGATION:
1378 if (intent == null || resultCode != Activity.RESULT_OK) {
1379 break;
1380 }
1381
1382 if (intent.getBooleanExtra("need_refresh", false) &&
1383 getCurrentTopWebView() != null) {
1384 getCurrentTopWebView().reload();
1385 }
1386 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001387 default:
1388 break;
1389 }
1390 getCurrentTopWebView().requestFocus();
1391 }
1392
1393 /**
1394 * Open the Go page.
1395 * @param startWithHistory If true, open starting on the history tab.
1396 * Otherwise, start with the bookmarks tab.
1397 */
1398 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001399 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 if (mTabControl.getCurrentWebView() == null) {
1401 return;
1402 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001403 // clear action mode
1404 if (isInCustomActionMode()) {
1405 endActionMode();
1406 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 Bundle extras = new Bundle();
1408 // Disable opening in a new window if we have maxed out the windows
1409 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1410 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001411 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001412 }
1413
1414 // combo view callbacks
1415
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 // key handling
1417 protected void onBackKey() {
1418 if (!mUi.onBackKey()) {
1419 WebView subwindow = mTabControl.getCurrentSubWindow();
1420 if (subwindow != null) {
1421 if (subwindow.canGoBack()) {
1422 subwindow.goBack();
1423 } else {
1424 dismissSubWindow(mTabControl.getCurrentTab());
1425 }
1426 } else {
1427 goBackOnePageOrQuit();
1428 }
1429 }
1430 }
1431
Michael Kolb4bd767d2011-05-27 11:33:55 -07001432 protected boolean onMenuKey() {
1433 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001434 }
1435
Michael Kolb8233fac2010-10-26 16:08:53 -07001436 // menu handling and state
1437 // TODO: maybe put into separate handler
1438
John Reck9c35b9c2012-05-30 10:08:50 -07001439 @Override
1440 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001441 if (mMenuState == EMPTY_MENU) {
1442 return false;
1443 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001444 MenuInflater inflater = mActivity.getMenuInflater();
1445 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001446 return true;
1447 }
1448
John Reck9c35b9c2012-05-30 10:08:50 -07001449 @Override
1450 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001452 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 return;
1454 }
1455 if (!(v instanceof WebView)) {
1456 return;
1457 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001458 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001459 WebView.HitTestResult result = webview.getHitTestResult();
1460 if (result == null) {
1461 return;
1462 }
1463
1464 int type = result.getType();
1465 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1466 Log.w(LOGTAG,
1467 "We should not show context menu when nothing is touched");
1468 return;
1469 }
1470 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1471 // let TextView handles context menu
1472 return;
1473 }
1474
1475 // Note, http://b/issue?id=1106666 is requesting that
1476 // an inflated menu can be used again. This is not available
1477 // yet, so inflate each time (yuk!)
1478 MenuInflater inflater = mActivity.getMenuInflater();
1479 inflater.inflate(R.menu.browsercontext, menu);
1480
1481 // Show the correct menu group
1482 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001483 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001484 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001485 menu.setGroupVisible(R.id.PHONE_MENU,
1486 type == WebView.HitTestResult.PHONE_TYPE);
1487 menu.setGroupVisible(R.id.EMAIL_MENU,
1488 type == WebView.HitTestResult.EMAIL_TYPE);
1489 menu.setGroupVisible(R.id.GEO_MENU,
1490 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001491 String itemUrl = null;
1492 String url = webview.getOriginalUrl();
1493 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1494 itemUrl = Uri.decode(navigationUrl);
1495 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1496 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1497 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1498 } else {
1499 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1500 }
1501 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1502 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1503 } else {
1504 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1505
1506 menu.setGroupVisible(R.id.IMAGE_MENU,
1507 type == WebView.HitTestResult.IMAGE_TYPE
1508 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1509 menu.setGroupVisible(R.id.ANCHOR_MENU,
1510 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1511 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1512 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001513 // Setup custom handling depending on the type
1514 switch (type) {
1515 case WebView.HitTestResult.PHONE_TYPE:
1516 menu.setHeaderTitle(Uri.decode(extra));
1517 menu.findItem(R.id.dial_context_menu_id).setIntent(
1518 new Intent(Intent.ACTION_VIEW, Uri
1519 .parse(WebView.SCHEME_TEL + extra)));
1520 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1521 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1522 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1523 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1524 addIntent);
1525 menu.findItem(R.id.copy_phone_context_menu_id)
1526 .setOnMenuItemClickListener(
1527 new Copy(extra));
1528 break;
1529
1530 case WebView.HitTestResult.EMAIL_TYPE:
1531 menu.setHeaderTitle(extra);
1532 menu.findItem(R.id.email_context_menu_id).setIntent(
1533 new Intent(Intent.ACTION_VIEW, Uri
1534 .parse(WebView.SCHEME_MAILTO + extra)));
1535 menu.findItem(R.id.copy_mail_context_menu_id)
1536 .setOnMenuItemClickListener(
1537 new Copy(extra));
1538 break;
1539
1540 case WebView.HitTestResult.GEO_TYPE:
1541 menu.setHeaderTitle(extra);
1542 menu.findItem(R.id.map_context_menu_id).setIntent(
1543 new Intent(Intent.ACTION_VIEW, Uri
1544 .parse(WebView.SCHEME_GEO
1545 + URLEncoder.encode(extra))));
1546 menu.findItem(R.id.copy_geo_context_menu_id)
1547 .setOnMenuItemClickListener(
1548 new Copy(extra));
1549 break;
1550
1551 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1552 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001553 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001554 // decide whether to show the open link in new tab option
1555 boolean showNewTab = mTabControl.canCreateNewTab();
1556 MenuItem newTabItem
1557 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001558 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001559 ? R.string.contextmenu_openlink_newwindow_background
1560 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001561 newTabItem.setVisible(showNewTab);
1562 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001563 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1564 newTabItem.setOnMenuItemClickListener(
1565 new MenuItem.OnMenuItemClickListener() {
1566 @Override
1567 public boolean onMenuItemClick(MenuItem item) {
1568 final HashMap<String, WebView> hrefMap =
1569 new HashMap<String, WebView>();
1570 hrefMap.put("webview", webview);
1571 final Message msg = mHandler.obtainMessage(
1572 FOCUS_NODE_HREF,
1573 R.id.open_newtab_context_menu_id,
1574 0, hrefMap);
1575 webview.requestFocusNodeHref(msg);
1576 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001577 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001578 });
1579 } else {
1580 newTabItem.setOnMenuItemClickListener(
1581 new MenuItem.OnMenuItemClickListener() {
1582 @Override
1583 public boolean onMenuItemClick(MenuItem item) {
1584 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001585 openTab(extra, parent,
1586 !mSettings.openInBackground(),
1587 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001588 return true;
1589 }
1590 });
1591 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001592 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001593 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1594 menu.setHeaderTitle(navigationUrl);
1595 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1596
1597 if (itemUrl != null) {
1598 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1599 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1600 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1601 @Override
1602 public boolean onMenuItemClick(MenuItem item) {
1603 final Intent intent = new Intent(Controller.this
1604 .getContext(),
1605 AddMyNavigationPage.class);
1606 Bundle bundle = new Bundle();
1607 String url = Uri.decode(navigationUrl);
1608 bundle.putBoolean("isAdding", false);
1609 bundle.putString("url", url);
1610 bundle.putString("name", getNameFromUrl(url));
1611 intent.putExtra("websites", bundle);
1612 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1613 return false;
1614 }
1615 });
1616 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1617 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1618 @Override
1619 public boolean onMenuItemClick(MenuItem item) {
1620 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1621 return false;
1622 }
1623 });
1624 }
1625 } else {
1626 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1627 }
1628 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1630 break;
1631 }
1632 // otherwise fall through to handle image part
1633 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001634 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1635 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001636 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1637 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001638 shareItem.setOnMenuItemClickListener(
1639 new MenuItem.OnMenuItemClickListener() {
1640 @Override
1641 public boolean onMenuItemClick(MenuItem item) {
1642 sharePage(mActivity, null, extra, null,
1643 null);
1644 return true;
1645 }
1646 }
1647 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001648 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001649 menu.findItem(R.id.view_image_context_menu_id)
1650 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1651 @Override
1652 public boolean onMenuItemClick(MenuItem item) {
1653 openTab(extra, mTabControl.getCurrentTab(), true, true);
1654 return false;
1655 }
1656 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001657 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1658 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1659 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001660 menu.findItem(R.id.set_wallpaper_context_menu_id).
1661 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1662 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001663 break;
1664
1665 default:
1666 Log.w(LOGTAG, "We should not get here.");
1667 break;
1668 }
1669 //update the ui
1670 mUi.onContextMenuCreated(menu);
1671 }
1672
kaiyiz6e5b3e02013-08-19 20:02:01 +08001673 public void startAddMyNavigation(String url) {
1674 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1675 Bundle bundle = new Bundle();
1676 bundle.putBoolean("isAdding", true);
1677 bundle.putString("url", url);
1678 bundle.putString("name", getNameFromUrl(url));
1679 intent.putExtra("websites", bundle);
1680 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1681 }
1682
1683 private void showMyNavigationDeleteDialog(final String itemUrl) {
1684 new AlertDialog.Builder(this.getContext())
1685 .setTitle(R.string.my_navigation_delete_label)
1686 .setIcon(android.R.drawable.ic_dialog_alert)
1687 .setMessage(R.string.my_navigation_delete_msg)
1688 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1689 @Override
1690 public void onClick(DialogInterface dialog, int whichButton) {
1691 deleteMyNavigationItem(itemUrl);
1692 }
1693 })
1694 .setNegativeButton(R.string.cancel, null)
1695 .show();
1696 }
1697
1698 private void deleteMyNavigationItem(final String itemUrl) {
1699 ContentResolver cr = this.getContext().getContentResolver();
1700 Cursor cursor = null;
1701
1702 try {
1703 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1704 new String[] {
1705 MyNavigationUtil.ID
1706 }, "url = ?", new String[] {
1707 itemUrl
1708 }, null);
1709 if (null != cursor && cursor.moveToFirst()) {
1710 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1711 cursor.getLong(0));
1712
1713 ContentValues values = new ContentValues();
1714 values.put(MyNavigationUtil.TITLE, "");
1715 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1716 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1717 ByteArrayOutputStream os = new ByteArrayOutputStream();
1718 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1719 R.raw.my_navigation_add);
1720 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1721 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1722 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1723 cr.update(uri, values, null, null);
1724 } else {
1725 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1726 }
1727 } catch (IllegalStateException e) {
1728 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1729 } finally {
1730 if (null != cursor) {
1731 cursor.close();
1732 }
1733 }
1734
1735 if (getCurrentTopWebView() != null) {
1736 getCurrentTopWebView().reload();
1737 }
1738 }
1739
1740 private String getNameFromUrl(String itemUrl) {
1741 ContentResolver cr = this.getContext().getContentResolver();
1742 Cursor cursor = null;
1743 String name = null;
1744
1745 try {
1746 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1747 new String[] {
1748 MyNavigationUtil.TITLE
1749 }, "url = ?", new String[] {
1750 itemUrl
1751 }, null);
1752 if (null != cursor && cursor.moveToFirst()) {
1753 name = cursor.getString(0);
1754 } else {
1755 Log.e(LOGTAG, "this item does not exist!");
1756 }
1757 } catch (IllegalStateException e) {
1758 Log.e(LOGTAG, "getNameFromUrl", e);
1759 } finally {
1760 if (null != cursor) {
1761 cursor.close();
1762 }
1763 }
1764 return name;
1765 }
1766
1767 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1768 int width = mActivity.getResources().getDimensionPixelOffset(
1769 R.dimen.myNavigationThumbnailWidth);
1770 int height = mActivity.getResources().getDimensionPixelOffset(
1771 R.dimen.myNavigationThumbnailHeight);
1772
1773 final Bitmap bm = createScreenshot(webView, width, height);
1774
1775 if (bm == null) {
1776 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1777 return;
1778 }
1779
1780 final ContentResolver cr = mActivity.getContentResolver();
1781 new AsyncTask<Void, Void, Void>() {
1782 @Override
1783 protected Void doInBackground(Void... unused) {
1784 ContentResolver cr = mActivity.getContentResolver();
1785 Cursor cursor = null;
1786 try {
1787 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1788 new String[] {
1789 MyNavigationUtil.ID
1790 }, "url = ?", new String[] {
1791 itemUrl
1792 }, null);
1793 if (null != cursor && cursor.moveToFirst()) {
1794 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1795 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1796
1797 ContentValues values = new ContentValues();
1798 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1799 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1800 cursor.getLong(0));
1801 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1802 cr.update(uri, values, null, null);
1803 os.close();
1804 }
1805 } catch (IllegalStateException e) {
1806 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1807 } catch (IOException e) {
1808 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1809 } finally {
1810 if (null != cursor) {
1811 cursor.close();
1812 }
1813 }
1814 return null;
1815 }
1816 }.execute();
1817 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001818 /**
1819 * As the menu can be open when loading state changes
1820 * we must manually update the state of the stop/reload menu
1821 * item
1822 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001823 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001824 if (menu == null) {
1825 return;
1826 }
1827 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001828 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001829 menu.findItem(R.id.stop_menu_id):
1830 menu.findItem(R.id.reload_menu_id);
1831 if (src != null) {
1832 dest.setIcon(src.getIcon());
1833 dest.setTitle(src.getTitle());
1834 }
1835 }
1836
John Reck9c35b9c2012-05-30 10:08:50 -07001837 @Override
1838 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001839 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001840 // hold on to the menu reference here; it is used by the page callbacks
1841 // to update the menu based on loading state
1842 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001843 // Note: setVisible will decide whether an item is visible; while
1844 // setEnabled() will decide whether an item is enabled, which also means
1845 // whether the matching shortcut key will function.
1846 switch (mMenuState) {
1847 case EMPTY_MENU:
1848 if (mCurrentMenuState != mMenuState) {
1849 menu.setGroupVisible(R.id.MAIN_MENU, false);
1850 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1851 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1852 }
1853 break;
1854 default:
1855 if (mCurrentMenuState != mMenuState) {
1856 menu.setGroupVisible(R.id.MAIN_MENU, true);
1857 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1858 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1859 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001860 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001861 break;
1862 }
1863 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001864 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001865 }
1866
Michael Kolb4bf79712011-07-14 14:18:12 -07001867 @Override
1868 public void updateMenuState(Tab tab, Menu menu) {
1869 boolean canGoBack = false;
1870 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001871 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001872 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001873 if (tab != null) {
1874 canGoBack = tab.canGoBack();
1875 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001876 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001877 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001878 }
1879 final MenuItem back = menu.findItem(R.id.back_menu_id);
1880 back.setEnabled(canGoBack);
1881
1882 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001883
1884 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1885 forward.setEnabled(canGoForward);
1886
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001887 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1888 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001889 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001890 if (source != null && dest != null) {
1891 dest.setTitle(source.getTitle());
1892 dest.setIcon(source.getIcon());
1893 }
John Recke1a03a32011-09-14 17:04:16 -07001894 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001895
1896 // decide whether to show the share link option
1897 PackageManager pm = mActivity.getPackageManager();
1898 Intent send = new Intent(Intent.ACTION_SEND);
1899 send.setType("text/plain");
1900 ResolveInfo ri = pm.resolveActivity(send,
1901 PackageManager.MATCH_DEFAULT_ONLY);
1902 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1903
1904 boolean isNavDump = mSettings.enableNavDump();
1905 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1906 nav.setVisible(isNavDump);
1907 nav.setEnabled(isNavDump);
1908
1909 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001910 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1911 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001912 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1913 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001914 // history and snapshots item are the members of COMBO menu group,
1915 // so if show history item, only make snapshots item invisible.
1916 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001917
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001918 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001919 }
1920
John Reck9c35b9c2012-05-30 10:08:50 -07001921 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001922 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001923 if (null == getCurrentTopWebView()) {
1924 return false;
1925 }
1926 if (mMenuIsDown) {
1927 // The shortcut action consumes the MENU. Even if it is still down,
1928 // it won't trigger the next shortcut action. In the case of the
1929 // shortcut action triggering a new activity, like Bookmarks, we
1930 // won't get onKeyUp for MENU. So it is important to reset it here.
1931 mMenuIsDown = false;
1932 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001933 if (mUi.onOptionsItemSelected(item)) {
1934 // ui callback handled it
1935 return true;
1936 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001937 switch (item.getItemId()) {
1938 // -- Main menu
1939 case R.id.new_tab_menu_id:
1940 openTabToHomePage();
1941 break;
1942
1943 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001944 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001945 break;
1946
Afzal Najamd4e33312012-04-26 01:54:01 -04001947 case R.id.close_other_tabs_id:
1948 closeOtherTabs();
1949 break;
1950
Michael Kolb8233fac2010-10-26 16:08:53 -07001951 case R.id.goto_menu_id:
1952 editUrl();
1953 break;
1954
1955 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001956 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1957 break;
1958
1959 case R.id.history_menu_id:
1960 bookmarksOrHistoryPicker(ComboViews.History);
1961 break;
1962
1963 case R.id.snapshots_menu_id:
1964 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001965 break;
1966
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001968 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001969 break;
1970
1971 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001972 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001973 stopLoading();
1974 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001975 Tab currentTab = mTabControl.getCurrentTab();
1976 if (currentTab.hasCrashed) {
1977 currentTab.replaceCrashView(getCurrentTopWebView(),
1978 currentTab.getViewContainer());
1979 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001980 getCurrentTopWebView().reload();
1981 }
1982 break;
1983
1984 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001985 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001986 break;
1987
1988 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001989 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001990 break;
1991
1992 case R.id.close_menu_id:
1993 // Close the subwindow if it exists.
1994 if (mTabControl.getCurrentSubWindow() != null) {
1995 dismissSubWindow(mTabControl.getCurrentTab());
1996 break;
1997 }
1998 closeCurrentTab();
1999 break;
2000
kaiyiza8b6dbb2013-07-29 18:11:22 +08002001 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002002 Object[] params = { new String("persist.debug.browsermonkeytest")};
2003 Class[] type = new Class[] {String.class};
2004 String ret = (String)ReflectHelper.invokeStaticMethod(
2005 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002006 if (ret != null && ret.equals("enable"))
2007 break;
luxiaolb40014b2013-07-19 10:01:43 +08002008 showExitDialog(mActivity);
2009 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002010 case R.id.homepage_menu_id:
2011 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002012 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002013 break;
2014
2015 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002016 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002017 break;
2018
2019 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002020 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002021 break;
2022
John Reck2bc80422011-06-30 15:11:49 -07002023 case R.id.save_snapshot_menu_id:
2024 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002025 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07002026 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05002027 break;
2028
Michael Kolb8233fac2010-10-26 16:08:53 -07002029 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002030 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002031 break;
2032
John Recke1a03a32011-09-14 17:04:16 -07002033 case R.id.snapshot_go_live:
2034 goLive();
2035 return true;
2036
Michael Kolb8233fac2010-10-26 16:08:53 -07002037 case R.id.share_page_menu_id:
2038 Tab currentTab = mTabControl.getCurrentTab();
2039 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002040 return false;
2041 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002042 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002043 break;
2044
2045 case R.id.dump_nav_menu_id:
2046 getCurrentTopWebView().debugDump();
2047 break;
2048
Michael Kolb8233fac2010-10-26 16:08:53 -07002049 case R.id.zoom_in_menu_id:
2050 getCurrentTopWebView().zoomIn();
2051 break;
2052
2053 case R.id.zoom_out_menu_id:
2054 getCurrentTopWebView().zoomOut();
2055 break;
2056
2057 case R.id.view_downloads_menu_id:
2058 viewDownloads();
2059 break;
2060
John Reck42229bc2011-08-19 13:26:43 -07002061 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002062 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002063 break;
2064
Michael Kolb8233fac2010-10-26 16:08:53 -07002065 case R.id.window_one_menu_id:
2066 case R.id.window_two_menu_id:
2067 case R.id.window_three_menu_id:
2068 case R.id.window_four_menu_id:
2069 case R.id.window_five_menu_id:
2070 case R.id.window_six_menu_id:
2071 case R.id.window_seven_menu_id:
2072 case R.id.window_eight_menu_id:
2073 {
2074 int menuid = item.getItemId();
2075 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2076 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2077 Tab desiredTab = mTabControl.getTab(id);
2078 if (desiredTab != null &&
2079 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002080 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 }
2082 break;
2083 }
2084 }
2085 }
2086 break;
2087
2088 default:
2089 return false;
2090 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 return true;
2092 }
2093
John Reck68234a92012-04-19 15:27:12 -07002094 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2095 implements OnCancelListener {
2096
2097 private Tab mTab;
2098 private Dialog mProgressDialog;
2099 private ContentValues mValues;
2100
2101 private SaveSnapshotTask(Tab tab) {
2102 mTab = tab;
2103 }
2104
2105 @Override
2106 protected void onPreExecute() {
2107 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2108 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2109 true, true, this);
2110 mValues = mTab.createSnapshotValues();
2111 }
2112
2113 @Override
2114 protected Long doInBackground(Void... params) {
2115 if (!mTab.saveViewState(mValues)) {
2116 return null;
2117 }
2118 if (isCancelled()) {
2119 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2120 File file = mActivity.getFileStreamPath(path);
2121 if (!file.delete()) {
2122 file.deleteOnExit();
2123 }
2124 return null;
2125 }
2126 final ContentResolver cr = mActivity.getContentResolver();
2127 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2128 if (result == null) {
2129 return null;
2130 }
2131 long id = ContentUris.parseId(result);
2132 return id;
2133 }
2134
2135 @Override
2136 protected void onPostExecute(Long id) {
2137 if (isCancelled()) {
2138 return;
2139 }
2140 mProgressDialog.dismiss();
2141 if (id == null) {
2142 Toast.makeText(mActivity, R.string.snapshot_failed,
2143 Toast.LENGTH_SHORT).show();
2144 return;
2145 }
2146 Bundle b = new Bundle();
2147 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2148 mUi.showComboView(ComboViews.Snapshots, b);
2149 }
2150
2151 @Override
2152 public void onCancel(DialogInterface dialog) {
2153 cancel(true);
2154 }
2155 }
2156
Michael Kolb80f75082012-04-10 10:50:06 -07002157 @Override
2158 public void toggleUserAgent() {
2159 WebView web = getCurrentWebView();
2160 mSettings.toggleDesktopUseragent(web);
2161 web.loadUrl(web.getOriginalUrl());
2162 }
2163
2164 @Override
2165 public void findOnPage() {
2166 getCurrentTopWebView().showFindDialog(null, true);
2167 }
2168
2169 @Override
2170 public void openPreferences() {
2171 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2172 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2173 getCurrentTopWebView().getUrl());
2174 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2175 }
2176
2177 @Override
2178 public void bookmarkCurrentPage() {
2179 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2180 if (bookmarkIntent != null) {
2181 mActivity.startActivity(bookmarkIntent);
2182 }
2183 }
2184
John Recke1a03a32011-09-14 17:04:16 -07002185 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002186 SnapshotTab t = (SnapshotTab) getCurrentTab();
2187 t.loadUrl(t.getLiveUrl(), null);
John Recke1a03a32011-09-14 17:04:16 -07002188 }
2189
luxiaolb40014b2013-07-19 10:01:43 +08002190 private void showExitDialog(final Activity activity) {
2191 new AlertDialog.Builder(activity)
2192 .setTitle(R.string.exit_browser_title)
2193 .setIcon(android.R.drawable.ic_dialog_alert)
2194 .setMessage(R.string.exit_browser_msg)
2195 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2196 public void onClick(DialogInterface dialog, int which) {
2197 activity.moveTaskToBack(true);
2198 dialog.dismiss();
2199 }
2200 })
2201 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2202 public void onClick(DialogInterface dialog, int which) {
2203 activity.finish();
2204 mHandler.postDelayed(new Runnable() {
2205 @Override
2206 public void run() {
2207 // TODO Auto-generated method stub
2208 mCrashRecoveryHandler.clearState(true);
2209 int pid = android.os.Process.myPid();
2210 android.os.Process.killProcess(pid);
2211 }
2212 }, 300);
2213 dialog.dismiss();
2214 }
2215 })
2216 .show();
2217 }
Michael Kolb315d5022011-10-13 12:47:11 -07002218 @Override
2219 public void showPageInfo() {
2220 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2221 }
2222
John Reck9c35b9c2012-05-30 10:08:50 -07002223 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002224 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002225 // Let the History and Bookmark fragments handle menus they created.
2226 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2227 return false;
2228 }
2229
Michael Kolb8233fac2010-10-26 16:08:53 -07002230 int id = item.getItemId();
2231 boolean result = true;
2232 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002233 // -- Browser context menu
2234 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002235 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002236 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002237 case R.id.copy_link_context_menu_id:
2238 final WebView webView = getCurrentTopWebView();
2239 if (null == webView) {
2240 result = false;
2241 break;
2242 }
2243 final HashMap<String, WebView> hrefMap =
2244 new HashMap<String, WebView>();
2245 hrefMap.put("webview", webView);
2246 final Message msg = mHandler.obtainMessage(
2247 FOCUS_NODE_HREF, id, 0, hrefMap);
2248 webView.requestFocusNodeHref(msg);
2249 break;
2250
2251 default:
2252 // For other context menus
2253 result = onOptionsItemSelected(item);
2254 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002255 return result;
2256 }
2257
2258 /**
2259 * support programmatically opening the context menu
2260 */
2261 public void openContextMenu(View view) {
2262 mActivity.openContextMenu(view);
2263 }
2264
2265 /**
2266 * programmatically open the options menu
2267 */
2268 public void openOptionsMenu() {
2269 mActivity.openOptionsMenu();
2270 }
2271
John Reck9c35b9c2012-05-30 10:08:50 -07002272 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002273 public boolean onMenuOpened(int featureId, Menu menu) {
2274 if (mOptionsMenuOpen) {
2275 if (mConfigChanged) {
2276 // We do not need to make any changes to the state of the
2277 // title bar, since the only thing that happened was a
2278 // change in orientation
2279 mConfigChanged = false;
2280 } else {
2281 if (!mExtendedMenuOpen) {
2282 mExtendedMenuOpen = true;
2283 mUi.onExtendedMenuOpened();
2284 } else {
2285 // Switching the menu back to icon view, so show the
2286 // title bar once again.
2287 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002288 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002289 }
2290 }
2291 } else {
2292 // The options menu is closed, so open it, and show the title
2293 mOptionsMenuOpen = true;
2294 mConfigChanged = false;
2295 mExtendedMenuOpen = false;
2296 mUi.onOptionsMenuOpened();
2297 }
2298 return true;
2299 }
2300
John Reck9c35b9c2012-05-30 10:08:50 -07002301 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 public void onOptionsMenuClosed(Menu menu) {
2303 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002304 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002305 }
2306
John Reck9c35b9c2012-05-30 10:08:50 -07002307 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002309 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002310 }
2311
2312 // Helper method for getting the top window.
2313 @Override
2314 public WebView getCurrentTopWebView() {
2315 return mTabControl.getCurrentTopWebView();
2316 }
2317
2318 @Override
2319 public WebView getCurrentWebView() {
2320 return mTabControl.getCurrentWebView();
2321 }
2322
2323 /*
2324 * This method is called as a result of the user selecting the options
2325 * menu to see the download window. It shows the download window on top of
2326 * the current window.
2327 */
2328 void viewDownloads() {
2329 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2330 mActivity.startActivity(intent);
2331 }
2332
John Reck30b065e2011-07-19 10:58:05 -07002333 int getActionModeHeight() {
2334 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2335 new int[] { android.R.attr.actionBarSize });
2336 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2337 actionBarSizeTypedArray.recycle();
2338 return size;
2339 }
2340
Michael Kolb8233fac2010-10-26 16:08:53 -07002341 // action mode
2342
John Reck9c35b9c2012-05-30 10:08:50 -07002343 @Override
2344 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002345 mUi.onActionModeStarted(mode);
2346 mActionMode = mode;
2347 }
2348
2349 /*
2350 * True if a custom ActionMode (i.e. find or select) is in use.
2351 */
2352 @Override
2353 public boolean isInCustomActionMode() {
2354 return mActionMode != null;
2355 }
2356
2357 /*
2358 * End the current ActionMode.
2359 */
2360 @Override
2361 public void endActionMode() {
2362 if (mActionMode != null) {
2363 mActionMode.finish();
2364 }
2365 }
2366
2367 /*
2368 * Called by find and select when they are finished. Replace title bars
2369 * as necessary.
2370 */
John Reck9c35b9c2012-05-30 10:08:50 -07002371 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002372 public void onActionModeFinished(ActionMode mode) {
2373 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002374 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002375 mActionMode = null;
2376 }
2377
2378 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002379 final Tab tab = getCurrentTab();
2380 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002381 }
2382
2383 // bookmark handling
2384
2385 /**
2386 * add the current page as a bookmark to the given folder id
2387 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002388 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002389 * bookmarked, and if it is, edit that bookmark. If false, and
2390 * the site is already bookmarked, do not attempt to edit the
2391 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002392 */
2393 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002394 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002395 WebView w = getCurrentTopWebView();
2396 if (w == null) {
2397 return null;
2398 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002399 Intent i = new Intent(mActivity,
2400 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002401 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2402 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2403 String touchIconUrl = w.getTouchIconUrl();
2404 if (touchIconUrl != null) {
2405 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2406 WebSettings settings = w.getSettings();
2407 if (settings != null) {
2408 i.putExtra(AddBookmarkPage.USER_AGENT,
2409 settings.getUserAgentString());
2410 }
2411 }
2412 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2413 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2414 getDesiredThumbnailHeight(mActivity)));
2415 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002416 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002417 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2418 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002419 // Put the dialog at the upper right of the screen, covering the
2420 // star on the title bar.
2421 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002422 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002423 }
2424
2425 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002426 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002427 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002428 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002429 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002430 }
2431
2432 // thumbnails
2433
2434 /**
2435 * Return the desired width for thumbnail screenshots, which are stored in
2436 * the database, and used on the bookmarks screen.
2437 * @param context Context for finding out the density of the screen.
2438 * @return desired width for thumbnail screenshot.
2439 */
2440 static int getDesiredThumbnailWidth(Context context) {
2441 return context.getResources().getDimensionPixelOffset(
2442 R.dimen.bookmarkThumbnailWidth);
2443 }
2444
2445 /**
2446 * Return the desired height for thumbnail screenshots, which are stored in
2447 * the database, and used on the bookmarks screen.
2448 * @param context Context for finding out the density of the screen.
2449 * @return desired height for thumbnail screenshot.
2450 */
2451 static int getDesiredThumbnailHeight(Context context) {
2452 return context.getResources().getDimensionPixelOffset(
2453 R.dimen.bookmarkThumbnailHeight);
2454 }
2455
John Reck8cc92352011-07-06 17:41:52 -07002456 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002457 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002458 return null;
2459 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002460
2461 Bitmap viewportBitmap = view.getViewportBitmap();
2462 if (viewportBitmap == null) {
2463 return null;
2464 }
2465
2466 float aspectRatio = (float) width/height;
2467 int viewportWidth = viewportBitmap.getWidth();
2468 int viewportHeight = viewportBitmap.getHeight();
2469
2470 //modify the size to attain the same aspect ratio of desired thumbnail size
2471 if (viewportHeight > viewportWidth) {
2472 viewportHeight= (int)Math.round(viewportWidth * aspectRatio);
2473 } else {
2474 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2475 }
2476
2477 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2478 Rect dstRect = new Rect(0, 0, width, height);
2479
2480 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2481 || sThumbnailBitmap.getHeight() != height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002482 if (sThumbnailBitmap != null) {
2483 sThumbnailBitmap.recycle();
2484 sThumbnailBitmap = null;
2485 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002486
2487 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002488 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002489
John Reckd7dd9b22011-08-30 09:18:29 -07002490 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002491 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
John Reckd7dd9b22011-08-30 09:18:29 -07002492
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002493 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002494 }
2495
John Reck34ef2672011-02-10 11:30:55 -08002496 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002498 // FIXME: Would like to make sure there is actually something to
2499 // draw, but the API for that (WebViewCore.pictureReady()) is not
2500 // currently accessible here.
2501
John Reck34ef2672011-02-10 11:30:55 -08002502 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002503 if (view == null) {
2504 // Tab was destroyed
2505 return;
2506 }
John Reck34ef2672011-02-10 11:30:55 -08002507 final String url = tab.getUrl();
2508 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002509 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002510 if (TextUtils.isEmpty(url)) {
2511 return;
2512 }
2513
kaiyiz6e5b3e02013-08-19 20:02:01 +08002514 //update My Navigation Thumbnails
2515 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2516 if (isMyNavigationUrl) {
2517 updateMyNavigationThumbnail(url, view);
2518 }
John Reck34ef2672011-02-10 11:30:55 -08002519 // Only update thumbnails for web urls (http(s)://), not for
2520 // about:, javascript:, data:, etc...
2521 // Unless it is a bookmarked site, then always update
2522 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2523 return;
2524 }
2525
kaiyiz6e5b3e02013-08-19 20:02:01 +08002526 if (url != null && mUpdateMyNavThumbnailUrl != null
2527 && Patterns.WEB_URL.matcher(url).matches()
2528 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2529 String urlHost = (new WebAddress(url)).getHost();
2530 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2531 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2532 || bookmarkHost.length() == 0) {
2533 return;
2534 }
2535 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2536 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2537 bookmarkHost.length());
2538 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2539 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2540 if (!bookmarkDomain.equals(urlDomain)) {
2541 return;
2542 }
2543 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002544 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2545 getDesiredThumbnailHeight(mActivity));
2546 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002547 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2548 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2549 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2550 500);
2551 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002552 return;
2553 }
2554
2555 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002556 new AsyncTask<Void, Void, Void>() {
2557 @Override
2558 protected Void doInBackground(Void... unused) {
2559 Cursor cursor = null;
2560 try {
2561 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002562 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2563 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2564 : url);
2565 if (mUpdateMyNavThumbnail) {
2566 mUpdateMyNavThumbnail = false;
2567 mUpdateMyNavThumbnailUrl = null;
2568 }
John Reck34ef2672011-02-10 11:30:55 -08002569 if (cursor != null && cursor.moveToFirst()) {
2570 final ByteArrayOutputStream os =
2571 new ByteArrayOutputStream();
2572 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002573
John Reck34ef2672011-02-10 11:30:55 -08002574 ContentValues values = new ContentValues();
2575 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002576
John Reck34ef2672011-02-10 11:30:55 -08002577 do {
John Reck617fd832011-02-16 14:35:59 -08002578 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002579 cr.update(Images.CONTENT_URI, values, null, null);
2580 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002581 }
John Reck34ef2672011-02-10 11:30:55 -08002582 } catch (IllegalStateException e) {
2583 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002584 } catch (SQLiteException s) {
2585 // Added for possible error when user tries to remove the same bookmark
2586 // that is being updated with a screen shot
2587 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002588 } finally {
2589 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002590 }
John Reck34ef2672011-02-10 11:30:55 -08002591 return null;
2592 }
2593 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002594 }
2595
2596 private class Copy implements OnMenuItemClickListener {
2597 private CharSequence mText;
2598
John Reck9c35b9c2012-05-30 10:08:50 -07002599 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002600 public boolean onMenuItemClick(MenuItem item) {
2601 copy(mText);
2602 return true;
2603 }
2604
2605 public Copy(CharSequence toCopy) {
2606 mText = toCopy;
2607 }
2608 }
2609
Leon Scroggins63c02662010-11-18 15:16:27 -05002610 private static class Download implements OnMenuItemClickListener {
2611 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002613 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002614 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002615 private static final String FALLBACK_EXTENSION = "dat";
2616 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002617
John Reck9c35b9c2012-05-30 10:08:50 -07002618 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002620 if (DataUri.isDataUri(mText)) {
2621 saveDataUri();
2622 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002623 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002624 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002625 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002626 return true;
2627 }
2628
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002629 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2630 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002631 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002632 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002633 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002634 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002635 }
George Mount387d45d2011-10-07 15:57:53 -07002636
2637 /**
2638 * Treats mText as a data URI and writes its contents to a file
2639 * based on the current time.
2640 */
2641 private void saveDataUri() {
2642 FileOutputStream outputStream = null;
2643 try {
2644 DataUri uri = new DataUri(mText);
2645 File target = getTarget(uri);
2646 outputStream = new FileOutputStream(target);
2647 outputStream.write(uri.getData());
2648 final DownloadManager manager =
2649 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2650 manager.addCompletedDownload(target.getName(),
2651 mActivity.getTitle().toString(), false,
2652 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002653 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002654 } catch (IOException e) {
2655 Log.e(LOGTAG, "Could not save data URL");
2656 } finally {
2657 if (outputStream != null) {
2658 try {
2659 outputStream.close();
2660 } catch (IOException e) {
2661 // ignore close errors
2662 }
2663 }
2664 }
2665 }
2666
2667 /**
2668 * Creates a File based on the current time stamp and uses
2669 * the mime type of the DataUri to get the extension.
2670 */
2671 private File getTarget(DataUri uri) throws IOException {
2672 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002673 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002674 String nameBase = format.format(new Date());
2675 String mimeType = uri.getMimeType();
2676 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2677 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2678 if (extension == null) {
2679 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2680 extension = FALLBACK_EXTENSION;
2681 }
2682 extension = "." + extension; // createTempFile needs the '.'
2683 File targetFile = File.createTempFile(nameBase, extension, dir);
2684 return targetFile;
2685 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002686 }
2687
Cary Clark8974d282010-11-22 10:46:05 -05002688 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002689 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002690
John Reck9c35b9c2012-05-30 10:08:50 -07002691 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002692 public boolean onMenuItemClick(MenuItem item) {
2693 if (mWebView != null) {
2694 return mWebView.selectText();
2695 }
2696 return false;
2697 }
2698
2699 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002700 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002701 }
2702
2703 }
2704
Michael Kolb8233fac2010-10-26 16:08:53 -07002705 /********************** TODO: UI stuff *****************************/
2706
2707 // these methods have been copied, they still need to be cleaned up
2708
2709 /****************** tabs ***************************************************/
2710
2711 // basic tab interactions:
2712
2713 // it is assumed that tabcontrol already knows about the tab
2714 protected void addTab(Tab tab) {
2715 mUi.addTab(tab);
2716 }
2717
2718 protected void removeTab(Tab tab) {
2719 mUi.removeTab(tab);
2720 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002721 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002722 }
2723
Michael Kolbf2055602011-04-09 17:20:03 -07002724 @Override
2725 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002726 // monkey protection against delayed start
2727 if (tab != null) {
2728 mTabControl.setCurrentTab(tab);
2729 // the tab is guaranteed to have a webview after setCurrentTab
2730 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002731 tab.setTimeStamp();
Michael Kolbcd424e92011-02-24 10:26:26 -08002732 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002733 }
2734
John Reck8bcafc12011-08-29 16:43:02 -07002735 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002736 Tab current = mTabControl.getCurrentTab();
2737 if (current != null
2738 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002739 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002740 }
2741 }
2742
John Reck26b18322011-06-21 13:08:58 -07002743 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002744 // Dismiss the subwindow if applicable.
2745 dismissSubWindow(appTab);
2746 // Since we might kill the WebView, remove it from the
2747 // content view first.
2748 mUi.detachTab(appTab);
2749 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002750 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002751 // TODO: analyze why the remove and add are necessary
2752 mUi.attachTab(appTab);
2753 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002754 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002755 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002756 } else {
2757 // If the tab was the current tab, we have to attach
2758 // it to the view system again.
2759 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002760 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002761 }
2762 }
2763
2764 // Remove the sub window if it exists. Also called by TabControl when the
2765 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002766 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002767 public void dismissSubWindow(Tab tab) {
2768 removeSubWindow(tab);
2769 // dismiss the subwindow. This will destroy the WebView.
2770 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002771 WebView wv = getCurrentTopWebView();
2772 if (wv != null) {
2773 wv.requestFocus();
2774 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002775 }
2776
2777 @Override
2778 public void removeSubWindow(Tab t) {
2779 if (t.getSubWebView() != null) {
2780 mUi.removeSubWindow(t.getSubViewContainer());
2781 }
2782 }
2783
2784 @Override
2785 public void attachSubWindow(Tab tab) {
2786 if (tab.getSubWebView() != null) {
2787 mUi.attachSubWindow(tab.getSubViewContainer());
2788 getCurrentTopWebView().requestFocus();
2789 }
2790 }
2791
Mathew Inwood29721c22011-06-29 17:55:24 +01002792 private Tab showPreloadedTab(final UrlData urlData) {
2793 if (!urlData.isPreloaded()) {
2794 return null;
2795 }
2796 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2797 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2798 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002799 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002800 // Could not submit query. Fallback to regular tab creation
2801 tabControl.destroy();
2802 return null;
2803 }
2804 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002805 // check tab count and make room for new tab
2806 if (!mTabControl.canCreateNewTab()) {
2807 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2808 if (leastUsed != null) {
2809 closeTab(leastUsed);
2810 }
2811 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002812 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002813 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002814 mTabControl.addPreloadedTab(t);
2815 addTab(t);
2816 setActiveTab(t);
2817 return t;
2818 }
2819
Michael Kolb7bcafde2011-05-09 13:55:59 -07002820 // open a non inconito tab with the given url data
2821 // and set as active tab
2822 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002823 Tab tab = showPreloadedTab(urlData);
2824 if (tab == null) {
2825 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002826 if ((tab != null) && !urlData.isEmpty()) {
2827 loadUrlDataIn(tab, urlData);
2828 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002829 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002830 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002831 }
2832
Michael Kolb843510f2010-12-09 10:51:49 -08002833 @Override
2834 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002835 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002836 }
2837
Michael Kolb8233fac2010-10-26 16:08:53 -07002838 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002839 public Tab openIncognitoTab() {
2840 return openTab(INCOGNITO_URI, true, true, false);
2841 }
2842
2843 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002844 public Tab openTab(String url, boolean incognito, boolean setActive,
2845 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002846 return openTab(url, incognito, setActive, useCurrent, null);
2847 }
2848
2849 @Override
2850 public Tab openTab(String url, Tab parent, boolean setActive,
2851 boolean useCurrent) {
2852 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2853 setActive, useCurrent, parent);
2854 }
2855
2856 public Tab openTab(String url, boolean incognito, boolean setActive,
2857 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002858 Tab tab = createNewTab(incognito, setActive, useCurrent);
2859 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002860 if (parent != null && parent != tab) {
2861 parent.addChildTab(tab);
2862 }
Michael Kolb519d2282011-05-09 17:03:19 -07002863 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002864 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002865 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002866 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002867 return tab;
2868 }
2869
2870 // this method will attempt to create a new tab
2871 // incognito: private browsing tab
2872 // setActive: ste tab as current tab
2873 // useCurrent: if no new tab can be created, return current tab
2874 private Tab createNewTab(boolean incognito, boolean setActive,
2875 boolean useCurrent) {
2876 Tab tab = null;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002877 MemoryMonitor memMonitor = null;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002878 if (mTabControl.canCreateNewTab()) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002879 if (mSettings.enableMemoryMonitor()) {
2880 Log.d(LOGTAG, " Memory Monitor Enabled .");
2881 memMonitor = MemoryMonitor.getInstance(mActivity.getApplicationContext(),this);
2882 if (memMonitor != null) {
2883 //Remove webview associated with the oldest tab
2884 memMonitor.destroyLeastRecentlyActiveTab();
2885 }
2886 } else {
2887 Log.d(LOGTAG, " Memory Monitor disabled .");
2888 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002889 tab = mTabControl.createNewTab(incognito);
2890 addTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002891 tab.setTimeStamp();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002892 if (setActive) {
2893 setActiveTab(tab);
2894 }
2895 } else {
2896 if (useCurrent) {
2897 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002898 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002899 } else {
2900 mUi.showMaxTabsWarning();
2901 }
2902 }
2903 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002904 }
2905
John Reck2bc80422011-06-30 15:11:49 -07002906 @Override
2907 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2908 SnapshotTab tab = null;
2909 if (mTabControl.canCreateNewTab()) {
2910 tab = mTabControl.createSnapshotTab(snapshotId);
2911 addTab(tab);
2912 if (setActive) {
2913 setActiveTab(tab);
2914 }
2915 } else {
2916 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002917 }
2918 return tab;
2919 }
2920
Michael Kolb8233fac2010-10-26 16:08:53 -07002921 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002922 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002923 * @return boolean True if we successfully switched to a different tab. If
2924 * the indexth tab is null, or if that tab is the same as
2925 * the current one, return false.
2926 */
2927 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002928 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002929 Tab currentTab = mTabControl.getCurrentTab();
2930 if (tab == null || tab == currentTab) {
2931 return false;
2932 }
2933 setActiveTab(tab);
2934 return true;
2935 }
2936
2937 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002938 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002939 closeCurrentTab(false);
2940 }
2941
2942 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002943 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002944 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002945 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002946 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002947 return;
2948 }
Michael Kolbc831b632011-05-11 09:30:34 -07002949 final Tab current = mTabControl.getCurrentTab();
2950 final int pos = mTabControl.getCurrentPosition();
2951 Tab newTab = current.getParent();
2952 if (newTab == null) {
2953 newTab = mTabControl.getTab(pos + 1);
2954 if (newTab == null) {
2955 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002956 }
2957 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002958 if (andQuit) {
2959 mTabControl.setCurrentTab(newTab);
2960 closeTab(current);
2961 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002962 // Close window
2963 closeTab(current);
2964 }
2965 }
2966
2967 /**
2968 * Close the tab, remove its associated title bar, and adjust mTabControl's
2969 * current tab to a valid value.
2970 */
2971 @Override
2972 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002973 if (tab == mTabControl.getCurrentTab()) {
2974 closeCurrentTab();
2975 } else {
2976 removeTab(tab);
2977 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002978 }
2979
Afzal Najamd4e33312012-04-26 01:54:01 -04002980 /**
2981 * Close all tabs except the current one
2982 */
2983 @Override
2984 public void closeOtherTabs() {
2985 int inactiveTabs = mTabControl.getTabCount() - 1;
2986 for (int i = inactiveTabs; i >= 0; i--) {
2987 Tab tab = mTabControl.getTab(i);
2988 if (tab != mTabControl.getCurrentTab()) {
2989 removeTab(tab);
2990 }
2991 }
2992 }
2993
Michael Kolb8233fac2010-10-26 16:08:53 -07002994 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002995 protected void loadUrlFromContext(String url) {
2996 Tab tab = getCurrentTab();
2997 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002999 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003000 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003001 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003002 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003003 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003004 }
3005 }
3006 }
3007
3008 /**
3009 * Load the URL into the given WebView and update the title bar
3010 * to reflect the new load. Call this instead of WebView.loadUrl
3011 * directly.
3012 * @param view The WebView used to load url.
3013 * @param url The URL to load.
3014 */
John Reck71e51422011-07-01 16:49:28 -07003015 @Override
3016 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003017 loadUrl(tab, url, null);
3018 }
3019
3020 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3021 if (tab != null) {
3022 dismissSubWindow(tab);
3023 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003024 if (tab.hasCrashed) {
3025 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3026 }
Michael Kolba53c9892011-10-05 13:31:40 -07003027 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003028 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003029 }
3030
3031 /**
3032 * Load UrlData into a Tab and update the title bar to reflect the new
3033 * load. Call this instead of UrlData.loadIn directly.
3034 * @param t The Tab used to load.
3035 * @param data The UrlData being loaded.
3036 */
3037 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003038 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003039 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003040 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003041 } else {
John Reck38b39652012-06-05 09:22:59 -07003042 if (t != null && data.mDisableUrlOverride) {
3043 t.disableUrlOverridingForLoad();
3044 }
John Reck26b18322011-06-21 13:08:58 -07003045 loadUrl(t, data.mUrl, data.mHeaders);
3046 }
3047 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003048 }
3049
John Reck30c714c2010-12-16 17:30:34 -08003050 @Override
3051 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003052 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003053 if (tab.canGoBack()) {
3054 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003055 } else {
John Reckef654f12011-07-12 16:42:08 -07003056 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003057 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003058 }
3059
3060 void goBackOnePageOrQuit() {
3061 Tab current = mTabControl.getCurrentTab();
3062 if (current == null) {
3063 /*
3064 * Instead of finishing the activity, simply push this to the back
3065 * of the stack and let ActivityManager to choose the foreground
3066 * activity. As BrowserActivity is singleTask, it will be always the
3067 * root of the task. So we can use either true or false for
3068 * moveTaskToBack().
3069 */
luxiaolb40014b2013-07-19 10:01:43 +08003070 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003071 return;
3072 }
John Reckef654f12011-07-12 16:42:08 -07003073 if (current.canGoBack()) {
3074 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 } else {
3076 // Check to see if we are closing a window that was created by
3077 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003078 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003079 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003080 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003081 // Now we close the other tab
3082 closeTab(current);
3083 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003084 /*
3085 * Instead of finishing the activity, simply push this to the back
3086 * of the stack and let ActivityManager to choose the foreground
3087 * activity. As BrowserActivity is singleTask, it will be always the
3088 * root of the task. So we can use either true or false for
3089 * moveTaskToBack().
3090 */
luxiaolb40014b2013-07-19 10:01:43 +08003091 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003092 }
3093 }
3094 }
3095
3096 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003097 * helper method for key handler
3098 * returns the current tab if it can't advance
3099 */
Michael Kolbc831b632011-05-11 09:30:34 -07003100 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003101 int pos = mTabControl.getCurrentPosition() + 1;
3102 if (pos >= mTabControl.getTabCount()) {
3103 pos = 0;
3104 }
3105 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003106 }
3107
3108 /**
3109 * helper method for key handler
3110 * returns the current tab if it can't advance
3111 */
Michael Kolbc831b632011-05-11 09:30:34 -07003112 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003113 int pos = mTabControl.getCurrentPosition() - 1;
3114 if ( pos < 0) {
3115 pos = mTabControl.getTabCount() - 1;
3116 }
3117 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003118 }
3119
John Reckbcef87f2012-02-03 14:58:34 -08003120 boolean isMenuOrCtrlKey(int keyCode) {
3121 return (KeyEvent.KEYCODE_MENU == keyCode)
3122 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3123 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3124 }
3125
Michael Kolb0035fad2011-03-14 13:25:28 -07003126 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003127 * handle key events in browser
3128 *
3129 * @param keyCode
3130 * @param event
3131 * @return true if handled, false to pass to super
3132 */
John Reck9c35b9c2012-05-30 10:08:50 -07003133 @Override
3134 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003135 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003136 // Even if MENU is already held down, we need to call to super to open
3137 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003138 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003139 mMenuIsDown = true;
3140 return false;
3141 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003142
Cary Clark8ff8c662010-12-29 15:03:05 -05003143 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003144 Tab tab = getCurrentTab();
3145 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003146
Cary Clark160bbb92011-01-10 11:17:07 -05003147 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3148 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003149
Michael Kolb8233fac2010-10-26 16:08:53 -07003150 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003151 case KeyEvent.KEYCODE_TAB:
3152 if (event.isCtrlPressed()) {
3153 if (event.isShiftPressed()) {
3154 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003155 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003156 } else {
3157 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003158 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003159 }
3160 return true;
3161 }
3162 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003163 case KeyEvent.KEYCODE_SPACE:
3164 // WebView/WebTextView handle the keys in the KeyDown. As
3165 // the Activity's shortcut keys are only handled when WebView
3166 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003167 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003168 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003169 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003170 pageDown();
3171 }
3172 return true;
3173 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003174 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003175 event.startTracking();
3176 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003177 case KeyEvent.KEYCODE_FORWARD:
3178 if (!noModifiers) break;
3179 tab.goForward();
3180 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003181 case KeyEvent.KEYCODE_DPAD_LEFT:
3182 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003183 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003184 return true;
3185 }
3186 break;
3187 case KeyEvent.KEYCODE_DPAD_RIGHT:
3188 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003189 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003190 return true;
3191 }
3192 break;
3193 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003194 if (ctrl) {
3195 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003196 return true;
3197 }
3198 break;
Michael Kolba4183062011-01-16 10:43:21 -08003199// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003200 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003201 if (ctrl ) {
3202 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003203 return true;
3204 }
3205 break;
Michael Kolba4183062011-01-16 10:43:21 -08003206// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003207// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003208// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003209// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003210// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003211// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003212// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003213// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003214// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003215// case KeyEvent.KEYCODE_M: // unused
3216// case KeyEvent.KEYCODE_N: // in Chrome: new window
3217// case KeyEvent.KEYCODE_O: // in Chrome: open file
3218// case KeyEvent.KEYCODE_P: // in Chrome: print page
3219// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003220// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003221// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3222 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003223 // we can't use the ctrl/shift flags, they check for
3224 // exclusive use of a modifier
3225 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003226 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003227 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003228 } else {
3229 openTabToHomePage();
3230 }
3231 return true;
3232 }
3233 break;
3234// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3235// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003236// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003237// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3238// case KeyEvent.KEYCODE_Y: // unused
3239// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003240 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003241 // it is a regular key and webview is not null
3242 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003243 }
3244
John Reck9c35b9c2012-05-30 10:08:50 -07003245 @Override
3246 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003247 switch(keyCode) {
3248 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003249 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003250 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003251 return true;
3252 }
3253 break;
3254 }
3255 return false;
3256 }
3257
John Reck9c35b9c2012-05-30 10:08:50 -07003258 @Override
3259 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003260 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003261 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003262 if (KeyEvent.KEYCODE_MENU == keyCode
3263 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003264 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003265 }
3266 }
Cary Clark160bbb92011-01-10 11:17:07 -05003267 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003268 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003269 case KeyEvent.KEYCODE_BACK:
3270 if (event.isTracking() && !event.isCanceled()) {
3271 onBackKey();
3272 return true;
3273 }
3274 break;
3275 }
3276 return false;
3277 }
3278
3279 public boolean isMenuDown() {
3280 return mMenuIsDown;
3281 }
3282
John Reck9c35b9c2012-05-30 10:08:50 -07003283 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003284 public void setupAutoFill(Message message) {
3285 // Open the settings activity at the AutoFill profile fragment so that
3286 // the user can create a new profile. When they return, we will dispatch
3287 // the message so that we can autofill the form using their new profile.
3288 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3289 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3290 AutoFillSettingsFragment.class.getName());
3291 mAutoFillSetupMessage = message;
3292 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3293 }
John Reckb3417f02011-01-14 11:01:05 -08003294
John Reck9c35b9c2012-05-30 10:08:50 -07003295 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003296 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003297 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003298 return true;
3299 }
3300
John Reck1cf4b792011-07-26 10:22:22 -07003301 @Override
3302 public boolean shouldCaptureThumbnails() {
3303 return mUi.shouldCaptureThumbnails();
3304 }
3305
Michael Kolbc3af0672011-08-09 10:24:41 -07003306 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003307 public boolean supportsVoice() {
3308 PackageManager pm = mActivity.getPackageManager();
3309 List activities = pm.queryIntentActivities(new Intent(
3310 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3311 return activities.size() != 0;
3312 }
3313
3314 @Override
3315 public void startVoiceRecognizer() {
3316 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003317 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003318 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3319 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3320 mActivity.startActivityForResult(voice, VOICE_RESULT);
3321 }
3322
3323 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003324 public void setBlockEvents(boolean block) {
3325 mBlockEvents = block;
3326 }
3327
John Reck9c35b9c2012-05-30 10:08:50 -07003328 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003329 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003330 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003331 }
3332
John Reck9c35b9c2012-05-30 10:08:50 -07003333 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003334 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003335 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003336 }
3337
John Reck9c35b9c2012-05-30 10:08:50 -07003338 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003339 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003340 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003341 }
3342
John Reck9c35b9c2012-05-30 10:08:50 -07003343 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003344 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003345 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003346 }
3347
John Reck9c35b9c2012-05-30 10:08:50 -07003348 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003349 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003350 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003351 }
3352
Michael Kolb8233fac2010-10-26 16:08:53 -07003353}