blob: 3f5319a01d25fb993c1b5bdb9d673d701fe6717d [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;
Pankaj Garg32e1b942015-06-03 18:13:24 -070091import org.codeaurora.swe.PermissionsServiceFactory;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080092import org.codeaurora.swe.SslErrorHandler;
Pankaj Garg32e1b942015-06-03 18:13:24 -070093import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080094import org.codeaurora.swe.WebSettings;
95import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070096import org.codeaurora.swe.WebBackForwardList;
97import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080098
Bijan Amirzada41242f22014-03-21 12:12:18 -070099import com.android.browser.IntentHandler.UrlData;
100import com.android.browser.UI.ComboViews;
Dave Tharp9a082b12015-06-19 08:46:57 -0700101import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -0700102import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -0700103import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700104import com.android.browser.mynavigation.AddMyNavigationPage;
105import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700106import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700107import com.android.browser.platformsupport.BrowserContract;
108import com.android.browser.platformsupport.WebAddress;
109import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800110import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700111import com.android.browser.provider.BrowserProvider2.Thumbnails;
112import com.android.browser.provider.SnapshotProvider.Snapshots;
113import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800114import com.android.browser.appmenu.AppMenuHandler;
115import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700116
Michael Kolb8233fac2010-10-26 16:08:53 -0700117import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700118import java.io.File;
119import java.io.FileOutputStream;
120import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700121import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700122import java.text.DateFormat;
123import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700124import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700125import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700126import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800127import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200128import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700129import java.util.Map;
Pankaj Garg32e1b942015-06-03 18:13:24 -0700130import java.util.Set;
Michael Kolb8233fac2010-10-26 16:08:53 -0700131
132/**
133 * Controller for browser
134 */
135public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800136 implements WebViewController, UiController, ActivityController,
137 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700138
139 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800140 private static final String SEND_APP_ID_EXTRA =
141 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Vivek Sekhared791da2015-02-22 12:39:05 -0800142 private static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800143 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
144 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800145
Vivek Sekharb54614f2014-05-01 19:03:37 -0700146 // Remind switch to data connection if wifi is unavailable
147 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800148
Michael Kolb8233fac2010-10-26 16:08:53 -0700149 // public message ids
150 public final static int LOAD_URL = 1001;
151 public final static int STOP_LOAD = 1002;
152
153 // Message Ids
154 private static final int FOCUS_NODE_HREF = 102;
155 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800156 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700157
158 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
159
160 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800161 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700162
163 private static final int EMPTY_MENU = -1;
164
Michael Kolb8233fac2010-10-26 16:08:53 -0700165 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700166 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700167 final static int PREFERENCES_PAGE = 3;
168 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000169 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700170 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800171 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000172
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
174
175 // As the ids are dynamically created, we can't guarantee that they will
176 // be in sequence, so this static array maps ids to a window number.
177 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
178 { R.id.window_one_menu_id, R.id.window_two_menu_id,
179 R.id.window_three_menu_id, R.id.window_four_menu_id,
180 R.id.window_five_menu_id, R.id.window_six_menu_id,
181 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
182
183 // "source" parameter for Google search through search key
184 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
185 // "source" parameter for Google search through simplily type
186 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
187
George Mount387d45d2011-10-07 15:57:53 -0700188 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700189 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
190
John Reckd7dd9b22011-08-30 09:18:29 -0700191 // A bitmap that is re-used in createScreenshot as scratch space
192 private static Bitmap sThumbnailBitmap;
193
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 private Activity mActivity;
195 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700196 private HomepageHandler mHomepageHandler;
197 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700198 private BrowserSettings mSettings;
199 private WebViewFactory mFactory;
200
201 private WakeLock mWakeLock;
202
203 private UrlHandler mUrlHandler;
204 private UploadHandler mUploadHandler;
205 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700206 private PageDialogsHandler mPageDialogsHandler;
207 private NetworkStateHandler mNetworkHandler;
208
Ben Murdoch8029a772010-11-16 11:58:21 +0000209 private Message mAutoFillSetupMessage;
210
kaiyiza016da12013-08-26 17:50:22 +0800211 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700212
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 // FIXME, temp address onPrepareMenu performance problem.
214 // When we move everything out of view, we should rewrite this.
215 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700216 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 private int mOldMenuState = EMPTY_MENU;
218 private Menu mCachedMenu;
219
Michael Kolb8233fac2010-10-26 16:08:53 -0700220 private boolean mMenuIsDown;
221
Pankaj Garg49b79252014-11-07 17:33:41 -0800222 private boolean mWasInPageLoad = false;
223 private AppMenuHandler mAppMenuHandler;
224
Michael Kolb8233fac2010-10-26 16:08:53 -0700225 // For select and find, we keep track of the ActionMode so that
226 // finish() can be called as desired.
227 private ActionMode mActionMode;
228
229 /**
230 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
231 * of whether the configuration has changed. The first onMenuOpened call
232 * after a configuration change is simply a reopening of the same menu
233 * (i.e. mIconView did not change).
234 */
235 private boolean mConfigChanged;
236
237 /**
238 * Keeps track of whether the options menu is open. This is important in
239 * determining whether to show or hide the title bar overlay
240 */
241 private boolean mOptionsMenuOpen;
242
243 /**
244 * Whether or not the options menu is in its bigger, popup menu form. When
245 * true, we want the title bar overlay to be gone. When false, we do not.
246 * Only meaningful if mOptionsMenuOpen is true.
247 */
248 private boolean mExtendedMenuOpen;
249
Michael Kolb8233fac2010-10-26 16:08:53 -0700250 private boolean mActivityPaused = true;
251 private boolean mLoadStopped;
252
253 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500254 // Checks to see when the bookmarks database has changed, and updates the
255 // Tabs' notion of whether they represent bookmarked sites.
256 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700257 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700258
Michael Kolbc3af0672011-08-09 10:24:41 -0700259 private boolean mBlockEvents;
260
Michael Kolb0b129122012-06-04 16:31:58 -0700261 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800262 private boolean mUpdateMyNavThumbnail;
263 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800264 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800265 private WebView.HitTestResult mResult;
Pankaj Gargeba076f2015-03-25 13:40:59 -0700266 private static Bitmap mBookmarkBitmap;
Site Maoabb7bd32015-03-20 15:34:02 -0700267 private PowerConnectionReceiver mLowPowerReceiver;
268 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700269
George Mount3636d0a2011-11-21 09:08:21 -0800270 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 mActivity = browser;
272 mSettings = BrowserSettings.getInstance();
273 mTabControl = new TabControl(this);
274 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700275 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800276 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700277 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700278
279 mUrlHandler = new UrlHandler(this);
280 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
282
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500284 mBookmarksObserver = new ContentObserver(mHandler) {
285 @Override
286 public void onChange(boolean selfChange) {
287 int size = mTabControl.getTabCount();
288 for (int i = 0; i < size; i++) {
289 mTabControl.getTab(i).updateBookmarkedStatus();
290 }
291 }
292
293 };
294 browser.getContentResolver().registerContentObserver(
295 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700296
297 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700298 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800299 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700300
301 final WebRefiner refiner = WebRefiner.getInstance();
302 if (refiner != null) {
303 refiner.setRulesEnabled(WebRefiner.CATEGORY_ALL,
304 !PermissionsServiceFactory.getDefaultPermissions(
305 PermissionsServiceFactory.PermissionType.WEBREFINER));
306
307 PermissionsServiceFactory.getPermissionsService(
308 new ValueCallback<PermissionsServiceFactory.PermissionsService>() {
309 @Override
310 public void onReceiveValue(
311 PermissionsServiceFactory.PermissionsService value) {
312 Set<String> origins = value.getOrigins();
313 ArrayList<String> allowList = new ArrayList<>();
314 ArrayList<String> blockList = new ArrayList<>();
315 for (String origin : origins) {
316 PermissionsServiceFactory.PermissionsService.OriginInfo
317 info = value.getOriginInfo(origin);
318 int perm = info.getPermission(
319 PermissionsServiceFactory.PermissionType.WEBREFINER);
320 if (perm == PermissionsServiceFactory.Permission.ALLOW) {
321 allowList.add(origin);
322 } else if (perm == PermissionsServiceFactory.Permission.BLOCK) {
323 blockList.add(origin);
324 }
325 }
326 if (!allowList.isEmpty()) {
327 refiner.enableRulesForDomains(WebRefiner.CATEGORY_ALL,
328 allowList.toArray(new String[allowList.size()]));
329 }
330
331 if (!blockList.isEmpty()) {
332 refiner.disableRulesForDomains(WebRefiner.CATEGORY_ALL,
333 blockList.toArray(new String[blockList.size()]));
334 }
335 }
336 }
337 );
338 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700339 }
340
John Reck9c35b9c2012-05-30 10:08:50 -0700341 @Override
342 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700343 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800344 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800345 // mCrashRecoverHandler has any previously saved state.
346 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700347 }
348
George Mount3636d0a2011-11-21 09:08:21 -0800349 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800350 // we dont want to ever recover incognito tabs
351 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700352
Patrick Scott7d50a932011-02-04 09:27:26 -0500353 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700354 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500355 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000356
Michael Kolbc831b632011-05-11 09:30:34 -0700357 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500358 // Not able to restore so we go ahead and clear session cookies. We
359 // must do this before trying to login the user as we don't want to
360 // clear any session cookies set during login.
361 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700362 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800363 if (intent == null) {
364 // This won't happen under common scenarios. The icicle is
365 // not null, but there aren't any tabs to restore.
366 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700367 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800368 final Bundle extra = intent.getExtras();
369 // Create an initial tab.
370 // If the intent is ACTION_VIEW and data is not null, the Browser is
371 // invoked to view the content by another application. In this case,
372 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800373 UrlData urlData = null;
374 if (intent.getData() != null
375 && Intent.ACTION_VIEW.equals(intent.getAction())
376 && intent.getData().toString().startsWith("content://")) {
377 urlData = new UrlData(intent.getData().toString());
378 } else {
379 urlData = IntentHandler.getUrlDataFromIntent(intent);
380 }
George Mount3636d0a2011-11-21 09:08:21 -0800381 Tab t = null;
382 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700383 String landingPage = mActivity.getResources().getString(
384 R.string.def_landing_page);
385 if (!landingPage.isEmpty()) {
386 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800387 } else {
388 t = openTabToHomePage();
389 }
George Mount3636d0a2011-11-21 09:08:21 -0800390 } else {
391 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700392 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800393 }
394 if (t != null) {
395 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
396 }
397 WebView webView = t.getWebView();
398 if (extra != null) {
399 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
400 if (scale > 0 && scale <= 1000) {
401 webView.setInitialScale(scale);
402 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700403 }
404 }
John Reckd8c74522011-06-14 08:45:00 -0700405 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700406 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700407 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500408 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700409 List<Tab> tabs = mTabControl.getTabs();
410 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700411
John Reck1cf4b792011-07-26 10:22:22 -0700412 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700413 //handle restored pages that may require a JS interface
414 if (t.getWebView() != null) {
415 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
416 if (backForwardList != null) {
417 for (int i = 0; i < backForwardList.getSize(); i++) {
418 WebHistoryItem item = backForwardList.getItemAtIndex(i);
419 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
420 }
421 }
422 }
John Reck1cf4b792011-07-26 10:22:22 -0700423 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700424 if (t != mTabControl.getCurrentTab()) {
425 t.pause();
426 }
John Reck1cf4b792011-07-26 10:22:22 -0700427 }
428 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700429 if (tabs.size() == 0) {
430 openTabToHomePage();
431 }
John Reck1cf4b792011-07-26 10:22:22 -0700432 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700433 // TabControl.restoreState() will create a new tab even if
434 // restoring the state fails.
435 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800436 // Intent is non-null when framework thinks the browser should be
437 // launching with a new intent (icicle is null).
438 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700439 mIntentHandler.onNewIntent(intent);
440 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700441 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700442 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700443 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800444 if (jsFlags.trim().length() != 0) {
445 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700446 }
George Mount3636d0a2011-11-21 09:08:21 -0800447 if (intent != null
448 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700449 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800450 }
Site Maoabb7bd32015-03-20 15:34:02 -0700451 mLowPowerReceiver = new PowerConnectionReceiver();
452 mPowerChangeReceiver = new PowerConnectionReceiver();
453
454 //always track the android framework's power save mode
455 IntentFilter filter = new IntentFilter();
456 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
457 // Power save mode only exists in Lollipop and above
458 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
459 }
460 filter.addAction(Intent.ACTION_BATTERY_OKAY);
461 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700462 }
463
John Reck1cf4b792011-07-26 10:22:22 -0700464 private static class PruneThumbnails implements Runnable {
465 private Context mContext;
466 private List<Long> mIds;
467
468 PruneThumbnails(Context context, List<Long> preserveIds) {
469 mContext = context.getApplicationContext();
470 mIds = preserveIds;
471 }
472
473 @Override
474 public void run() {
475 ContentResolver cr = mContext.getContentResolver();
476 if (mIds == null || mIds.size() == 0) {
477 cr.delete(Thumbnails.CONTENT_URI, null, null);
478 } else {
479 int length = mIds.size();
480 StringBuilder where = new StringBuilder();
481 where.append(Thumbnails._ID);
482 where.append(" not in (");
483 for (int i = 0; i < length; i++) {
484 where.append(mIds.get(i));
485 if (i < (length - 1)) {
486 where.append(",");
487 }
488 }
489 where.append(")");
490 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
491 }
492 }
493
494 }
495
Michael Kolb1514bb72010-11-22 09:11:48 -0800496 @Override
497 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700498 return mFactory;
499 }
500
501 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800502 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700503 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700504 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800505 }
506
507 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800508 public void createSubWindow(Tab tab) {
509 endActionMode();
510 WebView mainView = tab.getWebView();
511 WebView subView = mFactory.createWebView((mainView == null)
512 ? false
513 : mainView.isPrivateBrowsingEnabled());
514 mUi.createSubWindow(tab, subView);
515 }
516
517 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700518 public Context getContext() {
519 return mActivity;
520 }
521
522 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 public Activity getActivity() {
524 return mActivity;
525 }
526
527 void setUi(UI ui) {
528 mUi = ui;
529 }
530
Michael Kolbaf63dba2012-05-16 12:58:05 -0700531 @Override
532 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700533 return mSettings;
534 }
535
536 IntentHandler getIntentHandler() {
537 return mIntentHandler;
538 }
539
540 @Override
541 public UI getUi() {
542 return mUi;
543 }
544
545 int getMaxTabs() {
546 return mActivity.getResources().getInteger(R.integer.max_tabs);
547 }
548
549 @Override
550 public TabControl getTabControl() {
551 return mTabControl;
552 }
553
Michael Kolb1bf23132010-11-19 12:55:12 -0800554 @Override
555 public List<Tab> getTabs() {
556 return mTabControl.getTabs();
557 }
558
Michael Kolb8233fac2010-10-26 16:08:53 -0700559 private void startHandler() {
560 mHandler = new Handler() {
561
562 @Override
563 public void handleMessage(Message msg) {
564 switch (msg.what) {
565 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700566 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700567 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800568 case UNKNOWN_TYPE_MSG:
569 HashMap unknownTypeMap = (HashMap) msg.obj;
570 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
571 /*
572 * When the context menu is shown to the user
573 * we need to assure that its happening on the current webview
574 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
575 */
576 if (getCurrentWebView() != viewForUnknownType)
577 break;
578
579 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800580 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800581 WebView.HitTestResult result = new WebView.HitTestResult();
582
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800583 // Prevent unnecessary calls to context menu
584 // if url and image src are null
585 if (unknown_type_src == null && unknown_type_url == null)
586 break;
587
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800588 //setting the HitTestResult with new RESULT TYPE
589 if (!TextUtils.isEmpty(unknown_type_src)) {
590 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
591 result.setExtra(unknown_type_src);
592 } else {
593 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
594 result.setExtra("about:blank");
595 }
596
597 mResult = result;
598 openContextMenu(viewForUnknownType);
599 mResult = null;
600
601 break;
602
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 case FOCUS_NODE_HREF:
604 {
605 String url = (String) msg.getData().get("url");
606 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500607 String src = (String) msg.getData().get("src");
608 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700609 if (TextUtils.isEmpty(url)) {
610 break;
611 }
612 HashMap focusNodeMap = (HashMap) msg.obj;
613 WebView view = (WebView) focusNodeMap.get("webview");
614 // Only apply the action if the top window did not change.
615 if (getCurrentTopWebView() != view) {
616 break;
617 }
618 switch (msg.arg1) {
619 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700620 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500622 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700623 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500624 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500625 case R.id.open_newtab_context_menu_id:
626 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700627 openTab(url, parent,
628 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500629 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700630 case R.id.copy_link_context_menu_id:
631 copy(url);
632 break;
633 case R.id.save_link_context_menu_id:
634 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500635 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800636 mActivity, url, view.getSettings().getUserAgentString(),
637 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700638 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700639 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700640 if(title == null || title == "")
641 title = url;
642
643 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
644 //SWE TODO: No thumbnail support for the url obtained via
645 //browser context menu as its not loaded in webview.
646 if (bookmarkIntent != null) {
647 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
648 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
649 mActivity.startActivity(bookmarkIntent);
650 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700651 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 }
653 break;
654 }
655
656 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700657 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700658 break;
659
660 case STOP_LOAD:
661 stopLoading();
662 break;
663
664 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700665 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 mWakeLock.release();
667 // if we reach here, Browser should be still in the
668 // background loading after WAKELOCK_TIMEOUT (5-min).
669 // To avoid burning the battery, stop loading.
670 mTabControl.stopAllLoading();
671 }
672 break;
673
674 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800675 Tab tab = (Tab) msg.obj;
676 if (tab != null) {
677 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 }
679 break;
kaiyiz591110b2013-08-06 17:11:06 +0800680 case OPEN_MENU:
681 if (!mOptionsMenuOpen && mActivity != null ) {
682 mActivity.openOptionsMenu();
683 }
684 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700685 }
686 }
687 };
688
689 }
690
John Reckef654f12011-07-12 16:42:08 -0700691 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200692 public Tab getCurrentTab() {
693 return mTabControl.getCurrentTab();
694 }
695
Michael Kolbba99c5d2010-11-29 14:57:41 -0800696 @Override
697 public void shareCurrentPage() {
698 shareCurrentPage(mTabControl.getCurrentTab());
699 }
700
701 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700702 if (tab == null || tab.getWebView() == null)
703 return;
704
705 final Tab mytab = tab;
706 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
707 @Override
708 public void onReceiveValue(Bitmap bitmap) {
709 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
710 mytab.getFavicon(), bitmap);
711 }
712 };
713
714 createScreenshotAsync(
715 tab.getWebView(),
716 getDesiredThumbnailWidth(mActivity),
717 getDesiredThumbnailHeight(mActivity),
718 new ValueCallback<Bitmap>() {
719 @Override
720 public void onReceiveValue(Bitmap bitmap) {
721 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
722 mytab.getFavicon(), bitmap);
723 }
724 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800725 }
726
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 /**
728 * Share a page, providing the title, url, favicon, and a screenshot. Uses
729 * an {@link Intent} to launch the Activity chooser.
730 * @param c Context used to launch a new Activity.
731 * @param title Title of the page. Stored in the Intent with
732 * {@link Intent#EXTRA_SUBJECT}
733 * @param url URL of the page. Stored in the Intent with
734 * {@link Intent#EXTRA_TEXT}
735 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
736 * with {@link Browser#EXTRA_SHARE_FAVICON}
737 * @param screenshot Bitmap of a screenshot of the page. Stored in the
738 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
739 */
740 static final void sharePage(Context c, String title, String url,
741 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700742
743 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
744 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
745 R.layout.app_row, sDialog.getApps());
746 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700747 }
748
749 private void copy(CharSequence text) {
750 ClipboardManager cm = (ClipboardManager) mActivity
751 .getSystemService(Context.CLIPBOARD_SERVICE);
752 cm.setText(text);
753 }
754
755 // lifecycle
756
John Reck9c35b9c2012-05-30 10:08:50 -0700757 @Override
758 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700759 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800760 // update the menu in case of a locale change
761 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800762 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800763 if (mOptionsMenuOpen) {
764 mActivity.closeOptionsMenu();
765 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
766 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700767 if (mPageDialogsHandler != null) {
768 mPageDialogsHandler.onConfigurationChanged(config);
769 }
770 mUi.onConfigurationChanged(config);
771 }
772
773 @Override
774 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700775 if (!mUi.isWebShowing()) {
776 mUi.showWeb(false);
777 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700778 mIntentHandler.onNewIntent(intent);
779 }
780
John Reck9c35b9c2012-05-30 10:08:50 -0700781 @Override
782 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800783 if (mUi.isCustomViewShowing()) {
784 hideCustomView();
785 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 if (mActivityPaused) {
787 Log.e(LOGTAG, "BrowserActivity is already paused.");
788 return;
789 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700790 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800791 Tab tab = mTabControl.getCurrentTab();
792 if (tab != null) {
793 tab.pause();
794 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700795 if (mWakeLock == null) {
796 PowerManager pm = (PowerManager) mActivity
797 .getSystemService(Context.POWER_SERVICE);
798 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
799 }
Michael Kolb70976932010-11-30 11:34:01 -0800800 mWakeLock.acquire();
801 mHandler.sendMessageDelayed(mHandler
802 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
803 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 }
805 mUi.onPause();
806 mNetworkHandler.onPause();
807
808 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100809 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700810 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 }
812
John Reck9c35b9c2012-05-30 10:08:50 -0700813 @Override
814 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700815 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800816 Bundle saveState = createSaveState();
817
818 // crash recovery manages all save & restore state
819 mCrashRecoveryHandler.writeState(saveState);
820 mSettings.setLastRunPaused(true);
821 }
822
823 /**
824 * Save the current state to outState. Does not write the state to
825 * disk.
826 * @return Bundle containing the current state of all tabs.
827 */
828 /* package */ Bundle createSaveState() {
829 Bundle saveState = new Bundle();
830 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800831 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 }
833
John Reck9c35b9c2012-05-30 10:08:50 -0700834 @Override
835 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700836 if (!mActivityPaused) {
837 Log.e(LOGTAG, "BrowserActivity is already resumed.");
838 return;
839 }
George Mount3636d0a2011-11-21 09:08:21 -0800840 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800842 Tab current = mTabControl.getCurrentTab();
843 if (current != null) {
844 current.resume();
845 resumeWebViewTimers(current);
846 }
John Reckf57c0292011-07-21 18:15:39 -0700847 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200848
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 mUi.onResume();
850 mNetworkHandler.onResume();
851 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100852 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700853 if (mVoiceResult != null) {
854 mUi.onVoiceResult(mVoiceResult);
855 mVoiceResult = null;
856 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700857 if (current != null && current.getWebView().isShowingCrashView())
858 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700859 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700860 }
861
John Reckf57c0292011-07-21 18:15:39 -0700862 private void releaseWakeLock() {
863 if (mWakeLock != null && mWakeLock.isHeld()) {
864 mHandler.removeMessages(RELEASE_WAKELOCK);
865 mWakeLock.release();
866 }
867 }
868
Michael Kolb70976932010-11-30 11:34:01 -0800869 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800870 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800871 * @param tab guaranteed non-null
872 */
873 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700874 boolean inLoad = tab.inPageLoad();
875 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
876 CookieSyncManager.getInstance().startSync();
877 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100878 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 }
880 }
881
Michael Kolb70976932010-11-30 11:34:01 -0800882 /**
883 * Pause all WebView timers using the WebView of the given tab
884 * @param tab
885 * @return true if the timers are paused or tab is null
886 */
887 private boolean pauseWebViewTimers(Tab tab) {
888 if (tab == null) {
889 return true;
890 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700891 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100892 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700894 }
Michael Kolb70976932010-11-30 11:34:01 -0800895 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 }
897
John Reck9c35b9c2012-05-30 10:08:50 -0700898 @Override
899 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800900 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700901 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
902 mUploadHandler = null;
903 }
904 if (mTabControl == null) return;
905 mUi.onDestroy();
906 // Remove the current tab and sub window
907 Tab t = mTabControl.getCurrentTab();
908 if (t != null) {
909 dismissSubWindow(t);
910 removeTab(t);
911 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500912 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700913 // Destroy all the tabs
914 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700915 // Unregister receiver
916 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700917 }
918
919 protected boolean isActivityPaused() {
920 return mActivityPaused;
921 }
922
John Reck9c35b9c2012-05-30 10:08:50 -0700923 @Override
924 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700925 mTabControl.freeMemory();
926 }
927
928 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 public void stopLoading() {
930 mLoadStopped = true;
931 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700932 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700933 if (w != null) {
934 w.stopLoading();
935 mUi.onPageStopped(tab);
936 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700937 }
938
939 boolean didUserStopLoading() {
940 return mLoadStopped;
941 }
942
kaiyiza016da12013-08-26 17:50:22 +0800943 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700944 final String reminderType = getContext().getResources().getString(
945 R.string.def_wifi_browser_interaction_remind_type);
946 final String selectionConnnection = getContext().getResources().getString(
947 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700948 final String wifiSelection = getContext().getResources().getString(
949 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700950
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700951 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
952 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700953 return;
954
kaiyiza016da12013-08-26 17:50:22 +0800955 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700956 Context.CONNECTIVITY_SERVICE);
957 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700958 WifiManager wifiMgr = (WifiManager) this.getContext()
959 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700960 if (networkInfo == null
961 || (networkInfo != null && (networkInfo.getType() !=
962 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700963 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
964 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700965 List<ScanResult> list = wifiMgr.getScanResults();
966 // Have no AP's for Wifi's fall back to data
967 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700968 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700969 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
970 this.getContext().startActivity(intent);
971 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700972 // Request to select Wifi AP
973 try {
974 Intent intent = new Intent(wifiSelection);
975 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
976 this.getContext().startActivity(intent);
977 } catch (Exception e) {
978 String err_msg = this.getContext().getString(
979 R.string.acivity_not_found, wifiSelection);
980 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
981 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700982 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700983 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800984 }
985 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700986
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 // WebViewController
988
989 @Override
John Reck324d4402011-01-11 16:56:42 -0800990 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700991
992 // We've started to load a new page. If there was a pending message
993 // to save a screenshot then we will now take the new page and save
994 // an incorrect screenshot. Therefore, remove any pending thumbnail
995 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700996 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700997
998 // reset sync timer to avoid sync starts during loading a page
999 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -07001000 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001001 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -08001002 boolean networkNotifier = BrowserConfig.getInstance(getContext())
1003 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001004 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -07001005 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +08001006 } else {
1007 if (!mNetworkHandler.isNetworkUp()) {
1008 view.setNetworkAvailable(false);
1009 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001010 }
1011
1012 // when BrowserActivity just starts, onPageStarted may be called before
1013 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
1014 // to start the timer. As we won't switch tabs while an activity is in
1015 // pause state, we can ensure calling resume and pause in pair.
1016 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -08001017 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 }
1019 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001020 endActionMode();
1021
John Reck30c714c2010-12-16 17:30:34 -08001022 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001023
John Reck324d4402011-01-11 16:56:42 -08001024 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001025 // update the bookmark database for favicon
1026 maybeUpdateFavicon(tab, null, url, favicon);
1027
1028 Performance.tracePageStart(url);
1029
1030 // Performance probe
1031 if (false) {
1032 Performance.onPageStarted();
1033 }
1034
1035 }
1036
1037 @Override
John Reck324d4402011-01-11 16:56:42 -08001038 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -07001039 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -08001040 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001041
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 // Performance probe
1043 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001044 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001045 }
1046
Tarun Nainani8eb00912014-07-17 12:28:32 -07001047 tab.onPageFinished();
1048
Michael Kolb8233fac2010-10-26 16:08:53 -07001049 Performance.tracePageFinished();
1050 }
1051
1052 @Override
John Reck30c714c2010-12-16 17:30:34 -08001053 public void onProgressChanged(Tab tab) {
1054 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001055
1056 if (newProgress == 100) {
1057 CookieSyncManager.getInstance().sync();
1058 // onProgressChanged() may continue to be called after the main
1059 // frame has finished loading, as any remaining sub frames continue
1060 // to load. We'll only get called once though with newProgress as
1061 // 100 when everything is loaded. (onPageFinished is called once
1062 // when the main frame completes loading regardless of the state of
1063 // any sub frames so calls to onProgressChanges may continue after
1064 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001065 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001066 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001067 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001068 } else if (mWasInPageLoad) {
1069 mWasInPageLoad = false;
1070 updateInLoadMenuItems(mCachedMenu, tab);
1071 }
1072
1073 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001074 // pause the WebView timer and release the wake lock if it is
1075 // finished while BrowserActivity is in pause state.
1076 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 }
John Reckd9862372012-02-21 15:04:50 -08001078 if (!tab.isPrivateBrowsingEnabled()
1079 && !TextUtils.isEmpty(tab.getUrl())
1080 && !tab.isSnapshot()) {
1081 // Only update the bookmark screenshot if the user did not
1082 // cancel the load early and there is not already
1083 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001084 if (tab.shouldUpdateThumbnail() &&
1085 (tab.inForeground() && !didUserStopLoading()
1086 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001087 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1088 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1089 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001090 1500);
John Reckd9862372012-02-21 15:04:50 -08001091 }
1092 }
1093 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001094 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001095 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001097 // still loading
1098 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001099 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001100 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001101 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001102 } else {
1103 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001104 }
1105 }
John Reck30c714c2010-12-16 17:30:34 -08001106 mUi.onProgressChanged(tab);
1107 }
1108
1109 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001110 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001111 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001112 }
1113
1114 @Override
1115 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001116 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001117 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001118 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1120 return;
1121 }
1122 // Update the title in the history database if not in private browsing mode
1123 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001124 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 }
1126 }
1127
1128 @Override
1129 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001130 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1132 }
1133
1134 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001135 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1136 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 }
1138
1139 @Override
1140 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1141 if (mMenuIsDown) {
1142 // only check shortcut key when MENU is held
1143 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1144 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001146 int keyCode = event.getKeyCode();
1147 // We need to send almost every key to WebKit. However:
1148 // 1. We don't want to block the device on the renderer for
1149 // some keys like menu, home, call.
1150 // 2. There are no WebKit equivalents for some of these keys
1151 // (see app/keyboard_codes_win.h)
1152 // Note that these are not the same set as KeyEvent.isSystemKey:
1153 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1154 if (keyCode == KeyEvent.KEYCODE_MENU ||
1155 keyCode == KeyEvent.KEYCODE_HOME ||
1156 keyCode == KeyEvent.KEYCODE_BACK ||
1157 keyCode == KeyEvent.KEYCODE_CALL ||
1158 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1159 keyCode == KeyEvent.KEYCODE_POWER ||
1160 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1161 keyCode == KeyEvent.KEYCODE_CAMERA ||
1162 keyCode == KeyEvent.KEYCODE_FOCUS ||
1163 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1164 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1165 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1166 return true;
1167 }
1168
1169 // We also have to intercept some shortcuts before we send them to the ContentView.
1170 if (event.isCtrlPressed() && (
1171 keyCode == KeyEvent.KEYCODE_TAB ||
1172 keyCode == KeyEvent.KEYCODE_W ||
1173 keyCode == KeyEvent.KEYCODE_F4)) {
1174 return true;
1175 }
1176
1177 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001178 }
1179
Tarun Nainanif5563b62015-01-21 18:52:59 -08001180 private void handleMediaKeyEvent(KeyEvent event) {
1181
1182 int keyCode = event.getKeyCode();
1183 // send media key events to audio manager
1184 if (Build.VERSION.SDK_INT >= 19) {
1185
1186 switch (keyCode) {
1187 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1188 case KeyEvent.KEYCODE_MEDIA_STOP:
1189 case KeyEvent.KEYCODE_MEDIA_NEXT:
1190 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1191 case KeyEvent.KEYCODE_MEDIA_REWIND:
1192 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1193 case KeyEvent.KEYCODE_MUTE:
1194 case KeyEvent.KEYCODE_MEDIA_PLAY:
1195 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1196 case KeyEvent.META_SHIFT_RIGHT_ON:
1197 case KeyEvent.KEYCODE_MEDIA_EJECT:
1198 case KeyEvent.KEYCODE_MEDIA_RECORD:
1199 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1200
1201 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1202 .getSystemService(Context.AUDIO_SERVICE);
1203 audioManager.dispatchMediaKeyEvent(event);
1204 }
1205 }
1206 }
1207
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 @Override
John Reck997b1b72012-04-19 18:08:25 -07001209 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001210 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001211 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001212 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001213 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001214 } else {
John Reck997b1b72012-04-19 18:08:25 -07001215 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001216 }
1217 }
John Reck997b1b72012-04-19 18:08:25 -07001218 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 }
1220
1221 @Override
John Reck324d4402011-01-11 16:56:42 -08001222 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001223 // Don't save anything in private browsing mode or when disabling history
1224 // for regular tabs is enabled
1225 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1226 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1227 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001228
John Reck49a603c2011-03-03 09:33:05 -08001229 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001230
John Reck324d4402011-01-11 16:56:42 -08001231 if (TextUtils.isEmpty(url)
1232 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001233 return;
1234 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001235
John Reckf57c0292011-07-21 18:15:39 -07001236 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001237 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001238 }
1239
1240 @Override
1241 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1242 AsyncTask<Void, Void, String[]> task =
1243 new AsyncTask<Void, Void, String[]>() {
1244 @Override
1245 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001246 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001247 }
1248 @Override
1249 public void onPostExecute(String[] result) {
1250 callback.onReceiveValue(result);
1251 }
1252 };
1253 task.execute();
1254 }
1255
1256 @Override
1257 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1258 final HttpAuthHandler handler, final String host,
1259 final String realm) {
1260 String username = null;
1261 String password = null;
1262
1263 boolean reuseHttpAuthUsernamePassword
1264 = handler.useHttpAuthUsernamePassword();
1265
1266 if (reuseHttpAuthUsernamePassword && view != null) {
1267 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1268 if (credentials != null && credentials.length == 2) {
1269 username = credentials[0];
1270 password = credentials[1];
1271 }
1272 }
1273
1274 if (username != null && password != null) {
1275 handler.proceed(username, password);
1276 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001277 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001278 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1279 } else {
1280 handler.cancel();
1281 }
1282 }
1283 }
1284
1285 @Override
1286 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001287 String contentDisposition, String mimetype, String referer,
1288 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001289 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001290 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001291 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001292 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001293 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001294 // This Tab was opened for the sole purpose of downloading a
1295 // file. Remove it.
1296 if (tab == mTabControl.getCurrentTab()) {
1297 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001298 if (tab.getDerivedFromIntent())
1299 closeTab(tab);
1300 else
1301 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001302 } else {
1303 // In this case, it is not.
1304 closeTab(tab);
1305 }
1306 }
1307 }
1308
1309 @Override
1310 public Bitmap getDefaultVideoPoster() {
1311 return mUi.getDefaultVideoPoster();
1312 }
1313
1314 @Override
1315 public View getVideoLoadingProgressView() {
1316 return mUi.getVideoLoadingProgressView();
1317 }
1318
1319 @Override
1320 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1321 SslError error) {
1322 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1323 }
1324
1325 // helper method
1326
1327 /*
1328 * Update the favorites icon if the private browsing isn't enabled and the
1329 * icon is valid.
1330 */
1331 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1332 final String url, Bitmap favicon) {
1333 if (favicon == null) {
1334 return;
1335 }
1336 if (!tab.isPrivateBrowsingEnabled()) {
1337 Bookmarks.updateFavicon(mActivity
1338 .getContentResolver(), originalUrl, url, favicon);
1339 }
1340 }
1341
Leon Scroggins4cd97792010-12-03 15:31:56 -05001342 @Override
1343 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001344 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001345 mUi.bookmarkedStatusHasChanged(tab);
1346 }
1347
Michael Kolb8233fac2010-10-26 16:08:53 -07001348 // end WebViewController
1349
1350 protected void pageUp() {
1351 getCurrentTopWebView().pageUp(false);
1352 }
1353
1354 protected void pageDown() {
1355 getCurrentTopWebView().pageDown(false);
1356 }
1357
1358 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001359 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 public void editUrl() {
1361 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001362 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001363 }
1364
John Reck9c35b9c2012-05-30 10:08:50 -07001365 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001366 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001367 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 if (tab.inForeground()) {
1369 if (mUi.isCustomViewShowing()) {
1370 callback.onCustomViewHidden();
1371 return;
1372 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001373 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001374 // Save the menu state and set it to empty while the custom
1375 // view is showing.
1376 mOldMenuState = mMenuState;
1377 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001378 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001379 }
1380 }
1381
1382 @Override
1383 public void hideCustomView() {
1384 if (mUi.isCustomViewShowing()) {
1385 mUi.onHideCustomView();
1386 // Reset the old menu state.
1387 mMenuState = mOldMenuState;
1388 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001389 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001390 }
1391 }
1392
John Reck9c35b9c2012-05-30 10:08:50 -07001393 @Override
1394 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001395 Intent intent) {
1396 if (getCurrentTopWebView() == null) return;
1397 switch (requestCode) {
1398 case PREFERENCES_PAGE:
1399 if (resultCode == Activity.RESULT_OK && intent != null) {
1400 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001401 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001402 mTabControl.removeParentChildRelationShips();
1403 }
1404 }
1405 break;
1406 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001407 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001408 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001411 case AUTOFILL_SETUP:
1412 // Determine whether a profile was actually set up or not
1413 // and if so, send the message back to the WebTextView to
1414 // fill the form with the new profile.
1415 if (getSettings().getAutoFillProfile() != null) {
1416 mAutoFillSetupMessage.sendToTarget();
1417 mAutoFillSetupMessage = null;
1418 }
1419 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001420 case COMBO_VIEW:
1421 if (intent == null || resultCode != Activity.RESULT_OK) {
1422 break;
1423 }
John Reck3ba45532011-08-11 16:26:53 -07001424 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001425 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1426 Tab t = getCurrentTab();
1427 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001428 mUpdateMyNavThumbnail = true;
1429 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001430 loadUrl(t, uri.toString());
1431 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1432 String[] urls = intent.getStringArrayExtra(
1433 ComboViewActivity.EXTRA_OPEN_ALL);
1434 Tab parent = getCurrentTab();
1435 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001436 if (url != null) {
1437 parent = openTab(url, parent,
1438 !mSettings.openInBackground(), true);
1439 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001440 }
1441 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1442 long id = intent.getLongExtra(
1443 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1444 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001445 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001446 }
1447 }
1448 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001449 case VOICE_RESULT:
1450 if (resultCode == Activity.RESULT_OK && intent != null) {
1451 ArrayList<String> results = intent.getStringArrayListExtra(
1452 RecognizerIntent.EXTRA_RESULTS);
1453 if (results.size() >= 1) {
1454 mVoiceResult = results.get(0);
1455 }
1456 }
1457 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001458 case MY_NAVIGATION:
1459 if (intent == null || resultCode != Activity.RESULT_OK) {
1460 break;
1461 }
1462
1463 if (intent.getBooleanExtra("need_refresh", false) &&
1464 getCurrentTopWebView() != null) {
1465 getCurrentTopWebView().reload();
1466 }
1467 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001468 default:
1469 break;
1470 }
1471 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001472 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001473 }
1474
1475 /**
1476 * Open the Go page.
1477 * @param startWithHistory If true, open starting on the history tab.
1478 * Otherwise, start with the bookmarks tab.
1479 */
1480 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001481 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001482 if (mTabControl.getCurrentWebView() == null) {
1483 return;
1484 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001485 // clear action mode
1486 if (isInCustomActionMode()) {
1487 endActionMode();
1488 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001489 Bundle extras = new Bundle();
1490 // Disable opening in a new window if we have maxed out the windows
1491 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1492 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001493 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001494 }
1495
1496 // combo view callbacks
1497
Michael Kolb8233fac2010-10-26 16:08:53 -07001498 // key handling
1499 protected void onBackKey() {
1500 if (!mUi.onBackKey()) {
1501 WebView subwindow = mTabControl.getCurrentSubWindow();
1502 if (subwindow != null) {
1503 if (subwindow.canGoBack()) {
1504 subwindow.goBack();
1505 } else {
1506 dismissSubWindow(mTabControl.getCurrentTab());
1507 }
1508 } else {
1509 goBackOnePageOrQuit();
1510 }
1511 }
1512 }
1513
Michael Kolb4bd767d2011-05-27 11:33:55 -07001514 protected boolean onMenuKey() {
1515 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001516 }
1517
Michael Kolb8233fac2010-10-26 16:08:53 -07001518 // menu handling and state
1519 // TODO: maybe put into separate handler
1520
John Reck9c35b9c2012-05-30 10:08:50 -07001521 @Override
1522 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001523 if (mMenuState == EMPTY_MENU) {
1524 return false;
1525 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001526 MenuInflater inflater = mActivity.getMenuInflater();
1527 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001528 return true;
1529 }
1530
John Reck9c35b9c2012-05-30 10:08:50 -07001531 @Override
1532 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001533 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001534 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001535 return;
1536 }
1537 if (!(v instanceof WebView)) {
1538 return;
1539 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001540 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001541 WebView.HitTestResult result;
1542
1543 /* Determine whether the ContextMenu got triggered because
1544 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1545 * received. The mResult acts as a flag to identify, how it got trigerred
1546 */
1547 if (mResult == null){
1548 result = webview.getHitTestResult();
1549 } else {
1550 result = mResult;
1551 }
1552
Michael Kolb8233fac2010-10-26 16:08:53 -07001553 if (result == null) {
1554 return;
1555 }
1556
1557 int type = result.getType();
1558 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001559
1560 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1561 unknownTypeMap.put("webview", webview);
1562 final Message msg = mHandler.obtainMessage(
1563 UNKNOWN_TYPE_MSG, unknownTypeMap);
1564 /* As defined in android developers guide
1565 * when UNKNOWN_TYPE is received as a result of HitTest
1566 * you need to determing the type by invoking requestFocusNodeHref
1567 */
1568 webview.requestFocusNodeHref(msg);
1569
Michael Kolb8233fac2010-10-26 16:08:53 -07001570 Log.w(LOGTAG,
1571 "We should not show context menu when nothing is touched");
1572 return;
1573 }
1574 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1575 // let TextView handles context menu
1576 return;
1577 }
1578
1579 // Note, http://b/issue?id=1106666 is requesting that
1580 // an inflated menu can be used again. This is not available
1581 // yet, so inflate each time (yuk!)
1582 MenuInflater inflater = mActivity.getMenuInflater();
1583 inflater.inflate(R.menu.browsercontext, menu);
1584
1585 // Show the correct menu group
1586 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001587 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001588 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 menu.setGroupVisible(R.id.PHONE_MENU,
1590 type == WebView.HitTestResult.PHONE_TYPE);
1591 menu.setGroupVisible(R.id.EMAIL_MENU,
1592 type == WebView.HitTestResult.EMAIL_TYPE);
1593 menu.setGroupVisible(R.id.GEO_MENU,
1594 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001595 String itemUrl = null;
1596 String url = webview.getOriginalUrl();
1597 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1598 itemUrl = Uri.decode(navigationUrl);
1599 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1600 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1601 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1602 } else {
1603 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1604 }
1605 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1606 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1607 } else {
1608 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1609
1610 menu.setGroupVisible(R.id.IMAGE_MENU,
1611 type == WebView.HitTestResult.IMAGE_TYPE
1612 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1613 menu.setGroupVisible(R.id.ANCHOR_MENU,
1614 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1615 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001616 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1617 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001618 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001619 // Setup custom handling depending on the type
1620 switch (type) {
1621 case WebView.HitTestResult.PHONE_TYPE:
1622 menu.setHeaderTitle(Uri.decode(extra));
1623 menu.findItem(R.id.dial_context_menu_id).setIntent(
1624 new Intent(Intent.ACTION_VIEW, Uri
1625 .parse(WebView.SCHEME_TEL + extra)));
1626 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1627 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1628 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1629 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1630 addIntent);
1631 menu.findItem(R.id.copy_phone_context_menu_id)
1632 .setOnMenuItemClickListener(
1633 new Copy(extra));
1634 break;
1635
1636 case WebView.HitTestResult.EMAIL_TYPE:
1637 menu.setHeaderTitle(extra);
1638 menu.findItem(R.id.email_context_menu_id).setIntent(
1639 new Intent(Intent.ACTION_VIEW, Uri
1640 .parse(WebView.SCHEME_MAILTO + extra)));
1641 menu.findItem(R.id.copy_mail_context_menu_id)
1642 .setOnMenuItemClickListener(
1643 new Copy(extra));
1644 break;
1645
1646 case WebView.HitTestResult.GEO_TYPE:
1647 menu.setHeaderTitle(extra);
1648 menu.findItem(R.id.map_context_menu_id).setIntent(
1649 new Intent(Intent.ACTION_VIEW, Uri
1650 .parse(WebView.SCHEME_GEO
1651 + URLEncoder.encode(extra))));
1652 menu.findItem(R.id.copy_geo_context_menu_id)
1653 .setOnMenuItemClickListener(
1654 new Copy(extra));
1655 break;
1656
1657 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1658 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001659 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001660 // decide whether to show the open link in new tab option
1661 boolean showNewTab = mTabControl.canCreateNewTab();
1662 MenuItem newTabItem
1663 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001664 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001665 ? R.string.contextmenu_openlink_newwindow_background
1666 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001667 newTabItem.setVisible(showNewTab);
1668 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001669 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1670 newTabItem.setOnMenuItemClickListener(
1671 new MenuItem.OnMenuItemClickListener() {
1672 @Override
1673 public boolean onMenuItemClick(MenuItem item) {
1674 final HashMap<String, WebView> hrefMap =
1675 new HashMap<String, WebView>();
1676 hrefMap.put("webview", webview);
1677 final Message msg = mHandler.obtainMessage(
1678 FOCUS_NODE_HREF,
1679 R.id.open_newtab_context_menu_id,
1680 0, hrefMap);
1681 webview.requestFocusNodeHref(msg);
1682 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001683 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001684 });
1685 } else {
1686 newTabItem.setOnMenuItemClickListener(
1687 new MenuItem.OnMenuItemClickListener() {
1688 @Override
1689 public boolean onMenuItemClick(MenuItem item) {
1690 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001691 openTab(extra, parent,
1692 !mSettings.openInBackground(),
1693 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001694 return true;
1695 }
1696 });
1697 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001698 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001699 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1700 menu.setHeaderTitle(navigationUrl);
1701 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1702
1703 if (itemUrl != null) {
1704 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1705 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1706 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1707 @Override
1708 public boolean onMenuItemClick(MenuItem item) {
1709 final Intent intent = new Intent(Controller.this
1710 .getContext(),
1711 AddMyNavigationPage.class);
1712 Bundle bundle = new Bundle();
1713 String url = Uri.decode(navigationUrl);
1714 bundle.putBoolean("isAdding", false);
1715 bundle.putString("url", url);
1716 bundle.putString("name", getNameFromUrl(url));
1717 intent.putExtra("websites", bundle);
1718 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1719 return false;
1720 }
1721 });
1722 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1723 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1724 @Override
1725 public boolean onMenuItemClick(MenuItem item) {
1726 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1727 return false;
1728 }
1729 });
1730 }
1731 } else {
1732 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1733 }
1734 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001735 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1736 break;
1737 }
1738 // otherwise fall through to handle image part
1739 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001740 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1741 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001742 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1743 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001744 shareItem.setOnMenuItemClickListener(
1745 new MenuItem.OnMenuItemClickListener() {
1746 @Override
1747 public boolean onMenuItemClick(MenuItem item) {
1748 sharePage(mActivity, null, extra, null,
1749 null);
1750 return true;
1751 }
1752 }
1753 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001754 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001755 menu.findItem(R.id.view_image_context_menu_id)
1756 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1757 @Override
1758 public boolean onMenuItemClick(MenuItem item) {
1759 openTab(extra, mTabControl.getCurrentTab(), true, true);
1760 return false;
1761 }
1762 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001763 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1764 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1765 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001766 menu.findItem(R.id.set_wallpaper_context_menu_id).
1767 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1768 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001769 break;
1770
1771 default:
1772 Log.w(LOGTAG, "We should not get here.");
1773 break;
1774 }
1775 //update the ui
1776 mUi.onContextMenuCreated(menu);
1777 }
1778
kaiyiz6e5b3e02013-08-19 20:02:01 +08001779 public void startAddMyNavigation(String url) {
1780 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1781 Bundle bundle = new Bundle();
1782 bundle.putBoolean("isAdding", true);
1783 bundle.putString("url", url);
1784 bundle.putString("name", getNameFromUrl(url));
1785 intent.putExtra("websites", bundle);
1786 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1787 }
1788
1789 private void showMyNavigationDeleteDialog(final String itemUrl) {
1790 new AlertDialog.Builder(this.getContext())
1791 .setTitle(R.string.my_navigation_delete_label)
1792 .setIcon(android.R.drawable.ic_dialog_alert)
1793 .setMessage(R.string.my_navigation_delete_msg)
1794 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1795 @Override
1796 public void onClick(DialogInterface dialog, int whichButton) {
1797 deleteMyNavigationItem(itemUrl);
1798 }
1799 })
1800 .setNegativeButton(R.string.cancel, null)
1801 .show();
1802 }
1803
1804 private void deleteMyNavigationItem(final String itemUrl) {
1805 ContentResolver cr = this.getContext().getContentResolver();
1806 Cursor cursor = null;
1807
1808 try {
1809 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1810 new String[] {
1811 MyNavigationUtil.ID
1812 }, "url = ?", new String[] {
1813 itemUrl
1814 }, null);
1815 if (null != cursor && cursor.moveToFirst()) {
1816 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1817 cursor.getLong(0));
1818
1819 ContentValues values = new ContentValues();
1820 values.put(MyNavigationUtil.TITLE, "");
1821 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1822 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1823 ByteArrayOutputStream os = new ByteArrayOutputStream();
1824 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1825 R.raw.my_navigation_add);
1826 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1827 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1828 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1829 cr.update(uri, values, null, null);
1830 } else {
1831 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1832 }
1833 } catch (IllegalStateException e) {
1834 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1835 } finally {
1836 if (null != cursor) {
1837 cursor.close();
1838 }
1839 }
1840
1841 if (getCurrentTopWebView() != null) {
1842 getCurrentTopWebView().reload();
1843 }
1844 }
1845
1846 private String getNameFromUrl(String itemUrl) {
1847 ContentResolver cr = this.getContext().getContentResolver();
1848 Cursor cursor = null;
1849 String name = null;
1850
1851 try {
1852 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1853 new String[] {
1854 MyNavigationUtil.TITLE
1855 }, "url = ?", new String[] {
1856 itemUrl
1857 }, null);
1858 if (null != cursor && cursor.moveToFirst()) {
1859 name = cursor.getString(0);
1860 } else {
1861 Log.e(LOGTAG, "this item does not exist!");
1862 }
1863 } catch (IllegalStateException e) {
1864 Log.e(LOGTAG, "getNameFromUrl", e);
1865 } finally {
1866 if (null != cursor) {
1867 cursor.close();
1868 }
1869 }
1870 return name;
1871 }
1872
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001873 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001874 final ContentResolver cr = mActivity.getContentResolver();
1875 new AsyncTask<Void, Void, Void>() {
1876 @Override
1877 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001878 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1879 if(!isMyNavigationUrl)
1880 return null;
1881
kaiyiz6e5b3e02013-08-19 20:02:01 +08001882 ContentResolver cr = mActivity.getContentResolver();
1883 Cursor cursor = null;
1884 try {
1885 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1886 new String[] {
1887 MyNavigationUtil.ID
1888 }, "url = ?", new String[] {
1889 itemUrl
1890 }, null);
1891 if (null != cursor && cursor.moveToFirst()) {
1892 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001893 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001894
1895 ContentValues values = new ContentValues();
1896 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1897 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1898 cursor.getLong(0));
1899 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1900 cr.update(uri, values, null, null);
1901 os.close();
1902 }
1903 } catch (IllegalStateException e) {
1904 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1905 } catch (IOException e) {
1906 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1907 } finally {
1908 if (null != cursor) {
1909 cursor.close();
1910 }
1911 }
1912 return null;
1913 }
1914 }.execute();
1915 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001916 /**
1917 * As the menu can be open when loading state changes
1918 * we must manually update the state of the stop/reload menu
1919 * item
1920 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001921 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001922 if (menu == null) {
1923 return;
1924 }
1925 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001926 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001927 menu.findItem(R.id.stop_menu_id):
1928 menu.findItem(R.id.reload_menu_id);
1929 if (src != null) {
1930 dest.setIcon(src.getIcon());
1931 dest.setTitle(src.getTitle());
1932 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001933 mActivity.invalidateOptionsMenu();
1934 }
1935
1936 public void invalidateOptionsMenu() {
1937 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 }
1939
John Reck9c35b9c2012-05-30 10:08:50 -07001940 @Override
1941 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001942 // Software menu key (toolbar key)
1943 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1944 return true;
1945 }
1946
1947 @Override
1948 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001949 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001950 // hold on to the menu reference here; it is used by the page callbacks
1951 // to update the menu based on loading state
1952 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001953 // Note: setVisible will decide whether an item is visible; while
1954 // setEnabled() will decide whether an item is enabled, which also means
1955 // whether the matching shortcut key will function.
1956 switch (mMenuState) {
1957 case EMPTY_MENU:
1958 if (mCurrentMenuState != mMenuState) {
1959 menu.setGroupVisible(R.id.MAIN_MENU, false);
1960 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1961 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1962 }
1963 break;
1964 default:
1965 if (mCurrentMenuState != mMenuState) {
1966 menu.setGroupVisible(R.id.MAIN_MENU, true);
1967 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1968 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1969 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001970 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001971 break;
1972 }
1973 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001974 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001975
1976 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001977 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1978 EditBookmarksRestriction.getInstance()
1979 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001980 }
1981
1982 private void setMenuItemVisibility(Menu menu, int id,
1983 boolean visibility) {
1984 MenuItem item = menu.findItem(id);
1985 if (item != null) {
1986 item.setVisible(visibility);
1987 }
1988 }
1989
1990 private int lookupBookmark(String title, String url) {
1991 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001992 int count = 0;
1993 Cursor cursor = null;
1994 try {
1995 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1996 BookmarksLoader.PROJECTION,
1997 "title = ? OR url = ?",
1998 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001999 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08002000 },
2001 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08002002
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08002003 if (cursor != null)
2004 count = cursor.getCount();
2005
2006 } catch (IllegalStateException e) {
2007 Log.e(LOGTAG, "lookupBookmark ", e);
2008 } finally {
2009 if (null != cursor) {
2010 cursor.close();
2011 }
Pankaj Garg49b79252014-11-07 17:33:41 -08002012 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08002013 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08002014 }
2015
2016 private void resetMenuItems(Menu menu) {
2017 setMenuItemVisibility(menu, R.id.history_menu_id, true);
2018 setMenuItemVisibility(menu, R.id.find_menu_id, true);
2019
2020 WebView w = getCurrentTopWebView();
2021 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
2022
2023 String title = w.getTitle();
2024 String url = w.getUrl();
2025 if (title != null && url != null && lookupBookmark(title, url) > 0) {
2026 bookmark_icon.setChecked(true);
2027 } else {
2028 bookmark_icon.setChecked(false);
2029 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002030 }
2031
Michael Kolb4bf79712011-07-14 14:18:12 -07002032 @Override
2033 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002034 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07002035 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07002036 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07002037 // Following flag is used to identify schemes for which the LIVE_MENU
2038 // items defined in res/menu/browser.xml should be enabled
2039 boolean isLiveScheme = false;
2040 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002041 boolean isSavable = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002042 resetMenuItems(menu);
2043
Michael Kolb4bf79712011-07-14 14:18:12 -07002044 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002045 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002046 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002047 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002048 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002049 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002050 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07002051 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002052
2053 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2054 forward.setEnabled(canGoForward);
2055
Michael Kolb4bf79712011-07-14 14:18:12 -07002056 // decide whether to show the share link option
2057 PackageManager pm = mActivity.getPackageManager();
2058 Intent send = new Intent(Intent.ACTION_SEND);
2059 send.setType("text/plain");
2060 ResolveInfo ri = pm.resolveActivity(send,
2061 PackageManager.MATCH_DEFAULT_ONLY);
2062 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2063
2064 boolean isNavDump = mSettings.enableNavDump();
2065 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2066 nav.setVisible(isNavDump);
2067 nav.setEnabled(isNavDump);
2068
2069 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002070 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2071 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002072 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002073 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2074 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002075 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002076 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2077 isLive && isLiveScheme && isPageFinished);
2078 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002079 isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002080 // history and snapshots item are the members of COMBO menu group,
2081 // so if show history item, only make snapshots item invisible.
2082 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002083
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002084 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002085 }
2086
John Reck9c35b9c2012-05-30 10:08:50 -07002087 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002088 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002089 if (null == getCurrentTopWebView()) {
2090 return false;
2091 }
2092 if (mMenuIsDown) {
2093 // The shortcut action consumes the MENU. Even if it is still down,
2094 // it won't trigger the next shortcut action. In the case of the
2095 // shortcut action triggering a new activity, like Bookmarks, we
2096 // won't get onKeyUp for MENU. So it is important to reset it here.
2097 mMenuIsDown = false;
2098 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002099 if (mUi.onOptionsItemSelected(item)) {
2100 // ui callback handled it
2101 return true;
2102 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002103 switch (item.getItemId()) {
2104 // -- Main menu
2105 case R.id.new_tab_menu_id:
2106 openTabToHomePage();
2107 break;
2108
2109 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002110 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002111 break;
2112
2113 case R.id.goto_menu_id:
2114 editUrl();
2115 break;
2116
2117 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002118 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2119 break;
2120
2121 case R.id.history_menu_id:
2122 bookmarksOrHistoryPicker(ComboViews.History);
2123 break;
2124
2125 case R.id.snapshots_menu_id:
2126 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002127 break;
2128
Pankaj Garg49b79252014-11-07 17:33:41 -08002129 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002130 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002131 break;
2132
2133 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002134 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002135 stopLoading();
2136 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002137 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002138 getCurrentTopWebView().reload();
2139 }
2140 break;
2141
Michael Kolb8233fac2010-10-26 16:08:53 -07002142 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002143 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002144 break;
2145
2146 case R.id.close_menu_id:
2147 // Close the subwindow if it exists.
2148 if (mTabControl.getCurrentSubWindow() != null) {
2149 dismissSubWindow(mTabControl.getCurrentTab());
2150 break;
2151 }
2152 closeCurrentTab();
2153 break;
2154
kaiyiza8b6dbb2013-07-29 18:11:22 +08002155 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002156 Object[] params = { new String("persist.debug.browsermonkeytest")};
2157 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002158 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002159 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002160 if (ret != null && ret.equals("enable"))
2161 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002162 if (BrowserConfig.getInstance(getContext())
2163 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2164 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002165 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002166 case R.id.homepage_menu_id:
2167 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002168 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002169 break;
2170
2171 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002172 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002173 break;
2174
2175 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002176 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002177 break;
2178
John Reck2bc80422011-06-30 15:11:49 -07002179 case R.id.save_snapshot_menu_id:
2180 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002181 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002182 createScreenshotAsync(
2183 source.getWebView(),
2184 getDesiredThumbnailWidth(mActivity),
2185 getDesiredThumbnailHeight(mActivity),
2186 new ValueCallback<Bitmap>() {
2187 @Override
2188 public void onReceiveValue(Bitmap bitmap) {
2189 new SaveSnapshotTask(source, bitmap).execute();
2190 }
2191 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002192 break;
2193
Michael Kolb8233fac2010-10-26 16:08:53 -07002194 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002195 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002196 break;
2197
John Recke1a03a32011-09-14 17:04:16 -07002198 case R.id.snapshot_go_live:
2199 goLive();
2200 return true;
2201
Michael Kolb8233fac2010-10-26 16:08:53 -07002202 case R.id.share_page_menu_id:
2203 Tab currentTab = mTabControl.getCurrentTab();
2204 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002205 return false;
2206 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002207 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002208 break;
2209
2210 case R.id.dump_nav_menu_id:
2211 getCurrentTopWebView().debugDump();
2212 break;
2213
Michael Kolb8233fac2010-10-26 16:08:53 -07002214 case R.id.zoom_in_menu_id:
2215 getCurrentTopWebView().zoomIn();
2216 break;
2217
2218 case R.id.zoom_out_menu_id:
2219 getCurrentTopWebView().zoomOut();
2220 break;
2221
2222 case R.id.view_downloads_menu_id:
2223 viewDownloads();
2224 break;
2225
John Reck42229bc2011-08-19 13:26:43 -07002226 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002227 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002228 break;
2229
Michael Kolb8233fac2010-10-26 16:08:53 -07002230 case R.id.window_one_menu_id:
2231 case R.id.window_two_menu_id:
2232 case R.id.window_three_menu_id:
2233 case R.id.window_four_menu_id:
2234 case R.id.window_five_menu_id:
2235 case R.id.window_six_menu_id:
2236 case R.id.window_seven_menu_id:
2237 case R.id.window_eight_menu_id:
2238 {
2239 int menuid = item.getItemId();
2240 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2241 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2242 Tab desiredTab = mTabControl.getTab(id);
2243 if (desiredTab != null &&
2244 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002245 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002246 }
2247 break;
2248 }
2249 }
2250 }
2251 break;
2252
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002253 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002254 Bundle bundle = new Bundle();
2255 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2256 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2257 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2258 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2259 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002260 break;
2261
Pankaj Garg49b79252014-11-07 17:33:41 -08002262 case R.id.add_to_homescreen:
2263 final WebView w = getCurrentTopWebView();
2264 final EditText input = new EditText(getContext());
2265 input.setText(w.getTitle());
2266 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002267 .setTitle(getContext().getResources().getString(
2268 R.string.add_to_homescreen))
2269 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002270 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002271 .setPositiveButton(getContext().getResources().getString(
2272 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002273 public void onClick(DialogInterface dialog, int whichButton) {
2274 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2275 getContext(),
2276 w.getUrl(),
2277 input.getText().toString(),
2278 w.getViewportBitmap(),
2279 w.getFavicon()));
2280
2281 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2282 .addCategory(Intent.CATEGORY_HOME));
2283 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002284 .setNegativeButton(getContext().getResources().getString(
2285 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002286 public void onClick(DialogInterface dialog, int whichButton) {
2287 // Do nothing.
2288 }
2289 })
2290 .show();
2291 break;
2292
Michael Kolb8233fac2010-10-26 16:08:53 -07002293 default:
2294 return false;
2295 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002296 return true;
2297 }
2298
John Reck68234a92012-04-19 15:27:12 -07002299 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2300 implements OnCancelListener {
2301
2302 private Tab mTab;
2303 private Dialog mProgressDialog;
2304 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002305 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002306
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002307 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002308 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002309 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002310 }
2311
2312 @Override
2313 protected void onPreExecute() {
2314 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2315 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2316 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002317 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002318 }
2319
2320 @Override
2321 protected Long doInBackground(Void... params) {
2322 if (!mTab.saveViewState(mValues)) {
2323 return null;
2324 }
2325 if (isCancelled()) {
2326 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2327 File file = mActivity.getFileStreamPath(path);
2328 if (!file.delete()) {
2329 file.deleteOnExit();
2330 }
2331 return null;
2332 }
2333 final ContentResolver cr = mActivity.getContentResolver();
2334 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2335 if (result == null) {
2336 return null;
2337 }
2338 long id = ContentUris.parseId(result);
2339 return id;
2340 }
2341
2342 @Override
2343 protected void onPostExecute(Long id) {
2344 if (isCancelled()) {
2345 return;
2346 }
2347 mProgressDialog.dismiss();
2348 if (id == null) {
2349 Toast.makeText(mActivity, R.string.snapshot_failed,
2350 Toast.LENGTH_SHORT).show();
2351 return;
2352 }
2353 Bundle b = new Bundle();
2354 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2355 mUi.showComboView(ComboViews.Snapshots, b);
2356 }
2357
2358 @Override
2359 public void onCancel(DialogInterface dialog) {
2360 cancel(true);
2361 }
2362 }
2363
Michael Kolb80f75082012-04-10 10:50:06 -07002364 @Override
2365 public void toggleUserAgent() {
2366 WebView web = getCurrentWebView();
2367 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002368 }
2369
2370 @Override
2371 public void findOnPage() {
2372 getCurrentTopWebView().showFindDialog(null, true);
2373 }
2374
2375 @Override
2376 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002377 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002378 }
2379
Pankaj Gargeba076f2015-03-25 13:40:59 -07002380 // This function is specifically used from AddBookmark Activity.
2381 // The bookmark activity clears the bitmap after retrieving it.
2382 // The function usage elsewhere will result in breaking bookmark
2383 // functionality.
2384 public static Bitmap getAndReleaseLastBookmarkBitmapFromIntent() {
2385 Bitmap bitmap = mBookmarkBitmap;
2386 mBookmarkBitmap = null;
2387 return bitmap;
2388 }
2389
Michael Kolb80f75082012-04-10 10:50:06 -07002390 @Override
2391 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002392 if(EditBookmarksRestriction.getInstance().isEnabled()) {
2393 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
2394 }
2395 else {
2396 WebView w = getCurrentTopWebView();
2397 if (w == null)
2398 return;
2399 final Intent i = createBookmarkCurrentPageIntent(false);
2400 createScreenshotAsync(
2401 w, getDesiredThumbnailWidth(mActivity),
2402 getDesiredThumbnailHeight(mActivity),
2403 new ValueCallback<Bitmap>() {
2404 @Override
2405 public void onReceiveValue(Bitmap bitmap) {
2406 mBookmarkBitmap = bitmap;
2407 mActivity.startActivity(i);
2408 }
2409 });
2410 }
Michael Kolb80f75082012-04-10 10:50:06 -07002411 }
2412
John Recke1a03a32011-09-14 17:04:16 -07002413 private void goLive() {
Axesh R. Ajmera89cf4d82015-05-19 11:26:18 -07002414 if (!getCurrentTab().isSnapshot()) return;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002415 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002416 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002417 boolean onlySingleTabRemaining = false;
2418 if (mTabControl.getTabCount() > 1) {
2419 // destroy the old snapshot tab
2420 closeCurrentTab();
2421 } else {
2422 onlySingleTabRemaining = true;
2423 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002424 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002425 if (onlySingleTabRemaining) {
2426 closeTab(t);
2427 }
2428
Tarun Nainani8eb00912014-07-17 12:28:32 -07002429 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002430 }
2431
luxiaolb40014b2013-07-19 10:01:43 +08002432 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002433 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002434 new AlertDialog.Builder(activity)
2435 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002436 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002437 .setMessage(R.string.exit_browser_msg)
2438 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2439 public void onClick(DialogInterface dialog, int which) {
2440 activity.moveTaskToBack(true);
2441 dialog.dismiss();
2442 }
2443 })
2444 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2445 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002446 mCrashRecoveryHandler.clearState(true);
2447 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002448 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002449 dialog.dismiss();
2450 }
2451 })
2452 .show();
2453 }
Michael Kolb315d5022011-10-13 12:47:11 -07002454 @Override
2455 public void showPageInfo() {
2456 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2457 }
2458
John Reck9c35b9c2012-05-30 10:08:50 -07002459 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002460 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002461 // Let the History and Bookmark fragments handle menus they created.
2462 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2463 return false;
2464 }
2465
Michael Kolb8233fac2010-10-26 16:08:53 -07002466 int id = item.getItemId();
2467 boolean result = true;
2468 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002469 // -- Browser context menu
2470 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002471 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002472 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002473 case R.id.copy_link_context_menu_id:
2474 final WebView webView = getCurrentTopWebView();
2475 if (null == webView) {
2476 result = false;
2477 break;
2478 }
2479 final HashMap<String, WebView> hrefMap =
2480 new HashMap<String, WebView>();
2481 hrefMap.put("webview", webView);
2482 final Message msg = mHandler.obtainMessage(
2483 FOCUS_NODE_HREF, id, 0, hrefMap);
2484 webView.requestFocusNodeHref(msg);
2485 break;
2486
2487 default:
2488 // For other context menus
2489 result = onOptionsItemSelected(item);
2490 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002491 return result;
2492 }
2493
2494 /**
2495 * support programmatically opening the context menu
2496 */
2497 public void openContextMenu(View view) {
2498 mActivity.openContextMenu(view);
2499 }
2500
2501 /**
2502 * programmatically open the options menu
2503 */
2504 public void openOptionsMenu() {
2505 mActivity.openOptionsMenu();
2506 }
2507
John Reck9c35b9c2012-05-30 10:08:50 -07002508 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002509 public boolean onMenuOpened(int featureId, Menu menu) {
2510 if (mOptionsMenuOpen) {
2511 if (mConfigChanged) {
2512 // We do not need to make any changes to the state of the
2513 // title bar, since the only thing that happened was a
2514 // change in orientation
2515 mConfigChanged = false;
2516 } else {
2517 if (!mExtendedMenuOpen) {
2518 mExtendedMenuOpen = true;
2519 mUi.onExtendedMenuOpened();
2520 } else {
2521 // Switching the menu back to icon view, so show the
2522 // title bar once again.
2523 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002524 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 }
2526 }
2527 } else {
2528 // The options menu is closed, so open it, and show the title
2529 mOptionsMenuOpen = true;
2530 mConfigChanged = false;
2531 mExtendedMenuOpen = false;
2532 mUi.onOptionsMenuOpened();
2533 }
2534 return true;
2535 }
2536
John Reck9c35b9c2012-05-30 10:08:50 -07002537 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002538 public void onOptionsMenuClosed(Menu menu) {
2539 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002540 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 }
2542
John Reck9c35b9c2012-05-30 10:08:50 -07002543 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002544 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002545 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002546 }
2547
2548 // Helper method for getting the top window.
2549 @Override
2550 public WebView getCurrentTopWebView() {
2551 return mTabControl.getCurrentTopWebView();
2552 }
2553
2554 @Override
2555 public WebView getCurrentWebView() {
2556 return mTabControl.getCurrentWebView();
2557 }
2558
2559 /*
2560 * This method is called as a result of the user selecting the options
2561 * menu to see the download window. It shows the download window on top of
2562 * the current window.
2563 */
2564 void viewDownloads() {
2565 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2566 mActivity.startActivity(intent);
2567 }
2568
John Reck30b065e2011-07-19 10:58:05 -07002569 int getActionModeHeight() {
2570 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2571 new int[] { android.R.attr.actionBarSize });
2572 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2573 actionBarSizeTypedArray.recycle();
2574 return size;
2575 }
2576
Michael Kolb8233fac2010-10-26 16:08:53 -07002577 // action mode
2578
John Reck9c35b9c2012-05-30 10:08:50 -07002579 @Override
2580 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002581 mUi.onActionModeStarted(mode);
2582 mActionMode = mode;
2583 }
2584
2585 /*
2586 * True if a custom ActionMode (i.e. find or select) is in use.
2587 */
2588 @Override
2589 public boolean isInCustomActionMode() {
2590 return mActionMode != null;
2591 }
2592
2593 /*
2594 * End the current ActionMode.
2595 */
2596 @Override
2597 public void endActionMode() {
2598 if (mActionMode != null) {
2599 mActionMode.finish();
2600 }
2601 }
2602
2603 /*
2604 * Called by find and select when they are finished. Replace title bars
2605 * as necessary.
2606 */
John Reck9c35b9c2012-05-30 10:08:50 -07002607 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 public void onActionModeFinished(ActionMode mode) {
2609 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002610 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002611 mActionMode = null;
2612 }
2613
2614 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002615 final Tab tab = getCurrentTab();
2616 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002617 }
2618
2619 // bookmark handling
2620
2621 /**
2622 * add the current page as a bookmark to the given folder id
2623 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002624 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002625 * bookmarked, and if it is, edit that bookmark. If false, and
2626 * the site is already bookmarked, do not attempt to edit the
2627 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002628 */
2629 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002630 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002631 WebView w = getCurrentTopWebView();
2632 if (w == null) {
2633 return null;
2634 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002635 Intent i = new Intent(mActivity,
2636 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002637 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2638 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2639 String touchIconUrl = w.getTouchIconUrl();
2640 if (touchIconUrl != null) {
2641 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2642 WebSettings settings = w.getSettings();
2643 if (settings != null) {
2644 i.putExtra(AddBookmarkPage.USER_AGENT,
2645 settings.getUserAgentString());
2646 }
2647 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002648 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002649 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002650 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002651 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2652 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002653 // Put the dialog at the upper right of the screen, covering the
2654 // star on the title bar.
2655 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002656 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002657 }
2658
2659 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002660 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002661 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002662 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002663 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002664 }
2665
Vivek Sekharb54614f2014-05-01 19:03:37 -07002666 @Override
2667 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2668 boolean capture) {
2669 mUploadHandler = new UploadHandler(this);
2670 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2671 }
2672
Michael Kolb8233fac2010-10-26 16:08:53 -07002673 // thumbnails
2674
2675 /**
2676 * Return the desired width for thumbnail screenshots, which are stored in
2677 * the database, and used on the bookmarks screen.
2678 * @param context Context for finding out the density of the screen.
2679 * @return desired width for thumbnail screenshot.
2680 */
2681 static int getDesiredThumbnailWidth(Context context) {
2682 return context.getResources().getDimensionPixelOffset(
2683 R.dimen.bookmarkThumbnailWidth);
2684 }
2685
2686 /**
2687 * Return the desired height for thumbnail screenshots, which are stored in
2688 * the database, and used on the bookmarks screen.
2689 * @param context Context for finding out the density of the screen.
2690 * @return desired height for thumbnail screenshot.
2691 */
2692 static int getDesiredThumbnailHeight(Context context) {
2693 return context.getResources().getDimensionPixelOffset(
2694 R.dimen.bookmarkThumbnailHeight);
2695 }
2696
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002697 static void createScreenshotAsync(WebView view, int width, int height,
2698 final ValueCallback<Bitmap> cb) {
2699 if (view == null || width == 0 || height == 0) {
2700 return;
2701 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002702 view.getContentBitmapAsync(
2703 (float) width / view.getWidth(),
2704 new Rect(),
2705 new ValueCallback<Bitmap>() {
2706 @Override
2707 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002708 if (bitmap != null)
2709 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2710 cb.onReceiveValue(bitmap);
2711 }});
2712 }
2713
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002714 private void updateScreenshot(final Tab tab) {
2715 createScreenshotAsync(
2716 tab.getWebView(),
2717 getDesiredThumbnailWidth(mActivity),
2718 getDesiredThumbnailHeight(mActivity),
2719 new ValueCallback<Bitmap>() {
2720 @Override
2721 public void onReceiveValue(Bitmap bitmap) {
2722 updateScreenshot(tab, bitmap);
2723 }
2724 });
2725 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002726
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002727 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002728 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002729 // FIXME: Would like to make sure there is actually something to
2730 // draw, but the API for that (WebViewCore.pictureReady()) is not
2731 // currently accessible here.
2732
John Reck34ef2672011-02-10 11:30:55 -08002733 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002734 if (view == null) {
2735 // Tab was destroyed
2736 return;
2737 }
John Reck34ef2672011-02-10 11:30:55 -08002738 final String url = tab.getUrl();
2739 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002740 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002741 if (TextUtils.isEmpty(url)) {
2742 return;
2743 }
2744
kaiyiz6e5b3e02013-08-19 20:02:01 +08002745 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002746 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002747 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002748 }
John Reck34ef2672011-02-10 11:30:55 -08002749 // Only update thumbnails for web urls (http(s)://), not for
2750 // about:, javascript:, data:, etc...
2751 // Unless it is a bookmarked site, then always update
2752 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2753 return;
2754 }
2755
kaiyiz6e5b3e02013-08-19 20:02:01 +08002756 if (url != null && mUpdateMyNavThumbnailUrl != null
2757 && Patterns.WEB_URL.matcher(url).matches()
2758 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2759 String urlHost = (new WebAddress(url)).getHost();
2760 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2761 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2762 || bookmarkHost.length() == 0) {
2763 return;
2764 }
2765 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2766 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2767 bookmarkHost.length());
2768 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2769 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2770 if (!bookmarkDomain.equals(urlDomain)) {
2771 return;
2772 }
2773 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002774 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002775 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2776 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2777 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2778 500);
2779 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002780 return;
2781 }
2782
2783 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002784 new AsyncTask<Void, Void, Void>() {
2785 @Override
2786 protected Void doInBackground(Void... unused) {
2787 Cursor cursor = null;
2788 try {
2789 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002790 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2791 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2792 : url);
2793 if (mUpdateMyNavThumbnail) {
2794 mUpdateMyNavThumbnail = false;
2795 mUpdateMyNavThumbnailUrl = null;
2796 }
John Reck34ef2672011-02-10 11:30:55 -08002797 if (cursor != null && cursor.moveToFirst()) {
2798 final ByteArrayOutputStream os =
2799 new ByteArrayOutputStream();
2800 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002801
John Reck34ef2672011-02-10 11:30:55 -08002802 ContentValues values = new ContentValues();
2803 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002804
John Reck34ef2672011-02-10 11:30:55 -08002805 do {
John Reck617fd832011-02-16 14:35:59 -08002806 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002807 cr.update(Images.CONTENT_URI, values, null, null);
2808 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002809 }
John Reck34ef2672011-02-10 11:30:55 -08002810 } catch (IllegalStateException e) {
2811 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002812 } catch (SQLiteException s) {
2813 // Added for possible error when user tries to remove the same bookmark
2814 // that is being updated with a screen shot
2815 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002816 } finally {
2817 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002818 }
John Reck34ef2672011-02-10 11:30:55 -08002819 return null;
2820 }
2821 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002822 }
2823
2824 private class Copy implements OnMenuItemClickListener {
2825 private CharSequence mText;
2826
John Reck9c35b9c2012-05-30 10:08:50 -07002827 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002828 public boolean onMenuItemClick(MenuItem item) {
2829 copy(mText);
2830 return true;
2831 }
2832
2833 public Copy(CharSequence toCopy) {
2834 mText = toCopy;
2835 }
2836 }
2837
Leon Scroggins63c02662010-11-18 15:16:27 -05002838 private static class Download implements OnMenuItemClickListener {
2839 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002840 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002841 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002842 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002843 private static final String FALLBACK_EXTENSION = "dat";
2844 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002845
John Reck9c35b9c2012-05-30 10:08:50 -07002846 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002847 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002848 if (DataUri.isDataUri(mText)) {
2849 saveDataUri();
2850 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002851 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002852 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002853 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002854 return true;
2855 }
2856
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002857 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2858 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002859 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002860 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002861 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002862 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002863 }
George Mount387d45d2011-10-07 15:57:53 -07002864
2865 /**
2866 * Treats mText as a data URI and writes its contents to a file
2867 * based on the current time.
2868 */
2869 private void saveDataUri() {
2870 FileOutputStream outputStream = null;
2871 try {
2872 DataUri uri = new DataUri(mText);
2873 File target = getTarget(uri);
2874 outputStream = new FileOutputStream(target);
2875 outputStream.write(uri.getData());
2876 final DownloadManager manager =
2877 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2878 manager.addCompletedDownload(target.getName(),
2879 mActivity.getTitle().toString(), false,
2880 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002881 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002882 } catch (IOException e) {
2883 Log.e(LOGTAG, "Could not save data URL");
2884 } finally {
2885 if (outputStream != null) {
2886 try {
2887 outputStream.close();
2888 } catch (IOException e) {
2889 // ignore close errors
2890 }
2891 }
2892 }
2893 }
2894
2895 /**
2896 * Creates a File based on the current time stamp and uses
2897 * the mime type of the DataUri to get the extension.
2898 */
2899 private File getTarget(DataUri uri) throws IOException {
2900 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002901 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002902 String nameBase = format.format(new Date());
2903 String mimeType = uri.getMimeType();
2904 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2905 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2906 if (extension == null) {
2907 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2908 extension = FALLBACK_EXTENSION;
2909 }
2910 extension = "." + extension; // createTempFile needs the '.'
2911 File targetFile = File.createTempFile(nameBase, extension, dir);
2912 return targetFile;
2913 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002914 }
2915
Cary Clark8974d282010-11-22 10:46:05 -05002916 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002917 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002918
John Reck9c35b9c2012-05-30 10:08:50 -07002919 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002920 public boolean onMenuItemClick(MenuItem item) {
2921 if (mWebView != null) {
2922 return mWebView.selectText();
2923 }
2924 return false;
2925 }
2926
2927 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002928 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002929 }
2930
2931 }
2932
Michael Kolb8233fac2010-10-26 16:08:53 -07002933 /********************** TODO: UI stuff *****************************/
2934
2935 // these methods have been copied, they still need to be cleaned up
2936
2937 /****************** tabs ***************************************************/
2938
2939 // basic tab interactions:
2940
2941 // it is assumed that tabcontrol already knows about the tab
2942 protected void addTab(Tab tab) {
2943 mUi.addTab(tab);
2944 }
2945
2946 protected void removeTab(Tab tab) {
2947 mUi.removeTab(tab);
2948 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002949 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002950 }
2951
Michael Kolbf2055602011-04-09 17:20:03 -07002952 @Override
2953 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002954 // monkey protection against delayed start
2955 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002956
2957 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2958 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002959 mTabControl.setCurrentTab(tab);
2960 // the tab is guaranteed to have a webview after setCurrentTab
2961 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002962
2963
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002964 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002965 //Purge active tabs
2966 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002967 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002968 }
2969
John Reck8bcafc12011-08-29 16:43:02 -07002970 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002971 Tab current = mTabControl.getCurrentTab();
2972 if (current != null
2973 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002974 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002975 }
2976 }
2977
John Reck26b18322011-06-21 13:08:58 -07002978 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002979 //Cancel navscreen request
2980 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002981 // Dismiss the subwindow if applicable.
2982 dismissSubWindow(appTab);
2983 // Since we might kill the WebView, remove it from the
2984 // content view first.
2985 mUi.detachTab(appTab);
2986 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002987 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002988 // TODO: analyze why the remove and add are necessary
2989 mUi.attachTab(appTab);
2990 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002991 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002992 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002993 } else {
2994 // If the tab was the current tab, we have to attach
2995 // it to the view system again.
2996 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002997 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 }
2999 }
3000
3001 // Remove the sub window if it exists. Also called by TabControl when the
3002 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07003003 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003004 public void dismissSubWindow(Tab tab) {
3005 removeSubWindow(tab);
3006 // dismiss the subwindow. This will destroy the WebView.
3007 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08003008 WebView wv = getCurrentTopWebView();
3009 if (wv != null) {
3010 wv.requestFocus();
3011 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003012 }
3013
3014 @Override
3015 public void removeSubWindow(Tab t) {
3016 if (t.getSubWebView() != null) {
3017 mUi.removeSubWindow(t.getSubViewContainer());
3018 }
3019 }
3020
3021 @Override
3022 public void attachSubWindow(Tab tab) {
3023 if (tab.getSubWebView() != null) {
3024 mUi.attachSubWindow(tab.getSubViewContainer());
3025 getCurrentTopWebView().requestFocus();
3026 }
3027 }
3028
Mathew Inwood29721c22011-06-29 17:55:24 +01003029 private Tab showPreloadedTab(final UrlData urlData) {
3030 if (!urlData.isPreloaded()) {
3031 return null;
3032 }
3033 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
3034 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
3035 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01003036 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01003037 // Could not submit query. Fallback to regular tab creation
3038 tabControl.destroy();
3039 return null;
3040 }
3041 }
Michael Kolbff6a7482011-07-26 16:37:15 -07003042 // check tab count and make room for new tab
3043 if (!mTabControl.canCreateNewTab()) {
3044 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
3045 if (leastUsed != null) {
3046 closeTab(leastUsed);
3047 }
3048 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003049 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01003050 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01003051 mTabControl.addPreloadedTab(t);
3052 addTab(t);
3053 setActiveTab(t);
3054 return t;
3055 }
3056
Michael Kolb7bcafde2011-05-09 13:55:59 -07003057 // open a non inconito tab with the given url data
3058 // and set as active tab
3059 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01003060 Tab tab = showPreloadedTab(urlData);
3061 if (tab == null) {
3062 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07003063 if ((tab != null) && !urlData.isEmpty()) {
3064 loadUrlDataIn(tab, urlData);
3065 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003066 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003067 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07003068 }
3069
Michael Kolb843510f2010-12-09 10:51:49 -08003070 @Override
3071 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003072 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07003073 }
3074
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07003076 public Tab openIncognitoTab() {
3077 return openTab(INCOGNITO_URI, true, true, false);
3078 }
3079
3080 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003081 public Tab openTab(String url, boolean incognito, boolean setActive,
3082 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003083 return openTab(url, incognito, setActive, useCurrent, null);
3084 }
3085
3086 @Override
3087 public Tab openTab(String url, Tab parent, boolean setActive,
3088 boolean useCurrent) {
3089 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3090 setActive, useCurrent, parent);
3091 }
3092
3093 public Tab openTab(String url, boolean incognito, boolean setActive,
3094 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003095 Tab tab = createNewTab(incognito, setActive, useCurrent);
3096 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003097 if (parent instanceof SnapshotTab) {
3098 addTab(tab);
3099 if (setActive)
3100 setActiveTab(tab);
3101 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003102 parent.addChildTab(tab);
3103 }
Michael Kolb519d2282011-05-09 17:03:19 -07003104 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003105 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003106 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003107 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003108 return tab;
3109 }
3110
3111 // this method will attempt to create a new tab
3112 // incognito: private browsing tab
3113 // setActive: ste tab as current tab
3114 // useCurrent: if no new tab can be created, return current tab
3115 private Tab createNewTab(boolean incognito, boolean setActive,
3116 boolean useCurrent) {
3117 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07003118 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
3119 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003120 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003121 if (mTabControl.canCreateNewTab()) {
3122 tab = mTabControl.createNewTab(incognito, !setActive);
3123 addTab(tab);
3124 if (setActive) {
3125 setActiveTab(tab);
3126 } else {
3127 tab.pause();
3128 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003129 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003130 if (useCurrent) {
3131 tab = mTabControl.getCurrentTab();
3132 reuseTab(tab, null);
3133 } else {
3134 mUi.showMaxTabsWarning();
3135 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003136 }
3137 }
3138 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003139 }
3140
John Reck2bc80422011-06-30 15:11:49 -07003141 @Override
3142 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3143 SnapshotTab tab = null;
3144 if (mTabControl.canCreateNewTab()) {
3145 tab = mTabControl.createSnapshotTab(snapshotId);
3146 addTab(tab);
3147 if (setActive) {
3148 setActiveTab(tab);
3149 }
3150 } else {
3151 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003152 }
3153 return tab;
3154 }
3155
Michael Kolb8233fac2010-10-26 16:08:53 -07003156 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003157 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003158 * @return boolean True if we successfully switched to a different tab. If
3159 * the indexth tab is null, or if that tab is the same as
3160 * the current one, return false.
3161 */
3162 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003163 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003164 Tab currentTab = mTabControl.getCurrentTab();
3165 if (tab == null || tab == currentTab) {
3166 return false;
3167 }
3168 setActiveTab(tab);
3169 return true;
3170 }
3171
3172 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003173 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003174 closeCurrentTab(false);
3175 }
3176
3177 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003178 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003179 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003180 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003181 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003182 return;
3183 }
Michael Kolbc831b632011-05-11 09:30:34 -07003184 final Tab current = mTabControl.getCurrentTab();
3185 final int pos = mTabControl.getCurrentPosition();
3186 Tab newTab = current.getParent();
3187 if (newTab == null) {
3188 newTab = mTabControl.getTab(pos + 1);
3189 if (newTab == null) {
3190 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003191 }
3192 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003193 if (andQuit) {
3194 mTabControl.setCurrentTab(newTab);
3195 closeTab(current);
3196 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003197 // Close window
3198 closeTab(current);
3199 }
3200 }
3201
3202 /**
3203 * Close the tab, remove its associated title bar, and adjust mTabControl's
3204 * current tab to a valid value.
3205 */
3206 @Override
3207 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003208 if (tab == mTabControl.getCurrentTab()) {
3209 closeCurrentTab();
3210 } else {
3211 removeTab(tab);
3212 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003213 }
3214
Afzal Najamd4e33312012-04-26 01:54:01 -04003215 /**
3216 * Close all tabs except the current one
3217 */
3218 @Override
3219 public void closeOtherTabs() {
3220 int inactiveTabs = mTabControl.getTabCount() - 1;
3221 for (int i = inactiveTabs; i >= 0; i--) {
3222 Tab tab = mTabControl.getTab(i);
3223 if (tab != mTabControl.getCurrentTab()) {
3224 removeTab(tab);
3225 }
3226 }
3227 }
3228
Michael Kolb8233fac2010-10-26 16:08:53 -07003229 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003230 protected void loadUrlFromContext(String url) {
3231 Tab tab = getCurrentTab();
3232 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003233 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003234 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003235 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003236 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003237 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003238 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003239 }
3240 }
3241 }
3242
3243 /**
3244 * Load the URL into the given WebView and update the title bar
3245 * to reflect the new load. Call this instead of WebView.loadUrl
3246 * directly.
3247 * @param view The WebView used to load url.
3248 * @param url The URL to load.
3249 */
John Reck71e51422011-07-01 16:49:28 -07003250 @Override
3251 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003252 loadUrl(tab, url, null);
3253 }
3254
3255 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3256 if (tab != null) {
3257 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003258 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003259 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003260 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003261 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003262 }
3263
3264 /**
3265 * Load UrlData into a Tab and update the title bar to reflect the new
3266 * load. Call this instead of UrlData.loadIn directly.
3267 * @param t The Tab used to load.
3268 * @param data The UrlData being loaded.
3269 */
3270 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003271 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003272 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003273 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003274 } else {
John Reck38b39652012-06-05 09:22:59 -07003275 if (t != null && data.mDisableUrlOverride) {
3276 t.disableUrlOverridingForLoad();
3277 }
John Reck26b18322011-06-21 13:08:58 -07003278 loadUrl(t, data.mUrl, data.mHeaders);
3279 }
3280 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003281 }
3282
John Reck30c714c2010-12-16 17:30:34 -08003283 @Override
3284 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003285 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003286 //In case of tab can go back (aka tab has navigation entry) do nothing
3287 //else just load homepage in current tab.
3288 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003289 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003290 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003291 }
3292
3293 void goBackOnePageOrQuit() {
3294 Tab current = mTabControl.getCurrentTab();
3295 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003296 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3297 showExitDialog(mActivity);
3298 } else {
3299 /*
3300 * Instead of finishing the activity, simply push this to the back
3301 * of the stack and let ActivityManager to choose the foreground
3302 * activity. As BrowserActivity is singleTask, it will be always the
3303 * root of the task. So we can use either true or false for
3304 * moveTaskToBack().
3305 */
3306 mActivity.moveTaskToBack(true);
3307 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003308 return;
3309 }
John Reckef654f12011-07-12 16:42:08 -07003310 if (current.canGoBack()) {
3311 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003312 } else {
3313 // Check to see if we are closing a window that was created by
3314 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003315 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003316 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003317 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003318 // Now we close the other tab
3319 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003320 } else if (BrowserConfig.getInstance(getContext())
3321 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3322 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003323 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003324 if ((current.getAppId() != null) || current.closeOnBack()) {
3325 closeCurrentTab(true);
3326 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003327 /*
3328 * Instead of finishing the activity, simply push this to the back
3329 * of the stack and let ActivityManager to choose the foreground
3330 * activity. As BrowserActivity is singleTask, it will be always the
3331 * root of the task. So we can use either true or false for
3332 * moveTaskToBack().
3333 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003334 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003335 }
3336 }
3337 }
3338
3339 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003340 * helper method for key handler
3341 * returns the current tab if it can't advance
3342 */
Michael Kolbc831b632011-05-11 09:30:34 -07003343 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003344 int pos = mTabControl.getCurrentPosition() + 1;
3345 if (pos >= mTabControl.getTabCount()) {
3346 pos = 0;
3347 }
3348 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003349 }
3350
3351 /**
3352 * helper method for key handler
3353 * returns the current tab if it can't advance
3354 */
Michael Kolbc831b632011-05-11 09:30:34 -07003355 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003356 int pos = mTabControl.getCurrentPosition() - 1;
3357 if ( pos < 0) {
3358 pos = mTabControl.getTabCount() - 1;
3359 }
3360 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003361 }
3362
John Reckbcef87f2012-02-03 14:58:34 -08003363 boolean isMenuOrCtrlKey(int keyCode) {
3364 return (KeyEvent.KEYCODE_MENU == keyCode)
3365 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3366 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3367 }
3368
Michael Kolb0035fad2011-03-14 13:25:28 -07003369 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003370 * handle key events in browser
3371 *
3372 * @param keyCode
3373 * @param event
3374 * @return true if handled, false to pass to super
3375 */
John Reck9c35b9c2012-05-30 10:08:50 -07003376 @Override
3377 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003378 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3379 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003380 if (!mUi.isComboViewShowing()) {
3381 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3382 true, false);
3383 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003384 return true;
3385 }
3386
Cary Clark160bbb92011-01-10 11:17:07 -05003387 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003388 // Even if MENU is already held down, we need to call to super to open
3389 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003390 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003391 mMenuIsDown = true;
3392 return false;
3393 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003394
Cary Clark8ff8c662010-12-29 15:03:05 -05003395 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003396 Tab tab = getCurrentTab();
3397 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003398
Cary Clark160bbb92011-01-10 11:17:07 -05003399 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3400 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003401
Michael Kolb8233fac2010-10-26 16:08:53 -07003402 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003403 case KeyEvent.KEYCODE_TAB:
3404 if (event.isCtrlPressed()) {
3405 if (event.isShiftPressed()) {
3406 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003407 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003408 } else {
3409 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003410 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003411 }
3412 return true;
3413 }
3414 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003415 case KeyEvent.KEYCODE_SPACE:
3416 // WebView/WebTextView handle the keys in the KeyDown. As
3417 // the Activity's shortcut keys are only handled when WebView
3418 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003419 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003420 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003421 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003422 pageDown();
3423 }
3424 return true;
3425 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003426 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003427 event.startTracking();
3428 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003429 case KeyEvent.KEYCODE_FORWARD:
3430 if (!noModifiers) break;
3431 tab.goForward();
3432 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003433 case KeyEvent.KEYCODE_DPAD_LEFT:
3434 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003435 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003436 return true;
3437 }
3438 break;
3439 case KeyEvent.KEYCODE_DPAD_RIGHT:
3440 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003441 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003442 return true;
3443 }
3444 break;
3445 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003446 if (ctrl) {
3447 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003448 return true;
3449 }
3450 break;
Michael Kolba4183062011-01-16 10:43:21 -08003451// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003452 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003453 if (ctrl ) {
3454 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003455 return true;
3456 }
3457 break;
Michael Kolba4183062011-01-16 10:43:21 -08003458// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003459// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003460// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003461// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003462// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003463// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003464// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003465// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003466// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003467// case KeyEvent.KEYCODE_M: // unused
3468// case KeyEvent.KEYCODE_N: // in Chrome: new window
3469// case KeyEvent.KEYCODE_O: // in Chrome: open file
3470// case KeyEvent.KEYCODE_P: // in Chrome: print page
3471// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003472// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003473// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3474 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003475 // we can't use the ctrl/shift flags, they check for
3476 // exclusive use of a modifier
3477 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003478 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003479 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003480 } else {
3481 openTabToHomePage();
3482 }
3483 return true;
3484 }
3485 break;
3486// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3487// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003488// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003489// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3490// case KeyEvent.KEYCODE_Y: // unused
3491// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003492 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003493 // it is a regular key and webview is not null
3494 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003495 }
3496
John Reck9c35b9c2012-05-30 10:08:50 -07003497 @Override
3498 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003499 switch(keyCode) {
3500 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003501 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003502 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003503 return true;
3504 }
3505 break;
3506 }
3507 return false;
3508 }
3509
John Reck9c35b9c2012-05-30 10:08:50 -07003510 @Override
3511 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003512 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003513 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003514 if (KeyEvent.KEYCODE_MENU == keyCode
3515 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003516 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003517 }
3518 }
Cary Clark160bbb92011-01-10 11:17:07 -05003519 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003520 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003521 case KeyEvent.KEYCODE_BACK:
3522 if (event.isTracking() && !event.isCanceled()) {
3523 onBackKey();
3524 return true;
3525 }
3526 break;
3527 }
3528 return false;
3529 }
3530
3531 public boolean isMenuDown() {
3532 return mMenuIsDown;
3533 }
3534
John Reck9c35b9c2012-05-30 10:08:50 -07003535 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003536 public void setupAutoFill(Message message) {
3537 // Open the settings activity at the AutoFill profile fragment so that
3538 // the user can create a new profile. When they return, we will dispatch
3539 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003540 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003541 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3542 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003543 }
John Reckb3417f02011-01-14 11:01:05 -08003544
John Reck9c35b9c2012-05-30 10:08:50 -07003545 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003546 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003547 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003548 return true;
3549 }
3550
John Reck1cf4b792011-07-26 10:22:22 -07003551 @Override
3552 public boolean shouldCaptureThumbnails() {
3553 return mUi.shouldCaptureThumbnails();
3554 }
3555
Michael Kolbc3af0672011-08-09 10:24:41 -07003556 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003557 public boolean supportsVoice() {
3558 PackageManager pm = mActivity.getPackageManager();
3559 List activities = pm.queryIntentActivities(new Intent(
3560 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3561 return activities.size() != 0;
3562 }
3563
3564 @Override
3565 public void startVoiceRecognizer() {
3566 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003567 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003568 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3569 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3570 mActivity.startActivityForResult(voice, VOICE_RESULT);
3571 }
3572
Pankaj Garg7b279f62014-08-12 14:47:18 -07003573 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003574 if (mLevel == level)
3575 return;
3576 mLevel = level;
3577 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003578 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3579 lp.dimAmount = level;
3580 mActivity.getWindow().setAttributes(lp);
3581 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3582 } else {
3583 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3584 }
3585 }
3586
Michael Kolb0b129122012-06-04 16:31:58 -07003587 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003588 public void setBlockEvents(boolean block) {
3589 mBlockEvents = block;
3590 }
3591
John Reck9c35b9c2012-05-30 10:08:50 -07003592 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003593 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003594 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003595 }
3596
John Reck9c35b9c2012-05-30 10:08:50 -07003597 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003598 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003599 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003600 }
3601
John Reck9c35b9c2012-05-30 10:08:50 -07003602 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003603 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003604 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003605 }
3606
John Reck9c35b9c2012-05-30 10:08:50 -07003607 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003608 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003609 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003610 }
3611
John Reck9c35b9c2012-05-30 10:08:50 -07003612 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003613 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003614 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003615 }
3616
Pankaj Garg49b79252014-11-07 17:33:41 -08003617 @Override
3618 public boolean shouldShowAppMenu() {
3619 return true;
3620 }
3621
3622 @Override
3623 public int getMenuThemeResourceId() {
3624 return R.style.OverflowMenuTheme;
3625 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003626}