blob: 09dbc3c4086eff0fcd005bddc1bd372d443f422f [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;
50import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080051import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080052import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.os.AsyncTask;
Tarun Nainanif5563b62015-01-21 18:52:59 -080054import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070056import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.os.Handler;
58import android.os.Message;
59import android.os.PowerManager;
60import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070061import android.provider.ContactsContract;
62import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080063import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070064import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070065import android.text.TextUtils;
66import android.util.Log;
67import android.view.ActionMode;
68import android.view.ContextMenu;
69import android.view.ContextMenu.ContextMenuInfo;
70import android.view.Gravity;
71import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070072import android.view.Menu;
73import android.view.MenuInflater;
74import android.view.MenuItem;
75import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070076import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070077import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070078import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070079import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070080import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080081import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080082import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050083import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070084
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080085import org.codeaurora.swe.CookieManager;
86import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080087import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080088import org.codeaurora.swe.HttpAuthHandler;
89import org.codeaurora.swe.SslErrorHandler;
90import org.codeaurora.swe.WebSettings;
91import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070092import org.codeaurora.swe.WebBackForwardList;
93import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080094
Bijan Amirzada41242f22014-03-21 12:12:18 -070095import com.android.browser.IntentHandler.UrlData;
96import com.android.browser.UI.ComboViews;
Dave Tharp13f3f7c2015-07-16 14:58:51 -070097import com.android.browser.mdm.DownloadDirRestriction;
Dave Tharp9a082b12015-06-19 08:46:57 -070098import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -070099import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -0700100import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700101import com.android.browser.mynavigation.AddMyNavigationPage;
102import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700103import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700104import com.android.browser.platformsupport.BrowserContract;
Pankaj Garg18902562014-12-05 16:18:51 -0800105import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700106import com.android.browser.provider.BrowserProvider2.Thumbnails;
107import com.android.browser.provider.SnapshotProvider.Snapshots;
108import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800109import com.android.browser.appmenu.AppMenuHandler;
110import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700111
Michael Kolb8233fac2010-10-26 16:08:53 -0700112import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700113import java.io.File;
114import java.io.FileOutputStream;
115import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700116import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700117import java.text.DateFormat;
118import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700119import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700120import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700121import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800122import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200123import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700124import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700125
126/**
127 * Controller for browser
128 */
129public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800130 implements WebViewController, UiController, ActivityController,
131 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700132
133 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800134 private static final String SEND_APP_ID_EXTRA =
135 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -0700136 public static final String INCOGNITO_URI = "browser://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800137 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
138 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800139
Vivek Sekharb54614f2014-05-01 19:03:37 -0700140 // Remind switch to data connection if wifi is unavailable
141 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800142
Michael Kolb8233fac2010-10-26 16:08:53 -0700143 // public message ids
144 public final static int LOAD_URL = 1001;
145 public final static int STOP_LOAD = 1002;
146
147 // Message Ids
148 private static final int FOCUS_NODE_HREF = 102;
149 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800150 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700151
Michael Kolb8233fac2010-10-26 16:08:53 -0700152 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800153 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700154
155 private static final int EMPTY_MENU = -1;
156
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700158 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 final static int PREFERENCES_PAGE = 3;
160 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000161 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700162 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800163 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000164
Michael Kolb8233fac2010-10-26 16:08:53 -0700165 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
166
167 // As the ids are dynamically created, we can't guarantee that they will
168 // be in sequence, so this static array maps ids to a window number.
169 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
170 { R.id.window_one_menu_id, R.id.window_two_menu_id,
171 R.id.window_three_menu_id, R.id.window_four_menu_id,
172 R.id.window_five_menu_id, R.id.window_six_menu_id,
173 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
174
175 // "source" parameter for Google search through search key
176 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
177 // "source" parameter for Google search through simplily type
178 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
179
George Mount387d45d2011-10-07 15:57:53 -0700180 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700181 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
182
John Reckd7dd9b22011-08-30 09:18:29 -0700183 // A bitmap that is re-used in createScreenshot as scratch space
184 private static Bitmap sThumbnailBitmap;
185
Michael Kolb8233fac2010-10-26 16:08:53 -0700186 private Activity mActivity;
187 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700188 private HomepageHandler mHomepageHandler;
189 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700190 private BrowserSettings mSettings;
191 private WebViewFactory mFactory;
192
193 private WakeLock mWakeLock;
194
195 private UrlHandler mUrlHandler;
196 private UploadHandler mUploadHandler;
197 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700198 private PageDialogsHandler mPageDialogsHandler;
199 private NetworkStateHandler mNetworkHandler;
200
Ben Murdoch8029a772010-11-16 11:58:21 +0000201 private Message mAutoFillSetupMessage;
202
kaiyiza016da12013-08-26 17:50:22 +0800203 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700204
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 // FIXME, temp address onPrepareMenu performance problem.
206 // When we move everything out of view, we should rewrite this.
207 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700208 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 private int mOldMenuState = EMPTY_MENU;
210 private Menu mCachedMenu;
211
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 private boolean mMenuIsDown;
213
Pankaj Garg49b79252014-11-07 17:33:41 -0800214 private boolean mWasInPageLoad = false;
215 private AppMenuHandler mAppMenuHandler;
216
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 // For select and find, we keep track of the ActionMode so that
218 // finish() can be called as desired.
219 private ActionMode mActionMode;
220
221 /**
222 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
223 * of whether the configuration has changed. The first onMenuOpened call
224 * after a configuration change is simply a reopening of the same menu
225 * (i.e. mIconView did not change).
226 */
227 private boolean mConfigChanged;
228
229 /**
230 * Keeps track of whether the options menu is open. This is important in
231 * determining whether to show or hide the title bar overlay
232 */
233 private boolean mOptionsMenuOpen;
234
235 /**
236 * Whether or not the options menu is in its bigger, popup menu form. When
237 * true, we want the title bar overlay to be gone. When false, we do not.
238 * Only meaningful if mOptionsMenuOpen is true.
239 */
240 private boolean mExtendedMenuOpen;
241
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 private boolean mActivityPaused = true;
243 private boolean mLoadStopped;
244
245 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500246 // Checks to see when the bookmarks database has changed, and updates the
247 // Tabs' notion of whether they represent bookmarked sites.
248 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700249 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700250
Michael Kolbc3af0672011-08-09 10:24:41 -0700251 private boolean mBlockEvents;
252
Michael Kolb0b129122012-06-04 16:31:58 -0700253 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800254 private boolean mUpdateMyNavThumbnail;
255 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800256 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800257 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700258 private PowerConnectionReceiver mLowPowerReceiver;
259 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700260
Pankaj Garg634bf432015-07-13 09:54:21 -0700261 private boolean mCurrentPageBookmarked;
262
George Mount3636d0a2011-11-21 09:08:21 -0800263 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 mActivity = browser;
265 mSettings = BrowserSettings.getInstance();
266 mTabControl = new TabControl(this);
267 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700268 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800269 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700270 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700271
272 mUrlHandler = new UrlHandler(this);
273 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
275
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500277 mBookmarksObserver = new ContentObserver(mHandler) {
278 @Override
279 public void onChange(boolean selfChange) {
280 int size = mTabControl.getTabCount();
281 for (int i = 0; i < size; i++) {
282 mTabControl.getTab(i).updateBookmarkedStatus();
283 }
284 }
285
286 };
287 browser.getContentResolver().registerContentObserver(
288 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700289
290 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700291 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800292 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700293 }
294
John Reck9c35b9c2012-05-30 10:08:50 -0700295 @Override
296 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700297 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800298 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800299 // mCrashRecoverHandler has any previously saved state.
300 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700301 }
302
George Mount3636d0a2011-11-21 09:08:21 -0800303 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800304 // we dont want to ever recover incognito tabs
305 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700306
Patrick Scott7d50a932011-02-04 09:27:26 -0500307 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700308 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500309 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000310
Michael Kolbc831b632011-05-11 09:30:34 -0700311 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500312 // Not able to restore so we go ahead and clear session cookies. We
313 // must do this before trying to login the user as we don't want to
314 // clear any session cookies set during login.
315 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700316 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800317 if (intent == null) {
318 // This won't happen under common scenarios. The icicle is
319 // not null, but there aren't any tabs to restore.
320 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700321 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800322 final Bundle extra = intent.getExtras();
323 // Create an initial tab.
324 // If the intent is ACTION_VIEW and data is not null, the Browser is
325 // invoked to view the content by another application. In this case,
326 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800327 UrlData urlData = null;
328 if (intent.getData() != null
329 && Intent.ACTION_VIEW.equals(intent.getAction())
330 && intent.getData().toString().startsWith("content://")) {
331 urlData = new UrlData(intent.getData().toString());
332 } else {
333 urlData = IntentHandler.getUrlDataFromIntent(intent);
334 }
George Mount3636d0a2011-11-21 09:08:21 -0800335 Tab t = null;
336 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700337 String landingPage = mActivity.getResources().getString(
338 R.string.def_landing_page);
339 if (!landingPage.isEmpty()) {
340 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800341 } else {
342 t = openTabToHomePage();
343 }
George Mount3636d0a2011-11-21 09:08:21 -0800344 } else {
345 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700346 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800347 }
348 if (t != null) {
349 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
350 }
351 WebView webView = t.getWebView();
352 if (extra != null) {
353 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
354 if (scale > 0 && scale <= 1000) {
355 webView.setInitialScale(scale);
356 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 }
358 }
John Reckd8c74522011-06-14 08:45:00 -0700359 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700360 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700361 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500362 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700363 List<Tab> tabs = mTabControl.getTabs();
364 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700365
John Reck1cf4b792011-07-26 10:22:22 -0700366 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700367 //handle restored pages that may require a JS interface
368 if (t.getWebView() != null) {
369 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
370 if (backForwardList != null) {
371 for (int i = 0; i < backForwardList.getSize(); i++) {
372 WebHistoryItem item = backForwardList.getItemAtIndex(i);
373 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
374 }
375 }
376 }
John Reck1cf4b792011-07-26 10:22:22 -0700377 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700378 if (t != mTabControl.getCurrentTab()) {
379 t.pause();
380 }
John Reck1cf4b792011-07-26 10:22:22 -0700381 }
382 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700383 if (tabs.size() == 0) {
384 openTabToHomePage();
385 }
John Reck1cf4b792011-07-26 10:22:22 -0700386 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700387 // TabControl.restoreState() will create a new tab even if
388 // restoring the state fails.
389 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800390 // Intent is non-null when framework thinks the browser should be
391 // launching with a new intent (icicle is null).
392 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700393 mIntentHandler.onNewIntent(intent);
394 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700395 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700397 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800398 if (jsFlags.trim().length() != 0) {
399 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700400 }
George Mount3636d0a2011-11-21 09:08:21 -0800401 if (intent != null
402 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700403 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800404 }
Site Maoabb7bd32015-03-20 15:34:02 -0700405 mLowPowerReceiver = new PowerConnectionReceiver();
406 mPowerChangeReceiver = new PowerConnectionReceiver();
407
408 //always track the android framework's power save mode
409 IntentFilter filter = new IntentFilter();
410 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
411 // Power save mode only exists in Lollipop and above
412 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
413 }
414 filter.addAction(Intent.ACTION_BATTERY_OKAY);
415 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700416 }
417
John Reck1cf4b792011-07-26 10:22:22 -0700418 private static class PruneThumbnails implements Runnable {
419 private Context mContext;
420 private List<Long> mIds;
421
422 PruneThumbnails(Context context, List<Long> preserveIds) {
423 mContext = context.getApplicationContext();
424 mIds = preserveIds;
425 }
426
427 @Override
428 public void run() {
429 ContentResolver cr = mContext.getContentResolver();
430 if (mIds == null || mIds.size() == 0) {
431 cr.delete(Thumbnails.CONTENT_URI, null, null);
432 } else {
433 int length = mIds.size();
434 StringBuilder where = new StringBuilder();
435 where.append(Thumbnails._ID);
436 where.append(" not in (");
437 for (int i = 0; i < length; i++) {
438 where.append(mIds.get(i));
439 if (i < (length - 1)) {
440 where.append(",");
441 }
442 }
443 where.append(")");
444 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
445 }
446 }
447
448 }
449
Michael Kolb1514bb72010-11-22 09:11:48 -0800450 @Override
451 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700452 return mFactory;
453 }
454
455 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800456 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700457 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700458 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800459 }
460
461 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800462 public void createSubWindow(Tab tab) {
463 endActionMode();
464 WebView mainView = tab.getWebView();
465 WebView subView = mFactory.createWebView((mainView == null)
466 ? false
467 : mainView.isPrivateBrowsingEnabled());
468 mUi.createSubWindow(tab, subView);
469 }
470
471 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700472 public Context getContext() {
473 return mActivity;
474 }
475
476 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700477 public Activity getActivity() {
478 return mActivity;
479 }
480
481 void setUi(UI ui) {
482 mUi = ui;
483 }
484
Michael Kolbaf63dba2012-05-16 12:58:05 -0700485 @Override
486 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 return mSettings;
488 }
489
490 IntentHandler getIntentHandler() {
491 return mIntentHandler;
492 }
493
494 @Override
495 public UI getUi() {
496 return mUi;
497 }
498
499 int getMaxTabs() {
500 return mActivity.getResources().getInteger(R.integer.max_tabs);
501 }
502
503 @Override
504 public TabControl getTabControl() {
505 return mTabControl;
506 }
507
Michael Kolb1bf23132010-11-19 12:55:12 -0800508 @Override
509 public List<Tab> getTabs() {
510 return mTabControl.getTabs();
511 }
512
Michael Kolb8233fac2010-10-26 16:08:53 -0700513 private void startHandler() {
514 mHandler = new Handler() {
515
516 @Override
517 public void handleMessage(Message msg) {
518 switch (msg.what) {
519 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700520 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700521 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800522 case UNKNOWN_TYPE_MSG:
523 HashMap unknownTypeMap = (HashMap) msg.obj;
524 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
525 /*
526 * When the context menu is shown to the user
527 * we need to assure that its happening on the current webview
528 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
529 */
530 if (getCurrentWebView() != viewForUnknownType)
531 break;
532
533 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800534 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800535 WebView.HitTestResult result = new WebView.HitTestResult();
536
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800537 // Prevent unnecessary calls to context menu
538 // if url and image src are null
539 if (unknown_type_src == null && unknown_type_url == null)
540 break;
541
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800542 //setting the HitTestResult with new RESULT TYPE
543 if (!TextUtils.isEmpty(unknown_type_src)) {
544 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
545 result.setExtra(unknown_type_src);
546 } else {
547 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
548 result.setExtra("about:blank");
549 }
550
551 mResult = result;
552 openContextMenu(viewForUnknownType);
553 mResult = null;
554
555 break;
556
Michael Kolb8233fac2010-10-26 16:08:53 -0700557 case FOCUS_NODE_HREF:
558 {
559 String url = (String) msg.getData().get("url");
560 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500561 String src = (String) msg.getData().get("src");
562 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700563 if (TextUtils.isEmpty(url)) {
564 break;
565 }
566 HashMap focusNodeMap = (HashMap) msg.obj;
567 WebView view = (WebView) focusNodeMap.get("webview");
568 // Only apply the action if the top window did not change.
569 if (getCurrentTopWebView() != view) {
570 break;
571 }
572 switch (msg.arg1) {
573 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700574 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700575 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500576 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700577 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500578 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500579 case R.id.open_newtab_context_menu_id:
580 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700581 openTab(url, parent,
582 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500583 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700584 case R.id.copy_link_context_menu_id:
585 copy(url);
586 break;
587 case R.id.save_link_context_menu_id:
588 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500589 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800590 mActivity, url, view.getSettings().getUserAgentString(),
591 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700592 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700593 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700594 if(title == null || title == "")
595 title = url;
596
597 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
598 //SWE TODO: No thumbnail support for the url obtained via
599 //browser context menu as its not loaded in webview.
600 if (bookmarkIntent != null) {
601 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
602 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
603 mActivity.startActivity(bookmarkIntent);
604 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700605 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700606 }
607 break;
608 }
609
610 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700611 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 break;
613
614 case STOP_LOAD:
615 stopLoading();
616 break;
617
618 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700619 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 mWakeLock.release();
621 // if we reach here, Browser should be still in the
622 // background loading after WAKELOCK_TIMEOUT (5-min).
623 // To avoid burning the battery, stop loading.
624 mTabControl.stopAllLoading();
625 }
626 break;
627
kaiyiz591110b2013-08-06 17:11:06 +0800628 case OPEN_MENU:
629 if (!mOptionsMenuOpen && mActivity != null ) {
630 mActivity.openOptionsMenu();
631 }
632 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 }
634 }
635 };
636
637 }
638
John Reckef654f12011-07-12 16:42:08 -0700639 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200640 public Tab getCurrentTab() {
641 return mTabControl.getCurrentTab();
642 }
643
Michael Kolbba99c5d2010-11-29 14:57:41 -0800644 @Override
645 public void shareCurrentPage() {
646 shareCurrentPage(mTabControl.getCurrentTab());
647 }
648
649 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700650 if (tab == null || tab.getWebView() == null)
651 return;
652
653 final Tab mytab = tab;
654 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
655 @Override
656 public void onReceiveValue(Bitmap bitmap) {
657 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
658 mytab.getFavicon(), bitmap);
659 }
660 };
661
662 createScreenshotAsync(
663 tab.getWebView(),
664 getDesiredThumbnailWidth(mActivity),
665 getDesiredThumbnailHeight(mActivity),
666 new ValueCallback<Bitmap>() {
667 @Override
668 public void onReceiveValue(Bitmap bitmap) {
669 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
670 mytab.getFavicon(), bitmap);
671 }
672 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800673 }
674
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 /**
676 * Share a page, providing the title, url, favicon, and a screenshot. Uses
677 * an {@link Intent} to launch the Activity chooser.
678 * @param c Context used to launch a new Activity.
679 * @param title Title of the page. Stored in the Intent with
680 * {@link Intent#EXTRA_SUBJECT}
681 * @param url URL of the page. Stored in the Intent with
682 * {@link Intent#EXTRA_TEXT}
683 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
684 * with {@link Browser#EXTRA_SHARE_FAVICON}
685 * @param screenshot Bitmap of a screenshot of the page. Stored in the
686 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
687 */
688 static final void sharePage(Context c, String title, String url,
689 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700690
691 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
692 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
693 R.layout.app_row, sDialog.getApps());
694 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700695 }
696
697 private void copy(CharSequence text) {
698 ClipboardManager cm = (ClipboardManager) mActivity
699 .getSystemService(Context.CLIPBOARD_SERVICE);
700 cm.setText(text);
701 }
702
703 // lifecycle
704
John Reck9c35b9c2012-05-30 10:08:50 -0700705 @Override
706 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800708 // update the menu in case of a locale change
709 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800710 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800711 if (mOptionsMenuOpen) {
712 mActivity.closeOptionsMenu();
713 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
714 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 if (mPageDialogsHandler != null) {
716 mPageDialogsHandler.onConfigurationChanged(config);
717 }
718 mUi.onConfigurationChanged(config);
719 }
720
721 @Override
722 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700723 if (!mUi.isWebShowing()) {
724 mUi.showWeb(false);
725 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700726 mIntentHandler.onNewIntent(intent);
727 }
728
John Reck9c35b9c2012-05-30 10:08:50 -0700729 @Override
730 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800731 if (mUi.isCustomViewShowing()) {
732 hideCustomView();
733 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700734 if (mActivityPaused) {
735 Log.e(LOGTAG, "BrowserActivity is already paused.");
736 return;
737 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700738 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800739 Tab tab = mTabControl.getCurrentTab();
740 if (tab != null) {
741 tab.pause();
742 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700743 if (mWakeLock == null) {
744 PowerManager pm = (PowerManager) mActivity
745 .getSystemService(Context.POWER_SERVICE);
746 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
747 }
Michael Kolb70976932010-11-30 11:34:01 -0800748 mWakeLock.acquire();
749 mHandler.sendMessageDelayed(mHandler
750 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
751 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700752 }
753 mUi.onPause();
754 mNetworkHandler.onPause();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100755 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700756 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700757 }
758
John Reck9c35b9c2012-05-30 10:08:50 -0700759 @Override
760 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700761 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800762 Bundle saveState = createSaveState();
763
764 // crash recovery manages all save & restore state
765 mCrashRecoveryHandler.writeState(saveState);
766 mSettings.setLastRunPaused(true);
767 }
768
769 /**
770 * Save the current state to outState. Does not write the state to
771 * disk.
772 * @return Bundle containing the current state of all tabs.
773 */
774 /* package */ Bundle createSaveState() {
775 Bundle saveState = new Bundle();
776 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800777 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700778 }
779
John Reck9c35b9c2012-05-30 10:08:50 -0700780 @Override
781 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700782 if (!mActivityPaused) {
783 Log.e(LOGTAG, "BrowserActivity is already resumed.");
784 return;
785 }
Vivek Sekharb53a5952015-07-17 14:14:38 -0700786 UpdateNotificationService.updateCheck(mActivity);
George Mount3636d0a2011-11-21 09:08:21 -0800787 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700788 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800789 Tab current = mTabControl.getCurrentTab();
790 if (current != null) {
791 current.resume();
792 resumeWebViewTimers(current);
793 }
John Reckf57c0292011-07-21 18:15:39 -0700794 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200795
Michael Kolb8233fac2010-10-26 16:08:53 -0700796 mUi.onResume();
797 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100798 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700799 if (mVoiceResult != null) {
800 mUi.onVoiceResult(mVoiceResult);
801 mVoiceResult = null;
802 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700803 if (current != null && current.getWebView().isShowingCrashView())
804 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700805 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 }
807
John Reckf57c0292011-07-21 18:15:39 -0700808 private void releaseWakeLock() {
809 if (mWakeLock != null && mWakeLock.isHeld()) {
810 mHandler.removeMessages(RELEASE_WAKELOCK);
811 mWakeLock.release();
812 }
813 }
814
Michael Kolb70976932010-11-30 11:34:01 -0800815 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800816 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800817 * @param tab guaranteed non-null
818 */
819 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 boolean inLoad = tab.inPageLoad();
821 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
822 CookieSyncManager.getInstance().startSync();
823 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100824 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 }
826 }
827
Michael Kolb70976932010-11-30 11:34:01 -0800828 /**
829 * Pause all WebView timers using the WebView of the given tab
830 * @param tab
831 * @return true if the timers are paused or tab is null
832 */
833 private boolean pauseWebViewTimers(Tab tab) {
834 if (tab == null) {
835 return true;
836 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100838 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 }
Michael Kolb70976932010-11-30 11:34:01 -0800841 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 }
843
John Reck9c35b9c2012-05-30 10:08:50 -0700844 @Override
845 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800846 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
848 mUploadHandler = null;
849 }
850 if (mTabControl == null) return;
851 mUi.onDestroy();
852 // Remove the current tab and sub window
853 Tab t = mTabControl.getCurrentTab();
854 if (t != null) {
855 dismissSubWindow(t);
856 removeTab(t);
857 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500858 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 // Destroy all the tabs
860 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700861 // Unregister receiver
862 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 }
864
865 protected boolean isActivityPaused() {
866 return mActivityPaused;
867 }
868
John Reck9c35b9c2012-05-30 10:08:50 -0700869 @Override
870 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 mTabControl.freeMemory();
872 }
873
874 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700875 public void stopLoading() {
876 mLoadStopped = true;
877 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700879 if (w != null) {
880 w.stopLoading();
881 mUi.onPageStopped(tab);
882 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 }
884
885 boolean didUserStopLoading() {
886 return mLoadStopped;
887 }
888
kaiyiza016da12013-08-26 17:50:22 +0800889 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700890 final String reminderType = getContext().getResources().getString(
891 R.string.def_wifi_browser_interaction_remind_type);
892 final String selectionConnnection = getContext().getResources().getString(
893 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700894 final String wifiSelection = getContext().getResources().getString(
895 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700896
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700897 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
898 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700899 return;
900
kaiyiza016da12013-08-26 17:50:22 +0800901 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700902 Context.CONNECTIVITY_SERVICE);
903 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700904 WifiManager wifiMgr = (WifiManager) this.getContext()
905 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700906 if (networkInfo == null
907 || (networkInfo != null && (networkInfo.getType() !=
908 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700909 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
910 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700911 List<ScanResult> list = wifiMgr.getScanResults();
912 // Have no AP's for Wifi's fall back to data
913 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700914 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700915 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
916 this.getContext().startActivity(intent);
917 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700918 // Request to select Wifi AP
919 try {
920 Intent intent = new Intent(wifiSelection);
921 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
922 this.getContext().startActivity(intent);
923 } catch (Exception e) {
924 String err_msg = this.getContext().getString(
925 R.string.acivity_not_found, wifiSelection);
926 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
927 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700928 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700929 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800930 }
931 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700932
Michael Kolb8233fac2010-10-26 16:08:53 -0700933 // WebViewController
934
935 @Override
John Reck324d4402011-01-11 16:56:42 -0800936 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700937
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 // reset sync timer to avoid sync starts during loading a page
939 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700940 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700941 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800942 boolean networkNotifier = BrowserConfig.getInstance(getContext())
943 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700944 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700945 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800946 } else {
947 if (!mNetworkHandler.isNetworkUp()) {
948 view.setNetworkAvailable(false);
949 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 }
951
952 // when BrowserActivity just starts, onPageStarted may be called before
953 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
954 // to start the timer. As we won't switch tabs while an activity is in
955 // pause state, we can ensure calling resume and pause in pair.
956 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800957 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 }
959 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700960 endActionMode();
961
John Reck30c714c2010-12-16 17:30:34 -0800962 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700963
John Reck324d4402011-01-11 16:56:42 -0800964 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 // update the bookmark database for favicon
966 maybeUpdateFavicon(tab, null, url, favicon);
967
968 Performance.tracePageStart(url);
969
970 // Performance probe
971 if (false) {
972 Performance.onPageStarted();
973 }
974
975 }
976
977 @Override
John Reck324d4402011-01-11 16:56:42 -0800978 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700979 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800980 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200981
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 // Performance probe
983 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800984 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 }
986
Tarun Nainani8eb00912014-07-17 12:28:32 -0700987 tab.onPageFinished();
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700988 maybeUpdateFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700989
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 Performance.tracePageFinished();
991 }
992
993 @Override
John Reck30c714c2010-12-16 17:30:34 -0800994 public void onProgressChanged(Tab tab) {
995 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700996
997 if (newProgress == 100) {
998 CookieSyncManager.getInstance().sync();
999 // onProgressChanged() may continue to be called after the main
1000 // frame has finished loading, as any remaining sub frames continue
1001 // to load. We'll only get called once though with newProgress as
1002 // 100 when everything is loaded. (onPageFinished is called once
1003 // when the main frame completes loading regardless of the state of
1004 // any sub frames so calls to onProgressChanges may continue after
1005 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001006 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001007 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001008 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001009 } else if (mWasInPageLoad) {
1010 mWasInPageLoad = false;
1011 updateInLoadMenuItems(mCachedMenu, tab);
1012 }
1013
1014 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001015 // pause the WebView timer and release the wake lock if it is
1016 // finished while BrowserActivity is in pause state.
1017 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 }
1019 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001020 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001021 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001022 // still loading
1023 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001025 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001026 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001027 } else {
1028 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001029 }
1030 }
Vivek Sekhar30d0a2e2015-07-28 19:38:01 -07001031 if (isInLoad())
1032 mUi.onProgressChanged(tab);
John Reck30c714c2010-12-16 17:30:34 -08001033 }
1034
1035 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001036 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001037 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 }
1039
1040 @Override
1041 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001042 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001043 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001044 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001045 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1046 return;
1047 }
1048 // Update the title in the history database if not in private browsing mode
1049 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001050 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001051 }
1052 }
1053
1054 @Override
1055 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001056 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001057 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1058 }
1059
1060 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001061 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001062 // if tab is snapshot tab we want to prevent navigation from occuring
1063 // since snapshot tab opens a new tab with the url
1064 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001065 }
1066
1067 @Override
1068 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1069 if (mMenuIsDown) {
1070 // only check shortcut key when MENU is held
1071 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1072 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001073 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001074 int keyCode = event.getKeyCode();
1075 // We need to send almost every key to WebKit. However:
1076 // 1. We don't want to block the device on the renderer for
1077 // some keys like menu, home, call.
1078 // 2. There are no WebKit equivalents for some of these keys
1079 // (see app/keyboard_codes_win.h)
1080 // Note that these are not the same set as KeyEvent.isSystemKey:
1081 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1082 if (keyCode == KeyEvent.KEYCODE_MENU ||
1083 keyCode == KeyEvent.KEYCODE_HOME ||
1084 keyCode == KeyEvent.KEYCODE_BACK ||
1085 keyCode == KeyEvent.KEYCODE_CALL ||
1086 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1087 keyCode == KeyEvent.KEYCODE_POWER ||
1088 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1089 keyCode == KeyEvent.KEYCODE_CAMERA ||
1090 keyCode == KeyEvent.KEYCODE_FOCUS ||
1091 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1092 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1093 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1094 return true;
1095 }
1096
1097 // We also have to intercept some shortcuts before we send them to the ContentView.
1098 if (event.isCtrlPressed() && (
1099 keyCode == KeyEvent.KEYCODE_TAB ||
1100 keyCode == KeyEvent.KEYCODE_W ||
1101 keyCode == KeyEvent.KEYCODE_F4)) {
1102 return true;
1103 }
1104
1105 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 }
1107
Tarun Nainanif5563b62015-01-21 18:52:59 -08001108 private void handleMediaKeyEvent(KeyEvent event) {
1109
1110 int keyCode = event.getKeyCode();
1111 // send media key events to audio manager
1112 if (Build.VERSION.SDK_INT >= 19) {
1113
1114 switch (keyCode) {
1115 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1116 case KeyEvent.KEYCODE_MEDIA_STOP:
1117 case KeyEvent.KEYCODE_MEDIA_NEXT:
1118 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1119 case KeyEvent.KEYCODE_MEDIA_REWIND:
1120 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1121 case KeyEvent.KEYCODE_MUTE:
1122 case KeyEvent.KEYCODE_MEDIA_PLAY:
1123 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1124 case KeyEvent.META_SHIFT_RIGHT_ON:
1125 case KeyEvent.KEYCODE_MEDIA_EJECT:
1126 case KeyEvent.KEYCODE_MEDIA_RECORD:
1127 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1128
1129 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1130 .getSystemService(Context.AUDIO_SERVICE);
1131 audioManager.dispatchMediaKeyEvent(event);
1132 }
1133 }
1134 }
1135
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 @Override
John Reck997b1b72012-04-19 18:08:25 -07001137 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001139 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001140 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001141 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001142 } else {
John Reck997b1b72012-04-19 18:08:25 -07001143 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 }
1145 }
John Reck997b1b72012-04-19 18:08:25 -07001146 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001147 }
1148
1149 @Override
John Reck324d4402011-01-11 16:56:42 -08001150 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001151 // Don't save anything in private browsing mode or when disabling history
1152 // for regular tabs is enabled
1153 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1154 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1155 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001156
John Reck49a603c2011-03-03 09:33:05 -08001157 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001158
John Reck324d4402011-01-11 16:56:42 -08001159 if (TextUtils.isEmpty(url)
1160 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 return;
1162 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001163
John Reckf57c0292011-07-21 18:15:39 -07001164 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001165 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 }
1167
1168 @Override
1169 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1170 AsyncTask<Void, Void, String[]> task =
1171 new AsyncTask<Void, Void, String[]>() {
1172 @Override
1173 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001174 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001175 }
1176 @Override
1177 public void onPostExecute(String[] result) {
1178 callback.onReceiveValue(result);
1179 }
1180 };
1181 task.execute();
1182 }
1183
1184 @Override
1185 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1186 final HttpAuthHandler handler, final String host,
1187 final String realm) {
1188 String username = null;
1189 String password = null;
1190
1191 boolean reuseHttpAuthUsernamePassword
1192 = handler.useHttpAuthUsernamePassword();
1193
1194 if (reuseHttpAuthUsernamePassword && view != null) {
1195 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1196 if (credentials != null && credentials.length == 2) {
1197 username = credentials[0];
1198 password = credentials[1];
1199 }
1200 }
1201
1202 if (username != null && password != null) {
1203 handler.proceed(username, password);
1204 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001205 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1207 } else {
1208 handler.cancel();
1209 }
1210 }
1211 }
1212
1213 @Override
1214 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001215 String contentDisposition, String mimetype, String referer,
1216 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001217 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001218 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001219 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001220 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001221 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001222 // This Tab was opened for the sole purpose of downloading a
1223 // file. Remove it.
1224 if (tab == mTabControl.getCurrentTab()) {
1225 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001226 if (tab.getDerivedFromIntent())
1227 closeTab(tab);
1228 else
1229 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001230 } else {
1231 // In this case, it is not.
1232 closeTab(tab);
1233 }
1234 }
1235 }
1236
1237 @Override
1238 public Bitmap getDefaultVideoPoster() {
1239 return mUi.getDefaultVideoPoster();
1240 }
1241
1242 @Override
1243 public View getVideoLoadingProgressView() {
1244 return mUi.getVideoLoadingProgressView();
1245 }
1246
1247 @Override
1248 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1249 SslError error) {
1250 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1251 }
1252
1253 // helper method
1254
1255 /*
1256 * Update the favorites icon if the private browsing isn't enabled and the
1257 * icon is valid.
1258 */
1259 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1260 final String url, Bitmap favicon) {
1261 if (favicon == null) {
1262 return;
1263 }
1264 if (!tab.isPrivateBrowsingEnabled()) {
1265 Bookmarks.updateFavicon(mActivity
1266 .getContentResolver(), originalUrl, url, favicon);
1267 }
1268 }
1269
Leon Scroggins4cd97792010-12-03 15:31:56 -05001270 @Override
1271 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001272 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001273 mUi.bookmarkedStatusHasChanged(tab);
1274 }
1275
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 // end WebViewController
1277
1278 protected void pageUp() {
1279 getCurrentTopWebView().pageUp(false);
1280 }
1281
1282 protected void pageDown() {
1283 getCurrentTopWebView().pageDown(false);
1284 }
1285
1286 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001287 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001288 public void editUrl() {
1289 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001290 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 }
1292
John Reck9c35b9c2012-05-30 10:08:50 -07001293 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001294 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001295 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001296 if (tab.inForeground()) {
1297 if (mUi.isCustomViewShowing()) {
1298 callback.onCustomViewHidden();
1299 return;
1300 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001301 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001302 // Save the menu state and set it to empty while the custom
1303 // view is showing.
1304 mOldMenuState = mMenuState;
1305 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001306 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001307 }
1308 }
1309
1310 @Override
1311 public void hideCustomView() {
1312 if (mUi.isCustomViewShowing()) {
1313 mUi.onHideCustomView();
1314 // Reset the old menu state.
1315 mMenuState = mOldMenuState;
1316 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001317 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001318 }
1319 }
1320
John Reck9c35b9c2012-05-30 10:08:50 -07001321 @Override
1322 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001323 Intent intent) {
1324 if (getCurrentTopWebView() == null) return;
1325 switch (requestCode) {
1326 case PREFERENCES_PAGE:
1327 if (resultCode == Activity.RESULT_OK && intent != null) {
1328 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001329 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001330 mTabControl.removeParentChildRelationShips();
1331 }
1332 }
1333 break;
1334 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001335 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001336 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001338 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001339 case AUTOFILL_SETUP:
1340 // Determine whether a profile was actually set up or not
1341 // and if so, send the message back to the WebTextView to
1342 // fill the form with the new profile.
1343 if (getSettings().getAutoFillProfile() != null) {
1344 mAutoFillSetupMessage.sendToTarget();
1345 mAutoFillSetupMessage = null;
1346 }
1347 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001348 case COMBO_VIEW:
1349 if (intent == null || resultCode != Activity.RESULT_OK) {
1350 break;
1351 }
John Reck3ba45532011-08-11 16:26:53 -07001352 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001353 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1354 Tab t = getCurrentTab();
1355 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001356 mUpdateMyNavThumbnail = true;
1357 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001358 loadUrl(t, uri.toString());
1359 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1360 String[] urls = intent.getStringArrayExtra(
1361 ComboViewActivity.EXTRA_OPEN_ALL);
1362 Tab parent = getCurrentTab();
1363 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001364 if (url != null) {
1365 parent = openTab(url, parent,
1366 !mSettings.openInBackground(), true);
1367 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001368 }
1369 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1370 long id = intent.getLongExtra(
1371 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1372 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001373 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001374 }
1375 }
1376 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001377 case VOICE_RESULT:
1378 if (resultCode == Activity.RESULT_OK && intent != null) {
1379 ArrayList<String> results = intent.getStringArrayListExtra(
1380 RecognizerIntent.EXTRA_RESULTS);
1381 if (results.size() >= 1) {
1382 mVoiceResult = results.get(0);
1383 }
1384 }
1385 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001386 case MY_NAVIGATION:
1387 if (intent == null || resultCode != Activity.RESULT_OK) {
1388 break;
1389 }
1390
1391 if (intent.getBooleanExtra("need_refresh", false) &&
1392 getCurrentTopWebView() != null) {
1393 getCurrentTopWebView().reload();
1394 }
1395 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 default:
1397 break;
1398 }
1399 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001400 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 }
1402
1403 /**
1404 * Open the Go page.
1405 * @param startWithHistory If true, open starting on the history tab.
1406 * Otherwise, start with the bookmarks tab.
1407 */
1408 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001409 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 if (mTabControl.getCurrentWebView() == null) {
1411 return;
1412 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001413 // clear action mode
1414 if (isInCustomActionMode()) {
1415 endActionMode();
1416 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 Bundle extras = new Bundle();
1418 // Disable opening in a new window if we have maxed out the windows
1419 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1420 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001421 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001422 }
1423
1424 // combo view callbacks
1425
Michael Kolb8233fac2010-10-26 16:08:53 -07001426 // key handling
1427 protected void onBackKey() {
1428 if (!mUi.onBackKey()) {
1429 WebView subwindow = mTabControl.getCurrentSubWindow();
1430 if (subwindow != null) {
1431 if (subwindow.canGoBack()) {
1432 subwindow.goBack();
1433 } else {
1434 dismissSubWindow(mTabControl.getCurrentTab());
1435 }
1436 } else {
1437 goBackOnePageOrQuit();
1438 }
1439 }
1440 }
1441
Michael Kolb4bd767d2011-05-27 11:33:55 -07001442 protected boolean onMenuKey() {
1443 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001444 }
1445
Michael Kolb8233fac2010-10-26 16:08:53 -07001446 // menu handling and state
1447 // TODO: maybe put into separate handler
1448
John Reck9c35b9c2012-05-30 10:08:50 -07001449 @Override
1450 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001451 if (mMenuState == EMPTY_MENU) {
1452 return false;
1453 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001454 MenuInflater inflater = mActivity.getMenuInflater();
1455 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001456 return true;
1457 }
1458
John Reck9c35b9c2012-05-30 10:08:50 -07001459 @Override
1460 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001461 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001462 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 return;
1464 }
1465 if (!(v instanceof WebView)) {
1466 return;
1467 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001468 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001469 WebView.HitTestResult result;
1470
1471 /* Determine whether the ContextMenu got triggered because
1472 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1473 * received. The mResult acts as a flag to identify, how it got trigerred
1474 */
1475 if (mResult == null){
1476 result = webview.getHitTestResult();
1477 } else {
1478 result = mResult;
1479 }
1480
Michael Kolb8233fac2010-10-26 16:08:53 -07001481 if (result == null) {
1482 return;
1483 }
1484
1485 int type = result.getType();
1486 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001487
1488 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1489 unknownTypeMap.put("webview", webview);
1490 final Message msg = mHandler.obtainMessage(
1491 UNKNOWN_TYPE_MSG, unknownTypeMap);
1492 /* As defined in android developers guide
1493 * when UNKNOWN_TYPE is received as a result of HitTest
1494 * you need to determing the type by invoking requestFocusNodeHref
1495 */
1496 webview.requestFocusNodeHref(msg);
1497
Michael Kolb8233fac2010-10-26 16:08:53 -07001498 Log.w(LOGTAG,
1499 "We should not show context menu when nothing is touched");
1500 return;
1501 }
1502 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1503 // let TextView handles context menu
1504 return;
1505 }
1506
1507 // Note, http://b/issue?id=1106666 is requesting that
1508 // an inflated menu can be used again. This is not available
1509 // yet, so inflate each time (yuk!)
1510 MenuInflater inflater = mActivity.getMenuInflater();
1511 inflater.inflate(R.menu.browsercontext, menu);
1512
1513 // Show the correct menu group
1514 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001515 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001516 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001517 menu.setGroupVisible(R.id.PHONE_MENU,
1518 type == WebView.HitTestResult.PHONE_TYPE);
1519 menu.setGroupVisible(R.id.EMAIL_MENU,
1520 type == WebView.HitTestResult.EMAIL_TYPE);
1521 menu.setGroupVisible(R.id.GEO_MENU,
1522 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001523 String itemUrl = null;
1524 String url = webview.getOriginalUrl();
1525 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1526 itemUrl = Uri.decode(navigationUrl);
1527 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1528 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1529 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1530 } else {
1531 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1532 }
1533 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1534 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1535 } else {
1536 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1537
1538 menu.setGroupVisible(R.id.IMAGE_MENU,
1539 type == WebView.HitTestResult.IMAGE_TYPE
1540 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1541 menu.setGroupVisible(R.id.ANCHOR_MENU,
1542 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1543 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001544
1545 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1546 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1547 UrlUtils.isDownloadableScheme(extra));
1548 }
1549 else {
1550 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1551 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001552 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001553 // Setup custom handling depending on the type
1554 switch (type) {
1555 case WebView.HitTestResult.PHONE_TYPE:
1556 menu.setHeaderTitle(Uri.decode(extra));
1557 menu.findItem(R.id.dial_context_menu_id).setIntent(
1558 new Intent(Intent.ACTION_VIEW, Uri
1559 .parse(WebView.SCHEME_TEL + extra)));
1560 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1561 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1562 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1563 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1564 addIntent);
1565 menu.findItem(R.id.copy_phone_context_menu_id)
1566 .setOnMenuItemClickListener(
1567 new Copy(extra));
1568 break;
1569
1570 case WebView.HitTestResult.EMAIL_TYPE:
1571 menu.setHeaderTitle(extra);
1572 menu.findItem(R.id.email_context_menu_id).setIntent(
1573 new Intent(Intent.ACTION_VIEW, Uri
1574 .parse(WebView.SCHEME_MAILTO + extra)));
1575 menu.findItem(R.id.copy_mail_context_menu_id)
1576 .setOnMenuItemClickListener(
1577 new Copy(extra));
1578 break;
1579
1580 case WebView.HitTestResult.GEO_TYPE:
1581 menu.setHeaderTitle(extra);
1582 menu.findItem(R.id.map_context_menu_id).setIntent(
1583 new Intent(Intent.ACTION_VIEW, Uri
1584 .parse(WebView.SCHEME_GEO
1585 + URLEncoder.encode(extra))));
1586 menu.findItem(R.id.copy_geo_context_menu_id)
1587 .setOnMenuItemClickListener(
1588 new Copy(extra));
1589 break;
1590
1591 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1592 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001593 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001594 // decide whether to show the open link in new tab option
1595 boolean showNewTab = mTabControl.canCreateNewTab();
1596 MenuItem newTabItem
1597 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001598 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001599 ? R.string.contextmenu_openlink_newwindow_background
1600 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 newTabItem.setVisible(showNewTab);
1602 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001603 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1604 newTabItem.setOnMenuItemClickListener(
1605 new MenuItem.OnMenuItemClickListener() {
1606 @Override
1607 public boolean onMenuItemClick(MenuItem item) {
1608 final HashMap<String, WebView> hrefMap =
1609 new HashMap<String, WebView>();
1610 hrefMap.put("webview", webview);
1611 final Message msg = mHandler.obtainMessage(
1612 FOCUS_NODE_HREF,
1613 R.id.open_newtab_context_menu_id,
1614 0, hrefMap);
1615 webview.requestFocusNodeHref(msg);
1616 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001618 });
1619 } else {
1620 newTabItem.setOnMenuItemClickListener(
1621 new MenuItem.OnMenuItemClickListener() {
1622 @Override
1623 public boolean onMenuItemClick(MenuItem item) {
1624 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001625 openTab(extra, parent,
1626 !mSettings.openInBackground(),
1627 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001628 return true;
1629 }
1630 });
1631 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001632 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001633 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1634 menu.setHeaderTitle(navigationUrl);
1635 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1636
1637 if (itemUrl != null) {
1638 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1639 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1640 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1641 @Override
1642 public boolean onMenuItemClick(MenuItem item) {
1643 final Intent intent = new Intent(Controller.this
1644 .getContext(),
1645 AddMyNavigationPage.class);
1646 Bundle bundle = new Bundle();
1647 String url = Uri.decode(navigationUrl);
1648 bundle.putBoolean("isAdding", false);
1649 bundle.putString("url", url);
1650 bundle.putString("name", getNameFromUrl(url));
1651 intent.putExtra("websites", bundle);
1652 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1653 return false;
1654 }
1655 });
1656 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1657 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1658 @Override
1659 public boolean onMenuItemClick(MenuItem item) {
1660 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1661 return false;
1662 }
1663 });
1664 }
1665 } else {
1666 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1667 }
1668 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001669 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1670 break;
1671 }
1672 // otherwise fall through to handle image part
1673 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001674 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1675 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001676 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1677 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001678 shareItem.setOnMenuItemClickListener(
1679 new MenuItem.OnMenuItemClickListener() {
1680 @Override
1681 public boolean onMenuItemClick(MenuItem item) {
1682 sharePage(mActivity, null, extra, null,
1683 null);
1684 return true;
1685 }
1686 }
1687 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001688 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001689 menu.findItem(R.id.view_image_context_menu_id)
1690 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1691 @Override
1692 public boolean onMenuItemClick(MenuItem item) {
1693 openTab(extra, mTabControl.getCurrentTab(), true, true);
1694 return false;
1695 }
1696 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001697 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1698 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1699 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001700 menu.findItem(R.id.set_wallpaper_context_menu_id).
1701 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1702 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001703 break;
1704
1705 default:
1706 Log.w(LOGTAG, "We should not get here.");
1707 break;
1708 }
1709 //update the ui
1710 mUi.onContextMenuCreated(menu);
1711 }
1712
kaiyiz6e5b3e02013-08-19 20:02:01 +08001713 public void startAddMyNavigation(String url) {
1714 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1715 Bundle bundle = new Bundle();
1716 bundle.putBoolean("isAdding", true);
1717 bundle.putString("url", url);
1718 bundle.putString("name", getNameFromUrl(url));
1719 intent.putExtra("websites", bundle);
1720 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1721 }
1722
1723 private void showMyNavigationDeleteDialog(final String itemUrl) {
1724 new AlertDialog.Builder(this.getContext())
1725 .setTitle(R.string.my_navigation_delete_label)
1726 .setIcon(android.R.drawable.ic_dialog_alert)
1727 .setMessage(R.string.my_navigation_delete_msg)
1728 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1729 @Override
1730 public void onClick(DialogInterface dialog, int whichButton) {
1731 deleteMyNavigationItem(itemUrl);
1732 }
1733 })
1734 .setNegativeButton(R.string.cancel, null)
1735 .show();
1736 }
1737
1738 private void deleteMyNavigationItem(final String itemUrl) {
1739 ContentResolver cr = this.getContext().getContentResolver();
1740 Cursor cursor = null;
1741
1742 try {
1743 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1744 new String[] {
1745 MyNavigationUtil.ID
1746 }, "url = ?", new String[] {
1747 itemUrl
1748 }, null);
1749 if (null != cursor && cursor.moveToFirst()) {
1750 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1751 cursor.getLong(0));
1752
1753 ContentValues values = new ContentValues();
1754 values.put(MyNavigationUtil.TITLE, "");
1755 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1756 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1757 ByteArrayOutputStream os = new ByteArrayOutputStream();
1758 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1759 R.raw.my_navigation_add);
1760 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1761 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1762 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1763 cr.update(uri, values, null, null);
1764 } else {
1765 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1766 }
1767 } catch (IllegalStateException e) {
1768 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1769 } finally {
1770 if (null != cursor) {
1771 cursor.close();
1772 }
1773 }
1774
1775 if (getCurrentTopWebView() != null) {
1776 getCurrentTopWebView().reload();
1777 }
1778 }
1779
1780 private String getNameFromUrl(String itemUrl) {
1781 ContentResolver cr = this.getContext().getContentResolver();
1782 Cursor cursor = null;
1783 String name = null;
1784
1785 try {
1786 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1787 new String[] {
1788 MyNavigationUtil.TITLE
1789 }, "url = ?", new String[] {
1790 itemUrl
1791 }, null);
1792 if (null != cursor && cursor.moveToFirst()) {
1793 name = cursor.getString(0);
1794 } else {
1795 Log.e(LOGTAG, "this item does not exist!");
1796 }
1797 } catch (IllegalStateException e) {
1798 Log.e(LOGTAG, "getNameFromUrl", e);
1799 } finally {
1800 if (null != cursor) {
1801 cursor.close();
1802 }
1803 }
1804 return name;
1805 }
1806
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001807 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001808 final ContentResolver cr = mActivity.getContentResolver();
1809 new AsyncTask<Void, Void, Void>() {
1810 @Override
1811 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001812 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1813 if(!isMyNavigationUrl)
1814 return null;
1815
kaiyiz6e5b3e02013-08-19 20:02:01 +08001816 ContentResolver cr = mActivity.getContentResolver();
1817 Cursor cursor = null;
1818 try {
1819 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1820 new String[] {
1821 MyNavigationUtil.ID
1822 }, "url = ?", new String[] {
1823 itemUrl
1824 }, null);
1825 if (null != cursor && cursor.moveToFirst()) {
1826 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001827 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001828
1829 ContentValues values = new ContentValues();
1830 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1831 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1832 cursor.getLong(0));
1833 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1834 cr.update(uri, values, null, null);
1835 os.close();
1836 }
1837 } catch (IllegalStateException e) {
1838 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1839 } catch (IOException e) {
1840 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1841 } finally {
1842 if (null != cursor) {
1843 cursor.close();
1844 }
1845 }
1846 return null;
1847 }
1848 }.execute();
1849 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001850 /**
1851 * As the menu can be open when loading state changes
1852 * we must manually update the state of the stop/reload menu
1853 * item
1854 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001855 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001856 if (menu == null) {
1857 return;
1858 }
1859 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001860 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001861 menu.findItem(R.id.stop_menu_id):
1862 menu.findItem(R.id.reload_menu_id);
1863 if (src != null) {
1864 dest.setIcon(src.getIcon());
1865 dest.setTitle(src.getTitle());
1866 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001867 mActivity.invalidateOptionsMenu();
1868 }
1869
1870 public void invalidateOptionsMenu() {
1871 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001872 }
1873
John Reck9c35b9c2012-05-30 10:08:50 -07001874 @Override
1875 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001876 // Software menu key (toolbar key)
1877 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1878 return true;
1879 }
1880
1881 @Override
1882 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001883 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001884 // hold on to the menu reference here; it is used by the page callbacks
1885 // to update the menu based on loading state
1886 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 // Note: setVisible will decide whether an item is visible; while
1888 // setEnabled() will decide whether an item is enabled, which also means
1889 // whether the matching shortcut key will function.
1890 switch (mMenuState) {
1891 case EMPTY_MENU:
1892 if (mCurrentMenuState != mMenuState) {
1893 menu.setGroupVisible(R.id.MAIN_MENU, false);
1894 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1895 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1896 }
1897 break;
1898 default:
1899 if (mCurrentMenuState != mMenuState) {
1900 menu.setGroupVisible(R.id.MAIN_MENU, true);
1901 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1902 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1903 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001904 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001905 break;
1906 }
1907 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001908 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001909
1910 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001911 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1912 EditBookmarksRestriction.getInstance()
1913 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001914 }
1915
1916 private void setMenuItemVisibility(Menu menu, int id,
1917 boolean visibility) {
1918 MenuItem item = menu.findItem(id);
1919 if (item != null) {
1920 item.setVisible(visibility);
1921 }
1922 }
1923
1924 private int lookupBookmark(String title, String url) {
1925 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001926 int count = 0;
1927 Cursor cursor = null;
1928 try {
1929 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1930 BookmarksLoader.PROJECTION,
1931 "title = ? OR url = ?",
1932 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001933 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001934 },
1935 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001936
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001937 if (cursor != null)
1938 count = cursor.getCount();
1939
1940 } catch (IllegalStateException e) {
1941 Log.e(LOGTAG, "lookupBookmark ", e);
1942 } finally {
1943 if (null != cursor) {
1944 cursor.close();
1945 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001946 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001947 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001948 }
1949
1950 private void resetMenuItems(Menu menu) {
1951 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1952 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1953
1954 WebView w = getCurrentTopWebView();
1955 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1956
1957 String title = w.getTitle();
1958 String url = w.getUrl();
Pankaj Garg634bf432015-07-13 09:54:21 -07001959 mCurrentPageBookmarked = (lookupBookmark(title, url) > 0);
1960 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001961 bookmark_icon.setChecked(true);
1962 } else {
1963 bookmark_icon.setChecked(false);
1964 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001965
1966 // update reader mode checkbox
1967 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1968 readerSwitcher.setVisible(false);
1969 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001970 }
1971
Michael Kolb4bf79712011-07-14 14:18:12 -07001972 @Override
1973 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001974 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001975 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001976 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001977 // Following flag is used to identify schemes for which the LIVE_MENU
1978 // items defined in res/menu/browser.xml should be enabled
1979 boolean isLiveScheme = false;
1980 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001981 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07001982
1983 boolean isDistillable = false;
1984 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001985 resetMenuItems(menu);
1986
Michael Kolb4bf79712011-07-14 14:18:12 -07001987 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001988 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001989 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001990 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001991 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001992 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001993 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07001994
1995 isDistillable = tab.isDistillable();
1996 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07001997 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001998
1999 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2000 forward.setEnabled(canGoForward);
2001
Michael Kolb4bf79712011-07-14 14:18:12 -07002002 // decide whether to show the share link option
2003 PackageManager pm = mActivity.getPackageManager();
2004 Intent send = new Intent(Intent.ACTION_SEND);
2005 send.setType("text/plain");
2006 ResolveInfo ri = pm.resolveActivity(send,
2007 PackageManager.MATCH_DEFAULT_ONLY);
2008 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2009
2010 boolean isNavDump = mSettings.enableNavDump();
2011 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2012 nav.setVisible(isNavDump);
2013 nav.setEnabled(isNavDump);
2014
2015 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002016 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2017 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002018 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002019 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2020 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002021 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002022 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2023 isLive && isLiveScheme && isPageFinished);
2024 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002025 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002026 // history and snapshots item are the members of COMBO menu group,
2027 // so if show history item, only make snapshots item invisible.
2028 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002029
Ze G Riande2a675c22015-06-03 11:15:24 -07002030
2031 // update reader mode checkbox
2032 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2033 // The reader mode checkbox is hidden only
2034 // when the current page is neither distillable nor distilled
2035 readerSwitcher.setVisible(isDistillable || isDistilled);
2036 readerSwitcher.setChecked(isDistilled);
2037
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002038 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002039 }
2040
John Reck9c35b9c2012-05-30 10:08:50 -07002041 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002043 if (null == getCurrentTopWebView()) {
2044 return false;
2045 }
2046 if (mMenuIsDown) {
2047 // The shortcut action consumes the MENU. Even if it is still down,
2048 // it won't trigger the next shortcut action. In the case of the
2049 // shortcut action triggering a new activity, like Bookmarks, we
2050 // won't get onKeyUp for MENU. So it is important to reset it here.
2051 mMenuIsDown = false;
2052 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002053 if (mUi.onOptionsItemSelected(item)) {
2054 // ui callback handled it
2055 return true;
2056 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002057 switch (item.getItemId()) {
2058 // -- Main menu
2059 case R.id.new_tab_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002060 getCurrentTab().capture();
Michael Kolb8233fac2010-10-26 16:08:53 -07002061 openTabToHomePage();
2062 break;
2063
2064 case R.id.incognito_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002065 getCurrentTab().capture();
Michael Kolb519d2282011-05-09 17:03:19 -07002066 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 break;
2068
2069 case R.id.goto_menu_id:
2070 editUrl();
2071 break;
2072
2073 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002074 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2075 break;
2076
2077 case R.id.history_menu_id:
2078 bookmarksOrHistoryPicker(ComboViews.History);
2079 break;
2080
2081 case R.id.snapshots_menu_id:
2082 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002083 break;
2084
Pankaj Garg49b79252014-11-07 17:33:41 -08002085 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002086 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002087 break;
2088
2089 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002090 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 stopLoading();
2092 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002093 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002094 getCurrentTopWebView().reload();
2095 }
2096 break;
2097
Michael Kolb8233fac2010-10-26 16:08:53 -07002098 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002099 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002100 break;
2101
2102 case R.id.close_menu_id:
2103 // Close the subwindow if it exists.
2104 if (mTabControl.getCurrentSubWindow() != null) {
2105 dismissSubWindow(mTabControl.getCurrentTab());
2106 break;
2107 }
2108 closeCurrentTab();
2109 break;
2110
kaiyiza8b6dbb2013-07-29 18:11:22 +08002111 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002112 Object[] params = { new String("persist.debug.browsermonkeytest")};
2113 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002114 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002115 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002116 if (ret != null && ret.equals("enable"))
2117 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002118 if (BrowserConfig.getInstance(getContext())
2119 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2120 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002121 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002122 case R.id.homepage_menu_id:
2123 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002124 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002125 break;
2126
2127 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002128 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002129 break;
2130
2131 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002132 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 break;
2134
John Reck2bc80422011-06-30 15:11:49 -07002135 case R.id.save_snapshot_menu_id:
2136 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002137 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002138 createScreenshotAsync(
2139 source.getWebView(),
2140 getDesiredThumbnailWidth(mActivity),
2141 getDesiredThumbnailHeight(mActivity),
2142 new ValueCallback<Bitmap>() {
2143 @Override
2144 public void onReceiveValue(Bitmap bitmap) {
2145 new SaveSnapshotTask(source, bitmap).execute();
2146 }
2147 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002148 break;
2149
Michael Kolb8233fac2010-10-26 16:08:53 -07002150 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002151 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002152 break;
2153
John Recke1a03a32011-09-14 17:04:16 -07002154 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002155 // passing null to distinguish between
2156 // "go live" button and navigating a web page
2157 // on a snapshot tab
2158 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002159 case R.id.share_page_menu_id:
2160 Tab currentTab = mTabControl.getCurrentTab();
2161 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002162 return false;
2163 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002164 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002165 break;
2166
2167 case R.id.dump_nav_menu_id:
2168 getCurrentTopWebView().debugDump();
2169 break;
2170
Michael Kolb8233fac2010-10-26 16:08:53 -07002171 case R.id.zoom_in_menu_id:
2172 getCurrentTopWebView().zoomIn();
2173 break;
2174
2175 case R.id.zoom_out_menu_id:
2176 getCurrentTopWebView().zoomOut();
2177 break;
2178
2179 case R.id.view_downloads_menu_id:
2180 viewDownloads();
2181 break;
2182
John Reck42229bc2011-08-19 13:26:43 -07002183 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002184 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002185 break;
2186
Ze G Riande2a675c22015-06-03 11:15:24 -07002187 case R.id.reader_mode_menu_id:
2188 toggleReaderMode();
2189 break;
2190
Michael Kolb8233fac2010-10-26 16:08:53 -07002191 case R.id.window_one_menu_id:
2192 case R.id.window_two_menu_id:
2193 case R.id.window_three_menu_id:
2194 case R.id.window_four_menu_id:
2195 case R.id.window_five_menu_id:
2196 case R.id.window_six_menu_id:
2197 case R.id.window_seven_menu_id:
2198 case R.id.window_eight_menu_id:
2199 {
2200 int menuid = item.getItemId();
2201 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2202 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2203 Tab desiredTab = mTabControl.getTab(id);
2204 if (desiredTab != null &&
2205 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002206 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002207 }
2208 break;
2209 }
2210 }
2211 }
2212 break;
2213
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002214 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002215 Bundle bundle = new Bundle();
2216 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2217 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2218 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2219 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2220 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002221 break;
2222
Pankaj Garg49b79252014-11-07 17:33:41 -08002223 case R.id.add_to_homescreen:
2224 final WebView w = getCurrentTopWebView();
2225 final EditText input = new EditText(getContext());
2226 input.setText(w.getTitle());
2227 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002228 .setTitle(getContext().getResources().getString(
2229 R.string.add_to_homescreen))
2230 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002231 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002232 .setPositiveButton(getContext().getResources().getString(
2233 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002234 public void onClick(DialogInterface dialog, int whichButton) {
2235 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2236 getContext(),
2237 w.getUrl(),
2238 input.getText().toString(),
2239 w.getViewportBitmap(),
2240 w.getFavicon()));
2241
2242 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2243 .addCategory(Intent.CATEGORY_HOME));
2244 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002245 .setNegativeButton(getContext().getResources().getString(
2246 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002247 public void onClick(DialogInterface dialog, int whichButton) {
2248 // Do nothing.
2249 }
2250 })
2251 .show();
2252 break;
2253
Michael Kolb8233fac2010-10-26 16:08:53 -07002254 default:
2255 return false;
2256 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002257 return true;
2258 }
2259
John Reck68234a92012-04-19 15:27:12 -07002260 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2261 implements OnCancelListener {
2262
2263 private Tab mTab;
2264 private Dialog mProgressDialog;
2265 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002266 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002267
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002268 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002269 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002270 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002271 }
2272
2273 @Override
2274 protected void onPreExecute() {
2275 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2276 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2277 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002278 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002279 }
2280
2281 @Override
2282 protected Long doInBackground(Void... params) {
2283 if (!mTab.saveViewState(mValues)) {
2284 return null;
2285 }
2286 if (isCancelled()) {
2287 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2288 File file = mActivity.getFileStreamPath(path);
2289 if (!file.delete()) {
2290 file.deleteOnExit();
2291 }
2292 return null;
2293 }
2294 final ContentResolver cr = mActivity.getContentResolver();
2295 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2296 if (result == null) {
2297 return null;
2298 }
2299 long id = ContentUris.parseId(result);
2300 return id;
2301 }
2302
2303 @Override
2304 protected void onPostExecute(Long id) {
2305 if (isCancelled()) {
2306 return;
2307 }
2308 mProgressDialog.dismiss();
2309 if (id == null) {
2310 Toast.makeText(mActivity, R.string.snapshot_failed,
2311 Toast.LENGTH_SHORT).show();
2312 return;
2313 }
2314 Bundle b = new Bundle();
2315 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2316 mUi.showComboView(ComboViews.Snapshots, b);
2317 }
2318
2319 @Override
2320 public void onCancel(DialogInterface dialog) {
2321 cancel(true);
2322 }
2323 }
2324
Michael Kolb80f75082012-04-10 10:50:06 -07002325 @Override
2326 public void toggleUserAgent() {
2327 WebView web = getCurrentWebView();
2328 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002329 }
2330
Ze G Riande2a675c22015-06-03 11:15:24 -07002331 // This function calls the method in the webview to enable/disable
2332 // the reader mode through the DOM distiller
2333 public void toggleReaderMode() {
2334 Tab t = mTabControl.getCurrentTab();
2335 if (t.isDistilled()) {
2336 closeTab(t);
2337 } else if (t.isDistillable()) {
2338 openTab(t.getDistilledUrl(), false, true, false, t);
2339 }
2340 }
2341
Michael Kolb80f75082012-04-10 10:50:06 -07002342 @Override
2343 public void findOnPage() {
2344 getCurrentTopWebView().showFindDialog(null, true);
2345 }
2346
2347 @Override
2348 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002349 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002350 }
2351
2352 @Override
2353 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002354 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002355 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2356 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002357 }
2358 else {
2359 WebView w = getCurrentTopWebView();
2360 if (w == null)
2361 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002362 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002363 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002364 }
Michael Kolb80f75082012-04-10 10:50:06 -07002365 }
2366
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002367 public boolean goLive(String url) {
2368 if (!getCurrentTab().isSnapshot())
2369 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002370 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002371
2372 if (url == null) { // "go live" button was clicked
2373 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002374 closeTab(t);
2375 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002376 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002377 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002378 return true;
John Recke1a03a32011-09-14 17:04:16 -07002379 }
2380
luxiaolb40014b2013-07-19 10:01:43 +08002381 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002382 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002383 new AlertDialog.Builder(activity)
2384 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002385 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002386 .setMessage(R.string.exit_browser_msg)
2387 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2388 public void onClick(DialogInterface dialog, int which) {
2389 activity.moveTaskToBack(true);
2390 dialog.dismiss();
2391 }
2392 })
2393 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2394 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002395 mCrashRecoveryHandler.clearState(true);
2396 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002397 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002398 dialog.dismiss();
2399 }
2400 })
2401 .show();
2402 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002403
Michael Kolb315d5022011-10-13 12:47:11 -07002404 @Override
2405 public void showPageInfo() {
2406 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2407 }
2408
John Reck9c35b9c2012-05-30 10:08:50 -07002409 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002410 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002411 // Let the History and Bookmark fragments handle menus they created.
2412 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2413 return false;
2414 }
2415
Michael Kolb8233fac2010-10-26 16:08:53 -07002416 int id = item.getItemId();
2417 boolean result = true;
2418 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002419 // -- Browser context menu
2420 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002421 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002422 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002423 case R.id.copy_link_context_menu_id:
2424 final WebView webView = getCurrentTopWebView();
2425 if (null == webView) {
2426 result = false;
2427 break;
2428 }
2429 final HashMap<String, WebView> hrefMap =
2430 new HashMap<String, WebView>();
2431 hrefMap.put("webview", webView);
2432 final Message msg = mHandler.obtainMessage(
2433 FOCUS_NODE_HREF, id, 0, hrefMap);
2434 webView.requestFocusNodeHref(msg);
2435 break;
2436
2437 default:
2438 // For other context menus
2439 result = onOptionsItemSelected(item);
2440 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002441 return result;
2442 }
2443
2444 /**
2445 * support programmatically opening the context menu
2446 */
2447 public void openContextMenu(View view) {
2448 mActivity.openContextMenu(view);
2449 }
2450
2451 /**
2452 * programmatically open the options menu
2453 */
2454 public void openOptionsMenu() {
2455 mActivity.openOptionsMenu();
2456 }
2457
John Reck9c35b9c2012-05-30 10:08:50 -07002458 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002459 public boolean onMenuOpened(int featureId, Menu menu) {
2460 if (mOptionsMenuOpen) {
2461 if (mConfigChanged) {
2462 // We do not need to make any changes to the state of the
2463 // title bar, since the only thing that happened was a
2464 // change in orientation
2465 mConfigChanged = false;
2466 } else {
2467 if (!mExtendedMenuOpen) {
2468 mExtendedMenuOpen = true;
2469 mUi.onExtendedMenuOpened();
2470 } else {
2471 // Switching the menu back to icon view, so show the
2472 // title bar once again.
2473 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002474 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002475 }
2476 }
2477 } else {
2478 // The options menu is closed, so open it, and show the title
2479 mOptionsMenuOpen = true;
2480 mConfigChanged = false;
2481 mExtendedMenuOpen = false;
2482 mUi.onOptionsMenuOpened();
2483 }
2484 return true;
2485 }
2486
John Reck9c35b9c2012-05-30 10:08:50 -07002487 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002488 public void onOptionsMenuClosed(Menu menu) {
2489 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002490 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002491 }
2492
John Reck9c35b9c2012-05-30 10:08:50 -07002493 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002494 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002495 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002496 }
2497
2498 // Helper method for getting the top window.
2499 @Override
2500 public WebView getCurrentTopWebView() {
2501 return mTabControl.getCurrentTopWebView();
2502 }
2503
2504 @Override
2505 public WebView getCurrentWebView() {
2506 return mTabControl.getCurrentWebView();
2507 }
2508
2509 /*
2510 * This method is called as a result of the user selecting the options
2511 * menu to see the download window. It shows the download window on top of
2512 * the current window.
2513 */
2514 void viewDownloads() {
2515 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2516 mActivity.startActivity(intent);
2517 }
2518
John Reck30b065e2011-07-19 10:58:05 -07002519 int getActionModeHeight() {
2520 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2521 new int[] { android.R.attr.actionBarSize });
2522 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2523 actionBarSizeTypedArray.recycle();
2524 return size;
2525 }
2526
Michael Kolb8233fac2010-10-26 16:08:53 -07002527 // action mode
2528
John Reck9c35b9c2012-05-30 10:08:50 -07002529 @Override
2530 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002531 mUi.onActionModeStarted(mode);
2532 mActionMode = mode;
2533 }
2534
2535 /*
2536 * True if a custom ActionMode (i.e. find or select) is in use.
2537 */
2538 @Override
2539 public boolean isInCustomActionMode() {
2540 return mActionMode != null;
2541 }
2542
2543 /*
2544 * End the current ActionMode.
2545 */
2546 @Override
2547 public void endActionMode() {
2548 if (mActionMode != null) {
2549 mActionMode.finish();
2550 }
2551 }
2552
2553 /*
2554 * Called by find and select when they are finished. Replace title bars
2555 * as necessary.
2556 */
John Reck9c35b9c2012-05-30 10:08:50 -07002557 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002558 public void onActionModeFinished(ActionMode mode) {
2559 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002560 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002561 mActionMode = null;
2562 }
2563
2564 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002565 final Tab tab = getCurrentTab();
2566 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002567 }
2568
2569 // bookmark handling
2570
2571 /**
2572 * add the current page as a bookmark to the given folder id
2573 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002574 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002575 * bookmarked, and if it is, edit that bookmark. If false, and
2576 * the site is already bookmarked, do not attempt to edit the
2577 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 */
2579 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002580 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002581 WebView w = getCurrentTopWebView();
2582 if (w == null) {
2583 return null;
2584 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002585 Intent i = new Intent(mActivity,
2586 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002587 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2588 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002589 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002590 if (touchIconUrl != null) {
2591 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2592 WebSettings settings = w.getSettings();
2593 if (settings != null) {
2594 i.putExtra(AddBookmarkPage.USER_AGENT,
2595 settings.getUserAgentString());
2596 }
2597 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002598 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002600 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002601 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2602 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002603 // Put the dialog at the upper right of the screen, covering the
2604 // star on the title bar.
2605 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002606 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002607 }
2608
2609 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002610 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002611 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002613 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002614 }
2615
Vivek Sekharb54614f2014-05-01 19:03:37 -07002616 @Override
2617 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2618 boolean capture) {
2619 mUploadHandler = new UploadHandler(this);
2620 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2621 }
2622
Michael Kolb8233fac2010-10-26 16:08:53 -07002623 // thumbnails
2624
2625 /**
2626 * Return the desired width for thumbnail screenshots, which are stored in
2627 * the database, and used on the bookmarks screen.
2628 * @param context Context for finding out the density of the screen.
2629 * @return desired width for thumbnail screenshot.
2630 */
2631 static int getDesiredThumbnailWidth(Context context) {
2632 return context.getResources().getDimensionPixelOffset(
2633 R.dimen.bookmarkThumbnailWidth);
2634 }
2635
2636 /**
2637 * Return the desired height for thumbnail screenshots, which are stored in
2638 * the database, and used on the bookmarks screen.
2639 * @param context Context for finding out the density of the screen.
2640 * @return desired height for thumbnail screenshot.
2641 */
2642 static int getDesiredThumbnailHeight(Context context) {
2643 return context.getResources().getDimensionPixelOffset(
2644 R.dimen.bookmarkThumbnailHeight);
2645 }
2646
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002647 static void createScreenshotAsync(WebView view, int width, int height,
2648 final ValueCallback<Bitmap> cb) {
2649 if (view == null || width == 0 || height == 0) {
2650 return;
2651 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002652 view.getContentBitmapAsync(
2653 (float) width / view.getWidth(),
2654 new Rect(),
2655 new ValueCallback<Bitmap>() {
2656 @Override
2657 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002658 if (bitmap != null)
2659 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2660 cb.onReceiveValue(bitmap);
2661 }});
2662 }
2663
Michael Kolb8233fac2010-10-26 16:08:53 -07002664 private class Copy implements OnMenuItemClickListener {
2665 private CharSequence mText;
2666
John Reck9c35b9c2012-05-30 10:08:50 -07002667 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002668 public boolean onMenuItemClick(MenuItem item) {
2669 copy(mText);
2670 return true;
2671 }
2672
2673 public Copy(CharSequence toCopy) {
2674 mText = toCopy;
2675 }
2676 }
2677
Leon Scroggins63c02662010-11-18 15:16:27 -05002678 private static class Download implements OnMenuItemClickListener {
2679 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002680 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002681 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002682 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002683 private static final String FALLBACK_EXTENSION = "dat";
2684 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002685
John Reck9c35b9c2012-05-30 10:08:50 -07002686 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002687 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002688 if (DataUri.isDataUri(mText)) {
2689 saveDataUri();
2690 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002691 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002692 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002693 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002694 return true;
2695 }
2696
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002697 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2698 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002699 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002700 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002701 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002702 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002703 }
George Mount387d45d2011-10-07 15:57:53 -07002704
2705 /**
2706 * Treats mText as a data URI and writes its contents to a file
2707 * based on the current time.
2708 */
2709 private void saveDataUri() {
2710 FileOutputStream outputStream = null;
2711 try {
2712 DataUri uri = new DataUri(mText);
2713 File target = getTarget(uri);
2714 outputStream = new FileOutputStream(target);
2715 outputStream.write(uri.getData());
2716 final DownloadManager manager =
2717 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2718 manager.addCompletedDownload(target.getName(),
2719 mActivity.getTitle().toString(), false,
2720 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002721 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002722 } catch (IOException e) {
2723 Log.e(LOGTAG, "Could not save data URL");
2724 } finally {
2725 if (outputStream != null) {
2726 try {
2727 outputStream.close();
2728 } catch (IOException e) {
2729 // ignore close errors
2730 }
2731 }
2732 }
2733 }
2734
2735 /**
2736 * Creates a File based on the current time stamp and uses
2737 * the mime type of the DataUri to get the extension.
2738 */
2739 private File getTarget(DataUri uri) throws IOException {
2740 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002741 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002742 String nameBase = format.format(new Date());
2743 String mimeType = uri.getMimeType();
2744 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2745 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2746 if (extension == null) {
2747 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2748 extension = FALLBACK_EXTENSION;
2749 }
2750 extension = "." + extension; // createTempFile needs the '.'
2751 File targetFile = File.createTempFile(nameBase, extension, dir);
2752 return targetFile;
2753 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002754 }
2755
Cary Clark8974d282010-11-22 10:46:05 -05002756 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002757 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002758
John Reck9c35b9c2012-05-30 10:08:50 -07002759 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002760 public boolean onMenuItemClick(MenuItem item) {
2761 if (mWebView != null) {
2762 return mWebView.selectText();
2763 }
2764 return false;
2765 }
2766
2767 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002768 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002769 }
2770
2771 }
2772
Michael Kolb8233fac2010-10-26 16:08:53 -07002773 /********************** TODO: UI stuff *****************************/
2774
2775 // these methods have been copied, they still need to be cleaned up
2776
2777 /****************** tabs ***************************************************/
2778
2779 // basic tab interactions:
2780
2781 // it is assumed that tabcontrol already knows about the tab
2782 protected void addTab(Tab tab) {
2783 mUi.addTab(tab);
2784 }
2785
2786 protected void removeTab(Tab tab) {
2787 mUi.removeTab(tab);
2788 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002789 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002790 }
2791
Michael Kolbf2055602011-04-09 17:20:03 -07002792 @Override
2793 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002794 // monkey protection against delayed start
2795 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002796
2797 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2798 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002799 mTabControl.setCurrentTab(tab);
2800 // the tab is guaranteed to have a webview after setCurrentTab
2801 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002802
2803
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002804 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002805 //Purge active tabs
2806 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002807 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002808 }
2809
John Reck8bcafc12011-08-29 16:43:02 -07002810 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002811 Tab current = mTabControl.getCurrentTab();
2812 if (current != null
2813 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002814 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002815 }
2816 }
2817
John Reck26b18322011-06-21 13:08:58 -07002818 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002819 //Cancel navscreen request
2820 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002821 // Dismiss the subwindow if applicable.
2822 dismissSubWindow(appTab);
2823 // Since we might kill the WebView, remove it from the
2824 // content view first.
2825 mUi.detachTab(appTab);
2826 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002827 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002828 // TODO: analyze why the remove and add are necessary
2829 mUi.attachTab(appTab);
2830 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002831 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002832 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002833 } else {
2834 // If the tab was the current tab, we have to attach
2835 // it to the view system again.
2836 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002837 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002838 }
2839 }
2840
2841 // Remove the sub window if it exists. Also called by TabControl when the
2842 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002843 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002844 public void dismissSubWindow(Tab tab) {
2845 removeSubWindow(tab);
2846 // dismiss the subwindow. This will destroy the WebView.
2847 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002848 WebView wv = getCurrentTopWebView();
2849 if (wv != null) {
2850 wv.requestFocus();
2851 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002852 }
2853
2854 @Override
2855 public void removeSubWindow(Tab t) {
2856 if (t.getSubWebView() != null) {
2857 mUi.removeSubWindow(t.getSubViewContainer());
2858 }
2859 }
2860
2861 @Override
2862 public void attachSubWindow(Tab tab) {
2863 if (tab.getSubWebView() != null) {
2864 mUi.attachSubWindow(tab.getSubViewContainer());
2865 getCurrentTopWebView().requestFocus();
2866 }
2867 }
2868
Mathew Inwood29721c22011-06-29 17:55:24 +01002869 private Tab showPreloadedTab(final UrlData urlData) {
2870 if (!urlData.isPreloaded()) {
2871 return null;
2872 }
2873 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2874 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2875 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002876 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002877 // Could not submit query. Fallback to regular tab creation
2878 tabControl.destroy();
2879 return null;
2880 }
2881 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002882 // check tab count and make room for new tab
2883 if (!mTabControl.canCreateNewTab()) {
2884 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2885 if (leastUsed != null) {
2886 closeTab(leastUsed);
2887 }
2888 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002889 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002890 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002891 mTabControl.addPreloadedTab(t);
2892 addTab(t);
2893 setActiveTab(t);
2894 return t;
2895 }
2896
Michael Kolb7bcafde2011-05-09 13:55:59 -07002897 // open a non inconito tab with the given url data
2898 // and set as active tab
2899 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002900 Tab tab = showPreloadedTab(urlData);
2901 if (tab == null) {
2902 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002903 if ((tab != null) && !urlData.isEmpty()) {
2904 loadUrlDataIn(tab, urlData);
2905 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002906 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002907 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002908 }
2909
Michael Kolb843510f2010-12-09 10:51:49 -08002910 @Override
2911 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002912 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002913 }
2914
Michael Kolb8233fac2010-10-26 16:08:53 -07002915 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002916 public Tab openIncognitoTab() {
2917 return openTab(INCOGNITO_URI, true, true, false);
2918 }
2919
2920 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002921 public Tab openTab(String url, boolean incognito, boolean setActive,
2922 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002923 return openTab(url, incognito, setActive, useCurrent, null);
2924 }
2925
2926 @Override
2927 public Tab openTab(String url, Tab parent, boolean setActive,
2928 boolean useCurrent) {
2929 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2930 setActive, useCurrent, parent);
2931 }
2932
2933 public Tab openTab(String url, boolean incognito, boolean setActive,
2934 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002935 Tab tab = createNewTab(incognito, setActive, useCurrent);
2936 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002937 if (parent instanceof SnapshotTab) {
2938 addTab(tab);
2939 if (setActive)
2940 setActiveTab(tab);
2941 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002942 parent.addChildTab(tab);
2943 }
Michael Kolb519d2282011-05-09 17:03:19 -07002944 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002945 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002946 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002947 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002948 return tab;
2949 }
2950
2951 // this method will attempt to create a new tab
2952 // incognito: private browsing tab
2953 // setActive: ste tab as current tab
2954 // useCurrent: if no new tab can be created, return current tab
2955 private Tab createNewTab(boolean incognito, boolean setActive,
2956 boolean useCurrent) {
2957 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002958 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2959 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002960 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002961 if (mTabControl.canCreateNewTab()) {
2962 tab = mTabControl.createNewTab(incognito, !setActive);
2963 addTab(tab);
2964 if (setActive) {
2965 setActiveTab(tab);
2966 } else {
2967 tab.pause();
2968 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002969 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002970 if (useCurrent) {
2971 tab = mTabControl.getCurrentTab();
2972 reuseTab(tab, null);
2973 } else {
2974 mUi.showMaxTabsWarning();
2975 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002976 }
2977 }
2978 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002979 }
2980
John Reck2bc80422011-06-30 15:11:49 -07002981 @Override
2982 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2983 SnapshotTab tab = null;
2984 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07002985 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07002986 addTab(tab);
2987 if (setActive) {
2988 setActiveTab(tab);
2989 }
2990 } else {
2991 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002992 }
2993 return tab;
2994 }
2995
Michael Kolb8233fac2010-10-26 16:08:53 -07002996 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002997 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 * @return boolean True if we successfully switched to a different tab. If
2999 * the indexth tab is null, or if that tab is the same as
3000 * the current one, return false.
3001 */
3002 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003003 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003004 Tab currentTab = mTabControl.getCurrentTab();
3005 if (tab == null || tab == currentTab) {
3006 return false;
3007 }
3008 setActiveTab(tab);
3009 return true;
3010 }
3011
3012 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003013 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003014 closeCurrentTab(false);
3015 }
3016
3017 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003018 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003019 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003020 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003021 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003022 return;
3023 }
Michael Kolbc831b632011-05-11 09:30:34 -07003024 final Tab current = mTabControl.getCurrentTab();
3025 final int pos = mTabControl.getCurrentPosition();
3026 Tab newTab = current.getParent();
3027 if (newTab == null) {
3028 newTab = mTabControl.getTab(pos + 1);
3029 if (newTab == null) {
3030 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003031 }
3032 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003033 if (andQuit) {
3034 mTabControl.setCurrentTab(newTab);
3035 closeTab(current);
3036 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003037 // Close window
3038 closeTab(current);
3039 }
3040 }
3041
3042 /**
3043 * Close the tab, remove its associated title bar, and adjust mTabControl's
3044 * current tab to a valid value.
3045 */
3046 @Override
3047 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003048 if (tab == mTabControl.getCurrentTab()) {
3049 closeCurrentTab();
3050 } else {
3051 removeTab(tab);
3052 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003053 }
3054
Afzal Najamd4e33312012-04-26 01:54:01 -04003055 /**
3056 * Close all tabs except the current one
3057 */
3058 @Override
3059 public void closeOtherTabs() {
3060 int inactiveTabs = mTabControl.getTabCount() - 1;
3061 for (int i = inactiveTabs; i >= 0; i--) {
3062 Tab tab = mTabControl.getTab(i);
3063 if (tab != mTabControl.getCurrentTab()) {
3064 removeTab(tab);
3065 }
3066 }
3067 }
3068
Michael Kolb8233fac2010-10-26 16:08:53 -07003069 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003070 protected void loadUrlFromContext(String url) {
3071 Tab tab = getCurrentTab();
3072 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003073 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003074 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003076 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003077 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003078 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003079 }
3080 }
3081 }
3082
3083 /**
3084 * Load the URL into the given WebView and update the title bar
3085 * to reflect the new load. Call this instead of WebView.loadUrl
3086 * directly.
3087 * @param view The WebView used to load url.
3088 * @param url The URL to load.
3089 */
John Reck71e51422011-07-01 16:49:28 -07003090 @Override
3091 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003092 loadUrl(tab, url, null);
3093 }
3094
3095 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3096 if (tab != null) {
3097 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003098 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003099 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003100 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003101 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003102 }
3103
3104 /**
3105 * Load UrlData into a Tab and update the title bar to reflect the new
3106 * load. Call this instead of UrlData.loadIn directly.
3107 * @param t The Tab used to load.
3108 * @param data The UrlData being loaded.
3109 */
3110 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003111 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003112 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003113 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003114 } else {
John Reck38b39652012-06-05 09:22:59 -07003115 if (t != null && data.mDisableUrlOverride) {
3116 t.disableUrlOverridingForLoad();
3117 }
John Reck26b18322011-06-21 13:08:58 -07003118 loadUrl(t, data.mUrl, data.mHeaders);
3119 }
3120 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003121 }
3122
John Reck30c714c2010-12-16 17:30:34 -08003123 @Override
3124 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003125 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003126 //In case of tab can go back (aka tab has navigation entry) do nothing
3127 //else just load homepage in current tab.
3128 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003129 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003130 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003131 }
3132
3133 void goBackOnePageOrQuit() {
3134 Tab current = mTabControl.getCurrentTab();
3135 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003136 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3137 showExitDialog(mActivity);
3138 } else {
3139 /*
3140 * Instead of finishing the activity, simply push this to the back
3141 * of the stack and let ActivityManager to choose the foreground
3142 * activity. As BrowserActivity is singleTask, it will be always the
3143 * root of the task. So we can use either true or false for
3144 * moveTaskToBack().
3145 */
3146 mActivity.moveTaskToBack(true);
3147 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003148 return;
3149 }
John Reckef654f12011-07-12 16:42:08 -07003150 if (current.canGoBack()) {
3151 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003152 } else {
3153 // Check to see if we are closing a window that was created by
3154 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003155 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003156 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003157 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003158 // Now we close the other tab
3159 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003160 } else if (BrowserConfig.getInstance(getContext())
3161 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3162 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003163 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003164 /*
3165 * Instead of finishing the activity, simply push this to the back
3166 * of the stack and let ActivityManager to choose the foreground
3167 * activity. As BrowserActivity is singleTask, it will be always the
3168 * root of the task. So we can use either true or false for
3169 * moveTaskToBack().
3170 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003171 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003172 }
3173 }
3174 }
3175
3176 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003177 * helper method for key handler
3178 * returns the current tab if it can't advance
3179 */
Michael Kolbc831b632011-05-11 09:30:34 -07003180 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003181 int pos = mTabControl.getCurrentPosition() + 1;
3182 if (pos >= mTabControl.getTabCount()) {
3183 pos = 0;
3184 }
3185 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003186 }
3187
3188 /**
3189 * 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 getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003193 int pos = mTabControl.getCurrentPosition() - 1;
3194 if ( pos < 0) {
3195 pos = mTabControl.getTabCount() - 1;
3196 }
3197 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003198 }
3199
John Reckbcef87f2012-02-03 14:58:34 -08003200 boolean isMenuOrCtrlKey(int keyCode) {
3201 return (KeyEvent.KEYCODE_MENU == keyCode)
3202 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3203 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3204 }
3205
Michael Kolb0035fad2011-03-14 13:25:28 -07003206 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003207 * handle key events in browser
3208 *
3209 * @param keyCode
3210 * @param event
3211 * @return true if handled, false to pass to super
3212 */
John Reck9c35b9c2012-05-30 10:08:50 -07003213 @Override
3214 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003215 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3216 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003217 if (!mUi.isComboViewShowing()) {
3218 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3219 true, false);
3220 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003221 return true;
3222 }
3223
Cary Clark160bbb92011-01-10 11:17:07 -05003224 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003225 // Even if MENU is already held down, we need to call to super to open
3226 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003227 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003228 mMenuIsDown = true;
3229 return false;
3230 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003231
Cary Clark8ff8c662010-12-29 15:03:05 -05003232 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003233 Tab tab = getCurrentTab();
3234 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003235
Cary Clark160bbb92011-01-10 11:17:07 -05003236 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3237 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003238
Michael Kolb8233fac2010-10-26 16:08:53 -07003239 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003240 case KeyEvent.KEYCODE_TAB:
3241 if (event.isCtrlPressed()) {
3242 if (event.isShiftPressed()) {
3243 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003244 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003245 } else {
3246 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003247 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003248 }
3249 return true;
3250 }
3251 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003252 case KeyEvent.KEYCODE_SPACE:
3253 // WebView/WebTextView handle the keys in the KeyDown. As
3254 // the Activity's shortcut keys are only handled when WebView
3255 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003256 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003257 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003258 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003259 pageDown();
3260 }
3261 return true;
3262 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003263 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003264 event.startTracking();
3265 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003266 case KeyEvent.KEYCODE_FORWARD:
3267 if (!noModifiers) break;
3268 tab.goForward();
3269 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003270 case KeyEvent.KEYCODE_DPAD_LEFT:
3271 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003272 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003273 return true;
3274 }
3275 break;
3276 case KeyEvent.KEYCODE_DPAD_RIGHT:
3277 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003278 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003279 return true;
3280 }
3281 break;
3282 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003283 if (ctrl) {
3284 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003285 return true;
3286 }
3287 break;
Michael Kolba4183062011-01-16 10:43:21 -08003288// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003289 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003290 if (ctrl ) {
3291 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003292 return true;
3293 }
3294 break;
Michael Kolba4183062011-01-16 10:43:21 -08003295// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003296// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003297// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003298// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003299// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003300// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003301// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003302// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003303// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003304// case KeyEvent.KEYCODE_M: // unused
3305// case KeyEvent.KEYCODE_N: // in Chrome: new window
3306// case KeyEvent.KEYCODE_O: // in Chrome: open file
3307// case KeyEvent.KEYCODE_P: // in Chrome: print page
3308// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003309// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003310// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3311 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003312 // we can't use the ctrl/shift flags, they check for
3313 // exclusive use of a modifier
3314 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003315 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003316 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003317 } else {
3318 openTabToHomePage();
3319 }
3320 return true;
3321 }
3322 break;
3323// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3324// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003325// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003326// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3327// case KeyEvent.KEYCODE_Y: // unused
3328// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003329 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003330 // it is a regular key and webview is not null
3331 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003332 }
3333
John Reck9c35b9c2012-05-30 10:08:50 -07003334 @Override
3335 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003336 switch(keyCode) {
3337 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003338 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003339 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003340 return true;
3341 }
3342 break;
3343 }
3344 return false;
3345 }
3346
John Reck9c35b9c2012-05-30 10:08:50 -07003347 @Override
3348 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003349 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003350 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003351 if (KeyEvent.KEYCODE_MENU == keyCode
3352 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003353 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003354 }
3355 }
Cary Clark160bbb92011-01-10 11:17:07 -05003356 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003357 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003358 case KeyEvent.KEYCODE_BACK:
3359 if (event.isTracking() && !event.isCanceled()) {
3360 onBackKey();
3361 return true;
3362 }
3363 break;
3364 }
3365 return false;
3366 }
3367
3368 public boolean isMenuDown() {
3369 return mMenuIsDown;
3370 }
3371
John Reck9c35b9c2012-05-30 10:08:50 -07003372 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003373 public void setupAutoFill(Message message) {
3374 // Open the settings activity at the AutoFill profile fragment so that
3375 // the user can create a new profile. When they return, we will dispatch
3376 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003377 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003378 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3379 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003380 }
John Reckb3417f02011-01-14 11:01:05 -08003381
John Reck9c35b9c2012-05-30 10:08:50 -07003382 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003383 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003384 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003385 return true;
3386 }
3387
John Reck1cf4b792011-07-26 10:22:22 -07003388 @Override
3389 public boolean shouldCaptureThumbnails() {
3390 return mUi.shouldCaptureThumbnails();
3391 }
3392
Michael Kolbc3af0672011-08-09 10:24:41 -07003393 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003394 public boolean supportsVoice() {
3395 PackageManager pm = mActivity.getPackageManager();
3396 List activities = pm.queryIntentActivities(new Intent(
3397 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3398 return activities.size() != 0;
3399 }
3400
3401 @Override
3402 public void startVoiceRecognizer() {
3403 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003404 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003405 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3406 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3407 mActivity.startActivityForResult(voice, VOICE_RESULT);
3408 }
3409
Pankaj Garg7b279f62014-08-12 14:47:18 -07003410 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003411 if (mLevel == level)
3412 return;
3413 mLevel = level;
3414 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003415 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3416 lp.dimAmount = level;
3417 mActivity.getWindow().setAttributes(lp);
3418 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3419 } else {
3420 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3421 }
3422 }
3423
Michael Kolb0b129122012-06-04 16:31:58 -07003424 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003425 public void setBlockEvents(boolean block) {
3426 mBlockEvents = block;
3427 }
3428
John Reck9c35b9c2012-05-30 10:08:50 -07003429 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003430 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003431 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003432 }
3433
John Reck9c35b9c2012-05-30 10:08:50 -07003434 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003435 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003436 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003437 }
3438
John Reck9c35b9c2012-05-30 10:08:50 -07003439 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003440 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003441 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003442 }
3443
John Reck9c35b9c2012-05-30 10:08:50 -07003444 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003445 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003446 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003447 }
3448
John Reck9c35b9c2012-05-30 10:08:50 -07003449 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003450 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003451 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003452 }
3453
Pankaj Garg49b79252014-11-07 17:33:41 -08003454 @Override
3455 public boolean shouldShowAppMenu() {
3456 return true;
3457 }
3458
3459 @Override
3460 public int getMenuThemeResourceId() {
3461 return R.style.OverflowMenuTheme;
3462 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003463}