blob: 012191e9bdfa4dae97c0bed0cc3501608154b20e [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;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070028import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.content.ContentValues;
John Reck68234a92012-04-19 15:27:12 -070030import android.content.DialogInterface;
31import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070032import android.content.Intent;
33import android.content.pm.PackageManager;
34import android.content.pm.ResolveInfo;
35import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070036import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050037import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.database.Cursor;
39import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020040import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080042import android.graphics.BitmapFactory;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Canvas;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080044import android.graphics.Paint;
45import android.graphics.Rect;
kaiyiza016da12013-08-26 17:50:22 +080046import android.net.ConnectivityManager;
47import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.net.Uri;
49import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080050import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080051import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.os.AsyncTask;
53import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070054import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.os.Handler;
56import android.os.Message;
57import android.os.PowerManager;
58import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.provider.Browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070061import android.provider.ContactsContract;
62import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080063import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070064import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070065import android.text.TextUtils;
66import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080067import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070068import android.view.ActionMode;
69import android.view.ContextMenu;
70import android.view.ContextMenu.ContextMenuInfo;
71import android.view.Gravity;
72import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070073import android.view.Menu;
74import android.view.MenuInflater;
75import android.view.MenuItem;
76import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070077import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070078import android.view.View;
George Mount387d45d2011-10-07 15:57:53 -070079import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070080import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080081import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb8233fac2010-10-26 16:08:53 -070082import android.webkit.WebIconDatabase;
Leon Scrogginsac993842011-02-02 12:54:07 -050083import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070084
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080085import org.codeaurora.swe.CookieManager;
86import org.codeaurora.swe.CookieSyncManager;
87import org.codeaurora.swe.HttpAuthHandler;
88import org.codeaurora.swe.SslErrorHandler;
89import org.codeaurora.swe.WebSettings;
90import org.codeaurora.swe.WebView;
91
Bijan Amirzada41242f22014-03-21 12:12:18 -070092import com.android.browser.R;
93import com.android.browser.IntentHandler.UrlData;
94import com.android.browser.UI.ComboViews;
95import com.android.browser.mynavigation.AddMyNavigationPage;
96import com.android.browser.mynavigation.MyNavigationUtil;
97import com.android.browser.platformsupport.BrowserContract;
98import com.android.browser.platformsupport.WebAddress;
99import com.android.browser.platformsupport.BrowserContract.Images;
100import com.android.browser.provider.BrowserProvider2.Thumbnails;
101import com.android.browser.provider.SnapshotProvider.Snapshots;
102import com.android.browser.reflect.ReflectHelper;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700103
Michael Kolb8233fac2010-10-26 16:08:53 -0700104import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700105import java.io.File;
106import java.io.FileOutputStream;
107import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700108import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700109import java.text.DateFormat;
110import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700111import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700112import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700113import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800115import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200116import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700117import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700118
119/**
120 * Controller for browser
121 */
122public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700123 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700124
125 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800126 private static final String SEND_APP_ID_EXTRA =
127 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700128 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800129
Vivek Sekharb54614f2014-05-01 19:03:37 -0700130 // Remind switch to data connection if wifi is unavailable
131 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800132
133 public final static String EXTRA_SHARE_SCREENSHOT = "share_screenshot";
134 public final static String EXTRA_SHARE_FAVICON = "share_favicon";
Michael Kolb8233fac2010-10-26 16:08:53 -0700135 // public message ids
136 public final static int LOAD_URL = 1001;
137 public final static int STOP_LOAD = 1002;
138
139 // Message Ids
140 private static final int FOCUS_NODE_HREF = 102;
141 private static final int RELEASE_WAKELOCK = 107;
142
143 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
144
145 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800146 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700147
148 private static final int EMPTY_MENU = -1;
149
Michael Kolb8233fac2010-10-26 16:08:53 -0700150 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700151 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700152 final static int PREFERENCES_PAGE = 3;
153 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000154 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700155 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800156 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000157
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
159
160 // As the ids are dynamically created, we can't guarantee that they will
161 // be in sequence, so this static array maps ids to a window number.
162 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
163 { R.id.window_one_menu_id, R.id.window_two_menu_id,
164 R.id.window_three_menu_id, R.id.window_four_menu_id,
165 R.id.window_five_menu_id, R.id.window_six_menu_id,
166 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
167
168 // "source" parameter for Google search through search key
169 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
170 // "source" parameter for Google search through simplily type
171 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
172
George Mount387d45d2011-10-07 15:57:53 -0700173 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700174 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
175
John Reckd7dd9b22011-08-30 09:18:29 -0700176 // A bitmap that is re-used in createScreenshot as scratch space
177 private static Bitmap sThumbnailBitmap;
178
Michael Kolb8233fac2010-10-26 16:08:53 -0700179 private Activity mActivity;
180 private UI mUi;
181 private TabControl mTabControl;
182 private BrowserSettings mSettings;
183 private WebViewFactory mFactory;
184
185 private WakeLock mWakeLock;
186
187 private UrlHandler mUrlHandler;
188 private UploadHandler mUploadHandler;
189 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700190 private PageDialogsHandler mPageDialogsHandler;
191 private NetworkStateHandler mNetworkHandler;
192
Ben Murdoch8029a772010-11-16 11:58:21 +0000193 private Message mAutoFillSetupMessage;
194
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800196 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700197
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700198 private boolean mJsInterfaceEnabled = false;
199
Michael Kolb8233fac2010-10-26 16:08:53 -0700200 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
201
202 // FIXME, temp address onPrepareMenu performance problem.
203 // When we move everything out of view, we should rewrite this.
204 private int mCurrentMenuState = 0;
205 private int mMenuState = R.id.MAIN_MENU;
206 private int mOldMenuState = EMPTY_MENU;
207 private Menu mCachedMenu;
208
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 private boolean mMenuIsDown;
210
211 // For select and find, we keep track of the ActionMode so that
212 // finish() can be called as desired.
213 private ActionMode mActionMode;
214
215 /**
216 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
217 * of whether the configuration has changed. The first onMenuOpened call
218 * after a configuration change is simply a reopening of the same menu
219 * (i.e. mIconView did not change).
220 */
221 private boolean mConfigChanged;
222
223 /**
224 * Keeps track of whether the options menu is open. This is important in
225 * determining whether to show or hide the title bar overlay
226 */
227 private boolean mOptionsMenuOpen;
228
229 /**
230 * Whether or not the options menu is in its bigger, popup menu form. When
231 * true, we want the title bar overlay to be gone. When false, we do not.
232 * Only meaningful if mOptionsMenuOpen is true.
233 */
234 private boolean mExtendedMenuOpen;
235
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 private boolean mActivityPaused = true;
237 private boolean mLoadStopped;
238
239 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500240 // Checks to see when the bookmarks database has changed, and updates the
241 // Tabs' notion of whether they represent bookmarked sites.
242 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700243 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700244
Michael Kolbc3af0672011-08-09 10:24:41 -0700245 private boolean mBlockEvents;
246
Michael Kolb0b129122012-06-04 16:31:58 -0700247 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800248 private boolean mUpdateMyNavThumbnail;
249 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700250
George Mount3636d0a2011-11-21 09:08:21 -0800251 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700252 mActivity = browser;
253 mSettings = BrowserSettings.getInstance();
254 mTabControl = new TabControl(this);
255 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700256 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800257 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700258 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700259
260 mUrlHandler = new UrlHandler(this);
261 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
263
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500265 mBookmarksObserver = new ContentObserver(mHandler) {
266 @Override
267 public void onChange(boolean selfChange) {
268 int size = mTabControl.getTabCount();
269 for (int i = 0; i < size; i++) {
270 mTabControl.getTab(i).updateBookmarkedStatus();
271 }
272 }
273
274 };
275 browser.getContentResolver().registerContentObserver(
276 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700277
278 mNetworkHandler = new NetworkStateHandler(mActivity, this);
279 // Start watching the default geolocation permissions
280 mSystemAllowGeolocationOrigins =
281 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
282 mSystemAllowGeolocationOrigins.start();
283
John Reckaf262e72011-07-25 13:55:44 -0700284 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700285 }
286
John Reck9c35b9c2012-05-30 10:08:50 -0700287 @Override
288 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800289 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800290 // mCrashRecoverHandler has any previously saved state.
291 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700292 }
293
George Mount3636d0a2011-11-21 09:08:21 -0800294 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700295 // Unless the last browser usage was within 24 hours, destroy any
296 // remaining incognito tabs.
297
298 Calendar lastActiveDate = icicle != null ?
299 (Calendar) icicle.getSerializable("lastActiveDate") : null;
300 Calendar today = Calendar.getInstance();
301 Calendar yesterday = Calendar.getInstance();
302 yesterday.add(Calendar.DATE, -1);
303
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800304 // we dont want to ever recover incognito tabs
305 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700306
Patrick Scott7d50a932011-02-04 09:27:26 -0500307 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700308 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500309 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000310
Michael Kolbc831b632011-05-11 09:30:34 -0700311 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500312 // Not able to restore so we go ahead and clear session cookies. We
313 // must do this before trying to login the user as we don't want to
314 // clear any session cookies set during login.
315 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700316 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800317 if (intent == null) {
318 // This won't happen under common scenarios. The icicle is
319 // not null, but there aren't any tabs to restore.
320 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700321 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800322 final Bundle extra = intent.getExtras();
323 // Create an initial tab.
324 // If the intent is ACTION_VIEW and data is not null, the Browser is
325 // invoked to view the content by another application. In this case,
326 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800327 UrlData urlData = null;
328 if (intent.getData() != null
329 && Intent.ACTION_VIEW.equals(intent.getAction())
330 && intent.getData().toString().startsWith("content://")) {
331 urlData = new UrlData(intent.getData().toString());
332 } else {
333 urlData = IntentHandler.getUrlDataFromIntent(intent);
334 }
George Mount3636d0a2011-11-21 09:08:21 -0800335 Tab t = null;
336 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700337 String landingPage = mActivity.getResources().getString(
338 R.string.def_landing_page);
339 if (!landingPage.isEmpty()) {
340 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800341 } else {
342 t = openTabToHomePage();
343 }
George Mount3636d0a2011-11-21 09:08:21 -0800344 } else {
345 t = openTab(urlData);
346 }
347 if (t != null) {
348 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
349 }
350 WebView webView = t.getWebView();
351 if (extra != null) {
352 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
353 if (scale > 0 && scale <= 1000) {
354 webView.setInitialScale(scale);
355 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 }
357 }
John Reckd8c74522011-06-14 08:45:00 -0700358 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700360 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500361 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700362 List<Tab> tabs = mTabControl.getTabs();
363 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
364 for (Tab t : tabs) {
365 restoredTabs.add(t.getId());
366 }
367 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700368 if (tabs.size() == 0) {
369 openTabToHomePage();
370 }
John Reck1cf4b792011-07-26 10:22:22 -0700371 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700372 // TabControl.restoreState() will create a new tab even if
373 // restoring the state fails.
374 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800375 // Intent is non-null when framework thinks the browser should be
376 // launching with a new intent (icicle is null).
377 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700378 mIntentHandler.onNewIntent(intent);
379 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700380 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700381 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700382 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800383 if (jsFlags.trim().length() != 0) {
384 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700385 }
George Mount3636d0a2011-11-21 09:08:21 -0800386 if (intent != null
387 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700388 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800389 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700390 }
391
John Reck1cf4b792011-07-26 10:22:22 -0700392 private static class PruneThumbnails implements Runnable {
393 private Context mContext;
394 private List<Long> mIds;
395
396 PruneThumbnails(Context context, List<Long> preserveIds) {
397 mContext = context.getApplicationContext();
398 mIds = preserveIds;
399 }
400
401 @Override
402 public void run() {
403 ContentResolver cr = mContext.getContentResolver();
404 if (mIds == null || mIds.size() == 0) {
405 cr.delete(Thumbnails.CONTENT_URI, null, null);
406 } else {
407 int length = mIds.size();
408 StringBuilder where = new StringBuilder();
409 where.append(Thumbnails._ID);
410 where.append(" not in (");
411 for (int i = 0; i < length; i++) {
412 where.append(mIds.get(i));
413 if (i < (length - 1)) {
414 where.append(",");
415 }
416 }
417 where.append(")");
418 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
419 }
420 }
421
422 }
423
Michael Kolb1514bb72010-11-22 09:11:48 -0800424 @Override
425 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700426 return mFactory;
427 }
428
429 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800430 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800431 if (tab.hasCrashed)
432 tab.showCrashView();
433 else
434 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800435 }
436
437 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800438 public void createSubWindow(Tab tab) {
439 endActionMode();
440 WebView mainView = tab.getWebView();
441 WebView subView = mFactory.createWebView((mainView == null)
442 ? false
443 : mainView.isPrivateBrowsingEnabled());
444 mUi.createSubWindow(tab, subView);
445 }
446
447 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700448 public Context getContext() {
449 return mActivity;
450 }
451
452 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 public Activity getActivity() {
454 return mActivity;
455 }
456
457 void setUi(UI ui) {
458 mUi = ui;
459 }
460
Michael Kolbaf63dba2012-05-16 12:58:05 -0700461 @Override
462 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700463 return mSettings;
464 }
465
466 IntentHandler getIntentHandler() {
467 return mIntentHandler;
468 }
469
470 @Override
471 public UI getUi() {
472 return mUi;
473 }
474
475 int getMaxTabs() {
476 return mActivity.getResources().getInteger(R.integer.max_tabs);
477 }
478
479 @Override
480 public TabControl getTabControl() {
481 return mTabControl;
482 }
483
Michael Kolb1bf23132010-11-19 12:55:12 -0800484 @Override
485 public List<Tab> getTabs() {
486 return mTabControl.getTabs();
487 }
488
John Reckaf262e72011-07-25 13:55:44 -0700489 // Open the icon database.
490 private void openIconDatabase() {
491 // We have to call getInstance on the UI thread
492 final WebIconDatabase instance = WebIconDatabase.getInstance();
493 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000494
John Reckaf262e72011-07-25 13:55:44 -0700495 @Override
496 public void run() {
497 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700498 }
John Reckaf262e72011-07-25 13:55:44 -0700499 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700500 }
501
502 private void startHandler() {
503 mHandler = new Handler() {
504
505 @Override
506 public void handleMessage(Message msg) {
507 switch (msg.what) {
508 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700509 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700510 break;
511 case FOCUS_NODE_HREF:
512 {
513 String url = (String) msg.getData().get("url");
514 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500515 String src = (String) msg.getData().get("src");
516 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700517 if (TextUtils.isEmpty(url)) {
518 break;
519 }
520 HashMap focusNodeMap = (HashMap) msg.obj;
521 WebView view = (WebView) focusNodeMap.get("webview");
522 // Only apply the action if the top window did not change.
523 if (getCurrentTopWebView() != view) {
524 break;
525 }
526 switch (msg.arg1) {
527 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700528 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700529 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500530 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700531 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500532 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500533 case R.id.open_newtab_context_menu_id:
534 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700535 openTab(url, parent,
536 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500537 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700538 case R.id.copy_link_context_menu_id:
539 copy(url);
540 break;
541 case R.id.save_link_context_menu_id:
542 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500543 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800544 mActivity, url, view.getSettings().getUserAgentString(),
545 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700547 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700548 if(title == null || title == "")
549 title = url;
550
551 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
552 //SWE TODO: No thumbnail support for the url obtained via
553 //browser context menu as its not loaded in webview.
554 if (bookmarkIntent != null) {
555 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
556 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
557 mActivity.startActivity(bookmarkIntent);
558 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700559 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 }
561 break;
562 }
563
564 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700565 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700566 break;
567
568 case STOP_LOAD:
569 stopLoading();
570 break;
571
572 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700573 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700574 mWakeLock.release();
575 // if we reach here, Browser should be still in the
576 // background loading after WAKELOCK_TIMEOUT (5-min).
577 // To avoid burning the battery, stop loading.
578 mTabControl.stopAllLoading();
579 }
580 break;
581
582 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800583 Tab tab = (Tab) msg.obj;
584 if (tab != null) {
585 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700586 }
587 break;
kaiyiz591110b2013-08-06 17:11:06 +0800588 case OPEN_MENU:
589 if (!mOptionsMenuOpen && mActivity != null ) {
590 mActivity.openOptionsMenu();
591 }
592 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 }
594 }
595 };
596
597 }
598
John Reckef654f12011-07-12 16:42:08 -0700599 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200600 public Tab getCurrentTab() {
601 return mTabControl.getCurrentTab();
602 }
603
Michael Kolbba99c5d2010-11-29 14:57:41 -0800604 @Override
605 public void shareCurrentPage() {
606 shareCurrentPage(mTabControl.getCurrentTab());
607 }
608
609 private void shareCurrentPage(Tab tab) {
610 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800611 sharePage(mActivity, tab.getTitle(),
612 tab.getUrl(), tab.getFavicon(),
613 createScreenshot(tab.getWebView(),
614 getDesiredThumbnailWidth(mActivity),
615 getDesiredThumbnailHeight(mActivity)));
616 }
617 }
618
Michael Kolb8233fac2010-10-26 16:08:53 -0700619 /**
620 * Share a page, providing the title, url, favicon, and a screenshot. Uses
621 * an {@link Intent} to launch the Activity chooser.
622 * @param c Context used to launch a new Activity.
623 * @param title Title of the page. Stored in the Intent with
624 * {@link Intent#EXTRA_SUBJECT}
625 * @param url URL of the page. Stored in the Intent with
626 * {@link Intent#EXTRA_TEXT}
627 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
628 * with {@link Browser#EXTRA_SHARE_FAVICON}
629 * @param screenshot Bitmap of a screenshot of the page. Stored in the
630 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
631 */
632 static final void sharePage(Context c, String title, String url,
633 Bitmap favicon, Bitmap screenshot) {
634 Intent send = new Intent(Intent.ACTION_SEND);
635 send.setType("text/plain");
636 send.putExtra(Intent.EXTRA_TEXT, url);
637 send.putExtra(Intent.EXTRA_SUBJECT, title);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800638 send.putExtra(EXTRA_SHARE_FAVICON, favicon);
639 send.putExtra(EXTRA_SHARE_SCREENSHOT, screenshot);
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 try {
641 c.startActivity(Intent.createChooser(send, c.getString(
642 R.string.choosertitle_sharevia)));
643 } catch(android.content.ActivityNotFoundException ex) {
644 // if no app handles it, do nothing
645 }
646 }
647
648 private void copy(CharSequence text) {
649 ClipboardManager cm = (ClipboardManager) mActivity
650 .getSystemService(Context.CLIPBOARD_SERVICE);
651 cm.setText(text);
652 }
653
654 // lifecycle
655
John Reck9c35b9c2012-05-30 10:08:50 -0700656 @Override
657 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700658 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800659 // update the menu in case of a locale change
660 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800661 if (mOptionsMenuOpen) {
662 mActivity.closeOptionsMenu();
663 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
664 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 if (mPageDialogsHandler != null) {
666 mPageDialogsHandler.onConfigurationChanged(config);
667 }
668 mUi.onConfigurationChanged(config);
669 }
670
671 @Override
672 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700673 if (!mUi.isWebShowing()) {
674 mUi.showWeb(false);
675 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 mIntentHandler.onNewIntent(intent);
677 }
678
John Reck9c35b9c2012-05-30 10:08:50 -0700679 @Override
680 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800681 if (mUi.isCustomViewShowing()) {
682 hideCustomView();
683 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 if (mActivityPaused) {
685 Log.e(LOGTAG, "BrowserActivity is already paused.");
686 return;
687 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800689 Tab tab = mTabControl.getCurrentTab();
690 if (tab != null) {
691 tab.pause();
692 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700693 if (mWakeLock == null) {
694 PowerManager pm = (PowerManager) mActivity
695 .getSystemService(Context.POWER_SERVICE);
696 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
697 }
Michael Kolb70976932010-11-30 11:34:01 -0800698 mWakeLock.acquire();
699 mHandler.sendMessageDelayed(mHandler
700 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
701 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 }
703 mUi.onPause();
704 mNetworkHandler.onPause();
705
706 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100707 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700708 if (sThumbnailBitmap != null) {
709 sThumbnailBitmap.recycle();
710 sThumbnailBitmap = null;
711 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 }
713
John Reck9c35b9c2012-05-30 10:08:50 -0700714 @Override
715 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700716 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800717 Bundle saveState = createSaveState();
718
719 // crash recovery manages all save & restore state
720 mCrashRecoveryHandler.writeState(saveState);
721 mSettings.setLastRunPaused(true);
722 }
723
724 /**
725 * Save the current state to outState. Does not write the state to
726 * disk.
727 * @return Bundle containing the current state of all tabs.
728 */
729 /* package */ Bundle createSaveState() {
730 Bundle saveState = new Bundle();
731 mTabControl.saveState(saveState);
732 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700733 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800734 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700735 }
George Mount3636d0a2011-11-21 09:08:21 -0800736 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 }
738
John Reck9c35b9c2012-05-30 10:08:50 -0700739 @Override
740 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700741 if (!mActivityPaused) {
742 Log.e(LOGTAG, "BrowserActivity is already resumed.");
743 return;
744 }
George Mount3636d0a2011-11-21 09:08:21 -0800745 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700746 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800747 Tab current = mTabControl.getCurrentTab();
748 if (current != null) {
749 current.resume();
750 resumeWebViewTimers(current);
751 }
John Reckf57c0292011-07-21 18:15:39 -0700752 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200753
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 mUi.onResume();
755 mNetworkHandler.onResume();
756 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100757 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700758 if (mVoiceResult != null) {
759 mUi.onVoiceResult(mVoiceResult);
760 mVoiceResult = null;
761 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800762 if (current != null && current.hasCrashed) {
763 current.showCrashView();
764 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700765 }
766
John Reckf57c0292011-07-21 18:15:39 -0700767 private void releaseWakeLock() {
768 if (mWakeLock != null && mWakeLock.isHeld()) {
769 mHandler.removeMessages(RELEASE_WAKELOCK);
770 mWakeLock.release();
771 }
772 }
773
Michael Kolb70976932010-11-30 11:34:01 -0800774 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800775 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800776 * @param tab guaranteed non-null
777 */
778 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700779 boolean inLoad = tab.inPageLoad();
780 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
781 CookieSyncManager.getInstance().startSync();
782 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100783 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700784 }
785 }
786
Michael Kolb70976932010-11-30 11:34:01 -0800787 /**
788 * Pause all WebView timers using the WebView of the given tab
789 * @param tab
790 * @return true if the timers are paused or tab is null
791 */
792 private boolean pauseWebViewTimers(Tab tab) {
793 if (tab == null) {
794 return true;
795 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700796 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100797 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 }
Michael Kolb70976932010-11-30 11:34:01 -0800800 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700801 }
802
John Reck9c35b9c2012-05-30 10:08:50 -0700803 @Override
804 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800805 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
807 mUploadHandler = null;
808 }
809 if (mTabControl == null) return;
810 mUi.onDestroy();
811 // Remove the current tab and sub window
812 Tab t = mTabControl.getCurrentTab();
813 if (t != null) {
814 dismissSubWindow(t);
815 removeTab(t);
816 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500817 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700818 // Destroy all the tabs
819 mTabControl.destroy();
820 WebIconDatabase.getInstance().close();
821 // Stop watching the default geolocation permissions
822 mSystemAllowGeolocationOrigins.stop();
823 mSystemAllowGeolocationOrigins = null;
824 }
825
826 protected boolean isActivityPaused() {
827 return mActivityPaused;
828 }
829
John Reck9c35b9c2012-05-30 10:08:50 -0700830 @Override
831 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 mTabControl.freeMemory();
833 }
834
835 @Override
836 public boolean shouldShowErrorConsole() {
837 return mShouldShowErrorConsole;
838 }
839
840 protected void setShouldShowErrorConsole(boolean show) {
841 if (show == mShouldShowErrorConsole) {
842 // Nothing to do.
843 return;
844 }
845 mShouldShowErrorConsole = show;
846 Tab t = mTabControl.getCurrentTab();
847 if (t == null) {
848 // There is no current tab so we cannot toggle the error console
849 return;
850 }
851 mUi.setShouldShowErrorConsole(t, show);
852 }
853
854 @Override
855 public void stopLoading() {
856 mLoadStopped = true;
857 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700859 if (w != null) {
860 w.stopLoading();
861 mUi.onPageStopped(tab);
862 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 }
864
865 boolean didUserStopLoading() {
866 return mLoadStopped;
867 }
868
kaiyiza016da12013-08-26 17:50:22 +0800869 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700870 final String reminderType = getContext().getResources().getString(
871 R.string.def_wifi_browser_interaction_remind_type);
872 final String selectionConnnection = getContext().getResources().getString(
873 R.string.def_action_wifi_selection_data_connections);
874
875 if (reminderType.isEmpty() || selectionConnnection.isEmpty())
876 return;
877
kaiyiza016da12013-08-26 17:50:22 +0800878 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))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700884 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
885 reminderType, NETWORK_SWITCH_TYPE_OK);
886 if (isReminder == NETWORK_SWITCH_TYPE_OK) {
887 mNetworkShouldNotify = false;
888 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700889 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
890 this.getContext().startActivity(intent);
891 } else {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700892 if (!mNetworkHandler.isNetworkUp())
Vivek Sekharb54614f2014-05-01 19:03:37 -0700893 view.setNetworkAvailable(false);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700894 }
895 }
896 }
897
898 /***
899 * Add/remove a Javascript interface for a local default homepage only
900 */
901 private void handleJsInterface(WebView webview){
902 if (webview.getUrl() != null &&
903 webview.getUrl().equals(mActivity.getResources().getString(R.string.homepage_base)) &&
904 webview.getUrl().startsWith("file:///")) {
905 mJsInterfaceEnabled = true;
906 webview.getSettings().setJavaScriptEnabled(true);
907 webview.addJavascriptInterface(mActivity, "default_homepage");
908 } else {
909 if (mJsInterfaceEnabled) {
910 webview.removeJavascriptInterface("default_homepage");
911 mJsInterfaceEnabled = false;
kaiyiza016da12013-08-26 17:50:22 +0800912 }
913 }
914 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700915
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 // WebViewController
917
918 @Override
John Reck324d4402011-01-11 16:56:42 -0800919 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700920
921 // We've started to load a new page. If there was a pending message
922 // to save a screenshot then we will now take the new page and save
923 // an incorrect screenshot. Therefore, remove any pending thumbnail
924 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700925 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700926
927 // reset sync timer to avoid sync starts during loading a page
928 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700929 WifiManager wifiMgr = (WifiManager) this.getContext()
930 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700931 boolean networkNotifier =
932 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
933 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700934 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800935 } else {
936 if (!mNetworkHandler.isNetworkUp()) {
937 view.setNetworkAvailable(false);
938 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700939 }
940
941 // when BrowserActivity just starts, onPageStarted may be called before
942 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
943 // to start the timer. As we won't switch tabs while an activity is in
944 // pause state, we can ensure calling resume and pause in pair.
945 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800946 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700947 }
948 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700949 endActionMode();
950
John Reck30c714c2010-12-16 17:30:34 -0800951 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700952
John Reck324d4402011-01-11 16:56:42 -0800953 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700954 // update the bookmark database for favicon
955 maybeUpdateFavicon(tab, null, url, favicon);
956
957 Performance.tracePageStart(url);
958
959 // Performance probe
960 if (false) {
961 Performance.onPageStarted();
962 }
963
964 }
965
966 @Override
John Reck324d4402011-01-11 16:56:42 -0800967 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700968 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800969 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200970
Michael Kolb8233fac2010-10-26 16:08:53 -0700971 // Performance probe
972 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800973 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700974 }
975
976 Performance.tracePageFinished();
977 }
978
979 @Override
John Reck30c714c2010-12-16 17:30:34 -0800980 public void onProgressChanged(Tab tab) {
981 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700982
983 if (newProgress == 100) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700984 handleJsInterface(tab.getWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 CookieSyncManager.getInstance().sync();
986 // onProgressChanged() may continue to be called after the main
987 // frame has finished loading, as any remaining sub frames continue
988 // to load. We'll only get called once though with newProgress as
989 // 100 when everything is loaded. (onPageFinished is called once
990 // when the main frame completes loading regardless of the state of
991 // any sub frames so calls to onProgressChanges may continue after
992 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800993 if (tab.inPageLoad()) {
994 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200995 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
996 // pause the WebView timer and release the wake lock if it is
997 // finished while BrowserActivity is in pause state.
998 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700999 }
John Reckd9862372012-02-21 15:04:50 -08001000 if (!tab.isPrivateBrowsingEnabled()
1001 && !TextUtils.isEmpty(tab.getUrl())
1002 && !tab.isSnapshot()) {
1003 // Only update the bookmark screenshot if the user did not
1004 // cancel the load early and there is not already
1005 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001006 if (tab.shouldUpdateThumbnail() &&
1007 (tab.inForeground() && !didUserStopLoading()
1008 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001009 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1010 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1011 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001012 1500);
John Reckd9862372012-02-21 15:04:50 -08001013 }
1014 }
1015 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001016 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001017 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001019 // still loading
1020 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001021 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001022 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001023 }
1024 }
John Reck30c714c2010-12-16 17:30:34 -08001025 mUi.onProgressChanged(tab);
1026 }
1027
1028 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001029 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001030 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 }
1032
1033 @Override
1034 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001035 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001036 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001037 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1039 return;
1040 }
1041 // Update the title in the history database if not in private browsing mode
1042 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001043 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 }
1045 }
1046
1047 @Override
1048 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001049 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1051 }
1052
1053 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001054 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1055 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 }
1057
1058 @Override
1059 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1060 if (mMenuIsDown) {
1061 // only check shortcut key when MENU is held
1062 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1063 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001065 int keyCode = event.getKeyCode();
1066 // We need to send almost every key to WebKit. However:
1067 // 1. We don't want to block the device on the renderer for
1068 // some keys like menu, home, call.
1069 // 2. There are no WebKit equivalents for some of these keys
1070 // (see app/keyboard_codes_win.h)
1071 // Note that these are not the same set as KeyEvent.isSystemKey:
1072 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1073 if (keyCode == KeyEvent.KEYCODE_MENU ||
1074 keyCode == KeyEvent.KEYCODE_HOME ||
1075 keyCode == KeyEvent.KEYCODE_BACK ||
1076 keyCode == KeyEvent.KEYCODE_CALL ||
1077 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1078 keyCode == KeyEvent.KEYCODE_POWER ||
1079 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1080 keyCode == KeyEvent.KEYCODE_CAMERA ||
1081 keyCode == KeyEvent.KEYCODE_FOCUS ||
1082 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1083 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1084 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1085 return true;
1086 }
1087
1088 // We also have to intercept some shortcuts before we send them to the ContentView.
1089 if (event.isCtrlPressed() && (
1090 keyCode == KeyEvent.KEYCODE_TAB ||
1091 keyCode == KeyEvent.KEYCODE_W ||
1092 keyCode == KeyEvent.KEYCODE_F4)) {
1093 return true;
1094 }
1095
1096 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 }
1098
1099 @Override
John Reck997b1b72012-04-19 18:08:25 -07001100 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001101 if (!isActivityPaused()) {
1102 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001103 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001104 } else {
John Reck997b1b72012-04-19 18:08:25 -07001105 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 }
1107 }
John Reck997b1b72012-04-19 18:08:25 -07001108 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 }
1110
1111 @Override
John Reck324d4402011-01-11 16:56:42 -08001112 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001113 boolean disableHistoryWrites =
1114 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1115
1116 // Don't save anything in private browsing mode or when explicitly set
1117 // not to write history via an overlay
1118 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001119 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001120
John Reck324d4402011-01-11 16:56:42 -08001121 if (TextUtils.isEmpty(url)
1122 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001123 return;
1124 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001125
John Reckf57c0292011-07-21 18:15:39 -07001126 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001127 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001128 }
1129
1130 @Override
1131 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1132 AsyncTask<Void, Void, String[]> task =
1133 new AsyncTask<Void, Void, String[]>() {
1134 @Override
1135 public String[] doInBackground(Void... unused) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001136 Object[] params = {mActivity.getContentResolver()};
1137 Class[] type = new Class[] {ContentResolver.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001138 return (String[])ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001139 "android.provider.Browser","getVisitedHistory",
1140 type, params);
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 }
1142 @Override
1143 public void onPostExecute(String[] result) {
1144 callback.onReceiveValue(result);
1145 }
1146 };
1147 task.execute();
1148 }
1149
1150 @Override
1151 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1152 final HttpAuthHandler handler, final String host,
1153 final String realm) {
1154 String username = null;
1155 String password = null;
1156
1157 boolean reuseHttpAuthUsernamePassword
1158 = handler.useHttpAuthUsernamePassword();
1159
1160 if (reuseHttpAuthUsernamePassword && view != null) {
1161 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1162 if (credentials != null && credentials.length == 2) {
1163 username = credentials[0];
1164 password = credentials[1];
1165 }
1166 }
1167
1168 if (username != null && password != null) {
1169 handler.proceed(username, password);
1170 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001171 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1173 } else {
1174 handler.cancel();
1175 }
1176 }
1177 }
1178
1179 @Override
1180 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001181 String contentDisposition, String mimetype, String referer,
1182 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001183 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001184 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001185 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001186 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001187 // This Tab was opened for the sole purpose of downloading a
1188 // file. Remove it.
1189 if (tab == mTabControl.getCurrentTab()) {
1190 // In this case, the Tab is still on top.
1191 goBackOnePageOrQuit();
1192 } else {
1193 // In this case, it is not.
1194 closeTab(tab);
1195 }
1196 }
1197 }
1198
1199 @Override
1200 public Bitmap getDefaultVideoPoster() {
1201 return mUi.getDefaultVideoPoster();
1202 }
1203
1204 @Override
1205 public View getVideoLoadingProgressView() {
1206 return mUi.getVideoLoadingProgressView();
1207 }
1208
1209 @Override
1210 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1211 SslError error) {
1212 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1213 }
1214
1215 // helper method
1216
1217 /*
1218 * Update the favorites icon if the private browsing isn't enabled and the
1219 * icon is valid.
1220 */
1221 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1222 final String url, Bitmap favicon) {
1223 if (favicon == null) {
1224 return;
1225 }
1226 if (!tab.isPrivateBrowsingEnabled()) {
1227 Bookmarks.updateFavicon(mActivity
1228 .getContentResolver(), originalUrl, url, favicon);
1229 }
1230 }
1231
Leon Scroggins4cd97792010-12-03 15:31:56 -05001232 @Override
1233 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001234 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001235 mUi.bookmarkedStatusHasChanged(tab);
1236 }
1237
Michael Kolb8233fac2010-10-26 16:08:53 -07001238 // end WebViewController
1239
1240 protected void pageUp() {
1241 getCurrentTopWebView().pageUp(false);
1242 }
1243
1244 protected void pageDown() {
1245 getCurrentTopWebView().pageDown(false);
1246 }
1247
1248 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001249 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001250 public void editUrl() {
1251 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001252 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001253 }
1254
John Reck9c35b9c2012-05-30 10:08:50 -07001255 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001256 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001257 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001258 if (tab.inForeground()) {
1259 if (mUi.isCustomViewShowing()) {
1260 callback.onCustomViewHidden();
1261 return;
1262 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001263 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 // Save the menu state and set it to empty while the custom
1265 // view is showing.
1266 mOldMenuState = mMenuState;
1267 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001268 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001269 }
1270 }
1271
1272 @Override
1273 public void hideCustomView() {
1274 if (mUi.isCustomViewShowing()) {
1275 mUi.onHideCustomView();
1276 // Reset the old menu state.
1277 mMenuState = mOldMenuState;
1278 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001279 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001280 }
1281 }
1282
John Reck9c35b9c2012-05-30 10:08:50 -07001283 @Override
1284 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001285 Intent intent) {
1286 if (getCurrentTopWebView() == null) return;
1287 switch (requestCode) {
1288 case PREFERENCES_PAGE:
1289 if (resultCode == Activity.RESULT_OK && intent != null) {
1290 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001291 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 mTabControl.removeParentChildRelationShips();
1293 }
1294 }
1295 break;
1296 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001297 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001298 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001299 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001300 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001301 case AUTOFILL_SETUP:
1302 // Determine whether a profile was actually set up or not
1303 // and if so, send the message back to the WebTextView to
1304 // fill the form with the new profile.
1305 if (getSettings().getAutoFillProfile() != null) {
1306 mAutoFillSetupMessage.sendToTarget();
1307 mAutoFillSetupMessage = null;
1308 }
1309 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001310 case COMBO_VIEW:
1311 if (intent == null || resultCode != Activity.RESULT_OK) {
1312 break;
1313 }
John Reck3ba45532011-08-11 16:26:53 -07001314 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001315 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1316 Tab t = getCurrentTab();
1317 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001318 mUpdateMyNavThumbnail = true;
1319 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001320 loadUrl(t, uri.toString());
1321 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1322 String[] urls = intent.getStringArrayExtra(
1323 ComboViewActivity.EXTRA_OPEN_ALL);
1324 Tab parent = getCurrentTab();
1325 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001326 if (url != null) {
1327 parent = openTab(url, parent,
1328 !mSettings.openInBackground(), true);
1329 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001330 }
1331 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1332 long id = intent.getLongExtra(
1333 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1334 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001335 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001336 }
1337 }
1338 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001339 case VOICE_RESULT:
1340 if (resultCode == Activity.RESULT_OK && intent != null) {
1341 ArrayList<String> results = intent.getStringArrayListExtra(
1342 RecognizerIntent.EXTRA_RESULTS);
1343 if (results.size() >= 1) {
1344 mVoiceResult = results.get(0);
1345 }
1346 }
1347 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001348 case MY_NAVIGATION:
1349 if (intent == null || resultCode != Activity.RESULT_OK) {
1350 break;
1351 }
1352
1353 if (intent.getBooleanExtra("need_refresh", false) &&
1354 getCurrentTopWebView() != null) {
1355 getCurrentTopWebView().reload();
1356 }
1357 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 default:
1359 break;
1360 }
1361 getCurrentTopWebView().requestFocus();
1362 }
1363
1364 /**
1365 * Open the Go page.
1366 * @param startWithHistory If true, open starting on the history tab.
1367 * Otherwise, start with the bookmarks tab.
1368 */
1369 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001370 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001371 if (mTabControl.getCurrentWebView() == null) {
1372 return;
1373 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001374 // clear action mode
1375 if (isInCustomActionMode()) {
1376 endActionMode();
1377 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001378 Bundle extras = new Bundle();
1379 // Disable opening in a new window if we have maxed out the windows
1380 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1381 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001382 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001383 }
1384
1385 // combo view callbacks
1386
Michael Kolb8233fac2010-10-26 16:08:53 -07001387 // key handling
1388 protected void onBackKey() {
1389 if (!mUi.onBackKey()) {
1390 WebView subwindow = mTabControl.getCurrentSubWindow();
1391 if (subwindow != null) {
1392 if (subwindow.canGoBack()) {
1393 subwindow.goBack();
1394 } else {
1395 dismissSubWindow(mTabControl.getCurrentTab());
1396 }
1397 } else {
1398 goBackOnePageOrQuit();
1399 }
1400 }
1401 }
1402
Michael Kolb4bd767d2011-05-27 11:33:55 -07001403 protected boolean onMenuKey() {
1404 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001405 }
1406
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 // menu handling and state
1408 // TODO: maybe put into separate handler
1409
John Reck9c35b9c2012-05-30 10:08:50 -07001410 @Override
1411 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001412 if (mMenuState == EMPTY_MENU) {
1413 return false;
1414 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001415 MenuInflater inflater = mActivity.getMenuInflater();
1416 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 return true;
1418 }
1419
John Reck9c35b9c2012-05-30 10:08:50 -07001420 @Override
1421 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001422 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001423 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001424 return;
1425 }
1426 if (!(v instanceof WebView)) {
1427 return;
1428 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001429 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 WebView.HitTestResult result = webview.getHitTestResult();
1431 if (result == null) {
1432 return;
1433 }
1434
1435 int type = result.getType();
1436 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1437 Log.w(LOGTAG,
1438 "We should not show context menu when nothing is touched");
1439 return;
1440 }
1441 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1442 // let TextView handles context menu
1443 return;
1444 }
1445
1446 // Note, http://b/issue?id=1106666 is requesting that
1447 // an inflated menu can be used again. This is not available
1448 // yet, so inflate each time (yuk!)
1449 MenuInflater inflater = mActivity.getMenuInflater();
1450 inflater.inflate(R.menu.browsercontext, menu);
1451
1452 // Show the correct menu group
1453 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001454 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001455 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001456 menu.setGroupVisible(R.id.PHONE_MENU,
1457 type == WebView.HitTestResult.PHONE_TYPE);
1458 menu.setGroupVisible(R.id.EMAIL_MENU,
1459 type == WebView.HitTestResult.EMAIL_TYPE);
1460 menu.setGroupVisible(R.id.GEO_MENU,
1461 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001462 String itemUrl = null;
1463 String url = webview.getOriginalUrl();
1464 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1465 itemUrl = Uri.decode(navigationUrl);
1466 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1467 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1468 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1469 } else {
1470 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1471 }
1472 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1473 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1474 } else {
1475 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1476
1477 menu.setGroupVisible(R.id.IMAGE_MENU,
1478 type == WebView.HitTestResult.IMAGE_TYPE
1479 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1480 menu.setGroupVisible(R.id.ANCHOR_MENU,
1481 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1482 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001483 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1484 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001485 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001486 // Setup custom handling depending on the type
1487 switch (type) {
1488 case WebView.HitTestResult.PHONE_TYPE:
1489 menu.setHeaderTitle(Uri.decode(extra));
1490 menu.findItem(R.id.dial_context_menu_id).setIntent(
1491 new Intent(Intent.ACTION_VIEW, Uri
1492 .parse(WebView.SCHEME_TEL + extra)));
1493 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1494 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1495 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1496 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1497 addIntent);
1498 menu.findItem(R.id.copy_phone_context_menu_id)
1499 .setOnMenuItemClickListener(
1500 new Copy(extra));
1501 break;
1502
1503 case WebView.HitTestResult.EMAIL_TYPE:
1504 menu.setHeaderTitle(extra);
1505 menu.findItem(R.id.email_context_menu_id).setIntent(
1506 new Intent(Intent.ACTION_VIEW, Uri
1507 .parse(WebView.SCHEME_MAILTO + extra)));
1508 menu.findItem(R.id.copy_mail_context_menu_id)
1509 .setOnMenuItemClickListener(
1510 new Copy(extra));
1511 break;
1512
1513 case WebView.HitTestResult.GEO_TYPE:
1514 menu.setHeaderTitle(extra);
1515 menu.findItem(R.id.map_context_menu_id).setIntent(
1516 new Intent(Intent.ACTION_VIEW, Uri
1517 .parse(WebView.SCHEME_GEO
1518 + URLEncoder.encode(extra))));
1519 menu.findItem(R.id.copy_geo_context_menu_id)
1520 .setOnMenuItemClickListener(
1521 new Copy(extra));
1522 break;
1523
1524 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1525 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001526 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001527 // decide whether to show the open link in new tab option
1528 boolean showNewTab = mTabControl.canCreateNewTab();
1529 MenuItem newTabItem
1530 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001531 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001532 ? R.string.contextmenu_openlink_newwindow_background
1533 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001534 newTabItem.setVisible(showNewTab);
1535 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001536 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1537 newTabItem.setOnMenuItemClickListener(
1538 new MenuItem.OnMenuItemClickListener() {
1539 @Override
1540 public boolean onMenuItemClick(MenuItem item) {
1541 final HashMap<String, WebView> hrefMap =
1542 new HashMap<String, WebView>();
1543 hrefMap.put("webview", webview);
1544 final Message msg = mHandler.obtainMessage(
1545 FOCUS_NODE_HREF,
1546 R.id.open_newtab_context_menu_id,
1547 0, hrefMap);
1548 webview.requestFocusNodeHref(msg);
1549 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001550 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001551 });
1552 } else {
1553 newTabItem.setOnMenuItemClickListener(
1554 new MenuItem.OnMenuItemClickListener() {
1555 @Override
1556 public boolean onMenuItemClick(MenuItem item) {
1557 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001558 openTab(extra, parent,
1559 !mSettings.openInBackground(),
1560 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001561 return true;
1562 }
1563 });
1564 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001565 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001566 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1567 menu.setHeaderTitle(navigationUrl);
1568 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1569
1570 if (itemUrl != null) {
1571 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1572 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1573 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1574 @Override
1575 public boolean onMenuItemClick(MenuItem item) {
1576 final Intent intent = new Intent(Controller.this
1577 .getContext(),
1578 AddMyNavigationPage.class);
1579 Bundle bundle = new Bundle();
1580 String url = Uri.decode(navigationUrl);
1581 bundle.putBoolean("isAdding", false);
1582 bundle.putString("url", url);
1583 bundle.putString("name", getNameFromUrl(url));
1584 intent.putExtra("websites", bundle);
1585 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1586 return false;
1587 }
1588 });
1589 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1590 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1591 @Override
1592 public boolean onMenuItemClick(MenuItem item) {
1593 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1594 return false;
1595 }
1596 });
1597 }
1598 } else {
1599 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1600 }
1601 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1603 break;
1604 }
1605 // otherwise fall through to handle image part
1606 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001607 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1608 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1610 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001611 shareItem.setOnMenuItemClickListener(
1612 new MenuItem.OnMenuItemClickListener() {
1613 @Override
1614 public boolean onMenuItemClick(MenuItem item) {
1615 sharePage(mActivity, null, extra, null,
1616 null);
1617 return true;
1618 }
1619 }
1620 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001622 menu.findItem(R.id.view_image_context_menu_id)
1623 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1624 @Override
1625 public boolean onMenuItemClick(MenuItem item) {
1626 openTab(extra, mTabControl.getCurrentTab(), true, true);
1627 return false;
1628 }
1629 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001630 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1631 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1632 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001633 menu.findItem(R.id.set_wallpaper_context_menu_id).
1634 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1635 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001636 break;
1637
1638 default:
1639 Log.w(LOGTAG, "We should not get here.");
1640 break;
1641 }
1642 //update the ui
1643 mUi.onContextMenuCreated(menu);
1644 }
1645
kaiyiz6e5b3e02013-08-19 20:02:01 +08001646 public void startAddMyNavigation(String url) {
1647 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1648 Bundle bundle = new Bundle();
1649 bundle.putBoolean("isAdding", true);
1650 bundle.putString("url", url);
1651 bundle.putString("name", getNameFromUrl(url));
1652 intent.putExtra("websites", bundle);
1653 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1654 }
1655
1656 private void showMyNavigationDeleteDialog(final String itemUrl) {
1657 new AlertDialog.Builder(this.getContext())
1658 .setTitle(R.string.my_navigation_delete_label)
1659 .setIcon(android.R.drawable.ic_dialog_alert)
1660 .setMessage(R.string.my_navigation_delete_msg)
1661 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1662 @Override
1663 public void onClick(DialogInterface dialog, int whichButton) {
1664 deleteMyNavigationItem(itemUrl);
1665 }
1666 })
1667 .setNegativeButton(R.string.cancel, null)
1668 .show();
1669 }
1670
1671 private void deleteMyNavigationItem(final String itemUrl) {
1672 ContentResolver cr = this.getContext().getContentResolver();
1673 Cursor cursor = null;
1674
1675 try {
1676 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1677 new String[] {
1678 MyNavigationUtil.ID
1679 }, "url = ?", new String[] {
1680 itemUrl
1681 }, null);
1682 if (null != cursor && cursor.moveToFirst()) {
1683 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1684 cursor.getLong(0));
1685
1686 ContentValues values = new ContentValues();
1687 values.put(MyNavigationUtil.TITLE, "");
1688 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1689 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1690 ByteArrayOutputStream os = new ByteArrayOutputStream();
1691 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1692 R.raw.my_navigation_add);
1693 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1694 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1695 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1696 cr.update(uri, values, null, null);
1697 } else {
1698 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1699 }
1700 } catch (IllegalStateException e) {
1701 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1702 } finally {
1703 if (null != cursor) {
1704 cursor.close();
1705 }
1706 }
1707
1708 if (getCurrentTopWebView() != null) {
1709 getCurrentTopWebView().reload();
1710 }
1711 }
1712
1713 private String getNameFromUrl(String itemUrl) {
1714 ContentResolver cr = this.getContext().getContentResolver();
1715 Cursor cursor = null;
1716 String name = null;
1717
1718 try {
1719 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1720 new String[] {
1721 MyNavigationUtil.TITLE
1722 }, "url = ?", new String[] {
1723 itemUrl
1724 }, null);
1725 if (null != cursor && cursor.moveToFirst()) {
1726 name = cursor.getString(0);
1727 } else {
1728 Log.e(LOGTAG, "this item does not exist!");
1729 }
1730 } catch (IllegalStateException e) {
1731 Log.e(LOGTAG, "getNameFromUrl", e);
1732 } finally {
1733 if (null != cursor) {
1734 cursor.close();
1735 }
1736 }
1737 return name;
1738 }
1739
1740 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1741 int width = mActivity.getResources().getDimensionPixelOffset(
1742 R.dimen.myNavigationThumbnailWidth);
1743 int height = mActivity.getResources().getDimensionPixelOffset(
1744 R.dimen.myNavigationThumbnailHeight);
1745
1746 final Bitmap bm = createScreenshot(webView, width, height);
1747
1748 if (bm == null) {
1749 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1750 return;
1751 }
1752
1753 final ContentResolver cr = mActivity.getContentResolver();
1754 new AsyncTask<Void, Void, Void>() {
1755 @Override
1756 protected Void doInBackground(Void... unused) {
1757 ContentResolver cr = mActivity.getContentResolver();
1758 Cursor cursor = null;
1759 try {
1760 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1761 new String[] {
1762 MyNavigationUtil.ID
1763 }, "url = ?", new String[] {
1764 itemUrl
1765 }, null);
1766 if (null != cursor && cursor.moveToFirst()) {
1767 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1768 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1769
1770 ContentValues values = new ContentValues();
1771 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1772 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1773 cursor.getLong(0));
1774 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1775 cr.update(uri, values, null, null);
1776 os.close();
1777 }
1778 } catch (IllegalStateException e) {
1779 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1780 } catch (IOException e) {
1781 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1782 } finally {
1783 if (null != cursor) {
1784 cursor.close();
1785 }
1786 }
1787 return null;
1788 }
1789 }.execute();
1790 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001791 /**
1792 * As the menu can be open when loading state changes
1793 * we must manually update the state of the stop/reload menu
1794 * item
1795 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001796 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001797 if (menu == null) {
1798 return;
1799 }
1800 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001801 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001802 menu.findItem(R.id.stop_menu_id):
1803 menu.findItem(R.id.reload_menu_id);
1804 if (src != null) {
1805 dest.setIcon(src.getIcon());
1806 dest.setTitle(src.getTitle());
1807 }
1808 }
1809
John Reck9c35b9c2012-05-30 10:08:50 -07001810 @Override
1811 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001812 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001813 // hold on to the menu reference here; it is used by the page callbacks
1814 // to update the menu based on loading state
1815 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001816 // Note: setVisible will decide whether an item is visible; while
1817 // setEnabled() will decide whether an item is enabled, which also means
1818 // whether the matching shortcut key will function.
1819 switch (mMenuState) {
1820 case EMPTY_MENU:
1821 if (mCurrentMenuState != mMenuState) {
1822 menu.setGroupVisible(R.id.MAIN_MENU, false);
1823 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1824 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1825 }
1826 break;
1827 default:
1828 if (mCurrentMenuState != mMenuState) {
1829 menu.setGroupVisible(R.id.MAIN_MENU, true);
1830 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1831 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1832 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001833 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001834 break;
1835 }
1836 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001837 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001838 }
1839
Michael Kolb4bf79712011-07-14 14:18:12 -07001840 @Override
1841 public void updateMenuState(Tab tab, Menu menu) {
1842 boolean canGoBack = false;
1843 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001844 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001845 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001846 if (tab != null) {
1847 canGoBack = tab.canGoBack();
1848 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001849 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001850 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001851 }
1852 final MenuItem back = menu.findItem(R.id.back_menu_id);
1853 back.setEnabled(canGoBack);
1854
1855 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001856
1857 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1858 forward.setEnabled(canGoForward);
1859
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001860 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1861 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001862 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001863 if (source != null && dest != null) {
1864 dest.setTitle(source.getTitle());
1865 dest.setIcon(source.getIcon());
1866 }
John Recke1a03a32011-09-14 17:04:16 -07001867 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001868
1869 // decide whether to show the share link option
1870 PackageManager pm = mActivity.getPackageManager();
1871 Intent send = new Intent(Intent.ACTION_SEND);
1872 send.setType("text/plain");
1873 ResolveInfo ri = pm.resolveActivity(send,
1874 PackageManager.MATCH_DEFAULT_ONLY);
1875 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1876
1877 boolean isNavDump = mSettings.enableNavDump();
1878 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1879 nav.setVisible(isNavDump);
1880 nav.setEnabled(isNavDump);
1881
1882 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001883 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1884 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001885 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1886 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001887 // history and snapshots item are the members of COMBO menu group,
1888 // so if show history item, only make snapshots item invisible.
1889 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001890
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001891 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001892 }
1893
John Reck9c35b9c2012-05-30 10:08:50 -07001894 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001895 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001896 if (null == getCurrentTopWebView()) {
1897 return false;
1898 }
1899 if (mMenuIsDown) {
1900 // The shortcut action consumes the MENU. Even if it is still down,
1901 // it won't trigger the next shortcut action. In the case of the
1902 // shortcut action triggering a new activity, like Bookmarks, we
1903 // won't get onKeyUp for MENU. So it is important to reset it here.
1904 mMenuIsDown = false;
1905 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001906 if (mUi.onOptionsItemSelected(item)) {
1907 // ui callback handled it
1908 return true;
1909 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001910 switch (item.getItemId()) {
1911 // -- Main menu
1912 case R.id.new_tab_menu_id:
1913 openTabToHomePage();
1914 break;
1915
1916 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001917 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001918 break;
1919
Afzal Najamd4e33312012-04-26 01:54:01 -04001920 case R.id.close_other_tabs_id:
1921 closeOtherTabs();
1922 break;
1923
Michael Kolb8233fac2010-10-26 16:08:53 -07001924 case R.id.goto_menu_id:
1925 editUrl();
1926 break;
1927
1928 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001929 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1930 break;
1931
1932 case R.id.history_menu_id:
1933 bookmarksOrHistoryPicker(ComboViews.History);
1934 break;
1935
1936 case R.id.snapshots_menu_id:
1937 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 break;
1939
Michael Kolb8233fac2010-10-26 16:08:53 -07001940 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001941 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001942 break;
1943
1944 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001945 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001946 stopLoading();
1947 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001948 Tab currentTab = mTabControl.getCurrentTab();
1949 if (currentTab.hasCrashed) {
1950 currentTab.replaceCrashView(getCurrentTopWebView(),
1951 currentTab.getViewContainer());
1952 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001953 getCurrentTopWebView().reload();
1954 }
1955 break;
1956
1957 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001958 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001959 break;
1960
1961 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001962 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 break;
1964
1965 case R.id.close_menu_id:
1966 // Close the subwindow if it exists.
1967 if (mTabControl.getCurrentSubWindow() != null) {
1968 dismissSubWindow(mTabControl.getCurrentTab());
1969 break;
1970 }
1971 closeCurrentTab();
1972 break;
1973
kaiyiza8b6dbb2013-07-29 18:11:22 +08001974 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001975 Object[] params = { new String("persist.debug.browsermonkeytest")};
1976 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001977 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001978 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001979 if (ret != null && ret.equals("enable"))
1980 break;
luxiaolb40014b2013-07-19 10:01:43 +08001981 showExitDialog(mActivity);
1982 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 case R.id.homepage_menu_id:
1984 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001985 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001986 break;
1987
1988 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001989 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001990 break;
1991
1992 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001993 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 break;
1995
John Reck2bc80422011-06-30 15:11:49 -07001996 case R.id.save_snapshot_menu_id:
1997 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001998 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001999 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05002000 break;
2001
Michael Kolb8233fac2010-10-26 16:08:53 -07002002 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002003 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002004 break;
2005
John Recke1a03a32011-09-14 17:04:16 -07002006 case R.id.snapshot_go_live:
2007 goLive();
2008 return true;
2009
Michael Kolb8233fac2010-10-26 16:08:53 -07002010 case R.id.share_page_menu_id:
2011 Tab currentTab = mTabControl.getCurrentTab();
2012 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002013 return false;
2014 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002015 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002016 break;
2017
2018 case R.id.dump_nav_menu_id:
2019 getCurrentTopWebView().debugDump();
2020 break;
2021
Michael Kolb8233fac2010-10-26 16:08:53 -07002022 case R.id.zoom_in_menu_id:
2023 getCurrentTopWebView().zoomIn();
2024 break;
2025
2026 case R.id.zoom_out_menu_id:
2027 getCurrentTopWebView().zoomOut();
2028 break;
2029
2030 case R.id.view_downloads_menu_id:
2031 viewDownloads();
2032 break;
2033
John Reck42229bc2011-08-19 13:26:43 -07002034 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002035 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002036 break;
2037
Michael Kolb8233fac2010-10-26 16:08:53 -07002038 case R.id.window_one_menu_id:
2039 case R.id.window_two_menu_id:
2040 case R.id.window_three_menu_id:
2041 case R.id.window_four_menu_id:
2042 case R.id.window_five_menu_id:
2043 case R.id.window_six_menu_id:
2044 case R.id.window_seven_menu_id:
2045 case R.id.window_eight_menu_id:
2046 {
2047 int menuid = item.getItemId();
2048 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2049 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2050 Tab desiredTab = mTabControl.getTab(id);
2051 if (desiredTab != null &&
2052 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002053 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002054 }
2055 break;
2056 }
2057 }
2058 }
2059 break;
2060
2061 default:
2062 return false;
2063 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002064 return true;
2065 }
2066
John Reck68234a92012-04-19 15:27:12 -07002067 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2068 implements OnCancelListener {
2069
2070 private Tab mTab;
2071 private Dialog mProgressDialog;
2072 private ContentValues mValues;
2073
2074 private SaveSnapshotTask(Tab tab) {
2075 mTab = tab;
2076 }
2077
2078 @Override
2079 protected void onPreExecute() {
2080 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2081 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2082 true, true, this);
2083 mValues = mTab.createSnapshotValues();
2084 }
2085
2086 @Override
2087 protected Long doInBackground(Void... params) {
2088 if (!mTab.saveViewState(mValues)) {
2089 return null;
2090 }
2091 if (isCancelled()) {
2092 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2093 File file = mActivity.getFileStreamPath(path);
2094 if (!file.delete()) {
2095 file.deleteOnExit();
2096 }
2097 return null;
2098 }
2099 final ContentResolver cr = mActivity.getContentResolver();
2100 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2101 if (result == null) {
2102 return null;
2103 }
2104 long id = ContentUris.parseId(result);
2105 return id;
2106 }
2107
2108 @Override
2109 protected void onPostExecute(Long id) {
2110 if (isCancelled()) {
2111 return;
2112 }
2113 mProgressDialog.dismiss();
2114 if (id == null) {
2115 Toast.makeText(mActivity, R.string.snapshot_failed,
2116 Toast.LENGTH_SHORT).show();
2117 return;
2118 }
2119 Bundle b = new Bundle();
2120 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2121 mUi.showComboView(ComboViews.Snapshots, b);
2122 }
2123
2124 @Override
2125 public void onCancel(DialogInterface dialog) {
2126 cancel(true);
2127 }
2128 }
2129
Michael Kolb80f75082012-04-10 10:50:06 -07002130 @Override
2131 public void toggleUserAgent() {
2132 WebView web = getCurrentWebView();
2133 mSettings.toggleDesktopUseragent(web);
2134 web.loadUrl(web.getOriginalUrl());
2135 }
2136
2137 @Override
2138 public void findOnPage() {
2139 getCurrentTopWebView().showFindDialog(null, true);
2140 }
2141
2142 @Override
2143 public void openPreferences() {
2144 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2145 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2146 getCurrentTopWebView().getUrl());
2147 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2148 }
2149
2150 @Override
2151 public void bookmarkCurrentPage() {
2152 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2153 if (bookmarkIntent != null) {
2154 mActivity.startActivity(bookmarkIntent);
2155 }
2156 }
2157
John Recke1a03a32011-09-14 17:04:16 -07002158 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002159 SnapshotTab t = (SnapshotTab) getCurrentTab();
2160 t.loadUrl(t.getLiveUrl(), null);
John Recke1a03a32011-09-14 17:04:16 -07002161 }
2162
luxiaolb40014b2013-07-19 10:01:43 +08002163 private void showExitDialog(final Activity activity) {
2164 new AlertDialog.Builder(activity)
2165 .setTitle(R.string.exit_browser_title)
2166 .setIcon(android.R.drawable.ic_dialog_alert)
2167 .setMessage(R.string.exit_browser_msg)
2168 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2169 public void onClick(DialogInterface dialog, int which) {
2170 activity.moveTaskToBack(true);
2171 dialog.dismiss();
2172 }
2173 })
2174 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2175 public void onClick(DialogInterface dialog, int which) {
2176 activity.finish();
2177 mHandler.postDelayed(new Runnable() {
2178 @Override
2179 public void run() {
2180 // TODO Auto-generated method stub
2181 mCrashRecoveryHandler.clearState(true);
2182 int pid = android.os.Process.myPid();
2183 android.os.Process.killProcess(pid);
2184 }
2185 }, 300);
2186 dialog.dismiss();
2187 }
2188 })
2189 .show();
2190 }
Michael Kolb315d5022011-10-13 12:47:11 -07002191 @Override
2192 public void showPageInfo() {
2193 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2194 }
2195
John Reck9c35b9c2012-05-30 10:08:50 -07002196 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002197 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002198 // Let the History and Bookmark fragments handle menus they created.
2199 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2200 return false;
2201 }
2202
Michael Kolb8233fac2010-10-26 16:08:53 -07002203 int id = item.getItemId();
2204 boolean result = true;
2205 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002206 // -- Browser context menu
2207 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002208 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002209 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002210 case R.id.copy_link_context_menu_id:
2211 final WebView webView = getCurrentTopWebView();
2212 if (null == webView) {
2213 result = false;
2214 break;
2215 }
2216 final HashMap<String, WebView> hrefMap =
2217 new HashMap<String, WebView>();
2218 hrefMap.put("webview", webView);
2219 final Message msg = mHandler.obtainMessage(
2220 FOCUS_NODE_HREF, id, 0, hrefMap);
2221 webView.requestFocusNodeHref(msg);
2222 break;
2223
2224 default:
2225 // For other context menus
2226 result = onOptionsItemSelected(item);
2227 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002228 return result;
2229 }
2230
2231 /**
2232 * support programmatically opening the context menu
2233 */
2234 public void openContextMenu(View view) {
2235 mActivity.openContextMenu(view);
2236 }
2237
2238 /**
2239 * programmatically open the options menu
2240 */
2241 public void openOptionsMenu() {
2242 mActivity.openOptionsMenu();
2243 }
2244
John Reck9c35b9c2012-05-30 10:08:50 -07002245 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002246 public boolean onMenuOpened(int featureId, Menu menu) {
2247 if (mOptionsMenuOpen) {
2248 if (mConfigChanged) {
2249 // We do not need to make any changes to the state of the
2250 // title bar, since the only thing that happened was a
2251 // change in orientation
2252 mConfigChanged = false;
2253 } else {
2254 if (!mExtendedMenuOpen) {
2255 mExtendedMenuOpen = true;
2256 mUi.onExtendedMenuOpened();
2257 } else {
2258 // Switching the menu back to icon view, so show the
2259 // title bar once again.
2260 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002261 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002262 }
2263 }
2264 } else {
2265 // The options menu is closed, so open it, and show the title
2266 mOptionsMenuOpen = true;
2267 mConfigChanged = false;
2268 mExtendedMenuOpen = false;
2269 mUi.onOptionsMenuOpened();
2270 }
2271 return true;
2272 }
2273
John Reck9c35b9c2012-05-30 10:08:50 -07002274 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002275 public void onOptionsMenuClosed(Menu menu) {
2276 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002277 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002278 }
2279
John Reck9c35b9c2012-05-30 10:08:50 -07002280 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002281 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002282 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002283 }
2284
2285 // Helper method for getting the top window.
2286 @Override
2287 public WebView getCurrentTopWebView() {
2288 return mTabControl.getCurrentTopWebView();
2289 }
2290
2291 @Override
2292 public WebView getCurrentWebView() {
2293 return mTabControl.getCurrentWebView();
2294 }
2295
2296 /*
2297 * This method is called as a result of the user selecting the options
2298 * menu to see the download window. It shows the download window on top of
2299 * the current window.
2300 */
2301 void viewDownloads() {
2302 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2303 mActivity.startActivity(intent);
2304 }
2305
John Reck30b065e2011-07-19 10:58:05 -07002306 int getActionModeHeight() {
2307 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2308 new int[] { android.R.attr.actionBarSize });
2309 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2310 actionBarSizeTypedArray.recycle();
2311 return size;
2312 }
2313
Michael Kolb8233fac2010-10-26 16:08:53 -07002314 // action mode
2315
John Reck9c35b9c2012-05-30 10:08:50 -07002316 @Override
2317 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002318 mUi.onActionModeStarted(mode);
2319 mActionMode = mode;
2320 }
2321
2322 /*
2323 * True if a custom ActionMode (i.e. find or select) is in use.
2324 */
2325 @Override
2326 public boolean isInCustomActionMode() {
2327 return mActionMode != null;
2328 }
2329
2330 /*
2331 * End the current ActionMode.
2332 */
2333 @Override
2334 public void endActionMode() {
2335 if (mActionMode != null) {
2336 mActionMode.finish();
2337 }
2338 }
2339
2340 /*
2341 * Called by find and select when they are finished. Replace title bars
2342 * as necessary.
2343 */
John Reck9c35b9c2012-05-30 10:08:50 -07002344 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002345 public void onActionModeFinished(ActionMode mode) {
2346 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002347 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002348 mActionMode = null;
2349 }
2350
2351 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002352 final Tab tab = getCurrentTab();
2353 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002354 }
2355
2356 // bookmark handling
2357
2358 /**
2359 * add the current page as a bookmark to the given folder id
2360 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002361 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002362 * bookmarked, and if it is, edit that bookmark. If false, and
2363 * the site is already bookmarked, do not attempt to edit the
2364 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002365 */
2366 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002367 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002368 WebView w = getCurrentTopWebView();
2369 if (w == null) {
2370 return null;
2371 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002372 Intent i = new Intent(mActivity,
2373 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002374 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2375 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2376 String touchIconUrl = w.getTouchIconUrl();
2377 if (touchIconUrl != null) {
2378 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2379 WebSettings settings = w.getSettings();
2380 if (settings != null) {
2381 i.putExtra(AddBookmarkPage.USER_AGENT,
2382 settings.getUserAgentString());
2383 }
2384 }
2385 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2386 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2387 getDesiredThumbnailHeight(mActivity)));
2388 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002389 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002390 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2391 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002392 // Put the dialog at the upper right of the screen, covering the
2393 // star on the title bar.
2394 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002395 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002396 }
2397
2398 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002399 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002400 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002401 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002402 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002403 }
2404
Vivek Sekharb54614f2014-05-01 19:03:37 -07002405 @Override
2406 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2407 boolean capture) {
2408 mUploadHandler = new UploadHandler(this);
2409 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2410 }
2411
Michael Kolb8233fac2010-10-26 16:08:53 -07002412 // thumbnails
2413
2414 /**
2415 * Return the desired width for thumbnail screenshots, which are stored in
2416 * the database, and used on the bookmarks screen.
2417 * @param context Context for finding out the density of the screen.
2418 * @return desired width for thumbnail screenshot.
2419 */
2420 static int getDesiredThumbnailWidth(Context context) {
2421 return context.getResources().getDimensionPixelOffset(
2422 R.dimen.bookmarkThumbnailWidth);
2423 }
2424
2425 /**
2426 * Return the desired height for thumbnail screenshots, which are stored in
2427 * the database, and used on the bookmarks screen.
2428 * @param context Context for finding out the density of the screen.
2429 * @return desired height for thumbnail screenshot.
2430 */
2431 static int getDesiredThumbnailHeight(Context context) {
2432 return context.getResources().getDimensionPixelOffset(
2433 R.dimen.bookmarkThumbnailHeight);
2434 }
2435
John Reck8cc92352011-07-06 17:41:52 -07002436 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002437 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002438 return null;
2439 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002440
2441 Bitmap viewportBitmap = view.getViewportBitmap();
2442 if (viewportBitmap == null) {
2443 return null;
2444 }
2445
2446 float aspectRatio = (float) width/height;
2447 int viewportWidth = viewportBitmap.getWidth();
2448 int viewportHeight = viewportBitmap.getHeight();
2449
2450 //modify the size to attain the same aspect ratio of desired thumbnail size
2451 if (viewportHeight > viewportWidth) {
2452 viewportHeight= (int)Math.round(viewportWidth * aspectRatio);
2453 } else {
2454 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2455 }
2456
2457 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2458 Rect dstRect = new Rect(0, 0, width, height);
2459
2460 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2461 || sThumbnailBitmap.getHeight() != height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002462 if (sThumbnailBitmap != null) {
2463 sThumbnailBitmap.recycle();
2464 sThumbnailBitmap = null;
2465 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002466
2467 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002468 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002469
John Reckd7dd9b22011-08-30 09:18:29 -07002470 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002471 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
John Reckd7dd9b22011-08-30 09:18:29 -07002472
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002473 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002474 }
2475
John Reck34ef2672011-02-10 11:30:55 -08002476 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002477 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002478 // FIXME: Would like to make sure there is actually something to
2479 // draw, but the API for that (WebViewCore.pictureReady()) is not
2480 // currently accessible here.
2481
John Reck34ef2672011-02-10 11:30:55 -08002482 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002483 if (view == null) {
2484 // Tab was destroyed
2485 return;
2486 }
John Reck34ef2672011-02-10 11:30:55 -08002487 final String url = tab.getUrl();
2488 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002489 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002490 if (TextUtils.isEmpty(url)) {
2491 return;
2492 }
2493
kaiyiz6e5b3e02013-08-19 20:02:01 +08002494 //update My Navigation Thumbnails
2495 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2496 if (isMyNavigationUrl) {
2497 updateMyNavigationThumbnail(url, view);
2498 }
John Reck34ef2672011-02-10 11:30:55 -08002499 // Only update thumbnails for web urls (http(s)://), not for
2500 // about:, javascript:, data:, etc...
2501 // Unless it is a bookmarked site, then always update
2502 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2503 return;
2504 }
2505
kaiyiz6e5b3e02013-08-19 20:02:01 +08002506 if (url != null && mUpdateMyNavThumbnailUrl != null
2507 && Patterns.WEB_URL.matcher(url).matches()
2508 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2509 String urlHost = (new WebAddress(url)).getHost();
2510 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2511 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2512 || bookmarkHost.length() == 0) {
2513 return;
2514 }
2515 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2516 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2517 bookmarkHost.length());
2518 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2519 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2520 if (!bookmarkDomain.equals(urlDomain)) {
2521 return;
2522 }
2523 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002524 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2525 getDesiredThumbnailHeight(mActivity));
2526 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002527 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2528 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2529 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2530 500);
2531 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 return;
2533 }
2534
2535 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002536 new AsyncTask<Void, Void, Void>() {
2537 @Override
2538 protected Void doInBackground(Void... unused) {
2539 Cursor cursor = null;
2540 try {
2541 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002542 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2543 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2544 : url);
2545 if (mUpdateMyNavThumbnail) {
2546 mUpdateMyNavThumbnail = false;
2547 mUpdateMyNavThumbnailUrl = null;
2548 }
John Reck34ef2672011-02-10 11:30:55 -08002549 if (cursor != null && cursor.moveToFirst()) {
2550 final ByteArrayOutputStream os =
2551 new ByteArrayOutputStream();
2552 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002553
John Reck34ef2672011-02-10 11:30:55 -08002554 ContentValues values = new ContentValues();
2555 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002556
John Reck34ef2672011-02-10 11:30:55 -08002557 do {
John Reck617fd832011-02-16 14:35:59 -08002558 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002559 cr.update(Images.CONTENT_URI, values, null, null);
2560 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002561 }
John Reck34ef2672011-02-10 11:30:55 -08002562 } catch (IllegalStateException e) {
2563 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002564 } catch (SQLiteException s) {
2565 // Added for possible error when user tries to remove the same bookmark
2566 // that is being updated with a screen shot
2567 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002568 } finally {
2569 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002570 }
John Reck34ef2672011-02-10 11:30:55 -08002571 return null;
2572 }
2573 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002574 }
2575
2576 private class Copy implements OnMenuItemClickListener {
2577 private CharSequence mText;
2578
John Reck9c35b9c2012-05-30 10:08:50 -07002579 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 public boolean onMenuItemClick(MenuItem item) {
2581 copy(mText);
2582 return true;
2583 }
2584
2585 public Copy(CharSequence toCopy) {
2586 mText = toCopy;
2587 }
2588 }
2589
Leon Scroggins63c02662010-11-18 15:16:27 -05002590 private static class Download implements OnMenuItemClickListener {
2591 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002592 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002593 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002594 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002595 private static final String FALLBACK_EXTENSION = "dat";
2596 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002597
John Reck9c35b9c2012-05-30 10:08:50 -07002598 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002600 if (DataUri.isDataUri(mText)) {
2601 saveDataUri();
2602 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002603 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002604 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002605 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 return true;
2607 }
2608
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002609 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2610 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002611 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002613 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002614 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002615 }
George Mount387d45d2011-10-07 15:57:53 -07002616
2617 /**
2618 * Treats mText as a data URI and writes its contents to a file
2619 * based on the current time.
2620 */
2621 private void saveDataUri() {
2622 FileOutputStream outputStream = null;
2623 try {
2624 DataUri uri = new DataUri(mText);
2625 File target = getTarget(uri);
2626 outputStream = new FileOutputStream(target);
2627 outputStream.write(uri.getData());
2628 final DownloadManager manager =
2629 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2630 manager.addCompletedDownload(target.getName(),
2631 mActivity.getTitle().toString(), false,
2632 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002633 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002634 } catch (IOException e) {
2635 Log.e(LOGTAG, "Could not save data URL");
2636 } finally {
2637 if (outputStream != null) {
2638 try {
2639 outputStream.close();
2640 } catch (IOException e) {
2641 // ignore close errors
2642 }
2643 }
2644 }
2645 }
2646
2647 /**
2648 * Creates a File based on the current time stamp and uses
2649 * the mime type of the DataUri to get the extension.
2650 */
2651 private File getTarget(DataUri uri) throws IOException {
2652 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002653 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002654 String nameBase = format.format(new Date());
2655 String mimeType = uri.getMimeType();
2656 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2657 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2658 if (extension == null) {
2659 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2660 extension = FALLBACK_EXTENSION;
2661 }
2662 extension = "." + extension; // createTempFile needs the '.'
2663 File targetFile = File.createTempFile(nameBase, extension, dir);
2664 return targetFile;
2665 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002666 }
2667
Cary Clark8974d282010-11-22 10:46:05 -05002668 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002669 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002670
John Reck9c35b9c2012-05-30 10:08:50 -07002671 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002672 public boolean onMenuItemClick(MenuItem item) {
2673 if (mWebView != null) {
2674 return mWebView.selectText();
2675 }
2676 return false;
2677 }
2678
2679 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002680 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002681 }
2682
2683 }
2684
Michael Kolb8233fac2010-10-26 16:08:53 -07002685 /********************** TODO: UI stuff *****************************/
2686
2687 // these methods have been copied, they still need to be cleaned up
2688
2689 /****************** tabs ***************************************************/
2690
2691 // basic tab interactions:
2692
2693 // it is assumed that tabcontrol already knows about the tab
2694 protected void addTab(Tab tab) {
2695 mUi.addTab(tab);
2696 }
2697
2698 protected void removeTab(Tab tab) {
2699 mUi.removeTab(tab);
2700 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002701 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002702 }
2703
Michael Kolbf2055602011-04-09 17:20:03 -07002704 @Override
2705 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002706 // monkey protection against delayed start
2707 if (tab != null) {
2708 mTabControl.setCurrentTab(tab);
2709 // the tab is guaranteed to have a webview after setCurrentTab
2710 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002711 tab.setTimeStamp();
Michael Kolbcd424e92011-02-24 10:26:26 -08002712 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002713 }
2714
John Reck8bcafc12011-08-29 16:43:02 -07002715 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002716 Tab current = mTabControl.getCurrentTab();
2717 if (current != null
2718 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002719 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002720 }
2721 }
2722
John Reck26b18322011-06-21 13:08:58 -07002723 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002724 // Dismiss the subwindow if applicable.
2725 dismissSubWindow(appTab);
2726 // Since we might kill the WebView, remove it from the
2727 // content view first.
2728 mUi.detachTab(appTab);
2729 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002730 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002731 // TODO: analyze why the remove and add are necessary
2732 mUi.attachTab(appTab);
2733 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002734 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002735 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002736 } else {
2737 // If the tab was the current tab, we have to attach
2738 // it to the view system again.
2739 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002740 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002741 }
2742 }
2743
2744 // Remove the sub window if it exists. Also called by TabControl when the
2745 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002746 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002747 public void dismissSubWindow(Tab tab) {
2748 removeSubWindow(tab);
2749 // dismiss the subwindow. This will destroy the WebView.
2750 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002751 WebView wv = getCurrentTopWebView();
2752 if (wv != null) {
2753 wv.requestFocus();
2754 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002755 }
2756
2757 @Override
2758 public void removeSubWindow(Tab t) {
2759 if (t.getSubWebView() != null) {
2760 mUi.removeSubWindow(t.getSubViewContainer());
2761 }
2762 }
2763
2764 @Override
2765 public void attachSubWindow(Tab tab) {
2766 if (tab.getSubWebView() != null) {
2767 mUi.attachSubWindow(tab.getSubViewContainer());
2768 getCurrentTopWebView().requestFocus();
2769 }
2770 }
2771
Mathew Inwood29721c22011-06-29 17:55:24 +01002772 private Tab showPreloadedTab(final UrlData urlData) {
2773 if (!urlData.isPreloaded()) {
2774 return null;
2775 }
2776 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2777 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2778 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002779 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002780 // Could not submit query. Fallback to regular tab creation
2781 tabControl.destroy();
2782 return null;
2783 }
2784 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002785 // check tab count and make room for new tab
2786 if (!mTabControl.canCreateNewTab()) {
2787 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2788 if (leastUsed != null) {
2789 closeTab(leastUsed);
2790 }
2791 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002792 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002793 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002794 mTabControl.addPreloadedTab(t);
2795 addTab(t);
2796 setActiveTab(t);
2797 return t;
2798 }
2799
Michael Kolb7bcafde2011-05-09 13:55:59 -07002800 // open a non inconito tab with the given url data
2801 // and set as active tab
2802 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002803 Tab tab = showPreloadedTab(urlData);
2804 if (tab == null) {
2805 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002806 if ((tab != null) && !urlData.isEmpty()) {
2807 loadUrlDataIn(tab, urlData);
2808 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002809 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002810 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002811 }
2812
Michael Kolb843510f2010-12-09 10:51:49 -08002813 @Override
2814 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002815 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002816 }
2817
Michael Kolb8233fac2010-10-26 16:08:53 -07002818 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002819 public Tab openIncognitoTab() {
2820 return openTab(INCOGNITO_URI, true, true, false);
2821 }
2822
2823 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002824 public Tab openTab(String url, boolean incognito, boolean setActive,
2825 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002826 return openTab(url, incognito, setActive, useCurrent, null);
2827 }
2828
2829 @Override
2830 public Tab openTab(String url, Tab parent, boolean setActive,
2831 boolean useCurrent) {
2832 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2833 setActive, useCurrent, parent);
2834 }
2835
2836 public Tab openTab(String url, boolean incognito, boolean setActive,
2837 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002838 Tab tab = createNewTab(incognito, setActive, useCurrent);
2839 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002840 if (parent != null && parent != tab) {
2841 parent.addChildTab(tab);
2842 }
Michael Kolb519d2282011-05-09 17:03:19 -07002843 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002844 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002845 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002846 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002847 return tab;
2848 }
2849
2850 // this method will attempt to create a new tab
2851 // incognito: private browsing tab
2852 // setActive: ste tab as current tab
2853 // useCurrent: if no new tab can be created, return current tab
2854 private Tab createNewTab(boolean incognito, boolean setActive,
2855 boolean useCurrent) {
2856 Tab tab = null;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002857 MemoryMonitor memMonitor = null;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002858 if (mTabControl.canCreateNewTab()) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002859 if (mSettings.enableMemoryMonitor()) {
2860 Log.d(LOGTAG, " Memory Monitor Enabled .");
2861 memMonitor = MemoryMonitor.getInstance(mActivity.getApplicationContext(),this);
2862 if (memMonitor != null) {
2863 //Remove webview associated with the oldest tab
2864 memMonitor.destroyLeastRecentlyActiveTab();
2865 }
2866 } else {
2867 Log.d(LOGTAG, " Memory Monitor disabled .");
2868 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002869 tab = mTabControl.createNewTab(incognito);
2870 addTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002871 tab.setTimeStamp();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002872 if (setActive) {
2873 setActiveTab(tab);
2874 }
2875 } else {
2876 if (useCurrent) {
2877 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002878 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002879 } else {
2880 mUi.showMaxTabsWarning();
2881 }
2882 }
2883 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002884 }
2885
John Reck2bc80422011-06-30 15:11:49 -07002886 @Override
2887 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2888 SnapshotTab tab = null;
2889 if (mTabControl.canCreateNewTab()) {
2890 tab = mTabControl.createSnapshotTab(snapshotId);
2891 addTab(tab);
2892 if (setActive) {
2893 setActiveTab(tab);
2894 }
2895 } else {
2896 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002897 }
2898 return tab;
2899 }
2900
Michael Kolb8233fac2010-10-26 16:08:53 -07002901 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002902 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002903 * @return boolean True if we successfully switched to a different tab. If
2904 * the indexth tab is null, or if that tab is the same as
2905 * the current one, return false.
2906 */
2907 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002908 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002909 Tab currentTab = mTabControl.getCurrentTab();
2910 if (tab == null || tab == currentTab) {
2911 return false;
2912 }
2913 setActiveTab(tab);
2914 return true;
2915 }
2916
2917 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002918 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002919 closeCurrentTab(false);
2920 }
2921
2922 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002923 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002924 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002925 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002926 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002927 return;
2928 }
Michael Kolbc831b632011-05-11 09:30:34 -07002929 final Tab current = mTabControl.getCurrentTab();
2930 final int pos = mTabControl.getCurrentPosition();
2931 Tab newTab = current.getParent();
2932 if (newTab == null) {
2933 newTab = mTabControl.getTab(pos + 1);
2934 if (newTab == null) {
2935 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002936 }
2937 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002938 if (andQuit) {
2939 mTabControl.setCurrentTab(newTab);
2940 closeTab(current);
2941 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002942 // Close window
2943 closeTab(current);
2944 }
2945 }
2946
2947 /**
2948 * Close the tab, remove its associated title bar, and adjust mTabControl's
2949 * current tab to a valid value.
2950 */
2951 @Override
2952 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002953 if (tab == mTabControl.getCurrentTab()) {
2954 closeCurrentTab();
2955 } else {
2956 removeTab(tab);
2957 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002958 }
2959
Afzal Najamd4e33312012-04-26 01:54:01 -04002960 /**
2961 * Close all tabs except the current one
2962 */
2963 @Override
2964 public void closeOtherTabs() {
2965 int inactiveTabs = mTabControl.getTabCount() - 1;
2966 for (int i = inactiveTabs; i >= 0; i--) {
2967 Tab tab = mTabControl.getTab(i);
2968 if (tab != mTabControl.getCurrentTab()) {
2969 removeTab(tab);
2970 }
2971 }
2972 }
2973
Michael Kolb8233fac2010-10-26 16:08:53 -07002974 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002975 protected void loadUrlFromContext(String url) {
2976 Tab tab = getCurrentTab();
2977 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002978 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002979 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002980 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002981 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002982 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002983 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002984 }
2985 }
2986 }
2987
2988 /**
2989 * Load the URL into the given WebView and update the title bar
2990 * to reflect the new load. Call this instead of WebView.loadUrl
2991 * directly.
2992 * @param view The WebView used to load url.
2993 * @param url The URL to load.
2994 */
John Reck71e51422011-07-01 16:49:28 -07002995 @Override
2996 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002997 loadUrl(tab, url, null);
2998 }
2999
3000 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3001 if (tab != null) {
3002 dismissSubWindow(tab);
3003 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003004 if (tab.hasCrashed) {
3005 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3006 }
Michael Kolba53c9892011-10-05 13:31:40 -07003007 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003008 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003009 }
3010
3011 /**
3012 * Load UrlData into a Tab and update the title bar to reflect the new
3013 * load. Call this instead of UrlData.loadIn directly.
3014 * @param t The Tab used to load.
3015 * @param data The UrlData being loaded.
3016 */
3017 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003018 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003019 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003020 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003021 } else {
John Reck38b39652012-06-05 09:22:59 -07003022 if (t != null && data.mDisableUrlOverride) {
3023 t.disableUrlOverridingForLoad();
3024 }
John Reck26b18322011-06-21 13:08:58 -07003025 loadUrl(t, data.mUrl, data.mHeaders);
3026 }
3027 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003028 }
3029
John Reck30c714c2010-12-16 17:30:34 -08003030 @Override
3031 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003032 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003033 if (tab.canGoBack()) {
3034 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003035 } else {
John Reckef654f12011-07-12 16:42:08 -07003036 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003037 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003038 }
3039
3040 void goBackOnePageOrQuit() {
3041 Tab current = mTabControl.getCurrentTab();
3042 if (current == null) {
3043 /*
3044 * Instead of finishing the activity, simply push this to the back
3045 * of the stack and let ActivityManager to choose the foreground
3046 * activity. As BrowserActivity is singleTask, it will be always the
3047 * root of the task. So we can use either true or false for
3048 * moveTaskToBack().
3049 */
luxiaolb40014b2013-07-19 10:01:43 +08003050 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003051 return;
3052 }
John Reckef654f12011-07-12 16:42:08 -07003053 if (current.canGoBack()) {
3054 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003055 } else {
3056 // Check to see if we are closing a window that was created by
3057 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003058 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003059 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003060 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003061 // Now we close the other tab
3062 closeTab(current);
3063 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003064 /*
3065 * Instead of finishing the activity, simply push this to the back
3066 * of the stack and let ActivityManager to choose the foreground
3067 * activity. As BrowserActivity is singleTask, it will be always the
3068 * root of the task. So we can use either true or false for
3069 * moveTaskToBack().
3070 */
luxiaolb40014b2013-07-19 10:01:43 +08003071 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003072 }
3073 }
3074 }
3075
3076 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003077 * helper method for key handler
3078 * returns the current tab if it can't advance
3079 */
Michael Kolbc831b632011-05-11 09:30:34 -07003080 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003081 int pos = mTabControl.getCurrentPosition() + 1;
3082 if (pos >= mTabControl.getTabCount()) {
3083 pos = 0;
3084 }
3085 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003086 }
3087
3088 /**
3089 * helper method for key handler
3090 * returns the current tab if it can't advance
3091 */
Michael Kolbc831b632011-05-11 09:30:34 -07003092 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003093 int pos = mTabControl.getCurrentPosition() - 1;
3094 if ( pos < 0) {
3095 pos = mTabControl.getTabCount() - 1;
3096 }
3097 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003098 }
3099
John Reckbcef87f2012-02-03 14:58:34 -08003100 boolean isMenuOrCtrlKey(int keyCode) {
3101 return (KeyEvent.KEYCODE_MENU == keyCode)
3102 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3103 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3104 }
3105
Michael Kolb0035fad2011-03-14 13:25:28 -07003106 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003107 * handle key events in browser
3108 *
3109 * @param keyCode
3110 * @param event
3111 * @return true if handled, false to pass to super
3112 */
John Reck9c35b9c2012-05-30 10:08:50 -07003113 @Override
3114 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003115 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003116 // Even if MENU is already held down, we need to call to super to open
3117 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003118 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003119 mMenuIsDown = true;
3120 return false;
3121 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003122
Cary Clark8ff8c662010-12-29 15:03:05 -05003123 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003124 Tab tab = getCurrentTab();
3125 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003126
Cary Clark160bbb92011-01-10 11:17:07 -05003127 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3128 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003129
Michael Kolb8233fac2010-10-26 16:08:53 -07003130 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003131 case KeyEvent.KEYCODE_TAB:
3132 if (event.isCtrlPressed()) {
3133 if (event.isShiftPressed()) {
3134 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003135 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003136 } else {
3137 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003138 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003139 }
3140 return true;
3141 }
3142 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003143 case KeyEvent.KEYCODE_SPACE:
3144 // WebView/WebTextView handle the keys in the KeyDown. As
3145 // the Activity's shortcut keys are only handled when WebView
3146 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003147 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003148 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003149 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003150 pageDown();
3151 }
3152 return true;
3153 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003154 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003155 event.startTracking();
3156 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003157 case KeyEvent.KEYCODE_FORWARD:
3158 if (!noModifiers) break;
3159 tab.goForward();
3160 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003161 case KeyEvent.KEYCODE_DPAD_LEFT:
3162 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003163 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003164 return true;
3165 }
3166 break;
3167 case KeyEvent.KEYCODE_DPAD_RIGHT:
3168 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003169 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003170 return true;
3171 }
3172 break;
3173 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003174 if (ctrl) {
3175 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003176 return true;
3177 }
3178 break;
Michael Kolba4183062011-01-16 10:43:21 -08003179// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003180 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003181 if (ctrl ) {
3182 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003183 return true;
3184 }
3185 break;
Michael Kolba4183062011-01-16 10:43:21 -08003186// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003187// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003188// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003189// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003190// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003191// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003192// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003193// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003194// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003195// case KeyEvent.KEYCODE_M: // unused
3196// case KeyEvent.KEYCODE_N: // in Chrome: new window
3197// case KeyEvent.KEYCODE_O: // in Chrome: open file
3198// case KeyEvent.KEYCODE_P: // in Chrome: print page
3199// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003200// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003201// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3202 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003203 // we can't use the ctrl/shift flags, they check for
3204 // exclusive use of a modifier
3205 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003206 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003207 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003208 } else {
3209 openTabToHomePage();
3210 }
3211 return true;
3212 }
3213 break;
3214// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3215// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003216// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003217// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3218// case KeyEvent.KEYCODE_Y: // unused
3219// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003220 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003221 // it is a regular key and webview is not null
3222 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003223 }
3224
John Reck9c35b9c2012-05-30 10:08:50 -07003225 @Override
3226 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003227 switch(keyCode) {
3228 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003229 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003230 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003231 return true;
3232 }
3233 break;
3234 }
3235 return false;
3236 }
3237
John Reck9c35b9c2012-05-30 10:08:50 -07003238 @Override
3239 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003240 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003241 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003242 if (KeyEvent.KEYCODE_MENU == keyCode
3243 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003244 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003245 }
3246 }
Cary Clark160bbb92011-01-10 11:17:07 -05003247 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003248 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003249 case KeyEvent.KEYCODE_BACK:
3250 if (event.isTracking() && !event.isCanceled()) {
3251 onBackKey();
3252 return true;
3253 }
3254 break;
3255 }
3256 return false;
3257 }
3258
3259 public boolean isMenuDown() {
3260 return mMenuIsDown;
3261 }
3262
John Reck9c35b9c2012-05-30 10:08:50 -07003263 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003264 public void setupAutoFill(Message message) {
3265 // Open the settings activity at the AutoFill profile fragment so that
3266 // the user can create a new profile. When they return, we will dispatch
3267 // the message so that we can autofill the form using their new profile.
3268 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3269 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3270 AutoFillSettingsFragment.class.getName());
3271 mAutoFillSetupMessage = message;
3272 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3273 }
John Reckb3417f02011-01-14 11:01:05 -08003274
John Reck9c35b9c2012-05-30 10:08:50 -07003275 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003276 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003277 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003278 return true;
3279 }
3280
John Reck1cf4b792011-07-26 10:22:22 -07003281 @Override
3282 public boolean shouldCaptureThumbnails() {
3283 return mUi.shouldCaptureThumbnails();
3284 }
3285
Michael Kolbc3af0672011-08-09 10:24:41 -07003286 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003287 public boolean supportsVoice() {
3288 PackageManager pm = mActivity.getPackageManager();
3289 List activities = pm.queryIntentActivities(new Intent(
3290 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3291 return activities.size() != 0;
3292 }
3293
3294 @Override
3295 public void startVoiceRecognizer() {
3296 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003297 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003298 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3299 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3300 mActivity.startActivityForResult(voice, VOICE_RESULT);
3301 }
3302
3303 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003304 public void setBlockEvents(boolean block) {
3305 mBlockEvents = block;
3306 }
3307
John Reck9c35b9c2012-05-30 10:08:50 -07003308 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003309 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003310 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003311 }
3312
John Reck9c35b9c2012-05-30 10:08:50 -07003313 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003314 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003315 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003316 }
3317
John Reck9c35b9c2012-05-30 10:08:50 -07003318 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003319 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003320 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003321 }
3322
John Reck9c35b9c2012-05-30 10:08:50 -07003323 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003324 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003325 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003326 }
3327
John Reck9c35b9c2012-05-30 10:08:50 -07003328 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003329 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003330 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003331 }
3332
Michael Kolb8233fac2010-10-26 16:08:53 -07003333}