blob: 9803ec451e896ab5c20ebd29b0b15a940d00e4d8 [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;
Leon Scrogginsac993842011-02-02 12:54:07 -050081import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070082
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080083import org.codeaurora.swe.CookieManager;
84import org.codeaurora.swe.CookieSyncManager;
85import org.codeaurora.swe.HttpAuthHandler;
86import org.codeaurora.swe.SslErrorHandler;
87import org.codeaurora.swe.WebSettings;
88import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070089import org.codeaurora.swe.WebBackForwardList;
90import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080091
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070092import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070093import com.android.browser.R;
94import com.android.browser.IntentHandler.UrlData;
95import com.android.browser.UI.ComboViews;
96import com.android.browser.mynavigation.AddMyNavigationPage;
97import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070098import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070099import com.android.browser.platformsupport.BrowserContract;
100import com.android.browser.platformsupport.WebAddress;
101import com.android.browser.platformsupport.BrowserContract.Images;
102import com.android.browser.provider.BrowserProvider2.Thumbnails;
103import com.android.browser.provider.SnapshotProvider.Snapshots;
104import com.android.browser.reflect.ReflectHelper;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700107import java.io.File;
108import java.io.FileOutputStream;
109import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700110import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700111import java.text.DateFormat;
112import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700113import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700115import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700116import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800117import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200118import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700119import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700120
121/**
122 * Controller for browser
123 */
124public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700125 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700126
127 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800128 private static final String SEND_APP_ID_EXTRA =
129 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700130 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800131
Vivek Sekharb54614f2014-05-01 19:03:37 -0700132 // Remind switch to data connection if wifi is unavailable
133 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800134
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;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700181 private HomepageHandler mHomepageHandler;
182 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700183 private BrowserSettings mSettings;
184 private WebViewFactory mFactory;
185
186 private WakeLock mWakeLock;
187
188 private UrlHandler mUrlHandler;
189 private UploadHandler mUploadHandler;
190 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700191 private PageDialogsHandler mPageDialogsHandler;
192 private NetworkStateHandler mNetworkHandler;
193
Ben Murdoch8029a772010-11-16 11:58:21 +0000194 private Message mAutoFillSetupMessage;
195
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800197 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700198
Michael Kolb8233fac2010-10-26 16:08:53 -0700199 // FIXME, temp address onPrepareMenu performance problem.
200 // When we move everything out of view, we should rewrite this.
201 private int mCurrentMenuState = 0;
202 private int mMenuState = R.id.MAIN_MENU;
203 private int mOldMenuState = EMPTY_MENU;
204 private Menu mCachedMenu;
205
Michael Kolb8233fac2010-10-26 16:08:53 -0700206 private boolean mMenuIsDown;
207
208 // For select and find, we keep track of the ActionMode so that
209 // finish() can be called as desired.
210 private ActionMode mActionMode;
211
212 /**
213 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
214 * of whether the configuration has changed. The first onMenuOpened call
215 * after a configuration change is simply a reopening of the same menu
216 * (i.e. mIconView did not change).
217 */
218 private boolean mConfigChanged;
219
220 /**
221 * Keeps track of whether the options menu is open. This is important in
222 * determining whether to show or hide the title bar overlay
223 */
224 private boolean mOptionsMenuOpen;
225
226 /**
227 * Whether or not the options menu is in its bigger, popup menu form. When
228 * true, we want the title bar overlay to be gone. When false, we do not.
229 * Only meaningful if mOptionsMenuOpen is true.
230 */
231 private boolean mExtendedMenuOpen;
232
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 private boolean mActivityPaused = true;
234 private boolean mLoadStopped;
235
236 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500237 // Checks to see when the bookmarks database has changed, and updates the
238 // Tabs' notion of whether they represent bookmarked sites.
239 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700240 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700241
Michael Kolbc3af0672011-08-09 10:24:41 -0700242 private boolean mBlockEvents;
243
Michael Kolb0b129122012-06-04 16:31:58 -0700244 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800245 private boolean mUpdateMyNavThumbnail;
246 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700247
George Mount3636d0a2011-11-21 09:08:21 -0800248 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 mActivity = browser;
250 mSettings = BrowserSettings.getInstance();
251 mTabControl = new TabControl(this);
252 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700253 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800254 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700255 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700256
257 mUrlHandler = new UrlHandler(this);
258 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
260
Michael Kolb8233fac2010-10-26 16:08:53 -0700261 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500262 mBookmarksObserver = new ContentObserver(mHandler) {
263 @Override
264 public void onChange(boolean selfChange) {
265 int size = mTabControl.getTabCount();
266 for (int i = 0; i < size; i++) {
267 mTabControl.getTab(i).updateBookmarkedStatus();
268 }
269 }
270
271 };
272 browser.getContentResolver().registerContentObserver(
273 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700274
275 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700276 mHomepageHandler = new HomepageHandler(browser, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 }
278
John Reck9c35b9c2012-05-30 10:08:50 -0700279 @Override
280 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800281 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800282 // mCrashRecoverHandler has any previously saved state.
283 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700284 }
285
George Mount3636d0a2011-11-21 09:08:21 -0800286 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800287 // we dont want to ever recover incognito tabs
288 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700289
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700291 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000293
Michael Kolbc831b632011-05-11 09:30:34 -0700294 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500295 // Not able to restore so we go ahead and clear session cookies. We
296 // must do this before trying to login the user as we don't want to
297 // clear any session cookies set during login.
298 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700299 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800300 if (intent == null) {
301 // This won't happen under common scenarios. The icicle is
302 // not null, but there aren't any tabs to restore.
303 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700304 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800305 final Bundle extra = intent.getExtras();
306 // Create an initial tab.
307 // If the intent is ACTION_VIEW and data is not null, the Browser is
308 // invoked to view the content by another application. In this case,
309 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800310 UrlData urlData = null;
311 if (intent.getData() != null
312 && Intent.ACTION_VIEW.equals(intent.getAction())
313 && intent.getData().toString().startsWith("content://")) {
314 urlData = new UrlData(intent.getData().toString());
315 } else {
316 urlData = IntentHandler.getUrlDataFromIntent(intent);
317 }
George Mount3636d0a2011-11-21 09:08:21 -0800318 Tab t = null;
319 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700320 String landingPage = mActivity.getResources().getString(
321 R.string.def_landing_page);
322 if (!landingPage.isEmpty()) {
323 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800324 } else {
325 t = openTabToHomePage();
326 }
George Mount3636d0a2011-11-21 09:08:21 -0800327 } else {
328 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700329 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800330 }
331 if (t != null) {
332 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
333 }
334 WebView webView = t.getWebView();
335 if (extra != null) {
336 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
337 if (scale > 0 && scale <= 1000) {
338 webView.setInitialScale(scale);
339 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700340 }
341 }
John Reckd8c74522011-06-14 08:45:00 -0700342 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700343 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700344 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500345 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700346 List<Tab> tabs = mTabControl.getTabs();
347 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700348
John Reck1cf4b792011-07-26 10:22:22 -0700349 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700350 //handle restored pages that may require a JS interface
351 if (t.getWebView() != null) {
352 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
353 if (backForwardList != null) {
354 for (int i = 0; i < backForwardList.getSize(); i++) {
355 WebHistoryItem item = backForwardList.getItemAtIndex(i);
356 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
357 }
358 }
359 }
John Reck1cf4b792011-07-26 10:22:22 -0700360 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700361 if (t != mTabControl.getCurrentTab()) {
362 t.pause();
363 }
John Reck1cf4b792011-07-26 10:22:22 -0700364 }
365 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700366 if (tabs.size() == 0) {
367 openTabToHomePage();
368 }
John Reck1cf4b792011-07-26 10:22:22 -0700369 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700370 // TabControl.restoreState() will create a new tab even if
371 // restoring the state fails.
372 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800373 // Intent is non-null when framework thinks the browser should be
374 // launching with a new intent (icicle is null).
375 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700376 mIntentHandler.onNewIntent(intent);
377 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700378 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700380 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800381 if (jsFlags.trim().length() != 0) {
382 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700383 }
George Mount3636d0a2011-11-21 09:08:21 -0800384 if (intent != null
385 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700386 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800387 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 }
389
John Reck1cf4b792011-07-26 10:22:22 -0700390 private static class PruneThumbnails implements Runnable {
391 private Context mContext;
392 private List<Long> mIds;
393
394 PruneThumbnails(Context context, List<Long> preserveIds) {
395 mContext = context.getApplicationContext();
396 mIds = preserveIds;
397 }
398
399 @Override
400 public void run() {
401 ContentResolver cr = mContext.getContentResolver();
402 if (mIds == null || mIds.size() == 0) {
403 cr.delete(Thumbnails.CONTENT_URI, null, null);
404 } else {
405 int length = mIds.size();
406 StringBuilder where = new StringBuilder();
407 where.append(Thumbnails._ID);
408 where.append(" not in (");
409 for (int i = 0; i < length; i++) {
410 where.append(mIds.get(i));
411 if (i < (length - 1)) {
412 where.append(",");
413 }
414 }
415 where.append(")");
416 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
417 }
418 }
419
420 }
421
Michael Kolb1514bb72010-11-22 09:11:48 -0800422 @Override
423 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700424 return mFactory;
425 }
426
427 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800428 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800429 if (tab.hasCrashed)
430 tab.showCrashView();
431 else
432 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800433 }
434
435 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800436 public void createSubWindow(Tab tab) {
437 endActionMode();
438 WebView mainView = tab.getWebView();
439 WebView subView = mFactory.createWebView((mainView == null)
440 ? false
441 : mainView.isPrivateBrowsingEnabled());
442 mUi.createSubWindow(tab, subView);
443 }
444
445 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700446 public Context getContext() {
447 return mActivity;
448 }
449
450 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700451 public Activity getActivity() {
452 return mActivity;
453 }
454
455 void setUi(UI ui) {
456 mUi = ui;
457 }
458
Michael Kolbaf63dba2012-05-16 12:58:05 -0700459 @Override
460 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700461 return mSettings;
462 }
463
464 IntentHandler getIntentHandler() {
465 return mIntentHandler;
466 }
467
468 @Override
469 public UI getUi() {
470 return mUi;
471 }
472
473 int getMaxTabs() {
474 return mActivity.getResources().getInteger(R.integer.max_tabs);
475 }
476
477 @Override
478 public TabControl getTabControl() {
479 return mTabControl;
480 }
481
Michael Kolb1bf23132010-11-19 12:55:12 -0800482 @Override
483 public List<Tab> getTabs() {
484 return mTabControl.getTabs();
485 }
486
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 private void startHandler() {
488 mHandler = new Handler() {
489
490 @Override
491 public void handleMessage(Message msg) {
492 switch (msg.what) {
493 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700494 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 break;
496 case FOCUS_NODE_HREF:
497 {
498 String url = (String) msg.getData().get("url");
499 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500500 String src = (String) msg.getData().get("src");
501 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 if (TextUtils.isEmpty(url)) {
503 break;
504 }
505 HashMap focusNodeMap = (HashMap) msg.obj;
506 WebView view = (WebView) focusNodeMap.get("webview");
507 // Only apply the action if the top window did not change.
508 if (getCurrentTopWebView() != view) {
509 break;
510 }
511 switch (msg.arg1) {
512 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700513 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700514 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500515 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700516 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500517 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500518 case R.id.open_newtab_context_menu_id:
519 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700520 openTab(url, parent,
521 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500522 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 case R.id.copy_link_context_menu_id:
524 copy(url);
525 break;
526 case R.id.save_link_context_menu_id:
527 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500528 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800529 mActivity, url, view.getSettings().getUserAgentString(),
530 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700531 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700532 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700533 if(title == null || title == "")
534 title = url;
535
536 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
537 //SWE TODO: No thumbnail support for the url obtained via
538 //browser context menu as its not loaded in webview.
539 if (bookmarkIntent != null) {
540 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
541 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
542 mActivity.startActivity(bookmarkIntent);
543 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700544 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700545 }
546 break;
547 }
548
549 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700550 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700551 break;
552
553 case STOP_LOAD:
554 stopLoading();
555 break;
556
557 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700558 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700559 mWakeLock.release();
560 // if we reach here, Browser should be still in the
561 // background loading after WAKELOCK_TIMEOUT (5-min).
562 // To avoid burning the battery, stop loading.
563 mTabControl.stopAllLoading();
564 }
565 break;
566
567 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800568 Tab tab = (Tab) msg.obj;
569 if (tab != null) {
570 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700571 }
572 break;
kaiyiz591110b2013-08-06 17:11:06 +0800573 case OPEN_MENU:
574 if (!mOptionsMenuOpen && mActivity != null ) {
575 mActivity.openOptionsMenu();
576 }
577 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700578 }
579 }
580 };
581
582 }
583
John Reckef654f12011-07-12 16:42:08 -0700584 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200585 public Tab getCurrentTab() {
586 return mTabControl.getCurrentTab();
587 }
588
Michael Kolbba99c5d2010-11-29 14:57:41 -0800589 @Override
590 public void shareCurrentPage() {
591 shareCurrentPage(mTabControl.getCurrentTab());
592 }
593
594 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700595 if (tab == null || tab.getWebView() == null)
596 return;
597
598 final Tab mytab = tab;
599 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
600 @Override
601 public void onReceiveValue(Bitmap bitmap) {
602 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
603 mytab.getFavicon(), bitmap);
604 }
605 };
606
607 createScreenshotAsync(
608 tab.getWebView(),
609 getDesiredThumbnailWidth(mActivity),
610 getDesiredThumbnailHeight(mActivity),
611 new ValueCallback<Bitmap>() {
612 @Override
613 public void onReceiveValue(Bitmap bitmap) {
614 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
615 mytab.getFavicon(), bitmap);
616 }
617 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800618 }
619
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 /**
621 * Share a page, providing the title, url, favicon, and a screenshot. Uses
622 * an {@link Intent} to launch the Activity chooser.
623 * @param c Context used to launch a new Activity.
624 * @param title Title of the page. Stored in the Intent with
625 * {@link Intent#EXTRA_SUBJECT}
626 * @param url URL of the page. Stored in the Intent with
627 * {@link Intent#EXTRA_TEXT}
628 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
629 * with {@link Browser#EXTRA_SHARE_FAVICON}
630 * @param screenshot Bitmap of a screenshot of the page. Stored in the
631 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
632 */
633 static final void sharePage(Context c, String title, String url,
634 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700635
636 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
637 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
638 R.layout.app_row, sDialog.getApps());
639 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 }
641
642 private void copy(CharSequence text) {
643 ClipboardManager cm = (ClipboardManager) mActivity
644 .getSystemService(Context.CLIPBOARD_SERVICE);
645 cm.setText(text);
646 }
647
648 // lifecycle
649
John Reck9c35b9c2012-05-30 10:08:50 -0700650 @Override
651 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800653 // update the menu in case of a locale change
654 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800655 if (mOptionsMenuOpen) {
656 mActivity.closeOptionsMenu();
657 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
658 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700659 if (mPageDialogsHandler != null) {
660 mPageDialogsHandler.onConfigurationChanged(config);
661 }
662 mUi.onConfigurationChanged(config);
663 }
664
665 @Override
666 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700667 if (!mUi.isWebShowing()) {
668 mUi.showWeb(false);
669 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 mIntentHandler.onNewIntent(intent);
671 }
672
John Reck9c35b9c2012-05-30 10:08:50 -0700673 @Override
674 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800675 if (mUi.isCustomViewShowing()) {
676 hideCustomView();
677 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 if (mActivityPaused) {
679 Log.e(LOGTAG, "BrowserActivity is already paused.");
680 return;
681 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800683 Tab tab = mTabControl.getCurrentTab();
684 if (tab != null) {
685 tab.pause();
686 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700687 if (mWakeLock == null) {
688 PowerManager pm = (PowerManager) mActivity
689 .getSystemService(Context.POWER_SERVICE);
690 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
691 }
Michael Kolb70976932010-11-30 11:34:01 -0800692 mWakeLock.acquire();
693 mHandler.sendMessageDelayed(mHandler
694 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
695 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 }
697 mUi.onPause();
698 mNetworkHandler.onPause();
699
700 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100701 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 }
703
John Reck9c35b9c2012-05-30 10:08:50 -0700704 @Override
705 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800707 Bundle saveState = createSaveState();
708
709 // crash recovery manages all save & restore state
710 mCrashRecoveryHandler.writeState(saveState);
711 mSettings.setLastRunPaused(true);
712 }
713
714 /**
715 * Save the current state to outState. Does not write the state to
716 * disk.
717 * @return Bundle containing the current state of all tabs.
718 */
719 /* package */ Bundle createSaveState() {
720 Bundle saveState = new Bundle();
721 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800722 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700723 }
724
John Reck9c35b9c2012-05-30 10:08:50 -0700725 @Override
726 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 if (!mActivityPaused) {
728 Log.e(LOGTAG, "BrowserActivity is already resumed.");
729 return;
730 }
George Mount3636d0a2011-11-21 09:08:21 -0800731 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700732 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800733 Tab current = mTabControl.getCurrentTab();
734 if (current != null) {
735 current.resume();
736 resumeWebViewTimers(current);
737 }
John Reckf57c0292011-07-21 18:15:39 -0700738 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200739
Michael Kolb8233fac2010-10-26 16:08:53 -0700740 mUi.onResume();
741 mNetworkHandler.onResume();
742 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100743 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700744 if (mVoiceResult != null) {
745 mUi.onVoiceResult(mVoiceResult);
746 mVoiceResult = null;
747 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800748 if (current != null && current.hasCrashed) {
749 current.showCrashView();
750 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700751 }
752
John Reckf57c0292011-07-21 18:15:39 -0700753 private void releaseWakeLock() {
754 if (mWakeLock != null && mWakeLock.isHeld()) {
755 mHandler.removeMessages(RELEASE_WAKELOCK);
756 mWakeLock.release();
757 }
758 }
759
Michael Kolb70976932010-11-30 11:34:01 -0800760 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800761 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800762 * @param tab guaranteed non-null
763 */
764 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700765 boolean inLoad = tab.inPageLoad();
766 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
767 CookieSyncManager.getInstance().startSync();
768 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100769 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700770 }
771 }
772
Michael Kolb70976932010-11-30 11:34:01 -0800773 /**
774 * Pause all WebView timers using the WebView of the given tab
775 * @param tab
776 * @return true if the timers are paused or tab is null
777 */
778 private boolean pauseWebViewTimers(Tab tab) {
779 if (tab == null) {
780 return true;
781 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700782 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100783 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700784 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700785 }
Michael Kolb70976932010-11-30 11:34:01 -0800786 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700787 }
788
John Reck9c35b9c2012-05-30 10:08:50 -0700789 @Override
790 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800791 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
793 mUploadHandler = null;
794 }
795 if (mTabControl == null) return;
796 mUi.onDestroy();
797 // Remove the current tab and sub window
798 Tab t = mTabControl.getCurrentTab();
799 if (t != null) {
800 dismissSubWindow(t);
801 removeTab(t);
802 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500803 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 // Destroy all the tabs
805 mTabControl.destroy();
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 }
807
808 protected boolean isActivityPaused() {
809 return mActivityPaused;
810 }
811
John Reck9c35b9c2012-05-30 10:08:50 -0700812 @Override
813 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 mTabControl.freeMemory();
815 }
816
817 @Override
818 public boolean shouldShowErrorConsole() {
819 return mShouldShowErrorConsole;
820 }
821
822 protected void setShouldShowErrorConsole(boolean show) {
823 if (show == mShouldShowErrorConsole) {
824 // Nothing to do.
825 return;
826 }
827 mShouldShowErrorConsole = show;
828 Tab t = mTabControl.getCurrentTab();
829 if (t == null) {
830 // There is no current tab so we cannot toggle the error console
831 return;
832 }
833 mUi.setShouldShowErrorConsole(t, show);
834 }
835
836 @Override
837 public void stopLoading() {
838 mLoadStopped = true;
839 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700841 if (w != null) {
842 w.stopLoading();
843 mUi.onPageStopped(tab);
844 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 }
846
847 boolean didUserStopLoading() {
848 return mLoadStopped;
849 }
850
kaiyiza016da12013-08-26 17:50:22 +0800851 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700852 final String reminderType = getContext().getResources().getString(
853 R.string.def_wifi_browser_interaction_remind_type);
854 final String selectionConnnection = getContext().getResources().getString(
855 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700856 final String wifiSelection = getContext().getResources().getString(
857 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700858
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700859 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
860 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700861 return;
862
kaiyiza016da12013-08-26 17:50:22 +0800863 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700864 Context.CONNECTIVITY_SERVICE);
865 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700866 WifiManager wifiMgr = (WifiManager) this.getContext()
867 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700868 if (networkInfo == null
869 || (networkInfo != null && (networkInfo.getType() !=
870 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700871 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
872 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700873 List<ScanResult> list = wifiMgr.getScanResults();
874 // Have no AP's for Wifi's fall back to data
875 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700876 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700877 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
878 this.getContext().startActivity(intent);
879 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700880 // Request to select Wifi AP
881 try {
882 Intent intent = new Intent(wifiSelection);
883 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
884 this.getContext().startActivity(intent);
885 } catch (Exception e) {
886 String err_msg = this.getContext().getString(
887 R.string.acivity_not_found, wifiSelection);
888 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
889 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700890 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700891 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800892 }
893 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700894
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 // WebViewController
896
897 @Override
John Reck324d4402011-01-11 16:56:42 -0800898 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700899
900 // We've started to load a new page. If there was a pending message
901 // to save a screenshot then we will now take the new page and save
902 // an incorrect screenshot. Therefore, remove any pending thumbnail
903 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700904 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700905
906 // reset sync timer to avoid sync starts during loading a page
907 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700908 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700909 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700910 boolean networkNotifier =
911 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700912 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700913 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800914 } else {
915 if (!mNetworkHandler.isNetworkUp()) {
916 view.setNetworkAvailable(false);
917 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700918 }
919
920 // when BrowserActivity just starts, onPageStarted may be called before
921 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
922 // to start the timer. As we won't switch tabs while an activity is in
923 // pause state, we can ensure calling resume and pause in pair.
924 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800925 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700926 }
927 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 endActionMode();
929
John Reck30c714c2010-12-16 17:30:34 -0800930 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700931
John Reck324d4402011-01-11 16:56:42 -0800932 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700933 // update the bookmark database for favicon
934 maybeUpdateFavicon(tab, null, url, favicon);
935
936 Performance.tracePageStart(url);
937
938 // Performance probe
939 if (false) {
940 Performance.onPageStarted();
941 }
942
943 }
944
945 @Override
John Reck324d4402011-01-11 16:56:42 -0800946 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700947 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800948 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200949
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 // Performance probe
951 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800952 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700953 }
954
Tarun Nainani8eb00912014-07-17 12:28:32 -0700955 tab.onPageFinished();
956
Michael Kolb8233fac2010-10-26 16:08:53 -0700957 Performance.tracePageFinished();
958 }
959
960 @Override
John Reck30c714c2010-12-16 17:30:34 -0800961 public void onProgressChanged(Tab tab) {
962 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700963
964 if (newProgress == 100) {
965 CookieSyncManager.getInstance().sync();
966 // onProgressChanged() may continue to be called after the main
967 // frame has finished loading, as any remaining sub frames continue
968 // to load. We'll only get called once though with newProgress as
969 // 100 when everything is loaded. (onPageFinished is called once
970 // when the main frame completes loading regardless of the state of
971 // any sub frames so calls to onProgressChanges may continue after
972 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800973 if (tab.inPageLoad()) {
974 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200975 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
976 // pause the WebView timer and release the wake lock if it is
977 // finished while BrowserActivity is in pause state.
978 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 }
John Reckd9862372012-02-21 15:04:50 -0800980 if (!tab.isPrivateBrowsingEnabled()
981 && !TextUtils.isEmpty(tab.getUrl())
982 && !tab.isSnapshot()) {
983 // Only update the bookmark screenshot if the user did not
984 // cancel the load early and there is not already
985 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700986 if (tab.shouldUpdateThumbnail() &&
987 (tab.inForeground() && !didUserStopLoading()
988 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800989 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
990 mHandler.sendMessageDelayed(mHandler.obtainMessage(
991 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800992 1500);
John Reckd9862372012-02-21 15:04:50 -0800993 }
994 }
995 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800997 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700998 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800999 // still loading
1000 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001001 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001002 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 }
1004 }
John Reck30c714c2010-12-16 17:30:34 -08001005 mUi.onProgressChanged(tab);
1006 }
1007
1008 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001009 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001010 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001011 }
1012
1013 @Override
1014 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001015 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001016 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001017 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1019 return;
1020 }
1021 // Update the title in the history database if not in private browsing mode
1022 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001023 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 }
1025 }
1026
1027 @Override
1028 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001029 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001030 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1031 }
1032
1033 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001034 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1035 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001036 }
1037
1038 @Override
1039 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1040 if (mMenuIsDown) {
1041 // only check shortcut key when MENU is held
1042 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1043 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001045 int keyCode = event.getKeyCode();
1046 // We need to send almost every key to WebKit. However:
1047 // 1. We don't want to block the device on the renderer for
1048 // some keys like menu, home, call.
1049 // 2. There are no WebKit equivalents for some of these keys
1050 // (see app/keyboard_codes_win.h)
1051 // Note that these are not the same set as KeyEvent.isSystemKey:
1052 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1053 if (keyCode == KeyEvent.KEYCODE_MENU ||
1054 keyCode == KeyEvent.KEYCODE_HOME ||
1055 keyCode == KeyEvent.KEYCODE_BACK ||
1056 keyCode == KeyEvent.KEYCODE_CALL ||
1057 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1058 keyCode == KeyEvent.KEYCODE_POWER ||
1059 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1060 keyCode == KeyEvent.KEYCODE_CAMERA ||
1061 keyCode == KeyEvent.KEYCODE_FOCUS ||
1062 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1063 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1064 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1065 return true;
1066 }
1067
1068 // We also have to intercept some shortcuts before we send them to the ContentView.
1069 if (event.isCtrlPressed() && (
1070 keyCode == KeyEvent.KEYCODE_TAB ||
1071 keyCode == KeyEvent.KEYCODE_W ||
1072 keyCode == KeyEvent.KEYCODE_F4)) {
1073 return true;
1074 }
1075
1076 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 }
1078
1079 @Override
John Reck997b1b72012-04-19 18:08:25 -07001080 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 if (!isActivityPaused()) {
1082 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001083 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 } else {
John Reck997b1b72012-04-19 18:08:25 -07001085 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001086 }
1087 }
John Reck997b1b72012-04-19 18:08:25 -07001088 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001089 }
1090
1091 @Override
John Reck324d4402011-01-11 16:56:42 -08001092 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001093 boolean disableHistoryWrites =
1094 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1095
1096 // Don't save anything in private browsing mode or when explicitly set
1097 // not to write history via an overlay
1098 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001099 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001100
John Reck324d4402011-01-11 16:56:42 -08001101 if (TextUtils.isEmpty(url)
1102 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 return;
1104 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001105
John Reckf57c0292011-07-21 18:15:39 -07001106 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001107 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001108 }
1109
1110 @Override
1111 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1112 AsyncTask<Void, Void, String[]> task =
1113 new AsyncTask<Void, Void, String[]>() {
1114 @Override
1115 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001116 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 }
1118 @Override
1119 public void onPostExecute(String[] result) {
1120 callback.onReceiveValue(result);
1121 }
1122 };
1123 task.execute();
1124 }
1125
1126 @Override
1127 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1128 final HttpAuthHandler handler, final String host,
1129 final String realm) {
1130 String username = null;
1131 String password = null;
1132
1133 boolean reuseHttpAuthUsernamePassword
1134 = handler.useHttpAuthUsernamePassword();
1135
1136 if (reuseHttpAuthUsernamePassword && view != null) {
1137 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1138 if (credentials != null && credentials.length == 2) {
1139 username = credentials[0];
1140 password = credentials[1];
1141 }
1142 }
1143
1144 if (username != null && password != null) {
1145 handler.proceed(username, password);
1146 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001147 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1149 } else {
1150 handler.cancel();
1151 }
1152 }
1153 }
1154
1155 @Override
1156 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001157 String contentDisposition, String mimetype, String referer,
1158 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001159 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001160 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001161 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001162 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001163 // This Tab was opened for the sole purpose of downloading a
1164 // file. Remove it.
1165 if (tab == mTabControl.getCurrentTab()) {
1166 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001167 if (tab.getDerivedFromIntent())
1168 closeTab(tab);
1169 else
1170 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001171 } else {
1172 // In this case, it is not.
1173 closeTab(tab);
1174 }
1175 }
1176 }
1177
1178 @Override
1179 public Bitmap getDefaultVideoPoster() {
1180 return mUi.getDefaultVideoPoster();
1181 }
1182
1183 @Override
1184 public View getVideoLoadingProgressView() {
1185 return mUi.getVideoLoadingProgressView();
1186 }
1187
1188 @Override
1189 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1190 SslError error) {
1191 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1192 }
1193
1194 // helper method
1195
1196 /*
1197 * Update the favorites icon if the private browsing isn't enabled and the
1198 * icon is valid.
1199 */
1200 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1201 final String url, Bitmap favicon) {
1202 if (favicon == null) {
1203 return;
1204 }
1205 if (!tab.isPrivateBrowsingEnabled()) {
1206 Bookmarks.updateFavicon(mActivity
1207 .getContentResolver(), originalUrl, url, favicon);
1208 }
1209 }
1210
Leon Scroggins4cd97792010-12-03 15:31:56 -05001211 @Override
1212 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001213 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001214 mUi.bookmarkedStatusHasChanged(tab);
1215 }
1216
Michael Kolb8233fac2010-10-26 16:08:53 -07001217 // end WebViewController
1218
1219 protected void pageUp() {
1220 getCurrentTopWebView().pageUp(false);
1221 }
1222
1223 protected void pageDown() {
1224 getCurrentTopWebView().pageDown(false);
1225 }
1226
1227 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001228 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001229 public void editUrl() {
1230 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001231 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 }
1233
John Reck9c35b9c2012-05-30 10:08:50 -07001234 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001235 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001236 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001237 if (tab.inForeground()) {
1238 if (mUi.isCustomViewShowing()) {
1239 callback.onCustomViewHidden();
1240 return;
1241 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001242 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 // Save the menu state and set it to empty while the custom
1244 // view is showing.
1245 mOldMenuState = mMenuState;
1246 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001247 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001248 }
1249 }
1250
1251 @Override
1252 public void hideCustomView() {
1253 if (mUi.isCustomViewShowing()) {
1254 mUi.onHideCustomView();
1255 // Reset the old menu state.
1256 mMenuState = mOldMenuState;
1257 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001258 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 }
1260 }
1261
John Reck9c35b9c2012-05-30 10:08:50 -07001262 @Override
1263 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 Intent intent) {
1265 if (getCurrentTopWebView() == null) return;
1266 switch (requestCode) {
1267 case PREFERENCES_PAGE:
1268 if (resultCode == Activity.RESULT_OK && intent != null) {
1269 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001270 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001271 mTabControl.removeParentChildRelationShips();
1272 }
1273 }
1274 break;
1275 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001276 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001277 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001278 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001280 case AUTOFILL_SETUP:
1281 // Determine whether a profile was actually set up or not
1282 // and if so, send the message back to the WebTextView to
1283 // fill the form with the new profile.
1284 if (getSettings().getAutoFillProfile() != null) {
1285 mAutoFillSetupMessage.sendToTarget();
1286 mAutoFillSetupMessage = null;
1287 }
1288 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001289 case COMBO_VIEW:
1290 if (intent == null || resultCode != Activity.RESULT_OK) {
1291 break;
1292 }
John Reck3ba45532011-08-11 16:26:53 -07001293 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001294 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1295 Tab t = getCurrentTab();
1296 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001297 mUpdateMyNavThumbnail = true;
1298 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001299 loadUrl(t, uri.toString());
1300 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1301 String[] urls = intent.getStringArrayExtra(
1302 ComboViewActivity.EXTRA_OPEN_ALL);
1303 Tab parent = getCurrentTab();
1304 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001305 if (url != null) {
1306 parent = openTab(url, parent,
1307 !mSettings.openInBackground(), true);
1308 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001309 }
1310 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1311 long id = intent.getLongExtra(
1312 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1313 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001314 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001315 }
1316 }
1317 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001318 case VOICE_RESULT:
1319 if (resultCode == Activity.RESULT_OK && intent != null) {
1320 ArrayList<String> results = intent.getStringArrayListExtra(
1321 RecognizerIntent.EXTRA_RESULTS);
1322 if (results.size() >= 1) {
1323 mVoiceResult = results.get(0);
1324 }
1325 }
1326 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001327 case MY_NAVIGATION:
1328 if (intent == null || resultCode != Activity.RESULT_OK) {
1329 break;
1330 }
1331
1332 if (intent.getBooleanExtra("need_refresh", false) &&
1333 getCurrentTopWebView() != null) {
1334 getCurrentTopWebView().reload();
1335 }
1336 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 default:
1338 break;
1339 }
1340 getCurrentTopWebView().requestFocus();
1341 }
1342
1343 /**
1344 * Open the Go page.
1345 * @param startWithHistory If true, open starting on the history tab.
1346 * Otherwise, start with the bookmarks tab.
1347 */
1348 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001349 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001350 if (mTabControl.getCurrentWebView() == null) {
1351 return;
1352 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001353 // clear action mode
1354 if (isInCustomActionMode()) {
1355 endActionMode();
1356 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001357 Bundle extras = new Bundle();
1358 // Disable opening in a new window if we have maxed out the windows
1359 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1360 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001361 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001362 }
1363
1364 // combo view callbacks
1365
Michael Kolb8233fac2010-10-26 16:08:53 -07001366 // key handling
1367 protected void onBackKey() {
1368 if (!mUi.onBackKey()) {
1369 WebView subwindow = mTabControl.getCurrentSubWindow();
1370 if (subwindow != null) {
1371 if (subwindow.canGoBack()) {
1372 subwindow.goBack();
1373 } else {
1374 dismissSubWindow(mTabControl.getCurrentTab());
1375 }
1376 } else {
1377 goBackOnePageOrQuit();
1378 }
1379 }
1380 }
1381
Michael Kolb4bd767d2011-05-27 11:33:55 -07001382 protected boolean onMenuKey() {
1383 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001384 }
1385
Michael Kolb8233fac2010-10-26 16:08:53 -07001386 // menu handling and state
1387 // TODO: maybe put into separate handler
1388
John Reck9c35b9c2012-05-30 10:08:50 -07001389 @Override
1390 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001391 if (mMenuState == EMPTY_MENU) {
1392 return false;
1393 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 MenuInflater inflater = mActivity.getMenuInflater();
1395 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 return true;
1397 }
1398
John Reck9c35b9c2012-05-30 10:08:50 -07001399 @Override
1400 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001402 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001403 return;
1404 }
1405 if (!(v instanceof WebView)) {
1406 return;
1407 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001408 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 WebView.HitTestResult result = webview.getHitTestResult();
1410 if (result == null) {
1411 return;
1412 }
1413
1414 int type = result.getType();
1415 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1416 Log.w(LOGTAG,
1417 "We should not show context menu when nothing is touched");
1418 return;
1419 }
1420 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1421 // let TextView handles context menu
1422 return;
1423 }
1424
1425 // Note, http://b/issue?id=1106666 is requesting that
1426 // an inflated menu can be used again. This is not available
1427 // yet, so inflate each time (yuk!)
1428 MenuInflater inflater = mActivity.getMenuInflater();
1429 inflater.inflate(R.menu.browsercontext, menu);
1430
1431 // Show the correct menu group
1432 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001433 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001434 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 menu.setGroupVisible(R.id.PHONE_MENU,
1436 type == WebView.HitTestResult.PHONE_TYPE);
1437 menu.setGroupVisible(R.id.EMAIL_MENU,
1438 type == WebView.HitTestResult.EMAIL_TYPE);
1439 menu.setGroupVisible(R.id.GEO_MENU,
1440 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001441 String itemUrl = null;
1442 String url = webview.getOriginalUrl();
1443 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1444 itemUrl = Uri.decode(navigationUrl);
1445 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1446 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1447 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1448 } else {
1449 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1450 }
1451 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1452 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1453 } else {
1454 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1455
1456 menu.setGroupVisible(R.id.IMAGE_MENU,
1457 type == WebView.HitTestResult.IMAGE_TYPE
1458 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1459 menu.setGroupVisible(R.id.ANCHOR_MENU,
1460 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1461 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001462 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1463 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001464 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001465 // Setup custom handling depending on the type
1466 switch (type) {
1467 case WebView.HitTestResult.PHONE_TYPE:
1468 menu.setHeaderTitle(Uri.decode(extra));
1469 menu.findItem(R.id.dial_context_menu_id).setIntent(
1470 new Intent(Intent.ACTION_VIEW, Uri
1471 .parse(WebView.SCHEME_TEL + extra)));
1472 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1473 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1474 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1475 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1476 addIntent);
1477 menu.findItem(R.id.copy_phone_context_menu_id)
1478 .setOnMenuItemClickListener(
1479 new Copy(extra));
1480 break;
1481
1482 case WebView.HitTestResult.EMAIL_TYPE:
1483 menu.setHeaderTitle(extra);
1484 menu.findItem(R.id.email_context_menu_id).setIntent(
1485 new Intent(Intent.ACTION_VIEW, Uri
1486 .parse(WebView.SCHEME_MAILTO + extra)));
1487 menu.findItem(R.id.copy_mail_context_menu_id)
1488 .setOnMenuItemClickListener(
1489 new Copy(extra));
1490 break;
1491
1492 case WebView.HitTestResult.GEO_TYPE:
1493 menu.setHeaderTitle(extra);
1494 menu.findItem(R.id.map_context_menu_id).setIntent(
1495 new Intent(Intent.ACTION_VIEW, Uri
1496 .parse(WebView.SCHEME_GEO
1497 + URLEncoder.encode(extra))));
1498 menu.findItem(R.id.copy_geo_context_menu_id)
1499 .setOnMenuItemClickListener(
1500 new Copy(extra));
1501 break;
1502
1503 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1504 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001505 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001506 // decide whether to show the open link in new tab option
1507 boolean showNewTab = mTabControl.canCreateNewTab();
1508 MenuItem newTabItem
1509 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001510 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001511 ? R.string.contextmenu_openlink_newwindow_background
1512 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001513 newTabItem.setVisible(showNewTab);
1514 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001515 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1516 newTabItem.setOnMenuItemClickListener(
1517 new MenuItem.OnMenuItemClickListener() {
1518 @Override
1519 public boolean onMenuItemClick(MenuItem item) {
1520 final HashMap<String, WebView> hrefMap =
1521 new HashMap<String, WebView>();
1522 hrefMap.put("webview", webview);
1523 final Message msg = mHandler.obtainMessage(
1524 FOCUS_NODE_HREF,
1525 R.id.open_newtab_context_menu_id,
1526 0, hrefMap);
1527 webview.requestFocusNodeHref(msg);
1528 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001529 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001530 });
1531 } else {
1532 newTabItem.setOnMenuItemClickListener(
1533 new MenuItem.OnMenuItemClickListener() {
1534 @Override
1535 public boolean onMenuItemClick(MenuItem item) {
1536 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001537 openTab(extra, parent,
1538 !mSettings.openInBackground(),
1539 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001540 return true;
1541 }
1542 });
1543 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001544 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001545 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1546 menu.setHeaderTitle(navigationUrl);
1547 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1548
1549 if (itemUrl != null) {
1550 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1551 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1552 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1553 @Override
1554 public boolean onMenuItemClick(MenuItem item) {
1555 final Intent intent = new Intent(Controller.this
1556 .getContext(),
1557 AddMyNavigationPage.class);
1558 Bundle bundle = new Bundle();
1559 String url = Uri.decode(navigationUrl);
1560 bundle.putBoolean("isAdding", false);
1561 bundle.putString("url", url);
1562 bundle.putString("name", getNameFromUrl(url));
1563 intent.putExtra("websites", bundle);
1564 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1565 return false;
1566 }
1567 });
1568 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1569 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1570 @Override
1571 public boolean onMenuItemClick(MenuItem item) {
1572 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1573 return false;
1574 }
1575 });
1576 }
1577 } else {
1578 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1579 }
1580 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1582 break;
1583 }
1584 // otherwise fall through to handle image part
1585 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001586 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1587 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001588 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1589 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001590 shareItem.setOnMenuItemClickListener(
1591 new MenuItem.OnMenuItemClickListener() {
1592 @Override
1593 public boolean onMenuItemClick(MenuItem item) {
1594 sharePage(mActivity, null, extra, null,
1595 null);
1596 return true;
1597 }
1598 }
1599 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001601 menu.findItem(R.id.view_image_context_menu_id)
1602 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1603 @Override
1604 public boolean onMenuItemClick(MenuItem item) {
1605 openTab(extra, mTabControl.getCurrentTab(), true, true);
1606 return false;
1607 }
1608 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001609 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1610 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1611 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001612 menu.findItem(R.id.set_wallpaper_context_menu_id).
1613 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1614 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001615 break;
1616
1617 default:
1618 Log.w(LOGTAG, "We should not get here.");
1619 break;
1620 }
1621 //update the ui
1622 mUi.onContextMenuCreated(menu);
1623 }
1624
kaiyiz6e5b3e02013-08-19 20:02:01 +08001625 public void startAddMyNavigation(String url) {
1626 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1627 Bundle bundle = new Bundle();
1628 bundle.putBoolean("isAdding", true);
1629 bundle.putString("url", url);
1630 bundle.putString("name", getNameFromUrl(url));
1631 intent.putExtra("websites", bundle);
1632 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1633 }
1634
1635 private void showMyNavigationDeleteDialog(final String itemUrl) {
1636 new AlertDialog.Builder(this.getContext())
1637 .setTitle(R.string.my_navigation_delete_label)
1638 .setIcon(android.R.drawable.ic_dialog_alert)
1639 .setMessage(R.string.my_navigation_delete_msg)
1640 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1641 @Override
1642 public void onClick(DialogInterface dialog, int whichButton) {
1643 deleteMyNavigationItem(itemUrl);
1644 }
1645 })
1646 .setNegativeButton(R.string.cancel, null)
1647 .show();
1648 }
1649
1650 private void deleteMyNavigationItem(final String itemUrl) {
1651 ContentResolver cr = this.getContext().getContentResolver();
1652 Cursor cursor = null;
1653
1654 try {
1655 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1656 new String[] {
1657 MyNavigationUtil.ID
1658 }, "url = ?", new String[] {
1659 itemUrl
1660 }, null);
1661 if (null != cursor && cursor.moveToFirst()) {
1662 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1663 cursor.getLong(0));
1664
1665 ContentValues values = new ContentValues();
1666 values.put(MyNavigationUtil.TITLE, "");
1667 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1668 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1669 ByteArrayOutputStream os = new ByteArrayOutputStream();
1670 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1671 R.raw.my_navigation_add);
1672 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1673 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1674 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1675 cr.update(uri, values, null, null);
1676 } else {
1677 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1678 }
1679 } catch (IllegalStateException e) {
1680 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1681 } finally {
1682 if (null != cursor) {
1683 cursor.close();
1684 }
1685 }
1686
1687 if (getCurrentTopWebView() != null) {
1688 getCurrentTopWebView().reload();
1689 }
1690 }
1691
1692 private String getNameFromUrl(String itemUrl) {
1693 ContentResolver cr = this.getContext().getContentResolver();
1694 Cursor cursor = null;
1695 String name = null;
1696
1697 try {
1698 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1699 new String[] {
1700 MyNavigationUtil.TITLE
1701 }, "url = ?", new String[] {
1702 itemUrl
1703 }, null);
1704 if (null != cursor && cursor.moveToFirst()) {
1705 name = cursor.getString(0);
1706 } else {
1707 Log.e(LOGTAG, "this item does not exist!");
1708 }
1709 } catch (IllegalStateException e) {
1710 Log.e(LOGTAG, "getNameFromUrl", e);
1711 } finally {
1712 if (null != cursor) {
1713 cursor.close();
1714 }
1715 }
1716 return name;
1717 }
1718
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001719 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
1720 if (bitmap == null) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001721 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1722 return;
1723 }
1724
1725 final ContentResolver cr = mActivity.getContentResolver();
1726 new AsyncTask<Void, Void, Void>() {
1727 @Override
1728 protected Void doInBackground(Void... unused) {
1729 ContentResolver cr = mActivity.getContentResolver();
1730 Cursor cursor = null;
1731 try {
1732 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1733 new String[] {
1734 MyNavigationUtil.ID
1735 }, "url = ?", new String[] {
1736 itemUrl
1737 }, null);
1738 if (null != cursor && cursor.moveToFirst()) {
1739 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001740 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001741
1742 ContentValues values = new ContentValues();
1743 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1744 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1745 cursor.getLong(0));
1746 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1747 cr.update(uri, values, null, null);
1748 os.close();
1749 }
1750 } catch (IllegalStateException e) {
1751 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1752 } catch (IOException e) {
1753 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1754 } finally {
1755 if (null != cursor) {
1756 cursor.close();
1757 }
1758 }
1759 return null;
1760 }
1761 }.execute();
1762 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001763 /**
1764 * As the menu can be open when loading state changes
1765 * we must manually update the state of the stop/reload menu
1766 * item
1767 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001768 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001769 if (menu == null) {
1770 return;
1771 }
1772 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001773 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001774 menu.findItem(R.id.stop_menu_id):
1775 menu.findItem(R.id.reload_menu_id);
1776 if (src != null) {
1777 dest.setIcon(src.getIcon());
1778 dest.setTitle(src.getTitle());
1779 }
1780 }
1781
John Reck9c35b9c2012-05-30 10:08:50 -07001782 @Override
1783 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001784 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001785 // hold on to the menu reference here; it is used by the page callbacks
1786 // to update the menu based on loading state
1787 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001788 // Note: setVisible will decide whether an item is visible; while
1789 // setEnabled() will decide whether an item is enabled, which also means
1790 // whether the matching shortcut key will function.
1791 switch (mMenuState) {
1792 case EMPTY_MENU:
1793 if (mCurrentMenuState != mMenuState) {
1794 menu.setGroupVisible(R.id.MAIN_MENU, false);
1795 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1796 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1797 }
1798 break;
1799 default:
1800 if (mCurrentMenuState != mMenuState) {
1801 menu.setGroupVisible(R.id.MAIN_MENU, true);
1802 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1803 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1804 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001805 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001806 break;
1807 }
1808 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001809 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001810 }
1811
Michael Kolb4bf79712011-07-14 14:18:12 -07001812 @Override
1813 public void updateMenuState(Tab tab, Menu menu) {
1814 boolean canGoBack = false;
1815 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001816 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001817 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001818 // Following flag is used to identify schemes for which the LIVE_MENU
1819 // items defined in res/menu/browser.xml should be enabled
1820 boolean isLiveScheme = false;
1821 boolean isPageFinished = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001822 if (tab != null) {
1823 canGoBack = tab.canGoBack();
1824 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001825 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001826 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001827 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
1828 isPageFinished = tab.getPageFinishedStatus();
Michael Kolb4bf79712011-07-14 14:18:12 -07001829 }
1830 final MenuItem back = menu.findItem(R.id.back_menu_id);
1831 back.setEnabled(canGoBack);
1832
1833 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001834
1835 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1836 forward.setEnabled(canGoForward);
1837
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001838 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1839 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001840 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001841 if (source != null && dest != null) {
1842 dest.setTitle(source.getTitle());
1843 dest.setIcon(source.getIcon());
1844 }
John Recke1a03a32011-09-14 17:04:16 -07001845 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001846
1847 // decide whether to show the share link option
1848 PackageManager pm = mActivity.getPackageManager();
1849 Intent send = new Intent(Intent.ACTION_SEND);
1850 send.setType("text/plain");
1851 ResolveInfo ri = pm.resolveActivity(send,
1852 PackageManager.MATCH_DEFAULT_ONLY);
1853 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1854
1855 boolean isNavDump = mSettings.enableNavDump();
1856 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1857 nav.setVisible(isNavDump);
1858 nav.setEnabled(isNavDump);
1859
1860 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001861 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1862 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001863 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001864 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001865 menu.setGroupEnabled(R.id.OFFLINE_READING, isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001866 // history and snapshots item are the members of COMBO menu group,
1867 // so if show history item, only make snapshots item invisible.
1868 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001869
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001870 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001871 }
1872
John Reck9c35b9c2012-05-30 10:08:50 -07001873 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001874 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001875 if (null == getCurrentTopWebView()) {
1876 return false;
1877 }
1878 if (mMenuIsDown) {
1879 // The shortcut action consumes the MENU. Even if it is still down,
1880 // it won't trigger the next shortcut action. In the case of the
1881 // shortcut action triggering a new activity, like Bookmarks, we
1882 // won't get onKeyUp for MENU. So it is important to reset it here.
1883 mMenuIsDown = false;
1884 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001885 if (mUi.onOptionsItemSelected(item)) {
1886 // ui callback handled it
1887 return true;
1888 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001889 switch (item.getItemId()) {
1890 // -- Main menu
1891 case R.id.new_tab_menu_id:
1892 openTabToHomePage();
1893 break;
1894
1895 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001896 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 break;
1898
Afzal Najamd4e33312012-04-26 01:54:01 -04001899 case R.id.close_other_tabs_id:
1900 closeOtherTabs();
1901 break;
1902
Michael Kolb8233fac2010-10-26 16:08:53 -07001903 case R.id.goto_menu_id:
1904 editUrl();
1905 break;
1906
1907 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001908 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1909 break;
1910
1911 case R.id.history_menu_id:
1912 bookmarksOrHistoryPicker(ComboViews.History);
1913 break;
1914
1915 case R.id.snapshots_menu_id:
1916 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001917 break;
1918
Michael Kolb8233fac2010-10-26 16:08:53 -07001919 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001920 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001921 break;
1922
1923 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001924 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001925 stopLoading();
1926 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001927 Tab currentTab = mTabControl.getCurrentTab();
1928 if (currentTab.hasCrashed) {
1929 currentTab.replaceCrashView(getCurrentTopWebView(),
1930 currentTab.getViewContainer());
1931 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001932 getCurrentTopWebView().reload();
1933 }
1934 break;
1935
1936 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001937 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 break;
1939
1940 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001941 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001942 break;
1943
1944 case R.id.close_menu_id:
1945 // Close the subwindow if it exists.
1946 if (mTabControl.getCurrentSubWindow() != null) {
1947 dismissSubWindow(mTabControl.getCurrentTab());
1948 break;
1949 }
1950 closeCurrentTab();
1951 break;
1952
kaiyiza8b6dbb2013-07-29 18:11:22 +08001953 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001954 Object[] params = { new String("persist.debug.browsermonkeytest")};
1955 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001956 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001957 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001958 if (ret != null && ret.equals("enable"))
1959 break;
luxiaolb40014b2013-07-19 10:01:43 +08001960 showExitDialog(mActivity);
1961 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001962 case R.id.homepage_menu_id:
1963 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001964 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001965 break;
1966
1967 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001968 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001969 break;
1970
1971 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001972 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001973 break;
1974
John Reck2bc80422011-06-30 15:11:49 -07001975 case R.id.save_snapshot_menu_id:
1976 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001977 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001978 createScreenshotAsync(
1979 source.getWebView(),
1980 getDesiredThumbnailWidth(mActivity),
1981 getDesiredThumbnailHeight(mActivity),
1982 new ValueCallback<Bitmap>() {
1983 @Override
1984 public void onReceiveValue(Bitmap bitmap) {
1985 new SaveSnapshotTask(source, bitmap).execute();
1986 }
1987 });
Leon Scrogginsac993842011-02-02 12:54:07 -05001988 break;
1989
Michael Kolb8233fac2010-10-26 16:08:53 -07001990 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001991 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 break;
1993
John Recke1a03a32011-09-14 17:04:16 -07001994 case R.id.snapshot_go_live:
1995 goLive();
1996 return true;
1997
Michael Kolb8233fac2010-10-26 16:08:53 -07001998 case R.id.share_page_menu_id:
1999 Tab currentTab = mTabControl.getCurrentTab();
2000 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002001 return false;
2002 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002003 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002004 break;
2005
2006 case R.id.dump_nav_menu_id:
2007 getCurrentTopWebView().debugDump();
2008 break;
2009
Michael Kolb8233fac2010-10-26 16:08:53 -07002010 case R.id.zoom_in_menu_id:
2011 getCurrentTopWebView().zoomIn();
2012 break;
2013
2014 case R.id.zoom_out_menu_id:
2015 getCurrentTopWebView().zoomOut();
2016 break;
2017
2018 case R.id.view_downloads_menu_id:
2019 viewDownloads();
2020 break;
2021
John Reck42229bc2011-08-19 13:26:43 -07002022 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002023 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002024 break;
2025
Michael Kolb8233fac2010-10-26 16:08:53 -07002026 case R.id.window_one_menu_id:
2027 case R.id.window_two_menu_id:
2028 case R.id.window_three_menu_id:
2029 case R.id.window_four_menu_id:
2030 case R.id.window_five_menu_id:
2031 case R.id.window_six_menu_id:
2032 case R.id.window_seven_menu_id:
2033 case R.id.window_eight_menu_id:
2034 {
2035 int menuid = item.getItemId();
2036 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2037 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2038 Tab desiredTab = mTabControl.getTab(id);
2039 if (desiredTab != null &&
2040 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002041 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 }
2043 break;
2044 }
2045 }
2046 }
2047 break;
2048
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002049 case R.id.about_menu_id:
2050 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2051 builder.setTitle(R.string.about);
2052 builder.setCancelable(true);
2053 String ua = "";
2054 final WebView currentWebView = getCurrentWebView();
2055 if (currentWebView != null) {
2056 final WebSettings s = currentWebView.getSettings();
2057 if (s != null) {
2058 ua = s.getUserAgentString();
2059 }
2060 }
2061 builder.setMessage("Agent:" + ua);
2062 builder.setPositiveButton(android.R.string.ok, null);
2063 builder.create().show();
2064 break;
2065
Michael Kolb8233fac2010-10-26 16:08:53 -07002066 default:
2067 return false;
2068 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 return true;
2070 }
2071
John Reck68234a92012-04-19 15:27:12 -07002072 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2073 implements OnCancelListener {
2074
2075 private Tab mTab;
2076 private Dialog mProgressDialog;
2077 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002078 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002079
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002080 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002081 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002082 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002083 }
2084
2085 @Override
2086 protected void onPreExecute() {
2087 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2088 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2089 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002090 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002091 }
2092
2093 @Override
2094 protected Long doInBackground(Void... params) {
2095 if (!mTab.saveViewState(mValues)) {
2096 return null;
2097 }
2098 if (isCancelled()) {
2099 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2100 File file = mActivity.getFileStreamPath(path);
2101 if (!file.delete()) {
2102 file.deleteOnExit();
2103 }
2104 return null;
2105 }
2106 final ContentResolver cr = mActivity.getContentResolver();
2107 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2108 if (result == null) {
2109 return null;
2110 }
2111 long id = ContentUris.parseId(result);
2112 return id;
2113 }
2114
2115 @Override
2116 protected void onPostExecute(Long id) {
2117 if (isCancelled()) {
2118 return;
2119 }
2120 mProgressDialog.dismiss();
2121 if (id == null) {
2122 Toast.makeText(mActivity, R.string.snapshot_failed,
2123 Toast.LENGTH_SHORT).show();
2124 return;
2125 }
2126 Bundle b = new Bundle();
2127 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2128 mUi.showComboView(ComboViews.Snapshots, b);
2129 }
2130
2131 @Override
2132 public void onCancel(DialogInterface dialog) {
2133 cancel(true);
2134 }
2135 }
2136
Michael Kolb80f75082012-04-10 10:50:06 -07002137 @Override
2138 public void toggleUserAgent() {
2139 WebView web = getCurrentWebView();
2140 mSettings.toggleDesktopUseragent(web);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002141 web.reload();
Michael Kolb80f75082012-04-10 10:50:06 -07002142 }
2143
2144 @Override
2145 public void findOnPage() {
2146 getCurrentTopWebView().showFindDialog(null, true);
2147 }
2148
2149 @Override
2150 public void openPreferences() {
2151 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2152 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2153 getCurrentTopWebView().getUrl());
2154 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2155 }
2156
2157 @Override
2158 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002159 WebView w = getCurrentTopWebView();
2160 if (w == null)
2161 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002162 final Intent i = createBookmarkCurrentPageIntent(false);
2163 createScreenshotAsync(
2164 w, getDesiredThumbnailWidth(mActivity),
2165 getDesiredThumbnailHeight(mActivity),
2166 new ValueCallback<Bitmap>() {
2167 @Override
2168 public void onReceiveValue(Bitmap bitmap) {
2169 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2170 mActivity.startActivity(i);
2171 }
2172 });
Michael Kolb80f75082012-04-10 10:50:06 -07002173 }
2174
John Recke1a03a32011-09-14 17:04:16 -07002175 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002176 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002177 String url = t.getLiveUrl();
2178 // destroy the old snapshot tab
2179 closeCurrentTab();
2180 Tab liveTab = createNewTab(false, true, false);
2181 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002182 }
2183
luxiaolb40014b2013-07-19 10:01:43 +08002184 private void showExitDialog(final Activity activity) {
2185 new AlertDialog.Builder(activity)
2186 .setTitle(R.string.exit_browser_title)
2187 .setIcon(android.R.drawable.ic_dialog_alert)
2188 .setMessage(R.string.exit_browser_msg)
2189 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2190 public void onClick(DialogInterface dialog, int which) {
2191 activity.moveTaskToBack(true);
2192 dialog.dismiss();
2193 }
2194 })
2195 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2196 public void onClick(DialogInterface dialog, int which) {
2197 activity.finish();
2198 mHandler.postDelayed(new Runnable() {
2199 @Override
2200 public void run() {
2201 // TODO Auto-generated method stub
2202 mCrashRecoveryHandler.clearState(true);
2203 int pid = android.os.Process.myPid();
2204 android.os.Process.killProcess(pid);
2205 }
2206 }, 300);
2207 dialog.dismiss();
2208 }
2209 })
2210 .show();
2211 }
Michael Kolb315d5022011-10-13 12:47:11 -07002212 @Override
2213 public void showPageInfo() {
2214 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2215 }
2216
John Reck9c35b9c2012-05-30 10:08:50 -07002217 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002218 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002219 // Let the History and Bookmark fragments handle menus they created.
2220 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2221 return false;
2222 }
2223
Michael Kolb8233fac2010-10-26 16:08:53 -07002224 int id = item.getItemId();
2225 boolean result = true;
2226 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002227 // -- Browser context menu
2228 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002229 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002230 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002231 case R.id.copy_link_context_menu_id:
2232 final WebView webView = getCurrentTopWebView();
2233 if (null == webView) {
2234 result = false;
2235 break;
2236 }
2237 final HashMap<String, WebView> hrefMap =
2238 new HashMap<String, WebView>();
2239 hrefMap.put("webview", webView);
2240 final Message msg = mHandler.obtainMessage(
2241 FOCUS_NODE_HREF, id, 0, hrefMap);
2242 webView.requestFocusNodeHref(msg);
2243 break;
2244
2245 default:
2246 // For other context menus
2247 result = onOptionsItemSelected(item);
2248 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002249 return result;
2250 }
2251
2252 /**
2253 * support programmatically opening the context menu
2254 */
2255 public void openContextMenu(View view) {
2256 mActivity.openContextMenu(view);
2257 }
2258
2259 /**
2260 * programmatically open the options menu
2261 */
2262 public void openOptionsMenu() {
2263 mActivity.openOptionsMenu();
2264 }
2265
John Reck9c35b9c2012-05-30 10:08:50 -07002266 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002267 public boolean onMenuOpened(int featureId, Menu menu) {
2268 if (mOptionsMenuOpen) {
2269 if (mConfigChanged) {
2270 // We do not need to make any changes to the state of the
2271 // title bar, since the only thing that happened was a
2272 // change in orientation
2273 mConfigChanged = false;
2274 } else {
2275 if (!mExtendedMenuOpen) {
2276 mExtendedMenuOpen = true;
2277 mUi.onExtendedMenuOpened();
2278 } else {
2279 // Switching the menu back to icon view, so show the
2280 // title bar once again.
2281 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002282 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002283 }
2284 }
2285 } else {
2286 // The options menu is closed, so open it, and show the title
2287 mOptionsMenuOpen = true;
2288 mConfigChanged = false;
2289 mExtendedMenuOpen = false;
2290 mUi.onOptionsMenuOpened();
2291 }
2292 return true;
2293 }
2294
John Reck9c35b9c2012-05-30 10:08:50 -07002295 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002296 public void onOptionsMenuClosed(Menu menu) {
2297 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002298 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002299 }
2300
John Reck9c35b9c2012-05-30 10:08:50 -07002301 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002303 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002304 }
2305
2306 // Helper method for getting the top window.
2307 @Override
2308 public WebView getCurrentTopWebView() {
2309 return mTabControl.getCurrentTopWebView();
2310 }
2311
2312 @Override
2313 public WebView getCurrentWebView() {
2314 return mTabControl.getCurrentWebView();
2315 }
2316
2317 /*
2318 * This method is called as a result of the user selecting the options
2319 * menu to see the download window. It shows the download window on top of
2320 * the current window.
2321 */
2322 void viewDownloads() {
2323 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2324 mActivity.startActivity(intent);
2325 }
2326
John Reck30b065e2011-07-19 10:58:05 -07002327 int getActionModeHeight() {
2328 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2329 new int[] { android.R.attr.actionBarSize });
2330 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2331 actionBarSizeTypedArray.recycle();
2332 return size;
2333 }
2334
Michael Kolb8233fac2010-10-26 16:08:53 -07002335 // action mode
2336
John Reck9c35b9c2012-05-30 10:08:50 -07002337 @Override
2338 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002339 mUi.onActionModeStarted(mode);
2340 mActionMode = mode;
2341 }
2342
2343 /*
2344 * True if a custom ActionMode (i.e. find or select) is in use.
2345 */
2346 @Override
2347 public boolean isInCustomActionMode() {
2348 return mActionMode != null;
2349 }
2350
2351 /*
2352 * End the current ActionMode.
2353 */
2354 @Override
2355 public void endActionMode() {
2356 if (mActionMode != null) {
2357 mActionMode.finish();
2358 }
2359 }
2360
2361 /*
2362 * Called by find and select when they are finished. Replace title bars
2363 * as necessary.
2364 */
John Reck9c35b9c2012-05-30 10:08:50 -07002365 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002366 public void onActionModeFinished(ActionMode mode) {
2367 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002368 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002369 mActionMode = null;
2370 }
2371
2372 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002373 final Tab tab = getCurrentTab();
2374 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002375 }
2376
2377 // bookmark handling
2378
2379 /**
2380 * add the current page as a bookmark to the given folder id
2381 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002382 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002383 * bookmarked, and if it is, edit that bookmark. If false, and
2384 * the site is already bookmarked, do not attempt to edit the
2385 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002386 */
2387 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002388 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002389 WebView w = getCurrentTopWebView();
2390 if (w == null) {
2391 return null;
2392 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002393 Intent i = new Intent(mActivity,
2394 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002395 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2396 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2397 String touchIconUrl = w.getTouchIconUrl();
2398 if (touchIconUrl != null) {
2399 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2400 WebSettings settings = w.getSettings();
2401 if (settings != null) {
2402 i.putExtra(AddBookmarkPage.USER_AGENT,
2403 settings.getUserAgentString());
2404 }
2405 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002406 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002407 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002408 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002409 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2410 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002411 // Put the dialog at the upper right of the screen, covering the
2412 // star on the title bar.
2413 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002414 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002415 }
2416
2417 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002418 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002419 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002420 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002421 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 }
2423
Vivek Sekharb54614f2014-05-01 19:03:37 -07002424 @Override
2425 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2426 boolean capture) {
2427 mUploadHandler = new UploadHandler(this);
2428 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2429 }
2430
Michael Kolb8233fac2010-10-26 16:08:53 -07002431 // thumbnails
2432
2433 /**
2434 * Return the desired width for thumbnail screenshots, which are stored in
2435 * the database, and used on the bookmarks screen.
2436 * @param context Context for finding out the density of the screen.
2437 * @return desired width for thumbnail screenshot.
2438 */
2439 static int getDesiredThumbnailWidth(Context context) {
2440 return context.getResources().getDimensionPixelOffset(
2441 R.dimen.bookmarkThumbnailWidth);
2442 }
2443
2444 /**
2445 * Return the desired height for thumbnail screenshots, which are stored in
2446 * the database, and used on the bookmarks screen.
2447 * @param context Context for finding out the density of the screen.
2448 * @return desired height for thumbnail screenshot.
2449 */
2450 static int getDesiredThumbnailHeight(Context context) {
2451 return context.getResources().getDimensionPixelOffset(
2452 R.dimen.bookmarkThumbnailHeight);
2453 }
2454
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002455 static void createScreenshotAsync(WebView view, int width, int height,
2456 final ValueCallback<Bitmap> cb) {
2457 if (view == null || width == 0 || height == 0) {
2458 return;
2459 }
2460
2461 view.getContentBitmapAsync(
2462 (float) width / view.getWidth(),
2463 new Rect(),
2464 new ValueCallback<Bitmap>() {
2465 @Override
2466 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002467 if (bitmap != null)
2468 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2469 cb.onReceiveValue(bitmap);
2470 }});
2471 }
2472
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002473 private void updateScreenshot(final Tab tab) {
2474 createScreenshotAsync(
2475 tab.getWebView(),
2476 getDesiredThumbnailWidth(mActivity),
2477 getDesiredThumbnailHeight(mActivity),
2478 new ValueCallback<Bitmap>() {
2479 @Override
2480 public void onReceiveValue(Bitmap bitmap) {
2481 updateScreenshot(tab, bitmap);
2482 }
2483 });
2484 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002485
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002486 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002487 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002488 // FIXME: Would like to make sure there is actually something to
2489 // draw, but the API for that (WebViewCore.pictureReady()) is not
2490 // currently accessible here.
2491
John Reck34ef2672011-02-10 11:30:55 -08002492 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002493 if (view == null) {
2494 // Tab was destroyed
2495 return;
2496 }
John Reck34ef2672011-02-10 11:30:55 -08002497 final String url = tab.getUrl();
2498 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002499 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002500 if (TextUtils.isEmpty(url)) {
2501 return;
2502 }
2503
kaiyiz6e5b3e02013-08-19 20:02:01 +08002504 //update My Navigation Thumbnails
2505 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2506 if (isMyNavigationUrl) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002507 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002508 }
John Reck34ef2672011-02-10 11:30:55 -08002509 // Only update thumbnails for web urls (http(s)://), not for
2510 // about:, javascript:, data:, etc...
2511 // Unless it is a bookmarked site, then always update
2512 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2513 return;
2514 }
2515
kaiyiz6e5b3e02013-08-19 20:02:01 +08002516 if (url != null && mUpdateMyNavThumbnailUrl != null
2517 && Patterns.WEB_URL.matcher(url).matches()
2518 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2519 String urlHost = (new WebAddress(url)).getHost();
2520 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2521 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2522 || bookmarkHost.length() == 0) {
2523 return;
2524 }
2525 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2526 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2527 bookmarkHost.length());
2528 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2529 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2530 if (!bookmarkDomain.equals(urlDomain)) {
2531 return;
2532 }
2533 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002534 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002535 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2536 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2537 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2538 500);
2539 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002540 return;
2541 }
2542
2543 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002544 new AsyncTask<Void, Void, Void>() {
2545 @Override
2546 protected Void doInBackground(Void... unused) {
2547 Cursor cursor = null;
2548 try {
2549 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002550 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2551 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2552 : url);
2553 if (mUpdateMyNavThumbnail) {
2554 mUpdateMyNavThumbnail = false;
2555 mUpdateMyNavThumbnailUrl = null;
2556 }
John Reck34ef2672011-02-10 11:30:55 -08002557 if (cursor != null && cursor.moveToFirst()) {
2558 final ByteArrayOutputStream os =
2559 new ByteArrayOutputStream();
2560 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002561
John Reck34ef2672011-02-10 11:30:55 -08002562 ContentValues values = new ContentValues();
2563 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002564
John Reck34ef2672011-02-10 11:30:55 -08002565 do {
John Reck617fd832011-02-16 14:35:59 -08002566 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002567 cr.update(Images.CONTENT_URI, values, null, null);
2568 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002569 }
John Reck34ef2672011-02-10 11:30:55 -08002570 } catch (IllegalStateException e) {
2571 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002572 } catch (SQLiteException s) {
2573 // Added for possible error when user tries to remove the same bookmark
2574 // that is being updated with a screen shot
2575 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002576 } finally {
2577 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 }
John Reck34ef2672011-02-10 11:30:55 -08002579 return null;
2580 }
2581 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002582 }
2583
2584 private class Copy implements OnMenuItemClickListener {
2585 private CharSequence mText;
2586
John Reck9c35b9c2012-05-30 10:08:50 -07002587 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 public boolean onMenuItemClick(MenuItem item) {
2589 copy(mText);
2590 return true;
2591 }
2592
2593 public Copy(CharSequence toCopy) {
2594 mText = toCopy;
2595 }
2596 }
2597
Leon Scroggins63c02662010-11-18 15:16:27 -05002598 private static class Download implements OnMenuItemClickListener {
2599 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002600 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002601 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002602 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002603 private static final String FALLBACK_EXTENSION = "dat";
2604 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002605
John Reck9c35b9c2012-05-30 10:08:50 -07002606 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002607 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002608 if (DataUri.isDataUri(mText)) {
2609 saveDataUri();
2610 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002611 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002612 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002613 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002614 return true;
2615 }
2616
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002617 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2618 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002619 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002620 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002621 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002622 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002623 }
George Mount387d45d2011-10-07 15:57:53 -07002624
2625 /**
2626 * Treats mText as a data URI and writes its contents to a file
2627 * based on the current time.
2628 */
2629 private void saveDataUri() {
2630 FileOutputStream outputStream = null;
2631 try {
2632 DataUri uri = new DataUri(mText);
2633 File target = getTarget(uri);
2634 outputStream = new FileOutputStream(target);
2635 outputStream.write(uri.getData());
2636 final DownloadManager manager =
2637 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2638 manager.addCompletedDownload(target.getName(),
2639 mActivity.getTitle().toString(), false,
2640 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002641 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002642 } catch (IOException e) {
2643 Log.e(LOGTAG, "Could not save data URL");
2644 } finally {
2645 if (outputStream != null) {
2646 try {
2647 outputStream.close();
2648 } catch (IOException e) {
2649 // ignore close errors
2650 }
2651 }
2652 }
2653 }
2654
2655 /**
2656 * Creates a File based on the current time stamp and uses
2657 * the mime type of the DataUri to get the extension.
2658 */
2659 private File getTarget(DataUri uri) throws IOException {
2660 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002661 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002662 String nameBase = format.format(new Date());
2663 String mimeType = uri.getMimeType();
2664 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2665 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2666 if (extension == null) {
2667 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2668 extension = FALLBACK_EXTENSION;
2669 }
2670 extension = "." + extension; // createTempFile needs the '.'
2671 File targetFile = File.createTempFile(nameBase, extension, dir);
2672 return targetFile;
2673 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002674 }
2675
Cary Clark8974d282010-11-22 10:46:05 -05002676 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002677 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002678
John Reck9c35b9c2012-05-30 10:08:50 -07002679 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002680 public boolean onMenuItemClick(MenuItem item) {
2681 if (mWebView != null) {
2682 return mWebView.selectText();
2683 }
2684 return false;
2685 }
2686
2687 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002688 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002689 }
2690
2691 }
2692
Michael Kolb8233fac2010-10-26 16:08:53 -07002693 /********************** TODO: UI stuff *****************************/
2694
2695 // these methods have been copied, they still need to be cleaned up
2696
2697 /****************** tabs ***************************************************/
2698
2699 // basic tab interactions:
2700
2701 // it is assumed that tabcontrol already knows about the tab
2702 protected void addTab(Tab tab) {
2703 mUi.addTab(tab);
2704 }
2705
2706 protected void removeTab(Tab tab) {
2707 mUi.removeTab(tab);
2708 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002709 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002710 }
2711
Michael Kolbf2055602011-04-09 17:20:03 -07002712 @Override
2713 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002714 // monkey protection against delayed start
2715 if (tab != null) {
2716 mTabControl.setCurrentTab(tab);
2717 // the tab is guaranteed to have a webview after setCurrentTab
2718 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002719 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002720 //Purge active tabs
2721 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002722 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002723 }
2724
John Reck8bcafc12011-08-29 16:43:02 -07002725 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002726 Tab current = mTabControl.getCurrentTab();
2727 if (current != null
2728 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002729 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 }
2731 }
2732
John Reck26b18322011-06-21 13:08:58 -07002733 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002734 // Dismiss the subwindow if applicable.
2735 dismissSubWindow(appTab);
2736 // Since we might kill the WebView, remove it from the
2737 // content view first.
2738 mUi.detachTab(appTab);
2739 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002740 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002741 // TODO: analyze why the remove and add are necessary
2742 mUi.attachTab(appTab);
2743 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002744 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002745 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002746 } else {
2747 // If the tab was the current tab, we have to attach
2748 // it to the view system again.
2749 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002750 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002751 }
2752 }
2753
2754 // Remove the sub window if it exists. Also called by TabControl when the
2755 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002756 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002757 public void dismissSubWindow(Tab tab) {
2758 removeSubWindow(tab);
2759 // dismiss the subwindow. This will destroy the WebView.
2760 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002761 WebView wv = getCurrentTopWebView();
2762 if (wv != null) {
2763 wv.requestFocus();
2764 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002765 }
2766
2767 @Override
2768 public void removeSubWindow(Tab t) {
2769 if (t.getSubWebView() != null) {
2770 mUi.removeSubWindow(t.getSubViewContainer());
2771 }
2772 }
2773
2774 @Override
2775 public void attachSubWindow(Tab tab) {
2776 if (tab.getSubWebView() != null) {
2777 mUi.attachSubWindow(tab.getSubViewContainer());
2778 getCurrentTopWebView().requestFocus();
2779 }
2780 }
2781
Mathew Inwood29721c22011-06-29 17:55:24 +01002782 private Tab showPreloadedTab(final UrlData urlData) {
2783 if (!urlData.isPreloaded()) {
2784 return null;
2785 }
2786 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2787 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2788 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002789 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002790 // Could not submit query. Fallback to regular tab creation
2791 tabControl.destroy();
2792 return null;
2793 }
2794 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002795 // check tab count and make room for new tab
2796 if (!mTabControl.canCreateNewTab()) {
2797 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2798 if (leastUsed != null) {
2799 closeTab(leastUsed);
2800 }
2801 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002802 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002803 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002804 mTabControl.addPreloadedTab(t);
2805 addTab(t);
2806 setActiveTab(t);
2807 return t;
2808 }
2809
Michael Kolb7bcafde2011-05-09 13:55:59 -07002810 // open a non inconito tab with the given url data
2811 // and set as active tab
2812 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002813 Tab tab = showPreloadedTab(urlData);
2814 if (tab == null) {
2815 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002816 if ((tab != null) && !urlData.isEmpty()) {
2817 loadUrlDataIn(tab, urlData);
2818 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002819 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002820 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002821 }
2822
Michael Kolb843510f2010-12-09 10:51:49 -08002823 @Override
2824 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002825 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002826 }
2827
Michael Kolb8233fac2010-10-26 16:08:53 -07002828 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002829 public Tab openIncognitoTab() {
2830 return openTab(INCOGNITO_URI, true, true, false);
2831 }
2832
2833 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002834 public Tab openTab(String url, boolean incognito, boolean setActive,
2835 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002836 return openTab(url, incognito, setActive, useCurrent, null);
2837 }
2838
2839 @Override
2840 public Tab openTab(String url, Tab parent, boolean setActive,
2841 boolean useCurrent) {
2842 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2843 setActive, useCurrent, parent);
2844 }
2845
2846 public Tab openTab(String url, boolean incognito, boolean setActive,
2847 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002848 Tab tab = createNewTab(incognito, setActive, useCurrent);
2849 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002850 if (parent instanceof SnapshotTab) {
2851 addTab(tab);
2852 if (setActive)
2853 setActiveTab(tab);
2854 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002855 parent.addChildTab(tab);
2856 }
Michael Kolb519d2282011-05-09 17:03:19 -07002857 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002858 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002859 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002860 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002861 return tab;
2862 }
2863
2864 // this method will attempt to create a new tab
2865 // incognito: private browsing tab
2866 // setActive: ste tab as current tab
2867 // useCurrent: if no new tab can be created, return current tab
2868 private Tab createNewTab(boolean incognito, boolean setActive,
2869 boolean useCurrent) {
2870 Tab tab = null;
2871 if (mTabControl.canCreateNewTab()) {
2872 tab = mTabControl.createNewTab(incognito);
2873 addTab(tab);
2874 if (setActive) {
2875 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002876 } else {
2877 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002878 }
2879 } else {
2880 if (useCurrent) {
2881 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002882 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002883 } else {
2884 mUi.showMaxTabsWarning();
2885 }
2886 }
2887 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002888 }
2889
John Reck2bc80422011-06-30 15:11:49 -07002890 @Override
2891 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2892 SnapshotTab tab = null;
2893 if (mTabControl.canCreateNewTab()) {
2894 tab = mTabControl.createSnapshotTab(snapshotId);
2895 addTab(tab);
2896 if (setActive) {
2897 setActiveTab(tab);
2898 }
2899 } else {
2900 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002901 }
2902 return tab;
2903 }
2904
Michael Kolb8233fac2010-10-26 16:08:53 -07002905 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002906 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002907 * @return boolean True if we successfully switched to a different tab. If
2908 * the indexth tab is null, or if that tab is the same as
2909 * the current one, return false.
2910 */
2911 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002912 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002913 Tab currentTab = mTabControl.getCurrentTab();
2914 if (tab == null || tab == currentTab) {
2915 return false;
2916 }
2917 setActiveTab(tab);
2918 return true;
2919 }
2920
2921 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002922 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002923 closeCurrentTab(false);
2924 }
2925
2926 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002927 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002928 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002929 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002930 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002931 return;
2932 }
Michael Kolbc831b632011-05-11 09:30:34 -07002933 final Tab current = mTabControl.getCurrentTab();
2934 final int pos = mTabControl.getCurrentPosition();
2935 Tab newTab = current.getParent();
2936 if (newTab == null) {
2937 newTab = mTabControl.getTab(pos + 1);
2938 if (newTab == null) {
2939 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002940 }
2941 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002942 if (andQuit) {
2943 mTabControl.setCurrentTab(newTab);
2944 closeTab(current);
2945 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002946 // Close window
2947 closeTab(current);
2948 }
2949 }
2950
2951 /**
2952 * Close the tab, remove its associated title bar, and adjust mTabControl's
2953 * current tab to a valid value.
2954 */
2955 @Override
2956 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002957 if (tab == mTabControl.getCurrentTab()) {
2958 closeCurrentTab();
2959 } else {
2960 removeTab(tab);
2961 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002962 }
2963
Afzal Najamd4e33312012-04-26 01:54:01 -04002964 /**
2965 * Close all tabs except the current one
2966 */
2967 @Override
2968 public void closeOtherTabs() {
2969 int inactiveTabs = mTabControl.getTabCount() - 1;
2970 for (int i = inactiveTabs; i >= 0; i--) {
2971 Tab tab = mTabControl.getTab(i);
2972 if (tab != mTabControl.getCurrentTab()) {
2973 removeTab(tab);
2974 }
2975 }
2976 }
2977
Michael Kolb8233fac2010-10-26 16:08:53 -07002978 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002979 protected void loadUrlFromContext(String url) {
2980 Tab tab = getCurrentTab();
2981 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002982 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002983 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002984 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002985 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002986 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002987 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002988 }
2989 }
2990 }
2991
2992 /**
2993 * Load the URL into the given WebView and update the title bar
2994 * to reflect the new load. Call this instead of WebView.loadUrl
2995 * directly.
2996 * @param view The WebView used to load url.
2997 * @param url The URL to load.
2998 */
John Reck71e51422011-07-01 16:49:28 -07002999 @Override
3000 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003001 loadUrl(tab, url, null);
3002 }
3003
3004 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3005 if (tab != null) {
3006 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003007 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003008 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003009 if (tab.hasCrashed) {
3010 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3011 }
Michael Kolba53c9892011-10-05 13:31:40 -07003012 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003013 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003014 }
3015
3016 /**
3017 * Load UrlData into a Tab and update the title bar to reflect the new
3018 * load. Call this instead of UrlData.loadIn directly.
3019 * @param t The Tab used to load.
3020 * @param data The UrlData being loaded.
3021 */
3022 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003023 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003024 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003025 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003026 } else {
John Reck38b39652012-06-05 09:22:59 -07003027 if (t != null && data.mDisableUrlOverride) {
3028 t.disableUrlOverridingForLoad();
3029 }
John Reck26b18322011-06-21 13:08:58 -07003030 loadUrl(t, data.mUrl, data.mHeaders);
3031 }
3032 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003033 }
3034
John Reck30c714c2010-12-16 17:30:34 -08003035 @Override
3036 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003037 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003038 if (tab.canGoBack()) {
3039 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003040 } else {
John Reckef654f12011-07-12 16:42:08 -07003041 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003042 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003043 }
3044
3045 void goBackOnePageOrQuit() {
3046 Tab current = mTabControl.getCurrentTab();
3047 if (current == null) {
3048 /*
3049 * Instead of finishing the activity, simply push this to the back
3050 * of the stack and let ActivityManager to choose the foreground
3051 * activity. As BrowserActivity is singleTask, it will be always the
3052 * root of the task. So we can use either true or false for
3053 * moveTaskToBack().
3054 */
luxiaolb40014b2013-07-19 10:01:43 +08003055 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003056 return;
3057 }
John Reckef654f12011-07-12 16:42:08 -07003058 if (current.canGoBack()) {
3059 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003060 } else {
3061 // Check to see if we are closing a window that was created by
3062 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003063 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003064 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003065 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003066 // Now we close the other tab
3067 closeTab(current);
3068 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003069 /*
3070 * Instead of finishing the activity, simply push this to the back
3071 * of the stack and let ActivityManager to choose the foreground
3072 * activity. As BrowserActivity is singleTask, it will be always the
3073 * root of the task. So we can use either true or false for
3074 * moveTaskToBack().
3075 */
luxiaolb40014b2013-07-19 10:01:43 +08003076 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003077 }
3078 }
3079 }
3080
3081 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003082 * helper method for key handler
3083 * returns the current tab if it can't advance
3084 */
Michael Kolbc831b632011-05-11 09:30:34 -07003085 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003086 int pos = mTabControl.getCurrentPosition() + 1;
3087 if (pos >= mTabControl.getTabCount()) {
3088 pos = 0;
3089 }
3090 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003091 }
3092
3093 /**
3094 * helper method for key handler
3095 * returns the current tab if it can't advance
3096 */
Michael Kolbc831b632011-05-11 09:30:34 -07003097 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003098 int pos = mTabControl.getCurrentPosition() - 1;
3099 if ( pos < 0) {
3100 pos = mTabControl.getTabCount() - 1;
3101 }
3102 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003103 }
3104
John Reckbcef87f2012-02-03 14:58:34 -08003105 boolean isMenuOrCtrlKey(int keyCode) {
3106 return (KeyEvent.KEYCODE_MENU == keyCode)
3107 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3108 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3109 }
3110
Michael Kolb0035fad2011-03-14 13:25:28 -07003111 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003112 * handle key events in browser
3113 *
3114 * @param keyCode
3115 * @param event
3116 * @return true if handled, false to pass to super
3117 */
John Reck9c35b9c2012-05-30 10:08:50 -07003118 @Override
3119 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003120 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003121 // Even if MENU is already held down, we need to call to super to open
3122 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003123 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003124 mMenuIsDown = true;
3125 return false;
3126 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003127
Cary Clark8ff8c662010-12-29 15:03:05 -05003128 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003129 Tab tab = getCurrentTab();
3130 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003131
Cary Clark160bbb92011-01-10 11:17:07 -05003132 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3133 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003134
Michael Kolb8233fac2010-10-26 16:08:53 -07003135 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003136 case KeyEvent.KEYCODE_TAB:
3137 if (event.isCtrlPressed()) {
3138 if (event.isShiftPressed()) {
3139 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003140 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003141 } else {
3142 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003143 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003144 }
3145 return true;
3146 }
3147 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003148 case KeyEvent.KEYCODE_SPACE:
3149 // WebView/WebTextView handle the keys in the KeyDown. As
3150 // the Activity's shortcut keys are only handled when WebView
3151 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003152 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003153 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003154 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003155 pageDown();
3156 }
3157 return true;
3158 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003159 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003160 event.startTracking();
3161 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003162 case KeyEvent.KEYCODE_FORWARD:
3163 if (!noModifiers) break;
3164 tab.goForward();
3165 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003166 case KeyEvent.KEYCODE_DPAD_LEFT:
3167 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003168 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003169 return true;
3170 }
3171 break;
3172 case KeyEvent.KEYCODE_DPAD_RIGHT:
3173 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003174 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003175 return true;
3176 }
3177 break;
3178 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003179 if (ctrl) {
3180 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003181 return true;
3182 }
3183 break;
Michael Kolba4183062011-01-16 10:43:21 -08003184// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003185 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003186 if (ctrl ) {
3187 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003188 return true;
3189 }
3190 break;
Michael Kolba4183062011-01-16 10:43:21 -08003191// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003192// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003193// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003194// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003195// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003196// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003197// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003198// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003199// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003200// case KeyEvent.KEYCODE_M: // unused
3201// case KeyEvent.KEYCODE_N: // in Chrome: new window
3202// case KeyEvent.KEYCODE_O: // in Chrome: open file
3203// case KeyEvent.KEYCODE_P: // in Chrome: print page
3204// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003205// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003206// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3207 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003208 // we can't use the ctrl/shift flags, they check for
3209 // exclusive use of a modifier
3210 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003211 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003212 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003213 } else {
3214 openTabToHomePage();
3215 }
3216 return true;
3217 }
3218 break;
3219// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3220// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003221// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003222// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3223// case KeyEvent.KEYCODE_Y: // unused
3224// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003225 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003226 // it is a regular key and webview is not null
3227 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003228 }
3229
John Reck9c35b9c2012-05-30 10:08:50 -07003230 @Override
3231 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003232 switch(keyCode) {
3233 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003234 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003235 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003236 return true;
3237 }
3238 break;
3239 }
3240 return false;
3241 }
3242
John Reck9c35b9c2012-05-30 10:08:50 -07003243 @Override
3244 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003245 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003246 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003247 if (KeyEvent.KEYCODE_MENU == keyCode
3248 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003249 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003250 }
3251 }
Cary Clark160bbb92011-01-10 11:17:07 -05003252 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003253 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003254 case KeyEvent.KEYCODE_BACK:
3255 if (event.isTracking() && !event.isCanceled()) {
3256 onBackKey();
3257 return true;
3258 }
3259 break;
3260 }
3261 return false;
3262 }
3263
3264 public boolean isMenuDown() {
3265 return mMenuIsDown;
3266 }
3267
John Reck9c35b9c2012-05-30 10:08:50 -07003268 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003269 public void setupAutoFill(Message message) {
3270 // Open the settings activity at the AutoFill profile fragment so that
3271 // the user can create a new profile. When they return, we will dispatch
3272 // the message so that we can autofill the form using their new profile.
3273 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3274 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3275 AutoFillSettingsFragment.class.getName());
3276 mAutoFillSetupMessage = message;
3277 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3278 }
John Reckb3417f02011-01-14 11:01:05 -08003279
John Reck9c35b9c2012-05-30 10:08:50 -07003280 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003281 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003282 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003283 return true;
3284 }
3285
John Reck1cf4b792011-07-26 10:22:22 -07003286 @Override
3287 public boolean shouldCaptureThumbnails() {
3288 return mUi.shouldCaptureThumbnails();
3289 }
3290
Michael Kolbc3af0672011-08-09 10:24:41 -07003291 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003292 public boolean supportsVoice() {
3293 PackageManager pm = mActivity.getPackageManager();
3294 List activities = pm.queryIntentActivities(new Intent(
3295 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3296 return activities.size() != 0;
3297 }
3298
3299 @Override
3300 public void startVoiceRecognizer() {
3301 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003302 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003303 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3304 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3305 mActivity.startActivityForResult(voice, VOICE_RESULT);
3306 }
3307
3308 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003309 public void setBlockEvents(boolean block) {
3310 mBlockEvents = block;
3311 }
3312
John Reck9c35b9c2012-05-30 10:08:50 -07003313 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003314 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003315 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003316 }
3317
John Reck9c35b9c2012-05-30 10:08:50 -07003318 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003319 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003320 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003321 }
3322
John Reck9c35b9c2012-05-30 10:08:50 -07003323 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003324 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003325 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003326 }
3327
John Reck9c35b9c2012-05-30 10:08:50 -07003328 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003329 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003330 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003331 }
3332
John Reck9c35b9c2012-05-30 10:08:50 -07003333 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003334 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003335 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003336 }
3337
Michael Kolb8233fac2010-10-26 16:08:53 -07003338}