blob: c3befc8c0ced6b3f81a7343a42962ae0102f5808 [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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080089import org.codeaurora.swe.WebSettings;
90import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070091import org.codeaurora.swe.WebBackForwardList;
92import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080093
Bijan Amirzada41242f22014-03-21 12:12:18 -070094import com.android.browser.IntentHandler.UrlData;
95import com.android.browser.UI.ComboViews;
Dave Tharp13f3f7c2015-07-16 14:58:51 -070096import com.android.browser.mdm.DownloadDirRestriction;
Dave Tharp9a082b12015-06-19 08:46:57 -070097import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -070098import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -070099import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700100import com.android.browser.mynavigation.AddMyNavigationPage;
101import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700102import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700103import com.android.browser.platformsupport.BrowserContract;
Pankaj Garg18902562014-12-05 16:18:51 -0800104import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700105import com.android.browser.provider.BrowserProvider2.Thumbnails;
106import com.android.browser.provider.SnapshotProvider.Snapshots;
107import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800108import com.android.browser.appmenu.AppMenuHandler;
109import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700110
Michael Kolb8233fac2010-10-26 16:08:53 -0700111import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700112import java.io.File;
113import java.io.FileOutputStream;
114import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700116import java.text.DateFormat;
117import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700118import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700119import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700120import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800121import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200122import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700123import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700124
125/**
126 * Controller for browser
127 */
128public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800129 implements WebViewController, UiController, ActivityController,
130 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700131
132 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800133 private static final String SEND_APP_ID_EXTRA =
134 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Axesh R. Ajmera6fc73692015-08-11 16:32:10 -0700135 private static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800136 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
137 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800138
Vivek Sekharb54614f2014-05-01 19:03:37 -0700139 // Remind switch to data connection if wifi is unavailable
140 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800141
Michael Kolb8233fac2010-10-26 16:08:53 -0700142 // public message ids
143 public final static int LOAD_URL = 1001;
144 public final static int STOP_LOAD = 1002;
145
146 // Message Ids
147 private static final int FOCUS_NODE_HREF = 102;
148 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800149 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800152 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700153
154 private static final int EMPTY_MENU = -1;
155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700157 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 final static int PREFERENCES_PAGE = 3;
159 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700161 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800162 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000163
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
165
166 // As the ids are dynamically created, we can't guarantee that they will
167 // be in sequence, so this static array maps ids to a window number.
168 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
169 { R.id.window_one_menu_id, R.id.window_two_menu_id,
170 R.id.window_three_menu_id, R.id.window_four_menu_id,
171 R.id.window_five_menu_id, R.id.window_six_menu_id,
172 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
173
174 // "source" parameter for Google search through search key
175 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
176 // "source" parameter for Google search through simplily type
177 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
178
George Mount387d45d2011-10-07 15:57:53 -0700179 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700180 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
181
John Reckd7dd9b22011-08-30 09:18:29 -0700182 // A bitmap that is re-used in createScreenshot as scratch space
183 private static Bitmap sThumbnailBitmap;
184
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 private Activity mActivity;
186 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700187 private HomepageHandler mHomepageHandler;
188 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private BrowserSettings mSettings;
190 private WebViewFactory mFactory;
191
192 private WakeLock mWakeLock;
193
194 private UrlHandler mUrlHandler;
195 private UploadHandler mUploadHandler;
196 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 private NetworkStateHandler mNetworkHandler;
198
Ben Murdoch8029a772010-11-16 11:58:21 +0000199 private Message mAutoFillSetupMessage;
200
kaiyiza016da12013-08-26 17:50:22 +0800201 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700202
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 // FIXME, temp address onPrepareMenu performance problem.
204 // When we move everything out of view, we should rewrite this.
205 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700206 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 private int mOldMenuState = EMPTY_MENU;
208 private Menu mCachedMenu;
209
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 private boolean mMenuIsDown;
211
Pankaj Garg49b79252014-11-07 17:33:41 -0800212 private boolean mWasInPageLoad = false;
213 private AppMenuHandler mAppMenuHandler;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 // For select and find, we keep track of the ActionMode so that
216 // finish() can be called as desired.
217 private ActionMode mActionMode;
218
219 /**
220 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
221 * of whether the configuration has changed. The first onMenuOpened call
222 * after a configuration change is simply a reopening of the same menu
223 * (i.e. mIconView did not change).
224 */
225 private boolean mConfigChanged;
226
227 /**
228 * Keeps track of whether the options menu is open. This is important in
229 * determining whether to show or hide the title bar overlay
230 */
231 private boolean mOptionsMenuOpen;
232
233 /**
234 * Whether or not the options menu is in its bigger, popup menu form. When
235 * true, we want the title bar overlay to be gone. When false, we do not.
236 * Only meaningful if mOptionsMenuOpen is true.
237 */
238 private boolean mExtendedMenuOpen;
239
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 private boolean mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700241 private boolean mActivityStopped = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 private boolean mLoadStopped;
243
244 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500245 // Checks to see when the bookmarks database has changed, and updates the
246 // Tabs' notion of whether they represent bookmarked sites.
247 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700248 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700249
Michael Kolbc3af0672011-08-09 10:24:41 -0700250 private boolean mBlockEvents;
251
Michael Kolb0b129122012-06-04 16:31:58 -0700252 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800253 private boolean mUpdateMyNavThumbnail;
254 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800255 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800256 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700257 private PowerConnectionReceiver mLowPowerReceiver;
258 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700259
Pankaj Garg634bf432015-07-13 09:54:21 -0700260 private boolean mCurrentPageBookmarked;
261
George Mount3636d0a2011-11-21 09:08:21 -0800262 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 mActivity = browser;
264 mSettings = BrowserSettings.getInstance();
265 mTabControl = new TabControl(this);
266 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700267 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800268 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700269 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700270
271 mUrlHandler = new UrlHandler(this);
272 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700273
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500275 mBookmarksObserver = new ContentObserver(mHandler) {
276 @Override
277 public void onChange(boolean selfChange) {
278 int size = mTabControl.getTabCount();
279 for (int i = 0; i < size; i++) {
280 mTabControl.getTab(i).updateBookmarkedStatus();
281 }
282 }
283
284 };
285 browser.getContentResolver().registerContentObserver(
286 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700287
288 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700289 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800290 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700291 }
292
John Reck9c35b9c2012-05-30 10:08:50 -0700293 @Override
294 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700295 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800296 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800297 // mCrashRecoverHandler has any previously saved state.
298 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700299 }
300
George Mount3636d0a2011-11-21 09:08:21 -0800301 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800302 // we dont want to ever recover incognito tabs
303 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700304
Patrick Scott7d50a932011-02-04 09:27:26 -0500305 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700306 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500307 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000308
Michael Kolbc831b632011-05-11 09:30:34 -0700309 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 // Not able to restore so we go ahead and clear session cookies. We
311 // must do this before trying to login the user as we don't want to
312 // clear any session cookies set during login.
313 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700314 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800315 if (intent == null) {
316 // This won't happen under common scenarios. The icicle is
317 // not null, but there aren't any tabs to restore.
318 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700319 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800320 final Bundle extra = intent.getExtras();
321 // Create an initial tab.
322 // If the intent is ACTION_VIEW and data is not null, the Browser is
323 // invoked to view the content by another application. In this case,
324 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800325 UrlData urlData = null;
326 if (intent.getData() != null
327 && Intent.ACTION_VIEW.equals(intent.getAction())
328 && intent.getData().toString().startsWith("content://")) {
329 urlData = new UrlData(intent.getData().toString());
330 } else {
331 urlData = IntentHandler.getUrlDataFromIntent(intent);
332 }
George Mount3636d0a2011-11-21 09:08:21 -0800333 Tab t = null;
334 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700335 String landingPage = mActivity.getResources().getString(
336 R.string.def_landing_page);
337 if (!landingPage.isEmpty()) {
338 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800339 } else {
340 t = openTabToHomePage();
341 }
George Mount3636d0a2011-11-21 09:08:21 -0800342 } else {
343 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700344 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800345 }
346 if (t != null) {
347 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
348 }
349 WebView webView = t.getWebView();
350 if (extra != null) {
351 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
352 if (scale > 0 && scale <= 1000) {
353 webView.setInitialScale(scale);
354 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 }
356 }
John Reckd8c74522011-06-14 08:45:00 -0700357 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700359 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500360 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700361 List<Tab> tabs = mTabControl.getTabs();
362 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700363
John Reck1cf4b792011-07-26 10:22:22 -0700364 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700365 //handle restored pages that may require a JS interface
366 if (t.getWebView() != null) {
367 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
368 if (backForwardList != null) {
369 for (int i = 0; i < backForwardList.getSize(); i++) {
370 WebHistoryItem item = backForwardList.getItemAtIndex(i);
371 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
372 }
373 }
374 }
John Reck1cf4b792011-07-26 10:22:22 -0700375 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700376 if (t != mTabControl.getCurrentTab()) {
377 t.pause();
378 }
John Reck1cf4b792011-07-26 10:22:22 -0700379 }
380 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700381 if (tabs.size() == 0) {
382 openTabToHomePage();
383 }
John Reck1cf4b792011-07-26 10:22:22 -0700384 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700385 // TabControl.restoreState() will create a new tab even if
386 // restoring the state fails.
387 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800388 // Intent is non-null when framework thinks the browser should be
389 // launching with a new intent (icicle is null).
390 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700391 mIntentHandler.onNewIntent(intent);
392 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700394 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700395 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800396 if (jsFlags.trim().length() != 0) {
397 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700398 }
George Mount3636d0a2011-11-21 09:08:21 -0800399 if (intent != null
400 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700401 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800402 }
Site Maoabb7bd32015-03-20 15:34:02 -0700403 mLowPowerReceiver = new PowerConnectionReceiver();
404 mPowerChangeReceiver = new PowerConnectionReceiver();
405
406 //always track the android framework's power save mode
407 IntentFilter filter = new IntentFilter();
408 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
409 // Power save mode only exists in Lollipop and above
410 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
411 }
412 filter.addAction(Intent.ACTION_BATTERY_OKAY);
413 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700414 }
415
John Reck1cf4b792011-07-26 10:22:22 -0700416 private static class PruneThumbnails implements Runnable {
417 private Context mContext;
418 private List<Long> mIds;
419
420 PruneThumbnails(Context context, List<Long> preserveIds) {
421 mContext = context.getApplicationContext();
422 mIds = preserveIds;
423 }
424
425 @Override
426 public void run() {
427 ContentResolver cr = mContext.getContentResolver();
428 if (mIds == null || mIds.size() == 0) {
429 cr.delete(Thumbnails.CONTENT_URI, null, null);
430 } else {
431 int length = mIds.size();
432 StringBuilder where = new StringBuilder();
433 where.append(Thumbnails._ID);
434 where.append(" not in (");
435 for (int i = 0; i < length; i++) {
436 where.append(mIds.get(i));
437 if (i < (length - 1)) {
438 where.append(",");
439 }
440 }
441 where.append(")");
442 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
443 }
444 }
445
446 }
447
Michael Kolb1514bb72010-11-22 09:11:48 -0800448 @Override
449 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700450 return mFactory;
451 }
452
453 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800454 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700455 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700456 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800457 }
458
459 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800460 public void createSubWindow(Tab tab) {
461 endActionMode();
462 WebView mainView = tab.getWebView();
463 WebView subView = mFactory.createWebView((mainView == null)
464 ? false
465 : mainView.isPrivateBrowsingEnabled());
466 mUi.createSubWindow(tab, subView);
467 }
468
469 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700470 public Context getContext() {
471 return mActivity;
472 }
473
474 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 public Activity getActivity() {
476 return mActivity;
477 }
478
479 void setUi(UI ui) {
480 mUi = ui;
481 }
482
Michael Kolbaf63dba2012-05-16 12:58:05 -0700483 @Override
484 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700485 return mSettings;
486 }
487
488 IntentHandler getIntentHandler() {
489 return mIntentHandler;
490 }
491
492 @Override
493 public UI getUi() {
494 return mUi;
495 }
496
497 int getMaxTabs() {
498 return mActivity.getResources().getInteger(R.integer.max_tabs);
499 }
500
501 @Override
502 public TabControl getTabControl() {
503 return mTabControl;
504 }
505
Michael Kolb1bf23132010-11-19 12:55:12 -0800506 @Override
507 public List<Tab> getTabs() {
508 return mTabControl.getTabs();
509 }
510
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 private void startHandler() {
512 mHandler = new Handler() {
513
514 @Override
515 public void handleMessage(Message msg) {
516 switch (msg.what) {
517 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700518 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800520 case UNKNOWN_TYPE_MSG:
521 HashMap unknownTypeMap = (HashMap) msg.obj;
522 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
523 /*
524 * When the context menu is shown to the user
525 * we need to assure that its happening on the current webview
526 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
527 */
528 if (getCurrentWebView() != viewForUnknownType)
529 break;
530
531 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800532 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800533 WebView.HitTestResult result = new WebView.HitTestResult();
534
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800535 // Prevent unnecessary calls to context menu
536 // if url and image src are null
537 if (unknown_type_src == null && unknown_type_url == null)
538 break;
539
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800540 //setting the HitTestResult with new RESULT TYPE
541 if (!TextUtils.isEmpty(unknown_type_src)) {
542 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
543 result.setExtra(unknown_type_src);
544 } else {
545 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
546 result.setExtra("about:blank");
547 }
548
549 mResult = result;
550 openContextMenu(viewForUnknownType);
551 mResult = null;
552
553 break;
554
Michael Kolb8233fac2010-10-26 16:08:53 -0700555 case FOCUS_NODE_HREF:
556 {
557 String url = (String) msg.getData().get("url");
558 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500559 String src = (String) msg.getData().get("src");
560 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 if (TextUtils.isEmpty(url)) {
562 break;
563 }
564 HashMap focusNodeMap = (HashMap) msg.obj;
565 WebView view = (WebView) focusNodeMap.get("webview");
566 // Only apply the action if the top window did not change.
567 if (getCurrentTopWebView() != view) {
568 break;
569 }
570 switch (msg.arg1) {
571 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700572 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700573 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500574 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700575 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500576 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500577 case R.id.open_newtab_context_menu_id:
578 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700579 openTab(url, parent,
580 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500581 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700582 case R.id.copy_link_context_menu_id:
583 copy(url);
584 break;
585 case R.id.save_link_context_menu_id:
586 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500587 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800588 mActivity, url, view.getSettings().getUserAgentString(),
589 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700591 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700592 if(title == null || title == "")
593 title = url;
594
595 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
596 //SWE TODO: No thumbnail support for the url obtained via
597 //browser context menu as its not loaded in webview.
598 if (bookmarkIntent != null) {
599 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
600 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
601 mActivity.startActivity(bookmarkIntent);
602 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700603 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700604 }
605 break;
606 }
607
608 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700609 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700610 break;
611
612 case STOP_LOAD:
613 stopLoading();
614 break;
615
616 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700617 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 mWakeLock.release();
619 // if we reach here, Browser should be still in the
620 // background loading after WAKELOCK_TIMEOUT (5-min).
621 // To avoid burning the battery, stop loading.
622 mTabControl.stopAllLoading();
623 }
624 break;
625
kaiyiz591110b2013-08-06 17:11:06 +0800626 case OPEN_MENU:
627 if (!mOptionsMenuOpen && mActivity != null ) {
628 mActivity.openOptionsMenu();
629 }
630 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700631 }
632 }
633 };
634
635 }
636
John Reckef654f12011-07-12 16:42:08 -0700637 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200638 public Tab getCurrentTab() {
639 return mTabControl.getCurrentTab();
640 }
641
Michael Kolbba99c5d2010-11-29 14:57:41 -0800642 @Override
643 public void shareCurrentPage() {
644 shareCurrentPage(mTabControl.getCurrentTab());
645 }
646
647 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700648 if (tab == null || tab.getWebView() == null)
649 return;
650
651 final Tab mytab = tab;
652 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
653 @Override
654 public void onReceiveValue(Bitmap bitmap) {
655 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
656 mytab.getFavicon(), bitmap);
657 }
658 };
659
660 createScreenshotAsync(
661 tab.getWebView(),
662 getDesiredThumbnailWidth(mActivity),
663 getDesiredThumbnailHeight(mActivity),
664 new ValueCallback<Bitmap>() {
665 @Override
666 public void onReceiveValue(Bitmap bitmap) {
667 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
668 mytab.getFavicon(), bitmap);
669 }
670 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800671 }
672
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 /**
674 * Share a page, providing the title, url, favicon, and a screenshot. Uses
675 * an {@link Intent} to launch the Activity chooser.
676 * @param c Context used to launch a new Activity.
677 * @param title Title of the page. Stored in the Intent with
678 * {@link Intent#EXTRA_SUBJECT}
679 * @param url URL of the page. Stored in the Intent with
680 * {@link Intent#EXTRA_TEXT}
681 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
682 * with {@link Browser#EXTRA_SHARE_FAVICON}
683 * @param screenshot Bitmap of a screenshot of the page. Stored in the
684 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
685 */
686 static final void sharePage(Context c, String title, String url,
687 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700688
689 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
690 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
691 R.layout.app_row, sDialog.getApps());
692 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700693 }
694
695 private void copy(CharSequence text) {
696 ClipboardManager cm = (ClipboardManager) mActivity
697 .getSystemService(Context.CLIPBOARD_SERVICE);
698 cm.setText(text);
699 }
700
701 // lifecycle
702
John Reck9c35b9c2012-05-30 10:08:50 -0700703 @Override
704 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800706 // update the menu in case of a locale change
707 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800708 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800709 if (mOptionsMenuOpen) {
710 mActivity.closeOptionsMenu();
711 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
712 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 mUi.onConfigurationChanged(config);
714 }
715
716 @Override
717 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700718 if (!mUi.isWebShowing()) {
719 mUi.showWeb(false);
720 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700721 mIntentHandler.onNewIntent(intent);
722 }
723
John Reck9c35b9c2012-05-30 10:08:50 -0700724 @Override
725 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800726 if (mUi.isCustomViewShowing()) {
727 hideCustomView();
728 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700729 if (mActivityPaused) {
730 Log.e(LOGTAG, "BrowserActivity is already paused.");
731 return;
732 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700734
Michael Kolb8233fac2010-10-26 16:08:53 -0700735 }
736
John Reck9c35b9c2012-05-30 10:08:50 -0700737 @Override
738 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800740 Bundle saveState = createSaveState();
741
742 // crash recovery manages all save & restore state
743 mCrashRecoveryHandler.writeState(saveState);
744 mSettings.setLastRunPaused(true);
745 }
746
747 /**
748 * Save the current state to outState. Does not write the state to
749 * disk.
750 * @return Bundle containing the current state of all tabs.
751 */
752 /* package */ Bundle createSaveState() {
753 Bundle saveState = new Bundle();
754 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800755 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 }
757
John Reck9c35b9c2012-05-30 10:08:50 -0700758 @Override
759 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700760 if (!mActivityPaused) {
761 Log.e(LOGTAG, "BrowserActivity is already resumed.");
762 return;
763 }
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700764 mActivityPaused = false;
765 if (mVoiceResult != null) {
766 mUi.onVoiceResult(mVoiceResult);
767 mVoiceResult = null;
768 }
769 }
770
771 private void releaseWakeLock() {
772 if (mWakeLock != null && mWakeLock.isHeld()) {
773 mHandler.removeMessages(RELEASE_WAKELOCK);
774 mWakeLock.release();
775 }
776 }
777
778 @Override
779 public void onStop() {
780 if (mActivityStopped) {
781 Log.e(LOGTAG, "BrowserActivity is already stoped.");
782 return;
783 }
784 mActivityStopped = true;
785 Tab tab = mTabControl.getCurrentTab();
786 if (tab != null) {
787 tab.pause();
788 if (!pauseWebViewTimers(tab)) {
789 if (mWakeLock == null) {
790 PowerManager pm = (PowerManager) mActivity
791 .getSystemService(Context.POWER_SERVICE);
792 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
793 }
794 mWakeLock.acquire();
795 mHandler.sendMessageDelayed(mHandler
796 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
797 }
798 }
799 mUi.onPause();
800 mNetworkHandler.onPause();
801 NfcHandler.unregister(mActivity);
802 mActivity.unregisterReceiver(mLowPowerReceiver);
803 }
804
805 @Override
806 public void onStart() {
807 if (!mActivityStopped) {
808 Log.e(LOGTAG, "BrowserActivity is already started.");
809 return;
810 }
811 mActivityStopped = false;
Vivek Sekharb53a5952015-07-17 14:14:38 -0700812 UpdateNotificationService.updateCheck(mActivity);
George Mount3636d0a2011-11-21 09:08:21 -0800813 mSettings.setLastRunPaused(false);
Michael Kolb70976932010-11-30 11:34:01 -0800814 Tab current = mTabControl.getCurrentTab();
815 if (current != null) {
816 current.resume();
817 resumeWebViewTimers(current);
818 }
John Reckf57c0292011-07-21 18:15:39 -0700819 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200820
Michael Kolb8233fac2010-10-26 16:08:53 -0700821 mUi.onResume();
822 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100823 NfcHandler.register(mActivity, this);
Vivek Sekharc70ae632015-04-08 17:54:05 -0700824 if (current != null && current.getWebView().isShowingCrashView())
825 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700826 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700827
John Reckf57c0292011-07-21 18:15:39 -0700828 }
829
Michael Kolb70976932010-11-30 11:34:01 -0800830 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800831 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800832 * @param tab guaranteed non-null
833 */
834 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700835 boolean inLoad = tab.inPageLoad();
Tarun Nainani7c762c82015-08-04 13:42:09 -0700836 if ((!mActivityStopped && !inLoad) || (mActivityStopped && inLoad)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 CookieSyncManager.getInstance().startSync();
838 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100839 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 }
841 }
842
Michael Kolb70976932010-11-30 11:34:01 -0800843 /**
844 * Pause all WebView timers using the WebView of the given tab
845 * @param tab
846 * @return true if the timers are paused or tab is null
847 */
848 private boolean pauseWebViewTimers(Tab tab) {
849 if (tab == null) {
850 return true;
851 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100853 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 }
Michael Kolb70976932010-11-30 11:34:01 -0800856 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700857 }
858
John Reck9c35b9c2012-05-30 10:08:50 -0700859 @Override
860 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800861 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
863 mUploadHandler = null;
864 }
865 if (mTabControl == null) return;
866 mUi.onDestroy();
867 // Remove the current tab and sub window
868 Tab t = mTabControl.getCurrentTab();
869 if (t != null) {
870 dismissSubWindow(t);
871 removeTab(t);
872 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500873 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700874 // Destroy all the tabs
875 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700876 // Unregister receiver
877 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 }
879
880 protected boolean isActivityPaused() {
881 return mActivityPaused;
882 }
883
John Reck9c35b9c2012-05-30 10:08:50 -0700884 @Override
885 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 mTabControl.freeMemory();
887 }
888
889 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 public void stopLoading() {
891 mLoadStopped = true;
892 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700894 if (w != null) {
895 w.stopLoading();
896 mUi.onPageStopped(tab);
897 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700898 }
899
900 boolean didUserStopLoading() {
901 return mLoadStopped;
902 }
903
kaiyiza016da12013-08-26 17:50:22 +0800904 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700905 final String reminderType = getContext().getResources().getString(
906 R.string.def_wifi_browser_interaction_remind_type);
907 final String selectionConnnection = getContext().getResources().getString(
908 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700909 final String wifiSelection = getContext().getResources().getString(
910 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700911
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700912 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
913 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700914 return;
915
kaiyiza016da12013-08-26 17:50:22 +0800916 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700917 Context.CONNECTIVITY_SERVICE);
918 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700919 WifiManager wifiMgr = (WifiManager) this.getContext()
920 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700921 if (networkInfo == null
922 || (networkInfo != null && (networkInfo.getType() !=
923 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700924 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
925 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700926 List<ScanResult> list = wifiMgr.getScanResults();
927 // Have no AP's for Wifi's fall back to data
928 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700929 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700930 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
931 this.getContext().startActivity(intent);
932 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700933 // Request to select Wifi AP
934 try {
935 Intent intent = new Intent(wifiSelection);
936 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
937 this.getContext().startActivity(intent);
938 } catch (Exception e) {
939 String err_msg = this.getContext().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700940 R.string.activity_not_found, wifiSelection);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700941 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
942 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700943 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700944 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800945 }
946 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700947
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 // WebViewController
949
950 @Override
John Reck324d4402011-01-11 16:56:42 -0800951 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700952
Michael Kolb8233fac2010-10-26 16:08:53 -0700953 // reset sync timer to avoid sync starts during loading a page
954 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700955 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700956 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800957 boolean networkNotifier = BrowserConfig.getInstance(getContext())
958 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700959 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700960 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800961 } else {
962 if (!mNetworkHandler.isNetworkUp()) {
963 view.setNetworkAvailable(false);
964 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 }
966
967 // when BrowserActivity just starts, onPageStarted may be called before
968 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
969 // to start the timer. As we won't switch tabs while an activity is in
970 // pause state, we can ensure calling resume and pause in pair.
Tarun Nainani7c762c82015-08-04 13:42:09 -0700971 if (mActivityStopped) {
Michael Kolb70976932010-11-30 11:34:01 -0800972 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700973 }
974 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700975 endActionMode();
976
John Reck30c714c2010-12-16 17:30:34 -0800977 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700978
John Reck324d4402011-01-11 16:56:42 -0800979 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 // update the bookmark database for favicon
981 maybeUpdateFavicon(tab, null, url, favicon);
982
983 Performance.tracePageStart(url);
984
985 // Performance probe
986 if (false) {
987 Performance.onPageStarted();
988 }
989
990 }
991
992 @Override
John Reck324d4402011-01-11 16:56:42 -0800993 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700994 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800995 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200996
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 // Performance probe
998 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800999 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001000 }
1001
Tarun Nainani8eb00912014-07-17 12:28:32 -07001002 tab.onPageFinished();
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001003 maybeUpdateFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -07001004
Michael Kolb8233fac2010-10-26 16:08:53 -07001005 Performance.tracePageFinished();
1006 }
1007
1008 @Override
John Reck30c714c2010-12-16 17:30:34 -08001009 public void onProgressChanged(Tab tab) {
1010 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001011
1012 if (newProgress == 100) {
1013 CookieSyncManager.getInstance().sync();
1014 // onProgressChanged() may continue to be called after the main
1015 // frame has finished loading, as any remaining sub frames continue
1016 // to load. We'll only get called once though with newProgress as
1017 // 100 when everything is loaded. (onPageFinished is called once
1018 // when the main frame completes loading regardless of the state of
1019 // any sub frames so calls to onProgressChanges may continue after
1020 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001021 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001022 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001023 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001024 } else if (mWasInPageLoad) {
1025 mWasInPageLoad = false;
1026 updateInLoadMenuItems(mCachedMenu, tab);
1027 }
1028
Tarun Nainani7c762c82015-08-04 13:42:09 -07001029 if (mActivityStopped && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001030 // pause the WebView timer and release the wake lock if it is
1031 // finished while BrowserActivity is in pause state.
1032 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001033 }
1034 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001035 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001036 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001037 // still loading
1038 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001040 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001041 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001042 } else {
1043 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 }
1045 }
Vivek Sekhar30d0a2e2015-07-28 19:38:01 -07001046 if (isInLoad())
1047 mUi.onProgressChanged(tab);
John Reck30c714c2010-12-16 17:30:34 -08001048 }
1049
1050 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001051 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001052 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001053 }
1054
1055 @Override
1056 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001057 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001058 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001059 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1061 return;
1062 }
1063 // Update the title in the history database if not in private browsing mode
1064 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001065 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001066 }
1067 }
1068
1069 @Override
1070 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001071 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001072 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1073 }
1074
1075 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001076 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001077 // if tab is snapshot tab we want to prevent navigation from occuring
1078 // since snapshot tab opens a new tab with the url
1079 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001080 }
1081
1082 @Override
1083 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1084 if (mMenuIsDown) {
1085 // only check shortcut key when MENU is held
1086 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1087 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001088 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001089 int keyCode = event.getKeyCode();
1090 // We need to send almost every key to WebKit. However:
1091 // 1. We don't want to block the device on the renderer for
1092 // some keys like menu, home, call.
1093 // 2. There are no WebKit equivalents for some of these keys
1094 // (see app/keyboard_codes_win.h)
1095 // Note that these are not the same set as KeyEvent.isSystemKey:
1096 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1097 if (keyCode == KeyEvent.KEYCODE_MENU ||
1098 keyCode == KeyEvent.KEYCODE_HOME ||
1099 keyCode == KeyEvent.KEYCODE_BACK ||
1100 keyCode == KeyEvent.KEYCODE_CALL ||
1101 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1102 keyCode == KeyEvent.KEYCODE_POWER ||
1103 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1104 keyCode == KeyEvent.KEYCODE_CAMERA ||
1105 keyCode == KeyEvent.KEYCODE_FOCUS ||
1106 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1107 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1108 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1109 return true;
1110 }
1111
1112 // We also have to intercept some shortcuts before we send them to the ContentView.
1113 if (event.isCtrlPressed() && (
1114 keyCode == KeyEvent.KEYCODE_TAB ||
1115 keyCode == KeyEvent.KEYCODE_W ||
1116 keyCode == KeyEvent.KEYCODE_F4)) {
1117 return true;
1118 }
1119
1120 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001121 }
1122
Tarun Nainanif5563b62015-01-21 18:52:59 -08001123 private void handleMediaKeyEvent(KeyEvent event) {
1124
1125 int keyCode = event.getKeyCode();
1126 // send media key events to audio manager
1127 if (Build.VERSION.SDK_INT >= 19) {
1128
1129 switch (keyCode) {
1130 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1131 case KeyEvent.KEYCODE_MEDIA_STOP:
1132 case KeyEvent.KEYCODE_MEDIA_NEXT:
1133 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1134 case KeyEvent.KEYCODE_MEDIA_REWIND:
1135 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1136 case KeyEvent.KEYCODE_MUTE:
1137 case KeyEvent.KEYCODE_MEDIA_PLAY:
1138 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1139 case KeyEvent.META_SHIFT_RIGHT_ON:
1140 case KeyEvent.KEYCODE_MEDIA_EJECT:
1141 case KeyEvent.KEYCODE_MEDIA_RECORD:
1142 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1143
1144 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1145 .getSystemService(Context.AUDIO_SERVICE);
1146 audioManager.dispatchMediaKeyEvent(event);
1147 }
1148 }
1149 }
1150
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 @Override
John Reck997b1b72012-04-19 18:08:25 -07001152 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001153 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001154 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001156 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 } else {
John Reck997b1b72012-04-19 18:08:25 -07001158 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 }
1160 }
John Reck997b1b72012-04-19 18:08:25 -07001161 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001162 }
1163
1164 @Override
John Reck324d4402011-01-11 16:56:42 -08001165 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001166 // Don't save anything in private browsing mode or when disabling history
1167 // for regular tabs is enabled
1168 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1169 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1170 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001171
John Reck49a603c2011-03-03 09:33:05 -08001172 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001173
John Reck324d4402011-01-11 16:56:42 -08001174 if (TextUtils.isEmpty(url)
1175 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001176 return;
1177 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001178
John Reckf57c0292011-07-21 18:15:39 -07001179 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001180 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001181 }
1182
1183 @Override
1184 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1185 AsyncTask<Void, Void, String[]> task =
1186 new AsyncTask<Void, Void, String[]>() {
1187 @Override
1188 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001189 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001190 }
1191 @Override
1192 public void onPostExecute(String[] result) {
1193 callback.onReceiveValue(result);
1194 }
1195 };
1196 task.execute();
1197 }
1198
1199 @Override
1200 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1201 final HttpAuthHandler handler, final String host,
1202 final String realm) {
1203 String username = null;
1204 String password = null;
1205
1206 boolean reuseHttpAuthUsernamePassword
1207 = handler.useHttpAuthUsernamePassword();
1208
1209 if (reuseHttpAuthUsernamePassword && view != null) {
1210 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1211 if (credentials != null && credentials.length == 2) {
1212 username = credentials[0];
1213 password = credentials[1];
1214 }
1215 }
1216
1217 if (username != null && password != null) {
1218 handler.proceed(username, password);
1219 } else {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -07001220 if (!tab.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001221 handler.cancel();
1222 }
1223 }
1224 }
1225
1226 @Override
1227 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001228 String contentDisposition, String mimetype, String referer,
1229 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001230 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001231 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001232 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001233 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001234 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 // This Tab was opened for the sole purpose of downloading a
1236 // file. Remove it.
1237 if (tab == mTabControl.getCurrentTab()) {
1238 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001239 if (tab.getDerivedFromIntent())
1240 closeTab(tab);
1241 else
1242 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 } else {
1244 // In this case, it is not.
1245 closeTab(tab);
1246 }
1247 }
1248 }
1249
1250 @Override
1251 public Bitmap getDefaultVideoPoster() {
1252 return mUi.getDefaultVideoPoster();
1253 }
1254
1255 @Override
1256 public View getVideoLoadingProgressView() {
1257 return mUi.getVideoLoadingProgressView();
1258 }
1259
Michael Kolb8233fac2010-10-26 16:08:53 -07001260 // helper method
1261
1262 /*
1263 * Update the favorites icon if the private browsing isn't enabled and the
1264 * icon is valid.
1265 */
1266 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1267 final String url, Bitmap favicon) {
1268 if (favicon == null) {
1269 return;
1270 }
1271 if (!tab.isPrivateBrowsingEnabled()) {
1272 Bookmarks.updateFavicon(mActivity
1273 .getContentResolver(), originalUrl, url, favicon);
1274 }
1275 }
1276
Leon Scroggins4cd97792010-12-03 15:31:56 -05001277 @Override
1278 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001279 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001280 mUi.bookmarkedStatusHasChanged(tab);
1281 }
1282
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 // end WebViewController
1284
1285 protected void pageUp() {
1286 getCurrentTopWebView().pageUp(false);
1287 }
1288
1289 protected void pageDown() {
1290 getCurrentTopWebView().pageDown(false);
1291 }
1292
1293 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001294 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 public void editUrl() {
1296 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001297 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001298 }
1299
John Reck9c35b9c2012-05-30 10:08:50 -07001300 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001301 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001302 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001303 if (tab.inForeground()) {
1304 if (mUi.isCustomViewShowing()) {
1305 callback.onCustomViewHidden();
1306 return;
1307 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001308 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001309 // Save the menu state and set it to empty while the custom
1310 // view is showing.
1311 mOldMenuState = mMenuState;
1312 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001313 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001314 }
1315 }
1316
1317 @Override
1318 public void hideCustomView() {
1319 if (mUi.isCustomViewShowing()) {
1320 mUi.onHideCustomView();
1321 // Reset the old menu state.
1322 mMenuState = mOldMenuState;
1323 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001324 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001325 }
1326 }
1327
John Reck9c35b9c2012-05-30 10:08:50 -07001328 @Override
1329 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001330 Intent intent) {
1331 if (getCurrentTopWebView() == null) return;
1332 switch (requestCode) {
1333 case PREFERENCES_PAGE:
1334 if (resultCode == Activity.RESULT_OK && intent != null) {
1335 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001336 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 mTabControl.removeParentChildRelationShips();
1338 }
1339 }
1340 break;
1341 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001342 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001343 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001344 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001345 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001346 case AUTOFILL_SETUP:
1347 // Determine whether a profile was actually set up or not
1348 // and if so, send the message back to the WebTextView to
1349 // fill the form with the new profile.
1350 if (getSettings().getAutoFillProfile() != null) {
1351 mAutoFillSetupMessage.sendToTarget();
1352 mAutoFillSetupMessage = null;
1353 }
1354 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001355 case COMBO_VIEW:
1356 if (intent == null || resultCode != Activity.RESULT_OK) {
1357 break;
1358 }
John Reck3ba45532011-08-11 16:26:53 -07001359 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001360 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1361 Tab t = getCurrentTab();
1362 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001363 mUpdateMyNavThumbnail = true;
1364 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001365 loadUrl(t, uri.toString());
1366 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1367 String[] urls = intent.getStringArrayExtra(
1368 ComboViewActivity.EXTRA_OPEN_ALL);
1369 Tab parent = getCurrentTab();
1370 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001371 if (url != null) {
1372 parent = openTab(url, parent,
1373 !mSettings.openInBackground(), true);
1374 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001375 }
1376 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1377 long id = intent.getLongExtra(
1378 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1379 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001380 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001381 }
1382 }
1383 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001384 case VOICE_RESULT:
1385 if (resultCode == Activity.RESULT_OK && intent != null) {
1386 ArrayList<String> results = intent.getStringArrayListExtra(
1387 RecognizerIntent.EXTRA_RESULTS);
1388 if (results.size() >= 1) {
1389 mVoiceResult = results.get(0);
1390 }
1391 }
1392 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001393 case MY_NAVIGATION:
1394 if (intent == null || resultCode != Activity.RESULT_OK) {
1395 break;
1396 }
1397
1398 if (intent.getBooleanExtra("need_refresh", false) &&
1399 getCurrentTopWebView() != null) {
1400 getCurrentTopWebView().reload();
1401 }
1402 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001403 default:
1404 break;
1405 }
1406 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001407 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001408 }
1409
1410 /**
1411 * Open the Go page.
1412 * @param startWithHistory If true, open starting on the history tab.
1413 * Otherwise, start with the bookmarks tab.
1414 */
1415 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001416 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 if (mTabControl.getCurrentWebView() == null) {
1418 return;
1419 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001420 // clear action mode
1421 if (isInCustomActionMode()) {
1422 endActionMode();
1423 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001424 Bundle extras = new Bundle();
1425 // Disable opening in a new window if we have maxed out the windows
1426 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1427 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001428 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001429 }
1430
1431 // combo view callbacks
1432
Michael Kolb8233fac2010-10-26 16:08:53 -07001433 // key handling
1434 protected void onBackKey() {
1435 if (!mUi.onBackKey()) {
1436 WebView subwindow = mTabControl.getCurrentSubWindow();
1437 if (subwindow != null) {
1438 if (subwindow.canGoBack()) {
1439 subwindow.goBack();
1440 } else {
1441 dismissSubWindow(mTabControl.getCurrentTab());
1442 }
1443 } else {
1444 goBackOnePageOrQuit();
1445 }
1446 }
1447 }
1448
Michael Kolb4bd767d2011-05-27 11:33:55 -07001449 protected boolean onMenuKey() {
1450 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001451 }
1452
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 // menu handling and state
1454 // TODO: maybe put into separate handler
1455
John Reck9c35b9c2012-05-30 10:08:50 -07001456 @Override
1457 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001458 if (mMenuState == EMPTY_MENU) {
1459 return false;
1460 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001461 MenuInflater inflater = mActivity.getMenuInflater();
1462 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 return true;
1464 }
1465
John Reck9c35b9c2012-05-30 10:08:50 -07001466 @Override
1467 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001468 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001469 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001470 return;
1471 }
1472 if (!(v instanceof WebView)) {
1473 return;
1474 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001475 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001476 WebView.HitTestResult result;
1477
1478 /* Determine whether the ContextMenu got triggered because
1479 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1480 * received. The mResult acts as a flag to identify, how it got trigerred
1481 */
1482 if (mResult == null){
1483 result = webview.getHitTestResult();
1484 } else {
1485 result = mResult;
1486 }
1487
Michael Kolb8233fac2010-10-26 16:08:53 -07001488 if (result == null) {
1489 return;
1490 }
1491
1492 int type = result.getType();
1493 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001494
1495 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1496 unknownTypeMap.put("webview", webview);
1497 final Message msg = mHandler.obtainMessage(
1498 UNKNOWN_TYPE_MSG, unknownTypeMap);
1499 /* As defined in android developers guide
1500 * when UNKNOWN_TYPE is received as a result of HitTest
1501 * you need to determing the type by invoking requestFocusNodeHref
1502 */
1503 webview.requestFocusNodeHref(msg);
1504
Michael Kolb8233fac2010-10-26 16:08:53 -07001505 Log.w(LOGTAG,
1506 "We should not show context menu when nothing is touched");
1507 return;
1508 }
1509 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1510 // let TextView handles context menu
1511 return;
1512 }
1513
1514 // Note, http://b/issue?id=1106666 is requesting that
1515 // an inflated menu can be used again. This is not available
1516 // yet, so inflate each time (yuk!)
1517 MenuInflater inflater = mActivity.getMenuInflater();
1518 inflater.inflate(R.menu.browsercontext, menu);
1519
1520 // Show the correct menu group
1521 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001522 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001523 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001524 menu.setGroupVisible(R.id.PHONE_MENU,
1525 type == WebView.HitTestResult.PHONE_TYPE);
1526 menu.setGroupVisible(R.id.EMAIL_MENU,
1527 type == WebView.HitTestResult.EMAIL_TYPE);
1528 menu.setGroupVisible(R.id.GEO_MENU,
1529 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001530 String itemUrl = null;
1531 String url = webview.getOriginalUrl();
1532 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1533 itemUrl = Uri.decode(navigationUrl);
1534 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1535 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1536 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1537 } else {
1538 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1539 }
1540 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1541 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1542 } else {
1543 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1544
1545 menu.setGroupVisible(R.id.IMAGE_MENU,
1546 type == WebView.HitTestResult.IMAGE_TYPE
1547 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1548 menu.setGroupVisible(R.id.ANCHOR_MENU,
1549 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1550 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001551
1552 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1553 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1554 UrlUtils.isDownloadableScheme(extra));
1555 }
1556 else {
1557 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1558 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001559 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001560 // Setup custom handling depending on the type
1561 switch (type) {
1562 case WebView.HitTestResult.PHONE_TYPE:
1563 menu.setHeaderTitle(Uri.decode(extra));
1564 menu.findItem(R.id.dial_context_menu_id).setIntent(
1565 new Intent(Intent.ACTION_VIEW, Uri
1566 .parse(WebView.SCHEME_TEL + extra)));
1567 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1568 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1569 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1570 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1571 addIntent);
1572 menu.findItem(R.id.copy_phone_context_menu_id)
1573 .setOnMenuItemClickListener(
1574 new Copy(extra));
1575 break;
1576
1577 case WebView.HitTestResult.EMAIL_TYPE:
1578 menu.setHeaderTitle(extra);
1579 menu.findItem(R.id.email_context_menu_id).setIntent(
1580 new Intent(Intent.ACTION_VIEW, Uri
1581 .parse(WebView.SCHEME_MAILTO + extra)));
1582 menu.findItem(R.id.copy_mail_context_menu_id)
1583 .setOnMenuItemClickListener(
1584 new Copy(extra));
1585 break;
1586
1587 case WebView.HitTestResult.GEO_TYPE:
1588 menu.setHeaderTitle(extra);
1589 menu.findItem(R.id.map_context_menu_id).setIntent(
1590 new Intent(Intent.ACTION_VIEW, Uri
1591 .parse(WebView.SCHEME_GEO
1592 + URLEncoder.encode(extra))));
1593 menu.findItem(R.id.copy_geo_context_menu_id)
1594 .setOnMenuItemClickListener(
1595 new Copy(extra));
1596 break;
1597
1598 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1599 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001600 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 // decide whether to show the open link in new tab option
1602 boolean showNewTab = mTabControl.canCreateNewTab();
1603 MenuItem newTabItem
1604 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001605 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001606 ? R.string.contextmenu_openlink_newwindow_background
1607 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 newTabItem.setVisible(showNewTab);
1609 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001610 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1611 newTabItem.setOnMenuItemClickListener(
1612 new MenuItem.OnMenuItemClickListener() {
1613 @Override
1614 public boolean onMenuItemClick(MenuItem item) {
1615 final HashMap<String, WebView> hrefMap =
1616 new HashMap<String, WebView>();
1617 hrefMap.put("webview", webview);
1618 final Message msg = mHandler.obtainMessage(
1619 FOCUS_NODE_HREF,
1620 R.id.open_newtab_context_menu_id,
1621 0, hrefMap);
1622 webview.requestFocusNodeHref(msg);
1623 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001624 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001625 });
1626 } else {
1627 newTabItem.setOnMenuItemClickListener(
1628 new MenuItem.OnMenuItemClickListener() {
1629 @Override
1630 public boolean onMenuItemClick(MenuItem item) {
1631 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001632 openTab(extra, parent,
1633 !mSettings.openInBackground(),
1634 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001635 return true;
1636 }
1637 });
1638 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001639 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001640 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1641 menu.setHeaderTitle(navigationUrl);
1642 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1643
1644 if (itemUrl != null) {
1645 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1646 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1647 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1648 @Override
1649 public boolean onMenuItemClick(MenuItem item) {
1650 final Intent intent = new Intent(Controller.this
1651 .getContext(),
1652 AddMyNavigationPage.class);
1653 Bundle bundle = new Bundle();
1654 String url = Uri.decode(navigationUrl);
1655 bundle.putBoolean("isAdding", false);
1656 bundle.putString("url", url);
1657 bundle.putString("name", getNameFromUrl(url));
1658 intent.putExtra("websites", bundle);
1659 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1660 return false;
1661 }
1662 });
1663 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1664 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1665 @Override
1666 public boolean onMenuItemClick(MenuItem item) {
1667 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1668 return false;
1669 }
1670 });
1671 }
1672 } else {
1673 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1674 }
1675 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001676 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1677 break;
1678 }
1679 // otherwise fall through to handle image part
1680 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001681 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1682 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001683 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1684 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001685 shareItem.setOnMenuItemClickListener(
1686 new MenuItem.OnMenuItemClickListener() {
1687 @Override
1688 public boolean onMenuItemClick(MenuItem item) {
1689 sharePage(mActivity, null, extra, null,
1690 null);
1691 return true;
1692 }
1693 }
1694 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001695 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001696 menu.findItem(R.id.view_image_context_menu_id)
1697 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1698 @Override
1699 public boolean onMenuItemClick(MenuItem item) {
1700 openTab(extra, mTabControl.getCurrentTab(), true, true);
1701 return false;
1702 }
1703 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001704 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1705 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1706 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001707 menu.findItem(R.id.set_wallpaper_context_menu_id).
1708 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1709 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001710 break;
1711
1712 default:
1713 Log.w(LOGTAG, "We should not get here.");
1714 break;
1715 }
1716 //update the ui
1717 mUi.onContextMenuCreated(menu);
1718 }
1719
kaiyiz6e5b3e02013-08-19 20:02:01 +08001720 public void startAddMyNavigation(String url) {
1721 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1722 Bundle bundle = new Bundle();
1723 bundle.putBoolean("isAdding", true);
1724 bundle.putString("url", url);
1725 bundle.putString("name", getNameFromUrl(url));
1726 intent.putExtra("websites", bundle);
1727 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1728 }
1729
1730 private void showMyNavigationDeleteDialog(final String itemUrl) {
1731 new AlertDialog.Builder(this.getContext())
1732 .setTitle(R.string.my_navigation_delete_label)
1733 .setIcon(android.R.drawable.ic_dialog_alert)
1734 .setMessage(R.string.my_navigation_delete_msg)
1735 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1736 @Override
1737 public void onClick(DialogInterface dialog, int whichButton) {
1738 deleteMyNavigationItem(itemUrl);
1739 }
1740 })
1741 .setNegativeButton(R.string.cancel, null)
1742 .show();
1743 }
1744
1745 private void deleteMyNavigationItem(final String itemUrl) {
1746 ContentResolver cr = this.getContext().getContentResolver();
1747 Cursor cursor = null;
1748
1749 try {
1750 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1751 new String[] {
1752 MyNavigationUtil.ID
1753 }, "url = ?", new String[] {
1754 itemUrl
1755 }, null);
1756 if (null != cursor && cursor.moveToFirst()) {
1757 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1758 cursor.getLong(0));
1759
1760 ContentValues values = new ContentValues();
1761 values.put(MyNavigationUtil.TITLE, "");
1762 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1763 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1764 ByteArrayOutputStream os = new ByteArrayOutputStream();
1765 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1766 R.raw.my_navigation_add);
1767 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1768 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1769 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1770 cr.update(uri, values, null, null);
1771 } else {
1772 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1773 }
1774 } catch (IllegalStateException e) {
1775 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1776 } finally {
1777 if (null != cursor) {
1778 cursor.close();
1779 }
1780 }
1781
1782 if (getCurrentTopWebView() != null) {
1783 getCurrentTopWebView().reload();
1784 }
1785 }
1786
1787 private String getNameFromUrl(String itemUrl) {
1788 ContentResolver cr = this.getContext().getContentResolver();
1789 Cursor cursor = null;
1790 String name = null;
1791
1792 try {
1793 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1794 new String[] {
1795 MyNavigationUtil.TITLE
1796 }, "url = ?", new String[] {
1797 itemUrl
1798 }, null);
1799 if (null != cursor && cursor.moveToFirst()) {
1800 name = cursor.getString(0);
1801 } else {
1802 Log.e(LOGTAG, "this item does not exist!");
1803 }
1804 } catch (IllegalStateException e) {
1805 Log.e(LOGTAG, "getNameFromUrl", e);
1806 } finally {
1807 if (null != cursor) {
1808 cursor.close();
1809 }
1810 }
1811 return name;
1812 }
1813
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001814 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001815 final ContentResolver cr = mActivity.getContentResolver();
1816 new AsyncTask<Void, Void, Void>() {
1817 @Override
1818 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001819 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1820 if(!isMyNavigationUrl)
1821 return null;
1822
kaiyiz6e5b3e02013-08-19 20:02:01 +08001823 ContentResolver cr = mActivity.getContentResolver();
1824 Cursor cursor = null;
1825 try {
1826 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1827 new String[] {
1828 MyNavigationUtil.ID
1829 }, "url = ?", new String[] {
1830 itemUrl
1831 }, null);
1832 if (null != cursor && cursor.moveToFirst()) {
1833 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001834 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001835
1836 ContentValues values = new ContentValues();
1837 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1838 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1839 cursor.getLong(0));
1840 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1841 cr.update(uri, values, null, null);
1842 os.close();
1843 }
1844 } catch (IllegalStateException e) {
1845 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1846 } catch (IOException e) {
1847 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1848 } finally {
1849 if (null != cursor) {
1850 cursor.close();
1851 }
1852 }
1853 return null;
1854 }
1855 }.execute();
1856 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001857 /**
1858 * As the menu can be open when loading state changes
1859 * we must manually update the state of the stop/reload menu
1860 * item
1861 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001862 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001863 if (menu == null) {
1864 return;
1865 }
1866 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001867 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001868 menu.findItem(R.id.stop_menu_id):
1869 menu.findItem(R.id.reload_menu_id);
1870 if (src != null) {
1871 dest.setIcon(src.getIcon());
1872 dest.setTitle(src.getTitle());
1873 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001874 mActivity.invalidateOptionsMenu();
1875 }
1876
1877 public void invalidateOptionsMenu() {
1878 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001879 }
1880
John Reck9c35b9c2012-05-30 10:08:50 -07001881 @Override
1882 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001883 // Software menu key (toolbar key)
1884 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1885 return true;
1886 }
1887
1888 @Override
1889 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001890 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001891 // hold on to the menu reference here; it is used by the page callbacks
1892 // to update the menu based on loading state
1893 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001894 // Note: setVisible will decide whether an item is visible; while
1895 // setEnabled() will decide whether an item is enabled, which also means
1896 // whether the matching shortcut key will function.
1897 switch (mMenuState) {
1898 case EMPTY_MENU:
1899 if (mCurrentMenuState != mMenuState) {
1900 menu.setGroupVisible(R.id.MAIN_MENU, false);
1901 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1902 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1903 }
1904 break;
1905 default:
1906 if (mCurrentMenuState != mMenuState) {
1907 menu.setGroupVisible(R.id.MAIN_MENU, true);
1908 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1909 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1910 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001911 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001912 break;
1913 }
1914 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001915 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001916
1917 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001918 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1919 EditBookmarksRestriction.getInstance()
1920 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001921 }
1922
1923 private void setMenuItemVisibility(Menu menu, int id,
1924 boolean visibility) {
1925 MenuItem item = menu.findItem(id);
1926 if (item != null) {
1927 item.setVisible(visibility);
1928 }
1929 }
1930
1931 private int lookupBookmark(String title, String url) {
1932 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001933 int count = 0;
1934 Cursor cursor = null;
1935 try {
1936 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1937 BookmarksLoader.PROJECTION,
1938 "title = ? OR url = ?",
1939 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001940 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001941 },
1942 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001943
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001944 if (cursor != null)
1945 count = cursor.getCount();
1946
1947 } catch (IllegalStateException e) {
1948 Log.e(LOGTAG, "lookupBookmark ", e);
1949 } finally {
1950 if (null != cursor) {
1951 cursor.close();
1952 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001953 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001954 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001955 }
1956
1957 private void resetMenuItems(Menu menu) {
1958 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1959 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1960
1961 WebView w = getCurrentTopWebView();
1962 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1963
1964 String title = w.getTitle();
1965 String url = w.getUrl();
Pankaj Garg634bf432015-07-13 09:54:21 -07001966 mCurrentPageBookmarked = (lookupBookmark(title, url) > 0);
1967 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001968 bookmark_icon.setChecked(true);
1969 } else {
1970 bookmark_icon.setChecked(false);
1971 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001972
1973 // update reader mode checkbox
1974 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1975 readerSwitcher.setVisible(false);
1976 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001977 }
1978
Michael Kolb4bf79712011-07-14 14:18:12 -07001979 @Override
1980 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001981 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001982 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001983 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001984 // Following flag is used to identify schemes for which the LIVE_MENU
1985 // items defined in res/menu/browser.xml should be enabled
1986 boolean isLiveScheme = false;
1987 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001988 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07001989
1990 boolean isDistillable = false;
1991 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001992 resetMenuItems(menu);
1993
Michael Kolb4bf79712011-07-14 14:18:12 -07001994 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001995 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001996 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001997 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001998 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001999 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002000 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07002001
2002 isDistillable = tab.isDistillable();
2003 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07002004 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002005
2006 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2007 forward.setEnabled(canGoForward);
2008
Michael Kolb4bf79712011-07-14 14:18:12 -07002009 // decide whether to show the share link option
2010 PackageManager pm = mActivity.getPackageManager();
2011 Intent send = new Intent(Intent.ACTION_SEND);
2012 send.setType("text/plain");
2013 ResolveInfo ri = pm.resolveActivity(send,
2014 PackageManager.MATCH_DEFAULT_ONLY);
2015 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2016
2017 boolean isNavDump = mSettings.enableNavDump();
2018 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2019 nav.setVisible(isNavDump);
2020 nav.setEnabled(isNavDump);
2021
2022 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002023 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2024 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002025 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002026 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2027 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002028 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002029 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2030 isLive && isLiveScheme && isPageFinished);
2031 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002032 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002033 // history and snapshots item are the members of COMBO menu group,
2034 // so if show history item, only make snapshots item invisible.
2035 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002036
Ze G Riande2a675c22015-06-03 11:15:24 -07002037
2038 // update reader mode checkbox
2039 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2040 // The reader mode checkbox is hidden only
2041 // when the current page is neither distillable nor distilled
2042 readerSwitcher.setVisible(isDistillable || isDistilled);
2043 readerSwitcher.setChecked(isDistilled);
2044
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002045 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002046 }
2047
John Reck9c35b9c2012-05-30 10:08:50 -07002048 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002049 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002050 if (null == getCurrentTopWebView()) {
2051 return false;
2052 }
2053 if (mMenuIsDown) {
2054 // The shortcut action consumes the MENU. Even if it is still down,
2055 // it won't trigger the next shortcut action. In the case of the
2056 // shortcut action triggering a new activity, like Bookmarks, we
2057 // won't get onKeyUp for MENU. So it is important to reset it here.
2058 mMenuIsDown = false;
2059 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002060 if (mUi.onOptionsItemSelected(item)) {
2061 // ui callback handled it
2062 return true;
2063 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002064 switch (item.getItemId()) {
2065 // -- Main menu
2066 case R.id.new_tab_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002067 getCurrentTab().capture();
Michael Kolb8233fac2010-10-26 16:08:53 -07002068 openTabToHomePage();
2069 break;
2070
2071 case R.id.incognito_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002072 getCurrentTab().capture();
Michael Kolb519d2282011-05-09 17:03:19 -07002073 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002074 break;
2075
2076 case R.id.goto_menu_id:
2077 editUrl();
2078 break;
2079
2080 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002081 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2082 break;
2083
2084 case R.id.history_menu_id:
2085 bookmarksOrHistoryPicker(ComboViews.History);
2086 break;
2087
2088 case R.id.snapshots_menu_id:
2089 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002090 break;
2091
Pankaj Garg49b79252014-11-07 17:33:41 -08002092 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002093 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002094 break;
2095
2096 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002097 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002098 stopLoading();
2099 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002100 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002101 getCurrentTopWebView().reload();
2102 }
2103 break;
2104
Michael Kolb8233fac2010-10-26 16:08:53 -07002105 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002106 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002107 break;
2108
2109 case R.id.close_menu_id:
2110 // Close the subwindow if it exists.
2111 if (mTabControl.getCurrentSubWindow() != null) {
2112 dismissSubWindow(mTabControl.getCurrentTab());
2113 break;
2114 }
2115 closeCurrentTab();
2116 break;
2117
kaiyiza8b6dbb2013-07-29 18:11:22 +08002118 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002119 Object[] params = { new String("persist.debug.browsermonkeytest")};
2120 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002121 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002122 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002123 if (ret != null && ret.equals("enable"))
2124 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002125 if (BrowserConfig.getInstance(getContext())
2126 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2127 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002128 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002129 case R.id.homepage_menu_id:
2130 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002131 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002132 break;
2133
2134 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002135 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002136 break;
2137
2138 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002139 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002140 break;
2141
John Reck2bc80422011-06-30 15:11:49 -07002142 case R.id.save_snapshot_menu_id:
2143 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002144 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002145 createScreenshotAsync(
2146 source.getWebView(),
2147 getDesiredThumbnailWidth(mActivity),
2148 getDesiredThumbnailHeight(mActivity),
2149 new ValueCallback<Bitmap>() {
2150 @Override
2151 public void onReceiveValue(Bitmap bitmap) {
2152 new SaveSnapshotTask(source, bitmap).execute();
2153 }
2154 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002155 break;
2156
Michael Kolb8233fac2010-10-26 16:08:53 -07002157 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002158 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002159 break;
2160
John Recke1a03a32011-09-14 17:04:16 -07002161 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002162 // passing null to distinguish between
2163 // "go live" button and navigating a web page
2164 // on a snapshot tab
2165 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002166 case R.id.share_page_menu_id:
2167 Tab currentTab = mTabControl.getCurrentTab();
2168 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002169 return false;
2170 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002171 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002172 break;
2173
2174 case R.id.dump_nav_menu_id:
2175 getCurrentTopWebView().debugDump();
2176 break;
2177
Michael Kolb8233fac2010-10-26 16:08:53 -07002178 case R.id.zoom_in_menu_id:
2179 getCurrentTopWebView().zoomIn();
2180 break;
2181
2182 case R.id.zoom_out_menu_id:
2183 getCurrentTopWebView().zoomOut();
2184 break;
2185
2186 case R.id.view_downloads_menu_id:
2187 viewDownloads();
2188 break;
2189
John Reck42229bc2011-08-19 13:26:43 -07002190 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002191 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002192 break;
2193
Ze G Riande2a675c22015-06-03 11:15:24 -07002194 case R.id.reader_mode_menu_id:
2195 toggleReaderMode();
2196 break;
2197
Michael Kolb8233fac2010-10-26 16:08:53 -07002198 case R.id.window_one_menu_id:
2199 case R.id.window_two_menu_id:
2200 case R.id.window_three_menu_id:
2201 case R.id.window_four_menu_id:
2202 case R.id.window_five_menu_id:
2203 case R.id.window_six_menu_id:
2204 case R.id.window_seven_menu_id:
2205 case R.id.window_eight_menu_id:
2206 {
2207 int menuid = item.getItemId();
2208 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2209 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2210 Tab desiredTab = mTabControl.getTab(id);
2211 if (desiredTab != null &&
2212 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002213 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002214 }
2215 break;
2216 }
2217 }
2218 }
2219 break;
2220
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002221 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002222 Bundle bundle = new Bundle();
2223 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2224 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2225 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2226 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2227 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002228 break;
2229
Pankaj Garg49b79252014-11-07 17:33:41 -08002230 case R.id.add_to_homescreen:
2231 final WebView w = getCurrentTopWebView();
2232 final EditText input = new EditText(getContext());
2233 input.setText(w.getTitle());
2234 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002235 .setTitle(getContext().getResources().getString(
2236 R.string.add_to_homescreen))
2237 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002238 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002239 .setPositiveButton(getContext().getResources().getString(
2240 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002241 public void onClick(DialogInterface dialog, int whichButton) {
2242 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2243 getContext(),
2244 w.getUrl(),
2245 input.getText().toString(),
2246 w.getViewportBitmap(),
2247 w.getFavicon()));
2248
2249 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2250 .addCategory(Intent.CATEGORY_HOME));
2251 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002252 .setNegativeButton(getContext().getResources().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07002253 R.string.cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002254 public void onClick(DialogInterface dialog, int whichButton) {
2255 // Do nothing.
2256 }
2257 })
2258 .show();
2259 break;
2260
Michael Kolb8233fac2010-10-26 16:08:53 -07002261 default:
2262 return false;
2263 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002264 return true;
2265 }
2266
John Reck68234a92012-04-19 15:27:12 -07002267 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2268 implements OnCancelListener {
2269
2270 private Tab mTab;
2271 private Dialog mProgressDialog;
2272 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002273 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002274
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002275 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002276 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002277 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002278 }
2279
2280 @Override
2281 protected void onPreExecute() {
2282 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2283 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2284 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002285 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002286 }
2287
2288 @Override
2289 protected Long doInBackground(Void... params) {
2290 if (!mTab.saveViewState(mValues)) {
2291 return null;
2292 }
2293 if (isCancelled()) {
2294 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2295 File file = mActivity.getFileStreamPath(path);
2296 if (!file.delete()) {
2297 file.deleteOnExit();
2298 }
2299 return null;
2300 }
2301 final ContentResolver cr = mActivity.getContentResolver();
2302 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2303 if (result == null) {
2304 return null;
2305 }
2306 long id = ContentUris.parseId(result);
2307 return id;
2308 }
2309
2310 @Override
2311 protected void onPostExecute(Long id) {
2312 if (isCancelled()) {
2313 return;
2314 }
2315 mProgressDialog.dismiss();
2316 if (id == null) {
2317 Toast.makeText(mActivity, R.string.snapshot_failed,
2318 Toast.LENGTH_SHORT).show();
2319 return;
2320 }
2321 Bundle b = new Bundle();
2322 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2323 mUi.showComboView(ComboViews.Snapshots, b);
2324 }
2325
2326 @Override
2327 public void onCancel(DialogInterface dialog) {
2328 cancel(true);
2329 }
2330 }
2331
Michael Kolb80f75082012-04-10 10:50:06 -07002332 @Override
2333 public void toggleUserAgent() {
2334 WebView web = getCurrentWebView();
2335 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002336 }
2337
Ze G Riande2a675c22015-06-03 11:15:24 -07002338 // This function calls the method in the webview to enable/disable
2339 // the reader mode through the DOM distiller
2340 public void toggleReaderMode() {
2341 Tab t = mTabControl.getCurrentTab();
2342 if (t.isDistilled()) {
2343 closeTab(t);
2344 } else if (t.isDistillable()) {
2345 openTab(t.getDistilledUrl(), false, true, false, t);
2346 }
2347 }
2348
Michael Kolb80f75082012-04-10 10:50:06 -07002349 @Override
2350 public void findOnPage() {
2351 getCurrentTopWebView().showFindDialog(null, true);
2352 }
2353
2354 @Override
2355 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002356 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002357 }
2358
2359 @Override
2360 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002361 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002362 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2363 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002364 }
2365 else {
2366 WebView w = getCurrentTopWebView();
2367 if (w == null)
2368 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002369 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002370 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002371 }
Michael Kolb80f75082012-04-10 10:50:06 -07002372 }
2373
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002374 public boolean goLive(String url) {
2375 if (!getCurrentTab().isSnapshot())
2376 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002377 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002378
2379 if (url == null) { // "go live" button was clicked
2380 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002381 closeTab(t);
2382 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002383 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002384 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002385 return true;
John Recke1a03a32011-09-14 17:04:16 -07002386 }
2387
luxiaolb40014b2013-07-19 10:01:43 +08002388 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002389 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002390 new AlertDialog.Builder(activity)
2391 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002392 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002393 .setMessage(R.string.exit_browser_msg)
2394 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2395 public void onClick(DialogInterface dialog, int which) {
2396 activity.moveTaskToBack(true);
2397 dialog.dismiss();
2398 }
2399 })
2400 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2401 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002402 mCrashRecoveryHandler.clearState(true);
2403 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002404 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002405 dialog.dismiss();
2406 }
2407 })
2408 .show();
2409 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002410
Michael Kolb315d5022011-10-13 12:47:11 -07002411 @Override
2412 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002413 }
2414
John Reck9c35b9c2012-05-30 10:08:50 -07002415 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002416 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002417 // Let the History and Bookmark fragments handle menus they created.
2418 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2419 return false;
2420 }
2421
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 int id = item.getItemId();
2423 boolean result = true;
2424 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002425 // -- Browser context menu
2426 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002427 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002428 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002429 case R.id.copy_link_context_menu_id:
2430 final WebView webView = getCurrentTopWebView();
2431 if (null == webView) {
2432 result = false;
2433 break;
2434 }
2435 final HashMap<String, WebView> hrefMap =
2436 new HashMap<String, WebView>();
2437 hrefMap.put("webview", webView);
2438 final Message msg = mHandler.obtainMessage(
2439 FOCUS_NODE_HREF, id, 0, hrefMap);
2440 webView.requestFocusNodeHref(msg);
2441 break;
2442
2443 default:
2444 // For other context menus
2445 result = onOptionsItemSelected(item);
2446 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002447 return result;
2448 }
2449
2450 /**
2451 * support programmatically opening the context menu
2452 */
2453 public void openContextMenu(View view) {
2454 mActivity.openContextMenu(view);
2455 }
2456
2457 /**
2458 * programmatically open the options menu
2459 */
2460 public void openOptionsMenu() {
2461 mActivity.openOptionsMenu();
2462 }
2463
John Reck9c35b9c2012-05-30 10:08:50 -07002464 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002465 public boolean onMenuOpened(int featureId, Menu menu) {
2466 if (mOptionsMenuOpen) {
2467 if (mConfigChanged) {
2468 // We do not need to make any changes to the state of the
2469 // title bar, since the only thing that happened was a
2470 // change in orientation
2471 mConfigChanged = false;
2472 } else {
2473 if (!mExtendedMenuOpen) {
2474 mExtendedMenuOpen = true;
2475 mUi.onExtendedMenuOpened();
2476 } else {
2477 // Switching the menu back to icon view, so show the
2478 // title bar once again.
2479 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002480 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002481 }
2482 }
2483 } else {
2484 // The options menu is closed, so open it, and show the title
2485 mOptionsMenuOpen = true;
2486 mConfigChanged = false;
2487 mExtendedMenuOpen = false;
2488 mUi.onOptionsMenuOpened();
2489 }
2490 return true;
2491 }
2492
John Reck9c35b9c2012-05-30 10:08:50 -07002493 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002494 public void onOptionsMenuClosed(Menu menu) {
2495 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002496 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 }
2498
John Reck9c35b9c2012-05-30 10:08:50 -07002499 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002500 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002501 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002502 }
2503
2504 // Helper method for getting the top window.
2505 @Override
2506 public WebView getCurrentTopWebView() {
2507 return mTabControl.getCurrentTopWebView();
2508 }
2509
2510 @Override
2511 public WebView getCurrentWebView() {
2512 return mTabControl.getCurrentWebView();
2513 }
2514
2515 /*
2516 * This method is called as a result of the user selecting the options
2517 * menu to see the download window. It shows the download window on top of
2518 * the current window.
2519 */
2520 void viewDownloads() {
2521 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2522 mActivity.startActivity(intent);
2523 }
2524
John Reck30b065e2011-07-19 10:58:05 -07002525 int getActionModeHeight() {
2526 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2527 new int[] { android.R.attr.actionBarSize });
2528 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2529 actionBarSizeTypedArray.recycle();
2530 return size;
2531 }
2532
Michael Kolb8233fac2010-10-26 16:08:53 -07002533 // action mode
2534
John Reck9c35b9c2012-05-30 10:08:50 -07002535 @Override
2536 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002537 mUi.onActionModeStarted(mode);
2538 mActionMode = mode;
2539 }
2540
2541 /*
2542 * True if a custom ActionMode (i.e. find or select) is in use.
2543 */
2544 @Override
2545 public boolean isInCustomActionMode() {
2546 return mActionMode != null;
2547 }
2548
2549 /*
2550 * End the current ActionMode.
2551 */
2552 @Override
2553 public void endActionMode() {
2554 if (mActionMode != null) {
2555 mActionMode.finish();
2556 }
2557 }
2558
2559 /*
2560 * Called by find and select when they are finished. Replace title bars
2561 * as necessary.
2562 */
John Reck9c35b9c2012-05-30 10:08:50 -07002563 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002564 public void onActionModeFinished(ActionMode mode) {
2565 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002566 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002567 mActionMode = null;
2568 }
2569
2570 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002571 final Tab tab = getCurrentTab();
2572 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002573 }
2574
2575 // bookmark handling
2576
2577 /**
2578 * add the current page as a bookmark to the given folder id
2579 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002580 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002581 * bookmarked, and if it is, edit that bookmark. If false, and
2582 * the site is already bookmarked, do not attempt to edit the
2583 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002584 */
2585 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002586 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002587 WebView w = getCurrentTopWebView();
2588 if (w == null) {
2589 return null;
2590 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002591 Intent i = new Intent(mActivity,
2592 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002593 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2594 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002595 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002596 if (touchIconUrl != null) {
2597 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2598 WebSettings settings = w.getSettings();
2599 if (settings != null) {
2600 i.putExtra(AddBookmarkPage.USER_AGENT,
2601 settings.getUserAgentString());
2602 }
2603 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002604 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002606 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002607 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2608 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002609 // Put the dialog at the upper right of the screen, covering the
2610 // star on the title bar.
2611 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002612 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002613 }
2614
2615 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002616 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002617 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002618 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002619 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002620 }
2621
Vivek Sekharb54614f2014-05-01 19:03:37 -07002622 @Override
2623 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2624 boolean capture) {
2625 mUploadHandler = new UploadHandler(this);
2626 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2627 }
2628
Michael Kolb8233fac2010-10-26 16:08:53 -07002629 // thumbnails
2630
2631 /**
2632 * Return the desired width for thumbnail screenshots, which are stored in
2633 * the database, and used on the bookmarks screen.
2634 * @param context Context for finding out the density of the screen.
2635 * @return desired width for thumbnail screenshot.
2636 */
2637 static int getDesiredThumbnailWidth(Context context) {
2638 return context.getResources().getDimensionPixelOffset(
2639 R.dimen.bookmarkThumbnailWidth);
2640 }
2641
2642 /**
2643 * Return the desired height for thumbnail screenshots, which are stored in
2644 * the database, and used on the bookmarks screen.
2645 * @param context Context for finding out the density of the screen.
2646 * @return desired height for thumbnail screenshot.
2647 */
2648 static int getDesiredThumbnailHeight(Context context) {
2649 return context.getResources().getDimensionPixelOffset(
2650 R.dimen.bookmarkThumbnailHeight);
2651 }
2652
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002653 static void createScreenshotAsync(WebView view, int width, int height,
2654 final ValueCallback<Bitmap> cb) {
2655 if (view == null || width == 0 || height == 0) {
2656 return;
2657 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002658 view.getContentBitmapAsync(
2659 (float) width / view.getWidth(),
2660 new Rect(),
2661 new ValueCallback<Bitmap>() {
2662 @Override
2663 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002664 if (bitmap != null)
2665 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2666 cb.onReceiveValue(bitmap);
2667 }});
2668 }
2669
Michael Kolb8233fac2010-10-26 16:08:53 -07002670 private class Copy implements OnMenuItemClickListener {
2671 private CharSequence mText;
2672
John Reck9c35b9c2012-05-30 10:08:50 -07002673 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002674 public boolean onMenuItemClick(MenuItem item) {
2675 copy(mText);
2676 return true;
2677 }
2678
2679 public Copy(CharSequence toCopy) {
2680 mText = toCopy;
2681 }
2682 }
2683
Leon Scroggins63c02662010-11-18 15:16:27 -05002684 private static class Download implements OnMenuItemClickListener {
2685 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002686 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002687 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002688 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002689 private static final String FALLBACK_EXTENSION = "dat";
2690 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002691
John Reck9c35b9c2012-05-30 10:08:50 -07002692 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002693 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002694 if (DataUri.isDataUri(mText)) {
2695 saveDataUri();
2696 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002697 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002698 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002699 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002700 return true;
2701 }
2702
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002703 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2704 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002705 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002706 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002707 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002708 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002709 }
George Mount387d45d2011-10-07 15:57:53 -07002710
2711 /**
2712 * Treats mText as a data URI and writes its contents to a file
2713 * based on the current time.
2714 */
2715 private void saveDataUri() {
2716 FileOutputStream outputStream = null;
2717 try {
2718 DataUri uri = new DataUri(mText);
2719 File target = getTarget(uri);
2720 outputStream = new FileOutputStream(target);
2721 outputStream.write(uri.getData());
2722 final DownloadManager manager =
2723 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2724 manager.addCompletedDownload(target.getName(),
2725 mActivity.getTitle().toString(), false,
2726 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002727 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002728 } catch (IOException e) {
2729 Log.e(LOGTAG, "Could not save data URL");
2730 } finally {
2731 if (outputStream != null) {
2732 try {
2733 outputStream.close();
2734 } catch (IOException e) {
2735 // ignore close errors
2736 }
2737 }
2738 }
2739 }
2740
2741 /**
2742 * Creates a File based on the current time stamp and uses
2743 * the mime type of the DataUri to get the extension.
2744 */
2745 private File getTarget(DataUri uri) throws IOException {
2746 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002747 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002748 String nameBase = format.format(new Date());
2749 String mimeType = uri.getMimeType();
2750 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2751 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2752 if (extension == null) {
2753 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2754 extension = FALLBACK_EXTENSION;
2755 }
2756 extension = "." + extension; // createTempFile needs the '.'
2757 File targetFile = File.createTempFile(nameBase, extension, dir);
2758 return targetFile;
2759 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002760 }
2761
Cary Clark8974d282010-11-22 10:46:05 -05002762 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002763 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002764
John Reck9c35b9c2012-05-30 10:08:50 -07002765 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002766 public boolean onMenuItemClick(MenuItem item) {
2767 if (mWebView != null) {
2768 return mWebView.selectText();
2769 }
2770 return false;
2771 }
2772
2773 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002774 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002775 }
2776
2777 }
2778
Michael Kolb8233fac2010-10-26 16:08:53 -07002779 /********************** TODO: UI stuff *****************************/
2780
2781 // these methods have been copied, they still need to be cleaned up
2782
2783 /****************** tabs ***************************************************/
2784
2785 // basic tab interactions:
2786
2787 // it is assumed that tabcontrol already knows about the tab
2788 protected void addTab(Tab tab) {
2789 mUi.addTab(tab);
2790 }
2791
2792 protected void removeTab(Tab tab) {
2793 mUi.removeTab(tab);
2794 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002795 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002796 }
2797
Michael Kolbf2055602011-04-09 17:20:03 -07002798 @Override
2799 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002800 // monkey protection against delayed start
2801 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002802
2803 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2804 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002805 mTabControl.setCurrentTab(tab);
2806 // the tab is guaranteed to have a webview after setCurrentTab
2807 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002808
2809
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002810 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002811 //Purge active tabs
2812 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002813 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002814 }
2815
John Reck8bcafc12011-08-29 16:43:02 -07002816 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002817 Tab current = mTabControl.getCurrentTab();
2818 if (current != null
2819 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002820 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002821 }
2822 }
2823
John Reck26b18322011-06-21 13:08:58 -07002824 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002825 //Cancel navscreen request
2826 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002827 // Dismiss the subwindow if applicable.
2828 dismissSubWindow(appTab);
2829 // Since we might kill the WebView, remove it from the
2830 // content view first.
2831 mUi.detachTab(appTab);
2832 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002833 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002834 // TODO: analyze why the remove and add are necessary
2835 mUi.attachTab(appTab);
2836 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002837 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002838 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002839 } else {
2840 // If the tab was the current tab, we have to attach
2841 // it to the view system again.
2842 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002843 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002844 }
2845 }
2846
2847 // Remove the sub window if it exists. Also called by TabControl when the
2848 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002849 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002850 public void dismissSubWindow(Tab tab) {
2851 removeSubWindow(tab);
2852 // dismiss the subwindow. This will destroy the WebView.
2853 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002854 WebView wv = getCurrentTopWebView();
2855 if (wv != null) {
2856 wv.requestFocus();
2857 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002858 }
2859
2860 @Override
2861 public void removeSubWindow(Tab t) {
2862 if (t.getSubWebView() != null) {
2863 mUi.removeSubWindow(t.getSubViewContainer());
2864 }
2865 }
2866
2867 @Override
2868 public void attachSubWindow(Tab tab) {
2869 if (tab.getSubWebView() != null) {
2870 mUi.attachSubWindow(tab.getSubViewContainer());
2871 getCurrentTopWebView().requestFocus();
2872 }
2873 }
2874
Mathew Inwood29721c22011-06-29 17:55:24 +01002875 private Tab showPreloadedTab(final UrlData urlData) {
2876 if (!urlData.isPreloaded()) {
2877 return null;
2878 }
2879 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2880 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2881 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002882 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002883 // Could not submit query. Fallback to regular tab creation
2884 tabControl.destroy();
2885 return null;
2886 }
2887 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002888 // check tab count and make room for new tab
2889 if (!mTabControl.canCreateNewTab()) {
2890 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2891 if (leastUsed != null) {
2892 closeTab(leastUsed);
2893 }
2894 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002895 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002896 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002897 mTabControl.addPreloadedTab(t);
2898 addTab(t);
2899 setActiveTab(t);
2900 return t;
2901 }
2902
Michael Kolb7bcafde2011-05-09 13:55:59 -07002903 // open a non inconito tab with the given url data
2904 // and set as active tab
2905 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002906 Tab tab = showPreloadedTab(urlData);
2907 if (tab == null) {
2908 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002909 if ((tab != null) && !urlData.isEmpty()) {
2910 loadUrlDataIn(tab, urlData);
2911 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002912 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002913 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002914 }
2915
Michael Kolb843510f2010-12-09 10:51:49 -08002916 @Override
2917 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002918 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002919 }
2920
Michael Kolb8233fac2010-10-26 16:08:53 -07002921 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002922 public Tab openIncognitoTab() {
2923 return openTab(INCOGNITO_URI, true, true, false);
2924 }
2925
2926 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002927 public Tab openTab(String url, boolean incognito, boolean setActive,
2928 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002929 return openTab(url, incognito, setActive, useCurrent, null);
2930 }
2931
2932 @Override
2933 public Tab openTab(String url, Tab parent, boolean setActive,
2934 boolean useCurrent) {
2935 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2936 setActive, useCurrent, parent);
2937 }
2938
2939 public Tab openTab(String url, boolean incognito, boolean setActive,
2940 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002941 Tab tab = createNewTab(incognito, setActive, useCurrent);
2942 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002943 if (parent instanceof SnapshotTab) {
2944 addTab(tab);
2945 if (setActive)
2946 setActiveTab(tab);
2947 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002948 parent.addChildTab(tab);
2949 }
Michael Kolb519d2282011-05-09 17:03:19 -07002950 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002951 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002952 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002953 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002954 return tab;
2955 }
2956
2957 // this method will attempt to create a new tab
2958 // incognito: private browsing tab
2959 // setActive: ste tab as current tab
2960 // useCurrent: if no new tab can be created, return current tab
2961 private Tab createNewTab(boolean incognito, boolean setActive,
2962 boolean useCurrent) {
2963 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002964 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2965 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002966 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002967 if (mTabControl.canCreateNewTab()) {
2968 tab = mTabControl.createNewTab(incognito, !setActive);
2969 addTab(tab);
2970 if (setActive) {
2971 setActiveTab(tab);
2972 } else {
2973 tab.pause();
2974 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002975 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002976 if (useCurrent) {
2977 tab = mTabControl.getCurrentTab();
2978 reuseTab(tab, null);
2979 } else {
2980 mUi.showMaxTabsWarning();
2981 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002982 }
2983 }
2984 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002985 }
2986
John Reck2bc80422011-06-30 15:11:49 -07002987 @Override
2988 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2989 SnapshotTab tab = null;
2990 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07002991 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07002992 addTab(tab);
2993 if (setActive) {
2994 setActiveTab(tab);
2995 }
2996 } else {
2997 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002998 }
2999 return tab;
3000 }
3001
Michael Kolb8233fac2010-10-26 16:08:53 -07003002 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003003 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003004 * @return boolean True if we successfully switched to a different tab. If
3005 * the indexth tab is null, or if that tab is the same as
3006 * the current one, return false.
3007 */
3008 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003009 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003010 Tab currentTab = mTabControl.getCurrentTab();
3011 if (tab == null || tab == currentTab) {
3012 return false;
3013 }
3014 setActiveTab(tab);
3015 return true;
3016 }
3017
3018 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003019 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003020 closeCurrentTab(false);
3021 }
3022
3023 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003024 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003025 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003026 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003027 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003028 return;
3029 }
Michael Kolbc831b632011-05-11 09:30:34 -07003030 final Tab current = mTabControl.getCurrentTab();
3031 final int pos = mTabControl.getCurrentPosition();
3032 Tab newTab = current.getParent();
3033 if (newTab == null) {
3034 newTab = mTabControl.getTab(pos + 1);
3035 if (newTab == null) {
3036 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003037 }
3038 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003039 if (andQuit) {
3040 mTabControl.setCurrentTab(newTab);
3041 closeTab(current);
3042 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003043 // Close window
3044 closeTab(current);
3045 }
3046 }
3047
3048 /**
3049 * Close the tab, remove its associated title bar, and adjust mTabControl's
3050 * current tab to a valid value.
3051 */
3052 @Override
3053 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003054 if (tab == mTabControl.getCurrentTab()) {
3055 closeCurrentTab();
3056 } else {
3057 removeTab(tab);
3058 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003059 }
3060
Afzal Najamd4e33312012-04-26 01:54:01 -04003061 /**
3062 * Close all tabs except the current one
3063 */
3064 @Override
3065 public void closeOtherTabs() {
3066 int inactiveTabs = mTabControl.getTabCount() - 1;
3067 for (int i = inactiveTabs; i >= 0; i--) {
3068 Tab tab = mTabControl.getTab(i);
3069 if (tab != mTabControl.getCurrentTab()) {
3070 removeTab(tab);
3071 }
3072 }
3073 }
3074
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003076 protected void loadUrlFromContext(String url) {
3077 Tab tab = getCurrentTab();
3078 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003079 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003080 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003081 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003082 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003083 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003084 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003085 }
3086 }
3087 }
3088
3089 /**
3090 * Load the URL into the given WebView and update the title bar
3091 * to reflect the new load. Call this instead of WebView.loadUrl
3092 * directly.
3093 * @param view The WebView used to load url.
3094 * @param url The URL to load.
3095 */
John Reck71e51422011-07-01 16:49:28 -07003096 @Override
3097 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003098 loadUrl(tab, url, null);
3099 }
3100
3101 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3102 if (tab != null) {
3103 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003104 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003105 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003106 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003107 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003108 }
3109
3110 /**
3111 * Load UrlData into a Tab and update the title bar to reflect the new
3112 * load. Call this instead of UrlData.loadIn directly.
3113 * @param t The Tab used to load.
3114 * @param data The UrlData being loaded.
3115 */
3116 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003117 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003118 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003119 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003120 } else {
John Reck38b39652012-06-05 09:22:59 -07003121 if (t != null && data.mDisableUrlOverride) {
3122 t.disableUrlOverridingForLoad();
3123 }
John Reck26b18322011-06-21 13:08:58 -07003124 loadUrl(t, data.mUrl, data.mHeaders);
3125 }
3126 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003127 }
3128
John Reck30c714c2010-12-16 17:30:34 -08003129 @Override
3130 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003131 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003132 //In case of tab can go back (aka tab has navigation entry) do nothing
3133 //else just load homepage in current tab.
3134 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003135 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003136 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003137 }
3138
3139 void goBackOnePageOrQuit() {
3140 Tab current = mTabControl.getCurrentTab();
3141 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003142 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3143 showExitDialog(mActivity);
3144 } else {
3145 /*
3146 * Instead of finishing the activity, simply push this to the back
3147 * of the stack and let ActivityManager to choose the foreground
3148 * activity. As BrowserActivity is singleTask, it will be always the
3149 * root of the task. So we can use either true or false for
3150 * moveTaskToBack().
3151 */
3152 mActivity.moveTaskToBack(true);
3153 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003154 return;
3155 }
John Reckef654f12011-07-12 16:42:08 -07003156 if (current.canGoBack()) {
3157 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003158 } else {
3159 // Check to see if we are closing a window that was created by
3160 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003161 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003162 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003163 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003164 // Now we close the other tab
3165 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003166 } else if (BrowserConfig.getInstance(getContext())
3167 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3168 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003169 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003170 /*
3171 * Instead of finishing the activity, simply push this to the back
3172 * of the stack and let ActivityManager to choose the foreground
3173 * activity. As BrowserActivity is singleTask, it will be always the
3174 * root of the task. So we can use either true or false for
3175 * moveTaskToBack().
3176 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003177 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003178 }
3179 }
3180 }
3181
3182 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003183 * helper method for key handler
3184 * returns the current tab if it can't advance
3185 */
Michael Kolbc831b632011-05-11 09:30:34 -07003186 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003187 int pos = mTabControl.getCurrentPosition() + 1;
3188 if (pos >= mTabControl.getTabCount()) {
3189 pos = 0;
3190 }
3191 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003192 }
3193
3194 /**
3195 * helper method for key handler
3196 * returns the current tab if it can't advance
3197 */
Michael Kolbc831b632011-05-11 09:30:34 -07003198 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003199 int pos = mTabControl.getCurrentPosition() - 1;
3200 if ( pos < 0) {
3201 pos = mTabControl.getTabCount() - 1;
3202 }
3203 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003204 }
3205
John Reckbcef87f2012-02-03 14:58:34 -08003206 boolean isMenuOrCtrlKey(int keyCode) {
3207 return (KeyEvent.KEYCODE_MENU == keyCode)
3208 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3209 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3210 }
3211
Michael Kolb0035fad2011-03-14 13:25:28 -07003212 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003213 * handle key events in browser
3214 *
3215 * @param keyCode
3216 * @param event
3217 * @return true if handled, false to pass to super
3218 */
John Reck9c35b9c2012-05-30 10:08:50 -07003219 @Override
3220 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003221 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3222 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003223 if (!mUi.isComboViewShowing()) {
3224 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3225 true, false);
3226 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003227 return true;
3228 }
3229
Cary Clark160bbb92011-01-10 11:17:07 -05003230 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003231 // Even if MENU is already held down, we need to call to super to open
3232 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003233 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003234 mMenuIsDown = true;
3235 return false;
3236 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003237
Cary Clark8ff8c662010-12-29 15:03:05 -05003238 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003239 Tab tab = getCurrentTab();
3240 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003241
Cary Clark160bbb92011-01-10 11:17:07 -05003242 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3243 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003244
Michael Kolb8233fac2010-10-26 16:08:53 -07003245 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003246 case KeyEvent.KEYCODE_TAB:
3247 if (event.isCtrlPressed()) {
3248 if (event.isShiftPressed()) {
3249 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003250 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003251 } else {
3252 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003253 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003254 }
3255 return true;
3256 }
3257 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003258 case KeyEvent.KEYCODE_SPACE:
3259 // WebView/WebTextView handle the keys in the KeyDown. As
3260 // the Activity's shortcut keys are only handled when WebView
3261 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003262 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003263 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003264 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003265 pageDown();
3266 }
3267 return true;
3268 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003269 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003270 event.startTracking();
3271 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003272 case KeyEvent.KEYCODE_FORWARD:
3273 if (!noModifiers) break;
3274 tab.goForward();
3275 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003276 case KeyEvent.KEYCODE_DPAD_LEFT:
3277 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003278 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003279 return true;
3280 }
3281 break;
3282 case KeyEvent.KEYCODE_DPAD_RIGHT:
3283 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003284 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003285 return true;
3286 }
3287 break;
3288 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003289 if (ctrl) {
3290 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003291 return true;
3292 }
3293 break;
Michael Kolba4183062011-01-16 10:43:21 -08003294// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003295 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003296 if (ctrl ) {
3297 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003298 return true;
3299 }
3300 break;
Michael Kolba4183062011-01-16 10:43:21 -08003301// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003302// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003303// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003304// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003305// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003306// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003307// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003308// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003309// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003310// case KeyEvent.KEYCODE_M: // unused
3311// case KeyEvent.KEYCODE_N: // in Chrome: new window
3312// case KeyEvent.KEYCODE_O: // in Chrome: open file
3313// case KeyEvent.KEYCODE_P: // in Chrome: print page
3314// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003315// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003316// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3317 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003318 // we can't use the ctrl/shift flags, they check for
3319 // exclusive use of a modifier
3320 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003321 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003322 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003323 } else {
3324 openTabToHomePage();
3325 }
3326 return true;
3327 }
3328 break;
3329// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3330// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003331// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003332// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3333// case KeyEvent.KEYCODE_Y: // unused
3334// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003335 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003336 // it is a regular key and webview is not null
3337 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003338 }
3339
John Reck9c35b9c2012-05-30 10:08:50 -07003340 @Override
3341 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003342 switch(keyCode) {
3343 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003344 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003345 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003346 return true;
3347 }
3348 break;
3349 }
3350 return false;
3351 }
3352
John Reck9c35b9c2012-05-30 10:08:50 -07003353 @Override
3354 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003355 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003356 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003357 if (KeyEvent.KEYCODE_MENU == keyCode
3358 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003359 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003360 }
3361 }
Cary Clark160bbb92011-01-10 11:17:07 -05003362 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003363 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003364 case KeyEvent.KEYCODE_BACK:
3365 if (event.isTracking() && !event.isCanceled()) {
3366 onBackKey();
3367 return true;
3368 }
3369 break;
3370 }
3371 return false;
3372 }
3373
3374 public boolean isMenuDown() {
3375 return mMenuIsDown;
3376 }
3377
John Reck9c35b9c2012-05-30 10:08:50 -07003378 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003379 public void setupAutoFill(Message message) {
3380 // Open the settings activity at the AutoFill profile fragment so that
3381 // the user can create a new profile. When they return, we will dispatch
3382 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003383 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003384 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3385 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003386 }
John Reckb3417f02011-01-14 11:01:05 -08003387
John Reck9c35b9c2012-05-30 10:08:50 -07003388 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003389 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003390 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003391 return true;
3392 }
3393
John Reck1cf4b792011-07-26 10:22:22 -07003394 @Override
3395 public boolean shouldCaptureThumbnails() {
3396 return mUi.shouldCaptureThumbnails();
3397 }
3398
Michael Kolbc3af0672011-08-09 10:24:41 -07003399 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003400 public boolean supportsVoice() {
3401 PackageManager pm = mActivity.getPackageManager();
3402 List activities = pm.queryIntentActivities(new Intent(
3403 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3404 return activities.size() != 0;
3405 }
3406
3407 @Override
3408 public void startVoiceRecognizer() {
3409 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003410 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003411 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3412 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3413 mActivity.startActivityForResult(voice, VOICE_RESULT);
3414 }
3415
Pankaj Garg7b279f62014-08-12 14:47:18 -07003416 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003417 if (mLevel == level)
3418 return;
3419 mLevel = level;
3420 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003421 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3422 lp.dimAmount = level;
3423 mActivity.getWindow().setAttributes(lp);
3424 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3425 } else {
3426 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3427 }
3428 }
3429
Michael Kolb0b129122012-06-04 16:31:58 -07003430 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003431 public void setBlockEvents(boolean block) {
3432 mBlockEvents = block;
3433 }
3434
John Reck9c35b9c2012-05-30 10:08:50 -07003435 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003436 public boolean dispatchKeyEvent(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 dispatchKeyShortcutEvent(KeyEvent event) {
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 dispatchTouchEvent(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 dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003452 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003453 }
3454
John Reck9c35b9c2012-05-30 10:08:50 -07003455 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003456 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003457 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003458 }
3459
Pankaj Garg49b79252014-11-07 17:33:41 -08003460 @Override
3461 public boolean shouldShowAppMenu() {
3462 return true;
3463 }
3464
3465 @Override
3466 public int getMenuThemeResourceId() {
3467 return R.style.OverflowMenuTheme;
3468 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003469}