blob: c7ceb681bdd0ccdc0852a301c495228390ec08b0 [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 Tharp9a082b12015-06-19 08:46:57 -070099import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -0700100import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -0700101import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700102import com.android.browser.mynavigation.AddMyNavigationPage;
103import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700104import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700105import com.android.browser.platformsupport.BrowserContract;
106import com.android.browser.platformsupport.WebAddress;
107import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800108import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700109import com.android.browser.provider.BrowserProvider2.Thumbnails;
110import com.android.browser.provider.SnapshotProvider.Snapshots;
111import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800112import com.android.browser.appmenu.AppMenuHandler;
113import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700114
Michael Kolb8233fac2010-10-26 16:08:53 -0700115import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700116import java.io.File;
117import java.io.FileOutputStream;
118import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700119import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700120import java.text.DateFormat;
121import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700122import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700123import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700124import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800125import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200126import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700127import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700128
129/**
130 * Controller for browser
131 */
132public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800133 implements WebViewController, UiController, ActivityController,
134 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700135
136 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800137 private static final String SEND_APP_ID_EXTRA =
138 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Vivek Sekhared791da2015-02-22 12:39:05 -0800139 private static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800140 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
141 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800142
Vivek Sekharb54614f2014-05-01 19:03:37 -0700143 // Remind switch to data connection if wifi is unavailable
144 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800145
Michael Kolb8233fac2010-10-26 16:08:53 -0700146 // public message ids
147 public final static int LOAD_URL = 1001;
148 public final static int STOP_LOAD = 1002;
149
150 // Message Ids
151 private static final int FOCUS_NODE_HREF = 102;
152 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800153 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700154
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800156 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700157
158 private static final int EMPTY_MENU = -1;
159
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700161 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 final static int PREFERENCES_PAGE = 3;
163 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000164 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700165 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800166 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000167
Michael Kolb8233fac2010-10-26 16:08:53 -0700168 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
169
170 // As the ids are dynamically created, we can't guarantee that they will
171 // be in sequence, so this static array maps ids to a window number.
172 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
173 { R.id.window_one_menu_id, R.id.window_two_menu_id,
174 R.id.window_three_menu_id, R.id.window_four_menu_id,
175 R.id.window_five_menu_id, R.id.window_six_menu_id,
176 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
177
178 // "source" parameter for Google search through search key
179 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
180 // "source" parameter for Google search through simplily type
181 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
182
George Mount387d45d2011-10-07 15:57:53 -0700183 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700184 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
185
John Reckd7dd9b22011-08-30 09:18:29 -0700186 // A bitmap that is re-used in createScreenshot as scratch space
187 private static Bitmap sThumbnailBitmap;
188
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private Activity mActivity;
190 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700191 private HomepageHandler mHomepageHandler;
192 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 private BrowserSettings mSettings;
194 private WebViewFactory mFactory;
195
196 private WakeLock mWakeLock;
197
198 private UrlHandler mUrlHandler;
199 private UploadHandler mUploadHandler;
200 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700201 private PageDialogsHandler mPageDialogsHandler;
202 private NetworkStateHandler mNetworkHandler;
203
Ben Murdoch8029a772010-11-16 11:58:21 +0000204 private Message mAutoFillSetupMessage;
205
kaiyiza016da12013-08-26 17:50:22 +0800206 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700207
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 // FIXME, temp address onPrepareMenu performance problem.
209 // When we move everything out of view, we should rewrite this.
210 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700211 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 private int mOldMenuState = EMPTY_MENU;
213 private Menu mCachedMenu;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 private boolean mMenuIsDown;
216
Pankaj Garg49b79252014-11-07 17:33:41 -0800217 private boolean mWasInPageLoad = false;
218 private AppMenuHandler mAppMenuHandler;
219
Michael Kolb8233fac2010-10-26 16:08:53 -0700220 // For select and find, we keep track of the ActionMode so that
221 // finish() can be called as desired.
222 private ActionMode mActionMode;
223
224 /**
225 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
226 * of whether the configuration has changed. The first onMenuOpened call
227 * after a configuration change is simply a reopening of the same menu
228 * (i.e. mIconView did not change).
229 */
230 private boolean mConfigChanged;
231
232 /**
233 * Keeps track of whether the options menu is open. This is important in
234 * determining whether to show or hide the title bar overlay
235 */
236 private boolean mOptionsMenuOpen;
237
238 /**
239 * Whether or not the options menu is in its bigger, popup menu form. When
240 * true, we want the title bar overlay to be gone. When false, we do not.
241 * Only meaningful if mOptionsMenuOpen is true.
242 */
243 private boolean mExtendedMenuOpen;
244
Michael Kolb8233fac2010-10-26 16:08:53 -0700245 private boolean mActivityPaused = true;
246 private boolean mLoadStopped;
247
248 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500249 // Checks to see when the bookmarks database has changed, and updates the
250 // Tabs' notion of whether they represent bookmarked sites.
251 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700252 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700253
Michael Kolbc3af0672011-08-09 10:24:41 -0700254 private boolean mBlockEvents;
255
Michael Kolb0b129122012-06-04 16:31:58 -0700256 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800257 private boolean mUpdateMyNavThumbnail;
258 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800259 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800260 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700261 private PowerConnectionReceiver mLowPowerReceiver;
262 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700263
George Mount3636d0a2011-11-21 09:08:21 -0800264 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 mActivity = browser;
266 mSettings = BrowserSettings.getInstance();
267 mTabControl = new TabControl(this);
268 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700269 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800270 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700271 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700272
273 mUrlHandler = new UrlHandler(this);
274 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
276
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500278 mBookmarksObserver = new ContentObserver(mHandler) {
279 @Override
280 public void onChange(boolean selfChange) {
281 int size = mTabControl.getTabCount();
282 for (int i = 0; i < size; i++) {
283 mTabControl.getTab(i).updateBookmarkedStatus();
284 }
285 }
286
287 };
288 browser.getContentResolver().registerContentObserver(
289 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700290
291 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700292 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800293 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700294 }
295
John Reck9c35b9c2012-05-30 10:08:50 -0700296 @Override
297 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700298 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800299 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800300 // mCrashRecoverHandler has any previously saved state.
301 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700302 }
303
George Mount3636d0a2011-11-21 09:08:21 -0800304 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800305 // we dont want to ever recover incognito tabs
306 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700307
Patrick Scott7d50a932011-02-04 09:27:26 -0500308 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700309 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000311
Michael Kolbc831b632011-05-11 09:30:34 -0700312 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500313 // Not able to restore so we go ahead and clear session cookies. We
314 // must do this before trying to login the user as we don't want to
315 // clear any session cookies set during login.
316 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700317 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800318 if (intent == null) {
319 // This won't happen under common scenarios. The icicle is
320 // not null, but there aren't any tabs to restore.
321 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700322 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800323 final Bundle extra = intent.getExtras();
324 // Create an initial tab.
325 // If the intent is ACTION_VIEW and data is not null, the Browser is
326 // invoked to view the content by another application. In this case,
327 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800328 UrlData urlData = null;
329 if (intent.getData() != null
330 && Intent.ACTION_VIEW.equals(intent.getAction())
331 && intent.getData().toString().startsWith("content://")) {
332 urlData = new UrlData(intent.getData().toString());
333 } else {
334 urlData = IntentHandler.getUrlDataFromIntent(intent);
335 }
George Mount3636d0a2011-11-21 09:08:21 -0800336 Tab t = null;
337 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700338 String landingPage = mActivity.getResources().getString(
339 R.string.def_landing_page);
340 if (!landingPage.isEmpty()) {
341 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800342 } else {
343 t = openTabToHomePage();
344 }
George Mount3636d0a2011-11-21 09:08:21 -0800345 } else {
346 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700347 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800348 }
349 if (t != null) {
350 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
351 }
352 WebView webView = t.getWebView();
353 if (extra != null) {
354 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
355 if (scale > 0 && scale <= 1000) {
356 webView.setInitialScale(scale);
357 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 }
359 }
John Reckd8c74522011-06-14 08:45:00 -0700360 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700362 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500363 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700364 List<Tab> tabs = mTabControl.getTabs();
365 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700366
John Reck1cf4b792011-07-26 10:22:22 -0700367 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700368 //handle restored pages that may require a JS interface
369 if (t.getWebView() != null) {
370 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
371 if (backForwardList != null) {
372 for (int i = 0; i < backForwardList.getSize(); i++) {
373 WebHistoryItem item = backForwardList.getItemAtIndex(i);
374 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
375 }
376 }
377 }
John Reck1cf4b792011-07-26 10:22:22 -0700378 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700379 if (t != mTabControl.getCurrentTab()) {
380 t.pause();
381 }
John Reck1cf4b792011-07-26 10:22:22 -0700382 }
383 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700384 if (tabs.size() == 0) {
385 openTabToHomePage();
386 }
John Reck1cf4b792011-07-26 10:22:22 -0700387 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 // TabControl.restoreState() will create a new tab even if
389 // restoring the state fails.
390 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800391 // Intent is non-null when framework thinks the browser should be
392 // launching with a new intent (icicle is null).
393 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700394 mIntentHandler.onNewIntent(intent);
395 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700398 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800399 if (jsFlags.trim().length() != 0) {
400 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 }
George Mount3636d0a2011-11-21 09:08:21 -0800402 if (intent != null
403 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700404 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800405 }
Site Maoabb7bd32015-03-20 15:34:02 -0700406 mLowPowerReceiver = new PowerConnectionReceiver();
407 mPowerChangeReceiver = new PowerConnectionReceiver();
408
409 //always track the android framework's power save mode
410 IntentFilter filter = new IntentFilter();
411 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
412 // Power save mode only exists in Lollipop and above
413 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
414 }
415 filter.addAction(Intent.ACTION_BATTERY_OKAY);
416 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700417 }
418
John Reck1cf4b792011-07-26 10:22:22 -0700419 private static class PruneThumbnails implements Runnable {
420 private Context mContext;
421 private List<Long> mIds;
422
423 PruneThumbnails(Context context, List<Long> preserveIds) {
424 mContext = context.getApplicationContext();
425 mIds = preserveIds;
426 }
427
428 @Override
429 public void run() {
430 ContentResolver cr = mContext.getContentResolver();
431 if (mIds == null || mIds.size() == 0) {
432 cr.delete(Thumbnails.CONTENT_URI, null, null);
433 } else {
434 int length = mIds.size();
435 StringBuilder where = new StringBuilder();
436 where.append(Thumbnails._ID);
437 where.append(" not in (");
438 for (int i = 0; i < length; i++) {
439 where.append(mIds.get(i));
440 if (i < (length - 1)) {
441 where.append(",");
442 }
443 }
444 where.append(")");
445 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
446 }
447 }
448
449 }
450
Michael Kolb1514bb72010-11-22 09:11:48 -0800451 @Override
452 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 return mFactory;
454 }
455
456 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800457 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700458 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700459 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800460 }
461
462 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800463 public void createSubWindow(Tab tab) {
464 endActionMode();
465 WebView mainView = tab.getWebView();
466 WebView subView = mFactory.createWebView((mainView == null)
467 ? false
468 : mainView.isPrivateBrowsingEnabled());
469 mUi.createSubWindow(tab, subView);
470 }
471
472 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700473 public Context getContext() {
474 return mActivity;
475 }
476
477 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700478 public Activity getActivity() {
479 return mActivity;
480 }
481
482 void setUi(UI ui) {
483 mUi = ui;
484 }
485
Michael Kolbaf63dba2012-05-16 12:58:05 -0700486 @Override
487 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700488 return mSettings;
489 }
490
491 IntentHandler getIntentHandler() {
492 return mIntentHandler;
493 }
494
495 @Override
496 public UI getUi() {
497 return mUi;
498 }
499
500 int getMaxTabs() {
501 return mActivity.getResources().getInteger(R.integer.max_tabs);
502 }
503
504 @Override
505 public TabControl getTabControl() {
506 return mTabControl;
507 }
508
Michael Kolb1bf23132010-11-19 12:55:12 -0800509 @Override
510 public List<Tab> getTabs() {
511 return mTabControl.getTabs();
512 }
513
Michael Kolb8233fac2010-10-26 16:08:53 -0700514 private void startHandler() {
515 mHandler = new Handler() {
516
517 @Override
518 public void handleMessage(Message msg) {
519 switch (msg.what) {
520 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700521 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700522 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800523 case UNKNOWN_TYPE_MSG:
524 HashMap unknownTypeMap = (HashMap) msg.obj;
525 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
526 /*
527 * When the context menu is shown to the user
528 * we need to assure that its happening on the current webview
529 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
530 */
531 if (getCurrentWebView() != viewForUnknownType)
532 break;
533
534 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800535 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800536 WebView.HitTestResult result = new WebView.HitTestResult();
537
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800538 // Prevent unnecessary calls to context menu
539 // if url and image src are null
540 if (unknown_type_src == null && unknown_type_url == null)
541 break;
542
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800543 //setting the HitTestResult with new RESULT TYPE
544 if (!TextUtils.isEmpty(unknown_type_src)) {
545 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
546 result.setExtra(unknown_type_src);
547 } else {
548 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
549 result.setExtra("about:blank");
550 }
551
552 mResult = result;
553 openContextMenu(viewForUnknownType);
554 mResult = null;
555
556 break;
557
Michael Kolb8233fac2010-10-26 16:08:53 -0700558 case FOCUS_NODE_HREF:
559 {
560 String url = (String) msg.getData().get("url");
561 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500562 String src = (String) msg.getData().get("src");
563 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700564 if (TextUtils.isEmpty(url)) {
565 break;
566 }
567 HashMap focusNodeMap = (HashMap) msg.obj;
568 WebView view = (WebView) focusNodeMap.get("webview");
569 // Only apply the action if the top window did not change.
570 if (getCurrentTopWebView() != view) {
571 break;
572 }
573 switch (msg.arg1) {
574 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700575 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700576 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500577 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700578 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500579 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500580 case R.id.open_newtab_context_menu_id:
581 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700582 openTab(url, parent,
583 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500584 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700585 case R.id.copy_link_context_menu_id:
586 copy(url);
587 break;
588 case R.id.save_link_context_menu_id:
589 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500590 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800591 mActivity, url, view.getSettings().getUserAgentString(),
592 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700594 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700595 if(title == null || title == "")
596 title = url;
597
598 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
599 //SWE TODO: No thumbnail support for the url obtained via
600 //browser context menu as its not loaded in webview.
601 if (bookmarkIntent != null) {
602 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
603 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
604 mActivity.startActivity(bookmarkIntent);
605 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700606 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700607 }
608 break;
609 }
610
611 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700612 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 break;
614
615 case STOP_LOAD:
616 stopLoading();
617 break;
618
619 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700620 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 mWakeLock.release();
622 // if we reach here, Browser should be still in the
623 // background loading after WAKELOCK_TIMEOUT (5-min).
624 // To avoid burning the battery, stop loading.
625 mTabControl.stopAllLoading();
626 }
627 break;
628
kaiyiz591110b2013-08-06 17:11:06 +0800629 case OPEN_MENU:
630 if (!mOptionsMenuOpen && mActivity != null ) {
631 mActivity.openOptionsMenu();
632 }
633 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700634 }
635 }
636 };
637
638 }
639
John Reckef654f12011-07-12 16:42:08 -0700640 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200641 public Tab getCurrentTab() {
642 return mTabControl.getCurrentTab();
643 }
644
Michael Kolbba99c5d2010-11-29 14:57:41 -0800645 @Override
646 public void shareCurrentPage() {
647 shareCurrentPage(mTabControl.getCurrentTab());
648 }
649
650 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700651 if (tab == null || tab.getWebView() == null)
652 return;
653
654 final Tab mytab = tab;
655 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
656 @Override
657 public void onReceiveValue(Bitmap bitmap) {
658 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
659 mytab.getFavicon(), bitmap);
660 }
661 };
662
663 createScreenshotAsync(
664 tab.getWebView(),
665 getDesiredThumbnailWidth(mActivity),
666 getDesiredThumbnailHeight(mActivity),
667 new ValueCallback<Bitmap>() {
668 @Override
669 public void onReceiveValue(Bitmap bitmap) {
670 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
671 mytab.getFavicon(), bitmap);
672 }
673 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800674 }
675
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 /**
677 * Share a page, providing the title, url, favicon, and a screenshot. Uses
678 * an {@link Intent} to launch the Activity chooser.
679 * @param c Context used to launch a new Activity.
680 * @param title Title of the page. Stored in the Intent with
681 * {@link Intent#EXTRA_SUBJECT}
682 * @param url URL of the page. Stored in the Intent with
683 * {@link Intent#EXTRA_TEXT}
684 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
685 * with {@link Browser#EXTRA_SHARE_FAVICON}
686 * @param screenshot Bitmap of a screenshot of the page. Stored in the
687 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
688 */
689 static final void sharePage(Context c, String title, String url,
690 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700691
692 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
693 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
694 R.layout.app_row, sDialog.getApps());
695 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 }
697
698 private void copy(CharSequence text) {
699 ClipboardManager cm = (ClipboardManager) mActivity
700 .getSystemService(Context.CLIPBOARD_SERVICE);
701 cm.setText(text);
702 }
703
704 // lifecycle
705
John Reck9c35b9c2012-05-30 10:08:50 -0700706 @Override
707 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700708 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800709 // update the menu in case of a locale change
710 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800711 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800712 if (mOptionsMenuOpen) {
713 mActivity.closeOptionsMenu();
714 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
715 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700716 if (mPageDialogsHandler != null) {
717 mPageDialogsHandler.onConfigurationChanged(config);
718 }
719 mUi.onConfigurationChanged(config);
720 }
721
722 @Override
723 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700724 if (!mUi.isWebShowing()) {
725 mUi.showWeb(false);
726 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 mIntentHandler.onNewIntent(intent);
728 }
729
John Reck9c35b9c2012-05-30 10:08:50 -0700730 @Override
731 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800732 if (mUi.isCustomViewShowing()) {
733 hideCustomView();
734 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700735 if (mActivityPaused) {
736 Log.e(LOGTAG, "BrowserActivity is already paused.");
737 return;
738 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800740 Tab tab = mTabControl.getCurrentTab();
741 if (tab != null) {
742 tab.pause();
743 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700744 if (mWakeLock == null) {
745 PowerManager pm = (PowerManager) mActivity
746 .getSystemService(Context.POWER_SERVICE);
747 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
748 }
Michael Kolb70976932010-11-30 11:34:01 -0800749 mWakeLock.acquire();
750 mHandler.sendMessageDelayed(mHandler
751 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
752 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 }
754 mUi.onPause();
755 mNetworkHandler.onPause();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100756 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700757 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 }
759
John Reck9c35b9c2012-05-30 10:08:50 -0700760 @Override
761 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800763 Bundle saveState = createSaveState();
764
765 // crash recovery manages all save & restore state
766 mCrashRecoveryHandler.writeState(saveState);
767 mSettings.setLastRunPaused(true);
768 }
769
770 /**
771 * Save the current state to outState. Does not write the state to
772 * disk.
773 * @return Bundle containing the current state of all tabs.
774 */
775 /* package */ Bundle createSaveState() {
776 Bundle saveState = new Bundle();
777 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800778 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700779 }
780
John Reck9c35b9c2012-05-30 10:08:50 -0700781 @Override
782 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700783 if (!mActivityPaused) {
784 Log.e(LOGTAG, "BrowserActivity is already resumed.");
785 return;
786 }
George Mount3636d0a2011-11-21 09:08:21 -0800787 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700788 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800789 Tab current = mTabControl.getCurrentTab();
790 if (current != null) {
791 current.resume();
792 resumeWebViewTimers(current);
793 }
John Reckf57c0292011-07-21 18:15:39 -0700794 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200795
Michael Kolb8233fac2010-10-26 16:08:53 -0700796 mUi.onResume();
797 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100798 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700799 if (mVoiceResult != null) {
800 mUi.onVoiceResult(mVoiceResult);
801 mVoiceResult = null;
802 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700803 if (current != null && current.getWebView().isShowingCrashView())
804 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700805 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 }
807
John Reckf57c0292011-07-21 18:15:39 -0700808 private void releaseWakeLock() {
809 if (mWakeLock != null && mWakeLock.isHeld()) {
810 mHandler.removeMessages(RELEASE_WAKELOCK);
811 mWakeLock.release();
812 }
813 }
814
Michael Kolb70976932010-11-30 11:34:01 -0800815 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800816 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800817 * @param tab guaranteed non-null
818 */
819 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 boolean inLoad = tab.inPageLoad();
821 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
822 CookieSyncManager.getInstance().startSync();
823 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100824 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 }
826 }
827
Michael Kolb70976932010-11-30 11:34:01 -0800828 /**
829 * Pause all WebView timers using the WebView of the given tab
830 * @param tab
831 * @return true if the timers are paused or tab is null
832 */
833 private boolean pauseWebViewTimers(Tab tab) {
834 if (tab == null) {
835 return true;
836 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100838 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 }
Michael Kolb70976932010-11-30 11:34:01 -0800841 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 }
843
John Reck9c35b9c2012-05-30 10:08:50 -0700844 @Override
845 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800846 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
848 mUploadHandler = null;
849 }
850 if (mTabControl == null) return;
851 mUi.onDestroy();
852 // Remove the current tab and sub window
853 Tab t = mTabControl.getCurrentTab();
854 if (t != null) {
855 dismissSubWindow(t);
856 removeTab(t);
857 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500858 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 // Destroy all the tabs
860 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700861 // Unregister receiver
862 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 }
864
865 protected boolean isActivityPaused() {
866 return mActivityPaused;
867 }
868
John Reck9c35b9c2012-05-30 10:08:50 -0700869 @Override
870 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 mTabControl.freeMemory();
872 }
873
874 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700875 public void stopLoading() {
876 mLoadStopped = true;
877 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700879 if (w != null) {
880 w.stopLoading();
881 mUi.onPageStopped(tab);
882 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 }
884
885 boolean didUserStopLoading() {
886 return mLoadStopped;
887 }
888
kaiyiza016da12013-08-26 17:50:22 +0800889 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700890 final String reminderType = getContext().getResources().getString(
891 R.string.def_wifi_browser_interaction_remind_type);
892 final String selectionConnnection = getContext().getResources().getString(
893 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700894 final String wifiSelection = getContext().getResources().getString(
895 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700896
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700897 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
898 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700899 return;
900
kaiyiza016da12013-08-26 17:50:22 +0800901 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700902 Context.CONNECTIVITY_SERVICE);
903 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700904 WifiManager wifiMgr = (WifiManager) this.getContext()
905 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700906 if (networkInfo == null
907 || (networkInfo != null && (networkInfo.getType() !=
908 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700909 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
910 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700911 List<ScanResult> list = wifiMgr.getScanResults();
912 // Have no AP's for Wifi's fall back to data
913 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700914 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700915 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
916 this.getContext().startActivity(intent);
917 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700918 // Request to select Wifi AP
919 try {
920 Intent intent = new Intent(wifiSelection);
921 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
922 this.getContext().startActivity(intent);
923 } catch (Exception e) {
924 String err_msg = this.getContext().getString(
925 R.string.acivity_not_found, wifiSelection);
926 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
927 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700928 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700929 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800930 }
931 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700932
Michael Kolb8233fac2010-10-26 16:08:53 -0700933 // WebViewController
934
935 @Override
John Reck324d4402011-01-11 16:56:42 -0800936 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700937
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 // reset sync timer to avoid sync starts during loading a page
939 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700940 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700941 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800942 boolean networkNotifier = BrowserConfig.getInstance(getContext())
943 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700944 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700945 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800946 } else {
947 if (!mNetworkHandler.isNetworkUp()) {
948 view.setNetworkAvailable(false);
949 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 }
951
952 // when BrowserActivity just starts, onPageStarted may be called before
953 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
954 // to start the timer. As we won't switch tabs while an activity is in
955 // pause state, we can ensure calling resume and pause in pair.
956 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800957 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 }
959 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700960 endActionMode();
961
John Reck30c714c2010-12-16 17:30:34 -0800962 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700963
John Reck324d4402011-01-11 16:56:42 -0800964 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 // update the bookmark database for favicon
966 maybeUpdateFavicon(tab, null, url, favicon);
967
968 Performance.tracePageStart(url);
969
970 // Performance probe
971 if (false) {
972 Performance.onPageStarted();
973 }
974
975 }
976
977 @Override
John Reck324d4402011-01-11 16:56:42 -0800978 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700979 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800980 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200981
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 // Performance probe
983 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800984 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 }
986
Tarun Nainani8eb00912014-07-17 12:28:32 -0700987 tab.onPageFinished();
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700988 maybeUpdateFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700989
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 Performance.tracePageFinished();
991 }
992
993 @Override
John Reck30c714c2010-12-16 17:30:34 -0800994 public void onProgressChanged(Tab tab) {
995 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700996
997 if (newProgress == 100) {
998 CookieSyncManager.getInstance().sync();
999 // onProgressChanged() may continue to be called after the main
1000 // frame has finished loading, as any remaining sub frames continue
1001 // to load. We'll only get called once though with newProgress as
1002 // 100 when everything is loaded. (onPageFinished is called once
1003 // when the main frame completes loading regardless of the state of
1004 // any sub frames so calls to onProgressChanges may continue after
1005 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001006 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001007 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001008 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001009 } else if (mWasInPageLoad) {
1010 mWasInPageLoad = false;
1011 updateInLoadMenuItems(mCachedMenu, tab);
1012 }
1013
1014 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001015 // pause the WebView timer and release the wake lock if it is
1016 // finished while BrowserActivity is in pause state.
1017 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 }
1019 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001020 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001021 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001022 // still loading
1023 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001025 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001026 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001027 } else {
1028 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001029 }
1030 }
John Reck30c714c2010-12-16 17:30:34 -08001031 mUi.onProgressChanged(tab);
1032 }
1033
1034 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001035 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001036 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 }
1038
1039 @Override
1040 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001041 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001042 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001043 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1045 return;
1046 }
1047 // Update the title in the history database if not in private browsing mode
1048 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001049 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 }
1051 }
1052
1053 @Override
1054 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001055 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1057 }
1058
1059 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001060 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1061 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 }
1063
1064 @Override
1065 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1066 if (mMenuIsDown) {
1067 // only check shortcut key when MENU is held
1068 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1069 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001070 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001071 int keyCode = event.getKeyCode();
1072 // We need to send almost every key to WebKit. However:
1073 // 1. We don't want to block the device on the renderer for
1074 // some keys like menu, home, call.
1075 // 2. There are no WebKit equivalents for some of these keys
1076 // (see app/keyboard_codes_win.h)
1077 // Note that these are not the same set as KeyEvent.isSystemKey:
1078 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1079 if (keyCode == KeyEvent.KEYCODE_MENU ||
1080 keyCode == KeyEvent.KEYCODE_HOME ||
1081 keyCode == KeyEvent.KEYCODE_BACK ||
1082 keyCode == KeyEvent.KEYCODE_CALL ||
1083 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1084 keyCode == KeyEvent.KEYCODE_POWER ||
1085 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1086 keyCode == KeyEvent.KEYCODE_CAMERA ||
1087 keyCode == KeyEvent.KEYCODE_FOCUS ||
1088 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1089 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1090 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1091 return true;
1092 }
1093
1094 // We also have to intercept some shortcuts before we send them to the ContentView.
1095 if (event.isCtrlPressed() && (
1096 keyCode == KeyEvent.KEYCODE_TAB ||
1097 keyCode == KeyEvent.KEYCODE_W ||
1098 keyCode == KeyEvent.KEYCODE_F4)) {
1099 return true;
1100 }
1101
1102 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 }
1104
Tarun Nainanif5563b62015-01-21 18:52:59 -08001105 private void handleMediaKeyEvent(KeyEvent event) {
1106
1107 int keyCode = event.getKeyCode();
1108 // send media key events to audio manager
1109 if (Build.VERSION.SDK_INT >= 19) {
1110
1111 switch (keyCode) {
1112 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1113 case KeyEvent.KEYCODE_MEDIA_STOP:
1114 case KeyEvent.KEYCODE_MEDIA_NEXT:
1115 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1116 case KeyEvent.KEYCODE_MEDIA_REWIND:
1117 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1118 case KeyEvent.KEYCODE_MUTE:
1119 case KeyEvent.KEYCODE_MEDIA_PLAY:
1120 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1121 case KeyEvent.META_SHIFT_RIGHT_ON:
1122 case KeyEvent.KEYCODE_MEDIA_EJECT:
1123 case KeyEvent.KEYCODE_MEDIA_RECORD:
1124 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1125
1126 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1127 .getSystemService(Context.AUDIO_SERVICE);
1128 audioManager.dispatchMediaKeyEvent(event);
1129 }
1130 }
1131 }
1132
Michael Kolb8233fac2010-10-26 16:08:53 -07001133 @Override
John Reck997b1b72012-04-19 18:08:25 -07001134 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001136 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001138 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 } else {
John Reck997b1b72012-04-19 18:08:25 -07001140 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 }
1142 }
John Reck997b1b72012-04-19 18:08:25 -07001143 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 }
1145
1146 @Override
John Reck324d4402011-01-11 16:56:42 -08001147 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001148 // Don't save anything in private browsing mode or when disabling history
1149 // for regular tabs is enabled
1150 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1151 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1152 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001153
John Reck49a603c2011-03-03 09:33:05 -08001154 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001155
John Reck324d4402011-01-11 16:56:42 -08001156 if (TextUtils.isEmpty(url)
1157 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001158 return;
1159 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001160
John Reckf57c0292011-07-21 18:15:39 -07001161 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001162 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001163 }
1164
1165 @Override
1166 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1167 AsyncTask<Void, Void, String[]> task =
1168 new AsyncTask<Void, Void, String[]>() {
1169 @Override
1170 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001171 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 }
1173 @Override
1174 public void onPostExecute(String[] result) {
1175 callback.onReceiveValue(result);
1176 }
1177 };
1178 task.execute();
1179 }
1180
1181 @Override
1182 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1183 final HttpAuthHandler handler, final String host,
1184 final String realm) {
1185 String username = null;
1186 String password = null;
1187
1188 boolean reuseHttpAuthUsernamePassword
1189 = handler.useHttpAuthUsernamePassword();
1190
1191 if (reuseHttpAuthUsernamePassword && view != null) {
1192 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1193 if (credentials != null && credentials.length == 2) {
1194 username = credentials[0];
1195 password = credentials[1];
1196 }
1197 }
1198
1199 if (username != null && password != null) {
1200 handler.proceed(username, password);
1201 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001202 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1204 } else {
1205 handler.cancel();
1206 }
1207 }
1208 }
1209
1210 @Override
1211 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001212 String contentDisposition, String mimetype, String referer,
1213 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001214 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001215 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001216 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001217 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001218 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 // This Tab was opened for the sole purpose of downloading a
1220 // file. Remove it.
1221 if (tab == mTabControl.getCurrentTab()) {
1222 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001223 if (tab.getDerivedFromIntent())
1224 closeTab(tab);
1225 else
1226 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001227 } else {
1228 // In this case, it is not.
1229 closeTab(tab);
1230 }
1231 }
1232 }
1233
1234 @Override
1235 public Bitmap getDefaultVideoPoster() {
1236 return mUi.getDefaultVideoPoster();
1237 }
1238
1239 @Override
1240 public View getVideoLoadingProgressView() {
1241 return mUi.getVideoLoadingProgressView();
1242 }
1243
1244 @Override
1245 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1246 SslError error) {
1247 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1248 }
1249
1250 // helper method
1251
1252 /*
1253 * Update the favorites icon if the private browsing isn't enabled and the
1254 * icon is valid.
1255 */
1256 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1257 final String url, Bitmap favicon) {
1258 if (favicon == null) {
1259 return;
1260 }
1261 if (!tab.isPrivateBrowsingEnabled()) {
1262 Bookmarks.updateFavicon(mActivity
1263 .getContentResolver(), originalUrl, url, favicon);
1264 }
1265 }
1266
Leon Scroggins4cd97792010-12-03 15:31:56 -05001267 @Override
1268 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001269 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001270 mUi.bookmarkedStatusHasChanged(tab);
1271 }
1272
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 // end WebViewController
1274
1275 protected void pageUp() {
1276 getCurrentTopWebView().pageUp(false);
1277 }
1278
1279 protected void pageDown() {
1280 getCurrentTopWebView().pageDown(false);
1281 }
1282
1283 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001284 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001285 public void editUrl() {
1286 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001287 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001288 }
1289
John Reck9c35b9c2012-05-30 10:08:50 -07001290 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001291 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001292 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 if (tab.inForeground()) {
1294 if (mUi.isCustomViewShowing()) {
1295 callback.onCustomViewHidden();
1296 return;
1297 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001298 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001299 // Save the menu state and set it to empty while the custom
1300 // view is showing.
1301 mOldMenuState = mMenuState;
1302 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001303 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 }
1305 }
1306
1307 @Override
1308 public void hideCustomView() {
1309 if (mUi.isCustomViewShowing()) {
1310 mUi.onHideCustomView();
1311 // Reset the old menu state.
1312 mMenuState = mOldMenuState;
1313 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001314 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001315 }
1316 }
1317
John Reck9c35b9c2012-05-30 10:08:50 -07001318 @Override
1319 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001320 Intent intent) {
1321 if (getCurrentTopWebView() == null) return;
1322 switch (requestCode) {
1323 case PREFERENCES_PAGE:
1324 if (resultCode == Activity.RESULT_OK && intent != null) {
1325 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001326 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 mTabControl.removeParentChildRelationShips();
1328 }
1329 }
1330 break;
1331 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001332 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001333 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001334 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001336 case AUTOFILL_SETUP:
1337 // Determine whether a profile was actually set up or not
1338 // and if so, send the message back to the WebTextView to
1339 // fill the form with the new profile.
1340 if (getSettings().getAutoFillProfile() != null) {
1341 mAutoFillSetupMessage.sendToTarget();
1342 mAutoFillSetupMessage = null;
1343 }
1344 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001345 case COMBO_VIEW:
1346 if (intent == null || resultCode != Activity.RESULT_OK) {
1347 break;
1348 }
John Reck3ba45532011-08-11 16:26:53 -07001349 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001350 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1351 Tab t = getCurrentTab();
1352 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001353 mUpdateMyNavThumbnail = true;
1354 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001355 loadUrl(t, uri.toString());
1356 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1357 String[] urls = intent.getStringArrayExtra(
1358 ComboViewActivity.EXTRA_OPEN_ALL);
1359 Tab parent = getCurrentTab();
1360 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001361 if (url != null) {
1362 parent = openTab(url, parent,
1363 !mSettings.openInBackground(), true);
1364 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001365 }
1366 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1367 long id = intent.getLongExtra(
1368 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1369 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001370 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001371 }
1372 }
1373 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001374 case VOICE_RESULT:
1375 if (resultCode == Activity.RESULT_OK && intent != null) {
1376 ArrayList<String> results = intent.getStringArrayListExtra(
1377 RecognizerIntent.EXTRA_RESULTS);
1378 if (results.size() >= 1) {
1379 mVoiceResult = results.get(0);
1380 }
1381 }
1382 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001383 case MY_NAVIGATION:
1384 if (intent == null || resultCode != Activity.RESULT_OK) {
1385 break;
1386 }
1387
1388 if (intent.getBooleanExtra("need_refresh", false) &&
1389 getCurrentTopWebView() != null) {
1390 getCurrentTopWebView().reload();
1391 }
1392 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001393 default:
1394 break;
1395 }
1396 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001397 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 }
1399
1400 /**
1401 * Open the Go page.
1402 * @param startWithHistory If true, open starting on the history tab.
1403 * Otherwise, start with the bookmarks tab.
1404 */
1405 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001406 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 if (mTabControl.getCurrentWebView() == null) {
1408 return;
1409 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001410 // clear action mode
1411 if (isInCustomActionMode()) {
1412 endActionMode();
1413 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 Bundle extras = new Bundle();
1415 // Disable opening in a new window if we have maxed out the windows
1416 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1417 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001418 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 }
1420
1421 // combo view callbacks
1422
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 // key handling
1424 protected void onBackKey() {
1425 if (!mUi.onBackKey()) {
1426 WebView subwindow = mTabControl.getCurrentSubWindow();
1427 if (subwindow != null) {
1428 if (subwindow.canGoBack()) {
1429 subwindow.goBack();
1430 } else {
1431 dismissSubWindow(mTabControl.getCurrentTab());
1432 }
1433 } else {
1434 goBackOnePageOrQuit();
1435 }
1436 }
1437 }
1438
Michael Kolb4bd767d2011-05-27 11:33:55 -07001439 protected boolean onMenuKey() {
1440 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001441 }
1442
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 // menu handling and state
1444 // TODO: maybe put into separate handler
1445
John Reck9c35b9c2012-05-30 10:08:50 -07001446 @Override
1447 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001448 if (mMenuState == EMPTY_MENU) {
1449 return false;
1450 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 MenuInflater inflater = mActivity.getMenuInflater();
1452 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 return true;
1454 }
1455
John Reck9c35b9c2012-05-30 10:08:50 -07001456 @Override
1457 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001459 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001460 return;
1461 }
1462 if (!(v instanceof WebView)) {
1463 return;
1464 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001465 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001466 WebView.HitTestResult result;
1467
1468 /* Determine whether the ContextMenu got triggered because
1469 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1470 * received. The mResult acts as a flag to identify, how it got trigerred
1471 */
1472 if (mResult == null){
1473 result = webview.getHitTestResult();
1474 } else {
1475 result = mResult;
1476 }
1477
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 if (result == null) {
1479 return;
1480 }
1481
1482 int type = result.getType();
1483 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001484
1485 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1486 unknownTypeMap.put("webview", webview);
1487 final Message msg = mHandler.obtainMessage(
1488 UNKNOWN_TYPE_MSG, unknownTypeMap);
1489 /* As defined in android developers guide
1490 * when UNKNOWN_TYPE is received as a result of HitTest
1491 * you need to determing the type by invoking requestFocusNodeHref
1492 */
1493 webview.requestFocusNodeHref(msg);
1494
Michael Kolb8233fac2010-10-26 16:08:53 -07001495 Log.w(LOGTAG,
1496 "We should not show context menu when nothing is touched");
1497 return;
1498 }
1499 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1500 // let TextView handles context menu
1501 return;
1502 }
1503
1504 // Note, http://b/issue?id=1106666 is requesting that
1505 // an inflated menu can be used again. This is not available
1506 // yet, so inflate each time (yuk!)
1507 MenuInflater inflater = mActivity.getMenuInflater();
1508 inflater.inflate(R.menu.browsercontext, menu);
1509
1510 // Show the correct menu group
1511 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001512 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001513 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 menu.setGroupVisible(R.id.PHONE_MENU,
1515 type == WebView.HitTestResult.PHONE_TYPE);
1516 menu.setGroupVisible(R.id.EMAIL_MENU,
1517 type == WebView.HitTestResult.EMAIL_TYPE);
1518 menu.setGroupVisible(R.id.GEO_MENU,
1519 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001520 String itemUrl = null;
1521 String url = webview.getOriginalUrl();
1522 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1523 itemUrl = Uri.decode(navigationUrl);
1524 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1525 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1526 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1527 } else {
1528 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1529 }
1530 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1531 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1532 } else {
1533 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1534
1535 menu.setGroupVisible(R.id.IMAGE_MENU,
1536 type == WebView.HitTestResult.IMAGE_TYPE
1537 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1538 menu.setGroupVisible(R.id.ANCHOR_MENU,
1539 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1540 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001541 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1542 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001543 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001544 // Setup custom handling depending on the type
1545 switch (type) {
1546 case WebView.HitTestResult.PHONE_TYPE:
1547 menu.setHeaderTitle(Uri.decode(extra));
1548 menu.findItem(R.id.dial_context_menu_id).setIntent(
1549 new Intent(Intent.ACTION_VIEW, Uri
1550 .parse(WebView.SCHEME_TEL + extra)));
1551 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1552 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1553 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1554 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1555 addIntent);
1556 menu.findItem(R.id.copy_phone_context_menu_id)
1557 .setOnMenuItemClickListener(
1558 new Copy(extra));
1559 break;
1560
1561 case WebView.HitTestResult.EMAIL_TYPE:
1562 menu.setHeaderTitle(extra);
1563 menu.findItem(R.id.email_context_menu_id).setIntent(
1564 new Intent(Intent.ACTION_VIEW, Uri
1565 .parse(WebView.SCHEME_MAILTO + extra)));
1566 menu.findItem(R.id.copy_mail_context_menu_id)
1567 .setOnMenuItemClickListener(
1568 new Copy(extra));
1569 break;
1570
1571 case WebView.HitTestResult.GEO_TYPE:
1572 menu.setHeaderTitle(extra);
1573 menu.findItem(R.id.map_context_menu_id).setIntent(
1574 new Intent(Intent.ACTION_VIEW, Uri
1575 .parse(WebView.SCHEME_GEO
1576 + URLEncoder.encode(extra))));
1577 menu.findItem(R.id.copy_geo_context_menu_id)
1578 .setOnMenuItemClickListener(
1579 new Copy(extra));
1580 break;
1581
1582 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1583 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001584 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001585 // decide whether to show the open link in new tab option
1586 boolean showNewTab = mTabControl.canCreateNewTab();
1587 MenuItem newTabItem
1588 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001589 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001590 ? R.string.contextmenu_openlink_newwindow_background
1591 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001592 newTabItem.setVisible(showNewTab);
1593 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001594 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1595 newTabItem.setOnMenuItemClickListener(
1596 new MenuItem.OnMenuItemClickListener() {
1597 @Override
1598 public boolean onMenuItemClick(MenuItem item) {
1599 final HashMap<String, WebView> hrefMap =
1600 new HashMap<String, WebView>();
1601 hrefMap.put("webview", webview);
1602 final Message msg = mHandler.obtainMessage(
1603 FOCUS_NODE_HREF,
1604 R.id.open_newtab_context_menu_id,
1605 0, hrefMap);
1606 webview.requestFocusNodeHref(msg);
1607 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001609 });
1610 } else {
1611 newTabItem.setOnMenuItemClickListener(
1612 new MenuItem.OnMenuItemClickListener() {
1613 @Override
1614 public boolean onMenuItemClick(MenuItem item) {
1615 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001616 openTab(extra, parent,
1617 !mSettings.openInBackground(),
1618 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001619 return true;
1620 }
1621 });
1622 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001624 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1625 menu.setHeaderTitle(navigationUrl);
1626 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1627
1628 if (itemUrl != null) {
1629 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1630 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1631 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1632 @Override
1633 public boolean onMenuItemClick(MenuItem item) {
1634 final Intent intent = new Intent(Controller.this
1635 .getContext(),
1636 AddMyNavigationPage.class);
1637 Bundle bundle = new Bundle();
1638 String url = Uri.decode(navigationUrl);
1639 bundle.putBoolean("isAdding", false);
1640 bundle.putString("url", url);
1641 bundle.putString("name", getNameFromUrl(url));
1642 intent.putExtra("websites", bundle);
1643 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1644 return false;
1645 }
1646 });
1647 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1648 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1649 @Override
1650 public boolean onMenuItemClick(MenuItem item) {
1651 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1652 return false;
1653 }
1654 });
1655 }
1656 } else {
1657 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1658 }
1659 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001660 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1661 break;
1662 }
1663 // otherwise fall through to handle image part
1664 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001665 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1666 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001667 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1668 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001669 shareItem.setOnMenuItemClickListener(
1670 new MenuItem.OnMenuItemClickListener() {
1671 @Override
1672 public boolean onMenuItemClick(MenuItem item) {
1673 sharePage(mActivity, null, extra, null,
1674 null);
1675 return true;
1676 }
1677 }
1678 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001679 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001680 menu.findItem(R.id.view_image_context_menu_id)
1681 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1682 @Override
1683 public boolean onMenuItemClick(MenuItem item) {
1684 openTab(extra, mTabControl.getCurrentTab(), true, true);
1685 return false;
1686 }
1687 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001688 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1689 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1690 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001691 menu.findItem(R.id.set_wallpaper_context_menu_id).
1692 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1693 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001694 break;
1695
1696 default:
1697 Log.w(LOGTAG, "We should not get here.");
1698 break;
1699 }
1700 //update the ui
1701 mUi.onContextMenuCreated(menu);
1702 }
1703
kaiyiz6e5b3e02013-08-19 20:02:01 +08001704 public void startAddMyNavigation(String url) {
1705 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1706 Bundle bundle = new Bundle();
1707 bundle.putBoolean("isAdding", true);
1708 bundle.putString("url", url);
1709 bundle.putString("name", getNameFromUrl(url));
1710 intent.putExtra("websites", bundle);
1711 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1712 }
1713
1714 private void showMyNavigationDeleteDialog(final String itemUrl) {
1715 new AlertDialog.Builder(this.getContext())
1716 .setTitle(R.string.my_navigation_delete_label)
1717 .setIcon(android.R.drawable.ic_dialog_alert)
1718 .setMessage(R.string.my_navigation_delete_msg)
1719 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1720 @Override
1721 public void onClick(DialogInterface dialog, int whichButton) {
1722 deleteMyNavigationItem(itemUrl);
1723 }
1724 })
1725 .setNegativeButton(R.string.cancel, null)
1726 .show();
1727 }
1728
1729 private void deleteMyNavigationItem(final String itemUrl) {
1730 ContentResolver cr = this.getContext().getContentResolver();
1731 Cursor cursor = null;
1732
1733 try {
1734 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1735 new String[] {
1736 MyNavigationUtil.ID
1737 }, "url = ?", new String[] {
1738 itemUrl
1739 }, null);
1740 if (null != cursor && cursor.moveToFirst()) {
1741 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1742 cursor.getLong(0));
1743
1744 ContentValues values = new ContentValues();
1745 values.put(MyNavigationUtil.TITLE, "");
1746 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1747 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1748 ByteArrayOutputStream os = new ByteArrayOutputStream();
1749 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1750 R.raw.my_navigation_add);
1751 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1752 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1753 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1754 cr.update(uri, values, null, null);
1755 } else {
1756 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1757 }
1758 } catch (IllegalStateException e) {
1759 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1760 } finally {
1761 if (null != cursor) {
1762 cursor.close();
1763 }
1764 }
1765
1766 if (getCurrentTopWebView() != null) {
1767 getCurrentTopWebView().reload();
1768 }
1769 }
1770
1771 private String getNameFromUrl(String itemUrl) {
1772 ContentResolver cr = this.getContext().getContentResolver();
1773 Cursor cursor = null;
1774 String name = null;
1775
1776 try {
1777 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1778 new String[] {
1779 MyNavigationUtil.TITLE
1780 }, "url = ?", new String[] {
1781 itemUrl
1782 }, null);
1783 if (null != cursor && cursor.moveToFirst()) {
1784 name = cursor.getString(0);
1785 } else {
1786 Log.e(LOGTAG, "this item does not exist!");
1787 }
1788 } catch (IllegalStateException e) {
1789 Log.e(LOGTAG, "getNameFromUrl", e);
1790 } finally {
1791 if (null != cursor) {
1792 cursor.close();
1793 }
1794 }
1795 return name;
1796 }
1797
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001798 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001799 final ContentResolver cr = mActivity.getContentResolver();
1800 new AsyncTask<Void, Void, Void>() {
1801 @Override
1802 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001803 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1804 if(!isMyNavigationUrl)
1805 return null;
1806
kaiyiz6e5b3e02013-08-19 20:02:01 +08001807 ContentResolver cr = mActivity.getContentResolver();
1808 Cursor cursor = null;
1809 try {
1810 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1811 new String[] {
1812 MyNavigationUtil.ID
1813 }, "url = ?", new String[] {
1814 itemUrl
1815 }, null);
1816 if (null != cursor && cursor.moveToFirst()) {
1817 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001818 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001819
1820 ContentValues values = new ContentValues();
1821 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1822 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1823 cursor.getLong(0));
1824 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1825 cr.update(uri, values, null, null);
1826 os.close();
1827 }
1828 } catch (IllegalStateException e) {
1829 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1830 } catch (IOException e) {
1831 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1832 } finally {
1833 if (null != cursor) {
1834 cursor.close();
1835 }
1836 }
1837 return null;
1838 }
1839 }.execute();
1840 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001841 /**
1842 * As the menu can be open when loading state changes
1843 * we must manually update the state of the stop/reload menu
1844 * item
1845 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001846 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001847 if (menu == null) {
1848 return;
1849 }
1850 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001851 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001852 menu.findItem(R.id.stop_menu_id):
1853 menu.findItem(R.id.reload_menu_id);
1854 if (src != null) {
1855 dest.setIcon(src.getIcon());
1856 dest.setTitle(src.getTitle());
1857 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001858 mActivity.invalidateOptionsMenu();
1859 }
1860
1861 public void invalidateOptionsMenu() {
1862 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001863 }
1864
John Reck9c35b9c2012-05-30 10:08:50 -07001865 @Override
1866 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001867 // Software menu key (toolbar key)
1868 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1869 return true;
1870 }
1871
1872 @Override
1873 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001874 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001875 // hold on to the menu reference here; it is used by the page callbacks
1876 // to update the menu based on loading state
1877 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001878 // Note: setVisible will decide whether an item is visible; while
1879 // setEnabled() will decide whether an item is enabled, which also means
1880 // whether the matching shortcut key will function.
1881 switch (mMenuState) {
1882 case EMPTY_MENU:
1883 if (mCurrentMenuState != mMenuState) {
1884 menu.setGroupVisible(R.id.MAIN_MENU, false);
1885 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1886 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1887 }
1888 break;
1889 default:
1890 if (mCurrentMenuState != mMenuState) {
1891 menu.setGroupVisible(R.id.MAIN_MENU, true);
1892 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1893 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1894 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001895 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001896 break;
1897 }
1898 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001899 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001900
1901 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001902 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1903 EditBookmarksRestriction.getInstance()
1904 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001905 }
1906
1907 private void setMenuItemVisibility(Menu menu, int id,
1908 boolean visibility) {
1909 MenuItem item = menu.findItem(id);
1910 if (item != null) {
1911 item.setVisible(visibility);
1912 }
1913 }
1914
1915 private int lookupBookmark(String title, String url) {
1916 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001917 int count = 0;
1918 Cursor cursor = null;
1919 try {
1920 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1921 BookmarksLoader.PROJECTION,
1922 "title = ? OR url = ?",
1923 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001924 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001925 },
1926 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001927
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001928 if (cursor != null)
1929 count = cursor.getCount();
1930
1931 } catch (IllegalStateException e) {
1932 Log.e(LOGTAG, "lookupBookmark ", e);
1933 } finally {
1934 if (null != cursor) {
1935 cursor.close();
1936 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001937 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001938 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001939 }
1940
1941 private void resetMenuItems(Menu menu) {
1942 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1943 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1944
1945 WebView w = getCurrentTopWebView();
1946 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1947
1948 String title = w.getTitle();
1949 String url = w.getUrl();
1950 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1951 bookmark_icon.setChecked(true);
1952 } else {
1953 bookmark_icon.setChecked(false);
1954 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001955 }
1956
Michael Kolb4bf79712011-07-14 14:18:12 -07001957 @Override
1958 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001959 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001960 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001961 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001962 // Following flag is used to identify schemes for which the LIVE_MENU
1963 // items defined in res/menu/browser.xml should be enabled
1964 boolean isLiveScheme = false;
1965 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001966 boolean isSavable = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001967 resetMenuItems(menu);
1968
Michael Kolb4bf79712011-07-14 14:18:12 -07001969 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001970 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001971 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001972 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001973 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001974 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001975 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07001976 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001977
1978 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1979 forward.setEnabled(canGoForward);
1980
Michael Kolb4bf79712011-07-14 14:18:12 -07001981 // decide whether to show the share link option
1982 PackageManager pm = mActivity.getPackageManager();
1983 Intent send = new Intent(Intent.ACTION_SEND);
1984 send.setType("text/plain");
1985 ResolveInfo ri = pm.resolveActivity(send,
1986 PackageManager.MATCH_DEFAULT_ONLY);
1987 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1988
1989 boolean isNavDump = mSettings.enableNavDump();
1990 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1991 nav.setVisible(isNavDump);
1992 nav.setEnabled(isNavDump);
1993
1994 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001995 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1996 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001997 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08001998 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
1999 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002000 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002001 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2002 isLive && isLiveScheme && isPageFinished);
2003 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002004 isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002005 // history and snapshots item are the members of COMBO menu group,
2006 // so if show history item, only make snapshots item invisible.
2007 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002008
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002009 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002010 }
2011
John Reck9c35b9c2012-05-30 10:08:50 -07002012 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002013 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002014 if (null == getCurrentTopWebView()) {
2015 return false;
2016 }
2017 if (mMenuIsDown) {
2018 // The shortcut action consumes the MENU. Even if it is still down,
2019 // it won't trigger the next shortcut action. In the case of the
2020 // shortcut action triggering a new activity, like Bookmarks, we
2021 // won't get onKeyUp for MENU. So it is important to reset it here.
2022 mMenuIsDown = false;
2023 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002024 if (mUi.onOptionsItemSelected(item)) {
2025 // ui callback handled it
2026 return true;
2027 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002028 switch (item.getItemId()) {
2029 // -- Main menu
2030 case R.id.new_tab_menu_id:
2031 openTabToHomePage();
2032 break;
2033
2034 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002035 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002036 break;
2037
2038 case R.id.goto_menu_id:
2039 editUrl();
2040 break;
2041
2042 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002043 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2044 break;
2045
2046 case R.id.history_menu_id:
2047 bookmarksOrHistoryPicker(ComboViews.History);
2048 break;
2049
2050 case R.id.snapshots_menu_id:
2051 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002052 break;
2053
Pankaj Garg49b79252014-11-07 17:33:41 -08002054 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002055 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002056 break;
2057
2058 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002059 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002060 stopLoading();
2061 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002062 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002063 getCurrentTopWebView().reload();
2064 }
2065 break;
2066
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002068 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 break;
2070
2071 case R.id.close_menu_id:
2072 // Close the subwindow if it exists.
2073 if (mTabControl.getCurrentSubWindow() != null) {
2074 dismissSubWindow(mTabControl.getCurrentTab());
2075 break;
2076 }
2077 closeCurrentTab();
2078 break;
2079
kaiyiza8b6dbb2013-07-29 18:11:22 +08002080 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002081 Object[] params = { new String("persist.debug.browsermonkeytest")};
2082 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002083 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002084 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002085 if (ret != null && ret.equals("enable"))
2086 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002087 if (BrowserConfig.getInstance(getContext())
2088 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2089 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002090 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 case R.id.homepage_menu_id:
2092 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002093 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002094 break;
2095
2096 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002097 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002098 break;
2099
2100 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002101 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002102 break;
2103
John Reck2bc80422011-06-30 15:11:49 -07002104 case R.id.save_snapshot_menu_id:
2105 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002106 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002107 createScreenshotAsync(
2108 source.getWebView(),
2109 getDesiredThumbnailWidth(mActivity),
2110 getDesiredThumbnailHeight(mActivity),
2111 new ValueCallback<Bitmap>() {
2112 @Override
2113 public void onReceiveValue(Bitmap bitmap) {
2114 new SaveSnapshotTask(source, bitmap).execute();
2115 }
2116 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002117 break;
2118
Michael Kolb8233fac2010-10-26 16:08:53 -07002119 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002120 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002121 break;
2122
John Recke1a03a32011-09-14 17:04:16 -07002123 case R.id.snapshot_go_live:
2124 goLive();
2125 return true;
2126
Michael Kolb8233fac2010-10-26 16:08:53 -07002127 case R.id.share_page_menu_id:
2128 Tab currentTab = mTabControl.getCurrentTab();
2129 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002130 return false;
2131 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002132 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 break;
2134
2135 case R.id.dump_nav_menu_id:
2136 getCurrentTopWebView().debugDump();
2137 break;
2138
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 case R.id.zoom_in_menu_id:
2140 getCurrentTopWebView().zoomIn();
2141 break;
2142
2143 case R.id.zoom_out_menu_id:
2144 getCurrentTopWebView().zoomOut();
2145 break;
2146
2147 case R.id.view_downloads_menu_id:
2148 viewDownloads();
2149 break;
2150
John Reck42229bc2011-08-19 13:26:43 -07002151 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002152 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002153 break;
2154
Michael Kolb8233fac2010-10-26 16:08:53 -07002155 case R.id.window_one_menu_id:
2156 case R.id.window_two_menu_id:
2157 case R.id.window_three_menu_id:
2158 case R.id.window_four_menu_id:
2159 case R.id.window_five_menu_id:
2160 case R.id.window_six_menu_id:
2161 case R.id.window_seven_menu_id:
2162 case R.id.window_eight_menu_id:
2163 {
2164 int menuid = item.getItemId();
2165 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2166 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2167 Tab desiredTab = mTabControl.getTab(id);
2168 if (desiredTab != null &&
2169 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002170 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002171 }
2172 break;
2173 }
2174 }
2175 }
2176 break;
2177
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002178 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002179 Bundle bundle = new Bundle();
2180 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2181 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2182 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2183 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2184 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002185 break;
2186
Pankaj Garg49b79252014-11-07 17:33:41 -08002187 case R.id.add_to_homescreen:
2188 final WebView w = getCurrentTopWebView();
2189 final EditText input = new EditText(getContext());
2190 input.setText(w.getTitle());
2191 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002192 .setTitle(getContext().getResources().getString(
2193 R.string.add_to_homescreen))
2194 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002195 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002196 .setPositiveButton(getContext().getResources().getString(
2197 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002198 public void onClick(DialogInterface dialog, int whichButton) {
2199 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2200 getContext(),
2201 w.getUrl(),
2202 input.getText().toString(),
2203 w.getViewportBitmap(),
2204 w.getFavicon()));
2205
2206 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2207 .addCategory(Intent.CATEGORY_HOME));
2208 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002209 .setNegativeButton(getContext().getResources().getString(
2210 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002211 public void onClick(DialogInterface dialog, int whichButton) {
2212 // Do nothing.
2213 }
2214 })
2215 .show();
2216 break;
2217
Michael Kolb8233fac2010-10-26 16:08:53 -07002218 default:
2219 return false;
2220 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002221 return true;
2222 }
2223
John Reck68234a92012-04-19 15:27:12 -07002224 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2225 implements OnCancelListener {
2226
2227 private Tab mTab;
2228 private Dialog mProgressDialog;
2229 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002230 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002231
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002232 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002233 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002234 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002235 }
2236
2237 @Override
2238 protected void onPreExecute() {
2239 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2240 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2241 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002242 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002243 }
2244
2245 @Override
2246 protected Long doInBackground(Void... params) {
2247 if (!mTab.saveViewState(mValues)) {
2248 return null;
2249 }
2250 if (isCancelled()) {
2251 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2252 File file = mActivity.getFileStreamPath(path);
2253 if (!file.delete()) {
2254 file.deleteOnExit();
2255 }
2256 return null;
2257 }
2258 final ContentResolver cr = mActivity.getContentResolver();
2259 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2260 if (result == null) {
2261 return null;
2262 }
2263 long id = ContentUris.parseId(result);
2264 return id;
2265 }
2266
2267 @Override
2268 protected void onPostExecute(Long id) {
2269 if (isCancelled()) {
2270 return;
2271 }
2272 mProgressDialog.dismiss();
2273 if (id == null) {
2274 Toast.makeText(mActivity, R.string.snapshot_failed,
2275 Toast.LENGTH_SHORT).show();
2276 return;
2277 }
2278 Bundle b = new Bundle();
2279 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2280 mUi.showComboView(ComboViews.Snapshots, b);
2281 }
2282
2283 @Override
2284 public void onCancel(DialogInterface dialog) {
2285 cancel(true);
2286 }
2287 }
2288
Michael Kolb80f75082012-04-10 10:50:06 -07002289 @Override
2290 public void toggleUserAgent() {
2291 WebView web = getCurrentWebView();
2292 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002293 }
2294
2295 @Override
2296 public void findOnPage() {
2297 getCurrentTopWebView().showFindDialog(null, true);
2298 }
2299
2300 @Override
2301 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002302 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002303 }
2304
2305 @Override
2306 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002307 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002308 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2309 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002310 }
2311 else {
2312 WebView w = getCurrentTopWebView();
2313 if (w == null)
2314 return;
2315 final Intent i = createBookmarkCurrentPageIntent(false);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002316 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002317 }
Michael Kolb80f75082012-04-10 10:50:06 -07002318 }
2319
John Recke1a03a32011-09-14 17:04:16 -07002320 private void goLive() {
Axesh R. Ajmera89cf4d82015-05-19 11:26:18 -07002321 if (!getCurrentTab().isSnapshot()) return;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002322 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002323 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002324 boolean onlySingleTabRemaining = false;
2325 if (mTabControl.getTabCount() > 1) {
2326 // destroy the old snapshot tab
2327 closeCurrentTab();
2328 } else {
2329 onlySingleTabRemaining = true;
2330 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002331 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002332 if (onlySingleTabRemaining) {
2333 closeTab(t);
2334 }
2335
Tarun Nainani8eb00912014-07-17 12:28:32 -07002336 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002337 }
2338
luxiaolb40014b2013-07-19 10:01:43 +08002339 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002340 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002341 new AlertDialog.Builder(activity)
2342 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002343 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002344 .setMessage(R.string.exit_browser_msg)
2345 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2346 public void onClick(DialogInterface dialog, int which) {
2347 activity.moveTaskToBack(true);
2348 dialog.dismiss();
2349 }
2350 })
2351 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2352 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002353 mCrashRecoveryHandler.clearState(true);
2354 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002355 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002356 dialog.dismiss();
2357 }
2358 })
2359 .show();
2360 }
Michael Kolb315d5022011-10-13 12:47:11 -07002361 @Override
2362 public void showPageInfo() {
2363 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2364 }
2365
John Reck9c35b9c2012-05-30 10:08:50 -07002366 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002367 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002368 // Let the History and Bookmark fragments handle menus they created.
2369 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2370 return false;
2371 }
2372
Michael Kolb8233fac2010-10-26 16:08:53 -07002373 int id = item.getItemId();
2374 boolean result = true;
2375 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002376 // -- Browser context menu
2377 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002378 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002379 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002380 case R.id.copy_link_context_menu_id:
2381 final WebView webView = getCurrentTopWebView();
2382 if (null == webView) {
2383 result = false;
2384 break;
2385 }
2386 final HashMap<String, WebView> hrefMap =
2387 new HashMap<String, WebView>();
2388 hrefMap.put("webview", webView);
2389 final Message msg = mHandler.obtainMessage(
2390 FOCUS_NODE_HREF, id, 0, hrefMap);
2391 webView.requestFocusNodeHref(msg);
2392 break;
2393
2394 default:
2395 // For other context menus
2396 result = onOptionsItemSelected(item);
2397 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002398 return result;
2399 }
2400
2401 /**
2402 * support programmatically opening the context menu
2403 */
2404 public void openContextMenu(View view) {
2405 mActivity.openContextMenu(view);
2406 }
2407
2408 /**
2409 * programmatically open the options menu
2410 */
2411 public void openOptionsMenu() {
2412 mActivity.openOptionsMenu();
2413 }
2414
John Reck9c35b9c2012-05-30 10:08:50 -07002415 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002416 public boolean onMenuOpened(int featureId, Menu menu) {
2417 if (mOptionsMenuOpen) {
2418 if (mConfigChanged) {
2419 // We do not need to make any changes to the state of the
2420 // title bar, since the only thing that happened was a
2421 // change in orientation
2422 mConfigChanged = false;
2423 } else {
2424 if (!mExtendedMenuOpen) {
2425 mExtendedMenuOpen = true;
2426 mUi.onExtendedMenuOpened();
2427 } else {
2428 // Switching the menu back to icon view, so show the
2429 // title bar once again.
2430 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002431 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002432 }
2433 }
2434 } else {
2435 // The options menu is closed, so open it, and show the title
2436 mOptionsMenuOpen = true;
2437 mConfigChanged = false;
2438 mExtendedMenuOpen = false;
2439 mUi.onOptionsMenuOpened();
2440 }
2441 return true;
2442 }
2443
John Reck9c35b9c2012-05-30 10:08:50 -07002444 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002445 public void onOptionsMenuClosed(Menu menu) {
2446 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002447 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002448 }
2449
John Reck9c35b9c2012-05-30 10:08:50 -07002450 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002451 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002452 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002453 }
2454
2455 // Helper method for getting the top window.
2456 @Override
2457 public WebView getCurrentTopWebView() {
2458 return mTabControl.getCurrentTopWebView();
2459 }
2460
2461 @Override
2462 public WebView getCurrentWebView() {
2463 return mTabControl.getCurrentWebView();
2464 }
2465
2466 /*
2467 * This method is called as a result of the user selecting the options
2468 * menu to see the download window. It shows the download window on top of
2469 * the current window.
2470 */
2471 void viewDownloads() {
2472 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2473 mActivity.startActivity(intent);
2474 }
2475
John Reck30b065e2011-07-19 10:58:05 -07002476 int getActionModeHeight() {
2477 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2478 new int[] { android.R.attr.actionBarSize });
2479 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2480 actionBarSizeTypedArray.recycle();
2481 return size;
2482 }
2483
Michael Kolb8233fac2010-10-26 16:08:53 -07002484 // action mode
2485
John Reck9c35b9c2012-05-30 10:08:50 -07002486 @Override
2487 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002488 mUi.onActionModeStarted(mode);
2489 mActionMode = mode;
2490 }
2491
2492 /*
2493 * True if a custom ActionMode (i.e. find or select) is in use.
2494 */
2495 @Override
2496 public boolean isInCustomActionMode() {
2497 return mActionMode != null;
2498 }
2499
2500 /*
2501 * End the current ActionMode.
2502 */
2503 @Override
2504 public void endActionMode() {
2505 if (mActionMode != null) {
2506 mActionMode.finish();
2507 }
2508 }
2509
2510 /*
2511 * Called by find and select when they are finished. Replace title bars
2512 * as necessary.
2513 */
John Reck9c35b9c2012-05-30 10:08:50 -07002514 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002515 public void onActionModeFinished(ActionMode mode) {
2516 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002517 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002518 mActionMode = null;
2519 }
2520
2521 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002522 final Tab tab = getCurrentTab();
2523 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002524 }
2525
2526 // bookmark handling
2527
2528 /**
2529 * add the current page as a bookmark to the given folder id
2530 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002531 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002532 * bookmarked, and if it is, edit that bookmark. If false, and
2533 * the site is already bookmarked, do not attempt to edit the
2534 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002535 */
2536 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002537 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002538 WebView w = getCurrentTopWebView();
2539 if (w == null) {
2540 return null;
2541 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002542 Intent i = new Intent(mActivity,
2543 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002544 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2545 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2546 String touchIconUrl = w.getTouchIconUrl();
2547 if (touchIconUrl != null) {
2548 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2549 WebSettings settings = w.getSettings();
2550 if (settings != null) {
2551 i.putExtra(AddBookmarkPage.USER_AGENT,
2552 settings.getUserAgentString());
2553 }
2554 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002555 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002556 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002557 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002558 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2559 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002560 // Put the dialog at the upper right of the screen, covering the
2561 // star on the title bar.
2562 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002563 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002564 }
2565
2566 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002567 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002568 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002569 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002570 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002571 }
2572
Vivek Sekharb54614f2014-05-01 19:03:37 -07002573 @Override
2574 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2575 boolean capture) {
2576 mUploadHandler = new UploadHandler(this);
2577 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2578 }
2579
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 // thumbnails
2581
2582 /**
2583 * Return the desired width for thumbnail screenshots, which are stored in
2584 * the database, and used on the bookmarks screen.
2585 * @param context Context for finding out the density of the screen.
2586 * @return desired width for thumbnail screenshot.
2587 */
2588 static int getDesiredThumbnailWidth(Context context) {
2589 return context.getResources().getDimensionPixelOffset(
2590 R.dimen.bookmarkThumbnailWidth);
2591 }
2592
2593 /**
2594 * Return the desired height for thumbnail screenshots, which are stored in
2595 * the database, and used on the bookmarks screen.
2596 * @param context Context for finding out the density of the screen.
2597 * @return desired height for thumbnail screenshot.
2598 */
2599 static int getDesiredThumbnailHeight(Context context) {
2600 return context.getResources().getDimensionPixelOffset(
2601 R.dimen.bookmarkThumbnailHeight);
2602 }
2603
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002604 static void createScreenshotAsync(WebView view, int width, int height,
2605 final ValueCallback<Bitmap> cb) {
2606 if (view == null || width == 0 || height == 0) {
2607 return;
2608 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002609 view.getContentBitmapAsync(
2610 (float) width / view.getWidth(),
2611 new Rect(),
2612 new ValueCallback<Bitmap>() {
2613 @Override
2614 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002615 if (bitmap != null)
2616 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2617 cb.onReceiveValue(bitmap);
2618 }});
2619 }
2620
Michael Kolb8233fac2010-10-26 16:08:53 -07002621 private class Copy implements OnMenuItemClickListener {
2622 private CharSequence mText;
2623
John Reck9c35b9c2012-05-30 10:08:50 -07002624 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002625 public boolean onMenuItemClick(MenuItem item) {
2626 copy(mText);
2627 return true;
2628 }
2629
2630 public Copy(CharSequence toCopy) {
2631 mText = toCopy;
2632 }
2633 }
2634
Leon Scroggins63c02662010-11-18 15:16:27 -05002635 private static class Download implements OnMenuItemClickListener {
2636 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002637 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002638 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002639 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002640 private static final String FALLBACK_EXTENSION = "dat";
2641 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002642
John Reck9c35b9c2012-05-30 10:08:50 -07002643 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002644 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002645 if (DataUri.isDataUri(mText)) {
2646 saveDataUri();
2647 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002648 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002649 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002650 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002651 return true;
2652 }
2653
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002654 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2655 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002656 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002657 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002658 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002659 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002660 }
George Mount387d45d2011-10-07 15:57:53 -07002661
2662 /**
2663 * Treats mText as a data URI and writes its contents to a file
2664 * based on the current time.
2665 */
2666 private void saveDataUri() {
2667 FileOutputStream outputStream = null;
2668 try {
2669 DataUri uri = new DataUri(mText);
2670 File target = getTarget(uri);
2671 outputStream = new FileOutputStream(target);
2672 outputStream.write(uri.getData());
2673 final DownloadManager manager =
2674 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2675 manager.addCompletedDownload(target.getName(),
2676 mActivity.getTitle().toString(), false,
2677 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002678 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002679 } catch (IOException e) {
2680 Log.e(LOGTAG, "Could not save data URL");
2681 } finally {
2682 if (outputStream != null) {
2683 try {
2684 outputStream.close();
2685 } catch (IOException e) {
2686 // ignore close errors
2687 }
2688 }
2689 }
2690 }
2691
2692 /**
2693 * Creates a File based on the current time stamp and uses
2694 * the mime type of the DataUri to get the extension.
2695 */
2696 private File getTarget(DataUri uri) throws IOException {
2697 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002698 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002699 String nameBase = format.format(new Date());
2700 String mimeType = uri.getMimeType();
2701 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2702 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2703 if (extension == null) {
2704 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2705 extension = FALLBACK_EXTENSION;
2706 }
2707 extension = "." + extension; // createTempFile needs the '.'
2708 File targetFile = File.createTempFile(nameBase, extension, dir);
2709 return targetFile;
2710 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002711 }
2712
Cary Clark8974d282010-11-22 10:46:05 -05002713 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002714 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002715
John Reck9c35b9c2012-05-30 10:08:50 -07002716 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002717 public boolean onMenuItemClick(MenuItem item) {
2718 if (mWebView != null) {
2719 return mWebView.selectText();
2720 }
2721 return false;
2722 }
2723
2724 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002725 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002726 }
2727
2728 }
2729
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 /********************** TODO: UI stuff *****************************/
2731
2732 // these methods have been copied, they still need to be cleaned up
2733
2734 /****************** tabs ***************************************************/
2735
2736 // basic tab interactions:
2737
2738 // it is assumed that tabcontrol already knows about the tab
2739 protected void addTab(Tab tab) {
2740 mUi.addTab(tab);
2741 }
2742
2743 protected void removeTab(Tab tab) {
2744 mUi.removeTab(tab);
2745 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002746 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002747 }
2748
Michael Kolbf2055602011-04-09 17:20:03 -07002749 @Override
2750 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002751 // monkey protection against delayed start
2752 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002753
2754 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2755 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002756 mTabControl.setCurrentTab(tab);
2757 // the tab is guaranteed to have a webview after setCurrentTab
2758 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002759
2760
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002761 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002762 //Purge active tabs
2763 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002764 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002765 }
2766
John Reck8bcafc12011-08-29 16:43:02 -07002767 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002768 Tab current = mTabControl.getCurrentTab();
2769 if (current != null
2770 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002771 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002772 }
2773 }
2774
John Reck26b18322011-06-21 13:08:58 -07002775 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002776 //Cancel navscreen request
2777 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002778 // Dismiss the subwindow if applicable.
2779 dismissSubWindow(appTab);
2780 // Since we might kill the WebView, remove it from the
2781 // content view first.
2782 mUi.detachTab(appTab);
2783 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002784 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002785 // TODO: analyze why the remove and add are necessary
2786 mUi.attachTab(appTab);
2787 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002788 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002789 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002790 } else {
2791 // If the tab was the current tab, we have to attach
2792 // it to the view system again.
2793 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002794 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002795 }
2796 }
2797
2798 // Remove the sub window if it exists. Also called by TabControl when the
2799 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002800 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002801 public void dismissSubWindow(Tab tab) {
2802 removeSubWindow(tab);
2803 // dismiss the subwindow. This will destroy the WebView.
2804 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002805 WebView wv = getCurrentTopWebView();
2806 if (wv != null) {
2807 wv.requestFocus();
2808 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002809 }
2810
2811 @Override
2812 public void removeSubWindow(Tab t) {
2813 if (t.getSubWebView() != null) {
2814 mUi.removeSubWindow(t.getSubViewContainer());
2815 }
2816 }
2817
2818 @Override
2819 public void attachSubWindow(Tab tab) {
2820 if (tab.getSubWebView() != null) {
2821 mUi.attachSubWindow(tab.getSubViewContainer());
2822 getCurrentTopWebView().requestFocus();
2823 }
2824 }
2825
Mathew Inwood29721c22011-06-29 17:55:24 +01002826 private Tab showPreloadedTab(final UrlData urlData) {
2827 if (!urlData.isPreloaded()) {
2828 return null;
2829 }
2830 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2831 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2832 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002833 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002834 // Could not submit query. Fallback to regular tab creation
2835 tabControl.destroy();
2836 return null;
2837 }
2838 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002839 // check tab count and make room for new tab
2840 if (!mTabControl.canCreateNewTab()) {
2841 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2842 if (leastUsed != null) {
2843 closeTab(leastUsed);
2844 }
2845 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002846 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002847 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002848 mTabControl.addPreloadedTab(t);
2849 addTab(t);
2850 setActiveTab(t);
2851 return t;
2852 }
2853
Michael Kolb7bcafde2011-05-09 13:55:59 -07002854 // open a non inconito tab with the given url data
2855 // and set as active tab
2856 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002857 Tab tab = showPreloadedTab(urlData);
2858 if (tab == null) {
2859 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002860 if ((tab != null) && !urlData.isEmpty()) {
2861 loadUrlDataIn(tab, urlData);
2862 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002863 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002864 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002865 }
2866
Michael Kolb843510f2010-12-09 10:51:49 -08002867 @Override
2868 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002869 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002870 }
2871
Michael Kolb8233fac2010-10-26 16:08:53 -07002872 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002873 public Tab openIncognitoTab() {
2874 return openTab(INCOGNITO_URI, true, true, false);
2875 }
2876
2877 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002878 public Tab openTab(String url, boolean incognito, boolean setActive,
2879 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002880 return openTab(url, incognito, setActive, useCurrent, null);
2881 }
2882
2883 @Override
2884 public Tab openTab(String url, Tab parent, boolean setActive,
2885 boolean useCurrent) {
2886 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2887 setActive, useCurrent, parent);
2888 }
2889
2890 public Tab openTab(String url, boolean incognito, boolean setActive,
2891 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002892 Tab tab = createNewTab(incognito, setActive, useCurrent);
2893 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002894 if (parent instanceof SnapshotTab) {
2895 addTab(tab);
2896 if (setActive)
2897 setActiveTab(tab);
2898 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002899 parent.addChildTab(tab);
2900 }
Michael Kolb519d2282011-05-09 17:03:19 -07002901 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002902 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002903 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002904 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002905 return tab;
2906 }
2907
2908 // this method will attempt to create a new tab
2909 // incognito: private browsing tab
2910 // setActive: ste tab as current tab
2911 // useCurrent: if no new tab can be created, return current tab
2912 private Tab createNewTab(boolean incognito, boolean setActive,
2913 boolean useCurrent) {
2914 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002915 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2916 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002917 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002918 if (mTabControl.canCreateNewTab()) {
2919 tab = mTabControl.createNewTab(incognito, !setActive);
2920 addTab(tab);
2921 if (setActive) {
2922 setActiveTab(tab);
2923 } else {
2924 tab.pause();
2925 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002926 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002927 if (useCurrent) {
2928 tab = mTabControl.getCurrentTab();
2929 reuseTab(tab, null);
2930 } else {
2931 mUi.showMaxTabsWarning();
2932 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002933 }
2934 }
2935 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002936 }
2937
John Reck2bc80422011-06-30 15:11:49 -07002938 @Override
2939 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2940 SnapshotTab tab = null;
2941 if (mTabControl.canCreateNewTab()) {
2942 tab = mTabControl.createSnapshotTab(snapshotId);
2943 addTab(tab);
2944 if (setActive) {
2945 setActiveTab(tab);
2946 }
2947 } else {
2948 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002949 }
2950 return tab;
2951 }
2952
Michael Kolb8233fac2010-10-26 16:08:53 -07002953 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002954 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002955 * @return boolean True if we successfully switched to a different tab. If
2956 * the indexth tab is null, or if that tab is the same as
2957 * the current one, return false.
2958 */
2959 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002960 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002961 Tab currentTab = mTabControl.getCurrentTab();
2962 if (tab == null || tab == currentTab) {
2963 return false;
2964 }
2965 setActiveTab(tab);
2966 return true;
2967 }
2968
2969 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002970 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002971 closeCurrentTab(false);
2972 }
2973
2974 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002975 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002976 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002977 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002978 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002979 return;
2980 }
Michael Kolbc831b632011-05-11 09:30:34 -07002981 final Tab current = mTabControl.getCurrentTab();
2982 final int pos = mTabControl.getCurrentPosition();
2983 Tab newTab = current.getParent();
2984 if (newTab == null) {
2985 newTab = mTabControl.getTab(pos + 1);
2986 if (newTab == null) {
2987 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002988 }
2989 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002990 if (andQuit) {
2991 mTabControl.setCurrentTab(newTab);
2992 closeTab(current);
2993 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002994 // Close window
2995 closeTab(current);
2996 }
2997 }
2998
2999 /**
3000 * Close the tab, remove its associated title bar, and adjust mTabControl's
3001 * current tab to a valid value.
3002 */
3003 @Override
3004 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003005 if (tab == mTabControl.getCurrentTab()) {
3006 closeCurrentTab();
3007 } else {
3008 removeTab(tab);
3009 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003010 }
3011
Afzal Najamd4e33312012-04-26 01:54:01 -04003012 /**
3013 * Close all tabs except the current one
3014 */
3015 @Override
3016 public void closeOtherTabs() {
3017 int inactiveTabs = mTabControl.getTabCount() - 1;
3018 for (int i = inactiveTabs; i >= 0; i--) {
3019 Tab tab = mTabControl.getTab(i);
3020 if (tab != mTabControl.getCurrentTab()) {
3021 removeTab(tab);
3022 }
3023 }
3024 }
3025
Michael Kolb8233fac2010-10-26 16:08:53 -07003026 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003027 protected void loadUrlFromContext(String url) {
3028 Tab tab = getCurrentTab();
3029 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003030 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003031 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003032 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003033 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003034 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003035 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003036 }
3037 }
3038 }
3039
3040 /**
3041 * Load the URL into the given WebView and update the title bar
3042 * to reflect the new load. Call this instead of WebView.loadUrl
3043 * directly.
3044 * @param view The WebView used to load url.
3045 * @param url The URL to load.
3046 */
John Reck71e51422011-07-01 16:49:28 -07003047 @Override
3048 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003049 loadUrl(tab, url, null);
3050 }
3051
3052 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3053 if (tab != null) {
3054 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003055 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003056 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003057 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003058 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003059 }
3060
3061 /**
3062 * Load UrlData into a Tab and update the title bar to reflect the new
3063 * load. Call this instead of UrlData.loadIn directly.
3064 * @param t The Tab used to load.
3065 * @param data The UrlData being loaded.
3066 */
3067 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003068 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003069 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003070 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003071 } else {
John Reck38b39652012-06-05 09:22:59 -07003072 if (t != null && data.mDisableUrlOverride) {
3073 t.disableUrlOverridingForLoad();
3074 }
John Reck26b18322011-06-21 13:08:58 -07003075 loadUrl(t, data.mUrl, data.mHeaders);
3076 }
3077 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003078 }
3079
John Reck30c714c2010-12-16 17:30:34 -08003080 @Override
3081 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003082 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003083 //In case of tab can go back (aka tab has navigation entry) do nothing
3084 //else just load homepage in current tab.
3085 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003086 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003087 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003088 }
3089
3090 void goBackOnePageOrQuit() {
3091 Tab current = mTabControl.getCurrentTab();
3092 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003093 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3094 showExitDialog(mActivity);
3095 } else {
3096 /*
3097 * Instead of finishing the activity, simply push this to the back
3098 * of the stack and let ActivityManager to choose the foreground
3099 * activity. As BrowserActivity is singleTask, it will be always the
3100 * root of the task. So we can use either true or false for
3101 * moveTaskToBack().
3102 */
3103 mActivity.moveTaskToBack(true);
3104 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003105 return;
3106 }
John Reckef654f12011-07-12 16:42:08 -07003107 if (current.canGoBack()) {
3108 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003109 } else {
3110 // Check to see if we are closing a window that was created by
3111 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003112 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003113 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003114 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003115 // Now we close the other tab
3116 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003117 } else if (BrowserConfig.getInstance(getContext())
3118 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3119 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003120 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003121 /*
3122 * Instead of finishing the activity, simply push this to the back
3123 * of the stack and let ActivityManager to choose the foreground
3124 * activity. As BrowserActivity is singleTask, it will be always the
3125 * root of the task. So we can use either true or false for
3126 * moveTaskToBack().
3127 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003128 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003129 }
3130 }
3131 }
3132
3133 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003134 * helper method for key handler
3135 * returns the current tab if it can't advance
3136 */
Michael Kolbc831b632011-05-11 09:30:34 -07003137 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003138 int pos = mTabControl.getCurrentPosition() + 1;
3139 if (pos >= mTabControl.getTabCount()) {
3140 pos = 0;
3141 }
3142 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003143 }
3144
3145 /**
3146 * helper method for key handler
3147 * returns the current tab if it can't advance
3148 */
Michael Kolbc831b632011-05-11 09:30:34 -07003149 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003150 int pos = mTabControl.getCurrentPosition() - 1;
3151 if ( pos < 0) {
3152 pos = mTabControl.getTabCount() - 1;
3153 }
3154 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003155 }
3156
John Reckbcef87f2012-02-03 14:58:34 -08003157 boolean isMenuOrCtrlKey(int keyCode) {
3158 return (KeyEvent.KEYCODE_MENU == keyCode)
3159 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3160 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3161 }
3162
Michael Kolb0035fad2011-03-14 13:25:28 -07003163 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003164 * handle key events in browser
3165 *
3166 * @param keyCode
3167 * @param event
3168 * @return true if handled, false to pass to super
3169 */
John Reck9c35b9c2012-05-30 10:08:50 -07003170 @Override
3171 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003172 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3173 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003174 if (!mUi.isComboViewShowing()) {
3175 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3176 true, false);
3177 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003178 return true;
3179 }
3180
Cary Clark160bbb92011-01-10 11:17:07 -05003181 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003182 // Even if MENU is already held down, we need to call to super to open
3183 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003184 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003185 mMenuIsDown = true;
3186 return false;
3187 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003188
Cary Clark8ff8c662010-12-29 15:03:05 -05003189 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003190 Tab tab = getCurrentTab();
3191 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003192
Cary Clark160bbb92011-01-10 11:17:07 -05003193 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3194 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003195
Michael Kolb8233fac2010-10-26 16:08:53 -07003196 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003197 case KeyEvent.KEYCODE_TAB:
3198 if (event.isCtrlPressed()) {
3199 if (event.isShiftPressed()) {
3200 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003201 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003202 } else {
3203 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003204 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003205 }
3206 return true;
3207 }
3208 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003209 case KeyEvent.KEYCODE_SPACE:
3210 // WebView/WebTextView handle the keys in the KeyDown. As
3211 // the Activity's shortcut keys are only handled when WebView
3212 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003213 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003214 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003215 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003216 pageDown();
3217 }
3218 return true;
3219 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003220 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003221 event.startTracking();
3222 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003223 case KeyEvent.KEYCODE_FORWARD:
3224 if (!noModifiers) break;
3225 tab.goForward();
3226 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003227 case KeyEvent.KEYCODE_DPAD_LEFT:
3228 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003229 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003230 return true;
3231 }
3232 break;
3233 case KeyEvent.KEYCODE_DPAD_RIGHT:
3234 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003235 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003236 return true;
3237 }
3238 break;
3239 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003240 if (ctrl) {
3241 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003242 return true;
3243 }
3244 break;
Michael Kolba4183062011-01-16 10:43:21 -08003245// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003246 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003247 if (ctrl ) {
3248 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003249 return true;
3250 }
3251 break;
Michael Kolba4183062011-01-16 10:43:21 -08003252// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003253// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003254// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003255// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003256// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003257// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003258// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003259// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003260// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003261// case KeyEvent.KEYCODE_M: // unused
3262// case KeyEvent.KEYCODE_N: // in Chrome: new window
3263// case KeyEvent.KEYCODE_O: // in Chrome: open file
3264// case KeyEvent.KEYCODE_P: // in Chrome: print page
3265// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003266// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003267// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3268 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003269 // we can't use the ctrl/shift flags, they check for
3270 // exclusive use of a modifier
3271 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003272 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003273 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003274 } else {
3275 openTabToHomePage();
3276 }
3277 return true;
3278 }
3279 break;
3280// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3281// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003282// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003283// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3284// case KeyEvent.KEYCODE_Y: // unused
3285// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003286 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003287 // it is a regular key and webview is not null
3288 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003289 }
3290
John Reck9c35b9c2012-05-30 10:08:50 -07003291 @Override
3292 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003293 switch(keyCode) {
3294 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003295 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003296 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003297 return true;
3298 }
3299 break;
3300 }
3301 return false;
3302 }
3303
John Reck9c35b9c2012-05-30 10:08:50 -07003304 @Override
3305 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003306 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003307 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003308 if (KeyEvent.KEYCODE_MENU == keyCode
3309 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003310 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003311 }
3312 }
Cary Clark160bbb92011-01-10 11:17:07 -05003313 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003314 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003315 case KeyEvent.KEYCODE_BACK:
3316 if (event.isTracking() && !event.isCanceled()) {
3317 onBackKey();
3318 return true;
3319 }
3320 break;
3321 }
3322 return false;
3323 }
3324
3325 public boolean isMenuDown() {
3326 return mMenuIsDown;
3327 }
3328
John Reck9c35b9c2012-05-30 10:08:50 -07003329 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003330 public void setupAutoFill(Message message) {
3331 // Open the settings activity at the AutoFill profile fragment so that
3332 // the user can create a new profile. When they return, we will dispatch
3333 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003334 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003335 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3336 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003337 }
John Reckb3417f02011-01-14 11:01:05 -08003338
John Reck9c35b9c2012-05-30 10:08:50 -07003339 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003340 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003341 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003342 return true;
3343 }
3344
John Reck1cf4b792011-07-26 10:22:22 -07003345 @Override
3346 public boolean shouldCaptureThumbnails() {
3347 return mUi.shouldCaptureThumbnails();
3348 }
3349
Michael Kolbc3af0672011-08-09 10:24:41 -07003350 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003351 public boolean supportsVoice() {
3352 PackageManager pm = mActivity.getPackageManager();
3353 List activities = pm.queryIntentActivities(new Intent(
3354 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3355 return activities.size() != 0;
3356 }
3357
3358 @Override
3359 public void startVoiceRecognizer() {
3360 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003361 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003362 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3363 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3364 mActivity.startActivityForResult(voice, VOICE_RESULT);
3365 }
3366
Pankaj Garg7b279f62014-08-12 14:47:18 -07003367 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003368 if (mLevel == level)
3369 return;
3370 mLevel = level;
3371 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003372 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3373 lp.dimAmount = level;
3374 mActivity.getWindow().setAttributes(lp);
3375 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3376 } else {
3377 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3378 }
3379 }
3380
Michael Kolb0b129122012-06-04 16:31:58 -07003381 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003382 public void setBlockEvents(boolean block) {
3383 mBlockEvents = block;
3384 }
3385
John Reck9c35b9c2012-05-30 10:08:50 -07003386 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003387 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003388 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003389 }
3390
John Reck9c35b9c2012-05-30 10:08:50 -07003391 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003392 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003393 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003394 }
3395
John Reck9c35b9c2012-05-30 10:08:50 -07003396 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003397 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003398 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003399 }
3400
John Reck9c35b9c2012-05-30 10:08:50 -07003401 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003402 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003403 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003404 }
3405
John Reck9c35b9c2012-05-30 10:08:50 -07003406 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003407 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003408 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003409 }
3410
Pankaj Garg49b79252014-11-07 17:33:41 -08003411 @Override
3412 public boolean shouldShowAppMenu() {
3413 return true;
3414 }
3415
3416 @Override
3417 public int getMenuThemeResourceId() {
3418 return R.style.OverflowMenuTheme;
3419 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003420}