blob: cbc6ba05e2245fedcc5d30ef3f69a297f255c213 [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_";
Pankaj Garg75273bb2015-08-20 11:43:49 -0700137 public static final String EXTRA_UPDATED_URLS = "updated_urls";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800138
Vivek Sekharb54614f2014-05-01 19:03:37 -0700139 // Remind switch to data connection if wifi is unavailable
140 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800141
Michael Kolb8233fac2010-10-26 16:08:53 -0700142 // public message ids
143 public final static int LOAD_URL = 1001;
144 public final static int STOP_LOAD = 1002;
145
146 // Message Ids
147 private static final int FOCUS_NODE_HREF = 102;
148 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800149 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800152 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700153
154 private static final int EMPTY_MENU = -1;
155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700157 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 final static int PREFERENCES_PAGE = 3;
159 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700161 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800162 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000163
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
165
166 // As the ids are dynamically created, we can't guarantee that they will
167 // be in sequence, so this static array maps ids to a window number.
168 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
169 { R.id.window_one_menu_id, R.id.window_two_menu_id,
170 R.id.window_three_menu_id, R.id.window_four_menu_id,
171 R.id.window_five_menu_id, R.id.window_six_menu_id,
172 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
173
174 // "source" parameter for Google search through search key
175 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
176 // "source" parameter for Google search through simplily type
177 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
178
George Mount387d45d2011-10-07 15:57:53 -0700179 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700180 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
181
John Reckd7dd9b22011-08-30 09:18:29 -0700182 // A bitmap that is re-used in createScreenshot as scratch space
183 private static Bitmap sThumbnailBitmap;
184
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 private Activity mActivity;
186 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700187 private HomepageHandler mHomepageHandler;
188 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private BrowserSettings mSettings;
190 private WebViewFactory mFactory;
191
192 private WakeLock mWakeLock;
193
194 private UrlHandler mUrlHandler;
195 private UploadHandler mUploadHandler;
196 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 private NetworkStateHandler mNetworkHandler;
198
Ben Murdoch8029a772010-11-16 11:58:21 +0000199 private Message mAutoFillSetupMessage;
200
kaiyiza016da12013-08-26 17:50:22 +0800201 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700202
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 // FIXME, temp address onPrepareMenu performance problem.
204 // When we move everything out of view, we should rewrite this.
205 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700206 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 private int mOldMenuState = EMPTY_MENU;
208 private Menu mCachedMenu;
209
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 private boolean mMenuIsDown;
211
Pankaj Garg49b79252014-11-07 17:33:41 -0800212 private boolean mWasInPageLoad = false;
213 private AppMenuHandler mAppMenuHandler;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 // For select and find, we keep track of the ActionMode so that
216 // finish() can be called as desired.
217 private ActionMode mActionMode;
218
219 /**
220 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
221 * of whether the configuration has changed. The first onMenuOpened call
222 * after a configuration change is simply a reopening of the same menu
223 * (i.e. mIconView did not change).
224 */
225 private boolean mConfigChanged;
226
227 /**
228 * Keeps track of whether the options menu is open. This is important in
229 * determining whether to show or hide the title bar overlay
230 */
231 private boolean mOptionsMenuOpen;
232
233 /**
234 * Whether or not the options menu is in its bigger, popup menu form. When
235 * true, we want the title bar overlay to be gone. When false, we do not.
236 * Only meaningful if mOptionsMenuOpen is true.
237 */
238 private boolean mExtendedMenuOpen;
239
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 private boolean mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700241 private boolean mActivityStopped = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 private boolean mLoadStopped;
243
244 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500245 // Checks to see when the bookmarks database has changed, and updates the
246 // Tabs' notion of whether they represent bookmarked sites.
247 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700248 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700249
Michael Kolbc3af0672011-08-09 10:24:41 -0700250 private boolean mBlockEvents;
251
Michael Kolb0b129122012-06-04 16:31:58 -0700252 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800253 private boolean mUpdateMyNavThumbnail;
254 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800255 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800256 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700257 private PowerConnectionReceiver mLowPowerReceiver;
258 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700259
Pankaj Garg634bf432015-07-13 09:54:21 -0700260 private boolean mCurrentPageBookmarked;
261
George Mount3636d0a2011-11-21 09:08:21 -0800262 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 mActivity = browser;
264 mSettings = BrowserSettings.getInstance();
265 mTabControl = new TabControl(this);
266 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700267 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800268 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700269 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700270
271 mUrlHandler = new UrlHandler(this);
272 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700273
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500275 mBookmarksObserver = new ContentObserver(mHandler) {
276 @Override
277 public void onChange(boolean selfChange) {
278 int size = mTabControl.getTabCount();
279 for (int i = 0; i < size; i++) {
280 mTabControl.getTab(i).updateBookmarkedStatus();
281 }
282 }
283
284 };
285 browser.getContentResolver().registerContentObserver(
286 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700287
288 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700289 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800290 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700291 }
292
John Reck9c35b9c2012-05-30 10:08:50 -0700293 @Override
294 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700295 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800296 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800297 // mCrashRecoverHandler has any previously saved state.
298 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700299 }
300
George Mount3636d0a2011-11-21 09:08:21 -0800301 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800302 // we dont want to ever recover incognito tabs
303 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700304
Patrick Scott7d50a932011-02-04 09:27:26 -0500305 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700306 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500307 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000308
Michael Kolbc831b632011-05-11 09:30:34 -0700309 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 // Not able to restore so we go ahead and clear session cookies. We
311 // must do this before trying to login the user as we don't want to
312 // clear any session cookies set during login.
313 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700314 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800315 if (intent == null) {
316 // This won't happen under common scenarios. The icicle is
317 // not null, but there aren't any tabs to restore.
318 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700319 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800320 final Bundle extra = intent.getExtras();
321 // Create an initial tab.
322 // If the intent is ACTION_VIEW and data is not null, the Browser is
323 // invoked to view the content by another application. In this case,
324 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800325 UrlData urlData = null;
326 if (intent.getData() != null
327 && Intent.ACTION_VIEW.equals(intent.getAction())
328 && intent.getData().toString().startsWith("content://")) {
329 urlData = new UrlData(intent.getData().toString());
330 } else {
331 urlData = IntentHandler.getUrlDataFromIntent(intent);
332 }
George Mount3636d0a2011-11-21 09:08:21 -0800333 Tab t = null;
334 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700335 String landingPage = mActivity.getResources().getString(
336 R.string.def_landing_page);
337 if (!landingPage.isEmpty()) {
338 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800339 } else {
340 t = openTabToHomePage();
341 }
George Mount3636d0a2011-11-21 09:08:21 -0800342 } else {
343 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700344 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800345 }
346 if (t != null) {
347 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
348 }
349 WebView webView = t.getWebView();
350 if (extra != null) {
351 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
352 if (scale > 0 && scale <= 1000) {
353 webView.setInitialScale(scale);
354 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 }
356 }
John Reckd8c74522011-06-14 08:45:00 -0700357 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700359 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500360 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700361 List<Tab> tabs = mTabControl.getTabs();
362 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700363
John Reck1cf4b792011-07-26 10:22:22 -0700364 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700365 //handle restored pages that may require a JS interface
366 if (t.getWebView() != null) {
367 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
368 if (backForwardList != null) {
369 for (int i = 0; i < backForwardList.getSize(); i++) {
370 WebHistoryItem item = backForwardList.getItemAtIndex(i);
371 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
372 }
373 }
374 }
John Reck1cf4b792011-07-26 10:22:22 -0700375 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700376 if (t != mTabControl.getCurrentTab()) {
377 t.pause();
378 }
John Reck1cf4b792011-07-26 10:22:22 -0700379 }
380 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700381 if (tabs.size() == 0) {
382 openTabToHomePage();
383 }
John Reck1cf4b792011-07-26 10:22:22 -0700384 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700385 // TabControl.restoreState() will create a new tab even if
386 // restoring the state fails.
387 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800388 // Intent is non-null when framework thinks the browser should be
389 // launching with a new intent (icicle is null).
390 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700391 mIntentHandler.onNewIntent(intent);
392 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700394 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700395 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800396 if (jsFlags.trim().length() != 0) {
397 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700398 }
George Mount3636d0a2011-11-21 09:08:21 -0800399 if (intent != null
400 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700401 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800402 }
Site Maoabb7bd32015-03-20 15:34:02 -0700403 mLowPowerReceiver = new PowerConnectionReceiver();
404 mPowerChangeReceiver = new PowerConnectionReceiver();
405
406 //always track the android framework's power save mode
407 IntentFilter filter = new IntentFilter();
408 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
409 // Power save mode only exists in Lollipop and above
410 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
411 }
412 filter.addAction(Intent.ACTION_BATTERY_OKAY);
413 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700414 }
415
John Reck1cf4b792011-07-26 10:22:22 -0700416 private static class PruneThumbnails implements Runnable {
417 private Context mContext;
418 private List<Long> mIds;
419
420 PruneThumbnails(Context context, List<Long> preserveIds) {
421 mContext = context.getApplicationContext();
422 mIds = preserveIds;
423 }
424
425 @Override
426 public void run() {
427 ContentResolver cr = mContext.getContentResolver();
428 if (mIds == null || mIds.size() == 0) {
429 cr.delete(Thumbnails.CONTENT_URI, null, null);
430 } else {
431 int length = mIds.size();
432 StringBuilder where = new StringBuilder();
433 where.append(Thumbnails._ID);
434 where.append(" not in (");
435 for (int i = 0; i < length; i++) {
436 where.append(mIds.get(i));
437 if (i < (length - 1)) {
438 where.append(",");
439 }
440 }
441 where.append(")");
442 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
443 }
444 }
445
446 }
447
Michael Kolb1514bb72010-11-22 09:11:48 -0800448 @Override
449 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700450 return mFactory;
451 }
452
453 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800454 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700455 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700456 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800457 }
458
459 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800460 public void createSubWindow(Tab tab) {
461 endActionMode();
462 WebView mainView = tab.getWebView();
463 WebView subView = mFactory.createWebView((mainView == null)
464 ? false
465 : mainView.isPrivateBrowsingEnabled());
466 mUi.createSubWindow(tab, subView);
467 }
468
469 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700470 public Context getContext() {
471 return mActivity;
472 }
473
474 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 public Activity getActivity() {
476 return mActivity;
477 }
478
479 void setUi(UI ui) {
480 mUi = ui;
481 }
482
Michael Kolbaf63dba2012-05-16 12:58:05 -0700483 @Override
484 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700485 return mSettings;
486 }
487
488 IntentHandler getIntentHandler() {
489 return mIntentHandler;
490 }
491
492 @Override
493 public UI getUi() {
494 return mUi;
495 }
496
497 int getMaxTabs() {
498 return mActivity.getResources().getInteger(R.integer.max_tabs);
499 }
500
501 @Override
502 public TabControl getTabControl() {
503 return mTabControl;
504 }
505
Michael Kolb1bf23132010-11-19 12:55:12 -0800506 @Override
507 public List<Tab> getTabs() {
508 return mTabControl.getTabs();
509 }
510
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 private void startHandler() {
512 mHandler = new Handler() {
513
514 @Override
515 public void handleMessage(Message msg) {
516 switch (msg.what) {
517 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700518 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800520 case UNKNOWN_TYPE_MSG:
521 HashMap unknownTypeMap = (HashMap) msg.obj;
522 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
523 /*
524 * When the context menu is shown to the user
525 * we need to assure that its happening on the current webview
526 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
527 */
528 if (getCurrentWebView() != viewForUnknownType)
529 break;
530
531 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800532 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800533 WebView.HitTestResult result = new WebView.HitTestResult();
534
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800535 // Prevent unnecessary calls to context menu
536 // if url and image src are null
537 if (unknown_type_src == null && unknown_type_url == null)
538 break;
539
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800540 //setting the HitTestResult with new RESULT TYPE
541 if (!TextUtils.isEmpty(unknown_type_src)) {
542 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
543 result.setExtra(unknown_type_src);
544 } else {
545 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
546 result.setExtra("about:blank");
547 }
548
549 mResult = result;
550 openContextMenu(viewForUnknownType);
551 mResult = null;
552
553 break;
554
Michael Kolb8233fac2010-10-26 16:08:53 -0700555 case FOCUS_NODE_HREF:
556 {
557 String url = (String) msg.getData().get("url");
558 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500559 String src = (String) msg.getData().get("src");
560 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 if (TextUtils.isEmpty(url)) {
562 break;
563 }
564 HashMap focusNodeMap = (HashMap) msg.obj;
565 WebView view = (WebView) focusNodeMap.get("webview");
566 // Only apply the action if the top window did not change.
567 if (getCurrentTopWebView() != view) {
568 break;
569 }
570 switch (msg.arg1) {
571 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700572 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700573 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500574 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700575 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500576 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500577 case R.id.open_newtab_context_menu_id:
578 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700579 openTab(url, parent,
580 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500581 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700582 case R.id.copy_link_context_menu_id:
583 copy(url);
584 break;
585 case R.id.save_link_context_menu_id:
586 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500587 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800588 mActivity, url, view.getSettings().getUserAgentString(),
589 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700591 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700592 if(title == null || title == "")
593 title = url;
594
595 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
596 //SWE TODO: No thumbnail support for the url obtained via
597 //browser context menu as its not loaded in webview.
598 if (bookmarkIntent != null) {
599 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
600 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
601 mActivity.startActivity(bookmarkIntent);
602 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700603 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700604 }
605 break;
606 }
607
608 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700609 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700610 break;
611
612 case STOP_LOAD:
613 stopLoading();
614 break;
615
616 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700617 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 mWakeLock.release();
619 // if we reach here, Browser should be still in the
620 // background loading after WAKELOCK_TIMEOUT (5-min).
621 // To avoid burning the battery, stop loading.
622 mTabControl.stopAllLoading();
623 }
624 break;
625
kaiyiz591110b2013-08-06 17:11:06 +0800626 case OPEN_MENU:
627 if (!mOptionsMenuOpen && mActivity != null ) {
628 mActivity.openOptionsMenu();
629 }
630 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700631 }
632 }
633 };
634
635 }
636
John Reckef654f12011-07-12 16:42:08 -0700637 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200638 public Tab getCurrentTab() {
639 return mTabControl.getCurrentTab();
640 }
641
Michael Kolbba99c5d2010-11-29 14:57:41 -0800642 @Override
643 public void shareCurrentPage() {
644 shareCurrentPage(mTabControl.getCurrentTab());
645 }
646
647 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700648 if (tab == null || tab.getWebView() == null)
649 return;
650
651 final Tab mytab = tab;
652 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
653 @Override
654 public void onReceiveValue(Bitmap bitmap) {
655 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
656 mytab.getFavicon(), bitmap);
657 }
658 };
659
660 createScreenshotAsync(
661 tab.getWebView(),
662 getDesiredThumbnailWidth(mActivity),
663 getDesiredThumbnailHeight(mActivity),
664 new ValueCallback<Bitmap>() {
665 @Override
666 public void onReceiveValue(Bitmap bitmap) {
667 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
668 mytab.getFavicon(), bitmap);
669 }
670 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800671 }
672
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 /**
674 * Share a page, providing the title, url, favicon, and a screenshot. Uses
675 * an {@link Intent} to launch the Activity chooser.
676 * @param c Context used to launch a new Activity.
677 * @param title Title of the page. Stored in the Intent with
678 * {@link Intent#EXTRA_SUBJECT}
679 * @param url URL of the page. Stored in the Intent with
680 * {@link Intent#EXTRA_TEXT}
681 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
682 * with {@link Browser#EXTRA_SHARE_FAVICON}
683 * @param screenshot Bitmap of a screenshot of the page. Stored in the
684 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
685 */
686 static final void sharePage(Context c, String title, String url,
687 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700688
689 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
690 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
691 R.layout.app_row, sDialog.getApps());
692 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700693 }
694
695 private void copy(CharSequence text) {
696 ClipboardManager cm = (ClipboardManager) mActivity
697 .getSystemService(Context.CLIPBOARD_SERVICE);
698 cm.setText(text);
699 }
700
701 // lifecycle
702
John Reck9c35b9c2012-05-30 10:08:50 -0700703 @Override
704 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800706 // update the menu in case of a locale change
707 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800708 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800709 if (mOptionsMenuOpen) {
710 mActivity.closeOptionsMenu();
711 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
712 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 mUi.onConfigurationChanged(config);
714 }
715
716 @Override
717 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700718 if (!mUi.isWebShowing()) {
719 mUi.showWeb(false);
720 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700721 mIntentHandler.onNewIntent(intent);
722 }
723
John Reck9c35b9c2012-05-30 10:08:50 -0700724 @Override
725 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800726 if (mUi.isCustomViewShowing()) {
727 hideCustomView();
728 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700729 if (mActivityPaused) {
730 Log.e(LOGTAG, "BrowserActivity is already paused.");
731 return;
732 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700734
Michael Kolb8233fac2010-10-26 16:08:53 -0700735 }
736
John Reck9c35b9c2012-05-30 10:08:50 -0700737 @Override
738 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800740 Bundle saveState = createSaveState();
741
742 // crash recovery manages all save & restore state
743 mCrashRecoveryHandler.writeState(saveState);
744 mSettings.setLastRunPaused(true);
745 }
746
747 /**
748 * Save the current state to outState. Does not write the state to
749 * disk.
750 * @return Bundle containing the current state of all tabs.
751 */
752 /* package */ Bundle createSaveState() {
753 Bundle saveState = new Bundle();
754 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800755 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 }
757
John Reck9c35b9c2012-05-30 10:08:50 -0700758 @Override
759 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700760 if (!mActivityPaused) {
761 Log.e(LOGTAG, "BrowserActivity is already resumed.");
762 return;
763 }
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700764 mActivityPaused = false;
765 if (mVoiceResult != null) {
766 mUi.onVoiceResult(mVoiceResult);
767 mVoiceResult = null;
768 }
769 }
770
771 private void releaseWakeLock() {
772 if (mWakeLock != null && mWakeLock.isHeld()) {
773 mHandler.removeMessages(RELEASE_WAKELOCK);
774 mWakeLock.release();
775 }
776 }
777
778 @Override
779 public void onStop() {
780 if (mActivityStopped) {
781 Log.e(LOGTAG, "BrowserActivity is already stoped.");
782 return;
783 }
784 mActivityStopped = true;
785 Tab tab = mTabControl.getCurrentTab();
786 if (tab != null) {
787 tab.pause();
788 if (!pauseWebViewTimers(tab)) {
789 if (mWakeLock == null) {
790 PowerManager pm = (PowerManager) mActivity
791 .getSystemService(Context.POWER_SERVICE);
792 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
793 }
794 mWakeLock.acquire();
795 mHandler.sendMessageDelayed(mHandler
796 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
797 }
798 }
799 mUi.onPause();
800 mNetworkHandler.onPause();
801 NfcHandler.unregister(mActivity);
802 mActivity.unregisterReceiver(mLowPowerReceiver);
803 }
804
805 @Override
806 public void onStart() {
807 if (!mActivityStopped) {
808 Log.e(LOGTAG, "BrowserActivity is already started.");
809 return;
810 }
811 mActivityStopped = false;
Vivek Sekharb53a5952015-07-17 14:14:38 -0700812 UpdateNotificationService.updateCheck(mActivity);
George Mount3636d0a2011-11-21 09:08:21 -0800813 mSettings.setLastRunPaused(false);
Michael Kolb70976932010-11-30 11:34:01 -0800814 Tab current = mTabControl.getCurrentTab();
815 if (current != null) {
816 current.resume();
817 resumeWebViewTimers(current);
818 }
John Reckf57c0292011-07-21 18:15:39 -0700819 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200820
Michael Kolb8233fac2010-10-26 16:08:53 -0700821 mUi.onResume();
822 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100823 NfcHandler.register(mActivity, this);
Vivek Sekharc70ae632015-04-08 17:54:05 -0700824 if (current != null && current.getWebView().isShowingCrashView())
825 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700826 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700827
John Reckf57c0292011-07-21 18:15:39 -0700828 }
829
Michael Kolb70976932010-11-30 11:34:01 -0800830 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800831 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800832 * @param tab guaranteed non-null
833 */
834 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700835 boolean inLoad = tab.inPageLoad();
Tarun Nainani7c762c82015-08-04 13:42:09 -0700836 if ((!mActivityStopped && !inLoad) || (mActivityStopped && inLoad)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 CookieSyncManager.getInstance().startSync();
838 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100839 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 }
841 }
842
Michael Kolb70976932010-11-30 11:34:01 -0800843 /**
844 * Pause all WebView timers using the WebView of the given tab
845 * @param tab
846 * @return true if the timers are paused or tab is null
847 */
848 private boolean pauseWebViewTimers(Tab tab) {
849 if (tab == null) {
850 return true;
851 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100853 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 }
Michael Kolb70976932010-11-30 11:34:01 -0800856 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700857 }
858
John Reck9c35b9c2012-05-30 10:08:50 -0700859 @Override
860 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800861 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
863 mUploadHandler = null;
864 }
865 if (mTabControl == null) return;
866 mUi.onDestroy();
867 // Remove the current tab and sub window
868 Tab t = mTabControl.getCurrentTab();
869 if (t != null) {
870 dismissSubWindow(t);
871 removeTab(t);
872 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500873 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700874 // Destroy all the tabs
875 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700876 // Unregister receiver
877 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 }
879
880 protected boolean isActivityPaused() {
881 return mActivityPaused;
882 }
883
John Reck9c35b9c2012-05-30 10:08:50 -0700884 @Override
885 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 mTabControl.freeMemory();
887 }
888
889 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 public void stopLoading() {
891 mLoadStopped = true;
892 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700894 if (w != null) {
895 w.stopLoading();
896 mUi.onPageStopped(tab);
897 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700898 }
899
900 boolean didUserStopLoading() {
901 return mLoadStopped;
902 }
903
kaiyiza016da12013-08-26 17:50:22 +0800904 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700905 final String reminderType = getContext().getResources().getString(
906 R.string.def_wifi_browser_interaction_remind_type);
907 final String selectionConnnection = getContext().getResources().getString(
908 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700909 final String wifiSelection = getContext().getResources().getString(
910 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700911
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700912 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
913 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700914 return;
915
kaiyiza016da12013-08-26 17:50:22 +0800916 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700917 Context.CONNECTIVITY_SERVICE);
918 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700919 WifiManager wifiMgr = (WifiManager) this.getContext()
920 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700921 if (networkInfo == null
922 || (networkInfo != null && (networkInfo.getType() !=
923 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700924 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
925 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700926 List<ScanResult> list = wifiMgr.getScanResults();
927 // Have no AP's for Wifi's fall back to data
928 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700929 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700930 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
931 this.getContext().startActivity(intent);
932 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700933 // Request to select Wifi AP
934 try {
935 Intent intent = new Intent(wifiSelection);
936 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
937 this.getContext().startActivity(intent);
938 } catch (Exception e) {
939 String err_msg = this.getContext().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700940 R.string.activity_not_found, wifiSelection);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700941 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
942 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700943 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700944 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800945 }
946 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700947
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 // WebViewController
949
950 @Override
John Reck324d4402011-01-11 16:56:42 -0800951 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700952
Michael Kolb8233fac2010-10-26 16:08:53 -0700953 // reset sync timer to avoid sync starts during loading a page
954 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700955 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700956 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800957 boolean networkNotifier = BrowserConfig.getInstance(getContext())
958 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700959 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700960 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800961 } else {
962 if (!mNetworkHandler.isNetworkUp()) {
963 view.setNetworkAvailable(false);
964 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 }
966
967 // when BrowserActivity just starts, onPageStarted may be called before
968 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
969 // to start the timer. As we won't switch tabs while an activity is in
970 // pause state, we can ensure calling resume and pause in pair.
Tarun Nainani7c762c82015-08-04 13:42:09 -0700971 if (mActivityStopped) {
Michael Kolb70976932010-11-30 11:34:01 -0800972 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700973 }
974 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700975 endActionMode();
976
John Reck30c714c2010-12-16 17:30:34 -0800977 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700978
John Reck324d4402011-01-11 16:56:42 -0800979 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 // update the bookmark database for favicon
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700981 syncBookmarkFavicon(tab, null, url, favicon);
Michael Kolb8233fac2010-10-26 16:08:53 -0700982
983 Performance.tracePageStart(url);
984
985 // Performance probe
986 if (false) {
987 Performance.onPageStarted();
988 }
989
990 }
991
992 @Override
John Reck324d4402011-01-11 16:56:42 -0800993 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700994 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800995 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200996
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 // Performance probe
998 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800999 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001000 }
1001
Tarun Nainani8eb00912014-07-17 12:28:32 -07001002 tab.onPageFinished();
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001003 syncBookmarkFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -07001004
Michael Kolb8233fac2010-10-26 16:08:53 -07001005 Performance.tracePageFinished();
1006 }
1007
1008 @Override
John Reck30c714c2010-12-16 17:30:34 -08001009 public void onProgressChanged(Tab tab) {
1010 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001011
1012 if (newProgress == 100) {
1013 CookieSyncManager.getInstance().sync();
1014 // onProgressChanged() may continue to be called after the main
1015 // frame has finished loading, as any remaining sub frames continue
1016 // to load. We'll only get called once though with newProgress as
1017 // 100 when everything is loaded. (onPageFinished is called once
1018 // when the main frame completes loading regardless of the state of
1019 // any sub frames so calls to onProgressChanges may continue after
1020 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001021 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001022 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001023 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001024 } else if (mWasInPageLoad) {
1025 mWasInPageLoad = false;
1026 updateInLoadMenuItems(mCachedMenu, tab);
1027 }
1028
Tarun Nainani7c762c82015-08-04 13:42:09 -07001029 if (mActivityStopped && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001030 // pause the WebView timer and release the wake lock if it is
1031 // finished while BrowserActivity is in pause state.
1032 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001033 }
1034 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001035 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001036 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001037 // still loading
1038 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001040 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001041 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001042 } else {
1043 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 }
1045 }
Pankaj Gargf9040c82015-08-14 10:19:31 -07001046
1047 mUi.onProgressChanged(tab);
John Reck30c714c2010-12-16 17:30:34 -08001048 }
1049
1050 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001051 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001052 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001053 }
1054
1055 @Override
1056 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001057 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001058 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001059 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1061 return;
1062 }
1063 // Update the title in the history database if not in private browsing mode
1064 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001065 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001066 }
1067 }
1068
1069 @Override
1070 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001071 syncBookmarkFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1072 ((BaseUi)mUi).setFavicon(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001073 }
1074
1075 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001076 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001077 // if tab is snapshot tab we want to prevent navigation from occuring
1078 // since snapshot tab opens a new tab with the url
1079 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001080 }
1081
1082 @Override
1083 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1084 if (mMenuIsDown) {
1085 // only check shortcut key when MENU is held
1086 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1087 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001088 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001089 int keyCode = event.getKeyCode();
1090 // We need to send almost every key to WebKit. However:
1091 // 1. We don't want to block the device on the renderer for
1092 // some keys like menu, home, call.
1093 // 2. There are no WebKit equivalents for some of these keys
1094 // (see app/keyboard_codes_win.h)
1095 // Note that these are not the same set as KeyEvent.isSystemKey:
1096 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1097 if (keyCode == KeyEvent.KEYCODE_MENU ||
1098 keyCode == KeyEvent.KEYCODE_HOME ||
1099 keyCode == KeyEvent.KEYCODE_BACK ||
1100 keyCode == KeyEvent.KEYCODE_CALL ||
1101 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1102 keyCode == KeyEvent.KEYCODE_POWER ||
1103 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1104 keyCode == KeyEvent.KEYCODE_CAMERA ||
1105 keyCode == KeyEvent.KEYCODE_FOCUS ||
1106 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1107 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1108 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1109 return true;
1110 }
1111
1112 // We also have to intercept some shortcuts before we send them to the ContentView.
1113 if (event.isCtrlPressed() && (
1114 keyCode == KeyEvent.KEYCODE_TAB ||
1115 keyCode == KeyEvent.KEYCODE_W ||
1116 keyCode == KeyEvent.KEYCODE_F4)) {
1117 return true;
1118 }
1119
1120 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001121 }
1122
Tarun Nainanif5563b62015-01-21 18:52:59 -08001123 private void handleMediaKeyEvent(KeyEvent event) {
1124
1125 int keyCode = event.getKeyCode();
1126 // send media key events to audio manager
1127 if (Build.VERSION.SDK_INT >= 19) {
1128
1129 switch (keyCode) {
1130 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1131 case KeyEvent.KEYCODE_MEDIA_STOP:
1132 case KeyEvent.KEYCODE_MEDIA_NEXT:
1133 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1134 case KeyEvent.KEYCODE_MEDIA_REWIND:
1135 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1136 case KeyEvent.KEYCODE_MUTE:
1137 case KeyEvent.KEYCODE_MEDIA_PLAY:
1138 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1139 case KeyEvent.META_SHIFT_RIGHT_ON:
1140 case KeyEvent.KEYCODE_MEDIA_EJECT:
1141 case KeyEvent.KEYCODE_MEDIA_RECORD:
1142 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1143
1144 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1145 .getSystemService(Context.AUDIO_SERVICE);
1146 audioManager.dispatchMediaKeyEvent(event);
1147 }
1148 }
1149 }
1150
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 @Override
John Reck997b1b72012-04-19 18:08:25 -07001152 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001153 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001154 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001156 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 } else {
John Reck997b1b72012-04-19 18:08:25 -07001158 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 }
1160 }
John Reck997b1b72012-04-19 18:08:25 -07001161 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001162 }
1163
1164 @Override
John Reck324d4402011-01-11 16:56:42 -08001165 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001166 // Don't save anything in private browsing mode or when disabling history
1167 // for regular tabs is enabled
1168 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1169 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1170 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001171
John Reck49a603c2011-03-03 09:33:05 -08001172 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001173
John Reck324d4402011-01-11 16:56:42 -08001174 if (TextUtils.isEmpty(url)
1175 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001176 return;
1177 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001178
John Reckf57c0292011-07-21 18:15:39 -07001179 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001180 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001181 }
1182
1183 @Override
1184 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1185 AsyncTask<Void, Void, String[]> task =
1186 new AsyncTask<Void, Void, String[]>() {
1187 @Override
1188 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001189 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001190 }
1191 @Override
1192 public void onPostExecute(String[] result) {
1193 callback.onReceiveValue(result);
1194 }
1195 };
1196 task.execute();
1197 }
1198
1199 @Override
1200 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1201 final HttpAuthHandler handler, final String host,
1202 final String realm) {
1203 String username = null;
1204 String password = null;
1205
1206 boolean reuseHttpAuthUsernamePassword
1207 = handler.useHttpAuthUsernamePassword();
1208
1209 if (reuseHttpAuthUsernamePassword && view != null) {
1210 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1211 if (credentials != null && credentials.length == 2) {
1212 username = credentials[0];
1213 password = credentials[1];
1214 }
1215 }
1216
1217 if (username != null && password != null) {
1218 handler.proceed(username, password);
1219 } else {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -07001220 if (!tab.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001221 handler.cancel();
1222 }
1223 }
1224 }
1225
1226 @Override
1227 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001228 String contentDisposition, String mimetype, String referer,
1229 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001230 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001231 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001232 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001233 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001234 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 // This Tab was opened for the sole purpose of downloading a
1236 // file. Remove it.
1237 if (tab == mTabControl.getCurrentTab()) {
1238 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001239 if (tab.getDerivedFromIntent())
1240 closeTab(tab);
1241 else
1242 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 } else {
1244 // In this case, it is not.
1245 closeTab(tab);
1246 }
1247 }
1248 }
1249
1250 @Override
1251 public Bitmap getDefaultVideoPoster() {
1252 return mUi.getDefaultVideoPoster();
1253 }
1254
1255 @Override
1256 public View getVideoLoadingProgressView() {
1257 return mUi.getVideoLoadingProgressView();
1258 }
1259
Michael Kolb8233fac2010-10-26 16:08:53 -07001260 // helper method
1261
1262 /*
1263 * Update the favorites icon if the private browsing isn't enabled and the
1264 * icon is valid.
1265 */
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001266 private void syncBookmarkFavicon(Tab tab, final String originalUrl,
1267 final String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 if (favicon == null) {
1269 return;
1270 }
1271 if (!tab.isPrivateBrowsingEnabled()) {
1272 Bookmarks.updateFavicon(mActivity
1273 .getContentResolver(), originalUrl, url, favicon);
1274 }
1275 }
1276
Leon Scroggins4cd97792010-12-03 15:31:56 -05001277 @Override
1278 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001279 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001280 mUi.bookmarkedStatusHasChanged(tab);
1281 }
1282
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 // end WebViewController
1284
1285 protected void pageUp() {
1286 getCurrentTopWebView().pageUp(false);
1287 }
1288
1289 protected void pageDown() {
1290 getCurrentTopWebView().pageDown(false);
1291 }
1292
1293 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001294 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 public void editUrl() {
1296 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001297 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001298 }
1299
John Reck9c35b9c2012-05-30 10:08:50 -07001300 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001301 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001302 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001303 if (tab.inForeground()) {
1304 if (mUi.isCustomViewShowing()) {
1305 callback.onCustomViewHidden();
1306 return;
1307 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001308 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001309 // Save the menu state and set it to empty while the custom
1310 // view is showing.
1311 mOldMenuState = mMenuState;
1312 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001313 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001314 }
1315 }
1316
1317 @Override
1318 public void hideCustomView() {
1319 if (mUi.isCustomViewShowing()) {
1320 mUi.onHideCustomView();
1321 // Reset the old menu state.
1322 mMenuState = mOldMenuState;
1323 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001324 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001325 }
1326 }
1327
John Reck9c35b9c2012-05-30 10:08:50 -07001328 @Override
1329 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001330 Intent intent) {
1331 if (getCurrentTopWebView() == null) return;
1332 switch (requestCode) {
1333 case PREFERENCES_PAGE:
1334 if (resultCode == Activity.RESULT_OK && intent != null) {
1335 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001336 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 mTabControl.removeParentChildRelationShips();
Pankaj Garg75273bb2015-08-20 11:43:49 -07001338 } else if (action.equals(PreferenceKeys.ACTION_RELOAD_PAGE)) {
1339 ArrayList<String> origins =
1340 intent.getStringArrayListExtra(EXTRA_UPDATED_URLS);
1341 if (origins.isEmpty()) {
1342 mTabControl.reloadLiveTabs();
1343 }
1344 else{
1345 for (String origin : origins){
1346 mTabControl.findAndReload(origin);
1347 }
1348 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001349 }
1350 }
1351 break;
1352 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001353 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001354 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001355 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001356 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001357 case AUTOFILL_SETUP:
1358 // Determine whether a profile was actually set up or not
1359 // and if so, send the message back to the WebTextView to
1360 // fill the form with the new profile.
1361 if (getSettings().getAutoFillProfile() != null) {
1362 mAutoFillSetupMessage.sendToTarget();
1363 mAutoFillSetupMessage = null;
1364 }
1365 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001366 case COMBO_VIEW:
1367 if (intent == null || resultCode != Activity.RESULT_OK) {
1368 break;
1369 }
John Reck3ba45532011-08-11 16:26:53 -07001370 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001371 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1372 Tab t = getCurrentTab();
1373 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001374 mUpdateMyNavThumbnail = true;
1375 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001376 loadUrl(t, uri.toString());
1377 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1378 String[] urls = intent.getStringArrayExtra(
1379 ComboViewActivity.EXTRA_OPEN_ALL);
1380 Tab parent = getCurrentTab();
1381 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001382 if (url != null) {
1383 parent = openTab(url, parent,
1384 !mSettings.openInBackground(), true);
1385 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001386 }
1387 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1388 long id = intent.getLongExtra(
1389 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1390 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001391 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001392 }
1393 }
1394 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001395 case VOICE_RESULT:
1396 if (resultCode == Activity.RESULT_OK && intent != null) {
1397 ArrayList<String> results = intent.getStringArrayListExtra(
1398 RecognizerIntent.EXTRA_RESULTS);
1399 if (results.size() >= 1) {
1400 mVoiceResult = results.get(0);
1401 }
1402 }
1403 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001404 case MY_NAVIGATION:
1405 if (intent == null || resultCode != Activity.RESULT_OK) {
1406 break;
1407 }
1408
1409 if (intent.getBooleanExtra("need_refresh", false) &&
1410 getCurrentTopWebView() != null) {
1411 getCurrentTopWebView().reload();
1412 }
1413 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 default:
1415 break;
1416 }
1417 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001418 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 }
1420
1421 /**
1422 * Open the Go page.
1423 * @param startWithHistory If true, open starting on the history tab.
1424 * Otherwise, start with the bookmarks tab.
1425 */
1426 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001427 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001428 if (mTabControl.getCurrentWebView() == null) {
1429 return;
1430 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001431 // clear action mode
1432 if (isInCustomActionMode()) {
1433 endActionMode();
1434 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 Bundle extras = new Bundle();
1436 // Disable opening in a new window if we have maxed out the windows
1437 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1438 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001439 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001440 }
1441
1442 // combo view callbacks
1443
Michael Kolb8233fac2010-10-26 16:08:53 -07001444 // key handling
1445 protected void onBackKey() {
1446 if (!mUi.onBackKey()) {
1447 WebView subwindow = mTabControl.getCurrentSubWindow();
1448 if (subwindow != null) {
1449 if (subwindow.canGoBack()) {
1450 subwindow.goBack();
1451 } else {
1452 dismissSubWindow(mTabControl.getCurrentTab());
1453 }
1454 } else {
1455 goBackOnePageOrQuit();
1456 }
1457 }
1458 }
1459
Michael Kolb4bd767d2011-05-27 11:33:55 -07001460 protected boolean onMenuKey() {
1461 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001462 }
1463
Michael Kolb8233fac2010-10-26 16:08:53 -07001464 // menu handling and state
1465 // TODO: maybe put into separate handler
1466
John Reck9c35b9c2012-05-30 10:08:50 -07001467 @Override
1468 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001469 if (mMenuState == EMPTY_MENU) {
1470 return false;
1471 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001472 MenuInflater inflater = mActivity.getMenuInflater();
1473 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001474 return true;
1475 }
1476
John Reck9c35b9c2012-05-30 10:08:50 -07001477 @Override
1478 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001479 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001480 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001481 return;
1482 }
1483 if (!(v instanceof WebView)) {
1484 return;
1485 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001486 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001487 WebView.HitTestResult result;
1488
1489 /* Determine whether the ContextMenu got triggered because
1490 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1491 * received. The mResult acts as a flag to identify, how it got trigerred
1492 */
1493 if (mResult == null){
1494 result = webview.getHitTestResult();
1495 } else {
1496 result = mResult;
1497 }
1498
Michael Kolb8233fac2010-10-26 16:08:53 -07001499 if (result == null) {
1500 return;
1501 }
1502
1503 int type = result.getType();
1504 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001505
1506 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1507 unknownTypeMap.put("webview", webview);
1508 final Message msg = mHandler.obtainMessage(
1509 UNKNOWN_TYPE_MSG, unknownTypeMap);
1510 /* As defined in android developers guide
1511 * when UNKNOWN_TYPE is received as a result of HitTest
1512 * you need to determing the type by invoking requestFocusNodeHref
1513 */
1514 webview.requestFocusNodeHref(msg);
1515
Michael Kolb8233fac2010-10-26 16:08:53 -07001516 Log.w(LOGTAG,
1517 "We should not show context menu when nothing is touched");
1518 return;
1519 }
1520 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1521 // let TextView handles context menu
1522 return;
1523 }
1524
1525 // Note, http://b/issue?id=1106666 is requesting that
1526 // an inflated menu can be used again. This is not available
1527 // yet, so inflate each time (yuk!)
1528 MenuInflater inflater = mActivity.getMenuInflater();
1529 inflater.inflate(R.menu.browsercontext, menu);
1530
1531 // Show the correct menu group
1532 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001533 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001534 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001535 menu.setGroupVisible(R.id.PHONE_MENU,
1536 type == WebView.HitTestResult.PHONE_TYPE);
1537 menu.setGroupVisible(R.id.EMAIL_MENU,
1538 type == WebView.HitTestResult.EMAIL_TYPE);
1539 menu.setGroupVisible(R.id.GEO_MENU,
1540 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001541 String itemUrl = null;
1542 String url = webview.getOriginalUrl();
1543 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1544 itemUrl = Uri.decode(navigationUrl);
1545 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1546 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1547 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1548 } else {
1549 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1550 }
1551 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1552 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1553 } else {
1554 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1555
1556 menu.setGroupVisible(R.id.IMAGE_MENU,
1557 type == WebView.HitTestResult.IMAGE_TYPE
1558 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1559 menu.setGroupVisible(R.id.ANCHOR_MENU,
1560 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1561 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001562
1563 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1564 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1565 UrlUtils.isDownloadableScheme(extra));
1566 }
1567 else {
1568 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1569 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001570 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001571 // Setup custom handling depending on the type
1572 switch (type) {
1573 case WebView.HitTestResult.PHONE_TYPE:
1574 menu.setHeaderTitle(Uri.decode(extra));
1575 menu.findItem(R.id.dial_context_menu_id).setIntent(
1576 new Intent(Intent.ACTION_VIEW, Uri
1577 .parse(WebView.SCHEME_TEL + extra)));
1578 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1579 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1580 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1581 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1582 addIntent);
1583 menu.findItem(R.id.copy_phone_context_menu_id)
1584 .setOnMenuItemClickListener(
1585 new Copy(extra));
1586 break;
1587
1588 case WebView.HitTestResult.EMAIL_TYPE:
1589 menu.setHeaderTitle(extra);
1590 menu.findItem(R.id.email_context_menu_id).setIntent(
1591 new Intent(Intent.ACTION_VIEW, Uri
1592 .parse(WebView.SCHEME_MAILTO + extra)));
1593 menu.findItem(R.id.copy_mail_context_menu_id)
1594 .setOnMenuItemClickListener(
1595 new Copy(extra));
1596 break;
1597
1598 case WebView.HitTestResult.GEO_TYPE:
1599 menu.setHeaderTitle(extra);
1600 menu.findItem(R.id.map_context_menu_id).setIntent(
1601 new Intent(Intent.ACTION_VIEW, Uri
1602 .parse(WebView.SCHEME_GEO
1603 + URLEncoder.encode(extra))));
1604 menu.findItem(R.id.copy_geo_context_menu_id)
1605 .setOnMenuItemClickListener(
1606 new Copy(extra));
1607 break;
1608
1609 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1610 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001611 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001612 // decide whether to show the open link in new tab option
1613 boolean showNewTab = mTabControl.canCreateNewTab();
1614 MenuItem newTabItem
1615 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001616 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001617 ? R.string.contextmenu_openlink_newwindow_background
1618 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001619 newTabItem.setVisible(showNewTab);
1620 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001621 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1622 newTabItem.setOnMenuItemClickListener(
1623 new MenuItem.OnMenuItemClickListener() {
1624 @Override
1625 public boolean onMenuItemClick(MenuItem item) {
1626 final HashMap<String, WebView> hrefMap =
1627 new HashMap<String, WebView>();
1628 hrefMap.put("webview", webview);
1629 final Message msg = mHandler.obtainMessage(
1630 FOCUS_NODE_HREF,
1631 R.id.open_newtab_context_menu_id,
1632 0, hrefMap);
1633 webview.requestFocusNodeHref(msg);
1634 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001635 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001636 });
1637 } else {
1638 newTabItem.setOnMenuItemClickListener(
1639 new MenuItem.OnMenuItemClickListener() {
1640 @Override
1641 public boolean onMenuItemClick(MenuItem item) {
1642 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001643 openTab(extra, parent,
1644 !mSettings.openInBackground(),
1645 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001646 return true;
1647 }
1648 });
1649 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001650 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001651 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1652 menu.setHeaderTitle(navigationUrl);
1653 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1654
1655 if (itemUrl != null) {
1656 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1657 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1658 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1659 @Override
1660 public boolean onMenuItemClick(MenuItem item) {
1661 final Intent intent = new Intent(Controller.this
1662 .getContext(),
1663 AddMyNavigationPage.class);
1664 Bundle bundle = new Bundle();
1665 String url = Uri.decode(navigationUrl);
1666 bundle.putBoolean("isAdding", false);
1667 bundle.putString("url", url);
1668 bundle.putString("name", getNameFromUrl(url));
1669 intent.putExtra("websites", bundle);
1670 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1671 return false;
1672 }
1673 });
1674 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1675 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1676 @Override
1677 public boolean onMenuItemClick(MenuItem item) {
1678 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1679 return false;
1680 }
1681 });
1682 }
1683 } else {
1684 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1685 }
1686 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001687 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1688 break;
1689 }
1690 // otherwise fall through to handle image part
1691 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001692 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1693 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001694 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1695 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001696 shareItem.setOnMenuItemClickListener(
1697 new MenuItem.OnMenuItemClickListener() {
1698 @Override
1699 public boolean onMenuItemClick(MenuItem item) {
1700 sharePage(mActivity, null, extra, null,
1701 null);
1702 return true;
1703 }
1704 }
1705 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001706 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001707 menu.findItem(R.id.view_image_context_menu_id)
1708 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1709 @Override
1710 public boolean onMenuItemClick(MenuItem item) {
1711 openTab(extra, mTabControl.getCurrentTab(), true, true);
1712 return false;
1713 }
1714 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001715 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1716 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1717 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001718 menu.findItem(R.id.set_wallpaper_context_menu_id).
1719 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1720 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001721 break;
1722
1723 default:
1724 Log.w(LOGTAG, "We should not get here.");
1725 break;
1726 }
1727 //update the ui
1728 mUi.onContextMenuCreated(menu);
1729 }
1730
kaiyiz6e5b3e02013-08-19 20:02:01 +08001731 public void startAddMyNavigation(String url) {
1732 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1733 Bundle bundle = new Bundle();
1734 bundle.putBoolean("isAdding", true);
1735 bundle.putString("url", url);
1736 bundle.putString("name", getNameFromUrl(url));
1737 intent.putExtra("websites", bundle);
1738 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1739 }
1740
1741 private void showMyNavigationDeleteDialog(final String itemUrl) {
1742 new AlertDialog.Builder(this.getContext())
1743 .setTitle(R.string.my_navigation_delete_label)
1744 .setIcon(android.R.drawable.ic_dialog_alert)
1745 .setMessage(R.string.my_navigation_delete_msg)
1746 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1747 @Override
1748 public void onClick(DialogInterface dialog, int whichButton) {
1749 deleteMyNavigationItem(itemUrl);
1750 }
1751 })
1752 .setNegativeButton(R.string.cancel, null)
1753 .show();
1754 }
1755
1756 private void deleteMyNavigationItem(final String itemUrl) {
1757 ContentResolver cr = this.getContext().getContentResolver();
1758 Cursor cursor = null;
1759
1760 try {
1761 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1762 new String[] {
1763 MyNavigationUtil.ID
1764 }, "url = ?", new String[] {
1765 itemUrl
1766 }, null);
1767 if (null != cursor && cursor.moveToFirst()) {
1768 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1769 cursor.getLong(0));
1770
1771 ContentValues values = new ContentValues();
1772 values.put(MyNavigationUtil.TITLE, "");
1773 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1774 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1775 ByteArrayOutputStream os = new ByteArrayOutputStream();
1776 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1777 R.raw.my_navigation_add);
1778 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1779 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1780 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1781 cr.update(uri, values, null, null);
1782 } else {
1783 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1784 }
1785 } catch (IllegalStateException e) {
1786 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1787 } finally {
1788 if (null != cursor) {
1789 cursor.close();
1790 }
1791 }
1792
1793 if (getCurrentTopWebView() != null) {
1794 getCurrentTopWebView().reload();
1795 }
1796 }
1797
1798 private String getNameFromUrl(String itemUrl) {
1799 ContentResolver cr = this.getContext().getContentResolver();
1800 Cursor cursor = null;
1801 String name = null;
1802
1803 try {
1804 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1805 new String[] {
1806 MyNavigationUtil.TITLE
1807 }, "url = ?", new String[] {
1808 itemUrl
1809 }, null);
1810 if (null != cursor && cursor.moveToFirst()) {
1811 name = cursor.getString(0);
1812 } else {
1813 Log.e(LOGTAG, "this item does not exist!");
1814 }
1815 } catch (IllegalStateException e) {
1816 Log.e(LOGTAG, "getNameFromUrl", e);
1817 } finally {
1818 if (null != cursor) {
1819 cursor.close();
1820 }
1821 }
1822 return name;
1823 }
1824
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001825 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001826 final ContentResolver cr = mActivity.getContentResolver();
1827 new AsyncTask<Void, Void, Void>() {
1828 @Override
1829 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001830 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1831 if(!isMyNavigationUrl)
1832 return null;
1833
kaiyiz6e5b3e02013-08-19 20:02:01 +08001834 ContentResolver cr = mActivity.getContentResolver();
1835 Cursor cursor = null;
1836 try {
1837 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1838 new String[] {
1839 MyNavigationUtil.ID
1840 }, "url = ?", new String[] {
1841 itemUrl
1842 }, null);
1843 if (null != cursor && cursor.moveToFirst()) {
1844 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001845 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001846
1847 ContentValues values = new ContentValues();
1848 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1849 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1850 cursor.getLong(0));
1851 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1852 cr.update(uri, values, null, null);
1853 os.close();
1854 }
1855 } catch (IllegalStateException e) {
1856 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1857 } catch (IOException e) {
1858 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1859 } finally {
1860 if (null != cursor) {
1861 cursor.close();
1862 }
1863 }
1864 return null;
1865 }
1866 }.execute();
1867 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001868 /**
1869 * As the menu can be open when loading state changes
1870 * we must manually update the state of the stop/reload menu
1871 * item
1872 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001873 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001874 if (menu == null) {
1875 return;
1876 }
1877 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001878 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001879 menu.findItem(R.id.stop_menu_id):
1880 menu.findItem(R.id.reload_menu_id);
1881 if (src != null) {
1882 dest.setIcon(src.getIcon());
1883 dest.setTitle(src.getTitle());
1884 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001885 mActivity.invalidateOptionsMenu();
1886 }
1887
1888 public void invalidateOptionsMenu() {
1889 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001890 }
1891
John Reck9c35b9c2012-05-30 10:08:50 -07001892 @Override
1893 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001894 // Software menu key (toolbar key)
1895 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1896 return true;
1897 }
1898
1899 @Override
1900 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001901 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001902 // hold on to the menu reference here; it is used by the page callbacks
1903 // to update the menu based on loading state
1904 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001905 // Note: setVisible will decide whether an item is visible; while
1906 // setEnabled() will decide whether an item is enabled, which also means
1907 // whether the matching shortcut key will function.
1908 switch (mMenuState) {
1909 case EMPTY_MENU:
1910 if (mCurrentMenuState != mMenuState) {
1911 menu.setGroupVisible(R.id.MAIN_MENU, false);
1912 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1913 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1914 }
1915 break;
1916 default:
1917 if (mCurrentMenuState != mMenuState) {
1918 menu.setGroupVisible(R.id.MAIN_MENU, true);
1919 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1920 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1921 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001922 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001923 break;
1924 }
1925 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001926 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001927
1928 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001929 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1930 EditBookmarksRestriction.getInstance()
1931 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001932 }
1933
1934 private void setMenuItemVisibility(Menu menu, int id,
1935 boolean visibility) {
1936 MenuItem item = menu.findItem(id);
1937 if (item != null) {
1938 item.setVisible(visibility);
1939 }
1940 }
1941
1942 private int lookupBookmark(String title, String url) {
1943 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001944 int count = 0;
1945 Cursor cursor = null;
1946 try {
1947 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1948 BookmarksLoader.PROJECTION,
1949 "title = ? OR url = ?",
1950 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001951 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001952 },
1953 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001954
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001955 if (cursor != null)
1956 count = cursor.getCount();
1957
1958 } catch (IllegalStateException e) {
1959 Log.e(LOGTAG, "lookupBookmark ", e);
1960 } finally {
1961 if (null != cursor) {
1962 cursor.close();
1963 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001964 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001965 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001966 }
1967
1968 private void resetMenuItems(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001969 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1970
1971 WebView w = getCurrentTopWebView();
1972 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1973
1974 String title = w.getTitle();
1975 String url = w.getUrl();
Pankaj Garg634bf432015-07-13 09:54:21 -07001976 mCurrentPageBookmarked = (lookupBookmark(title, url) > 0);
1977 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001978 bookmark_icon.setChecked(true);
1979 } else {
1980 bookmark_icon.setChecked(false);
1981 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001982
1983 // update reader mode checkbox
1984 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1985 readerSwitcher.setVisible(false);
1986 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001987 }
1988
Michael Kolb4bf79712011-07-14 14:18:12 -07001989 @Override
1990 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001991 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001992 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001993 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001994 // Following flag is used to identify schemes for which the LIVE_MENU
1995 // items defined in res/menu/browser.xml should be enabled
1996 boolean isLiveScheme = false;
1997 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001998 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07001999
2000 boolean isDistillable = false;
2001 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002002 resetMenuItems(menu);
2003
Michael Kolb4bf79712011-07-14 14:18:12 -07002004 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002005 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002006 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002007 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002008 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002009 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002010 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07002011
2012 isDistillable = tab.isDistillable();
2013 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07002014 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002015
2016 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2017 forward.setEnabled(canGoForward);
2018
Michael Kolb4bf79712011-07-14 14:18:12 -07002019 // decide whether to show the share link option
2020 PackageManager pm = mActivity.getPackageManager();
2021 Intent send = new Intent(Intent.ACTION_SEND);
2022 send.setType("text/plain");
2023 ResolveInfo ri = pm.resolveActivity(send,
2024 PackageManager.MATCH_DEFAULT_ONLY);
2025 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2026
2027 boolean isNavDump = mSettings.enableNavDump();
2028 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2029 nav.setVisible(isNavDump);
2030 nav.setEnabled(isNavDump);
2031
2032 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002033 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2034 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002035 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002036 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2037 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002038 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002039 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2040 isLive && isLiveScheme && isPageFinished);
2041 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002042 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002043 // history and snapshots item are the members of COMBO menu group,
2044 // so if show history item, only make snapshots item invisible.
2045 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002046
Ze G Riande2a675c22015-06-03 11:15:24 -07002047
2048 // update reader mode checkbox
2049 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2050 // The reader mode checkbox is hidden only
2051 // when the current page is neither distillable nor distilled
2052 readerSwitcher.setVisible(isDistillable || isDistilled);
2053 readerSwitcher.setChecked(isDistilled);
2054
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002055 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002056 }
2057
John Reck9c35b9c2012-05-30 10:08:50 -07002058 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002059 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002060 if (null == getCurrentTopWebView()) {
2061 return false;
2062 }
2063 if (mMenuIsDown) {
2064 // The shortcut action consumes the MENU. Even if it is still down,
2065 // it won't trigger the next shortcut action. In the case of the
2066 // shortcut action triggering a new activity, like Bookmarks, we
2067 // won't get onKeyUp for MENU. So it is important to reset it here.
2068 mMenuIsDown = false;
2069 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002070 if (mUi.onOptionsItemSelected(item)) {
2071 // ui callback handled it
2072 return true;
2073 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002074 switch (item.getItemId()) {
2075 // -- Main menu
2076 case R.id.new_tab_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002077 getCurrentTab().capture();
Michael Kolb8233fac2010-10-26 16:08:53 -07002078 openTabToHomePage();
2079 break;
2080
2081 case R.id.incognito_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002082 getCurrentTab().capture();
Michael Kolb519d2282011-05-09 17:03:19 -07002083 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 break;
2085
2086 case R.id.goto_menu_id:
2087 editUrl();
2088 break;
2089
2090 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002091 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2092 break;
2093
Michael Kolb315d5022011-10-13 12:47:11 -07002094 case R.id.snapshots_menu_id:
2095 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002096 break;
2097
Pankaj Garg49b79252014-11-07 17:33:41 -08002098 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002099 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002100 break;
2101
2102 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002103 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002104 stopLoading();
2105 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002106 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002107 getCurrentTopWebView().reload();
2108 }
2109 break;
2110
Michael Kolb8233fac2010-10-26 16:08:53 -07002111 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002112 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002113 break;
2114
2115 case R.id.close_menu_id:
2116 // Close the subwindow if it exists.
2117 if (mTabControl.getCurrentSubWindow() != null) {
2118 dismissSubWindow(mTabControl.getCurrentTab());
2119 break;
2120 }
2121 closeCurrentTab();
2122 break;
2123
kaiyiza8b6dbb2013-07-29 18:11:22 +08002124 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002125 Object[] params = { new String("persist.debug.browsermonkeytest")};
2126 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002127 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002128 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002129 if (ret != null && ret.equals("enable"))
2130 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002131 if (BrowserConfig.getInstance(getContext())
2132 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2133 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002134 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002135 case R.id.homepage_menu_id:
2136 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002137 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002138 break;
2139
2140 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002141 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002142 break;
2143
2144 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002145 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002146 break;
2147
John Reck2bc80422011-06-30 15:11:49 -07002148 case R.id.save_snapshot_menu_id:
2149 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002150 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002151 createScreenshotAsync(
2152 source.getWebView(),
2153 getDesiredThumbnailWidth(mActivity),
2154 getDesiredThumbnailHeight(mActivity),
2155 new ValueCallback<Bitmap>() {
2156 @Override
2157 public void onReceiveValue(Bitmap bitmap) {
2158 new SaveSnapshotTask(source, bitmap).execute();
2159 }
2160 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002161 break;
2162
Michael Kolb8233fac2010-10-26 16:08:53 -07002163 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002164 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002165 break;
2166
John Recke1a03a32011-09-14 17:04:16 -07002167 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002168 // passing null to distinguish between
2169 // "go live" button and navigating a web page
2170 // on a snapshot tab
2171 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002172 case R.id.share_page_menu_id:
2173 Tab currentTab = mTabControl.getCurrentTab();
2174 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002175 return false;
2176 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002177 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002178 break;
2179
2180 case R.id.dump_nav_menu_id:
2181 getCurrentTopWebView().debugDump();
2182 break;
2183
Michael Kolb8233fac2010-10-26 16:08:53 -07002184 case R.id.zoom_in_menu_id:
2185 getCurrentTopWebView().zoomIn();
2186 break;
2187
2188 case R.id.zoom_out_menu_id:
2189 getCurrentTopWebView().zoomOut();
2190 break;
2191
2192 case R.id.view_downloads_menu_id:
2193 viewDownloads();
2194 break;
2195
John Reck42229bc2011-08-19 13:26:43 -07002196 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002197 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002198 break;
2199
Ze G Riande2a675c22015-06-03 11:15:24 -07002200 case R.id.reader_mode_menu_id:
2201 toggleReaderMode();
2202 break;
2203
Michael Kolb8233fac2010-10-26 16:08:53 -07002204 case R.id.window_one_menu_id:
2205 case R.id.window_two_menu_id:
2206 case R.id.window_three_menu_id:
2207 case R.id.window_four_menu_id:
2208 case R.id.window_five_menu_id:
2209 case R.id.window_six_menu_id:
2210 case R.id.window_seven_menu_id:
2211 case R.id.window_eight_menu_id:
2212 {
2213 int menuid = item.getItemId();
2214 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2215 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2216 Tab desiredTab = mTabControl.getTab(id);
2217 if (desiredTab != null &&
2218 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002219 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002220 }
2221 break;
2222 }
2223 }
2224 }
2225 break;
2226
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002227 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002228 Bundle bundle = new Bundle();
2229 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2230 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2231 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2232 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2233 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002234 break;
2235
Pankaj Garg49b79252014-11-07 17:33:41 -08002236 case R.id.add_to_homescreen:
2237 final WebView w = getCurrentTopWebView();
2238 final EditText input = new EditText(getContext());
2239 input.setText(w.getTitle());
2240 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002241 .setTitle(getContext().getResources().getString(
2242 R.string.add_to_homescreen))
2243 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002244 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002245 .setPositiveButton(getContext().getResources().getString(
2246 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002247 public void onClick(DialogInterface dialog, int whichButton) {
2248 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2249 getContext(),
2250 w.getUrl(),
2251 input.getText().toString(),
2252 w.getViewportBitmap(),
2253 w.getFavicon()));
2254
2255 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2256 .addCategory(Intent.CATEGORY_HOME));
2257 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002258 .setNegativeButton(getContext().getResources().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07002259 R.string.cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002260 public void onClick(DialogInterface dialog, int whichButton) {
2261 // Do nothing.
2262 }
2263 })
2264 .show();
2265 break;
2266
Michael Kolb8233fac2010-10-26 16:08:53 -07002267 default:
2268 return false;
2269 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002270 return true;
2271 }
2272
John Reck68234a92012-04-19 15:27:12 -07002273 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2274 implements OnCancelListener {
2275
2276 private Tab mTab;
2277 private Dialog mProgressDialog;
2278 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002279 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002280
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002281 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002282 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002283 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002284 }
2285
2286 @Override
2287 protected void onPreExecute() {
2288 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2289 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2290 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002291 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002292 }
2293
2294 @Override
2295 protected Long doInBackground(Void... params) {
2296 if (!mTab.saveViewState(mValues)) {
2297 return null;
2298 }
2299 if (isCancelled()) {
2300 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2301 File file = mActivity.getFileStreamPath(path);
2302 if (!file.delete()) {
2303 file.deleteOnExit();
2304 }
2305 return null;
2306 }
2307 final ContentResolver cr = mActivity.getContentResolver();
2308 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2309 if (result == null) {
2310 return null;
2311 }
2312 long id = ContentUris.parseId(result);
2313 return id;
2314 }
2315
2316 @Override
2317 protected void onPostExecute(Long id) {
2318 if (isCancelled()) {
2319 return;
2320 }
2321 mProgressDialog.dismiss();
2322 if (id == null) {
2323 Toast.makeText(mActivity, R.string.snapshot_failed,
2324 Toast.LENGTH_SHORT).show();
2325 return;
2326 }
2327 Bundle b = new Bundle();
2328 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2329 mUi.showComboView(ComboViews.Snapshots, b);
2330 }
2331
2332 @Override
2333 public void onCancel(DialogInterface dialog) {
2334 cancel(true);
2335 }
2336 }
2337
Michael Kolb80f75082012-04-10 10:50:06 -07002338 @Override
2339 public void toggleUserAgent() {
2340 WebView web = getCurrentWebView();
2341 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002342 }
2343
Ze G Riande2a675c22015-06-03 11:15:24 -07002344 // This function calls the method in the webview to enable/disable
2345 // the reader mode through the DOM distiller
2346 public void toggleReaderMode() {
2347 Tab t = mTabControl.getCurrentTab();
2348 if (t.isDistilled()) {
2349 closeTab(t);
2350 } else if (t.isDistillable()) {
2351 openTab(t.getDistilledUrl(), false, true, false, t);
2352 }
2353 }
2354
Michael Kolb80f75082012-04-10 10:50:06 -07002355 @Override
2356 public void findOnPage() {
2357 getCurrentTopWebView().showFindDialog(null, true);
2358 }
2359
2360 @Override
2361 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002362 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002363 }
2364
2365 @Override
2366 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002367 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002368 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2369 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002370 }
2371 else {
2372 WebView w = getCurrentTopWebView();
2373 if (w == null)
2374 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002375 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002376 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002377 }
Michael Kolb80f75082012-04-10 10:50:06 -07002378 }
2379
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002380 public boolean goLive(String url) {
2381 if (!getCurrentTab().isSnapshot())
2382 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002383 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002384
2385 if (url == null) { // "go live" button was clicked
2386 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002387 closeTab(t);
2388 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002389 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002390 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002391 return true;
John Recke1a03a32011-09-14 17:04:16 -07002392 }
2393
luxiaolb40014b2013-07-19 10:01:43 +08002394 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002395 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002396 new AlertDialog.Builder(activity)
2397 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002398 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002399 .setMessage(R.string.exit_browser_msg)
2400 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2401 public void onClick(DialogInterface dialog, int which) {
2402 activity.moveTaskToBack(true);
2403 dialog.dismiss();
2404 }
2405 })
2406 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2407 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002408 mCrashRecoveryHandler.clearState(true);
2409 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002410 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002411 dialog.dismiss();
2412 }
2413 })
2414 .show();
2415 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002416
Michael Kolb315d5022011-10-13 12:47:11 -07002417 @Override
2418 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002419 }
2420
John Reck9c35b9c2012-05-30 10:08:50 -07002421 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002423 // Let the History and Bookmark fragments handle menus they created.
2424 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2425 return false;
2426 }
2427
Michael Kolb8233fac2010-10-26 16:08:53 -07002428 int id = item.getItemId();
2429 boolean result = true;
2430 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002431 // -- Browser context menu
2432 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002434 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002435 case R.id.copy_link_context_menu_id:
2436 final WebView webView = getCurrentTopWebView();
2437 if (null == webView) {
2438 result = false;
2439 break;
2440 }
2441 final HashMap<String, WebView> hrefMap =
2442 new HashMap<String, WebView>();
2443 hrefMap.put("webview", webView);
2444 final Message msg = mHandler.obtainMessage(
2445 FOCUS_NODE_HREF, id, 0, hrefMap);
2446 webView.requestFocusNodeHref(msg);
2447 break;
2448
2449 default:
2450 // For other context menus
2451 result = onOptionsItemSelected(item);
2452 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002453 return result;
2454 }
2455
2456 /**
2457 * support programmatically opening the context menu
2458 */
2459 public void openContextMenu(View view) {
2460 mActivity.openContextMenu(view);
2461 }
2462
2463 /**
2464 * programmatically open the options menu
2465 */
2466 public void openOptionsMenu() {
2467 mActivity.openOptionsMenu();
2468 }
2469
John Reck9c35b9c2012-05-30 10:08:50 -07002470 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002471 public boolean onMenuOpened(int featureId, Menu menu) {
2472 if (mOptionsMenuOpen) {
2473 if (mConfigChanged) {
2474 // We do not need to make any changes to the state of the
2475 // title bar, since the only thing that happened was a
2476 // change in orientation
2477 mConfigChanged = false;
2478 } else {
2479 if (!mExtendedMenuOpen) {
2480 mExtendedMenuOpen = true;
2481 mUi.onExtendedMenuOpened();
2482 } else {
2483 // Switching the menu back to icon view, so show the
2484 // title bar once again.
2485 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002486 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002487 }
2488 }
2489 } else {
2490 // The options menu is closed, so open it, and show the title
2491 mOptionsMenuOpen = true;
2492 mConfigChanged = false;
2493 mExtendedMenuOpen = false;
2494 mUi.onOptionsMenuOpened();
2495 }
2496 return true;
2497 }
2498
John Reck9c35b9c2012-05-30 10:08:50 -07002499 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002500 public void onOptionsMenuClosed(Menu menu) {
2501 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002502 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002503 }
2504
John Reck9c35b9c2012-05-30 10:08:50 -07002505 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002506 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002507 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002508 }
2509
2510 // Helper method for getting the top window.
2511 @Override
2512 public WebView getCurrentTopWebView() {
2513 return mTabControl.getCurrentTopWebView();
2514 }
2515
2516 @Override
2517 public WebView getCurrentWebView() {
2518 return mTabControl.getCurrentWebView();
2519 }
2520
2521 /*
2522 * This method is called as a result of the user selecting the options
2523 * menu to see the download window. It shows the download window on top of
2524 * the current window.
2525 */
2526 void viewDownloads() {
2527 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2528 mActivity.startActivity(intent);
2529 }
2530
John Reck30b065e2011-07-19 10:58:05 -07002531 int getActionModeHeight() {
2532 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2533 new int[] { android.R.attr.actionBarSize });
2534 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2535 actionBarSizeTypedArray.recycle();
2536 return size;
2537 }
2538
Michael Kolb8233fac2010-10-26 16:08:53 -07002539 // action mode
2540
John Reck9c35b9c2012-05-30 10:08:50 -07002541 @Override
2542 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002543 mUi.onActionModeStarted(mode);
2544 mActionMode = mode;
2545 }
2546
2547 /*
2548 * True if a custom ActionMode (i.e. find or select) is in use.
2549 */
2550 @Override
2551 public boolean isInCustomActionMode() {
2552 return mActionMode != null;
2553 }
2554
2555 /*
2556 * End the current ActionMode.
2557 */
2558 @Override
2559 public void endActionMode() {
2560 if (mActionMode != null) {
2561 mActionMode.finish();
2562 }
2563 }
2564
2565 /*
2566 * Called by find and select when they are finished. Replace title bars
2567 * as necessary.
2568 */
John Reck9c35b9c2012-05-30 10:08:50 -07002569 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002570 public void onActionModeFinished(ActionMode mode) {
2571 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002572 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002573 mActionMode = null;
2574 }
2575
2576 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002577 final Tab tab = getCurrentTab();
2578 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002579 }
2580
2581 // bookmark handling
2582
2583 /**
2584 * add the current page as a bookmark to the given folder id
2585 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002586 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002587 * bookmarked, and if it is, edit that bookmark. If false, and
2588 * the site is already bookmarked, do not attempt to edit the
2589 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002590 */
2591 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002592 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002593 WebView w = getCurrentTopWebView();
2594 if (w == null) {
2595 return null;
2596 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002597 Intent i = new Intent(mActivity,
2598 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2600 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002601 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 if (touchIconUrl != null) {
2603 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2604 WebSettings settings = w.getSettings();
2605 if (settings != null) {
2606 i.putExtra(AddBookmarkPage.USER_AGENT,
2607 settings.getUserAgentString());
2608 }
2609 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002610 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002611 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002612 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002613 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2614 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002615 // Put the dialog at the upper right of the screen, covering the
2616 // star on the title bar.
2617 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002618 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 }
2620
2621 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002622 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002623 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002624 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002625 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002626 }
2627
Vivek Sekharb54614f2014-05-01 19:03:37 -07002628 @Override
2629 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2630 boolean capture) {
2631 mUploadHandler = new UploadHandler(this);
2632 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2633 }
2634
Michael Kolb8233fac2010-10-26 16:08:53 -07002635 // thumbnails
2636
2637 /**
2638 * Return the desired width for thumbnail screenshots, which are stored in
2639 * the database, and used on the bookmarks screen.
2640 * @param context Context for finding out the density of the screen.
2641 * @return desired width for thumbnail screenshot.
2642 */
2643 static int getDesiredThumbnailWidth(Context context) {
2644 return context.getResources().getDimensionPixelOffset(
2645 R.dimen.bookmarkThumbnailWidth);
2646 }
2647
2648 /**
2649 * Return the desired height for thumbnail screenshots, which are stored in
2650 * the database, and used on the bookmarks screen.
2651 * @param context Context for finding out the density of the screen.
2652 * @return desired height for thumbnail screenshot.
2653 */
2654 static int getDesiredThumbnailHeight(Context context) {
2655 return context.getResources().getDimensionPixelOffset(
2656 R.dimen.bookmarkThumbnailHeight);
2657 }
2658
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002659 static void createScreenshotAsync(WebView view, int width, int height,
2660 final ValueCallback<Bitmap> cb) {
2661 if (view == null || width == 0 || height == 0) {
2662 return;
2663 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002664 view.getContentBitmapAsync(
2665 (float) width / view.getWidth(),
2666 new Rect(),
2667 new ValueCallback<Bitmap>() {
2668 @Override
2669 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002670 if (bitmap != null)
2671 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2672 cb.onReceiveValue(bitmap);
2673 }});
2674 }
2675
Michael Kolb8233fac2010-10-26 16:08:53 -07002676 private class Copy implements OnMenuItemClickListener {
2677 private CharSequence mText;
2678
John Reck9c35b9c2012-05-30 10:08:50 -07002679 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002680 public boolean onMenuItemClick(MenuItem item) {
2681 copy(mText);
2682 return true;
2683 }
2684
2685 public Copy(CharSequence toCopy) {
2686 mText = toCopy;
2687 }
2688 }
2689
Leon Scroggins63c02662010-11-18 15:16:27 -05002690 private static class Download implements OnMenuItemClickListener {
2691 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002692 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002693 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002694 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002695 private static final String FALLBACK_EXTENSION = "dat";
2696 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002697
John Reck9c35b9c2012-05-30 10:08:50 -07002698 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002699 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002700 if (DataUri.isDataUri(mText)) {
2701 saveDataUri();
2702 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002703 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002704 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002705 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002706 return true;
2707 }
2708
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002709 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2710 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002711 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002712 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002713 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002714 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002715 }
George Mount387d45d2011-10-07 15:57:53 -07002716
2717 /**
2718 * Treats mText as a data URI and writes its contents to a file
2719 * based on the current time.
2720 */
2721 private void saveDataUri() {
2722 FileOutputStream outputStream = null;
2723 try {
2724 DataUri uri = new DataUri(mText);
2725 File target = getTarget(uri);
2726 outputStream = new FileOutputStream(target);
2727 outputStream.write(uri.getData());
2728 final DownloadManager manager =
2729 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2730 manager.addCompletedDownload(target.getName(),
2731 mActivity.getTitle().toString(), false,
2732 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002733 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002734 } catch (IOException e) {
2735 Log.e(LOGTAG, "Could not save data URL");
2736 } finally {
2737 if (outputStream != null) {
2738 try {
2739 outputStream.close();
2740 } catch (IOException e) {
2741 // ignore close errors
2742 }
2743 }
2744 }
2745 }
2746
2747 /**
2748 * Creates a File based on the current time stamp and uses
2749 * the mime type of the DataUri to get the extension.
2750 */
2751 private File getTarget(DataUri uri) throws IOException {
2752 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002753 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002754 String nameBase = format.format(new Date());
2755 String mimeType = uri.getMimeType();
2756 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2757 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2758 if (extension == null) {
2759 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2760 extension = FALLBACK_EXTENSION;
2761 }
2762 extension = "." + extension; // createTempFile needs the '.'
2763 File targetFile = File.createTempFile(nameBase, extension, dir);
2764 return targetFile;
2765 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002766 }
2767
Cary Clark8974d282010-11-22 10:46:05 -05002768 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002769 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002770
John Reck9c35b9c2012-05-30 10:08:50 -07002771 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002772 public boolean onMenuItemClick(MenuItem item) {
2773 if (mWebView != null) {
2774 return mWebView.selectText();
2775 }
2776 return false;
2777 }
2778
2779 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002780 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002781 }
2782
2783 }
2784
Michael Kolb8233fac2010-10-26 16:08:53 -07002785 /********************** TODO: UI stuff *****************************/
2786
2787 // these methods have been copied, they still need to be cleaned up
2788
2789 /****************** tabs ***************************************************/
2790
2791 // basic tab interactions:
2792
2793 // it is assumed that tabcontrol already knows about the tab
2794 protected void addTab(Tab tab) {
2795 mUi.addTab(tab);
2796 }
2797
2798 protected void removeTab(Tab tab) {
2799 mUi.removeTab(tab);
2800 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002801 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002802 }
2803
Michael Kolbf2055602011-04-09 17:20:03 -07002804 @Override
2805 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002806 // monkey protection against delayed start
2807 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002808
2809 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2810 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002811 mTabControl.setCurrentTab(tab);
2812 // the tab is guaranteed to have a webview after setCurrentTab
2813 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002814
2815
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002816 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002817 //Purge active tabs
2818 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002819 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002820 }
2821
John Reck8bcafc12011-08-29 16:43:02 -07002822 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002823 Tab current = mTabControl.getCurrentTab();
2824 if (current != null
2825 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002826 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002827 }
2828 }
2829
John Reck26b18322011-06-21 13:08:58 -07002830 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002831 //Cancel navscreen request
2832 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002833 // Dismiss the subwindow if applicable.
2834 dismissSubWindow(appTab);
2835 // Since we might kill the WebView, remove it from the
2836 // content view first.
2837 mUi.detachTab(appTab);
2838 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002839 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002840 // TODO: analyze why the remove and add are necessary
2841 mUi.attachTab(appTab);
2842 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002843 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002844 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002845 } else {
2846 // If the tab was the current tab, we have to attach
2847 // it to the view system again.
2848 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002849 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002850 }
2851 }
2852
2853 // Remove the sub window if it exists. Also called by TabControl when the
2854 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002855 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002856 public void dismissSubWindow(Tab tab) {
2857 removeSubWindow(tab);
2858 // dismiss the subwindow. This will destroy the WebView.
2859 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002860 WebView wv = getCurrentTopWebView();
2861 if (wv != null) {
2862 wv.requestFocus();
2863 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002864 }
2865
2866 @Override
2867 public void removeSubWindow(Tab t) {
2868 if (t.getSubWebView() != null) {
2869 mUi.removeSubWindow(t.getSubViewContainer());
2870 }
2871 }
2872
2873 @Override
2874 public void attachSubWindow(Tab tab) {
2875 if (tab.getSubWebView() != null) {
2876 mUi.attachSubWindow(tab.getSubViewContainer());
2877 getCurrentTopWebView().requestFocus();
2878 }
2879 }
2880
Mathew Inwood29721c22011-06-29 17:55:24 +01002881 private Tab showPreloadedTab(final UrlData urlData) {
2882 if (!urlData.isPreloaded()) {
2883 return null;
2884 }
2885 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2886 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2887 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002888 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002889 // Could not submit query. Fallback to regular tab creation
2890 tabControl.destroy();
2891 return null;
2892 }
2893 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002894 // check tab count and make room for new tab
2895 if (!mTabControl.canCreateNewTab()) {
2896 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2897 if (leastUsed != null) {
2898 closeTab(leastUsed);
2899 }
2900 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002901 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002902 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002903 mTabControl.addPreloadedTab(t);
2904 addTab(t);
2905 setActiveTab(t);
2906 return t;
2907 }
2908
Michael Kolb7bcafde2011-05-09 13:55:59 -07002909 // open a non inconito tab with the given url data
2910 // and set as active tab
2911 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002912 Tab tab = showPreloadedTab(urlData);
2913 if (tab == null) {
2914 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002915 if ((tab != null) && !urlData.isEmpty()) {
2916 loadUrlDataIn(tab, urlData);
2917 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002918 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002919 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002920 }
2921
Michael Kolb843510f2010-12-09 10:51:49 -08002922 @Override
2923 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002924 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002925 }
2926
Michael Kolb8233fac2010-10-26 16:08:53 -07002927 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002928 public Tab openIncognitoTab() {
2929 return openTab(INCOGNITO_URI, true, true, false);
2930 }
2931
2932 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002933 public Tab openTab(String url, boolean incognito, boolean setActive,
2934 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002935 return openTab(url, incognito, setActive, useCurrent, null);
2936 }
2937
2938 @Override
2939 public Tab openTab(String url, Tab parent, boolean setActive,
2940 boolean useCurrent) {
2941 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2942 setActive, useCurrent, parent);
2943 }
2944
2945 public Tab openTab(String url, boolean incognito, boolean setActive,
2946 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002947 Tab tab = createNewTab(incognito, setActive, useCurrent);
2948 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002949 if (parent instanceof SnapshotTab) {
2950 addTab(tab);
2951 if (setActive)
2952 setActiveTab(tab);
2953 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002954 parent.addChildTab(tab);
2955 }
Michael Kolb519d2282011-05-09 17:03:19 -07002956 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002957 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002958 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002959 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002960 return tab;
2961 }
2962
2963 // this method will attempt to create a new tab
2964 // incognito: private browsing tab
2965 // setActive: ste tab as current tab
2966 // useCurrent: if no new tab can be created, return current tab
2967 private Tab createNewTab(boolean incognito, boolean setActive,
2968 boolean useCurrent) {
2969 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002970 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2971 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002972 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002973 if (mTabControl.canCreateNewTab()) {
2974 tab = mTabControl.createNewTab(incognito, !setActive);
2975 addTab(tab);
2976 if (setActive) {
2977 setActiveTab(tab);
2978 } else {
2979 tab.pause();
2980 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002981 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002982 if (useCurrent) {
2983 tab = mTabControl.getCurrentTab();
2984 reuseTab(tab, null);
2985 } else {
2986 mUi.showMaxTabsWarning();
2987 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002988 }
2989 }
2990 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002991 }
2992
John Reck2bc80422011-06-30 15:11:49 -07002993 @Override
2994 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2995 SnapshotTab tab = null;
2996 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07002997 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07002998 addTab(tab);
2999 if (setActive) {
3000 setActiveTab(tab);
3001 }
3002 } else {
3003 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003004 }
3005 return tab;
3006 }
3007
Michael Kolb8233fac2010-10-26 16:08:53 -07003008 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003009 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003010 * @return boolean True if we successfully switched to a different tab. If
3011 * the indexth tab is null, or if that tab is the same as
3012 * the current one, return false.
3013 */
3014 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003015 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003016 Tab currentTab = mTabControl.getCurrentTab();
3017 if (tab == null || tab == currentTab) {
3018 return false;
3019 }
3020 setActiveTab(tab);
3021 return true;
3022 }
3023
3024 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003025 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003026 closeCurrentTab(false);
3027 }
3028
3029 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003030 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003031 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003032 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003033 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003034 return;
3035 }
Michael Kolbc831b632011-05-11 09:30:34 -07003036 final Tab current = mTabControl.getCurrentTab();
3037 final int pos = mTabControl.getCurrentPosition();
3038 Tab newTab = current.getParent();
3039 if (newTab == null) {
3040 newTab = mTabControl.getTab(pos + 1);
3041 if (newTab == null) {
3042 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003043 }
3044 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003045 if (andQuit) {
3046 mTabControl.setCurrentTab(newTab);
3047 closeTab(current);
3048 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003049 // Close window
3050 closeTab(current);
3051 }
3052 }
3053
3054 /**
3055 * Close the tab, remove its associated title bar, and adjust mTabControl's
3056 * current tab to a valid value.
3057 */
3058 @Override
3059 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003060 if (tab == mTabControl.getCurrentTab()) {
3061 closeCurrentTab();
3062 } else {
3063 removeTab(tab);
3064 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003065 }
3066
Afzal Najamd4e33312012-04-26 01:54:01 -04003067 /**
3068 * Close all tabs except the current one
3069 */
3070 @Override
3071 public void closeOtherTabs() {
3072 int inactiveTabs = mTabControl.getTabCount() - 1;
3073 for (int i = inactiveTabs; i >= 0; i--) {
3074 Tab tab = mTabControl.getTab(i);
3075 if (tab != mTabControl.getCurrentTab()) {
3076 removeTab(tab);
3077 }
3078 }
3079 }
3080
Michael Kolb8233fac2010-10-26 16:08:53 -07003081 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003082 protected void loadUrlFromContext(String url) {
3083 Tab tab = getCurrentTab();
3084 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003085 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003086 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003087 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003088 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003089 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003090 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003091 }
3092 }
3093 }
3094
3095 /**
3096 * Load the URL into the given WebView and update the title bar
3097 * to reflect the new load. Call this instead of WebView.loadUrl
3098 * directly.
3099 * @param view The WebView used to load url.
3100 * @param url The URL to load.
3101 */
John Reck71e51422011-07-01 16:49:28 -07003102 @Override
3103 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003104 loadUrl(tab, url, null);
3105 }
3106
3107 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3108 if (tab != null) {
3109 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003110 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003111 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003112 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003113 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003114 }
3115
3116 /**
3117 * Load UrlData into a Tab and update the title bar to reflect the new
3118 * load. Call this instead of UrlData.loadIn directly.
3119 * @param t The Tab used to load.
3120 * @param data The UrlData being loaded.
3121 */
3122 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003123 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003124 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003125 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003126 } else {
John Reck38b39652012-06-05 09:22:59 -07003127 if (t != null && data.mDisableUrlOverride) {
3128 t.disableUrlOverridingForLoad();
3129 }
John Reck26b18322011-06-21 13:08:58 -07003130 loadUrl(t, data.mUrl, data.mHeaders);
3131 }
3132 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003133 }
3134
John Reck30c714c2010-12-16 17:30:34 -08003135 @Override
3136 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003137 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003138 //In case of tab can go back (aka tab has navigation entry) do nothing
3139 //else just load homepage in current tab.
3140 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003141 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003142 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003143 }
3144
3145 void goBackOnePageOrQuit() {
3146 Tab current = mTabControl.getCurrentTab();
3147 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003148 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3149 showExitDialog(mActivity);
3150 } else {
3151 /*
3152 * Instead of finishing the activity, simply push this to the back
3153 * of the stack and let ActivityManager to choose the foreground
3154 * activity. As BrowserActivity is singleTask, it will be always the
3155 * root of the task. So we can use either true or false for
3156 * moveTaskToBack().
3157 */
3158 mActivity.moveTaskToBack(true);
3159 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003160 return;
3161 }
John Reckef654f12011-07-12 16:42:08 -07003162 if (current.canGoBack()) {
3163 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003164 } else {
3165 // Check to see if we are closing a window that was created by
3166 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003167 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003168 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003169 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003170 // Now we close the other tab
3171 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003172 } else if (BrowserConfig.getInstance(getContext())
3173 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3174 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003175 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003176 /*
3177 * Instead of finishing the activity, simply push this to the back
3178 * of the stack and let ActivityManager to choose the foreground
3179 * activity. As BrowserActivity is singleTask, it will be always the
3180 * root of the task. So we can use either true or false for
3181 * moveTaskToBack().
3182 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003183 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003184 }
3185 }
3186 }
3187
3188 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003189 * helper method for key handler
3190 * returns the current tab if it can't advance
3191 */
Michael Kolbc831b632011-05-11 09:30:34 -07003192 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003193 int pos = mTabControl.getCurrentPosition() + 1;
3194 if (pos >= mTabControl.getTabCount()) {
3195 pos = 0;
3196 }
3197 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003198 }
3199
3200 /**
3201 * helper method for key handler
3202 * returns the current tab if it can't advance
3203 */
Michael Kolbc831b632011-05-11 09:30:34 -07003204 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003205 int pos = mTabControl.getCurrentPosition() - 1;
3206 if ( pos < 0) {
3207 pos = mTabControl.getTabCount() - 1;
3208 }
3209 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003210 }
3211
John Reckbcef87f2012-02-03 14:58:34 -08003212 boolean isMenuOrCtrlKey(int keyCode) {
3213 return (KeyEvent.KEYCODE_MENU == keyCode)
3214 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3215 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3216 }
3217
Michael Kolb0035fad2011-03-14 13:25:28 -07003218 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003219 * handle key events in browser
3220 *
3221 * @param keyCode
3222 * @param event
3223 * @return true if handled, false to pass to super
3224 */
John Reck9c35b9c2012-05-30 10:08:50 -07003225 @Override
3226 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003227 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3228 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003229 if (!mUi.isComboViewShowing()) {
3230 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3231 true, false);
3232 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003233 return true;
3234 }
3235
Cary Clark160bbb92011-01-10 11:17:07 -05003236 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003237 // Even if MENU is already held down, we need to call to super to open
3238 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003239 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003240 mMenuIsDown = true;
3241 return false;
3242 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003243
Cary Clark8ff8c662010-12-29 15:03:05 -05003244 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003245 Tab tab = getCurrentTab();
3246 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003247
Cary Clark160bbb92011-01-10 11:17:07 -05003248 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3249 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003250
Michael Kolb8233fac2010-10-26 16:08:53 -07003251 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003252 case KeyEvent.KEYCODE_TAB:
3253 if (event.isCtrlPressed()) {
3254 if (event.isShiftPressed()) {
3255 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003256 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003257 } else {
3258 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003259 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003260 }
3261 return true;
3262 }
3263 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003264 case KeyEvent.KEYCODE_SPACE:
3265 // WebView/WebTextView handle the keys in the KeyDown. As
3266 // the Activity's shortcut keys are only handled when WebView
3267 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003268 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003269 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003270 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003271 pageDown();
3272 }
3273 return true;
3274 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003275 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003276 event.startTracking();
3277 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003278 case KeyEvent.KEYCODE_FORWARD:
3279 if (!noModifiers) break;
3280 tab.goForward();
3281 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003282 case KeyEvent.KEYCODE_DPAD_LEFT:
3283 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003284 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003285 return true;
3286 }
3287 break;
3288 case KeyEvent.KEYCODE_DPAD_RIGHT:
3289 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003290 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003291 return true;
3292 }
3293 break;
3294 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003295 if (ctrl) {
3296 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003297 return true;
3298 }
3299 break;
Michael Kolba4183062011-01-16 10:43:21 -08003300// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003301 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003302 if (ctrl ) {
3303 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003304 return true;
3305 }
3306 break;
Michael Kolba4183062011-01-16 10:43:21 -08003307// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003308// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003309// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003310// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003311// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003312// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003313// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003314// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003315// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003316// case KeyEvent.KEYCODE_M: // unused
3317// case KeyEvent.KEYCODE_N: // in Chrome: new window
3318// case KeyEvent.KEYCODE_O: // in Chrome: open file
3319// case KeyEvent.KEYCODE_P: // in Chrome: print page
3320// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003321// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003322// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3323 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003324 // we can't use the ctrl/shift flags, they check for
3325 // exclusive use of a modifier
3326 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003327 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003328 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003329 } else {
3330 openTabToHomePage();
3331 }
3332 return true;
3333 }
3334 break;
3335// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3336// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003337// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003338// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3339// case KeyEvent.KEYCODE_Y: // unused
3340// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003341 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003342 // it is a regular key and webview is not null
3343 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003344 }
3345
John Reck9c35b9c2012-05-30 10:08:50 -07003346 @Override
3347 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003348 switch(keyCode) {
3349 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003350 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003351 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003352 return true;
3353 }
3354 break;
3355 }
3356 return false;
3357 }
3358
John Reck9c35b9c2012-05-30 10:08:50 -07003359 @Override
3360 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003361 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003362 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003363 if (KeyEvent.KEYCODE_MENU == keyCode
3364 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003365 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003366 }
3367 }
Cary Clark160bbb92011-01-10 11:17:07 -05003368 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003369 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003370 case KeyEvent.KEYCODE_BACK:
3371 if (event.isTracking() && !event.isCanceled()) {
3372 onBackKey();
3373 return true;
3374 }
3375 break;
3376 }
3377 return false;
3378 }
3379
3380 public boolean isMenuDown() {
3381 return mMenuIsDown;
3382 }
3383
John Reck9c35b9c2012-05-30 10:08:50 -07003384 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003385 public void setupAutoFill(Message message) {
3386 // Open the settings activity at the AutoFill profile fragment so that
3387 // the user can create a new profile. When they return, we will dispatch
3388 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003389 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003390 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3391 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003392 }
John Reckb3417f02011-01-14 11:01:05 -08003393
John Reck9c35b9c2012-05-30 10:08:50 -07003394 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003395 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003396 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003397 return true;
3398 }
3399
John Reck1cf4b792011-07-26 10:22:22 -07003400 @Override
3401 public boolean shouldCaptureThumbnails() {
3402 return mUi.shouldCaptureThumbnails();
3403 }
3404
Michael Kolbc3af0672011-08-09 10:24:41 -07003405 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003406 public boolean supportsVoice() {
3407 PackageManager pm = mActivity.getPackageManager();
3408 List activities = pm.queryIntentActivities(new Intent(
3409 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3410 return activities.size() != 0;
3411 }
3412
3413 @Override
3414 public void startVoiceRecognizer() {
3415 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003416 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003417 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3418 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3419 mActivity.startActivityForResult(voice, VOICE_RESULT);
3420 }
3421
Pankaj Garg7b279f62014-08-12 14:47:18 -07003422 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003423 if (mLevel == level)
3424 return;
3425 mLevel = level;
3426 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003427 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3428 lp.dimAmount = level;
3429 mActivity.getWindow().setAttributes(lp);
3430 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3431 } else {
3432 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3433 }
3434 }
3435
Michael Kolb0b129122012-06-04 16:31:58 -07003436 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003437 public void setBlockEvents(boolean block) {
3438 mBlockEvents = block;
3439 }
3440
John Reck9c35b9c2012-05-30 10:08:50 -07003441 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003442 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003443 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003444 }
3445
John Reck9c35b9c2012-05-30 10:08:50 -07003446 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003447 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003448 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003449 }
3450
John Reck9c35b9c2012-05-30 10:08:50 -07003451 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003452 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003453 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003454 }
3455
John Reck9c35b9c2012-05-30 10:08:50 -07003456 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003457 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003458 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003459 }
3460
John Reck9c35b9c2012-05-30 10:08:50 -07003461 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003462 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003463 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003464 }
3465
Pankaj Garg49b79252014-11-07 17:33:41 -08003466 @Override
3467 public boolean shouldShowAppMenu() {
3468 return true;
3469 }
3470
3471 @Override
3472 public int getMenuThemeResourceId() {
3473 return R.style.OverflowMenuTheme;
3474 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003475}