blob: a5151dd522e20b80010e7187ace560968c2d9506 [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.ContactsContract;
61import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080062import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070063import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070064import android.text.TextUtils;
65import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080066import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070067import android.view.ActionMode;
68import android.view.ContextMenu;
69import android.view.ContextMenu.ContextMenuInfo;
70import android.view.Gravity;
71import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070072import android.view.Menu;
73import android.view.MenuInflater;
74import android.view.MenuItem;
75import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070076import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070077import android.view.View;
George Mount387d45d2011-10-07 15:57:53 -070078import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb8233fac2010-10-26 16:08:53 -070081import android.webkit.WebIconDatabase;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import org.codeaurora.swe.CookieManager;
85import org.codeaurora.swe.CookieSyncManager;
86import org.codeaurora.swe.HttpAuthHandler;
87import org.codeaurora.swe.SslErrorHandler;
88import org.codeaurora.swe.WebSettings;
89import org.codeaurora.swe.WebView;
90
Bijan Amirzada41242f22014-03-21 12:12:18 -070091import com.android.browser.R;
92import com.android.browser.IntentHandler.UrlData;
93import com.android.browser.UI.ComboViews;
94import com.android.browser.mynavigation.AddMyNavigationPage;
95import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070096import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070097import 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 Amirzada3f04dc72014-06-25 11:48:36 -07001124 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 }
1126 @Override
1127 public void onPostExecute(String[] result) {
1128 callback.onReceiveValue(result);
1129 }
1130 };
1131 task.execute();
1132 }
1133
1134 @Override
1135 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1136 final HttpAuthHandler handler, final String host,
1137 final String realm) {
1138 String username = null;
1139 String password = null;
1140
1141 boolean reuseHttpAuthUsernamePassword
1142 = handler.useHttpAuthUsernamePassword();
1143
1144 if (reuseHttpAuthUsernamePassword && view != null) {
1145 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1146 if (credentials != null && credentials.length == 2) {
1147 username = credentials[0];
1148 password = credentials[1];
1149 }
1150 }
1151
1152 if (username != null && password != null) {
1153 handler.proceed(username, password);
1154 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001155 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1157 } else {
1158 handler.cancel();
1159 }
1160 }
1161 }
1162
1163 @Override
1164 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001165 String contentDisposition, String mimetype, String referer,
1166 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001167 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001168 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001169 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001170 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001171 // This Tab was opened for the sole purpose of downloading a
1172 // file. Remove it.
1173 if (tab == mTabControl.getCurrentTab()) {
1174 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001175 if (tab.getDerivedFromIntent())
1176 closeTab(tab);
1177 else
1178 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001179 } else {
1180 // In this case, it is not.
1181 closeTab(tab);
1182 }
1183 }
1184 }
1185
1186 @Override
1187 public Bitmap getDefaultVideoPoster() {
1188 return mUi.getDefaultVideoPoster();
1189 }
1190
1191 @Override
1192 public View getVideoLoadingProgressView() {
1193 return mUi.getVideoLoadingProgressView();
1194 }
1195
1196 @Override
1197 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1198 SslError error) {
1199 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1200 }
1201
1202 // helper method
1203
1204 /*
1205 * Update the favorites icon if the private browsing isn't enabled and the
1206 * icon is valid.
1207 */
1208 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1209 final String url, Bitmap favicon) {
1210 if (favicon == null) {
1211 return;
1212 }
1213 if (!tab.isPrivateBrowsingEnabled()) {
1214 Bookmarks.updateFavicon(mActivity
1215 .getContentResolver(), originalUrl, url, favicon);
1216 }
1217 }
1218
Leon Scroggins4cd97792010-12-03 15:31:56 -05001219 @Override
1220 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001221 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001222 mUi.bookmarkedStatusHasChanged(tab);
1223 }
1224
Michael Kolb8233fac2010-10-26 16:08:53 -07001225 // end WebViewController
1226
1227 protected void pageUp() {
1228 getCurrentTopWebView().pageUp(false);
1229 }
1230
1231 protected void pageDown() {
1232 getCurrentTopWebView().pageDown(false);
1233 }
1234
1235 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001236 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001237 public void editUrl() {
1238 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001239 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 }
1241
John Reck9c35b9c2012-05-30 10:08:50 -07001242 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001243 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001244 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001245 if (tab.inForeground()) {
1246 if (mUi.isCustomViewShowing()) {
1247 callback.onCustomViewHidden();
1248 return;
1249 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001250 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 // Save the menu state and set it to empty while the custom
1252 // view is showing.
1253 mOldMenuState = mMenuState;
1254 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001255 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001256 }
1257 }
1258
1259 @Override
1260 public void hideCustomView() {
1261 if (mUi.isCustomViewShowing()) {
1262 mUi.onHideCustomView();
1263 // Reset the old menu state.
1264 mMenuState = mOldMenuState;
1265 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001266 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001267 }
1268 }
1269
John Reck9c35b9c2012-05-30 10:08:50 -07001270 @Override
1271 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 Intent intent) {
1273 if (getCurrentTopWebView() == null) return;
1274 switch (requestCode) {
1275 case PREFERENCES_PAGE:
1276 if (resultCode == Activity.RESULT_OK && intent != null) {
1277 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001278 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 mTabControl.removeParentChildRelationShips();
1280 }
1281 }
1282 break;
1283 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001284 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001285 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001286 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001287 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001288 case AUTOFILL_SETUP:
1289 // Determine whether a profile was actually set up or not
1290 // and if so, send the message back to the WebTextView to
1291 // fill the form with the new profile.
1292 if (getSettings().getAutoFillProfile() != null) {
1293 mAutoFillSetupMessage.sendToTarget();
1294 mAutoFillSetupMessage = null;
1295 }
1296 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001297 case COMBO_VIEW:
1298 if (intent == null || resultCode != Activity.RESULT_OK) {
1299 break;
1300 }
John Reck3ba45532011-08-11 16:26:53 -07001301 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001302 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1303 Tab t = getCurrentTab();
1304 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001305 mUpdateMyNavThumbnail = true;
1306 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001307 loadUrl(t, uri.toString());
1308 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1309 String[] urls = intent.getStringArrayExtra(
1310 ComboViewActivity.EXTRA_OPEN_ALL);
1311 Tab parent = getCurrentTab();
1312 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001313 if (url != null) {
1314 parent = openTab(url, parent,
1315 !mSettings.openInBackground(), true);
1316 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001317 }
1318 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1319 long id = intent.getLongExtra(
1320 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1321 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001322 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001323 }
1324 }
1325 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001326 case VOICE_RESULT:
1327 if (resultCode == Activity.RESULT_OK && intent != null) {
1328 ArrayList<String> results = intent.getStringArrayListExtra(
1329 RecognizerIntent.EXTRA_RESULTS);
1330 if (results.size() >= 1) {
1331 mVoiceResult = results.get(0);
1332 }
1333 }
1334 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001335 case MY_NAVIGATION:
1336 if (intent == null || resultCode != Activity.RESULT_OK) {
1337 break;
1338 }
1339
1340 if (intent.getBooleanExtra("need_refresh", false) &&
1341 getCurrentTopWebView() != null) {
1342 getCurrentTopWebView().reload();
1343 }
1344 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001345 default:
1346 break;
1347 }
1348 getCurrentTopWebView().requestFocus();
1349 }
1350
1351 /**
1352 * Open the Go page.
1353 * @param startWithHistory If true, open starting on the history tab.
1354 * Otherwise, start with the bookmarks tab.
1355 */
1356 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001357 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 if (mTabControl.getCurrentWebView() == null) {
1359 return;
1360 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001361 // clear action mode
1362 if (isInCustomActionMode()) {
1363 endActionMode();
1364 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001365 Bundle extras = new Bundle();
1366 // Disable opening in a new window if we have maxed out the windows
1367 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1368 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001369 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001370 }
1371
1372 // combo view callbacks
1373
Michael Kolb8233fac2010-10-26 16:08:53 -07001374 // key handling
1375 protected void onBackKey() {
1376 if (!mUi.onBackKey()) {
1377 WebView subwindow = mTabControl.getCurrentSubWindow();
1378 if (subwindow != null) {
1379 if (subwindow.canGoBack()) {
1380 subwindow.goBack();
1381 } else {
1382 dismissSubWindow(mTabControl.getCurrentTab());
1383 }
1384 } else {
1385 goBackOnePageOrQuit();
1386 }
1387 }
1388 }
1389
Michael Kolb4bd767d2011-05-27 11:33:55 -07001390 protected boolean onMenuKey() {
1391 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001392 }
1393
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 // menu handling and state
1395 // TODO: maybe put into separate handler
1396
John Reck9c35b9c2012-05-30 10:08:50 -07001397 @Override
1398 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001399 if (mMenuState == EMPTY_MENU) {
1400 return false;
1401 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001402 MenuInflater inflater = mActivity.getMenuInflater();
1403 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001404 return true;
1405 }
1406
John Reck9c35b9c2012-05-30 10:08:50 -07001407 @Override
1408 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001410 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001411 return;
1412 }
1413 if (!(v instanceof WebView)) {
1414 return;
1415 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001416 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 WebView.HitTestResult result = webview.getHitTestResult();
1418 if (result == null) {
1419 return;
1420 }
1421
1422 int type = result.getType();
1423 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1424 Log.w(LOGTAG,
1425 "We should not show context menu when nothing is touched");
1426 return;
1427 }
1428 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1429 // let TextView handles context menu
1430 return;
1431 }
1432
1433 // Note, http://b/issue?id=1106666 is requesting that
1434 // an inflated menu can be used again. This is not available
1435 // yet, so inflate each time (yuk!)
1436 MenuInflater inflater = mActivity.getMenuInflater();
1437 inflater.inflate(R.menu.browsercontext, menu);
1438
1439 // Show the correct menu group
1440 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001441 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001442 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 menu.setGroupVisible(R.id.PHONE_MENU,
1444 type == WebView.HitTestResult.PHONE_TYPE);
1445 menu.setGroupVisible(R.id.EMAIL_MENU,
1446 type == WebView.HitTestResult.EMAIL_TYPE);
1447 menu.setGroupVisible(R.id.GEO_MENU,
1448 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001449 String itemUrl = null;
1450 String url = webview.getOriginalUrl();
1451 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1452 itemUrl = Uri.decode(navigationUrl);
1453 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1454 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1455 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1456 } else {
1457 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1458 }
1459 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1460 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1461 } else {
1462 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1463
1464 menu.setGroupVisible(R.id.IMAGE_MENU,
1465 type == WebView.HitTestResult.IMAGE_TYPE
1466 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1467 menu.setGroupVisible(R.id.ANCHOR_MENU,
1468 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1469 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001470 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1471 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001472 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001473 // Setup custom handling depending on the type
1474 switch (type) {
1475 case WebView.HitTestResult.PHONE_TYPE:
1476 menu.setHeaderTitle(Uri.decode(extra));
1477 menu.findItem(R.id.dial_context_menu_id).setIntent(
1478 new Intent(Intent.ACTION_VIEW, Uri
1479 .parse(WebView.SCHEME_TEL + extra)));
1480 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1481 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1482 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1483 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1484 addIntent);
1485 menu.findItem(R.id.copy_phone_context_menu_id)
1486 .setOnMenuItemClickListener(
1487 new Copy(extra));
1488 break;
1489
1490 case WebView.HitTestResult.EMAIL_TYPE:
1491 menu.setHeaderTitle(extra);
1492 menu.findItem(R.id.email_context_menu_id).setIntent(
1493 new Intent(Intent.ACTION_VIEW, Uri
1494 .parse(WebView.SCHEME_MAILTO + extra)));
1495 menu.findItem(R.id.copy_mail_context_menu_id)
1496 .setOnMenuItemClickListener(
1497 new Copy(extra));
1498 break;
1499
1500 case WebView.HitTestResult.GEO_TYPE:
1501 menu.setHeaderTitle(extra);
1502 menu.findItem(R.id.map_context_menu_id).setIntent(
1503 new Intent(Intent.ACTION_VIEW, Uri
1504 .parse(WebView.SCHEME_GEO
1505 + URLEncoder.encode(extra))));
1506 menu.findItem(R.id.copy_geo_context_menu_id)
1507 .setOnMenuItemClickListener(
1508 new Copy(extra));
1509 break;
1510
1511 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1512 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001513 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 // decide whether to show the open link in new tab option
1515 boolean showNewTab = mTabControl.canCreateNewTab();
1516 MenuItem newTabItem
1517 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001518 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001519 ? R.string.contextmenu_openlink_newwindow_background
1520 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001521 newTabItem.setVisible(showNewTab);
1522 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001523 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1524 newTabItem.setOnMenuItemClickListener(
1525 new MenuItem.OnMenuItemClickListener() {
1526 @Override
1527 public boolean onMenuItemClick(MenuItem item) {
1528 final HashMap<String, WebView> hrefMap =
1529 new HashMap<String, WebView>();
1530 hrefMap.put("webview", webview);
1531 final Message msg = mHandler.obtainMessage(
1532 FOCUS_NODE_HREF,
1533 R.id.open_newtab_context_menu_id,
1534 0, hrefMap);
1535 webview.requestFocusNodeHref(msg);
1536 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001537 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001538 });
1539 } else {
1540 newTabItem.setOnMenuItemClickListener(
1541 new MenuItem.OnMenuItemClickListener() {
1542 @Override
1543 public boolean onMenuItemClick(MenuItem item) {
1544 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001545 openTab(extra, parent,
1546 !mSettings.openInBackground(),
1547 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001548 return true;
1549 }
1550 });
1551 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001552 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001553 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1554 menu.setHeaderTitle(navigationUrl);
1555 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1556
1557 if (itemUrl != null) {
1558 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1559 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1560 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1561 @Override
1562 public boolean onMenuItemClick(MenuItem item) {
1563 final Intent intent = new Intent(Controller.this
1564 .getContext(),
1565 AddMyNavigationPage.class);
1566 Bundle bundle = new Bundle();
1567 String url = Uri.decode(navigationUrl);
1568 bundle.putBoolean("isAdding", false);
1569 bundle.putString("url", url);
1570 bundle.putString("name", getNameFromUrl(url));
1571 intent.putExtra("websites", bundle);
1572 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1573 return false;
1574 }
1575 });
1576 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1577 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1578 @Override
1579 public boolean onMenuItemClick(MenuItem item) {
1580 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1581 return false;
1582 }
1583 });
1584 }
1585 } else {
1586 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1587 }
1588 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1590 break;
1591 }
1592 // otherwise fall through to handle image part
1593 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001594 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1595 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001596 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1597 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001598 shareItem.setOnMenuItemClickListener(
1599 new MenuItem.OnMenuItemClickListener() {
1600 @Override
1601 public boolean onMenuItemClick(MenuItem item) {
1602 sharePage(mActivity, null, extra, null,
1603 null);
1604 return true;
1605 }
1606 }
1607 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001609 menu.findItem(R.id.view_image_context_menu_id)
1610 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1611 @Override
1612 public boolean onMenuItemClick(MenuItem item) {
1613 openTab(extra, mTabControl.getCurrentTab(), true, true);
1614 return false;
1615 }
1616 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001617 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1618 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1619 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001620 menu.findItem(R.id.set_wallpaper_context_menu_id).
1621 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1622 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 break;
1624
1625 default:
1626 Log.w(LOGTAG, "We should not get here.");
1627 break;
1628 }
1629 //update the ui
1630 mUi.onContextMenuCreated(menu);
1631 }
1632
kaiyiz6e5b3e02013-08-19 20:02:01 +08001633 public void startAddMyNavigation(String url) {
1634 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1635 Bundle bundle = new Bundle();
1636 bundle.putBoolean("isAdding", true);
1637 bundle.putString("url", url);
1638 bundle.putString("name", getNameFromUrl(url));
1639 intent.putExtra("websites", bundle);
1640 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1641 }
1642
1643 private void showMyNavigationDeleteDialog(final String itemUrl) {
1644 new AlertDialog.Builder(this.getContext())
1645 .setTitle(R.string.my_navigation_delete_label)
1646 .setIcon(android.R.drawable.ic_dialog_alert)
1647 .setMessage(R.string.my_navigation_delete_msg)
1648 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1649 @Override
1650 public void onClick(DialogInterface dialog, int whichButton) {
1651 deleteMyNavigationItem(itemUrl);
1652 }
1653 })
1654 .setNegativeButton(R.string.cancel, null)
1655 .show();
1656 }
1657
1658 private void deleteMyNavigationItem(final String itemUrl) {
1659 ContentResolver cr = this.getContext().getContentResolver();
1660 Cursor cursor = null;
1661
1662 try {
1663 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1664 new String[] {
1665 MyNavigationUtil.ID
1666 }, "url = ?", new String[] {
1667 itemUrl
1668 }, null);
1669 if (null != cursor && cursor.moveToFirst()) {
1670 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1671 cursor.getLong(0));
1672
1673 ContentValues values = new ContentValues();
1674 values.put(MyNavigationUtil.TITLE, "");
1675 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1676 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1677 ByteArrayOutputStream os = new ByteArrayOutputStream();
1678 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1679 R.raw.my_navigation_add);
1680 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1681 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1682 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1683 cr.update(uri, values, null, null);
1684 } else {
1685 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1686 }
1687 } catch (IllegalStateException e) {
1688 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1689 } finally {
1690 if (null != cursor) {
1691 cursor.close();
1692 }
1693 }
1694
1695 if (getCurrentTopWebView() != null) {
1696 getCurrentTopWebView().reload();
1697 }
1698 }
1699
1700 private String getNameFromUrl(String itemUrl) {
1701 ContentResolver cr = this.getContext().getContentResolver();
1702 Cursor cursor = null;
1703 String name = null;
1704
1705 try {
1706 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1707 new String[] {
1708 MyNavigationUtil.TITLE
1709 }, "url = ?", new String[] {
1710 itemUrl
1711 }, null);
1712 if (null != cursor && cursor.moveToFirst()) {
1713 name = cursor.getString(0);
1714 } else {
1715 Log.e(LOGTAG, "this item does not exist!");
1716 }
1717 } catch (IllegalStateException e) {
1718 Log.e(LOGTAG, "getNameFromUrl", e);
1719 } finally {
1720 if (null != cursor) {
1721 cursor.close();
1722 }
1723 }
1724 return name;
1725 }
1726
1727 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1728 int width = mActivity.getResources().getDimensionPixelOffset(
1729 R.dimen.myNavigationThumbnailWidth);
1730 int height = mActivity.getResources().getDimensionPixelOffset(
1731 R.dimen.myNavigationThumbnailHeight);
1732
1733 final Bitmap bm = createScreenshot(webView, width, height);
1734
1735 if (bm == null) {
1736 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1737 return;
1738 }
1739
1740 final ContentResolver cr = mActivity.getContentResolver();
1741 new AsyncTask<Void, Void, Void>() {
1742 @Override
1743 protected Void doInBackground(Void... unused) {
1744 ContentResolver cr = mActivity.getContentResolver();
1745 Cursor cursor = null;
1746 try {
1747 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1748 new String[] {
1749 MyNavigationUtil.ID
1750 }, "url = ?", new String[] {
1751 itemUrl
1752 }, null);
1753 if (null != cursor && cursor.moveToFirst()) {
1754 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1755 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1756
1757 ContentValues values = new ContentValues();
1758 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1759 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1760 cursor.getLong(0));
1761 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1762 cr.update(uri, values, null, null);
1763 os.close();
1764 }
1765 } catch (IllegalStateException e) {
1766 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1767 } catch (IOException e) {
1768 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1769 } finally {
1770 if (null != cursor) {
1771 cursor.close();
1772 }
1773 }
1774 return null;
1775 }
1776 }.execute();
1777 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001778 /**
1779 * As the menu can be open when loading state changes
1780 * we must manually update the state of the stop/reload menu
1781 * item
1782 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001783 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001784 if (menu == null) {
1785 return;
1786 }
1787 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001788 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001789 menu.findItem(R.id.stop_menu_id):
1790 menu.findItem(R.id.reload_menu_id);
1791 if (src != null) {
1792 dest.setIcon(src.getIcon());
1793 dest.setTitle(src.getTitle());
1794 }
1795 }
1796
John Reck9c35b9c2012-05-30 10:08:50 -07001797 @Override
1798 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001799 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001800 // hold on to the menu reference here; it is used by the page callbacks
1801 // to update the menu based on loading state
1802 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001803 // Note: setVisible will decide whether an item is visible; while
1804 // setEnabled() will decide whether an item is enabled, which also means
1805 // whether the matching shortcut key will function.
1806 switch (mMenuState) {
1807 case EMPTY_MENU:
1808 if (mCurrentMenuState != mMenuState) {
1809 menu.setGroupVisible(R.id.MAIN_MENU, false);
1810 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1811 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1812 }
1813 break;
1814 default:
1815 if (mCurrentMenuState != mMenuState) {
1816 menu.setGroupVisible(R.id.MAIN_MENU, true);
1817 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1818 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1819 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001820 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001821 break;
1822 }
1823 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001824 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001825 }
1826
Michael Kolb4bf79712011-07-14 14:18:12 -07001827 @Override
1828 public void updateMenuState(Tab tab, Menu menu) {
1829 boolean canGoBack = false;
1830 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001831 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001832 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001833 if (tab != null) {
1834 canGoBack = tab.canGoBack();
1835 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001836 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001837 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001838 }
1839 final MenuItem back = menu.findItem(R.id.back_menu_id);
1840 back.setEnabled(canGoBack);
1841
1842 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001843
1844 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1845 forward.setEnabled(canGoForward);
1846
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001847 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1848 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001849 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001850 if (source != null && dest != null) {
1851 dest.setTitle(source.getTitle());
1852 dest.setIcon(source.getIcon());
1853 }
John Recke1a03a32011-09-14 17:04:16 -07001854 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001855
1856 // decide whether to show the share link option
1857 PackageManager pm = mActivity.getPackageManager();
1858 Intent send = new Intent(Intent.ACTION_SEND);
1859 send.setType("text/plain");
1860 ResolveInfo ri = pm.resolveActivity(send,
1861 PackageManager.MATCH_DEFAULT_ONLY);
1862 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1863
1864 boolean isNavDump = mSettings.enableNavDump();
1865 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1866 nav.setVisible(isNavDump);
1867 nav.setEnabled(isNavDump);
1868
1869 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001870 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1871 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001872 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1873 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001874 // history and snapshots item are the members of COMBO menu group,
1875 // so if show history item, only make snapshots item invisible.
1876 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001877
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001878 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001879 }
1880
John Reck9c35b9c2012-05-30 10:08:50 -07001881 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001882 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001883 if (null == getCurrentTopWebView()) {
1884 return false;
1885 }
1886 if (mMenuIsDown) {
1887 // The shortcut action consumes the MENU. Even if it is still down,
1888 // it won't trigger the next shortcut action. In the case of the
1889 // shortcut action triggering a new activity, like Bookmarks, we
1890 // won't get onKeyUp for MENU. So it is important to reset it here.
1891 mMenuIsDown = false;
1892 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001893 if (mUi.onOptionsItemSelected(item)) {
1894 // ui callback handled it
1895 return true;
1896 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 switch (item.getItemId()) {
1898 // -- Main menu
1899 case R.id.new_tab_menu_id:
1900 openTabToHomePage();
1901 break;
1902
1903 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001904 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001905 break;
1906
Afzal Najamd4e33312012-04-26 01:54:01 -04001907 case R.id.close_other_tabs_id:
1908 closeOtherTabs();
1909 break;
1910
Michael Kolb8233fac2010-10-26 16:08:53 -07001911 case R.id.goto_menu_id:
1912 editUrl();
1913 break;
1914
1915 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001916 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1917 break;
1918
1919 case R.id.history_menu_id:
1920 bookmarksOrHistoryPicker(ComboViews.History);
1921 break;
1922
1923 case R.id.snapshots_menu_id:
1924 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001925 break;
1926
Michael Kolb8233fac2010-10-26 16:08:53 -07001927 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001928 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001929 break;
1930
1931 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001932 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 stopLoading();
1934 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001935 Tab currentTab = mTabControl.getCurrentTab();
1936 if (currentTab.hasCrashed) {
1937 currentTab.replaceCrashView(getCurrentTopWebView(),
1938 currentTab.getViewContainer());
1939 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001940 getCurrentTopWebView().reload();
1941 }
1942 break;
1943
1944 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001945 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001946 break;
1947
1948 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001949 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001950 break;
1951
1952 case R.id.close_menu_id:
1953 // Close the subwindow if it exists.
1954 if (mTabControl.getCurrentSubWindow() != null) {
1955 dismissSubWindow(mTabControl.getCurrentTab());
1956 break;
1957 }
1958 closeCurrentTab();
1959 break;
1960
kaiyiza8b6dbb2013-07-29 18:11:22 +08001961 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001962 Object[] params = { new String("persist.debug.browsermonkeytest")};
1963 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001964 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001965 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001966 if (ret != null && ret.equals("enable"))
1967 break;
luxiaolb40014b2013-07-19 10:01:43 +08001968 showExitDialog(mActivity);
1969 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001970 case R.id.homepage_menu_id:
1971 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001972 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001973 break;
1974
1975 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001976 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001977 break;
1978
1979 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001980 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001981 break;
1982
John Reck2bc80422011-06-30 15:11:49 -07001983 case R.id.save_snapshot_menu_id:
1984 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001985 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001986 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001987 break;
1988
Michael Kolb8233fac2010-10-26 16:08:53 -07001989 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001990 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001991 break;
1992
John Recke1a03a32011-09-14 17:04:16 -07001993 case R.id.snapshot_go_live:
1994 goLive();
1995 return true;
1996
Michael Kolb8233fac2010-10-26 16:08:53 -07001997 case R.id.share_page_menu_id:
1998 Tab currentTab = mTabControl.getCurrentTab();
1999 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002000 return false;
2001 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002002 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002003 break;
2004
2005 case R.id.dump_nav_menu_id:
2006 getCurrentTopWebView().debugDump();
2007 break;
2008
Michael Kolb8233fac2010-10-26 16:08:53 -07002009 case R.id.zoom_in_menu_id:
2010 getCurrentTopWebView().zoomIn();
2011 break;
2012
2013 case R.id.zoom_out_menu_id:
2014 getCurrentTopWebView().zoomOut();
2015 break;
2016
2017 case R.id.view_downloads_menu_id:
2018 viewDownloads();
2019 break;
2020
John Reck42229bc2011-08-19 13:26:43 -07002021 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002022 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002023 break;
2024
Michael Kolb8233fac2010-10-26 16:08:53 -07002025 case R.id.window_one_menu_id:
2026 case R.id.window_two_menu_id:
2027 case R.id.window_three_menu_id:
2028 case R.id.window_four_menu_id:
2029 case R.id.window_five_menu_id:
2030 case R.id.window_six_menu_id:
2031 case R.id.window_seven_menu_id:
2032 case R.id.window_eight_menu_id:
2033 {
2034 int menuid = item.getItemId();
2035 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2036 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2037 Tab desiredTab = mTabControl.getTab(id);
2038 if (desiredTab != null &&
2039 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002040 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002041 }
2042 break;
2043 }
2044 }
2045 }
2046 break;
2047
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002048 case R.id.about_menu_id:
2049 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2050 builder.setTitle(R.string.about);
2051 builder.setCancelable(true);
2052 String ua = "";
2053 final WebView currentWebView = getCurrentWebView();
2054 if (currentWebView != null) {
2055 final WebSettings s = currentWebView.getSettings();
2056 if (s != null) {
2057 ua = s.getUserAgentString();
2058 }
2059 }
2060 builder.setMessage("Agent:" + ua);
2061 builder.setPositiveButton(android.R.string.ok, null);
2062 builder.create().show();
2063 break;
2064
Michael Kolb8233fac2010-10-26 16:08:53 -07002065 default:
2066 return false;
2067 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002068 return true;
2069 }
2070
John Reck68234a92012-04-19 15:27:12 -07002071 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2072 implements OnCancelListener {
2073
2074 private Tab mTab;
2075 private Dialog mProgressDialog;
2076 private ContentValues mValues;
2077
2078 private SaveSnapshotTask(Tab tab) {
2079 mTab = tab;
2080 }
2081
2082 @Override
2083 protected void onPreExecute() {
2084 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2085 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2086 true, true, this);
2087 mValues = mTab.createSnapshotValues();
2088 }
2089
2090 @Override
2091 protected Long doInBackground(Void... params) {
2092 if (!mTab.saveViewState(mValues)) {
2093 return null;
2094 }
2095 if (isCancelled()) {
2096 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2097 File file = mActivity.getFileStreamPath(path);
2098 if (!file.delete()) {
2099 file.deleteOnExit();
2100 }
2101 return null;
2102 }
2103 final ContentResolver cr = mActivity.getContentResolver();
2104 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2105 if (result == null) {
2106 return null;
2107 }
2108 long id = ContentUris.parseId(result);
2109 return id;
2110 }
2111
2112 @Override
2113 protected void onPostExecute(Long id) {
2114 if (isCancelled()) {
2115 return;
2116 }
2117 mProgressDialog.dismiss();
2118 if (id == null) {
2119 Toast.makeText(mActivity, R.string.snapshot_failed,
2120 Toast.LENGTH_SHORT).show();
2121 return;
2122 }
2123 Bundle b = new Bundle();
2124 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2125 mUi.showComboView(ComboViews.Snapshots, b);
2126 }
2127
2128 @Override
2129 public void onCancel(DialogInterface dialog) {
2130 cancel(true);
2131 }
2132 }
2133
Michael Kolb80f75082012-04-10 10:50:06 -07002134 @Override
2135 public void toggleUserAgent() {
2136 WebView web = getCurrentWebView();
2137 mSettings.toggleDesktopUseragent(web);
2138 web.loadUrl(web.getOriginalUrl());
2139 }
2140
2141 @Override
2142 public void findOnPage() {
2143 getCurrentTopWebView().showFindDialog(null, true);
2144 }
2145
2146 @Override
2147 public void openPreferences() {
2148 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2149 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2150 getCurrentTopWebView().getUrl());
2151 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2152 }
2153
2154 @Override
2155 public void bookmarkCurrentPage() {
2156 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2157 if (bookmarkIntent != null) {
2158 mActivity.startActivity(bookmarkIntent);
2159 }
2160 }
2161
John Recke1a03a32011-09-14 17:04:16 -07002162 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002163 SnapshotTab t = (SnapshotTab) getCurrentTab();
2164 t.loadUrl(t.getLiveUrl(), null);
John Recke1a03a32011-09-14 17:04:16 -07002165 }
2166
luxiaolb40014b2013-07-19 10:01:43 +08002167 private void showExitDialog(final Activity activity) {
2168 new AlertDialog.Builder(activity)
2169 .setTitle(R.string.exit_browser_title)
2170 .setIcon(android.R.drawable.ic_dialog_alert)
2171 .setMessage(R.string.exit_browser_msg)
2172 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2173 public void onClick(DialogInterface dialog, int which) {
2174 activity.moveTaskToBack(true);
2175 dialog.dismiss();
2176 }
2177 })
2178 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2179 public void onClick(DialogInterface dialog, int which) {
2180 activity.finish();
2181 mHandler.postDelayed(new Runnable() {
2182 @Override
2183 public void run() {
2184 // TODO Auto-generated method stub
2185 mCrashRecoveryHandler.clearState(true);
2186 int pid = android.os.Process.myPid();
2187 android.os.Process.killProcess(pid);
2188 }
2189 }, 300);
2190 dialog.dismiss();
2191 }
2192 })
2193 .show();
2194 }
Michael Kolb315d5022011-10-13 12:47:11 -07002195 @Override
2196 public void showPageInfo() {
2197 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2198 }
2199
John Reck9c35b9c2012-05-30 10:08:50 -07002200 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002201 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002202 // Let the History and Bookmark fragments handle menus they created.
2203 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2204 return false;
2205 }
2206
Michael Kolb8233fac2010-10-26 16:08:53 -07002207 int id = item.getItemId();
2208 boolean result = true;
2209 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002210 // -- Browser context menu
2211 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002212 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002213 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002214 case R.id.copy_link_context_menu_id:
2215 final WebView webView = getCurrentTopWebView();
2216 if (null == webView) {
2217 result = false;
2218 break;
2219 }
2220 final HashMap<String, WebView> hrefMap =
2221 new HashMap<String, WebView>();
2222 hrefMap.put("webview", webView);
2223 final Message msg = mHandler.obtainMessage(
2224 FOCUS_NODE_HREF, id, 0, hrefMap);
2225 webView.requestFocusNodeHref(msg);
2226 break;
2227
2228 default:
2229 // For other context menus
2230 result = onOptionsItemSelected(item);
2231 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002232 return result;
2233 }
2234
2235 /**
2236 * support programmatically opening the context menu
2237 */
2238 public void openContextMenu(View view) {
2239 mActivity.openContextMenu(view);
2240 }
2241
2242 /**
2243 * programmatically open the options menu
2244 */
2245 public void openOptionsMenu() {
2246 mActivity.openOptionsMenu();
2247 }
2248
John Reck9c35b9c2012-05-30 10:08:50 -07002249 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002250 public boolean onMenuOpened(int featureId, Menu menu) {
2251 if (mOptionsMenuOpen) {
2252 if (mConfigChanged) {
2253 // We do not need to make any changes to the state of the
2254 // title bar, since the only thing that happened was a
2255 // change in orientation
2256 mConfigChanged = false;
2257 } else {
2258 if (!mExtendedMenuOpen) {
2259 mExtendedMenuOpen = true;
2260 mUi.onExtendedMenuOpened();
2261 } else {
2262 // Switching the menu back to icon view, so show the
2263 // title bar once again.
2264 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002265 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002266 }
2267 }
2268 } else {
2269 // The options menu is closed, so open it, and show the title
2270 mOptionsMenuOpen = true;
2271 mConfigChanged = false;
2272 mExtendedMenuOpen = false;
2273 mUi.onOptionsMenuOpened();
2274 }
2275 return true;
2276 }
2277
John Reck9c35b9c2012-05-30 10:08:50 -07002278 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002279 public void onOptionsMenuClosed(Menu menu) {
2280 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002281 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002282 }
2283
John Reck9c35b9c2012-05-30 10:08:50 -07002284 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002285 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002286 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002287 }
2288
2289 // Helper method for getting the top window.
2290 @Override
2291 public WebView getCurrentTopWebView() {
2292 return mTabControl.getCurrentTopWebView();
2293 }
2294
2295 @Override
2296 public WebView getCurrentWebView() {
2297 return mTabControl.getCurrentWebView();
2298 }
2299
2300 /*
2301 * This method is called as a result of the user selecting the options
2302 * menu to see the download window. It shows the download window on top of
2303 * the current window.
2304 */
2305 void viewDownloads() {
2306 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2307 mActivity.startActivity(intent);
2308 }
2309
John Reck30b065e2011-07-19 10:58:05 -07002310 int getActionModeHeight() {
2311 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2312 new int[] { android.R.attr.actionBarSize });
2313 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2314 actionBarSizeTypedArray.recycle();
2315 return size;
2316 }
2317
Michael Kolb8233fac2010-10-26 16:08:53 -07002318 // action mode
2319
John Reck9c35b9c2012-05-30 10:08:50 -07002320 @Override
2321 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002322 mUi.onActionModeStarted(mode);
2323 mActionMode = mode;
2324 }
2325
2326 /*
2327 * True if a custom ActionMode (i.e. find or select) is in use.
2328 */
2329 @Override
2330 public boolean isInCustomActionMode() {
2331 return mActionMode != null;
2332 }
2333
2334 /*
2335 * End the current ActionMode.
2336 */
2337 @Override
2338 public void endActionMode() {
2339 if (mActionMode != null) {
2340 mActionMode.finish();
2341 }
2342 }
2343
2344 /*
2345 * Called by find and select when they are finished. Replace title bars
2346 * as necessary.
2347 */
John Reck9c35b9c2012-05-30 10:08:50 -07002348 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002349 public void onActionModeFinished(ActionMode mode) {
2350 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002351 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002352 mActionMode = null;
2353 }
2354
2355 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002356 final Tab tab = getCurrentTab();
2357 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002358 }
2359
2360 // bookmark handling
2361
2362 /**
2363 * add the current page as a bookmark to the given folder id
2364 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002365 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002366 * bookmarked, and if it is, edit that bookmark. If false, and
2367 * the site is already bookmarked, do not attempt to edit the
2368 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002369 */
2370 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002371 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002372 WebView w = getCurrentTopWebView();
2373 if (w == null) {
2374 return null;
2375 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002376 Intent i = new Intent(mActivity,
2377 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002378 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2379 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2380 String touchIconUrl = w.getTouchIconUrl();
2381 if (touchIconUrl != null) {
2382 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2383 WebSettings settings = w.getSettings();
2384 if (settings != null) {
2385 i.putExtra(AddBookmarkPage.USER_AGENT,
2386 settings.getUserAgentString());
2387 }
2388 }
2389 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2390 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2391 getDesiredThumbnailHeight(mActivity)));
2392 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002393 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002394 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2395 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002396 // Put the dialog at the upper right of the screen, covering the
2397 // star on the title bar.
2398 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002399 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002400 }
2401
2402 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002403 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002404 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002405 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002406 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002407 }
2408
Vivek Sekharb54614f2014-05-01 19:03:37 -07002409 @Override
2410 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2411 boolean capture) {
2412 mUploadHandler = new UploadHandler(this);
2413 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2414 }
2415
Michael Kolb8233fac2010-10-26 16:08:53 -07002416 // thumbnails
2417
2418 /**
2419 * Return the desired width for thumbnail screenshots, which are stored in
2420 * the database, and used on the bookmarks screen.
2421 * @param context Context for finding out the density of the screen.
2422 * @return desired width for thumbnail screenshot.
2423 */
2424 static int getDesiredThumbnailWidth(Context context) {
2425 return context.getResources().getDimensionPixelOffset(
2426 R.dimen.bookmarkThumbnailWidth);
2427 }
2428
2429 /**
2430 * Return the desired height for thumbnail screenshots, which are stored in
2431 * the database, and used on the bookmarks screen.
2432 * @param context Context for finding out the density of the screen.
2433 * @return desired height for thumbnail screenshot.
2434 */
2435 static int getDesiredThumbnailHeight(Context context) {
2436 return context.getResources().getDimensionPixelOffset(
2437 R.dimen.bookmarkThumbnailHeight);
2438 }
2439
John Reck8cc92352011-07-06 17:41:52 -07002440 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002441 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002442 return null;
2443 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002444
2445 Bitmap viewportBitmap = view.getViewportBitmap();
2446 if (viewportBitmap == null) {
2447 return null;
2448 }
2449
2450 float aspectRatio = (float) width/height;
2451 int viewportWidth = viewportBitmap.getWidth();
2452 int viewportHeight = viewportBitmap.getHeight();
2453
2454 //modify the size to attain the same aspect ratio of desired thumbnail size
2455 if (viewportHeight > viewportWidth) {
2456 viewportHeight= (int)Math.round(viewportWidth * aspectRatio);
2457 } else {
2458 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2459 }
2460
2461 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2462 Rect dstRect = new Rect(0, 0, width, height);
2463
2464 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2465 || sThumbnailBitmap.getHeight() != height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002466 if (sThumbnailBitmap != null) {
2467 sThumbnailBitmap.recycle();
2468 sThumbnailBitmap = null;
2469 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002470
2471 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002472 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002473
John Reckd7dd9b22011-08-30 09:18:29 -07002474 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002475 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
John Reckd7dd9b22011-08-30 09:18:29 -07002476
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002477 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002478 }
2479
John Reck34ef2672011-02-10 11:30:55 -08002480 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002481 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002482 // FIXME: Would like to make sure there is actually something to
2483 // draw, but the API for that (WebViewCore.pictureReady()) is not
2484 // currently accessible here.
2485
John Reck34ef2672011-02-10 11:30:55 -08002486 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002487 if (view == null) {
2488 // Tab was destroyed
2489 return;
2490 }
John Reck34ef2672011-02-10 11:30:55 -08002491 final String url = tab.getUrl();
2492 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002493 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002494 if (TextUtils.isEmpty(url)) {
2495 return;
2496 }
2497
kaiyiz6e5b3e02013-08-19 20:02:01 +08002498 //update My Navigation Thumbnails
2499 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2500 if (isMyNavigationUrl) {
2501 updateMyNavigationThumbnail(url, view);
2502 }
John Reck34ef2672011-02-10 11:30:55 -08002503 // Only update thumbnails for web urls (http(s)://), not for
2504 // about:, javascript:, data:, etc...
2505 // Unless it is a bookmarked site, then always update
2506 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2507 return;
2508 }
2509
kaiyiz6e5b3e02013-08-19 20:02:01 +08002510 if (url != null && mUpdateMyNavThumbnailUrl != null
2511 && Patterns.WEB_URL.matcher(url).matches()
2512 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2513 String urlHost = (new WebAddress(url)).getHost();
2514 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2515 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2516 || bookmarkHost.length() == 0) {
2517 return;
2518 }
2519 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2520 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2521 bookmarkHost.length());
2522 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2523 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2524 if (!bookmarkDomain.equals(urlDomain)) {
2525 return;
2526 }
2527 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002528 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2529 getDesiredThumbnailHeight(mActivity));
2530 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002531 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2532 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2533 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2534 500);
2535 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002536 return;
2537 }
2538
2539 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002540 new AsyncTask<Void, Void, Void>() {
2541 @Override
2542 protected Void doInBackground(Void... unused) {
2543 Cursor cursor = null;
2544 try {
2545 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002546 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2547 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2548 : url);
2549 if (mUpdateMyNavThumbnail) {
2550 mUpdateMyNavThumbnail = false;
2551 mUpdateMyNavThumbnailUrl = null;
2552 }
John Reck34ef2672011-02-10 11:30:55 -08002553 if (cursor != null && cursor.moveToFirst()) {
2554 final ByteArrayOutputStream os =
2555 new ByteArrayOutputStream();
2556 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002557
John Reck34ef2672011-02-10 11:30:55 -08002558 ContentValues values = new ContentValues();
2559 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002560
John Reck34ef2672011-02-10 11:30:55 -08002561 do {
John Reck617fd832011-02-16 14:35:59 -08002562 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002563 cr.update(Images.CONTENT_URI, values, null, null);
2564 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002565 }
John Reck34ef2672011-02-10 11:30:55 -08002566 } catch (IllegalStateException e) {
2567 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002568 } catch (SQLiteException s) {
2569 // Added for possible error when user tries to remove the same bookmark
2570 // that is being updated with a screen shot
2571 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002572 } finally {
2573 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002574 }
John Reck34ef2672011-02-10 11:30:55 -08002575 return null;
2576 }
2577 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 }
2579
2580 private class Copy implements OnMenuItemClickListener {
2581 private CharSequence mText;
2582
John Reck9c35b9c2012-05-30 10:08:50 -07002583 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002584 public boolean onMenuItemClick(MenuItem item) {
2585 copy(mText);
2586 return true;
2587 }
2588
2589 public Copy(CharSequence toCopy) {
2590 mText = toCopy;
2591 }
2592 }
2593
Leon Scroggins63c02662010-11-18 15:16:27 -05002594 private static class Download implements OnMenuItemClickListener {
2595 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002596 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002597 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002598 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002599 private static final String FALLBACK_EXTENSION = "dat";
2600 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002601
John Reck9c35b9c2012-05-30 10:08:50 -07002602 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002603 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002604 if (DataUri.isDataUri(mText)) {
2605 saveDataUri();
2606 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002607 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002608 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002609 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002610 return true;
2611 }
2612
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002613 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2614 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002615 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002616 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002617 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002618 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 }
George Mount387d45d2011-10-07 15:57:53 -07002620
2621 /**
2622 * Treats mText as a data URI and writes its contents to a file
2623 * based on the current time.
2624 */
2625 private void saveDataUri() {
2626 FileOutputStream outputStream = null;
2627 try {
2628 DataUri uri = new DataUri(mText);
2629 File target = getTarget(uri);
2630 outputStream = new FileOutputStream(target);
2631 outputStream.write(uri.getData());
2632 final DownloadManager manager =
2633 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2634 manager.addCompletedDownload(target.getName(),
2635 mActivity.getTitle().toString(), false,
2636 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002637 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002638 } catch (IOException e) {
2639 Log.e(LOGTAG, "Could not save data URL");
2640 } finally {
2641 if (outputStream != null) {
2642 try {
2643 outputStream.close();
2644 } catch (IOException e) {
2645 // ignore close errors
2646 }
2647 }
2648 }
2649 }
2650
2651 /**
2652 * Creates a File based on the current time stamp and uses
2653 * the mime type of the DataUri to get the extension.
2654 */
2655 private File getTarget(DataUri uri) throws IOException {
2656 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002657 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002658 String nameBase = format.format(new Date());
2659 String mimeType = uri.getMimeType();
2660 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2661 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2662 if (extension == null) {
2663 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2664 extension = FALLBACK_EXTENSION;
2665 }
2666 extension = "." + extension; // createTempFile needs the '.'
2667 File targetFile = File.createTempFile(nameBase, extension, dir);
2668 return targetFile;
2669 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002670 }
2671
Cary Clark8974d282010-11-22 10:46:05 -05002672 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002673 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002674
John Reck9c35b9c2012-05-30 10:08:50 -07002675 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002676 public boolean onMenuItemClick(MenuItem item) {
2677 if (mWebView != null) {
2678 return mWebView.selectText();
2679 }
2680 return false;
2681 }
2682
2683 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002684 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002685 }
2686
2687 }
2688
Michael Kolb8233fac2010-10-26 16:08:53 -07002689 /********************** TODO: UI stuff *****************************/
2690
2691 // these methods have been copied, they still need to be cleaned up
2692
2693 /****************** tabs ***************************************************/
2694
2695 // basic tab interactions:
2696
2697 // it is assumed that tabcontrol already knows about the tab
2698 protected void addTab(Tab tab) {
2699 mUi.addTab(tab);
2700 }
2701
2702 protected void removeTab(Tab tab) {
2703 mUi.removeTab(tab);
2704 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002705 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002706 }
2707
Michael Kolbf2055602011-04-09 17:20:03 -07002708 @Override
2709 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002710 // monkey protection against delayed start
2711 if (tab != null) {
2712 mTabControl.setCurrentTab(tab);
2713 // the tab is guaranteed to have a webview after setCurrentTab
2714 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002715 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002716 //Purge active tabs
2717 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002718 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002719 }
2720
John Reck8bcafc12011-08-29 16:43:02 -07002721 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002722 Tab current = mTabControl.getCurrentTab();
2723 if (current != null
2724 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002725 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002726 }
2727 }
2728
John Reck26b18322011-06-21 13:08:58 -07002729 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 // Dismiss the subwindow if applicable.
2731 dismissSubWindow(appTab);
2732 // Since we might kill the WebView, remove it from the
2733 // content view first.
2734 mUi.detachTab(appTab);
2735 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002736 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 // TODO: analyze why the remove and add are necessary
2738 mUi.attachTab(appTab);
2739 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002740 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002741 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002742 } else {
2743 // If the tab was the current tab, we have to attach
2744 // it to the view system again.
2745 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002746 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002747 }
2748 }
2749
2750 // Remove the sub window if it exists. Also called by TabControl when the
2751 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002752 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002753 public void dismissSubWindow(Tab tab) {
2754 removeSubWindow(tab);
2755 // dismiss the subwindow. This will destroy the WebView.
2756 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002757 WebView wv = getCurrentTopWebView();
2758 if (wv != null) {
2759 wv.requestFocus();
2760 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002761 }
2762
2763 @Override
2764 public void removeSubWindow(Tab t) {
2765 if (t.getSubWebView() != null) {
2766 mUi.removeSubWindow(t.getSubViewContainer());
2767 }
2768 }
2769
2770 @Override
2771 public void attachSubWindow(Tab tab) {
2772 if (tab.getSubWebView() != null) {
2773 mUi.attachSubWindow(tab.getSubViewContainer());
2774 getCurrentTopWebView().requestFocus();
2775 }
2776 }
2777
Mathew Inwood29721c22011-06-29 17:55:24 +01002778 private Tab showPreloadedTab(final UrlData urlData) {
2779 if (!urlData.isPreloaded()) {
2780 return null;
2781 }
2782 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2783 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2784 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002785 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002786 // Could not submit query. Fallback to regular tab creation
2787 tabControl.destroy();
2788 return null;
2789 }
2790 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002791 // check tab count and make room for new tab
2792 if (!mTabControl.canCreateNewTab()) {
2793 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2794 if (leastUsed != null) {
2795 closeTab(leastUsed);
2796 }
2797 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002798 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002799 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002800 mTabControl.addPreloadedTab(t);
2801 addTab(t);
2802 setActiveTab(t);
2803 return t;
2804 }
2805
Michael Kolb7bcafde2011-05-09 13:55:59 -07002806 // open a non inconito tab with the given url data
2807 // and set as active tab
2808 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002809 Tab tab = showPreloadedTab(urlData);
2810 if (tab == null) {
2811 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002812 if ((tab != null) && !urlData.isEmpty()) {
2813 loadUrlDataIn(tab, urlData);
2814 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002815 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002816 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002817 }
2818
Michael Kolb843510f2010-12-09 10:51:49 -08002819 @Override
2820 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002821 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002822 }
2823
Michael Kolb8233fac2010-10-26 16:08:53 -07002824 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002825 public Tab openIncognitoTab() {
2826 return openTab(INCOGNITO_URI, true, true, false);
2827 }
2828
2829 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002830 public Tab openTab(String url, boolean incognito, boolean setActive,
2831 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002832 return openTab(url, incognito, setActive, useCurrent, null);
2833 }
2834
2835 @Override
2836 public Tab openTab(String url, Tab parent, boolean setActive,
2837 boolean useCurrent) {
2838 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2839 setActive, useCurrent, parent);
2840 }
2841
2842 public Tab openTab(String url, boolean incognito, boolean setActive,
2843 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002844 Tab tab = createNewTab(incognito, setActive, useCurrent);
2845 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002846 if (parent != null && parent != tab) {
2847 parent.addChildTab(tab);
2848 }
Michael Kolb519d2282011-05-09 17:03:19 -07002849 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002850 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002851 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002852 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002853 return tab;
2854 }
2855
2856 // this method will attempt to create a new tab
2857 // incognito: private browsing tab
2858 // setActive: ste tab as current tab
2859 // useCurrent: if no new tab can be created, return current tab
2860 private Tab createNewTab(boolean incognito, boolean setActive,
2861 boolean useCurrent) {
2862 Tab tab = null;
2863 if (mTabControl.canCreateNewTab()) {
2864 tab = mTabControl.createNewTab(incognito);
2865 addTab(tab);
2866 if (setActive) {
2867 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002868 } else {
2869 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002870 }
2871 } else {
2872 if (useCurrent) {
2873 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002874 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002875 } else {
2876 mUi.showMaxTabsWarning();
2877 }
2878 }
2879 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002880 }
2881
John Reck2bc80422011-06-30 15:11:49 -07002882 @Override
2883 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2884 SnapshotTab tab = null;
2885 if (mTabControl.canCreateNewTab()) {
2886 tab = mTabControl.createSnapshotTab(snapshotId);
2887 addTab(tab);
2888 if (setActive) {
2889 setActiveTab(tab);
2890 }
2891 } else {
2892 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002893 }
2894 return tab;
2895 }
2896
Michael Kolb8233fac2010-10-26 16:08:53 -07002897 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002898 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002899 * @return boolean True if we successfully switched to a different tab. If
2900 * the indexth tab is null, or if that tab is the same as
2901 * the current one, return false.
2902 */
2903 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002904 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002905 Tab currentTab = mTabControl.getCurrentTab();
2906 if (tab == null || tab == currentTab) {
2907 return false;
2908 }
2909 setActiveTab(tab);
2910 return true;
2911 }
2912
2913 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002914 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002915 closeCurrentTab(false);
2916 }
2917
2918 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002919 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002920 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002921 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002922 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002923 return;
2924 }
Michael Kolbc831b632011-05-11 09:30:34 -07002925 final Tab current = mTabControl.getCurrentTab();
2926 final int pos = mTabControl.getCurrentPosition();
2927 Tab newTab = current.getParent();
2928 if (newTab == null) {
2929 newTab = mTabControl.getTab(pos + 1);
2930 if (newTab == null) {
2931 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002932 }
2933 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002934 if (andQuit) {
2935 mTabControl.setCurrentTab(newTab);
2936 closeTab(current);
2937 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002938 // Close window
2939 closeTab(current);
2940 }
2941 }
2942
2943 /**
2944 * Close the tab, remove its associated title bar, and adjust mTabControl's
2945 * current tab to a valid value.
2946 */
2947 @Override
2948 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002949 if (tab == mTabControl.getCurrentTab()) {
2950 closeCurrentTab();
2951 } else {
2952 removeTab(tab);
2953 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002954 }
2955
Afzal Najamd4e33312012-04-26 01:54:01 -04002956 /**
2957 * Close all tabs except the current one
2958 */
2959 @Override
2960 public void closeOtherTabs() {
2961 int inactiveTabs = mTabControl.getTabCount() - 1;
2962 for (int i = inactiveTabs; i >= 0; i--) {
2963 Tab tab = mTabControl.getTab(i);
2964 if (tab != mTabControl.getCurrentTab()) {
2965 removeTab(tab);
2966 }
2967 }
2968 }
2969
Michael Kolb8233fac2010-10-26 16:08:53 -07002970 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002971 protected void loadUrlFromContext(String url) {
2972 Tab tab = getCurrentTab();
2973 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002974 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002975 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002976 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002977 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002978 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002979 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002980 }
2981 }
2982 }
2983
2984 /**
2985 * Load the URL into the given WebView and update the title bar
2986 * to reflect the new load. Call this instead of WebView.loadUrl
2987 * directly.
2988 * @param view The WebView used to load url.
2989 * @param url The URL to load.
2990 */
John Reck71e51422011-07-01 16:49:28 -07002991 @Override
2992 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002993 loadUrl(tab, url, null);
2994 }
2995
2996 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2997 if (tab != null) {
2998 dismissSubWindow(tab);
2999 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003000 if (tab.hasCrashed) {
3001 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3002 }
Michael Kolba53c9892011-10-05 13:31:40 -07003003 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003004 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003005 }
3006
3007 /**
3008 * Load UrlData into a Tab and update the title bar to reflect the new
3009 * load. Call this instead of UrlData.loadIn directly.
3010 * @param t The Tab used to load.
3011 * @param data The UrlData being loaded.
3012 */
3013 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003014 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003015 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003016 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003017 } else {
John Reck38b39652012-06-05 09:22:59 -07003018 if (t != null && data.mDisableUrlOverride) {
3019 t.disableUrlOverridingForLoad();
3020 }
John Reck26b18322011-06-21 13:08:58 -07003021 loadUrl(t, data.mUrl, data.mHeaders);
3022 }
3023 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003024 }
3025
John Reck30c714c2010-12-16 17:30:34 -08003026 @Override
3027 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003028 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003029 if (tab.canGoBack()) {
3030 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003031 } else {
John Reckef654f12011-07-12 16:42:08 -07003032 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003033 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003034 }
3035
3036 void goBackOnePageOrQuit() {
3037 Tab current = mTabControl.getCurrentTab();
3038 if (current == null) {
3039 /*
3040 * Instead of finishing the activity, simply push this to the back
3041 * of the stack and let ActivityManager to choose the foreground
3042 * activity. As BrowserActivity is singleTask, it will be always the
3043 * root of the task. So we can use either true or false for
3044 * moveTaskToBack().
3045 */
luxiaolb40014b2013-07-19 10:01:43 +08003046 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003047 return;
3048 }
John Reckef654f12011-07-12 16:42:08 -07003049 if (current.canGoBack()) {
3050 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003051 } else {
3052 // Check to see if we are closing a window that was created by
3053 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003054 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003055 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003056 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003057 // Now we close the other tab
3058 closeTab(current);
3059 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003060 /*
3061 * Instead of finishing the activity, simply push this to the back
3062 * of the stack and let ActivityManager to choose the foreground
3063 * activity. As BrowserActivity is singleTask, it will be always the
3064 * root of the task. So we can use either true or false for
3065 * moveTaskToBack().
3066 */
luxiaolb40014b2013-07-19 10:01:43 +08003067 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003068 }
3069 }
3070 }
3071
3072 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003073 * helper method for key handler
3074 * returns the current tab if it can't advance
3075 */
Michael Kolbc831b632011-05-11 09:30:34 -07003076 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003077 int pos = mTabControl.getCurrentPosition() + 1;
3078 if (pos >= mTabControl.getTabCount()) {
3079 pos = 0;
3080 }
3081 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003082 }
3083
3084 /**
3085 * helper method for key handler
3086 * returns the current tab if it can't advance
3087 */
Michael Kolbc831b632011-05-11 09:30:34 -07003088 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003089 int pos = mTabControl.getCurrentPosition() - 1;
3090 if ( pos < 0) {
3091 pos = mTabControl.getTabCount() - 1;
3092 }
3093 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003094 }
3095
John Reckbcef87f2012-02-03 14:58:34 -08003096 boolean isMenuOrCtrlKey(int keyCode) {
3097 return (KeyEvent.KEYCODE_MENU == keyCode)
3098 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3099 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3100 }
3101
Michael Kolb0035fad2011-03-14 13:25:28 -07003102 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003103 * handle key events in browser
3104 *
3105 * @param keyCode
3106 * @param event
3107 * @return true if handled, false to pass to super
3108 */
John Reck9c35b9c2012-05-30 10:08:50 -07003109 @Override
3110 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003111 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003112 // Even if MENU is already held down, we need to call to super to open
3113 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003114 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003115 mMenuIsDown = true;
3116 return false;
3117 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003118
Cary Clark8ff8c662010-12-29 15:03:05 -05003119 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003120 Tab tab = getCurrentTab();
3121 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003122
Cary Clark160bbb92011-01-10 11:17:07 -05003123 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3124 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003125
Michael Kolb8233fac2010-10-26 16:08:53 -07003126 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003127 case KeyEvent.KEYCODE_TAB:
3128 if (event.isCtrlPressed()) {
3129 if (event.isShiftPressed()) {
3130 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003131 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003132 } else {
3133 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003134 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003135 }
3136 return true;
3137 }
3138 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003139 case KeyEvent.KEYCODE_SPACE:
3140 // WebView/WebTextView handle the keys in the KeyDown. As
3141 // the Activity's shortcut keys are only handled when WebView
3142 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003143 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003144 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003145 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003146 pageDown();
3147 }
3148 return true;
3149 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003150 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003151 event.startTracking();
3152 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003153 case KeyEvent.KEYCODE_FORWARD:
3154 if (!noModifiers) break;
3155 tab.goForward();
3156 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003157 case KeyEvent.KEYCODE_DPAD_LEFT:
3158 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003159 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003160 return true;
3161 }
3162 break;
3163 case KeyEvent.KEYCODE_DPAD_RIGHT:
3164 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003165 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003166 return true;
3167 }
3168 break;
3169 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003170 if (ctrl) {
3171 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003172 return true;
3173 }
3174 break;
Michael Kolba4183062011-01-16 10:43:21 -08003175// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003176 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003177 if (ctrl ) {
3178 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003179 return true;
3180 }
3181 break;
Michael Kolba4183062011-01-16 10:43:21 -08003182// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003183// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003184// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003185// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003186// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003187// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003188// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003189// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003190// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003191// case KeyEvent.KEYCODE_M: // unused
3192// case KeyEvent.KEYCODE_N: // in Chrome: new window
3193// case KeyEvent.KEYCODE_O: // in Chrome: open file
3194// case KeyEvent.KEYCODE_P: // in Chrome: print page
3195// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003196// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003197// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3198 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003199 // we can't use the ctrl/shift flags, they check for
3200 // exclusive use of a modifier
3201 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003202 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003203 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003204 } else {
3205 openTabToHomePage();
3206 }
3207 return true;
3208 }
3209 break;
3210// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3211// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003212// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003213// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3214// case KeyEvent.KEYCODE_Y: // unused
3215// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003216 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003217 // it is a regular key and webview is not null
3218 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003219 }
3220
John Reck9c35b9c2012-05-30 10:08:50 -07003221 @Override
3222 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003223 switch(keyCode) {
3224 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003225 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003226 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003227 return true;
3228 }
3229 break;
3230 }
3231 return false;
3232 }
3233
John Reck9c35b9c2012-05-30 10:08:50 -07003234 @Override
3235 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003236 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003237 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003238 if (KeyEvent.KEYCODE_MENU == keyCode
3239 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003240 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003241 }
3242 }
Cary Clark160bbb92011-01-10 11:17:07 -05003243 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003244 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003245 case KeyEvent.KEYCODE_BACK:
3246 if (event.isTracking() && !event.isCanceled()) {
3247 onBackKey();
3248 return true;
3249 }
3250 break;
3251 }
3252 return false;
3253 }
3254
3255 public boolean isMenuDown() {
3256 return mMenuIsDown;
3257 }
3258
John Reck9c35b9c2012-05-30 10:08:50 -07003259 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003260 public void setupAutoFill(Message message) {
3261 // Open the settings activity at the AutoFill profile fragment so that
3262 // the user can create a new profile. When they return, we will dispatch
3263 // the message so that we can autofill the form using their new profile.
3264 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3265 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3266 AutoFillSettingsFragment.class.getName());
3267 mAutoFillSetupMessage = message;
3268 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3269 }
John Reckb3417f02011-01-14 11:01:05 -08003270
John Reck9c35b9c2012-05-30 10:08:50 -07003271 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003272 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003273 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003274 return true;
3275 }
3276
John Reck1cf4b792011-07-26 10:22:22 -07003277 @Override
3278 public boolean shouldCaptureThumbnails() {
3279 return mUi.shouldCaptureThumbnails();
3280 }
3281
Michael Kolbc3af0672011-08-09 10:24:41 -07003282 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003283 public boolean supportsVoice() {
3284 PackageManager pm = mActivity.getPackageManager();
3285 List activities = pm.queryIntentActivities(new Intent(
3286 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3287 return activities.size() != 0;
3288 }
3289
3290 @Override
3291 public void startVoiceRecognizer() {
3292 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003293 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003294 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3295 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3296 mActivity.startActivityForResult(voice, VOICE_RESULT);
3297 }
3298
3299 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003300 public void setBlockEvents(boolean block) {
3301 mBlockEvents = block;
3302 }
3303
John Reck9c35b9c2012-05-30 10:08:50 -07003304 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003305 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003306 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003307 }
3308
John Reck9c35b9c2012-05-30 10:08:50 -07003309 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003310 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003311 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003312 }
3313
John Reck9c35b9c2012-05-30 10:08:50 -07003314 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003315 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003316 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003317 }
3318
John Reck9c35b9c2012-05-30 10:08:50 -07003319 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003320 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003321 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003322 }
3323
John Reck9c35b9c2012-05-30 10:08:50 -07003324 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003325 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003326 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003327 }
3328
Michael Kolb8233fac2010-10-26 16:08:53 -07003329}