blob: 27b8ce6cd91dd5e32fb7553c0021f2c5a359f4cd [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
Michael Kolb8233fac2010-10-26 16:08:53 -0700198 // FIXME, temp address onPrepareMenu performance problem.
199 // When we move everything out of view, we should rewrite this.
200 private int mCurrentMenuState = 0;
201 private int mMenuState = R.id.MAIN_MENU;
202 private int mOldMenuState = EMPTY_MENU;
203 private Menu mCachedMenu;
204
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 private boolean mMenuIsDown;
206
207 // For select and find, we keep track of the ActionMode so that
208 // finish() can be called as desired.
209 private ActionMode mActionMode;
210
211 /**
212 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
213 * of whether the configuration has changed. The first onMenuOpened call
214 * after a configuration change is simply a reopening of the same menu
215 * (i.e. mIconView did not change).
216 */
217 private boolean mConfigChanged;
218
219 /**
220 * Keeps track of whether the options menu is open. This is important in
221 * determining whether to show or hide the title bar overlay
222 */
223 private boolean mOptionsMenuOpen;
224
225 /**
226 * Whether or not the options menu is in its bigger, popup menu form. When
227 * true, we want the title bar overlay to be gone. When false, we do not.
228 * Only meaningful if mOptionsMenuOpen is true.
229 */
230 private boolean mExtendedMenuOpen;
231
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 private boolean mActivityPaused = true;
233 private boolean mLoadStopped;
234
235 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500236 // Checks to see when the bookmarks database has changed, and updates the
237 // Tabs' notion of whether they represent bookmarked sites.
238 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700239 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700240
Michael Kolbc3af0672011-08-09 10:24:41 -0700241 private boolean mBlockEvents;
242
Michael Kolb0b129122012-06-04 16:31:58 -0700243 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800244 private boolean mUpdateMyNavThumbnail;
245 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700246
George Mount3636d0a2011-11-21 09:08:21 -0800247 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 mActivity = browser;
249 mSettings = BrowserSettings.getInstance();
250 mTabControl = new TabControl(this);
251 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700252 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800253 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700254 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700255
256 mUrlHandler = new UrlHandler(this);
257 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
259
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500261 mBookmarksObserver = new ContentObserver(mHandler) {
262 @Override
263 public void onChange(boolean selfChange) {
264 int size = mTabControl.getTabCount();
265 for (int i = 0; i < size; i++) {
266 mTabControl.getTab(i).updateBookmarkedStatus();
267 }
268 }
269
270 };
271 browser.getContentResolver().registerContentObserver(
272 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700273
274 mNetworkHandler = new NetworkStateHandler(mActivity, this);
John Reckaf262e72011-07-25 13:55:44 -0700275 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 }
277
John Reck9c35b9c2012-05-30 10:08:50 -0700278 @Override
279 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800280 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800281 // mCrashRecoverHandler has any previously saved state.
282 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700283 }
284
George Mount3636d0a2011-11-21 09:08:21 -0800285 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700286 // Unless the last browser usage was within 24 hours, destroy any
287 // remaining incognito tabs.
288
289 Calendar lastActiveDate = icicle != null ?
290 (Calendar) icicle.getSerializable("lastActiveDate") : null;
291 Calendar today = Calendar.getInstance();
292 Calendar yesterday = Calendar.getInstance();
293 yesterday.add(Calendar.DATE, -1);
294
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800295 // we dont want to ever recover incognito tabs
296 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700297
Patrick Scott7d50a932011-02-04 09:27:26 -0500298 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700299 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500300 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000301
Michael Kolbc831b632011-05-11 09:30:34 -0700302 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500303 // Not able to restore so we go ahead and clear session cookies. We
304 // must do this before trying to login the user as we don't want to
305 // clear any session cookies set during login.
306 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700307 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800308 if (intent == null) {
309 // This won't happen under common scenarios. The icicle is
310 // not null, but there aren't any tabs to restore.
311 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700312 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800313 final Bundle extra = intent.getExtras();
314 // Create an initial tab.
315 // If the intent is ACTION_VIEW and data is not null, the Browser is
316 // invoked to view the content by another application. In this case,
317 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800318 UrlData urlData = null;
319 if (intent.getData() != null
320 && Intent.ACTION_VIEW.equals(intent.getAction())
321 && intent.getData().toString().startsWith("content://")) {
322 urlData = new UrlData(intent.getData().toString());
323 } else {
324 urlData = IntentHandler.getUrlDataFromIntent(intent);
325 }
George Mount3636d0a2011-11-21 09:08:21 -0800326 Tab t = null;
327 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700328 String landingPage = mActivity.getResources().getString(
329 R.string.def_landing_page);
330 if (!landingPage.isEmpty()) {
331 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800332 } else {
333 t = openTabToHomePage();
334 }
George Mount3636d0a2011-11-21 09:08:21 -0800335 } else {
336 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700337 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800338 }
339 if (t != null) {
340 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
341 }
342 WebView webView = t.getWebView();
343 if (extra != null) {
344 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
345 if (scale > 0 && scale <= 1000) {
346 webView.setInitialScale(scale);
347 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700348 }
349 }
John Reckd8c74522011-06-14 08:45:00 -0700350 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700352 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500353 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700354 List<Tab> tabs = mTabControl.getTabs();
355 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
356 for (Tab t : tabs) {
357 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700358 if (t != mTabControl.getCurrentTab()) {
359 t.pause();
360 }
John Reck1cf4b792011-07-26 10:22:22 -0700361 }
362 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700363 if (tabs.size() == 0) {
364 openTabToHomePage();
365 }
John Reck1cf4b792011-07-26 10:22:22 -0700366 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700367 // TabControl.restoreState() will create a new tab even if
368 // restoring the state fails.
369 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800370 // Intent is non-null when framework thinks the browser should be
371 // launching with a new intent (icicle is null).
372 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700373 mIntentHandler.onNewIntent(intent);
374 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700375 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700376 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700377 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800378 if (jsFlags.trim().length() != 0) {
379 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700380 }
George Mount3636d0a2011-11-21 09:08:21 -0800381 if (intent != null
382 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700383 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800384 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700385 }
386
John Reck1cf4b792011-07-26 10:22:22 -0700387 private static class PruneThumbnails implements Runnable {
388 private Context mContext;
389 private List<Long> mIds;
390
391 PruneThumbnails(Context context, List<Long> preserveIds) {
392 mContext = context.getApplicationContext();
393 mIds = preserveIds;
394 }
395
396 @Override
397 public void run() {
398 ContentResolver cr = mContext.getContentResolver();
399 if (mIds == null || mIds.size() == 0) {
400 cr.delete(Thumbnails.CONTENT_URI, null, null);
401 } else {
402 int length = mIds.size();
403 StringBuilder where = new StringBuilder();
404 where.append(Thumbnails._ID);
405 where.append(" not in (");
406 for (int i = 0; i < length; i++) {
407 where.append(mIds.get(i));
408 if (i < (length - 1)) {
409 where.append(",");
410 }
411 }
412 where.append(")");
413 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
414 }
415 }
416
417 }
418
Michael Kolb1514bb72010-11-22 09:11:48 -0800419 @Override
420 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700421 return mFactory;
422 }
423
424 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800425 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800426 if (tab.hasCrashed)
427 tab.showCrashView();
428 else
429 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800430 }
431
432 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800433 public void createSubWindow(Tab tab) {
434 endActionMode();
435 WebView mainView = tab.getWebView();
436 WebView subView = mFactory.createWebView((mainView == null)
437 ? false
438 : mainView.isPrivateBrowsingEnabled());
439 mUi.createSubWindow(tab, subView);
440 }
441
442 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700443 public Context getContext() {
444 return mActivity;
445 }
446
447 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700448 public Activity getActivity() {
449 return mActivity;
450 }
451
452 void setUi(UI ui) {
453 mUi = ui;
454 }
455
Michael Kolbaf63dba2012-05-16 12:58:05 -0700456 @Override
457 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700458 return mSettings;
459 }
460
461 IntentHandler getIntentHandler() {
462 return mIntentHandler;
463 }
464
465 @Override
466 public UI getUi() {
467 return mUi;
468 }
469
470 int getMaxTabs() {
471 return mActivity.getResources().getInteger(R.integer.max_tabs);
472 }
473
474 @Override
475 public TabControl getTabControl() {
476 return mTabControl;
477 }
478
Michael Kolb1bf23132010-11-19 12:55:12 -0800479 @Override
480 public List<Tab> getTabs() {
481 return mTabControl.getTabs();
482 }
483
John Reckaf262e72011-07-25 13:55:44 -0700484 // Open the icon database.
485 private void openIconDatabase() {
486 // We have to call getInstance on the UI thread
487 final WebIconDatabase instance = WebIconDatabase.getInstance();
488 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000489
John Reckaf262e72011-07-25 13:55:44 -0700490 @Override
491 public void run() {
492 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 }
John Reckaf262e72011-07-25 13:55:44 -0700494 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 }
496
497 private void startHandler() {
498 mHandler = new Handler() {
499
500 @Override
501 public void handleMessage(Message msg) {
502 switch (msg.what) {
503 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700504 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700505 break;
506 case FOCUS_NODE_HREF:
507 {
508 String url = (String) msg.getData().get("url");
509 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500510 String src = (String) msg.getData().get("src");
511 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700512 if (TextUtils.isEmpty(url)) {
513 break;
514 }
515 HashMap focusNodeMap = (HashMap) msg.obj;
516 WebView view = (WebView) focusNodeMap.get("webview");
517 // Only apply the action if the top window did not change.
518 if (getCurrentTopWebView() != view) {
519 break;
520 }
521 switch (msg.arg1) {
522 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700523 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700524 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500525 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700526 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500527 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500528 case R.id.open_newtab_context_menu_id:
529 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700530 openTab(url, parent,
531 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500532 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700533 case R.id.copy_link_context_menu_id:
534 copy(url);
535 break;
536 case R.id.save_link_context_menu_id:
537 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500538 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800539 mActivity, url, view.getSettings().getUserAgentString(),
540 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700541 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700542 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700543 if(title == null || title == "")
544 title = url;
545
546 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
547 //SWE TODO: No thumbnail support for the url obtained via
548 //browser context menu as its not loaded in webview.
549 if (bookmarkIntent != null) {
550 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
551 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
552 mActivity.startActivity(bookmarkIntent);
553 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700554 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700555 }
556 break;
557 }
558
559 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700560 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 break;
562
563 case STOP_LOAD:
564 stopLoading();
565 break;
566
567 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700568 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700569 mWakeLock.release();
570 // if we reach here, Browser should be still in the
571 // background loading after WAKELOCK_TIMEOUT (5-min).
572 // To avoid burning the battery, stop loading.
573 mTabControl.stopAllLoading();
574 }
575 break;
576
577 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800578 Tab tab = (Tab) msg.obj;
579 if (tab != null) {
580 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 }
582 break;
kaiyiz591110b2013-08-06 17:11:06 +0800583 case OPEN_MENU:
584 if (!mOptionsMenuOpen && mActivity != null ) {
585 mActivity.openOptionsMenu();
586 }
587 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700588 }
589 }
590 };
591
592 }
593
John Reckef654f12011-07-12 16:42:08 -0700594 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200595 public Tab getCurrentTab() {
596 return mTabControl.getCurrentTab();
597 }
598
Michael Kolbba99c5d2010-11-29 14:57:41 -0800599 @Override
600 public void shareCurrentPage() {
601 shareCurrentPage(mTabControl.getCurrentTab());
602 }
603
604 private void shareCurrentPage(Tab tab) {
605 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800606 sharePage(mActivity, tab.getTitle(),
607 tab.getUrl(), tab.getFavicon(),
608 createScreenshot(tab.getWebView(),
609 getDesiredThumbnailWidth(mActivity),
610 getDesiredThumbnailHeight(mActivity)));
611 }
612 }
613
Michael Kolb8233fac2010-10-26 16:08:53 -0700614 /**
615 * Share a page, providing the title, url, favicon, and a screenshot. Uses
616 * an {@link Intent} to launch the Activity chooser.
617 * @param c Context used to launch a new Activity.
618 * @param title Title of the page. Stored in the Intent with
619 * {@link Intent#EXTRA_SUBJECT}
620 * @param url URL of the page. Stored in the Intent with
621 * {@link Intent#EXTRA_TEXT}
622 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
623 * with {@link Browser#EXTRA_SHARE_FAVICON}
624 * @param screenshot Bitmap of a screenshot of the page. Stored in the
625 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
626 */
627 static final void sharePage(Context c, String title, String url,
628 Bitmap favicon, Bitmap screenshot) {
629 Intent send = new Intent(Intent.ACTION_SEND);
630 send.setType("text/plain");
631 send.putExtra(Intent.EXTRA_TEXT, url);
632 send.putExtra(Intent.EXTRA_SUBJECT, title);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800633 send.putExtra(EXTRA_SHARE_FAVICON, favicon);
634 send.putExtra(EXTRA_SHARE_SCREENSHOT, screenshot);
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 try {
636 c.startActivity(Intent.createChooser(send, c.getString(
637 R.string.choosertitle_sharevia)));
638 } catch(android.content.ActivityNotFoundException ex) {
639 // if no app handles it, do nothing
640 }
641 }
642
643 private void copy(CharSequence text) {
644 ClipboardManager cm = (ClipboardManager) mActivity
645 .getSystemService(Context.CLIPBOARD_SERVICE);
646 cm.setText(text);
647 }
648
649 // lifecycle
650
John Reck9c35b9c2012-05-30 10:08:50 -0700651 @Override
652 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800654 // update the menu in case of a locale change
655 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800656 if (mOptionsMenuOpen) {
657 mActivity.closeOptionsMenu();
658 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
659 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 if (mPageDialogsHandler != null) {
661 mPageDialogsHandler.onConfigurationChanged(config);
662 }
663 mUi.onConfigurationChanged(config);
664 }
665
666 @Override
667 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700668 if (!mUi.isWebShowing()) {
669 mUi.showWeb(false);
670 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700671 mIntentHandler.onNewIntent(intent);
672 }
673
John Reck9c35b9c2012-05-30 10:08:50 -0700674 @Override
675 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800676 if (mUi.isCustomViewShowing()) {
677 hideCustomView();
678 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 if (mActivityPaused) {
680 Log.e(LOGTAG, "BrowserActivity is already paused.");
681 return;
682 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800684 Tab tab = mTabControl.getCurrentTab();
685 if (tab != null) {
686 tab.pause();
687 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700688 if (mWakeLock == null) {
689 PowerManager pm = (PowerManager) mActivity
690 .getSystemService(Context.POWER_SERVICE);
691 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
692 }
Michael Kolb70976932010-11-30 11:34:01 -0800693 mWakeLock.acquire();
694 mHandler.sendMessageDelayed(mHandler
695 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
696 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 }
698 mUi.onPause();
699 mNetworkHandler.onPause();
700
701 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100702 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700703 if (sThumbnailBitmap != null) {
704 sThumbnailBitmap.recycle();
705 sThumbnailBitmap = null;
706 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 }
708
John Reck9c35b9c2012-05-30 10:08:50 -0700709 @Override
710 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700711 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800712 Bundle saveState = createSaveState();
713
714 // crash recovery manages all save & restore state
715 mCrashRecoveryHandler.writeState(saveState);
716 mSettings.setLastRunPaused(true);
717 }
718
719 /**
720 * Save the current state to outState. Does not write the state to
721 * disk.
722 * @return Bundle containing the current state of all tabs.
723 */
724 /* package */ Bundle createSaveState() {
725 Bundle saveState = new Bundle();
726 mTabControl.saveState(saveState);
727 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700728 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800729 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700730 }
George Mount3636d0a2011-11-21 09:08:21 -0800731 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700732 }
733
John Reck9c35b9c2012-05-30 10:08:50 -0700734 @Override
735 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700736 if (!mActivityPaused) {
737 Log.e(LOGTAG, "BrowserActivity is already resumed.");
738 return;
739 }
George Mount3636d0a2011-11-21 09:08:21 -0800740 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700741 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800742 Tab current = mTabControl.getCurrentTab();
743 if (current != null) {
744 current.resume();
745 resumeWebViewTimers(current);
746 }
John Reckf57c0292011-07-21 18:15:39 -0700747 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200748
Michael Kolb8233fac2010-10-26 16:08:53 -0700749 mUi.onResume();
750 mNetworkHandler.onResume();
751 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100752 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700753 if (mVoiceResult != null) {
754 mUi.onVoiceResult(mVoiceResult);
755 mVoiceResult = null;
756 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800757 if (current != null && current.hasCrashed) {
758 current.showCrashView();
759 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700760 }
761
John Reckf57c0292011-07-21 18:15:39 -0700762 private void releaseWakeLock() {
763 if (mWakeLock != null && mWakeLock.isHeld()) {
764 mHandler.removeMessages(RELEASE_WAKELOCK);
765 mWakeLock.release();
766 }
767 }
768
Michael Kolb70976932010-11-30 11:34:01 -0800769 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800770 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800771 * @param tab guaranteed non-null
772 */
773 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700774 boolean inLoad = tab.inPageLoad();
775 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
776 CookieSyncManager.getInstance().startSync();
777 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100778 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700779 }
780 }
781
Michael Kolb70976932010-11-30 11:34:01 -0800782 /**
783 * Pause all WebView timers using the WebView of the given tab
784 * @param tab
785 * @return true if the timers are paused or tab is null
786 */
787 private boolean pauseWebViewTimers(Tab tab) {
788 if (tab == null) {
789 return true;
790 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700791 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100792 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700793 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 }
Michael Kolb70976932010-11-30 11:34:01 -0800795 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700796 }
797
John Reck9c35b9c2012-05-30 10:08:50 -0700798 @Override
799 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800800 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700801 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
802 mUploadHandler = null;
803 }
804 if (mTabControl == null) return;
805 mUi.onDestroy();
806 // Remove the current tab and sub window
807 Tab t = mTabControl.getCurrentTab();
808 if (t != null) {
809 dismissSubWindow(t);
810 removeTab(t);
811 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500812 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700813 // Destroy all the tabs
814 mTabControl.destroy();
815 WebIconDatabase.getInstance().close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700816 }
817
818 protected boolean isActivityPaused() {
819 return mActivityPaused;
820 }
821
John Reck9c35b9c2012-05-30 10:08:50 -0700822 @Override
823 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700824 mTabControl.freeMemory();
825 }
826
827 @Override
828 public boolean shouldShowErrorConsole() {
829 return mShouldShowErrorConsole;
830 }
831
832 protected void setShouldShowErrorConsole(boolean show) {
833 if (show == mShouldShowErrorConsole) {
834 // Nothing to do.
835 return;
836 }
837 mShouldShowErrorConsole = show;
838 Tab t = mTabControl.getCurrentTab();
839 if (t == null) {
840 // There is no current tab so we cannot toggle the error console
841 return;
842 }
843 mUi.setShouldShowErrorConsole(t, show);
844 }
845
846 @Override
847 public void stopLoading() {
848 mLoadStopped = true;
849 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700851 if (w != null) {
852 w.stopLoading();
853 mUi.onPageStopped(tab);
854 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 }
856
857 boolean didUserStopLoading() {
858 return mLoadStopped;
859 }
860
kaiyiza016da12013-08-26 17:50:22 +0800861 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700862 final String reminderType = getContext().getResources().getString(
863 R.string.def_wifi_browser_interaction_remind_type);
864 final String selectionConnnection = getContext().getResources().getString(
865 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700866 final String wifiSelection = getContext().getResources().getString(
867 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700868
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700869 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
870 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700871 return;
872
kaiyiza016da12013-08-26 17:50:22 +0800873 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700874 Context.CONNECTIVITY_SERVICE);
875 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700876 WifiManager wifiMgr = (WifiManager) this.getContext()
877 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700878 if (networkInfo == null
879 || (networkInfo != null && (networkInfo.getType() !=
880 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700881 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
882 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700883 List<ScanResult> list = wifiMgr.getScanResults();
884 // Have no AP's for Wifi's fall back to data
885 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700886 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700887 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
888 this.getContext().startActivity(intent);
889 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700890 // Request to select Wifi AP
891 try {
892 Intent intent = new Intent(wifiSelection);
893 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
894 this.getContext().startActivity(intent);
895 } catch (Exception e) {
896 String err_msg = this.getContext().getString(
897 R.string.acivity_not_found, wifiSelection);
898 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
899 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700900 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700901 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800902 }
903 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700904
Michael Kolb8233fac2010-10-26 16:08:53 -0700905 // WebViewController
906
907 @Override
John Reck324d4402011-01-11 16:56:42 -0800908 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700909
910 // We've started to load a new page. If there was a pending message
911 // to save a screenshot then we will now take the new page and save
912 // an incorrect screenshot. Therefore, remove any pending thumbnail
913 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700914 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700915
916 // reset sync timer to avoid sync starts during loading a page
917 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700918 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700919 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700920 boolean networkNotifier =
921 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700922 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700923 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800924 } else {
925 if (!mNetworkHandler.isNetworkUp()) {
926 view.setNetworkAvailable(false);
927 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 }
929
930 // when BrowserActivity just starts, onPageStarted may be called before
931 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
932 // to start the timer. As we won't switch tabs while an activity is in
933 // pause state, we can ensure calling resume and pause in pair.
934 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800935 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 }
937 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 endActionMode();
939
John Reck30c714c2010-12-16 17:30:34 -0800940 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700941
John Reck324d4402011-01-11 16:56:42 -0800942 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 // update the bookmark database for favicon
944 maybeUpdateFavicon(tab, null, url, favicon);
945
946 Performance.tracePageStart(url);
947
948 // Performance probe
949 if (false) {
950 Performance.onPageStarted();
951 }
952
953 }
954
955 @Override
John Reck324d4402011-01-11 16:56:42 -0800956 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700957 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800958 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200959
Michael Kolb8233fac2010-10-26 16:08:53 -0700960 // Performance probe
961 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800962 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700963 }
964
965 Performance.tracePageFinished();
966 }
967
968 @Override
John Reck30c714c2010-12-16 17:30:34 -0800969 public void onProgressChanged(Tab tab) {
970 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700971
972 if (newProgress == 100) {
973 CookieSyncManager.getInstance().sync();
974 // onProgressChanged() may continue to be called after the main
975 // frame has finished loading, as any remaining sub frames continue
976 // to load. We'll only get called once though with newProgress as
977 // 100 when everything is loaded. (onPageFinished is called once
978 // when the main frame completes loading regardless of the state of
979 // any sub frames so calls to onProgressChanges may continue after
980 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800981 if (tab.inPageLoad()) {
982 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200983 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
984 // pause the WebView timer and release the wake lock if it is
985 // finished while BrowserActivity is in pause state.
986 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 }
John Reckd9862372012-02-21 15:04:50 -0800988 if (!tab.isPrivateBrowsingEnabled()
989 && !TextUtils.isEmpty(tab.getUrl())
990 && !tab.isSnapshot()) {
991 // Only update the bookmark screenshot if the user did not
992 // cancel the load early and there is not already
993 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700994 if (tab.shouldUpdateThumbnail() &&
995 (tab.inForeground() && !didUserStopLoading()
996 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800997 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
998 mHandler.sendMessageDelayed(mHandler.obtainMessage(
999 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001000 1500);
John Reckd9862372012-02-21 15:04:50 -08001001 }
1002 }
1003 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001005 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001006 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001007 // still loading
1008 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001009 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001010 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001011 }
1012 }
John Reck30c714c2010-12-16 17:30:34 -08001013 mUi.onProgressChanged(tab);
1014 }
1015
1016 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001017 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001018 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001019 }
1020
1021 @Override
1022 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001023 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001024 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001025 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1027 return;
1028 }
1029 // Update the title in the history database if not in private browsing mode
1030 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001031 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001032 }
1033 }
1034
1035 @Override
1036 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001037 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1039 }
1040
1041 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001042 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1043 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 }
1045
1046 @Override
1047 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1048 if (mMenuIsDown) {
1049 // only check shortcut key when MENU is held
1050 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1051 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001053 int keyCode = event.getKeyCode();
1054 // We need to send almost every key to WebKit. However:
1055 // 1. We don't want to block the device on the renderer for
1056 // some keys like menu, home, call.
1057 // 2. There are no WebKit equivalents for some of these keys
1058 // (see app/keyboard_codes_win.h)
1059 // Note that these are not the same set as KeyEvent.isSystemKey:
1060 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1061 if (keyCode == KeyEvent.KEYCODE_MENU ||
1062 keyCode == KeyEvent.KEYCODE_HOME ||
1063 keyCode == KeyEvent.KEYCODE_BACK ||
1064 keyCode == KeyEvent.KEYCODE_CALL ||
1065 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1066 keyCode == KeyEvent.KEYCODE_POWER ||
1067 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1068 keyCode == KeyEvent.KEYCODE_CAMERA ||
1069 keyCode == KeyEvent.KEYCODE_FOCUS ||
1070 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1071 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1072 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1073 return true;
1074 }
1075
1076 // We also have to intercept some shortcuts before we send them to the ContentView.
1077 if (event.isCtrlPressed() && (
1078 keyCode == KeyEvent.KEYCODE_TAB ||
1079 keyCode == KeyEvent.KEYCODE_W ||
1080 keyCode == KeyEvent.KEYCODE_F4)) {
1081 return true;
1082 }
1083
1084 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 }
1086
1087 @Override
John Reck997b1b72012-04-19 18:08:25 -07001088 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001089 if (!isActivityPaused()) {
1090 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001091 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 } else {
John Reck997b1b72012-04-19 18:08:25 -07001093 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001094 }
1095 }
John Reck997b1b72012-04-19 18:08:25 -07001096 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 }
1098
1099 @Override
John Reck324d4402011-01-11 16:56:42 -08001100 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001101 boolean disableHistoryWrites =
1102 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1103
1104 // Don't save anything in private browsing mode or when explicitly set
1105 // not to write history via an overlay
1106 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001107 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001108
John Reck324d4402011-01-11 16:56:42 -08001109 if (TextUtils.isEmpty(url)
1110 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001111 return;
1112 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001113
John Reckf57c0292011-07-21 18:15:39 -07001114 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001115 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 }
1117
1118 @Override
1119 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1120 AsyncTask<Void, Void, String[]> task =
1121 new AsyncTask<Void, Void, String[]>() {
1122 @Override
1123 public String[] doInBackground(Void... unused) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001124 Object[] params = {mActivity.getContentResolver()};
1125 Class[] type = new Class[] {ContentResolver.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001126 return (String[])ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001127 "android.provider.Browser","getVisitedHistory",
1128 type, params);
Michael Kolb8233fac2010-10-26 16:08:53 -07001129 }
1130 @Override
1131 public void onPostExecute(String[] result) {
1132 callback.onReceiveValue(result);
1133 }
1134 };
1135 task.execute();
1136 }
1137
1138 @Override
1139 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1140 final HttpAuthHandler handler, final String host,
1141 final String realm) {
1142 String username = null;
1143 String password = null;
1144
1145 boolean reuseHttpAuthUsernamePassword
1146 = handler.useHttpAuthUsernamePassword();
1147
1148 if (reuseHttpAuthUsernamePassword && view != null) {
1149 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1150 if (credentials != null && credentials.length == 2) {
1151 username = credentials[0];
1152 password = credentials[1];
1153 }
1154 }
1155
1156 if (username != null && password != null) {
1157 handler.proceed(username, password);
1158 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001159 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1161 } else {
1162 handler.cancel();
1163 }
1164 }
1165 }
1166
1167 @Override
1168 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001169 String contentDisposition, String mimetype, String referer,
1170 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001171 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001172 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001173 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001174 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001175 // This Tab was opened for the sole purpose of downloading a
1176 // file. Remove it.
1177 if (tab == mTabControl.getCurrentTab()) {
1178 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001179 if (tab.getDerivedFromIntent())
1180 closeTab(tab);
1181 else
1182 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001183 } else {
1184 // In this case, it is not.
1185 closeTab(tab);
1186 }
1187 }
1188 }
1189
1190 @Override
1191 public Bitmap getDefaultVideoPoster() {
1192 return mUi.getDefaultVideoPoster();
1193 }
1194
1195 @Override
1196 public View getVideoLoadingProgressView() {
1197 return mUi.getVideoLoadingProgressView();
1198 }
1199
1200 @Override
1201 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1202 SslError error) {
1203 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1204 }
1205
1206 // helper method
1207
1208 /*
1209 * Update the favorites icon if the private browsing isn't enabled and the
1210 * icon is valid.
1211 */
1212 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1213 final String url, Bitmap favicon) {
1214 if (favicon == null) {
1215 return;
1216 }
1217 if (!tab.isPrivateBrowsingEnabled()) {
1218 Bookmarks.updateFavicon(mActivity
1219 .getContentResolver(), originalUrl, url, favicon);
1220 }
1221 }
1222
Leon Scroggins4cd97792010-12-03 15:31:56 -05001223 @Override
1224 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001225 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001226 mUi.bookmarkedStatusHasChanged(tab);
1227 }
1228
Michael Kolb8233fac2010-10-26 16:08:53 -07001229 // end WebViewController
1230
1231 protected void pageUp() {
1232 getCurrentTopWebView().pageUp(false);
1233 }
1234
1235 protected void pageDown() {
1236 getCurrentTopWebView().pageDown(false);
1237 }
1238
1239 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001240 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001241 public void editUrl() {
1242 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001243 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001244 }
1245
John Reck9c35b9c2012-05-30 10:08:50 -07001246 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001247 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001248 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001249 if (tab.inForeground()) {
1250 if (mUi.isCustomViewShowing()) {
1251 callback.onCustomViewHidden();
1252 return;
1253 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001254 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001255 // Save the menu state and set it to empty while the custom
1256 // view is showing.
1257 mOldMenuState = mMenuState;
1258 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001259 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001260 }
1261 }
1262
1263 @Override
1264 public void hideCustomView() {
1265 if (mUi.isCustomViewShowing()) {
1266 mUi.onHideCustomView();
1267 // Reset the old menu state.
1268 mMenuState = mOldMenuState;
1269 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001270 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001271 }
1272 }
1273
John Reck9c35b9c2012-05-30 10:08:50 -07001274 @Override
1275 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 Intent intent) {
1277 if (getCurrentTopWebView() == null) return;
1278 switch (requestCode) {
1279 case PREFERENCES_PAGE:
1280 if (resultCode == Activity.RESULT_OK && intent != null) {
1281 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001282 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 mTabControl.removeParentChildRelationShips();
1284 }
1285 }
1286 break;
1287 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001288 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001289 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001290 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001292 case AUTOFILL_SETUP:
1293 // Determine whether a profile was actually set up or not
1294 // and if so, send the message back to the WebTextView to
1295 // fill the form with the new profile.
1296 if (getSettings().getAutoFillProfile() != null) {
1297 mAutoFillSetupMessage.sendToTarget();
1298 mAutoFillSetupMessage = null;
1299 }
1300 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001301 case COMBO_VIEW:
1302 if (intent == null || resultCode != Activity.RESULT_OK) {
1303 break;
1304 }
John Reck3ba45532011-08-11 16:26:53 -07001305 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001306 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1307 Tab t = getCurrentTab();
1308 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001309 mUpdateMyNavThumbnail = true;
1310 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001311 loadUrl(t, uri.toString());
1312 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1313 String[] urls = intent.getStringArrayExtra(
1314 ComboViewActivity.EXTRA_OPEN_ALL);
1315 Tab parent = getCurrentTab();
1316 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001317 if (url != null) {
1318 parent = openTab(url, parent,
1319 !mSettings.openInBackground(), true);
1320 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001321 }
1322 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1323 long id = intent.getLongExtra(
1324 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1325 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001326 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001327 }
1328 }
1329 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001330 case VOICE_RESULT:
1331 if (resultCode == Activity.RESULT_OK && intent != null) {
1332 ArrayList<String> results = intent.getStringArrayListExtra(
1333 RecognizerIntent.EXTRA_RESULTS);
1334 if (results.size() >= 1) {
1335 mVoiceResult = results.get(0);
1336 }
1337 }
1338 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001339 case MY_NAVIGATION:
1340 if (intent == null || resultCode != Activity.RESULT_OK) {
1341 break;
1342 }
1343
1344 if (intent.getBooleanExtra("need_refresh", false) &&
1345 getCurrentTopWebView() != null) {
1346 getCurrentTopWebView().reload();
1347 }
1348 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001349 default:
1350 break;
1351 }
1352 getCurrentTopWebView().requestFocus();
1353 }
1354
1355 /**
1356 * Open the Go page.
1357 * @param startWithHistory If true, open starting on the history tab.
1358 * Otherwise, start with the bookmarks tab.
1359 */
1360 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001361 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001362 if (mTabControl.getCurrentWebView() == null) {
1363 return;
1364 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001365 // clear action mode
1366 if (isInCustomActionMode()) {
1367 endActionMode();
1368 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 Bundle extras = new Bundle();
1370 // Disable opening in a new window if we have maxed out the windows
1371 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1372 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001373 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001374 }
1375
1376 // combo view callbacks
1377
Michael Kolb8233fac2010-10-26 16:08:53 -07001378 // key handling
1379 protected void onBackKey() {
1380 if (!mUi.onBackKey()) {
1381 WebView subwindow = mTabControl.getCurrentSubWindow();
1382 if (subwindow != null) {
1383 if (subwindow.canGoBack()) {
1384 subwindow.goBack();
1385 } else {
1386 dismissSubWindow(mTabControl.getCurrentTab());
1387 }
1388 } else {
1389 goBackOnePageOrQuit();
1390 }
1391 }
1392 }
1393
Michael Kolb4bd767d2011-05-27 11:33:55 -07001394 protected boolean onMenuKey() {
1395 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001396 }
1397
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 // menu handling and state
1399 // TODO: maybe put into separate handler
1400
John Reck9c35b9c2012-05-30 10:08:50 -07001401 @Override
1402 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001403 if (mMenuState == EMPTY_MENU) {
1404 return false;
1405 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 MenuInflater inflater = mActivity.getMenuInflater();
1407 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001408 return true;
1409 }
1410
John Reck9c35b9c2012-05-30 10:08:50 -07001411 @Override
1412 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001413 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001414 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001415 return;
1416 }
1417 if (!(v instanceof WebView)) {
1418 return;
1419 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001420 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001421 WebView.HitTestResult result = webview.getHitTestResult();
1422 if (result == null) {
1423 return;
1424 }
1425
1426 int type = result.getType();
1427 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1428 Log.w(LOGTAG,
1429 "We should not show context menu when nothing is touched");
1430 return;
1431 }
1432 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1433 // let TextView handles context menu
1434 return;
1435 }
1436
1437 // Note, http://b/issue?id=1106666 is requesting that
1438 // an inflated menu can be used again. This is not available
1439 // yet, so inflate each time (yuk!)
1440 MenuInflater inflater = mActivity.getMenuInflater();
1441 inflater.inflate(R.menu.browsercontext, menu);
1442
1443 // Show the correct menu group
1444 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001445 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001446 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001447 menu.setGroupVisible(R.id.PHONE_MENU,
1448 type == WebView.HitTestResult.PHONE_TYPE);
1449 menu.setGroupVisible(R.id.EMAIL_MENU,
1450 type == WebView.HitTestResult.EMAIL_TYPE);
1451 menu.setGroupVisible(R.id.GEO_MENU,
1452 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001453 String itemUrl = null;
1454 String url = webview.getOriginalUrl();
1455 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1456 itemUrl = Uri.decode(navigationUrl);
1457 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1458 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1459 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1460 } else {
1461 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1462 }
1463 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1464 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1465 } else {
1466 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1467
1468 menu.setGroupVisible(R.id.IMAGE_MENU,
1469 type == WebView.HitTestResult.IMAGE_TYPE
1470 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1471 menu.setGroupVisible(R.id.ANCHOR_MENU,
1472 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1473 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001474 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1475 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001476 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001477 // Setup custom handling depending on the type
1478 switch (type) {
1479 case WebView.HitTestResult.PHONE_TYPE:
1480 menu.setHeaderTitle(Uri.decode(extra));
1481 menu.findItem(R.id.dial_context_menu_id).setIntent(
1482 new Intent(Intent.ACTION_VIEW, Uri
1483 .parse(WebView.SCHEME_TEL + extra)));
1484 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1485 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1486 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1487 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1488 addIntent);
1489 menu.findItem(R.id.copy_phone_context_menu_id)
1490 .setOnMenuItemClickListener(
1491 new Copy(extra));
1492 break;
1493
1494 case WebView.HitTestResult.EMAIL_TYPE:
1495 menu.setHeaderTitle(extra);
1496 menu.findItem(R.id.email_context_menu_id).setIntent(
1497 new Intent(Intent.ACTION_VIEW, Uri
1498 .parse(WebView.SCHEME_MAILTO + extra)));
1499 menu.findItem(R.id.copy_mail_context_menu_id)
1500 .setOnMenuItemClickListener(
1501 new Copy(extra));
1502 break;
1503
1504 case WebView.HitTestResult.GEO_TYPE:
1505 menu.setHeaderTitle(extra);
1506 menu.findItem(R.id.map_context_menu_id).setIntent(
1507 new Intent(Intent.ACTION_VIEW, Uri
1508 .parse(WebView.SCHEME_GEO
1509 + URLEncoder.encode(extra))));
1510 menu.findItem(R.id.copy_geo_context_menu_id)
1511 .setOnMenuItemClickListener(
1512 new Copy(extra));
1513 break;
1514
1515 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1516 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001517 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001518 // decide whether to show the open link in new tab option
1519 boolean showNewTab = mTabControl.canCreateNewTab();
1520 MenuItem newTabItem
1521 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001522 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001523 ? R.string.contextmenu_openlink_newwindow_background
1524 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001525 newTabItem.setVisible(showNewTab);
1526 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001527 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1528 newTabItem.setOnMenuItemClickListener(
1529 new MenuItem.OnMenuItemClickListener() {
1530 @Override
1531 public boolean onMenuItemClick(MenuItem item) {
1532 final HashMap<String, WebView> hrefMap =
1533 new HashMap<String, WebView>();
1534 hrefMap.put("webview", webview);
1535 final Message msg = mHandler.obtainMessage(
1536 FOCUS_NODE_HREF,
1537 R.id.open_newtab_context_menu_id,
1538 0, hrefMap);
1539 webview.requestFocusNodeHref(msg);
1540 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001541 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001542 });
1543 } else {
1544 newTabItem.setOnMenuItemClickListener(
1545 new MenuItem.OnMenuItemClickListener() {
1546 @Override
1547 public boolean onMenuItemClick(MenuItem item) {
1548 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001549 openTab(extra, parent,
1550 !mSettings.openInBackground(),
1551 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001552 return true;
1553 }
1554 });
1555 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001556 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001557 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1558 menu.setHeaderTitle(navigationUrl);
1559 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1560
1561 if (itemUrl != null) {
1562 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1563 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1564 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1565 @Override
1566 public boolean onMenuItemClick(MenuItem item) {
1567 final Intent intent = new Intent(Controller.this
1568 .getContext(),
1569 AddMyNavigationPage.class);
1570 Bundle bundle = new Bundle();
1571 String url = Uri.decode(navigationUrl);
1572 bundle.putBoolean("isAdding", false);
1573 bundle.putString("url", url);
1574 bundle.putString("name", getNameFromUrl(url));
1575 intent.putExtra("websites", bundle);
1576 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1577 return false;
1578 }
1579 });
1580 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1581 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1582 @Override
1583 public boolean onMenuItemClick(MenuItem item) {
1584 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1585 return false;
1586 }
1587 });
1588 }
1589 } else {
1590 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1591 }
1592 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001593 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1594 break;
1595 }
1596 // otherwise fall through to handle image part
1597 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001598 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1599 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1601 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001602 shareItem.setOnMenuItemClickListener(
1603 new MenuItem.OnMenuItemClickListener() {
1604 @Override
1605 public boolean onMenuItemClick(MenuItem item) {
1606 sharePage(mActivity, null, extra, null,
1607 null);
1608 return true;
1609 }
1610 }
1611 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001612 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001613 menu.findItem(R.id.view_image_context_menu_id)
1614 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1615 @Override
1616 public boolean onMenuItemClick(MenuItem item) {
1617 openTab(extra, mTabControl.getCurrentTab(), true, true);
1618 return false;
1619 }
1620 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001621 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1622 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1623 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001624 menu.findItem(R.id.set_wallpaper_context_menu_id).
1625 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1626 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001627 break;
1628
1629 default:
1630 Log.w(LOGTAG, "We should not get here.");
1631 break;
1632 }
1633 //update the ui
1634 mUi.onContextMenuCreated(menu);
1635 }
1636
kaiyiz6e5b3e02013-08-19 20:02:01 +08001637 public void startAddMyNavigation(String url) {
1638 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1639 Bundle bundle = new Bundle();
1640 bundle.putBoolean("isAdding", true);
1641 bundle.putString("url", url);
1642 bundle.putString("name", getNameFromUrl(url));
1643 intent.putExtra("websites", bundle);
1644 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1645 }
1646
1647 private void showMyNavigationDeleteDialog(final String itemUrl) {
1648 new AlertDialog.Builder(this.getContext())
1649 .setTitle(R.string.my_navigation_delete_label)
1650 .setIcon(android.R.drawable.ic_dialog_alert)
1651 .setMessage(R.string.my_navigation_delete_msg)
1652 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1653 @Override
1654 public void onClick(DialogInterface dialog, int whichButton) {
1655 deleteMyNavigationItem(itemUrl);
1656 }
1657 })
1658 .setNegativeButton(R.string.cancel, null)
1659 .show();
1660 }
1661
1662 private void deleteMyNavigationItem(final String itemUrl) {
1663 ContentResolver cr = this.getContext().getContentResolver();
1664 Cursor cursor = null;
1665
1666 try {
1667 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1668 new String[] {
1669 MyNavigationUtil.ID
1670 }, "url = ?", new String[] {
1671 itemUrl
1672 }, null);
1673 if (null != cursor && cursor.moveToFirst()) {
1674 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1675 cursor.getLong(0));
1676
1677 ContentValues values = new ContentValues();
1678 values.put(MyNavigationUtil.TITLE, "");
1679 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1680 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1681 ByteArrayOutputStream os = new ByteArrayOutputStream();
1682 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1683 R.raw.my_navigation_add);
1684 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1685 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1686 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1687 cr.update(uri, values, null, null);
1688 } else {
1689 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1690 }
1691 } catch (IllegalStateException e) {
1692 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1693 } finally {
1694 if (null != cursor) {
1695 cursor.close();
1696 }
1697 }
1698
1699 if (getCurrentTopWebView() != null) {
1700 getCurrentTopWebView().reload();
1701 }
1702 }
1703
1704 private String getNameFromUrl(String itemUrl) {
1705 ContentResolver cr = this.getContext().getContentResolver();
1706 Cursor cursor = null;
1707 String name = null;
1708
1709 try {
1710 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1711 new String[] {
1712 MyNavigationUtil.TITLE
1713 }, "url = ?", new String[] {
1714 itemUrl
1715 }, null);
1716 if (null != cursor && cursor.moveToFirst()) {
1717 name = cursor.getString(0);
1718 } else {
1719 Log.e(LOGTAG, "this item does not exist!");
1720 }
1721 } catch (IllegalStateException e) {
1722 Log.e(LOGTAG, "getNameFromUrl", e);
1723 } finally {
1724 if (null != cursor) {
1725 cursor.close();
1726 }
1727 }
1728 return name;
1729 }
1730
1731 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1732 int width = mActivity.getResources().getDimensionPixelOffset(
1733 R.dimen.myNavigationThumbnailWidth);
1734 int height = mActivity.getResources().getDimensionPixelOffset(
1735 R.dimen.myNavigationThumbnailHeight);
1736
1737 final Bitmap bm = createScreenshot(webView, width, height);
1738
1739 if (bm == null) {
1740 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1741 return;
1742 }
1743
1744 final ContentResolver cr = mActivity.getContentResolver();
1745 new AsyncTask<Void, Void, Void>() {
1746 @Override
1747 protected Void doInBackground(Void... unused) {
1748 ContentResolver cr = mActivity.getContentResolver();
1749 Cursor cursor = null;
1750 try {
1751 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1752 new String[] {
1753 MyNavigationUtil.ID
1754 }, "url = ?", new String[] {
1755 itemUrl
1756 }, null);
1757 if (null != cursor && cursor.moveToFirst()) {
1758 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1759 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1760
1761 ContentValues values = new ContentValues();
1762 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1763 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1764 cursor.getLong(0));
1765 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1766 cr.update(uri, values, null, null);
1767 os.close();
1768 }
1769 } catch (IllegalStateException e) {
1770 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1771 } catch (IOException e) {
1772 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1773 } finally {
1774 if (null != cursor) {
1775 cursor.close();
1776 }
1777 }
1778 return null;
1779 }
1780 }.execute();
1781 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001782 /**
1783 * As the menu can be open when loading state changes
1784 * we must manually update the state of the stop/reload menu
1785 * item
1786 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001787 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001788 if (menu == null) {
1789 return;
1790 }
1791 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001792 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001793 menu.findItem(R.id.stop_menu_id):
1794 menu.findItem(R.id.reload_menu_id);
1795 if (src != null) {
1796 dest.setIcon(src.getIcon());
1797 dest.setTitle(src.getTitle());
1798 }
1799 }
1800
John Reck9c35b9c2012-05-30 10:08:50 -07001801 @Override
1802 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001803 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001804 // hold on to the menu reference here; it is used by the page callbacks
1805 // to update the menu based on loading state
1806 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001807 // Note: setVisible will decide whether an item is visible; while
1808 // setEnabled() will decide whether an item is enabled, which also means
1809 // whether the matching shortcut key will function.
1810 switch (mMenuState) {
1811 case EMPTY_MENU:
1812 if (mCurrentMenuState != mMenuState) {
1813 menu.setGroupVisible(R.id.MAIN_MENU, false);
1814 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1815 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1816 }
1817 break;
1818 default:
1819 if (mCurrentMenuState != mMenuState) {
1820 menu.setGroupVisible(R.id.MAIN_MENU, true);
1821 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1822 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1823 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001824 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001825 break;
1826 }
1827 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001828 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001829 }
1830
Michael Kolb4bf79712011-07-14 14:18:12 -07001831 @Override
1832 public void updateMenuState(Tab tab, Menu menu) {
1833 boolean canGoBack = false;
1834 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001835 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001836 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001837 if (tab != null) {
1838 canGoBack = tab.canGoBack();
1839 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001840 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001841 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001842 }
1843 final MenuItem back = menu.findItem(R.id.back_menu_id);
1844 back.setEnabled(canGoBack);
1845
1846 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001847
1848 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1849 forward.setEnabled(canGoForward);
1850
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001851 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1852 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001853 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001854 if (source != null && dest != null) {
1855 dest.setTitle(source.getTitle());
1856 dest.setIcon(source.getIcon());
1857 }
John Recke1a03a32011-09-14 17:04:16 -07001858 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001859
1860 // decide whether to show the share link option
1861 PackageManager pm = mActivity.getPackageManager();
1862 Intent send = new Intent(Intent.ACTION_SEND);
1863 send.setType("text/plain");
1864 ResolveInfo ri = pm.resolveActivity(send,
1865 PackageManager.MATCH_DEFAULT_ONLY);
1866 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1867
1868 boolean isNavDump = mSettings.enableNavDump();
1869 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1870 nav.setVisible(isNavDump);
1871 nav.setEnabled(isNavDump);
1872
1873 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001874 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1875 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001876 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1877 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001878 // history and snapshots item are the members of COMBO menu group,
1879 // so if show history item, only make snapshots item invisible.
1880 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001881
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001882 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001883 }
1884
John Reck9c35b9c2012-05-30 10:08:50 -07001885 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001886 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 if (null == getCurrentTopWebView()) {
1888 return false;
1889 }
1890 if (mMenuIsDown) {
1891 // The shortcut action consumes the MENU. Even if it is still down,
1892 // it won't trigger the next shortcut action. In the case of the
1893 // shortcut action triggering a new activity, like Bookmarks, we
1894 // won't get onKeyUp for MENU. So it is important to reset it here.
1895 mMenuIsDown = false;
1896 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001897 if (mUi.onOptionsItemSelected(item)) {
1898 // ui callback handled it
1899 return true;
1900 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001901 switch (item.getItemId()) {
1902 // -- Main menu
1903 case R.id.new_tab_menu_id:
1904 openTabToHomePage();
1905 break;
1906
1907 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001908 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001909 break;
1910
Afzal Najamd4e33312012-04-26 01:54:01 -04001911 case R.id.close_other_tabs_id:
1912 closeOtherTabs();
1913 break;
1914
Michael Kolb8233fac2010-10-26 16:08:53 -07001915 case R.id.goto_menu_id:
1916 editUrl();
1917 break;
1918
1919 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001920 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1921 break;
1922
1923 case R.id.history_menu_id:
1924 bookmarksOrHistoryPicker(ComboViews.History);
1925 break;
1926
1927 case R.id.snapshots_menu_id:
1928 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001929 break;
1930
Michael Kolb8233fac2010-10-26 16:08:53 -07001931 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001932 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 break;
1934
1935 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001936 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001937 stopLoading();
1938 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001939 Tab currentTab = mTabControl.getCurrentTab();
1940 if (currentTab.hasCrashed) {
1941 currentTab.replaceCrashView(getCurrentTopWebView(),
1942 currentTab.getViewContainer());
1943 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001944 getCurrentTopWebView().reload();
1945 }
1946 break;
1947
1948 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001949 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001950 break;
1951
1952 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001953 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001954 break;
1955
1956 case R.id.close_menu_id:
1957 // Close the subwindow if it exists.
1958 if (mTabControl.getCurrentSubWindow() != null) {
1959 dismissSubWindow(mTabControl.getCurrentTab());
1960 break;
1961 }
1962 closeCurrentTab();
1963 break;
1964
kaiyiza8b6dbb2013-07-29 18:11:22 +08001965 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001966 Object[] params = { new String("persist.debug.browsermonkeytest")};
1967 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001968 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001969 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001970 if (ret != null && ret.equals("enable"))
1971 break;
luxiaolb40014b2013-07-19 10:01:43 +08001972 showExitDialog(mActivity);
1973 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001974 case R.id.homepage_menu_id:
1975 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001976 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001977 break;
1978
1979 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001980 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001981 break;
1982
1983 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001984 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001985 break;
1986
John Reck2bc80422011-06-30 15:11:49 -07001987 case R.id.save_snapshot_menu_id:
1988 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001989 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001990 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001991 break;
1992
Michael Kolb8233fac2010-10-26 16:08:53 -07001993 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001994 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001995 break;
1996
John Recke1a03a32011-09-14 17:04:16 -07001997 case R.id.snapshot_go_live:
1998 goLive();
1999 return true;
2000
Michael Kolb8233fac2010-10-26 16:08:53 -07002001 case R.id.share_page_menu_id:
2002 Tab currentTab = mTabControl.getCurrentTab();
2003 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002004 return false;
2005 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002006 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002007 break;
2008
2009 case R.id.dump_nav_menu_id:
2010 getCurrentTopWebView().debugDump();
2011 break;
2012
Michael Kolb8233fac2010-10-26 16:08:53 -07002013 case R.id.zoom_in_menu_id:
2014 getCurrentTopWebView().zoomIn();
2015 break;
2016
2017 case R.id.zoom_out_menu_id:
2018 getCurrentTopWebView().zoomOut();
2019 break;
2020
2021 case R.id.view_downloads_menu_id:
2022 viewDownloads();
2023 break;
2024
John Reck42229bc2011-08-19 13:26:43 -07002025 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002026 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002027 break;
2028
Michael Kolb8233fac2010-10-26 16:08:53 -07002029 case R.id.window_one_menu_id:
2030 case R.id.window_two_menu_id:
2031 case R.id.window_three_menu_id:
2032 case R.id.window_four_menu_id:
2033 case R.id.window_five_menu_id:
2034 case R.id.window_six_menu_id:
2035 case R.id.window_seven_menu_id:
2036 case R.id.window_eight_menu_id:
2037 {
2038 int menuid = item.getItemId();
2039 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2040 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2041 Tab desiredTab = mTabControl.getTab(id);
2042 if (desiredTab != null &&
2043 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002044 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002045 }
2046 break;
2047 }
2048 }
2049 }
2050 break;
2051
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002052 case R.id.about_menu_id:
2053 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2054 builder.setTitle(R.string.about);
2055 builder.setCancelable(true);
2056 String ua = "";
2057 final WebView currentWebView = getCurrentWebView();
2058 if (currentWebView != null) {
2059 final WebSettings s = currentWebView.getSettings();
2060 if (s != null) {
2061 ua = s.getUserAgentString();
2062 }
2063 }
2064 builder.setMessage("Agent:" + ua);
2065 builder.setPositiveButton(android.R.string.ok, null);
2066 builder.create().show();
2067 break;
2068
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 default:
2070 return false;
2071 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002072 return true;
2073 }
2074
John Reck68234a92012-04-19 15:27:12 -07002075 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2076 implements OnCancelListener {
2077
2078 private Tab mTab;
2079 private Dialog mProgressDialog;
2080 private ContentValues mValues;
2081
2082 private SaveSnapshotTask(Tab tab) {
2083 mTab = tab;
2084 }
2085
2086 @Override
2087 protected void onPreExecute() {
2088 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2089 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2090 true, true, this);
2091 mValues = mTab.createSnapshotValues();
2092 }
2093
2094 @Override
2095 protected Long doInBackground(Void... params) {
2096 if (!mTab.saveViewState(mValues)) {
2097 return null;
2098 }
2099 if (isCancelled()) {
2100 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2101 File file = mActivity.getFileStreamPath(path);
2102 if (!file.delete()) {
2103 file.deleteOnExit();
2104 }
2105 return null;
2106 }
2107 final ContentResolver cr = mActivity.getContentResolver();
2108 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2109 if (result == null) {
2110 return null;
2111 }
2112 long id = ContentUris.parseId(result);
2113 return id;
2114 }
2115
2116 @Override
2117 protected void onPostExecute(Long id) {
2118 if (isCancelled()) {
2119 return;
2120 }
2121 mProgressDialog.dismiss();
2122 if (id == null) {
2123 Toast.makeText(mActivity, R.string.snapshot_failed,
2124 Toast.LENGTH_SHORT).show();
2125 return;
2126 }
2127 Bundle b = new Bundle();
2128 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2129 mUi.showComboView(ComboViews.Snapshots, b);
2130 }
2131
2132 @Override
2133 public void onCancel(DialogInterface dialog) {
2134 cancel(true);
2135 }
2136 }
2137
Michael Kolb80f75082012-04-10 10:50:06 -07002138 @Override
2139 public void toggleUserAgent() {
2140 WebView web = getCurrentWebView();
2141 mSettings.toggleDesktopUseragent(web);
2142 web.loadUrl(web.getOriginalUrl());
2143 }
2144
2145 @Override
2146 public void findOnPage() {
2147 getCurrentTopWebView().showFindDialog(null, true);
2148 }
2149
2150 @Override
2151 public void openPreferences() {
2152 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2153 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2154 getCurrentTopWebView().getUrl());
2155 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2156 }
2157
2158 @Override
2159 public void bookmarkCurrentPage() {
2160 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2161 if (bookmarkIntent != null) {
2162 mActivity.startActivity(bookmarkIntent);
2163 }
2164 }
2165
John Recke1a03a32011-09-14 17:04:16 -07002166 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002167 SnapshotTab t = (SnapshotTab) getCurrentTab();
2168 t.loadUrl(t.getLiveUrl(), null);
John Recke1a03a32011-09-14 17:04:16 -07002169 }
2170
luxiaolb40014b2013-07-19 10:01:43 +08002171 private void showExitDialog(final Activity activity) {
2172 new AlertDialog.Builder(activity)
2173 .setTitle(R.string.exit_browser_title)
2174 .setIcon(android.R.drawable.ic_dialog_alert)
2175 .setMessage(R.string.exit_browser_msg)
2176 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2177 public void onClick(DialogInterface dialog, int which) {
2178 activity.moveTaskToBack(true);
2179 dialog.dismiss();
2180 }
2181 })
2182 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2183 public void onClick(DialogInterface dialog, int which) {
2184 activity.finish();
2185 mHandler.postDelayed(new Runnable() {
2186 @Override
2187 public void run() {
2188 // TODO Auto-generated method stub
2189 mCrashRecoveryHandler.clearState(true);
2190 int pid = android.os.Process.myPid();
2191 android.os.Process.killProcess(pid);
2192 }
2193 }, 300);
2194 dialog.dismiss();
2195 }
2196 })
2197 .show();
2198 }
Michael Kolb315d5022011-10-13 12:47:11 -07002199 @Override
2200 public void showPageInfo() {
2201 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2202 }
2203
John Reck9c35b9c2012-05-30 10:08:50 -07002204 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002205 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002206 // Let the History and Bookmark fragments handle menus they created.
2207 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2208 return false;
2209 }
2210
Michael Kolb8233fac2010-10-26 16:08:53 -07002211 int id = item.getItemId();
2212 boolean result = true;
2213 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002214 // -- Browser context menu
2215 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002216 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002217 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002218 case R.id.copy_link_context_menu_id:
2219 final WebView webView = getCurrentTopWebView();
2220 if (null == webView) {
2221 result = false;
2222 break;
2223 }
2224 final HashMap<String, WebView> hrefMap =
2225 new HashMap<String, WebView>();
2226 hrefMap.put("webview", webView);
2227 final Message msg = mHandler.obtainMessage(
2228 FOCUS_NODE_HREF, id, 0, hrefMap);
2229 webView.requestFocusNodeHref(msg);
2230 break;
2231
2232 default:
2233 // For other context menus
2234 result = onOptionsItemSelected(item);
2235 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002236 return result;
2237 }
2238
2239 /**
2240 * support programmatically opening the context menu
2241 */
2242 public void openContextMenu(View view) {
2243 mActivity.openContextMenu(view);
2244 }
2245
2246 /**
2247 * programmatically open the options menu
2248 */
2249 public void openOptionsMenu() {
2250 mActivity.openOptionsMenu();
2251 }
2252
John Reck9c35b9c2012-05-30 10:08:50 -07002253 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002254 public boolean onMenuOpened(int featureId, Menu menu) {
2255 if (mOptionsMenuOpen) {
2256 if (mConfigChanged) {
2257 // We do not need to make any changes to the state of the
2258 // title bar, since the only thing that happened was a
2259 // change in orientation
2260 mConfigChanged = false;
2261 } else {
2262 if (!mExtendedMenuOpen) {
2263 mExtendedMenuOpen = true;
2264 mUi.onExtendedMenuOpened();
2265 } else {
2266 // Switching the menu back to icon view, so show the
2267 // title bar once again.
2268 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002269 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002270 }
2271 }
2272 } else {
2273 // The options menu is closed, so open it, and show the title
2274 mOptionsMenuOpen = true;
2275 mConfigChanged = false;
2276 mExtendedMenuOpen = false;
2277 mUi.onOptionsMenuOpened();
2278 }
2279 return true;
2280 }
2281
John Reck9c35b9c2012-05-30 10:08:50 -07002282 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002283 public void onOptionsMenuClosed(Menu menu) {
2284 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002285 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002286 }
2287
John Reck9c35b9c2012-05-30 10:08:50 -07002288 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002289 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002290 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002291 }
2292
2293 // Helper method for getting the top window.
2294 @Override
2295 public WebView getCurrentTopWebView() {
2296 return mTabControl.getCurrentTopWebView();
2297 }
2298
2299 @Override
2300 public WebView getCurrentWebView() {
2301 return mTabControl.getCurrentWebView();
2302 }
2303
2304 /*
2305 * This method is called as a result of the user selecting the options
2306 * menu to see the download window. It shows the download window on top of
2307 * the current window.
2308 */
2309 void viewDownloads() {
2310 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2311 mActivity.startActivity(intent);
2312 }
2313
John Reck30b065e2011-07-19 10:58:05 -07002314 int getActionModeHeight() {
2315 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2316 new int[] { android.R.attr.actionBarSize });
2317 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2318 actionBarSizeTypedArray.recycle();
2319 return size;
2320 }
2321
Michael Kolb8233fac2010-10-26 16:08:53 -07002322 // action mode
2323
John Reck9c35b9c2012-05-30 10:08:50 -07002324 @Override
2325 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002326 mUi.onActionModeStarted(mode);
2327 mActionMode = mode;
2328 }
2329
2330 /*
2331 * True if a custom ActionMode (i.e. find or select) is in use.
2332 */
2333 @Override
2334 public boolean isInCustomActionMode() {
2335 return mActionMode != null;
2336 }
2337
2338 /*
2339 * End the current ActionMode.
2340 */
2341 @Override
2342 public void endActionMode() {
2343 if (mActionMode != null) {
2344 mActionMode.finish();
2345 }
2346 }
2347
2348 /*
2349 * Called by find and select when they are finished. Replace title bars
2350 * as necessary.
2351 */
John Reck9c35b9c2012-05-30 10:08:50 -07002352 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002353 public void onActionModeFinished(ActionMode mode) {
2354 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002355 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002356 mActionMode = null;
2357 }
2358
2359 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002360 final Tab tab = getCurrentTab();
2361 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002362 }
2363
2364 // bookmark handling
2365
2366 /**
2367 * add the current page as a bookmark to the given folder id
2368 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002369 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002370 * bookmarked, and if it is, edit that bookmark. If false, and
2371 * the site is already bookmarked, do not attempt to edit the
2372 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002373 */
2374 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002375 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002376 WebView w = getCurrentTopWebView();
2377 if (w == null) {
2378 return null;
2379 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002380 Intent i = new Intent(mActivity,
2381 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002382 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2383 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2384 String touchIconUrl = w.getTouchIconUrl();
2385 if (touchIconUrl != null) {
2386 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2387 WebSettings settings = w.getSettings();
2388 if (settings != null) {
2389 i.putExtra(AddBookmarkPage.USER_AGENT,
2390 settings.getUserAgentString());
2391 }
2392 }
2393 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2394 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2395 getDesiredThumbnailHeight(mActivity)));
2396 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002397 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002398 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2399 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002400 // Put the dialog at the upper right of the screen, covering the
2401 // star on the title bar.
2402 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002403 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002404 }
2405
2406 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002407 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002408 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002409 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002410 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002411 }
2412
Vivek Sekharb54614f2014-05-01 19:03:37 -07002413 @Override
2414 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2415 boolean capture) {
2416 mUploadHandler = new UploadHandler(this);
2417 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2418 }
2419
Michael Kolb8233fac2010-10-26 16:08:53 -07002420 // thumbnails
2421
2422 /**
2423 * Return the desired width for thumbnail screenshots, which are stored in
2424 * the database, and used on the bookmarks screen.
2425 * @param context Context for finding out the density of the screen.
2426 * @return desired width for thumbnail screenshot.
2427 */
2428 static int getDesiredThumbnailWidth(Context context) {
2429 return context.getResources().getDimensionPixelOffset(
2430 R.dimen.bookmarkThumbnailWidth);
2431 }
2432
2433 /**
2434 * Return the desired height for thumbnail screenshots, which are stored in
2435 * the database, and used on the bookmarks screen.
2436 * @param context Context for finding out the density of the screen.
2437 * @return desired height for thumbnail screenshot.
2438 */
2439 static int getDesiredThumbnailHeight(Context context) {
2440 return context.getResources().getDimensionPixelOffset(
2441 R.dimen.bookmarkThumbnailHeight);
2442 }
2443
John Reck8cc92352011-07-06 17:41:52 -07002444 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002445 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002446 return null;
2447 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002448
2449 Bitmap viewportBitmap = view.getViewportBitmap();
2450 if (viewportBitmap == null) {
2451 return null;
2452 }
2453
2454 float aspectRatio = (float) width/height;
2455 int viewportWidth = viewportBitmap.getWidth();
2456 int viewportHeight = viewportBitmap.getHeight();
2457
2458 //modify the size to attain the same aspect ratio of desired thumbnail size
2459 if (viewportHeight > viewportWidth) {
2460 viewportHeight= (int)Math.round(viewportWidth * aspectRatio);
2461 } else {
2462 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2463 }
2464
2465 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2466 Rect dstRect = new Rect(0, 0, width, height);
2467
2468 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2469 || sThumbnailBitmap.getHeight() != height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002470 if (sThumbnailBitmap != null) {
2471 sThumbnailBitmap.recycle();
2472 sThumbnailBitmap = null;
2473 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002474
2475 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002476 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002477
John Reckd7dd9b22011-08-30 09:18:29 -07002478 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002479 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
John Reckd7dd9b22011-08-30 09:18:29 -07002480
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002481 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002482 }
2483
John Reck34ef2672011-02-10 11:30:55 -08002484 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002485 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002486 // FIXME: Would like to make sure there is actually something to
2487 // draw, but the API for that (WebViewCore.pictureReady()) is not
2488 // currently accessible here.
2489
John Reck34ef2672011-02-10 11:30:55 -08002490 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002491 if (view == null) {
2492 // Tab was destroyed
2493 return;
2494 }
John Reck34ef2672011-02-10 11:30:55 -08002495 final String url = tab.getUrl();
2496 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002497 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002498 if (TextUtils.isEmpty(url)) {
2499 return;
2500 }
2501
kaiyiz6e5b3e02013-08-19 20:02:01 +08002502 //update My Navigation Thumbnails
2503 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2504 if (isMyNavigationUrl) {
2505 updateMyNavigationThumbnail(url, view);
2506 }
John Reck34ef2672011-02-10 11:30:55 -08002507 // Only update thumbnails for web urls (http(s)://), not for
2508 // about:, javascript:, data:, etc...
2509 // Unless it is a bookmarked site, then always update
2510 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2511 return;
2512 }
2513
kaiyiz6e5b3e02013-08-19 20:02:01 +08002514 if (url != null && mUpdateMyNavThumbnailUrl != null
2515 && Patterns.WEB_URL.matcher(url).matches()
2516 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2517 String urlHost = (new WebAddress(url)).getHost();
2518 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2519 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2520 || bookmarkHost.length() == 0) {
2521 return;
2522 }
2523 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2524 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2525 bookmarkHost.length());
2526 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2527 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2528 if (!bookmarkDomain.equals(urlDomain)) {
2529 return;
2530 }
2531 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2533 getDesiredThumbnailHeight(mActivity));
2534 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002535 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2536 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2537 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2538 500);
2539 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002540 return;
2541 }
2542
2543 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002544 new AsyncTask<Void, Void, Void>() {
2545 @Override
2546 protected Void doInBackground(Void... unused) {
2547 Cursor cursor = null;
2548 try {
2549 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002550 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2551 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2552 : url);
2553 if (mUpdateMyNavThumbnail) {
2554 mUpdateMyNavThumbnail = false;
2555 mUpdateMyNavThumbnailUrl = null;
2556 }
John Reck34ef2672011-02-10 11:30:55 -08002557 if (cursor != null && cursor.moveToFirst()) {
2558 final ByteArrayOutputStream os =
2559 new ByteArrayOutputStream();
2560 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002561
John Reck34ef2672011-02-10 11:30:55 -08002562 ContentValues values = new ContentValues();
2563 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002564
John Reck34ef2672011-02-10 11:30:55 -08002565 do {
John Reck617fd832011-02-16 14:35:59 -08002566 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002567 cr.update(Images.CONTENT_URI, values, null, null);
2568 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002569 }
John Reck34ef2672011-02-10 11:30:55 -08002570 } catch (IllegalStateException e) {
2571 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002572 } catch (SQLiteException s) {
2573 // Added for possible error when user tries to remove the same bookmark
2574 // that is being updated with a screen shot
2575 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002576 } finally {
2577 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 }
John Reck34ef2672011-02-10 11:30:55 -08002579 return null;
2580 }
2581 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002582 }
2583
2584 private class Copy implements OnMenuItemClickListener {
2585 private CharSequence mText;
2586
John Reck9c35b9c2012-05-30 10:08:50 -07002587 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 public boolean onMenuItemClick(MenuItem item) {
2589 copy(mText);
2590 return true;
2591 }
2592
2593 public Copy(CharSequence toCopy) {
2594 mText = toCopy;
2595 }
2596 }
2597
Leon Scroggins63c02662010-11-18 15:16:27 -05002598 private static class Download implements OnMenuItemClickListener {
2599 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002600 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002601 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002602 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002603 private static final String FALLBACK_EXTENSION = "dat";
2604 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002605
John Reck9c35b9c2012-05-30 10:08:50 -07002606 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002607 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002608 if (DataUri.isDataUri(mText)) {
2609 saveDataUri();
2610 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002611 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002612 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002613 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002614 return true;
2615 }
2616
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002617 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2618 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002619 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002620 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002621 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002622 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002623 }
George Mount387d45d2011-10-07 15:57:53 -07002624
2625 /**
2626 * Treats mText as a data URI and writes its contents to a file
2627 * based on the current time.
2628 */
2629 private void saveDataUri() {
2630 FileOutputStream outputStream = null;
2631 try {
2632 DataUri uri = new DataUri(mText);
2633 File target = getTarget(uri);
2634 outputStream = new FileOutputStream(target);
2635 outputStream.write(uri.getData());
2636 final DownloadManager manager =
2637 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2638 manager.addCompletedDownload(target.getName(),
2639 mActivity.getTitle().toString(), false,
2640 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002641 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002642 } catch (IOException e) {
2643 Log.e(LOGTAG, "Could not save data URL");
2644 } finally {
2645 if (outputStream != null) {
2646 try {
2647 outputStream.close();
2648 } catch (IOException e) {
2649 // ignore close errors
2650 }
2651 }
2652 }
2653 }
2654
2655 /**
2656 * Creates a File based on the current time stamp and uses
2657 * the mime type of the DataUri to get the extension.
2658 */
2659 private File getTarget(DataUri uri) throws IOException {
2660 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002661 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002662 String nameBase = format.format(new Date());
2663 String mimeType = uri.getMimeType();
2664 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2665 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2666 if (extension == null) {
2667 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2668 extension = FALLBACK_EXTENSION;
2669 }
2670 extension = "." + extension; // createTempFile needs the '.'
2671 File targetFile = File.createTempFile(nameBase, extension, dir);
2672 return targetFile;
2673 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002674 }
2675
Cary Clark8974d282010-11-22 10:46:05 -05002676 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002677 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002678
John Reck9c35b9c2012-05-30 10:08:50 -07002679 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002680 public boolean onMenuItemClick(MenuItem item) {
2681 if (mWebView != null) {
2682 return mWebView.selectText();
2683 }
2684 return false;
2685 }
2686
2687 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002688 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002689 }
2690
2691 }
2692
Michael Kolb8233fac2010-10-26 16:08:53 -07002693 /********************** TODO: UI stuff *****************************/
2694
2695 // these methods have been copied, they still need to be cleaned up
2696
2697 /****************** tabs ***************************************************/
2698
2699 // basic tab interactions:
2700
2701 // it is assumed that tabcontrol already knows about the tab
2702 protected void addTab(Tab tab) {
2703 mUi.addTab(tab);
2704 }
2705
2706 protected void removeTab(Tab tab) {
2707 mUi.removeTab(tab);
2708 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002709 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002710 }
2711
Michael Kolbf2055602011-04-09 17:20:03 -07002712 @Override
2713 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002714 // monkey protection against delayed start
2715 if (tab != null) {
2716 mTabControl.setCurrentTab(tab);
2717 // the tab is guaranteed to have a webview after setCurrentTab
2718 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002719 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002720 //Purge active tabs
2721 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002722 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002723 }
2724
John Reck8bcafc12011-08-29 16:43:02 -07002725 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002726 Tab current = mTabControl.getCurrentTab();
2727 if (current != null
2728 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002729 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 }
2731 }
2732
John Reck26b18322011-06-21 13:08:58 -07002733 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002734 // Dismiss the subwindow if applicable.
2735 dismissSubWindow(appTab);
2736 // Since we might kill the WebView, remove it from the
2737 // content view first.
2738 mUi.detachTab(appTab);
2739 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002740 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002741 // TODO: analyze why the remove and add are necessary
2742 mUi.attachTab(appTab);
2743 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002744 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002745 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002746 } else {
2747 // If the tab was the current tab, we have to attach
2748 // it to the view system again.
2749 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002750 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002751 }
2752 }
2753
2754 // Remove the sub window if it exists. Also called by TabControl when the
2755 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002756 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002757 public void dismissSubWindow(Tab tab) {
2758 removeSubWindow(tab);
2759 // dismiss the subwindow. This will destroy the WebView.
2760 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002761 WebView wv = getCurrentTopWebView();
2762 if (wv != null) {
2763 wv.requestFocus();
2764 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002765 }
2766
2767 @Override
2768 public void removeSubWindow(Tab t) {
2769 if (t.getSubWebView() != null) {
2770 mUi.removeSubWindow(t.getSubViewContainer());
2771 }
2772 }
2773
2774 @Override
2775 public void attachSubWindow(Tab tab) {
2776 if (tab.getSubWebView() != null) {
2777 mUi.attachSubWindow(tab.getSubViewContainer());
2778 getCurrentTopWebView().requestFocus();
2779 }
2780 }
2781
Mathew Inwood29721c22011-06-29 17:55:24 +01002782 private Tab showPreloadedTab(final UrlData urlData) {
2783 if (!urlData.isPreloaded()) {
2784 return null;
2785 }
2786 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2787 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2788 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002789 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002790 // Could not submit query. Fallback to regular tab creation
2791 tabControl.destroy();
2792 return null;
2793 }
2794 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002795 // check tab count and make room for new tab
2796 if (!mTabControl.canCreateNewTab()) {
2797 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2798 if (leastUsed != null) {
2799 closeTab(leastUsed);
2800 }
2801 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002802 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002803 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002804 mTabControl.addPreloadedTab(t);
2805 addTab(t);
2806 setActiveTab(t);
2807 return t;
2808 }
2809
Michael Kolb7bcafde2011-05-09 13:55:59 -07002810 // open a non inconito tab with the given url data
2811 // and set as active tab
2812 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002813 Tab tab = showPreloadedTab(urlData);
2814 if (tab == null) {
2815 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002816 if ((tab != null) && !urlData.isEmpty()) {
2817 loadUrlDataIn(tab, urlData);
2818 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002819 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002820 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002821 }
2822
Michael Kolb843510f2010-12-09 10:51:49 -08002823 @Override
2824 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002825 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002826 }
2827
Michael Kolb8233fac2010-10-26 16:08:53 -07002828 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002829 public Tab openIncognitoTab() {
2830 return openTab(INCOGNITO_URI, true, true, false);
2831 }
2832
2833 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002834 public Tab openTab(String url, boolean incognito, boolean setActive,
2835 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002836 return openTab(url, incognito, setActive, useCurrent, null);
2837 }
2838
2839 @Override
2840 public Tab openTab(String url, Tab parent, boolean setActive,
2841 boolean useCurrent) {
2842 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2843 setActive, useCurrent, parent);
2844 }
2845
2846 public Tab openTab(String url, boolean incognito, boolean setActive,
2847 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002848 Tab tab = createNewTab(incognito, setActive, useCurrent);
2849 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002850 if (parent != null && parent != tab) {
2851 parent.addChildTab(tab);
2852 }
Michael Kolb519d2282011-05-09 17:03:19 -07002853 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002854 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002855 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002856 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002857 return tab;
2858 }
2859
2860 // this method will attempt to create a new tab
2861 // incognito: private browsing tab
2862 // setActive: ste tab as current tab
2863 // useCurrent: if no new tab can be created, return current tab
2864 private Tab createNewTab(boolean incognito, boolean setActive,
2865 boolean useCurrent) {
2866 Tab tab = null;
2867 if (mTabControl.canCreateNewTab()) {
2868 tab = mTabControl.createNewTab(incognito);
2869 addTab(tab);
2870 if (setActive) {
2871 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002872 } else {
2873 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002874 }
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}