blob: 63372370ab37ec10e4eb8c0f491e58a32b364b6d [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
Pankaj Garg634bf432015-07-13 09:54:21 -0700264 private boolean mCurrentPageBookmarked;
265
George Mount3636d0a2011-11-21 09:08:21 -0800266 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 mActivity = browser;
268 mSettings = BrowserSettings.getInstance();
269 mTabControl = new TabControl(this);
270 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700271 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800272 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700273 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700274
275 mUrlHandler = new UrlHandler(this);
276 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
278
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500280 mBookmarksObserver = new ContentObserver(mHandler) {
281 @Override
282 public void onChange(boolean selfChange) {
283 int size = mTabControl.getTabCount();
284 for (int i = 0; i < size; i++) {
285 mTabControl.getTab(i).updateBookmarkedStatus();
286 }
287 }
288
289 };
290 browser.getContentResolver().registerContentObserver(
291 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700292
293 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700294 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800295 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700296 }
297
John Reck9c35b9c2012-05-30 10:08:50 -0700298 @Override
299 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700300 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800301 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800302 // mCrashRecoverHandler has any previously saved state.
303 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700304 }
305
George Mount3636d0a2011-11-21 09:08:21 -0800306 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800307 // we dont want to ever recover incognito tabs
308 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700309
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700311 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500312 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000313
Michael Kolbc831b632011-05-11 09:30:34 -0700314 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500315 // Not able to restore so we go ahead and clear session cookies. We
316 // must do this before trying to login the user as we don't want to
317 // clear any session cookies set during login.
318 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700319 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800320 if (intent == null) {
321 // This won't happen under common scenarios. The icicle is
322 // not null, but there aren't any tabs to restore.
323 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700324 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800325 final Bundle extra = intent.getExtras();
326 // Create an initial tab.
327 // If the intent is ACTION_VIEW and data is not null, the Browser is
328 // invoked to view the content by another application. In this case,
329 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800330 UrlData urlData = null;
331 if (intent.getData() != null
332 && Intent.ACTION_VIEW.equals(intent.getAction())
333 && intent.getData().toString().startsWith("content://")) {
334 urlData = new UrlData(intent.getData().toString());
335 } else {
336 urlData = IntentHandler.getUrlDataFromIntent(intent);
337 }
George Mount3636d0a2011-11-21 09:08:21 -0800338 Tab t = null;
339 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700340 String landingPage = mActivity.getResources().getString(
341 R.string.def_landing_page);
342 if (!landingPage.isEmpty()) {
343 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800344 } else {
345 t = openTabToHomePage();
346 }
George Mount3636d0a2011-11-21 09:08:21 -0800347 } else {
348 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700349 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800350 }
351 if (t != null) {
352 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
353 }
354 WebView webView = t.getWebView();
355 if (extra != null) {
356 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
357 if (scale > 0 && scale <= 1000) {
358 webView.setInitialScale(scale);
359 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700360 }
361 }
John Reckd8c74522011-06-14 08:45:00 -0700362 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700364 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500365 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700366 List<Tab> tabs = mTabControl.getTabs();
367 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700368
John Reck1cf4b792011-07-26 10:22:22 -0700369 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700370 //handle restored pages that may require a JS interface
371 if (t.getWebView() != null) {
372 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
373 if (backForwardList != null) {
374 for (int i = 0; i < backForwardList.getSize(); i++) {
375 WebHistoryItem item = backForwardList.getItemAtIndex(i);
376 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
377 }
378 }
379 }
John Reck1cf4b792011-07-26 10:22:22 -0700380 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700381 if (t != mTabControl.getCurrentTab()) {
382 t.pause();
383 }
John Reck1cf4b792011-07-26 10:22:22 -0700384 }
385 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700386 if (tabs.size() == 0) {
387 openTabToHomePage();
388 }
John Reck1cf4b792011-07-26 10:22:22 -0700389 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700390 // TabControl.restoreState() will create a new tab even if
391 // restoring the state fails.
392 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800393 // Intent is non-null when framework thinks the browser should be
394 // launching with a new intent (icicle is null).
395 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700396 mIntentHandler.onNewIntent(intent);
397 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700398 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700399 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700400 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800401 if (jsFlags.trim().length() != 0) {
402 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700403 }
George Mount3636d0a2011-11-21 09:08:21 -0800404 if (intent != null
405 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700406 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800407 }
Site Maoabb7bd32015-03-20 15:34:02 -0700408 mLowPowerReceiver = new PowerConnectionReceiver();
409 mPowerChangeReceiver = new PowerConnectionReceiver();
410
411 //always track the android framework's power save mode
412 IntentFilter filter = new IntentFilter();
413 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
414 // Power save mode only exists in Lollipop and above
415 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
416 }
417 filter.addAction(Intent.ACTION_BATTERY_OKAY);
418 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700419 }
420
John Reck1cf4b792011-07-26 10:22:22 -0700421 private static class PruneThumbnails implements Runnable {
422 private Context mContext;
423 private List<Long> mIds;
424
425 PruneThumbnails(Context context, List<Long> preserveIds) {
426 mContext = context.getApplicationContext();
427 mIds = preserveIds;
428 }
429
430 @Override
431 public void run() {
432 ContentResolver cr = mContext.getContentResolver();
433 if (mIds == null || mIds.size() == 0) {
434 cr.delete(Thumbnails.CONTENT_URI, null, null);
435 } else {
436 int length = mIds.size();
437 StringBuilder where = new StringBuilder();
438 where.append(Thumbnails._ID);
439 where.append(" not in (");
440 for (int i = 0; i < length; i++) {
441 where.append(mIds.get(i));
442 if (i < (length - 1)) {
443 where.append(",");
444 }
445 }
446 where.append(")");
447 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
448 }
449 }
450
451 }
452
Michael Kolb1514bb72010-11-22 09:11:48 -0800453 @Override
454 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700455 return mFactory;
456 }
457
458 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800459 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700460 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700461 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800462 }
463
464 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800465 public void createSubWindow(Tab tab) {
466 endActionMode();
467 WebView mainView = tab.getWebView();
468 WebView subView = mFactory.createWebView((mainView == null)
469 ? false
470 : mainView.isPrivateBrowsingEnabled());
471 mUi.createSubWindow(tab, subView);
472 }
473
474 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700475 public Context getContext() {
476 return mActivity;
477 }
478
479 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700480 public Activity getActivity() {
481 return mActivity;
482 }
483
484 void setUi(UI ui) {
485 mUi = ui;
486 }
487
Michael Kolbaf63dba2012-05-16 12:58:05 -0700488 @Override
489 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700490 return mSettings;
491 }
492
493 IntentHandler getIntentHandler() {
494 return mIntentHandler;
495 }
496
497 @Override
498 public UI getUi() {
499 return mUi;
500 }
501
502 int getMaxTabs() {
503 return mActivity.getResources().getInteger(R.integer.max_tabs);
504 }
505
506 @Override
507 public TabControl getTabControl() {
508 return mTabControl;
509 }
510
Michael Kolb1bf23132010-11-19 12:55:12 -0800511 @Override
512 public List<Tab> getTabs() {
513 return mTabControl.getTabs();
514 }
515
Michael Kolb8233fac2010-10-26 16:08:53 -0700516 private void startHandler() {
517 mHandler = new Handler() {
518
519 @Override
520 public void handleMessage(Message msg) {
521 switch (msg.what) {
522 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700523 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700524 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800525 case UNKNOWN_TYPE_MSG:
526 HashMap unknownTypeMap = (HashMap) msg.obj;
527 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
528 /*
529 * When the context menu is shown to the user
530 * we need to assure that its happening on the current webview
531 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
532 */
533 if (getCurrentWebView() != viewForUnknownType)
534 break;
535
536 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800537 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800538 WebView.HitTestResult result = new WebView.HitTestResult();
539
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800540 // Prevent unnecessary calls to context menu
541 // if url and image src are null
542 if (unknown_type_src == null && unknown_type_url == null)
543 break;
544
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800545 //setting the HitTestResult with new RESULT TYPE
546 if (!TextUtils.isEmpty(unknown_type_src)) {
547 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
548 result.setExtra(unknown_type_src);
549 } else {
550 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
551 result.setExtra("about:blank");
552 }
553
554 mResult = result;
555 openContextMenu(viewForUnknownType);
556 mResult = null;
557
558 break;
559
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 case FOCUS_NODE_HREF:
561 {
562 String url = (String) msg.getData().get("url");
563 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500564 String src = (String) msg.getData().get("src");
565 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700566 if (TextUtils.isEmpty(url)) {
567 break;
568 }
569 HashMap focusNodeMap = (HashMap) msg.obj;
570 WebView view = (WebView) focusNodeMap.get("webview");
571 // Only apply the action if the top window did not change.
572 if (getCurrentTopWebView() != view) {
573 break;
574 }
575 switch (msg.arg1) {
576 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700577 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700578 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500579 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700580 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500581 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500582 case R.id.open_newtab_context_menu_id:
583 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700584 openTab(url, parent,
585 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500586 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700587 case R.id.copy_link_context_menu_id:
588 copy(url);
589 break;
590 case R.id.save_link_context_menu_id:
591 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500592 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800593 mActivity, url, view.getSettings().getUserAgentString(),
594 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700595 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700596 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700597 if(title == null || title == "")
598 title = url;
599
600 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
601 //SWE TODO: No thumbnail support for the url obtained via
602 //browser context menu as its not loaded in webview.
603 if (bookmarkIntent != null) {
604 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
605 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
606 mActivity.startActivity(bookmarkIntent);
607 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700608 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700609 }
610 break;
611 }
612
613 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700614 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700615 break;
616
617 case STOP_LOAD:
618 stopLoading();
619 break;
620
621 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700622 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700623 mWakeLock.release();
624 // if we reach here, Browser should be still in the
625 // background loading after WAKELOCK_TIMEOUT (5-min).
626 // To avoid burning the battery, stop loading.
627 mTabControl.stopAllLoading();
628 }
629 break;
630
kaiyiz591110b2013-08-06 17:11:06 +0800631 case OPEN_MENU:
632 if (!mOptionsMenuOpen && mActivity != null ) {
633 mActivity.openOptionsMenu();
634 }
635 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 }
637 }
638 };
639
640 }
641
John Reckef654f12011-07-12 16:42:08 -0700642 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200643 public Tab getCurrentTab() {
644 return mTabControl.getCurrentTab();
645 }
646
Michael Kolbba99c5d2010-11-29 14:57:41 -0800647 @Override
648 public void shareCurrentPage() {
649 shareCurrentPage(mTabControl.getCurrentTab());
650 }
651
652 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700653 if (tab == null || tab.getWebView() == null)
654 return;
655
656 final Tab mytab = tab;
657 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
658 @Override
659 public void onReceiveValue(Bitmap bitmap) {
660 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
661 mytab.getFavicon(), bitmap);
662 }
663 };
664
665 createScreenshotAsync(
666 tab.getWebView(),
667 getDesiredThumbnailWidth(mActivity),
668 getDesiredThumbnailHeight(mActivity),
669 new ValueCallback<Bitmap>() {
670 @Override
671 public void onReceiveValue(Bitmap bitmap) {
672 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
673 mytab.getFavicon(), bitmap);
674 }
675 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800676 }
677
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 /**
679 * Share a page, providing the title, url, favicon, and a screenshot. Uses
680 * an {@link Intent} to launch the Activity chooser.
681 * @param c Context used to launch a new Activity.
682 * @param title Title of the page. Stored in the Intent with
683 * {@link Intent#EXTRA_SUBJECT}
684 * @param url URL of the page. Stored in the Intent with
685 * {@link Intent#EXTRA_TEXT}
686 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
687 * with {@link Browser#EXTRA_SHARE_FAVICON}
688 * @param screenshot Bitmap of a screenshot of the page. Stored in the
689 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
690 */
691 static final void sharePage(Context c, String title, String url,
692 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700693
694 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
695 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
696 R.layout.app_row, sDialog.getApps());
697 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 }
699
700 private void copy(CharSequence text) {
701 ClipboardManager cm = (ClipboardManager) mActivity
702 .getSystemService(Context.CLIPBOARD_SERVICE);
703 cm.setText(text);
704 }
705
706 // lifecycle
707
John Reck9c35b9c2012-05-30 10:08:50 -0700708 @Override
709 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700710 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800711 // update the menu in case of a locale change
712 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800713 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800714 if (mOptionsMenuOpen) {
715 mActivity.closeOptionsMenu();
716 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
717 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700718 if (mPageDialogsHandler != null) {
719 mPageDialogsHandler.onConfigurationChanged(config);
720 }
721 mUi.onConfigurationChanged(config);
722 }
723
724 @Override
725 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700726 if (!mUi.isWebShowing()) {
727 mUi.showWeb(false);
728 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700729 mIntentHandler.onNewIntent(intent);
730 }
731
John Reck9c35b9c2012-05-30 10:08:50 -0700732 @Override
733 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800734 if (mUi.isCustomViewShowing()) {
735 hideCustomView();
736 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 if (mActivityPaused) {
738 Log.e(LOGTAG, "BrowserActivity is already paused.");
739 return;
740 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700741 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800742 Tab tab = mTabControl.getCurrentTab();
743 if (tab != null) {
744 tab.pause();
745 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700746 if (mWakeLock == null) {
747 PowerManager pm = (PowerManager) mActivity
748 .getSystemService(Context.POWER_SERVICE);
749 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
750 }
Michael Kolb70976932010-11-30 11:34:01 -0800751 mWakeLock.acquire();
752 mHandler.sendMessageDelayed(mHandler
753 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
754 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700755 }
756 mUi.onPause();
757 mNetworkHandler.onPause();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100758 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700759 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700760 }
761
John Reck9c35b9c2012-05-30 10:08:50 -0700762 @Override
763 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800765 Bundle saveState = createSaveState();
766
767 // crash recovery manages all save & restore state
768 mCrashRecoveryHandler.writeState(saveState);
769 mSettings.setLastRunPaused(true);
770 }
771
772 /**
773 * Save the current state to outState. Does not write the state to
774 * disk.
775 * @return Bundle containing the current state of all tabs.
776 */
777 /* package */ Bundle createSaveState() {
778 Bundle saveState = new Bundle();
779 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800780 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700781 }
782
John Reck9c35b9c2012-05-30 10:08:50 -0700783 @Override
784 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700785 if (!mActivityPaused) {
786 Log.e(LOGTAG, "BrowserActivity is already resumed.");
787 return;
788 }
George Mount3636d0a2011-11-21 09:08:21 -0800789 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700790 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800791 Tab current = mTabControl.getCurrentTab();
792 if (current != null) {
793 current.resume();
794 resumeWebViewTimers(current);
795 }
John Reckf57c0292011-07-21 18:15:39 -0700796 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200797
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 mUi.onResume();
799 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100800 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700801 if (mVoiceResult != null) {
802 mUi.onVoiceResult(mVoiceResult);
803 mVoiceResult = null;
804 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700805 if (current != null && current.getWebView().isShowingCrashView())
806 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700807 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700808 }
809
John Reckf57c0292011-07-21 18:15:39 -0700810 private void releaseWakeLock() {
811 if (mWakeLock != null && mWakeLock.isHeld()) {
812 mHandler.removeMessages(RELEASE_WAKELOCK);
813 mWakeLock.release();
814 }
815 }
816
Michael Kolb70976932010-11-30 11:34:01 -0800817 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800818 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800819 * @param tab guaranteed non-null
820 */
821 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700822 boolean inLoad = tab.inPageLoad();
823 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
824 CookieSyncManager.getInstance().startSync();
825 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100826 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700827 }
828 }
829
Michael Kolb70976932010-11-30 11:34:01 -0800830 /**
831 * Pause all WebView timers using the WebView of the given tab
832 * @param tab
833 * @return true if the timers are paused or tab is null
834 */
835 private boolean pauseWebViewTimers(Tab tab) {
836 if (tab == null) {
837 return true;
838 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100840 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 }
Michael Kolb70976932010-11-30 11:34:01 -0800843 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 }
845
John Reck9c35b9c2012-05-30 10:08:50 -0700846 @Override
847 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800848 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
850 mUploadHandler = null;
851 }
852 if (mTabControl == null) return;
853 mUi.onDestroy();
854 // Remove the current tab and sub window
855 Tab t = mTabControl.getCurrentTab();
856 if (t != null) {
857 dismissSubWindow(t);
858 removeTab(t);
859 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500860 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 // Destroy all the tabs
862 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700863 // Unregister receiver
864 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 }
866
867 protected boolean isActivityPaused() {
868 return mActivityPaused;
869 }
870
John Reck9c35b9c2012-05-30 10:08:50 -0700871 @Override
872 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700873 mTabControl.freeMemory();
874 }
875
876 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700877 public void stopLoading() {
878 mLoadStopped = true;
879 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700880 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700881 if (w != null) {
882 w.stopLoading();
883 mUi.onPageStopped(tab);
884 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 }
886
887 boolean didUserStopLoading() {
888 return mLoadStopped;
889 }
890
kaiyiza016da12013-08-26 17:50:22 +0800891 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700892 final String reminderType = getContext().getResources().getString(
893 R.string.def_wifi_browser_interaction_remind_type);
894 final String selectionConnnection = getContext().getResources().getString(
895 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700896 final String wifiSelection = getContext().getResources().getString(
897 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700898
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700899 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
900 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700901 return;
902
kaiyiza016da12013-08-26 17:50:22 +0800903 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700904 Context.CONNECTIVITY_SERVICE);
905 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700906 WifiManager wifiMgr = (WifiManager) this.getContext()
907 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700908 if (networkInfo == null
909 || (networkInfo != null && (networkInfo.getType() !=
910 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700911 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
912 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700913 List<ScanResult> list = wifiMgr.getScanResults();
914 // Have no AP's for Wifi's fall back to data
915 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700916 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700917 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
918 this.getContext().startActivity(intent);
919 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700920 // Request to select Wifi AP
921 try {
922 Intent intent = new Intent(wifiSelection);
923 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
924 this.getContext().startActivity(intent);
925 } catch (Exception e) {
926 String err_msg = this.getContext().getString(
927 R.string.acivity_not_found, wifiSelection);
928 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
929 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700930 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700931 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800932 }
933 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700934
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 // WebViewController
936
937 @Override
John Reck324d4402011-01-11 16:56:42 -0800938 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700939
Michael Kolb8233fac2010-10-26 16:08:53 -0700940 // reset sync timer to avoid sync starts during loading a page
941 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700942 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700943 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800944 boolean networkNotifier = BrowserConfig.getInstance(getContext())
945 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700946 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700947 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800948 } else {
949 if (!mNetworkHandler.isNetworkUp()) {
950 view.setNetworkAvailable(false);
951 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700952 }
953
954 // when BrowserActivity just starts, onPageStarted may be called before
955 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
956 // to start the timer. As we won't switch tabs while an activity is in
957 // pause state, we can ensure calling resume and pause in pair.
958 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800959 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700960 }
961 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700962 endActionMode();
963
John Reck30c714c2010-12-16 17:30:34 -0800964 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700965
John Reck324d4402011-01-11 16:56:42 -0800966 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 // update the bookmark database for favicon
968 maybeUpdateFavicon(tab, null, url, favicon);
969
970 Performance.tracePageStart(url);
971
972 // Performance probe
973 if (false) {
974 Performance.onPageStarted();
975 }
976
977 }
978
979 @Override
John Reck324d4402011-01-11 16:56:42 -0800980 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700981 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800982 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200983
Michael Kolb8233fac2010-10-26 16:08:53 -0700984 // Performance probe
985 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800986 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 }
988
Tarun Nainani8eb00912014-07-17 12:28:32 -0700989 tab.onPageFinished();
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700990 maybeUpdateFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700991
Michael Kolb8233fac2010-10-26 16:08:53 -0700992 Performance.tracePageFinished();
993 }
994
995 @Override
John Reck30c714c2010-12-16 17:30:34 -0800996 public void onProgressChanged(Tab tab) {
997 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700998
999 if (newProgress == 100) {
1000 CookieSyncManager.getInstance().sync();
1001 // onProgressChanged() may continue to be called after the main
1002 // frame has finished loading, as any remaining sub frames continue
1003 // to load. We'll only get called once though with newProgress as
1004 // 100 when everything is loaded. (onPageFinished is called once
1005 // when the main frame completes loading regardless of the state of
1006 // any sub frames so calls to onProgressChanges may continue after
1007 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001008 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001009 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001010 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001011 } else if (mWasInPageLoad) {
1012 mWasInPageLoad = false;
1013 updateInLoadMenuItems(mCachedMenu, tab);
1014 }
1015
1016 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001017 // pause the WebView timer and release the wake lock if it is
1018 // finished while BrowserActivity is in pause state.
1019 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001020 }
1021 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001022 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001023 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001024 // still loading
1025 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001027 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001028 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001029 } else {
1030 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 }
1032 }
John Reck30c714c2010-12-16 17:30:34 -08001033 mUi.onProgressChanged(tab);
1034 }
1035
1036 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001037 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001038 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 }
1040
1041 @Override
1042 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001043 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001044 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001045 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1047 return;
1048 }
1049 // Update the title in the history database if not in private browsing mode
1050 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001051 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 }
1053 }
1054
1055 @Override
1056 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001057 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001058 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1059 }
1060
1061 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001062 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1063 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 }
1065
1066 @Override
1067 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1068 if (mMenuIsDown) {
1069 // only check shortcut key when MENU is held
1070 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1071 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001072 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001073 int keyCode = event.getKeyCode();
1074 // We need to send almost every key to WebKit. However:
1075 // 1. We don't want to block the device on the renderer for
1076 // some keys like menu, home, call.
1077 // 2. There are no WebKit equivalents for some of these keys
1078 // (see app/keyboard_codes_win.h)
1079 // Note that these are not the same set as KeyEvent.isSystemKey:
1080 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1081 if (keyCode == KeyEvent.KEYCODE_MENU ||
1082 keyCode == KeyEvent.KEYCODE_HOME ||
1083 keyCode == KeyEvent.KEYCODE_BACK ||
1084 keyCode == KeyEvent.KEYCODE_CALL ||
1085 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1086 keyCode == KeyEvent.KEYCODE_POWER ||
1087 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1088 keyCode == KeyEvent.KEYCODE_CAMERA ||
1089 keyCode == KeyEvent.KEYCODE_FOCUS ||
1090 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1091 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1092 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1093 return true;
1094 }
1095
1096 // We also have to intercept some shortcuts before we send them to the ContentView.
1097 if (event.isCtrlPressed() && (
1098 keyCode == KeyEvent.KEYCODE_TAB ||
1099 keyCode == KeyEvent.KEYCODE_W ||
1100 keyCode == KeyEvent.KEYCODE_F4)) {
1101 return true;
1102 }
1103
1104 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 }
1106
Tarun Nainanif5563b62015-01-21 18:52:59 -08001107 private void handleMediaKeyEvent(KeyEvent event) {
1108
1109 int keyCode = event.getKeyCode();
1110 // send media key events to audio manager
1111 if (Build.VERSION.SDK_INT >= 19) {
1112
1113 switch (keyCode) {
1114 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1115 case KeyEvent.KEYCODE_MEDIA_STOP:
1116 case KeyEvent.KEYCODE_MEDIA_NEXT:
1117 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1118 case KeyEvent.KEYCODE_MEDIA_REWIND:
1119 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1120 case KeyEvent.KEYCODE_MUTE:
1121 case KeyEvent.KEYCODE_MEDIA_PLAY:
1122 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1123 case KeyEvent.META_SHIFT_RIGHT_ON:
1124 case KeyEvent.KEYCODE_MEDIA_EJECT:
1125 case KeyEvent.KEYCODE_MEDIA_RECORD:
1126 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1127
1128 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1129 .getSystemService(Context.AUDIO_SERVICE);
1130 audioManager.dispatchMediaKeyEvent(event);
1131 }
1132 }
1133 }
1134
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 @Override
John Reck997b1b72012-04-19 18:08:25 -07001136 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001138 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001140 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 } else {
John Reck997b1b72012-04-19 18:08:25 -07001142 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001143 }
1144 }
John Reck997b1b72012-04-19 18:08:25 -07001145 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 }
1147
1148 @Override
John Reck324d4402011-01-11 16:56:42 -08001149 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001150 // Don't save anything in private browsing mode or when disabling history
1151 // for regular tabs is enabled
1152 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1153 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1154 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001155
John Reck49a603c2011-03-03 09:33:05 -08001156 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001157
John Reck324d4402011-01-11 16:56:42 -08001158 if (TextUtils.isEmpty(url)
1159 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 return;
1161 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001162
John Reckf57c0292011-07-21 18:15:39 -07001163 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001164 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 }
1166
1167 @Override
1168 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1169 AsyncTask<Void, Void, String[]> task =
1170 new AsyncTask<Void, Void, String[]>() {
1171 @Override
1172 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001173 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 }
1175 @Override
1176 public void onPostExecute(String[] result) {
1177 callback.onReceiveValue(result);
1178 }
1179 };
1180 task.execute();
1181 }
1182
1183 @Override
1184 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1185 final HttpAuthHandler handler, final String host,
1186 final String realm) {
1187 String username = null;
1188 String password = null;
1189
1190 boolean reuseHttpAuthUsernamePassword
1191 = handler.useHttpAuthUsernamePassword();
1192
1193 if (reuseHttpAuthUsernamePassword && view != null) {
1194 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1195 if (credentials != null && credentials.length == 2) {
1196 username = credentials[0];
1197 password = credentials[1];
1198 }
1199 }
1200
1201 if (username != null && password != null) {
1202 handler.proceed(username, password);
1203 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001204 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001205 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1206 } else {
1207 handler.cancel();
1208 }
1209 }
1210 }
1211
1212 @Override
1213 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001214 String contentDisposition, String mimetype, String referer,
1215 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001216 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001217 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001218 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001219 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001220 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001221 // This Tab was opened for the sole purpose of downloading a
1222 // file. Remove it.
1223 if (tab == mTabControl.getCurrentTab()) {
1224 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001225 if (tab.getDerivedFromIntent())
1226 closeTab(tab);
1227 else
1228 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001229 } else {
1230 // In this case, it is not.
1231 closeTab(tab);
1232 }
1233 }
1234 }
1235
1236 @Override
1237 public Bitmap getDefaultVideoPoster() {
1238 return mUi.getDefaultVideoPoster();
1239 }
1240
1241 @Override
1242 public View getVideoLoadingProgressView() {
1243 return mUi.getVideoLoadingProgressView();
1244 }
1245
1246 @Override
1247 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1248 SslError error) {
1249 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1250 }
1251
1252 // helper method
1253
1254 /*
1255 * Update the favorites icon if the private browsing isn't enabled and the
1256 * icon is valid.
1257 */
1258 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1259 final String url, Bitmap favicon) {
1260 if (favicon == null) {
1261 return;
1262 }
1263 if (!tab.isPrivateBrowsingEnabled()) {
1264 Bookmarks.updateFavicon(mActivity
1265 .getContentResolver(), originalUrl, url, favicon);
1266 }
1267 }
1268
Leon Scroggins4cd97792010-12-03 15:31:56 -05001269 @Override
1270 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001271 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001272 mUi.bookmarkedStatusHasChanged(tab);
1273 }
1274
Michael Kolb8233fac2010-10-26 16:08:53 -07001275 // end WebViewController
1276
1277 protected void pageUp() {
1278 getCurrentTopWebView().pageUp(false);
1279 }
1280
1281 protected void pageDown() {
1282 getCurrentTopWebView().pageDown(false);
1283 }
1284
1285 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001286 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001287 public void editUrl() {
1288 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001289 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001290 }
1291
John Reck9c35b9c2012-05-30 10:08:50 -07001292 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001293 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001294 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 if (tab.inForeground()) {
1296 if (mUi.isCustomViewShowing()) {
1297 callback.onCustomViewHidden();
1298 return;
1299 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001300 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001301 // Save the menu state and set it to empty while the custom
1302 // view is showing.
1303 mOldMenuState = mMenuState;
1304 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001305 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001306 }
1307 }
1308
1309 @Override
1310 public void hideCustomView() {
1311 if (mUi.isCustomViewShowing()) {
1312 mUi.onHideCustomView();
1313 // Reset the old menu state.
1314 mMenuState = mOldMenuState;
1315 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001316 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001317 }
1318 }
1319
John Reck9c35b9c2012-05-30 10:08:50 -07001320 @Override
1321 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001322 Intent intent) {
1323 if (getCurrentTopWebView() == null) return;
1324 switch (requestCode) {
1325 case PREFERENCES_PAGE:
1326 if (resultCode == Activity.RESULT_OK && intent != null) {
1327 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001328 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001329 mTabControl.removeParentChildRelationShips();
1330 }
1331 }
1332 break;
1333 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001334 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001335 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001336 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001338 case AUTOFILL_SETUP:
1339 // Determine whether a profile was actually set up or not
1340 // and if so, send the message back to the WebTextView to
1341 // fill the form with the new profile.
1342 if (getSettings().getAutoFillProfile() != null) {
1343 mAutoFillSetupMessage.sendToTarget();
1344 mAutoFillSetupMessage = null;
1345 }
1346 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001347 case COMBO_VIEW:
1348 if (intent == null || resultCode != Activity.RESULT_OK) {
1349 break;
1350 }
John Reck3ba45532011-08-11 16:26:53 -07001351 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001352 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1353 Tab t = getCurrentTab();
1354 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001355 mUpdateMyNavThumbnail = true;
1356 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001357 loadUrl(t, uri.toString());
1358 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1359 String[] urls = intent.getStringArrayExtra(
1360 ComboViewActivity.EXTRA_OPEN_ALL);
1361 Tab parent = getCurrentTab();
1362 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001363 if (url != null) {
1364 parent = openTab(url, parent,
1365 !mSettings.openInBackground(), true);
1366 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001367 }
1368 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1369 long id = intent.getLongExtra(
1370 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1371 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001372 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001373 }
1374 }
1375 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001376 case VOICE_RESULT:
1377 if (resultCode == Activity.RESULT_OK && intent != null) {
1378 ArrayList<String> results = intent.getStringArrayListExtra(
1379 RecognizerIntent.EXTRA_RESULTS);
1380 if (results.size() >= 1) {
1381 mVoiceResult = results.get(0);
1382 }
1383 }
1384 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001385 case MY_NAVIGATION:
1386 if (intent == null || resultCode != Activity.RESULT_OK) {
1387 break;
1388 }
1389
1390 if (intent.getBooleanExtra("need_refresh", false) &&
1391 getCurrentTopWebView() != null) {
1392 getCurrentTopWebView().reload();
1393 }
1394 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001395 default:
1396 break;
1397 }
1398 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001399 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 }
1401
1402 /**
1403 * Open the Go page.
1404 * @param startWithHistory If true, open starting on the history tab.
1405 * Otherwise, start with the bookmarks tab.
1406 */
1407 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001408 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 if (mTabControl.getCurrentWebView() == null) {
1410 return;
1411 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001412 // clear action mode
1413 if (isInCustomActionMode()) {
1414 endActionMode();
1415 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 Bundle extras = new Bundle();
1417 // Disable opening in a new window if we have maxed out the windows
1418 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1419 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001420 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001421 }
1422
1423 // combo view callbacks
1424
Michael Kolb8233fac2010-10-26 16:08:53 -07001425 // key handling
1426 protected void onBackKey() {
1427 if (!mUi.onBackKey()) {
1428 WebView subwindow = mTabControl.getCurrentSubWindow();
1429 if (subwindow != null) {
1430 if (subwindow.canGoBack()) {
1431 subwindow.goBack();
1432 } else {
1433 dismissSubWindow(mTabControl.getCurrentTab());
1434 }
1435 } else {
1436 goBackOnePageOrQuit();
1437 }
1438 }
1439 }
1440
Michael Kolb4bd767d2011-05-27 11:33:55 -07001441 protected boolean onMenuKey() {
1442 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001443 }
1444
Michael Kolb8233fac2010-10-26 16:08:53 -07001445 // menu handling and state
1446 // TODO: maybe put into separate handler
1447
John Reck9c35b9c2012-05-30 10:08:50 -07001448 @Override
1449 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001450 if (mMenuState == EMPTY_MENU) {
1451 return false;
1452 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 MenuInflater inflater = mActivity.getMenuInflater();
1454 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001455 return true;
1456 }
1457
John Reck9c35b9c2012-05-30 10:08:50 -07001458 @Override
1459 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001460 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001461 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 return;
1463 }
1464 if (!(v instanceof WebView)) {
1465 return;
1466 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001467 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001468 WebView.HitTestResult result;
1469
1470 /* Determine whether the ContextMenu got triggered because
1471 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1472 * received. The mResult acts as a flag to identify, how it got trigerred
1473 */
1474 if (mResult == null){
1475 result = webview.getHitTestResult();
1476 } else {
1477 result = mResult;
1478 }
1479
Michael Kolb8233fac2010-10-26 16:08:53 -07001480 if (result == null) {
1481 return;
1482 }
1483
1484 int type = result.getType();
1485 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001486
1487 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1488 unknownTypeMap.put("webview", webview);
1489 final Message msg = mHandler.obtainMessage(
1490 UNKNOWN_TYPE_MSG, unknownTypeMap);
1491 /* As defined in android developers guide
1492 * when UNKNOWN_TYPE is received as a result of HitTest
1493 * you need to determing the type by invoking requestFocusNodeHref
1494 */
1495 webview.requestFocusNodeHref(msg);
1496
Michael Kolb8233fac2010-10-26 16:08:53 -07001497 Log.w(LOGTAG,
1498 "We should not show context menu when nothing is touched");
1499 return;
1500 }
1501 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1502 // let TextView handles context menu
1503 return;
1504 }
1505
1506 // Note, http://b/issue?id=1106666 is requesting that
1507 // an inflated menu can be used again. This is not available
1508 // yet, so inflate each time (yuk!)
1509 MenuInflater inflater = mActivity.getMenuInflater();
1510 inflater.inflate(R.menu.browsercontext, menu);
1511
1512 // Show the correct menu group
1513 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001514 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001515 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001516 menu.setGroupVisible(R.id.PHONE_MENU,
1517 type == WebView.HitTestResult.PHONE_TYPE);
1518 menu.setGroupVisible(R.id.EMAIL_MENU,
1519 type == WebView.HitTestResult.EMAIL_TYPE);
1520 menu.setGroupVisible(R.id.GEO_MENU,
1521 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001522 String itemUrl = null;
1523 String url = webview.getOriginalUrl();
1524 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1525 itemUrl = Uri.decode(navigationUrl);
1526 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1527 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1528 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1529 } else {
1530 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1531 }
1532 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1533 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1534 } else {
1535 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1536
1537 menu.setGroupVisible(R.id.IMAGE_MENU,
1538 type == WebView.HitTestResult.IMAGE_TYPE
1539 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1540 menu.setGroupVisible(R.id.ANCHOR_MENU,
1541 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1542 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001543 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1544 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001545 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001546 // Setup custom handling depending on the type
1547 switch (type) {
1548 case WebView.HitTestResult.PHONE_TYPE:
1549 menu.setHeaderTitle(Uri.decode(extra));
1550 menu.findItem(R.id.dial_context_menu_id).setIntent(
1551 new Intent(Intent.ACTION_VIEW, Uri
1552 .parse(WebView.SCHEME_TEL + extra)));
1553 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1554 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1555 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1556 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1557 addIntent);
1558 menu.findItem(R.id.copy_phone_context_menu_id)
1559 .setOnMenuItemClickListener(
1560 new Copy(extra));
1561 break;
1562
1563 case WebView.HitTestResult.EMAIL_TYPE:
1564 menu.setHeaderTitle(extra);
1565 menu.findItem(R.id.email_context_menu_id).setIntent(
1566 new Intent(Intent.ACTION_VIEW, Uri
1567 .parse(WebView.SCHEME_MAILTO + extra)));
1568 menu.findItem(R.id.copy_mail_context_menu_id)
1569 .setOnMenuItemClickListener(
1570 new Copy(extra));
1571 break;
1572
1573 case WebView.HitTestResult.GEO_TYPE:
1574 menu.setHeaderTitle(extra);
1575 menu.findItem(R.id.map_context_menu_id).setIntent(
1576 new Intent(Intent.ACTION_VIEW, Uri
1577 .parse(WebView.SCHEME_GEO
1578 + URLEncoder.encode(extra))));
1579 menu.findItem(R.id.copy_geo_context_menu_id)
1580 .setOnMenuItemClickListener(
1581 new Copy(extra));
1582 break;
1583
1584 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1585 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001586 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 // decide whether to show the open link in new tab option
1588 boolean showNewTab = mTabControl.canCreateNewTab();
1589 MenuItem newTabItem
1590 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001591 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001592 ? R.string.contextmenu_openlink_newwindow_background
1593 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001594 newTabItem.setVisible(showNewTab);
1595 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001596 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1597 newTabItem.setOnMenuItemClickListener(
1598 new MenuItem.OnMenuItemClickListener() {
1599 @Override
1600 public boolean onMenuItemClick(MenuItem item) {
1601 final HashMap<String, WebView> hrefMap =
1602 new HashMap<String, WebView>();
1603 hrefMap.put("webview", webview);
1604 final Message msg = mHandler.obtainMessage(
1605 FOCUS_NODE_HREF,
1606 R.id.open_newtab_context_menu_id,
1607 0, hrefMap);
1608 webview.requestFocusNodeHref(msg);
1609 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001610 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001611 });
1612 } else {
1613 newTabItem.setOnMenuItemClickListener(
1614 new MenuItem.OnMenuItemClickListener() {
1615 @Override
1616 public boolean onMenuItemClick(MenuItem item) {
1617 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001618 openTab(extra, parent,
1619 !mSettings.openInBackground(),
1620 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001621 return true;
1622 }
1623 });
1624 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001625 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001626 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1627 menu.setHeaderTitle(navigationUrl);
1628 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1629
1630 if (itemUrl != null) {
1631 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1632 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1633 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1634 @Override
1635 public boolean onMenuItemClick(MenuItem item) {
1636 final Intent intent = new Intent(Controller.this
1637 .getContext(),
1638 AddMyNavigationPage.class);
1639 Bundle bundle = new Bundle();
1640 String url = Uri.decode(navigationUrl);
1641 bundle.putBoolean("isAdding", false);
1642 bundle.putString("url", url);
1643 bundle.putString("name", getNameFromUrl(url));
1644 intent.putExtra("websites", bundle);
1645 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1646 return false;
1647 }
1648 });
1649 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1650 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1651 @Override
1652 public boolean onMenuItemClick(MenuItem item) {
1653 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1654 return false;
1655 }
1656 });
1657 }
1658 } else {
1659 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1660 }
1661 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001662 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1663 break;
1664 }
1665 // otherwise fall through to handle image part
1666 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001667 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1668 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001669 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1670 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001671 shareItem.setOnMenuItemClickListener(
1672 new MenuItem.OnMenuItemClickListener() {
1673 @Override
1674 public boolean onMenuItemClick(MenuItem item) {
1675 sharePage(mActivity, null, extra, null,
1676 null);
1677 return true;
1678 }
1679 }
1680 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001681 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001682 menu.findItem(R.id.view_image_context_menu_id)
1683 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1684 @Override
1685 public boolean onMenuItemClick(MenuItem item) {
1686 openTab(extra, mTabControl.getCurrentTab(), true, true);
1687 return false;
1688 }
1689 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001690 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1691 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1692 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001693 menu.findItem(R.id.set_wallpaper_context_menu_id).
1694 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1695 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001696 break;
1697
1698 default:
1699 Log.w(LOGTAG, "We should not get here.");
1700 break;
1701 }
1702 //update the ui
1703 mUi.onContextMenuCreated(menu);
1704 }
1705
kaiyiz6e5b3e02013-08-19 20:02:01 +08001706 public void startAddMyNavigation(String url) {
1707 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1708 Bundle bundle = new Bundle();
1709 bundle.putBoolean("isAdding", true);
1710 bundle.putString("url", url);
1711 bundle.putString("name", getNameFromUrl(url));
1712 intent.putExtra("websites", bundle);
1713 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1714 }
1715
1716 private void showMyNavigationDeleteDialog(final String itemUrl) {
1717 new AlertDialog.Builder(this.getContext())
1718 .setTitle(R.string.my_navigation_delete_label)
1719 .setIcon(android.R.drawable.ic_dialog_alert)
1720 .setMessage(R.string.my_navigation_delete_msg)
1721 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1722 @Override
1723 public void onClick(DialogInterface dialog, int whichButton) {
1724 deleteMyNavigationItem(itemUrl);
1725 }
1726 })
1727 .setNegativeButton(R.string.cancel, null)
1728 .show();
1729 }
1730
1731 private void deleteMyNavigationItem(final String itemUrl) {
1732 ContentResolver cr = this.getContext().getContentResolver();
1733 Cursor cursor = null;
1734
1735 try {
1736 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1737 new String[] {
1738 MyNavigationUtil.ID
1739 }, "url = ?", new String[] {
1740 itemUrl
1741 }, null);
1742 if (null != cursor && cursor.moveToFirst()) {
1743 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1744 cursor.getLong(0));
1745
1746 ContentValues values = new ContentValues();
1747 values.put(MyNavigationUtil.TITLE, "");
1748 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1749 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1750 ByteArrayOutputStream os = new ByteArrayOutputStream();
1751 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1752 R.raw.my_navigation_add);
1753 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1754 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1755 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1756 cr.update(uri, values, null, null);
1757 } else {
1758 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1759 }
1760 } catch (IllegalStateException e) {
1761 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1762 } finally {
1763 if (null != cursor) {
1764 cursor.close();
1765 }
1766 }
1767
1768 if (getCurrentTopWebView() != null) {
1769 getCurrentTopWebView().reload();
1770 }
1771 }
1772
1773 private String getNameFromUrl(String itemUrl) {
1774 ContentResolver cr = this.getContext().getContentResolver();
1775 Cursor cursor = null;
1776 String name = null;
1777
1778 try {
1779 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1780 new String[] {
1781 MyNavigationUtil.TITLE
1782 }, "url = ?", new String[] {
1783 itemUrl
1784 }, null);
1785 if (null != cursor && cursor.moveToFirst()) {
1786 name = cursor.getString(0);
1787 } else {
1788 Log.e(LOGTAG, "this item does not exist!");
1789 }
1790 } catch (IllegalStateException e) {
1791 Log.e(LOGTAG, "getNameFromUrl", e);
1792 } finally {
1793 if (null != cursor) {
1794 cursor.close();
1795 }
1796 }
1797 return name;
1798 }
1799
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001800 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001801 final ContentResolver cr = mActivity.getContentResolver();
1802 new AsyncTask<Void, Void, Void>() {
1803 @Override
1804 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001805 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1806 if(!isMyNavigationUrl)
1807 return null;
1808
kaiyiz6e5b3e02013-08-19 20:02:01 +08001809 ContentResolver cr = mActivity.getContentResolver();
1810 Cursor cursor = null;
1811 try {
1812 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1813 new String[] {
1814 MyNavigationUtil.ID
1815 }, "url = ?", new String[] {
1816 itemUrl
1817 }, null);
1818 if (null != cursor && cursor.moveToFirst()) {
1819 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001820 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001821
1822 ContentValues values = new ContentValues();
1823 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1824 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1825 cursor.getLong(0));
1826 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1827 cr.update(uri, values, null, null);
1828 os.close();
1829 }
1830 } catch (IllegalStateException e) {
1831 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1832 } catch (IOException e) {
1833 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1834 } finally {
1835 if (null != cursor) {
1836 cursor.close();
1837 }
1838 }
1839 return null;
1840 }
1841 }.execute();
1842 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001843 /**
1844 * As the menu can be open when loading state changes
1845 * we must manually update the state of the stop/reload menu
1846 * item
1847 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001848 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001849 if (menu == null) {
1850 return;
1851 }
1852 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001853 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001854 menu.findItem(R.id.stop_menu_id):
1855 menu.findItem(R.id.reload_menu_id);
1856 if (src != null) {
1857 dest.setIcon(src.getIcon());
1858 dest.setTitle(src.getTitle());
1859 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001860 mActivity.invalidateOptionsMenu();
1861 }
1862
1863 public void invalidateOptionsMenu() {
1864 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001865 }
1866
John Reck9c35b9c2012-05-30 10:08:50 -07001867 @Override
1868 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001869 // Software menu key (toolbar key)
1870 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1871 return true;
1872 }
1873
1874 @Override
1875 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001876 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001877 // hold on to the menu reference here; it is used by the page callbacks
1878 // to update the menu based on loading state
1879 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001880 // Note: setVisible will decide whether an item is visible; while
1881 // setEnabled() will decide whether an item is enabled, which also means
1882 // whether the matching shortcut key will function.
1883 switch (mMenuState) {
1884 case EMPTY_MENU:
1885 if (mCurrentMenuState != mMenuState) {
1886 menu.setGroupVisible(R.id.MAIN_MENU, false);
1887 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1888 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1889 }
1890 break;
1891 default:
1892 if (mCurrentMenuState != mMenuState) {
1893 menu.setGroupVisible(R.id.MAIN_MENU, true);
1894 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1895 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1896 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001897 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001898 break;
1899 }
1900 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001901 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001902
1903 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001904 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1905 EditBookmarksRestriction.getInstance()
1906 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001907 }
1908
1909 private void setMenuItemVisibility(Menu menu, int id,
1910 boolean visibility) {
1911 MenuItem item = menu.findItem(id);
1912 if (item != null) {
1913 item.setVisible(visibility);
1914 }
1915 }
1916
1917 private int lookupBookmark(String title, String url) {
1918 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001919 int count = 0;
1920 Cursor cursor = null;
1921 try {
1922 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1923 BookmarksLoader.PROJECTION,
1924 "title = ? OR url = ?",
1925 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001926 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001927 },
1928 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001929
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001930 if (cursor != null)
1931 count = cursor.getCount();
1932
1933 } catch (IllegalStateException e) {
1934 Log.e(LOGTAG, "lookupBookmark ", e);
1935 } finally {
1936 if (null != cursor) {
1937 cursor.close();
1938 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001939 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001940 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001941 }
1942
1943 private void resetMenuItems(Menu menu) {
1944 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1945 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1946
1947 WebView w = getCurrentTopWebView();
1948 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1949
1950 String title = w.getTitle();
1951 String url = w.getUrl();
Pankaj Garg634bf432015-07-13 09:54:21 -07001952 mCurrentPageBookmarked = (lookupBookmark(title, url) > 0);
1953 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001954 bookmark_icon.setChecked(true);
1955 } else {
1956 bookmark_icon.setChecked(false);
1957 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001958
1959 // update reader mode checkbox
1960 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1961 readerSwitcher.setVisible(false);
1962 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 }
1964
Michael Kolb4bf79712011-07-14 14:18:12 -07001965 @Override
1966 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001967 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001968 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001969 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001970 // Following flag is used to identify schemes for which the LIVE_MENU
1971 // items defined in res/menu/browser.xml should be enabled
1972 boolean isLiveScheme = false;
1973 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001974 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07001975
1976 boolean isDistillable = false;
1977 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001978 resetMenuItems(menu);
1979
Michael Kolb4bf79712011-07-14 14:18:12 -07001980 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001981 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001982 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001983 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001984 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001985 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001986 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07001987
1988 isDistillable = tab.isDistillable();
1989 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07001990 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001991
1992 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1993 forward.setEnabled(canGoForward);
1994
Michael Kolb4bf79712011-07-14 14:18:12 -07001995 // decide whether to show the share link option
1996 PackageManager pm = mActivity.getPackageManager();
1997 Intent send = new Intent(Intent.ACTION_SEND);
1998 send.setType("text/plain");
1999 ResolveInfo ri = pm.resolveActivity(send,
2000 PackageManager.MATCH_DEFAULT_ONLY);
2001 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2002
2003 boolean isNavDump = mSettings.enableNavDump();
2004 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2005 nav.setVisible(isNavDump);
2006 nav.setEnabled(isNavDump);
2007
2008 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002009 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2010 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002011 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002012 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2013 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002014 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002015 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2016 isLive && isLiveScheme && isPageFinished);
2017 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002018 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002019 // history and snapshots item are the members of COMBO menu group,
2020 // so if show history item, only make snapshots item invisible.
2021 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002022
Ze G Riande2a675c22015-06-03 11:15:24 -07002023
2024 // update reader mode checkbox
2025 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2026 // The reader mode checkbox is hidden only
2027 // when the current page is neither distillable nor distilled
2028 readerSwitcher.setVisible(isDistillable || isDistilled);
2029 readerSwitcher.setChecked(isDistilled);
2030
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002031 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002032 }
2033
John Reck9c35b9c2012-05-30 10:08:50 -07002034 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002035 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002036 if (null == getCurrentTopWebView()) {
2037 return false;
2038 }
2039 if (mMenuIsDown) {
2040 // The shortcut action consumes the MENU. Even if it is still down,
2041 // it won't trigger the next shortcut action. In the case of the
2042 // shortcut action triggering a new activity, like Bookmarks, we
2043 // won't get onKeyUp for MENU. So it is important to reset it here.
2044 mMenuIsDown = false;
2045 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002046 if (mUi.onOptionsItemSelected(item)) {
2047 // ui callback handled it
2048 return true;
2049 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002050 switch (item.getItemId()) {
2051 // -- Main menu
2052 case R.id.new_tab_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002053 getCurrentTab().capture();
Michael Kolb8233fac2010-10-26 16:08:53 -07002054 openTabToHomePage();
2055 break;
2056
2057 case R.id.incognito_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002058 getCurrentTab().capture();
Michael Kolb519d2282011-05-09 17:03:19 -07002059 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002060 break;
2061
2062 case R.id.goto_menu_id:
2063 editUrl();
2064 break;
2065
2066 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002067 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2068 break;
2069
2070 case R.id.history_menu_id:
2071 bookmarksOrHistoryPicker(ComboViews.History);
2072 break;
2073
2074 case R.id.snapshots_menu_id:
2075 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002076 break;
2077
Pankaj Garg49b79252014-11-07 17:33:41 -08002078 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002079 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 break;
2081
2082 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002083 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 stopLoading();
2085 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002086 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002087 getCurrentTopWebView().reload();
2088 }
2089 break;
2090
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002092 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002093 break;
2094
2095 case R.id.close_menu_id:
2096 // Close the subwindow if it exists.
2097 if (mTabControl.getCurrentSubWindow() != null) {
2098 dismissSubWindow(mTabControl.getCurrentTab());
2099 break;
2100 }
2101 closeCurrentTab();
2102 break;
2103
kaiyiza8b6dbb2013-07-29 18:11:22 +08002104 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002105 Object[] params = { new String("persist.debug.browsermonkeytest")};
2106 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002107 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002108 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002109 if (ret != null && ret.equals("enable"))
2110 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002111 if (BrowserConfig.getInstance(getContext())
2112 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2113 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002114 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002115 case R.id.homepage_menu_id:
2116 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002117 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002118 break;
2119
2120 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002121 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002122 break;
2123
2124 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002125 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 break;
2127
John Reck2bc80422011-06-30 15:11:49 -07002128 case R.id.save_snapshot_menu_id:
2129 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002130 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002131 createScreenshotAsync(
2132 source.getWebView(),
2133 getDesiredThumbnailWidth(mActivity),
2134 getDesiredThumbnailHeight(mActivity),
2135 new ValueCallback<Bitmap>() {
2136 @Override
2137 public void onReceiveValue(Bitmap bitmap) {
2138 new SaveSnapshotTask(source, bitmap).execute();
2139 }
2140 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002141 break;
2142
Michael Kolb8233fac2010-10-26 16:08:53 -07002143 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002144 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 break;
2146
John Recke1a03a32011-09-14 17:04:16 -07002147 case R.id.snapshot_go_live:
2148 goLive();
2149 return true;
2150
Michael Kolb8233fac2010-10-26 16:08:53 -07002151 case R.id.share_page_menu_id:
2152 Tab currentTab = mTabControl.getCurrentTab();
2153 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 return false;
2155 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002156 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002157 break;
2158
2159 case R.id.dump_nav_menu_id:
2160 getCurrentTopWebView().debugDump();
2161 break;
2162
Michael Kolb8233fac2010-10-26 16:08:53 -07002163 case R.id.zoom_in_menu_id:
2164 getCurrentTopWebView().zoomIn();
2165 break;
2166
2167 case R.id.zoom_out_menu_id:
2168 getCurrentTopWebView().zoomOut();
2169 break;
2170
2171 case R.id.view_downloads_menu_id:
2172 viewDownloads();
2173 break;
2174
John Reck42229bc2011-08-19 13:26:43 -07002175 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002176 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002177 break;
2178
Ze G Riande2a675c22015-06-03 11:15:24 -07002179 case R.id.reader_mode_menu_id:
2180 toggleReaderMode();
2181 break;
2182
Michael Kolb8233fac2010-10-26 16:08:53 -07002183 case R.id.window_one_menu_id:
2184 case R.id.window_two_menu_id:
2185 case R.id.window_three_menu_id:
2186 case R.id.window_four_menu_id:
2187 case R.id.window_five_menu_id:
2188 case R.id.window_six_menu_id:
2189 case R.id.window_seven_menu_id:
2190 case R.id.window_eight_menu_id:
2191 {
2192 int menuid = item.getItemId();
2193 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2194 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2195 Tab desiredTab = mTabControl.getTab(id);
2196 if (desiredTab != null &&
2197 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002198 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002199 }
2200 break;
2201 }
2202 }
2203 }
2204 break;
2205
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002206 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002207 Bundle bundle = new Bundle();
2208 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2209 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2210 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2211 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2212 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002213 break;
2214
Pankaj Garg49b79252014-11-07 17:33:41 -08002215 case R.id.add_to_homescreen:
2216 final WebView w = getCurrentTopWebView();
2217 final EditText input = new EditText(getContext());
2218 input.setText(w.getTitle());
2219 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002220 .setTitle(getContext().getResources().getString(
2221 R.string.add_to_homescreen))
2222 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002223 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002224 .setPositiveButton(getContext().getResources().getString(
2225 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002226 public void onClick(DialogInterface dialog, int whichButton) {
2227 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2228 getContext(),
2229 w.getUrl(),
2230 input.getText().toString(),
2231 w.getViewportBitmap(),
2232 w.getFavicon()));
2233
2234 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2235 .addCategory(Intent.CATEGORY_HOME));
2236 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002237 .setNegativeButton(getContext().getResources().getString(
2238 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002239 public void onClick(DialogInterface dialog, int whichButton) {
2240 // Do nothing.
2241 }
2242 })
2243 .show();
2244 break;
2245
Michael Kolb8233fac2010-10-26 16:08:53 -07002246 default:
2247 return false;
2248 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002249 return true;
2250 }
2251
John Reck68234a92012-04-19 15:27:12 -07002252 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2253 implements OnCancelListener {
2254
2255 private Tab mTab;
2256 private Dialog mProgressDialog;
2257 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002258 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002259
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002260 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002261 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002262 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002263 }
2264
2265 @Override
2266 protected void onPreExecute() {
2267 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2268 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2269 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002270 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002271 }
2272
2273 @Override
2274 protected Long doInBackground(Void... params) {
2275 if (!mTab.saveViewState(mValues)) {
2276 return null;
2277 }
2278 if (isCancelled()) {
2279 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2280 File file = mActivity.getFileStreamPath(path);
2281 if (!file.delete()) {
2282 file.deleteOnExit();
2283 }
2284 return null;
2285 }
2286 final ContentResolver cr = mActivity.getContentResolver();
2287 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2288 if (result == null) {
2289 return null;
2290 }
2291 long id = ContentUris.parseId(result);
2292 return id;
2293 }
2294
2295 @Override
2296 protected void onPostExecute(Long id) {
2297 if (isCancelled()) {
2298 return;
2299 }
2300 mProgressDialog.dismiss();
2301 if (id == null) {
2302 Toast.makeText(mActivity, R.string.snapshot_failed,
2303 Toast.LENGTH_SHORT).show();
2304 return;
2305 }
2306 Bundle b = new Bundle();
2307 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2308 mUi.showComboView(ComboViews.Snapshots, b);
2309 }
2310
2311 @Override
2312 public void onCancel(DialogInterface dialog) {
2313 cancel(true);
2314 }
2315 }
2316
Michael Kolb80f75082012-04-10 10:50:06 -07002317 @Override
2318 public void toggleUserAgent() {
2319 WebView web = getCurrentWebView();
2320 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002321 }
2322
Ze G Riande2a675c22015-06-03 11:15:24 -07002323 // This function calls the method in the webview to enable/disable
2324 // the reader mode through the DOM distiller
2325 public void toggleReaderMode() {
2326 Tab t = mTabControl.getCurrentTab();
2327 if (t.isDistilled()) {
2328 closeTab(t);
2329 } else if (t.isDistillable()) {
2330 openTab(t.getDistilledUrl(), false, true, false, t);
2331 }
2332 }
2333
Michael Kolb80f75082012-04-10 10:50:06 -07002334 @Override
2335 public void findOnPage() {
2336 getCurrentTopWebView().showFindDialog(null, true);
2337 }
2338
2339 @Override
2340 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002341 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002342 }
2343
2344 @Override
2345 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002346 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002347 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2348 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002349 }
2350 else {
2351 WebView w = getCurrentTopWebView();
2352 if (w == null)
2353 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002354 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002355 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002356 }
Michael Kolb80f75082012-04-10 10:50:06 -07002357 }
2358
John Recke1a03a32011-09-14 17:04:16 -07002359 private void goLive() {
Axesh R. Ajmera89cf4d82015-05-19 11:26:18 -07002360 if (!getCurrentTab().isSnapshot()) return;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002361 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002362 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002363 boolean onlySingleTabRemaining = false;
2364 if (mTabControl.getTabCount() > 1) {
2365 // destroy the old snapshot tab
2366 closeCurrentTab();
2367 } else {
2368 onlySingleTabRemaining = true;
2369 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002370 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002371 if (onlySingleTabRemaining) {
2372 closeTab(t);
2373 }
2374
Tarun Nainani8eb00912014-07-17 12:28:32 -07002375 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002376 }
2377
luxiaolb40014b2013-07-19 10:01:43 +08002378 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002379 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002380 new AlertDialog.Builder(activity)
2381 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002382 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002383 .setMessage(R.string.exit_browser_msg)
2384 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2385 public void onClick(DialogInterface dialog, int which) {
2386 activity.moveTaskToBack(true);
2387 dialog.dismiss();
2388 }
2389 })
2390 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2391 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002392 mCrashRecoveryHandler.clearState(true);
2393 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002394 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002395 dialog.dismiss();
2396 }
2397 })
2398 .show();
2399 }
Michael Kolb315d5022011-10-13 12:47:11 -07002400 @Override
2401 public void showPageInfo() {
2402 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2403 }
2404
John Reck9c35b9c2012-05-30 10:08:50 -07002405 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002406 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002407 // Let the History and Bookmark fragments handle menus they created.
2408 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2409 return false;
2410 }
2411
Michael Kolb8233fac2010-10-26 16:08:53 -07002412 int id = item.getItemId();
2413 boolean result = true;
2414 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002415 // -- Browser context menu
2416 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002417 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002418 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002419 case R.id.copy_link_context_menu_id:
2420 final WebView webView = getCurrentTopWebView();
2421 if (null == webView) {
2422 result = false;
2423 break;
2424 }
2425 final HashMap<String, WebView> hrefMap =
2426 new HashMap<String, WebView>();
2427 hrefMap.put("webview", webView);
2428 final Message msg = mHandler.obtainMessage(
2429 FOCUS_NODE_HREF, id, 0, hrefMap);
2430 webView.requestFocusNodeHref(msg);
2431 break;
2432
2433 default:
2434 // For other context menus
2435 result = onOptionsItemSelected(item);
2436 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002437 return result;
2438 }
2439
2440 /**
2441 * support programmatically opening the context menu
2442 */
2443 public void openContextMenu(View view) {
2444 mActivity.openContextMenu(view);
2445 }
2446
2447 /**
2448 * programmatically open the options menu
2449 */
2450 public void openOptionsMenu() {
2451 mActivity.openOptionsMenu();
2452 }
2453
John Reck9c35b9c2012-05-30 10:08:50 -07002454 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002455 public boolean onMenuOpened(int featureId, Menu menu) {
2456 if (mOptionsMenuOpen) {
2457 if (mConfigChanged) {
2458 // We do not need to make any changes to the state of the
2459 // title bar, since the only thing that happened was a
2460 // change in orientation
2461 mConfigChanged = false;
2462 } else {
2463 if (!mExtendedMenuOpen) {
2464 mExtendedMenuOpen = true;
2465 mUi.onExtendedMenuOpened();
2466 } else {
2467 // Switching the menu back to icon view, so show the
2468 // title bar once again.
2469 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002470 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002471 }
2472 }
2473 } else {
2474 // The options menu is closed, so open it, and show the title
2475 mOptionsMenuOpen = true;
2476 mConfigChanged = false;
2477 mExtendedMenuOpen = false;
2478 mUi.onOptionsMenuOpened();
2479 }
2480 return true;
2481 }
2482
John Reck9c35b9c2012-05-30 10:08:50 -07002483 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002484 public void onOptionsMenuClosed(Menu menu) {
2485 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002486 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002487 }
2488
John Reck9c35b9c2012-05-30 10:08:50 -07002489 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002490 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002491 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002492 }
2493
2494 // Helper method for getting the top window.
2495 @Override
2496 public WebView getCurrentTopWebView() {
2497 return mTabControl.getCurrentTopWebView();
2498 }
2499
2500 @Override
2501 public WebView getCurrentWebView() {
2502 return mTabControl.getCurrentWebView();
2503 }
2504
2505 /*
2506 * This method is called as a result of the user selecting the options
2507 * menu to see the download window. It shows the download window on top of
2508 * the current window.
2509 */
2510 void viewDownloads() {
2511 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2512 mActivity.startActivity(intent);
2513 }
2514
John Reck30b065e2011-07-19 10:58:05 -07002515 int getActionModeHeight() {
2516 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2517 new int[] { android.R.attr.actionBarSize });
2518 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2519 actionBarSizeTypedArray.recycle();
2520 return size;
2521 }
2522
Michael Kolb8233fac2010-10-26 16:08:53 -07002523 // action mode
2524
John Reck9c35b9c2012-05-30 10:08:50 -07002525 @Override
2526 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002527 mUi.onActionModeStarted(mode);
2528 mActionMode = mode;
2529 }
2530
2531 /*
2532 * True if a custom ActionMode (i.e. find or select) is in use.
2533 */
2534 @Override
2535 public boolean isInCustomActionMode() {
2536 return mActionMode != null;
2537 }
2538
2539 /*
2540 * End the current ActionMode.
2541 */
2542 @Override
2543 public void endActionMode() {
2544 if (mActionMode != null) {
2545 mActionMode.finish();
2546 }
2547 }
2548
2549 /*
2550 * Called by find and select when they are finished. Replace title bars
2551 * as necessary.
2552 */
John Reck9c35b9c2012-05-30 10:08:50 -07002553 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002554 public void onActionModeFinished(ActionMode mode) {
2555 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002556 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002557 mActionMode = null;
2558 }
2559
2560 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002561 final Tab tab = getCurrentTab();
2562 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002563 }
2564
2565 // bookmark handling
2566
2567 /**
2568 * add the current page as a bookmark to the given folder id
2569 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002570 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002571 * bookmarked, and if it is, edit that bookmark. If false, and
2572 * the site is already bookmarked, do not attempt to edit the
2573 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002574 */
2575 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002576 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002577 WebView w = getCurrentTopWebView();
2578 if (w == null) {
2579 return null;
2580 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002581 Intent i = new Intent(mActivity,
2582 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002583 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2584 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002585 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002586 if (touchIconUrl != null) {
2587 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2588 WebSettings settings = w.getSettings();
2589 if (settings != null) {
2590 i.putExtra(AddBookmarkPage.USER_AGENT,
2591 settings.getUserAgentString());
2592 }
2593 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002594 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002595 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002596 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002597 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2598 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 // Put the dialog at the upper right of the screen, covering the
2600 // star on the title bar.
2601 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002602 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002603 }
2604
2605 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002606 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002607 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002609 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002610 }
2611
Vivek Sekharb54614f2014-05-01 19:03:37 -07002612 @Override
2613 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2614 boolean capture) {
2615 mUploadHandler = new UploadHandler(this);
2616 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2617 }
2618
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 // thumbnails
2620
2621 /**
2622 * Return the desired width for thumbnail screenshots, which are stored in
2623 * the database, and used on the bookmarks screen.
2624 * @param context Context for finding out the density of the screen.
2625 * @return desired width for thumbnail screenshot.
2626 */
2627 static int getDesiredThumbnailWidth(Context context) {
2628 return context.getResources().getDimensionPixelOffset(
2629 R.dimen.bookmarkThumbnailWidth);
2630 }
2631
2632 /**
2633 * Return the desired height for thumbnail screenshots, which are stored in
2634 * the database, and used on the bookmarks screen.
2635 * @param context Context for finding out the density of the screen.
2636 * @return desired height for thumbnail screenshot.
2637 */
2638 static int getDesiredThumbnailHeight(Context context) {
2639 return context.getResources().getDimensionPixelOffset(
2640 R.dimen.bookmarkThumbnailHeight);
2641 }
2642
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002643 static void createScreenshotAsync(WebView view, int width, int height,
2644 final ValueCallback<Bitmap> cb) {
2645 if (view == null || width == 0 || height == 0) {
2646 return;
2647 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002648 view.getContentBitmapAsync(
2649 (float) width / view.getWidth(),
2650 new Rect(),
2651 new ValueCallback<Bitmap>() {
2652 @Override
2653 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002654 if (bitmap != null)
2655 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2656 cb.onReceiveValue(bitmap);
2657 }});
2658 }
2659
Michael Kolb8233fac2010-10-26 16:08:53 -07002660 private class Copy implements OnMenuItemClickListener {
2661 private CharSequence mText;
2662
John Reck9c35b9c2012-05-30 10:08:50 -07002663 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002664 public boolean onMenuItemClick(MenuItem item) {
2665 copy(mText);
2666 return true;
2667 }
2668
2669 public Copy(CharSequence toCopy) {
2670 mText = toCopy;
2671 }
2672 }
2673
Leon Scroggins63c02662010-11-18 15:16:27 -05002674 private static class Download implements OnMenuItemClickListener {
2675 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002676 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002677 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002678 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002679 private static final String FALLBACK_EXTENSION = "dat";
2680 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002681
John Reck9c35b9c2012-05-30 10:08:50 -07002682 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002683 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002684 if (DataUri.isDataUri(mText)) {
2685 saveDataUri();
2686 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002687 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002688 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002689 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002690 return true;
2691 }
2692
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002693 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2694 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002695 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002696 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002697 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002698 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002699 }
George Mount387d45d2011-10-07 15:57:53 -07002700
2701 /**
2702 * Treats mText as a data URI and writes its contents to a file
2703 * based on the current time.
2704 */
2705 private void saveDataUri() {
2706 FileOutputStream outputStream = null;
2707 try {
2708 DataUri uri = new DataUri(mText);
2709 File target = getTarget(uri);
2710 outputStream = new FileOutputStream(target);
2711 outputStream.write(uri.getData());
2712 final DownloadManager manager =
2713 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2714 manager.addCompletedDownload(target.getName(),
2715 mActivity.getTitle().toString(), false,
2716 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002717 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002718 } catch (IOException e) {
2719 Log.e(LOGTAG, "Could not save data URL");
2720 } finally {
2721 if (outputStream != null) {
2722 try {
2723 outputStream.close();
2724 } catch (IOException e) {
2725 // ignore close errors
2726 }
2727 }
2728 }
2729 }
2730
2731 /**
2732 * Creates a File based on the current time stamp and uses
2733 * the mime type of the DataUri to get the extension.
2734 */
2735 private File getTarget(DataUri uri) throws IOException {
2736 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002737 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002738 String nameBase = format.format(new Date());
2739 String mimeType = uri.getMimeType();
2740 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2741 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2742 if (extension == null) {
2743 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2744 extension = FALLBACK_EXTENSION;
2745 }
2746 extension = "." + extension; // createTempFile needs the '.'
2747 File targetFile = File.createTempFile(nameBase, extension, dir);
2748 return targetFile;
2749 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002750 }
2751
Cary Clark8974d282010-11-22 10:46:05 -05002752 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002753 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002754
John Reck9c35b9c2012-05-30 10:08:50 -07002755 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002756 public boolean onMenuItemClick(MenuItem item) {
2757 if (mWebView != null) {
2758 return mWebView.selectText();
2759 }
2760 return false;
2761 }
2762
2763 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002764 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002765 }
2766
2767 }
2768
Michael Kolb8233fac2010-10-26 16:08:53 -07002769 /********************** TODO: UI stuff *****************************/
2770
2771 // these methods have been copied, they still need to be cleaned up
2772
2773 /****************** tabs ***************************************************/
2774
2775 // basic tab interactions:
2776
2777 // it is assumed that tabcontrol already knows about the tab
2778 protected void addTab(Tab tab) {
2779 mUi.addTab(tab);
2780 }
2781
2782 protected void removeTab(Tab tab) {
2783 mUi.removeTab(tab);
2784 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002785 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002786 }
2787
Michael Kolbf2055602011-04-09 17:20:03 -07002788 @Override
2789 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002790 // monkey protection against delayed start
2791 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002792
2793 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2794 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002795 mTabControl.setCurrentTab(tab);
2796 // the tab is guaranteed to have a webview after setCurrentTab
2797 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002798
2799
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002800 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002801 //Purge active tabs
2802 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002803 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002804 }
2805
John Reck8bcafc12011-08-29 16:43:02 -07002806 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002807 Tab current = mTabControl.getCurrentTab();
2808 if (current != null
2809 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002810 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002811 }
2812 }
2813
John Reck26b18322011-06-21 13:08:58 -07002814 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002815 //Cancel navscreen request
2816 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002817 // Dismiss the subwindow if applicable.
2818 dismissSubWindow(appTab);
2819 // Since we might kill the WebView, remove it from the
2820 // content view first.
2821 mUi.detachTab(appTab);
2822 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002823 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002824 // TODO: analyze why the remove and add are necessary
2825 mUi.attachTab(appTab);
2826 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002827 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002828 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002829 } else {
2830 // If the tab was the current tab, we have to attach
2831 // it to the view system again.
2832 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002833 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002834 }
2835 }
2836
2837 // Remove the sub window if it exists. Also called by TabControl when the
2838 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002839 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002840 public void dismissSubWindow(Tab tab) {
2841 removeSubWindow(tab);
2842 // dismiss the subwindow. This will destroy the WebView.
2843 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002844 WebView wv = getCurrentTopWebView();
2845 if (wv != null) {
2846 wv.requestFocus();
2847 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002848 }
2849
2850 @Override
2851 public void removeSubWindow(Tab t) {
2852 if (t.getSubWebView() != null) {
2853 mUi.removeSubWindow(t.getSubViewContainer());
2854 }
2855 }
2856
2857 @Override
2858 public void attachSubWindow(Tab tab) {
2859 if (tab.getSubWebView() != null) {
2860 mUi.attachSubWindow(tab.getSubViewContainer());
2861 getCurrentTopWebView().requestFocus();
2862 }
2863 }
2864
Mathew Inwood29721c22011-06-29 17:55:24 +01002865 private Tab showPreloadedTab(final UrlData urlData) {
2866 if (!urlData.isPreloaded()) {
2867 return null;
2868 }
2869 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2870 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2871 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002872 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002873 // Could not submit query. Fallback to regular tab creation
2874 tabControl.destroy();
2875 return null;
2876 }
2877 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002878 // check tab count and make room for new tab
2879 if (!mTabControl.canCreateNewTab()) {
2880 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2881 if (leastUsed != null) {
2882 closeTab(leastUsed);
2883 }
2884 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002885 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002886 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002887 mTabControl.addPreloadedTab(t);
2888 addTab(t);
2889 setActiveTab(t);
2890 return t;
2891 }
2892
Michael Kolb7bcafde2011-05-09 13:55:59 -07002893 // open a non inconito tab with the given url data
2894 // and set as active tab
2895 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002896 Tab tab = showPreloadedTab(urlData);
2897 if (tab == null) {
2898 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002899 if ((tab != null) && !urlData.isEmpty()) {
2900 loadUrlDataIn(tab, urlData);
2901 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002902 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002903 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002904 }
2905
Michael Kolb843510f2010-12-09 10:51:49 -08002906 @Override
2907 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002908 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002909 }
2910
Michael Kolb8233fac2010-10-26 16:08:53 -07002911 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002912 public Tab openIncognitoTab() {
2913 return openTab(INCOGNITO_URI, true, true, false);
2914 }
2915
2916 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002917 public Tab openTab(String url, boolean incognito, boolean setActive,
2918 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002919 return openTab(url, incognito, setActive, useCurrent, null);
2920 }
2921
2922 @Override
2923 public Tab openTab(String url, Tab parent, boolean setActive,
2924 boolean useCurrent) {
2925 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2926 setActive, useCurrent, parent);
2927 }
2928
2929 public Tab openTab(String url, boolean incognito, boolean setActive,
2930 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002931 Tab tab = createNewTab(incognito, setActive, useCurrent);
2932 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002933 if (parent instanceof SnapshotTab) {
2934 addTab(tab);
2935 if (setActive)
2936 setActiveTab(tab);
2937 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002938 parent.addChildTab(tab);
2939 }
Michael Kolb519d2282011-05-09 17:03:19 -07002940 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002941 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002942 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002943 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002944 return tab;
2945 }
2946
2947 // this method will attempt to create a new tab
2948 // incognito: private browsing tab
2949 // setActive: ste tab as current tab
2950 // useCurrent: if no new tab can be created, return current tab
2951 private Tab createNewTab(boolean incognito, boolean setActive,
2952 boolean useCurrent) {
2953 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002954 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2955 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002956 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002957 if (mTabControl.canCreateNewTab()) {
2958 tab = mTabControl.createNewTab(incognito, !setActive);
2959 addTab(tab);
2960 if (setActive) {
2961 setActiveTab(tab);
2962 } else {
2963 tab.pause();
2964 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002965 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002966 if (useCurrent) {
2967 tab = mTabControl.getCurrentTab();
2968 reuseTab(tab, null);
2969 } else {
2970 mUi.showMaxTabsWarning();
2971 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002972 }
2973 }
2974 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002975 }
2976
John Reck2bc80422011-06-30 15:11:49 -07002977 @Override
2978 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2979 SnapshotTab tab = null;
2980 if (mTabControl.canCreateNewTab()) {
2981 tab = mTabControl.createSnapshotTab(snapshotId);
2982 addTab(tab);
2983 if (setActive) {
2984 setActiveTab(tab);
2985 }
2986 } else {
2987 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002988 }
2989 return tab;
2990 }
2991
Michael Kolb8233fac2010-10-26 16:08:53 -07002992 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002993 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002994 * @return boolean True if we successfully switched to a different tab. If
2995 * the indexth tab is null, or if that tab is the same as
2996 * the current one, return false.
2997 */
2998 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002999 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003000 Tab currentTab = mTabControl.getCurrentTab();
3001 if (tab == null || tab == currentTab) {
3002 return false;
3003 }
3004 setActiveTab(tab);
3005 return true;
3006 }
3007
3008 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003009 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003010 closeCurrentTab(false);
3011 }
3012
3013 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003014 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003015 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003016 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003017 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003018 return;
3019 }
Michael Kolbc831b632011-05-11 09:30:34 -07003020 final Tab current = mTabControl.getCurrentTab();
3021 final int pos = mTabControl.getCurrentPosition();
3022 Tab newTab = current.getParent();
3023 if (newTab == null) {
3024 newTab = mTabControl.getTab(pos + 1);
3025 if (newTab == null) {
3026 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003027 }
3028 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003029 if (andQuit) {
3030 mTabControl.setCurrentTab(newTab);
3031 closeTab(current);
3032 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003033 // Close window
3034 closeTab(current);
3035 }
3036 }
3037
3038 /**
3039 * Close the tab, remove its associated title bar, and adjust mTabControl's
3040 * current tab to a valid value.
3041 */
3042 @Override
3043 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003044 if (tab == mTabControl.getCurrentTab()) {
3045 closeCurrentTab();
3046 } else {
3047 removeTab(tab);
3048 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003049 }
3050
Afzal Najamd4e33312012-04-26 01:54:01 -04003051 /**
3052 * Close all tabs except the current one
3053 */
3054 @Override
3055 public void closeOtherTabs() {
3056 int inactiveTabs = mTabControl.getTabCount() - 1;
3057 for (int i = inactiveTabs; i >= 0; i--) {
3058 Tab tab = mTabControl.getTab(i);
3059 if (tab != mTabControl.getCurrentTab()) {
3060 removeTab(tab);
3061 }
3062 }
3063 }
3064
Michael Kolb8233fac2010-10-26 16:08:53 -07003065 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003066 protected void loadUrlFromContext(String url) {
3067 Tab tab = getCurrentTab();
3068 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003069 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003070 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003071 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003072 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003073 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003074 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 }
3076 }
3077 }
3078
3079 /**
3080 * Load the URL into the given WebView and update the title bar
3081 * to reflect the new load. Call this instead of WebView.loadUrl
3082 * directly.
3083 * @param view The WebView used to load url.
3084 * @param url The URL to load.
3085 */
John Reck71e51422011-07-01 16:49:28 -07003086 @Override
3087 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003088 loadUrl(tab, url, null);
3089 }
3090
3091 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3092 if (tab != null) {
3093 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003094 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003095 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003096 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003097 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003098 }
3099
3100 /**
3101 * Load UrlData into a Tab and update the title bar to reflect the new
3102 * load. Call this instead of UrlData.loadIn directly.
3103 * @param t The Tab used to load.
3104 * @param data The UrlData being loaded.
3105 */
3106 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003107 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003108 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003109 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003110 } else {
John Reck38b39652012-06-05 09:22:59 -07003111 if (t != null && data.mDisableUrlOverride) {
3112 t.disableUrlOverridingForLoad();
3113 }
John Reck26b18322011-06-21 13:08:58 -07003114 loadUrl(t, data.mUrl, data.mHeaders);
3115 }
3116 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003117 }
3118
John Reck30c714c2010-12-16 17:30:34 -08003119 @Override
3120 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003121 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003122 //In case of tab can go back (aka tab has navigation entry) do nothing
3123 //else just load homepage in current tab.
3124 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003125 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003126 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003127 }
3128
3129 void goBackOnePageOrQuit() {
3130 Tab current = mTabControl.getCurrentTab();
3131 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003132 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3133 showExitDialog(mActivity);
3134 } else {
3135 /*
3136 * Instead of finishing the activity, simply push this to the back
3137 * of the stack and let ActivityManager to choose the foreground
3138 * activity. As BrowserActivity is singleTask, it will be always the
3139 * root of the task. So we can use either true or false for
3140 * moveTaskToBack().
3141 */
3142 mActivity.moveTaskToBack(true);
3143 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003144 return;
3145 }
John Reckef654f12011-07-12 16:42:08 -07003146 if (current.canGoBack()) {
3147 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003148 } else {
3149 // Check to see if we are closing a window that was created by
3150 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003151 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003152 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003153 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003154 // Now we close the other tab
3155 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003156 } else if (BrowserConfig.getInstance(getContext())
3157 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3158 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003159 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003160 /*
3161 * Instead of finishing the activity, simply push this to the back
3162 * of the stack and let ActivityManager to choose the foreground
3163 * activity. As BrowserActivity is singleTask, it will be always the
3164 * root of the task. So we can use either true or false for
3165 * moveTaskToBack().
3166 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003167 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003168 }
3169 }
3170 }
3171
3172 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003173 * helper method for key handler
3174 * returns the current tab if it can't advance
3175 */
Michael Kolbc831b632011-05-11 09:30:34 -07003176 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003177 int pos = mTabControl.getCurrentPosition() + 1;
3178 if (pos >= mTabControl.getTabCount()) {
3179 pos = 0;
3180 }
3181 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003182 }
3183
3184 /**
3185 * helper method for key handler
3186 * returns the current tab if it can't advance
3187 */
Michael Kolbc831b632011-05-11 09:30:34 -07003188 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003189 int pos = mTabControl.getCurrentPosition() - 1;
3190 if ( pos < 0) {
3191 pos = mTabControl.getTabCount() - 1;
3192 }
3193 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003194 }
3195
John Reckbcef87f2012-02-03 14:58:34 -08003196 boolean isMenuOrCtrlKey(int keyCode) {
3197 return (KeyEvent.KEYCODE_MENU == keyCode)
3198 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3199 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3200 }
3201
Michael Kolb0035fad2011-03-14 13:25:28 -07003202 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003203 * handle key events in browser
3204 *
3205 * @param keyCode
3206 * @param event
3207 * @return true if handled, false to pass to super
3208 */
John Reck9c35b9c2012-05-30 10:08:50 -07003209 @Override
3210 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003211 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3212 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003213 if (!mUi.isComboViewShowing()) {
3214 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3215 true, false);
3216 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003217 return true;
3218 }
3219
Cary Clark160bbb92011-01-10 11:17:07 -05003220 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003221 // Even if MENU is already held down, we need to call to super to open
3222 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003223 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003224 mMenuIsDown = true;
3225 return false;
3226 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003227
Cary Clark8ff8c662010-12-29 15:03:05 -05003228 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003229 Tab tab = getCurrentTab();
3230 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003231
Cary Clark160bbb92011-01-10 11:17:07 -05003232 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3233 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003234
Michael Kolb8233fac2010-10-26 16:08:53 -07003235 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003236 case KeyEvent.KEYCODE_TAB:
3237 if (event.isCtrlPressed()) {
3238 if (event.isShiftPressed()) {
3239 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003240 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003241 } else {
3242 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003243 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003244 }
3245 return true;
3246 }
3247 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003248 case KeyEvent.KEYCODE_SPACE:
3249 // WebView/WebTextView handle the keys in the KeyDown. As
3250 // the Activity's shortcut keys are only handled when WebView
3251 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003252 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003253 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003254 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003255 pageDown();
3256 }
3257 return true;
3258 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003259 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003260 event.startTracking();
3261 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003262 case KeyEvent.KEYCODE_FORWARD:
3263 if (!noModifiers) break;
3264 tab.goForward();
3265 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003266 case KeyEvent.KEYCODE_DPAD_LEFT:
3267 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003268 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003269 return true;
3270 }
3271 break;
3272 case KeyEvent.KEYCODE_DPAD_RIGHT:
3273 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003274 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003275 return true;
3276 }
3277 break;
3278 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003279 if (ctrl) {
3280 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003281 return true;
3282 }
3283 break;
Michael Kolba4183062011-01-16 10:43:21 -08003284// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003285 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003286 if (ctrl ) {
3287 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003288 return true;
3289 }
3290 break;
Michael Kolba4183062011-01-16 10:43:21 -08003291// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003292// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003293// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003294// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003295// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003296// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003297// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003298// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003299// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003300// case KeyEvent.KEYCODE_M: // unused
3301// case KeyEvent.KEYCODE_N: // in Chrome: new window
3302// case KeyEvent.KEYCODE_O: // in Chrome: open file
3303// case KeyEvent.KEYCODE_P: // in Chrome: print page
3304// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003305// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003306// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3307 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003308 // we can't use the ctrl/shift flags, they check for
3309 // exclusive use of a modifier
3310 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003311 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003312 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003313 } else {
3314 openTabToHomePage();
3315 }
3316 return true;
3317 }
3318 break;
3319// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3320// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003321// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003322// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3323// case KeyEvent.KEYCODE_Y: // unused
3324// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003325 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003326 // it is a regular key and webview is not null
3327 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003328 }
3329
John Reck9c35b9c2012-05-30 10:08:50 -07003330 @Override
3331 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003332 switch(keyCode) {
3333 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003334 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003335 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003336 return true;
3337 }
3338 break;
3339 }
3340 return false;
3341 }
3342
John Reck9c35b9c2012-05-30 10:08:50 -07003343 @Override
3344 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003345 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003346 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003347 if (KeyEvent.KEYCODE_MENU == keyCode
3348 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003349 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003350 }
3351 }
Cary Clark160bbb92011-01-10 11:17:07 -05003352 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003353 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003354 case KeyEvent.KEYCODE_BACK:
3355 if (event.isTracking() && !event.isCanceled()) {
3356 onBackKey();
3357 return true;
3358 }
3359 break;
3360 }
3361 return false;
3362 }
3363
3364 public boolean isMenuDown() {
3365 return mMenuIsDown;
3366 }
3367
John Reck9c35b9c2012-05-30 10:08:50 -07003368 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003369 public void setupAutoFill(Message message) {
3370 // Open the settings activity at the AutoFill profile fragment so that
3371 // the user can create a new profile. When they return, we will dispatch
3372 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003373 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003374 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3375 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003376 }
John Reckb3417f02011-01-14 11:01:05 -08003377
John Reck9c35b9c2012-05-30 10:08:50 -07003378 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003379 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003380 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003381 return true;
3382 }
3383
John Reck1cf4b792011-07-26 10:22:22 -07003384 @Override
3385 public boolean shouldCaptureThumbnails() {
3386 return mUi.shouldCaptureThumbnails();
3387 }
3388
Michael Kolbc3af0672011-08-09 10:24:41 -07003389 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003390 public boolean supportsVoice() {
3391 PackageManager pm = mActivity.getPackageManager();
3392 List activities = pm.queryIntentActivities(new Intent(
3393 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3394 return activities.size() != 0;
3395 }
3396
3397 @Override
3398 public void startVoiceRecognizer() {
3399 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003400 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003401 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3402 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3403 mActivity.startActivityForResult(voice, VOICE_RESULT);
3404 }
3405
Pankaj Garg7b279f62014-08-12 14:47:18 -07003406 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003407 if (mLevel == level)
3408 return;
3409 mLevel = level;
3410 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003411 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3412 lp.dimAmount = level;
3413 mActivity.getWindow().setAttributes(lp);
3414 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3415 } else {
3416 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3417 }
3418 }
3419
Michael Kolb0b129122012-06-04 16:31:58 -07003420 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003421 public void setBlockEvents(boolean block) {
3422 mBlockEvents = block;
3423 }
3424
John Reck9c35b9c2012-05-30 10:08:50 -07003425 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003426 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003427 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003428 }
3429
John Reck9c35b9c2012-05-30 10:08:50 -07003430 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003431 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003432 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003433 }
3434
John Reck9c35b9c2012-05-30 10:08:50 -07003435 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003436 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003437 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003438 }
3439
John Reck9c35b9c2012-05-30 10:08:50 -07003440 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003441 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003442 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003443 }
3444
John Reck9c35b9c2012-05-30 10:08:50 -07003445 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003446 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003447 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003448 }
3449
Pankaj Garg49b79252014-11-07 17:33:41 -08003450 @Override
3451 public boolean shouldShowAppMenu() {
3452 return true;
3453 }
3454
3455 @Override
3456 public int getMenuThemeResourceId() {
3457 return R.style.OverflowMenuTheme;
3458 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003459}