blob: 4d75a5ff02383a04846ce7e12e0d2813ea528dfe [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;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080044import android.graphics.BitmapFactory;
Tarun Nainanif5563b62015-01-21 18:52:59 -080045import android.media.AudioManager;
kaiyiza016da12013-08-26 17:50:22 +080046import android.net.ConnectivityManager;
47import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.net.Uri;
kaiyiza016da12013-08-26 17:50:22 +080049import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080050import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.os.AsyncTask;
Tarun Nainanif5563b62015-01-21 18:52:59 -080052import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.os.Bundle;
Pankaj Gargf49e0222015-09-01 12:19:13 -070054import android.os.CountDownTimer;
George Mount387d45d2011-10-07 15:57:53 -070055import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.os.Handler;
57import android.os.Message;
58import android.os.PowerManager;
59import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.provider.ContactsContract;
61import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080062import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070063import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070064import android.text.TextUtils;
65import android.util.Log;
66import android.view.ActionMode;
67import android.view.ContextMenu;
68import android.view.ContextMenu.ContextMenuInfo;
69import android.view.Gravity;
70import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070071import android.view.Menu;
72import android.view.MenuInflater;
73import android.view.MenuItem;
74import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070075import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070076import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070077import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070078import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080081import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import org.codeaurora.swe.CookieManager;
85import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080086import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.HttpAuthHandler;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080088import org.codeaurora.swe.WebSettings;
89import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070090import org.codeaurora.swe.WebBackForwardList;
91import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080092
Bijan Amirzada41242f22014-03-21 12:12:18 -070093import com.android.browser.IntentHandler.UrlData;
94import com.android.browser.UI.ComboViews;
Dave Tharp13f3f7c2015-07-16 14:58:51 -070095import com.android.browser.mdm.DownloadDirRestriction;
Dave Tharp9a082b12015-06-19 08:46:57 -070096import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -070097import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -070098import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -070099import com.android.browser.mynavigation.AddMyNavigationPage;
100import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700101import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700102import com.android.browser.platformsupport.BrowserContract;
Pankaj Garg18902562014-12-05 16:18:51 -0800103import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700104import com.android.browser.provider.BrowserProvider2.Thumbnails;
105import com.android.browser.provider.SnapshotProvider.Snapshots;
106import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800107import com.android.browser.appmenu.AppMenuHandler;
108import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700109
Michael Kolb8233fac2010-10-26 16:08:53 -0700110import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700111import java.io.File;
112import java.io.FileOutputStream;
113import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700115import java.text.DateFormat;
116import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700117import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700118import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700119import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800120import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200121import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700122import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123
124/**
125 * Controller for browser
126 */
127public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800128 implements WebViewController, UiController, ActivityController,
129 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
131 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800132 private static final String SEND_APP_ID_EXTRA =
133 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700134 public static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800135 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
136 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Pankaj Garg75273bb2015-08-20 11:43:49 -0700137 public static final String EXTRA_UPDATED_URLS = "updated_urls";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800138
Vivek Sekharb54614f2014-05-01 19:03:37 -0700139 // Remind switch to data connection if wifi is unavailable
140 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800141
Michael Kolb8233fac2010-10-26 16:08:53 -0700142 // public message ids
143 public final static int LOAD_URL = 1001;
144 public final static int STOP_LOAD = 1002;
145
146 // Message Ids
147 private static final int FOCUS_NODE_HREF = 102;
148 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800149 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800152 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700153
154 private static final int EMPTY_MENU = -1;
155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700157 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 final static int PREFERENCES_PAGE = 3;
159 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700161 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800162 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000163
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
165
166 // As the ids are dynamically created, we can't guarantee that they will
167 // be in sequence, so this static array maps ids to a window number.
168 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
169 { R.id.window_one_menu_id, R.id.window_two_menu_id,
170 R.id.window_three_menu_id, R.id.window_four_menu_id,
171 R.id.window_five_menu_id, R.id.window_six_menu_id,
172 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
173
174 // "source" parameter for Google search through search key
175 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
176 // "source" parameter for Google search through simplily type
177 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
178
George Mount387d45d2011-10-07 15:57:53 -0700179 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700180 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
181
John Reckd7dd9b22011-08-30 09:18:29 -0700182 // A bitmap that is re-used in createScreenshot as scratch space
183 private static Bitmap sThumbnailBitmap;
184
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 private Activity mActivity;
186 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700187 private HomepageHandler mHomepageHandler;
188 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private BrowserSettings mSettings;
190 private WebViewFactory mFactory;
191
192 private WakeLock mWakeLock;
193
194 private UrlHandler mUrlHandler;
195 private UploadHandler mUploadHandler;
196 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 private NetworkStateHandler mNetworkHandler;
198
Ben Murdoch8029a772010-11-16 11:58:21 +0000199 private Message mAutoFillSetupMessage;
200
kaiyiza016da12013-08-26 17:50:22 +0800201 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700202
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 // FIXME, temp address onPrepareMenu performance problem.
204 // When we move everything out of view, we should rewrite this.
205 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700206 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 private int mOldMenuState = EMPTY_MENU;
208 private Menu mCachedMenu;
209
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 private boolean mMenuIsDown;
211
Pankaj Garg49b79252014-11-07 17:33:41 -0800212 private boolean mWasInPageLoad = false;
213 private AppMenuHandler mAppMenuHandler;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 // For select and find, we keep track of the ActionMode so that
216 // finish() can be called as desired.
217 private ActionMode mActionMode;
218
219 /**
220 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
221 * of whether the configuration has changed. The first onMenuOpened call
222 * after a configuration change is simply a reopening of the same menu
223 * (i.e. mIconView did not change).
224 */
225 private boolean mConfigChanged;
226
227 /**
228 * Keeps track of whether the options menu is open. This is important in
229 * determining whether to show or hide the title bar overlay
230 */
231 private boolean mOptionsMenuOpen;
232
233 /**
234 * Whether or not the options menu is in its bigger, popup menu form. When
235 * true, we want the title bar overlay to be gone. When false, we do not.
236 * Only meaningful if mOptionsMenuOpen is true.
237 */
238 private boolean mExtendedMenuOpen;
239
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 private boolean mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700241 private boolean mActivityStopped = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 private boolean mLoadStopped;
243
244 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500245 // Checks to see when the bookmarks database has changed, and updates the
246 // Tabs' notion of whether they represent bookmarked sites.
247 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700248 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700249
Michael Kolbc3af0672011-08-09 10:24:41 -0700250 private boolean mBlockEvents;
251
Michael Kolb0b129122012-06-04 16:31:58 -0700252 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800253 private boolean mUpdateMyNavThumbnail;
254 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800255 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800256 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700257 private PowerConnectionReceiver mLowPowerReceiver;
258 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700259
Pankaj Garg634bf432015-07-13 09:54:21 -0700260 private boolean mCurrentPageBookmarked;
261
Pankaj Gargf49e0222015-09-01 12:19:13 -0700262 private Tab mLatestCreatedTab;
263
264 private List<ValueCallback> mThumbnailCbList;
265
266 private CountDownTimer mCaptureTimer;
267 private static final int mCaptureMaxWaitMS = 200;
268
George Mount3636d0a2011-11-21 09:08:21 -0800269 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 mActivity = browser;
271 mSettings = BrowserSettings.getInstance();
272 mTabControl = new TabControl(this);
273 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700274 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800275 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700276 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700277
278 mUrlHandler = new UrlHandler(this);
279 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700280
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500282 mBookmarksObserver = new ContentObserver(mHandler) {
283 @Override
284 public void onChange(boolean selfChange) {
285 int size = mTabControl.getTabCount();
286 for (int i = 0; i < size; i++) {
287 mTabControl.getTab(i).updateBookmarkedStatus();
288 }
289 }
290
291 };
292 browser.getContentResolver().registerContentObserver(
293 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700294
295 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700296 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800297 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Pankaj Gargf49e0222015-09-01 12:19:13 -0700298 mThumbnailCbList = new ArrayList<ValueCallback>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700299 }
300
John Reck9c35b9c2012-05-30 10:08:50 -0700301 @Override
302 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700303 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800304 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800305 // mCrashRecoverHandler has any previously saved state.
306 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700307 }
308
George Mount3636d0a2011-11-21 09:08:21 -0800309 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800310 // we dont want to ever recover incognito tabs
311 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700312
Patrick Scott7d50a932011-02-04 09:27:26 -0500313 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700314 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500315 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000316
Michael Kolbc831b632011-05-11 09:30:34 -0700317 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500318 // Not able to restore so we go ahead and clear session cookies. We
319 // must do this before trying to login the user as we don't want to
320 // clear any session cookies set during login.
321 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700322 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800323 if (intent == null) {
324 // This won't happen under common scenarios. The icicle is
325 // not null, but there aren't any tabs to restore.
326 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700327 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800328 final Bundle extra = intent.getExtras();
329 // Create an initial tab.
330 // If the intent is ACTION_VIEW and data is not null, the Browser is
331 // invoked to view the content by another application. In this case,
332 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800333 UrlData urlData = null;
334 if (intent.getData() != null
335 && Intent.ACTION_VIEW.equals(intent.getAction())
336 && intent.getData().toString().startsWith("content://")) {
337 urlData = new UrlData(intent.getData().toString());
338 } else {
339 urlData = IntentHandler.getUrlDataFromIntent(intent);
340 }
George Mount3636d0a2011-11-21 09:08:21 -0800341 Tab t = null;
342 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700343 String landingPage = mActivity.getResources().getString(
344 R.string.def_landing_page);
345 if (!landingPage.isEmpty()) {
346 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800347 } else {
348 t = openTabToHomePage();
349 }
George Mount3636d0a2011-11-21 09:08:21 -0800350 } else {
351 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700352 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800353 }
354 if (t != null) {
355 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
356 }
357 WebView webView = t.getWebView();
358 if (extra != null) {
359 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
360 if (scale > 0 && scale <= 1000) {
361 webView.setInitialScale(scale);
362 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 }
364 }
John Reckd8c74522011-06-14 08:45:00 -0700365 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700367 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500368 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700369 List<Tab> tabs = mTabControl.getTabs();
370 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700371
John Reck1cf4b792011-07-26 10:22:22 -0700372 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700373 //handle restored pages that may require a JS interface
374 if (t.getWebView() != null) {
375 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
376 if (backForwardList != null) {
377 for (int i = 0; i < backForwardList.getSize(); i++) {
378 WebHistoryItem item = backForwardList.getItemAtIndex(i);
379 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
380 }
381 }
382 }
John Reck1cf4b792011-07-26 10:22:22 -0700383 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700384 if (t != mTabControl.getCurrentTab()) {
385 t.pause();
386 }
John Reck1cf4b792011-07-26 10:22:22 -0700387 }
388 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700389 if (tabs.size() == 0) {
390 openTabToHomePage();
391 }
John Reck1cf4b792011-07-26 10:22:22 -0700392 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 // TabControl.restoreState() will create a new tab even if
394 // restoring the state fails.
395 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800396 // Intent is non-null when framework thinks the browser should be
397 // launching with a new intent (icicle is null).
398 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700399 mIntentHandler.onNewIntent(intent);
400 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700403 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800404 if (jsFlags.trim().length() != 0) {
405 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700406 }
George Mount3636d0a2011-11-21 09:08:21 -0800407 if (intent != null
408 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700409 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800410 }
Site Maoabb7bd32015-03-20 15:34:02 -0700411 mLowPowerReceiver = new PowerConnectionReceiver();
412 mPowerChangeReceiver = new PowerConnectionReceiver();
413
414 //always track the android framework's power save mode
415 IntentFilter filter = new IntentFilter();
416 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
417 // Power save mode only exists in Lollipop and above
418 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
419 }
420 filter.addAction(Intent.ACTION_BATTERY_OKAY);
421 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700422 }
423
John Reck1cf4b792011-07-26 10:22:22 -0700424 private static class PruneThumbnails implements Runnable {
425 private Context mContext;
426 private List<Long> mIds;
427
428 PruneThumbnails(Context context, List<Long> preserveIds) {
429 mContext = context.getApplicationContext();
430 mIds = preserveIds;
431 }
432
433 @Override
434 public void run() {
435 ContentResolver cr = mContext.getContentResolver();
436 if (mIds == null || mIds.size() == 0) {
437 cr.delete(Thumbnails.CONTENT_URI, null, null);
438 } else {
439 int length = mIds.size();
440 StringBuilder where = new StringBuilder();
441 where.append(Thumbnails._ID);
442 where.append(" not in (");
443 for (int i = 0; i < length; i++) {
444 where.append(mIds.get(i));
445 if (i < (length - 1)) {
446 where.append(",");
447 }
448 }
449 where.append(")");
450 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
451 }
452 }
453
454 }
455
Michael Kolb1514bb72010-11-22 09:11:48 -0800456 @Override
457 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700458 return mFactory;
459 }
460
461 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800462 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700463 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700464 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800465 }
466
467 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800468 public void createSubWindow(Tab tab) {
469 endActionMode();
470 WebView mainView = tab.getWebView();
471 WebView subView = mFactory.createWebView((mainView == null)
472 ? false
473 : mainView.isPrivateBrowsingEnabled());
474 mUi.createSubWindow(tab, subView);
475 }
476
477 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700478 public Context getContext() {
479 return mActivity;
480 }
481
482 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700483 public Activity getActivity() {
484 return mActivity;
485 }
486
487 void setUi(UI ui) {
488 mUi = ui;
489 }
490
Michael Kolbaf63dba2012-05-16 12:58:05 -0700491 @Override
492 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 return mSettings;
494 }
495
496 IntentHandler getIntentHandler() {
497 return mIntentHandler;
498 }
499
500 @Override
501 public UI getUi() {
502 return mUi;
503 }
504
505 int getMaxTabs() {
506 return mActivity.getResources().getInteger(R.integer.max_tabs);
507 }
508
509 @Override
510 public TabControl getTabControl() {
511 return mTabControl;
512 }
513
Michael Kolb1bf23132010-11-19 12:55:12 -0800514 @Override
515 public List<Tab> getTabs() {
516 return mTabControl.getTabs();
517 }
518
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 private void startHandler() {
520 mHandler = new Handler() {
521
522 @Override
523 public void handleMessage(Message msg) {
524 switch (msg.what) {
525 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700526 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800528 case UNKNOWN_TYPE_MSG:
529 HashMap unknownTypeMap = (HashMap) msg.obj;
530 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
531 /*
532 * When the context menu is shown to the user
533 * we need to assure that its happening on the current webview
534 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
535 */
536 if (getCurrentWebView() != viewForUnknownType)
537 break;
538
539 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800540 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800541 WebView.HitTestResult result = new WebView.HitTestResult();
542
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800543 // Prevent unnecessary calls to context menu
544 // if url and image src are null
545 if (unknown_type_src == null && unknown_type_url == null)
546 break;
547
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800548 //setting the HitTestResult with new RESULT TYPE
549 if (!TextUtils.isEmpty(unknown_type_src)) {
550 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
551 result.setExtra(unknown_type_src);
552 } else {
553 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
554 result.setExtra("about:blank");
555 }
556
557 mResult = result;
558 openContextMenu(viewForUnknownType);
559 mResult = null;
560
561 break;
562
Michael Kolb8233fac2010-10-26 16:08:53 -0700563 case FOCUS_NODE_HREF:
564 {
565 String url = (String) msg.getData().get("url");
566 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500567 String src = (String) msg.getData().get("src");
568 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700569 if (TextUtils.isEmpty(url)) {
570 break;
571 }
572 HashMap focusNodeMap = (HashMap) msg.obj;
573 WebView view = (WebView) focusNodeMap.get("webview");
574 // Only apply the action if the top window did not change.
575 if (getCurrentTopWebView() != view) {
576 break;
577 }
578 switch (msg.arg1) {
579 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700580 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500582 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700583 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500584 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500585 case R.id.open_newtab_context_menu_id:
586 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700587 openTab(url, parent,
588 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500589 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 case R.id.copy_link_context_menu_id:
591 copy(url);
592 break;
593 case R.id.save_link_context_menu_id:
594 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500595 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800596 mActivity, url, view.getSettings().getUserAgentString(),
Pankaj Garg5762b362015-11-02 07:57:06 -0800597 null, null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700599 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700600 if(title == null || title == "")
601 title = url;
602
603 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
604 //SWE TODO: No thumbnail support for the url obtained via
605 //browser context menu as its not loaded in webview.
606 if (bookmarkIntent != null) {
607 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
608 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
609 mActivity.startActivity(bookmarkIntent);
610 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700611 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 }
613 break;
614 }
615
616 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700617 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 break;
619
620 case STOP_LOAD:
621 stopLoading();
622 break;
623
624 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700625 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 mWakeLock.release();
627 // if we reach here, Browser should be still in the
628 // background loading after WAKELOCK_TIMEOUT (5-min).
629 // To avoid burning the battery, stop loading.
630 mTabControl.stopAllLoading();
631 }
632 break;
633
kaiyiz591110b2013-08-06 17:11:06 +0800634 case OPEN_MENU:
635 if (!mOptionsMenuOpen && mActivity != null ) {
636 mActivity.openOptionsMenu();
637 }
638 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 }
640 }
641 };
642
643 }
644
John Reckef654f12011-07-12 16:42:08 -0700645 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200646 public Tab getCurrentTab() {
647 return mTabControl.getCurrentTab();
648 }
649
Michael Kolbba99c5d2010-11-29 14:57:41 -0800650 @Override
651 public void shareCurrentPage() {
652 shareCurrentPage(mTabControl.getCurrentTab());
653 }
654
655 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700656 if (tab == null || tab.getWebView() == null)
657 return;
658
659 final Tab mytab = tab;
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700660
661 createScreenshotAsync(
Pankaj Gargf49e0222015-09-01 12:19:13 -0700662 tab,
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700663 new ValueCallback<Bitmap>() {
664 @Override
665 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -0700666 Bitmap bm = cropAndScaleBitmap(bitmap,
667 getDesiredThumbnailWidth(mActivity),
668 getDesiredThumbnailHeight(mActivity));
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700669 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
Pankaj Gargf49e0222015-09-01 12:19:13 -0700670 mytab.getFavicon(), bm);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700671 }
672 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800673 }
674
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 /**
676 * Share a page, providing the title, url, favicon, and a screenshot. Uses
677 * an {@link Intent} to launch the Activity chooser.
678 * @param c Context used to launch a new Activity.
679 * @param title Title of the page. Stored in the Intent with
680 * {@link Intent#EXTRA_SUBJECT}
681 * @param url URL of the page. Stored in the Intent with
682 * {@link Intent#EXTRA_TEXT}
683 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
684 * with {@link Browser#EXTRA_SHARE_FAVICON}
685 * @param screenshot Bitmap of a screenshot of the page. Stored in the
686 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
687 */
688 static final void sharePage(Context c, String title, String url,
689 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700690
691 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
692 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
693 R.layout.app_row, sDialog.getApps());
694 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700695 }
696
697 private void copy(CharSequence text) {
698 ClipboardManager cm = (ClipboardManager) mActivity
699 .getSystemService(Context.CLIPBOARD_SERVICE);
700 cm.setText(text);
701 }
702
703 // lifecycle
704
John Reck9c35b9c2012-05-30 10:08:50 -0700705 @Override
706 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800708 // update the menu in case of a locale change
709 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800710 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800711 if (mOptionsMenuOpen) {
712 mActivity.closeOptionsMenu();
713 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
714 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 mUi.onConfigurationChanged(config);
716 }
717
718 @Override
719 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700720 if (!mUi.isWebShowing()) {
721 mUi.showWeb(false);
722 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700723 mIntentHandler.onNewIntent(intent);
724 }
725
John Reck9c35b9c2012-05-30 10:08:50 -0700726 @Override
727 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800728 if (mUi.isCustomViewShowing()) {
729 hideCustomView();
730 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700731 if (mActivityPaused) {
732 Log.e(LOGTAG, "BrowserActivity is already paused.");
733 return;
734 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700735 mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700736
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 }
738
John Reck9c35b9c2012-05-30 10:08:50 -0700739 @Override
740 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700741 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800742 Bundle saveState = createSaveState();
743
744 // crash recovery manages all save & restore state
745 mCrashRecoveryHandler.writeState(saveState);
746 mSettings.setLastRunPaused(true);
747 }
748
749 /**
750 * Save the current state to outState. Does not write the state to
751 * disk.
752 * @return Bundle containing the current state of all tabs.
753 */
754 /* package */ Bundle createSaveState() {
755 Bundle saveState = new Bundle();
756 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800757 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 }
759
John Reck9c35b9c2012-05-30 10:08:50 -0700760 @Override
761 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 if (!mActivityPaused) {
763 Log.e(LOGTAG, "BrowserActivity is already resumed.");
764 return;
765 }
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700766 mActivityPaused = false;
767 if (mVoiceResult != null) {
768 mUi.onVoiceResult(mVoiceResult);
769 mVoiceResult = null;
770 }
771 }
772
773 private void releaseWakeLock() {
774 if (mWakeLock != null && mWakeLock.isHeld()) {
775 mHandler.removeMessages(RELEASE_WAKELOCK);
776 mWakeLock.release();
777 }
778 }
779
780 @Override
781 public void onStop() {
782 if (mActivityStopped) {
783 Log.e(LOGTAG, "BrowserActivity is already stoped.");
784 return;
785 }
786 mActivityStopped = true;
787 Tab tab = mTabControl.getCurrentTab();
788 if (tab != null) {
789 tab.pause();
790 if (!pauseWebViewTimers(tab)) {
791 if (mWakeLock == null) {
792 PowerManager pm = (PowerManager) mActivity
793 .getSystemService(Context.POWER_SERVICE);
794 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
795 }
796 mWakeLock.acquire();
797 mHandler.sendMessageDelayed(mHandler
798 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
799 }
800 }
801 mUi.onPause();
802 mNetworkHandler.onPause();
803 NfcHandler.unregister(mActivity);
804 mActivity.unregisterReceiver(mLowPowerReceiver);
805 }
806
807 @Override
808 public void onStart() {
809 if (!mActivityStopped) {
810 Log.e(LOGTAG, "BrowserActivity is already started.");
811 return;
812 }
813 mActivityStopped = false;
Vivek Sekharb53a5952015-07-17 14:14:38 -0700814 UpdateNotificationService.updateCheck(mActivity);
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700815 // reset the search engine based on locale
816 mSettings.updateSearchEngine(false);
George Mount3636d0a2011-11-21 09:08:21 -0800817 mSettings.setLastRunPaused(false);
Michael Kolb70976932010-11-30 11:34:01 -0800818 Tab current = mTabControl.getCurrentTab();
819 if (current != null) {
820 current.resume();
821 resumeWebViewTimers(current);
822 }
John Reckf57c0292011-07-21 18:15:39 -0700823 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200824
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 mUi.onResume();
826 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100827 NfcHandler.register(mActivity, this);
Vivek Sekharc70ae632015-04-08 17:54:05 -0700828 if (current != null && current.getWebView().isShowingCrashView())
829 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700830 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700831
John Reckf57c0292011-07-21 18:15:39 -0700832 }
833
Michael Kolb70976932010-11-30 11:34:01 -0800834 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800835 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800836 * @param tab guaranteed non-null
837 */
838 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 boolean inLoad = tab.inPageLoad();
Tarun Nainani7c762c82015-08-04 13:42:09 -0700840 if ((!mActivityStopped && !inLoad) || (mActivityStopped && inLoad)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 CookieSyncManager.getInstance().startSync();
842 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100843 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 }
845 }
846
Michael Kolb70976932010-11-30 11:34:01 -0800847 /**
848 * Pause all WebView timers using the WebView of the given tab
849 * @param tab
850 * @return true if the timers are paused or tab is null
851 */
852 private boolean pauseWebViewTimers(Tab tab) {
853 if (tab == null) {
854 return true;
855 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100857 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 }
Michael Kolb70976932010-11-30 11:34:01 -0800860 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 }
862
John Reck9c35b9c2012-05-30 10:08:50 -0700863 @Override
864 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800865 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
867 mUploadHandler = null;
868 }
869 if (mTabControl == null) return;
870 mUi.onDestroy();
871 // Remove the current tab and sub window
872 Tab t = mTabControl.getCurrentTab();
873 if (t != null) {
874 dismissSubWindow(t);
875 removeTab(t);
876 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500877 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 // Destroy all the tabs
879 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700880 // Unregister receiver
881 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 }
883
884 protected boolean isActivityPaused() {
885 return mActivityPaused;
886 }
887
John Reck9c35b9c2012-05-30 10:08:50 -0700888 @Override
889 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 mTabControl.freeMemory();
891 }
892
893 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700894 public void stopLoading() {
895 mLoadStopped = true;
896 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700898 if (w != null) {
899 w.stopLoading();
900 mUi.onPageStopped(tab);
901 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700902 }
903
904 boolean didUserStopLoading() {
905 return mLoadStopped;
906 }
907
kaiyiza016da12013-08-26 17:50:22 +0800908 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700909 final String reminderType = getContext().getResources().getString(
910 R.string.def_wifi_browser_interaction_remind_type);
911 final String selectionConnnection = getContext().getResources().getString(
912 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700913 final String wifiSelection = getContext().getResources().getString(
914 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700915
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700916 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
917 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700918 return;
919
kaiyiza016da12013-08-26 17:50:22 +0800920 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700921 Context.CONNECTIVITY_SERVICE);
922 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700923 WifiManager wifiMgr = (WifiManager) this.getContext()
924 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700925 if (networkInfo == null
926 || (networkInfo != null && (networkInfo.getType() !=
927 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700928 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
929 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700930 List<ScanResult> list = wifiMgr.getScanResults();
931 // Have no AP's for Wifi's fall back to data
932 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700933 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700934 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
935 this.getContext().startActivity(intent);
936 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700937 // Request to select Wifi AP
938 try {
939 Intent intent = new Intent(wifiSelection);
940 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
941 this.getContext().startActivity(intent);
942 } catch (Exception e) {
943 String err_msg = this.getContext().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700944 R.string.activity_not_found, wifiSelection);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700945 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
946 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700947 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700948 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800949 }
950 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700951
Michael Kolb8233fac2010-10-26 16:08:53 -0700952 // WebViewController
953
954 @Override
John Reck324d4402011-01-11 16:56:42 -0800955 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700956
Michael Kolb8233fac2010-10-26 16:08:53 -0700957 // reset sync timer to avoid sync starts during loading a page
958 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700959 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700960 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800961 boolean networkNotifier = BrowserConfig.getInstance(getContext())
962 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700963 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700964 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800965 } else {
966 if (!mNetworkHandler.isNetworkUp()) {
967 view.setNetworkAvailable(false);
968 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700969 }
970
971 // when BrowserActivity just starts, onPageStarted may be called before
972 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
973 // to start the timer. As we won't switch tabs while an activity is in
974 // pause state, we can ensure calling resume and pause in pair.
Tarun Nainani7c762c82015-08-04 13:42:09 -0700975 if (mActivityStopped) {
Michael Kolb70976932010-11-30 11:34:01 -0800976 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 }
978 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 endActionMode();
980
John Reck30c714c2010-12-16 17:30:34 -0800981 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700982
John Reck324d4402011-01-11 16:56:42 -0800983 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700984 // update the bookmark database for favicon
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700985 syncBookmarkFavicon(tab, null, url, favicon);
Michael Kolb8233fac2010-10-26 16:08:53 -0700986
987 Performance.tracePageStart(url);
988
989 // Performance probe
990 if (false) {
991 Performance.onPageStarted();
992 }
993
994 }
995
996 @Override
John Reck324d4402011-01-11 16:56:42 -0800997 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700998 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800999 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001000
Michael Kolb8233fac2010-10-26 16:08:53 -07001001 // Performance probe
1002 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001003 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 }
1005
Tarun Nainani8eb00912014-07-17 12:28:32 -07001006 tab.onPageFinished();
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001007 syncBookmarkFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -07001008
Michael Kolb8233fac2010-10-26 16:08:53 -07001009 Performance.tracePageFinished();
1010 }
1011
1012 @Override
John Reck30c714c2010-12-16 17:30:34 -08001013 public void onProgressChanged(Tab tab) {
1014 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001015
1016 if (newProgress == 100) {
1017 CookieSyncManager.getInstance().sync();
1018 // onProgressChanged() may continue to be called after the main
1019 // frame has finished loading, as any remaining sub frames continue
1020 // to load. We'll only get called once though with newProgress as
1021 // 100 when everything is loaded. (onPageFinished is called once
1022 // when the main frame completes loading regardless of the state of
1023 // any sub frames so calls to onProgressChanges may continue after
1024 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001025 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001026 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001027 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001028 } else if (mWasInPageLoad) {
1029 mWasInPageLoad = false;
1030 updateInLoadMenuItems(mCachedMenu, tab);
1031 }
1032
Tarun Nainani7c762c82015-08-04 13:42:09 -07001033 if (mActivityStopped && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001034 // pause the WebView timer and release the wake lock if it is
1035 // finished while BrowserActivity is in pause state.
1036 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 }
1038 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001039 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001041 // still loading
1042 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001044 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001045 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001046 } else {
1047 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001048 }
1049 }
Pankaj Gargf9040c82015-08-14 10:19:31 -07001050
1051 mUi.onProgressChanged(tab);
John Reck30c714c2010-12-16 17:30:34 -08001052 }
1053
1054 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001055 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001056 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001057 }
1058
1059 @Override
1060 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001061 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001062 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001063 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1065 return;
1066 }
1067 // Update the title in the history database if not in private browsing mode
1068 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001069 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001070 }
1071 }
1072
1073 @Override
1074 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001075 syncBookmarkFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1076 ((BaseUi)mUi).setFavicon(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 }
1078
1079 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001080 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001081 // if tab is snapshot tab we want to prevent navigation from occuring
1082 // since snapshot tab opens a new tab with the url
1083 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 }
1085
1086 @Override
1087 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1088 if (mMenuIsDown) {
1089 // only check shortcut key when MENU is held
1090 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1091 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001093 int keyCode = event.getKeyCode();
1094 // We need to send almost every key to WebKit. However:
1095 // 1. We don't want to block the device on the renderer for
1096 // some keys like menu, home, call.
1097 // 2. There are no WebKit equivalents for some of these keys
1098 // (see app/keyboard_codes_win.h)
1099 // Note that these are not the same set as KeyEvent.isSystemKey:
1100 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1101 if (keyCode == KeyEvent.KEYCODE_MENU ||
1102 keyCode == KeyEvent.KEYCODE_HOME ||
1103 keyCode == KeyEvent.KEYCODE_BACK ||
1104 keyCode == KeyEvent.KEYCODE_CALL ||
1105 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1106 keyCode == KeyEvent.KEYCODE_POWER ||
1107 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1108 keyCode == KeyEvent.KEYCODE_CAMERA ||
1109 keyCode == KeyEvent.KEYCODE_FOCUS ||
1110 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1111 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1112 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1113 return true;
1114 }
1115
1116 // We also have to intercept some shortcuts before we send them to the ContentView.
1117 if (event.isCtrlPressed() && (
1118 keyCode == KeyEvent.KEYCODE_TAB ||
1119 keyCode == KeyEvent.KEYCODE_W ||
1120 keyCode == KeyEvent.KEYCODE_F4)) {
1121 return true;
1122 }
1123
1124 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 }
1126
Tarun Nainanif5563b62015-01-21 18:52:59 -08001127 private void handleMediaKeyEvent(KeyEvent event) {
1128
1129 int keyCode = event.getKeyCode();
1130 // send media key events to audio manager
1131 if (Build.VERSION.SDK_INT >= 19) {
1132
1133 switch (keyCode) {
1134 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1135 case KeyEvent.KEYCODE_MEDIA_STOP:
1136 case KeyEvent.KEYCODE_MEDIA_NEXT:
1137 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1138 case KeyEvent.KEYCODE_MEDIA_REWIND:
1139 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1140 case KeyEvent.KEYCODE_MUTE:
1141 case KeyEvent.KEYCODE_MEDIA_PLAY:
1142 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1143 case KeyEvent.META_SHIFT_RIGHT_ON:
1144 case KeyEvent.KEYCODE_MEDIA_EJECT:
1145 case KeyEvent.KEYCODE_MEDIA_RECORD:
1146 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1147
1148 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1149 .getSystemService(Context.AUDIO_SERVICE);
1150 audioManager.dispatchMediaKeyEvent(event);
1151 }
1152 }
1153 }
1154
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 @Override
John Reck997b1b72012-04-19 18:08:25 -07001156 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001158 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001160 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 } else {
John Reck997b1b72012-04-19 18:08:25 -07001162 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001163 }
1164 }
John Reck997b1b72012-04-19 18:08:25 -07001165 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 }
1167
1168 @Override
John Reck324d4402011-01-11 16:56:42 -08001169 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001170 // Don't save anything in private browsing mode or when disabling history
1171 // for regular tabs is enabled
1172 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1173 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1174 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001175
John Reck49a603c2011-03-03 09:33:05 -08001176 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001177
John Reck324d4402011-01-11 16:56:42 -08001178 if (TextUtils.isEmpty(url)
1179 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 return;
1181 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001182
John Reckf57c0292011-07-21 18:15:39 -07001183 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001184 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 }
1186
1187 @Override
1188 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1189 AsyncTask<Void, Void, String[]> task =
1190 new AsyncTask<Void, Void, String[]>() {
1191 @Override
1192 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001193 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001194 }
1195 @Override
1196 public void onPostExecute(String[] result) {
1197 callback.onReceiveValue(result);
1198 }
1199 };
1200 task.execute();
1201 }
1202
1203 @Override
1204 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1205 final HttpAuthHandler handler, final String host,
1206 final String realm) {
1207 String username = null;
1208 String password = null;
1209
1210 boolean reuseHttpAuthUsernamePassword
1211 = handler.useHttpAuthUsernamePassword();
1212
1213 if (reuseHttpAuthUsernamePassword && view != null) {
1214 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1215 if (credentials != null && credentials.length == 2) {
1216 username = credentials[0];
1217 password = credentials[1];
1218 }
1219 }
1220
1221 if (username != null && password != null) {
1222 handler.proceed(username, password);
1223 } else {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -07001224 if (!tab.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001225 handler.cancel();
1226 }
1227 }
1228 }
1229
1230 @Override
1231 public void onDownloadStart(Tab tab, String url, String userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001232 String contentDisposition, String mimetype, String referer, String auth,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001233 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001234 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001235 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001236 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001237 contentDisposition, mimetype, referer, auth,
1238 w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001239 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 // This Tab was opened for the sole purpose of downloading a
1241 // file. Remove it.
1242 if (tab == mTabControl.getCurrentTab()) {
1243 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001244 if (tab.getDerivedFromIntent())
1245 closeTab(tab);
1246 else
1247 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001248 } else {
1249 // In this case, it is not.
1250 closeTab(tab);
1251 }
1252 }
1253 }
1254
1255 @Override
1256 public Bitmap getDefaultVideoPoster() {
1257 return mUi.getDefaultVideoPoster();
1258 }
1259
1260 @Override
1261 public View getVideoLoadingProgressView() {
1262 return mUi.getVideoLoadingProgressView();
1263 }
1264
Michael Kolb8233fac2010-10-26 16:08:53 -07001265 // helper method
1266
1267 /*
1268 * Update the favorites icon if the private browsing isn't enabled and the
1269 * icon is valid.
1270 */
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001271 private void syncBookmarkFavicon(Tab tab, final String originalUrl,
1272 final String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 if (favicon == null) {
1274 return;
1275 }
1276 if (!tab.isPrivateBrowsingEnabled()) {
1277 Bookmarks.updateFavicon(mActivity
1278 .getContentResolver(), originalUrl, url, favicon);
1279 }
1280 }
1281
Leon Scroggins4cd97792010-12-03 15:31:56 -05001282 @Override
1283 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001284 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001285 mUi.bookmarkedStatusHasChanged(tab);
1286 }
1287
Michael Kolb8233fac2010-10-26 16:08:53 -07001288 // end WebViewController
1289
1290 protected void pageUp() {
1291 getCurrentTopWebView().pageUp(false);
1292 }
1293
1294 protected void pageDown() {
1295 getCurrentTopWebView().pageDown(false);
1296 }
1297
1298 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001299 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001300 public void editUrl() {
1301 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001302 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001303 }
1304
John Reck9c35b9c2012-05-30 10:08:50 -07001305 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001306 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001307 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001308 if (tab.inForeground()) {
1309 if (mUi.isCustomViewShowing()) {
1310 callback.onCustomViewHidden();
1311 return;
1312 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001313 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001314 // Save the menu state and set it to empty while the custom
1315 // view is showing.
1316 mOldMenuState = mMenuState;
1317 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001318 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001319 }
1320 }
1321
1322 @Override
1323 public void hideCustomView() {
1324 if (mUi.isCustomViewShowing()) {
1325 mUi.onHideCustomView();
1326 // Reset the old menu state.
1327 mMenuState = mOldMenuState;
1328 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001329 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001330 }
1331 }
1332
John Reck9c35b9c2012-05-30 10:08:50 -07001333 @Override
1334 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 Intent intent) {
1336 if (getCurrentTopWebView() == null) return;
1337 switch (requestCode) {
1338 case PREFERENCES_PAGE:
1339 if (resultCode == Activity.RESULT_OK && intent != null) {
1340 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001341 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001342 mTabControl.removeParentChildRelationShips();
Pankaj Garg75273bb2015-08-20 11:43:49 -07001343 } else if (action.equals(PreferenceKeys.ACTION_RELOAD_PAGE)) {
1344 ArrayList<String> origins =
1345 intent.getStringArrayListExtra(EXTRA_UPDATED_URLS);
1346 if (origins.isEmpty()) {
1347 mTabControl.reloadLiveTabs();
1348 }
1349 else{
1350 for (String origin : origins){
1351 mTabControl.findAndReload(origin);
1352 }
1353 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001354 }
1355 }
1356 break;
1357 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001358 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001359 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001361 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001362 case AUTOFILL_SETUP:
1363 // Determine whether a profile was actually set up or not
1364 // and if so, send the message back to the WebTextView to
1365 // fill the form with the new profile.
1366 if (getSettings().getAutoFillProfile() != null) {
1367 mAutoFillSetupMessage.sendToTarget();
1368 mAutoFillSetupMessage = null;
1369 }
1370 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001371 case COMBO_VIEW:
1372 if (intent == null || resultCode != Activity.RESULT_OK) {
1373 break;
1374 }
John Reck3ba45532011-08-11 16:26:53 -07001375 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001376 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1377 Tab t = getCurrentTab();
1378 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001379 mUpdateMyNavThumbnail = true;
1380 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001381 loadUrl(t, uri.toString());
1382 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1383 String[] urls = intent.getStringArrayExtra(
1384 ComboViewActivity.EXTRA_OPEN_ALL);
1385 Tab parent = getCurrentTab();
1386 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001387 if (url != null) {
1388 parent = openTab(url, parent,
1389 !mSettings.openInBackground(), true);
1390 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001391 }
1392 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1393 long id = intent.getLongExtra(
1394 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1395 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001396 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001397 }
1398 }
1399 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001400 case VOICE_RESULT:
1401 if (resultCode == Activity.RESULT_OK && intent != null) {
1402 ArrayList<String> results = intent.getStringArrayListExtra(
1403 RecognizerIntent.EXTRA_RESULTS);
1404 if (results.size() >= 1) {
1405 mVoiceResult = results.get(0);
1406 }
1407 }
1408 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001409 case MY_NAVIGATION:
1410 if (intent == null || resultCode != Activity.RESULT_OK) {
1411 break;
1412 }
1413
1414 if (intent.getBooleanExtra("need_refresh", false) &&
1415 getCurrentTopWebView() != null) {
1416 getCurrentTopWebView().reload();
1417 }
1418 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 default:
1420 break;
1421 }
1422 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001423 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001424 }
1425
1426 /**
1427 * Open the Go page.
1428 * @param startWithHistory If true, open starting on the history tab.
1429 * Otherwise, start with the bookmarks tab.
1430 */
1431 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001432 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001433 if (mTabControl.getCurrentWebView() == null) {
1434 return;
1435 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001436 // clear action mode
1437 if (isInCustomActionMode()) {
1438 endActionMode();
1439 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001440 Bundle extras = new Bundle();
1441 // Disable opening in a new window if we have maxed out the windows
1442 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1443 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001444 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001445 }
1446
1447 // combo view callbacks
1448
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 // key handling
1450 protected void onBackKey() {
1451 if (!mUi.onBackKey()) {
1452 WebView subwindow = mTabControl.getCurrentSubWindow();
1453 if (subwindow != null) {
1454 if (subwindow.canGoBack()) {
1455 subwindow.goBack();
1456 } else {
1457 dismissSubWindow(mTabControl.getCurrentTab());
1458 }
1459 } else {
1460 goBackOnePageOrQuit();
1461 }
1462 }
1463 }
1464
Michael Kolb4bd767d2011-05-27 11:33:55 -07001465 protected boolean onMenuKey() {
1466 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001467 }
1468
Michael Kolb8233fac2010-10-26 16:08:53 -07001469 // menu handling and state
1470 // TODO: maybe put into separate handler
1471
John Reck9c35b9c2012-05-30 10:08:50 -07001472 @Override
1473 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001474 if (mMenuState == EMPTY_MENU) {
1475 return false;
1476 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001477 MenuInflater inflater = mActivity.getMenuInflater();
1478 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001479 return true;
1480 }
1481
John Reck9c35b9c2012-05-30 10:08:50 -07001482 @Override
1483 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001484 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001485 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001486 return;
1487 }
1488 if (!(v instanceof WebView)) {
1489 return;
1490 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001491 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001492 WebView.HitTestResult result;
1493
1494 /* Determine whether the ContextMenu got triggered because
1495 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1496 * received. The mResult acts as a flag to identify, how it got trigerred
1497 */
1498 if (mResult == null){
1499 result = webview.getHitTestResult();
1500 } else {
1501 result = mResult;
1502 }
1503
Michael Kolb8233fac2010-10-26 16:08:53 -07001504 if (result == null) {
1505 return;
1506 }
1507
1508 int type = result.getType();
1509 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001510
1511 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1512 unknownTypeMap.put("webview", webview);
1513 final Message msg = mHandler.obtainMessage(
1514 UNKNOWN_TYPE_MSG, unknownTypeMap);
1515 /* As defined in android developers guide
1516 * when UNKNOWN_TYPE is received as a result of HitTest
1517 * you need to determing the type by invoking requestFocusNodeHref
1518 */
1519 webview.requestFocusNodeHref(msg);
1520
Michael Kolb8233fac2010-10-26 16:08:53 -07001521 Log.w(LOGTAG,
1522 "We should not show context menu when nothing is touched");
1523 return;
1524 }
1525 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1526 // let TextView handles context menu
1527 return;
1528 }
1529
1530 // Note, http://b/issue?id=1106666 is requesting that
1531 // an inflated menu can be used again. This is not available
1532 // yet, so inflate each time (yuk!)
1533 MenuInflater inflater = mActivity.getMenuInflater();
1534 inflater.inflate(R.menu.browsercontext, menu);
1535
1536 // Show the correct menu group
1537 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001538 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001539 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001540 menu.setGroupVisible(R.id.PHONE_MENU,
1541 type == WebView.HitTestResult.PHONE_TYPE);
1542 menu.setGroupVisible(R.id.EMAIL_MENU,
1543 type == WebView.HitTestResult.EMAIL_TYPE);
1544 menu.setGroupVisible(R.id.GEO_MENU,
1545 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001546 String itemUrl = null;
1547 String url = webview.getOriginalUrl();
1548 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1549 itemUrl = Uri.decode(navigationUrl);
1550 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1551 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1552 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1553 } else {
1554 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1555 }
1556 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1557 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1558 } else {
1559 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1560
1561 menu.setGroupVisible(R.id.IMAGE_MENU,
1562 type == WebView.HitTestResult.IMAGE_TYPE
1563 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1564 menu.setGroupVisible(R.id.ANCHOR_MENU,
1565 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1566 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001567
1568 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1569 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1570 UrlUtils.isDownloadableScheme(extra));
1571 }
1572 else {
1573 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1574 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001575 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001576 // Setup custom handling depending on the type
1577 switch (type) {
1578 case WebView.HitTestResult.PHONE_TYPE:
1579 menu.setHeaderTitle(Uri.decode(extra));
1580 menu.findItem(R.id.dial_context_menu_id).setIntent(
1581 new Intent(Intent.ACTION_VIEW, Uri
1582 .parse(WebView.SCHEME_TEL + extra)));
1583 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1584 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1585 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1586 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1587 addIntent);
1588 menu.findItem(R.id.copy_phone_context_menu_id)
1589 .setOnMenuItemClickListener(
1590 new Copy(extra));
1591 break;
1592
1593 case WebView.HitTestResult.EMAIL_TYPE:
1594 menu.setHeaderTitle(extra);
1595 menu.findItem(R.id.email_context_menu_id).setIntent(
1596 new Intent(Intent.ACTION_VIEW, Uri
1597 .parse(WebView.SCHEME_MAILTO + extra)));
1598 menu.findItem(R.id.copy_mail_context_menu_id)
1599 .setOnMenuItemClickListener(
1600 new Copy(extra));
1601 break;
1602
1603 case WebView.HitTestResult.GEO_TYPE:
1604 menu.setHeaderTitle(extra);
1605 menu.findItem(R.id.map_context_menu_id).setIntent(
1606 new Intent(Intent.ACTION_VIEW, Uri
1607 .parse(WebView.SCHEME_GEO
1608 + URLEncoder.encode(extra))));
1609 menu.findItem(R.id.copy_geo_context_menu_id)
1610 .setOnMenuItemClickListener(
1611 new Copy(extra));
1612 break;
1613
1614 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1615 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001616 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 // decide whether to show the open link in new tab option
1618 boolean showNewTab = mTabControl.canCreateNewTab();
1619 MenuItem newTabItem
1620 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001621 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001622 ? R.string.contextmenu_openlink_newwindow_background
1623 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001624 newTabItem.setVisible(showNewTab);
1625 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001626 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1627 newTabItem.setOnMenuItemClickListener(
1628 new MenuItem.OnMenuItemClickListener() {
1629 @Override
1630 public boolean onMenuItemClick(MenuItem item) {
1631 final HashMap<String, WebView> hrefMap =
1632 new HashMap<String, WebView>();
1633 hrefMap.put("webview", webview);
1634 final Message msg = mHandler.obtainMessage(
1635 FOCUS_NODE_HREF,
1636 R.id.open_newtab_context_menu_id,
1637 0, hrefMap);
1638 webview.requestFocusNodeHref(msg);
1639 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001640 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001641 });
1642 } else {
1643 newTabItem.setOnMenuItemClickListener(
1644 new MenuItem.OnMenuItemClickListener() {
1645 @Override
1646 public boolean onMenuItemClick(MenuItem item) {
1647 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001648 openTab(extra, parent,
1649 !mSettings.openInBackground(),
1650 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001651 return true;
1652 }
1653 });
1654 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001655 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001656 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1657 menu.setHeaderTitle(navigationUrl);
1658 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1659
1660 if (itemUrl != null) {
1661 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1662 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1663 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1664 @Override
1665 public boolean onMenuItemClick(MenuItem item) {
1666 final Intent intent = new Intent(Controller.this
1667 .getContext(),
1668 AddMyNavigationPage.class);
1669 Bundle bundle = new Bundle();
1670 String url = Uri.decode(navigationUrl);
1671 bundle.putBoolean("isAdding", false);
1672 bundle.putString("url", url);
1673 bundle.putString("name", getNameFromUrl(url));
1674 intent.putExtra("websites", bundle);
1675 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1676 return false;
1677 }
1678 });
1679 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1680 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1681 @Override
1682 public boolean onMenuItemClick(MenuItem item) {
1683 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1684 return false;
1685 }
1686 });
1687 }
1688 } else {
1689 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1690 }
1691 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001692 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1693 break;
1694 }
1695 // otherwise fall through to handle image part
1696 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001697 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1698 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001699 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1700 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001701 shareItem.setOnMenuItemClickListener(
1702 new MenuItem.OnMenuItemClickListener() {
1703 @Override
1704 public boolean onMenuItemClick(MenuItem item) {
1705 sharePage(mActivity, null, extra, null,
1706 null);
1707 return true;
1708 }
1709 }
1710 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001711 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001712 menu.findItem(R.id.view_image_context_menu_id)
1713 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1714 @Override
1715 public boolean onMenuItemClick(MenuItem item) {
1716 openTab(extra, mTabControl.getCurrentTab(), true, true);
1717 return false;
1718 }
1719 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001720 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1721 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1722 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001723 menu.findItem(R.id.set_wallpaper_context_menu_id).
1724 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1725 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001726 break;
1727
1728 default:
1729 Log.w(LOGTAG, "We should not get here.");
1730 break;
1731 }
1732 //update the ui
1733 mUi.onContextMenuCreated(menu);
1734 }
1735
kaiyiz6e5b3e02013-08-19 20:02:01 +08001736 public void startAddMyNavigation(String url) {
1737 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1738 Bundle bundle = new Bundle();
1739 bundle.putBoolean("isAdding", true);
1740 bundle.putString("url", url);
1741 bundle.putString("name", getNameFromUrl(url));
1742 intent.putExtra("websites", bundle);
1743 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1744 }
1745
1746 private void showMyNavigationDeleteDialog(final String itemUrl) {
1747 new AlertDialog.Builder(this.getContext())
1748 .setTitle(R.string.my_navigation_delete_label)
1749 .setIcon(android.R.drawable.ic_dialog_alert)
1750 .setMessage(R.string.my_navigation_delete_msg)
1751 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1752 @Override
1753 public void onClick(DialogInterface dialog, int whichButton) {
1754 deleteMyNavigationItem(itemUrl);
1755 }
1756 })
1757 .setNegativeButton(R.string.cancel, null)
1758 .show();
1759 }
1760
1761 private void deleteMyNavigationItem(final String itemUrl) {
1762 ContentResolver cr = this.getContext().getContentResolver();
1763 Cursor cursor = null;
1764
1765 try {
1766 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1767 new String[] {
1768 MyNavigationUtil.ID
1769 }, "url = ?", new String[] {
1770 itemUrl
1771 }, null);
1772 if (null != cursor && cursor.moveToFirst()) {
1773 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1774 cursor.getLong(0));
1775
1776 ContentValues values = new ContentValues();
1777 values.put(MyNavigationUtil.TITLE, "");
1778 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1779 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1780 ByteArrayOutputStream os = new ByteArrayOutputStream();
1781 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1782 R.raw.my_navigation_add);
1783 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1784 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1785 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1786 cr.update(uri, values, null, null);
1787 } else {
1788 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1789 }
1790 } catch (IllegalStateException e) {
1791 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1792 } finally {
1793 if (null != cursor) {
1794 cursor.close();
1795 }
1796 }
1797
1798 if (getCurrentTopWebView() != null) {
1799 getCurrentTopWebView().reload();
1800 }
1801 }
1802
1803 private String getNameFromUrl(String itemUrl) {
1804 ContentResolver cr = this.getContext().getContentResolver();
1805 Cursor cursor = null;
1806 String name = null;
1807
1808 try {
1809 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1810 new String[] {
1811 MyNavigationUtil.TITLE
1812 }, "url = ?", new String[] {
1813 itemUrl
1814 }, null);
1815 if (null != cursor && cursor.moveToFirst()) {
1816 name = cursor.getString(0);
1817 } else {
1818 Log.e(LOGTAG, "this item does not exist!");
1819 }
1820 } catch (IllegalStateException e) {
1821 Log.e(LOGTAG, "getNameFromUrl", e);
1822 } finally {
1823 if (null != cursor) {
1824 cursor.close();
1825 }
1826 }
1827 return name;
1828 }
1829
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001830 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001831 final ContentResolver cr = mActivity.getContentResolver();
1832 new AsyncTask<Void, Void, Void>() {
1833 @Override
1834 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001835 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1836 if(!isMyNavigationUrl)
1837 return null;
1838
kaiyiz6e5b3e02013-08-19 20:02:01 +08001839 ContentResolver cr = mActivity.getContentResolver();
1840 Cursor cursor = null;
1841 try {
1842 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1843 new String[] {
1844 MyNavigationUtil.ID
1845 }, "url = ?", new String[] {
1846 itemUrl
1847 }, null);
1848 if (null != cursor && cursor.moveToFirst()) {
1849 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001850 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001851
1852 ContentValues values = new ContentValues();
1853 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1854 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1855 cursor.getLong(0));
1856 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1857 cr.update(uri, values, null, null);
1858 os.close();
1859 }
1860 } catch (IllegalStateException e) {
1861 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1862 } catch (IOException e) {
1863 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1864 } finally {
1865 if (null != cursor) {
1866 cursor.close();
1867 }
1868 }
1869 return null;
1870 }
1871 }.execute();
1872 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001873 /**
1874 * As the menu can be open when loading state changes
1875 * we must manually update the state of the stop/reload menu
1876 * item
1877 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001878 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001879 if (menu == null) {
1880 return;
1881 }
1882 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001883 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001884 menu.findItem(R.id.stop_menu_id):
1885 menu.findItem(R.id.reload_menu_id);
1886 if (src != null) {
1887 dest.setIcon(src.getIcon());
1888 dest.setTitle(src.getTitle());
1889 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001890 mActivity.invalidateOptionsMenu();
1891 }
1892
1893 public void invalidateOptionsMenu() {
1894 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001895 }
1896
John Reck9c35b9c2012-05-30 10:08:50 -07001897 @Override
1898 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001899 // Software menu key (toolbar key)
1900 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1901 return true;
1902 }
1903
1904 @Override
1905 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001906 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001907 // hold on to the menu reference here; it is used by the page callbacks
1908 // to update the menu based on loading state
1909 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001910 // Note: setVisible will decide whether an item is visible; while
1911 // setEnabled() will decide whether an item is enabled, which also means
1912 // whether the matching shortcut key will function.
1913 switch (mMenuState) {
1914 case EMPTY_MENU:
1915 if (mCurrentMenuState != mMenuState) {
1916 menu.setGroupVisible(R.id.MAIN_MENU, false);
1917 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1918 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1919 }
1920 break;
1921 default:
1922 if (mCurrentMenuState != mMenuState) {
1923 menu.setGroupVisible(R.id.MAIN_MENU, true);
1924 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1925 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1926 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001927 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001928 break;
1929 }
1930 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001931 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001932
1933 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001934 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1935 EditBookmarksRestriction.getInstance()
1936 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001937 }
1938
1939 private void setMenuItemVisibility(Menu menu, int id,
1940 boolean visibility) {
1941 MenuItem item = menu.findItem(id);
1942 if (item != null) {
1943 item.setVisible(visibility);
1944 }
1945 }
1946
Sagar Dhawanef06f562015-09-07 15:23:22 +02001947 private int lookupBookmark( String url) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001948 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001949 int count = 0;
1950 Cursor cursor = null;
1951 try {
1952 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1953 BookmarksLoader.PROJECTION,
Sagar Dhawanef06f562015-09-07 15:23:22 +02001954 "url = ?",
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001955 new String[] {
Sagar Dhawanef06f562015-09-07 15:23:22 +02001956 url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001957 },
1958 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001959
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001960 if (cursor != null)
1961 count = cursor.getCount();
1962
1963 } catch (IllegalStateException e) {
1964 Log.e(LOGTAG, "lookupBookmark ", e);
1965 } finally {
1966 if (null != cursor) {
1967 cursor.close();
1968 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001969 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001970 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001971 }
1972
1973 private void resetMenuItems(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001974 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1975
1976 WebView w = getCurrentTopWebView();
1977 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1978
1979 String title = w.getTitle();
1980 String url = w.getUrl();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001981 mCurrentPageBookmarked = (lookupBookmark(url) > 0);
Pankaj Garg634bf432015-07-13 09:54:21 -07001982 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001983 bookmark_icon.setChecked(true);
1984 } else {
1985 bookmark_icon.setChecked(false);
1986 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001987
1988 // update reader mode checkbox
1989 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1990 readerSwitcher.setVisible(false);
1991 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 }
1993
Michael Kolb4bf79712011-07-14 14:18:12 -07001994 @Override
1995 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001996 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001997 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001998 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001999 // Following flag is used to identify schemes for which the LIVE_MENU
2000 // items defined in res/menu/browser.xml should be enabled
2001 boolean isLiveScheme = false;
2002 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002003 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07002004
2005 boolean isDistillable = false;
2006 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002007 resetMenuItems(menu);
2008
Michael Kolb4bf79712011-07-14 14:18:12 -07002009 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002010 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002011 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002012 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002013 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002014 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002015 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07002016
2017 isDistillable = tab.isDistillable();
2018 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07002019 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002020
2021 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2022 forward.setEnabled(canGoForward);
2023
Michael Kolb4bf79712011-07-14 14:18:12 -07002024 // decide whether to show the share link option
2025 PackageManager pm = mActivity.getPackageManager();
2026 Intent send = new Intent(Intent.ACTION_SEND);
2027 send.setType("text/plain");
2028 ResolveInfo ri = pm.resolveActivity(send,
2029 PackageManager.MATCH_DEFAULT_ONLY);
2030 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2031
2032 boolean isNavDump = mSettings.enableNavDump();
2033 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2034 nav.setVisible(isNavDump);
2035 nav.setEnabled(isNavDump);
2036
2037 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002038 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2039 uaSwitcher.setChecked(isDesktopUa);
Sagar Dhawanef06f562015-09-07 15:23:22 +02002040 setMenuItemVisibility(menu, R.id.find_menu_id, isLive);
2041 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002042 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002043 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002044 setMenuItemVisibility(menu, R.id.add_to_homescreen,
Sagar Dhawanef06f562015-09-07 15:23:22 +02002045 isLive && isPageFinished);
Pankaj Garg49b79252014-11-07 17:33:41 -08002046 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002047 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002048 // history and snapshots item are the members of COMBO menu group,
2049 // so if show history item, only make snapshots item invisible.
2050 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002051
Ze G Riande2a675c22015-06-03 11:15:24 -07002052
2053 // update reader mode checkbox
2054 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2055 // The reader mode checkbox is hidden only
2056 // when the current page is neither distillable nor distilled
2057 readerSwitcher.setVisible(isDistillable || isDistilled);
2058 readerSwitcher.setChecked(isDistilled);
2059
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002060 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002061 }
2062
Pankaj Gargf49e0222015-09-01 12:19:13 -07002063 private Bitmap cropAndScaleBitmap(Bitmap bm, int width, int height) {
2064 if (width == 0 || height == 0 || bm == null)
2065 return bm;
2066
2067 Bitmap cropped;
2068
2069 if (bm.getHeight() > bm.getWidth()) {
2070 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
2071 bm.getWidth() * height / width, null, true);
2072 } else {
2073 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getHeight() * width / height,
2074 bm.getHeight(), null, true);
2075 }
2076
2077 Bitmap scaled = Bitmap.createScaledBitmap(cropped, width, height, true);
2078 cropped.recycle();
2079 return scaled;
2080 }
2081
John Reck9c35b9c2012-05-30 10:08:50 -07002082 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002083 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 if (null == getCurrentTopWebView()) {
2085 return false;
2086 }
2087 if (mMenuIsDown) {
2088 // The shortcut action consumes the MENU. Even if it is still down,
2089 // it won't trigger the next shortcut action. In the case of the
2090 // shortcut action triggering a new activity, like Bookmarks, we
2091 // won't get onKeyUp for MENU. So it is important to reset it here.
2092 mMenuIsDown = false;
2093 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002094 if (mUi.onOptionsItemSelected(item)) {
2095 // ui callback handled it
2096 return true;
2097 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002098 switch (item.getItemId()) {
2099 // -- Main menu
2100 case R.id.new_tab_menu_id:
2101 openTabToHomePage();
2102 break;
2103
2104 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002105 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002106 break;
2107
2108 case R.id.goto_menu_id:
2109 editUrl();
2110 break;
2111
2112 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002113 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2114 break;
2115
Michael Kolb315d5022011-10-13 12:47:11 -07002116 case R.id.snapshots_menu_id:
2117 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002118 break;
2119
Pankaj Garg49b79252014-11-07 17:33:41 -08002120 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002121 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002122 break;
2123
2124 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002125 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 stopLoading();
2127 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002128 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002129 getCurrentTopWebView().reload();
2130 }
2131 break;
2132
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002134 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002135 break;
2136
2137 case R.id.close_menu_id:
2138 // Close the subwindow if it exists.
2139 if (mTabControl.getCurrentSubWindow() != null) {
2140 dismissSubWindow(mTabControl.getCurrentTab());
2141 break;
2142 }
2143 closeCurrentTab();
2144 break;
2145
kaiyiza8b6dbb2013-07-29 18:11:22 +08002146 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002147 Object[] params = { new String("persist.debug.browsermonkeytest")};
2148 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002149 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002150 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002151 if (ret != null && ret.equals("enable"))
2152 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002153 if (BrowserConfig.getInstance(getContext())
2154 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2155 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002156 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002157 case R.id.homepage_menu_id:
2158 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002159 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002160 break;
2161
2162 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002163 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002164 break;
2165
2166 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002167 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002168 break;
2169
John Reck2bc80422011-06-30 15:11:49 -07002170 case R.id.save_snapshot_menu_id:
2171 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002172 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002173 createScreenshotAsync(
Pankaj Gargf49e0222015-09-01 12:19:13 -07002174 source,
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002175 new ValueCallback<Bitmap>() {
2176 @Override
2177 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002178 Bitmap bm = cropAndScaleBitmap(bitmap,
2179 getDesiredThumbnailWidth(mActivity),
2180 getDesiredThumbnailHeight(mActivity));
2181 new SaveSnapshotTask(source, bm).execute();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002182 }
2183 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002184 break;
2185
Michael Kolb8233fac2010-10-26 16:08:53 -07002186 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002187 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002188 break;
2189
John Recke1a03a32011-09-14 17:04:16 -07002190 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002191 // passing null to distinguish between
2192 // "go live" button and navigating a web page
2193 // on a snapshot tab
2194 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002195 case R.id.share_page_menu_id:
2196 Tab currentTab = mTabControl.getCurrentTab();
2197 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002198 return false;
2199 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002200 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002201 break;
2202
2203 case R.id.dump_nav_menu_id:
2204 getCurrentTopWebView().debugDump();
2205 break;
2206
Michael Kolb8233fac2010-10-26 16:08:53 -07002207 case R.id.zoom_in_menu_id:
2208 getCurrentTopWebView().zoomIn();
2209 break;
2210
2211 case R.id.zoom_out_menu_id:
2212 getCurrentTopWebView().zoomOut();
2213 break;
2214
2215 case R.id.view_downloads_menu_id:
2216 viewDownloads();
2217 break;
2218
John Reck42229bc2011-08-19 13:26:43 -07002219 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002220 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002221 break;
2222
Ze G Riande2a675c22015-06-03 11:15:24 -07002223 case R.id.reader_mode_menu_id:
2224 toggleReaderMode();
2225 break;
2226
Michael Kolb8233fac2010-10-26 16:08:53 -07002227 case R.id.window_one_menu_id:
2228 case R.id.window_two_menu_id:
2229 case R.id.window_three_menu_id:
2230 case R.id.window_four_menu_id:
2231 case R.id.window_five_menu_id:
2232 case R.id.window_six_menu_id:
2233 case R.id.window_seven_menu_id:
2234 case R.id.window_eight_menu_id:
2235 {
2236 int menuid = item.getItemId();
2237 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2238 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2239 Tab desiredTab = mTabControl.getTab(id);
2240 if (desiredTab != null &&
2241 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002242 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002243 }
2244 break;
2245 }
2246 }
2247 }
2248 break;
2249
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002250 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002251 Bundle bundle = new Bundle();
2252 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2253 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2254 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2255 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2256 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002257 break;
2258
Pankaj Garg49b79252014-11-07 17:33:41 -08002259 case R.id.add_to_homescreen:
2260 final WebView w = getCurrentTopWebView();
2261 final EditText input = new EditText(getContext());
2262 input.setText(w.getTitle());
2263 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002264 .setTitle(getContext().getResources().getString(
2265 R.string.add_to_homescreen))
2266 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002267 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002268 .setPositiveButton(getContext().getResources().getString(
2269 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002270 public void onClick(DialogInterface dialog, int whichButton) {
2271 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2272 getContext(),
2273 w.getUrl(),
2274 input.getText().toString(),
2275 w.getViewportBitmap(),
2276 w.getFavicon()));
2277
2278 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2279 .addCategory(Intent.CATEGORY_HOME));
2280 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002281 .setNegativeButton(getContext().getResources().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07002282 R.string.cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002283 public void onClick(DialogInterface dialog, int whichButton) {
2284 // Do nothing.
2285 }
2286 })
2287 .show();
2288 break;
2289
Michael Kolb8233fac2010-10-26 16:08:53 -07002290 default:
2291 return false;
2292 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002293 return true;
2294 }
2295
John Reck68234a92012-04-19 15:27:12 -07002296 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2297 implements OnCancelListener {
2298
2299 private Tab mTab;
2300 private Dialog mProgressDialog;
2301 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002302 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002303
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002304 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002305 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002306 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002307 }
2308
2309 @Override
2310 protected void onPreExecute() {
2311 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2312 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2313 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002314 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002315 }
2316
2317 @Override
2318 protected Long doInBackground(Void... params) {
2319 if (!mTab.saveViewState(mValues)) {
2320 return null;
2321 }
2322 if (isCancelled()) {
2323 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2324 File file = mActivity.getFileStreamPath(path);
2325 if (!file.delete()) {
2326 file.deleteOnExit();
2327 }
2328 return null;
2329 }
2330 final ContentResolver cr = mActivity.getContentResolver();
2331 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2332 if (result == null) {
2333 return null;
2334 }
2335 long id = ContentUris.parseId(result);
2336 return id;
2337 }
2338
2339 @Override
2340 protected void onPostExecute(Long id) {
2341 if (isCancelled()) {
2342 return;
2343 }
2344 mProgressDialog.dismiss();
2345 if (id == null) {
2346 Toast.makeText(mActivity, R.string.snapshot_failed,
2347 Toast.LENGTH_SHORT).show();
2348 return;
2349 }
2350 Bundle b = new Bundle();
2351 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2352 mUi.showComboView(ComboViews.Snapshots, b);
2353 }
2354
2355 @Override
2356 public void onCancel(DialogInterface dialog) {
2357 cancel(true);
2358 }
2359 }
2360
Michael Kolb80f75082012-04-10 10:50:06 -07002361 @Override
2362 public void toggleUserAgent() {
2363 WebView web = getCurrentWebView();
2364 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002365 }
2366
Ze G Riande2a675c22015-06-03 11:15:24 -07002367 // This function calls the method in the webview to enable/disable
2368 // the reader mode through the DOM distiller
2369 public void toggleReaderMode() {
2370 Tab t = mTabControl.getCurrentTab();
2371 if (t.isDistilled()) {
2372 closeTab(t);
2373 } else if (t.isDistillable()) {
2374 openTab(t.getDistilledUrl(), false, true, false, t);
2375 }
2376 }
2377
Michael Kolb80f75082012-04-10 10:50:06 -07002378 @Override
2379 public void findOnPage() {
2380 getCurrentTopWebView().showFindDialog(null, true);
2381 }
2382
2383 @Override
2384 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002385 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002386 }
2387
2388 @Override
2389 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002390 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002391 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2392 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002393 }
2394 else {
2395 WebView w = getCurrentTopWebView();
2396 if (w == null)
2397 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002398 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002399 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002400 }
Michael Kolb80f75082012-04-10 10:50:06 -07002401 }
2402
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002403 public boolean goLive(String url) {
2404 if (!getCurrentTab().isSnapshot())
2405 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002406 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002407
2408 if (url == null) { // "go live" button was clicked
2409 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002410 closeTab(t);
2411 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002412 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002413 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002414 return true;
John Recke1a03a32011-09-14 17:04:16 -07002415 }
2416
luxiaolb40014b2013-07-19 10:01:43 +08002417 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002418 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002419 new AlertDialog.Builder(activity)
2420 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002421 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002422 .setMessage(R.string.exit_browser_msg)
2423 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2424 public void onClick(DialogInterface dialog, int which) {
2425 activity.moveTaskToBack(true);
2426 dialog.dismiss();
2427 }
2428 })
2429 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2430 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002431 mCrashRecoveryHandler.clearState(true);
2432 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002433 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002434 dialog.dismiss();
2435 }
2436 })
2437 .show();
2438 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002439
Michael Kolb315d5022011-10-13 12:47:11 -07002440 @Override
2441 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002442 }
2443
John Reck9c35b9c2012-05-30 10:08:50 -07002444 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002445 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002446 // Let the History and Bookmark fragments handle menus they created.
2447 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2448 return false;
2449 }
2450
Michael Kolb8233fac2010-10-26 16:08:53 -07002451 int id = item.getItemId();
2452 boolean result = true;
2453 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002454 // -- Browser context menu
2455 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002456 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002457 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002458 case R.id.copy_link_context_menu_id:
2459 final WebView webView = getCurrentTopWebView();
2460 if (null == webView) {
2461 result = false;
2462 break;
2463 }
2464 final HashMap<String, WebView> hrefMap =
2465 new HashMap<String, WebView>();
2466 hrefMap.put("webview", webView);
2467 final Message msg = mHandler.obtainMessage(
2468 FOCUS_NODE_HREF, id, 0, hrefMap);
2469 webView.requestFocusNodeHref(msg);
2470 break;
2471
2472 default:
2473 // For other context menus
2474 result = onOptionsItemSelected(item);
2475 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002476 return result;
2477 }
2478
2479 /**
2480 * support programmatically opening the context menu
2481 */
2482 public void openContextMenu(View view) {
2483 mActivity.openContextMenu(view);
2484 }
2485
2486 /**
2487 * programmatically open the options menu
2488 */
2489 public void openOptionsMenu() {
2490 mActivity.openOptionsMenu();
2491 }
2492
John Reck9c35b9c2012-05-30 10:08:50 -07002493 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002494 public boolean onMenuOpened(int featureId, Menu menu) {
2495 if (mOptionsMenuOpen) {
2496 if (mConfigChanged) {
2497 // We do not need to make any changes to the state of the
2498 // title bar, since the only thing that happened was a
2499 // change in orientation
2500 mConfigChanged = false;
2501 } else {
2502 if (!mExtendedMenuOpen) {
2503 mExtendedMenuOpen = true;
2504 mUi.onExtendedMenuOpened();
2505 } else {
2506 // Switching the menu back to icon view, so show the
2507 // title bar once again.
2508 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002509 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002510 }
2511 }
2512 } else {
2513 // The options menu is closed, so open it, and show the title
2514 mOptionsMenuOpen = true;
2515 mConfigChanged = false;
2516 mExtendedMenuOpen = false;
2517 mUi.onOptionsMenuOpened();
2518 }
2519 return true;
2520 }
2521
John Reck9c35b9c2012-05-30 10:08:50 -07002522 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002523 public void onOptionsMenuClosed(Menu menu) {
2524 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002525 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002526 }
2527
John Reck9c35b9c2012-05-30 10:08:50 -07002528 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002529 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002530 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002531 }
2532
2533 // Helper method for getting the top window.
2534 @Override
2535 public WebView getCurrentTopWebView() {
2536 return mTabControl.getCurrentTopWebView();
2537 }
2538
2539 @Override
2540 public WebView getCurrentWebView() {
2541 return mTabControl.getCurrentWebView();
2542 }
2543
2544 /*
2545 * This method is called as a result of the user selecting the options
2546 * menu to see the download window. It shows the download window on top of
2547 * the current window.
2548 */
2549 void viewDownloads() {
2550 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2551 mActivity.startActivity(intent);
2552 }
2553
John Reck30b065e2011-07-19 10:58:05 -07002554 int getActionModeHeight() {
2555 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2556 new int[] { android.R.attr.actionBarSize });
2557 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2558 actionBarSizeTypedArray.recycle();
2559 return size;
2560 }
2561
Michael Kolb8233fac2010-10-26 16:08:53 -07002562 // action mode
2563
John Reck9c35b9c2012-05-30 10:08:50 -07002564 @Override
2565 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002566 mUi.onActionModeStarted(mode);
2567 mActionMode = mode;
2568 }
2569
2570 /*
2571 * True if a custom ActionMode (i.e. find or select) is in use.
2572 */
2573 @Override
2574 public boolean isInCustomActionMode() {
2575 return mActionMode != null;
2576 }
2577
2578 /*
2579 * End the current ActionMode.
2580 */
2581 @Override
2582 public void endActionMode() {
2583 if (mActionMode != null) {
2584 mActionMode.finish();
2585 }
2586 }
2587
2588 /*
2589 * Called by find and select when they are finished. Replace title bars
2590 * as necessary.
2591 */
John Reck9c35b9c2012-05-30 10:08:50 -07002592 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002593 public void onActionModeFinished(ActionMode mode) {
2594 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002595 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002596 mActionMode = null;
2597 }
2598
2599 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002600 final Tab tab = getCurrentTab();
2601 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 }
2603
2604 // bookmark handling
2605
2606 /**
2607 * add the current page as a bookmark to the given folder id
2608 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002609 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002610 * bookmarked, and if it is, edit that bookmark. If false, and
2611 * the site is already bookmarked, do not attempt to edit the
2612 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002613 */
2614 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002615 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002616 WebView w = getCurrentTopWebView();
2617 if (w == null) {
2618 return null;
2619 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002620 Intent i = new Intent(mActivity,
2621 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002622 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2623 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002624 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002625 if (touchIconUrl != null) {
2626 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2627 WebSettings settings = w.getSettings();
2628 if (settings != null) {
2629 i.putExtra(AddBookmarkPage.USER_AGENT,
2630 settings.getUserAgentString());
2631 }
2632 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002633 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002634 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002635 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002636 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2637 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002638 // Put the dialog at the upper right of the screen, covering the
2639 // star on the title bar.
2640 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002641 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002642 }
2643
2644 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002645 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002646 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002647 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002648 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002649 }
2650
Vivek Sekharb54614f2014-05-01 19:03:37 -07002651 @Override
2652 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2653 boolean capture) {
2654 mUploadHandler = new UploadHandler(this);
2655 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2656 }
2657
Michael Kolb8233fac2010-10-26 16:08:53 -07002658 // thumbnails
2659
2660 /**
2661 * Return the desired width for thumbnail screenshots, which are stored in
2662 * the database, and used on the bookmarks screen.
2663 * @param context Context for finding out the density of the screen.
2664 * @return desired width for thumbnail screenshot.
2665 */
2666 static int getDesiredThumbnailWidth(Context context) {
2667 return context.getResources().getDimensionPixelOffset(
2668 R.dimen.bookmarkThumbnailWidth);
2669 }
2670
2671 /**
2672 * Return the desired height for thumbnail screenshots, which are stored in
2673 * the database, and used on the bookmarks screen.
2674 * @param context Context for finding out the density of the screen.
2675 * @return desired height for thumbnail screenshot.
2676 */
2677 static int getDesiredThumbnailHeight(Context context) {
2678 return context.getResources().getDimensionPixelOffset(
2679 R.dimen.bookmarkThumbnailHeight);
2680 }
2681
Pankaj Gargf49e0222015-09-01 12:19:13 -07002682 void createScreenshotAsync(Tab tab, final ValueCallback<Bitmap> cb) {
2683 if (tab == null) {
2684 cb.onReceiveValue(null);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002685 return;
2686 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002687
2688 tab.capture();
2689
2690 synchronized (mThumbnailCbList) {
2691 mThumbnailCbList.add(cb);
2692 }
2693 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002694
Michael Kolb8233fac2010-10-26 16:08:53 -07002695 private class Copy implements OnMenuItemClickListener {
2696 private CharSequence mText;
2697
John Reck9c35b9c2012-05-30 10:08:50 -07002698 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002699 public boolean onMenuItemClick(MenuItem item) {
2700 copy(mText);
2701 return true;
2702 }
2703
2704 public Copy(CharSequence toCopy) {
2705 mText = toCopy;
2706 }
2707 }
2708
Leon Scroggins63c02662010-11-18 15:16:27 -05002709 private static class Download implements OnMenuItemClickListener {
2710 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002711 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002712 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002713 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002714 private static final String FALLBACK_EXTENSION = "dat";
2715 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002716
John Reck9c35b9c2012-05-30 10:08:50 -07002717 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002718 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002719 if (DataUri.isDataUri(mText)) {
2720 saveDataUri();
2721 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002722 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08002723 null, null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002724 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002725 return true;
2726 }
2727
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002728 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2729 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002730 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002731 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002732 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002733 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002734 }
George Mount387d45d2011-10-07 15:57:53 -07002735
2736 /**
2737 * Treats mText as a data URI and writes its contents to a file
2738 * based on the current time.
2739 */
2740 private void saveDataUri() {
2741 FileOutputStream outputStream = null;
2742 try {
2743 DataUri uri = new DataUri(mText);
2744 File target = getTarget(uri);
2745 outputStream = new FileOutputStream(target);
2746 outputStream.write(uri.getData());
2747 final DownloadManager manager =
2748 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2749 manager.addCompletedDownload(target.getName(),
2750 mActivity.getTitle().toString(), false,
2751 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002752 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002753 } catch (IOException e) {
2754 Log.e(LOGTAG, "Could not save data URL");
2755 } finally {
2756 if (outputStream != null) {
2757 try {
2758 outputStream.close();
2759 } catch (IOException e) {
2760 // ignore close errors
2761 }
2762 }
2763 }
2764 }
2765
2766 /**
2767 * Creates a File based on the current time stamp and uses
2768 * the mime type of the DataUri to get the extension.
2769 */
2770 private File getTarget(DataUri uri) throws IOException {
2771 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002772 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002773 String nameBase = format.format(new Date());
2774 String mimeType = uri.getMimeType();
2775 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2776 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2777 if (extension == null) {
2778 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2779 extension = FALLBACK_EXTENSION;
2780 }
2781 extension = "." + extension; // createTempFile needs the '.'
2782 File targetFile = File.createTempFile(nameBase, extension, dir);
2783 return targetFile;
2784 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002785 }
2786
Cary Clark8974d282010-11-22 10:46:05 -05002787 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002788 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002789
John Reck9c35b9c2012-05-30 10:08:50 -07002790 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002791 public boolean onMenuItemClick(MenuItem item) {
2792 if (mWebView != null) {
2793 return mWebView.selectText();
2794 }
2795 return false;
2796 }
2797
2798 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002799 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002800 }
2801
2802 }
2803
Michael Kolb8233fac2010-10-26 16:08:53 -07002804 /********************** TODO: UI stuff *****************************/
2805
2806 // these methods have been copied, they still need to be cleaned up
2807
2808 /****************** tabs ***************************************************/
2809
2810 // basic tab interactions:
2811
2812 // it is assumed that tabcontrol already knows about the tab
2813 protected void addTab(Tab tab) {
2814 mUi.addTab(tab);
2815 }
2816
2817 protected void removeTab(Tab tab) {
2818 mUi.removeTab(tab);
2819 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002820 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002821 }
2822
Michael Kolbf2055602011-04-09 17:20:03 -07002823 @Override
2824 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002825 // monkey protection against delayed start
2826 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002827
2828 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2829 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002830 mTabControl.setCurrentTab(tab);
2831 // the tab is guaranteed to have a webview after setCurrentTab
2832 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002833
2834
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002835 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002836 //Purge active tabs
2837 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002838 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002839 }
2840
John Reck8bcafc12011-08-29 16:43:02 -07002841 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002842 Tab current = mTabControl.getCurrentTab();
2843 if (current != null
2844 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002845 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002846 }
2847 }
2848
John Reck26b18322011-06-21 13:08:58 -07002849 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002850 //Cancel navscreen request
2851 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002852 // Dismiss the subwindow if applicable.
2853 dismissSubWindow(appTab);
2854 // Since we might kill the WebView, remove it from the
2855 // content view first.
2856 mUi.detachTab(appTab);
2857 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002858 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002859 // TODO: analyze why the remove and add are necessary
2860 mUi.attachTab(appTab);
2861 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002862 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002863 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002864 } else {
2865 // If the tab was the current tab, we have to attach
2866 // it to the view system again.
2867 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002868 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002869 }
2870 }
2871
2872 // Remove the sub window if it exists. Also called by TabControl when the
2873 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002874 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002875 public void dismissSubWindow(Tab tab) {
2876 removeSubWindow(tab);
2877 // dismiss the subwindow. This will destroy the WebView.
2878 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002879 WebView wv = getCurrentTopWebView();
2880 if (wv != null) {
2881 wv.requestFocus();
2882 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002883 }
2884
2885 @Override
2886 public void removeSubWindow(Tab t) {
2887 if (t.getSubWebView() != null) {
2888 mUi.removeSubWindow(t.getSubViewContainer());
2889 }
2890 }
2891
2892 @Override
2893 public void attachSubWindow(Tab tab) {
2894 if (tab.getSubWebView() != null) {
2895 mUi.attachSubWindow(tab.getSubViewContainer());
2896 getCurrentTopWebView().requestFocus();
2897 }
2898 }
2899
Mathew Inwood29721c22011-06-29 17:55:24 +01002900 private Tab showPreloadedTab(final UrlData urlData) {
2901 if (!urlData.isPreloaded()) {
2902 return null;
2903 }
2904 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2905 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2906 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002907 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002908 // Could not submit query. Fallback to regular tab creation
2909 tabControl.destroy();
2910 return null;
2911 }
2912 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002913 // check tab count and make room for new tab
2914 if (!mTabControl.canCreateNewTab()) {
2915 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2916 if (leastUsed != null) {
2917 closeTab(leastUsed);
2918 }
2919 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002920 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002921 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002922 mTabControl.addPreloadedTab(t);
2923 addTab(t);
2924 setActiveTab(t);
2925 return t;
2926 }
2927
Michael Kolb7bcafde2011-05-09 13:55:59 -07002928 // open a non inconito tab with the given url data
2929 // and set as active tab
2930 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002931 Tab tab = showPreloadedTab(urlData);
2932 if (tab == null) {
2933 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002934 if ((tab != null) && !urlData.isEmpty()) {
2935 loadUrlDataIn(tab, urlData);
2936 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002937 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002938 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002939 }
2940
Michael Kolb843510f2010-12-09 10:51:49 -08002941 @Override
2942 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002943 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002944 }
2945
Michael Kolb8233fac2010-10-26 16:08:53 -07002946 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002947 public Tab openIncognitoTab() {
2948 return openTab(INCOGNITO_URI, true, true, false);
2949 }
2950
2951 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002952 public Tab openTab(String url, boolean incognito, boolean setActive,
2953 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002954 return openTab(url, incognito, setActive, useCurrent, null);
2955 }
2956
2957 @Override
2958 public Tab openTab(String url, Tab parent, boolean setActive,
2959 boolean useCurrent) {
2960 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2961 setActive, useCurrent, parent);
2962 }
2963
2964 public Tab openTab(String url, boolean incognito, boolean setActive,
2965 boolean useCurrent, Tab parent) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002966 boolean change_tabs = false;
2967 if (setActive) {
2968 Tab currentTab = mTabControl.getCurrentTab();
2969 if (currentTab != null) {
2970 change_tabs = setActive;
2971 setActive = false;
Sagar Dhawan9a1d8e72015-09-11 17:14:38 -07002972 if (mUi instanceof PhoneUi)
2973 currentTab.capture();
Pankaj Gargf49e0222015-09-01 12:19:13 -07002974 }
2975 }
2976
Michael Kolb7bcafde2011-05-09 13:55:59 -07002977 Tab tab = createNewTab(incognito, setActive, useCurrent);
2978 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002979 if (parent instanceof SnapshotTab) {
2980 addTab(tab);
2981 if (setActive)
2982 setActiveTab(tab);
2983 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002984 parent.addChildTab(tab);
2985 }
Michael Kolb519d2282011-05-09 17:03:19 -07002986 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002987 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002988 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002989 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002990
2991 if (change_tabs) {
Sagar Dhawan20b1deb2015-10-12 11:13:18 -07002992 setActiveTab(tab);
Pankaj Gargf49e0222015-09-01 12:19:13 -07002993 synchronized (mThumbnailCbList) {
2994 startCaptureTimer();
2995 mLatestCreatedTab = tab;
2996 mThumbnailCbList.add(new ValueCallback<Bitmap>() {
2997 @Override
2998 public void onReceiveValue(Bitmap bitmap) {
2999 synchronized (mThumbnailCbList) {
3000 if (mLatestCreatedTab != null) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07003001 mLatestCreatedTab = null;
3002 }
3003 stopCaptureTimer();
3004 }
3005 }
3006 });
3007 }
3008 }
3009
Michael Kolb7bcafde2011-05-09 13:55:59 -07003010 return tab;
3011 }
3012
Pankaj Gargf49e0222015-09-01 12:19:13 -07003013 private void startCaptureTimer() {
3014 mCaptureTimer = new CountDownTimer(mCaptureMaxWaitMS, mCaptureMaxWaitMS) {
3015 @Override
3016 public void onTick(long millisUntilFinished) {
3017 // Do nothing
3018 }
3019
3020 @Override
3021 public void onFinish() {
3022 synchronized (mThumbnailCbList) {
3023 Log.e(LOGTAG, "Screen capture timed out while opening new tab");
3024 if (mLatestCreatedTab != null) {
3025 setActiveTab(mLatestCreatedTab);
3026 mLatestCreatedTab = null;
3027 }
3028 }
3029 }
3030 }.start();
3031 }
3032
3033 private void stopCaptureTimer() {
3034 if (mCaptureTimer != null) {
3035 mCaptureTimer.cancel();
3036 mCaptureTimer = null;
3037 }
3038 }
3039
3040
Michael Kolb7bcafde2011-05-09 13:55:59 -07003041 // this method will attempt to create a new tab
3042 // incognito: private browsing tab
3043 // setActive: ste tab as current tab
3044 // useCurrent: if no new tab can be created, return current tab
3045 private Tab createNewTab(boolean incognito, boolean setActive,
3046 boolean useCurrent) {
3047 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07003048 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
3049 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003050 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003051 if (mTabControl.canCreateNewTab()) {
3052 tab = mTabControl.createNewTab(incognito, !setActive);
3053 addTab(tab);
3054 if (setActive) {
3055 setActiveTab(tab);
3056 } else {
3057 tab.pause();
3058 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003059 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003060 if (useCurrent) {
3061 tab = mTabControl.getCurrentTab();
3062 reuseTab(tab, null);
3063 } else {
3064 mUi.showMaxTabsWarning();
3065 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003066 }
3067 }
3068 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003069 }
3070
John Reck2bc80422011-06-30 15:11:49 -07003071 @Override
3072 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3073 SnapshotTab tab = null;
3074 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07003075 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07003076 addTab(tab);
3077 if (setActive) {
3078 setActiveTab(tab);
3079 }
3080 } else {
3081 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003082 }
3083 return tab;
3084 }
3085
Michael Kolb8233fac2010-10-26 16:08:53 -07003086 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003087 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003088 * @return boolean True if we successfully switched to a different tab. If
3089 * the indexth tab is null, or if that tab is the same as
3090 * the current one, return false.
3091 */
3092 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003093 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003094 Tab currentTab = mTabControl.getCurrentTab();
3095 if (tab == null || tab == currentTab) {
3096 return false;
3097 }
3098 setActiveTab(tab);
3099 return true;
3100 }
3101
3102 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003103 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003104 closeCurrentTab(false);
3105 }
3106
3107 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003108 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003109 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003110 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003111 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003112 return;
3113 }
Michael Kolbc831b632011-05-11 09:30:34 -07003114 final Tab current = mTabControl.getCurrentTab();
3115 final int pos = mTabControl.getCurrentPosition();
3116 Tab newTab = current.getParent();
3117 if (newTab == null) {
3118 newTab = mTabControl.getTab(pos + 1);
3119 if (newTab == null) {
3120 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003121 }
3122 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003123 if (andQuit) {
3124 mTabControl.setCurrentTab(newTab);
3125 closeTab(current);
3126 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003127 // Close window
3128 closeTab(current);
3129 }
3130 }
3131
3132 /**
3133 * Close the tab, remove its associated title bar, and adjust mTabControl's
3134 * current tab to a valid value.
3135 */
3136 @Override
3137 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003138 if (tab == mTabControl.getCurrentTab()) {
3139 closeCurrentTab();
3140 } else {
3141 removeTab(tab);
3142 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003143 }
3144
Afzal Najamd4e33312012-04-26 01:54:01 -04003145 /**
3146 * Close all tabs except the current one
3147 */
3148 @Override
3149 public void closeOtherTabs() {
3150 int inactiveTabs = mTabControl.getTabCount() - 1;
3151 for (int i = inactiveTabs; i >= 0; i--) {
3152 Tab tab = mTabControl.getTab(i);
3153 if (tab != mTabControl.getCurrentTab()) {
3154 removeTab(tab);
3155 }
3156 }
3157 }
3158
Michael Kolb8233fac2010-10-26 16:08:53 -07003159 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003160 protected void loadUrlFromContext(String url) {
3161 Tab tab = getCurrentTab();
3162 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003163 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003164 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003165 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003166 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003167 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003168 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003169 }
3170 }
3171 }
3172
3173 /**
3174 * Load the URL into the given WebView and update the title bar
3175 * to reflect the new load. Call this instead of WebView.loadUrl
3176 * directly.
3177 * @param view The WebView used to load url.
3178 * @param url The URL to load.
3179 */
John Reck71e51422011-07-01 16:49:28 -07003180 @Override
3181 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003182 loadUrl(tab, url, null);
3183 }
3184
3185 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3186 if (tab != null) {
3187 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003188 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003189 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003190 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003191 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003192 }
3193
3194 /**
3195 * Load UrlData into a Tab and update the title bar to reflect the new
3196 * load. Call this instead of UrlData.loadIn directly.
3197 * @param t The Tab used to load.
3198 * @param data The UrlData being loaded.
3199 */
3200 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003201 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003202 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003203 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003204 } else {
John Reck38b39652012-06-05 09:22:59 -07003205 if (t != null && data.mDisableUrlOverride) {
3206 t.disableUrlOverridingForLoad();
3207 }
John Reck26b18322011-06-21 13:08:58 -07003208 loadUrl(t, data.mUrl, data.mHeaders);
3209 }
3210 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003211 }
3212
John Reck30c714c2010-12-16 17:30:34 -08003213 @Override
3214 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003215 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003216 //In case of tab can go back (aka tab has navigation entry) do nothing
3217 //else just load homepage in current tab.
3218 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003219 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003220 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003221 }
3222
3223 void goBackOnePageOrQuit() {
3224 Tab current = mTabControl.getCurrentTab();
3225 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003226 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3227 showExitDialog(mActivity);
3228 } else {
3229 /*
3230 * Instead of finishing the activity, simply push this to the back
3231 * of the stack and let ActivityManager to choose the foreground
3232 * activity. As BrowserActivity is singleTask, it will be always the
3233 * root of the task. So we can use either true or false for
3234 * moveTaskToBack().
3235 */
3236 mActivity.moveTaskToBack(true);
3237 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003238 return;
3239 }
John Reckef654f12011-07-12 16:42:08 -07003240 if (current.canGoBack()) {
3241 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003242 } else {
3243 // Check to see if we are closing a window that was created by
3244 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003245 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003246 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003247 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003248 // Now we close the other tab
3249 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003250 } else if (BrowserConfig.getInstance(getContext())
3251 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3252 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003253 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003254 /*
3255 * Instead of finishing the activity, simply push this to the back
3256 * of the stack and let ActivityManager to choose the foreground
3257 * activity. As BrowserActivity is singleTask, it will be always the
3258 * root of the task. So we can use either true or false for
3259 * moveTaskToBack().
3260 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003261 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003262 }
3263 }
3264 }
3265
3266 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003267 * helper method for key handler
3268 * returns the current tab if it can't advance
3269 */
Michael Kolbc831b632011-05-11 09:30:34 -07003270 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003271 int pos = mTabControl.getCurrentPosition() + 1;
3272 if (pos >= mTabControl.getTabCount()) {
3273 pos = 0;
3274 }
3275 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003276 }
3277
3278 /**
3279 * helper method for key handler
3280 * returns the current tab if it can't advance
3281 */
Michael Kolbc831b632011-05-11 09:30:34 -07003282 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003283 int pos = mTabControl.getCurrentPosition() - 1;
3284 if ( pos < 0) {
3285 pos = mTabControl.getTabCount() - 1;
3286 }
3287 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003288 }
3289
John Reckbcef87f2012-02-03 14:58:34 -08003290 boolean isMenuOrCtrlKey(int keyCode) {
3291 return (KeyEvent.KEYCODE_MENU == keyCode)
3292 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3293 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3294 }
3295
Michael Kolb0035fad2011-03-14 13:25:28 -07003296 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003297 * handle key events in browser
3298 *
3299 * @param keyCode
3300 * @param event
3301 * @return true if handled, false to pass to super
3302 */
John Reck9c35b9c2012-05-30 10:08:50 -07003303 @Override
3304 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003305 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3306 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003307 if (!mUi.isComboViewShowing()) {
3308 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3309 true, false);
3310 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003311 return true;
3312 }
3313
Cary Clark160bbb92011-01-10 11:17:07 -05003314 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003315 // Even if MENU is already held down, we need to call to super to open
3316 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003317 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003318 mMenuIsDown = true;
3319 return false;
3320 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003321
Cary Clark8ff8c662010-12-29 15:03:05 -05003322 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003323 Tab tab = getCurrentTab();
3324 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003325
Cary Clark160bbb92011-01-10 11:17:07 -05003326 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3327 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003328
Michael Kolb8233fac2010-10-26 16:08:53 -07003329 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003330 case KeyEvent.KEYCODE_TAB:
3331 if (event.isCtrlPressed()) {
3332 if (event.isShiftPressed()) {
3333 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003334 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003335 } else {
3336 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003337 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003338 }
3339 return true;
3340 }
3341 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003342 case KeyEvent.KEYCODE_SPACE:
3343 // WebView/WebTextView handle the keys in the KeyDown. As
3344 // the Activity's shortcut keys are only handled when WebView
3345 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003346 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003347 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003348 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003349 pageDown();
3350 }
3351 return true;
3352 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003353 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003354 event.startTracking();
3355 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003356 case KeyEvent.KEYCODE_FORWARD:
3357 if (!noModifiers) break;
3358 tab.goForward();
3359 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003360 case KeyEvent.KEYCODE_DPAD_LEFT:
3361 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003362 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003363 return true;
3364 }
3365 break;
3366 case KeyEvent.KEYCODE_DPAD_RIGHT:
3367 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003368 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003369 return true;
3370 }
3371 break;
3372 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003373 if (ctrl) {
3374 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003375 return true;
3376 }
3377 break;
Michael Kolba4183062011-01-16 10:43:21 -08003378// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003379 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003380 if (ctrl ) {
3381 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003382 return true;
3383 }
3384 break;
Michael Kolba4183062011-01-16 10:43:21 -08003385// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003386// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003387// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003388// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003389// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003390// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003391// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003392// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003393// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003394// case KeyEvent.KEYCODE_M: // unused
3395// case KeyEvent.KEYCODE_N: // in Chrome: new window
3396// case KeyEvent.KEYCODE_O: // in Chrome: open file
3397// case KeyEvent.KEYCODE_P: // in Chrome: print page
3398// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003399// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003400// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3401 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003402 // we can't use the ctrl/shift flags, they check for
3403 // exclusive use of a modifier
3404 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003405 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003406 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003407 } else {
3408 openTabToHomePage();
3409 }
3410 return true;
3411 }
3412 break;
3413// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3414// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003415// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003416// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3417// case KeyEvent.KEYCODE_Y: // unused
3418// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003419 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003420 // it is a regular key and webview is not null
3421 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003422 }
3423
John Reck9c35b9c2012-05-30 10:08:50 -07003424 @Override
3425 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003426 switch(keyCode) {
3427 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003428 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003429 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003430 return true;
3431 }
3432 break;
3433 }
3434 return false;
3435 }
3436
John Reck9c35b9c2012-05-30 10:08:50 -07003437 @Override
3438 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003439 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003440 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003441 if (KeyEvent.KEYCODE_MENU == keyCode
3442 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003443 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003444 }
3445 }
Cary Clark160bbb92011-01-10 11:17:07 -05003446 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003447 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003448 case KeyEvent.KEYCODE_BACK:
3449 if (event.isTracking() && !event.isCanceled()) {
3450 onBackKey();
3451 return true;
3452 }
3453 break;
3454 }
3455 return false;
3456 }
3457
3458 public boolean isMenuDown() {
3459 return mMenuIsDown;
3460 }
3461
John Reck9c35b9c2012-05-30 10:08:50 -07003462 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003463 public void setupAutoFill(Message message) {
3464 // Open the settings activity at the AutoFill profile fragment so that
3465 // the user can create a new profile. When they return, we will dispatch
3466 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003467 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003468 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3469 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003470 }
John Reckb3417f02011-01-14 11:01:05 -08003471
John Reck9c35b9c2012-05-30 10:08:50 -07003472 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003473 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003474 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003475 return true;
3476 }
3477
John Reck1cf4b792011-07-26 10:22:22 -07003478 @Override
3479 public boolean shouldCaptureThumbnails() {
3480 return mUi.shouldCaptureThumbnails();
3481 }
3482
Michael Kolbc3af0672011-08-09 10:24:41 -07003483 @Override
Pankaj Gargf49e0222015-09-01 12:19:13 -07003484 public void onThumbnailCapture(Bitmap bm) {
3485 synchronized (mThumbnailCbList) {
3486 int num_entries = mThumbnailCbList.size();
3487 while (num_entries > 0){
3488 num_entries--;
3489 ValueCallback<Bitmap> cb = mThumbnailCbList.get(num_entries);
3490 cb.onReceiveValue(bm);
3491 }
3492 mThumbnailCbList.clear();
3493 }
3494 }
3495
3496 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003497 public boolean supportsVoice() {
3498 PackageManager pm = mActivity.getPackageManager();
3499 List activities = pm.queryIntentActivities(new Intent(
3500 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3501 return activities.size() != 0;
3502 }
3503
3504 @Override
3505 public void startVoiceRecognizer() {
3506 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003507 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003508 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3509 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3510 mActivity.startActivityForResult(voice, VOICE_RESULT);
3511 }
3512
Pankaj Garg7b279f62014-08-12 14:47:18 -07003513 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003514 if (mLevel == level)
3515 return;
3516 mLevel = level;
3517 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003518 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3519 lp.dimAmount = level;
3520 mActivity.getWindow().setAttributes(lp);
3521 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3522 } else {
3523 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3524 }
3525 }
3526
Michael Kolb0b129122012-06-04 16:31:58 -07003527 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003528 public void setBlockEvents(boolean block) {
3529 mBlockEvents = block;
3530 }
3531
John Reck9c35b9c2012-05-30 10:08:50 -07003532 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003533 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003534 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003535 }
3536
John Reck9c35b9c2012-05-30 10:08:50 -07003537 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003538 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003539 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003540 }
3541
John Reck9c35b9c2012-05-30 10:08:50 -07003542 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003543 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003544 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003545 }
3546
John Reck9c35b9c2012-05-30 10:08:50 -07003547 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003548 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003549 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003550 }
3551
John Reck9c35b9c2012-05-30 10:08:50 -07003552 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003553 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003554 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003555 }
3556
Pankaj Garg49b79252014-11-07 17:33:41 -08003557 @Override
3558 public boolean shouldShowAppMenu() {
3559 return true;
3560 }
3561
3562 @Override
3563 public int getMenuThemeResourceId() {
3564 return R.style.OverflowMenuTheme;
3565 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003566}