blob: 2b66988cf7665ff45ca95bf7eec540434835cc24 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
Dave Tharp851e8d52015-04-26 14:58:18 -07004 * Copyright (c) 2015 The Linux Foundation, All rights reserved.
Site Maoabb7bd32015-03-20 15:34:02 -07005 *
Michael Kolb8233fac2010-10-26 16:08:53 -07006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Bijan Amirzada41242f22014-03-21 12:12:18 -070019package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070020
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080022
luxiaolb40014b2013-07-19 10:01:43 +080023import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070025import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070026import android.app.ProgressDialog;
Vivek Sekharce2a4832014-03-26 13:26:53 -070027import android.content.Context;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070030import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070031import android.content.ContentValues;
John Reck68234a92012-04-19 15:27:12 -070032import android.content.DialogInterface;
33import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.content.Intent;
Site Maoabb7bd32015-03-20 15:34:02 -070035import android.content.IntentFilter;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
38import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070039import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050040import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.database.Cursor;
42import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080044import android.graphics.BitmapFactory;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080045import android.graphics.Rect;
Tarun Nainanif5563b62015-01-21 18:52:59 -080046import android.media.AudioManager;
kaiyiza016da12013-08-26 17:50:22 +080047import android.net.ConnectivityManager;
48import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070049import android.net.Uri;
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;
Tarun Nainanif5563b62015-01-21 18:52:59 -080053import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070055import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.os.Handler;
57import android.os.Message;
58import android.os.PowerManager;
59import android.os.PowerManager.WakeLock;
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;
66import android.view.ActionMode;
67import android.view.ContextMenu;
68import android.view.ContextMenu.ContextMenuInfo;
69import android.view.Gravity;
70import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070071import android.view.Menu;
72import android.view.MenuInflater;
73import android.view.MenuItem;
74import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070075import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070076import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070077import android.view.WindowManager;
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;
Pankaj Garg49b79252014-11-07 17:33:41 -080081import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import org.codeaurora.swe.CookieManager;
85import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080086import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.HttpAuthHandler;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080088import org.codeaurora.swe.WebSettings;
89import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070090import org.codeaurora.swe.WebBackForwardList;
91import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080092
Bijan Amirzada41242f22014-03-21 12:12:18 -070093import com.android.browser.IntentHandler.UrlData;
94import com.android.browser.UI.ComboViews;
Dave Tharp13f3f7c2015-07-16 14:58:51 -070095import com.android.browser.mdm.DownloadDirRestriction;
Dave Tharp9a082b12015-06-19 08:46:57 -070096import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -070097import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -070098import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -070099import com.android.browser.mynavigation.AddMyNavigationPage;
100import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700101import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700102import com.android.browser.platformsupport.BrowserContract;
Pankaj Garg18902562014-12-05 16:18:51 -0800103import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700104import com.android.browser.provider.BrowserProvider2.Thumbnails;
105import com.android.browser.provider.SnapshotProvider.Snapshots;
106import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800107import com.android.browser.appmenu.AppMenuHandler;
108import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700109
Michael Kolb8233fac2010-10-26 16:08:53 -0700110import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700111import java.io.File;
112import java.io.FileOutputStream;
113import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700115import java.text.DateFormat;
116import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700117import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700118import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700119import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800120import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200121import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700122import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123
124/**
125 * Controller for browser
126 */
127public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800128 implements WebViewController, UiController, ActivityController,
129 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
131 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800132 private static final String SEND_APP_ID_EXTRA =
133 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700134 public static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800135 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
136 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800137
Vivek Sekharb54614f2014-05-01 19:03:37 -0700138 // Remind switch to data connection if wifi is unavailable
139 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800140
Michael Kolb8233fac2010-10-26 16:08:53 -0700141 // public message ids
142 public final static int LOAD_URL = 1001;
143 public final static int STOP_LOAD = 1002;
144
145 // Message Ids
146 private static final int FOCUS_NODE_HREF = 102;
147 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800148 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700149
Michael Kolb8233fac2010-10-26 16:08:53 -0700150 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800151 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700152
153 private static final int EMPTY_MENU = -1;
154
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700156 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 final static int PREFERENCES_PAGE = 3;
158 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000159 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700160 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800161 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000162
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
164
165 // As the ids are dynamically created, we can't guarantee that they will
166 // be in sequence, so this static array maps ids to a window number.
167 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
168 { R.id.window_one_menu_id, R.id.window_two_menu_id,
169 R.id.window_three_menu_id, R.id.window_four_menu_id,
170 R.id.window_five_menu_id, R.id.window_six_menu_id,
171 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
172
173 // "source" parameter for Google search through search key
174 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
175 // "source" parameter for Google search through simplily type
176 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
177
George Mount387d45d2011-10-07 15:57:53 -0700178 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700179 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
180
John Reckd7dd9b22011-08-30 09:18:29 -0700181 // A bitmap that is re-used in createScreenshot as scratch space
182 private static Bitmap sThumbnailBitmap;
183
Michael Kolb8233fac2010-10-26 16:08:53 -0700184 private Activity mActivity;
185 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700186 private HomepageHandler mHomepageHandler;
187 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700188 private BrowserSettings mSettings;
189 private WebViewFactory mFactory;
190
191 private WakeLock mWakeLock;
192
193 private UrlHandler mUrlHandler;
194 private UploadHandler mUploadHandler;
195 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 private NetworkStateHandler mNetworkHandler;
197
Ben Murdoch8029a772010-11-16 11:58:21 +0000198 private Message mAutoFillSetupMessage;
199
kaiyiza016da12013-08-26 17:50:22 +0800200 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700201
Michael Kolb8233fac2010-10-26 16:08:53 -0700202 // FIXME, temp address onPrepareMenu performance problem.
203 // When we move everything out of view, we should rewrite this.
204 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700205 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700206 private int mOldMenuState = EMPTY_MENU;
207 private Menu mCachedMenu;
208
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 private boolean mMenuIsDown;
210
Pankaj Garg49b79252014-11-07 17:33:41 -0800211 private boolean mWasInPageLoad = false;
212 private AppMenuHandler mAppMenuHandler;
213
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 // For select and find, we keep track of the ActionMode so that
215 // finish() can be called as desired.
216 private ActionMode mActionMode;
217
218 /**
219 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
220 * of whether the configuration has changed. The first onMenuOpened call
221 * after a configuration change is simply a reopening of the same menu
222 * (i.e. mIconView did not change).
223 */
224 private boolean mConfigChanged;
225
226 /**
227 * Keeps track of whether the options menu is open. This is important in
228 * determining whether to show or hide the title bar overlay
229 */
230 private boolean mOptionsMenuOpen;
231
232 /**
233 * Whether or not the options menu is in its bigger, popup menu form. When
234 * true, we want the title bar overlay to be gone. When false, we do not.
235 * Only meaningful if mOptionsMenuOpen is true.
236 */
237 private boolean mExtendedMenuOpen;
238
Michael Kolb8233fac2010-10-26 16:08:53 -0700239 private boolean mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700240 private boolean mActivityStopped = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700241 private boolean mLoadStopped;
242
243 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500244 // Checks to see when the bookmarks database has changed, and updates the
245 // Tabs' notion of whether they represent bookmarked sites.
246 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700247 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700248
Michael Kolbc3af0672011-08-09 10:24:41 -0700249 private boolean mBlockEvents;
250
Michael Kolb0b129122012-06-04 16:31:58 -0700251 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800252 private boolean mUpdateMyNavThumbnail;
253 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800254 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800255 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700256 private PowerConnectionReceiver mLowPowerReceiver;
257 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700258
Pankaj Garg634bf432015-07-13 09:54:21 -0700259 private boolean mCurrentPageBookmarked;
260
George Mount3636d0a2011-11-21 09:08:21 -0800261 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 mActivity = browser;
263 mSettings = BrowserSettings.getInstance();
264 mTabControl = new TabControl(this);
265 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700266 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800267 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700268 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700269
270 mUrlHandler = new UrlHandler(this);
271 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700272
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500274 mBookmarksObserver = new ContentObserver(mHandler) {
275 @Override
276 public void onChange(boolean selfChange) {
277 int size = mTabControl.getTabCount();
278 for (int i = 0; i < size; i++) {
279 mTabControl.getTab(i).updateBookmarkedStatus();
280 }
281 }
282
283 };
284 browser.getContentResolver().registerContentObserver(
285 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700286
287 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700288 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800289 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700290 }
291
John Reck9c35b9c2012-05-30 10:08:50 -0700292 @Override
293 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700294 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800295 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800296 // mCrashRecoverHandler has any previously saved state.
297 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700298 }
299
George Mount3636d0a2011-11-21 09:08:21 -0800300 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800301 // we dont want to ever recover incognito tabs
302 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700303
Patrick Scott7d50a932011-02-04 09:27:26 -0500304 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700305 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500306 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000307
Michael Kolbc831b632011-05-11 09:30:34 -0700308 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500309 // Not able to restore so we go ahead and clear session cookies. We
310 // must do this before trying to login the user as we don't want to
311 // clear any session cookies set during login.
312 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700313 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800314 if (intent == null) {
315 // This won't happen under common scenarios. The icicle is
316 // not null, but there aren't any tabs to restore.
317 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700318 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800319 final Bundle extra = intent.getExtras();
320 // Create an initial tab.
321 // If the intent is ACTION_VIEW and data is not null, the Browser is
322 // invoked to view the content by another application. In this case,
323 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800324 UrlData urlData = null;
325 if (intent.getData() != null
326 && Intent.ACTION_VIEW.equals(intent.getAction())
327 && intent.getData().toString().startsWith("content://")) {
328 urlData = new UrlData(intent.getData().toString());
329 } else {
330 urlData = IntentHandler.getUrlDataFromIntent(intent);
331 }
George Mount3636d0a2011-11-21 09:08:21 -0800332 Tab t = null;
333 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700334 String landingPage = mActivity.getResources().getString(
335 R.string.def_landing_page);
336 if (!landingPage.isEmpty()) {
337 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800338 } else {
339 t = openTabToHomePage();
340 }
George Mount3636d0a2011-11-21 09:08:21 -0800341 } else {
342 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700343 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800344 }
345 if (t != null) {
346 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
347 }
348 WebView webView = t.getWebView();
349 if (extra != null) {
350 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
351 if (scale > 0 && scale <= 1000) {
352 webView.setInitialScale(scale);
353 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700354 }
355 }
John Reckd8c74522011-06-14 08:45:00 -0700356 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700358 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500359 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700360 List<Tab> tabs = mTabControl.getTabs();
361 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700362
John Reck1cf4b792011-07-26 10:22:22 -0700363 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700364 //handle restored pages that may require a JS interface
365 if (t.getWebView() != null) {
366 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
367 if (backForwardList != null) {
368 for (int i = 0; i < backForwardList.getSize(); i++) {
369 WebHistoryItem item = backForwardList.getItemAtIndex(i);
370 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
371 }
372 }
373 }
John Reck1cf4b792011-07-26 10:22:22 -0700374 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700375 if (t != mTabControl.getCurrentTab()) {
376 t.pause();
377 }
John Reck1cf4b792011-07-26 10:22:22 -0700378 }
379 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700380 if (tabs.size() == 0) {
381 openTabToHomePage();
382 }
John Reck1cf4b792011-07-26 10:22:22 -0700383 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700384 // TabControl.restoreState() will create a new tab even if
385 // restoring the state fails.
386 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800387 // Intent is non-null when framework thinks the browser should be
388 // launching with a new intent (icicle is null).
389 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700390 mIntentHandler.onNewIntent(intent);
391 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700392 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700394 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800395 if (jsFlags.trim().length() != 0) {
396 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 }
George Mount3636d0a2011-11-21 09:08:21 -0800398 if (intent != null
399 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700400 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800401 }
Site Maoabb7bd32015-03-20 15:34:02 -0700402 mLowPowerReceiver = new PowerConnectionReceiver();
403 mPowerChangeReceiver = new PowerConnectionReceiver();
404
405 //always track the android framework's power save mode
406 IntentFilter filter = new IntentFilter();
407 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
408 // Power save mode only exists in Lollipop and above
409 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
410 }
411 filter.addAction(Intent.ACTION_BATTERY_OKAY);
412 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700413 }
414
John Reck1cf4b792011-07-26 10:22:22 -0700415 private static class PruneThumbnails implements Runnable {
416 private Context mContext;
417 private List<Long> mIds;
418
419 PruneThumbnails(Context context, List<Long> preserveIds) {
420 mContext = context.getApplicationContext();
421 mIds = preserveIds;
422 }
423
424 @Override
425 public void run() {
426 ContentResolver cr = mContext.getContentResolver();
427 if (mIds == null || mIds.size() == 0) {
428 cr.delete(Thumbnails.CONTENT_URI, null, null);
429 } else {
430 int length = mIds.size();
431 StringBuilder where = new StringBuilder();
432 where.append(Thumbnails._ID);
433 where.append(" not in (");
434 for (int i = 0; i < length; i++) {
435 where.append(mIds.get(i));
436 if (i < (length - 1)) {
437 where.append(",");
438 }
439 }
440 where.append(")");
441 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
442 }
443 }
444
445 }
446
Michael Kolb1514bb72010-11-22 09:11:48 -0800447 @Override
448 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700449 return mFactory;
450 }
451
452 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800453 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700454 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700455 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800456 }
457
458 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800459 public void createSubWindow(Tab tab) {
460 endActionMode();
461 WebView mainView = tab.getWebView();
462 WebView subView = mFactory.createWebView((mainView == null)
463 ? false
464 : mainView.isPrivateBrowsingEnabled());
465 mUi.createSubWindow(tab, subView);
466 }
467
468 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700469 public Context getContext() {
470 return mActivity;
471 }
472
473 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700474 public Activity getActivity() {
475 return mActivity;
476 }
477
478 void setUi(UI ui) {
479 mUi = ui;
480 }
481
Michael Kolbaf63dba2012-05-16 12:58:05 -0700482 @Override
483 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 return mSettings;
485 }
486
487 IntentHandler getIntentHandler() {
488 return mIntentHandler;
489 }
490
491 @Override
492 public UI getUi() {
493 return mUi;
494 }
495
496 int getMaxTabs() {
497 return mActivity.getResources().getInteger(R.integer.max_tabs);
498 }
499
500 @Override
501 public TabControl getTabControl() {
502 return mTabControl;
503 }
504
Michael Kolb1bf23132010-11-19 12:55:12 -0800505 @Override
506 public List<Tab> getTabs() {
507 return mTabControl.getTabs();
508 }
509
Michael Kolb8233fac2010-10-26 16:08:53 -0700510 private void startHandler() {
511 mHandler = new Handler() {
512
513 @Override
514 public void handleMessage(Message msg) {
515 switch (msg.what) {
516 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700517 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700518 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800519 case UNKNOWN_TYPE_MSG:
520 HashMap unknownTypeMap = (HashMap) msg.obj;
521 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
522 /*
523 * When the context menu is shown to the user
524 * we need to assure that its happening on the current webview
525 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
526 */
527 if (getCurrentWebView() != viewForUnknownType)
528 break;
529
530 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800531 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800532 WebView.HitTestResult result = new WebView.HitTestResult();
533
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800534 // Prevent unnecessary calls to context menu
535 // if url and image src are null
536 if (unknown_type_src == null && unknown_type_url == null)
537 break;
538
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800539 //setting the HitTestResult with new RESULT TYPE
540 if (!TextUtils.isEmpty(unknown_type_src)) {
541 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
542 result.setExtra(unknown_type_src);
543 } else {
544 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
545 result.setExtra("about:blank");
546 }
547
548 mResult = result;
549 openContextMenu(viewForUnknownType);
550 mResult = null;
551
552 break;
553
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 case FOCUS_NODE_HREF:
555 {
556 String url = (String) msg.getData().get("url");
557 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500558 String src = (String) msg.getData().get("src");
559 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 if (TextUtils.isEmpty(url)) {
561 break;
562 }
563 HashMap focusNodeMap = (HashMap) msg.obj;
564 WebView view = (WebView) focusNodeMap.get("webview");
565 // Only apply the action if the top window did not change.
566 if (getCurrentTopWebView() != view) {
567 break;
568 }
569 switch (msg.arg1) {
570 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700571 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700572 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500573 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700574 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500575 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500576 case R.id.open_newtab_context_menu_id:
577 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700578 openTab(url, parent,
579 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500580 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 case R.id.copy_link_context_menu_id:
582 copy(url);
583 break;
584 case R.id.save_link_context_menu_id:
585 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500586 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800587 mActivity, url, view.getSettings().getUserAgentString(),
588 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700589 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700590 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700591 if(title == null || title == "")
592 title = url;
593
594 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
595 //SWE TODO: No thumbnail support for the url obtained via
596 //browser context menu as its not loaded in webview.
597 if (bookmarkIntent != null) {
598 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
599 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
600 mActivity.startActivity(bookmarkIntent);
601 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700602 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 }
604 break;
605 }
606
607 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700608 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700609 break;
610
611 case STOP_LOAD:
612 stopLoading();
613 break;
614
615 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700616 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 mWakeLock.release();
618 // if we reach here, Browser should be still in the
619 // background loading after WAKELOCK_TIMEOUT (5-min).
620 // To avoid burning the battery, stop loading.
621 mTabControl.stopAllLoading();
622 }
623 break;
624
kaiyiz591110b2013-08-06 17:11:06 +0800625 case OPEN_MENU:
626 if (!mOptionsMenuOpen && mActivity != null ) {
627 mActivity.openOptionsMenu();
628 }
629 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700630 }
631 }
632 };
633
634 }
635
John Reckef654f12011-07-12 16:42:08 -0700636 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200637 public Tab getCurrentTab() {
638 return mTabControl.getCurrentTab();
639 }
640
Michael Kolbba99c5d2010-11-29 14:57:41 -0800641 @Override
642 public void shareCurrentPage() {
643 shareCurrentPage(mTabControl.getCurrentTab());
644 }
645
646 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700647 if (tab == null || tab.getWebView() == null)
648 return;
649
650 final Tab mytab = tab;
651 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
652 @Override
653 public void onReceiveValue(Bitmap bitmap) {
654 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
655 mytab.getFavicon(), bitmap);
656 }
657 };
658
659 createScreenshotAsync(
660 tab.getWebView(),
661 getDesiredThumbnailWidth(mActivity),
662 getDesiredThumbnailHeight(mActivity),
663 new ValueCallback<Bitmap>() {
664 @Override
665 public void onReceiveValue(Bitmap bitmap) {
666 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
667 mytab.getFavicon(), bitmap);
668 }
669 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800670 }
671
Michael Kolb8233fac2010-10-26 16:08:53 -0700672 /**
673 * Share a page, providing the title, url, favicon, and a screenshot. Uses
674 * an {@link Intent} to launch the Activity chooser.
675 * @param c Context used to launch a new Activity.
676 * @param title Title of the page. Stored in the Intent with
677 * {@link Intent#EXTRA_SUBJECT}
678 * @param url URL of the page. Stored in the Intent with
679 * {@link Intent#EXTRA_TEXT}
680 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
681 * with {@link Browser#EXTRA_SHARE_FAVICON}
682 * @param screenshot Bitmap of a screenshot of the page. Stored in the
683 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
684 */
685 static final void sharePage(Context c, String title, String url,
686 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700687
688 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
689 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
690 R.layout.app_row, sDialog.getApps());
691 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 }
693
694 private void copy(CharSequence text) {
695 ClipboardManager cm = (ClipboardManager) mActivity
696 .getSystemService(Context.CLIPBOARD_SERVICE);
697 cm.setText(text);
698 }
699
700 // lifecycle
701
John Reck9c35b9c2012-05-30 10:08:50 -0700702 @Override
703 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800705 // update the menu in case of a locale change
706 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800707 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800708 if (mOptionsMenuOpen) {
709 mActivity.closeOptionsMenu();
710 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
711 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 mUi.onConfigurationChanged(config);
713 }
714
715 @Override
716 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700717 if (!mUi.isWebShowing()) {
718 mUi.showWeb(false);
719 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700720 mIntentHandler.onNewIntent(intent);
721 }
722
John Reck9c35b9c2012-05-30 10:08:50 -0700723 @Override
724 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800725 if (mUi.isCustomViewShowing()) {
726 hideCustomView();
727 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700728 if (mActivityPaused) {
729 Log.e(LOGTAG, "BrowserActivity is already paused.");
730 return;
731 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700732 mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700733
Michael Kolb8233fac2010-10-26 16:08:53 -0700734 }
735
John Reck9c35b9c2012-05-30 10:08:50 -0700736 @Override
737 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700738 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800739 Bundle saveState = createSaveState();
740
741 // crash recovery manages all save & restore state
742 mCrashRecoveryHandler.writeState(saveState);
743 mSettings.setLastRunPaused(true);
744 }
745
746 /**
747 * Save the current state to outState. Does not write the state to
748 * disk.
749 * @return Bundle containing the current state of all tabs.
750 */
751 /* package */ Bundle createSaveState() {
752 Bundle saveState = new Bundle();
753 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800754 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700755 }
756
John Reck9c35b9c2012-05-30 10:08:50 -0700757 @Override
758 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700759 if (!mActivityPaused) {
760 Log.e(LOGTAG, "BrowserActivity is already resumed.");
761 return;
762 }
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700763 mActivityPaused = false;
764 if (mVoiceResult != null) {
765 mUi.onVoiceResult(mVoiceResult);
766 mVoiceResult = null;
767 }
768 }
769
770 private void releaseWakeLock() {
771 if (mWakeLock != null && mWakeLock.isHeld()) {
772 mHandler.removeMessages(RELEASE_WAKELOCK);
773 mWakeLock.release();
774 }
775 }
776
777 @Override
778 public void onStop() {
779 if (mActivityStopped) {
780 Log.e(LOGTAG, "BrowserActivity is already stoped.");
781 return;
782 }
783 mActivityStopped = true;
784 Tab tab = mTabControl.getCurrentTab();
785 if (tab != null) {
786 tab.pause();
787 if (!pauseWebViewTimers(tab)) {
788 if (mWakeLock == null) {
789 PowerManager pm = (PowerManager) mActivity
790 .getSystemService(Context.POWER_SERVICE);
791 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
792 }
793 mWakeLock.acquire();
794 mHandler.sendMessageDelayed(mHandler
795 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
796 }
797 }
798 mUi.onPause();
799 mNetworkHandler.onPause();
800 NfcHandler.unregister(mActivity);
801 mActivity.unregisterReceiver(mLowPowerReceiver);
802 }
803
804 @Override
805 public void onStart() {
806 if (!mActivityStopped) {
807 Log.e(LOGTAG, "BrowserActivity is already started.");
808 return;
809 }
810 mActivityStopped = false;
Vivek Sekharb53a5952015-07-17 14:14:38 -0700811 UpdateNotificationService.updateCheck(mActivity);
George Mount3636d0a2011-11-21 09:08:21 -0800812 mSettings.setLastRunPaused(false);
Michael Kolb70976932010-11-30 11:34:01 -0800813 Tab current = mTabControl.getCurrentTab();
814 if (current != null) {
815 current.resume();
816 resumeWebViewTimers(current);
817 }
John Reckf57c0292011-07-21 18:15:39 -0700818 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200819
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 mUi.onResume();
821 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100822 NfcHandler.register(mActivity, this);
Vivek Sekharc70ae632015-04-08 17:54:05 -0700823 if (current != null && current.getWebView().isShowingCrashView())
824 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700825 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700826
John Reckf57c0292011-07-21 18:15:39 -0700827 }
828
Michael Kolb70976932010-11-30 11:34:01 -0800829 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800830 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800831 * @param tab guaranteed non-null
832 */
833 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700834 boolean inLoad = tab.inPageLoad();
Tarun Nainani7c762c82015-08-04 13:42:09 -0700835 if ((!mActivityStopped && !inLoad) || (mActivityStopped && inLoad)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700836 CookieSyncManager.getInstance().startSync();
837 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100838 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 }
840 }
841
Michael Kolb70976932010-11-30 11:34:01 -0800842 /**
843 * Pause all WebView timers using the WebView of the given tab
844 * @param tab
845 * @return true if the timers are paused or tab is null
846 */
847 private boolean pauseWebViewTimers(Tab tab) {
848 if (tab == null) {
849 return true;
850 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700851 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100852 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 }
Michael Kolb70976932010-11-30 11:34:01 -0800855 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 }
857
John Reck9c35b9c2012-05-30 10:08:50 -0700858 @Override
859 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800860 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
862 mUploadHandler = null;
863 }
864 if (mTabControl == null) return;
865 mUi.onDestroy();
866 // Remove the current tab and sub window
867 Tab t = mTabControl.getCurrentTab();
868 if (t != null) {
869 dismissSubWindow(t);
870 removeTab(t);
871 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500872 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700873 // Destroy all the tabs
874 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700875 // Unregister receiver
876 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700877 }
878
879 protected boolean isActivityPaused() {
880 return mActivityPaused;
881 }
882
John Reck9c35b9c2012-05-30 10:08:50 -0700883 @Override
884 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 mTabControl.freeMemory();
886 }
887
888 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 public void stopLoading() {
890 mLoadStopped = true;
891 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700893 if (w != null) {
894 w.stopLoading();
895 mUi.onPageStopped(tab);
896 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 }
898
899 boolean didUserStopLoading() {
900 return mLoadStopped;
901 }
902
kaiyiza016da12013-08-26 17:50:22 +0800903 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700904 final String reminderType = getContext().getResources().getString(
905 R.string.def_wifi_browser_interaction_remind_type);
906 final String selectionConnnection = getContext().getResources().getString(
907 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700908 final String wifiSelection = getContext().getResources().getString(
909 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700910
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700911 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
912 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700913 return;
914
kaiyiza016da12013-08-26 17:50:22 +0800915 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700916 Context.CONNECTIVITY_SERVICE);
917 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700918 WifiManager wifiMgr = (WifiManager) this.getContext()
919 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700920 if (networkInfo == null
921 || (networkInfo != null && (networkInfo.getType() !=
922 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700923 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
924 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700925 List<ScanResult> list = wifiMgr.getScanResults();
926 // Have no AP's for Wifi's fall back to data
927 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700928 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700929 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
930 this.getContext().startActivity(intent);
931 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700932 // Request to select Wifi AP
933 try {
934 Intent intent = new Intent(wifiSelection);
935 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
936 this.getContext().startActivity(intent);
937 } catch (Exception e) {
938 String err_msg = this.getContext().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700939 R.string.activity_not_found, wifiSelection);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700940 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
941 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700942 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700943 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800944 }
945 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700946
Michael Kolb8233fac2010-10-26 16:08:53 -0700947 // WebViewController
948
949 @Override
John Reck324d4402011-01-11 16:56:42 -0800950 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700951
Michael Kolb8233fac2010-10-26 16:08:53 -0700952 // reset sync timer to avoid sync starts during loading a page
953 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700954 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700955 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800956 boolean networkNotifier = BrowserConfig.getInstance(getContext())
957 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700958 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700959 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800960 } else {
961 if (!mNetworkHandler.isNetworkUp()) {
962 view.setNetworkAvailable(false);
963 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 }
965
966 // when BrowserActivity just starts, onPageStarted may be called before
967 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
968 // to start the timer. As we won't switch tabs while an activity is in
969 // pause state, we can ensure calling resume and pause in pair.
Tarun Nainani7c762c82015-08-04 13:42:09 -0700970 if (mActivityStopped) {
Michael Kolb70976932010-11-30 11:34:01 -0800971 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 }
973 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700974 endActionMode();
975
John Reck30c714c2010-12-16 17:30:34 -0800976 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700977
John Reck324d4402011-01-11 16:56:42 -0800978 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 // update the bookmark database for favicon
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700980 syncBookmarkFavicon(tab, null, url, favicon);
Michael Kolb8233fac2010-10-26 16:08:53 -0700981
982 Performance.tracePageStart(url);
983
984 // Performance probe
985 if (false) {
986 Performance.onPageStarted();
987 }
988
989 }
990
991 @Override
John Reck324d4402011-01-11 16:56:42 -0800992 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700993 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800994 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200995
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 // Performance probe
997 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800998 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700999 }
1000
Tarun Nainani8eb00912014-07-17 12:28:32 -07001001 tab.onPageFinished();
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001002 syncBookmarkFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -07001003
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 Performance.tracePageFinished();
1005 }
1006
1007 @Override
John Reck30c714c2010-12-16 17:30:34 -08001008 public void onProgressChanged(Tab tab) {
1009 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001010
1011 if (newProgress == 100) {
1012 CookieSyncManager.getInstance().sync();
1013 // onProgressChanged() may continue to be called after the main
1014 // frame has finished loading, as any remaining sub frames continue
1015 // to load. We'll only get called once though with newProgress as
1016 // 100 when everything is loaded. (onPageFinished is called once
1017 // when the main frame completes loading regardless of the state of
1018 // any sub frames so calls to onProgressChanges may continue after
1019 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001020 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001021 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001022 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001023 } else if (mWasInPageLoad) {
1024 mWasInPageLoad = false;
1025 updateInLoadMenuItems(mCachedMenu, tab);
1026 }
1027
Tarun Nainani7c762c82015-08-04 13:42:09 -07001028 if (mActivityStopped && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001029 // pause the WebView timer and release the wake lock if it is
1030 // finished while BrowserActivity is in pause state.
1031 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001032 }
1033 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001034 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001035 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001036 // still loading
1037 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001039 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001040 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001041 } else {
1042 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 }
1044 }
Pankaj Gargf9040c82015-08-14 10:19:31 -07001045
1046 mUi.onProgressChanged(tab);
John Reck30c714c2010-12-16 17:30:34 -08001047 }
1048
1049 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001050 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001051 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 }
1053
1054 @Override
1055 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001056 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001057 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001058 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001059 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1060 return;
1061 }
1062 // Update the title in the history database if not in private browsing mode
1063 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001064 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001065 }
1066 }
1067
1068 @Override
1069 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001070 syncBookmarkFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1071 ((BaseUi)mUi).setFavicon(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001072 }
1073
1074 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001075 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001076 // if tab is snapshot tab we want to prevent navigation from occuring
1077 // since snapshot tab opens a new tab with the url
1078 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 }
1080
1081 @Override
1082 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1083 if (mMenuIsDown) {
1084 // only check shortcut key when MENU is held
1085 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1086 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001087 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001088 int keyCode = event.getKeyCode();
1089 // We need to send almost every key to WebKit. However:
1090 // 1. We don't want to block the device on the renderer for
1091 // some keys like menu, home, call.
1092 // 2. There are no WebKit equivalents for some of these keys
1093 // (see app/keyboard_codes_win.h)
1094 // Note that these are not the same set as KeyEvent.isSystemKey:
1095 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1096 if (keyCode == KeyEvent.KEYCODE_MENU ||
1097 keyCode == KeyEvent.KEYCODE_HOME ||
1098 keyCode == KeyEvent.KEYCODE_BACK ||
1099 keyCode == KeyEvent.KEYCODE_CALL ||
1100 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1101 keyCode == KeyEvent.KEYCODE_POWER ||
1102 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1103 keyCode == KeyEvent.KEYCODE_CAMERA ||
1104 keyCode == KeyEvent.KEYCODE_FOCUS ||
1105 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1106 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1107 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1108 return true;
1109 }
1110
1111 // We also have to intercept some shortcuts before we send them to the ContentView.
1112 if (event.isCtrlPressed() && (
1113 keyCode == KeyEvent.KEYCODE_TAB ||
1114 keyCode == KeyEvent.KEYCODE_W ||
1115 keyCode == KeyEvent.KEYCODE_F4)) {
1116 return true;
1117 }
1118
1119 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 }
1121
Tarun Nainanif5563b62015-01-21 18:52:59 -08001122 private void handleMediaKeyEvent(KeyEvent event) {
1123
1124 int keyCode = event.getKeyCode();
1125 // send media key events to audio manager
1126 if (Build.VERSION.SDK_INT >= 19) {
1127
1128 switch (keyCode) {
1129 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1130 case KeyEvent.KEYCODE_MEDIA_STOP:
1131 case KeyEvent.KEYCODE_MEDIA_NEXT:
1132 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1133 case KeyEvent.KEYCODE_MEDIA_REWIND:
1134 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1135 case KeyEvent.KEYCODE_MUTE:
1136 case KeyEvent.KEYCODE_MEDIA_PLAY:
1137 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1138 case KeyEvent.META_SHIFT_RIGHT_ON:
1139 case KeyEvent.KEYCODE_MEDIA_EJECT:
1140 case KeyEvent.KEYCODE_MEDIA_RECORD:
1141 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1142
1143 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1144 .getSystemService(Context.AUDIO_SERVICE);
1145 audioManager.dispatchMediaKeyEvent(event);
1146 }
1147 }
1148 }
1149
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 @Override
John Reck997b1b72012-04-19 18:08:25 -07001151 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001153 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001154 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001155 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 } else {
John Reck997b1b72012-04-19 18:08:25 -07001157 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001158 }
1159 }
John Reck997b1b72012-04-19 18:08:25 -07001160 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 }
1162
1163 @Override
John Reck324d4402011-01-11 16:56:42 -08001164 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001165 // Don't save anything in private browsing mode or when disabling history
1166 // for regular tabs is enabled
1167 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1168 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1169 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001170
John Reck49a603c2011-03-03 09:33:05 -08001171 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001172
John Reck324d4402011-01-11 16:56:42 -08001173 if (TextUtils.isEmpty(url)
1174 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001175 return;
1176 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001177
John Reckf57c0292011-07-21 18:15:39 -07001178 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001179 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 }
1181
1182 @Override
1183 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1184 AsyncTask<Void, Void, String[]> task =
1185 new AsyncTask<Void, Void, String[]>() {
1186 @Override
1187 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001188 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001189 }
1190 @Override
1191 public void onPostExecute(String[] result) {
1192 callback.onReceiveValue(result);
1193 }
1194 };
1195 task.execute();
1196 }
1197
1198 @Override
1199 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1200 final HttpAuthHandler handler, final String host,
1201 final String realm) {
1202 String username = null;
1203 String password = null;
1204
1205 boolean reuseHttpAuthUsernamePassword
1206 = handler.useHttpAuthUsernamePassword();
1207
1208 if (reuseHttpAuthUsernamePassword && view != null) {
1209 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1210 if (credentials != null && credentials.length == 2) {
1211 username = credentials[0];
1212 password = credentials[1];
1213 }
1214 }
1215
1216 if (username != null && password != null) {
1217 handler.proceed(username, password);
1218 } else {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -07001219 if (!tab.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 handler.cancel();
1221 }
1222 }
1223 }
1224
1225 @Override
1226 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001227 String contentDisposition, String mimetype, String referer,
1228 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001229 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001230 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001231 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001232 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001233 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001234 // This Tab was opened for the sole purpose of downloading a
1235 // file. Remove it.
1236 if (tab == mTabControl.getCurrentTab()) {
1237 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001238 if (tab.getDerivedFromIntent())
1239 closeTab(tab);
1240 else
1241 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001242 } else {
1243 // In this case, it is not.
1244 closeTab(tab);
1245 }
1246 }
1247 }
1248
1249 @Override
1250 public Bitmap getDefaultVideoPoster() {
1251 return mUi.getDefaultVideoPoster();
1252 }
1253
1254 @Override
1255 public View getVideoLoadingProgressView() {
1256 return mUi.getVideoLoadingProgressView();
1257 }
1258
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 // helper method
1260
1261 /*
1262 * Update the favorites icon if the private browsing isn't enabled and the
1263 * icon is valid.
1264 */
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001265 private void syncBookmarkFavicon(Tab tab, final String originalUrl,
1266 final String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001267 if (favicon == null) {
1268 return;
1269 }
1270 if (!tab.isPrivateBrowsingEnabled()) {
1271 Bookmarks.updateFavicon(mActivity
1272 .getContentResolver(), originalUrl, url, favicon);
1273 }
1274 }
1275
Leon Scroggins4cd97792010-12-03 15:31:56 -05001276 @Override
1277 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001278 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001279 mUi.bookmarkedStatusHasChanged(tab);
1280 }
1281
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 // end WebViewController
1283
1284 protected void pageUp() {
1285 getCurrentTopWebView().pageUp(false);
1286 }
1287
1288 protected void pageDown() {
1289 getCurrentTopWebView().pageDown(false);
1290 }
1291
1292 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001293 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001294 public void editUrl() {
1295 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001296 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001297 }
1298
John Reck9c35b9c2012-05-30 10:08:50 -07001299 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001300 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001301 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001302 if (tab.inForeground()) {
1303 if (mUi.isCustomViewShowing()) {
1304 callback.onCustomViewHidden();
1305 return;
1306 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001307 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001308 // Save the menu state and set it to empty while the custom
1309 // view is showing.
1310 mOldMenuState = mMenuState;
1311 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001312 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001313 }
1314 }
1315
1316 @Override
1317 public void hideCustomView() {
1318 if (mUi.isCustomViewShowing()) {
1319 mUi.onHideCustomView();
1320 // Reset the old menu state.
1321 mMenuState = mOldMenuState;
1322 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001323 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001324 }
1325 }
1326
John Reck9c35b9c2012-05-30 10:08:50 -07001327 @Override
1328 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001329 Intent intent) {
1330 if (getCurrentTopWebView() == null) return;
1331 switch (requestCode) {
1332 case PREFERENCES_PAGE:
1333 if (resultCode == Activity.RESULT_OK && intent != null) {
1334 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001335 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001336 mTabControl.removeParentChildRelationShips();
1337 }
1338 }
1339 break;
1340 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001341 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001342 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001343 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001344 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001345 case AUTOFILL_SETUP:
1346 // Determine whether a profile was actually set up or not
1347 // and if so, send the message back to the WebTextView to
1348 // fill the form with the new profile.
1349 if (getSettings().getAutoFillProfile() != null) {
1350 mAutoFillSetupMessage.sendToTarget();
1351 mAutoFillSetupMessage = null;
1352 }
1353 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001354 case COMBO_VIEW:
1355 if (intent == null || resultCode != Activity.RESULT_OK) {
1356 break;
1357 }
John Reck3ba45532011-08-11 16:26:53 -07001358 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001359 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1360 Tab t = getCurrentTab();
1361 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001362 mUpdateMyNavThumbnail = true;
1363 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001364 loadUrl(t, uri.toString());
1365 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1366 String[] urls = intent.getStringArrayExtra(
1367 ComboViewActivity.EXTRA_OPEN_ALL);
1368 Tab parent = getCurrentTab();
1369 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001370 if (url != null) {
1371 parent = openTab(url, parent,
1372 !mSettings.openInBackground(), true);
1373 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001374 }
1375 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1376 long id = intent.getLongExtra(
1377 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1378 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001379 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001380 }
1381 }
1382 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001383 case VOICE_RESULT:
1384 if (resultCode == Activity.RESULT_OK && intent != null) {
1385 ArrayList<String> results = intent.getStringArrayListExtra(
1386 RecognizerIntent.EXTRA_RESULTS);
1387 if (results.size() >= 1) {
1388 mVoiceResult = results.get(0);
1389 }
1390 }
1391 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001392 case MY_NAVIGATION:
1393 if (intent == null || resultCode != Activity.RESULT_OK) {
1394 break;
1395 }
1396
1397 if (intent.getBooleanExtra("need_refresh", false) &&
1398 getCurrentTopWebView() != null) {
1399 getCurrentTopWebView().reload();
1400 }
1401 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001402 default:
1403 break;
1404 }
1405 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001406 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 }
1408
1409 /**
1410 * Open the Go page.
1411 * @param startWithHistory If true, open starting on the history tab.
1412 * Otherwise, start with the bookmarks tab.
1413 */
1414 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001415 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 if (mTabControl.getCurrentWebView() == null) {
1417 return;
1418 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001419 // clear action mode
1420 if (isInCustomActionMode()) {
1421 endActionMode();
1422 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 Bundle extras = new Bundle();
1424 // Disable opening in a new window if we have maxed out the windows
1425 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1426 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001427 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001428 }
1429
1430 // combo view callbacks
1431
Michael Kolb8233fac2010-10-26 16:08:53 -07001432 // key handling
1433 protected void onBackKey() {
1434 if (!mUi.onBackKey()) {
1435 WebView subwindow = mTabControl.getCurrentSubWindow();
1436 if (subwindow != null) {
1437 if (subwindow.canGoBack()) {
1438 subwindow.goBack();
1439 } else {
1440 dismissSubWindow(mTabControl.getCurrentTab());
1441 }
1442 } else {
1443 goBackOnePageOrQuit();
1444 }
1445 }
1446 }
1447
Michael Kolb4bd767d2011-05-27 11:33:55 -07001448 protected boolean onMenuKey() {
1449 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001450 }
1451
Michael Kolb8233fac2010-10-26 16:08:53 -07001452 // menu handling and state
1453 // TODO: maybe put into separate handler
1454
John Reck9c35b9c2012-05-30 10:08:50 -07001455 @Override
1456 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001457 if (mMenuState == EMPTY_MENU) {
1458 return false;
1459 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001460 MenuInflater inflater = mActivity.getMenuInflater();
1461 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 return true;
1463 }
1464
John Reck9c35b9c2012-05-30 10:08:50 -07001465 @Override
1466 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001467 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001468 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001469 return;
1470 }
1471 if (!(v instanceof WebView)) {
1472 return;
1473 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001474 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001475 WebView.HitTestResult result;
1476
1477 /* Determine whether the ContextMenu got triggered because
1478 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1479 * received. The mResult acts as a flag to identify, how it got trigerred
1480 */
1481 if (mResult == null){
1482 result = webview.getHitTestResult();
1483 } else {
1484 result = mResult;
1485 }
1486
Michael Kolb8233fac2010-10-26 16:08:53 -07001487 if (result == null) {
1488 return;
1489 }
1490
1491 int type = result.getType();
1492 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001493
1494 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1495 unknownTypeMap.put("webview", webview);
1496 final Message msg = mHandler.obtainMessage(
1497 UNKNOWN_TYPE_MSG, unknownTypeMap);
1498 /* As defined in android developers guide
1499 * when UNKNOWN_TYPE is received as a result of HitTest
1500 * you need to determing the type by invoking requestFocusNodeHref
1501 */
1502 webview.requestFocusNodeHref(msg);
1503
Michael Kolb8233fac2010-10-26 16:08:53 -07001504 Log.w(LOGTAG,
1505 "We should not show context menu when nothing is touched");
1506 return;
1507 }
1508 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1509 // let TextView handles context menu
1510 return;
1511 }
1512
1513 // Note, http://b/issue?id=1106666 is requesting that
1514 // an inflated menu can be used again. This is not available
1515 // yet, so inflate each time (yuk!)
1516 MenuInflater inflater = mActivity.getMenuInflater();
1517 inflater.inflate(R.menu.browsercontext, menu);
1518
1519 // Show the correct menu group
1520 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001521 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001522 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001523 menu.setGroupVisible(R.id.PHONE_MENU,
1524 type == WebView.HitTestResult.PHONE_TYPE);
1525 menu.setGroupVisible(R.id.EMAIL_MENU,
1526 type == WebView.HitTestResult.EMAIL_TYPE);
1527 menu.setGroupVisible(R.id.GEO_MENU,
1528 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001529 String itemUrl = null;
1530 String url = webview.getOriginalUrl();
1531 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1532 itemUrl = Uri.decode(navigationUrl);
1533 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1534 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1535 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1536 } else {
1537 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1538 }
1539 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1540 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1541 } else {
1542 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1543
1544 menu.setGroupVisible(R.id.IMAGE_MENU,
1545 type == WebView.HitTestResult.IMAGE_TYPE
1546 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1547 menu.setGroupVisible(R.id.ANCHOR_MENU,
1548 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1549 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001550
1551 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1552 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1553 UrlUtils.isDownloadableScheme(extra));
1554 }
1555 else {
1556 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1557 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001558 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 // Setup custom handling depending on the type
1560 switch (type) {
1561 case WebView.HitTestResult.PHONE_TYPE:
1562 menu.setHeaderTitle(Uri.decode(extra));
1563 menu.findItem(R.id.dial_context_menu_id).setIntent(
1564 new Intent(Intent.ACTION_VIEW, Uri
1565 .parse(WebView.SCHEME_TEL + extra)));
1566 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1567 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1568 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1569 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1570 addIntent);
1571 menu.findItem(R.id.copy_phone_context_menu_id)
1572 .setOnMenuItemClickListener(
1573 new Copy(extra));
1574 break;
1575
1576 case WebView.HitTestResult.EMAIL_TYPE:
1577 menu.setHeaderTitle(extra);
1578 menu.findItem(R.id.email_context_menu_id).setIntent(
1579 new Intent(Intent.ACTION_VIEW, Uri
1580 .parse(WebView.SCHEME_MAILTO + extra)));
1581 menu.findItem(R.id.copy_mail_context_menu_id)
1582 .setOnMenuItemClickListener(
1583 new Copy(extra));
1584 break;
1585
1586 case WebView.HitTestResult.GEO_TYPE:
1587 menu.setHeaderTitle(extra);
1588 menu.findItem(R.id.map_context_menu_id).setIntent(
1589 new Intent(Intent.ACTION_VIEW, Uri
1590 .parse(WebView.SCHEME_GEO
1591 + URLEncoder.encode(extra))));
1592 menu.findItem(R.id.copy_geo_context_menu_id)
1593 .setOnMenuItemClickListener(
1594 new Copy(extra));
1595 break;
1596
1597 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1598 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001599 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 // decide whether to show the open link in new tab option
1601 boolean showNewTab = mTabControl.canCreateNewTab();
1602 MenuItem newTabItem
1603 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001604 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001605 ? R.string.contextmenu_openlink_newwindow_background
1606 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001607 newTabItem.setVisible(showNewTab);
1608 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001609 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1610 newTabItem.setOnMenuItemClickListener(
1611 new MenuItem.OnMenuItemClickListener() {
1612 @Override
1613 public boolean onMenuItemClick(MenuItem item) {
1614 final HashMap<String, WebView> hrefMap =
1615 new HashMap<String, WebView>();
1616 hrefMap.put("webview", webview);
1617 final Message msg = mHandler.obtainMessage(
1618 FOCUS_NODE_HREF,
1619 R.id.open_newtab_context_menu_id,
1620 0, hrefMap);
1621 webview.requestFocusNodeHref(msg);
1622 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001624 });
1625 } else {
1626 newTabItem.setOnMenuItemClickListener(
1627 new MenuItem.OnMenuItemClickListener() {
1628 @Override
1629 public boolean onMenuItemClick(MenuItem item) {
1630 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001631 openTab(extra, parent,
1632 !mSettings.openInBackground(),
1633 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001634 return true;
1635 }
1636 });
1637 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001638 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001639 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1640 menu.setHeaderTitle(navigationUrl);
1641 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1642
1643 if (itemUrl != null) {
1644 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1645 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1646 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1647 @Override
1648 public boolean onMenuItemClick(MenuItem item) {
1649 final Intent intent = new Intent(Controller.this
1650 .getContext(),
1651 AddMyNavigationPage.class);
1652 Bundle bundle = new Bundle();
1653 String url = Uri.decode(navigationUrl);
1654 bundle.putBoolean("isAdding", false);
1655 bundle.putString("url", url);
1656 bundle.putString("name", getNameFromUrl(url));
1657 intent.putExtra("websites", bundle);
1658 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1659 return false;
1660 }
1661 });
1662 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1663 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1664 @Override
1665 public boolean onMenuItemClick(MenuItem item) {
1666 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1667 return false;
1668 }
1669 });
1670 }
1671 } else {
1672 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1673 }
1674 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001675 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1676 break;
1677 }
1678 // otherwise fall through to handle image part
1679 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001680 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1681 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001682 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1683 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001684 shareItem.setOnMenuItemClickListener(
1685 new MenuItem.OnMenuItemClickListener() {
1686 @Override
1687 public boolean onMenuItemClick(MenuItem item) {
1688 sharePage(mActivity, null, extra, null,
1689 null);
1690 return true;
1691 }
1692 }
1693 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001694 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001695 menu.findItem(R.id.view_image_context_menu_id)
1696 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1697 @Override
1698 public boolean onMenuItemClick(MenuItem item) {
1699 openTab(extra, mTabControl.getCurrentTab(), true, true);
1700 return false;
1701 }
1702 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001703 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1704 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1705 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001706 menu.findItem(R.id.set_wallpaper_context_menu_id).
1707 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1708 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001709 break;
1710
1711 default:
1712 Log.w(LOGTAG, "We should not get here.");
1713 break;
1714 }
1715 //update the ui
1716 mUi.onContextMenuCreated(menu);
1717 }
1718
kaiyiz6e5b3e02013-08-19 20:02:01 +08001719 public void startAddMyNavigation(String url) {
1720 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1721 Bundle bundle = new Bundle();
1722 bundle.putBoolean("isAdding", true);
1723 bundle.putString("url", url);
1724 bundle.putString("name", getNameFromUrl(url));
1725 intent.putExtra("websites", bundle);
1726 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1727 }
1728
1729 private void showMyNavigationDeleteDialog(final String itemUrl) {
1730 new AlertDialog.Builder(this.getContext())
1731 .setTitle(R.string.my_navigation_delete_label)
1732 .setIcon(android.R.drawable.ic_dialog_alert)
1733 .setMessage(R.string.my_navigation_delete_msg)
1734 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1735 @Override
1736 public void onClick(DialogInterface dialog, int whichButton) {
1737 deleteMyNavigationItem(itemUrl);
1738 }
1739 })
1740 .setNegativeButton(R.string.cancel, null)
1741 .show();
1742 }
1743
1744 private void deleteMyNavigationItem(final String itemUrl) {
1745 ContentResolver cr = this.getContext().getContentResolver();
1746 Cursor cursor = null;
1747
1748 try {
1749 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1750 new String[] {
1751 MyNavigationUtil.ID
1752 }, "url = ?", new String[] {
1753 itemUrl
1754 }, null);
1755 if (null != cursor && cursor.moveToFirst()) {
1756 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1757 cursor.getLong(0));
1758
1759 ContentValues values = new ContentValues();
1760 values.put(MyNavigationUtil.TITLE, "");
1761 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1762 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1763 ByteArrayOutputStream os = new ByteArrayOutputStream();
1764 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1765 R.raw.my_navigation_add);
1766 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1767 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1768 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1769 cr.update(uri, values, null, null);
1770 } else {
1771 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1772 }
1773 } catch (IllegalStateException e) {
1774 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1775 } finally {
1776 if (null != cursor) {
1777 cursor.close();
1778 }
1779 }
1780
1781 if (getCurrentTopWebView() != null) {
1782 getCurrentTopWebView().reload();
1783 }
1784 }
1785
1786 private String getNameFromUrl(String itemUrl) {
1787 ContentResolver cr = this.getContext().getContentResolver();
1788 Cursor cursor = null;
1789 String name = null;
1790
1791 try {
1792 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1793 new String[] {
1794 MyNavigationUtil.TITLE
1795 }, "url = ?", new String[] {
1796 itemUrl
1797 }, null);
1798 if (null != cursor && cursor.moveToFirst()) {
1799 name = cursor.getString(0);
1800 } else {
1801 Log.e(LOGTAG, "this item does not exist!");
1802 }
1803 } catch (IllegalStateException e) {
1804 Log.e(LOGTAG, "getNameFromUrl", e);
1805 } finally {
1806 if (null != cursor) {
1807 cursor.close();
1808 }
1809 }
1810 return name;
1811 }
1812
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001813 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001814 final ContentResolver cr = mActivity.getContentResolver();
1815 new AsyncTask<Void, Void, Void>() {
1816 @Override
1817 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001818 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1819 if(!isMyNavigationUrl)
1820 return null;
1821
kaiyiz6e5b3e02013-08-19 20:02:01 +08001822 ContentResolver cr = mActivity.getContentResolver();
1823 Cursor cursor = null;
1824 try {
1825 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1826 new String[] {
1827 MyNavigationUtil.ID
1828 }, "url = ?", new String[] {
1829 itemUrl
1830 }, null);
1831 if (null != cursor && cursor.moveToFirst()) {
1832 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001833 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001834
1835 ContentValues values = new ContentValues();
1836 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1837 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1838 cursor.getLong(0));
1839 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1840 cr.update(uri, values, null, null);
1841 os.close();
1842 }
1843 } catch (IllegalStateException e) {
1844 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1845 } catch (IOException e) {
1846 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1847 } finally {
1848 if (null != cursor) {
1849 cursor.close();
1850 }
1851 }
1852 return null;
1853 }
1854 }.execute();
1855 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001856 /**
1857 * As the menu can be open when loading state changes
1858 * we must manually update the state of the stop/reload menu
1859 * item
1860 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001861 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001862 if (menu == null) {
1863 return;
1864 }
1865 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001866 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001867 menu.findItem(R.id.stop_menu_id):
1868 menu.findItem(R.id.reload_menu_id);
1869 if (src != null) {
1870 dest.setIcon(src.getIcon());
1871 dest.setTitle(src.getTitle());
1872 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001873 mActivity.invalidateOptionsMenu();
1874 }
1875
1876 public void invalidateOptionsMenu() {
1877 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001878 }
1879
John Reck9c35b9c2012-05-30 10:08:50 -07001880 @Override
1881 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001882 // Software menu key (toolbar key)
1883 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1884 return true;
1885 }
1886
1887 @Override
1888 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001889 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001890 // hold on to the menu reference here; it is used by the page callbacks
1891 // to update the menu based on loading state
1892 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001893 // Note: setVisible will decide whether an item is visible; while
1894 // setEnabled() will decide whether an item is enabled, which also means
1895 // whether the matching shortcut key will function.
1896 switch (mMenuState) {
1897 case EMPTY_MENU:
1898 if (mCurrentMenuState != mMenuState) {
1899 menu.setGroupVisible(R.id.MAIN_MENU, false);
1900 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1901 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1902 }
1903 break;
1904 default:
1905 if (mCurrentMenuState != mMenuState) {
1906 menu.setGroupVisible(R.id.MAIN_MENU, true);
1907 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1908 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1909 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001910 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001911 break;
1912 }
1913 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001914 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001915
1916 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001917 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1918 EditBookmarksRestriction.getInstance()
1919 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001920 }
1921
1922 private void setMenuItemVisibility(Menu menu, int id,
1923 boolean visibility) {
1924 MenuItem item = menu.findItem(id);
1925 if (item != null) {
1926 item.setVisible(visibility);
1927 }
1928 }
1929
1930 private int lookupBookmark(String title, String url) {
1931 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001932 int count = 0;
1933 Cursor cursor = null;
1934 try {
1935 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1936 BookmarksLoader.PROJECTION,
1937 "title = ? OR url = ?",
1938 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001939 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001940 },
1941 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001942
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001943 if (cursor != null)
1944 count = cursor.getCount();
1945
1946 } catch (IllegalStateException e) {
1947 Log.e(LOGTAG, "lookupBookmark ", e);
1948 } finally {
1949 if (null != cursor) {
1950 cursor.close();
1951 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001952 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001953 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001954 }
1955
1956 private void resetMenuItems(Menu menu) {
1957 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1958 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1959
1960 WebView w = getCurrentTopWebView();
1961 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1962
1963 String title = w.getTitle();
1964 String url = w.getUrl();
Pankaj Garg634bf432015-07-13 09:54:21 -07001965 mCurrentPageBookmarked = (lookupBookmark(title, url) > 0);
1966 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001967 bookmark_icon.setChecked(true);
1968 } else {
1969 bookmark_icon.setChecked(false);
1970 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001971
1972 // update reader mode checkbox
1973 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1974 readerSwitcher.setVisible(false);
1975 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001976 }
1977
Michael Kolb4bf79712011-07-14 14:18:12 -07001978 @Override
1979 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001980 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001981 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001982 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001983 // Following flag is used to identify schemes for which the LIVE_MENU
1984 // items defined in res/menu/browser.xml should be enabled
1985 boolean isLiveScheme = false;
1986 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001987 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07001988
1989 boolean isDistillable = false;
1990 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001991 resetMenuItems(menu);
1992
Michael Kolb4bf79712011-07-14 14:18:12 -07001993 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001994 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001995 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001996 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001997 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001998 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001999 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07002000
2001 isDistillable = tab.isDistillable();
2002 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07002003 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002004
2005 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2006 forward.setEnabled(canGoForward);
2007
Michael Kolb4bf79712011-07-14 14:18:12 -07002008 // decide whether to show the share link option
2009 PackageManager pm = mActivity.getPackageManager();
2010 Intent send = new Intent(Intent.ACTION_SEND);
2011 send.setType("text/plain");
2012 ResolveInfo ri = pm.resolveActivity(send,
2013 PackageManager.MATCH_DEFAULT_ONLY);
2014 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2015
2016 boolean isNavDump = mSettings.enableNavDump();
2017 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2018 nav.setVisible(isNavDump);
2019 nav.setEnabled(isNavDump);
2020
2021 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002022 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2023 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002024 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002025 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2026 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002027 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002028 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2029 isLive && isLiveScheme && isPageFinished);
2030 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002031 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002032 // history and snapshots item are the members of COMBO menu group,
2033 // so if show history item, only make snapshots item invisible.
2034 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002035
Ze G Riande2a675c22015-06-03 11:15:24 -07002036
2037 // update reader mode checkbox
2038 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2039 // The reader mode checkbox is hidden only
2040 // when the current page is neither distillable nor distilled
2041 readerSwitcher.setVisible(isDistillable || isDistilled);
2042 readerSwitcher.setChecked(isDistilled);
2043
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002044 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002045 }
2046
John Reck9c35b9c2012-05-30 10:08:50 -07002047 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002048 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002049 if (null == getCurrentTopWebView()) {
2050 return false;
2051 }
2052 if (mMenuIsDown) {
2053 // The shortcut action consumes the MENU. Even if it is still down,
2054 // it won't trigger the next shortcut action. In the case of the
2055 // shortcut action triggering a new activity, like Bookmarks, we
2056 // won't get onKeyUp for MENU. So it is important to reset it here.
2057 mMenuIsDown = false;
2058 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002059 if (mUi.onOptionsItemSelected(item)) {
2060 // ui callback handled it
2061 return true;
2062 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002063 switch (item.getItemId()) {
2064 // -- Main menu
2065 case R.id.new_tab_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002066 getCurrentTab().capture();
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 openTabToHomePage();
2068 break;
2069
2070 case R.id.incognito_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002071 getCurrentTab().capture();
Michael Kolb519d2282011-05-09 17:03:19 -07002072 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002073 break;
2074
2075 case R.id.goto_menu_id:
2076 editUrl();
2077 break;
2078
2079 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002080 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2081 break;
2082
2083 case R.id.history_menu_id:
2084 bookmarksOrHistoryPicker(ComboViews.History);
2085 break;
2086
2087 case R.id.snapshots_menu_id:
2088 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002089 break;
2090
Pankaj Garg49b79252014-11-07 17:33:41 -08002091 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002092 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002093 break;
2094
2095 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002096 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002097 stopLoading();
2098 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002099 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002100 getCurrentTopWebView().reload();
2101 }
2102 break;
2103
Michael Kolb8233fac2010-10-26 16:08:53 -07002104 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002105 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002106 break;
2107
2108 case R.id.close_menu_id:
2109 // Close the subwindow if it exists.
2110 if (mTabControl.getCurrentSubWindow() != null) {
2111 dismissSubWindow(mTabControl.getCurrentTab());
2112 break;
2113 }
2114 closeCurrentTab();
2115 break;
2116
kaiyiza8b6dbb2013-07-29 18:11:22 +08002117 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002118 Object[] params = { new String("persist.debug.browsermonkeytest")};
2119 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002120 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002121 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002122 if (ret != null && ret.equals("enable"))
2123 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002124 if (BrowserConfig.getInstance(getContext())
2125 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2126 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002127 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002128 case R.id.homepage_menu_id:
2129 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002130 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002131 break;
2132
2133 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002134 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002135 break;
2136
2137 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002138 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 break;
2140
John Reck2bc80422011-06-30 15:11:49 -07002141 case R.id.save_snapshot_menu_id:
2142 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002143 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002144 createScreenshotAsync(
2145 source.getWebView(),
2146 getDesiredThumbnailWidth(mActivity),
2147 getDesiredThumbnailHeight(mActivity),
2148 new ValueCallback<Bitmap>() {
2149 @Override
2150 public void onReceiveValue(Bitmap bitmap) {
2151 new SaveSnapshotTask(source, bitmap).execute();
2152 }
2153 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002154 break;
2155
Michael Kolb8233fac2010-10-26 16:08:53 -07002156 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002157 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002158 break;
2159
John Recke1a03a32011-09-14 17:04:16 -07002160 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002161 // passing null to distinguish between
2162 // "go live" button and navigating a web page
2163 // on a snapshot tab
2164 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002165 case R.id.share_page_menu_id:
2166 Tab currentTab = mTabControl.getCurrentTab();
2167 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002168 return false;
2169 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002170 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002171 break;
2172
2173 case R.id.dump_nav_menu_id:
2174 getCurrentTopWebView().debugDump();
2175 break;
2176
Michael Kolb8233fac2010-10-26 16:08:53 -07002177 case R.id.zoom_in_menu_id:
2178 getCurrentTopWebView().zoomIn();
2179 break;
2180
2181 case R.id.zoom_out_menu_id:
2182 getCurrentTopWebView().zoomOut();
2183 break;
2184
2185 case R.id.view_downloads_menu_id:
2186 viewDownloads();
2187 break;
2188
John Reck42229bc2011-08-19 13:26:43 -07002189 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002190 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002191 break;
2192
Ze G Riande2a675c22015-06-03 11:15:24 -07002193 case R.id.reader_mode_menu_id:
2194 toggleReaderMode();
2195 break;
2196
Michael Kolb8233fac2010-10-26 16:08:53 -07002197 case R.id.window_one_menu_id:
2198 case R.id.window_two_menu_id:
2199 case R.id.window_three_menu_id:
2200 case R.id.window_four_menu_id:
2201 case R.id.window_five_menu_id:
2202 case R.id.window_six_menu_id:
2203 case R.id.window_seven_menu_id:
2204 case R.id.window_eight_menu_id:
2205 {
2206 int menuid = item.getItemId();
2207 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2208 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2209 Tab desiredTab = mTabControl.getTab(id);
2210 if (desiredTab != null &&
2211 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002212 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002213 }
2214 break;
2215 }
2216 }
2217 }
2218 break;
2219
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002220 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002221 Bundle bundle = new Bundle();
2222 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2223 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2224 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2225 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2226 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002227 break;
2228
Pankaj Garg49b79252014-11-07 17:33:41 -08002229 case R.id.add_to_homescreen:
2230 final WebView w = getCurrentTopWebView();
2231 final EditText input = new EditText(getContext());
2232 input.setText(w.getTitle());
2233 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002234 .setTitle(getContext().getResources().getString(
2235 R.string.add_to_homescreen))
2236 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002237 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002238 .setPositiveButton(getContext().getResources().getString(
2239 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002240 public void onClick(DialogInterface dialog, int whichButton) {
2241 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2242 getContext(),
2243 w.getUrl(),
2244 input.getText().toString(),
2245 w.getViewportBitmap(),
2246 w.getFavicon()));
2247
2248 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2249 .addCategory(Intent.CATEGORY_HOME));
2250 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002251 .setNegativeButton(getContext().getResources().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07002252 R.string.cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002253 public void onClick(DialogInterface dialog, int whichButton) {
2254 // Do nothing.
2255 }
2256 })
2257 .show();
2258 break;
2259
Michael Kolb8233fac2010-10-26 16:08:53 -07002260 default:
2261 return false;
2262 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002263 return true;
2264 }
2265
John Reck68234a92012-04-19 15:27:12 -07002266 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2267 implements OnCancelListener {
2268
2269 private Tab mTab;
2270 private Dialog mProgressDialog;
2271 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002272 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002273
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002274 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002275 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002276 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002277 }
2278
2279 @Override
2280 protected void onPreExecute() {
2281 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2282 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2283 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002284 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002285 }
2286
2287 @Override
2288 protected Long doInBackground(Void... params) {
2289 if (!mTab.saveViewState(mValues)) {
2290 return null;
2291 }
2292 if (isCancelled()) {
2293 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2294 File file = mActivity.getFileStreamPath(path);
2295 if (!file.delete()) {
2296 file.deleteOnExit();
2297 }
2298 return null;
2299 }
2300 final ContentResolver cr = mActivity.getContentResolver();
2301 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2302 if (result == null) {
2303 return null;
2304 }
2305 long id = ContentUris.parseId(result);
2306 return id;
2307 }
2308
2309 @Override
2310 protected void onPostExecute(Long id) {
2311 if (isCancelled()) {
2312 return;
2313 }
2314 mProgressDialog.dismiss();
2315 if (id == null) {
2316 Toast.makeText(mActivity, R.string.snapshot_failed,
2317 Toast.LENGTH_SHORT).show();
2318 return;
2319 }
2320 Bundle b = new Bundle();
2321 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2322 mUi.showComboView(ComboViews.Snapshots, b);
2323 }
2324
2325 @Override
2326 public void onCancel(DialogInterface dialog) {
2327 cancel(true);
2328 }
2329 }
2330
Michael Kolb80f75082012-04-10 10:50:06 -07002331 @Override
2332 public void toggleUserAgent() {
2333 WebView web = getCurrentWebView();
2334 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002335 }
2336
Ze G Riande2a675c22015-06-03 11:15:24 -07002337 // This function calls the method in the webview to enable/disable
2338 // the reader mode through the DOM distiller
2339 public void toggleReaderMode() {
2340 Tab t = mTabControl.getCurrentTab();
2341 if (t.isDistilled()) {
2342 closeTab(t);
2343 } else if (t.isDistillable()) {
2344 openTab(t.getDistilledUrl(), false, true, false, t);
2345 }
2346 }
2347
Michael Kolb80f75082012-04-10 10:50:06 -07002348 @Override
2349 public void findOnPage() {
2350 getCurrentTopWebView().showFindDialog(null, true);
2351 }
2352
2353 @Override
2354 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002355 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002356 }
2357
2358 @Override
2359 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002360 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002361 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2362 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002363 }
2364 else {
2365 WebView w = getCurrentTopWebView();
2366 if (w == null)
2367 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002368 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002369 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002370 }
Michael Kolb80f75082012-04-10 10:50:06 -07002371 }
2372
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002373 public boolean goLive(String url) {
2374 if (!getCurrentTab().isSnapshot())
2375 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002376 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002377
2378 if (url == null) { // "go live" button was clicked
2379 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002380 closeTab(t);
2381 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002382 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002383 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002384 return true;
John Recke1a03a32011-09-14 17:04:16 -07002385 }
2386
luxiaolb40014b2013-07-19 10:01:43 +08002387 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002388 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002389 new AlertDialog.Builder(activity)
2390 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002391 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002392 .setMessage(R.string.exit_browser_msg)
2393 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2394 public void onClick(DialogInterface dialog, int which) {
2395 activity.moveTaskToBack(true);
2396 dialog.dismiss();
2397 }
2398 })
2399 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2400 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002401 mCrashRecoveryHandler.clearState(true);
2402 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002403 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002404 dialog.dismiss();
2405 }
2406 })
2407 .show();
2408 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002409
Michael Kolb315d5022011-10-13 12:47:11 -07002410 @Override
2411 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002412 }
2413
John Reck9c35b9c2012-05-30 10:08:50 -07002414 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002415 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002416 // Let the History and Bookmark fragments handle menus they created.
2417 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2418 return false;
2419 }
2420
Michael Kolb8233fac2010-10-26 16:08:53 -07002421 int id = item.getItemId();
2422 boolean result = true;
2423 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002424 // -- Browser context menu
2425 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002426 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002427 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002428 case R.id.copy_link_context_menu_id:
2429 final WebView webView = getCurrentTopWebView();
2430 if (null == webView) {
2431 result = false;
2432 break;
2433 }
2434 final HashMap<String, WebView> hrefMap =
2435 new HashMap<String, WebView>();
2436 hrefMap.put("webview", webView);
2437 final Message msg = mHandler.obtainMessage(
2438 FOCUS_NODE_HREF, id, 0, hrefMap);
2439 webView.requestFocusNodeHref(msg);
2440 break;
2441
2442 default:
2443 // For other context menus
2444 result = onOptionsItemSelected(item);
2445 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002446 return result;
2447 }
2448
2449 /**
2450 * support programmatically opening the context menu
2451 */
2452 public void openContextMenu(View view) {
2453 mActivity.openContextMenu(view);
2454 }
2455
2456 /**
2457 * programmatically open the options menu
2458 */
2459 public void openOptionsMenu() {
2460 mActivity.openOptionsMenu();
2461 }
2462
John Reck9c35b9c2012-05-30 10:08:50 -07002463 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002464 public boolean onMenuOpened(int featureId, Menu menu) {
2465 if (mOptionsMenuOpen) {
2466 if (mConfigChanged) {
2467 // We do not need to make any changes to the state of the
2468 // title bar, since the only thing that happened was a
2469 // change in orientation
2470 mConfigChanged = false;
2471 } else {
2472 if (!mExtendedMenuOpen) {
2473 mExtendedMenuOpen = true;
2474 mUi.onExtendedMenuOpened();
2475 } else {
2476 // Switching the menu back to icon view, so show the
2477 // title bar once again.
2478 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002479 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002480 }
2481 }
2482 } else {
2483 // The options menu is closed, so open it, and show the title
2484 mOptionsMenuOpen = true;
2485 mConfigChanged = false;
2486 mExtendedMenuOpen = false;
2487 mUi.onOptionsMenuOpened();
2488 }
2489 return true;
2490 }
2491
John Reck9c35b9c2012-05-30 10:08:50 -07002492 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002493 public void onOptionsMenuClosed(Menu menu) {
2494 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002495 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002496 }
2497
John Reck9c35b9c2012-05-30 10:08:50 -07002498 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002499 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002500 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002501 }
2502
2503 // Helper method for getting the top window.
2504 @Override
2505 public WebView getCurrentTopWebView() {
2506 return mTabControl.getCurrentTopWebView();
2507 }
2508
2509 @Override
2510 public WebView getCurrentWebView() {
2511 return mTabControl.getCurrentWebView();
2512 }
2513
2514 /*
2515 * This method is called as a result of the user selecting the options
2516 * menu to see the download window. It shows the download window on top of
2517 * the current window.
2518 */
2519 void viewDownloads() {
2520 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2521 mActivity.startActivity(intent);
2522 }
2523
John Reck30b065e2011-07-19 10:58:05 -07002524 int getActionModeHeight() {
2525 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2526 new int[] { android.R.attr.actionBarSize });
2527 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2528 actionBarSizeTypedArray.recycle();
2529 return size;
2530 }
2531
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 // action mode
2533
John Reck9c35b9c2012-05-30 10:08:50 -07002534 @Override
2535 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002536 mUi.onActionModeStarted(mode);
2537 mActionMode = mode;
2538 }
2539
2540 /*
2541 * True if a custom ActionMode (i.e. find or select) is in use.
2542 */
2543 @Override
2544 public boolean isInCustomActionMode() {
2545 return mActionMode != null;
2546 }
2547
2548 /*
2549 * End the current ActionMode.
2550 */
2551 @Override
2552 public void endActionMode() {
2553 if (mActionMode != null) {
2554 mActionMode.finish();
2555 }
2556 }
2557
2558 /*
2559 * Called by find and select when they are finished. Replace title bars
2560 * as necessary.
2561 */
John Reck9c35b9c2012-05-30 10:08:50 -07002562 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002563 public void onActionModeFinished(ActionMode mode) {
2564 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002565 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002566 mActionMode = null;
2567 }
2568
2569 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002570 final Tab tab = getCurrentTab();
2571 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002572 }
2573
2574 // bookmark handling
2575
2576 /**
2577 * add the current page as a bookmark to the given folder id
2578 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002579 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002580 * bookmarked, and if it is, edit that bookmark. If false, and
2581 * the site is already bookmarked, do not attempt to edit the
2582 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002583 */
2584 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002585 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002586 WebView w = getCurrentTopWebView();
2587 if (w == null) {
2588 return null;
2589 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002590 Intent i = new Intent(mActivity,
2591 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002592 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2593 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002594 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002595 if (touchIconUrl != null) {
2596 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2597 WebSettings settings = w.getSettings();
2598 if (settings != null) {
2599 i.putExtra(AddBookmarkPage.USER_AGENT,
2600 settings.getUserAgentString());
2601 }
2602 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002603 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002604 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002605 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002606 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2607 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 // Put the dialog at the upper right of the screen, covering the
2609 // star on the title bar.
2610 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002611 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 }
2613
2614 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002615 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002616 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002617 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002618 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 }
2620
Vivek Sekharb54614f2014-05-01 19:03:37 -07002621 @Override
2622 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2623 boolean capture) {
2624 mUploadHandler = new UploadHandler(this);
2625 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2626 }
2627
Michael Kolb8233fac2010-10-26 16:08:53 -07002628 // thumbnails
2629
2630 /**
2631 * Return the desired width for thumbnail screenshots, which are stored in
2632 * the database, and used on the bookmarks screen.
2633 * @param context Context for finding out the density of the screen.
2634 * @return desired width for thumbnail screenshot.
2635 */
2636 static int getDesiredThumbnailWidth(Context context) {
2637 return context.getResources().getDimensionPixelOffset(
2638 R.dimen.bookmarkThumbnailWidth);
2639 }
2640
2641 /**
2642 * Return the desired height for thumbnail screenshots, which are stored in
2643 * the database, and used on the bookmarks screen.
2644 * @param context Context for finding out the density of the screen.
2645 * @return desired height for thumbnail screenshot.
2646 */
2647 static int getDesiredThumbnailHeight(Context context) {
2648 return context.getResources().getDimensionPixelOffset(
2649 R.dimen.bookmarkThumbnailHeight);
2650 }
2651
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002652 static void createScreenshotAsync(WebView view, int width, int height,
2653 final ValueCallback<Bitmap> cb) {
2654 if (view == null || width == 0 || height == 0) {
2655 return;
2656 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002657 view.getContentBitmapAsync(
2658 (float) width / view.getWidth(),
2659 new Rect(),
2660 new ValueCallback<Bitmap>() {
2661 @Override
2662 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002663 if (bitmap != null)
2664 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2665 cb.onReceiveValue(bitmap);
2666 }});
2667 }
2668
Michael Kolb8233fac2010-10-26 16:08:53 -07002669 private class Copy implements OnMenuItemClickListener {
2670 private CharSequence mText;
2671
John Reck9c35b9c2012-05-30 10:08:50 -07002672 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002673 public boolean onMenuItemClick(MenuItem item) {
2674 copy(mText);
2675 return true;
2676 }
2677
2678 public Copy(CharSequence toCopy) {
2679 mText = toCopy;
2680 }
2681 }
2682
Leon Scroggins63c02662010-11-18 15:16:27 -05002683 private static class Download implements OnMenuItemClickListener {
2684 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002685 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002686 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002687 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002688 private static final String FALLBACK_EXTENSION = "dat";
2689 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002690
John Reck9c35b9c2012-05-30 10:08:50 -07002691 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002692 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002693 if (DataUri.isDataUri(mText)) {
2694 saveDataUri();
2695 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002696 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002697 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002698 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002699 return true;
2700 }
2701
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002702 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2703 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002704 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002705 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002706 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002707 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002708 }
George Mount387d45d2011-10-07 15:57:53 -07002709
2710 /**
2711 * Treats mText as a data URI and writes its contents to a file
2712 * based on the current time.
2713 */
2714 private void saveDataUri() {
2715 FileOutputStream outputStream = null;
2716 try {
2717 DataUri uri = new DataUri(mText);
2718 File target = getTarget(uri);
2719 outputStream = new FileOutputStream(target);
2720 outputStream.write(uri.getData());
2721 final DownloadManager manager =
2722 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2723 manager.addCompletedDownload(target.getName(),
2724 mActivity.getTitle().toString(), false,
2725 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002726 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002727 } catch (IOException e) {
2728 Log.e(LOGTAG, "Could not save data URL");
2729 } finally {
2730 if (outputStream != null) {
2731 try {
2732 outputStream.close();
2733 } catch (IOException e) {
2734 // ignore close errors
2735 }
2736 }
2737 }
2738 }
2739
2740 /**
2741 * Creates a File based on the current time stamp and uses
2742 * the mime type of the DataUri to get the extension.
2743 */
2744 private File getTarget(DataUri uri) throws IOException {
2745 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002746 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002747 String nameBase = format.format(new Date());
2748 String mimeType = uri.getMimeType();
2749 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2750 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2751 if (extension == null) {
2752 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2753 extension = FALLBACK_EXTENSION;
2754 }
2755 extension = "." + extension; // createTempFile needs the '.'
2756 File targetFile = File.createTempFile(nameBase, extension, dir);
2757 return targetFile;
2758 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002759 }
2760
Cary Clark8974d282010-11-22 10:46:05 -05002761 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002762 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002763
John Reck9c35b9c2012-05-30 10:08:50 -07002764 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002765 public boolean onMenuItemClick(MenuItem item) {
2766 if (mWebView != null) {
2767 return mWebView.selectText();
2768 }
2769 return false;
2770 }
2771
2772 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002773 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002774 }
2775
2776 }
2777
Michael Kolb8233fac2010-10-26 16:08:53 -07002778 /********************** TODO: UI stuff *****************************/
2779
2780 // these methods have been copied, they still need to be cleaned up
2781
2782 /****************** tabs ***************************************************/
2783
2784 // basic tab interactions:
2785
2786 // it is assumed that tabcontrol already knows about the tab
2787 protected void addTab(Tab tab) {
2788 mUi.addTab(tab);
2789 }
2790
2791 protected void removeTab(Tab tab) {
2792 mUi.removeTab(tab);
2793 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002794 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002795 }
2796
Michael Kolbf2055602011-04-09 17:20:03 -07002797 @Override
2798 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002799 // monkey protection against delayed start
2800 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002801
2802 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2803 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002804 mTabControl.setCurrentTab(tab);
2805 // the tab is guaranteed to have a webview after setCurrentTab
2806 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002807
2808
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002809 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002810 //Purge active tabs
2811 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002812 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002813 }
2814
John Reck8bcafc12011-08-29 16:43:02 -07002815 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002816 Tab current = mTabControl.getCurrentTab();
2817 if (current != null
2818 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002819 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002820 }
2821 }
2822
John Reck26b18322011-06-21 13:08:58 -07002823 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002824 //Cancel navscreen request
2825 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002826 // Dismiss the subwindow if applicable.
2827 dismissSubWindow(appTab);
2828 // Since we might kill the WebView, remove it from the
2829 // content view first.
2830 mUi.detachTab(appTab);
2831 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002832 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002833 // TODO: analyze why the remove and add are necessary
2834 mUi.attachTab(appTab);
2835 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002836 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002837 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002838 } else {
2839 // If the tab was the current tab, we have to attach
2840 // it to the view system again.
2841 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002842 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002843 }
2844 }
2845
2846 // Remove the sub window if it exists. Also called by TabControl when the
2847 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002848 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002849 public void dismissSubWindow(Tab tab) {
2850 removeSubWindow(tab);
2851 // dismiss the subwindow. This will destroy the WebView.
2852 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002853 WebView wv = getCurrentTopWebView();
2854 if (wv != null) {
2855 wv.requestFocus();
2856 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002857 }
2858
2859 @Override
2860 public void removeSubWindow(Tab t) {
2861 if (t.getSubWebView() != null) {
2862 mUi.removeSubWindow(t.getSubViewContainer());
2863 }
2864 }
2865
2866 @Override
2867 public void attachSubWindow(Tab tab) {
2868 if (tab.getSubWebView() != null) {
2869 mUi.attachSubWindow(tab.getSubViewContainer());
2870 getCurrentTopWebView().requestFocus();
2871 }
2872 }
2873
Mathew Inwood29721c22011-06-29 17:55:24 +01002874 private Tab showPreloadedTab(final UrlData urlData) {
2875 if (!urlData.isPreloaded()) {
2876 return null;
2877 }
2878 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2879 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2880 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002881 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002882 // Could not submit query. Fallback to regular tab creation
2883 tabControl.destroy();
2884 return null;
2885 }
2886 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002887 // check tab count and make room for new tab
2888 if (!mTabControl.canCreateNewTab()) {
2889 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2890 if (leastUsed != null) {
2891 closeTab(leastUsed);
2892 }
2893 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002894 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002895 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002896 mTabControl.addPreloadedTab(t);
2897 addTab(t);
2898 setActiveTab(t);
2899 return t;
2900 }
2901
Michael Kolb7bcafde2011-05-09 13:55:59 -07002902 // open a non inconito tab with the given url data
2903 // and set as active tab
2904 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002905 Tab tab = showPreloadedTab(urlData);
2906 if (tab == null) {
2907 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002908 if ((tab != null) && !urlData.isEmpty()) {
2909 loadUrlDataIn(tab, urlData);
2910 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002911 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002912 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002913 }
2914
Michael Kolb843510f2010-12-09 10:51:49 -08002915 @Override
2916 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002917 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002918 }
2919
Michael Kolb8233fac2010-10-26 16:08:53 -07002920 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002921 public Tab openIncognitoTab() {
2922 return openTab(INCOGNITO_URI, true, true, false);
2923 }
2924
2925 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002926 public Tab openTab(String url, boolean incognito, boolean setActive,
2927 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002928 return openTab(url, incognito, setActive, useCurrent, null);
2929 }
2930
2931 @Override
2932 public Tab openTab(String url, Tab parent, boolean setActive,
2933 boolean useCurrent) {
2934 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2935 setActive, useCurrent, parent);
2936 }
2937
2938 public Tab openTab(String url, boolean incognito, boolean setActive,
2939 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002940 Tab tab = createNewTab(incognito, setActive, useCurrent);
2941 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002942 if (parent instanceof SnapshotTab) {
2943 addTab(tab);
2944 if (setActive)
2945 setActiveTab(tab);
2946 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002947 parent.addChildTab(tab);
2948 }
Michael Kolb519d2282011-05-09 17:03:19 -07002949 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002950 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002951 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002952 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002953 return tab;
2954 }
2955
2956 // this method will attempt to create a new tab
2957 // incognito: private browsing tab
2958 // setActive: ste tab as current tab
2959 // useCurrent: if no new tab can be created, return current tab
2960 private Tab createNewTab(boolean incognito, boolean setActive,
2961 boolean useCurrent) {
2962 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002963 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2964 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002965 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002966 if (mTabControl.canCreateNewTab()) {
2967 tab = mTabControl.createNewTab(incognito, !setActive);
2968 addTab(tab);
2969 if (setActive) {
2970 setActiveTab(tab);
2971 } else {
2972 tab.pause();
2973 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002974 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002975 if (useCurrent) {
2976 tab = mTabControl.getCurrentTab();
2977 reuseTab(tab, null);
2978 } else {
2979 mUi.showMaxTabsWarning();
2980 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002981 }
2982 }
2983 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002984 }
2985
John Reck2bc80422011-06-30 15:11:49 -07002986 @Override
2987 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2988 SnapshotTab tab = null;
2989 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07002990 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07002991 addTab(tab);
2992 if (setActive) {
2993 setActiveTab(tab);
2994 }
2995 } else {
2996 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002997 }
2998 return tab;
2999 }
3000
Michael Kolb8233fac2010-10-26 16:08:53 -07003001 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003002 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003003 * @return boolean True if we successfully switched to a different tab. If
3004 * the indexth tab is null, or if that tab is the same as
3005 * the current one, return false.
3006 */
3007 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003008 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003009 Tab currentTab = mTabControl.getCurrentTab();
3010 if (tab == null || tab == currentTab) {
3011 return false;
3012 }
3013 setActiveTab(tab);
3014 return true;
3015 }
3016
3017 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003018 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003019 closeCurrentTab(false);
3020 }
3021
3022 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003023 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003024 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003025 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003026 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003027 return;
3028 }
Michael Kolbc831b632011-05-11 09:30:34 -07003029 final Tab current = mTabControl.getCurrentTab();
3030 final int pos = mTabControl.getCurrentPosition();
3031 Tab newTab = current.getParent();
3032 if (newTab == null) {
3033 newTab = mTabControl.getTab(pos + 1);
3034 if (newTab == null) {
3035 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003036 }
3037 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003038 if (andQuit) {
3039 mTabControl.setCurrentTab(newTab);
3040 closeTab(current);
3041 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003042 // Close window
3043 closeTab(current);
3044 }
3045 }
3046
3047 /**
3048 * Close the tab, remove its associated title bar, and adjust mTabControl's
3049 * current tab to a valid value.
3050 */
3051 @Override
3052 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003053 if (tab == mTabControl.getCurrentTab()) {
3054 closeCurrentTab();
3055 } else {
3056 removeTab(tab);
3057 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003058 }
3059
Afzal Najamd4e33312012-04-26 01:54:01 -04003060 /**
3061 * Close all tabs except the current one
3062 */
3063 @Override
3064 public void closeOtherTabs() {
3065 int inactiveTabs = mTabControl.getTabCount() - 1;
3066 for (int i = inactiveTabs; i >= 0; i--) {
3067 Tab tab = mTabControl.getTab(i);
3068 if (tab != mTabControl.getCurrentTab()) {
3069 removeTab(tab);
3070 }
3071 }
3072 }
3073
Michael Kolb8233fac2010-10-26 16:08:53 -07003074 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003075 protected void loadUrlFromContext(String url) {
3076 Tab tab = getCurrentTab();
3077 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003078 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003079 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003080 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003081 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003082 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003083 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003084 }
3085 }
3086 }
3087
3088 /**
3089 * Load the URL into the given WebView and update the title bar
3090 * to reflect the new load. Call this instead of WebView.loadUrl
3091 * directly.
3092 * @param view The WebView used to load url.
3093 * @param url The URL to load.
3094 */
John Reck71e51422011-07-01 16:49:28 -07003095 @Override
3096 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003097 loadUrl(tab, url, null);
3098 }
3099
3100 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3101 if (tab != null) {
3102 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003103 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003104 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003105 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003106 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003107 }
3108
3109 /**
3110 * Load UrlData into a Tab and update the title bar to reflect the new
3111 * load. Call this instead of UrlData.loadIn directly.
3112 * @param t The Tab used to load.
3113 * @param data The UrlData being loaded.
3114 */
3115 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003116 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003117 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003118 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003119 } else {
John Reck38b39652012-06-05 09:22:59 -07003120 if (t != null && data.mDisableUrlOverride) {
3121 t.disableUrlOverridingForLoad();
3122 }
John Reck26b18322011-06-21 13:08:58 -07003123 loadUrl(t, data.mUrl, data.mHeaders);
3124 }
3125 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003126 }
3127
John Reck30c714c2010-12-16 17:30:34 -08003128 @Override
3129 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003130 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003131 //In case of tab can go back (aka tab has navigation entry) do nothing
3132 //else just load homepage in current tab.
3133 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003134 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003135 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003136 }
3137
3138 void goBackOnePageOrQuit() {
3139 Tab current = mTabControl.getCurrentTab();
3140 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003141 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3142 showExitDialog(mActivity);
3143 } else {
3144 /*
3145 * Instead of finishing the activity, simply push this to the back
3146 * of the stack and let ActivityManager to choose the foreground
3147 * activity. As BrowserActivity is singleTask, it will be always the
3148 * root of the task. So we can use either true or false for
3149 * moveTaskToBack().
3150 */
3151 mActivity.moveTaskToBack(true);
3152 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003153 return;
3154 }
John Reckef654f12011-07-12 16:42:08 -07003155 if (current.canGoBack()) {
3156 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 } else {
3158 // Check to see if we are closing a window that was created by
3159 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003160 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003161 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003162 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003163 // Now we close the other tab
3164 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003165 } else if (BrowserConfig.getInstance(getContext())
3166 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3167 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003168 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003169 /*
3170 * Instead of finishing the activity, simply push this to the back
3171 * of the stack and let ActivityManager to choose the foreground
3172 * activity. As BrowserActivity is singleTask, it will be always the
3173 * root of the task. So we can use either true or false for
3174 * moveTaskToBack().
3175 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003176 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003177 }
3178 }
3179 }
3180
3181 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003182 * helper method for key handler
3183 * returns the current tab if it can't advance
3184 */
Michael Kolbc831b632011-05-11 09:30:34 -07003185 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003186 int pos = mTabControl.getCurrentPosition() + 1;
3187 if (pos >= mTabControl.getTabCount()) {
3188 pos = 0;
3189 }
3190 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003191 }
3192
3193 /**
3194 * helper method for key handler
3195 * returns the current tab if it can't advance
3196 */
Michael Kolbc831b632011-05-11 09:30:34 -07003197 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003198 int pos = mTabControl.getCurrentPosition() - 1;
3199 if ( pos < 0) {
3200 pos = mTabControl.getTabCount() - 1;
3201 }
3202 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003203 }
3204
John Reckbcef87f2012-02-03 14:58:34 -08003205 boolean isMenuOrCtrlKey(int keyCode) {
3206 return (KeyEvent.KEYCODE_MENU == keyCode)
3207 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3208 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3209 }
3210
Michael Kolb0035fad2011-03-14 13:25:28 -07003211 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003212 * handle key events in browser
3213 *
3214 * @param keyCode
3215 * @param event
3216 * @return true if handled, false to pass to super
3217 */
John Reck9c35b9c2012-05-30 10:08:50 -07003218 @Override
3219 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003220 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3221 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003222 if (!mUi.isComboViewShowing()) {
3223 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3224 true, false);
3225 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003226 return true;
3227 }
3228
Cary Clark160bbb92011-01-10 11:17:07 -05003229 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003230 // Even if MENU is already held down, we need to call to super to open
3231 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003232 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003233 mMenuIsDown = true;
3234 return false;
3235 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003236
Cary Clark8ff8c662010-12-29 15:03:05 -05003237 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003238 Tab tab = getCurrentTab();
3239 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003240
Cary Clark160bbb92011-01-10 11:17:07 -05003241 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3242 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003243
Michael Kolb8233fac2010-10-26 16:08:53 -07003244 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003245 case KeyEvent.KEYCODE_TAB:
3246 if (event.isCtrlPressed()) {
3247 if (event.isShiftPressed()) {
3248 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003249 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003250 } else {
3251 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003252 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003253 }
3254 return true;
3255 }
3256 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003257 case KeyEvent.KEYCODE_SPACE:
3258 // WebView/WebTextView handle the keys in the KeyDown. As
3259 // the Activity's shortcut keys are only handled when WebView
3260 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003261 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003262 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003263 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003264 pageDown();
3265 }
3266 return true;
3267 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003268 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003269 event.startTracking();
3270 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003271 case KeyEvent.KEYCODE_FORWARD:
3272 if (!noModifiers) break;
3273 tab.goForward();
3274 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003275 case KeyEvent.KEYCODE_DPAD_LEFT:
3276 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003277 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003278 return true;
3279 }
3280 break;
3281 case KeyEvent.KEYCODE_DPAD_RIGHT:
3282 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003283 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003284 return true;
3285 }
3286 break;
3287 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003288 if (ctrl) {
3289 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003290 return true;
3291 }
3292 break;
Michael Kolba4183062011-01-16 10:43:21 -08003293// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003294 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003295 if (ctrl ) {
3296 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003297 return true;
3298 }
3299 break;
Michael Kolba4183062011-01-16 10:43:21 -08003300// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003301// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003302// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003303// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003304// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003305// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003306// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003307// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003308// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003309// case KeyEvent.KEYCODE_M: // unused
3310// case KeyEvent.KEYCODE_N: // in Chrome: new window
3311// case KeyEvent.KEYCODE_O: // in Chrome: open file
3312// case KeyEvent.KEYCODE_P: // in Chrome: print page
3313// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003314// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003315// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3316 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003317 // we can't use the ctrl/shift flags, they check for
3318 // exclusive use of a modifier
3319 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003320 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003321 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003322 } else {
3323 openTabToHomePage();
3324 }
3325 return true;
3326 }
3327 break;
3328// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3329// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003330// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003331// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3332// case KeyEvent.KEYCODE_Y: // unused
3333// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003334 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003335 // it is a regular key and webview is not null
3336 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003337 }
3338
John Reck9c35b9c2012-05-30 10:08:50 -07003339 @Override
3340 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003341 switch(keyCode) {
3342 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003343 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003344 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003345 return true;
3346 }
3347 break;
3348 }
3349 return false;
3350 }
3351
John Reck9c35b9c2012-05-30 10:08:50 -07003352 @Override
3353 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003354 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003355 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003356 if (KeyEvent.KEYCODE_MENU == keyCode
3357 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003358 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003359 }
3360 }
Cary Clark160bbb92011-01-10 11:17:07 -05003361 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003362 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003363 case KeyEvent.KEYCODE_BACK:
3364 if (event.isTracking() && !event.isCanceled()) {
3365 onBackKey();
3366 return true;
3367 }
3368 break;
3369 }
3370 return false;
3371 }
3372
3373 public boolean isMenuDown() {
3374 return mMenuIsDown;
3375 }
3376
John Reck9c35b9c2012-05-30 10:08:50 -07003377 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003378 public void setupAutoFill(Message message) {
3379 // Open the settings activity at the AutoFill profile fragment so that
3380 // the user can create a new profile. When they return, we will dispatch
3381 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003382 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003383 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3384 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003385 }
John Reckb3417f02011-01-14 11:01:05 -08003386
John Reck9c35b9c2012-05-30 10:08:50 -07003387 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003388 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003389 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003390 return true;
3391 }
3392
John Reck1cf4b792011-07-26 10:22:22 -07003393 @Override
3394 public boolean shouldCaptureThumbnails() {
3395 return mUi.shouldCaptureThumbnails();
3396 }
3397
Michael Kolbc3af0672011-08-09 10:24:41 -07003398 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003399 public boolean supportsVoice() {
3400 PackageManager pm = mActivity.getPackageManager();
3401 List activities = pm.queryIntentActivities(new Intent(
3402 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3403 return activities.size() != 0;
3404 }
3405
3406 @Override
3407 public void startVoiceRecognizer() {
3408 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003409 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003410 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3411 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3412 mActivity.startActivityForResult(voice, VOICE_RESULT);
3413 }
3414
Pankaj Garg7b279f62014-08-12 14:47:18 -07003415 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003416 if (mLevel == level)
3417 return;
3418 mLevel = level;
3419 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003420 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3421 lp.dimAmount = level;
3422 mActivity.getWindow().setAttributes(lp);
3423 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3424 } else {
3425 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3426 }
3427 }
3428
Michael Kolb0b129122012-06-04 16:31:58 -07003429 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003430 public void setBlockEvents(boolean block) {
3431 mBlockEvents = block;
3432 }
3433
John Reck9c35b9c2012-05-30 10:08:50 -07003434 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003435 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003436 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003437 }
3438
John Reck9c35b9c2012-05-30 10:08:50 -07003439 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003440 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003441 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003442 }
3443
John Reck9c35b9c2012-05-30 10:08:50 -07003444 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003445 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003446 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003447 }
3448
John Reck9c35b9c2012-05-30 10:08:50 -07003449 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003450 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003451 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003452 }
3453
John Reck9c35b9c2012-05-30 10:08:50 -07003454 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003455 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003456 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003457 }
3458
Pankaj Garg49b79252014-11-07 17:33:41 -08003459 @Override
3460 public boolean shouldShowAppMenu() {
3461 return true;
3462 }
3463
3464 @Override
3465 public int getMenuThemeResourceId() {
3466 return R.style.OverflowMenuTheme;
3467 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003468}