blob: dbbc164a20ee01531ffc73aa8e535d8d59c2ed4f [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) {
1061 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 }
1063
1064 @Override
1065 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1066 if (mMenuIsDown) {
1067 // only check shortcut key when MENU is held
1068 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1069 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001070 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001071 int keyCode = event.getKeyCode();
1072 // We need to send almost every key to WebKit. However:
1073 // 1. We don't want to block the device on the renderer for
1074 // some keys like menu, home, call.
1075 // 2. There are no WebKit equivalents for some of these keys
1076 // (see app/keyboard_codes_win.h)
1077 // Note that these are not the same set as KeyEvent.isSystemKey:
1078 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1079 if (keyCode == KeyEvent.KEYCODE_MENU ||
1080 keyCode == KeyEvent.KEYCODE_HOME ||
1081 keyCode == KeyEvent.KEYCODE_BACK ||
1082 keyCode == KeyEvent.KEYCODE_CALL ||
1083 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1084 keyCode == KeyEvent.KEYCODE_POWER ||
1085 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1086 keyCode == KeyEvent.KEYCODE_CAMERA ||
1087 keyCode == KeyEvent.KEYCODE_FOCUS ||
1088 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1089 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1090 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1091 return true;
1092 }
1093
1094 // We also have to intercept some shortcuts before we send them to the ContentView.
1095 if (event.isCtrlPressed() && (
1096 keyCode == KeyEvent.KEYCODE_TAB ||
1097 keyCode == KeyEvent.KEYCODE_W ||
1098 keyCode == KeyEvent.KEYCODE_F4)) {
1099 return true;
1100 }
1101
1102 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 }
1104
Tarun Nainanif5563b62015-01-21 18:52:59 -08001105 private void handleMediaKeyEvent(KeyEvent event) {
1106
1107 int keyCode = event.getKeyCode();
1108 // send media key events to audio manager
1109 if (Build.VERSION.SDK_INT >= 19) {
1110
1111 switch (keyCode) {
1112 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1113 case KeyEvent.KEYCODE_MEDIA_STOP:
1114 case KeyEvent.KEYCODE_MEDIA_NEXT:
1115 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1116 case KeyEvent.KEYCODE_MEDIA_REWIND:
1117 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1118 case KeyEvent.KEYCODE_MUTE:
1119 case KeyEvent.KEYCODE_MEDIA_PLAY:
1120 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1121 case KeyEvent.META_SHIFT_RIGHT_ON:
1122 case KeyEvent.KEYCODE_MEDIA_EJECT:
1123 case KeyEvent.KEYCODE_MEDIA_RECORD:
1124 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1125
1126 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1127 .getSystemService(Context.AUDIO_SERVICE);
1128 audioManager.dispatchMediaKeyEvent(event);
1129 }
1130 }
1131 }
1132
Michael Kolb8233fac2010-10-26 16:08:53 -07001133 @Override
John Reck997b1b72012-04-19 18:08:25 -07001134 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001136 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001138 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 } else {
John Reck997b1b72012-04-19 18:08:25 -07001140 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 }
1142 }
John Reck997b1b72012-04-19 18:08:25 -07001143 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 }
1145
1146 @Override
John Reck324d4402011-01-11 16:56:42 -08001147 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001148 // Don't save anything in private browsing mode or when disabling history
1149 // for regular tabs is enabled
1150 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1151 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1152 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001153
John Reck49a603c2011-03-03 09:33:05 -08001154 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001155
John Reck324d4402011-01-11 16:56:42 -08001156 if (TextUtils.isEmpty(url)
1157 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001158 return;
1159 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001160
John Reckf57c0292011-07-21 18:15:39 -07001161 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001162 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001163 }
1164
1165 @Override
1166 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1167 AsyncTask<Void, Void, String[]> task =
1168 new AsyncTask<Void, Void, String[]>() {
1169 @Override
1170 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001171 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 }
1173 @Override
1174 public void onPostExecute(String[] result) {
1175 callback.onReceiveValue(result);
1176 }
1177 };
1178 task.execute();
1179 }
1180
1181 @Override
1182 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1183 final HttpAuthHandler handler, final String host,
1184 final String realm) {
1185 String username = null;
1186 String password = null;
1187
1188 boolean reuseHttpAuthUsernamePassword
1189 = handler.useHttpAuthUsernamePassword();
1190
1191 if (reuseHttpAuthUsernamePassword && view != null) {
1192 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1193 if (credentials != null && credentials.length == 2) {
1194 username = credentials[0];
1195 password = credentials[1];
1196 }
1197 }
1198
1199 if (username != null && password != null) {
1200 handler.proceed(username, password);
1201 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001202 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1204 } else {
1205 handler.cancel();
1206 }
1207 }
1208 }
1209
1210 @Override
1211 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001212 String contentDisposition, String mimetype, String referer,
1213 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001214 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001215 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001216 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001217 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001218 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 // This Tab was opened for the sole purpose of downloading a
1220 // file. Remove it.
1221 if (tab == mTabControl.getCurrentTab()) {
1222 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001223 if (tab.getDerivedFromIntent())
1224 closeTab(tab);
1225 else
1226 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001227 } else {
1228 // In this case, it is not.
1229 closeTab(tab);
1230 }
1231 }
1232 }
1233
1234 @Override
1235 public Bitmap getDefaultVideoPoster() {
1236 return mUi.getDefaultVideoPoster();
1237 }
1238
1239 @Override
1240 public View getVideoLoadingProgressView() {
1241 return mUi.getVideoLoadingProgressView();
1242 }
1243
1244 @Override
1245 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1246 SslError error) {
1247 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1248 }
1249
1250 // helper method
1251
1252 /*
1253 * Update the favorites icon if the private browsing isn't enabled and the
1254 * icon is valid.
1255 */
1256 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1257 final String url, Bitmap favicon) {
1258 if (favicon == null) {
1259 return;
1260 }
1261 if (!tab.isPrivateBrowsingEnabled()) {
1262 Bookmarks.updateFavicon(mActivity
1263 .getContentResolver(), originalUrl, url, favicon);
1264 }
1265 }
1266
Leon Scroggins4cd97792010-12-03 15:31:56 -05001267 @Override
1268 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001269 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001270 mUi.bookmarkedStatusHasChanged(tab);
1271 }
1272
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 // end WebViewController
1274
1275 protected void pageUp() {
1276 getCurrentTopWebView().pageUp(false);
1277 }
1278
1279 protected void pageDown() {
1280 getCurrentTopWebView().pageDown(false);
1281 }
1282
1283 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001284 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001285 public void editUrl() {
1286 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001287 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001288 }
1289
John Reck9c35b9c2012-05-30 10:08:50 -07001290 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001291 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001292 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 if (tab.inForeground()) {
1294 if (mUi.isCustomViewShowing()) {
1295 callback.onCustomViewHidden();
1296 return;
1297 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001298 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001299 // Save the menu state and set it to empty while the custom
1300 // view is showing.
1301 mOldMenuState = mMenuState;
1302 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001303 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 }
1305 }
1306
1307 @Override
1308 public void hideCustomView() {
1309 if (mUi.isCustomViewShowing()) {
1310 mUi.onHideCustomView();
1311 // Reset the old menu state.
1312 mMenuState = mOldMenuState;
1313 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001314 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001315 }
1316 }
1317
John Reck9c35b9c2012-05-30 10:08:50 -07001318 @Override
1319 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001320 Intent intent) {
1321 if (getCurrentTopWebView() == null) return;
1322 switch (requestCode) {
1323 case PREFERENCES_PAGE:
1324 if (resultCode == Activity.RESULT_OK && intent != null) {
1325 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001326 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 mTabControl.removeParentChildRelationShips();
1328 }
1329 }
1330 break;
1331 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001332 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001333 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001334 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001336 case AUTOFILL_SETUP:
1337 // Determine whether a profile was actually set up or not
1338 // and if so, send the message back to the WebTextView to
1339 // fill the form with the new profile.
1340 if (getSettings().getAutoFillProfile() != null) {
1341 mAutoFillSetupMessage.sendToTarget();
1342 mAutoFillSetupMessage = null;
1343 }
1344 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001345 case COMBO_VIEW:
1346 if (intent == null || resultCode != Activity.RESULT_OK) {
1347 break;
1348 }
John Reck3ba45532011-08-11 16:26:53 -07001349 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001350 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1351 Tab t = getCurrentTab();
1352 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001353 mUpdateMyNavThumbnail = true;
1354 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001355 loadUrl(t, uri.toString());
1356 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1357 String[] urls = intent.getStringArrayExtra(
1358 ComboViewActivity.EXTRA_OPEN_ALL);
1359 Tab parent = getCurrentTab();
1360 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001361 if (url != null) {
1362 parent = openTab(url, parent,
1363 !mSettings.openInBackground(), true);
1364 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001365 }
1366 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1367 long id = intent.getLongExtra(
1368 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1369 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001370 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001371 }
1372 }
1373 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001374 case VOICE_RESULT:
1375 if (resultCode == Activity.RESULT_OK && intent != null) {
1376 ArrayList<String> results = intent.getStringArrayListExtra(
1377 RecognizerIntent.EXTRA_RESULTS);
1378 if (results.size() >= 1) {
1379 mVoiceResult = results.get(0);
1380 }
1381 }
1382 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001383 case MY_NAVIGATION:
1384 if (intent == null || resultCode != Activity.RESULT_OK) {
1385 break;
1386 }
1387
1388 if (intent.getBooleanExtra("need_refresh", false) &&
1389 getCurrentTopWebView() != null) {
1390 getCurrentTopWebView().reload();
1391 }
1392 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001393 default:
1394 break;
1395 }
1396 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001397 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 }
1399
1400 /**
1401 * Open the Go page.
1402 * @param startWithHistory If true, open starting on the history tab.
1403 * Otherwise, start with the bookmarks tab.
1404 */
1405 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001406 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 if (mTabControl.getCurrentWebView() == null) {
1408 return;
1409 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001410 // clear action mode
1411 if (isInCustomActionMode()) {
1412 endActionMode();
1413 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 Bundle extras = new Bundle();
1415 // Disable opening in a new window if we have maxed out the windows
1416 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1417 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001418 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 }
1420
1421 // combo view callbacks
1422
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 // key handling
1424 protected void onBackKey() {
1425 if (!mUi.onBackKey()) {
1426 WebView subwindow = mTabControl.getCurrentSubWindow();
1427 if (subwindow != null) {
1428 if (subwindow.canGoBack()) {
1429 subwindow.goBack();
1430 } else {
1431 dismissSubWindow(mTabControl.getCurrentTab());
1432 }
1433 } else {
1434 goBackOnePageOrQuit();
1435 }
1436 }
1437 }
1438
Michael Kolb4bd767d2011-05-27 11:33:55 -07001439 protected boolean onMenuKey() {
1440 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001441 }
1442
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 // menu handling and state
1444 // TODO: maybe put into separate handler
1445
John Reck9c35b9c2012-05-30 10:08:50 -07001446 @Override
1447 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001448 if (mMenuState == EMPTY_MENU) {
1449 return false;
1450 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 MenuInflater inflater = mActivity.getMenuInflater();
1452 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 return true;
1454 }
1455
John Reck9c35b9c2012-05-30 10:08:50 -07001456 @Override
1457 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001459 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001460 return;
1461 }
1462 if (!(v instanceof WebView)) {
1463 return;
1464 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001465 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001466 WebView.HitTestResult result;
1467
1468 /* Determine whether the ContextMenu got triggered because
1469 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1470 * received. The mResult acts as a flag to identify, how it got trigerred
1471 */
1472 if (mResult == null){
1473 result = webview.getHitTestResult();
1474 } else {
1475 result = mResult;
1476 }
1477
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 if (result == null) {
1479 return;
1480 }
1481
1482 int type = result.getType();
1483 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001484
1485 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1486 unknownTypeMap.put("webview", webview);
1487 final Message msg = mHandler.obtainMessage(
1488 UNKNOWN_TYPE_MSG, unknownTypeMap);
1489 /* As defined in android developers guide
1490 * when UNKNOWN_TYPE is received as a result of HitTest
1491 * you need to determing the type by invoking requestFocusNodeHref
1492 */
1493 webview.requestFocusNodeHref(msg);
1494
Michael Kolb8233fac2010-10-26 16:08:53 -07001495 Log.w(LOGTAG,
1496 "We should not show context menu when nothing is touched");
1497 return;
1498 }
1499 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1500 // let TextView handles context menu
1501 return;
1502 }
1503
1504 // Note, http://b/issue?id=1106666 is requesting that
1505 // an inflated menu can be used again. This is not available
1506 // yet, so inflate each time (yuk!)
1507 MenuInflater inflater = mActivity.getMenuInflater();
1508 inflater.inflate(R.menu.browsercontext, menu);
1509
1510 // Show the correct menu group
1511 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001512 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001513 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 menu.setGroupVisible(R.id.PHONE_MENU,
1515 type == WebView.HitTestResult.PHONE_TYPE);
1516 menu.setGroupVisible(R.id.EMAIL_MENU,
1517 type == WebView.HitTestResult.EMAIL_TYPE);
1518 menu.setGroupVisible(R.id.GEO_MENU,
1519 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001520 String itemUrl = null;
1521 String url = webview.getOriginalUrl();
1522 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1523 itemUrl = Uri.decode(navigationUrl);
1524 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1525 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1526 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1527 } else {
1528 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1529 }
1530 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1531 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1532 } else {
1533 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1534
1535 menu.setGroupVisible(R.id.IMAGE_MENU,
1536 type == WebView.HitTestResult.IMAGE_TYPE
1537 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1538 menu.setGroupVisible(R.id.ANCHOR_MENU,
1539 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1540 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001541
1542 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1543 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1544 UrlUtils.isDownloadableScheme(extra));
1545 }
1546 else {
1547 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1548 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001549 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001550 // Setup custom handling depending on the type
1551 switch (type) {
1552 case WebView.HitTestResult.PHONE_TYPE:
1553 menu.setHeaderTitle(Uri.decode(extra));
1554 menu.findItem(R.id.dial_context_menu_id).setIntent(
1555 new Intent(Intent.ACTION_VIEW, Uri
1556 .parse(WebView.SCHEME_TEL + extra)));
1557 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1558 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1559 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1560 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1561 addIntent);
1562 menu.findItem(R.id.copy_phone_context_menu_id)
1563 .setOnMenuItemClickListener(
1564 new Copy(extra));
1565 break;
1566
1567 case WebView.HitTestResult.EMAIL_TYPE:
1568 menu.setHeaderTitle(extra);
1569 menu.findItem(R.id.email_context_menu_id).setIntent(
1570 new Intent(Intent.ACTION_VIEW, Uri
1571 .parse(WebView.SCHEME_MAILTO + extra)));
1572 menu.findItem(R.id.copy_mail_context_menu_id)
1573 .setOnMenuItemClickListener(
1574 new Copy(extra));
1575 break;
1576
1577 case WebView.HitTestResult.GEO_TYPE:
1578 menu.setHeaderTitle(extra);
1579 menu.findItem(R.id.map_context_menu_id).setIntent(
1580 new Intent(Intent.ACTION_VIEW, Uri
1581 .parse(WebView.SCHEME_GEO
1582 + URLEncoder.encode(extra))));
1583 menu.findItem(R.id.copy_geo_context_menu_id)
1584 .setOnMenuItemClickListener(
1585 new Copy(extra));
1586 break;
1587
1588 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1589 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001590 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001591 // decide whether to show the open link in new tab option
1592 boolean showNewTab = mTabControl.canCreateNewTab();
1593 MenuItem newTabItem
1594 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001595 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001596 ? R.string.contextmenu_openlink_newwindow_background
1597 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001598 newTabItem.setVisible(showNewTab);
1599 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001600 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1601 newTabItem.setOnMenuItemClickListener(
1602 new MenuItem.OnMenuItemClickListener() {
1603 @Override
1604 public boolean onMenuItemClick(MenuItem item) {
1605 final HashMap<String, WebView> hrefMap =
1606 new HashMap<String, WebView>();
1607 hrefMap.put("webview", webview);
1608 final Message msg = mHandler.obtainMessage(
1609 FOCUS_NODE_HREF,
1610 R.id.open_newtab_context_menu_id,
1611 0, hrefMap);
1612 webview.requestFocusNodeHref(msg);
1613 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001614 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001615 });
1616 } else {
1617 newTabItem.setOnMenuItemClickListener(
1618 new MenuItem.OnMenuItemClickListener() {
1619 @Override
1620 public boolean onMenuItemClick(MenuItem item) {
1621 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001622 openTab(extra, parent,
1623 !mSettings.openInBackground(),
1624 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001625 return true;
1626 }
1627 });
1628 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001630 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1631 menu.setHeaderTitle(navigationUrl);
1632 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1633
1634 if (itemUrl != null) {
1635 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1636 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1637 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1638 @Override
1639 public boolean onMenuItemClick(MenuItem item) {
1640 final Intent intent = new Intent(Controller.this
1641 .getContext(),
1642 AddMyNavigationPage.class);
1643 Bundle bundle = new Bundle();
1644 String url = Uri.decode(navigationUrl);
1645 bundle.putBoolean("isAdding", false);
1646 bundle.putString("url", url);
1647 bundle.putString("name", getNameFromUrl(url));
1648 intent.putExtra("websites", bundle);
1649 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1650 return false;
1651 }
1652 });
1653 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1654 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1655 @Override
1656 public boolean onMenuItemClick(MenuItem item) {
1657 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1658 return false;
1659 }
1660 });
1661 }
1662 } else {
1663 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1664 }
1665 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001666 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1667 break;
1668 }
1669 // otherwise fall through to handle image part
1670 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001671 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1672 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001673 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1674 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001675 shareItem.setOnMenuItemClickListener(
1676 new MenuItem.OnMenuItemClickListener() {
1677 @Override
1678 public boolean onMenuItemClick(MenuItem item) {
1679 sharePage(mActivity, null, extra, null,
1680 null);
1681 return true;
1682 }
1683 }
1684 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001685 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001686 menu.findItem(R.id.view_image_context_menu_id)
1687 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1688 @Override
1689 public boolean onMenuItemClick(MenuItem item) {
1690 openTab(extra, mTabControl.getCurrentTab(), true, true);
1691 return false;
1692 }
1693 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001694 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1695 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1696 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001697 menu.findItem(R.id.set_wallpaper_context_menu_id).
1698 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1699 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001700 break;
1701
1702 default:
1703 Log.w(LOGTAG, "We should not get here.");
1704 break;
1705 }
1706 //update the ui
1707 mUi.onContextMenuCreated(menu);
1708 }
1709
kaiyiz6e5b3e02013-08-19 20:02:01 +08001710 public void startAddMyNavigation(String url) {
1711 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1712 Bundle bundle = new Bundle();
1713 bundle.putBoolean("isAdding", true);
1714 bundle.putString("url", url);
1715 bundle.putString("name", getNameFromUrl(url));
1716 intent.putExtra("websites", bundle);
1717 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1718 }
1719
1720 private void showMyNavigationDeleteDialog(final String itemUrl) {
1721 new AlertDialog.Builder(this.getContext())
1722 .setTitle(R.string.my_navigation_delete_label)
1723 .setIcon(android.R.drawable.ic_dialog_alert)
1724 .setMessage(R.string.my_navigation_delete_msg)
1725 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1726 @Override
1727 public void onClick(DialogInterface dialog, int whichButton) {
1728 deleteMyNavigationItem(itemUrl);
1729 }
1730 })
1731 .setNegativeButton(R.string.cancel, null)
1732 .show();
1733 }
1734
1735 private void deleteMyNavigationItem(final String itemUrl) {
1736 ContentResolver cr = this.getContext().getContentResolver();
1737 Cursor cursor = null;
1738
1739 try {
1740 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1741 new String[] {
1742 MyNavigationUtil.ID
1743 }, "url = ?", new String[] {
1744 itemUrl
1745 }, null);
1746 if (null != cursor && cursor.moveToFirst()) {
1747 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1748 cursor.getLong(0));
1749
1750 ContentValues values = new ContentValues();
1751 values.put(MyNavigationUtil.TITLE, "");
1752 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1753 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1754 ByteArrayOutputStream os = new ByteArrayOutputStream();
1755 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1756 R.raw.my_navigation_add);
1757 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1758 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1759 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1760 cr.update(uri, values, null, null);
1761 } else {
1762 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1763 }
1764 } catch (IllegalStateException e) {
1765 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1766 } finally {
1767 if (null != cursor) {
1768 cursor.close();
1769 }
1770 }
1771
1772 if (getCurrentTopWebView() != null) {
1773 getCurrentTopWebView().reload();
1774 }
1775 }
1776
1777 private String getNameFromUrl(String itemUrl) {
1778 ContentResolver cr = this.getContext().getContentResolver();
1779 Cursor cursor = null;
1780 String name = null;
1781
1782 try {
1783 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1784 new String[] {
1785 MyNavigationUtil.TITLE
1786 }, "url = ?", new String[] {
1787 itemUrl
1788 }, null);
1789 if (null != cursor && cursor.moveToFirst()) {
1790 name = cursor.getString(0);
1791 } else {
1792 Log.e(LOGTAG, "this item does not exist!");
1793 }
1794 } catch (IllegalStateException e) {
1795 Log.e(LOGTAG, "getNameFromUrl", e);
1796 } finally {
1797 if (null != cursor) {
1798 cursor.close();
1799 }
1800 }
1801 return name;
1802 }
1803
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001804 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001805 final ContentResolver cr = mActivity.getContentResolver();
1806 new AsyncTask<Void, Void, Void>() {
1807 @Override
1808 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001809 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1810 if(!isMyNavigationUrl)
1811 return null;
1812
kaiyiz6e5b3e02013-08-19 20:02:01 +08001813 ContentResolver cr = mActivity.getContentResolver();
1814 Cursor cursor = null;
1815 try {
1816 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1817 new String[] {
1818 MyNavigationUtil.ID
1819 }, "url = ?", new String[] {
1820 itemUrl
1821 }, null);
1822 if (null != cursor && cursor.moveToFirst()) {
1823 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001824 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001825
1826 ContentValues values = new ContentValues();
1827 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1828 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1829 cursor.getLong(0));
1830 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1831 cr.update(uri, values, null, null);
1832 os.close();
1833 }
1834 } catch (IllegalStateException e) {
1835 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1836 } catch (IOException e) {
1837 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1838 } finally {
1839 if (null != cursor) {
1840 cursor.close();
1841 }
1842 }
1843 return null;
1844 }
1845 }.execute();
1846 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001847 /**
1848 * As the menu can be open when loading state changes
1849 * we must manually update the state of the stop/reload menu
1850 * item
1851 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001852 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001853 if (menu == null) {
1854 return;
1855 }
1856 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001857 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001858 menu.findItem(R.id.stop_menu_id):
1859 menu.findItem(R.id.reload_menu_id);
1860 if (src != null) {
1861 dest.setIcon(src.getIcon());
1862 dest.setTitle(src.getTitle());
1863 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001864 mActivity.invalidateOptionsMenu();
1865 }
1866
1867 public void invalidateOptionsMenu() {
1868 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001869 }
1870
John Reck9c35b9c2012-05-30 10:08:50 -07001871 @Override
1872 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001873 // Software menu key (toolbar key)
1874 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1875 return true;
1876 }
1877
1878 @Override
1879 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001880 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001881 // hold on to the menu reference here; it is used by the page callbacks
1882 // to update the menu based on loading state
1883 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001884 // Note: setVisible will decide whether an item is visible; while
1885 // setEnabled() will decide whether an item is enabled, which also means
1886 // whether the matching shortcut key will function.
1887 switch (mMenuState) {
1888 case EMPTY_MENU:
1889 if (mCurrentMenuState != mMenuState) {
1890 menu.setGroupVisible(R.id.MAIN_MENU, false);
1891 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1892 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1893 }
1894 break;
1895 default:
1896 if (mCurrentMenuState != mMenuState) {
1897 menu.setGroupVisible(R.id.MAIN_MENU, true);
1898 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1899 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1900 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001901 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001902 break;
1903 }
1904 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001905 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001906
1907 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001908 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1909 EditBookmarksRestriction.getInstance()
1910 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001911 }
1912
1913 private void setMenuItemVisibility(Menu menu, int id,
1914 boolean visibility) {
1915 MenuItem item = menu.findItem(id);
1916 if (item != null) {
1917 item.setVisible(visibility);
1918 }
1919 }
1920
1921 private int lookupBookmark(String title, String url) {
1922 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001923 int count = 0;
1924 Cursor cursor = null;
1925 try {
1926 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1927 BookmarksLoader.PROJECTION,
1928 "title = ? OR url = ?",
1929 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001930 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001931 },
1932 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001933
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001934 if (cursor != null)
1935 count = cursor.getCount();
1936
1937 } catch (IllegalStateException e) {
1938 Log.e(LOGTAG, "lookupBookmark ", e);
1939 } finally {
1940 if (null != cursor) {
1941 cursor.close();
1942 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001943 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001944 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001945 }
1946
1947 private void resetMenuItems(Menu menu) {
1948 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1949 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1950
1951 WebView w = getCurrentTopWebView();
1952 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1953
1954 String title = w.getTitle();
1955 String url = w.getUrl();
Pankaj Garg634bf432015-07-13 09:54:21 -07001956 mCurrentPageBookmarked = (lookupBookmark(title, url) > 0);
1957 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001958 bookmark_icon.setChecked(true);
1959 } else {
1960 bookmark_icon.setChecked(false);
1961 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001962
1963 // update reader mode checkbox
1964 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1965 readerSwitcher.setVisible(false);
1966 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 }
1968
Michael Kolb4bf79712011-07-14 14:18:12 -07001969 @Override
1970 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001971 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001972 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001973 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001974 // Following flag is used to identify schemes for which the LIVE_MENU
1975 // items defined in res/menu/browser.xml should be enabled
1976 boolean isLiveScheme = false;
1977 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001978 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07001979
1980 boolean isDistillable = false;
1981 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001982 resetMenuItems(menu);
1983
Michael Kolb4bf79712011-07-14 14:18:12 -07001984 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001985 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001986 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001987 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001988 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001989 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001990 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07001991
1992 isDistillable = tab.isDistillable();
1993 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07001994 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001995
1996 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1997 forward.setEnabled(canGoForward);
1998
Michael Kolb4bf79712011-07-14 14:18:12 -07001999 // decide whether to show the share link option
2000 PackageManager pm = mActivity.getPackageManager();
2001 Intent send = new Intent(Intent.ACTION_SEND);
2002 send.setType("text/plain");
2003 ResolveInfo ri = pm.resolveActivity(send,
2004 PackageManager.MATCH_DEFAULT_ONLY);
2005 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2006
2007 boolean isNavDump = mSettings.enableNavDump();
2008 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2009 nav.setVisible(isNavDump);
2010 nav.setEnabled(isNavDump);
2011
2012 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002013 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2014 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002015 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002016 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2017 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002018 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002019 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2020 isLive && isLiveScheme && isPageFinished);
2021 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002022 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002023 // history and snapshots item are the members of COMBO menu group,
2024 // so if show history item, only make snapshots item invisible.
2025 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002026
Ze G Riande2a675c22015-06-03 11:15:24 -07002027
2028 // update reader mode checkbox
2029 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2030 // The reader mode checkbox is hidden only
2031 // when the current page is neither distillable nor distilled
2032 readerSwitcher.setVisible(isDistillable || isDistilled);
2033 readerSwitcher.setChecked(isDistilled);
2034
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002035 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002036 }
2037
John Reck9c35b9c2012-05-30 10:08:50 -07002038 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002039 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002040 if (null == getCurrentTopWebView()) {
2041 return false;
2042 }
2043 if (mMenuIsDown) {
2044 // The shortcut action consumes the MENU. Even if it is still down,
2045 // it won't trigger the next shortcut action. In the case of the
2046 // shortcut action triggering a new activity, like Bookmarks, we
2047 // won't get onKeyUp for MENU. So it is important to reset it here.
2048 mMenuIsDown = false;
2049 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002050 if (mUi.onOptionsItemSelected(item)) {
2051 // ui callback handled it
2052 return true;
2053 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002054 switch (item.getItemId()) {
2055 // -- Main menu
2056 case R.id.new_tab_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002057 getCurrentTab().capture();
Michael Kolb8233fac2010-10-26 16:08:53 -07002058 openTabToHomePage();
2059 break;
2060
2061 case R.id.incognito_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002062 getCurrentTab().capture();
Michael Kolb519d2282011-05-09 17:03:19 -07002063 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002064 break;
2065
2066 case R.id.goto_menu_id:
2067 editUrl();
2068 break;
2069
2070 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002071 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2072 break;
2073
2074 case R.id.history_menu_id:
2075 bookmarksOrHistoryPicker(ComboViews.History);
2076 break;
2077
2078 case R.id.snapshots_menu_id:
2079 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 break;
2081
Pankaj Garg49b79252014-11-07 17:33:41 -08002082 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002083 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 break;
2085
2086 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002087 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002088 stopLoading();
2089 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002090 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 getCurrentTopWebView().reload();
2092 }
2093 break;
2094
Michael Kolb8233fac2010-10-26 16:08:53 -07002095 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002096 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002097 break;
2098
2099 case R.id.close_menu_id:
2100 // Close the subwindow if it exists.
2101 if (mTabControl.getCurrentSubWindow() != null) {
2102 dismissSubWindow(mTabControl.getCurrentTab());
2103 break;
2104 }
2105 closeCurrentTab();
2106 break;
2107
kaiyiza8b6dbb2013-07-29 18:11:22 +08002108 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002109 Object[] params = { new String("persist.debug.browsermonkeytest")};
2110 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002111 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002112 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002113 if (ret != null && ret.equals("enable"))
2114 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002115 if (BrowserConfig.getInstance(getContext())
2116 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2117 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002118 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002119 case R.id.homepage_menu_id:
2120 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002121 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002122 break;
2123
2124 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002125 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 break;
2127
2128 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002129 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002130 break;
2131
John Reck2bc80422011-06-30 15:11:49 -07002132 case R.id.save_snapshot_menu_id:
2133 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002134 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002135 createScreenshotAsync(
2136 source.getWebView(),
2137 getDesiredThumbnailWidth(mActivity),
2138 getDesiredThumbnailHeight(mActivity),
2139 new ValueCallback<Bitmap>() {
2140 @Override
2141 public void onReceiveValue(Bitmap bitmap) {
2142 new SaveSnapshotTask(source, bitmap).execute();
2143 }
2144 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002145 break;
2146
Michael Kolb8233fac2010-10-26 16:08:53 -07002147 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002148 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 break;
2150
John Recke1a03a32011-09-14 17:04:16 -07002151 case R.id.snapshot_go_live:
2152 goLive();
2153 return true;
2154
Michael Kolb8233fac2010-10-26 16:08:53 -07002155 case R.id.share_page_menu_id:
2156 Tab currentTab = mTabControl.getCurrentTab();
2157 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002158 return false;
2159 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002160 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002161 break;
2162
2163 case R.id.dump_nav_menu_id:
2164 getCurrentTopWebView().debugDump();
2165 break;
2166
Michael Kolb8233fac2010-10-26 16:08:53 -07002167 case R.id.zoom_in_menu_id:
2168 getCurrentTopWebView().zoomIn();
2169 break;
2170
2171 case R.id.zoom_out_menu_id:
2172 getCurrentTopWebView().zoomOut();
2173 break;
2174
2175 case R.id.view_downloads_menu_id:
2176 viewDownloads();
2177 break;
2178
John Reck42229bc2011-08-19 13:26:43 -07002179 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002180 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002181 break;
2182
Ze G Riande2a675c22015-06-03 11:15:24 -07002183 case R.id.reader_mode_menu_id:
2184 toggleReaderMode();
2185 break;
2186
Michael Kolb8233fac2010-10-26 16:08:53 -07002187 case R.id.window_one_menu_id:
2188 case R.id.window_two_menu_id:
2189 case R.id.window_three_menu_id:
2190 case R.id.window_four_menu_id:
2191 case R.id.window_five_menu_id:
2192 case R.id.window_six_menu_id:
2193 case R.id.window_seven_menu_id:
2194 case R.id.window_eight_menu_id:
2195 {
2196 int menuid = item.getItemId();
2197 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2198 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2199 Tab desiredTab = mTabControl.getTab(id);
2200 if (desiredTab != null &&
2201 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002202 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002203 }
2204 break;
2205 }
2206 }
2207 }
2208 break;
2209
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002210 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002211 Bundle bundle = new Bundle();
2212 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2213 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2214 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2215 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2216 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002217 break;
2218
Pankaj Garg49b79252014-11-07 17:33:41 -08002219 case R.id.add_to_homescreen:
2220 final WebView w = getCurrentTopWebView();
2221 final EditText input = new EditText(getContext());
2222 input.setText(w.getTitle());
2223 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002224 .setTitle(getContext().getResources().getString(
2225 R.string.add_to_homescreen))
2226 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002227 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002228 .setPositiveButton(getContext().getResources().getString(
2229 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002230 public void onClick(DialogInterface dialog, int whichButton) {
2231 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2232 getContext(),
2233 w.getUrl(),
2234 input.getText().toString(),
2235 w.getViewportBitmap(),
2236 w.getFavicon()));
2237
2238 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2239 .addCategory(Intent.CATEGORY_HOME));
2240 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002241 .setNegativeButton(getContext().getResources().getString(
2242 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002243 public void onClick(DialogInterface dialog, int whichButton) {
2244 // Do nothing.
2245 }
2246 })
2247 .show();
2248 break;
2249
Michael Kolb8233fac2010-10-26 16:08:53 -07002250 default:
2251 return false;
2252 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002253 return true;
2254 }
2255
John Reck68234a92012-04-19 15:27:12 -07002256 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2257 implements OnCancelListener {
2258
2259 private Tab mTab;
2260 private Dialog mProgressDialog;
2261 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002262 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002263
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002264 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002265 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002266 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002267 }
2268
2269 @Override
2270 protected void onPreExecute() {
2271 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2272 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2273 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002274 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002275 }
2276
2277 @Override
2278 protected Long doInBackground(Void... params) {
2279 if (!mTab.saveViewState(mValues)) {
2280 return null;
2281 }
2282 if (isCancelled()) {
2283 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2284 File file = mActivity.getFileStreamPath(path);
2285 if (!file.delete()) {
2286 file.deleteOnExit();
2287 }
2288 return null;
2289 }
2290 final ContentResolver cr = mActivity.getContentResolver();
2291 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2292 if (result == null) {
2293 return null;
2294 }
2295 long id = ContentUris.parseId(result);
2296 return id;
2297 }
2298
2299 @Override
2300 protected void onPostExecute(Long id) {
2301 if (isCancelled()) {
2302 return;
2303 }
2304 mProgressDialog.dismiss();
2305 if (id == null) {
2306 Toast.makeText(mActivity, R.string.snapshot_failed,
2307 Toast.LENGTH_SHORT).show();
2308 return;
2309 }
2310 Bundle b = new Bundle();
2311 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2312 mUi.showComboView(ComboViews.Snapshots, b);
2313 }
2314
2315 @Override
2316 public void onCancel(DialogInterface dialog) {
2317 cancel(true);
2318 }
2319 }
2320
Michael Kolb80f75082012-04-10 10:50:06 -07002321 @Override
2322 public void toggleUserAgent() {
2323 WebView web = getCurrentWebView();
2324 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002325 }
2326
Ze G Riande2a675c22015-06-03 11:15:24 -07002327 // This function calls the method in the webview to enable/disable
2328 // the reader mode through the DOM distiller
2329 public void toggleReaderMode() {
2330 Tab t = mTabControl.getCurrentTab();
2331 if (t.isDistilled()) {
2332 closeTab(t);
2333 } else if (t.isDistillable()) {
2334 openTab(t.getDistilledUrl(), false, true, false, t);
2335 }
2336 }
2337
Michael Kolb80f75082012-04-10 10:50:06 -07002338 @Override
2339 public void findOnPage() {
2340 getCurrentTopWebView().showFindDialog(null, true);
2341 }
2342
2343 @Override
2344 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002345 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002346 }
2347
2348 @Override
2349 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002350 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002351 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2352 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002353 }
2354 else {
2355 WebView w = getCurrentTopWebView();
2356 if (w == null)
2357 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002358 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002359 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002360 }
Michael Kolb80f75082012-04-10 10:50:06 -07002361 }
2362
John Recke1a03a32011-09-14 17:04:16 -07002363 private void goLive() {
Axesh R. Ajmera89cf4d82015-05-19 11:26:18 -07002364 if (!getCurrentTab().isSnapshot()) return;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002365 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002366 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002367 boolean onlySingleTabRemaining = false;
2368 if (mTabControl.getTabCount() > 1) {
2369 // destroy the old snapshot tab
2370 closeCurrentTab();
2371 } else {
2372 onlySingleTabRemaining = true;
2373 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002374 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002375 if (onlySingleTabRemaining) {
2376 closeTab(t);
2377 }
2378
Tarun Nainani8eb00912014-07-17 12:28:32 -07002379 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002380 }
2381
luxiaolb40014b2013-07-19 10:01:43 +08002382 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002383 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002384 new AlertDialog.Builder(activity)
2385 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002386 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002387 .setMessage(R.string.exit_browser_msg)
2388 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2389 public void onClick(DialogInterface dialog, int which) {
2390 activity.moveTaskToBack(true);
2391 dialog.dismiss();
2392 }
2393 })
2394 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2395 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002396 mCrashRecoveryHandler.clearState(true);
2397 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002398 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002399 dialog.dismiss();
2400 }
2401 })
2402 .show();
2403 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002404
Michael Kolb315d5022011-10-13 12:47:11 -07002405 @Override
2406 public void showPageInfo() {
2407 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2408 }
2409
John Reck9c35b9c2012-05-30 10:08:50 -07002410 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002411 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002412 // Let the History and Bookmark fragments handle menus they created.
2413 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2414 return false;
2415 }
2416
Michael Kolb8233fac2010-10-26 16:08:53 -07002417 int id = item.getItemId();
2418 boolean result = true;
2419 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002420 // -- Browser context menu
2421 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002423 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002424 case R.id.copy_link_context_menu_id:
2425 final WebView webView = getCurrentTopWebView();
2426 if (null == webView) {
2427 result = false;
2428 break;
2429 }
2430 final HashMap<String, WebView> hrefMap =
2431 new HashMap<String, WebView>();
2432 hrefMap.put("webview", webView);
2433 final Message msg = mHandler.obtainMessage(
2434 FOCUS_NODE_HREF, id, 0, hrefMap);
2435 webView.requestFocusNodeHref(msg);
2436 break;
2437
2438 default:
2439 // For other context menus
2440 result = onOptionsItemSelected(item);
2441 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002442 return result;
2443 }
2444
2445 /**
2446 * support programmatically opening the context menu
2447 */
2448 public void openContextMenu(View view) {
2449 mActivity.openContextMenu(view);
2450 }
2451
2452 /**
2453 * programmatically open the options menu
2454 */
2455 public void openOptionsMenu() {
2456 mActivity.openOptionsMenu();
2457 }
2458
John Reck9c35b9c2012-05-30 10:08:50 -07002459 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002460 public boolean onMenuOpened(int featureId, Menu menu) {
2461 if (mOptionsMenuOpen) {
2462 if (mConfigChanged) {
2463 // We do not need to make any changes to the state of the
2464 // title bar, since the only thing that happened was a
2465 // change in orientation
2466 mConfigChanged = false;
2467 } else {
2468 if (!mExtendedMenuOpen) {
2469 mExtendedMenuOpen = true;
2470 mUi.onExtendedMenuOpened();
2471 } else {
2472 // Switching the menu back to icon view, so show the
2473 // title bar once again.
2474 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002475 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002476 }
2477 }
2478 } else {
2479 // The options menu is closed, so open it, and show the title
2480 mOptionsMenuOpen = true;
2481 mConfigChanged = false;
2482 mExtendedMenuOpen = false;
2483 mUi.onOptionsMenuOpened();
2484 }
2485 return true;
2486 }
2487
John Reck9c35b9c2012-05-30 10:08:50 -07002488 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002489 public void onOptionsMenuClosed(Menu menu) {
2490 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002491 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002492 }
2493
John Reck9c35b9c2012-05-30 10:08:50 -07002494 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002495 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002496 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 }
2498
2499 // Helper method for getting the top window.
2500 @Override
2501 public WebView getCurrentTopWebView() {
2502 return mTabControl.getCurrentTopWebView();
2503 }
2504
2505 @Override
2506 public WebView getCurrentWebView() {
2507 return mTabControl.getCurrentWebView();
2508 }
2509
2510 /*
2511 * This method is called as a result of the user selecting the options
2512 * menu to see the download window. It shows the download window on top of
2513 * the current window.
2514 */
2515 void viewDownloads() {
2516 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2517 mActivity.startActivity(intent);
2518 }
2519
John Reck30b065e2011-07-19 10:58:05 -07002520 int getActionModeHeight() {
2521 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2522 new int[] { android.R.attr.actionBarSize });
2523 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2524 actionBarSizeTypedArray.recycle();
2525 return size;
2526 }
2527
Michael Kolb8233fac2010-10-26 16:08:53 -07002528 // action mode
2529
John Reck9c35b9c2012-05-30 10:08:50 -07002530 @Override
2531 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 mUi.onActionModeStarted(mode);
2533 mActionMode = mode;
2534 }
2535
2536 /*
2537 * True if a custom ActionMode (i.e. find or select) is in use.
2538 */
2539 @Override
2540 public boolean isInCustomActionMode() {
2541 return mActionMode != null;
2542 }
2543
2544 /*
2545 * End the current ActionMode.
2546 */
2547 @Override
2548 public void endActionMode() {
2549 if (mActionMode != null) {
2550 mActionMode.finish();
2551 }
2552 }
2553
2554 /*
2555 * Called by find and select when they are finished. Replace title bars
2556 * as necessary.
2557 */
John Reck9c35b9c2012-05-30 10:08:50 -07002558 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002559 public void onActionModeFinished(ActionMode mode) {
2560 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002561 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002562 mActionMode = null;
2563 }
2564
2565 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002566 final Tab tab = getCurrentTab();
2567 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002568 }
2569
2570 // bookmark handling
2571
2572 /**
2573 * add the current page as a bookmark to the given folder id
2574 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002575 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002576 * bookmarked, and if it is, edit that bookmark. If false, and
2577 * the site is already bookmarked, do not attempt to edit the
2578 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002579 */
2580 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002581 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002582 WebView w = getCurrentTopWebView();
2583 if (w == null) {
2584 return null;
2585 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002586 Intent i = new Intent(mActivity,
2587 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2589 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002590 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002591 if (touchIconUrl != null) {
2592 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2593 WebSettings settings = w.getSettings();
2594 if (settings != null) {
2595 i.putExtra(AddBookmarkPage.USER_AGENT,
2596 settings.getUserAgentString());
2597 }
2598 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002599 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002600 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002601 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002602 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2603 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002604 // Put the dialog at the upper right of the screen, covering the
2605 // star on the title bar.
2606 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002607 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 }
2609
2610 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002611 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002612 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002613 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002614 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002615 }
2616
Vivek Sekharb54614f2014-05-01 19:03:37 -07002617 @Override
2618 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2619 boolean capture) {
2620 mUploadHandler = new UploadHandler(this);
2621 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2622 }
2623
Michael Kolb8233fac2010-10-26 16:08:53 -07002624 // thumbnails
2625
2626 /**
2627 * Return the desired width for thumbnail screenshots, which are stored in
2628 * the database, and used on the bookmarks screen.
2629 * @param context Context for finding out the density of the screen.
2630 * @return desired width for thumbnail screenshot.
2631 */
2632 static int getDesiredThumbnailWidth(Context context) {
2633 return context.getResources().getDimensionPixelOffset(
2634 R.dimen.bookmarkThumbnailWidth);
2635 }
2636
2637 /**
2638 * Return the desired height for thumbnail screenshots, which are stored in
2639 * the database, and used on the bookmarks screen.
2640 * @param context Context for finding out the density of the screen.
2641 * @return desired height for thumbnail screenshot.
2642 */
2643 static int getDesiredThumbnailHeight(Context context) {
2644 return context.getResources().getDimensionPixelOffset(
2645 R.dimen.bookmarkThumbnailHeight);
2646 }
2647
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002648 static void createScreenshotAsync(WebView view, int width, int height,
2649 final ValueCallback<Bitmap> cb) {
2650 if (view == null || width == 0 || height == 0) {
2651 return;
2652 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002653 view.getContentBitmapAsync(
2654 (float) width / view.getWidth(),
2655 new Rect(),
2656 new ValueCallback<Bitmap>() {
2657 @Override
2658 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002659 if (bitmap != null)
2660 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2661 cb.onReceiveValue(bitmap);
2662 }});
2663 }
2664
Michael Kolb8233fac2010-10-26 16:08:53 -07002665 private class Copy implements OnMenuItemClickListener {
2666 private CharSequence mText;
2667
John Reck9c35b9c2012-05-30 10:08:50 -07002668 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002669 public boolean onMenuItemClick(MenuItem item) {
2670 copy(mText);
2671 return true;
2672 }
2673
2674 public Copy(CharSequence toCopy) {
2675 mText = toCopy;
2676 }
2677 }
2678
Leon Scroggins63c02662010-11-18 15:16:27 -05002679 private static class Download implements OnMenuItemClickListener {
2680 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002681 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002682 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002683 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002684 private static final String FALLBACK_EXTENSION = "dat";
2685 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002686
John Reck9c35b9c2012-05-30 10:08:50 -07002687 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002688 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002689 if (DataUri.isDataUri(mText)) {
2690 saveDataUri();
2691 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002692 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002693 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002694 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002695 return true;
2696 }
2697
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002698 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2699 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002700 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002701 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002702 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002703 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002704 }
George Mount387d45d2011-10-07 15:57:53 -07002705
2706 /**
2707 * Treats mText as a data URI and writes its contents to a file
2708 * based on the current time.
2709 */
2710 private void saveDataUri() {
2711 FileOutputStream outputStream = null;
2712 try {
2713 DataUri uri = new DataUri(mText);
2714 File target = getTarget(uri);
2715 outputStream = new FileOutputStream(target);
2716 outputStream.write(uri.getData());
2717 final DownloadManager manager =
2718 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2719 manager.addCompletedDownload(target.getName(),
2720 mActivity.getTitle().toString(), false,
2721 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002722 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002723 } catch (IOException e) {
2724 Log.e(LOGTAG, "Could not save data URL");
2725 } finally {
2726 if (outputStream != null) {
2727 try {
2728 outputStream.close();
2729 } catch (IOException e) {
2730 // ignore close errors
2731 }
2732 }
2733 }
2734 }
2735
2736 /**
2737 * Creates a File based on the current time stamp and uses
2738 * the mime type of the DataUri to get the extension.
2739 */
2740 private File getTarget(DataUri uri) throws IOException {
2741 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002742 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002743 String nameBase = format.format(new Date());
2744 String mimeType = uri.getMimeType();
2745 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2746 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2747 if (extension == null) {
2748 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2749 extension = FALLBACK_EXTENSION;
2750 }
2751 extension = "." + extension; // createTempFile needs the '.'
2752 File targetFile = File.createTempFile(nameBase, extension, dir);
2753 return targetFile;
2754 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002755 }
2756
Cary Clark8974d282010-11-22 10:46:05 -05002757 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002758 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002759
John Reck9c35b9c2012-05-30 10:08:50 -07002760 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002761 public boolean onMenuItemClick(MenuItem item) {
2762 if (mWebView != null) {
2763 return mWebView.selectText();
2764 }
2765 return false;
2766 }
2767
2768 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002769 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002770 }
2771
2772 }
2773
Michael Kolb8233fac2010-10-26 16:08:53 -07002774 /********************** TODO: UI stuff *****************************/
2775
2776 // these methods have been copied, they still need to be cleaned up
2777
2778 /****************** tabs ***************************************************/
2779
2780 // basic tab interactions:
2781
2782 // it is assumed that tabcontrol already knows about the tab
2783 protected void addTab(Tab tab) {
2784 mUi.addTab(tab);
2785 }
2786
2787 protected void removeTab(Tab tab) {
2788 mUi.removeTab(tab);
2789 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002790 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002791 }
2792
Michael Kolbf2055602011-04-09 17:20:03 -07002793 @Override
2794 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002795 // monkey protection against delayed start
2796 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002797
2798 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2799 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002800 mTabControl.setCurrentTab(tab);
2801 // the tab is guaranteed to have a webview after setCurrentTab
2802 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002803
2804
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002805 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002806 //Purge active tabs
2807 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002808 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002809 }
2810
John Reck8bcafc12011-08-29 16:43:02 -07002811 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002812 Tab current = mTabControl.getCurrentTab();
2813 if (current != null
2814 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002815 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002816 }
2817 }
2818
John Reck26b18322011-06-21 13:08:58 -07002819 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002820 //Cancel navscreen request
2821 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002822 // Dismiss the subwindow if applicable.
2823 dismissSubWindow(appTab);
2824 // Since we might kill the WebView, remove it from the
2825 // content view first.
2826 mUi.detachTab(appTab);
2827 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002828 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002829 // TODO: analyze why the remove and add are necessary
2830 mUi.attachTab(appTab);
2831 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002832 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002833 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002834 } else {
2835 // If the tab was the current tab, we have to attach
2836 // it to the view system again.
2837 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002838 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002839 }
2840 }
2841
2842 // Remove the sub window if it exists. Also called by TabControl when the
2843 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002844 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002845 public void dismissSubWindow(Tab tab) {
2846 removeSubWindow(tab);
2847 // dismiss the subwindow. This will destroy the WebView.
2848 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002849 WebView wv = getCurrentTopWebView();
2850 if (wv != null) {
2851 wv.requestFocus();
2852 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002853 }
2854
2855 @Override
2856 public void removeSubWindow(Tab t) {
2857 if (t.getSubWebView() != null) {
2858 mUi.removeSubWindow(t.getSubViewContainer());
2859 }
2860 }
2861
2862 @Override
2863 public void attachSubWindow(Tab tab) {
2864 if (tab.getSubWebView() != null) {
2865 mUi.attachSubWindow(tab.getSubViewContainer());
2866 getCurrentTopWebView().requestFocus();
2867 }
2868 }
2869
Mathew Inwood29721c22011-06-29 17:55:24 +01002870 private Tab showPreloadedTab(final UrlData urlData) {
2871 if (!urlData.isPreloaded()) {
2872 return null;
2873 }
2874 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2875 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2876 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002877 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002878 // Could not submit query. Fallback to regular tab creation
2879 tabControl.destroy();
2880 return null;
2881 }
2882 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002883 // check tab count and make room for new tab
2884 if (!mTabControl.canCreateNewTab()) {
2885 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2886 if (leastUsed != null) {
2887 closeTab(leastUsed);
2888 }
2889 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002890 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002891 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002892 mTabControl.addPreloadedTab(t);
2893 addTab(t);
2894 setActiveTab(t);
2895 return t;
2896 }
2897
Michael Kolb7bcafde2011-05-09 13:55:59 -07002898 // open a non inconito tab with the given url data
2899 // and set as active tab
2900 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002901 Tab tab = showPreloadedTab(urlData);
2902 if (tab == null) {
2903 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002904 if ((tab != null) && !urlData.isEmpty()) {
2905 loadUrlDataIn(tab, urlData);
2906 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002907 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002908 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002909 }
2910
Michael Kolb843510f2010-12-09 10:51:49 -08002911 @Override
2912 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002913 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002914 }
2915
Michael Kolb8233fac2010-10-26 16:08:53 -07002916 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002917 public Tab openIncognitoTab() {
2918 return openTab(INCOGNITO_URI, true, true, false);
2919 }
2920
2921 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002922 public Tab openTab(String url, boolean incognito, boolean setActive,
2923 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002924 return openTab(url, incognito, setActive, useCurrent, null);
2925 }
2926
2927 @Override
2928 public Tab openTab(String url, Tab parent, boolean setActive,
2929 boolean useCurrent) {
2930 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2931 setActive, useCurrent, parent);
2932 }
2933
2934 public Tab openTab(String url, boolean incognito, boolean setActive,
2935 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002936 Tab tab = createNewTab(incognito, setActive, useCurrent);
2937 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002938 if (parent instanceof SnapshotTab) {
2939 addTab(tab);
2940 if (setActive)
2941 setActiveTab(tab);
2942 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002943 parent.addChildTab(tab);
2944 }
Michael Kolb519d2282011-05-09 17:03:19 -07002945 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002946 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002947 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002948 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002949 return tab;
2950 }
2951
2952 // this method will attempt to create a new tab
2953 // incognito: private browsing tab
2954 // setActive: ste tab as current tab
2955 // useCurrent: if no new tab can be created, return current tab
2956 private Tab createNewTab(boolean incognito, boolean setActive,
2957 boolean useCurrent) {
2958 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002959 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2960 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002961 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002962 if (mTabControl.canCreateNewTab()) {
2963 tab = mTabControl.createNewTab(incognito, !setActive);
2964 addTab(tab);
2965 if (setActive) {
2966 setActiveTab(tab);
2967 } else {
2968 tab.pause();
2969 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002970 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002971 if (useCurrent) {
2972 tab = mTabControl.getCurrentTab();
2973 reuseTab(tab, null);
2974 } else {
2975 mUi.showMaxTabsWarning();
2976 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002977 }
2978 }
2979 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002980 }
2981
John Reck2bc80422011-06-30 15:11:49 -07002982 @Override
2983 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2984 SnapshotTab tab = null;
2985 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07002986 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07002987 addTab(tab);
2988 if (setActive) {
2989 setActiveTab(tab);
2990 }
2991 } else {
2992 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002993 }
2994 return tab;
2995 }
2996
Michael Kolb8233fac2010-10-26 16:08:53 -07002997 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002998 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002999 * @return boolean True if we successfully switched to a different tab. If
3000 * the indexth tab is null, or if that tab is the same as
3001 * the current one, return false.
3002 */
3003 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003004 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003005 Tab currentTab = mTabControl.getCurrentTab();
3006 if (tab == null || tab == currentTab) {
3007 return false;
3008 }
3009 setActiveTab(tab);
3010 return true;
3011 }
3012
3013 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003014 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003015 closeCurrentTab(false);
3016 }
3017
3018 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003019 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003020 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003021 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003022 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003023 return;
3024 }
Michael Kolbc831b632011-05-11 09:30:34 -07003025 final Tab current = mTabControl.getCurrentTab();
3026 final int pos = mTabControl.getCurrentPosition();
3027 Tab newTab = current.getParent();
3028 if (newTab == null) {
3029 newTab = mTabControl.getTab(pos + 1);
3030 if (newTab == null) {
3031 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003032 }
3033 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003034 if (andQuit) {
3035 mTabControl.setCurrentTab(newTab);
3036 closeTab(current);
3037 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003038 // Close window
3039 closeTab(current);
3040 }
3041 }
3042
3043 /**
3044 * Close the tab, remove its associated title bar, and adjust mTabControl's
3045 * current tab to a valid value.
3046 */
3047 @Override
3048 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003049 if (tab == mTabControl.getCurrentTab()) {
3050 closeCurrentTab();
3051 } else {
3052 removeTab(tab);
3053 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003054 }
3055
Afzal Najamd4e33312012-04-26 01:54:01 -04003056 /**
3057 * Close all tabs except the current one
3058 */
3059 @Override
3060 public void closeOtherTabs() {
3061 int inactiveTabs = mTabControl.getTabCount() - 1;
3062 for (int i = inactiveTabs; i >= 0; i--) {
3063 Tab tab = mTabControl.getTab(i);
3064 if (tab != mTabControl.getCurrentTab()) {
3065 removeTab(tab);
3066 }
3067 }
3068 }
3069
Michael Kolb8233fac2010-10-26 16:08:53 -07003070 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003071 protected void loadUrlFromContext(String url) {
3072 Tab tab = getCurrentTab();
3073 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003074 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003075 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003076 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003077 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003078 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003079 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003080 }
3081 }
3082 }
3083
3084 /**
3085 * Load the URL into the given WebView and update the title bar
3086 * to reflect the new load. Call this instead of WebView.loadUrl
3087 * directly.
3088 * @param view The WebView used to load url.
3089 * @param url The URL to load.
3090 */
John Reck71e51422011-07-01 16:49:28 -07003091 @Override
3092 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003093 loadUrl(tab, url, null);
3094 }
3095
3096 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3097 if (tab != null) {
3098 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003099 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003100 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003101 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003102 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003103 }
3104
3105 /**
3106 * Load UrlData into a Tab and update the title bar to reflect the new
3107 * load. Call this instead of UrlData.loadIn directly.
3108 * @param t The Tab used to load.
3109 * @param data The UrlData being loaded.
3110 */
3111 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003112 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003113 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003114 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003115 } else {
John Reck38b39652012-06-05 09:22:59 -07003116 if (t != null && data.mDisableUrlOverride) {
3117 t.disableUrlOverridingForLoad();
3118 }
John Reck26b18322011-06-21 13:08:58 -07003119 loadUrl(t, data.mUrl, data.mHeaders);
3120 }
3121 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003122 }
3123
John Reck30c714c2010-12-16 17:30:34 -08003124 @Override
3125 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003126 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003127 //In case of tab can go back (aka tab has navigation entry) do nothing
3128 //else just load homepage in current tab.
3129 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003130 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003131 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003132 }
3133
3134 void goBackOnePageOrQuit() {
3135 Tab current = mTabControl.getCurrentTab();
3136 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003137 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3138 showExitDialog(mActivity);
3139 } else {
3140 /*
3141 * Instead of finishing the activity, simply push this to the back
3142 * of the stack and let ActivityManager to choose the foreground
3143 * activity. As BrowserActivity is singleTask, it will be always the
3144 * root of the task. So we can use either true or false for
3145 * moveTaskToBack().
3146 */
3147 mActivity.moveTaskToBack(true);
3148 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003149 return;
3150 }
John Reckef654f12011-07-12 16:42:08 -07003151 if (current.canGoBack()) {
3152 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003153 } else {
3154 // Check to see if we are closing a window that was created by
3155 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003156 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003158 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003159 // Now we close the other tab
3160 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003161 } else if (BrowserConfig.getInstance(getContext())
3162 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3163 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003164 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003165 /*
3166 * Instead of finishing the activity, simply push this to the back
3167 * of the stack and let ActivityManager to choose the foreground
3168 * activity. As BrowserActivity is singleTask, it will be always the
3169 * root of the task. So we can use either true or false for
3170 * moveTaskToBack().
3171 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003172 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003173 }
3174 }
3175 }
3176
3177 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003178 * helper method for key handler
3179 * returns the current tab if it can't advance
3180 */
Michael Kolbc831b632011-05-11 09:30:34 -07003181 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003182 int pos = mTabControl.getCurrentPosition() + 1;
3183 if (pos >= mTabControl.getTabCount()) {
3184 pos = 0;
3185 }
3186 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003187 }
3188
3189 /**
3190 * helper method for key handler
3191 * returns the current tab if it can't advance
3192 */
Michael Kolbc831b632011-05-11 09:30:34 -07003193 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003194 int pos = mTabControl.getCurrentPosition() - 1;
3195 if ( pos < 0) {
3196 pos = mTabControl.getTabCount() - 1;
3197 }
3198 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003199 }
3200
John Reckbcef87f2012-02-03 14:58:34 -08003201 boolean isMenuOrCtrlKey(int keyCode) {
3202 return (KeyEvent.KEYCODE_MENU == keyCode)
3203 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3204 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3205 }
3206
Michael Kolb0035fad2011-03-14 13:25:28 -07003207 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003208 * handle key events in browser
3209 *
3210 * @param keyCode
3211 * @param event
3212 * @return true if handled, false to pass to super
3213 */
John Reck9c35b9c2012-05-30 10:08:50 -07003214 @Override
3215 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003216 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3217 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003218 if (!mUi.isComboViewShowing()) {
3219 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3220 true, false);
3221 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003222 return true;
3223 }
3224
Cary Clark160bbb92011-01-10 11:17:07 -05003225 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003226 // Even if MENU is already held down, we need to call to super to open
3227 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003228 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003229 mMenuIsDown = true;
3230 return false;
3231 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003232
Cary Clark8ff8c662010-12-29 15:03:05 -05003233 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003234 Tab tab = getCurrentTab();
3235 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003236
Cary Clark160bbb92011-01-10 11:17:07 -05003237 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3238 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003239
Michael Kolb8233fac2010-10-26 16:08:53 -07003240 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003241 case KeyEvent.KEYCODE_TAB:
3242 if (event.isCtrlPressed()) {
3243 if (event.isShiftPressed()) {
3244 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003245 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003246 } else {
3247 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003248 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003249 }
3250 return true;
3251 }
3252 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003253 case KeyEvent.KEYCODE_SPACE:
3254 // WebView/WebTextView handle the keys in the KeyDown. As
3255 // the Activity's shortcut keys are only handled when WebView
3256 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003257 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003258 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003259 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003260 pageDown();
3261 }
3262 return true;
3263 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003264 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003265 event.startTracking();
3266 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003267 case KeyEvent.KEYCODE_FORWARD:
3268 if (!noModifiers) break;
3269 tab.goForward();
3270 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003271 case KeyEvent.KEYCODE_DPAD_LEFT:
3272 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003273 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003274 return true;
3275 }
3276 break;
3277 case KeyEvent.KEYCODE_DPAD_RIGHT:
3278 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003279 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003280 return true;
3281 }
3282 break;
3283 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003284 if (ctrl) {
3285 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003286 return true;
3287 }
3288 break;
Michael Kolba4183062011-01-16 10:43:21 -08003289// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003290 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003291 if (ctrl ) {
3292 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003293 return true;
3294 }
3295 break;
Michael Kolba4183062011-01-16 10:43:21 -08003296// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003297// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003298// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003299// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003300// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003301// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003302// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003303// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003304// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003305// case KeyEvent.KEYCODE_M: // unused
3306// case KeyEvent.KEYCODE_N: // in Chrome: new window
3307// case KeyEvent.KEYCODE_O: // in Chrome: open file
3308// case KeyEvent.KEYCODE_P: // in Chrome: print page
3309// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003310// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003311// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3312 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003313 // we can't use the ctrl/shift flags, they check for
3314 // exclusive use of a modifier
3315 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003316 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003317 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003318 } else {
3319 openTabToHomePage();
3320 }
3321 return true;
3322 }
3323 break;
3324// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3325// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003326// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003327// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3328// case KeyEvent.KEYCODE_Y: // unused
3329// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003330 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003331 // it is a regular key and webview is not null
3332 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003333 }
3334
John Reck9c35b9c2012-05-30 10:08:50 -07003335 @Override
3336 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003337 switch(keyCode) {
3338 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003339 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003340 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003341 return true;
3342 }
3343 break;
3344 }
3345 return false;
3346 }
3347
John Reck9c35b9c2012-05-30 10:08:50 -07003348 @Override
3349 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003350 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003351 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003352 if (KeyEvent.KEYCODE_MENU == keyCode
3353 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003354 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003355 }
3356 }
Cary Clark160bbb92011-01-10 11:17:07 -05003357 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003358 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003359 case KeyEvent.KEYCODE_BACK:
3360 if (event.isTracking() && !event.isCanceled()) {
3361 onBackKey();
3362 return true;
3363 }
3364 break;
3365 }
3366 return false;
3367 }
3368
3369 public boolean isMenuDown() {
3370 return mMenuIsDown;
3371 }
3372
John Reck9c35b9c2012-05-30 10:08:50 -07003373 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003374 public void setupAutoFill(Message message) {
3375 // Open the settings activity at the AutoFill profile fragment so that
3376 // the user can create a new profile. When they return, we will dispatch
3377 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003378 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003379 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3380 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003381 }
John Reckb3417f02011-01-14 11:01:05 -08003382
John Reck9c35b9c2012-05-30 10:08:50 -07003383 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003384 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003385 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003386 return true;
3387 }
3388
John Reck1cf4b792011-07-26 10:22:22 -07003389 @Override
3390 public boolean shouldCaptureThumbnails() {
3391 return mUi.shouldCaptureThumbnails();
3392 }
3393
Michael Kolbc3af0672011-08-09 10:24:41 -07003394 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003395 public boolean supportsVoice() {
3396 PackageManager pm = mActivity.getPackageManager();
3397 List activities = pm.queryIntentActivities(new Intent(
3398 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3399 return activities.size() != 0;
3400 }
3401
3402 @Override
3403 public void startVoiceRecognizer() {
3404 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003405 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003406 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3407 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3408 mActivity.startActivityForResult(voice, VOICE_RESULT);
3409 }
3410
Pankaj Garg7b279f62014-08-12 14:47:18 -07003411 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003412 if (mLevel == level)
3413 return;
3414 mLevel = level;
3415 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003416 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3417 lp.dimAmount = level;
3418 mActivity.getWindow().setAttributes(lp);
3419 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3420 } else {
3421 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3422 }
3423 }
3424
Michael Kolb0b129122012-06-04 16:31:58 -07003425 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003426 public void setBlockEvents(boolean block) {
3427 mBlockEvents = block;
3428 }
3429
John Reck9c35b9c2012-05-30 10:08:50 -07003430 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003431 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003432 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003433 }
3434
John Reck9c35b9c2012-05-30 10:08:50 -07003435 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003436 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003437 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003438 }
3439
John Reck9c35b9c2012-05-30 10:08:50 -07003440 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003441 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003442 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003443 }
3444
John Reck9c35b9c2012-05-30 10:08:50 -07003445 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003446 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003447 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003448 }
3449
John Reck9c35b9c2012-05-30 10:08:50 -07003450 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003451 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003452 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003453 }
3454
Pankaj Garg49b79252014-11-07 17:33:41 -08003455 @Override
3456 public boolean shouldShowAppMenu() {
3457 return true;
3458 }
3459
3460 @Override
3461 public int getMenuThemeResourceId() {
3462 return R.style.OverflowMenuTheme;
3463 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003464}