blob: c546f485fd743ae9ac969ada12ec6fb6a41eca20 [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";
Vivek Sekhared791da2015-02-22 12:39:05 -0800136 private static final String INCOGNITO_URI = "chrome://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 }
John Reck30c714c2010-12-16 17:30:34 -08001031 mUi.onProgressChanged(tab);
1032 }
1033
1034 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001035 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001036 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 }
1038
1039 @Override
1040 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001041 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001042 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001043 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1045 return;
1046 }
1047 // Update the title in the history database if not in private browsing mode
1048 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001049 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 }
1051 }
1052
1053 @Override
1054 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001055 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1057 }
1058
1059 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001060 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001061 // if tab is snapshot tab we want to prevent navigation from occuring
1062 // since snapshot tab opens a new tab with the url
1063 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 }
1065
1066 @Override
1067 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1068 if (mMenuIsDown) {
1069 // only check shortcut key when MENU is held
1070 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1071 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001072 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001073 int keyCode = event.getKeyCode();
1074 // We need to send almost every key to WebKit. However:
1075 // 1. We don't want to block the device on the renderer for
1076 // some keys like menu, home, call.
1077 // 2. There are no WebKit equivalents for some of these keys
1078 // (see app/keyboard_codes_win.h)
1079 // Note that these are not the same set as KeyEvent.isSystemKey:
1080 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1081 if (keyCode == KeyEvent.KEYCODE_MENU ||
1082 keyCode == KeyEvent.KEYCODE_HOME ||
1083 keyCode == KeyEvent.KEYCODE_BACK ||
1084 keyCode == KeyEvent.KEYCODE_CALL ||
1085 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1086 keyCode == KeyEvent.KEYCODE_POWER ||
1087 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1088 keyCode == KeyEvent.KEYCODE_CAMERA ||
1089 keyCode == KeyEvent.KEYCODE_FOCUS ||
1090 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1091 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1092 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1093 return true;
1094 }
1095
1096 // We also have to intercept some shortcuts before we send them to the ContentView.
1097 if (event.isCtrlPressed() && (
1098 keyCode == KeyEvent.KEYCODE_TAB ||
1099 keyCode == KeyEvent.KEYCODE_W ||
1100 keyCode == KeyEvent.KEYCODE_F4)) {
1101 return true;
1102 }
1103
1104 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 }
1106
Tarun Nainanif5563b62015-01-21 18:52:59 -08001107 private void handleMediaKeyEvent(KeyEvent event) {
1108
1109 int keyCode = event.getKeyCode();
1110 // send media key events to audio manager
1111 if (Build.VERSION.SDK_INT >= 19) {
1112
1113 switch (keyCode) {
1114 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1115 case KeyEvent.KEYCODE_MEDIA_STOP:
1116 case KeyEvent.KEYCODE_MEDIA_NEXT:
1117 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1118 case KeyEvent.KEYCODE_MEDIA_REWIND:
1119 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1120 case KeyEvent.KEYCODE_MUTE:
1121 case KeyEvent.KEYCODE_MEDIA_PLAY:
1122 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1123 case KeyEvent.META_SHIFT_RIGHT_ON:
1124 case KeyEvent.KEYCODE_MEDIA_EJECT:
1125 case KeyEvent.KEYCODE_MEDIA_RECORD:
1126 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1127
1128 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1129 .getSystemService(Context.AUDIO_SERVICE);
1130 audioManager.dispatchMediaKeyEvent(event);
1131 }
1132 }
1133 }
1134
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 @Override
John Reck997b1b72012-04-19 18:08:25 -07001136 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001138 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001140 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 } else {
John Reck997b1b72012-04-19 18:08:25 -07001142 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001143 }
1144 }
John Reck997b1b72012-04-19 18:08:25 -07001145 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 }
1147
1148 @Override
John Reck324d4402011-01-11 16:56:42 -08001149 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001150 // Don't save anything in private browsing mode or when disabling history
1151 // for regular tabs is enabled
1152 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1153 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1154 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001155
John Reck49a603c2011-03-03 09:33:05 -08001156 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001157
John Reck324d4402011-01-11 16:56:42 -08001158 if (TextUtils.isEmpty(url)
1159 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 return;
1161 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001162
John Reckf57c0292011-07-21 18:15:39 -07001163 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001164 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 }
1166
1167 @Override
1168 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1169 AsyncTask<Void, Void, String[]> task =
1170 new AsyncTask<Void, Void, String[]>() {
1171 @Override
1172 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001173 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 }
1175 @Override
1176 public void onPostExecute(String[] result) {
1177 callback.onReceiveValue(result);
1178 }
1179 };
1180 task.execute();
1181 }
1182
1183 @Override
1184 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1185 final HttpAuthHandler handler, final String host,
1186 final String realm) {
1187 String username = null;
1188 String password = null;
1189
1190 boolean reuseHttpAuthUsernamePassword
1191 = handler.useHttpAuthUsernamePassword();
1192
1193 if (reuseHttpAuthUsernamePassword && view != null) {
1194 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1195 if (credentials != null && credentials.length == 2) {
1196 username = credentials[0];
1197 password = credentials[1];
1198 }
1199 }
1200
1201 if (username != null && password != null) {
1202 handler.proceed(username, password);
1203 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001204 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001205 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1206 } else {
1207 handler.cancel();
1208 }
1209 }
1210 }
1211
1212 @Override
1213 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001214 String contentDisposition, String mimetype, String referer,
1215 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001216 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001217 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001218 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001219 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001220 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001221 // This Tab was opened for the sole purpose of downloading a
1222 // file. Remove it.
1223 if (tab == mTabControl.getCurrentTab()) {
1224 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001225 if (tab.getDerivedFromIntent())
1226 closeTab(tab);
1227 else
1228 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001229 } else {
1230 // In this case, it is not.
1231 closeTab(tab);
1232 }
1233 }
1234 }
1235
1236 @Override
1237 public Bitmap getDefaultVideoPoster() {
1238 return mUi.getDefaultVideoPoster();
1239 }
1240
1241 @Override
1242 public View getVideoLoadingProgressView() {
1243 return mUi.getVideoLoadingProgressView();
1244 }
1245
1246 @Override
1247 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1248 SslError error) {
1249 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1250 }
1251
1252 // helper method
1253
1254 /*
1255 * Update the favorites icon if the private browsing isn't enabled and the
1256 * icon is valid.
1257 */
1258 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1259 final String url, Bitmap favicon) {
1260 if (favicon == null) {
1261 return;
1262 }
1263 if (!tab.isPrivateBrowsingEnabled()) {
1264 Bookmarks.updateFavicon(mActivity
1265 .getContentResolver(), originalUrl, url, favicon);
1266 }
1267 }
1268
Leon Scroggins4cd97792010-12-03 15:31:56 -05001269 @Override
1270 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001271 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001272 mUi.bookmarkedStatusHasChanged(tab);
1273 }
1274
Michael Kolb8233fac2010-10-26 16:08:53 -07001275 // end WebViewController
1276
1277 protected void pageUp() {
1278 getCurrentTopWebView().pageUp(false);
1279 }
1280
1281 protected void pageDown() {
1282 getCurrentTopWebView().pageDown(false);
1283 }
1284
1285 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001286 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001287 public void editUrl() {
1288 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001289 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001290 }
1291
John Reck9c35b9c2012-05-30 10:08:50 -07001292 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001293 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001294 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 if (tab.inForeground()) {
1296 if (mUi.isCustomViewShowing()) {
1297 callback.onCustomViewHidden();
1298 return;
1299 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001300 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001301 // Save the menu state and set it to empty while the custom
1302 // view is showing.
1303 mOldMenuState = mMenuState;
1304 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001305 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001306 }
1307 }
1308
1309 @Override
1310 public void hideCustomView() {
1311 if (mUi.isCustomViewShowing()) {
1312 mUi.onHideCustomView();
1313 // Reset the old menu state.
1314 mMenuState = mOldMenuState;
1315 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001316 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001317 }
1318 }
1319
John Reck9c35b9c2012-05-30 10:08:50 -07001320 @Override
1321 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001322 Intent intent) {
1323 if (getCurrentTopWebView() == null) return;
1324 switch (requestCode) {
1325 case PREFERENCES_PAGE:
1326 if (resultCode == Activity.RESULT_OK && intent != null) {
1327 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001328 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001329 mTabControl.removeParentChildRelationShips();
1330 }
1331 }
1332 break;
1333 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001334 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001335 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001336 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001338 case AUTOFILL_SETUP:
1339 // Determine whether a profile was actually set up or not
1340 // and if so, send the message back to the WebTextView to
1341 // fill the form with the new profile.
1342 if (getSettings().getAutoFillProfile() != null) {
1343 mAutoFillSetupMessage.sendToTarget();
1344 mAutoFillSetupMessage = null;
1345 }
1346 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001347 case COMBO_VIEW:
1348 if (intent == null || resultCode != Activity.RESULT_OK) {
1349 break;
1350 }
John Reck3ba45532011-08-11 16:26:53 -07001351 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001352 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1353 Tab t = getCurrentTab();
1354 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001355 mUpdateMyNavThumbnail = true;
1356 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001357 loadUrl(t, uri.toString());
1358 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1359 String[] urls = intent.getStringArrayExtra(
1360 ComboViewActivity.EXTRA_OPEN_ALL);
1361 Tab parent = getCurrentTab();
1362 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001363 if (url != null) {
1364 parent = openTab(url, parent,
1365 !mSettings.openInBackground(), true);
1366 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001367 }
1368 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1369 long id = intent.getLongExtra(
1370 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1371 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001372 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001373 }
1374 }
1375 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001376 case VOICE_RESULT:
1377 if (resultCode == Activity.RESULT_OK && intent != null) {
1378 ArrayList<String> results = intent.getStringArrayListExtra(
1379 RecognizerIntent.EXTRA_RESULTS);
1380 if (results.size() >= 1) {
1381 mVoiceResult = results.get(0);
1382 }
1383 }
1384 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001385 case MY_NAVIGATION:
1386 if (intent == null || resultCode != Activity.RESULT_OK) {
1387 break;
1388 }
1389
1390 if (intent.getBooleanExtra("need_refresh", false) &&
1391 getCurrentTopWebView() != null) {
1392 getCurrentTopWebView().reload();
1393 }
1394 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001395 default:
1396 break;
1397 }
1398 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001399 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 }
1401
1402 /**
1403 * Open the Go page.
1404 * @param startWithHistory If true, open starting on the history tab.
1405 * Otherwise, start with the bookmarks tab.
1406 */
1407 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001408 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 if (mTabControl.getCurrentWebView() == null) {
1410 return;
1411 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001412 // clear action mode
1413 if (isInCustomActionMode()) {
1414 endActionMode();
1415 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 Bundle extras = new Bundle();
1417 // Disable opening in a new window if we have maxed out the windows
1418 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1419 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001420 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001421 }
1422
1423 // combo view callbacks
1424
Michael Kolb8233fac2010-10-26 16:08:53 -07001425 // key handling
1426 protected void onBackKey() {
1427 if (!mUi.onBackKey()) {
1428 WebView subwindow = mTabControl.getCurrentSubWindow();
1429 if (subwindow != null) {
1430 if (subwindow.canGoBack()) {
1431 subwindow.goBack();
1432 } else {
1433 dismissSubWindow(mTabControl.getCurrentTab());
1434 }
1435 } else {
1436 goBackOnePageOrQuit();
1437 }
1438 }
1439 }
1440
Michael Kolb4bd767d2011-05-27 11:33:55 -07001441 protected boolean onMenuKey() {
1442 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001443 }
1444
Michael Kolb8233fac2010-10-26 16:08:53 -07001445 // menu handling and state
1446 // TODO: maybe put into separate handler
1447
John Reck9c35b9c2012-05-30 10:08:50 -07001448 @Override
1449 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001450 if (mMenuState == EMPTY_MENU) {
1451 return false;
1452 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 MenuInflater inflater = mActivity.getMenuInflater();
1454 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001455 return true;
1456 }
1457
John Reck9c35b9c2012-05-30 10:08:50 -07001458 @Override
1459 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001460 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001461 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 return;
1463 }
1464 if (!(v instanceof WebView)) {
1465 return;
1466 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001467 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001468 WebView.HitTestResult result;
1469
1470 /* Determine whether the ContextMenu got triggered because
1471 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1472 * received. The mResult acts as a flag to identify, how it got trigerred
1473 */
1474 if (mResult == null){
1475 result = webview.getHitTestResult();
1476 } else {
1477 result = mResult;
1478 }
1479
Michael Kolb8233fac2010-10-26 16:08:53 -07001480 if (result == null) {
1481 return;
1482 }
1483
1484 int type = result.getType();
1485 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001486
1487 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1488 unknownTypeMap.put("webview", webview);
1489 final Message msg = mHandler.obtainMessage(
1490 UNKNOWN_TYPE_MSG, unknownTypeMap);
1491 /* As defined in android developers guide
1492 * when UNKNOWN_TYPE is received as a result of HitTest
1493 * you need to determing the type by invoking requestFocusNodeHref
1494 */
1495 webview.requestFocusNodeHref(msg);
1496
Michael Kolb8233fac2010-10-26 16:08:53 -07001497 Log.w(LOGTAG,
1498 "We should not show context menu when nothing is touched");
1499 return;
1500 }
1501 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1502 // let TextView handles context menu
1503 return;
1504 }
1505
1506 // Note, http://b/issue?id=1106666 is requesting that
1507 // an inflated menu can be used again. This is not available
1508 // yet, so inflate each time (yuk!)
1509 MenuInflater inflater = mActivity.getMenuInflater();
1510 inflater.inflate(R.menu.browsercontext, menu);
1511
1512 // Show the correct menu group
1513 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001514 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001515 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001516 menu.setGroupVisible(R.id.PHONE_MENU,
1517 type == WebView.HitTestResult.PHONE_TYPE);
1518 menu.setGroupVisible(R.id.EMAIL_MENU,
1519 type == WebView.HitTestResult.EMAIL_TYPE);
1520 menu.setGroupVisible(R.id.GEO_MENU,
1521 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001522 String itemUrl = null;
1523 String url = webview.getOriginalUrl();
1524 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1525 itemUrl = Uri.decode(navigationUrl);
1526 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1527 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1528 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1529 } else {
1530 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1531 }
1532 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1533 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1534 } else {
1535 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1536
1537 menu.setGroupVisible(R.id.IMAGE_MENU,
1538 type == WebView.HitTestResult.IMAGE_TYPE
1539 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1540 menu.setGroupVisible(R.id.ANCHOR_MENU,
1541 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1542 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001543
1544 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1545 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1546 UrlUtils.isDownloadableScheme(extra));
1547 }
1548 else {
1549 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1550 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001551 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001552 // Setup custom handling depending on the type
1553 switch (type) {
1554 case WebView.HitTestResult.PHONE_TYPE:
1555 menu.setHeaderTitle(Uri.decode(extra));
1556 menu.findItem(R.id.dial_context_menu_id).setIntent(
1557 new Intent(Intent.ACTION_VIEW, Uri
1558 .parse(WebView.SCHEME_TEL + extra)));
1559 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1560 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1561 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1562 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1563 addIntent);
1564 menu.findItem(R.id.copy_phone_context_menu_id)
1565 .setOnMenuItemClickListener(
1566 new Copy(extra));
1567 break;
1568
1569 case WebView.HitTestResult.EMAIL_TYPE:
1570 menu.setHeaderTitle(extra);
1571 menu.findItem(R.id.email_context_menu_id).setIntent(
1572 new Intent(Intent.ACTION_VIEW, Uri
1573 .parse(WebView.SCHEME_MAILTO + extra)));
1574 menu.findItem(R.id.copy_mail_context_menu_id)
1575 .setOnMenuItemClickListener(
1576 new Copy(extra));
1577 break;
1578
1579 case WebView.HitTestResult.GEO_TYPE:
1580 menu.setHeaderTitle(extra);
1581 menu.findItem(R.id.map_context_menu_id).setIntent(
1582 new Intent(Intent.ACTION_VIEW, Uri
1583 .parse(WebView.SCHEME_GEO
1584 + URLEncoder.encode(extra))));
1585 menu.findItem(R.id.copy_geo_context_menu_id)
1586 .setOnMenuItemClickListener(
1587 new Copy(extra));
1588 break;
1589
1590 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1591 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001592 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001593 // decide whether to show the open link in new tab option
1594 boolean showNewTab = mTabControl.canCreateNewTab();
1595 MenuItem newTabItem
1596 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001597 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001598 ? R.string.contextmenu_openlink_newwindow_background
1599 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 newTabItem.setVisible(showNewTab);
1601 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001602 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1603 newTabItem.setOnMenuItemClickListener(
1604 new MenuItem.OnMenuItemClickListener() {
1605 @Override
1606 public boolean onMenuItemClick(MenuItem item) {
1607 final HashMap<String, WebView> hrefMap =
1608 new HashMap<String, WebView>();
1609 hrefMap.put("webview", webview);
1610 final Message msg = mHandler.obtainMessage(
1611 FOCUS_NODE_HREF,
1612 R.id.open_newtab_context_menu_id,
1613 0, hrefMap);
1614 webview.requestFocusNodeHref(msg);
1615 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001617 });
1618 } else {
1619 newTabItem.setOnMenuItemClickListener(
1620 new MenuItem.OnMenuItemClickListener() {
1621 @Override
1622 public boolean onMenuItemClick(MenuItem item) {
1623 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001624 openTab(extra, parent,
1625 !mSettings.openInBackground(),
1626 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001627 return true;
1628 }
1629 });
1630 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001631 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001632 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1633 menu.setHeaderTitle(navigationUrl);
1634 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1635
1636 if (itemUrl != null) {
1637 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1638 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1639 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1640 @Override
1641 public boolean onMenuItemClick(MenuItem item) {
1642 final Intent intent = new Intent(Controller.this
1643 .getContext(),
1644 AddMyNavigationPage.class);
1645 Bundle bundle = new Bundle();
1646 String url = Uri.decode(navigationUrl);
1647 bundle.putBoolean("isAdding", false);
1648 bundle.putString("url", url);
1649 bundle.putString("name", getNameFromUrl(url));
1650 intent.putExtra("websites", bundle);
1651 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1652 return false;
1653 }
1654 });
1655 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1656 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1657 @Override
1658 public boolean onMenuItemClick(MenuItem item) {
1659 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1660 return false;
1661 }
1662 });
1663 }
1664 } else {
1665 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1666 }
1667 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001668 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1669 break;
1670 }
1671 // otherwise fall through to handle image part
1672 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001673 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1674 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001675 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1676 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001677 shareItem.setOnMenuItemClickListener(
1678 new MenuItem.OnMenuItemClickListener() {
1679 @Override
1680 public boolean onMenuItemClick(MenuItem item) {
1681 sharePage(mActivity, null, extra, null,
1682 null);
1683 return true;
1684 }
1685 }
1686 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001687 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001688 menu.findItem(R.id.view_image_context_menu_id)
1689 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1690 @Override
1691 public boolean onMenuItemClick(MenuItem item) {
1692 openTab(extra, mTabControl.getCurrentTab(), true, true);
1693 return false;
1694 }
1695 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001696 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1697 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1698 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001699 menu.findItem(R.id.set_wallpaper_context_menu_id).
1700 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1701 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001702 break;
1703
1704 default:
1705 Log.w(LOGTAG, "We should not get here.");
1706 break;
1707 }
1708 //update the ui
1709 mUi.onContextMenuCreated(menu);
1710 }
1711
kaiyiz6e5b3e02013-08-19 20:02:01 +08001712 public void startAddMyNavigation(String url) {
1713 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1714 Bundle bundle = new Bundle();
1715 bundle.putBoolean("isAdding", true);
1716 bundle.putString("url", url);
1717 bundle.putString("name", getNameFromUrl(url));
1718 intent.putExtra("websites", bundle);
1719 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1720 }
1721
1722 private void showMyNavigationDeleteDialog(final String itemUrl) {
1723 new AlertDialog.Builder(this.getContext())
1724 .setTitle(R.string.my_navigation_delete_label)
1725 .setIcon(android.R.drawable.ic_dialog_alert)
1726 .setMessage(R.string.my_navigation_delete_msg)
1727 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1728 @Override
1729 public void onClick(DialogInterface dialog, int whichButton) {
1730 deleteMyNavigationItem(itemUrl);
1731 }
1732 })
1733 .setNegativeButton(R.string.cancel, null)
1734 .show();
1735 }
1736
1737 private void deleteMyNavigationItem(final String itemUrl) {
1738 ContentResolver cr = this.getContext().getContentResolver();
1739 Cursor cursor = null;
1740
1741 try {
1742 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1743 new String[] {
1744 MyNavigationUtil.ID
1745 }, "url = ?", new String[] {
1746 itemUrl
1747 }, null);
1748 if (null != cursor && cursor.moveToFirst()) {
1749 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1750 cursor.getLong(0));
1751
1752 ContentValues values = new ContentValues();
1753 values.put(MyNavigationUtil.TITLE, "");
1754 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1755 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1756 ByteArrayOutputStream os = new ByteArrayOutputStream();
1757 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1758 R.raw.my_navigation_add);
1759 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1760 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1761 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1762 cr.update(uri, values, null, null);
1763 } else {
1764 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1765 }
1766 } catch (IllegalStateException e) {
1767 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1768 } finally {
1769 if (null != cursor) {
1770 cursor.close();
1771 }
1772 }
1773
1774 if (getCurrentTopWebView() != null) {
1775 getCurrentTopWebView().reload();
1776 }
1777 }
1778
1779 private String getNameFromUrl(String itemUrl) {
1780 ContentResolver cr = this.getContext().getContentResolver();
1781 Cursor cursor = null;
1782 String name = null;
1783
1784 try {
1785 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1786 new String[] {
1787 MyNavigationUtil.TITLE
1788 }, "url = ?", new String[] {
1789 itemUrl
1790 }, null);
1791 if (null != cursor && cursor.moveToFirst()) {
1792 name = cursor.getString(0);
1793 } else {
1794 Log.e(LOGTAG, "this item does not exist!");
1795 }
1796 } catch (IllegalStateException e) {
1797 Log.e(LOGTAG, "getNameFromUrl", e);
1798 } finally {
1799 if (null != cursor) {
1800 cursor.close();
1801 }
1802 }
1803 return name;
1804 }
1805
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001806 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001807 final ContentResolver cr = mActivity.getContentResolver();
1808 new AsyncTask<Void, Void, Void>() {
1809 @Override
1810 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001811 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1812 if(!isMyNavigationUrl)
1813 return null;
1814
kaiyiz6e5b3e02013-08-19 20:02:01 +08001815 ContentResolver cr = mActivity.getContentResolver();
1816 Cursor cursor = null;
1817 try {
1818 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1819 new String[] {
1820 MyNavigationUtil.ID
1821 }, "url = ?", new String[] {
1822 itemUrl
1823 }, null);
1824 if (null != cursor && cursor.moveToFirst()) {
1825 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001826 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001827
1828 ContentValues values = new ContentValues();
1829 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1830 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1831 cursor.getLong(0));
1832 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1833 cr.update(uri, values, null, null);
1834 os.close();
1835 }
1836 } catch (IllegalStateException e) {
1837 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1838 } catch (IOException e) {
1839 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1840 } finally {
1841 if (null != cursor) {
1842 cursor.close();
1843 }
1844 }
1845 return null;
1846 }
1847 }.execute();
1848 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001849 /**
1850 * As the menu can be open when loading state changes
1851 * we must manually update the state of the stop/reload menu
1852 * item
1853 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001854 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001855 if (menu == null) {
1856 return;
1857 }
1858 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001859 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001860 menu.findItem(R.id.stop_menu_id):
1861 menu.findItem(R.id.reload_menu_id);
1862 if (src != null) {
1863 dest.setIcon(src.getIcon());
1864 dest.setTitle(src.getTitle());
1865 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001866 mActivity.invalidateOptionsMenu();
1867 }
1868
1869 public void invalidateOptionsMenu() {
1870 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001871 }
1872
John Reck9c35b9c2012-05-30 10:08:50 -07001873 @Override
1874 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001875 // Software menu key (toolbar key)
1876 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1877 return true;
1878 }
1879
1880 @Override
1881 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001882 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001883 // hold on to the menu reference here; it is used by the page callbacks
1884 // to update the menu based on loading state
1885 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001886 // Note: setVisible will decide whether an item is visible; while
1887 // setEnabled() will decide whether an item is enabled, which also means
1888 // whether the matching shortcut key will function.
1889 switch (mMenuState) {
1890 case EMPTY_MENU:
1891 if (mCurrentMenuState != mMenuState) {
1892 menu.setGroupVisible(R.id.MAIN_MENU, false);
1893 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1894 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1895 }
1896 break;
1897 default:
1898 if (mCurrentMenuState != mMenuState) {
1899 menu.setGroupVisible(R.id.MAIN_MENU, true);
1900 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1901 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1902 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001903 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001904 break;
1905 }
1906 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001907 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001908
1909 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001910 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1911 EditBookmarksRestriction.getInstance()
1912 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001913 }
1914
1915 private void setMenuItemVisibility(Menu menu, int id,
1916 boolean visibility) {
1917 MenuItem item = menu.findItem(id);
1918 if (item != null) {
1919 item.setVisible(visibility);
1920 }
1921 }
1922
1923 private int lookupBookmark(String title, String url) {
1924 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001925 int count = 0;
1926 Cursor cursor = null;
1927 try {
1928 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1929 BookmarksLoader.PROJECTION,
1930 "title = ? OR url = ?",
1931 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001932 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001933 },
1934 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001935
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001936 if (cursor != null)
1937 count = cursor.getCount();
1938
1939 } catch (IllegalStateException e) {
1940 Log.e(LOGTAG, "lookupBookmark ", e);
1941 } finally {
1942 if (null != cursor) {
1943 cursor.close();
1944 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001945 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001946 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001947 }
1948
1949 private void resetMenuItems(Menu menu) {
1950 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1951 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1952
1953 WebView w = getCurrentTopWebView();
1954 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1955
1956 String title = w.getTitle();
1957 String url = w.getUrl();
Pankaj Garg634bf432015-07-13 09:54:21 -07001958 mCurrentPageBookmarked = (lookupBookmark(title, url) > 0);
1959 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001960 bookmark_icon.setChecked(true);
1961 } else {
1962 bookmark_icon.setChecked(false);
1963 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001964
1965 // update reader mode checkbox
1966 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1967 readerSwitcher.setVisible(false);
1968 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001969 }
1970
Michael Kolb4bf79712011-07-14 14:18:12 -07001971 @Override
1972 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001973 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001974 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001975 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001976 // Following flag is used to identify schemes for which the LIVE_MENU
1977 // items defined in res/menu/browser.xml should be enabled
1978 boolean isLiveScheme = false;
1979 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001980 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07001981
1982 boolean isDistillable = false;
1983 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001984 resetMenuItems(menu);
1985
Michael Kolb4bf79712011-07-14 14:18:12 -07001986 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001987 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001988 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001989 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001990 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001991 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001992 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07001993
1994 isDistillable = tab.isDistillable();
1995 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07001996 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001997
1998 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1999 forward.setEnabled(canGoForward);
2000
Michael Kolb4bf79712011-07-14 14:18:12 -07002001 // decide whether to show the share link option
2002 PackageManager pm = mActivity.getPackageManager();
2003 Intent send = new Intent(Intent.ACTION_SEND);
2004 send.setType("text/plain");
2005 ResolveInfo ri = pm.resolveActivity(send,
2006 PackageManager.MATCH_DEFAULT_ONLY);
2007 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2008
2009 boolean isNavDump = mSettings.enableNavDump();
2010 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2011 nav.setVisible(isNavDump);
2012 nav.setEnabled(isNavDump);
2013
2014 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002015 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2016 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002017 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002018 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2019 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002020 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002021 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2022 isLive && isLiveScheme && isPageFinished);
2023 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002024 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002025 // history and snapshots item are the members of COMBO menu group,
2026 // so if show history item, only make snapshots item invisible.
2027 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002028
Ze G Riande2a675c22015-06-03 11:15:24 -07002029
2030 // update reader mode checkbox
2031 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2032 // The reader mode checkbox is hidden only
2033 // when the current page is neither distillable nor distilled
2034 readerSwitcher.setVisible(isDistillable || isDistilled);
2035 readerSwitcher.setChecked(isDistilled);
2036
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002037 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002038 }
2039
John Reck9c35b9c2012-05-30 10:08:50 -07002040 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002041 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 if (null == getCurrentTopWebView()) {
2043 return false;
2044 }
2045 if (mMenuIsDown) {
2046 // The shortcut action consumes the MENU. Even if it is still down,
2047 // it won't trigger the next shortcut action. In the case of the
2048 // shortcut action triggering a new activity, like Bookmarks, we
2049 // won't get onKeyUp for MENU. So it is important to reset it here.
2050 mMenuIsDown = false;
2051 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002052 if (mUi.onOptionsItemSelected(item)) {
2053 // ui callback handled it
2054 return true;
2055 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002056 switch (item.getItemId()) {
2057 // -- Main menu
2058 case R.id.new_tab_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002059 getCurrentTab().capture();
Michael Kolb8233fac2010-10-26 16:08:53 -07002060 openTabToHomePage();
2061 break;
2062
2063 case R.id.incognito_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002064 getCurrentTab().capture();
Michael Kolb519d2282011-05-09 17:03:19 -07002065 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002066 break;
2067
2068 case R.id.goto_menu_id:
2069 editUrl();
2070 break;
2071
2072 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002073 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2074 break;
2075
2076 case R.id.history_menu_id:
2077 bookmarksOrHistoryPicker(ComboViews.History);
2078 break;
2079
2080 case R.id.snapshots_menu_id:
2081 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002082 break;
2083
Pankaj Garg49b79252014-11-07 17:33:41 -08002084 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002085 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002086 break;
2087
2088 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002089 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002090 stopLoading();
2091 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002092 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002093 getCurrentTopWebView().reload();
2094 }
2095 break;
2096
Michael Kolb8233fac2010-10-26 16:08:53 -07002097 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002098 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002099 break;
2100
2101 case R.id.close_menu_id:
2102 // Close the subwindow if it exists.
2103 if (mTabControl.getCurrentSubWindow() != null) {
2104 dismissSubWindow(mTabControl.getCurrentTab());
2105 break;
2106 }
2107 closeCurrentTab();
2108 break;
2109
kaiyiza8b6dbb2013-07-29 18:11:22 +08002110 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002111 Object[] params = { new String("persist.debug.browsermonkeytest")};
2112 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002113 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002114 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002115 if (ret != null && ret.equals("enable"))
2116 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002117 if (BrowserConfig.getInstance(getContext())
2118 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2119 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002120 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002121 case R.id.homepage_menu_id:
2122 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002123 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002124 break;
2125
2126 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002127 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002128 break;
2129
2130 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002131 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002132 break;
2133
John Reck2bc80422011-06-30 15:11:49 -07002134 case R.id.save_snapshot_menu_id:
2135 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002136 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002137 createScreenshotAsync(
2138 source.getWebView(),
2139 getDesiredThumbnailWidth(mActivity),
2140 getDesiredThumbnailHeight(mActivity),
2141 new ValueCallback<Bitmap>() {
2142 @Override
2143 public void onReceiveValue(Bitmap bitmap) {
2144 new SaveSnapshotTask(source, bitmap).execute();
2145 }
2146 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002147 break;
2148
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002150 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002151 break;
2152
John Recke1a03a32011-09-14 17:04:16 -07002153 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002154 // passing null to distinguish between
2155 // "go live" button and navigating a web page
2156 // on a snapshot tab
2157 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002158 case R.id.share_page_menu_id:
2159 Tab currentTab = mTabControl.getCurrentTab();
2160 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002161 return false;
2162 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002163 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002164 break;
2165
2166 case R.id.dump_nav_menu_id:
2167 getCurrentTopWebView().debugDump();
2168 break;
2169
Michael Kolb8233fac2010-10-26 16:08:53 -07002170 case R.id.zoom_in_menu_id:
2171 getCurrentTopWebView().zoomIn();
2172 break;
2173
2174 case R.id.zoom_out_menu_id:
2175 getCurrentTopWebView().zoomOut();
2176 break;
2177
2178 case R.id.view_downloads_menu_id:
2179 viewDownloads();
2180 break;
2181
John Reck42229bc2011-08-19 13:26:43 -07002182 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002183 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002184 break;
2185
Ze G Riande2a675c22015-06-03 11:15:24 -07002186 case R.id.reader_mode_menu_id:
2187 toggleReaderMode();
2188 break;
2189
Michael Kolb8233fac2010-10-26 16:08:53 -07002190 case R.id.window_one_menu_id:
2191 case R.id.window_two_menu_id:
2192 case R.id.window_three_menu_id:
2193 case R.id.window_four_menu_id:
2194 case R.id.window_five_menu_id:
2195 case R.id.window_six_menu_id:
2196 case R.id.window_seven_menu_id:
2197 case R.id.window_eight_menu_id:
2198 {
2199 int menuid = item.getItemId();
2200 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2201 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2202 Tab desiredTab = mTabControl.getTab(id);
2203 if (desiredTab != null &&
2204 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002205 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002206 }
2207 break;
2208 }
2209 }
2210 }
2211 break;
2212
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002213 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002214 Bundle bundle = new Bundle();
2215 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2216 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2217 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2218 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2219 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002220 break;
2221
Pankaj Garg49b79252014-11-07 17:33:41 -08002222 case R.id.add_to_homescreen:
2223 final WebView w = getCurrentTopWebView();
2224 final EditText input = new EditText(getContext());
2225 input.setText(w.getTitle());
2226 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002227 .setTitle(getContext().getResources().getString(
2228 R.string.add_to_homescreen))
2229 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002230 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002231 .setPositiveButton(getContext().getResources().getString(
2232 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002233 public void onClick(DialogInterface dialog, int whichButton) {
2234 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2235 getContext(),
2236 w.getUrl(),
2237 input.getText().toString(),
2238 w.getViewportBitmap(),
2239 w.getFavicon()));
2240
2241 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2242 .addCategory(Intent.CATEGORY_HOME));
2243 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002244 .setNegativeButton(getContext().getResources().getString(
2245 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002246 public void onClick(DialogInterface dialog, int whichButton) {
2247 // Do nothing.
2248 }
2249 })
2250 .show();
2251 break;
2252
Michael Kolb8233fac2010-10-26 16:08:53 -07002253 default:
2254 return false;
2255 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002256 return true;
2257 }
2258
John Reck68234a92012-04-19 15:27:12 -07002259 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2260 implements OnCancelListener {
2261
2262 private Tab mTab;
2263 private Dialog mProgressDialog;
2264 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002265 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002266
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002267 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002268 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002269 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002270 }
2271
2272 @Override
2273 protected void onPreExecute() {
2274 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2275 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2276 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002277 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002278 }
2279
2280 @Override
2281 protected Long doInBackground(Void... params) {
2282 if (!mTab.saveViewState(mValues)) {
2283 return null;
2284 }
2285 if (isCancelled()) {
2286 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2287 File file = mActivity.getFileStreamPath(path);
2288 if (!file.delete()) {
2289 file.deleteOnExit();
2290 }
2291 return null;
2292 }
2293 final ContentResolver cr = mActivity.getContentResolver();
2294 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2295 if (result == null) {
2296 return null;
2297 }
2298 long id = ContentUris.parseId(result);
2299 return id;
2300 }
2301
2302 @Override
2303 protected void onPostExecute(Long id) {
2304 if (isCancelled()) {
2305 return;
2306 }
2307 mProgressDialog.dismiss();
2308 if (id == null) {
2309 Toast.makeText(mActivity, R.string.snapshot_failed,
2310 Toast.LENGTH_SHORT).show();
2311 return;
2312 }
2313 Bundle b = new Bundle();
2314 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2315 mUi.showComboView(ComboViews.Snapshots, b);
2316 }
2317
2318 @Override
2319 public void onCancel(DialogInterface dialog) {
2320 cancel(true);
2321 }
2322 }
2323
Michael Kolb80f75082012-04-10 10:50:06 -07002324 @Override
2325 public void toggleUserAgent() {
2326 WebView web = getCurrentWebView();
2327 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002328 }
2329
Ze G Riande2a675c22015-06-03 11:15:24 -07002330 // This function calls the method in the webview to enable/disable
2331 // the reader mode through the DOM distiller
2332 public void toggleReaderMode() {
2333 Tab t = mTabControl.getCurrentTab();
2334 if (t.isDistilled()) {
2335 closeTab(t);
2336 } else if (t.isDistillable()) {
2337 openTab(t.getDistilledUrl(), false, true, false, t);
2338 }
2339 }
2340
Michael Kolb80f75082012-04-10 10:50:06 -07002341 @Override
2342 public void findOnPage() {
2343 getCurrentTopWebView().showFindDialog(null, true);
2344 }
2345
2346 @Override
2347 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002348 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002349 }
2350
2351 @Override
2352 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002353 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002354 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2355 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002356 }
2357 else {
2358 WebView w = getCurrentTopWebView();
2359 if (w == null)
2360 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002361 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002362 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002363 }
Michael Kolb80f75082012-04-10 10:50:06 -07002364 }
2365
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002366 public boolean goLive(String url) {
2367 if (!getCurrentTab().isSnapshot())
2368 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002369 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002370
2371 if (url == null) { // "go live" button was clicked
2372 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002373 closeTab(t);
2374 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002375 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002376 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002377 return true;
John Recke1a03a32011-09-14 17:04:16 -07002378 }
2379
luxiaolb40014b2013-07-19 10:01:43 +08002380 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002381 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002382 new AlertDialog.Builder(activity)
2383 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002384 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002385 .setMessage(R.string.exit_browser_msg)
2386 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2387 public void onClick(DialogInterface dialog, int which) {
2388 activity.moveTaskToBack(true);
2389 dialog.dismiss();
2390 }
2391 })
2392 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2393 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002394 mCrashRecoveryHandler.clearState(true);
2395 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002396 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002397 dialog.dismiss();
2398 }
2399 })
2400 .show();
2401 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002402
Michael Kolb315d5022011-10-13 12:47:11 -07002403 @Override
2404 public void showPageInfo() {
2405 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2406 }
2407
John Reck9c35b9c2012-05-30 10:08:50 -07002408 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002409 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002410 // Let the History and Bookmark fragments handle menus they created.
2411 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2412 return false;
2413 }
2414
Michael Kolb8233fac2010-10-26 16:08:53 -07002415 int id = item.getItemId();
2416 boolean result = true;
2417 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002418 // -- Browser context menu
2419 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002420 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002421 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 case R.id.copy_link_context_menu_id:
2423 final WebView webView = getCurrentTopWebView();
2424 if (null == webView) {
2425 result = false;
2426 break;
2427 }
2428 final HashMap<String, WebView> hrefMap =
2429 new HashMap<String, WebView>();
2430 hrefMap.put("webview", webView);
2431 final Message msg = mHandler.obtainMessage(
2432 FOCUS_NODE_HREF, id, 0, hrefMap);
2433 webView.requestFocusNodeHref(msg);
2434 break;
2435
2436 default:
2437 // For other context menus
2438 result = onOptionsItemSelected(item);
2439 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002440 return result;
2441 }
2442
2443 /**
2444 * support programmatically opening the context menu
2445 */
2446 public void openContextMenu(View view) {
2447 mActivity.openContextMenu(view);
2448 }
2449
2450 /**
2451 * programmatically open the options menu
2452 */
2453 public void openOptionsMenu() {
2454 mActivity.openOptionsMenu();
2455 }
2456
John Reck9c35b9c2012-05-30 10:08:50 -07002457 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002458 public boolean onMenuOpened(int featureId, Menu menu) {
2459 if (mOptionsMenuOpen) {
2460 if (mConfigChanged) {
2461 // We do not need to make any changes to the state of the
2462 // title bar, since the only thing that happened was a
2463 // change in orientation
2464 mConfigChanged = false;
2465 } else {
2466 if (!mExtendedMenuOpen) {
2467 mExtendedMenuOpen = true;
2468 mUi.onExtendedMenuOpened();
2469 } else {
2470 // Switching the menu back to icon view, so show the
2471 // title bar once again.
2472 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002473 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002474 }
2475 }
2476 } else {
2477 // The options menu is closed, so open it, and show the title
2478 mOptionsMenuOpen = true;
2479 mConfigChanged = false;
2480 mExtendedMenuOpen = false;
2481 mUi.onOptionsMenuOpened();
2482 }
2483 return true;
2484 }
2485
John Reck9c35b9c2012-05-30 10:08:50 -07002486 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002487 public void onOptionsMenuClosed(Menu menu) {
2488 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002489 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002490 }
2491
John Reck9c35b9c2012-05-30 10:08:50 -07002492 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002493 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002494 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002495 }
2496
2497 // Helper method for getting the top window.
2498 @Override
2499 public WebView getCurrentTopWebView() {
2500 return mTabControl.getCurrentTopWebView();
2501 }
2502
2503 @Override
2504 public WebView getCurrentWebView() {
2505 return mTabControl.getCurrentWebView();
2506 }
2507
2508 /*
2509 * This method is called as a result of the user selecting the options
2510 * menu to see the download window. It shows the download window on top of
2511 * the current window.
2512 */
2513 void viewDownloads() {
2514 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2515 mActivity.startActivity(intent);
2516 }
2517
John Reck30b065e2011-07-19 10:58:05 -07002518 int getActionModeHeight() {
2519 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2520 new int[] { android.R.attr.actionBarSize });
2521 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2522 actionBarSizeTypedArray.recycle();
2523 return size;
2524 }
2525
Michael Kolb8233fac2010-10-26 16:08:53 -07002526 // action mode
2527
John Reck9c35b9c2012-05-30 10:08:50 -07002528 @Override
2529 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002530 mUi.onActionModeStarted(mode);
2531 mActionMode = mode;
2532 }
2533
2534 /*
2535 * True if a custom ActionMode (i.e. find or select) is in use.
2536 */
2537 @Override
2538 public boolean isInCustomActionMode() {
2539 return mActionMode != null;
2540 }
2541
2542 /*
2543 * End the current ActionMode.
2544 */
2545 @Override
2546 public void endActionMode() {
2547 if (mActionMode != null) {
2548 mActionMode.finish();
2549 }
2550 }
2551
2552 /*
2553 * Called by find and select when they are finished. Replace title bars
2554 * as necessary.
2555 */
John Reck9c35b9c2012-05-30 10:08:50 -07002556 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002557 public void onActionModeFinished(ActionMode mode) {
2558 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002559 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002560 mActionMode = null;
2561 }
2562
2563 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002564 final Tab tab = getCurrentTab();
2565 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002566 }
2567
2568 // bookmark handling
2569
2570 /**
2571 * add the current page as a bookmark to the given folder id
2572 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002573 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002574 * bookmarked, and if it is, edit that bookmark. If false, and
2575 * the site is already bookmarked, do not attempt to edit the
2576 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002577 */
2578 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002579 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002580 WebView w = getCurrentTopWebView();
2581 if (w == null) {
2582 return null;
2583 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002584 Intent i = new Intent(mActivity,
2585 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002586 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2587 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002588 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002589 if (touchIconUrl != null) {
2590 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2591 WebSettings settings = w.getSettings();
2592 if (settings != null) {
2593 i.putExtra(AddBookmarkPage.USER_AGENT,
2594 settings.getUserAgentString());
2595 }
2596 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002597 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002598 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002599 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002600 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2601 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 // Put the dialog at the upper right of the screen, covering the
2603 // star on the title bar.
2604 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002605 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 }
2607
2608 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002609 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002610 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002611 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002612 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002613 }
2614
Vivek Sekharb54614f2014-05-01 19:03:37 -07002615 @Override
2616 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2617 boolean capture) {
2618 mUploadHandler = new UploadHandler(this);
2619 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2620 }
2621
Michael Kolb8233fac2010-10-26 16:08:53 -07002622 // thumbnails
2623
2624 /**
2625 * Return the desired width for thumbnail screenshots, which are stored in
2626 * the database, and used on the bookmarks screen.
2627 * @param context Context for finding out the density of the screen.
2628 * @return desired width for thumbnail screenshot.
2629 */
2630 static int getDesiredThumbnailWidth(Context context) {
2631 return context.getResources().getDimensionPixelOffset(
2632 R.dimen.bookmarkThumbnailWidth);
2633 }
2634
2635 /**
2636 * Return the desired height for thumbnail screenshots, which are stored in
2637 * the database, and used on the bookmarks screen.
2638 * @param context Context for finding out the density of the screen.
2639 * @return desired height for thumbnail screenshot.
2640 */
2641 static int getDesiredThumbnailHeight(Context context) {
2642 return context.getResources().getDimensionPixelOffset(
2643 R.dimen.bookmarkThumbnailHeight);
2644 }
2645
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002646 static void createScreenshotAsync(WebView view, int width, int height,
2647 final ValueCallback<Bitmap> cb) {
2648 if (view == null || width == 0 || height == 0) {
2649 return;
2650 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002651 view.getContentBitmapAsync(
2652 (float) width / view.getWidth(),
2653 new Rect(),
2654 new ValueCallback<Bitmap>() {
2655 @Override
2656 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002657 if (bitmap != null)
2658 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2659 cb.onReceiveValue(bitmap);
2660 }});
2661 }
2662
Michael Kolb8233fac2010-10-26 16:08:53 -07002663 private class Copy implements OnMenuItemClickListener {
2664 private CharSequence mText;
2665
John Reck9c35b9c2012-05-30 10:08:50 -07002666 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002667 public boolean onMenuItemClick(MenuItem item) {
2668 copy(mText);
2669 return true;
2670 }
2671
2672 public Copy(CharSequence toCopy) {
2673 mText = toCopy;
2674 }
2675 }
2676
Leon Scroggins63c02662010-11-18 15:16:27 -05002677 private static class Download implements OnMenuItemClickListener {
2678 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002679 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002680 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002681 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002682 private static final String FALLBACK_EXTENSION = "dat";
2683 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002684
John Reck9c35b9c2012-05-30 10:08:50 -07002685 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002686 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002687 if (DataUri.isDataUri(mText)) {
2688 saveDataUri();
2689 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002690 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002691 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002692 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002693 return true;
2694 }
2695
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002696 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2697 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002698 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002699 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002700 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002701 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002702 }
George Mount387d45d2011-10-07 15:57:53 -07002703
2704 /**
2705 * Treats mText as a data URI and writes its contents to a file
2706 * based on the current time.
2707 */
2708 private void saveDataUri() {
2709 FileOutputStream outputStream = null;
2710 try {
2711 DataUri uri = new DataUri(mText);
2712 File target = getTarget(uri);
2713 outputStream = new FileOutputStream(target);
2714 outputStream.write(uri.getData());
2715 final DownloadManager manager =
2716 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2717 manager.addCompletedDownload(target.getName(),
2718 mActivity.getTitle().toString(), false,
2719 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002720 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002721 } catch (IOException e) {
2722 Log.e(LOGTAG, "Could not save data URL");
2723 } finally {
2724 if (outputStream != null) {
2725 try {
2726 outputStream.close();
2727 } catch (IOException e) {
2728 // ignore close errors
2729 }
2730 }
2731 }
2732 }
2733
2734 /**
2735 * Creates a File based on the current time stamp and uses
2736 * the mime type of the DataUri to get the extension.
2737 */
2738 private File getTarget(DataUri uri) throws IOException {
2739 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002740 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002741 String nameBase = format.format(new Date());
2742 String mimeType = uri.getMimeType();
2743 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2744 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2745 if (extension == null) {
2746 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2747 extension = FALLBACK_EXTENSION;
2748 }
2749 extension = "." + extension; // createTempFile needs the '.'
2750 File targetFile = File.createTempFile(nameBase, extension, dir);
2751 return targetFile;
2752 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002753 }
2754
Cary Clark8974d282010-11-22 10:46:05 -05002755 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002756 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002757
John Reck9c35b9c2012-05-30 10:08:50 -07002758 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002759 public boolean onMenuItemClick(MenuItem item) {
2760 if (mWebView != null) {
2761 return mWebView.selectText();
2762 }
2763 return false;
2764 }
2765
2766 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002767 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002768 }
2769
2770 }
2771
Michael Kolb8233fac2010-10-26 16:08:53 -07002772 /********************** TODO: UI stuff *****************************/
2773
2774 // these methods have been copied, they still need to be cleaned up
2775
2776 /****************** tabs ***************************************************/
2777
2778 // basic tab interactions:
2779
2780 // it is assumed that tabcontrol already knows about the tab
2781 protected void addTab(Tab tab) {
2782 mUi.addTab(tab);
2783 }
2784
2785 protected void removeTab(Tab tab) {
2786 mUi.removeTab(tab);
2787 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002788 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002789 }
2790
Michael Kolbf2055602011-04-09 17:20:03 -07002791 @Override
2792 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002793 // monkey protection against delayed start
2794 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002795
2796 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2797 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002798 mTabControl.setCurrentTab(tab);
2799 // the tab is guaranteed to have a webview after setCurrentTab
2800 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002801
2802
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002803 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002804 //Purge active tabs
2805 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002806 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002807 }
2808
John Reck8bcafc12011-08-29 16:43:02 -07002809 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002810 Tab current = mTabControl.getCurrentTab();
2811 if (current != null
2812 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002813 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002814 }
2815 }
2816
John Reck26b18322011-06-21 13:08:58 -07002817 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002818 //Cancel navscreen request
2819 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002820 // Dismiss the subwindow if applicable.
2821 dismissSubWindow(appTab);
2822 // Since we might kill the WebView, remove it from the
2823 // content view first.
2824 mUi.detachTab(appTab);
2825 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002826 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002827 // TODO: analyze why the remove and add are necessary
2828 mUi.attachTab(appTab);
2829 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002830 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002831 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002832 } else {
2833 // If the tab was the current tab, we have to attach
2834 // it to the view system again.
2835 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002836 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002837 }
2838 }
2839
2840 // Remove the sub window if it exists. Also called by TabControl when the
2841 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002842 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002843 public void dismissSubWindow(Tab tab) {
2844 removeSubWindow(tab);
2845 // dismiss the subwindow. This will destroy the WebView.
2846 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002847 WebView wv = getCurrentTopWebView();
2848 if (wv != null) {
2849 wv.requestFocus();
2850 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002851 }
2852
2853 @Override
2854 public void removeSubWindow(Tab t) {
2855 if (t.getSubWebView() != null) {
2856 mUi.removeSubWindow(t.getSubViewContainer());
2857 }
2858 }
2859
2860 @Override
2861 public void attachSubWindow(Tab tab) {
2862 if (tab.getSubWebView() != null) {
2863 mUi.attachSubWindow(tab.getSubViewContainer());
2864 getCurrentTopWebView().requestFocus();
2865 }
2866 }
2867
Mathew Inwood29721c22011-06-29 17:55:24 +01002868 private Tab showPreloadedTab(final UrlData urlData) {
2869 if (!urlData.isPreloaded()) {
2870 return null;
2871 }
2872 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2873 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2874 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002875 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002876 // Could not submit query. Fallback to regular tab creation
2877 tabControl.destroy();
2878 return null;
2879 }
2880 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002881 // check tab count and make room for new tab
2882 if (!mTabControl.canCreateNewTab()) {
2883 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2884 if (leastUsed != null) {
2885 closeTab(leastUsed);
2886 }
2887 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002888 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002889 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002890 mTabControl.addPreloadedTab(t);
2891 addTab(t);
2892 setActiveTab(t);
2893 return t;
2894 }
2895
Michael Kolb7bcafde2011-05-09 13:55:59 -07002896 // open a non inconito tab with the given url data
2897 // and set as active tab
2898 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002899 Tab tab = showPreloadedTab(urlData);
2900 if (tab == null) {
2901 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002902 if ((tab != null) && !urlData.isEmpty()) {
2903 loadUrlDataIn(tab, urlData);
2904 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002905 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002906 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002907 }
2908
Michael Kolb843510f2010-12-09 10:51:49 -08002909 @Override
2910 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002911 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002912 }
2913
Michael Kolb8233fac2010-10-26 16:08:53 -07002914 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002915 public Tab openIncognitoTab() {
2916 return openTab(INCOGNITO_URI, true, true, false);
2917 }
2918
2919 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002920 public Tab openTab(String url, boolean incognito, boolean setActive,
2921 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002922 return openTab(url, incognito, setActive, useCurrent, null);
2923 }
2924
2925 @Override
2926 public Tab openTab(String url, Tab parent, boolean setActive,
2927 boolean useCurrent) {
2928 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2929 setActive, useCurrent, parent);
2930 }
2931
2932 public Tab openTab(String url, boolean incognito, boolean setActive,
2933 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002934 Tab tab = createNewTab(incognito, setActive, useCurrent);
2935 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002936 if (parent instanceof SnapshotTab) {
2937 addTab(tab);
2938 if (setActive)
2939 setActiveTab(tab);
2940 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002941 parent.addChildTab(tab);
2942 }
Michael Kolb519d2282011-05-09 17:03:19 -07002943 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002944 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002945 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002946 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002947 return tab;
2948 }
2949
2950 // this method will attempt to create a new tab
2951 // incognito: private browsing tab
2952 // setActive: ste tab as current tab
2953 // useCurrent: if no new tab can be created, return current tab
2954 private Tab createNewTab(boolean incognito, boolean setActive,
2955 boolean useCurrent) {
2956 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002957 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2958 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002959 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002960 if (mTabControl.canCreateNewTab()) {
2961 tab = mTabControl.createNewTab(incognito, !setActive);
2962 addTab(tab);
2963 if (setActive) {
2964 setActiveTab(tab);
2965 } else {
2966 tab.pause();
2967 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002968 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002969 if (useCurrent) {
2970 tab = mTabControl.getCurrentTab();
2971 reuseTab(tab, null);
2972 } else {
2973 mUi.showMaxTabsWarning();
2974 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002975 }
2976 }
2977 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002978 }
2979
John Reck2bc80422011-06-30 15:11:49 -07002980 @Override
2981 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2982 SnapshotTab tab = null;
2983 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07002984 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07002985 addTab(tab);
2986 if (setActive) {
2987 setActiveTab(tab);
2988 }
2989 } else {
2990 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002991 }
2992 return tab;
2993 }
2994
Michael Kolb8233fac2010-10-26 16:08:53 -07002995 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002996 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002997 * @return boolean True if we successfully switched to a different tab. If
2998 * the indexth tab is null, or if that tab is the same as
2999 * the current one, return false.
3000 */
3001 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003002 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003003 Tab currentTab = mTabControl.getCurrentTab();
3004 if (tab == null || tab == currentTab) {
3005 return false;
3006 }
3007 setActiveTab(tab);
3008 return true;
3009 }
3010
3011 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003012 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003013 closeCurrentTab(false);
3014 }
3015
3016 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003017 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003018 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003019 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003020 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003021 return;
3022 }
Michael Kolbc831b632011-05-11 09:30:34 -07003023 final Tab current = mTabControl.getCurrentTab();
3024 final int pos = mTabControl.getCurrentPosition();
3025 Tab newTab = current.getParent();
3026 if (newTab == null) {
3027 newTab = mTabControl.getTab(pos + 1);
3028 if (newTab == null) {
3029 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003030 }
3031 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003032 if (andQuit) {
3033 mTabControl.setCurrentTab(newTab);
3034 closeTab(current);
3035 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003036 // Close window
3037 closeTab(current);
3038 }
3039 }
3040
3041 /**
3042 * Close the tab, remove its associated title bar, and adjust mTabControl's
3043 * current tab to a valid value.
3044 */
3045 @Override
3046 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003047 if (tab == mTabControl.getCurrentTab()) {
3048 closeCurrentTab();
3049 } else {
3050 removeTab(tab);
3051 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003052 }
3053
Afzal Najamd4e33312012-04-26 01:54:01 -04003054 /**
3055 * Close all tabs except the current one
3056 */
3057 @Override
3058 public void closeOtherTabs() {
3059 int inactiveTabs = mTabControl.getTabCount() - 1;
3060 for (int i = inactiveTabs; i >= 0; i--) {
3061 Tab tab = mTabControl.getTab(i);
3062 if (tab != mTabControl.getCurrentTab()) {
3063 removeTab(tab);
3064 }
3065 }
3066 }
3067
Michael Kolb8233fac2010-10-26 16:08:53 -07003068 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003069 protected void loadUrlFromContext(String url) {
3070 Tab tab = getCurrentTab();
3071 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003072 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003073 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003074 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003075 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003076 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003077 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003078 }
3079 }
3080 }
3081
3082 /**
3083 * Load the URL into the given WebView and update the title bar
3084 * to reflect the new load. Call this instead of WebView.loadUrl
3085 * directly.
3086 * @param view The WebView used to load url.
3087 * @param url The URL to load.
3088 */
John Reck71e51422011-07-01 16:49:28 -07003089 @Override
3090 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003091 loadUrl(tab, url, null);
3092 }
3093
3094 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3095 if (tab != null) {
3096 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003097 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003098 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003099 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003100 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003101 }
3102
3103 /**
3104 * Load UrlData into a Tab and update the title bar to reflect the new
3105 * load. Call this instead of UrlData.loadIn directly.
3106 * @param t The Tab used to load.
3107 * @param data The UrlData being loaded.
3108 */
3109 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003110 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003111 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003112 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003113 } else {
John Reck38b39652012-06-05 09:22:59 -07003114 if (t != null && data.mDisableUrlOverride) {
3115 t.disableUrlOverridingForLoad();
3116 }
John Reck26b18322011-06-21 13:08:58 -07003117 loadUrl(t, data.mUrl, data.mHeaders);
3118 }
3119 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003120 }
3121
John Reck30c714c2010-12-16 17:30:34 -08003122 @Override
3123 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003124 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003125 //In case of tab can go back (aka tab has navigation entry) do nothing
3126 //else just load homepage in current tab.
3127 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003128 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003129 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003130 }
3131
3132 void goBackOnePageOrQuit() {
3133 Tab current = mTabControl.getCurrentTab();
3134 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003135 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3136 showExitDialog(mActivity);
3137 } else {
3138 /*
3139 * Instead of finishing the activity, simply push this to the back
3140 * of the stack and let ActivityManager to choose the foreground
3141 * activity. As BrowserActivity is singleTask, it will be always the
3142 * root of the task. So we can use either true or false for
3143 * moveTaskToBack().
3144 */
3145 mActivity.moveTaskToBack(true);
3146 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003147 return;
3148 }
John Reckef654f12011-07-12 16:42:08 -07003149 if (current.canGoBack()) {
3150 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003151 } else {
3152 // Check to see if we are closing a window that was created by
3153 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003154 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003155 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003156 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 // Now we close the other tab
3158 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003159 } else if (BrowserConfig.getInstance(getContext())
3160 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3161 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003162 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003163 /*
3164 * Instead of finishing the activity, simply push this to the back
3165 * of the stack and let ActivityManager to choose the foreground
3166 * activity. As BrowserActivity is singleTask, it will be always the
3167 * root of the task. So we can use either true or false for
3168 * moveTaskToBack().
3169 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003170 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003171 }
3172 }
3173 }
3174
3175 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003176 * helper method for key handler
3177 * returns the current tab if it can't advance
3178 */
Michael Kolbc831b632011-05-11 09:30:34 -07003179 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003180 int pos = mTabControl.getCurrentPosition() + 1;
3181 if (pos >= mTabControl.getTabCount()) {
3182 pos = 0;
3183 }
3184 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003185 }
3186
3187 /**
3188 * helper method for key handler
3189 * returns the current tab if it can't advance
3190 */
Michael Kolbc831b632011-05-11 09:30:34 -07003191 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003192 int pos = mTabControl.getCurrentPosition() - 1;
3193 if ( pos < 0) {
3194 pos = mTabControl.getTabCount() - 1;
3195 }
3196 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003197 }
3198
John Reckbcef87f2012-02-03 14:58:34 -08003199 boolean isMenuOrCtrlKey(int keyCode) {
3200 return (KeyEvent.KEYCODE_MENU == keyCode)
3201 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3202 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3203 }
3204
Michael Kolb0035fad2011-03-14 13:25:28 -07003205 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003206 * handle key events in browser
3207 *
3208 * @param keyCode
3209 * @param event
3210 * @return true if handled, false to pass to super
3211 */
John Reck9c35b9c2012-05-30 10:08:50 -07003212 @Override
3213 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003214 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3215 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003216 if (!mUi.isComboViewShowing()) {
3217 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3218 true, false);
3219 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003220 return true;
3221 }
3222
Cary Clark160bbb92011-01-10 11:17:07 -05003223 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003224 // Even if MENU is already held down, we need to call to super to open
3225 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003226 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003227 mMenuIsDown = true;
3228 return false;
3229 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003230
Cary Clark8ff8c662010-12-29 15:03:05 -05003231 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003232 Tab tab = getCurrentTab();
3233 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003234
Cary Clark160bbb92011-01-10 11:17:07 -05003235 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3236 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003237
Michael Kolb8233fac2010-10-26 16:08:53 -07003238 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003239 case KeyEvent.KEYCODE_TAB:
3240 if (event.isCtrlPressed()) {
3241 if (event.isShiftPressed()) {
3242 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003243 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003244 } else {
3245 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003246 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003247 }
3248 return true;
3249 }
3250 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003251 case KeyEvent.KEYCODE_SPACE:
3252 // WebView/WebTextView handle the keys in the KeyDown. As
3253 // the Activity's shortcut keys are only handled when WebView
3254 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003255 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003256 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003257 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003258 pageDown();
3259 }
3260 return true;
3261 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003262 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003263 event.startTracking();
3264 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003265 case KeyEvent.KEYCODE_FORWARD:
3266 if (!noModifiers) break;
3267 tab.goForward();
3268 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003269 case KeyEvent.KEYCODE_DPAD_LEFT:
3270 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003271 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003272 return true;
3273 }
3274 break;
3275 case KeyEvent.KEYCODE_DPAD_RIGHT:
3276 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003277 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003278 return true;
3279 }
3280 break;
3281 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003282 if (ctrl) {
3283 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003284 return true;
3285 }
3286 break;
Michael Kolba4183062011-01-16 10:43:21 -08003287// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003288 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003289 if (ctrl ) {
3290 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003291 return true;
3292 }
3293 break;
Michael Kolba4183062011-01-16 10:43:21 -08003294// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003295// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003296// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003297// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003298// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003299// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003300// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003301// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003302// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003303// case KeyEvent.KEYCODE_M: // unused
3304// case KeyEvent.KEYCODE_N: // in Chrome: new window
3305// case KeyEvent.KEYCODE_O: // in Chrome: open file
3306// case KeyEvent.KEYCODE_P: // in Chrome: print page
3307// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003308// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003309// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3310 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003311 // we can't use the ctrl/shift flags, they check for
3312 // exclusive use of a modifier
3313 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003314 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003315 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003316 } else {
3317 openTabToHomePage();
3318 }
3319 return true;
3320 }
3321 break;
3322// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3323// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003324// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003325// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3326// case KeyEvent.KEYCODE_Y: // unused
3327// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003328 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003329 // it is a regular key and webview is not null
3330 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003331 }
3332
John Reck9c35b9c2012-05-30 10:08:50 -07003333 @Override
3334 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003335 switch(keyCode) {
3336 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003337 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003338 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003339 return true;
3340 }
3341 break;
3342 }
3343 return false;
3344 }
3345
John Reck9c35b9c2012-05-30 10:08:50 -07003346 @Override
3347 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003348 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003349 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003350 if (KeyEvent.KEYCODE_MENU == keyCode
3351 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003352 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003353 }
3354 }
Cary Clark160bbb92011-01-10 11:17:07 -05003355 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003356 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003357 case KeyEvent.KEYCODE_BACK:
3358 if (event.isTracking() && !event.isCanceled()) {
3359 onBackKey();
3360 return true;
3361 }
3362 break;
3363 }
3364 return false;
3365 }
3366
3367 public boolean isMenuDown() {
3368 return mMenuIsDown;
3369 }
3370
John Reck9c35b9c2012-05-30 10:08:50 -07003371 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003372 public void setupAutoFill(Message message) {
3373 // Open the settings activity at the AutoFill profile fragment so that
3374 // the user can create a new profile. When they return, we will dispatch
3375 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003376 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003377 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3378 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003379 }
John Reckb3417f02011-01-14 11:01:05 -08003380
John Reck9c35b9c2012-05-30 10:08:50 -07003381 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003382 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003383 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003384 return true;
3385 }
3386
John Reck1cf4b792011-07-26 10:22:22 -07003387 @Override
3388 public boolean shouldCaptureThumbnails() {
3389 return mUi.shouldCaptureThumbnails();
3390 }
3391
Michael Kolbc3af0672011-08-09 10:24:41 -07003392 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003393 public boolean supportsVoice() {
3394 PackageManager pm = mActivity.getPackageManager();
3395 List activities = pm.queryIntentActivities(new Intent(
3396 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3397 return activities.size() != 0;
3398 }
3399
3400 @Override
3401 public void startVoiceRecognizer() {
3402 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003403 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003404 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3405 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3406 mActivity.startActivityForResult(voice, VOICE_RESULT);
3407 }
3408
Pankaj Garg7b279f62014-08-12 14:47:18 -07003409 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003410 if (mLevel == level)
3411 return;
3412 mLevel = level;
3413 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003414 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3415 lp.dimAmount = level;
3416 mActivity.getWindow().setAttributes(lp);
3417 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3418 } else {
3419 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3420 }
3421 }
3422
Michael Kolb0b129122012-06-04 16:31:58 -07003423 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003424 public void setBlockEvents(boolean block) {
3425 mBlockEvents = block;
3426 }
3427
John Reck9c35b9c2012-05-30 10:08:50 -07003428 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003429 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003430 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003431 }
3432
John Reck9c35b9c2012-05-30 10:08:50 -07003433 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003434 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003435 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003436 }
3437
John Reck9c35b9c2012-05-30 10:08:50 -07003438 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003439 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003440 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003441 }
3442
John Reck9c35b9c2012-05-30 10:08:50 -07003443 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003444 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003445 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003446 }
3447
John Reck9c35b9c2012-05-30 10:08:50 -07003448 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003449 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003450 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003451 }
3452
Pankaj Garg49b79252014-11-07 17:33:41 -08003453 @Override
3454 public boolean shouldShowAppMenu() {
3455 return true;
3456 }
3457
3458 @Override
3459 public int getMenuThemeResourceId() {
3460 return R.style.OverflowMenuTheme;
3461 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003462}