blob: 1d535a44fae63bfc1fe342e793aca9db616f76df [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);
1465 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001466 // Setup custom handling depending on the type
1467 switch (type) {
1468 case WebView.HitTestResult.PHONE_TYPE:
1469 menu.setHeaderTitle(Uri.decode(extra));
1470 menu.findItem(R.id.dial_context_menu_id).setIntent(
1471 new Intent(Intent.ACTION_VIEW, Uri
1472 .parse(WebView.SCHEME_TEL + extra)));
1473 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1474 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1475 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1476 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1477 addIntent);
1478 menu.findItem(R.id.copy_phone_context_menu_id)
1479 .setOnMenuItemClickListener(
1480 new Copy(extra));
1481 break;
1482
1483 case WebView.HitTestResult.EMAIL_TYPE:
1484 menu.setHeaderTitle(extra);
1485 menu.findItem(R.id.email_context_menu_id).setIntent(
1486 new Intent(Intent.ACTION_VIEW, Uri
1487 .parse(WebView.SCHEME_MAILTO + extra)));
1488 menu.findItem(R.id.copy_mail_context_menu_id)
1489 .setOnMenuItemClickListener(
1490 new Copy(extra));
1491 break;
1492
1493 case WebView.HitTestResult.GEO_TYPE:
1494 menu.setHeaderTitle(extra);
1495 menu.findItem(R.id.map_context_menu_id).setIntent(
1496 new Intent(Intent.ACTION_VIEW, Uri
1497 .parse(WebView.SCHEME_GEO
1498 + URLEncoder.encode(extra))));
1499 menu.findItem(R.id.copy_geo_context_menu_id)
1500 .setOnMenuItemClickListener(
1501 new Copy(extra));
1502 break;
1503
1504 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1505 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001506 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001507 // decide whether to show the open link in new tab option
1508 boolean showNewTab = mTabControl.canCreateNewTab();
1509 MenuItem newTabItem
1510 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001511 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001512 ? R.string.contextmenu_openlink_newwindow_background
1513 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 newTabItem.setVisible(showNewTab);
1515 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001516 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1517 newTabItem.setOnMenuItemClickListener(
1518 new MenuItem.OnMenuItemClickListener() {
1519 @Override
1520 public boolean onMenuItemClick(MenuItem item) {
1521 final HashMap<String, WebView> hrefMap =
1522 new HashMap<String, WebView>();
1523 hrefMap.put("webview", webview);
1524 final Message msg = mHandler.obtainMessage(
1525 FOCUS_NODE_HREF,
1526 R.id.open_newtab_context_menu_id,
1527 0, hrefMap);
1528 webview.requestFocusNodeHref(msg);
1529 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001530 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001531 });
1532 } else {
1533 newTabItem.setOnMenuItemClickListener(
1534 new MenuItem.OnMenuItemClickListener() {
1535 @Override
1536 public boolean onMenuItemClick(MenuItem item) {
1537 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001538 openTab(extra, parent,
1539 !mSettings.openInBackground(),
1540 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001541 return true;
1542 }
1543 });
1544 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001545 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001546 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1547 menu.setHeaderTitle(navigationUrl);
1548 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1549
1550 if (itemUrl != null) {
1551 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1552 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1553 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1554 @Override
1555 public boolean onMenuItemClick(MenuItem item) {
1556 final Intent intent = new Intent(Controller.this
1557 .getContext(),
1558 AddMyNavigationPage.class);
1559 Bundle bundle = new Bundle();
1560 String url = Uri.decode(navigationUrl);
1561 bundle.putBoolean("isAdding", false);
1562 bundle.putString("url", url);
1563 bundle.putString("name", getNameFromUrl(url));
1564 intent.putExtra("websites", bundle);
1565 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1566 return false;
1567 }
1568 });
1569 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1570 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1571 @Override
1572 public boolean onMenuItemClick(MenuItem item) {
1573 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1574 return false;
1575 }
1576 });
1577 }
1578 } else {
1579 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1580 }
1581 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001582 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1583 break;
1584 }
1585 // otherwise fall through to handle image part
1586 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001587 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1588 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1590 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001591 shareItem.setOnMenuItemClickListener(
1592 new MenuItem.OnMenuItemClickListener() {
1593 @Override
1594 public boolean onMenuItemClick(MenuItem item) {
1595 sharePage(mActivity, null, extra, null,
1596 null);
1597 return true;
1598 }
1599 }
1600 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001602 menu.findItem(R.id.view_image_context_menu_id)
1603 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1604 @Override
1605 public boolean onMenuItemClick(MenuItem item) {
1606 openTab(extra, mTabControl.getCurrentTab(), true, true);
1607 return false;
1608 }
1609 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001610 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1611 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1612 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001613 menu.findItem(R.id.set_wallpaper_context_menu_id).
1614 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1615 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 break;
1617
1618 default:
1619 Log.w(LOGTAG, "We should not get here.");
1620 break;
1621 }
1622 //update the ui
1623 mUi.onContextMenuCreated(menu);
1624 }
1625
kaiyiz6e5b3e02013-08-19 20:02:01 +08001626 public void startAddMyNavigation(String url) {
1627 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1628 Bundle bundle = new Bundle();
1629 bundle.putBoolean("isAdding", true);
1630 bundle.putString("url", url);
1631 bundle.putString("name", getNameFromUrl(url));
1632 intent.putExtra("websites", bundle);
1633 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1634 }
1635
1636 private void showMyNavigationDeleteDialog(final String itemUrl) {
1637 new AlertDialog.Builder(this.getContext())
1638 .setTitle(R.string.my_navigation_delete_label)
1639 .setIcon(android.R.drawable.ic_dialog_alert)
1640 .setMessage(R.string.my_navigation_delete_msg)
1641 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1642 @Override
1643 public void onClick(DialogInterface dialog, int whichButton) {
1644 deleteMyNavigationItem(itemUrl);
1645 }
1646 })
1647 .setNegativeButton(R.string.cancel, null)
1648 .show();
1649 }
1650
1651 private void deleteMyNavigationItem(final String itemUrl) {
1652 ContentResolver cr = this.getContext().getContentResolver();
1653 Cursor cursor = null;
1654
1655 try {
1656 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1657 new String[] {
1658 MyNavigationUtil.ID
1659 }, "url = ?", new String[] {
1660 itemUrl
1661 }, null);
1662 if (null != cursor && cursor.moveToFirst()) {
1663 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1664 cursor.getLong(0));
1665
1666 ContentValues values = new ContentValues();
1667 values.put(MyNavigationUtil.TITLE, "");
1668 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1669 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1670 ByteArrayOutputStream os = new ByteArrayOutputStream();
1671 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1672 R.raw.my_navigation_add);
1673 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1674 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1675 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1676 cr.update(uri, values, null, null);
1677 } else {
1678 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1679 }
1680 } catch (IllegalStateException e) {
1681 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1682 } finally {
1683 if (null != cursor) {
1684 cursor.close();
1685 }
1686 }
1687
1688 if (getCurrentTopWebView() != null) {
1689 getCurrentTopWebView().reload();
1690 }
1691 }
1692
1693 private String getNameFromUrl(String itemUrl) {
1694 ContentResolver cr = this.getContext().getContentResolver();
1695 Cursor cursor = null;
1696 String name = null;
1697
1698 try {
1699 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1700 new String[] {
1701 MyNavigationUtil.TITLE
1702 }, "url = ?", new String[] {
1703 itemUrl
1704 }, null);
1705 if (null != cursor && cursor.moveToFirst()) {
1706 name = cursor.getString(0);
1707 } else {
1708 Log.e(LOGTAG, "this item does not exist!");
1709 }
1710 } catch (IllegalStateException e) {
1711 Log.e(LOGTAG, "getNameFromUrl", e);
1712 } finally {
1713 if (null != cursor) {
1714 cursor.close();
1715 }
1716 }
1717 return name;
1718 }
1719
1720 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1721 int width = mActivity.getResources().getDimensionPixelOffset(
1722 R.dimen.myNavigationThumbnailWidth);
1723 int height = mActivity.getResources().getDimensionPixelOffset(
1724 R.dimen.myNavigationThumbnailHeight);
1725
1726 final Bitmap bm = createScreenshot(webView, width, height);
1727
1728 if (bm == null) {
1729 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1730 return;
1731 }
1732
1733 final ContentResolver cr = mActivity.getContentResolver();
1734 new AsyncTask<Void, Void, Void>() {
1735 @Override
1736 protected Void doInBackground(Void... unused) {
1737 ContentResolver cr = mActivity.getContentResolver();
1738 Cursor cursor = null;
1739 try {
1740 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1741 new String[] {
1742 MyNavigationUtil.ID
1743 }, "url = ?", new String[] {
1744 itemUrl
1745 }, null);
1746 if (null != cursor && cursor.moveToFirst()) {
1747 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1748 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1749
1750 ContentValues values = new ContentValues();
1751 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1752 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1753 cursor.getLong(0));
1754 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1755 cr.update(uri, values, null, null);
1756 os.close();
1757 }
1758 } catch (IllegalStateException e) {
1759 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1760 } catch (IOException e) {
1761 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1762 } finally {
1763 if (null != cursor) {
1764 cursor.close();
1765 }
1766 }
1767 return null;
1768 }
1769 }.execute();
1770 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001771 /**
1772 * As the menu can be open when loading state changes
1773 * we must manually update the state of the stop/reload menu
1774 * item
1775 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001776 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001777 if (menu == null) {
1778 return;
1779 }
1780 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001781 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001782 menu.findItem(R.id.stop_menu_id):
1783 menu.findItem(R.id.reload_menu_id);
1784 if (src != null) {
1785 dest.setIcon(src.getIcon());
1786 dest.setTitle(src.getTitle());
1787 }
1788 }
1789
John Reck9c35b9c2012-05-30 10:08:50 -07001790 @Override
1791 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001792 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001793 // hold on to the menu reference here; it is used by the page callbacks
1794 // to update the menu based on loading state
1795 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001796 // Note: setVisible will decide whether an item is visible; while
1797 // setEnabled() will decide whether an item is enabled, which also means
1798 // whether the matching shortcut key will function.
1799 switch (mMenuState) {
1800 case EMPTY_MENU:
1801 if (mCurrentMenuState != mMenuState) {
1802 menu.setGroupVisible(R.id.MAIN_MENU, false);
1803 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1804 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1805 }
1806 break;
1807 default:
1808 if (mCurrentMenuState != mMenuState) {
1809 menu.setGroupVisible(R.id.MAIN_MENU, true);
1810 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1811 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1812 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001813 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001814 break;
1815 }
1816 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001817 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001818 }
1819
Michael Kolb4bf79712011-07-14 14:18:12 -07001820 @Override
1821 public void updateMenuState(Tab tab, Menu menu) {
1822 boolean canGoBack = false;
1823 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001824 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001825 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001826 if (tab != null) {
1827 canGoBack = tab.canGoBack();
1828 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001829 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001830 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001831 }
1832 final MenuItem back = menu.findItem(R.id.back_menu_id);
1833 back.setEnabled(canGoBack);
1834
1835 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001836
1837 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1838 forward.setEnabled(canGoForward);
1839
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001840 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1841 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001842 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001843 if (source != null && dest != null) {
1844 dest.setTitle(source.getTitle());
1845 dest.setIcon(source.getIcon());
1846 }
John Recke1a03a32011-09-14 17:04:16 -07001847 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001848
1849 // decide whether to show the share link option
1850 PackageManager pm = mActivity.getPackageManager();
1851 Intent send = new Intent(Intent.ACTION_SEND);
1852 send.setType("text/plain");
1853 ResolveInfo ri = pm.resolveActivity(send,
1854 PackageManager.MATCH_DEFAULT_ONLY);
1855 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1856
1857 boolean isNavDump = mSettings.enableNavDump();
1858 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1859 nav.setVisible(isNavDump);
1860 nav.setEnabled(isNavDump);
1861
1862 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001863 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1864 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001865 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1866 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001867 // history and snapshots item are the members of COMBO menu group,
1868 // so if show history item, only make snapshots item invisible.
1869 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001870
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001871 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001872 }
1873
John Reck9c35b9c2012-05-30 10:08:50 -07001874 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001875 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001876 if (null == getCurrentTopWebView()) {
1877 return false;
1878 }
1879 if (mMenuIsDown) {
1880 // The shortcut action consumes the MENU. Even if it is still down,
1881 // it won't trigger the next shortcut action. In the case of the
1882 // shortcut action triggering a new activity, like Bookmarks, we
1883 // won't get onKeyUp for MENU. So it is important to reset it here.
1884 mMenuIsDown = false;
1885 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001886 if (mUi.onOptionsItemSelected(item)) {
1887 // ui callback handled it
1888 return true;
1889 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001890 switch (item.getItemId()) {
1891 // -- Main menu
1892 case R.id.new_tab_menu_id:
1893 openTabToHomePage();
1894 break;
1895
1896 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001897 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001898 break;
1899
Afzal Najamd4e33312012-04-26 01:54:01 -04001900 case R.id.close_other_tabs_id:
1901 closeOtherTabs();
1902 break;
1903
Michael Kolb8233fac2010-10-26 16:08:53 -07001904 case R.id.goto_menu_id:
1905 editUrl();
1906 break;
1907
1908 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001909 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1910 break;
1911
1912 case R.id.history_menu_id:
1913 bookmarksOrHistoryPicker(ComboViews.History);
1914 break;
1915
1916 case R.id.snapshots_menu_id:
1917 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001918 break;
1919
Michael Kolb8233fac2010-10-26 16:08:53 -07001920 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001921 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001922 break;
1923
1924 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001925 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001926 stopLoading();
1927 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001928 Tab currentTab = mTabControl.getCurrentTab();
1929 if (currentTab.hasCrashed) {
1930 currentTab.replaceCrashView(getCurrentTopWebView(),
1931 currentTab.getViewContainer());
1932 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 getCurrentTopWebView().reload();
1934 }
1935 break;
1936
1937 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001938 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001939 break;
1940
1941 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001942 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001943 break;
1944
1945 case R.id.close_menu_id:
1946 // Close the subwindow if it exists.
1947 if (mTabControl.getCurrentSubWindow() != null) {
1948 dismissSubWindow(mTabControl.getCurrentTab());
1949 break;
1950 }
1951 closeCurrentTab();
1952 break;
1953
kaiyiza8b6dbb2013-07-29 18:11:22 +08001954 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001955 Object[] params = { new String("persist.debug.browsermonkeytest")};
1956 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001957 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001958 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001959 if (ret != null && ret.equals("enable"))
1960 break;
luxiaolb40014b2013-07-19 10:01:43 +08001961 showExitDialog(mActivity);
1962 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 case R.id.homepage_menu_id:
1964 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001965 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001966 break;
1967
1968 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001969 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001970 break;
1971
1972 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001973 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001974 break;
1975
John Reck2bc80422011-06-30 15:11:49 -07001976 case R.id.save_snapshot_menu_id:
1977 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001978 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001979 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001980 break;
1981
Michael Kolb8233fac2010-10-26 16:08:53 -07001982 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001983 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001984 break;
1985
John Recke1a03a32011-09-14 17:04:16 -07001986 case R.id.snapshot_go_live:
1987 goLive();
1988 return true;
1989
Michael Kolb8233fac2010-10-26 16:08:53 -07001990 case R.id.share_page_menu_id:
1991 Tab currentTab = mTabControl.getCurrentTab();
1992 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001993 return false;
1994 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001995 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001996 break;
1997
1998 case R.id.dump_nav_menu_id:
1999 getCurrentTopWebView().debugDump();
2000 break;
2001
Michael Kolb8233fac2010-10-26 16:08:53 -07002002 case R.id.zoom_in_menu_id:
2003 getCurrentTopWebView().zoomIn();
2004 break;
2005
2006 case R.id.zoom_out_menu_id:
2007 getCurrentTopWebView().zoomOut();
2008 break;
2009
2010 case R.id.view_downloads_menu_id:
2011 viewDownloads();
2012 break;
2013
John Reck42229bc2011-08-19 13:26:43 -07002014 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002015 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002016 break;
2017
Michael Kolb8233fac2010-10-26 16:08:53 -07002018 case R.id.window_one_menu_id:
2019 case R.id.window_two_menu_id:
2020 case R.id.window_three_menu_id:
2021 case R.id.window_four_menu_id:
2022 case R.id.window_five_menu_id:
2023 case R.id.window_six_menu_id:
2024 case R.id.window_seven_menu_id:
2025 case R.id.window_eight_menu_id:
2026 {
2027 int menuid = item.getItemId();
2028 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2029 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2030 Tab desiredTab = mTabControl.getTab(id);
2031 if (desiredTab != null &&
2032 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002033 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002034 }
2035 break;
2036 }
2037 }
2038 }
2039 break;
2040
2041 default:
2042 return false;
2043 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002044 return true;
2045 }
2046
John Reck68234a92012-04-19 15:27:12 -07002047 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2048 implements OnCancelListener {
2049
2050 private Tab mTab;
2051 private Dialog mProgressDialog;
2052 private ContentValues mValues;
2053
2054 private SaveSnapshotTask(Tab tab) {
2055 mTab = tab;
2056 }
2057
2058 @Override
2059 protected void onPreExecute() {
2060 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2061 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2062 true, true, this);
2063 mValues = mTab.createSnapshotValues();
2064 }
2065
2066 @Override
2067 protected Long doInBackground(Void... params) {
2068 if (!mTab.saveViewState(mValues)) {
2069 return null;
2070 }
2071 if (isCancelled()) {
2072 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2073 File file = mActivity.getFileStreamPath(path);
2074 if (!file.delete()) {
2075 file.deleteOnExit();
2076 }
2077 return null;
2078 }
2079 final ContentResolver cr = mActivity.getContentResolver();
2080 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2081 if (result == null) {
2082 return null;
2083 }
2084 long id = ContentUris.parseId(result);
2085 return id;
2086 }
2087
2088 @Override
2089 protected void onPostExecute(Long id) {
2090 if (isCancelled()) {
2091 return;
2092 }
2093 mProgressDialog.dismiss();
2094 if (id == null) {
2095 Toast.makeText(mActivity, R.string.snapshot_failed,
2096 Toast.LENGTH_SHORT).show();
2097 return;
2098 }
2099 Bundle b = new Bundle();
2100 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2101 mUi.showComboView(ComboViews.Snapshots, b);
2102 }
2103
2104 @Override
2105 public void onCancel(DialogInterface dialog) {
2106 cancel(true);
2107 }
2108 }
2109
Michael Kolb80f75082012-04-10 10:50:06 -07002110 @Override
2111 public void toggleUserAgent() {
2112 WebView web = getCurrentWebView();
2113 mSettings.toggleDesktopUseragent(web);
2114 web.loadUrl(web.getOriginalUrl());
2115 }
2116
2117 @Override
2118 public void findOnPage() {
2119 getCurrentTopWebView().showFindDialog(null, true);
2120 }
2121
2122 @Override
2123 public void openPreferences() {
2124 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2125 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2126 getCurrentTopWebView().getUrl());
2127 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2128 }
2129
2130 @Override
2131 public void bookmarkCurrentPage() {
2132 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2133 if (bookmarkIntent != null) {
2134 mActivity.startActivity(bookmarkIntent);
2135 }
2136 }
2137
John Recke1a03a32011-09-14 17:04:16 -07002138 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002139 SnapshotTab t = (SnapshotTab) getCurrentTab();
2140 t.loadUrl(t.getLiveUrl(), null);
John Recke1a03a32011-09-14 17:04:16 -07002141 }
2142
luxiaolb40014b2013-07-19 10:01:43 +08002143 private void showExitDialog(final Activity activity) {
2144 new AlertDialog.Builder(activity)
2145 .setTitle(R.string.exit_browser_title)
2146 .setIcon(android.R.drawable.ic_dialog_alert)
2147 .setMessage(R.string.exit_browser_msg)
2148 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2149 public void onClick(DialogInterface dialog, int which) {
2150 activity.moveTaskToBack(true);
2151 dialog.dismiss();
2152 }
2153 })
2154 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2155 public void onClick(DialogInterface dialog, int which) {
2156 activity.finish();
2157 mHandler.postDelayed(new Runnable() {
2158 @Override
2159 public void run() {
2160 // TODO Auto-generated method stub
2161 mCrashRecoveryHandler.clearState(true);
2162 int pid = android.os.Process.myPid();
2163 android.os.Process.killProcess(pid);
2164 }
2165 }, 300);
2166 dialog.dismiss();
2167 }
2168 })
2169 .show();
2170 }
Michael Kolb315d5022011-10-13 12:47:11 -07002171 @Override
2172 public void showPageInfo() {
2173 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2174 }
2175
John Reck9c35b9c2012-05-30 10:08:50 -07002176 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002177 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002178 // Let the History and Bookmark fragments handle menus they created.
2179 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2180 return false;
2181 }
2182
Michael Kolb8233fac2010-10-26 16:08:53 -07002183 int id = item.getItemId();
2184 boolean result = true;
2185 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002186 // -- Browser context menu
2187 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002188 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002189 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002190 case R.id.copy_link_context_menu_id:
2191 final WebView webView = getCurrentTopWebView();
2192 if (null == webView) {
2193 result = false;
2194 break;
2195 }
2196 final HashMap<String, WebView> hrefMap =
2197 new HashMap<String, WebView>();
2198 hrefMap.put("webview", webView);
2199 final Message msg = mHandler.obtainMessage(
2200 FOCUS_NODE_HREF, id, 0, hrefMap);
2201 webView.requestFocusNodeHref(msg);
2202 break;
2203
2204 default:
2205 // For other context menus
2206 result = onOptionsItemSelected(item);
2207 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002208 return result;
2209 }
2210
2211 /**
2212 * support programmatically opening the context menu
2213 */
2214 public void openContextMenu(View view) {
2215 mActivity.openContextMenu(view);
2216 }
2217
2218 /**
2219 * programmatically open the options menu
2220 */
2221 public void openOptionsMenu() {
2222 mActivity.openOptionsMenu();
2223 }
2224
John Reck9c35b9c2012-05-30 10:08:50 -07002225 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002226 public boolean onMenuOpened(int featureId, Menu menu) {
2227 if (mOptionsMenuOpen) {
2228 if (mConfigChanged) {
2229 // We do not need to make any changes to the state of the
2230 // title bar, since the only thing that happened was a
2231 // change in orientation
2232 mConfigChanged = false;
2233 } else {
2234 if (!mExtendedMenuOpen) {
2235 mExtendedMenuOpen = true;
2236 mUi.onExtendedMenuOpened();
2237 } else {
2238 // Switching the menu back to icon view, so show the
2239 // title bar once again.
2240 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002241 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002242 }
2243 }
2244 } else {
2245 // The options menu is closed, so open it, and show the title
2246 mOptionsMenuOpen = true;
2247 mConfigChanged = false;
2248 mExtendedMenuOpen = false;
2249 mUi.onOptionsMenuOpened();
2250 }
2251 return true;
2252 }
2253
John Reck9c35b9c2012-05-30 10:08:50 -07002254 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002255 public void onOptionsMenuClosed(Menu menu) {
2256 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002257 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002258 }
2259
John Reck9c35b9c2012-05-30 10:08:50 -07002260 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002261 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002262 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002263 }
2264
2265 // Helper method for getting the top window.
2266 @Override
2267 public WebView getCurrentTopWebView() {
2268 return mTabControl.getCurrentTopWebView();
2269 }
2270
2271 @Override
2272 public WebView getCurrentWebView() {
2273 return mTabControl.getCurrentWebView();
2274 }
2275
2276 /*
2277 * This method is called as a result of the user selecting the options
2278 * menu to see the download window. It shows the download window on top of
2279 * the current window.
2280 */
2281 void viewDownloads() {
2282 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2283 mActivity.startActivity(intent);
2284 }
2285
John Reck30b065e2011-07-19 10:58:05 -07002286 int getActionModeHeight() {
2287 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2288 new int[] { android.R.attr.actionBarSize });
2289 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2290 actionBarSizeTypedArray.recycle();
2291 return size;
2292 }
2293
Michael Kolb8233fac2010-10-26 16:08:53 -07002294 // action mode
2295
John Reck9c35b9c2012-05-30 10:08:50 -07002296 @Override
2297 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002298 mUi.onActionModeStarted(mode);
2299 mActionMode = mode;
2300 }
2301
2302 /*
2303 * True if a custom ActionMode (i.e. find or select) is in use.
2304 */
2305 @Override
2306 public boolean isInCustomActionMode() {
2307 return mActionMode != null;
2308 }
2309
2310 /*
2311 * End the current ActionMode.
2312 */
2313 @Override
2314 public void endActionMode() {
2315 if (mActionMode != null) {
2316 mActionMode.finish();
2317 }
2318 }
2319
2320 /*
2321 * Called by find and select when they are finished. Replace title bars
2322 * as necessary.
2323 */
John Reck9c35b9c2012-05-30 10:08:50 -07002324 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002325 public void onActionModeFinished(ActionMode mode) {
2326 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002327 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002328 mActionMode = null;
2329 }
2330
2331 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002332 final Tab tab = getCurrentTab();
2333 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002334 }
2335
2336 // bookmark handling
2337
2338 /**
2339 * add the current page as a bookmark to the given folder id
2340 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002341 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002342 * bookmarked, and if it is, edit that bookmark. If false, and
2343 * the site is already bookmarked, do not attempt to edit the
2344 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002345 */
2346 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002347 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002348 WebView w = getCurrentTopWebView();
2349 if (w == null) {
2350 return null;
2351 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002352 Intent i = new Intent(mActivity,
2353 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002354 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2355 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2356 String touchIconUrl = w.getTouchIconUrl();
2357 if (touchIconUrl != null) {
2358 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2359 WebSettings settings = w.getSettings();
2360 if (settings != null) {
2361 i.putExtra(AddBookmarkPage.USER_AGENT,
2362 settings.getUserAgentString());
2363 }
2364 }
2365 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2366 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2367 getDesiredThumbnailHeight(mActivity)));
2368 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002369 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002370 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2371 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002372 // Put the dialog at the upper right of the screen, covering the
2373 // star on the title bar.
2374 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002375 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002376 }
2377
2378 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002379 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002380 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002381 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002382 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002383 }
2384
Vivek Sekharb54614f2014-05-01 19:03:37 -07002385 @Override
2386 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2387 boolean capture) {
2388 mUploadHandler = new UploadHandler(this);
2389 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2390 }
2391
Michael Kolb8233fac2010-10-26 16:08:53 -07002392 // thumbnails
2393
2394 /**
2395 * Return the desired width for thumbnail screenshots, which are stored in
2396 * the database, and used on the bookmarks screen.
2397 * @param context Context for finding out the density of the screen.
2398 * @return desired width for thumbnail screenshot.
2399 */
2400 static int getDesiredThumbnailWidth(Context context) {
2401 return context.getResources().getDimensionPixelOffset(
2402 R.dimen.bookmarkThumbnailWidth);
2403 }
2404
2405 /**
2406 * Return the desired height for thumbnail screenshots, which are stored in
2407 * the database, and used on the bookmarks screen.
2408 * @param context Context for finding out the density of the screen.
2409 * @return desired height for thumbnail screenshot.
2410 */
2411 static int getDesiredThumbnailHeight(Context context) {
2412 return context.getResources().getDimensionPixelOffset(
2413 R.dimen.bookmarkThumbnailHeight);
2414 }
2415
John Reck8cc92352011-07-06 17:41:52 -07002416 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002417 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002418 return null;
2419 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002420
2421 Bitmap viewportBitmap = view.getViewportBitmap();
2422 if (viewportBitmap == null) {
2423 return null;
2424 }
2425
2426 float aspectRatio = (float) width/height;
2427 int viewportWidth = viewportBitmap.getWidth();
2428 int viewportHeight = viewportBitmap.getHeight();
2429
2430 //modify the size to attain the same aspect ratio of desired thumbnail size
2431 if (viewportHeight > viewportWidth) {
2432 viewportHeight= (int)Math.round(viewportWidth * aspectRatio);
2433 } else {
2434 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2435 }
2436
2437 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2438 Rect dstRect = new Rect(0, 0, width, height);
2439
2440 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2441 || sThumbnailBitmap.getHeight() != height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002442 if (sThumbnailBitmap != null) {
2443 sThumbnailBitmap.recycle();
2444 sThumbnailBitmap = null;
2445 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002446
2447 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002448 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002449
John Reckd7dd9b22011-08-30 09:18:29 -07002450 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002451 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
John Reckd7dd9b22011-08-30 09:18:29 -07002452
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002453 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002454 }
2455
John Reck34ef2672011-02-10 11:30:55 -08002456 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002457 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002458 // FIXME: Would like to make sure there is actually something to
2459 // draw, but the API for that (WebViewCore.pictureReady()) is not
2460 // currently accessible here.
2461
John Reck34ef2672011-02-10 11:30:55 -08002462 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002463 if (view == null) {
2464 // Tab was destroyed
2465 return;
2466 }
John Reck34ef2672011-02-10 11:30:55 -08002467 final String url = tab.getUrl();
2468 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002469 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002470 if (TextUtils.isEmpty(url)) {
2471 return;
2472 }
2473
kaiyiz6e5b3e02013-08-19 20:02:01 +08002474 //update My Navigation Thumbnails
2475 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2476 if (isMyNavigationUrl) {
2477 updateMyNavigationThumbnail(url, view);
2478 }
John Reck34ef2672011-02-10 11:30:55 -08002479 // Only update thumbnails for web urls (http(s)://), not for
2480 // about:, javascript:, data:, etc...
2481 // Unless it is a bookmarked site, then always update
2482 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2483 return;
2484 }
2485
kaiyiz6e5b3e02013-08-19 20:02:01 +08002486 if (url != null && mUpdateMyNavThumbnailUrl != null
2487 && Patterns.WEB_URL.matcher(url).matches()
2488 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2489 String urlHost = (new WebAddress(url)).getHost();
2490 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2491 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2492 || bookmarkHost.length() == 0) {
2493 return;
2494 }
2495 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2496 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2497 bookmarkHost.length());
2498 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2499 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2500 if (!bookmarkDomain.equals(urlDomain)) {
2501 return;
2502 }
2503 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002504 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2505 getDesiredThumbnailHeight(mActivity));
2506 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002507 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2508 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2509 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2510 500);
2511 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002512 return;
2513 }
2514
2515 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002516 new AsyncTask<Void, Void, Void>() {
2517 @Override
2518 protected Void doInBackground(Void... unused) {
2519 Cursor cursor = null;
2520 try {
2521 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002522 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2523 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2524 : url);
2525 if (mUpdateMyNavThumbnail) {
2526 mUpdateMyNavThumbnail = false;
2527 mUpdateMyNavThumbnailUrl = null;
2528 }
John Reck34ef2672011-02-10 11:30:55 -08002529 if (cursor != null && cursor.moveToFirst()) {
2530 final ByteArrayOutputStream os =
2531 new ByteArrayOutputStream();
2532 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002533
John Reck34ef2672011-02-10 11:30:55 -08002534 ContentValues values = new ContentValues();
2535 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002536
John Reck34ef2672011-02-10 11:30:55 -08002537 do {
John Reck617fd832011-02-16 14:35:59 -08002538 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002539 cr.update(Images.CONTENT_URI, values, null, null);
2540 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 }
John Reck34ef2672011-02-10 11:30:55 -08002542 } catch (IllegalStateException e) {
2543 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002544 } catch (SQLiteException s) {
2545 // Added for possible error when user tries to remove the same bookmark
2546 // that is being updated with a screen shot
2547 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002548 } finally {
2549 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002550 }
John Reck34ef2672011-02-10 11:30:55 -08002551 return null;
2552 }
2553 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002554 }
2555
2556 private class Copy implements OnMenuItemClickListener {
2557 private CharSequence mText;
2558
John Reck9c35b9c2012-05-30 10:08:50 -07002559 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002560 public boolean onMenuItemClick(MenuItem item) {
2561 copy(mText);
2562 return true;
2563 }
2564
2565 public Copy(CharSequence toCopy) {
2566 mText = toCopy;
2567 }
2568 }
2569
Leon Scroggins63c02662010-11-18 15:16:27 -05002570 private static class Download implements OnMenuItemClickListener {
2571 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002572 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002573 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002574 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002575 private static final String FALLBACK_EXTENSION = "dat";
2576 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002577
John Reck9c35b9c2012-05-30 10:08:50 -07002578 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002579 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002580 if (DataUri.isDataUri(mText)) {
2581 saveDataUri();
2582 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002583 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002584 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002585 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002586 return true;
2587 }
2588
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002589 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2590 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002591 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002592 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002593 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002594 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002595 }
George Mount387d45d2011-10-07 15:57:53 -07002596
2597 /**
2598 * Treats mText as a data URI and writes its contents to a file
2599 * based on the current time.
2600 */
2601 private void saveDataUri() {
2602 FileOutputStream outputStream = null;
2603 try {
2604 DataUri uri = new DataUri(mText);
2605 File target = getTarget(uri);
2606 outputStream = new FileOutputStream(target);
2607 outputStream.write(uri.getData());
2608 final DownloadManager manager =
2609 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2610 manager.addCompletedDownload(target.getName(),
2611 mActivity.getTitle().toString(), false,
2612 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002613 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002614 } catch (IOException e) {
2615 Log.e(LOGTAG, "Could not save data URL");
2616 } finally {
2617 if (outputStream != null) {
2618 try {
2619 outputStream.close();
2620 } catch (IOException e) {
2621 // ignore close errors
2622 }
2623 }
2624 }
2625 }
2626
2627 /**
2628 * Creates a File based on the current time stamp and uses
2629 * the mime type of the DataUri to get the extension.
2630 */
2631 private File getTarget(DataUri uri) throws IOException {
2632 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002633 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002634 String nameBase = format.format(new Date());
2635 String mimeType = uri.getMimeType();
2636 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2637 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2638 if (extension == null) {
2639 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2640 extension = FALLBACK_EXTENSION;
2641 }
2642 extension = "." + extension; // createTempFile needs the '.'
2643 File targetFile = File.createTempFile(nameBase, extension, dir);
2644 return targetFile;
2645 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002646 }
2647
Cary Clark8974d282010-11-22 10:46:05 -05002648 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002649 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002650
John Reck9c35b9c2012-05-30 10:08:50 -07002651 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002652 public boolean onMenuItemClick(MenuItem item) {
2653 if (mWebView != null) {
2654 return mWebView.selectText();
2655 }
2656 return false;
2657 }
2658
2659 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002660 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002661 }
2662
2663 }
2664
Michael Kolb8233fac2010-10-26 16:08:53 -07002665 /********************** TODO: UI stuff *****************************/
2666
2667 // these methods have been copied, they still need to be cleaned up
2668
2669 /****************** tabs ***************************************************/
2670
2671 // basic tab interactions:
2672
2673 // it is assumed that tabcontrol already knows about the tab
2674 protected void addTab(Tab tab) {
2675 mUi.addTab(tab);
2676 }
2677
2678 protected void removeTab(Tab tab) {
2679 mUi.removeTab(tab);
2680 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002681 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002682 }
2683
Michael Kolbf2055602011-04-09 17:20:03 -07002684 @Override
2685 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002686 // monkey protection against delayed start
2687 if (tab != null) {
2688 mTabControl.setCurrentTab(tab);
2689 // the tab is guaranteed to have a webview after setCurrentTab
2690 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002691 tab.setTimeStamp();
Michael Kolbcd424e92011-02-24 10:26:26 -08002692 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002693 }
2694
John Reck8bcafc12011-08-29 16:43:02 -07002695 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002696 Tab current = mTabControl.getCurrentTab();
2697 if (current != null
2698 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002699 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002700 }
2701 }
2702
John Reck26b18322011-06-21 13:08:58 -07002703 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002704 // Dismiss the subwindow if applicable.
2705 dismissSubWindow(appTab);
2706 // Since we might kill the WebView, remove it from the
2707 // content view first.
2708 mUi.detachTab(appTab);
2709 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002710 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002711 // TODO: analyze why the remove and add are necessary
2712 mUi.attachTab(appTab);
2713 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002714 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002715 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002716 } else {
2717 // If the tab was the current tab, we have to attach
2718 // it to the view system again.
2719 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002720 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002721 }
2722 }
2723
2724 // Remove the sub window if it exists. Also called by TabControl when the
2725 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002726 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002727 public void dismissSubWindow(Tab tab) {
2728 removeSubWindow(tab);
2729 // dismiss the subwindow. This will destroy the WebView.
2730 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002731 WebView wv = getCurrentTopWebView();
2732 if (wv != null) {
2733 wv.requestFocus();
2734 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002735 }
2736
2737 @Override
2738 public void removeSubWindow(Tab t) {
2739 if (t.getSubWebView() != null) {
2740 mUi.removeSubWindow(t.getSubViewContainer());
2741 }
2742 }
2743
2744 @Override
2745 public void attachSubWindow(Tab tab) {
2746 if (tab.getSubWebView() != null) {
2747 mUi.attachSubWindow(tab.getSubViewContainer());
2748 getCurrentTopWebView().requestFocus();
2749 }
2750 }
2751
Mathew Inwood29721c22011-06-29 17:55:24 +01002752 private Tab showPreloadedTab(final UrlData urlData) {
2753 if (!urlData.isPreloaded()) {
2754 return null;
2755 }
2756 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2757 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2758 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002759 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002760 // Could not submit query. Fallback to regular tab creation
2761 tabControl.destroy();
2762 return null;
2763 }
2764 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002765 // check tab count and make room for new tab
2766 if (!mTabControl.canCreateNewTab()) {
2767 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2768 if (leastUsed != null) {
2769 closeTab(leastUsed);
2770 }
2771 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002772 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002773 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002774 mTabControl.addPreloadedTab(t);
2775 addTab(t);
2776 setActiveTab(t);
2777 return t;
2778 }
2779
Michael Kolb7bcafde2011-05-09 13:55:59 -07002780 // open a non inconito tab with the given url data
2781 // and set as active tab
2782 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002783 Tab tab = showPreloadedTab(urlData);
2784 if (tab == null) {
2785 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002786 if ((tab != null) && !urlData.isEmpty()) {
2787 loadUrlDataIn(tab, urlData);
2788 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002789 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002790 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002791 }
2792
Michael Kolb843510f2010-12-09 10:51:49 -08002793 @Override
2794 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002795 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002796 }
2797
Michael Kolb8233fac2010-10-26 16:08:53 -07002798 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002799 public Tab openIncognitoTab() {
2800 return openTab(INCOGNITO_URI, true, true, false);
2801 }
2802
2803 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002804 public Tab openTab(String url, boolean incognito, boolean setActive,
2805 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002806 return openTab(url, incognito, setActive, useCurrent, null);
2807 }
2808
2809 @Override
2810 public Tab openTab(String url, Tab parent, boolean setActive,
2811 boolean useCurrent) {
2812 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2813 setActive, useCurrent, parent);
2814 }
2815
2816 public Tab openTab(String url, boolean incognito, boolean setActive,
2817 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002818 Tab tab = createNewTab(incognito, setActive, useCurrent);
2819 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002820 if (parent != null && parent != tab) {
2821 parent.addChildTab(tab);
2822 }
Michael Kolb519d2282011-05-09 17:03:19 -07002823 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002824 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002825 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002826 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002827 return tab;
2828 }
2829
2830 // this method will attempt to create a new tab
2831 // incognito: private browsing tab
2832 // setActive: ste tab as current tab
2833 // useCurrent: if no new tab can be created, return current tab
2834 private Tab createNewTab(boolean incognito, boolean setActive,
2835 boolean useCurrent) {
2836 Tab tab = null;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002837 MemoryMonitor memMonitor = null;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002838 if (mTabControl.canCreateNewTab()) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002839 if (mSettings.enableMemoryMonitor()) {
2840 Log.d(LOGTAG, " Memory Monitor Enabled .");
2841 memMonitor = MemoryMonitor.getInstance(mActivity.getApplicationContext(),this);
2842 if (memMonitor != null) {
2843 //Remove webview associated with the oldest tab
2844 memMonitor.destroyLeastRecentlyActiveTab();
2845 }
2846 } else {
2847 Log.d(LOGTAG, " Memory Monitor disabled .");
2848 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002849 tab = mTabControl.createNewTab(incognito);
2850 addTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002851 tab.setTimeStamp();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002852 if (setActive) {
2853 setActiveTab(tab);
2854 }
2855 } else {
2856 if (useCurrent) {
2857 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002858 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002859 } else {
2860 mUi.showMaxTabsWarning();
2861 }
2862 }
2863 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002864 }
2865
John Reck2bc80422011-06-30 15:11:49 -07002866 @Override
2867 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2868 SnapshotTab tab = null;
2869 if (mTabControl.canCreateNewTab()) {
2870 tab = mTabControl.createSnapshotTab(snapshotId);
2871 addTab(tab);
2872 if (setActive) {
2873 setActiveTab(tab);
2874 }
2875 } else {
2876 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002877 }
2878 return tab;
2879 }
2880
Michael Kolb8233fac2010-10-26 16:08:53 -07002881 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002882 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002883 * @return boolean True if we successfully switched to a different tab. If
2884 * the indexth tab is null, or if that tab is the same as
2885 * the current one, return false.
2886 */
2887 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002888 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002889 Tab currentTab = mTabControl.getCurrentTab();
2890 if (tab == null || tab == currentTab) {
2891 return false;
2892 }
2893 setActiveTab(tab);
2894 return true;
2895 }
2896
2897 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002898 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002899 closeCurrentTab(false);
2900 }
2901
2902 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002903 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002904 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002905 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002906 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002907 return;
2908 }
Michael Kolbc831b632011-05-11 09:30:34 -07002909 final Tab current = mTabControl.getCurrentTab();
2910 final int pos = mTabControl.getCurrentPosition();
2911 Tab newTab = current.getParent();
2912 if (newTab == null) {
2913 newTab = mTabControl.getTab(pos + 1);
2914 if (newTab == null) {
2915 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002916 }
2917 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002918 if (andQuit) {
2919 mTabControl.setCurrentTab(newTab);
2920 closeTab(current);
2921 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002922 // Close window
2923 closeTab(current);
2924 }
2925 }
2926
2927 /**
2928 * Close the tab, remove its associated title bar, and adjust mTabControl's
2929 * current tab to a valid value.
2930 */
2931 @Override
2932 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002933 if (tab == mTabControl.getCurrentTab()) {
2934 closeCurrentTab();
2935 } else {
2936 removeTab(tab);
2937 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002938 }
2939
Afzal Najamd4e33312012-04-26 01:54:01 -04002940 /**
2941 * Close all tabs except the current one
2942 */
2943 @Override
2944 public void closeOtherTabs() {
2945 int inactiveTabs = mTabControl.getTabCount() - 1;
2946 for (int i = inactiveTabs; i >= 0; i--) {
2947 Tab tab = mTabControl.getTab(i);
2948 if (tab != mTabControl.getCurrentTab()) {
2949 removeTab(tab);
2950 }
2951 }
2952 }
2953
Michael Kolb8233fac2010-10-26 16:08:53 -07002954 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002955 protected void loadUrlFromContext(String url) {
2956 Tab tab = getCurrentTab();
2957 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002958 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002959 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002960 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002961 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002962 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002963 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002964 }
2965 }
2966 }
2967
2968 /**
2969 * Load the URL into the given WebView and update the title bar
2970 * to reflect the new load. Call this instead of WebView.loadUrl
2971 * directly.
2972 * @param view The WebView used to load url.
2973 * @param url The URL to load.
2974 */
John Reck71e51422011-07-01 16:49:28 -07002975 @Override
2976 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002977 loadUrl(tab, url, null);
2978 }
2979
2980 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2981 if (tab != null) {
2982 dismissSubWindow(tab);
2983 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002984 if (tab.hasCrashed) {
2985 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
2986 }
Michael Kolba53c9892011-10-05 13:31:40 -07002987 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002988 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002989 }
2990
2991 /**
2992 * Load UrlData into a Tab and update the title bar to reflect the new
2993 * load. Call this instead of UrlData.loadIn directly.
2994 * @param t The Tab used to load.
2995 * @param data The UrlData being loaded.
2996 */
2997 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002998 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002999 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003000 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003001 } else {
John Reck38b39652012-06-05 09:22:59 -07003002 if (t != null && data.mDisableUrlOverride) {
3003 t.disableUrlOverridingForLoad();
3004 }
John Reck26b18322011-06-21 13:08:58 -07003005 loadUrl(t, data.mUrl, data.mHeaders);
3006 }
3007 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003008 }
3009
John Reck30c714c2010-12-16 17:30:34 -08003010 @Override
3011 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003012 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003013 if (tab.canGoBack()) {
3014 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003015 } else {
John Reckef654f12011-07-12 16:42:08 -07003016 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003017 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003018 }
3019
3020 void goBackOnePageOrQuit() {
3021 Tab current = mTabControl.getCurrentTab();
3022 if (current == null) {
3023 /*
3024 * Instead of finishing the activity, simply push this to the back
3025 * of the stack and let ActivityManager to choose the foreground
3026 * activity. As BrowserActivity is singleTask, it will be always the
3027 * root of the task. So we can use either true or false for
3028 * moveTaskToBack().
3029 */
luxiaolb40014b2013-07-19 10:01:43 +08003030 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003031 return;
3032 }
John Reckef654f12011-07-12 16:42:08 -07003033 if (current.canGoBack()) {
3034 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003035 } else {
3036 // Check to see if we are closing a window that was created by
3037 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003038 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003039 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003040 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003041 // Now we close the other tab
3042 closeTab(current);
3043 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003044 /*
3045 * Instead of finishing the activity, simply push this to the back
3046 * of the stack and let ActivityManager to choose the foreground
3047 * activity. As BrowserActivity is singleTask, it will be always the
3048 * root of the task. So we can use either true or false for
3049 * moveTaskToBack().
3050 */
luxiaolb40014b2013-07-19 10:01:43 +08003051 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003052 }
3053 }
3054 }
3055
3056 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003057 * helper method for key handler
3058 * returns the current tab if it can't advance
3059 */
Michael Kolbc831b632011-05-11 09:30:34 -07003060 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003061 int pos = mTabControl.getCurrentPosition() + 1;
3062 if (pos >= mTabControl.getTabCount()) {
3063 pos = 0;
3064 }
3065 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003066 }
3067
3068 /**
3069 * helper method for key handler
3070 * returns the current tab if it can't advance
3071 */
Michael Kolbc831b632011-05-11 09:30:34 -07003072 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003073 int pos = mTabControl.getCurrentPosition() - 1;
3074 if ( pos < 0) {
3075 pos = mTabControl.getTabCount() - 1;
3076 }
3077 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003078 }
3079
John Reckbcef87f2012-02-03 14:58:34 -08003080 boolean isMenuOrCtrlKey(int keyCode) {
3081 return (KeyEvent.KEYCODE_MENU == keyCode)
3082 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3083 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3084 }
3085
Michael Kolb0035fad2011-03-14 13:25:28 -07003086 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003087 * handle key events in browser
3088 *
3089 * @param keyCode
3090 * @param event
3091 * @return true if handled, false to pass to super
3092 */
John Reck9c35b9c2012-05-30 10:08:50 -07003093 @Override
3094 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003095 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003096 // Even if MENU is already held down, we need to call to super to open
3097 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003098 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003099 mMenuIsDown = true;
3100 return false;
3101 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003102
Cary Clark8ff8c662010-12-29 15:03:05 -05003103 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003104 Tab tab = getCurrentTab();
3105 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003106
Cary Clark160bbb92011-01-10 11:17:07 -05003107 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3108 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003109
Michael Kolb8233fac2010-10-26 16:08:53 -07003110 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003111 case KeyEvent.KEYCODE_TAB:
3112 if (event.isCtrlPressed()) {
3113 if (event.isShiftPressed()) {
3114 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003115 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003116 } else {
3117 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003118 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003119 }
3120 return true;
3121 }
3122 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003123 case KeyEvent.KEYCODE_SPACE:
3124 // WebView/WebTextView handle the keys in the KeyDown. As
3125 // the Activity's shortcut keys are only handled when WebView
3126 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003127 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003128 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003129 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003130 pageDown();
3131 }
3132 return true;
3133 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003134 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003135 event.startTracking();
3136 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003137 case KeyEvent.KEYCODE_FORWARD:
3138 if (!noModifiers) break;
3139 tab.goForward();
3140 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003141 case KeyEvent.KEYCODE_DPAD_LEFT:
3142 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003143 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003144 return true;
3145 }
3146 break;
3147 case KeyEvent.KEYCODE_DPAD_RIGHT:
3148 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003149 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003150 return true;
3151 }
3152 break;
3153 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003154 if (ctrl) {
3155 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003156 return true;
3157 }
3158 break;
Michael Kolba4183062011-01-16 10:43:21 -08003159// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003160 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003161 if (ctrl ) {
3162 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003163 return true;
3164 }
3165 break;
Michael Kolba4183062011-01-16 10:43:21 -08003166// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003167// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003168// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003169// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003170// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003171// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003172// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003173// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003174// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003175// case KeyEvent.KEYCODE_M: // unused
3176// case KeyEvent.KEYCODE_N: // in Chrome: new window
3177// case KeyEvent.KEYCODE_O: // in Chrome: open file
3178// case KeyEvent.KEYCODE_P: // in Chrome: print page
3179// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003180// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003181// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3182 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003183 // we can't use the ctrl/shift flags, they check for
3184 // exclusive use of a modifier
3185 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003186 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003187 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003188 } else {
3189 openTabToHomePage();
3190 }
3191 return true;
3192 }
3193 break;
3194// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3195// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003196// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003197// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3198// case KeyEvent.KEYCODE_Y: // unused
3199// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003200 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003201 // it is a regular key and webview is not null
3202 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003203 }
3204
John Reck9c35b9c2012-05-30 10:08:50 -07003205 @Override
3206 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003207 switch(keyCode) {
3208 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003209 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003210 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003211 return true;
3212 }
3213 break;
3214 }
3215 return false;
3216 }
3217
John Reck9c35b9c2012-05-30 10:08:50 -07003218 @Override
3219 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003220 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003221 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003222 if (KeyEvent.KEYCODE_MENU == keyCode
3223 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003224 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003225 }
3226 }
Cary Clark160bbb92011-01-10 11:17:07 -05003227 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003228 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003229 case KeyEvent.KEYCODE_BACK:
3230 if (event.isTracking() && !event.isCanceled()) {
3231 onBackKey();
3232 return true;
3233 }
3234 break;
3235 }
3236 return false;
3237 }
3238
3239 public boolean isMenuDown() {
3240 return mMenuIsDown;
3241 }
3242
John Reck9c35b9c2012-05-30 10:08:50 -07003243 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003244 public void setupAutoFill(Message message) {
3245 // Open the settings activity at the AutoFill profile fragment so that
3246 // the user can create a new profile. When they return, we will dispatch
3247 // the message so that we can autofill the form using their new profile.
3248 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3249 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3250 AutoFillSettingsFragment.class.getName());
3251 mAutoFillSetupMessage = message;
3252 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3253 }
John Reckb3417f02011-01-14 11:01:05 -08003254
John Reck9c35b9c2012-05-30 10:08:50 -07003255 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003256 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003257 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003258 return true;
3259 }
3260
John Reck1cf4b792011-07-26 10:22:22 -07003261 @Override
3262 public boolean shouldCaptureThumbnails() {
3263 return mUi.shouldCaptureThumbnails();
3264 }
3265
Michael Kolbc3af0672011-08-09 10:24:41 -07003266 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003267 public boolean supportsVoice() {
3268 PackageManager pm = mActivity.getPackageManager();
3269 List activities = pm.queryIntentActivities(new Intent(
3270 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3271 return activities.size() != 0;
3272 }
3273
3274 @Override
3275 public void startVoiceRecognizer() {
3276 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003277 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003278 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3279 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3280 mActivity.startActivityForResult(voice, VOICE_RESULT);
3281 }
3282
3283 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003284 public void setBlockEvents(boolean block) {
3285 mBlockEvents = block;
3286 }
3287
John Reck9c35b9c2012-05-30 10:08:50 -07003288 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003289 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003290 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003291 }
3292
John Reck9c35b9c2012-05-30 10:08:50 -07003293 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003294 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003295 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003296 }
3297
John Reck9c35b9c2012-05-30 10:08:50 -07003298 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003299 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003300 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003301 }
3302
John Reck9c35b9c2012-05-30 10:08:50 -07003303 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003304 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003305 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003306 }
3307
John Reck9c35b9c2012-05-30 10:08:50 -07003308 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003309 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003310 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003311 }
3312
Michael Kolb8233fac2010-10-26 16:08:53 -07003313}