blob: 652755bea3fab894b1dc7c20eac2dc911e951f19 [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
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800159 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700160
161 private static final int EMPTY_MENU = -1;
162
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700164 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700165 final static int PREFERENCES_PAGE = 3;
166 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000167 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700168 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800169 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000170
Michael Kolb8233fac2010-10-26 16:08:53 -0700171 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
172
173 // As the ids are dynamically created, we can't guarantee that they will
174 // be in sequence, so this static array maps ids to a window number.
175 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
176 { R.id.window_one_menu_id, R.id.window_two_menu_id,
177 R.id.window_three_menu_id, R.id.window_four_menu_id,
178 R.id.window_five_menu_id, R.id.window_six_menu_id,
179 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
180
181 // "source" parameter for Google search through search key
182 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
183 // "source" parameter for Google search through simplily type
184 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
185
George Mount387d45d2011-10-07 15:57:53 -0700186 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700187 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
188
John Reckd7dd9b22011-08-30 09:18:29 -0700189 // A bitmap that is re-used in createScreenshot as scratch space
190 private static Bitmap sThumbnailBitmap;
191
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 private Activity mActivity;
193 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700194 private HomepageHandler mHomepageHandler;
195 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 private BrowserSettings mSettings;
197 private WebViewFactory mFactory;
198
199 private WakeLock mWakeLock;
200
201 private UrlHandler mUrlHandler;
202 private UploadHandler mUploadHandler;
203 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 private PageDialogsHandler mPageDialogsHandler;
205 private NetworkStateHandler mNetworkHandler;
206
Ben Murdoch8029a772010-11-16 11:58:21 +0000207 private Message mAutoFillSetupMessage;
208
kaiyiza016da12013-08-26 17:50:22 +0800209 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700210
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 // FIXME, temp address onPrepareMenu performance problem.
212 // When we move everything out of view, we should rewrite this.
213 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700214 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 private int mOldMenuState = EMPTY_MENU;
216 private Menu mCachedMenu;
217
Michael Kolb8233fac2010-10-26 16:08:53 -0700218 private boolean mMenuIsDown;
219
Pankaj Garg49b79252014-11-07 17:33:41 -0800220 private boolean mWasInPageLoad = false;
221 private AppMenuHandler mAppMenuHandler;
222
Michael Kolb8233fac2010-10-26 16:08:53 -0700223 // For select and find, we keep track of the ActionMode so that
224 // finish() can be called as desired.
225 private ActionMode mActionMode;
226
227 /**
228 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
229 * of whether the configuration has changed. The first onMenuOpened call
230 * after a configuration change is simply a reopening of the same menu
231 * (i.e. mIconView did not change).
232 */
233 private boolean mConfigChanged;
234
235 /**
236 * Keeps track of whether the options menu is open. This is important in
237 * determining whether to show or hide the title bar overlay
238 */
239 private boolean mOptionsMenuOpen;
240
241 /**
242 * Whether or not the options menu is in its bigger, popup menu form. When
243 * true, we want the title bar overlay to be gone. When false, we do not.
244 * Only meaningful if mOptionsMenuOpen is true.
245 */
246 private boolean mExtendedMenuOpen;
247
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 private boolean mActivityPaused = true;
249 private boolean mLoadStopped;
250
251 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500252 // Checks to see when the bookmarks database has changed, and updates the
253 // Tabs' notion of whether they represent bookmarked sites.
254 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700255 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700256
Michael Kolbc3af0672011-08-09 10:24:41 -0700257 private boolean mBlockEvents;
258
Michael Kolb0b129122012-06-04 16:31:58 -0700259 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800260 private boolean mUpdateMyNavThumbnail;
261 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800262 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800263 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700264 private PowerConnectionReceiver mLowPowerReceiver;
265 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700266
George Mount3636d0a2011-11-21 09:08:21 -0800267 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 mActivity = browser;
269 mSettings = BrowserSettings.getInstance();
270 mTabControl = new TabControl(this);
271 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700272 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800273 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700274 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700275
276 mUrlHandler = new UrlHandler(this);
277 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
279
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500281 mBookmarksObserver = new ContentObserver(mHandler) {
282 @Override
283 public void onChange(boolean selfChange) {
284 int size = mTabControl.getTabCount();
285 for (int i = 0; i < size; i++) {
286 mTabControl.getTab(i).updateBookmarkedStatus();
287 }
288 }
289
290 };
291 browser.getContentResolver().registerContentObserver(
292 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700293
294 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700295 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800296 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700297
298 final WebRefiner refiner = WebRefiner.getInstance();
299 if (refiner != null) {
300 refiner.setRulesEnabled(WebRefiner.CATEGORY_ALL,
301 !PermissionsServiceFactory.getDefaultPermissions(
302 PermissionsServiceFactory.PermissionType.WEBREFINER));
303
304 PermissionsServiceFactory.getPermissionsService(
305 new ValueCallback<PermissionsServiceFactory.PermissionsService>() {
306 @Override
307 public void onReceiveValue(
308 PermissionsServiceFactory.PermissionsService value) {
309 Set<String> origins = value.getOrigins();
310 ArrayList<String> allowList = new ArrayList<>();
311 ArrayList<String> blockList = new ArrayList<>();
312 for (String origin : origins) {
313 PermissionsServiceFactory.PermissionsService.OriginInfo
314 info = value.getOriginInfo(origin);
315 int perm = info.getPermission(
316 PermissionsServiceFactory.PermissionType.WEBREFINER);
317 if (perm == PermissionsServiceFactory.Permission.ALLOW) {
318 allowList.add(origin);
319 } else if (perm == PermissionsServiceFactory.Permission.BLOCK) {
320 blockList.add(origin);
321 }
322 }
323 if (!allowList.isEmpty()) {
324 refiner.enableRulesForDomains(WebRefiner.CATEGORY_ALL,
325 allowList.toArray(new String[allowList.size()]));
326 }
327
328 if (!blockList.isEmpty()) {
329 refiner.disableRulesForDomains(WebRefiner.CATEGORY_ALL,
330 blockList.toArray(new String[blockList.size()]));
331 }
332 }
333 }
334 );
335 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700336 }
337
John Reck9c35b9c2012-05-30 10:08:50 -0700338 @Override
339 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700340 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800341 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800342 // mCrashRecoverHandler has any previously saved state.
343 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700344 }
345
George Mount3636d0a2011-11-21 09:08:21 -0800346 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800347 // we dont want to ever recover incognito tabs
348 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700349
Patrick Scott7d50a932011-02-04 09:27:26 -0500350 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700351 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500352 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000353
Michael Kolbc831b632011-05-11 09:30:34 -0700354 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500355 // Not able to restore so we go ahead and clear session cookies. We
356 // must do this before trying to login the user as we don't want to
357 // clear any session cookies set during login.
358 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700359 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800360 if (intent == null) {
361 // This won't happen under common scenarios. The icicle is
362 // not null, but there aren't any tabs to restore.
363 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700364 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800365 final Bundle extra = intent.getExtras();
366 // Create an initial tab.
367 // If the intent is ACTION_VIEW and data is not null, the Browser is
368 // invoked to view the content by another application. In this case,
369 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800370 UrlData urlData = null;
371 if (intent.getData() != null
372 && Intent.ACTION_VIEW.equals(intent.getAction())
373 && intent.getData().toString().startsWith("content://")) {
374 urlData = new UrlData(intent.getData().toString());
375 } else {
376 urlData = IntentHandler.getUrlDataFromIntent(intent);
377 }
George Mount3636d0a2011-11-21 09:08:21 -0800378 Tab t = null;
379 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700380 String landingPage = mActivity.getResources().getString(
381 R.string.def_landing_page);
382 if (!landingPage.isEmpty()) {
383 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800384 } else {
385 t = openTabToHomePage();
386 }
George Mount3636d0a2011-11-21 09:08:21 -0800387 } else {
388 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700389 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800390 }
391 if (t != null) {
392 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
393 }
394 WebView webView = t.getWebView();
395 if (extra != null) {
396 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
397 if (scale > 0 && scale <= 1000) {
398 webView.setInitialScale(scale);
399 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700400 }
401 }
John Reckd8c74522011-06-14 08:45:00 -0700402 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700403 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700404 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500405 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700406 List<Tab> tabs = mTabControl.getTabs();
407 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700408
John Reck1cf4b792011-07-26 10:22:22 -0700409 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700410 //handle restored pages that may require a JS interface
411 if (t.getWebView() != null) {
412 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
413 if (backForwardList != null) {
414 for (int i = 0; i < backForwardList.getSize(); i++) {
415 WebHistoryItem item = backForwardList.getItemAtIndex(i);
416 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
417 }
418 }
419 }
John Reck1cf4b792011-07-26 10:22:22 -0700420 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700421 if (t != mTabControl.getCurrentTab()) {
422 t.pause();
423 }
John Reck1cf4b792011-07-26 10:22:22 -0700424 }
425 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700426 if (tabs.size() == 0) {
427 openTabToHomePage();
428 }
John Reck1cf4b792011-07-26 10:22:22 -0700429 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700430 // TabControl.restoreState() will create a new tab even if
431 // restoring the state fails.
432 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800433 // Intent is non-null when framework thinks the browser should be
434 // launching with a new intent (icicle is null).
435 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700436 mIntentHandler.onNewIntent(intent);
437 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700438 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700439 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700440 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800441 if (jsFlags.trim().length() != 0) {
442 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700443 }
George Mount3636d0a2011-11-21 09:08:21 -0800444 if (intent != null
445 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700446 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800447 }
Site Maoabb7bd32015-03-20 15:34:02 -0700448 mLowPowerReceiver = new PowerConnectionReceiver();
449 mPowerChangeReceiver = new PowerConnectionReceiver();
450
451 //always track the android framework's power save mode
452 IntentFilter filter = new IntentFilter();
453 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
454 // Power save mode only exists in Lollipop and above
455 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
456 }
457 filter.addAction(Intent.ACTION_BATTERY_OKAY);
458 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700459 }
460
John Reck1cf4b792011-07-26 10:22:22 -0700461 private static class PruneThumbnails implements Runnable {
462 private Context mContext;
463 private List<Long> mIds;
464
465 PruneThumbnails(Context context, List<Long> preserveIds) {
466 mContext = context.getApplicationContext();
467 mIds = preserveIds;
468 }
469
470 @Override
471 public void run() {
472 ContentResolver cr = mContext.getContentResolver();
473 if (mIds == null || mIds.size() == 0) {
474 cr.delete(Thumbnails.CONTENT_URI, null, null);
475 } else {
476 int length = mIds.size();
477 StringBuilder where = new StringBuilder();
478 where.append(Thumbnails._ID);
479 where.append(" not in (");
480 for (int i = 0; i < length; i++) {
481 where.append(mIds.get(i));
482 if (i < (length - 1)) {
483 where.append(",");
484 }
485 }
486 where.append(")");
487 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
488 }
489 }
490
491 }
492
Michael Kolb1514bb72010-11-22 09:11:48 -0800493 @Override
494 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 return mFactory;
496 }
497
498 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800499 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700500 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700501 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800502 }
503
504 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800505 public void createSubWindow(Tab tab) {
506 endActionMode();
507 WebView mainView = tab.getWebView();
508 WebView subView = mFactory.createWebView((mainView == null)
509 ? false
510 : mainView.isPrivateBrowsingEnabled());
511 mUi.createSubWindow(tab, subView);
512 }
513
514 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700515 public Context getContext() {
516 return mActivity;
517 }
518
519 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700520 public Activity getActivity() {
521 return mActivity;
522 }
523
524 void setUi(UI ui) {
525 mUi = ui;
526 }
527
Michael Kolbaf63dba2012-05-16 12:58:05 -0700528 @Override
529 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 return mSettings;
531 }
532
533 IntentHandler getIntentHandler() {
534 return mIntentHandler;
535 }
536
537 @Override
538 public UI getUi() {
539 return mUi;
540 }
541
542 int getMaxTabs() {
543 return mActivity.getResources().getInteger(R.integer.max_tabs);
544 }
545
546 @Override
547 public TabControl getTabControl() {
548 return mTabControl;
549 }
550
Michael Kolb1bf23132010-11-19 12:55:12 -0800551 @Override
552 public List<Tab> getTabs() {
553 return mTabControl.getTabs();
554 }
555
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 private void startHandler() {
557 mHandler = new Handler() {
558
559 @Override
560 public void handleMessage(Message msg) {
561 switch (msg.what) {
562 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700563 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700564 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800565 case UNKNOWN_TYPE_MSG:
566 HashMap unknownTypeMap = (HashMap) msg.obj;
567 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
568 /*
569 * When the context menu is shown to the user
570 * we need to assure that its happening on the current webview
571 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
572 */
573 if (getCurrentWebView() != viewForUnknownType)
574 break;
575
576 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800577 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800578 WebView.HitTestResult result = new WebView.HitTestResult();
579
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800580 // Prevent unnecessary calls to context menu
581 // if url and image src are null
582 if (unknown_type_src == null && unknown_type_url == null)
583 break;
584
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800585 //setting the HitTestResult with new RESULT TYPE
586 if (!TextUtils.isEmpty(unknown_type_src)) {
587 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
588 result.setExtra(unknown_type_src);
589 } else {
590 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
591 result.setExtra("about:blank");
592 }
593
594 mResult = result;
595 openContextMenu(viewForUnknownType);
596 mResult = null;
597
598 break;
599
Michael Kolb8233fac2010-10-26 16:08:53 -0700600 case FOCUS_NODE_HREF:
601 {
602 String url = (String) msg.getData().get("url");
603 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500604 String src = (String) msg.getData().get("src");
605 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700606 if (TextUtils.isEmpty(url)) {
607 break;
608 }
609 HashMap focusNodeMap = (HashMap) msg.obj;
610 WebView view = (WebView) focusNodeMap.get("webview");
611 // Only apply the action if the top window did not change.
612 if (getCurrentTopWebView() != view) {
613 break;
614 }
615 switch (msg.arg1) {
616 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700617 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500619 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700620 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500621 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500622 case R.id.open_newtab_context_menu_id:
623 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700624 openTab(url, parent,
625 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500626 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700627 case R.id.copy_link_context_menu_id:
628 copy(url);
629 break;
630 case R.id.save_link_context_menu_id:
631 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500632 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800633 mActivity, url, view.getSettings().getUserAgentString(),
634 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700636 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700637 if(title == null || title == "")
638 title = url;
639
640 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
641 //SWE TODO: No thumbnail support for the url obtained via
642 //browser context menu as its not loaded in webview.
643 if (bookmarkIntent != null) {
644 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
645 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
646 mActivity.startActivity(bookmarkIntent);
647 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700648 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700649 }
650 break;
651 }
652
653 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700654 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 break;
656
657 case STOP_LOAD:
658 stopLoading();
659 break;
660
661 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700662 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 mWakeLock.release();
664 // if we reach here, Browser should be still in the
665 // background loading after WAKELOCK_TIMEOUT (5-min).
666 // To avoid burning the battery, stop loading.
667 mTabControl.stopAllLoading();
668 }
669 break;
670
kaiyiz591110b2013-08-06 17:11:06 +0800671 case OPEN_MENU:
672 if (!mOptionsMenuOpen && mActivity != null ) {
673 mActivity.openOptionsMenu();
674 }
675 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 }
677 }
678 };
679
680 }
681
John Reckef654f12011-07-12 16:42:08 -0700682 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200683 public Tab getCurrentTab() {
684 return mTabControl.getCurrentTab();
685 }
686
Michael Kolbba99c5d2010-11-29 14:57:41 -0800687 @Override
688 public void shareCurrentPage() {
689 shareCurrentPage(mTabControl.getCurrentTab());
690 }
691
692 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700693 if (tab == null || tab.getWebView() == null)
694 return;
695
696 final Tab mytab = tab;
697 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
698 @Override
699 public void onReceiveValue(Bitmap bitmap) {
700 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
701 mytab.getFavicon(), bitmap);
702 }
703 };
704
705 createScreenshotAsync(
706 tab.getWebView(),
707 getDesiredThumbnailWidth(mActivity),
708 getDesiredThumbnailHeight(mActivity),
709 new ValueCallback<Bitmap>() {
710 @Override
711 public void onReceiveValue(Bitmap bitmap) {
712 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
713 mytab.getFavicon(), bitmap);
714 }
715 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800716 }
717
Michael Kolb8233fac2010-10-26 16:08:53 -0700718 /**
719 * Share a page, providing the title, url, favicon, and a screenshot. Uses
720 * an {@link Intent} to launch the Activity chooser.
721 * @param c Context used to launch a new Activity.
722 * @param title Title of the page. Stored in the Intent with
723 * {@link Intent#EXTRA_SUBJECT}
724 * @param url URL of the page. Stored in the Intent with
725 * {@link Intent#EXTRA_TEXT}
726 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
727 * with {@link Browser#EXTRA_SHARE_FAVICON}
728 * @param screenshot Bitmap of a screenshot of the page. Stored in the
729 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
730 */
731 static final void sharePage(Context c, String title, String url,
732 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700733
734 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
735 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
736 R.layout.app_row, sDialog.getApps());
737 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700738 }
739
740 private void copy(CharSequence text) {
741 ClipboardManager cm = (ClipboardManager) mActivity
742 .getSystemService(Context.CLIPBOARD_SERVICE);
743 cm.setText(text);
744 }
745
746 // lifecycle
747
John Reck9c35b9c2012-05-30 10:08:50 -0700748 @Override
749 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700750 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800751 // update the menu in case of a locale change
752 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800753 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800754 if (mOptionsMenuOpen) {
755 mActivity.closeOptionsMenu();
756 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
757 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 if (mPageDialogsHandler != null) {
759 mPageDialogsHandler.onConfigurationChanged(config);
760 }
761 mUi.onConfigurationChanged(config);
762 }
763
764 @Override
765 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700766 if (!mUi.isWebShowing()) {
767 mUi.showWeb(false);
768 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700769 mIntentHandler.onNewIntent(intent);
770 }
771
John Reck9c35b9c2012-05-30 10:08:50 -0700772 @Override
773 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800774 if (mUi.isCustomViewShowing()) {
775 hideCustomView();
776 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700777 if (mActivityPaused) {
778 Log.e(LOGTAG, "BrowserActivity is already paused.");
779 return;
780 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700781 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800782 Tab tab = mTabControl.getCurrentTab();
783 if (tab != null) {
784 tab.pause();
785 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700786 if (mWakeLock == null) {
787 PowerManager pm = (PowerManager) mActivity
788 .getSystemService(Context.POWER_SERVICE);
789 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
790 }
Michael Kolb70976932010-11-30 11:34:01 -0800791 mWakeLock.acquire();
792 mHandler.sendMessageDelayed(mHandler
793 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
794 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 }
796 mUi.onPause();
797 mNetworkHandler.onPause();
798
799 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100800 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700801 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700802 }
803
John Reck9c35b9c2012-05-30 10:08:50 -0700804 @Override
805 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800807 Bundle saveState = createSaveState();
808
809 // crash recovery manages all save & restore state
810 mCrashRecoveryHandler.writeState(saveState);
811 mSettings.setLastRunPaused(true);
812 }
813
814 /**
815 * Save the current state to outState. Does not write the state to
816 * disk.
817 * @return Bundle containing the current state of all tabs.
818 */
819 /* package */ Bundle createSaveState() {
820 Bundle saveState = new Bundle();
821 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800822 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700823 }
824
John Reck9c35b9c2012-05-30 10:08:50 -0700825 @Override
826 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700827 if (!mActivityPaused) {
828 Log.e(LOGTAG, "BrowserActivity is already resumed.");
829 return;
830 }
George Mount3636d0a2011-11-21 09:08:21 -0800831 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800833 Tab current = mTabControl.getCurrentTab();
834 if (current != null) {
835 current.resume();
836 resumeWebViewTimers(current);
837 }
John Reckf57c0292011-07-21 18:15:39 -0700838 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200839
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 mUi.onResume();
841 mNetworkHandler.onResume();
842 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100843 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700844 if (mVoiceResult != null) {
845 mUi.onVoiceResult(mVoiceResult);
846 mVoiceResult = null;
847 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700848 if (current != null && current.getWebView().isShowingCrashView())
849 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700850 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700851 }
852
John Reckf57c0292011-07-21 18:15:39 -0700853 private void releaseWakeLock() {
854 if (mWakeLock != null && mWakeLock.isHeld()) {
855 mHandler.removeMessages(RELEASE_WAKELOCK);
856 mWakeLock.release();
857 }
858 }
859
Michael Kolb70976932010-11-30 11:34:01 -0800860 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800861 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800862 * @param tab guaranteed non-null
863 */
864 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 boolean inLoad = tab.inPageLoad();
866 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
867 CookieSyncManager.getInstance().startSync();
868 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100869 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700870 }
871 }
872
Michael Kolb70976932010-11-30 11:34:01 -0800873 /**
874 * Pause all WebView timers using the WebView of the given tab
875 * @param tab
876 * @return true if the timers are paused or tab is null
877 */
878 private boolean pauseWebViewTimers(Tab tab) {
879 if (tab == null) {
880 return true;
881 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100883 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 }
Michael Kolb70976932010-11-30 11:34:01 -0800886 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700887 }
888
John Reck9c35b9c2012-05-30 10:08:50 -0700889 @Override
890 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800891 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
893 mUploadHandler = null;
894 }
895 if (mTabControl == null) return;
896 mUi.onDestroy();
897 // Remove the current tab and sub window
898 Tab t = mTabControl.getCurrentTab();
899 if (t != null) {
900 dismissSubWindow(t);
901 removeTab(t);
902 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500903 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700904 // Destroy all the tabs
905 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700906 // Unregister receiver
907 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 }
909
910 protected boolean isActivityPaused() {
911 return mActivityPaused;
912 }
913
John Reck9c35b9c2012-05-30 10:08:50 -0700914 @Override
915 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 mTabControl.freeMemory();
917 }
918
919 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700920 public void stopLoading() {
921 mLoadStopped = true;
922 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700923 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700924 if (w != null) {
925 w.stopLoading();
926 mUi.onPageStopped(tab);
927 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 }
929
930 boolean didUserStopLoading() {
931 return mLoadStopped;
932 }
933
kaiyiza016da12013-08-26 17:50:22 +0800934 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700935 final String reminderType = getContext().getResources().getString(
936 R.string.def_wifi_browser_interaction_remind_type);
937 final String selectionConnnection = getContext().getResources().getString(
938 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700939 final String wifiSelection = getContext().getResources().getString(
940 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700941
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700942 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
943 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700944 return;
945
kaiyiza016da12013-08-26 17:50:22 +0800946 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700947 Context.CONNECTIVITY_SERVICE);
948 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700949 WifiManager wifiMgr = (WifiManager) this.getContext()
950 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700951 if (networkInfo == null
952 || (networkInfo != null && (networkInfo.getType() !=
953 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700954 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
955 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700956 List<ScanResult> list = wifiMgr.getScanResults();
957 // Have no AP's for Wifi's fall back to data
958 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700959 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700960 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
961 this.getContext().startActivity(intent);
962 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700963 // Request to select Wifi AP
964 try {
965 Intent intent = new Intent(wifiSelection);
966 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
967 this.getContext().startActivity(intent);
968 } catch (Exception e) {
969 String err_msg = this.getContext().getString(
970 R.string.acivity_not_found, wifiSelection);
971 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
972 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700973 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700974 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800975 }
976 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700977
Michael Kolb8233fac2010-10-26 16:08:53 -0700978 // WebViewController
979
980 @Override
John Reck324d4402011-01-11 16:56:42 -0800981 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700982
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 // reset sync timer to avoid sync starts during loading a page
984 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700985 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700986 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800987 boolean networkNotifier = BrowserConfig.getInstance(getContext())
988 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700989 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700990 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800991 } else {
992 if (!mNetworkHandler.isNetworkUp()) {
993 view.setNetworkAvailable(false);
994 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700995 }
996
997 // when BrowserActivity just starts, onPageStarted may be called before
998 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
999 // to start the timer. As we won't switch tabs while an activity is in
1000 // pause state, we can ensure calling resume and pause in pair.
1001 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -08001002 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 }
1004 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001005 endActionMode();
1006
John Reck30c714c2010-12-16 17:30:34 -08001007 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001008
John Reck324d4402011-01-11 16:56:42 -08001009 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001010 // update the bookmark database for favicon
1011 maybeUpdateFavicon(tab, null, url, favicon);
1012
1013 Performance.tracePageStart(url);
1014
1015 // Performance probe
1016 if (false) {
1017 Performance.onPageStarted();
1018 }
1019
1020 }
1021
1022 @Override
John Reck324d4402011-01-11 16:56:42 -08001023 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -07001024 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -08001025 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001026
Michael Kolb8233fac2010-10-26 16:08:53 -07001027 // Performance probe
1028 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001029 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001030 }
1031
Tarun Nainani8eb00912014-07-17 12:28:32 -07001032 tab.onPageFinished();
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001033 maybeUpdateFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -07001034
Michael Kolb8233fac2010-10-26 16:08:53 -07001035 Performance.tracePageFinished();
1036 }
1037
1038 @Override
John Reck30c714c2010-12-16 17:30:34 -08001039 public void onProgressChanged(Tab tab) {
1040 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001041
1042 if (newProgress == 100) {
1043 CookieSyncManager.getInstance().sync();
1044 // onProgressChanged() may continue to be called after the main
1045 // frame has finished loading, as any remaining sub frames continue
1046 // to load. We'll only get called once though with newProgress as
1047 // 100 when everything is loaded. (onPageFinished is called once
1048 // when the main frame completes loading regardless of the state of
1049 // any sub frames so calls to onProgressChanges may continue after
1050 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001051 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001052 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001053 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001054 } else if (mWasInPageLoad) {
1055 mWasInPageLoad = false;
1056 updateInLoadMenuItems(mCachedMenu, tab);
1057 }
1058
1059 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001060 // pause the WebView timer and release the wake lock if it is
1061 // finished while BrowserActivity is in pause state.
1062 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 }
1064 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001065 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001066 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001067 // still loading
1068 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001069 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001070 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001071 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001072 } else {
1073 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001074 }
1075 }
John Reck30c714c2010-12-16 17:30:34 -08001076 mUi.onProgressChanged(tab);
1077 }
1078
1079 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001080 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001081 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 }
1083
1084 @Override
1085 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001086 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001087 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001088 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001089 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1090 return;
1091 }
1092 // Update the title in the history database if not in private browsing mode
1093 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001094 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001095 }
1096 }
1097
1098 @Override
1099 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001100 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001101 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1102 }
1103
1104 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001105 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1106 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001107 }
1108
1109 @Override
1110 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1111 if (mMenuIsDown) {
1112 // only check shortcut key when MENU is held
1113 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1114 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001116 int keyCode = event.getKeyCode();
1117 // We need to send almost every key to WebKit. However:
1118 // 1. We don't want to block the device on the renderer for
1119 // some keys like menu, home, call.
1120 // 2. There are no WebKit equivalents for some of these keys
1121 // (see app/keyboard_codes_win.h)
1122 // Note that these are not the same set as KeyEvent.isSystemKey:
1123 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1124 if (keyCode == KeyEvent.KEYCODE_MENU ||
1125 keyCode == KeyEvent.KEYCODE_HOME ||
1126 keyCode == KeyEvent.KEYCODE_BACK ||
1127 keyCode == KeyEvent.KEYCODE_CALL ||
1128 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1129 keyCode == KeyEvent.KEYCODE_POWER ||
1130 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1131 keyCode == KeyEvent.KEYCODE_CAMERA ||
1132 keyCode == KeyEvent.KEYCODE_FOCUS ||
1133 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1134 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1135 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1136 return true;
1137 }
1138
1139 // We also have to intercept some shortcuts before we send them to the ContentView.
1140 if (event.isCtrlPressed() && (
1141 keyCode == KeyEvent.KEYCODE_TAB ||
1142 keyCode == KeyEvent.KEYCODE_W ||
1143 keyCode == KeyEvent.KEYCODE_F4)) {
1144 return true;
1145 }
1146
1147 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 }
1149
Tarun Nainanif5563b62015-01-21 18:52:59 -08001150 private void handleMediaKeyEvent(KeyEvent event) {
1151
1152 int keyCode = event.getKeyCode();
1153 // send media key events to audio manager
1154 if (Build.VERSION.SDK_INT >= 19) {
1155
1156 switch (keyCode) {
1157 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1158 case KeyEvent.KEYCODE_MEDIA_STOP:
1159 case KeyEvent.KEYCODE_MEDIA_NEXT:
1160 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1161 case KeyEvent.KEYCODE_MEDIA_REWIND:
1162 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1163 case KeyEvent.KEYCODE_MUTE:
1164 case KeyEvent.KEYCODE_MEDIA_PLAY:
1165 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1166 case KeyEvent.META_SHIFT_RIGHT_ON:
1167 case KeyEvent.KEYCODE_MEDIA_EJECT:
1168 case KeyEvent.KEYCODE_MEDIA_RECORD:
1169 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1170
1171 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1172 .getSystemService(Context.AUDIO_SERVICE);
1173 audioManager.dispatchMediaKeyEvent(event);
1174 }
1175 }
1176 }
1177
Michael Kolb8233fac2010-10-26 16:08:53 -07001178 @Override
John Reck997b1b72012-04-19 18:08:25 -07001179 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001181 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001182 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001183 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001184 } else {
John Reck997b1b72012-04-19 18:08:25 -07001185 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001186 }
1187 }
John Reck997b1b72012-04-19 18:08:25 -07001188 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001189 }
1190
1191 @Override
John Reck324d4402011-01-11 16:56:42 -08001192 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001193 // Don't save anything in private browsing mode or when disabling history
1194 // for regular tabs is enabled
1195 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1196 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1197 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001198
John Reck49a603c2011-03-03 09:33:05 -08001199 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001200
John Reck324d4402011-01-11 16:56:42 -08001201 if (TextUtils.isEmpty(url)
1202 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 return;
1204 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001205
John Reckf57c0292011-07-21 18:15:39 -07001206 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001207 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 }
1209
1210 @Override
1211 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1212 AsyncTask<Void, Void, String[]> task =
1213 new AsyncTask<Void, Void, String[]>() {
1214 @Override
1215 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001216 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001217 }
1218 @Override
1219 public void onPostExecute(String[] result) {
1220 callback.onReceiveValue(result);
1221 }
1222 };
1223 task.execute();
1224 }
1225
1226 @Override
1227 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1228 final HttpAuthHandler handler, final String host,
1229 final String realm) {
1230 String username = null;
1231 String password = null;
1232
1233 boolean reuseHttpAuthUsernamePassword
1234 = handler.useHttpAuthUsernamePassword();
1235
1236 if (reuseHttpAuthUsernamePassword && view != null) {
1237 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1238 if (credentials != null && credentials.length == 2) {
1239 username = credentials[0];
1240 password = credentials[1];
1241 }
1242 }
1243
1244 if (username != null && password != null) {
1245 handler.proceed(username, password);
1246 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001247 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001248 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1249 } else {
1250 handler.cancel();
1251 }
1252 }
1253 }
1254
1255 @Override
1256 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001257 String contentDisposition, String mimetype, String referer,
1258 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001259 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001260 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001261 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001262 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001263 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 // This Tab was opened for the sole purpose of downloading a
1265 // file. Remove it.
1266 if (tab == mTabControl.getCurrentTab()) {
1267 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001268 if (tab.getDerivedFromIntent())
1269 closeTab(tab);
1270 else
1271 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 } else {
1273 // In this case, it is not.
1274 closeTab(tab);
1275 }
1276 }
1277 }
1278
1279 @Override
1280 public Bitmap getDefaultVideoPoster() {
1281 return mUi.getDefaultVideoPoster();
1282 }
1283
1284 @Override
1285 public View getVideoLoadingProgressView() {
1286 return mUi.getVideoLoadingProgressView();
1287 }
1288
1289 @Override
1290 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1291 SslError error) {
1292 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1293 }
1294
1295 // helper method
1296
1297 /*
1298 * Update the favorites icon if the private browsing isn't enabled and the
1299 * icon is valid.
1300 */
1301 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1302 final String url, Bitmap favicon) {
1303 if (favicon == null) {
1304 return;
1305 }
1306 if (!tab.isPrivateBrowsingEnabled()) {
1307 Bookmarks.updateFavicon(mActivity
1308 .getContentResolver(), originalUrl, url, favicon);
1309 }
1310 }
1311
Leon Scroggins4cd97792010-12-03 15:31:56 -05001312 @Override
1313 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001314 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001315 mUi.bookmarkedStatusHasChanged(tab);
1316 }
1317
Michael Kolb8233fac2010-10-26 16:08:53 -07001318 // end WebViewController
1319
1320 protected void pageUp() {
1321 getCurrentTopWebView().pageUp(false);
1322 }
1323
1324 protected void pageDown() {
1325 getCurrentTopWebView().pageDown(false);
1326 }
1327
1328 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001329 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001330 public void editUrl() {
1331 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001332 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001333 }
1334
John Reck9c35b9c2012-05-30 10:08:50 -07001335 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001336 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001337 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001338 if (tab.inForeground()) {
1339 if (mUi.isCustomViewShowing()) {
1340 callback.onCustomViewHidden();
1341 return;
1342 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001343 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001344 // Save the menu state and set it to empty while the custom
1345 // view is showing.
1346 mOldMenuState = mMenuState;
1347 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001348 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001349 }
1350 }
1351
1352 @Override
1353 public void hideCustomView() {
1354 if (mUi.isCustomViewShowing()) {
1355 mUi.onHideCustomView();
1356 // Reset the old menu state.
1357 mMenuState = mOldMenuState;
1358 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001359 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 }
1361 }
1362
John Reck9c35b9c2012-05-30 10:08:50 -07001363 @Override
1364 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001365 Intent intent) {
1366 if (getCurrentTopWebView() == null) return;
1367 switch (requestCode) {
1368 case PREFERENCES_PAGE:
1369 if (resultCode == Activity.RESULT_OK && intent != null) {
1370 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001371 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001372 mTabControl.removeParentChildRelationShips();
1373 }
1374 }
1375 break;
1376 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001377 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001378 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001379 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001380 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001381 case AUTOFILL_SETUP:
1382 // Determine whether a profile was actually set up or not
1383 // and if so, send the message back to the WebTextView to
1384 // fill the form with the new profile.
1385 if (getSettings().getAutoFillProfile() != null) {
1386 mAutoFillSetupMessage.sendToTarget();
1387 mAutoFillSetupMessage = null;
1388 }
1389 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001390 case COMBO_VIEW:
1391 if (intent == null || resultCode != Activity.RESULT_OK) {
1392 break;
1393 }
John Reck3ba45532011-08-11 16:26:53 -07001394 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001395 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1396 Tab t = getCurrentTab();
1397 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001398 mUpdateMyNavThumbnail = true;
1399 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001400 loadUrl(t, uri.toString());
1401 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1402 String[] urls = intent.getStringArrayExtra(
1403 ComboViewActivity.EXTRA_OPEN_ALL);
1404 Tab parent = getCurrentTab();
1405 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001406 if (url != null) {
1407 parent = openTab(url, parent,
1408 !mSettings.openInBackground(), true);
1409 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001410 }
1411 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1412 long id = intent.getLongExtra(
1413 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1414 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001415 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001416 }
1417 }
1418 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001419 case VOICE_RESULT:
1420 if (resultCode == Activity.RESULT_OK && intent != null) {
1421 ArrayList<String> results = intent.getStringArrayListExtra(
1422 RecognizerIntent.EXTRA_RESULTS);
1423 if (results.size() >= 1) {
1424 mVoiceResult = results.get(0);
1425 }
1426 }
1427 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001428 case MY_NAVIGATION:
1429 if (intent == null || resultCode != Activity.RESULT_OK) {
1430 break;
1431 }
1432
1433 if (intent.getBooleanExtra("need_refresh", false) &&
1434 getCurrentTopWebView() != null) {
1435 getCurrentTopWebView().reload();
1436 }
1437 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001438 default:
1439 break;
1440 }
1441 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001442 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 }
1444
1445 /**
1446 * Open the Go page.
1447 * @param startWithHistory If true, open starting on the history tab.
1448 * Otherwise, start with the bookmarks tab.
1449 */
1450 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001451 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001452 if (mTabControl.getCurrentWebView() == null) {
1453 return;
1454 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001455 // clear action mode
1456 if (isInCustomActionMode()) {
1457 endActionMode();
1458 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001459 Bundle extras = new Bundle();
1460 // Disable opening in a new window if we have maxed out the windows
1461 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1462 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001463 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001464 }
1465
1466 // combo view callbacks
1467
Michael Kolb8233fac2010-10-26 16:08:53 -07001468 // key handling
1469 protected void onBackKey() {
1470 if (!mUi.onBackKey()) {
1471 WebView subwindow = mTabControl.getCurrentSubWindow();
1472 if (subwindow != null) {
1473 if (subwindow.canGoBack()) {
1474 subwindow.goBack();
1475 } else {
1476 dismissSubWindow(mTabControl.getCurrentTab());
1477 }
1478 } else {
1479 goBackOnePageOrQuit();
1480 }
1481 }
1482 }
1483
Michael Kolb4bd767d2011-05-27 11:33:55 -07001484 protected boolean onMenuKey() {
1485 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001486 }
1487
Michael Kolb8233fac2010-10-26 16:08:53 -07001488 // menu handling and state
1489 // TODO: maybe put into separate handler
1490
John Reck9c35b9c2012-05-30 10:08:50 -07001491 @Override
1492 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001493 if (mMenuState == EMPTY_MENU) {
1494 return false;
1495 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001496 MenuInflater inflater = mActivity.getMenuInflater();
1497 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001498 return true;
1499 }
1500
John Reck9c35b9c2012-05-30 10:08:50 -07001501 @Override
1502 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001503 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001504 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001505 return;
1506 }
1507 if (!(v instanceof WebView)) {
1508 return;
1509 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001510 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001511 WebView.HitTestResult result;
1512
1513 /* Determine whether the ContextMenu got triggered because
1514 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1515 * received. The mResult acts as a flag to identify, how it got trigerred
1516 */
1517 if (mResult == null){
1518 result = webview.getHitTestResult();
1519 } else {
1520 result = mResult;
1521 }
1522
Michael Kolb8233fac2010-10-26 16:08:53 -07001523 if (result == null) {
1524 return;
1525 }
1526
1527 int type = result.getType();
1528 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001529
1530 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1531 unknownTypeMap.put("webview", webview);
1532 final Message msg = mHandler.obtainMessage(
1533 UNKNOWN_TYPE_MSG, unknownTypeMap);
1534 /* As defined in android developers guide
1535 * when UNKNOWN_TYPE is received as a result of HitTest
1536 * you need to determing the type by invoking requestFocusNodeHref
1537 */
1538 webview.requestFocusNodeHref(msg);
1539
Michael Kolb8233fac2010-10-26 16:08:53 -07001540 Log.w(LOGTAG,
1541 "We should not show context menu when nothing is touched");
1542 return;
1543 }
1544 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1545 // let TextView handles context menu
1546 return;
1547 }
1548
1549 // Note, http://b/issue?id=1106666 is requesting that
1550 // an inflated menu can be used again. This is not available
1551 // yet, so inflate each time (yuk!)
1552 MenuInflater inflater = mActivity.getMenuInflater();
1553 inflater.inflate(R.menu.browsercontext, menu);
1554
1555 // Show the correct menu group
1556 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001557 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001558 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 menu.setGroupVisible(R.id.PHONE_MENU,
1560 type == WebView.HitTestResult.PHONE_TYPE);
1561 menu.setGroupVisible(R.id.EMAIL_MENU,
1562 type == WebView.HitTestResult.EMAIL_TYPE);
1563 menu.setGroupVisible(R.id.GEO_MENU,
1564 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001565 String itemUrl = null;
1566 String url = webview.getOriginalUrl();
1567 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1568 itemUrl = Uri.decode(navigationUrl);
1569 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1570 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1571 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1572 } else {
1573 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1574 }
1575 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1576 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1577 } else {
1578 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1579
1580 menu.setGroupVisible(R.id.IMAGE_MENU,
1581 type == WebView.HitTestResult.IMAGE_TYPE
1582 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1583 menu.setGroupVisible(R.id.ANCHOR_MENU,
1584 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1585 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001586 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1587 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001588 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 // Setup custom handling depending on the type
1590 switch (type) {
1591 case WebView.HitTestResult.PHONE_TYPE:
1592 menu.setHeaderTitle(Uri.decode(extra));
1593 menu.findItem(R.id.dial_context_menu_id).setIntent(
1594 new Intent(Intent.ACTION_VIEW, Uri
1595 .parse(WebView.SCHEME_TEL + extra)));
1596 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1597 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1598 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1599 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1600 addIntent);
1601 menu.findItem(R.id.copy_phone_context_menu_id)
1602 .setOnMenuItemClickListener(
1603 new Copy(extra));
1604 break;
1605
1606 case WebView.HitTestResult.EMAIL_TYPE:
1607 menu.setHeaderTitle(extra);
1608 menu.findItem(R.id.email_context_menu_id).setIntent(
1609 new Intent(Intent.ACTION_VIEW, Uri
1610 .parse(WebView.SCHEME_MAILTO + extra)));
1611 menu.findItem(R.id.copy_mail_context_menu_id)
1612 .setOnMenuItemClickListener(
1613 new Copy(extra));
1614 break;
1615
1616 case WebView.HitTestResult.GEO_TYPE:
1617 menu.setHeaderTitle(extra);
1618 menu.findItem(R.id.map_context_menu_id).setIntent(
1619 new Intent(Intent.ACTION_VIEW, Uri
1620 .parse(WebView.SCHEME_GEO
1621 + URLEncoder.encode(extra))));
1622 menu.findItem(R.id.copy_geo_context_menu_id)
1623 .setOnMenuItemClickListener(
1624 new Copy(extra));
1625 break;
1626
1627 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1628 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001629 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001630 // decide whether to show the open link in new tab option
1631 boolean showNewTab = mTabControl.canCreateNewTab();
1632 MenuItem newTabItem
1633 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001634 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001635 ? R.string.contextmenu_openlink_newwindow_background
1636 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001637 newTabItem.setVisible(showNewTab);
1638 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001639 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1640 newTabItem.setOnMenuItemClickListener(
1641 new MenuItem.OnMenuItemClickListener() {
1642 @Override
1643 public boolean onMenuItemClick(MenuItem item) {
1644 final HashMap<String, WebView> hrefMap =
1645 new HashMap<String, WebView>();
1646 hrefMap.put("webview", webview);
1647 final Message msg = mHandler.obtainMessage(
1648 FOCUS_NODE_HREF,
1649 R.id.open_newtab_context_menu_id,
1650 0, hrefMap);
1651 webview.requestFocusNodeHref(msg);
1652 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001653 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001654 });
1655 } else {
1656 newTabItem.setOnMenuItemClickListener(
1657 new MenuItem.OnMenuItemClickListener() {
1658 @Override
1659 public boolean onMenuItemClick(MenuItem item) {
1660 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001661 openTab(extra, parent,
1662 !mSettings.openInBackground(),
1663 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001664 return true;
1665 }
1666 });
1667 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001668 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001669 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1670 menu.setHeaderTitle(navigationUrl);
1671 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1672
1673 if (itemUrl != null) {
1674 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1675 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1676 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1677 @Override
1678 public boolean onMenuItemClick(MenuItem item) {
1679 final Intent intent = new Intent(Controller.this
1680 .getContext(),
1681 AddMyNavigationPage.class);
1682 Bundle bundle = new Bundle();
1683 String url = Uri.decode(navigationUrl);
1684 bundle.putBoolean("isAdding", false);
1685 bundle.putString("url", url);
1686 bundle.putString("name", getNameFromUrl(url));
1687 intent.putExtra("websites", bundle);
1688 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1689 return false;
1690 }
1691 });
1692 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1693 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1694 @Override
1695 public boolean onMenuItemClick(MenuItem item) {
1696 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1697 return false;
1698 }
1699 });
1700 }
1701 } else {
1702 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1703 }
1704 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001705 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1706 break;
1707 }
1708 // otherwise fall through to handle image part
1709 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001710 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1711 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001712 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1713 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001714 shareItem.setOnMenuItemClickListener(
1715 new MenuItem.OnMenuItemClickListener() {
1716 @Override
1717 public boolean onMenuItemClick(MenuItem item) {
1718 sharePage(mActivity, null, extra, null,
1719 null);
1720 return true;
1721 }
1722 }
1723 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001724 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001725 menu.findItem(R.id.view_image_context_menu_id)
1726 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1727 @Override
1728 public boolean onMenuItemClick(MenuItem item) {
1729 openTab(extra, mTabControl.getCurrentTab(), true, true);
1730 return false;
1731 }
1732 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001733 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1734 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1735 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001736 menu.findItem(R.id.set_wallpaper_context_menu_id).
1737 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1738 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001739 break;
1740
1741 default:
1742 Log.w(LOGTAG, "We should not get here.");
1743 break;
1744 }
1745 //update the ui
1746 mUi.onContextMenuCreated(menu);
1747 }
1748
kaiyiz6e5b3e02013-08-19 20:02:01 +08001749 public void startAddMyNavigation(String url) {
1750 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1751 Bundle bundle = new Bundle();
1752 bundle.putBoolean("isAdding", true);
1753 bundle.putString("url", url);
1754 bundle.putString("name", getNameFromUrl(url));
1755 intent.putExtra("websites", bundle);
1756 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1757 }
1758
1759 private void showMyNavigationDeleteDialog(final String itemUrl) {
1760 new AlertDialog.Builder(this.getContext())
1761 .setTitle(R.string.my_navigation_delete_label)
1762 .setIcon(android.R.drawable.ic_dialog_alert)
1763 .setMessage(R.string.my_navigation_delete_msg)
1764 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1765 @Override
1766 public void onClick(DialogInterface dialog, int whichButton) {
1767 deleteMyNavigationItem(itemUrl);
1768 }
1769 })
1770 .setNegativeButton(R.string.cancel, null)
1771 .show();
1772 }
1773
1774 private void deleteMyNavigationItem(final String itemUrl) {
1775 ContentResolver cr = this.getContext().getContentResolver();
1776 Cursor cursor = null;
1777
1778 try {
1779 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1780 new String[] {
1781 MyNavigationUtil.ID
1782 }, "url = ?", new String[] {
1783 itemUrl
1784 }, null);
1785 if (null != cursor && cursor.moveToFirst()) {
1786 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1787 cursor.getLong(0));
1788
1789 ContentValues values = new ContentValues();
1790 values.put(MyNavigationUtil.TITLE, "");
1791 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1792 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1793 ByteArrayOutputStream os = new ByteArrayOutputStream();
1794 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1795 R.raw.my_navigation_add);
1796 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1797 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1798 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1799 cr.update(uri, values, null, null);
1800 } else {
1801 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1802 }
1803 } catch (IllegalStateException e) {
1804 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1805 } finally {
1806 if (null != cursor) {
1807 cursor.close();
1808 }
1809 }
1810
1811 if (getCurrentTopWebView() != null) {
1812 getCurrentTopWebView().reload();
1813 }
1814 }
1815
1816 private String getNameFromUrl(String itemUrl) {
1817 ContentResolver cr = this.getContext().getContentResolver();
1818 Cursor cursor = null;
1819 String name = null;
1820
1821 try {
1822 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1823 new String[] {
1824 MyNavigationUtil.TITLE
1825 }, "url = ?", new String[] {
1826 itemUrl
1827 }, null);
1828 if (null != cursor && cursor.moveToFirst()) {
1829 name = cursor.getString(0);
1830 } else {
1831 Log.e(LOGTAG, "this item does not exist!");
1832 }
1833 } catch (IllegalStateException e) {
1834 Log.e(LOGTAG, "getNameFromUrl", e);
1835 } finally {
1836 if (null != cursor) {
1837 cursor.close();
1838 }
1839 }
1840 return name;
1841 }
1842
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001843 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001844 final ContentResolver cr = mActivity.getContentResolver();
1845 new AsyncTask<Void, Void, Void>() {
1846 @Override
1847 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001848 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1849 if(!isMyNavigationUrl)
1850 return null;
1851
kaiyiz6e5b3e02013-08-19 20:02:01 +08001852 ContentResolver cr = mActivity.getContentResolver();
1853 Cursor cursor = null;
1854 try {
1855 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1856 new String[] {
1857 MyNavigationUtil.ID
1858 }, "url = ?", new String[] {
1859 itemUrl
1860 }, null);
1861 if (null != cursor && cursor.moveToFirst()) {
1862 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001863 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001864
1865 ContentValues values = new ContentValues();
1866 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1867 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1868 cursor.getLong(0));
1869 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1870 cr.update(uri, values, null, null);
1871 os.close();
1872 }
1873 } catch (IllegalStateException e) {
1874 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1875 } catch (IOException e) {
1876 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1877 } finally {
1878 if (null != cursor) {
1879 cursor.close();
1880 }
1881 }
1882 return null;
1883 }
1884 }.execute();
1885 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001886 /**
1887 * As the menu can be open when loading state changes
1888 * we must manually update the state of the stop/reload menu
1889 * item
1890 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001891 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001892 if (menu == null) {
1893 return;
1894 }
1895 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001896 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 menu.findItem(R.id.stop_menu_id):
1898 menu.findItem(R.id.reload_menu_id);
1899 if (src != null) {
1900 dest.setIcon(src.getIcon());
1901 dest.setTitle(src.getTitle());
1902 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001903 mActivity.invalidateOptionsMenu();
1904 }
1905
1906 public void invalidateOptionsMenu() {
1907 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001908 }
1909
John Reck9c35b9c2012-05-30 10:08:50 -07001910 @Override
1911 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001912 // Software menu key (toolbar key)
1913 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1914 return true;
1915 }
1916
1917 @Override
1918 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001919 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001920 // hold on to the menu reference here; it is used by the page callbacks
1921 // to update the menu based on loading state
1922 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001923 // Note: setVisible will decide whether an item is visible; while
1924 // setEnabled() will decide whether an item is enabled, which also means
1925 // whether the matching shortcut key will function.
1926 switch (mMenuState) {
1927 case EMPTY_MENU:
1928 if (mCurrentMenuState != mMenuState) {
1929 menu.setGroupVisible(R.id.MAIN_MENU, false);
1930 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1931 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1932 }
1933 break;
1934 default:
1935 if (mCurrentMenuState != mMenuState) {
1936 menu.setGroupVisible(R.id.MAIN_MENU, true);
1937 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1938 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1939 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001940 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001941 break;
1942 }
1943 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001944 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001945
1946 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001947 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1948 EditBookmarksRestriction.getInstance()
1949 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001950 }
1951
1952 private void setMenuItemVisibility(Menu menu, int id,
1953 boolean visibility) {
1954 MenuItem item = menu.findItem(id);
1955 if (item != null) {
1956 item.setVisible(visibility);
1957 }
1958 }
1959
1960 private int lookupBookmark(String title, String url) {
1961 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001962 int count = 0;
1963 Cursor cursor = null;
1964 try {
1965 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1966 BookmarksLoader.PROJECTION,
1967 "title = ? OR url = ?",
1968 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001969 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001970 },
1971 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001972
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001973 if (cursor != null)
1974 count = cursor.getCount();
1975
1976 } catch (IllegalStateException e) {
1977 Log.e(LOGTAG, "lookupBookmark ", e);
1978 } finally {
1979 if (null != cursor) {
1980 cursor.close();
1981 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001982 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001983 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001984 }
1985
1986 private void resetMenuItems(Menu menu) {
1987 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1988 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1989
1990 WebView w = getCurrentTopWebView();
1991 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1992
1993 String title = w.getTitle();
1994 String url = w.getUrl();
1995 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1996 bookmark_icon.setChecked(true);
1997 } else {
1998 bookmark_icon.setChecked(false);
1999 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002000 }
2001
Michael Kolb4bf79712011-07-14 14:18:12 -07002002 @Override
2003 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002004 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07002005 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07002006 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07002007 // Following flag is used to identify schemes for which the LIVE_MENU
2008 // items defined in res/menu/browser.xml should be enabled
2009 boolean isLiveScheme = false;
2010 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002011 boolean isSavable = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002012 resetMenuItems(menu);
2013
Michael Kolb4bf79712011-07-14 14:18:12 -07002014 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002015 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002016 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002017 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002018 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002019 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002020 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07002021 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002022
2023 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2024 forward.setEnabled(canGoForward);
2025
Michael Kolb4bf79712011-07-14 14:18:12 -07002026 // decide whether to show the share link option
2027 PackageManager pm = mActivity.getPackageManager();
2028 Intent send = new Intent(Intent.ACTION_SEND);
2029 send.setType("text/plain");
2030 ResolveInfo ri = pm.resolveActivity(send,
2031 PackageManager.MATCH_DEFAULT_ONLY);
2032 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2033
2034 boolean isNavDump = mSettings.enableNavDump();
2035 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2036 nav.setVisible(isNavDump);
2037 nav.setEnabled(isNavDump);
2038
2039 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002040 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2041 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002042 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002043 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2044 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002045 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002046 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2047 isLive && isLiveScheme && isPageFinished);
2048 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002049 isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002050 // history and snapshots item are the members of COMBO menu group,
2051 // so if show history item, only make snapshots item invisible.
2052 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002053
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002054 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002055 }
2056
John Reck9c35b9c2012-05-30 10:08:50 -07002057 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002058 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002059 if (null == getCurrentTopWebView()) {
2060 return false;
2061 }
2062 if (mMenuIsDown) {
2063 // The shortcut action consumes the MENU. Even if it is still down,
2064 // it won't trigger the next shortcut action. In the case of the
2065 // shortcut action triggering a new activity, like Bookmarks, we
2066 // won't get onKeyUp for MENU. So it is important to reset it here.
2067 mMenuIsDown = false;
2068 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002069 if (mUi.onOptionsItemSelected(item)) {
2070 // ui callback handled it
2071 return true;
2072 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002073 switch (item.getItemId()) {
2074 // -- Main menu
2075 case R.id.new_tab_menu_id:
2076 openTabToHomePage();
2077 break;
2078
2079 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002080 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 break;
2082
2083 case R.id.goto_menu_id:
2084 editUrl();
2085 break;
2086
2087 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002088 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2089 break;
2090
2091 case R.id.history_menu_id:
2092 bookmarksOrHistoryPicker(ComboViews.History);
2093 break;
2094
2095 case R.id.snapshots_menu_id:
2096 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002097 break;
2098
Pankaj Garg49b79252014-11-07 17:33:41 -08002099 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002100 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002101 break;
2102
2103 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002104 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002105 stopLoading();
2106 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002107 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002108 getCurrentTopWebView().reload();
2109 }
2110 break;
2111
Michael Kolb8233fac2010-10-26 16:08:53 -07002112 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002113 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002114 break;
2115
2116 case R.id.close_menu_id:
2117 // Close the subwindow if it exists.
2118 if (mTabControl.getCurrentSubWindow() != null) {
2119 dismissSubWindow(mTabControl.getCurrentTab());
2120 break;
2121 }
2122 closeCurrentTab();
2123 break;
2124
kaiyiza8b6dbb2013-07-29 18:11:22 +08002125 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002126 Object[] params = { new String("persist.debug.browsermonkeytest")};
2127 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002128 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002129 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002130 if (ret != null && ret.equals("enable"))
2131 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002132 if (BrowserConfig.getInstance(getContext())
2133 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2134 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002135 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002136 case R.id.homepage_menu_id:
2137 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002138 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 break;
2140
2141 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002142 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002143 break;
2144
2145 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002146 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002147 break;
2148
John Reck2bc80422011-06-30 15:11:49 -07002149 case R.id.save_snapshot_menu_id:
2150 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002151 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002152 createScreenshotAsync(
2153 source.getWebView(),
2154 getDesiredThumbnailWidth(mActivity),
2155 getDesiredThumbnailHeight(mActivity),
2156 new ValueCallback<Bitmap>() {
2157 @Override
2158 public void onReceiveValue(Bitmap bitmap) {
2159 new SaveSnapshotTask(source, bitmap).execute();
2160 }
2161 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002162 break;
2163
Michael Kolb8233fac2010-10-26 16:08:53 -07002164 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002165 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002166 break;
2167
John Recke1a03a32011-09-14 17:04:16 -07002168 case R.id.snapshot_go_live:
2169 goLive();
2170 return true;
2171
Michael Kolb8233fac2010-10-26 16:08:53 -07002172 case R.id.share_page_menu_id:
2173 Tab currentTab = mTabControl.getCurrentTab();
2174 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002175 return false;
2176 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002177 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002178 break;
2179
2180 case R.id.dump_nav_menu_id:
2181 getCurrentTopWebView().debugDump();
2182 break;
2183
Michael Kolb8233fac2010-10-26 16:08:53 -07002184 case R.id.zoom_in_menu_id:
2185 getCurrentTopWebView().zoomIn();
2186 break;
2187
2188 case R.id.zoom_out_menu_id:
2189 getCurrentTopWebView().zoomOut();
2190 break;
2191
2192 case R.id.view_downloads_menu_id:
2193 viewDownloads();
2194 break;
2195
John Reck42229bc2011-08-19 13:26:43 -07002196 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002197 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002198 break;
2199
Michael Kolb8233fac2010-10-26 16:08:53 -07002200 case R.id.window_one_menu_id:
2201 case R.id.window_two_menu_id:
2202 case R.id.window_three_menu_id:
2203 case R.id.window_four_menu_id:
2204 case R.id.window_five_menu_id:
2205 case R.id.window_six_menu_id:
2206 case R.id.window_seven_menu_id:
2207 case R.id.window_eight_menu_id:
2208 {
2209 int menuid = item.getItemId();
2210 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2211 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2212 Tab desiredTab = mTabControl.getTab(id);
2213 if (desiredTab != null &&
2214 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002215 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002216 }
2217 break;
2218 }
2219 }
2220 }
2221 break;
2222
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002223 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002224 Bundle bundle = new Bundle();
2225 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2226 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2227 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2228 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2229 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002230 break;
2231
Pankaj Garg49b79252014-11-07 17:33:41 -08002232 case R.id.add_to_homescreen:
2233 final WebView w = getCurrentTopWebView();
2234 final EditText input = new EditText(getContext());
2235 input.setText(w.getTitle());
2236 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002237 .setTitle(getContext().getResources().getString(
2238 R.string.add_to_homescreen))
2239 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002240 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002241 .setPositiveButton(getContext().getResources().getString(
2242 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002243 public void onClick(DialogInterface dialog, int whichButton) {
2244 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2245 getContext(),
2246 w.getUrl(),
2247 input.getText().toString(),
2248 w.getViewportBitmap(),
2249 w.getFavicon()));
2250
2251 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2252 .addCategory(Intent.CATEGORY_HOME));
2253 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002254 .setNegativeButton(getContext().getResources().getString(
2255 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002256 public void onClick(DialogInterface dialog, int whichButton) {
2257 // Do nothing.
2258 }
2259 })
2260 .show();
2261 break;
2262
Michael Kolb8233fac2010-10-26 16:08:53 -07002263 default:
2264 return false;
2265 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002266 return true;
2267 }
2268
John Reck68234a92012-04-19 15:27:12 -07002269 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2270 implements OnCancelListener {
2271
2272 private Tab mTab;
2273 private Dialog mProgressDialog;
2274 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002275 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002276
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002277 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002278 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002279 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002280 }
2281
2282 @Override
2283 protected void onPreExecute() {
2284 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2285 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2286 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002287 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002288 }
2289
2290 @Override
2291 protected Long doInBackground(Void... params) {
2292 if (!mTab.saveViewState(mValues)) {
2293 return null;
2294 }
2295 if (isCancelled()) {
2296 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2297 File file = mActivity.getFileStreamPath(path);
2298 if (!file.delete()) {
2299 file.deleteOnExit();
2300 }
2301 return null;
2302 }
2303 final ContentResolver cr = mActivity.getContentResolver();
2304 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2305 if (result == null) {
2306 return null;
2307 }
2308 long id = ContentUris.parseId(result);
2309 return id;
2310 }
2311
2312 @Override
2313 protected void onPostExecute(Long id) {
2314 if (isCancelled()) {
2315 return;
2316 }
2317 mProgressDialog.dismiss();
2318 if (id == null) {
2319 Toast.makeText(mActivity, R.string.snapshot_failed,
2320 Toast.LENGTH_SHORT).show();
2321 return;
2322 }
2323 Bundle b = new Bundle();
2324 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2325 mUi.showComboView(ComboViews.Snapshots, b);
2326 }
2327
2328 @Override
2329 public void onCancel(DialogInterface dialog) {
2330 cancel(true);
2331 }
2332 }
2333
Michael Kolb80f75082012-04-10 10:50:06 -07002334 @Override
2335 public void toggleUserAgent() {
2336 WebView web = getCurrentWebView();
2337 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002338 }
2339
2340 @Override
2341 public void findOnPage() {
2342 getCurrentTopWebView().showFindDialog(null, true);
2343 }
2344
2345 @Override
2346 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002347 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002348 }
2349
2350 @Override
2351 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002352 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002353 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2354 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002355 }
2356 else {
2357 WebView w = getCurrentTopWebView();
2358 if (w == null)
2359 return;
2360 final Intent i = createBookmarkCurrentPageIntent(false);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002361 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002362 }
Michael Kolb80f75082012-04-10 10:50:06 -07002363 }
2364
John Recke1a03a32011-09-14 17:04:16 -07002365 private void goLive() {
Axesh R. Ajmera89cf4d82015-05-19 11:26:18 -07002366 if (!getCurrentTab().isSnapshot()) return;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002367 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002368 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002369 boolean onlySingleTabRemaining = false;
2370 if (mTabControl.getTabCount() > 1) {
2371 // destroy the old snapshot tab
2372 closeCurrentTab();
2373 } else {
2374 onlySingleTabRemaining = true;
2375 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002376 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002377 if (onlySingleTabRemaining) {
2378 closeTab(t);
2379 }
2380
Tarun Nainani8eb00912014-07-17 12:28:32 -07002381 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002382 }
2383
luxiaolb40014b2013-07-19 10:01:43 +08002384 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002385 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002386 new AlertDialog.Builder(activity)
2387 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002388 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002389 .setMessage(R.string.exit_browser_msg)
2390 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2391 public void onClick(DialogInterface dialog, int which) {
2392 activity.moveTaskToBack(true);
2393 dialog.dismiss();
2394 }
2395 })
2396 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2397 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002398 mCrashRecoveryHandler.clearState(true);
2399 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002400 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002401 dialog.dismiss();
2402 }
2403 })
2404 .show();
2405 }
Michael Kolb315d5022011-10-13 12:47:11 -07002406 @Override
2407 public void showPageInfo() {
2408 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2409 }
2410
John Reck9c35b9c2012-05-30 10:08:50 -07002411 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002412 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002413 // Let the History and Bookmark fragments handle menus they created.
2414 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2415 return false;
2416 }
2417
Michael Kolb8233fac2010-10-26 16:08:53 -07002418 int id = item.getItemId();
2419 boolean result = true;
2420 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002421 // -- Browser context menu
2422 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002423 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002424 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002425 case R.id.copy_link_context_menu_id:
2426 final WebView webView = getCurrentTopWebView();
2427 if (null == webView) {
2428 result = false;
2429 break;
2430 }
2431 final HashMap<String, WebView> hrefMap =
2432 new HashMap<String, WebView>();
2433 hrefMap.put("webview", webView);
2434 final Message msg = mHandler.obtainMessage(
2435 FOCUS_NODE_HREF, id, 0, hrefMap);
2436 webView.requestFocusNodeHref(msg);
2437 break;
2438
2439 default:
2440 // For other context menus
2441 result = onOptionsItemSelected(item);
2442 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002443 return result;
2444 }
2445
2446 /**
2447 * support programmatically opening the context menu
2448 */
2449 public void openContextMenu(View view) {
2450 mActivity.openContextMenu(view);
2451 }
2452
2453 /**
2454 * programmatically open the options menu
2455 */
2456 public void openOptionsMenu() {
2457 mActivity.openOptionsMenu();
2458 }
2459
John Reck9c35b9c2012-05-30 10:08:50 -07002460 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002461 public boolean onMenuOpened(int featureId, Menu menu) {
2462 if (mOptionsMenuOpen) {
2463 if (mConfigChanged) {
2464 // We do not need to make any changes to the state of the
2465 // title bar, since the only thing that happened was a
2466 // change in orientation
2467 mConfigChanged = false;
2468 } else {
2469 if (!mExtendedMenuOpen) {
2470 mExtendedMenuOpen = true;
2471 mUi.onExtendedMenuOpened();
2472 } else {
2473 // Switching the menu back to icon view, so show the
2474 // title bar once again.
2475 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002476 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002477 }
2478 }
2479 } else {
2480 // The options menu is closed, so open it, and show the title
2481 mOptionsMenuOpen = true;
2482 mConfigChanged = false;
2483 mExtendedMenuOpen = false;
2484 mUi.onOptionsMenuOpened();
2485 }
2486 return true;
2487 }
2488
John Reck9c35b9c2012-05-30 10:08:50 -07002489 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002490 public void onOptionsMenuClosed(Menu menu) {
2491 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002492 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002493 }
2494
John Reck9c35b9c2012-05-30 10:08:50 -07002495 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002496 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002497 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002498 }
2499
2500 // Helper method for getting the top window.
2501 @Override
2502 public WebView getCurrentTopWebView() {
2503 return mTabControl.getCurrentTopWebView();
2504 }
2505
2506 @Override
2507 public WebView getCurrentWebView() {
2508 return mTabControl.getCurrentWebView();
2509 }
2510
2511 /*
2512 * This method is called as a result of the user selecting the options
2513 * menu to see the download window. It shows the download window on top of
2514 * the current window.
2515 */
2516 void viewDownloads() {
2517 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2518 mActivity.startActivity(intent);
2519 }
2520
John Reck30b065e2011-07-19 10:58:05 -07002521 int getActionModeHeight() {
2522 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2523 new int[] { android.R.attr.actionBarSize });
2524 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2525 actionBarSizeTypedArray.recycle();
2526 return size;
2527 }
2528
Michael Kolb8233fac2010-10-26 16:08:53 -07002529 // action mode
2530
John Reck9c35b9c2012-05-30 10:08:50 -07002531 @Override
2532 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002533 mUi.onActionModeStarted(mode);
2534 mActionMode = mode;
2535 }
2536
2537 /*
2538 * True if a custom ActionMode (i.e. find or select) is in use.
2539 */
2540 @Override
2541 public boolean isInCustomActionMode() {
2542 return mActionMode != null;
2543 }
2544
2545 /*
2546 * End the current ActionMode.
2547 */
2548 @Override
2549 public void endActionMode() {
2550 if (mActionMode != null) {
2551 mActionMode.finish();
2552 }
2553 }
2554
2555 /*
2556 * Called by find and select when they are finished. Replace title bars
2557 * as necessary.
2558 */
John Reck9c35b9c2012-05-30 10:08:50 -07002559 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002560 public void onActionModeFinished(ActionMode mode) {
2561 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002562 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002563 mActionMode = null;
2564 }
2565
2566 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002567 final Tab tab = getCurrentTab();
2568 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002569 }
2570
2571 // bookmark handling
2572
2573 /**
2574 * add the current page as a bookmark to the given folder id
2575 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002576 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002577 * bookmarked, and if it is, edit that bookmark. If false, and
2578 * the site is already bookmarked, do not attempt to edit the
2579 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 */
2581 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002582 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002583 WebView w = getCurrentTopWebView();
2584 if (w == null) {
2585 return null;
2586 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002587 Intent i = new Intent(mActivity,
2588 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002589 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2590 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2591 String touchIconUrl = w.getTouchIconUrl();
2592 if (touchIconUrl != null) {
2593 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2594 WebSettings settings = w.getSettings();
2595 if (settings != null) {
2596 i.putExtra(AddBookmarkPage.USER_AGENT,
2597 settings.getUserAgentString());
2598 }
2599 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002600 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002601 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002602 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002603 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2604 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 // Put the dialog at the upper right of the screen, covering the
2606 // star on the title bar.
2607 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002608 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002609 }
2610
2611 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002612 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002613 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002614 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002615 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002616 }
2617
Vivek Sekharb54614f2014-05-01 19:03:37 -07002618 @Override
2619 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2620 boolean capture) {
2621 mUploadHandler = new UploadHandler(this);
2622 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2623 }
2624
Michael Kolb8233fac2010-10-26 16:08:53 -07002625 // thumbnails
2626
2627 /**
2628 * Return the desired width for thumbnail screenshots, which are stored in
2629 * the database, and used on the bookmarks screen.
2630 * @param context Context for finding out the density of the screen.
2631 * @return desired width for thumbnail screenshot.
2632 */
2633 static int getDesiredThumbnailWidth(Context context) {
2634 return context.getResources().getDimensionPixelOffset(
2635 R.dimen.bookmarkThumbnailWidth);
2636 }
2637
2638 /**
2639 * Return the desired height for thumbnail screenshots, which are stored in
2640 * the database, and used on the bookmarks screen.
2641 * @param context Context for finding out the density of the screen.
2642 * @return desired height for thumbnail screenshot.
2643 */
2644 static int getDesiredThumbnailHeight(Context context) {
2645 return context.getResources().getDimensionPixelOffset(
2646 R.dimen.bookmarkThumbnailHeight);
2647 }
2648
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002649 static void createScreenshotAsync(WebView view, int width, int height,
2650 final ValueCallback<Bitmap> cb) {
2651 if (view == null || width == 0 || height == 0) {
2652 return;
2653 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002654 view.getContentBitmapAsync(
2655 (float) width / view.getWidth(),
2656 new Rect(),
2657 new ValueCallback<Bitmap>() {
2658 @Override
2659 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002660 if (bitmap != null)
2661 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2662 cb.onReceiveValue(bitmap);
2663 }});
2664 }
2665
Michael Kolb8233fac2010-10-26 16:08:53 -07002666 private class Copy implements OnMenuItemClickListener {
2667 private CharSequence mText;
2668
John Reck9c35b9c2012-05-30 10:08:50 -07002669 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002670 public boolean onMenuItemClick(MenuItem item) {
2671 copy(mText);
2672 return true;
2673 }
2674
2675 public Copy(CharSequence toCopy) {
2676 mText = toCopy;
2677 }
2678 }
2679
Leon Scroggins63c02662010-11-18 15:16:27 -05002680 private static class Download implements OnMenuItemClickListener {
2681 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002682 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002683 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002684 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002685 private static final String FALLBACK_EXTENSION = "dat";
2686 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002687
John Reck9c35b9c2012-05-30 10:08:50 -07002688 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002689 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002690 if (DataUri.isDataUri(mText)) {
2691 saveDataUri();
2692 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002693 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002694 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002695 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002696 return true;
2697 }
2698
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002699 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2700 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002701 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002702 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002703 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002704 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002705 }
George Mount387d45d2011-10-07 15:57:53 -07002706
2707 /**
2708 * Treats mText as a data URI and writes its contents to a file
2709 * based on the current time.
2710 */
2711 private void saveDataUri() {
2712 FileOutputStream outputStream = null;
2713 try {
2714 DataUri uri = new DataUri(mText);
2715 File target = getTarget(uri);
2716 outputStream = new FileOutputStream(target);
2717 outputStream.write(uri.getData());
2718 final DownloadManager manager =
2719 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2720 manager.addCompletedDownload(target.getName(),
2721 mActivity.getTitle().toString(), false,
2722 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002723 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002724 } catch (IOException e) {
2725 Log.e(LOGTAG, "Could not save data URL");
2726 } finally {
2727 if (outputStream != null) {
2728 try {
2729 outputStream.close();
2730 } catch (IOException e) {
2731 // ignore close errors
2732 }
2733 }
2734 }
2735 }
2736
2737 /**
2738 * Creates a File based on the current time stamp and uses
2739 * the mime type of the DataUri to get the extension.
2740 */
2741 private File getTarget(DataUri uri) throws IOException {
2742 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002743 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002744 String nameBase = format.format(new Date());
2745 String mimeType = uri.getMimeType();
2746 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2747 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2748 if (extension == null) {
2749 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2750 extension = FALLBACK_EXTENSION;
2751 }
2752 extension = "." + extension; // createTempFile needs the '.'
2753 File targetFile = File.createTempFile(nameBase, extension, dir);
2754 return targetFile;
2755 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002756 }
2757
Cary Clark8974d282010-11-22 10:46:05 -05002758 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002759 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002760
John Reck9c35b9c2012-05-30 10:08:50 -07002761 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002762 public boolean onMenuItemClick(MenuItem item) {
2763 if (mWebView != null) {
2764 return mWebView.selectText();
2765 }
2766 return false;
2767 }
2768
2769 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002770 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002771 }
2772
2773 }
2774
Michael Kolb8233fac2010-10-26 16:08:53 -07002775 /********************** TODO: UI stuff *****************************/
2776
2777 // these methods have been copied, they still need to be cleaned up
2778
2779 /****************** tabs ***************************************************/
2780
2781 // basic tab interactions:
2782
2783 // it is assumed that tabcontrol already knows about the tab
2784 protected void addTab(Tab tab) {
2785 mUi.addTab(tab);
2786 }
2787
2788 protected void removeTab(Tab tab) {
2789 mUi.removeTab(tab);
2790 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002791 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002792 }
2793
Michael Kolbf2055602011-04-09 17:20:03 -07002794 @Override
2795 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002796 // monkey protection against delayed start
2797 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002798
2799 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2800 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002801 mTabControl.setCurrentTab(tab);
2802 // the tab is guaranteed to have a webview after setCurrentTab
2803 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002804
2805
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002806 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002807 //Purge active tabs
2808 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002809 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002810 }
2811
John Reck8bcafc12011-08-29 16:43:02 -07002812 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002813 Tab current = mTabControl.getCurrentTab();
2814 if (current != null
2815 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002816 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002817 }
2818 }
2819
John Reck26b18322011-06-21 13:08:58 -07002820 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002821 //Cancel navscreen request
2822 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002823 // Dismiss the subwindow if applicable.
2824 dismissSubWindow(appTab);
2825 // Since we might kill the WebView, remove it from the
2826 // content view first.
2827 mUi.detachTab(appTab);
2828 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002829 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002830 // TODO: analyze why the remove and add are necessary
2831 mUi.attachTab(appTab);
2832 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002833 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002834 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002835 } else {
2836 // If the tab was the current tab, we have to attach
2837 // it to the view system again.
2838 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002839 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002840 }
2841 }
2842
2843 // Remove the sub window if it exists. Also called by TabControl when the
2844 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002845 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002846 public void dismissSubWindow(Tab tab) {
2847 removeSubWindow(tab);
2848 // dismiss the subwindow. This will destroy the WebView.
2849 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002850 WebView wv = getCurrentTopWebView();
2851 if (wv != null) {
2852 wv.requestFocus();
2853 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002854 }
2855
2856 @Override
2857 public void removeSubWindow(Tab t) {
2858 if (t.getSubWebView() != null) {
2859 mUi.removeSubWindow(t.getSubViewContainer());
2860 }
2861 }
2862
2863 @Override
2864 public void attachSubWindow(Tab tab) {
2865 if (tab.getSubWebView() != null) {
2866 mUi.attachSubWindow(tab.getSubViewContainer());
2867 getCurrentTopWebView().requestFocus();
2868 }
2869 }
2870
Mathew Inwood29721c22011-06-29 17:55:24 +01002871 private Tab showPreloadedTab(final UrlData urlData) {
2872 if (!urlData.isPreloaded()) {
2873 return null;
2874 }
2875 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2876 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2877 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002878 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002879 // Could not submit query. Fallback to regular tab creation
2880 tabControl.destroy();
2881 return null;
2882 }
2883 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002884 // check tab count and make room for new tab
2885 if (!mTabControl.canCreateNewTab()) {
2886 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2887 if (leastUsed != null) {
2888 closeTab(leastUsed);
2889 }
2890 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002891 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002892 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002893 mTabControl.addPreloadedTab(t);
2894 addTab(t);
2895 setActiveTab(t);
2896 return t;
2897 }
2898
Michael Kolb7bcafde2011-05-09 13:55:59 -07002899 // open a non inconito tab with the given url data
2900 // and set as active tab
2901 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002902 Tab tab = showPreloadedTab(urlData);
2903 if (tab == null) {
2904 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002905 if ((tab != null) && !urlData.isEmpty()) {
2906 loadUrlDataIn(tab, urlData);
2907 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002908 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002909 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002910 }
2911
Michael Kolb843510f2010-12-09 10:51:49 -08002912 @Override
2913 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002914 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002915 }
2916
Michael Kolb8233fac2010-10-26 16:08:53 -07002917 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002918 public Tab openIncognitoTab() {
2919 return openTab(INCOGNITO_URI, true, true, false);
2920 }
2921
2922 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002923 public Tab openTab(String url, boolean incognito, boolean setActive,
2924 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002925 return openTab(url, incognito, setActive, useCurrent, null);
2926 }
2927
2928 @Override
2929 public Tab openTab(String url, Tab parent, boolean setActive,
2930 boolean useCurrent) {
2931 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2932 setActive, useCurrent, parent);
2933 }
2934
2935 public Tab openTab(String url, boolean incognito, boolean setActive,
2936 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002937 Tab tab = createNewTab(incognito, setActive, useCurrent);
2938 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002939 if (parent instanceof SnapshotTab) {
2940 addTab(tab);
2941 if (setActive)
2942 setActiveTab(tab);
2943 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002944 parent.addChildTab(tab);
2945 }
Michael Kolb519d2282011-05-09 17:03:19 -07002946 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002947 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002948 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002949 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002950 return tab;
2951 }
2952
2953 // this method will attempt to create a new tab
2954 // incognito: private browsing tab
2955 // setActive: ste tab as current tab
2956 // useCurrent: if no new tab can be created, return current tab
2957 private Tab createNewTab(boolean incognito, boolean setActive,
2958 boolean useCurrent) {
2959 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002960 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2961 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002962 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002963 if (mTabControl.canCreateNewTab()) {
2964 tab = mTabControl.createNewTab(incognito, !setActive);
2965 addTab(tab);
2966 if (setActive) {
2967 setActiveTab(tab);
2968 } else {
2969 tab.pause();
2970 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002971 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002972 if (useCurrent) {
2973 tab = mTabControl.getCurrentTab();
2974 reuseTab(tab, null);
2975 } else {
2976 mUi.showMaxTabsWarning();
2977 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002978 }
2979 }
2980 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002981 }
2982
John Reck2bc80422011-06-30 15:11:49 -07002983 @Override
2984 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2985 SnapshotTab tab = null;
2986 if (mTabControl.canCreateNewTab()) {
2987 tab = mTabControl.createSnapshotTab(snapshotId);
2988 addTab(tab);
2989 if (setActive) {
2990 setActiveTab(tab);
2991 }
2992 } else {
2993 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002994 }
2995 return tab;
2996 }
2997
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002999 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003000 * @return boolean True if we successfully switched to a different tab. If
3001 * the indexth tab is null, or if that tab is the same as
3002 * the current one, return false.
3003 */
3004 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003005 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003006 Tab currentTab = mTabControl.getCurrentTab();
3007 if (tab == null || tab == currentTab) {
3008 return false;
3009 }
3010 setActiveTab(tab);
3011 return true;
3012 }
3013
3014 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003015 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003016 closeCurrentTab(false);
3017 }
3018
3019 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003020 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003021 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003022 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003023 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003024 return;
3025 }
Michael Kolbc831b632011-05-11 09:30:34 -07003026 final Tab current = mTabControl.getCurrentTab();
3027 final int pos = mTabControl.getCurrentPosition();
3028 Tab newTab = current.getParent();
3029 if (newTab == null) {
3030 newTab = mTabControl.getTab(pos + 1);
3031 if (newTab == null) {
3032 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003033 }
3034 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003035 if (andQuit) {
3036 mTabControl.setCurrentTab(newTab);
3037 closeTab(current);
3038 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003039 // Close window
3040 closeTab(current);
3041 }
3042 }
3043
3044 /**
3045 * Close the tab, remove its associated title bar, and adjust mTabControl's
3046 * current tab to a valid value.
3047 */
3048 @Override
3049 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003050 if (tab == mTabControl.getCurrentTab()) {
3051 closeCurrentTab();
3052 } else {
3053 removeTab(tab);
3054 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003055 }
3056
Afzal Najamd4e33312012-04-26 01:54:01 -04003057 /**
3058 * Close all tabs except the current one
3059 */
3060 @Override
3061 public void closeOtherTabs() {
3062 int inactiveTabs = mTabControl.getTabCount() - 1;
3063 for (int i = inactiveTabs; i >= 0; i--) {
3064 Tab tab = mTabControl.getTab(i);
3065 if (tab != mTabControl.getCurrentTab()) {
3066 removeTab(tab);
3067 }
3068 }
3069 }
3070
Michael Kolb8233fac2010-10-26 16:08:53 -07003071 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003072 protected void loadUrlFromContext(String url) {
3073 Tab tab = getCurrentTab();
3074 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003076 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003077 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003078 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003079 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003080 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003081 }
3082 }
3083 }
3084
3085 /**
3086 * Load the URL into the given WebView and update the title bar
3087 * to reflect the new load. Call this instead of WebView.loadUrl
3088 * directly.
3089 * @param view The WebView used to load url.
3090 * @param url The URL to load.
3091 */
John Reck71e51422011-07-01 16:49:28 -07003092 @Override
3093 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003094 loadUrl(tab, url, null);
3095 }
3096
3097 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3098 if (tab != null) {
3099 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003100 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003101 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003102 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003103 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003104 }
3105
3106 /**
3107 * Load UrlData into a Tab and update the title bar to reflect the new
3108 * load. Call this instead of UrlData.loadIn directly.
3109 * @param t The Tab used to load.
3110 * @param data The UrlData being loaded.
3111 */
3112 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003113 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003114 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003115 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003116 } else {
John Reck38b39652012-06-05 09:22:59 -07003117 if (t != null && data.mDisableUrlOverride) {
3118 t.disableUrlOverridingForLoad();
3119 }
John Reck26b18322011-06-21 13:08:58 -07003120 loadUrl(t, data.mUrl, data.mHeaders);
3121 }
3122 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003123 }
3124
John Reck30c714c2010-12-16 17:30:34 -08003125 @Override
3126 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003127 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003128 //In case of tab can go back (aka tab has navigation entry) do nothing
3129 //else just load homepage in current tab.
3130 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003131 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003132 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003133 }
3134
3135 void goBackOnePageOrQuit() {
3136 Tab current = mTabControl.getCurrentTab();
3137 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003138 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3139 showExitDialog(mActivity);
3140 } else {
3141 /*
3142 * Instead of finishing the activity, simply push this to the back
3143 * of the stack and let ActivityManager to choose the foreground
3144 * activity. As BrowserActivity is singleTask, it will be always the
3145 * root of the task. So we can use either true or false for
3146 * moveTaskToBack().
3147 */
3148 mActivity.moveTaskToBack(true);
3149 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003150 return;
3151 }
John Reckef654f12011-07-12 16:42:08 -07003152 if (current.canGoBack()) {
3153 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003154 } else {
3155 // Check to see if we are closing a window that was created by
3156 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003157 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003158 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003159 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003160 // Now we close the other tab
3161 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003162 } else if (BrowserConfig.getInstance(getContext())
3163 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3164 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003165 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003166 if ((current.getAppId() != null) || current.closeOnBack()) {
3167 closeCurrentTab(true);
3168 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003169 /*
3170 * Instead of finishing the activity, simply push this to the back
3171 * of the stack and let ActivityManager to choose the foreground
3172 * activity. As BrowserActivity is singleTask, it will be always the
3173 * root of the task. So we can use either true or false for
3174 * moveTaskToBack().
3175 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003176 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003177 }
3178 }
3179 }
3180
3181 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003182 * helper method for key handler
3183 * returns the current tab if it can't advance
3184 */
Michael Kolbc831b632011-05-11 09:30:34 -07003185 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003186 int pos = mTabControl.getCurrentPosition() + 1;
3187 if (pos >= mTabControl.getTabCount()) {
3188 pos = 0;
3189 }
3190 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003191 }
3192
3193 /**
3194 * helper method for key handler
3195 * returns the current tab if it can't advance
3196 */
Michael Kolbc831b632011-05-11 09:30:34 -07003197 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003198 int pos = mTabControl.getCurrentPosition() - 1;
3199 if ( pos < 0) {
3200 pos = mTabControl.getTabCount() - 1;
3201 }
3202 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003203 }
3204
John Reckbcef87f2012-02-03 14:58:34 -08003205 boolean isMenuOrCtrlKey(int keyCode) {
3206 return (KeyEvent.KEYCODE_MENU == keyCode)
3207 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3208 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3209 }
3210
Michael Kolb0035fad2011-03-14 13:25:28 -07003211 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003212 * handle key events in browser
3213 *
3214 * @param keyCode
3215 * @param event
3216 * @return true if handled, false to pass to super
3217 */
John Reck9c35b9c2012-05-30 10:08:50 -07003218 @Override
3219 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003220 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3221 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003222 if (!mUi.isComboViewShowing()) {
3223 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3224 true, false);
3225 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003226 return true;
3227 }
3228
Cary Clark160bbb92011-01-10 11:17:07 -05003229 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003230 // Even if MENU is already held down, we need to call to super to open
3231 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003232 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003233 mMenuIsDown = true;
3234 return false;
3235 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003236
Cary Clark8ff8c662010-12-29 15:03:05 -05003237 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003238 Tab tab = getCurrentTab();
3239 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003240
Cary Clark160bbb92011-01-10 11:17:07 -05003241 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3242 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003243
Michael Kolb8233fac2010-10-26 16:08:53 -07003244 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003245 case KeyEvent.KEYCODE_TAB:
3246 if (event.isCtrlPressed()) {
3247 if (event.isShiftPressed()) {
3248 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003249 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003250 } else {
3251 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003252 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003253 }
3254 return true;
3255 }
3256 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003257 case KeyEvent.KEYCODE_SPACE:
3258 // WebView/WebTextView handle the keys in the KeyDown. As
3259 // the Activity's shortcut keys are only handled when WebView
3260 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003261 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003262 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003263 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003264 pageDown();
3265 }
3266 return true;
3267 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003268 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003269 event.startTracking();
3270 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003271 case KeyEvent.KEYCODE_FORWARD:
3272 if (!noModifiers) break;
3273 tab.goForward();
3274 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003275 case KeyEvent.KEYCODE_DPAD_LEFT:
3276 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003277 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003278 return true;
3279 }
3280 break;
3281 case KeyEvent.KEYCODE_DPAD_RIGHT:
3282 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003283 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003284 return true;
3285 }
3286 break;
3287 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003288 if (ctrl) {
3289 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003290 return true;
3291 }
3292 break;
Michael Kolba4183062011-01-16 10:43:21 -08003293// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003294 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003295 if (ctrl ) {
3296 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003297 return true;
3298 }
3299 break;
Michael Kolba4183062011-01-16 10:43:21 -08003300// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003301// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003302// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003303// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003304// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003305// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003306// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003307// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003308// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003309// case KeyEvent.KEYCODE_M: // unused
3310// case KeyEvent.KEYCODE_N: // in Chrome: new window
3311// case KeyEvent.KEYCODE_O: // in Chrome: open file
3312// case KeyEvent.KEYCODE_P: // in Chrome: print page
3313// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003314// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003315// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3316 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003317 // we can't use the ctrl/shift flags, they check for
3318 // exclusive use of a modifier
3319 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003320 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003321 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003322 } else {
3323 openTabToHomePage();
3324 }
3325 return true;
3326 }
3327 break;
3328// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3329// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003330// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003331// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3332// case KeyEvent.KEYCODE_Y: // unused
3333// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003334 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003335 // it is a regular key and webview is not null
3336 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003337 }
3338
John Reck9c35b9c2012-05-30 10:08:50 -07003339 @Override
3340 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003341 switch(keyCode) {
3342 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003343 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003344 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003345 return true;
3346 }
3347 break;
3348 }
3349 return false;
3350 }
3351
John Reck9c35b9c2012-05-30 10:08:50 -07003352 @Override
3353 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003354 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003355 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003356 if (KeyEvent.KEYCODE_MENU == keyCode
3357 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003358 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003359 }
3360 }
Cary Clark160bbb92011-01-10 11:17:07 -05003361 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003362 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003363 case KeyEvent.KEYCODE_BACK:
3364 if (event.isTracking() && !event.isCanceled()) {
3365 onBackKey();
3366 return true;
3367 }
3368 break;
3369 }
3370 return false;
3371 }
3372
3373 public boolean isMenuDown() {
3374 return mMenuIsDown;
3375 }
3376
John Reck9c35b9c2012-05-30 10:08:50 -07003377 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003378 public void setupAutoFill(Message message) {
3379 // Open the settings activity at the AutoFill profile fragment so that
3380 // the user can create a new profile. When they return, we will dispatch
3381 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003382 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003383 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3384 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003385 }
John Reckb3417f02011-01-14 11:01:05 -08003386
John Reck9c35b9c2012-05-30 10:08:50 -07003387 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003388 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003389 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003390 return true;
3391 }
3392
John Reck1cf4b792011-07-26 10:22:22 -07003393 @Override
3394 public boolean shouldCaptureThumbnails() {
3395 return mUi.shouldCaptureThumbnails();
3396 }
3397
Michael Kolbc3af0672011-08-09 10:24:41 -07003398 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003399 public boolean supportsVoice() {
3400 PackageManager pm = mActivity.getPackageManager();
3401 List activities = pm.queryIntentActivities(new Intent(
3402 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3403 return activities.size() != 0;
3404 }
3405
3406 @Override
3407 public void startVoiceRecognizer() {
3408 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003409 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003410 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3411 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3412 mActivity.startActivityForResult(voice, VOICE_RESULT);
3413 }
3414
Pankaj Garg7b279f62014-08-12 14:47:18 -07003415 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003416 if (mLevel == level)
3417 return;
3418 mLevel = level;
3419 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003420 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3421 lp.dimAmount = level;
3422 mActivity.getWindow().setAttributes(lp);
3423 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3424 } else {
3425 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3426 }
3427 }
3428
Michael Kolb0b129122012-06-04 16:31:58 -07003429 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003430 public void setBlockEvents(boolean block) {
3431 mBlockEvents = block;
3432 }
3433
John Reck9c35b9c2012-05-30 10:08:50 -07003434 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003435 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003436 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003437 }
3438
John Reck9c35b9c2012-05-30 10:08:50 -07003439 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003440 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003441 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003442 }
3443
John Reck9c35b9c2012-05-30 10:08:50 -07003444 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003445 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003446 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003447 }
3448
John Reck9c35b9c2012-05-30 10:08:50 -07003449 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003450 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003451 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003452 }
3453
John Reck9c35b9c2012-05-30 10:08:50 -07003454 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003455 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003456 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003457 }
3458
Pankaj Garg49b79252014-11-07 17:33:41 -08003459 @Override
3460 public boolean shouldShowAppMenu() {
3461 return true;
3462 }
3463
3464 @Override
3465 public int getMenuThemeResourceId() {
3466 return R.style.OverflowMenuTheme;
3467 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003468}