blob: 6728274237e60b5b8bcfa31c6f510a27924c26dc [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;
Mattias Nilsson561d1952011-10-04 10:18:50 +020043import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070044import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080045import android.graphics.BitmapFactory;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080046import android.graphics.Rect;
Tarun Nainanif5563b62015-01-21 18:52:59 -080047import android.media.AudioManager;
kaiyiza016da12013-08-26 17:50:22 +080048import android.net.ConnectivityManager;
49import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.net.Uri;
51import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080052import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080053import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.os.AsyncTask;
Tarun Nainanif5563b62015-01-21 18:52:59 -080055import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070057import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.os.Handler;
59import android.os.Message;
60import android.os.PowerManager;
61import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.provider.ContactsContract;
63import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080064import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070065import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070066import android.text.TextUtils;
67import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080068import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070069import android.view.ActionMode;
70import android.view.ContextMenu;
71import android.view.ContextMenu.ContextMenuInfo;
72import android.view.Gravity;
73import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070074import android.view.Menu;
75import android.view.MenuInflater;
76import android.view.MenuItem;
77import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070078import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070080import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070081import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070082import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080083import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080084import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050085import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070086
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.CookieManager;
88import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080089import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080090import org.codeaurora.swe.HttpAuthHandler;
91import org.codeaurora.swe.SslErrorHandler;
92import org.codeaurora.swe.WebSettings;
93import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070094import org.codeaurora.swe.WebBackForwardList;
95import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080096
Bijan Amirzada41242f22014-03-21 12:12:18 -070097import com.android.browser.IntentHandler.UrlData;
98import com.android.browser.UI.ComboViews;
Dave Tharp13f3f7c2015-07-16 14:58:51 -070099import com.android.browser.mdm.DownloadDirRestriction;
Dave Tharp9a082b12015-06-19 08:46:57 -0700100import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -0700101import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -0700102import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700103import com.android.browser.mynavigation.AddMyNavigationPage;
104import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700105import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700106import com.android.browser.platformsupport.BrowserContract;
107import com.android.browser.platformsupport.WebAddress;
108import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800109import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700110import com.android.browser.provider.BrowserProvider2.Thumbnails;
111import com.android.browser.provider.SnapshotProvider.Snapshots;
112import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800113import com.android.browser.appmenu.AppMenuHandler;
114import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700115
Michael Kolb8233fac2010-10-26 16:08:53 -0700116import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700117import java.io.File;
118import java.io.FileOutputStream;
119import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700120import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700121import java.text.DateFormat;
122import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700123import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700124import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700125import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800126import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200127import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700128import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700129
130/**
131 * Controller for browser
132 */
133public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800134 implements WebViewController, UiController, ActivityController,
135 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700136
137 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800138 private static final String SEND_APP_ID_EXTRA =
139 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Vivek Sekhared791da2015-02-22 12:39:05 -0800140 private static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800141 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
142 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800143
Vivek Sekharb54614f2014-05-01 19:03:37 -0700144 // Remind switch to data connection if wifi is unavailable
145 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800146
Michael Kolb8233fac2010-10-26 16:08:53 -0700147 // public message ids
148 public final static int LOAD_URL = 1001;
149 public final static int STOP_LOAD = 1002;
150
151 // Message Ids
152 private static final int FOCUS_NODE_HREF = 102;
153 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800154 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800157 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158
159 private static final int EMPTY_MENU = -1;
160
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700162 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 final static int PREFERENCES_PAGE = 3;
164 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000165 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700166 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800167 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000168
Michael Kolb8233fac2010-10-26 16:08:53 -0700169 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
170
171 // As the ids are dynamically created, we can't guarantee that they will
172 // be in sequence, so this static array maps ids to a window number.
173 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
174 { R.id.window_one_menu_id, R.id.window_two_menu_id,
175 R.id.window_three_menu_id, R.id.window_four_menu_id,
176 R.id.window_five_menu_id, R.id.window_six_menu_id,
177 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
178
179 // "source" parameter for Google search through search key
180 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
181 // "source" parameter for Google search through simplily type
182 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
183
George Mount387d45d2011-10-07 15:57:53 -0700184 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700185 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
186
John Reckd7dd9b22011-08-30 09:18:29 -0700187 // A bitmap that is re-used in createScreenshot as scratch space
188 private static Bitmap sThumbnailBitmap;
189
Michael Kolb8233fac2010-10-26 16:08:53 -0700190 private Activity mActivity;
191 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700192 private HomepageHandler mHomepageHandler;
193 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 private BrowserSettings mSettings;
195 private WebViewFactory mFactory;
196
197 private WakeLock mWakeLock;
198
199 private UrlHandler mUrlHandler;
200 private UploadHandler mUploadHandler;
201 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700202 private PageDialogsHandler mPageDialogsHandler;
203 private NetworkStateHandler mNetworkHandler;
204
Ben Murdoch8029a772010-11-16 11:58:21 +0000205 private Message mAutoFillSetupMessage;
206
kaiyiza016da12013-08-26 17:50:22 +0800207 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700208
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 // FIXME, temp address onPrepareMenu performance problem.
210 // When we move everything out of view, we should rewrite this.
211 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700212 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 private int mOldMenuState = EMPTY_MENU;
214 private Menu mCachedMenu;
215
Michael Kolb8233fac2010-10-26 16:08:53 -0700216 private boolean mMenuIsDown;
217
Pankaj Garg49b79252014-11-07 17:33:41 -0800218 private boolean mWasInPageLoad = false;
219 private AppMenuHandler mAppMenuHandler;
220
Michael Kolb8233fac2010-10-26 16:08:53 -0700221 // For select and find, we keep track of the ActionMode so that
222 // finish() can be called as desired.
223 private ActionMode mActionMode;
224
225 /**
226 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
227 * of whether the configuration has changed. The first onMenuOpened call
228 * after a configuration change is simply a reopening of the same menu
229 * (i.e. mIconView did not change).
230 */
231 private boolean mConfigChanged;
232
233 /**
234 * Keeps track of whether the options menu is open. This is important in
235 * determining whether to show or hide the title bar overlay
236 */
237 private boolean mOptionsMenuOpen;
238
239 /**
240 * Whether or not the options menu is in its bigger, popup menu form. When
241 * true, we want the title bar overlay to be gone. When false, we do not.
242 * Only meaningful if mOptionsMenuOpen is true.
243 */
244 private boolean mExtendedMenuOpen;
245
Michael Kolb8233fac2010-10-26 16:08:53 -0700246 private boolean mActivityPaused = true;
247 private boolean mLoadStopped;
248
249 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500250 // Checks to see when the bookmarks database has changed, and updates the
251 // Tabs' notion of whether they represent bookmarked sites.
252 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700253 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700254
Michael Kolbc3af0672011-08-09 10:24:41 -0700255 private boolean mBlockEvents;
256
Michael Kolb0b129122012-06-04 16:31:58 -0700257 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800258 private boolean mUpdateMyNavThumbnail;
259 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800260 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800261 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700262 private PowerConnectionReceiver mLowPowerReceiver;
263 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700264
Pankaj Garg634bf432015-07-13 09:54:21 -0700265 private boolean mCurrentPageBookmarked;
266
George Mount3636d0a2011-11-21 09:08:21 -0800267 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 mActivity = browser;
269 mSettings = BrowserSettings.getInstance();
270 mTabControl = new TabControl(this);
271 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700272 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800273 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700274 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700275
276 mUrlHandler = new UrlHandler(this);
277 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
279
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500281 mBookmarksObserver = new ContentObserver(mHandler) {
282 @Override
283 public void onChange(boolean selfChange) {
284 int size = mTabControl.getTabCount();
285 for (int i = 0; i < size; i++) {
286 mTabControl.getTab(i).updateBookmarkedStatus();
287 }
288 }
289
290 };
291 browser.getContentResolver().registerContentObserver(
292 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700293
294 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700295 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800296 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700297 }
298
John Reck9c35b9c2012-05-30 10:08:50 -0700299 @Override
300 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700301 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800302 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800303 // mCrashRecoverHandler has any previously saved state.
304 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700305 }
306
George Mount3636d0a2011-11-21 09:08:21 -0800307 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800308 // we dont want to ever recover incognito tabs
309 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700310
Patrick Scott7d50a932011-02-04 09:27:26 -0500311 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700312 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500313 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000314
Michael Kolbc831b632011-05-11 09:30:34 -0700315 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500316 // Not able to restore so we go ahead and clear session cookies. We
317 // must do this before trying to login the user as we don't want to
318 // clear any session cookies set during login.
319 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700320 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800321 if (intent == null) {
322 // This won't happen under common scenarios. The icicle is
323 // not null, but there aren't any tabs to restore.
324 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700325 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800326 final Bundle extra = intent.getExtras();
327 // Create an initial tab.
328 // If the intent is ACTION_VIEW and data is not null, the Browser is
329 // invoked to view the content by another application. In this case,
330 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800331 UrlData urlData = null;
332 if (intent.getData() != null
333 && Intent.ACTION_VIEW.equals(intent.getAction())
334 && intent.getData().toString().startsWith("content://")) {
335 urlData = new UrlData(intent.getData().toString());
336 } else {
337 urlData = IntentHandler.getUrlDataFromIntent(intent);
338 }
George Mount3636d0a2011-11-21 09:08:21 -0800339 Tab t = null;
340 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700341 String landingPage = mActivity.getResources().getString(
342 R.string.def_landing_page);
343 if (!landingPage.isEmpty()) {
344 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800345 } else {
346 t = openTabToHomePage();
347 }
George Mount3636d0a2011-11-21 09:08:21 -0800348 } else {
349 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700350 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800351 }
352 if (t != null) {
353 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
354 }
355 WebView webView = t.getWebView();
356 if (extra != null) {
357 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
358 if (scale > 0 && scale <= 1000) {
359 webView.setInitialScale(scale);
360 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 }
362 }
John Reckd8c74522011-06-14 08:45:00 -0700363 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700364 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700365 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500366 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700367 List<Tab> tabs = mTabControl.getTabs();
368 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700369
John Reck1cf4b792011-07-26 10:22:22 -0700370 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700371 //handle restored pages that may require a JS interface
372 if (t.getWebView() != null) {
373 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
374 if (backForwardList != null) {
375 for (int i = 0; i < backForwardList.getSize(); i++) {
376 WebHistoryItem item = backForwardList.getItemAtIndex(i);
377 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
378 }
379 }
380 }
John Reck1cf4b792011-07-26 10:22:22 -0700381 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700382 if (t != mTabControl.getCurrentTab()) {
383 t.pause();
384 }
John Reck1cf4b792011-07-26 10:22:22 -0700385 }
386 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700387 if (tabs.size() == 0) {
388 openTabToHomePage();
389 }
John Reck1cf4b792011-07-26 10:22:22 -0700390 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700391 // TabControl.restoreState() will create a new tab even if
392 // restoring the state fails.
393 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800394 // Intent is non-null when framework thinks the browser should be
395 // launching with a new intent (icicle is null).
396 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700397 mIntentHandler.onNewIntent(intent);
398 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700399 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700400 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700401 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800402 if (jsFlags.trim().length() != 0) {
403 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700404 }
George Mount3636d0a2011-11-21 09:08:21 -0800405 if (intent != null
406 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700407 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800408 }
Site Maoabb7bd32015-03-20 15:34:02 -0700409 mLowPowerReceiver = new PowerConnectionReceiver();
410 mPowerChangeReceiver = new PowerConnectionReceiver();
411
412 //always track the android framework's power save mode
413 IntentFilter filter = new IntentFilter();
414 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
415 // Power save mode only exists in Lollipop and above
416 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
417 }
418 filter.addAction(Intent.ACTION_BATTERY_OKAY);
419 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700420 }
421
John Reck1cf4b792011-07-26 10:22:22 -0700422 private static class PruneThumbnails implements Runnable {
423 private Context mContext;
424 private List<Long> mIds;
425
426 PruneThumbnails(Context context, List<Long> preserveIds) {
427 mContext = context.getApplicationContext();
428 mIds = preserveIds;
429 }
430
431 @Override
432 public void run() {
433 ContentResolver cr = mContext.getContentResolver();
434 if (mIds == null || mIds.size() == 0) {
435 cr.delete(Thumbnails.CONTENT_URI, null, null);
436 } else {
437 int length = mIds.size();
438 StringBuilder where = new StringBuilder();
439 where.append(Thumbnails._ID);
440 where.append(" not in (");
441 for (int i = 0; i < length; i++) {
442 where.append(mIds.get(i));
443 if (i < (length - 1)) {
444 where.append(",");
445 }
446 }
447 where.append(")");
448 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
449 }
450 }
451
452 }
453
Michael Kolb1514bb72010-11-22 09:11:48 -0800454 @Override
455 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700456 return mFactory;
457 }
458
459 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800460 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700461 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700462 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800463 }
464
465 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800466 public void createSubWindow(Tab tab) {
467 endActionMode();
468 WebView mainView = tab.getWebView();
469 WebView subView = mFactory.createWebView((mainView == null)
470 ? false
471 : mainView.isPrivateBrowsingEnabled());
472 mUi.createSubWindow(tab, subView);
473 }
474
475 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700476 public Context getContext() {
477 return mActivity;
478 }
479
480 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700481 public Activity getActivity() {
482 return mActivity;
483 }
484
485 void setUi(UI ui) {
486 mUi = ui;
487 }
488
Michael Kolbaf63dba2012-05-16 12:58:05 -0700489 @Override
490 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700491 return mSettings;
492 }
493
494 IntentHandler getIntentHandler() {
495 return mIntentHandler;
496 }
497
498 @Override
499 public UI getUi() {
500 return mUi;
501 }
502
503 int getMaxTabs() {
504 return mActivity.getResources().getInteger(R.integer.max_tabs);
505 }
506
507 @Override
508 public TabControl getTabControl() {
509 return mTabControl;
510 }
511
Michael Kolb1bf23132010-11-19 12:55:12 -0800512 @Override
513 public List<Tab> getTabs() {
514 return mTabControl.getTabs();
515 }
516
Michael Kolb8233fac2010-10-26 16:08:53 -0700517 private void startHandler() {
518 mHandler = new Handler() {
519
520 @Override
521 public void handleMessage(Message msg) {
522 switch (msg.what) {
523 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700524 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800526 case UNKNOWN_TYPE_MSG:
527 HashMap unknownTypeMap = (HashMap) msg.obj;
528 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
529 /*
530 * When the context menu is shown to the user
531 * we need to assure that its happening on the current webview
532 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
533 */
534 if (getCurrentWebView() != viewForUnknownType)
535 break;
536
537 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800538 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800539 WebView.HitTestResult result = new WebView.HitTestResult();
540
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800541 // Prevent unnecessary calls to context menu
542 // if url and image src are null
543 if (unknown_type_src == null && unknown_type_url == null)
544 break;
545
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800546 //setting the HitTestResult with new RESULT TYPE
547 if (!TextUtils.isEmpty(unknown_type_src)) {
548 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
549 result.setExtra(unknown_type_src);
550 } else {
551 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
552 result.setExtra("about:blank");
553 }
554
555 mResult = result;
556 openContextMenu(viewForUnknownType);
557 mResult = null;
558
559 break;
560
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 case FOCUS_NODE_HREF:
562 {
563 String url = (String) msg.getData().get("url");
564 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500565 String src = (String) msg.getData().get("src");
566 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700567 if (TextUtils.isEmpty(url)) {
568 break;
569 }
570 HashMap focusNodeMap = (HashMap) msg.obj;
571 WebView view = (WebView) focusNodeMap.get("webview");
572 // Only apply the action if the top window did not change.
573 if (getCurrentTopWebView() != view) {
574 break;
575 }
576 switch (msg.arg1) {
577 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700578 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500580 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700581 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500582 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500583 case R.id.open_newtab_context_menu_id:
584 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700585 openTab(url, parent,
586 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500587 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700588 case R.id.copy_link_context_menu_id:
589 copy(url);
590 break;
591 case R.id.save_link_context_menu_id:
592 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500593 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800594 mActivity, url, view.getSettings().getUserAgentString(),
595 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700596 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700597 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700598 if(title == null || title == "")
599 title = url;
600
601 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
602 //SWE TODO: No thumbnail support for the url obtained via
603 //browser context menu as its not loaded in webview.
604 if (bookmarkIntent != null) {
605 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
606 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
607 mActivity.startActivity(bookmarkIntent);
608 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700609 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700610 }
611 break;
612 }
613
614 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700615 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 break;
617
618 case STOP_LOAD:
619 stopLoading();
620 break;
621
622 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700623 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700624 mWakeLock.release();
625 // if we reach here, Browser should be still in the
626 // background loading after WAKELOCK_TIMEOUT (5-min).
627 // To avoid burning the battery, stop loading.
628 mTabControl.stopAllLoading();
629 }
630 break;
631
kaiyiz591110b2013-08-06 17:11:06 +0800632 case OPEN_MENU:
633 if (!mOptionsMenuOpen && mActivity != null ) {
634 mActivity.openOptionsMenu();
635 }
636 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 }
638 }
639 };
640
641 }
642
John Reckef654f12011-07-12 16:42:08 -0700643 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200644 public Tab getCurrentTab() {
645 return mTabControl.getCurrentTab();
646 }
647
Michael Kolbba99c5d2010-11-29 14:57:41 -0800648 @Override
649 public void shareCurrentPage() {
650 shareCurrentPage(mTabControl.getCurrentTab());
651 }
652
653 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700654 if (tab == null || tab.getWebView() == null)
655 return;
656
657 final Tab mytab = tab;
658 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
659 @Override
660 public void onReceiveValue(Bitmap bitmap) {
661 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
662 mytab.getFavicon(), bitmap);
663 }
664 };
665
666 createScreenshotAsync(
667 tab.getWebView(),
668 getDesiredThumbnailWidth(mActivity),
669 getDesiredThumbnailHeight(mActivity),
670 new ValueCallback<Bitmap>() {
671 @Override
672 public void onReceiveValue(Bitmap bitmap) {
673 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
674 mytab.getFavicon(), bitmap);
675 }
676 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800677 }
678
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 /**
680 * Share a page, providing the title, url, favicon, and a screenshot. Uses
681 * an {@link Intent} to launch the Activity chooser.
682 * @param c Context used to launch a new Activity.
683 * @param title Title of the page. Stored in the Intent with
684 * {@link Intent#EXTRA_SUBJECT}
685 * @param url URL of the page. Stored in the Intent with
686 * {@link Intent#EXTRA_TEXT}
687 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
688 * with {@link Browser#EXTRA_SHARE_FAVICON}
689 * @param screenshot Bitmap of a screenshot of the page. Stored in the
690 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
691 */
692 static final void sharePage(Context c, String title, String url,
693 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700694
695 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
696 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
697 R.layout.app_row, sDialog.getApps());
698 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700699 }
700
701 private void copy(CharSequence text) {
702 ClipboardManager cm = (ClipboardManager) mActivity
703 .getSystemService(Context.CLIPBOARD_SERVICE);
704 cm.setText(text);
705 }
706
707 // lifecycle
708
John Reck9c35b9c2012-05-30 10:08:50 -0700709 @Override
710 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700711 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800712 // update the menu in case of a locale change
713 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800714 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800715 if (mOptionsMenuOpen) {
716 mActivity.closeOptionsMenu();
717 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
718 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700719 if (mPageDialogsHandler != null) {
720 mPageDialogsHandler.onConfigurationChanged(config);
721 }
722 mUi.onConfigurationChanged(config);
723 }
724
725 @Override
726 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700727 if (!mUi.isWebShowing()) {
728 mUi.showWeb(false);
729 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700730 mIntentHandler.onNewIntent(intent);
731 }
732
John Reck9c35b9c2012-05-30 10:08:50 -0700733 @Override
734 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800735 if (mUi.isCustomViewShowing()) {
736 hideCustomView();
737 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700738 if (mActivityPaused) {
739 Log.e(LOGTAG, "BrowserActivity is already paused.");
740 return;
741 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800743 Tab tab = mTabControl.getCurrentTab();
744 if (tab != null) {
745 tab.pause();
746 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700747 if (mWakeLock == null) {
748 PowerManager pm = (PowerManager) mActivity
749 .getSystemService(Context.POWER_SERVICE);
750 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
751 }
Michael Kolb70976932010-11-30 11:34:01 -0800752 mWakeLock.acquire();
753 mHandler.sendMessageDelayed(mHandler
754 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
755 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 }
757 mUi.onPause();
758 mNetworkHandler.onPause();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100759 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700760 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700761 }
762
John Reck9c35b9c2012-05-30 10:08:50 -0700763 @Override
764 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700765 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800766 Bundle saveState = createSaveState();
767
768 // crash recovery manages all save & restore state
769 mCrashRecoveryHandler.writeState(saveState);
770 mSettings.setLastRunPaused(true);
771 }
772
773 /**
774 * Save the current state to outState. Does not write the state to
775 * disk.
776 * @return Bundle containing the current state of all tabs.
777 */
778 /* package */ Bundle createSaveState() {
779 Bundle saveState = new Bundle();
780 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800781 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700782 }
783
John Reck9c35b9c2012-05-30 10:08:50 -0700784 @Override
785 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 if (!mActivityPaused) {
787 Log.e(LOGTAG, "BrowserActivity is already resumed.");
788 return;
789 }
George Mount3636d0a2011-11-21 09:08:21 -0800790 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700791 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800792 Tab current = mTabControl.getCurrentTab();
793 if (current != null) {
794 current.resume();
795 resumeWebViewTimers(current);
796 }
John Reckf57c0292011-07-21 18:15:39 -0700797 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200798
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 mUi.onResume();
800 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100801 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700802 if (mVoiceResult != null) {
803 mUi.onVoiceResult(mVoiceResult);
804 mVoiceResult = null;
805 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700806 if (current != null && current.getWebView().isShowingCrashView())
807 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700808 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700809 }
810
John Reckf57c0292011-07-21 18:15:39 -0700811 private void releaseWakeLock() {
812 if (mWakeLock != null && mWakeLock.isHeld()) {
813 mHandler.removeMessages(RELEASE_WAKELOCK);
814 mWakeLock.release();
815 }
816 }
817
Michael Kolb70976932010-11-30 11:34:01 -0800818 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800819 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800820 * @param tab guaranteed non-null
821 */
822 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700823 boolean inLoad = tab.inPageLoad();
824 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
825 CookieSyncManager.getInstance().startSync();
826 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100827 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700828 }
829 }
830
Michael Kolb70976932010-11-30 11:34:01 -0800831 /**
832 * Pause all WebView timers using the WebView of the given tab
833 * @param tab
834 * @return true if the timers are paused or tab is null
835 */
836 private boolean pauseWebViewTimers(Tab tab) {
837 if (tab == null) {
838 return true;
839 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100841 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700843 }
Michael Kolb70976932010-11-30 11:34:01 -0800844 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 }
846
John Reck9c35b9c2012-05-30 10:08:50 -0700847 @Override
848 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800849 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
851 mUploadHandler = null;
852 }
853 if (mTabControl == null) return;
854 mUi.onDestroy();
855 // Remove the current tab and sub window
856 Tab t = mTabControl.getCurrentTab();
857 if (t != null) {
858 dismissSubWindow(t);
859 removeTab(t);
860 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500861 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 // Destroy all the tabs
863 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700864 // Unregister receiver
865 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 }
867
868 protected boolean isActivityPaused() {
869 return mActivityPaused;
870 }
871
John Reck9c35b9c2012-05-30 10:08:50 -0700872 @Override
873 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700874 mTabControl.freeMemory();
875 }
876
877 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 public void stopLoading() {
879 mLoadStopped = true;
880 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700881 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700882 if (w != null) {
883 w.stopLoading();
884 mUi.onPageStopped(tab);
885 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 }
887
888 boolean didUserStopLoading() {
889 return mLoadStopped;
890 }
891
kaiyiza016da12013-08-26 17:50:22 +0800892 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700893 final String reminderType = getContext().getResources().getString(
894 R.string.def_wifi_browser_interaction_remind_type);
895 final String selectionConnnection = getContext().getResources().getString(
896 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700897 final String wifiSelection = getContext().getResources().getString(
898 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700899
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700900 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
901 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700902 return;
903
kaiyiza016da12013-08-26 17:50:22 +0800904 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700905 Context.CONNECTIVITY_SERVICE);
906 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700907 WifiManager wifiMgr = (WifiManager) this.getContext()
908 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700909 if (networkInfo == null
910 || (networkInfo != null && (networkInfo.getType() !=
911 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700912 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
913 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700914 List<ScanResult> list = wifiMgr.getScanResults();
915 // Have no AP's for Wifi's fall back to data
916 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700917 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700918 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
919 this.getContext().startActivity(intent);
920 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700921 // Request to select Wifi AP
922 try {
923 Intent intent = new Intent(wifiSelection);
924 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
925 this.getContext().startActivity(intent);
926 } catch (Exception e) {
927 String err_msg = this.getContext().getString(
928 R.string.acivity_not_found, wifiSelection);
929 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
930 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700931 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700932 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800933 }
934 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700935
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 // WebViewController
937
938 @Override
John Reck324d4402011-01-11 16:56:42 -0800939 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700940
Michael Kolb8233fac2010-10-26 16:08:53 -0700941 // reset sync timer to avoid sync starts during loading a page
942 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700943 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700944 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800945 boolean networkNotifier = BrowserConfig.getInstance(getContext())
946 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700947 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700948 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800949 } else {
950 if (!mNetworkHandler.isNetworkUp()) {
951 view.setNetworkAvailable(false);
952 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700953 }
954
955 // when BrowserActivity just starts, onPageStarted may be called before
956 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
957 // to start the timer. As we won't switch tabs while an activity is in
958 // pause state, we can ensure calling resume and pause in pair.
959 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800960 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700961 }
962 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700963 endActionMode();
964
John Reck30c714c2010-12-16 17:30:34 -0800965 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700966
John Reck324d4402011-01-11 16:56:42 -0800967 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700968 // update the bookmark database for favicon
969 maybeUpdateFavicon(tab, null, url, favicon);
970
971 Performance.tracePageStart(url);
972
973 // Performance probe
974 if (false) {
975 Performance.onPageStarted();
976 }
977
978 }
979
980 @Override
John Reck324d4402011-01-11 16:56:42 -0800981 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700982 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800983 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200984
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 // Performance probe
986 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800987 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700988 }
989
Tarun Nainani8eb00912014-07-17 12:28:32 -0700990 tab.onPageFinished();
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700991 maybeUpdateFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700992
Michael Kolb8233fac2010-10-26 16:08:53 -0700993 Performance.tracePageFinished();
994 }
995
996 @Override
John Reck30c714c2010-12-16 17:30:34 -0800997 public void onProgressChanged(Tab tab) {
998 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700999
1000 if (newProgress == 100) {
1001 CookieSyncManager.getInstance().sync();
1002 // onProgressChanged() may continue to be called after the main
1003 // frame has finished loading, as any remaining sub frames continue
1004 // to load. We'll only get called once though with newProgress as
1005 // 100 when everything is loaded. (onPageFinished is called once
1006 // when the main frame completes loading regardless of the state of
1007 // any sub frames so calls to onProgressChanges may continue after
1008 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001009 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001010 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001011 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001012 } else if (mWasInPageLoad) {
1013 mWasInPageLoad = false;
1014 updateInLoadMenuItems(mCachedMenu, tab);
1015 }
1016
1017 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001018 // pause the WebView timer and release the wake lock if it is
1019 // finished while BrowserActivity is in pause state.
1020 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001021 }
1022 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001023 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001025 // still loading
1026 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001027 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001028 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001029 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001030 } else {
1031 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001032 }
1033 }
John Reck30c714c2010-12-16 17:30:34 -08001034 mUi.onProgressChanged(tab);
1035 }
1036
1037 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001038 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001039 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 }
1041
1042 @Override
1043 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001044 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001045 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001046 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001047 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1048 return;
1049 }
1050 // Update the title in the history database if not in private browsing mode
1051 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001052 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001053 }
1054 }
1055
1056 @Override
1057 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001058 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001059 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1060 }
1061
1062 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001063 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1064 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001065 }
1066
1067 @Override
1068 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1069 if (mMenuIsDown) {
1070 // only check shortcut key when MENU is held
1071 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1072 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001073 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001074 int keyCode = event.getKeyCode();
1075 // We need to send almost every key to WebKit. However:
1076 // 1. We don't want to block the device on the renderer for
1077 // some keys like menu, home, call.
1078 // 2. There are no WebKit equivalents for some of these keys
1079 // (see app/keyboard_codes_win.h)
1080 // Note that these are not the same set as KeyEvent.isSystemKey:
1081 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1082 if (keyCode == KeyEvent.KEYCODE_MENU ||
1083 keyCode == KeyEvent.KEYCODE_HOME ||
1084 keyCode == KeyEvent.KEYCODE_BACK ||
1085 keyCode == KeyEvent.KEYCODE_CALL ||
1086 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1087 keyCode == KeyEvent.KEYCODE_POWER ||
1088 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1089 keyCode == KeyEvent.KEYCODE_CAMERA ||
1090 keyCode == KeyEvent.KEYCODE_FOCUS ||
1091 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1092 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1093 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1094 return true;
1095 }
1096
1097 // We also have to intercept some shortcuts before we send them to the ContentView.
1098 if (event.isCtrlPressed() && (
1099 keyCode == KeyEvent.KEYCODE_TAB ||
1100 keyCode == KeyEvent.KEYCODE_W ||
1101 keyCode == KeyEvent.KEYCODE_F4)) {
1102 return true;
1103 }
1104
1105 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 }
1107
Tarun Nainanif5563b62015-01-21 18:52:59 -08001108 private void handleMediaKeyEvent(KeyEvent event) {
1109
1110 int keyCode = event.getKeyCode();
1111 // send media key events to audio manager
1112 if (Build.VERSION.SDK_INT >= 19) {
1113
1114 switch (keyCode) {
1115 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1116 case KeyEvent.KEYCODE_MEDIA_STOP:
1117 case KeyEvent.KEYCODE_MEDIA_NEXT:
1118 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1119 case KeyEvent.KEYCODE_MEDIA_REWIND:
1120 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1121 case KeyEvent.KEYCODE_MUTE:
1122 case KeyEvent.KEYCODE_MEDIA_PLAY:
1123 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1124 case KeyEvent.META_SHIFT_RIGHT_ON:
1125 case KeyEvent.KEYCODE_MEDIA_EJECT:
1126 case KeyEvent.KEYCODE_MEDIA_RECORD:
1127 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1128
1129 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1130 .getSystemService(Context.AUDIO_SERVICE);
1131 audioManager.dispatchMediaKeyEvent(event);
1132 }
1133 }
1134 }
1135
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 @Override
John Reck997b1b72012-04-19 18:08:25 -07001137 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001139 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001140 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001141 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001142 } else {
John Reck997b1b72012-04-19 18:08:25 -07001143 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 }
1145 }
John Reck997b1b72012-04-19 18:08:25 -07001146 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001147 }
1148
1149 @Override
John Reck324d4402011-01-11 16:56:42 -08001150 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001151 // Don't save anything in private browsing mode or when disabling history
1152 // for regular tabs is enabled
1153 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1154 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1155 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001156
John Reck49a603c2011-03-03 09:33:05 -08001157 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001158
John Reck324d4402011-01-11 16:56:42 -08001159 if (TextUtils.isEmpty(url)
1160 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 return;
1162 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001163
John Reckf57c0292011-07-21 18:15:39 -07001164 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001165 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 }
1167
1168 @Override
1169 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1170 AsyncTask<Void, Void, String[]> task =
1171 new AsyncTask<Void, Void, String[]>() {
1172 @Override
1173 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001174 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001175 }
1176 @Override
1177 public void onPostExecute(String[] result) {
1178 callback.onReceiveValue(result);
1179 }
1180 };
1181 task.execute();
1182 }
1183
1184 @Override
1185 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1186 final HttpAuthHandler handler, final String host,
1187 final String realm) {
1188 String username = null;
1189 String password = null;
1190
1191 boolean reuseHttpAuthUsernamePassword
1192 = handler.useHttpAuthUsernamePassword();
1193
1194 if (reuseHttpAuthUsernamePassword && view != null) {
1195 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1196 if (credentials != null && credentials.length == 2) {
1197 username = credentials[0];
1198 password = credentials[1];
1199 }
1200 }
1201
1202 if (username != null && password != null) {
1203 handler.proceed(username, password);
1204 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001205 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1207 } else {
1208 handler.cancel();
1209 }
1210 }
1211 }
1212
1213 @Override
1214 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001215 String contentDisposition, String mimetype, String referer,
1216 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001217 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001218 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001219 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001220 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001221 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001222 // This Tab was opened for the sole purpose of downloading a
1223 // file. Remove it.
1224 if (tab == mTabControl.getCurrentTab()) {
1225 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001226 if (tab.getDerivedFromIntent())
1227 closeTab(tab);
1228 else
1229 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001230 } else {
1231 // In this case, it is not.
1232 closeTab(tab);
1233 }
1234 }
1235 }
1236
1237 @Override
1238 public Bitmap getDefaultVideoPoster() {
1239 return mUi.getDefaultVideoPoster();
1240 }
1241
1242 @Override
1243 public View getVideoLoadingProgressView() {
1244 return mUi.getVideoLoadingProgressView();
1245 }
1246
1247 @Override
1248 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1249 SslError error) {
1250 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1251 }
1252
1253 // helper method
1254
1255 /*
1256 * Update the favorites icon if the private browsing isn't enabled and the
1257 * icon is valid.
1258 */
1259 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1260 final String url, Bitmap favicon) {
1261 if (favicon == null) {
1262 return;
1263 }
1264 if (!tab.isPrivateBrowsingEnabled()) {
1265 Bookmarks.updateFavicon(mActivity
1266 .getContentResolver(), originalUrl, url, favicon);
1267 }
1268 }
1269
Leon Scroggins4cd97792010-12-03 15:31:56 -05001270 @Override
1271 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001272 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001273 mUi.bookmarkedStatusHasChanged(tab);
1274 }
1275
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 // end WebViewController
1277
1278 protected void pageUp() {
1279 getCurrentTopWebView().pageUp(false);
1280 }
1281
1282 protected void pageDown() {
1283 getCurrentTopWebView().pageDown(false);
1284 }
1285
1286 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001287 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001288 public void editUrl() {
1289 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001290 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 }
1292
John Reck9c35b9c2012-05-30 10:08:50 -07001293 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001294 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001295 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001296 if (tab.inForeground()) {
1297 if (mUi.isCustomViewShowing()) {
1298 callback.onCustomViewHidden();
1299 return;
1300 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001301 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001302 // Save the menu state and set it to empty while the custom
1303 // view is showing.
1304 mOldMenuState = mMenuState;
1305 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001306 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001307 }
1308 }
1309
1310 @Override
1311 public void hideCustomView() {
1312 if (mUi.isCustomViewShowing()) {
1313 mUi.onHideCustomView();
1314 // Reset the old menu state.
1315 mMenuState = mOldMenuState;
1316 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001317 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001318 }
1319 }
1320
John Reck9c35b9c2012-05-30 10:08:50 -07001321 @Override
1322 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001323 Intent intent) {
1324 if (getCurrentTopWebView() == null) return;
1325 switch (requestCode) {
1326 case PREFERENCES_PAGE:
1327 if (resultCode == Activity.RESULT_OK && intent != null) {
1328 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001329 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001330 mTabControl.removeParentChildRelationShips();
1331 }
1332 }
1333 break;
1334 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001335 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001336 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001338 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001339 case AUTOFILL_SETUP:
1340 // Determine whether a profile was actually set up or not
1341 // and if so, send the message back to the WebTextView to
1342 // fill the form with the new profile.
1343 if (getSettings().getAutoFillProfile() != null) {
1344 mAutoFillSetupMessage.sendToTarget();
1345 mAutoFillSetupMessage = null;
1346 }
1347 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001348 case COMBO_VIEW:
1349 if (intent == null || resultCode != Activity.RESULT_OK) {
1350 break;
1351 }
John Reck3ba45532011-08-11 16:26:53 -07001352 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001353 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1354 Tab t = getCurrentTab();
1355 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001356 mUpdateMyNavThumbnail = true;
1357 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001358 loadUrl(t, uri.toString());
1359 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1360 String[] urls = intent.getStringArrayExtra(
1361 ComboViewActivity.EXTRA_OPEN_ALL);
1362 Tab parent = getCurrentTab();
1363 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001364 if (url != null) {
1365 parent = openTab(url, parent,
1366 !mSettings.openInBackground(), true);
1367 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001368 }
1369 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1370 long id = intent.getLongExtra(
1371 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1372 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001373 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001374 }
1375 }
1376 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001377 case VOICE_RESULT:
1378 if (resultCode == Activity.RESULT_OK && intent != null) {
1379 ArrayList<String> results = intent.getStringArrayListExtra(
1380 RecognizerIntent.EXTRA_RESULTS);
1381 if (results.size() >= 1) {
1382 mVoiceResult = results.get(0);
1383 }
1384 }
1385 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001386 case MY_NAVIGATION:
1387 if (intent == null || resultCode != Activity.RESULT_OK) {
1388 break;
1389 }
1390
1391 if (intent.getBooleanExtra("need_refresh", false) &&
1392 getCurrentTopWebView() != null) {
1393 getCurrentTopWebView().reload();
1394 }
1395 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 default:
1397 break;
1398 }
1399 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001400 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 }
1402
1403 /**
1404 * Open the Go page.
1405 * @param startWithHistory If true, open starting on the history tab.
1406 * Otherwise, start with the bookmarks tab.
1407 */
1408 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001409 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 if (mTabControl.getCurrentWebView() == null) {
1411 return;
1412 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001413 // clear action mode
1414 if (isInCustomActionMode()) {
1415 endActionMode();
1416 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 Bundle extras = new Bundle();
1418 // Disable opening in a new window if we have maxed out the windows
1419 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1420 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001421 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001422 }
1423
1424 // combo view callbacks
1425
Michael Kolb8233fac2010-10-26 16:08:53 -07001426 // key handling
1427 protected void onBackKey() {
1428 if (!mUi.onBackKey()) {
1429 WebView subwindow = mTabControl.getCurrentSubWindow();
1430 if (subwindow != null) {
1431 if (subwindow.canGoBack()) {
1432 subwindow.goBack();
1433 } else {
1434 dismissSubWindow(mTabControl.getCurrentTab());
1435 }
1436 } else {
1437 goBackOnePageOrQuit();
1438 }
1439 }
1440 }
1441
Michael Kolb4bd767d2011-05-27 11:33:55 -07001442 protected boolean onMenuKey() {
1443 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001444 }
1445
Michael Kolb8233fac2010-10-26 16:08:53 -07001446 // menu handling and state
1447 // TODO: maybe put into separate handler
1448
John Reck9c35b9c2012-05-30 10:08:50 -07001449 @Override
1450 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001451 if (mMenuState == EMPTY_MENU) {
1452 return false;
1453 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001454 MenuInflater inflater = mActivity.getMenuInflater();
1455 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001456 return true;
1457 }
1458
John Reck9c35b9c2012-05-30 10:08:50 -07001459 @Override
1460 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001461 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001462 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 return;
1464 }
1465 if (!(v instanceof WebView)) {
1466 return;
1467 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001468 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001469 WebView.HitTestResult result;
1470
1471 /* Determine whether the ContextMenu got triggered because
1472 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1473 * received. The mResult acts as a flag to identify, how it got trigerred
1474 */
1475 if (mResult == null){
1476 result = webview.getHitTestResult();
1477 } else {
1478 result = mResult;
1479 }
1480
Michael Kolb8233fac2010-10-26 16:08:53 -07001481 if (result == null) {
1482 return;
1483 }
1484
1485 int type = result.getType();
1486 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001487
1488 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1489 unknownTypeMap.put("webview", webview);
1490 final Message msg = mHandler.obtainMessage(
1491 UNKNOWN_TYPE_MSG, unknownTypeMap);
1492 /* As defined in android developers guide
1493 * when UNKNOWN_TYPE is received as a result of HitTest
1494 * you need to determing the type by invoking requestFocusNodeHref
1495 */
1496 webview.requestFocusNodeHref(msg);
1497
Michael Kolb8233fac2010-10-26 16:08:53 -07001498 Log.w(LOGTAG,
1499 "We should not show context menu when nothing is touched");
1500 return;
1501 }
1502 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1503 // let TextView handles context menu
1504 return;
1505 }
1506
1507 // Note, http://b/issue?id=1106666 is requesting that
1508 // an inflated menu can be used again. This is not available
1509 // yet, so inflate each time (yuk!)
1510 MenuInflater inflater = mActivity.getMenuInflater();
1511 inflater.inflate(R.menu.browsercontext, menu);
1512
1513 // Show the correct menu group
1514 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001515 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001516 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001517 menu.setGroupVisible(R.id.PHONE_MENU,
1518 type == WebView.HitTestResult.PHONE_TYPE);
1519 menu.setGroupVisible(R.id.EMAIL_MENU,
1520 type == WebView.HitTestResult.EMAIL_TYPE);
1521 menu.setGroupVisible(R.id.GEO_MENU,
1522 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001523 String itemUrl = null;
1524 String url = webview.getOriginalUrl();
1525 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1526 itemUrl = Uri.decode(navigationUrl);
1527 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1528 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1529 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1530 } else {
1531 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1532 }
1533 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1534 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1535 } else {
1536 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1537
1538 menu.setGroupVisible(R.id.IMAGE_MENU,
1539 type == WebView.HitTestResult.IMAGE_TYPE
1540 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1541 menu.setGroupVisible(R.id.ANCHOR_MENU,
1542 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1543 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001544
1545 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1546 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1547 UrlUtils.isDownloadableScheme(extra));
1548 }
1549 else {
1550 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1551 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001552 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001553 // Setup custom handling depending on the type
1554 switch (type) {
1555 case WebView.HitTestResult.PHONE_TYPE:
1556 menu.setHeaderTitle(Uri.decode(extra));
1557 menu.findItem(R.id.dial_context_menu_id).setIntent(
1558 new Intent(Intent.ACTION_VIEW, Uri
1559 .parse(WebView.SCHEME_TEL + extra)));
1560 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1561 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1562 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1563 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1564 addIntent);
1565 menu.findItem(R.id.copy_phone_context_menu_id)
1566 .setOnMenuItemClickListener(
1567 new Copy(extra));
1568 break;
1569
1570 case WebView.HitTestResult.EMAIL_TYPE:
1571 menu.setHeaderTitle(extra);
1572 menu.findItem(R.id.email_context_menu_id).setIntent(
1573 new Intent(Intent.ACTION_VIEW, Uri
1574 .parse(WebView.SCHEME_MAILTO + extra)));
1575 menu.findItem(R.id.copy_mail_context_menu_id)
1576 .setOnMenuItemClickListener(
1577 new Copy(extra));
1578 break;
1579
1580 case WebView.HitTestResult.GEO_TYPE:
1581 menu.setHeaderTitle(extra);
1582 menu.findItem(R.id.map_context_menu_id).setIntent(
1583 new Intent(Intent.ACTION_VIEW, Uri
1584 .parse(WebView.SCHEME_GEO
1585 + URLEncoder.encode(extra))));
1586 menu.findItem(R.id.copy_geo_context_menu_id)
1587 .setOnMenuItemClickListener(
1588 new Copy(extra));
1589 break;
1590
1591 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1592 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001593 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001594 // decide whether to show the open link in new tab option
1595 boolean showNewTab = mTabControl.canCreateNewTab();
1596 MenuItem newTabItem
1597 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001598 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001599 ? R.string.contextmenu_openlink_newwindow_background
1600 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 newTabItem.setVisible(showNewTab);
1602 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001603 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1604 newTabItem.setOnMenuItemClickListener(
1605 new MenuItem.OnMenuItemClickListener() {
1606 @Override
1607 public boolean onMenuItemClick(MenuItem item) {
1608 final HashMap<String, WebView> hrefMap =
1609 new HashMap<String, WebView>();
1610 hrefMap.put("webview", webview);
1611 final Message msg = mHandler.obtainMessage(
1612 FOCUS_NODE_HREF,
1613 R.id.open_newtab_context_menu_id,
1614 0, hrefMap);
1615 webview.requestFocusNodeHref(msg);
1616 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001618 });
1619 } else {
1620 newTabItem.setOnMenuItemClickListener(
1621 new MenuItem.OnMenuItemClickListener() {
1622 @Override
1623 public boolean onMenuItemClick(MenuItem item) {
1624 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001625 openTab(extra, parent,
1626 !mSettings.openInBackground(),
1627 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001628 return true;
1629 }
1630 });
1631 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001632 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001633 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1634 menu.setHeaderTitle(navigationUrl);
1635 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1636
1637 if (itemUrl != null) {
1638 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1639 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1640 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1641 @Override
1642 public boolean onMenuItemClick(MenuItem item) {
1643 final Intent intent = new Intent(Controller.this
1644 .getContext(),
1645 AddMyNavigationPage.class);
1646 Bundle bundle = new Bundle();
1647 String url = Uri.decode(navigationUrl);
1648 bundle.putBoolean("isAdding", false);
1649 bundle.putString("url", url);
1650 bundle.putString("name", getNameFromUrl(url));
1651 intent.putExtra("websites", bundle);
1652 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1653 return false;
1654 }
1655 });
1656 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1657 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1658 @Override
1659 public boolean onMenuItemClick(MenuItem item) {
1660 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1661 return false;
1662 }
1663 });
1664 }
1665 } else {
1666 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1667 }
1668 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001669 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1670 break;
1671 }
1672 // otherwise fall through to handle image part
1673 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001674 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1675 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001676 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1677 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001678 shareItem.setOnMenuItemClickListener(
1679 new MenuItem.OnMenuItemClickListener() {
1680 @Override
1681 public boolean onMenuItemClick(MenuItem item) {
1682 sharePage(mActivity, null, extra, null,
1683 null);
1684 return true;
1685 }
1686 }
1687 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001688 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001689 menu.findItem(R.id.view_image_context_menu_id)
1690 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1691 @Override
1692 public boolean onMenuItemClick(MenuItem item) {
1693 openTab(extra, mTabControl.getCurrentTab(), true, true);
1694 return false;
1695 }
1696 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001697 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1698 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1699 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001700 menu.findItem(R.id.set_wallpaper_context_menu_id).
1701 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1702 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001703 break;
1704
1705 default:
1706 Log.w(LOGTAG, "We should not get here.");
1707 break;
1708 }
1709 //update the ui
1710 mUi.onContextMenuCreated(menu);
1711 }
1712
kaiyiz6e5b3e02013-08-19 20:02:01 +08001713 public void startAddMyNavigation(String url) {
1714 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1715 Bundle bundle = new Bundle();
1716 bundle.putBoolean("isAdding", true);
1717 bundle.putString("url", url);
1718 bundle.putString("name", getNameFromUrl(url));
1719 intent.putExtra("websites", bundle);
1720 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1721 }
1722
1723 private void showMyNavigationDeleteDialog(final String itemUrl) {
1724 new AlertDialog.Builder(this.getContext())
1725 .setTitle(R.string.my_navigation_delete_label)
1726 .setIcon(android.R.drawable.ic_dialog_alert)
1727 .setMessage(R.string.my_navigation_delete_msg)
1728 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1729 @Override
1730 public void onClick(DialogInterface dialog, int whichButton) {
1731 deleteMyNavigationItem(itemUrl);
1732 }
1733 })
1734 .setNegativeButton(R.string.cancel, null)
1735 .show();
1736 }
1737
1738 private void deleteMyNavigationItem(final String itemUrl) {
1739 ContentResolver cr = this.getContext().getContentResolver();
1740 Cursor cursor = null;
1741
1742 try {
1743 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1744 new String[] {
1745 MyNavigationUtil.ID
1746 }, "url = ?", new String[] {
1747 itemUrl
1748 }, null);
1749 if (null != cursor && cursor.moveToFirst()) {
1750 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1751 cursor.getLong(0));
1752
1753 ContentValues values = new ContentValues();
1754 values.put(MyNavigationUtil.TITLE, "");
1755 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1756 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1757 ByteArrayOutputStream os = new ByteArrayOutputStream();
1758 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1759 R.raw.my_navigation_add);
1760 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1761 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1762 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1763 cr.update(uri, values, null, null);
1764 } else {
1765 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1766 }
1767 } catch (IllegalStateException e) {
1768 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1769 } finally {
1770 if (null != cursor) {
1771 cursor.close();
1772 }
1773 }
1774
1775 if (getCurrentTopWebView() != null) {
1776 getCurrentTopWebView().reload();
1777 }
1778 }
1779
1780 private String getNameFromUrl(String itemUrl) {
1781 ContentResolver cr = this.getContext().getContentResolver();
1782 Cursor cursor = null;
1783 String name = null;
1784
1785 try {
1786 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1787 new String[] {
1788 MyNavigationUtil.TITLE
1789 }, "url = ?", new String[] {
1790 itemUrl
1791 }, null);
1792 if (null != cursor && cursor.moveToFirst()) {
1793 name = cursor.getString(0);
1794 } else {
1795 Log.e(LOGTAG, "this item does not exist!");
1796 }
1797 } catch (IllegalStateException e) {
1798 Log.e(LOGTAG, "getNameFromUrl", e);
1799 } finally {
1800 if (null != cursor) {
1801 cursor.close();
1802 }
1803 }
1804 return name;
1805 }
1806
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001807 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001808 final ContentResolver cr = mActivity.getContentResolver();
1809 new AsyncTask<Void, Void, Void>() {
1810 @Override
1811 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001812 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1813 if(!isMyNavigationUrl)
1814 return null;
1815
kaiyiz6e5b3e02013-08-19 20:02:01 +08001816 ContentResolver cr = mActivity.getContentResolver();
1817 Cursor cursor = null;
1818 try {
1819 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1820 new String[] {
1821 MyNavigationUtil.ID
1822 }, "url = ?", new String[] {
1823 itemUrl
1824 }, null);
1825 if (null != cursor && cursor.moveToFirst()) {
1826 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001827 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001828
1829 ContentValues values = new ContentValues();
1830 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1831 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1832 cursor.getLong(0));
1833 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1834 cr.update(uri, values, null, null);
1835 os.close();
1836 }
1837 } catch (IllegalStateException e) {
1838 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1839 } catch (IOException e) {
1840 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1841 } finally {
1842 if (null != cursor) {
1843 cursor.close();
1844 }
1845 }
1846 return null;
1847 }
1848 }.execute();
1849 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001850 /**
1851 * As the menu can be open when loading state changes
1852 * we must manually update the state of the stop/reload menu
1853 * item
1854 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001855 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001856 if (menu == null) {
1857 return;
1858 }
1859 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001860 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001861 menu.findItem(R.id.stop_menu_id):
1862 menu.findItem(R.id.reload_menu_id);
1863 if (src != null) {
1864 dest.setIcon(src.getIcon());
1865 dest.setTitle(src.getTitle());
1866 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001867 mActivity.invalidateOptionsMenu();
1868 }
1869
1870 public void invalidateOptionsMenu() {
1871 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001872 }
1873
John Reck9c35b9c2012-05-30 10:08:50 -07001874 @Override
1875 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001876 // Software menu key (toolbar key)
1877 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1878 return true;
1879 }
1880
1881 @Override
1882 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001883 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001884 // hold on to the menu reference here; it is used by the page callbacks
1885 // to update the menu based on loading state
1886 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 // Note: setVisible will decide whether an item is visible; while
1888 // setEnabled() will decide whether an item is enabled, which also means
1889 // whether the matching shortcut key will function.
1890 switch (mMenuState) {
1891 case EMPTY_MENU:
1892 if (mCurrentMenuState != mMenuState) {
1893 menu.setGroupVisible(R.id.MAIN_MENU, false);
1894 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1895 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1896 }
1897 break;
1898 default:
1899 if (mCurrentMenuState != mMenuState) {
1900 menu.setGroupVisible(R.id.MAIN_MENU, true);
1901 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1902 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1903 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001904 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001905 break;
1906 }
1907 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001908 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001909
1910 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001911 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1912 EditBookmarksRestriction.getInstance()
1913 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001914 }
1915
1916 private void setMenuItemVisibility(Menu menu, int id,
1917 boolean visibility) {
1918 MenuItem item = menu.findItem(id);
1919 if (item != null) {
1920 item.setVisible(visibility);
1921 }
1922 }
1923
1924 private int lookupBookmark(String title, String url) {
1925 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001926 int count = 0;
1927 Cursor cursor = null;
1928 try {
1929 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1930 BookmarksLoader.PROJECTION,
1931 "title = ? OR url = ?",
1932 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001933 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001934 },
1935 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001936
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001937 if (cursor != null)
1938 count = cursor.getCount();
1939
1940 } catch (IllegalStateException e) {
1941 Log.e(LOGTAG, "lookupBookmark ", e);
1942 } finally {
1943 if (null != cursor) {
1944 cursor.close();
1945 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001946 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001947 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001948 }
1949
1950 private void resetMenuItems(Menu menu) {
1951 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1952 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1953
1954 WebView w = getCurrentTopWebView();
1955 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1956
1957 String title = w.getTitle();
1958 String url = w.getUrl();
Pankaj Garg634bf432015-07-13 09:54:21 -07001959 mCurrentPageBookmarked = (lookupBookmark(title, url) > 0);
1960 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001961 bookmark_icon.setChecked(true);
1962 } else {
1963 bookmark_icon.setChecked(false);
1964 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001965
1966 // update reader mode checkbox
1967 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1968 readerSwitcher.setVisible(false);
1969 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001970 }
1971
Michael Kolb4bf79712011-07-14 14:18:12 -07001972 @Override
1973 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001974 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001975 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001976 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001977 // Following flag is used to identify schemes for which the LIVE_MENU
1978 // items defined in res/menu/browser.xml should be enabled
1979 boolean isLiveScheme = false;
1980 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001981 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07001982
1983 boolean isDistillable = false;
1984 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001985 resetMenuItems(menu);
1986
Michael Kolb4bf79712011-07-14 14:18:12 -07001987 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001988 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001989 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001990 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001991 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001992 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001993 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07001994
1995 isDistillable = tab.isDistillable();
1996 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07001997 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001998
1999 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2000 forward.setEnabled(canGoForward);
2001
Michael Kolb4bf79712011-07-14 14:18:12 -07002002 // decide whether to show the share link option
2003 PackageManager pm = mActivity.getPackageManager();
2004 Intent send = new Intent(Intent.ACTION_SEND);
2005 send.setType("text/plain");
2006 ResolveInfo ri = pm.resolveActivity(send,
2007 PackageManager.MATCH_DEFAULT_ONLY);
2008 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2009
2010 boolean isNavDump = mSettings.enableNavDump();
2011 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2012 nav.setVisible(isNavDump);
2013 nav.setEnabled(isNavDump);
2014
2015 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002016 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2017 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002018 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002019 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2020 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002021 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002022 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2023 isLive && isLiveScheme && isPageFinished);
2024 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002025 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002026 // history and snapshots item are the members of COMBO menu group,
2027 // so if show history item, only make snapshots item invisible.
2028 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002029
Ze G Riande2a675c22015-06-03 11:15:24 -07002030
2031 // update reader mode checkbox
2032 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2033 // The reader mode checkbox is hidden only
2034 // when the current page is neither distillable nor distilled
2035 readerSwitcher.setVisible(isDistillable || isDistilled);
2036 readerSwitcher.setChecked(isDistilled);
2037
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002038 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002039 }
2040
John Reck9c35b9c2012-05-30 10:08:50 -07002041 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002043 if (null == getCurrentTopWebView()) {
2044 return false;
2045 }
2046 if (mMenuIsDown) {
2047 // The shortcut action consumes the MENU. Even if it is still down,
2048 // it won't trigger the next shortcut action. In the case of the
2049 // shortcut action triggering a new activity, like Bookmarks, we
2050 // won't get onKeyUp for MENU. So it is important to reset it here.
2051 mMenuIsDown = false;
2052 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002053 if (mUi.onOptionsItemSelected(item)) {
2054 // ui callback handled it
2055 return true;
2056 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002057 switch (item.getItemId()) {
2058 // -- Main menu
2059 case R.id.new_tab_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002060 getCurrentTab().capture();
Michael Kolb8233fac2010-10-26 16:08:53 -07002061 openTabToHomePage();
2062 break;
2063
2064 case R.id.incognito_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002065 getCurrentTab().capture();
Michael Kolb519d2282011-05-09 17:03:19 -07002066 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 break;
2068
2069 case R.id.goto_menu_id:
2070 editUrl();
2071 break;
2072
2073 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002074 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2075 break;
2076
2077 case R.id.history_menu_id:
2078 bookmarksOrHistoryPicker(ComboViews.History);
2079 break;
2080
2081 case R.id.snapshots_menu_id:
2082 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002083 break;
2084
Pankaj Garg49b79252014-11-07 17:33:41 -08002085 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002086 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002087 break;
2088
2089 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002090 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 stopLoading();
2092 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002093 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002094 getCurrentTopWebView().reload();
2095 }
2096 break;
2097
Michael Kolb8233fac2010-10-26 16:08:53 -07002098 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002099 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002100 break;
2101
2102 case R.id.close_menu_id:
2103 // Close the subwindow if it exists.
2104 if (mTabControl.getCurrentSubWindow() != null) {
2105 dismissSubWindow(mTabControl.getCurrentTab());
2106 break;
2107 }
2108 closeCurrentTab();
2109 break;
2110
kaiyiza8b6dbb2013-07-29 18:11:22 +08002111 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002112 Object[] params = { new String("persist.debug.browsermonkeytest")};
2113 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002114 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002115 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002116 if (ret != null && ret.equals("enable"))
2117 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002118 if (BrowserConfig.getInstance(getContext())
2119 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2120 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002121 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002122 case R.id.homepage_menu_id:
2123 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002124 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002125 break;
2126
2127 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002128 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002129 break;
2130
2131 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002132 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 break;
2134
John Reck2bc80422011-06-30 15:11:49 -07002135 case R.id.save_snapshot_menu_id:
2136 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002137 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002138 createScreenshotAsync(
2139 source.getWebView(),
2140 getDesiredThumbnailWidth(mActivity),
2141 getDesiredThumbnailHeight(mActivity),
2142 new ValueCallback<Bitmap>() {
2143 @Override
2144 public void onReceiveValue(Bitmap bitmap) {
2145 new SaveSnapshotTask(source, bitmap).execute();
2146 }
2147 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002148 break;
2149
Michael Kolb8233fac2010-10-26 16:08:53 -07002150 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002151 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002152 break;
2153
John Recke1a03a32011-09-14 17:04:16 -07002154 case R.id.snapshot_go_live:
2155 goLive();
2156 return true;
2157
Michael Kolb8233fac2010-10-26 16:08:53 -07002158 case R.id.share_page_menu_id:
2159 Tab currentTab = mTabControl.getCurrentTab();
2160 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002161 return false;
2162 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002163 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002164 break;
2165
2166 case R.id.dump_nav_menu_id:
2167 getCurrentTopWebView().debugDump();
2168 break;
2169
Michael Kolb8233fac2010-10-26 16:08:53 -07002170 case R.id.zoom_in_menu_id:
2171 getCurrentTopWebView().zoomIn();
2172 break;
2173
2174 case R.id.zoom_out_menu_id:
2175 getCurrentTopWebView().zoomOut();
2176 break;
2177
2178 case R.id.view_downloads_menu_id:
2179 viewDownloads();
2180 break;
2181
John Reck42229bc2011-08-19 13:26:43 -07002182 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002183 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002184 break;
2185
Ze G Riande2a675c22015-06-03 11:15:24 -07002186 case R.id.reader_mode_menu_id:
2187 toggleReaderMode();
2188 break;
2189
Michael Kolb8233fac2010-10-26 16:08:53 -07002190 case R.id.window_one_menu_id:
2191 case R.id.window_two_menu_id:
2192 case R.id.window_three_menu_id:
2193 case R.id.window_four_menu_id:
2194 case R.id.window_five_menu_id:
2195 case R.id.window_six_menu_id:
2196 case R.id.window_seven_menu_id:
2197 case R.id.window_eight_menu_id:
2198 {
2199 int menuid = item.getItemId();
2200 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2201 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2202 Tab desiredTab = mTabControl.getTab(id);
2203 if (desiredTab != null &&
2204 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002205 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002206 }
2207 break;
2208 }
2209 }
2210 }
2211 break;
2212
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002213 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002214 Bundle bundle = new Bundle();
2215 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2216 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2217 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2218 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2219 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002220 break;
2221
Pankaj Garg49b79252014-11-07 17:33:41 -08002222 case R.id.add_to_homescreen:
2223 final WebView w = getCurrentTopWebView();
2224 final EditText input = new EditText(getContext());
2225 input.setText(w.getTitle());
2226 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002227 .setTitle(getContext().getResources().getString(
2228 R.string.add_to_homescreen))
2229 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002230 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002231 .setPositiveButton(getContext().getResources().getString(
2232 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002233 public void onClick(DialogInterface dialog, int whichButton) {
2234 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2235 getContext(),
2236 w.getUrl(),
2237 input.getText().toString(),
2238 w.getViewportBitmap(),
2239 w.getFavicon()));
2240
2241 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2242 .addCategory(Intent.CATEGORY_HOME));
2243 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002244 .setNegativeButton(getContext().getResources().getString(
2245 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002246 public void onClick(DialogInterface dialog, int whichButton) {
2247 // Do nothing.
2248 }
2249 })
2250 .show();
2251 break;
2252
Michael Kolb8233fac2010-10-26 16:08:53 -07002253 default:
2254 return false;
2255 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002256 return true;
2257 }
2258
John Reck68234a92012-04-19 15:27:12 -07002259 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2260 implements OnCancelListener {
2261
2262 private Tab mTab;
2263 private Dialog mProgressDialog;
2264 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002265 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002266
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002267 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002268 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002269 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002270 }
2271
2272 @Override
2273 protected void onPreExecute() {
2274 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2275 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2276 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002277 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002278 }
2279
2280 @Override
2281 protected Long doInBackground(Void... params) {
2282 if (!mTab.saveViewState(mValues)) {
2283 return null;
2284 }
2285 if (isCancelled()) {
2286 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2287 File file = mActivity.getFileStreamPath(path);
2288 if (!file.delete()) {
2289 file.deleteOnExit();
2290 }
2291 return null;
2292 }
2293 final ContentResolver cr = mActivity.getContentResolver();
2294 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2295 if (result == null) {
2296 return null;
2297 }
2298 long id = ContentUris.parseId(result);
2299 return id;
2300 }
2301
2302 @Override
2303 protected void onPostExecute(Long id) {
2304 if (isCancelled()) {
2305 return;
2306 }
2307 mProgressDialog.dismiss();
2308 if (id == null) {
2309 Toast.makeText(mActivity, R.string.snapshot_failed,
2310 Toast.LENGTH_SHORT).show();
2311 return;
2312 }
2313 Bundle b = new Bundle();
2314 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2315 mUi.showComboView(ComboViews.Snapshots, b);
2316 }
2317
2318 @Override
2319 public void onCancel(DialogInterface dialog) {
2320 cancel(true);
2321 }
2322 }
2323
Michael Kolb80f75082012-04-10 10:50:06 -07002324 @Override
2325 public void toggleUserAgent() {
2326 WebView web = getCurrentWebView();
2327 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002328 }
2329
Ze G Riande2a675c22015-06-03 11:15:24 -07002330 // This function calls the method in the webview to enable/disable
2331 // the reader mode through the DOM distiller
2332 public void toggleReaderMode() {
2333 Tab t = mTabControl.getCurrentTab();
2334 if (t.isDistilled()) {
2335 closeTab(t);
2336 } else if (t.isDistillable()) {
2337 openTab(t.getDistilledUrl(), false, true, false, t);
2338 }
2339 }
2340
Michael Kolb80f75082012-04-10 10:50:06 -07002341 @Override
2342 public void findOnPage() {
2343 getCurrentTopWebView().showFindDialog(null, true);
2344 }
2345
2346 @Override
2347 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002348 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002349 }
2350
2351 @Override
2352 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002353 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002354 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2355 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002356 }
2357 else {
2358 WebView w = getCurrentTopWebView();
2359 if (w == null)
2360 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002361 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002362 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002363 }
Michael Kolb80f75082012-04-10 10:50:06 -07002364 }
2365
John Recke1a03a32011-09-14 17:04:16 -07002366 private void goLive() {
Axesh R. Ajmera89cf4d82015-05-19 11:26:18 -07002367 if (!getCurrentTab().isSnapshot()) return;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002368 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002369 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002370 boolean onlySingleTabRemaining = false;
2371 if (mTabControl.getTabCount() > 1) {
2372 // destroy the old snapshot tab
2373 closeCurrentTab();
2374 } else {
2375 onlySingleTabRemaining = true;
2376 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002377 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002378 if (onlySingleTabRemaining) {
2379 closeTab(t);
2380 }
2381
Tarun Nainani8eb00912014-07-17 12:28:32 -07002382 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002383 }
2384
luxiaolb40014b2013-07-19 10:01:43 +08002385 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002386 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002387 new AlertDialog.Builder(activity)
2388 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002389 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002390 .setMessage(R.string.exit_browser_msg)
2391 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2392 public void onClick(DialogInterface dialog, int which) {
2393 activity.moveTaskToBack(true);
2394 dialog.dismiss();
2395 }
2396 })
2397 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2398 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002399 mCrashRecoveryHandler.clearState(true);
2400 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002401 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002402 dialog.dismiss();
2403 }
2404 })
2405 .show();
2406 }
Michael Kolb315d5022011-10-13 12:47:11 -07002407 @Override
2408 public void showPageInfo() {
2409 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2410 }
2411
John Reck9c35b9c2012-05-30 10:08:50 -07002412 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002413 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002414 // Let the History and Bookmark fragments handle menus they created.
2415 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2416 return false;
2417 }
2418
Michael Kolb8233fac2010-10-26 16:08:53 -07002419 int id = item.getItemId();
2420 boolean result = true;
2421 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 // -- Browser context menu
2423 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002424 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002425 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002426 case R.id.copy_link_context_menu_id:
2427 final WebView webView = getCurrentTopWebView();
2428 if (null == webView) {
2429 result = false;
2430 break;
2431 }
2432 final HashMap<String, WebView> hrefMap =
2433 new HashMap<String, WebView>();
2434 hrefMap.put("webview", webView);
2435 final Message msg = mHandler.obtainMessage(
2436 FOCUS_NODE_HREF, id, 0, hrefMap);
2437 webView.requestFocusNodeHref(msg);
2438 break;
2439
2440 default:
2441 // For other context menus
2442 result = onOptionsItemSelected(item);
2443 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002444 return result;
2445 }
2446
2447 /**
2448 * support programmatically opening the context menu
2449 */
2450 public void openContextMenu(View view) {
2451 mActivity.openContextMenu(view);
2452 }
2453
2454 /**
2455 * programmatically open the options menu
2456 */
2457 public void openOptionsMenu() {
2458 mActivity.openOptionsMenu();
2459 }
2460
John Reck9c35b9c2012-05-30 10:08:50 -07002461 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002462 public boolean onMenuOpened(int featureId, Menu menu) {
2463 if (mOptionsMenuOpen) {
2464 if (mConfigChanged) {
2465 // We do not need to make any changes to the state of the
2466 // title bar, since the only thing that happened was a
2467 // change in orientation
2468 mConfigChanged = false;
2469 } else {
2470 if (!mExtendedMenuOpen) {
2471 mExtendedMenuOpen = true;
2472 mUi.onExtendedMenuOpened();
2473 } else {
2474 // Switching the menu back to icon view, so show the
2475 // title bar once again.
2476 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002477 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002478 }
2479 }
2480 } else {
2481 // The options menu is closed, so open it, and show the title
2482 mOptionsMenuOpen = true;
2483 mConfigChanged = false;
2484 mExtendedMenuOpen = false;
2485 mUi.onOptionsMenuOpened();
2486 }
2487 return true;
2488 }
2489
John Reck9c35b9c2012-05-30 10:08:50 -07002490 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002491 public void onOptionsMenuClosed(Menu menu) {
2492 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002493 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002494 }
2495
John Reck9c35b9c2012-05-30 10:08:50 -07002496 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002498 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002499 }
2500
2501 // Helper method for getting the top window.
2502 @Override
2503 public WebView getCurrentTopWebView() {
2504 return mTabControl.getCurrentTopWebView();
2505 }
2506
2507 @Override
2508 public WebView getCurrentWebView() {
2509 return mTabControl.getCurrentWebView();
2510 }
2511
2512 /*
2513 * This method is called as a result of the user selecting the options
2514 * menu to see the download window. It shows the download window on top of
2515 * the current window.
2516 */
2517 void viewDownloads() {
2518 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2519 mActivity.startActivity(intent);
2520 }
2521
John Reck30b065e2011-07-19 10:58:05 -07002522 int getActionModeHeight() {
2523 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2524 new int[] { android.R.attr.actionBarSize });
2525 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2526 actionBarSizeTypedArray.recycle();
2527 return size;
2528 }
2529
Michael Kolb8233fac2010-10-26 16:08:53 -07002530 // action mode
2531
John Reck9c35b9c2012-05-30 10:08:50 -07002532 @Override
2533 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002534 mUi.onActionModeStarted(mode);
2535 mActionMode = mode;
2536 }
2537
2538 /*
2539 * True if a custom ActionMode (i.e. find or select) is in use.
2540 */
2541 @Override
2542 public boolean isInCustomActionMode() {
2543 return mActionMode != null;
2544 }
2545
2546 /*
2547 * End the current ActionMode.
2548 */
2549 @Override
2550 public void endActionMode() {
2551 if (mActionMode != null) {
2552 mActionMode.finish();
2553 }
2554 }
2555
2556 /*
2557 * Called by find and select when they are finished. Replace title bars
2558 * as necessary.
2559 */
John Reck9c35b9c2012-05-30 10:08:50 -07002560 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002561 public void onActionModeFinished(ActionMode mode) {
2562 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002563 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002564 mActionMode = null;
2565 }
2566
2567 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002568 final Tab tab = getCurrentTab();
2569 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002570 }
2571
2572 // bookmark handling
2573
2574 /**
2575 * add the current page as a bookmark to the given folder id
2576 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002577 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002578 * bookmarked, and if it is, edit that bookmark. If false, and
2579 * the site is already bookmarked, do not attempt to edit the
2580 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002581 */
2582 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002583 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002584 WebView w = getCurrentTopWebView();
2585 if (w == null) {
2586 return null;
2587 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 Intent i = new Intent(mActivity,
2589 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002590 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2591 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002592 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002593 if (touchIconUrl != null) {
2594 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2595 WebSettings settings = w.getSettings();
2596 if (settings != null) {
2597 i.putExtra(AddBookmarkPage.USER_AGENT,
2598 settings.getUserAgentString());
2599 }
2600 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002601 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002603 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002604 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2605 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 // Put the dialog at the upper right of the screen, covering the
2607 // star on the title bar.
2608 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002609 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002610 }
2611
2612 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002613 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002614 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002615 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002616 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002617 }
2618
Vivek Sekharb54614f2014-05-01 19:03:37 -07002619 @Override
2620 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2621 boolean capture) {
2622 mUploadHandler = new UploadHandler(this);
2623 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2624 }
2625
Michael Kolb8233fac2010-10-26 16:08:53 -07002626 // thumbnails
2627
2628 /**
2629 * Return the desired width for thumbnail screenshots, which are stored in
2630 * the database, and used on the bookmarks screen.
2631 * @param context Context for finding out the density of the screen.
2632 * @return desired width for thumbnail screenshot.
2633 */
2634 static int getDesiredThumbnailWidth(Context context) {
2635 return context.getResources().getDimensionPixelOffset(
2636 R.dimen.bookmarkThumbnailWidth);
2637 }
2638
2639 /**
2640 * Return the desired height for thumbnail screenshots, which are stored in
2641 * the database, and used on the bookmarks screen.
2642 * @param context Context for finding out the density of the screen.
2643 * @return desired height for thumbnail screenshot.
2644 */
2645 static int getDesiredThumbnailHeight(Context context) {
2646 return context.getResources().getDimensionPixelOffset(
2647 R.dimen.bookmarkThumbnailHeight);
2648 }
2649
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002650 static void createScreenshotAsync(WebView view, int width, int height,
2651 final ValueCallback<Bitmap> cb) {
2652 if (view == null || width == 0 || height == 0) {
2653 return;
2654 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002655 view.getContentBitmapAsync(
2656 (float) width / view.getWidth(),
2657 new Rect(),
2658 new ValueCallback<Bitmap>() {
2659 @Override
2660 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002661 if (bitmap != null)
2662 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2663 cb.onReceiveValue(bitmap);
2664 }});
2665 }
2666
Michael Kolb8233fac2010-10-26 16:08:53 -07002667 private class Copy implements OnMenuItemClickListener {
2668 private CharSequence mText;
2669
John Reck9c35b9c2012-05-30 10:08:50 -07002670 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002671 public boolean onMenuItemClick(MenuItem item) {
2672 copy(mText);
2673 return true;
2674 }
2675
2676 public Copy(CharSequence toCopy) {
2677 mText = toCopy;
2678 }
2679 }
2680
Leon Scroggins63c02662010-11-18 15:16:27 -05002681 private static class Download implements OnMenuItemClickListener {
2682 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002683 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002684 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002685 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002686 private static final String FALLBACK_EXTENSION = "dat";
2687 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002688
John Reck9c35b9c2012-05-30 10:08:50 -07002689 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002690 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002691 if (DataUri.isDataUri(mText)) {
2692 saveDataUri();
2693 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002694 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002695 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002696 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002697 return true;
2698 }
2699
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002700 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2701 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002702 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002703 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002704 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002705 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002706 }
George Mount387d45d2011-10-07 15:57:53 -07002707
2708 /**
2709 * Treats mText as a data URI and writes its contents to a file
2710 * based on the current time.
2711 */
2712 private void saveDataUri() {
2713 FileOutputStream outputStream = null;
2714 try {
2715 DataUri uri = new DataUri(mText);
2716 File target = getTarget(uri);
2717 outputStream = new FileOutputStream(target);
2718 outputStream.write(uri.getData());
2719 final DownloadManager manager =
2720 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2721 manager.addCompletedDownload(target.getName(),
2722 mActivity.getTitle().toString(), false,
2723 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002724 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002725 } catch (IOException e) {
2726 Log.e(LOGTAG, "Could not save data URL");
2727 } finally {
2728 if (outputStream != null) {
2729 try {
2730 outputStream.close();
2731 } catch (IOException e) {
2732 // ignore close errors
2733 }
2734 }
2735 }
2736 }
2737
2738 /**
2739 * Creates a File based on the current time stamp and uses
2740 * the mime type of the DataUri to get the extension.
2741 */
2742 private File getTarget(DataUri uri) throws IOException {
2743 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002744 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002745 String nameBase = format.format(new Date());
2746 String mimeType = uri.getMimeType();
2747 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2748 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2749 if (extension == null) {
2750 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2751 extension = FALLBACK_EXTENSION;
2752 }
2753 extension = "." + extension; // createTempFile needs the '.'
2754 File targetFile = File.createTempFile(nameBase, extension, dir);
2755 return targetFile;
2756 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002757 }
2758
Cary Clark8974d282010-11-22 10:46:05 -05002759 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002760 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002761
John Reck9c35b9c2012-05-30 10:08:50 -07002762 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002763 public boolean onMenuItemClick(MenuItem item) {
2764 if (mWebView != null) {
2765 return mWebView.selectText();
2766 }
2767 return false;
2768 }
2769
2770 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002771 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002772 }
2773
2774 }
2775
Michael Kolb8233fac2010-10-26 16:08:53 -07002776 /********************** TODO: UI stuff *****************************/
2777
2778 // these methods have been copied, they still need to be cleaned up
2779
2780 /****************** tabs ***************************************************/
2781
2782 // basic tab interactions:
2783
2784 // it is assumed that tabcontrol already knows about the tab
2785 protected void addTab(Tab tab) {
2786 mUi.addTab(tab);
2787 }
2788
2789 protected void removeTab(Tab tab) {
2790 mUi.removeTab(tab);
2791 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002792 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002793 }
2794
Michael Kolbf2055602011-04-09 17:20:03 -07002795 @Override
2796 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002797 // monkey protection against delayed start
2798 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002799
2800 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2801 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002802 mTabControl.setCurrentTab(tab);
2803 // the tab is guaranteed to have a webview after setCurrentTab
2804 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002805
2806
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002807 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002808 //Purge active tabs
2809 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002810 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002811 }
2812
John Reck8bcafc12011-08-29 16:43:02 -07002813 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002814 Tab current = mTabControl.getCurrentTab();
2815 if (current != null
2816 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002817 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002818 }
2819 }
2820
John Reck26b18322011-06-21 13:08:58 -07002821 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002822 //Cancel navscreen request
2823 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002824 // Dismiss the subwindow if applicable.
2825 dismissSubWindow(appTab);
2826 // Since we might kill the WebView, remove it from the
2827 // content view first.
2828 mUi.detachTab(appTab);
2829 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002830 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002831 // TODO: analyze why the remove and add are necessary
2832 mUi.attachTab(appTab);
2833 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002834 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002835 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002836 } else {
2837 // If the tab was the current tab, we have to attach
2838 // it to the view system again.
2839 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002840 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002841 }
2842 }
2843
2844 // Remove the sub window if it exists. Also called by TabControl when the
2845 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002846 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002847 public void dismissSubWindow(Tab tab) {
2848 removeSubWindow(tab);
2849 // dismiss the subwindow. This will destroy the WebView.
2850 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002851 WebView wv = getCurrentTopWebView();
2852 if (wv != null) {
2853 wv.requestFocus();
2854 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002855 }
2856
2857 @Override
2858 public void removeSubWindow(Tab t) {
2859 if (t.getSubWebView() != null) {
2860 mUi.removeSubWindow(t.getSubViewContainer());
2861 }
2862 }
2863
2864 @Override
2865 public void attachSubWindow(Tab tab) {
2866 if (tab.getSubWebView() != null) {
2867 mUi.attachSubWindow(tab.getSubViewContainer());
2868 getCurrentTopWebView().requestFocus();
2869 }
2870 }
2871
Mathew Inwood29721c22011-06-29 17:55:24 +01002872 private Tab showPreloadedTab(final UrlData urlData) {
2873 if (!urlData.isPreloaded()) {
2874 return null;
2875 }
2876 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2877 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2878 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002879 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002880 // Could not submit query. Fallback to regular tab creation
2881 tabControl.destroy();
2882 return null;
2883 }
2884 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002885 // check tab count and make room for new tab
2886 if (!mTabControl.canCreateNewTab()) {
2887 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2888 if (leastUsed != null) {
2889 closeTab(leastUsed);
2890 }
2891 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002892 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002893 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002894 mTabControl.addPreloadedTab(t);
2895 addTab(t);
2896 setActiveTab(t);
2897 return t;
2898 }
2899
Michael Kolb7bcafde2011-05-09 13:55:59 -07002900 // open a non inconito tab with the given url data
2901 // and set as active tab
2902 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002903 Tab tab = showPreloadedTab(urlData);
2904 if (tab == null) {
2905 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002906 if ((tab != null) && !urlData.isEmpty()) {
2907 loadUrlDataIn(tab, urlData);
2908 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002909 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002910 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002911 }
2912
Michael Kolb843510f2010-12-09 10:51:49 -08002913 @Override
2914 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002915 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002916 }
2917
Michael Kolb8233fac2010-10-26 16:08:53 -07002918 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002919 public Tab openIncognitoTab() {
2920 return openTab(INCOGNITO_URI, true, true, false);
2921 }
2922
2923 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002924 public Tab openTab(String url, boolean incognito, boolean setActive,
2925 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002926 return openTab(url, incognito, setActive, useCurrent, null);
2927 }
2928
2929 @Override
2930 public Tab openTab(String url, Tab parent, boolean setActive,
2931 boolean useCurrent) {
2932 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2933 setActive, useCurrent, parent);
2934 }
2935
2936 public Tab openTab(String url, boolean incognito, boolean setActive,
2937 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002938 Tab tab = createNewTab(incognito, setActive, useCurrent);
2939 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002940 if (parent instanceof SnapshotTab) {
2941 addTab(tab);
2942 if (setActive)
2943 setActiveTab(tab);
2944 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002945 parent.addChildTab(tab);
2946 }
Michael Kolb519d2282011-05-09 17:03:19 -07002947 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002948 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002949 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002950 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002951 return tab;
2952 }
2953
2954 // this method will attempt to create a new tab
2955 // incognito: private browsing tab
2956 // setActive: ste tab as current tab
2957 // useCurrent: if no new tab can be created, return current tab
2958 private Tab createNewTab(boolean incognito, boolean setActive,
2959 boolean useCurrent) {
2960 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002961 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2962 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002963 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002964 if (mTabControl.canCreateNewTab()) {
2965 tab = mTabControl.createNewTab(incognito, !setActive);
2966 addTab(tab);
2967 if (setActive) {
2968 setActiveTab(tab);
2969 } else {
2970 tab.pause();
2971 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002972 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002973 if (useCurrent) {
2974 tab = mTabControl.getCurrentTab();
2975 reuseTab(tab, null);
2976 } else {
2977 mUi.showMaxTabsWarning();
2978 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002979 }
2980 }
2981 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002982 }
2983
John Reck2bc80422011-06-30 15:11:49 -07002984 @Override
2985 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2986 SnapshotTab tab = null;
2987 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07002988 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07002989 addTab(tab);
2990 if (setActive) {
2991 setActiveTab(tab);
2992 }
2993 } else {
2994 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002995 }
2996 return tab;
2997 }
2998
Michael Kolb8233fac2010-10-26 16:08:53 -07002999 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003000 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003001 * @return boolean True if we successfully switched to a different tab. If
3002 * the indexth tab is null, or if that tab is the same as
3003 * the current one, return false.
3004 */
3005 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003006 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003007 Tab currentTab = mTabControl.getCurrentTab();
3008 if (tab == null || tab == currentTab) {
3009 return false;
3010 }
3011 setActiveTab(tab);
3012 return true;
3013 }
3014
3015 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003016 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003017 closeCurrentTab(false);
3018 }
3019
3020 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003021 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003022 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003023 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003024 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003025 return;
3026 }
Michael Kolbc831b632011-05-11 09:30:34 -07003027 final Tab current = mTabControl.getCurrentTab();
3028 final int pos = mTabControl.getCurrentPosition();
3029 Tab newTab = current.getParent();
3030 if (newTab == null) {
3031 newTab = mTabControl.getTab(pos + 1);
3032 if (newTab == null) {
3033 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003034 }
3035 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003036 if (andQuit) {
3037 mTabControl.setCurrentTab(newTab);
3038 closeTab(current);
3039 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003040 // Close window
3041 closeTab(current);
3042 }
3043 }
3044
3045 /**
3046 * Close the tab, remove its associated title bar, and adjust mTabControl's
3047 * current tab to a valid value.
3048 */
3049 @Override
3050 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003051 if (tab == mTabControl.getCurrentTab()) {
3052 closeCurrentTab();
3053 } else {
3054 removeTab(tab);
3055 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003056 }
3057
Afzal Najamd4e33312012-04-26 01:54:01 -04003058 /**
3059 * Close all tabs except the current one
3060 */
3061 @Override
3062 public void closeOtherTabs() {
3063 int inactiveTabs = mTabControl.getTabCount() - 1;
3064 for (int i = inactiveTabs; i >= 0; i--) {
3065 Tab tab = mTabControl.getTab(i);
3066 if (tab != mTabControl.getCurrentTab()) {
3067 removeTab(tab);
3068 }
3069 }
3070 }
3071
Michael Kolb8233fac2010-10-26 16:08:53 -07003072 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003073 protected void loadUrlFromContext(String url) {
3074 Tab tab = getCurrentTab();
3075 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003076 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003077 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003078 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003079 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003080 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003081 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003082 }
3083 }
3084 }
3085
3086 /**
3087 * Load the URL into the given WebView and update the title bar
3088 * to reflect the new load. Call this instead of WebView.loadUrl
3089 * directly.
3090 * @param view The WebView used to load url.
3091 * @param url The URL to load.
3092 */
John Reck71e51422011-07-01 16:49:28 -07003093 @Override
3094 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003095 loadUrl(tab, url, null);
3096 }
3097
3098 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3099 if (tab != null) {
3100 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003101 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003102 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003103 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003104 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003105 }
3106
3107 /**
3108 * Load UrlData into a Tab and update the title bar to reflect the new
3109 * load. Call this instead of UrlData.loadIn directly.
3110 * @param t The Tab used to load.
3111 * @param data The UrlData being loaded.
3112 */
3113 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003114 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003115 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003116 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003117 } else {
John Reck38b39652012-06-05 09:22:59 -07003118 if (t != null && data.mDisableUrlOverride) {
3119 t.disableUrlOverridingForLoad();
3120 }
John Reck26b18322011-06-21 13:08:58 -07003121 loadUrl(t, data.mUrl, data.mHeaders);
3122 }
3123 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003124 }
3125
John Reck30c714c2010-12-16 17:30:34 -08003126 @Override
3127 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003128 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003129 //In case of tab can go back (aka tab has navigation entry) do nothing
3130 //else just load homepage in current tab.
3131 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003132 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003133 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003134 }
3135
3136 void goBackOnePageOrQuit() {
3137 Tab current = mTabControl.getCurrentTab();
3138 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003139 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3140 showExitDialog(mActivity);
3141 } else {
3142 /*
3143 * Instead of finishing the activity, simply push this to the back
3144 * of the stack and let ActivityManager to choose the foreground
3145 * activity. As BrowserActivity is singleTask, it will be always the
3146 * root of the task. So we can use either true or false for
3147 * moveTaskToBack().
3148 */
3149 mActivity.moveTaskToBack(true);
3150 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003151 return;
3152 }
John Reckef654f12011-07-12 16:42:08 -07003153 if (current.canGoBack()) {
3154 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003155 } else {
3156 // Check to see if we are closing a window that was created by
3157 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003158 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003159 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003160 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003161 // Now we close the other tab
3162 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003163 } else if (BrowserConfig.getInstance(getContext())
3164 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3165 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003166 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003167 /*
3168 * Instead of finishing the activity, simply push this to the back
3169 * of the stack and let ActivityManager to choose the foreground
3170 * activity. As BrowserActivity is singleTask, it will be always the
3171 * root of the task. So we can use either true or false for
3172 * moveTaskToBack().
3173 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003174 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003175 }
3176 }
3177 }
3178
3179 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003180 * helper method for key handler
3181 * returns the current tab if it can't advance
3182 */
Michael Kolbc831b632011-05-11 09:30:34 -07003183 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003184 int pos = mTabControl.getCurrentPosition() + 1;
3185 if (pos >= mTabControl.getTabCount()) {
3186 pos = 0;
3187 }
3188 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003189 }
3190
3191 /**
3192 * helper method for key handler
3193 * returns the current tab if it can't advance
3194 */
Michael Kolbc831b632011-05-11 09:30:34 -07003195 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003196 int pos = mTabControl.getCurrentPosition() - 1;
3197 if ( pos < 0) {
3198 pos = mTabControl.getTabCount() - 1;
3199 }
3200 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003201 }
3202
John Reckbcef87f2012-02-03 14:58:34 -08003203 boolean isMenuOrCtrlKey(int keyCode) {
3204 return (KeyEvent.KEYCODE_MENU == keyCode)
3205 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3206 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3207 }
3208
Michael Kolb0035fad2011-03-14 13:25:28 -07003209 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003210 * handle key events in browser
3211 *
3212 * @param keyCode
3213 * @param event
3214 * @return true if handled, false to pass to super
3215 */
John Reck9c35b9c2012-05-30 10:08:50 -07003216 @Override
3217 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003218 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3219 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003220 if (!mUi.isComboViewShowing()) {
3221 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3222 true, false);
3223 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003224 return true;
3225 }
3226
Cary Clark160bbb92011-01-10 11:17:07 -05003227 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003228 // Even if MENU is already held down, we need to call to super to open
3229 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003230 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003231 mMenuIsDown = true;
3232 return false;
3233 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003234
Cary Clark8ff8c662010-12-29 15:03:05 -05003235 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003236 Tab tab = getCurrentTab();
3237 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003238
Cary Clark160bbb92011-01-10 11:17:07 -05003239 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3240 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003241
Michael Kolb8233fac2010-10-26 16:08:53 -07003242 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003243 case KeyEvent.KEYCODE_TAB:
3244 if (event.isCtrlPressed()) {
3245 if (event.isShiftPressed()) {
3246 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003247 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003248 } else {
3249 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003250 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003251 }
3252 return true;
3253 }
3254 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003255 case KeyEvent.KEYCODE_SPACE:
3256 // WebView/WebTextView handle the keys in the KeyDown. As
3257 // the Activity's shortcut keys are only handled when WebView
3258 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003259 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003260 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003261 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003262 pageDown();
3263 }
3264 return true;
3265 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003266 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003267 event.startTracking();
3268 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003269 case KeyEvent.KEYCODE_FORWARD:
3270 if (!noModifiers) break;
3271 tab.goForward();
3272 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003273 case KeyEvent.KEYCODE_DPAD_LEFT:
3274 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003275 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003276 return true;
3277 }
3278 break;
3279 case KeyEvent.KEYCODE_DPAD_RIGHT:
3280 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003281 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003282 return true;
3283 }
3284 break;
3285 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003286 if (ctrl) {
3287 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003288 return true;
3289 }
3290 break;
Michael Kolba4183062011-01-16 10:43:21 -08003291// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003292 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003293 if (ctrl ) {
3294 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003295 return true;
3296 }
3297 break;
Michael Kolba4183062011-01-16 10:43:21 -08003298// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003299// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003300// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003301// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003302// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003303// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003304// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003305// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003306// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003307// case KeyEvent.KEYCODE_M: // unused
3308// case KeyEvent.KEYCODE_N: // in Chrome: new window
3309// case KeyEvent.KEYCODE_O: // in Chrome: open file
3310// case KeyEvent.KEYCODE_P: // in Chrome: print page
3311// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003312// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003313// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3314 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003315 // we can't use the ctrl/shift flags, they check for
3316 // exclusive use of a modifier
3317 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003318 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003319 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003320 } else {
3321 openTabToHomePage();
3322 }
3323 return true;
3324 }
3325 break;
3326// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3327// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003328// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003329// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3330// case KeyEvent.KEYCODE_Y: // unused
3331// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003332 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003333 // it is a regular key and webview is not null
3334 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003335 }
3336
John Reck9c35b9c2012-05-30 10:08:50 -07003337 @Override
3338 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003339 switch(keyCode) {
3340 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003341 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003342 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003343 return true;
3344 }
3345 break;
3346 }
3347 return false;
3348 }
3349
John Reck9c35b9c2012-05-30 10:08:50 -07003350 @Override
3351 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003352 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003353 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003354 if (KeyEvent.KEYCODE_MENU == keyCode
3355 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003356 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003357 }
3358 }
Cary Clark160bbb92011-01-10 11:17:07 -05003359 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003360 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003361 case KeyEvent.KEYCODE_BACK:
3362 if (event.isTracking() && !event.isCanceled()) {
3363 onBackKey();
3364 return true;
3365 }
3366 break;
3367 }
3368 return false;
3369 }
3370
3371 public boolean isMenuDown() {
3372 return mMenuIsDown;
3373 }
3374
John Reck9c35b9c2012-05-30 10:08:50 -07003375 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003376 public void setupAutoFill(Message message) {
3377 // Open the settings activity at the AutoFill profile fragment so that
3378 // the user can create a new profile. When they return, we will dispatch
3379 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003380 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003381 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3382 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003383 }
John Reckb3417f02011-01-14 11:01:05 -08003384
John Reck9c35b9c2012-05-30 10:08:50 -07003385 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003386 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003387 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003388 return true;
3389 }
3390
John Reck1cf4b792011-07-26 10:22:22 -07003391 @Override
3392 public boolean shouldCaptureThumbnails() {
3393 return mUi.shouldCaptureThumbnails();
3394 }
3395
Michael Kolbc3af0672011-08-09 10:24:41 -07003396 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003397 public boolean supportsVoice() {
3398 PackageManager pm = mActivity.getPackageManager();
3399 List activities = pm.queryIntentActivities(new Intent(
3400 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3401 return activities.size() != 0;
3402 }
3403
3404 @Override
3405 public void startVoiceRecognizer() {
3406 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003407 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003408 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3409 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3410 mActivity.startActivityForResult(voice, VOICE_RESULT);
3411 }
3412
Pankaj Garg7b279f62014-08-12 14:47:18 -07003413 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003414 if (mLevel == level)
3415 return;
3416 mLevel = level;
3417 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003418 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3419 lp.dimAmount = level;
3420 mActivity.getWindow().setAttributes(lp);
3421 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3422 } else {
3423 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3424 }
3425 }
3426
Michael Kolb0b129122012-06-04 16:31:58 -07003427 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003428 public void setBlockEvents(boolean block) {
3429 mBlockEvents = block;
3430 }
3431
John Reck9c35b9c2012-05-30 10:08:50 -07003432 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003433 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003434 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003435 }
3436
John Reck9c35b9c2012-05-30 10:08:50 -07003437 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003438 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003439 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003440 }
3441
John Reck9c35b9c2012-05-30 10:08:50 -07003442 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003443 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003444 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003445 }
3446
John Reck9c35b9c2012-05-30 10:08:50 -07003447 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003448 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003449 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003450 }
3451
John Reck9c35b9c2012-05-30 10:08:50 -07003452 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003453 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003454 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003455 }
3456
Pankaj Garg49b79252014-11-07 17:33:41 -08003457 @Override
3458 public boolean shouldShowAppMenu() {
3459 return true;
3460 }
3461
3462 @Override
3463 public int getMenuThemeResourceId() {
3464 return R.style.OverflowMenuTheme;
3465 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003466}