blob: 406c73c4a5b8abdbff56a1d579869899d79b2974 [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
kaiyiz6e5b3e02013-08-19 20:02:01 +0800132 private static final String OFFLINE_PAGE =
Vivek Sekharb54614f2014-05-01 19:03:37 -0700133 "content://com.android.browser.mynavigation/websites";
134
135 private static final String ACTION_WIFI_SELECTION_DATA_CONNECTION =
136 "android.net.wifi.cmcc.WIFI_SELECTION_DATA_CONNECTION";
137 // Remind switch to data connection if wifi is unavailable
138 private static final int NETWORK_SWITCH_TYPE_OK = 1;
139 private static final String WIFI_BROWSER_INTERACTION_REMIND_TYPE =
140 "wifi_browser_interaction_remind";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800141
142 public final static String EXTRA_SHARE_SCREENSHOT = "share_screenshot";
143 public final static String EXTRA_SHARE_FAVICON = "share_favicon";
Michael Kolb8233fac2010-10-26 16:08:53 -0700144 // public message ids
145 public final static int LOAD_URL = 1001;
146 public final static int STOP_LOAD = 1002;
147
148 // Message Ids
149 private static final int FOCUS_NODE_HREF = 102;
150 private static final int RELEASE_WAKELOCK = 107;
151
152 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
153
154 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800155 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156
157 private static final int EMPTY_MENU = -1;
158
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700160 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 final static int PREFERENCES_PAGE = 3;
162 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000163 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700164 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800165 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000166
Michael Kolb8233fac2010-10-26 16:08:53 -0700167 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
168
169 // As the ids are dynamically created, we can't guarantee that they will
170 // be in sequence, so this static array maps ids to a window number.
171 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
172 { R.id.window_one_menu_id, R.id.window_two_menu_id,
173 R.id.window_three_menu_id, R.id.window_four_menu_id,
174 R.id.window_five_menu_id, R.id.window_six_menu_id,
175 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
176
177 // "source" parameter for Google search through search key
178 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
179 // "source" parameter for Google search through simplily type
180 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
181
George Mount387d45d2011-10-07 15:57:53 -0700182 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700183 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
184
John Reckd7dd9b22011-08-30 09:18:29 -0700185 // A bitmap that is re-used in createScreenshot as scratch space
186 private static Bitmap sThumbnailBitmap;
187
Michael Kolb8233fac2010-10-26 16:08:53 -0700188 private Activity mActivity;
189 private UI mUi;
190 private TabControl mTabControl;
191 private BrowserSettings mSettings;
192 private WebViewFactory mFactory;
193
194 private WakeLock mWakeLock;
195
196 private UrlHandler mUrlHandler;
197 private UploadHandler mUploadHandler;
198 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700199 private PageDialogsHandler mPageDialogsHandler;
200 private NetworkStateHandler mNetworkHandler;
201
Ben Murdoch8029a772010-11-16 11:58:21 +0000202 private Message mAutoFillSetupMessage;
203
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800205 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700206
207 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
208
209 // FIXME, temp address onPrepareMenu performance problem.
210 // When we move everything out of view, we should rewrite this.
211 private int mCurrentMenuState = 0;
212 private int mMenuState = R.id.MAIN_MENU;
213 private int mOldMenuState = EMPTY_MENU;
214 private Menu mCachedMenu;
215
Michael Kolb8233fac2010-10-26 16:08:53 -0700216 private boolean mMenuIsDown;
217
218 // For select and find, we keep track of the ActionMode so that
219 // finish() can be called as desired.
220 private ActionMode mActionMode;
221
222 /**
223 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
224 * of whether the configuration has changed. The first onMenuOpened call
225 * after a configuration change is simply a reopening of the same menu
226 * (i.e. mIconView did not change).
227 */
228 private boolean mConfigChanged;
229
230 /**
231 * Keeps track of whether the options menu is open. This is important in
232 * determining whether to show or hide the title bar overlay
233 */
234 private boolean mOptionsMenuOpen;
235
236 /**
237 * Whether or not the options menu is in its bigger, popup menu form. When
238 * true, we want the title bar overlay to be gone. When false, we do not.
239 * Only meaningful if mOptionsMenuOpen is true.
240 */
241 private boolean mExtendedMenuOpen;
242
Michael Kolb8233fac2010-10-26 16:08:53 -0700243 private boolean mActivityPaused = true;
244 private boolean mLoadStopped;
245
246 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500247 // Checks to see when the bookmarks database has changed, and updates the
248 // Tabs' notion of whether they represent bookmarked sites.
249 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700250 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700251
Michael Kolbc3af0672011-08-09 10:24:41 -0700252 private boolean mBlockEvents;
253
Michael Kolb0b129122012-06-04 16:31:58 -0700254 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800255 private boolean mUpdateMyNavThumbnail;
256 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700257
George Mount3636d0a2011-11-21 09:08:21 -0800258 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 mActivity = browser;
260 mSettings = BrowserSettings.getInstance();
261 mTabControl = new TabControl(this);
262 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700263 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800264 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700265 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700266
267 mUrlHandler = new UrlHandler(this);
268 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
270
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500272 mBookmarksObserver = new ContentObserver(mHandler) {
273 @Override
274 public void onChange(boolean selfChange) {
275 int size = mTabControl.getTabCount();
276 for (int i = 0; i < size; i++) {
277 mTabControl.getTab(i).updateBookmarkedStatus();
278 }
279 }
280
281 };
282 browser.getContentResolver().registerContentObserver(
283 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700284
285 mNetworkHandler = new NetworkStateHandler(mActivity, this);
286 // Start watching the default geolocation permissions
287 mSystemAllowGeolocationOrigins =
288 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
289 mSystemAllowGeolocationOrigins.start();
290
John Reckaf262e72011-07-25 13:55:44 -0700291 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700292 }
293
John Reck9c35b9c2012-05-30 10:08:50 -0700294 @Override
295 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800296 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800297 // mCrashRecoverHandler has any previously saved state.
298 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700299 }
300
George Mount3636d0a2011-11-21 09:08:21 -0800301 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700302 // Unless the last browser usage was within 24 hours, destroy any
303 // remaining incognito tabs.
304
305 Calendar lastActiveDate = icicle != null ?
306 (Calendar) icicle.getSerializable("lastActiveDate") : null;
307 Calendar today = Calendar.getInstance();
308 Calendar yesterday = Calendar.getInstance();
309 yesterday.add(Calendar.DATE, -1);
310
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800311 // we dont want to ever recover incognito tabs
312 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700313
Patrick Scott7d50a932011-02-04 09:27:26 -0500314 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700315 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500316 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000317
Michael Kolbc831b632011-05-11 09:30:34 -0700318 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500319 // Not able to restore so we go ahead and clear session cookies. We
320 // must do this before trying to login the user as we don't want to
321 // clear any session cookies set during login.
322 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700323 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800324 if (intent == null) {
325 // This won't happen under common scenarios. The icicle is
326 // not null, but there aren't any tabs to restore.
327 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700328 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800329 final Bundle extra = intent.getExtras();
330 // Create an initial tab.
331 // If the intent is ACTION_VIEW and data is not null, the Browser is
332 // invoked to view the content by another application. In this case,
333 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800334 UrlData urlData = null;
335 if (intent.getData() != null
336 && Intent.ACTION_VIEW.equals(intent.getAction())
337 && intent.getData().toString().startsWith("content://")) {
338 urlData = new UrlData(intent.getData().toString());
339 } else {
340 urlData = IntentHandler.getUrlDataFromIntent(intent);
341 }
George Mount3636d0a2011-11-21 09:08:21 -0800342 Tab t = null;
343 if (urlData.isEmpty()) {
Vivek Sekharce2a4832014-03-26 13:26:53 -0700344 Resources res = mActivity.getResources();
345 String browserRes = res.getString(R.string.config_carrier_resource);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800346 if (browserRes.equals(
kaiyiz6e5b3e02013-08-19 20:02:01 +0800347 "cmcc")) {
348 t = openTab(OFFLINE_PAGE, false, true, true);
349 } else {
350 t = openTabToHomePage();
351 }
George Mount3636d0a2011-11-21 09:08:21 -0800352 } else {
353 t = openTab(urlData);
354 }
355 if (t != null) {
356 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
357 }
358 WebView webView = t.getWebView();
359 if (extra != null) {
360 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
361 if (scale > 0 && scale <= 1000) {
362 webView.setInitialScale(scale);
363 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700364 }
365 }
John Reckd8c74522011-06-14 08:45:00 -0700366 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700367 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700368 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500369 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700370 List<Tab> tabs = mTabControl.getTabs();
371 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
372 for (Tab t : tabs) {
373 restoredTabs.add(t.getId());
374 }
375 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700376 if (tabs.size() == 0) {
377 openTabToHomePage();
378 }
John Reck1cf4b792011-07-26 10:22:22 -0700379 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700380 // TabControl.restoreState() will create a new tab even if
381 // restoring the state fails.
382 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800383 // Intent is non-null when framework thinks the browser should be
384 // launching with a new intent (icicle is null).
385 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700386 mIntentHandler.onNewIntent(intent);
387 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700389 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700390 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800391 if (jsFlags.trim().length() != 0) {
392 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 }
George Mount3636d0a2011-11-21 09:08:21 -0800394 if (intent != null
395 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700396 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800397 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700398 }
399
John Reck1cf4b792011-07-26 10:22:22 -0700400 private static class PruneThumbnails implements Runnable {
401 private Context mContext;
402 private List<Long> mIds;
403
404 PruneThumbnails(Context context, List<Long> preserveIds) {
405 mContext = context.getApplicationContext();
406 mIds = preserveIds;
407 }
408
409 @Override
410 public void run() {
411 ContentResolver cr = mContext.getContentResolver();
412 if (mIds == null || mIds.size() == 0) {
413 cr.delete(Thumbnails.CONTENT_URI, null, null);
414 } else {
415 int length = mIds.size();
416 StringBuilder where = new StringBuilder();
417 where.append(Thumbnails._ID);
418 where.append(" not in (");
419 for (int i = 0; i < length; i++) {
420 where.append(mIds.get(i));
421 if (i < (length - 1)) {
422 where.append(",");
423 }
424 }
425 where.append(")");
426 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
427 }
428 }
429
430 }
431
Michael Kolb1514bb72010-11-22 09:11:48 -0800432 @Override
433 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700434 return mFactory;
435 }
436
437 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800438 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800439 if (tab.hasCrashed)
440 tab.showCrashView();
441 else
442 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800443 }
444
445 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800446 public void createSubWindow(Tab tab) {
447 endActionMode();
448 WebView mainView = tab.getWebView();
449 WebView subView = mFactory.createWebView((mainView == null)
450 ? false
451 : mainView.isPrivateBrowsingEnabled());
452 mUi.createSubWindow(tab, subView);
453 }
454
455 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700456 public Context getContext() {
457 return mActivity;
458 }
459
460 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700461 public Activity getActivity() {
462 return mActivity;
463 }
464
465 void setUi(UI ui) {
466 mUi = ui;
467 }
468
Michael Kolbaf63dba2012-05-16 12:58:05 -0700469 @Override
470 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700471 return mSettings;
472 }
473
474 IntentHandler getIntentHandler() {
475 return mIntentHandler;
476 }
477
478 @Override
479 public UI getUi() {
480 return mUi;
481 }
482
483 int getMaxTabs() {
484 return mActivity.getResources().getInteger(R.integer.max_tabs);
485 }
486
487 @Override
488 public TabControl getTabControl() {
489 return mTabControl;
490 }
491
Michael Kolb1bf23132010-11-19 12:55:12 -0800492 @Override
493 public List<Tab> getTabs() {
494 return mTabControl.getTabs();
495 }
496
John Reckaf262e72011-07-25 13:55:44 -0700497 // Open the icon database.
498 private void openIconDatabase() {
499 // We have to call getInstance on the UI thread
500 final WebIconDatabase instance = WebIconDatabase.getInstance();
501 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000502
John Reckaf262e72011-07-25 13:55:44 -0700503 @Override
504 public void run() {
505 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700506 }
John Reckaf262e72011-07-25 13:55:44 -0700507 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 }
509
510 private void startHandler() {
511 mHandler = new Handler() {
512
513 @Override
514 public void handleMessage(Message msg) {
515 switch (msg.what) {
516 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700517 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700518 break;
519 case FOCUS_NODE_HREF:
520 {
521 String url = (String) msg.getData().get("url");
522 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500523 String src = (String) msg.getData().get("src");
524 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 if (TextUtils.isEmpty(url)) {
526 break;
527 }
528 HashMap focusNodeMap = (HashMap) msg.obj;
529 WebView view = (WebView) focusNodeMap.get("webview");
530 // Only apply the action if the top window did not change.
531 if (getCurrentTopWebView() != view) {
532 break;
533 }
534 switch (msg.arg1) {
535 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700536 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700537 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500538 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700539 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500540 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500541 case R.id.open_newtab_context_menu_id:
542 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700543 openTab(url, parent,
544 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500545 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 case R.id.copy_link_context_menu_id:
547 copy(url);
548 break;
549 case R.id.save_link_context_menu_id:
550 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500551 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800552 mActivity, url, view.getSettings().getUserAgentString(),
553 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700555 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700556 if(title == null || title == "")
557 title = url;
558
559 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
560 //SWE TODO: No thumbnail support for the url obtained via
561 //browser context menu as its not loaded in webview.
562 if (bookmarkIntent != null) {
563 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
564 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
565 mActivity.startActivity(bookmarkIntent);
566 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700567 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700568 }
569 break;
570 }
571
572 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700573 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700574 break;
575
576 case STOP_LOAD:
577 stopLoading();
578 break;
579
580 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700581 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700582 mWakeLock.release();
583 // if we reach here, Browser should be still in the
584 // background loading after WAKELOCK_TIMEOUT (5-min).
585 // To avoid burning the battery, stop loading.
586 mTabControl.stopAllLoading();
587 }
588 break;
589
590 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800591 Tab tab = (Tab) msg.obj;
592 if (tab != null) {
593 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700594 }
595 break;
kaiyiz591110b2013-08-06 17:11:06 +0800596 case OPEN_MENU:
597 if (!mOptionsMenuOpen && mActivity != null ) {
598 mActivity.openOptionsMenu();
599 }
600 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700601 }
602 }
603 };
604
605 }
606
John Reckef654f12011-07-12 16:42:08 -0700607 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200608 public Tab getCurrentTab() {
609 return mTabControl.getCurrentTab();
610 }
611
Michael Kolbba99c5d2010-11-29 14:57:41 -0800612 @Override
613 public void shareCurrentPage() {
614 shareCurrentPage(mTabControl.getCurrentTab());
615 }
616
617 private void shareCurrentPage(Tab tab) {
618 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800619 sharePage(mActivity, tab.getTitle(),
620 tab.getUrl(), tab.getFavicon(),
621 createScreenshot(tab.getWebView(),
622 getDesiredThumbnailWidth(mActivity),
623 getDesiredThumbnailHeight(mActivity)));
624 }
625 }
626
Michael Kolb8233fac2010-10-26 16:08:53 -0700627 /**
628 * Share a page, providing the title, url, favicon, and a screenshot. Uses
629 * an {@link Intent} to launch the Activity chooser.
630 * @param c Context used to launch a new Activity.
631 * @param title Title of the page. Stored in the Intent with
632 * {@link Intent#EXTRA_SUBJECT}
633 * @param url URL of the page. Stored in the Intent with
634 * {@link Intent#EXTRA_TEXT}
635 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
636 * with {@link Browser#EXTRA_SHARE_FAVICON}
637 * @param screenshot Bitmap of a screenshot of the page. Stored in the
638 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
639 */
640 static final void sharePage(Context c, String title, String url,
641 Bitmap favicon, Bitmap screenshot) {
642 Intent send = new Intent(Intent.ACTION_SEND);
643 send.setType("text/plain");
644 send.putExtra(Intent.EXTRA_TEXT, url);
645 send.putExtra(Intent.EXTRA_SUBJECT, title);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800646 send.putExtra(EXTRA_SHARE_FAVICON, favicon);
647 send.putExtra(EXTRA_SHARE_SCREENSHOT, screenshot);
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 try {
649 c.startActivity(Intent.createChooser(send, c.getString(
650 R.string.choosertitle_sharevia)));
651 } catch(android.content.ActivityNotFoundException ex) {
652 // if no app handles it, do nothing
653 }
654 }
655
656 private void copy(CharSequence text) {
657 ClipboardManager cm = (ClipboardManager) mActivity
658 .getSystemService(Context.CLIPBOARD_SERVICE);
659 cm.setText(text);
660 }
661
662 // lifecycle
663
John Reck9c35b9c2012-05-30 10:08:50 -0700664 @Override
665 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800667 // update the menu in case of a locale change
668 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800669 if (mOptionsMenuOpen) {
670 mActivity.closeOptionsMenu();
671 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
672 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 if (mPageDialogsHandler != null) {
674 mPageDialogsHandler.onConfigurationChanged(config);
675 }
676 mUi.onConfigurationChanged(config);
677 }
678
679 @Override
680 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700681 if (!mUi.isWebShowing()) {
682 mUi.showWeb(false);
683 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 mIntentHandler.onNewIntent(intent);
685 }
686
John Reck9c35b9c2012-05-30 10:08:50 -0700687 @Override
688 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800689 if (mUi.isCustomViewShowing()) {
690 hideCustomView();
691 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 if (mActivityPaused) {
693 Log.e(LOGTAG, "BrowserActivity is already paused.");
694 return;
695 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800697 Tab tab = mTabControl.getCurrentTab();
698 if (tab != null) {
699 tab.pause();
700 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700701 if (mWakeLock == null) {
702 PowerManager pm = (PowerManager) mActivity
703 .getSystemService(Context.POWER_SERVICE);
704 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
705 }
Michael Kolb70976932010-11-30 11:34:01 -0800706 mWakeLock.acquire();
707 mHandler.sendMessageDelayed(mHandler
708 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
709 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700710 }
711 mUi.onPause();
712 mNetworkHandler.onPause();
713
714 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100715 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700716 if (sThumbnailBitmap != null) {
717 sThumbnailBitmap.recycle();
718 sThumbnailBitmap = null;
719 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700720 }
721
John Reck9c35b9c2012-05-30 10:08:50 -0700722 @Override
723 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700724 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800725 Bundle saveState = createSaveState();
726
727 // crash recovery manages all save & restore state
728 mCrashRecoveryHandler.writeState(saveState);
729 mSettings.setLastRunPaused(true);
730 }
731
732 /**
733 * Save the current state to outState. Does not write the state to
734 * disk.
735 * @return Bundle containing the current state of all tabs.
736 */
737 /* package */ Bundle createSaveState() {
738 Bundle saveState = new Bundle();
739 mTabControl.saveState(saveState);
740 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700741 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800742 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700743 }
George Mount3636d0a2011-11-21 09:08:21 -0800744 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 }
746
John Reck9c35b9c2012-05-30 10:08:50 -0700747 @Override
748 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700749 if (!mActivityPaused) {
750 Log.e(LOGTAG, "BrowserActivity is already resumed.");
751 return;
752 }
George Mount3636d0a2011-11-21 09:08:21 -0800753 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800755 Tab current = mTabControl.getCurrentTab();
756 if (current != null) {
757 current.resume();
758 resumeWebViewTimers(current);
759 }
John Reckf57c0292011-07-21 18:15:39 -0700760 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200761
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 mUi.onResume();
763 mNetworkHandler.onResume();
764 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100765 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700766 if (mVoiceResult != null) {
767 mUi.onVoiceResult(mVoiceResult);
768 mVoiceResult = null;
769 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800770 if (current != null && current.hasCrashed) {
771 current.showCrashView();
772 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700773 }
774
John Reckf57c0292011-07-21 18:15:39 -0700775 private void releaseWakeLock() {
776 if (mWakeLock != null && mWakeLock.isHeld()) {
777 mHandler.removeMessages(RELEASE_WAKELOCK);
778 mWakeLock.release();
779 }
780 }
781
Michael Kolb70976932010-11-30 11:34:01 -0800782 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800783 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800784 * @param tab guaranteed non-null
785 */
786 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700787 boolean inLoad = tab.inPageLoad();
788 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
789 CookieSyncManager.getInstance().startSync();
790 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100791 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 }
793 }
794
Michael Kolb70976932010-11-30 11:34:01 -0800795 /**
796 * Pause all WebView timers using the WebView of the given tab
797 * @param tab
798 * @return true if the timers are paused or tab is null
799 */
800 private boolean pauseWebViewTimers(Tab tab) {
801 if (tab == null) {
802 return true;
803 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100805 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700807 }
Michael Kolb70976932010-11-30 11:34:01 -0800808 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700809 }
810
John Reck9c35b9c2012-05-30 10:08:50 -0700811 @Override
812 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800813 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
815 mUploadHandler = null;
816 }
817 if (mTabControl == null) return;
818 mUi.onDestroy();
819 // Remove the current tab and sub window
820 Tab t = mTabControl.getCurrentTab();
821 if (t != null) {
822 dismissSubWindow(t);
823 removeTab(t);
824 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500825 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700826 // Destroy all the tabs
827 mTabControl.destroy();
828 WebIconDatabase.getInstance().close();
829 // Stop watching the default geolocation permissions
830 mSystemAllowGeolocationOrigins.stop();
831 mSystemAllowGeolocationOrigins = null;
832 }
833
834 protected boolean isActivityPaused() {
835 return mActivityPaused;
836 }
837
John Reck9c35b9c2012-05-30 10:08:50 -0700838 @Override
839 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 mTabControl.freeMemory();
841 }
842
843 @Override
844 public boolean shouldShowErrorConsole() {
845 return mShouldShowErrorConsole;
846 }
847
848 protected void setShouldShowErrorConsole(boolean show) {
849 if (show == mShouldShowErrorConsole) {
850 // Nothing to do.
851 return;
852 }
853 mShouldShowErrorConsole = show;
854 Tab t = mTabControl.getCurrentTab();
855 if (t == null) {
856 // There is no current tab so we cannot toggle the error console
857 return;
858 }
859 mUi.setShouldShowErrorConsole(t, show);
860 }
861
862 @Override
863 public void stopLoading() {
864 mLoadStopped = true;
865 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700867 if (w != null) {
868 w.stopLoading();
869 mUi.onPageStopped(tab);
870 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 }
872
873 boolean didUserStopLoading() {
874 return mLoadStopped;
875 }
876
kaiyiza016da12013-08-26 17:50:22 +0800877 private void handleNetworkNotify(WebView view) {
878 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700879 Context.CONNECTIVITY_SERVICE);
880 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
881 if (networkInfo == null
882 || (networkInfo != null && (networkInfo.getType() !=
883 ConnectivityManager.TYPE_WIFI))) {
884 int isReminder = Settings.System.getInt(
885 mActivity.getContentResolver(),
886 WIFI_BROWSER_INTERACTION_REMIND_TYPE,
887 NETWORK_SWITCH_TYPE_OK);
kaiyiza016da12013-08-26 17:50:22 +0800888
Vivek Sekharb54614f2014-05-01 19:03:37 -0700889 if (isReminder == NETWORK_SWITCH_TYPE_OK) {
890 mNetworkShouldNotify = false;
891 Intent intent = new Intent(
892 ACTION_WIFI_SELECTION_DATA_CONNECTION);
893 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
894 this.getContext().startActivity(intent);
895 } else {
896 if (!mNetworkHandler.isNetworkUp()) {
897 view.setNetworkAvailable(false);
kaiyiza016da12013-08-26 17:50:22 +0800898 }
kaiyiza016da12013-08-26 17:50:22 +0800899 }
900 }
901 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700902
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 // WebViewController
904
905 @Override
John Reck324d4402011-01-11 16:56:42 -0800906 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700907
908 // We've started to load a new page. If there was a pending message
909 // to save a screenshot then we will now take the new page and save
910 // an incorrect screenshot. Therefore, remove any pending thumbnail
911 // messages from the queue.
912 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800913 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700914
915 // reset sync timer to avoid sync starts during loading a page
916 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700917 WifiManager wifiMgr = (WifiManager) this.getContext()
918 .getSystemService(Context.WIFI_SERVICE);
919 String browserRes = mActivity.getResources().getString(R.string.config_carrier_resource);
920 if ("cmcc".equals(browserRes) && mNetworkShouldNotify && wifiMgr.isWifiEnabled()) {
921 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800922 } else {
923 if (!mNetworkHandler.isNetworkUp()) {
924 view.setNetworkAvailable(false);
925 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700926 }
927
928 // when BrowserActivity just starts, onPageStarted may be called before
929 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
930 // to start the timer. As we won't switch tabs while an activity is in
931 // pause state, we can ensure calling resume and pause in pair.
932 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800933 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700934 }
935 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 endActionMode();
937
John Reck30c714c2010-12-16 17:30:34 -0800938 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700939
John Reck324d4402011-01-11 16:56:42 -0800940 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700941 // update the bookmark database for favicon
942 maybeUpdateFavicon(tab, null, url, favicon);
943
944 Performance.tracePageStart(url);
945
946 // Performance probe
947 if (false) {
948 Performance.onPageStarted();
949 }
950
951 }
952
953 @Override
John Reck324d4402011-01-11 16:56:42 -0800954 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700955 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800956 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200957
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 // Performance probe
959 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800960 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700961 }
962
963 Performance.tracePageFinished();
964 }
965
966 @Override
John Reck30c714c2010-12-16 17:30:34 -0800967 public void onProgressChanged(Tab tab) {
968 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700969
970 if (newProgress == 100) {
971 CookieSyncManager.getInstance().sync();
972 // onProgressChanged() may continue to be called after the main
973 // frame has finished loading, as any remaining sub frames continue
974 // to load. We'll only get called once though with newProgress as
975 // 100 when everything is loaded. (onPageFinished is called once
976 // when the main frame completes loading regardless of the state of
977 // any sub frames so calls to onProgressChanges may continue after
978 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800979 if (tab.inPageLoad()) {
980 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200981 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
982 // pause the WebView timer and release the wake lock if it is
983 // finished while BrowserActivity is in pause state.
984 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 }
John Reckd9862372012-02-21 15:04:50 -0800986 if (!tab.isPrivateBrowsingEnabled()
987 && !TextUtils.isEmpty(tab.getUrl())
988 && !tab.isSnapshot()) {
989 // Only update the bookmark screenshot if the user did not
990 // cancel the load early and there is not already
991 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700992 if (tab.shouldUpdateThumbnail() &&
993 (tab.inForeground() && !didUserStopLoading()
994 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800995 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
996 mHandler.sendMessageDelayed(mHandler.obtainMessage(
997 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800998 1500);
John Reckd9862372012-02-21 15:04:50 -0800999 }
1000 }
1001 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001002 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001003 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001005 // still loading
1006 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001007 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001008 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001009 }
1010 }
John Reck30c714c2010-12-16 17:30:34 -08001011 mUi.onProgressChanged(tab);
1012 }
1013
1014 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001015 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001016 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001017 }
1018
1019 @Override
1020 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001021 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001022 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001023 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1025 return;
1026 }
1027 // Update the title in the history database if not in private browsing mode
1028 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001029 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001030 }
1031 }
1032
1033 @Override
1034 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001035 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001036 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1037 }
1038
1039 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001040 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1041 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 }
1043
1044 @Override
1045 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1046 if (mMenuIsDown) {
1047 // only check shortcut key when MENU is held
1048 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1049 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001051 int keyCode = event.getKeyCode();
1052 // We need to send almost every key to WebKit. However:
1053 // 1. We don't want to block the device on the renderer for
1054 // some keys like menu, home, call.
1055 // 2. There are no WebKit equivalents for some of these keys
1056 // (see app/keyboard_codes_win.h)
1057 // Note that these are not the same set as KeyEvent.isSystemKey:
1058 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1059 if (keyCode == KeyEvent.KEYCODE_MENU ||
1060 keyCode == KeyEvent.KEYCODE_HOME ||
1061 keyCode == KeyEvent.KEYCODE_BACK ||
1062 keyCode == KeyEvent.KEYCODE_CALL ||
1063 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1064 keyCode == KeyEvent.KEYCODE_POWER ||
1065 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1066 keyCode == KeyEvent.KEYCODE_CAMERA ||
1067 keyCode == KeyEvent.KEYCODE_FOCUS ||
1068 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1069 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1070 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1071 return true;
1072 }
1073
1074 // We also have to intercept some shortcuts before we send them to the ContentView.
1075 if (event.isCtrlPressed() && (
1076 keyCode == KeyEvent.KEYCODE_TAB ||
1077 keyCode == KeyEvent.KEYCODE_W ||
1078 keyCode == KeyEvent.KEYCODE_F4)) {
1079 return true;
1080 }
1081
1082 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001083 }
1084
1085 @Override
John Reck997b1b72012-04-19 18:08:25 -07001086 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001087 if (!isActivityPaused()) {
1088 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001089 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001090 } else {
John Reck997b1b72012-04-19 18:08:25 -07001091 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 }
1093 }
John Reck997b1b72012-04-19 18:08:25 -07001094 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001095 }
1096
1097 @Override
John Reck324d4402011-01-11 16:56:42 -08001098 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001099 // Don't save anything in private browsing mode
1100 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -08001101 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001102
John Reck324d4402011-01-11 16:56:42 -08001103 if (TextUtils.isEmpty(url)
1104 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 return;
1106 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001107
John Reckf57c0292011-07-21 18:15:39 -07001108 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001109 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001110 }
1111
1112 @Override
1113 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1114 AsyncTask<Void, Void, String[]> task =
1115 new AsyncTask<Void, Void, String[]>() {
1116 @Override
1117 public String[] doInBackground(Void... unused) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001118 Object[] params = {mActivity.getContentResolver()};
1119 Class[] type = new Class[] {ContentResolver.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001120 return (String[])ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001121 "android.provider.Browser","getVisitedHistory",
1122 type, params);
Michael Kolb8233fac2010-10-26 16:08:53 -07001123 }
1124 @Override
1125 public void onPostExecute(String[] result) {
1126 callback.onReceiveValue(result);
1127 }
1128 };
1129 task.execute();
1130 }
1131
1132 @Override
1133 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1134 final HttpAuthHandler handler, final String host,
1135 final String realm) {
1136 String username = null;
1137 String password = null;
1138
1139 boolean reuseHttpAuthUsernamePassword
1140 = handler.useHttpAuthUsernamePassword();
1141
1142 if (reuseHttpAuthUsernamePassword && view != null) {
1143 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1144 if (credentials != null && credentials.length == 2) {
1145 username = credentials[0];
1146 password = credentials[1];
1147 }
1148 }
1149
1150 if (username != null && password != null) {
1151 handler.proceed(username, password);
1152 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001153 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001154 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1155 } else {
1156 handler.cancel();
1157 }
1158 }
1159 }
1160
1161 @Override
1162 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001163 String contentDisposition, String mimetype, String referer,
1164 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001165 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001166 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001167 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001168 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 // This Tab was opened for the sole purpose of downloading a
1170 // file. Remove it.
1171 if (tab == mTabControl.getCurrentTab()) {
1172 // In this case, the Tab is still on top.
1173 goBackOnePageOrQuit();
1174 } else {
1175 // In this case, it is not.
1176 closeTab(tab);
1177 }
1178 }
1179 }
1180
1181 @Override
1182 public Bitmap getDefaultVideoPoster() {
1183 return mUi.getDefaultVideoPoster();
1184 }
1185
1186 @Override
1187 public View getVideoLoadingProgressView() {
1188 return mUi.getVideoLoadingProgressView();
1189 }
1190
1191 @Override
1192 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1193 SslError error) {
1194 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1195 }
1196
1197 // helper method
1198
1199 /*
1200 * Update the favorites icon if the private browsing isn't enabled and the
1201 * icon is valid.
1202 */
1203 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1204 final String url, Bitmap favicon) {
1205 if (favicon == null) {
1206 return;
1207 }
1208 if (!tab.isPrivateBrowsingEnabled()) {
1209 Bookmarks.updateFavicon(mActivity
1210 .getContentResolver(), originalUrl, url, favicon);
1211 }
1212 }
1213
Leon Scroggins4cd97792010-12-03 15:31:56 -05001214 @Override
1215 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001216 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001217 mUi.bookmarkedStatusHasChanged(tab);
1218 }
1219
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 // end WebViewController
1221
1222 protected void pageUp() {
1223 getCurrentTopWebView().pageUp(false);
1224 }
1225
1226 protected void pageDown() {
1227 getCurrentTopWebView().pageDown(false);
1228 }
1229
1230 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001231 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 public void editUrl() {
1233 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001234 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 }
1236
John Reck9c35b9c2012-05-30 10:08:50 -07001237 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001238 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001239 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 if (tab.inForeground()) {
1241 if (mUi.isCustomViewShowing()) {
1242 callback.onCustomViewHidden();
1243 return;
1244 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001245 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 // Save the menu state and set it to empty while the custom
1247 // view is showing.
1248 mOldMenuState = mMenuState;
1249 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001250 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 }
1252 }
1253
1254 @Override
1255 public void hideCustomView() {
1256 if (mUi.isCustomViewShowing()) {
1257 mUi.onHideCustomView();
1258 // Reset the old menu state.
1259 mMenuState = mOldMenuState;
1260 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001261 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 }
1263 }
1264
John Reck9c35b9c2012-05-30 10:08:50 -07001265 @Override
1266 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001267 Intent intent) {
1268 if (getCurrentTopWebView() == null) return;
1269 switch (requestCode) {
1270 case PREFERENCES_PAGE:
1271 if (resultCode == Activity.RESULT_OK && intent != null) {
1272 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001273 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 mTabControl.removeParentChildRelationShips();
1275 }
1276 }
1277 break;
1278 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001279 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001280 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001283 case AUTOFILL_SETUP:
1284 // Determine whether a profile was actually set up or not
1285 // and if so, send the message back to the WebTextView to
1286 // fill the form with the new profile.
1287 if (getSettings().getAutoFillProfile() != null) {
1288 mAutoFillSetupMessage.sendToTarget();
1289 mAutoFillSetupMessage = null;
1290 }
1291 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001292 case COMBO_VIEW:
1293 if (intent == null || resultCode != Activity.RESULT_OK) {
1294 break;
1295 }
John Reck3ba45532011-08-11 16:26:53 -07001296 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001297 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1298 Tab t = getCurrentTab();
1299 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001300 mUpdateMyNavThumbnail = true;
1301 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001302 loadUrl(t, uri.toString());
1303 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1304 String[] urls = intent.getStringArrayExtra(
1305 ComboViewActivity.EXTRA_OPEN_ALL);
1306 Tab parent = getCurrentTab();
1307 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001308 if (url != null) {
1309 parent = openTab(url, parent,
1310 !mSettings.openInBackground(), true);
1311 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001312 }
1313 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1314 long id = intent.getLongExtra(
1315 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1316 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001317 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001318 }
1319 }
1320 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001321 case VOICE_RESULT:
1322 if (resultCode == Activity.RESULT_OK && intent != null) {
1323 ArrayList<String> results = intent.getStringArrayListExtra(
1324 RecognizerIntent.EXTRA_RESULTS);
1325 if (results.size() >= 1) {
1326 mVoiceResult = results.get(0);
1327 }
1328 }
1329 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001330 case MY_NAVIGATION:
1331 if (intent == null || resultCode != Activity.RESULT_OK) {
1332 break;
1333 }
1334
1335 if (intent.getBooleanExtra("need_refresh", false) &&
1336 getCurrentTopWebView() != null) {
1337 getCurrentTopWebView().reload();
1338 }
1339 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001340 default:
1341 break;
1342 }
1343 getCurrentTopWebView().requestFocus();
1344 }
1345
1346 /**
1347 * Open the Go page.
1348 * @param startWithHistory If true, open starting on the history tab.
1349 * Otherwise, start with the bookmarks tab.
1350 */
1351 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001352 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001353 if (mTabControl.getCurrentWebView() == null) {
1354 return;
1355 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001356 // clear action mode
1357 if (isInCustomActionMode()) {
1358 endActionMode();
1359 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 Bundle extras = new Bundle();
1361 // Disable opening in a new window if we have maxed out the windows
1362 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1363 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001364 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001365 }
1366
1367 // combo view callbacks
1368
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 // key handling
1370 protected void onBackKey() {
1371 if (!mUi.onBackKey()) {
1372 WebView subwindow = mTabControl.getCurrentSubWindow();
1373 if (subwindow != null) {
1374 if (subwindow.canGoBack()) {
1375 subwindow.goBack();
1376 } else {
1377 dismissSubWindow(mTabControl.getCurrentTab());
1378 }
1379 } else {
1380 goBackOnePageOrQuit();
1381 }
1382 }
1383 }
1384
Michael Kolb4bd767d2011-05-27 11:33:55 -07001385 protected boolean onMenuKey() {
1386 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001387 }
1388
Michael Kolb8233fac2010-10-26 16:08:53 -07001389 // menu handling and state
1390 // TODO: maybe put into separate handler
1391
John Reck9c35b9c2012-05-30 10:08:50 -07001392 @Override
1393 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001394 if (mMenuState == EMPTY_MENU) {
1395 return false;
1396 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001397 MenuInflater inflater = mActivity.getMenuInflater();
1398 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001399 return true;
1400 }
1401
John Reck9c35b9c2012-05-30 10:08:50 -07001402 @Override
1403 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001404 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001405 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 return;
1407 }
1408 if (!(v instanceof WebView)) {
1409 return;
1410 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001411 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001412 WebView.HitTestResult result = webview.getHitTestResult();
1413 if (result == null) {
1414 return;
1415 }
1416
1417 int type = result.getType();
1418 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1419 Log.w(LOGTAG,
1420 "We should not show context menu when nothing is touched");
1421 return;
1422 }
1423 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1424 // let TextView handles context menu
1425 return;
1426 }
1427
1428 // Note, http://b/issue?id=1106666 is requesting that
1429 // an inflated menu can be used again. This is not available
1430 // yet, so inflate each time (yuk!)
1431 MenuInflater inflater = mActivity.getMenuInflater();
1432 inflater.inflate(R.menu.browsercontext, menu);
1433
1434 // Show the correct menu group
1435 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001436 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001437 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001438 menu.setGroupVisible(R.id.PHONE_MENU,
1439 type == WebView.HitTestResult.PHONE_TYPE);
1440 menu.setGroupVisible(R.id.EMAIL_MENU,
1441 type == WebView.HitTestResult.EMAIL_TYPE);
1442 menu.setGroupVisible(R.id.GEO_MENU,
1443 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001444 String itemUrl = null;
1445 String url = webview.getOriginalUrl();
1446 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1447 itemUrl = Uri.decode(navigationUrl);
1448 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1449 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1450 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1451 } else {
1452 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1453 }
1454 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1455 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1456 } else {
1457 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1458
1459 menu.setGroupVisible(R.id.IMAGE_MENU,
1460 type == WebView.HitTestResult.IMAGE_TYPE
1461 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1462 menu.setGroupVisible(R.id.ANCHOR_MENU,
1463 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1464 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001465 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1466 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001467 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001468 // Setup custom handling depending on the type
1469 switch (type) {
1470 case WebView.HitTestResult.PHONE_TYPE:
1471 menu.setHeaderTitle(Uri.decode(extra));
1472 menu.findItem(R.id.dial_context_menu_id).setIntent(
1473 new Intent(Intent.ACTION_VIEW, Uri
1474 .parse(WebView.SCHEME_TEL + extra)));
1475 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1476 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1477 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1478 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1479 addIntent);
1480 menu.findItem(R.id.copy_phone_context_menu_id)
1481 .setOnMenuItemClickListener(
1482 new Copy(extra));
1483 break;
1484
1485 case WebView.HitTestResult.EMAIL_TYPE:
1486 menu.setHeaderTitle(extra);
1487 menu.findItem(R.id.email_context_menu_id).setIntent(
1488 new Intent(Intent.ACTION_VIEW, Uri
1489 .parse(WebView.SCHEME_MAILTO + extra)));
1490 menu.findItem(R.id.copy_mail_context_menu_id)
1491 .setOnMenuItemClickListener(
1492 new Copy(extra));
1493 break;
1494
1495 case WebView.HitTestResult.GEO_TYPE:
1496 menu.setHeaderTitle(extra);
1497 menu.findItem(R.id.map_context_menu_id).setIntent(
1498 new Intent(Intent.ACTION_VIEW, Uri
1499 .parse(WebView.SCHEME_GEO
1500 + URLEncoder.encode(extra))));
1501 menu.findItem(R.id.copy_geo_context_menu_id)
1502 .setOnMenuItemClickListener(
1503 new Copy(extra));
1504 break;
1505
1506 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1507 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001508 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001509 // decide whether to show the open link in new tab option
1510 boolean showNewTab = mTabControl.canCreateNewTab();
1511 MenuItem newTabItem
1512 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001513 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001514 ? R.string.contextmenu_openlink_newwindow_background
1515 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001516 newTabItem.setVisible(showNewTab);
1517 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001518 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1519 newTabItem.setOnMenuItemClickListener(
1520 new MenuItem.OnMenuItemClickListener() {
1521 @Override
1522 public boolean onMenuItemClick(MenuItem item) {
1523 final HashMap<String, WebView> hrefMap =
1524 new HashMap<String, WebView>();
1525 hrefMap.put("webview", webview);
1526 final Message msg = mHandler.obtainMessage(
1527 FOCUS_NODE_HREF,
1528 R.id.open_newtab_context_menu_id,
1529 0, hrefMap);
1530 webview.requestFocusNodeHref(msg);
1531 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001532 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001533 });
1534 } else {
1535 newTabItem.setOnMenuItemClickListener(
1536 new MenuItem.OnMenuItemClickListener() {
1537 @Override
1538 public boolean onMenuItemClick(MenuItem item) {
1539 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001540 openTab(extra, parent,
1541 !mSettings.openInBackground(),
1542 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001543 return true;
1544 }
1545 });
1546 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001547 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001548 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1549 menu.setHeaderTitle(navigationUrl);
1550 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1551
1552 if (itemUrl != null) {
1553 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1554 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1555 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1556 @Override
1557 public boolean onMenuItemClick(MenuItem item) {
1558 final Intent intent = new Intent(Controller.this
1559 .getContext(),
1560 AddMyNavigationPage.class);
1561 Bundle bundle = new Bundle();
1562 String url = Uri.decode(navigationUrl);
1563 bundle.putBoolean("isAdding", false);
1564 bundle.putString("url", url);
1565 bundle.putString("name", getNameFromUrl(url));
1566 intent.putExtra("websites", bundle);
1567 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1568 return false;
1569 }
1570 });
1571 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1572 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1573 @Override
1574 public boolean onMenuItemClick(MenuItem item) {
1575 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1576 return false;
1577 }
1578 });
1579 }
1580 } else {
1581 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1582 }
1583 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001584 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1585 break;
1586 }
1587 // otherwise fall through to handle image part
1588 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001589 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1590 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001591 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1592 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001593 shareItem.setOnMenuItemClickListener(
1594 new MenuItem.OnMenuItemClickListener() {
1595 @Override
1596 public boolean onMenuItemClick(MenuItem item) {
1597 sharePage(mActivity, null, extra, null,
1598 null);
1599 return true;
1600 }
1601 }
1602 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001604 menu.findItem(R.id.view_image_context_menu_id)
1605 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1606 @Override
1607 public boolean onMenuItemClick(MenuItem item) {
1608 openTab(extra, mTabControl.getCurrentTab(), true, true);
1609 return false;
1610 }
1611 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001612 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1613 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1614 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001615 menu.findItem(R.id.set_wallpaper_context_menu_id).
1616 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1617 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 break;
1619
1620 default:
1621 Log.w(LOGTAG, "We should not get here.");
1622 break;
1623 }
1624 //update the ui
1625 mUi.onContextMenuCreated(menu);
1626 }
1627
kaiyiz6e5b3e02013-08-19 20:02:01 +08001628 public void startAddMyNavigation(String url) {
1629 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1630 Bundle bundle = new Bundle();
1631 bundle.putBoolean("isAdding", true);
1632 bundle.putString("url", url);
1633 bundle.putString("name", getNameFromUrl(url));
1634 intent.putExtra("websites", bundle);
1635 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1636 }
1637
1638 private void showMyNavigationDeleteDialog(final String itemUrl) {
1639 new AlertDialog.Builder(this.getContext())
1640 .setTitle(R.string.my_navigation_delete_label)
1641 .setIcon(android.R.drawable.ic_dialog_alert)
1642 .setMessage(R.string.my_navigation_delete_msg)
1643 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1644 @Override
1645 public void onClick(DialogInterface dialog, int whichButton) {
1646 deleteMyNavigationItem(itemUrl);
1647 }
1648 })
1649 .setNegativeButton(R.string.cancel, null)
1650 .show();
1651 }
1652
1653 private void deleteMyNavigationItem(final String itemUrl) {
1654 ContentResolver cr = this.getContext().getContentResolver();
1655 Cursor cursor = null;
1656
1657 try {
1658 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1659 new String[] {
1660 MyNavigationUtil.ID
1661 }, "url = ?", new String[] {
1662 itemUrl
1663 }, null);
1664 if (null != cursor && cursor.moveToFirst()) {
1665 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1666 cursor.getLong(0));
1667
1668 ContentValues values = new ContentValues();
1669 values.put(MyNavigationUtil.TITLE, "");
1670 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1671 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1672 ByteArrayOutputStream os = new ByteArrayOutputStream();
1673 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1674 R.raw.my_navigation_add);
1675 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1676 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1677 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1678 cr.update(uri, values, null, null);
1679 } else {
1680 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1681 }
1682 } catch (IllegalStateException e) {
1683 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1684 } finally {
1685 if (null != cursor) {
1686 cursor.close();
1687 }
1688 }
1689
1690 if (getCurrentTopWebView() != null) {
1691 getCurrentTopWebView().reload();
1692 }
1693 }
1694
1695 private String getNameFromUrl(String itemUrl) {
1696 ContentResolver cr = this.getContext().getContentResolver();
1697 Cursor cursor = null;
1698 String name = null;
1699
1700 try {
1701 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1702 new String[] {
1703 MyNavigationUtil.TITLE
1704 }, "url = ?", new String[] {
1705 itemUrl
1706 }, null);
1707 if (null != cursor && cursor.moveToFirst()) {
1708 name = cursor.getString(0);
1709 } else {
1710 Log.e(LOGTAG, "this item does not exist!");
1711 }
1712 } catch (IllegalStateException e) {
1713 Log.e(LOGTAG, "getNameFromUrl", e);
1714 } finally {
1715 if (null != cursor) {
1716 cursor.close();
1717 }
1718 }
1719 return name;
1720 }
1721
1722 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1723 int width = mActivity.getResources().getDimensionPixelOffset(
1724 R.dimen.myNavigationThumbnailWidth);
1725 int height = mActivity.getResources().getDimensionPixelOffset(
1726 R.dimen.myNavigationThumbnailHeight);
1727
1728 final Bitmap bm = createScreenshot(webView, width, height);
1729
1730 if (bm == null) {
1731 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1732 return;
1733 }
1734
1735 final ContentResolver cr = mActivity.getContentResolver();
1736 new AsyncTask<Void, Void, Void>() {
1737 @Override
1738 protected Void doInBackground(Void... unused) {
1739 ContentResolver cr = mActivity.getContentResolver();
1740 Cursor cursor = null;
1741 try {
1742 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1743 new String[] {
1744 MyNavigationUtil.ID
1745 }, "url = ?", new String[] {
1746 itemUrl
1747 }, null);
1748 if (null != cursor && cursor.moveToFirst()) {
1749 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1750 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1751
1752 ContentValues values = new ContentValues();
1753 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1754 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1755 cursor.getLong(0));
1756 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1757 cr.update(uri, values, null, null);
1758 os.close();
1759 }
1760 } catch (IllegalStateException e) {
1761 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1762 } catch (IOException e) {
1763 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1764 } finally {
1765 if (null != cursor) {
1766 cursor.close();
1767 }
1768 }
1769 return null;
1770 }
1771 }.execute();
1772 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001773 /**
1774 * As the menu can be open when loading state changes
1775 * we must manually update the state of the stop/reload menu
1776 * item
1777 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001778 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001779 if (menu == null) {
1780 return;
1781 }
1782 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001783 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001784 menu.findItem(R.id.stop_menu_id):
1785 menu.findItem(R.id.reload_menu_id);
1786 if (src != null) {
1787 dest.setIcon(src.getIcon());
1788 dest.setTitle(src.getTitle());
1789 }
1790 }
1791
John Reck9c35b9c2012-05-30 10:08:50 -07001792 @Override
1793 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001794 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001795 // hold on to the menu reference here; it is used by the page callbacks
1796 // to update the menu based on loading state
1797 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001798 // Note: setVisible will decide whether an item is visible; while
1799 // setEnabled() will decide whether an item is enabled, which also means
1800 // whether the matching shortcut key will function.
1801 switch (mMenuState) {
1802 case EMPTY_MENU:
1803 if (mCurrentMenuState != mMenuState) {
1804 menu.setGroupVisible(R.id.MAIN_MENU, false);
1805 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1806 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1807 }
1808 break;
1809 default:
1810 if (mCurrentMenuState != mMenuState) {
1811 menu.setGroupVisible(R.id.MAIN_MENU, true);
1812 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1813 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1814 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001815 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001816 break;
1817 }
1818 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001819 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001820 }
1821
Michael Kolb4bf79712011-07-14 14:18:12 -07001822 @Override
1823 public void updateMenuState(Tab tab, Menu menu) {
1824 boolean canGoBack = false;
1825 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001826 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001827 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001828 if (tab != null) {
1829 canGoBack = tab.canGoBack();
1830 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001831 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001832 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001833 }
1834 final MenuItem back = menu.findItem(R.id.back_menu_id);
1835 back.setEnabled(canGoBack);
1836
1837 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001838
1839 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1840 forward.setEnabled(canGoForward);
1841
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001842 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1843 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001844 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001845 if (source != null && dest != null) {
1846 dest.setTitle(source.getTitle());
1847 dest.setIcon(source.getIcon());
1848 }
John Recke1a03a32011-09-14 17:04:16 -07001849 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001850
1851 // decide whether to show the share link option
1852 PackageManager pm = mActivity.getPackageManager();
1853 Intent send = new Intent(Intent.ACTION_SEND);
1854 send.setType("text/plain");
1855 ResolveInfo ri = pm.resolveActivity(send,
1856 PackageManager.MATCH_DEFAULT_ONLY);
1857 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1858
1859 boolean isNavDump = mSettings.enableNavDump();
1860 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1861 nav.setVisible(isNavDump);
1862 nav.setEnabled(isNavDump);
1863
1864 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001865 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1866 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001867 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1868 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001869 // history and snapshots item are the members of COMBO menu group,
1870 // so if show history item, only make snapshots item invisible.
1871 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001872
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001873 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001874 }
1875
John Reck9c35b9c2012-05-30 10:08:50 -07001876 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001877 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001878 if (null == getCurrentTopWebView()) {
1879 return false;
1880 }
1881 if (mMenuIsDown) {
1882 // The shortcut action consumes the MENU. Even if it is still down,
1883 // it won't trigger the next shortcut action. In the case of the
1884 // shortcut action triggering a new activity, like Bookmarks, we
1885 // won't get onKeyUp for MENU. So it is important to reset it here.
1886 mMenuIsDown = false;
1887 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001888 if (mUi.onOptionsItemSelected(item)) {
1889 // ui callback handled it
1890 return true;
1891 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001892 switch (item.getItemId()) {
1893 // -- Main menu
1894 case R.id.new_tab_menu_id:
1895 openTabToHomePage();
1896 break;
1897
1898 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001899 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001900 break;
1901
Afzal Najamd4e33312012-04-26 01:54:01 -04001902 case R.id.close_other_tabs_id:
1903 closeOtherTabs();
1904 break;
1905
Michael Kolb8233fac2010-10-26 16:08:53 -07001906 case R.id.goto_menu_id:
1907 editUrl();
1908 break;
1909
1910 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001911 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1912 break;
1913
1914 case R.id.history_menu_id:
1915 bookmarksOrHistoryPicker(ComboViews.History);
1916 break;
1917
1918 case R.id.snapshots_menu_id:
1919 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001920 break;
1921
Michael Kolb8233fac2010-10-26 16:08:53 -07001922 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001923 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001924 break;
1925
1926 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001927 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001928 stopLoading();
1929 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001930 Tab currentTab = mTabControl.getCurrentTab();
1931 if (currentTab.hasCrashed) {
1932 currentTab.replaceCrashView(getCurrentTopWebView(),
1933 currentTab.getViewContainer());
1934 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001935 getCurrentTopWebView().reload();
1936 }
1937 break;
1938
1939 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001940 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001941 break;
1942
1943 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001944 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001945 break;
1946
1947 case R.id.close_menu_id:
1948 // Close the subwindow if it exists.
1949 if (mTabControl.getCurrentSubWindow() != null) {
1950 dismissSubWindow(mTabControl.getCurrentTab());
1951 break;
1952 }
1953 closeCurrentTab();
1954 break;
1955
kaiyiza8b6dbb2013-07-29 18:11:22 +08001956 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001957 Object[] params = { new String("persist.debug.browsermonkeytest")};
1958 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001959 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001960 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001961 if (ret != null && ret.equals("enable"))
1962 break;
luxiaolb40014b2013-07-19 10:01:43 +08001963 showExitDialog(mActivity);
1964 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001965 case R.id.homepage_menu_id:
1966 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001967 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001968 break;
1969
1970 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001971 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001972 break;
1973
1974 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001975 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001976 break;
1977
John Reck2bc80422011-06-30 15:11:49 -07001978 case R.id.save_snapshot_menu_id:
1979 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001980 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001981 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001982 break;
1983
Michael Kolb8233fac2010-10-26 16:08:53 -07001984 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001985 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001986 break;
1987
John Recke1a03a32011-09-14 17:04:16 -07001988 case R.id.snapshot_go_live:
1989 goLive();
1990 return true;
1991
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 case R.id.share_page_menu_id:
1993 Tab currentTab = mTabControl.getCurrentTab();
1994 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001995 return false;
1996 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001997 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001998 break;
1999
2000 case R.id.dump_nav_menu_id:
2001 getCurrentTopWebView().debugDump();
2002 break;
2003
Michael Kolb8233fac2010-10-26 16:08:53 -07002004 case R.id.zoom_in_menu_id:
2005 getCurrentTopWebView().zoomIn();
2006 break;
2007
2008 case R.id.zoom_out_menu_id:
2009 getCurrentTopWebView().zoomOut();
2010 break;
2011
2012 case R.id.view_downloads_menu_id:
2013 viewDownloads();
2014 break;
2015
John Reck42229bc2011-08-19 13:26:43 -07002016 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002017 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002018 break;
2019
Michael Kolb8233fac2010-10-26 16:08:53 -07002020 case R.id.window_one_menu_id:
2021 case R.id.window_two_menu_id:
2022 case R.id.window_three_menu_id:
2023 case R.id.window_four_menu_id:
2024 case R.id.window_five_menu_id:
2025 case R.id.window_six_menu_id:
2026 case R.id.window_seven_menu_id:
2027 case R.id.window_eight_menu_id:
2028 {
2029 int menuid = item.getItemId();
2030 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2031 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2032 Tab desiredTab = mTabControl.getTab(id);
2033 if (desiredTab != null &&
2034 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002035 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002036 }
2037 break;
2038 }
2039 }
2040 }
2041 break;
2042
2043 default:
2044 return false;
2045 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002046 return true;
2047 }
2048
John Reck68234a92012-04-19 15:27:12 -07002049 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2050 implements OnCancelListener {
2051
2052 private Tab mTab;
2053 private Dialog mProgressDialog;
2054 private ContentValues mValues;
2055
2056 private SaveSnapshotTask(Tab tab) {
2057 mTab = tab;
2058 }
2059
2060 @Override
2061 protected void onPreExecute() {
2062 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2063 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2064 true, true, this);
2065 mValues = mTab.createSnapshotValues();
2066 }
2067
2068 @Override
2069 protected Long doInBackground(Void... params) {
2070 if (!mTab.saveViewState(mValues)) {
2071 return null;
2072 }
2073 if (isCancelled()) {
2074 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2075 File file = mActivity.getFileStreamPath(path);
2076 if (!file.delete()) {
2077 file.deleteOnExit();
2078 }
2079 return null;
2080 }
2081 final ContentResolver cr = mActivity.getContentResolver();
2082 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2083 if (result == null) {
2084 return null;
2085 }
2086 long id = ContentUris.parseId(result);
2087 return id;
2088 }
2089
2090 @Override
2091 protected void onPostExecute(Long id) {
2092 if (isCancelled()) {
2093 return;
2094 }
2095 mProgressDialog.dismiss();
2096 if (id == null) {
2097 Toast.makeText(mActivity, R.string.snapshot_failed,
2098 Toast.LENGTH_SHORT).show();
2099 return;
2100 }
2101 Bundle b = new Bundle();
2102 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2103 mUi.showComboView(ComboViews.Snapshots, b);
2104 }
2105
2106 @Override
2107 public void onCancel(DialogInterface dialog) {
2108 cancel(true);
2109 }
2110 }
2111
Michael Kolb80f75082012-04-10 10:50:06 -07002112 @Override
2113 public void toggleUserAgent() {
2114 WebView web = getCurrentWebView();
2115 mSettings.toggleDesktopUseragent(web);
2116 web.loadUrl(web.getOriginalUrl());
2117 }
2118
2119 @Override
2120 public void findOnPage() {
2121 getCurrentTopWebView().showFindDialog(null, true);
2122 }
2123
2124 @Override
2125 public void openPreferences() {
2126 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2127 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2128 getCurrentTopWebView().getUrl());
2129 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2130 }
2131
2132 @Override
2133 public void bookmarkCurrentPage() {
2134 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2135 if (bookmarkIntent != null) {
2136 mActivity.startActivity(bookmarkIntent);
2137 }
2138 }
2139
John Recke1a03a32011-09-14 17:04:16 -07002140 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002141 SnapshotTab t = (SnapshotTab) getCurrentTab();
2142 t.loadUrl(t.getLiveUrl(), null);
John Recke1a03a32011-09-14 17:04:16 -07002143 }
2144
luxiaolb40014b2013-07-19 10:01:43 +08002145 private void showExitDialog(final Activity activity) {
2146 new AlertDialog.Builder(activity)
2147 .setTitle(R.string.exit_browser_title)
2148 .setIcon(android.R.drawable.ic_dialog_alert)
2149 .setMessage(R.string.exit_browser_msg)
2150 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2151 public void onClick(DialogInterface dialog, int which) {
2152 activity.moveTaskToBack(true);
2153 dialog.dismiss();
2154 }
2155 })
2156 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2157 public void onClick(DialogInterface dialog, int which) {
2158 activity.finish();
2159 mHandler.postDelayed(new Runnable() {
2160 @Override
2161 public void run() {
2162 // TODO Auto-generated method stub
2163 mCrashRecoveryHandler.clearState(true);
2164 int pid = android.os.Process.myPid();
2165 android.os.Process.killProcess(pid);
2166 }
2167 }, 300);
2168 dialog.dismiss();
2169 }
2170 })
2171 .show();
2172 }
Michael Kolb315d5022011-10-13 12:47:11 -07002173 @Override
2174 public void showPageInfo() {
2175 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2176 }
2177
John Reck9c35b9c2012-05-30 10:08:50 -07002178 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002179 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002180 // Let the History and Bookmark fragments handle menus they created.
2181 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2182 return false;
2183 }
2184
Michael Kolb8233fac2010-10-26 16:08:53 -07002185 int id = item.getItemId();
2186 boolean result = true;
2187 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002188 // -- Browser context menu
2189 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002190 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002191 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002192 case R.id.copy_link_context_menu_id:
2193 final WebView webView = getCurrentTopWebView();
2194 if (null == webView) {
2195 result = false;
2196 break;
2197 }
2198 final HashMap<String, WebView> hrefMap =
2199 new HashMap<String, WebView>();
2200 hrefMap.put("webview", webView);
2201 final Message msg = mHandler.obtainMessage(
2202 FOCUS_NODE_HREF, id, 0, hrefMap);
2203 webView.requestFocusNodeHref(msg);
2204 break;
2205
2206 default:
2207 // For other context menus
2208 result = onOptionsItemSelected(item);
2209 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002210 return result;
2211 }
2212
2213 /**
2214 * support programmatically opening the context menu
2215 */
2216 public void openContextMenu(View view) {
2217 mActivity.openContextMenu(view);
2218 }
2219
2220 /**
2221 * programmatically open the options menu
2222 */
2223 public void openOptionsMenu() {
2224 mActivity.openOptionsMenu();
2225 }
2226
John Reck9c35b9c2012-05-30 10:08:50 -07002227 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002228 public boolean onMenuOpened(int featureId, Menu menu) {
2229 if (mOptionsMenuOpen) {
2230 if (mConfigChanged) {
2231 // We do not need to make any changes to the state of the
2232 // title bar, since the only thing that happened was a
2233 // change in orientation
2234 mConfigChanged = false;
2235 } else {
2236 if (!mExtendedMenuOpen) {
2237 mExtendedMenuOpen = true;
2238 mUi.onExtendedMenuOpened();
2239 } else {
2240 // Switching the menu back to icon view, so show the
2241 // title bar once again.
2242 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002243 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002244 }
2245 }
2246 } else {
2247 // The options menu is closed, so open it, and show the title
2248 mOptionsMenuOpen = true;
2249 mConfigChanged = false;
2250 mExtendedMenuOpen = false;
2251 mUi.onOptionsMenuOpened();
2252 }
2253 return true;
2254 }
2255
John Reck9c35b9c2012-05-30 10:08:50 -07002256 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002257 public void onOptionsMenuClosed(Menu menu) {
2258 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002259 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002260 }
2261
John Reck9c35b9c2012-05-30 10:08:50 -07002262 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002263 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002264 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002265 }
2266
2267 // Helper method for getting the top window.
2268 @Override
2269 public WebView getCurrentTopWebView() {
2270 return mTabControl.getCurrentTopWebView();
2271 }
2272
2273 @Override
2274 public WebView getCurrentWebView() {
2275 return mTabControl.getCurrentWebView();
2276 }
2277
2278 /*
2279 * This method is called as a result of the user selecting the options
2280 * menu to see the download window. It shows the download window on top of
2281 * the current window.
2282 */
2283 void viewDownloads() {
2284 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2285 mActivity.startActivity(intent);
2286 }
2287
John Reck30b065e2011-07-19 10:58:05 -07002288 int getActionModeHeight() {
2289 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2290 new int[] { android.R.attr.actionBarSize });
2291 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2292 actionBarSizeTypedArray.recycle();
2293 return size;
2294 }
2295
Michael Kolb8233fac2010-10-26 16:08:53 -07002296 // action mode
2297
John Reck9c35b9c2012-05-30 10:08:50 -07002298 @Override
2299 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002300 mUi.onActionModeStarted(mode);
2301 mActionMode = mode;
2302 }
2303
2304 /*
2305 * True if a custom ActionMode (i.e. find or select) is in use.
2306 */
2307 @Override
2308 public boolean isInCustomActionMode() {
2309 return mActionMode != null;
2310 }
2311
2312 /*
2313 * End the current ActionMode.
2314 */
2315 @Override
2316 public void endActionMode() {
2317 if (mActionMode != null) {
2318 mActionMode.finish();
2319 }
2320 }
2321
2322 /*
2323 * Called by find and select when they are finished. Replace title bars
2324 * as necessary.
2325 */
John Reck9c35b9c2012-05-30 10:08:50 -07002326 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002327 public void onActionModeFinished(ActionMode mode) {
2328 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002329 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002330 mActionMode = null;
2331 }
2332
2333 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002334 final Tab tab = getCurrentTab();
2335 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002336 }
2337
2338 // bookmark handling
2339
2340 /**
2341 * add the current page as a bookmark to the given folder id
2342 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002343 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002344 * bookmarked, and if it is, edit that bookmark. If false, and
2345 * the site is already bookmarked, do not attempt to edit the
2346 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002347 */
2348 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002349 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002350 WebView w = getCurrentTopWebView();
2351 if (w == null) {
2352 return null;
2353 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002354 Intent i = new Intent(mActivity,
2355 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002356 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2357 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2358 String touchIconUrl = w.getTouchIconUrl();
2359 if (touchIconUrl != null) {
2360 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2361 WebSettings settings = w.getSettings();
2362 if (settings != null) {
2363 i.putExtra(AddBookmarkPage.USER_AGENT,
2364 settings.getUserAgentString());
2365 }
2366 }
2367 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2368 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2369 getDesiredThumbnailHeight(mActivity)));
2370 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002371 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002372 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2373 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002374 // Put the dialog at the upper right of the screen, covering the
2375 // star on the title bar.
2376 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002377 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002378 }
2379
2380 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002381 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002382 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002383 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002384 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002385 }
2386
Vivek Sekharb54614f2014-05-01 19:03:37 -07002387 @Override
2388 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2389 boolean capture) {
2390 mUploadHandler = new UploadHandler(this);
2391 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2392 }
2393
Michael Kolb8233fac2010-10-26 16:08:53 -07002394 // thumbnails
2395
2396 /**
2397 * Return the desired width for thumbnail screenshots, which are stored in
2398 * the database, and used on the bookmarks screen.
2399 * @param context Context for finding out the density of the screen.
2400 * @return desired width for thumbnail screenshot.
2401 */
2402 static int getDesiredThumbnailWidth(Context context) {
2403 return context.getResources().getDimensionPixelOffset(
2404 R.dimen.bookmarkThumbnailWidth);
2405 }
2406
2407 /**
2408 * Return the desired height for thumbnail screenshots, which are stored in
2409 * the database, and used on the bookmarks screen.
2410 * @param context Context for finding out the density of the screen.
2411 * @return desired height for thumbnail screenshot.
2412 */
2413 static int getDesiredThumbnailHeight(Context context) {
2414 return context.getResources().getDimensionPixelOffset(
2415 R.dimen.bookmarkThumbnailHeight);
2416 }
2417
John Reck8cc92352011-07-06 17:41:52 -07002418 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002419 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002420 return null;
2421 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002422
2423 Bitmap viewportBitmap = view.getViewportBitmap();
2424 if (viewportBitmap == null) {
2425 return null;
2426 }
2427
2428 float aspectRatio = (float) width/height;
2429 int viewportWidth = viewportBitmap.getWidth();
2430 int viewportHeight = viewportBitmap.getHeight();
2431
2432 //modify the size to attain the same aspect ratio of desired thumbnail size
2433 if (viewportHeight > viewportWidth) {
2434 viewportHeight= (int)Math.round(viewportWidth * aspectRatio);
2435 } else {
2436 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2437 }
2438
2439 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2440 Rect dstRect = new Rect(0, 0, width, height);
2441
2442 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2443 || sThumbnailBitmap.getHeight() != height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002444 if (sThumbnailBitmap != null) {
2445 sThumbnailBitmap.recycle();
2446 sThumbnailBitmap = null;
2447 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002448
2449 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002450 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002451
John Reckd7dd9b22011-08-30 09:18:29 -07002452 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002453 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
John Reckd7dd9b22011-08-30 09:18:29 -07002454
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002455 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002456 }
2457
John Reck34ef2672011-02-10 11:30:55 -08002458 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002459 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002460 // FIXME: Would like to make sure there is actually something to
2461 // draw, but the API for that (WebViewCore.pictureReady()) is not
2462 // currently accessible here.
2463
John Reck34ef2672011-02-10 11:30:55 -08002464 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002465 if (view == null) {
2466 // Tab was destroyed
2467 return;
2468 }
John Reck34ef2672011-02-10 11:30:55 -08002469 final String url = tab.getUrl();
2470 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002471 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002472 if (TextUtils.isEmpty(url)) {
2473 return;
2474 }
2475
kaiyiz6e5b3e02013-08-19 20:02:01 +08002476 //update My Navigation Thumbnails
2477 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2478 if (isMyNavigationUrl) {
2479 updateMyNavigationThumbnail(url, view);
2480 }
John Reck34ef2672011-02-10 11:30:55 -08002481 // Only update thumbnails for web urls (http(s)://), not for
2482 // about:, javascript:, data:, etc...
2483 // Unless it is a bookmarked site, then always update
2484 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2485 return;
2486 }
2487
kaiyiz6e5b3e02013-08-19 20:02:01 +08002488 if (url != null && mUpdateMyNavThumbnailUrl != null
2489 && Patterns.WEB_URL.matcher(url).matches()
2490 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2491 String urlHost = (new WebAddress(url)).getHost();
2492 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2493 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2494 || bookmarkHost.length() == 0) {
2495 return;
2496 }
2497 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2498 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2499 bookmarkHost.length());
2500 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2501 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2502 if (!bookmarkDomain.equals(urlDomain)) {
2503 return;
2504 }
2505 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002506 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2507 getDesiredThumbnailHeight(mActivity));
2508 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002509 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2510 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2511 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2512 500);
2513 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002514 return;
2515 }
2516
2517 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002518 new AsyncTask<Void, Void, Void>() {
2519 @Override
2520 protected Void doInBackground(Void... unused) {
2521 Cursor cursor = null;
2522 try {
2523 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002524 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2525 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2526 : url);
2527 if (mUpdateMyNavThumbnail) {
2528 mUpdateMyNavThumbnail = false;
2529 mUpdateMyNavThumbnailUrl = null;
2530 }
John Reck34ef2672011-02-10 11:30:55 -08002531 if (cursor != null && cursor.moveToFirst()) {
2532 final ByteArrayOutputStream os =
2533 new ByteArrayOutputStream();
2534 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002535
John Reck34ef2672011-02-10 11:30:55 -08002536 ContentValues values = new ContentValues();
2537 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002538
John Reck34ef2672011-02-10 11:30:55 -08002539 do {
John Reck617fd832011-02-16 14:35:59 -08002540 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002541 cr.update(Images.CONTENT_URI, values, null, null);
2542 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002543 }
John Reck34ef2672011-02-10 11:30:55 -08002544 } catch (IllegalStateException e) {
2545 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002546 } catch (SQLiteException s) {
2547 // Added for possible error when user tries to remove the same bookmark
2548 // that is being updated with a screen shot
2549 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002550 } finally {
2551 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002552 }
John Reck34ef2672011-02-10 11:30:55 -08002553 return null;
2554 }
2555 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002556 }
2557
2558 private class Copy implements OnMenuItemClickListener {
2559 private CharSequence mText;
2560
John Reck9c35b9c2012-05-30 10:08:50 -07002561 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002562 public boolean onMenuItemClick(MenuItem item) {
2563 copy(mText);
2564 return true;
2565 }
2566
2567 public Copy(CharSequence toCopy) {
2568 mText = toCopy;
2569 }
2570 }
2571
Leon Scroggins63c02662010-11-18 15:16:27 -05002572 private static class Download implements OnMenuItemClickListener {
2573 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002574 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002575 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002576 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002577 private static final String FALLBACK_EXTENSION = "dat";
2578 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002579
John Reck9c35b9c2012-05-30 10:08:50 -07002580 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002581 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002582 if (DataUri.isDataUri(mText)) {
2583 saveDataUri();
2584 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002585 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002586 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002587 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 return true;
2589 }
2590
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002591 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2592 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002593 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002594 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002595 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002596 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002597 }
George Mount387d45d2011-10-07 15:57:53 -07002598
2599 /**
2600 * Treats mText as a data URI and writes its contents to a file
2601 * based on the current time.
2602 */
2603 private void saveDataUri() {
2604 FileOutputStream outputStream = null;
2605 try {
2606 DataUri uri = new DataUri(mText);
2607 File target = getTarget(uri);
2608 outputStream = new FileOutputStream(target);
2609 outputStream.write(uri.getData());
2610 final DownloadManager manager =
2611 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2612 manager.addCompletedDownload(target.getName(),
2613 mActivity.getTitle().toString(), false,
2614 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002615 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002616 } catch (IOException e) {
2617 Log.e(LOGTAG, "Could not save data URL");
2618 } finally {
2619 if (outputStream != null) {
2620 try {
2621 outputStream.close();
2622 } catch (IOException e) {
2623 // ignore close errors
2624 }
2625 }
2626 }
2627 }
2628
2629 /**
2630 * Creates a File based on the current time stamp and uses
2631 * the mime type of the DataUri to get the extension.
2632 */
2633 private File getTarget(DataUri uri) throws IOException {
2634 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002635 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002636 String nameBase = format.format(new Date());
2637 String mimeType = uri.getMimeType();
2638 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2639 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2640 if (extension == null) {
2641 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2642 extension = FALLBACK_EXTENSION;
2643 }
2644 extension = "." + extension; // createTempFile needs the '.'
2645 File targetFile = File.createTempFile(nameBase, extension, dir);
2646 return targetFile;
2647 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002648 }
2649
Cary Clark8974d282010-11-22 10:46:05 -05002650 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002651 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002652
John Reck9c35b9c2012-05-30 10:08:50 -07002653 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002654 public boolean onMenuItemClick(MenuItem item) {
2655 if (mWebView != null) {
2656 return mWebView.selectText();
2657 }
2658 return false;
2659 }
2660
2661 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002662 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002663 }
2664
2665 }
2666
Michael Kolb8233fac2010-10-26 16:08:53 -07002667 /********************** TODO: UI stuff *****************************/
2668
2669 // these methods have been copied, they still need to be cleaned up
2670
2671 /****************** tabs ***************************************************/
2672
2673 // basic tab interactions:
2674
2675 // it is assumed that tabcontrol already knows about the tab
2676 protected void addTab(Tab tab) {
2677 mUi.addTab(tab);
2678 }
2679
2680 protected void removeTab(Tab tab) {
2681 mUi.removeTab(tab);
2682 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002683 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002684 }
2685
Michael Kolbf2055602011-04-09 17:20:03 -07002686 @Override
2687 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002688 // monkey protection against delayed start
2689 if (tab != null) {
2690 mTabControl.setCurrentTab(tab);
2691 // the tab is guaranteed to have a webview after setCurrentTab
2692 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002693 tab.setTimeStamp();
Michael Kolbcd424e92011-02-24 10:26:26 -08002694 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002695 }
2696
John Reck8bcafc12011-08-29 16:43:02 -07002697 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002698 Tab current = mTabControl.getCurrentTab();
2699 if (current != null
2700 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002701 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002702 }
2703 }
2704
John Reck26b18322011-06-21 13:08:58 -07002705 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002706 // Dismiss the subwindow if applicable.
2707 dismissSubWindow(appTab);
2708 // Since we might kill the WebView, remove it from the
2709 // content view first.
2710 mUi.detachTab(appTab);
2711 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002712 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002713 // TODO: analyze why the remove and add are necessary
2714 mUi.attachTab(appTab);
2715 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002716 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002717 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002718 } else {
2719 // If the tab was the current tab, we have to attach
2720 // it to the view system again.
2721 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002722 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002723 }
2724 }
2725
2726 // Remove the sub window if it exists. Also called by TabControl when the
2727 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002728 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002729 public void dismissSubWindow(Tab tab) {
2730 removeSubWindow(tab);
2731 // dismiss the subwindow. This will destroy the WebView.
2732 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002733 WebView wv = getCurrentTopWebView();
2734 if (wv != null) {
2735 wv.requestFocus();
2736 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 }
2738
2739 @Override
2740 public void removeSubWindow(Tab t) {
2741 if (t.getSubWebView() != null) {
2742 mUi.removeSubWindow(t.getSubViewContainer());
2743 }
2744 }
2745
2746 @Override
2747 public void attachSubWindow(Tab tab) {
2748 if (tab.getSubWebView() != null) {
2749 mUi.attachSubWindow(tab.getSubViewContainer());
2750 getCurrentTopWebView().requestFocus();
2751 }
2752 }
2753
Mathew Inwood29721c22011-06-29 17:55:24 +01002754 private Tab showPreloadedTab(final UrlData urlData) {
2755 if (!urlData.isPreloaded()) {
2756 return null;
2757 }
2758 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2759 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2760 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002761 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002762 // Could not submit query. Fallback to regular tab creation
2763 tabControl.destroy();
2764 return null;
2765 }
2766 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002767 // check tab count and make room for new tab
2768 if (!mTabControl.canCreateNewTab()) {
2769 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2770 if (leastUsed != null) {
2771 closeTab(leastUsed);
2772 }
2773 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002774 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002775 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002776 mTabControl.addPreloadedTab(t);
2777 addTab(t);
2778 setActiveTab(t);
2779 return t;
2780 }
2781
Michael Kolb7bcafde2011-05-09 13:55:59 -07002782 // open a non inconito tab with the given url data
2783 // and set as active tab
2784 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002785 Tab tab = showPreloadedTab(urlData);
2786 if (tab == null) {
2787 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002788 if ((tab != null) && !urlData.isEmpty()) {
2789 loadUrlDataIn(tab, urlData);
2790 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002791 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002792 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002793 }
2794
Michael Kolb843510f2010-12-09 10:51:49 -08002795 @Override
2796 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002797 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002798 }
2799
Michael Kolb8233fac2010-10-26 16:08:53 -07002800 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002801 public Tab openIncognitoTab() {
2802 return openTab(INCOGNITO_URI, true, true, false);
2803 }
2804
2805 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002806 public Tab openTab(String url, boolean incognito, boolean setActive,
2807 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002808 return openTab(url, incognito, setActive, useCurrent, null);
2809 }
2810
2811 @Override
2812 public Tab openTab(String url, Tab parent, boolean setActive,
2813 boolean useCurrent) {
2814 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2815 setActive, useCurrent, parent);
2816 }
2817
2818 public Tab openTab(String url, boolean incognito, boolean setActive,
2819 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002820 Tab tab = createNewTab(incognito, setActive, useCurrent);
2821 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002822 if (parent != null && parent != tab) {
2823 parent.addChildTab(tab);
2824 }
Michael Kolb519d2282011-05-09 17:03:19 -07002825 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002826 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002827 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002828 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002829 return tab;
2830 }
2831
2832 // this method will attempt to create a new tab
2833 // incognito: private browsing tab
2834 // setActive: ste tab as current tab
2835 // useCurrent: if no new tab can be created, return current tab
2836 private Tab createNewTab(boolean incognito, boolean setActive,
2837 boolean useCurrent) {
2838 Tab tab = null;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002839 MemoryMonitor memMonitor = null;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002840 if (mTabControl.canCreateNewTab()) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002841 if (mSettings.enableMemoryMonitor()) {
2842 Log.d(LOGTAG, " Memory Monitor Enabled .");
2843 memMonitor = MemoryMonitor.getInstance(mActivity.getApplicationContext(),this);
2844 if (memMonitor != null) {
2845 //Remove webview associated with the oldest tab
2846 memMonitor.destroyLeastRecentlyActiveTab();
2847 }
2848 } else {
2849 Log.d(LOGTAG, " Memory Monitor disabled .");
2850 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002851 tab = mTabControl.createNewTab(incognito);
2852 addTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002853 tab.setTimeStamp();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002854 if (setActive) {
2855 setActiveTab(tab);
2856 }
2857 } else {
2858 if (useCurrent) {
2859 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002860 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002861 } else {
2862 mUi.showMaxTabsWarning();
2863 }
2864 }
2865 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002866 }
2867
John Reck2bc80422011-06-30 15:11:49 -07002868 @Override
2869 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2870 SnapshotTab tab = null;
2871 if (mTabControl.canCreateNewTab()) {
2872 tab = mTabControl.createSnapshotTab(snapshotId);
2873 addTab(tab);
2874 if (setActive) {
2875 setActiveTab(tab);
2876 }
2877 } else {
2878 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002879 }
2880 return tab;
2881 }
2882
Michael Kolb8233fac2010-10-26 16:08:53 -07002883 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002884 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002885 * @return boolean True if we successfully switched to a different tab. If
2886 * the indexth tab is null, or if that tab is the same as
2887 * the current one, return false.
2888 */
2889 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002890 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002891 Tab currentTab = mTabControl.getCurrentTab();
2892 if (tab == null || tab == currentTab) {
2893 return false;
2894 }
2895 setActiveTab(tab);
2896 return true;
2897 }
2898
2899 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002900 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002901 closeCurrentTab(false);
2902 }
2903
2904 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002905 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002906 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002907 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002908 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002909 return;
2910 }
Michael Kolbc831b632011-05-11 09:30:34 -07002911 final Tab current = mTabControl.getCurrentTab();
2912 final int pos = mTabControl.getCurrentPosition();
2913 Tab newTab = current.getParent();
2914 if (newTab == null) {
2915 newTab = mTabControl.getTab(pos + 1);
2916 if (newTab == null) {
2917 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002918 }
2919 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002920 if (andQuit) {
2921 mTabControl.setCurrentTab(newTab);
2922 closeTab(current);
2923 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002924 // Close window
2925 closeTab(current);
2926 }
2927 }
2928
2929 /**
2930 * Close the tab, remove its associated title bar, and adjust mTabControl's
2931 * current tab to a valid value.
2932 */
2933 @Override
2934 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002935 if (tab == mTabControl.getCurrentTab()) {
2936 closeCurrentTab();
2937 } else {
2938 removeTab(tab);
2939 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002940 }
2941
Afzal Najamd4e33312012-04-26 01:54:01 -04002942 /**
2943 * Close all tabs except the current one
2944 */
2945 @Override
2946 public void closeOtherTabs() {
2947 int inactiveTabs = mTabControl.getTabCount() - 1;
2948 for (int i = inactiveTabs; i >= 0; i--) {
2949 Tab tab = mTabControl.getTab(i);
2950 if (tab != mTabControl.getCurrentTab()) {
2951 removeTab(tab);
2952 }
2953 }
2954 }
2955
Michael Kolb8233fac2010-10-26 16:08:53 -07002956 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002957 protected void loadUrlFromContext(String url) {
2958 Tab tab = getCurrentTab();
2959 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002960 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002961 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002962 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002963 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002964 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002965 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002966 }
2967 }
2968 }
2969
2970 /**
2971 * Load the URL into the given WebView and update the title bar
2972 * to reflect the new load. Call this instead of WebView.loadUrl
2973 * directly.
2974 * @param view The WebView used to load url.
2975 * @param url The URL to load.
2976 */
John Reck71e51422011-07-01 16:49:28 -07002977 @Override
2978 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002979 loadUrl(tab, url, null);
2980 }
2981
2982 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2983 if (tab != null) {
2984 dismissSubWindow(tab);
2985 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002986 if (tab.hasCrashed) {
2987 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
2988 }
Michael Kolba53c9892011-10-05 13:31:40 -07002989 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002990 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002991 }
2992
2993 /**
2994 * Load UrlData into a Tab and update the title bar to reflect the new
2995 * load. Call this instead of UrlData.loadIn directly.
2996 * @param t The Tab used to load.
2997 * @param data The UrlData being loaded.
2998 */
2999 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003000 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003001 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003002 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003003 } else {
John Reck38b39652012-06-05 09:22:59 -07003004 if (t != null && data.mDisableUrlOverride) {
3005 t.disableUrlOverridingForLoad();
3006 }
John Reck26b18322011-06-21 13:08:58 -07003007 loadUrl(t, data.mUrl, data.mHeaders);
3008 }
3009 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003010 }
3011
John Reck30c714c2010-12-16 17:30:34 -08003012 @Override
3013 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003014 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003015 if (tab.canGoBack()) {
3016 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003017 } else {
John Reckef654f12011-07-12 16:42:08 -07003018 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003019 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003020 }
3021
3022 void goBackOnePageOrQuit() {
3023 Tab current = mTabControl.getCurrentTab();
3024 if (current == null) {
3025 /*
3026 * Instead of finishing the activity, simply push this to the back
3027 * of the stack and let ActivityManager to choose the foreground
3028 * activity. As BrowserActivity is singleTask, it will be always the
3029 * root of the task. So we can use either true or false for
3030 * moveTaskToBack().
3031 */
luxiaolb40014b2013-07-19 10:01:43 +08003032 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003033 return;
3034 }
John Reckef654f12011-07-12 16:42:08 -07003035 if (current.canGoBack()) {
3036 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003037 } else {
3038 // Check to see if we are closing a window that was created by
3039 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003040 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003041 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003042 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003043 // Now we close the other tab
3044 closeTab(current);
3045 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003046 /*
3047 * Instead of finishing the activity, simply push this to the back
3048 * of the stack and let ActivityManager to choose the foreground
3049 * activity. As BrowserActivity is singleTask, it will be always the
3050 * root of the task. So we can use either true or false for
3051 * moveTaskToBack().
3052 */
luxiaolb40014b2013-07-19 10:01:43 +08003053 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003054 }
3055 }
3056 }
3057
3058 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003059 * helper method for key handler
3060 * returns the current tab if it can't advance
3061 */
Michael Kolbc831b632011-05-11 09:30:34 -07003062 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003063 int pos = mTabControl.getCurrentPosition() + 1;
3064 if (pos >= mTabControl.getTabCount()) {
3065 pos = 0;
3066 }
3067 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003068 }
3069
3070 /**
3071 * helper method for key handler
3072 * returns the current tab if it can't advance
3073 */
Michael Kolbc831b632011-05-11 09:30:34 -07003074 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003075 int pos = mTabControl.getCurrentPosition() - 1;
3076 if ( pos < 0) {
3077 pos = mTabControl.getTabCount() - 1;
3078 }
3079 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003080 }
3081
John Reckbcef87f2012-02-03 14:58:34 -08003082 boolean isMenuOrCtrlKey(int keyCode) {
3083 return (KeyEvent.KEYCODE_MENU == keyCode)
3084 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3085 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3086 }
3087
Michael Kolb0035fad2011-03-14 13:25:28 -07003088 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003089 * handle key events in browser
3090 *
3091 * @param keyCode
3092 * @param event
3093 * @return true if handled, false to pass to super
3094 */
John Reck9c35b9c2012-05-30 10:08:50 -07003095 @Override
3096 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003097 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003098 // Even if MENU is already held down, we need to call to super to open
3099 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003100 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003101 mMenuIsDown = true;
3102 return false;
3103 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003104
Cary Clark8ff8c662010-12-29 15:03:05 -05003105 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003106 Tab tab = getCurrentTab();
3107 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003108
Cary Clark160bbb92011-01-10 11:17:07 -05003109 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3110 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003111
Michael Kolb8233fac2010-10-26 16:08:53 -07003112 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003113 case KeyEvent.KEYCODE_TAB:
3114 if (event.isCtrlPressed()) {
3115 if (event.isShiftPressed()) {
3116 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003117 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003118 } else {
3119 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003120 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003121 }
3122 return true;
3123 }
3124 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003125 case KeyEvent.KEYCODE_SPACE:
3126 // WebView/WebTextView handle the keys in the KeyDown. As
3127 // the Activity's shortcut keys are only handled when WebView
3128 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003129 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003130 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003131 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003132 pageDown();
3133 }
3134 return true;
3135 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003136 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003137 event.startTracking();
3138 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003139 case KeyEvent.KEYCODE_FORWARD:
3140 if (!noModifiers) break;
3141 tab.goForward();
3142 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003143 case KeyEvent.KEYCODE_DPAD_LEFT:
3144 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003145 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003146 return true;
3147 }
3148 break;
3149 case KeyEvent.KEYCODE_DPAD_RIGHT:
3150 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003151 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003152 return true;
3153 }
3154 break;
3155 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003156 if (ctrl) {
3157 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003158 return true;
3159 }
3160 break;
Michael Kolba4183062011-01-16 10:43:21 -08003161// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003162 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003163 if (ctrl ) {
3164 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003165 return true;
3166 }
3167 break;
Michael Kolba4183062011-01-16 10:43:21 -08003168// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003169// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003170// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003171// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003172// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003173// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003174// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003175// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003176// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003177// case KeyEvent.KEYCODE_M: // unused
3178// case KeyEvent.KEYCODE_N: // in Chrome: new window
3179// case KeyEvent.KEYCODE_O: // in Chrome: open file
3180// case KeyEvent.KEYCODE_P: // in Chrome: print page
3181// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003182// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003183// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3184 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003185 // we can't use the ctrl/shift flags, they check for
3186 // exclusive use of a modifier
3187 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003188 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003189 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003190 } else {
3191 openTabToHomePage();
3192 }
3193 return true;
3194 }
3195 break;
3196// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3197// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003198// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003199// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3200// case KeyEvent.KEYCODE_Y: // unused
3201// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003202 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003203 // it is a regular key and webview is not null
3204 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003205 }
3206
John Reck9c35b9c2012-05-30 10:08:50 -07003207 @Override
3208 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003209 switch(keyCode) {
3210 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003211 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003212 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003213 return true;
3214 }
3215 break;
3216 }
3217 return false;
3218 }
3219
John Reck9c35b9c2012-05-30 10:08:50 -07003220 @Override
3221 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003222 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003223 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003224 if (KeyEvent.KEYCODE_MENU == keyCode
3225 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003226 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003227 }
3228 }
Cary Clark160bbb92011-01-10 11:17:07 -05003229 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003230 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003231 case KeyEvent.KEYCODE_BACK:
3232 if (event.isTracking() && !event.isCanceled()) {
3233 onBackKey();
3234 return true;
3235 }
3236 break;
3237 }
3238 return false;
3239 }
3240
3241 public boolean isMenuDown() {
3242 return mMenuIsDown;
3243 }
3244
John Reck9c35b9c2012-05-30 10:08:50 -07003245 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003246 public void setupAutoFill(Message message) {
3247 // Open the settings activity at the AutoFill profile fragment so that
3248 // the user can create a new profile. When they return, we will dispatch
3249 // the message so that we can autofill the form using their new profile.
3250 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3251 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3252 AutoFillSettingsFragment.class.getName());
3253 mAutoFillSetupMessage = message;
3254 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3255 }
John Reckb3417f02011-01-14 11:01:05 -08003256
John Reck9c35b9c2012-05-30 10:08:50 -07003257 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003258 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003259 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003260 return true;
3261 }
3262
John Reck1cf4b792011-07-26 10:22:22 -07003263 @Override
3264 public boolean shouldCaptureThumbnails() {
3265 return mUi.shouldCaptureThumbnails();
3266 }
3267
Michael Kolbc3af0672011-08-09 10:24:41 -07003268 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003269 public boolean supportsVoice() {
3270 PackageManager pm = mActivity.getPackageManager();
3271 List activities = pm.queryIntentActivities(new Intent(
3272 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3273 return activities.size() != 0;
3274 }
3275
3276 @Override
3277 public void startVoiceRecognizer() {
3278 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003279 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003280 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3281 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3282 mActivity.startActivityForResult(voice, VOICE_RESULT);
3283 }
3284
3285 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003286 public void setBlockEvents(boolean block) {
3287 mBlockEvents = block;
3288 }
3289
John Reck9c35b9c2012-05-30 10:08:50 -07003290 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003291 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003292 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003293 }
3294
John Reck9c35b9c2012-05-30 10:08:50 -07003295 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003296 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003297 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003298 }
3299
John Reck9c35b9c2012-05-30 10:08:50 -07003300 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003301 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003302 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003303 }
3304
John Reck9c35b9c2012-05-30 10:08:50 -07003305 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003306 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003307 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003308 }
3309
John Reck9c35b9c2012-05-30 10:08:50 -07003310 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003311 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003312 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003313 }
3314
Michael Kolb8233fac2010-10-26 16:08:53 -07003315}