blob: 946e45643e2a1c0a086e532fd3cfe0b3e7d6399a [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(),
597 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);
George Mount3636d0a2011-11-21 09:08:21 -0800815 mSettings.setLastRunPaused(false);
Michael Kolb70976932010-11-30 11:34:01 -0800816 Tab current = mTabControl.getCurrentTab();
817 if (current != null) {
818 current.resume();
819 resumeWebViewTimers(current);
820 }
John Reckf57c0292011-07-21 18:15:39 -0700821 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200822
Michael Kolb8233fac2010-10-26 16:08:53 -0700823 mUi.onResume();
824 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100825 NfcHandler.register(mActivity, this);
Vivek Sekharc70ae632015-04-08 17:54:05 -0700826 if (current != null && current.getWebView().isShowingCrashView())
827 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700828 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700829
John Reckf57c0292011-07-21 18:15:39 -0700830 }
831
Michael Kolb70976932010-11-30 11:34:01 -0800832 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800833 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800834 * @param tab guaranteed non-null
835 */
836 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 boolean inLoad = tab.inPageLoad();
Tarun Nainani7c762c82015-08-04 13:42:09 -0700838 if ((!mActivityStopped && !inLoad) || (mActivityStopped && inLoad)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 CookieSyncManager.getInstance().startSync();
840 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100841 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 }
843 }
844
Michael Kolb70976932010-11-30 11:34:01 -0800845 /**
846 * Pause all WebView timers using the WebView of the given tab
847 * @param tab
848 * @return true if the timers are paused or tab is null
849 */
850 private boolean pauseWebViewTimers(Tab tab) {
851 if (tab == null) {
852 return true;
853 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100855 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700857 }
Michael Kolb70976932010-11-30 11:34:01 -0800858 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 }
860
John Reck9c35b9c2012-05-30 10:08:50 -0700861 @Override
862 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800863 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
865 mUploadHandler = null;
866 }
867 if (mTabControl == null) return;
868 mUi.onDestroy();
869 // Remove the current tab and sub window
870 Tab t = mTabControl.getCurrentTab();
871 if (t != null) {
872 dismissSubWindow(t);
873 removeTab(t);
874 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500875 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 // Destroy all the tabs
877 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700878 // Unregister receiver
879 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700880 }
881
882 protected boolean isActivityPaused() {
883 return mActivityPaused;
884 }
885
John Reck9c35b9c2012-05-30 10:08:50 -0700886 @Override
887 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700888 mTabControl.freeMemory();
889 }
890
891 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 public void stopLoading() {
893 mLoadStopped = true;
894 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700896 if (w != null) {
897 w.stopLoading();
898 mUi.onPageStopped(tab);
899 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700900 }
901
902 boolean didUserStopLoading() {
903 return mLoadStopped;
904 }
905
kaiyiza016da12013-08-26 17:50:22 +0800906 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700907 final String reminderType = getContext().getResources().getString(
908 R.string.def_wifi_browser_interaction_remind_type);
909 final String selectionConnnection = getContext().getResources().getString(
910 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700911 final String wifiSelection = getContext().getResources().getString(
912 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700913
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700914 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
915 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700916 return;
917
kaiyiza016da12013-08-26 17:50:22 +0800918 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700919 Context.CONNECTIVITY_SERVICE);
920 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700921 WifiManager wifiMgr = (WifiManager) this.getContext()
922 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700923 if (networkInfo == null
924 || (networkInfo != null && (networkInfo.getType() !=
925 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700926 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
927 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700928 List<ScanResult> list = wifiMgr.getScanResults();
929 // Have no AP's for Wifi's fall back to data
930 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700931 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700932 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
933 this.getContext().startActivity(intent);
934 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700935 // Request to select Wifi AP
936 try {
937 Intent intent = new Intent(wifiSelection);
938 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
939 this.getContext().startActivity(intent);
940 } catch (Exception e) {
941 String err_msg = this.getContext().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700942 R.string.activity_not_found, wifiSelection);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700943 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
944 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700945 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700946 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800947 }
948 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700949
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 // WebViewController
951
952 @Override
John Reck324d4402011-01-11 16:56:42 -0800953 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700954
Michael Kolb8233fac2010-10-26 16:08:53 -0700955 // reset sync timer to avoid sync starts during loading a page
956 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700957 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700958 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800959 boolean networkNotifier = BrowserConfig.getInstance(getContext())
960 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700961 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700962 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800963 } else {
964 if (!mNetworkHandler.isNetworkUp()) {
965 view.setNetworkAvailable(false);
966 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 }
968
969 // when BrowserActivity just starts, onPageStarted may be called before
970 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
971 // to start the timer. As we won't switch tabs while an activity is in
972 // pause state, we can ensure calling resume and pause in pair.
Tarun Nainani7c762c82015-08-04 13:42:09 -0700973 if (mActivityStopped) {
Michael Kolb70976932010-11-30 11:34:01 -0800974 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700975 }
976 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 endActionMode();
978
John Reck30c714c2010-12-16 17:30:34 -0800979 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700980
John Reck324d4402011-01-11 16:56:42 -0800981 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 // update the bookmark database for favicon
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700983 syncBookmarkFavicon(tab, null, url, favicon);
Michael Kolb8233fac2010-10-26 16:08:53 -0700984
985 Performance.tracePageStart(url);
986
987 // Performance probe
988 if (false) {
989 Performance.onPageStarted();
990 }
991
992 }
993
994 @Override
John Reck324d4402011-01-11 16:56:42 -0800995 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700996 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800997 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200998
Michael Kolb8233fac2010-10-26 16:08:53 -0700999 // Performance probe
1000 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001001 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001002 }
1003
Tarun Nainani8eb00912014-07-17 12:28:32 -07001004 tab.onPageFinished();
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001005 syncBookmarkFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -07001006
Michael Kolb8233fac2010-10-26 16:08:53 -07001007 Performance.tracePageFinished();
1008 }
1009
1010 @Override
John Reck30c714c2010-12-16 17:30:34 -08001011 public void onProgressChanged(Tab tab) {
1012 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001013
1014 if (newProgress == 100) {
1015 CookieSyncManager.getInstance().sync();
1016 // onProgressChanged() may continue to be called after the main
1017 // frame has finished loading, as any remaining sub frames continue
1018 // to load. We'll only get called once though with newProgress as
1019 // 100 when everything is loaded. (onPageFinished is called once
1020 // when the main frame completes loading regardless of the state of
1021 // any sub frames so calls to onProgressChanges may continue after
1022 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001023 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001024 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001025 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001026 } else if (mWasInPageLoad) {
1027 mWasInPageLoad = false;
1028 updateInLoadMenuItems(mCachedMenu, tab);
1029 }
1030
Tarun Nainani7c762c82015-08-04 13:42:09 -07001031 if (mActivityStopped && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001032 // pause the WebView timer and release the wake lock if it is
1033 // finished while BrowserActivity is in pause state.
1034 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001035 }
1036 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001037 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001039 // still loading
1040 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001042 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001043 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001044 } else {
1045 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 }
1047 }
Pankaj Gargf9040c82015-08-14 10:19:31 -07001048
1049 mUi.onProgressChanged(tab);
John Reck30c714c2010-12-16 17:30:34 -08001050 }
1051
1052 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001053 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001054 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 }
1056
1057 @Override
1058 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001059 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001060 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001061 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1063 return;
1064 }
1065 // Update the title in the history database if not in private browsing mode
1066 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001067 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001068 }
1069 }
1070
1071 @Override
1072 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001073 syncBookmarkFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1074 ((BaseUi)mUi).setFavicon(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001075 }
1076
1077 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001078 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001079 // if tab is snapshot tab we want to prevent navigation from occuring
1080 // since snapshot tab opens a new tab with the url
1081 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 }
1083
1084 @Override
1085 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1086 if (mMenuIsDown) {
1087 // only check shortcut key when MENU is held
1088 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1089 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001090 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001091 int keyCode = event.getKeyCode();
1092 // We need to send almost every key to WebKit. However:
1093 // 1. We don't want to block the device on the renderer for
1094 // some keys like menu, home, call.
1095 // 2. There are no WebKit equivalents for some of these keys
1096 // (see app/keyboard_codes_win.h)
1097 // Note that these are not the same set as KeyEvent.isSystemKey:
1098 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1099 if (keyCode == KeyEvent.KEYCODE_MENU ||
1100 keyCode == KeyEvent.KEYCODE_HOME ||
1101 keyCode == KeyEvent.KEYCODE_BACK ||
1102 keyCode == KeyEvent.KEYCODE_CALL ||
1103 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1104 keyCode == KeyEvent.KEYCODE_POWER ||
1105 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1106 keyCode == KeyEvent.KEYCODE_CAMERA ||
1107 keyCode == KeyEvent.KEYCODE_FOCUS ||
1108 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1109 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1110 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1111 return true;
1112 }
1113
1114 // We also have to intercept some shortcuts before we send them to the ContentView.
1115 if (event.isCtrlPressed() && (
1116 keyCode == KeyEvent.KEYCODE_TAB ||
1117 keyCode == KeyEvent.KEYCODE_W ||
1118 keyCode == KeyEvent.KEYCODE_F4)) {
1119 return true;
1120 }
1121
1122 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001123 }
1124
Tarun Nainanif5563b62015-01-21 18:52:59 -08001125 private void handleMediaKeyEvent(KeyEvent event) {
1126
1127 int keyCode = event.getKeyCode();
1128 // send media key events to audio manager
1129 if (Build.VERSION.SDK_INT >= 19) {
1130
1131 switch (keyCode) {
1132 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1133 case KeyEvent.KEYCODE_MEDIA_STOP:
1134 case KeyEvent.KEYCODE_MEDIA_NEXT:
1135 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1136 case KeyEvent.KEYCODE_MEDIA_REWIND:
1137 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1138 case KeyEvent.KEYCODE_MUTE:
1139 case KeyEvent.KEYCODE_MEDIA_PLAY:
1140 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1141 case KeyEvent.META_SHIFT_RIGHT_ON:
1142 case KeyEvent.KEYCODE_MEDIA_EJECT:
1143 case KeyEvent.KEYCODE_MEDIA_RECORD:
1144 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1145
1146 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1147 .getSystemService(Context.AUDIO_SERVICE);
1148 audioManager.dispatchMediaKeyEvent(event);
1149 }
1150 }
1151 }
1152
Michael Kolb8233fac2010-10-26 16:08:53 -07001153 @Override
John Reck997b1b72012-04-19 18:08:25 -07001154 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001156 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001158 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 } else {
John Reck997b1b72012-04-19 18:08:25 -07001160 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 }
1162 }
John Reck997b1b72012-04-19 18:08:25 -07001163 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 }
1165
1166 @Override
John Reck324d4402011-01-11 16:56:42 -08001167 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001168 // Don't save anything in private browsing mode or when disabling history
1169 // for regular tabs is enabled
1170 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1171 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1172 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001173
John Reck49a603c2011-03-03 09:33:05 -08001174 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001175
John Reck324d4402011-01-11 16:56:42 -08001176 if (TextUtils.isEmpty(url)
1177 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001178 return;
1179 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001180
John Reckf57c0292011-07-21 18:15:39 -07001181 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001182 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001183 }
1184
1185 @Override
1186 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1187 AsyncTask<Void, Void, String[]> task =
1188 new AsyncTask<Void, Void, String[]>() {
1189 @Override
1190 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001191 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001192 }
1193 @Override
1194 public void onPostExecute(String[] result) {
1195 callback.onReceiveValue(result);
1196 }
1197 };
1198 task.execute();
1199 }
1200
1201 @Override
1202 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1203 final HttpAuthHandler handler, final String host,
1204 final String realm) {
1205 String username = null;
1206 String password = null;
1207
1208 boolean reuseHttpAuthUsernamePassword
1209 = handler.useHttpAuthUsernamePassword();
1210
1211 if (reuseHttpAuthUsernamePassword && view != null) {
1212 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1213 if (credentials != null && credentials.length == 2) {
1214 username = credentials[0];
1215 password = credentials[1];
1216 }
1217 }
1218
1219 if (username != null && password != null) {
1220 handler.proceed(username, password);
1221 } else {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -07001222 if (!tab.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001223 handler.cancel();
1224 }
1225 }
1226 }
1227
1228 @Override
1229 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001230 String contentDisposition, String mimetype, String referer,
1231 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001232 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001233 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001234 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001235 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001236 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001237 // This Tab was opened for the sole purpose of downloading a
1238 // file. Remove it.
1239 if (tab == mTabControl.getCurrentTab()) {
1240 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001241 if (tab.getDerivedFromIntent())
1242 closeTab(tab);
1243 else
1244 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001245 } else {
1246 // In this case, it is not.
1247 closeTab(tab);
1248 }
1249 }
1250 }
1251
1252 @Override
1253 public Bitmap getDefaultVideoPoster() {
1254 return mUi.getDefaultVideoPoster();
1255 }
1256
1257 @Override
1258 public View getVideoLoadingProgressView() {
1259 return mUi.getVideoLoadingProgressView();
1260 }
1261
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 // helper method
1263
1264 /*
1265 * Update the favorites icon if the private browsing isn't enabled and the
1266 * icon is valid.
1267 */
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001268 private void syncBookmarkFavicon(Tab tab, final String originalUrl,
1269 final String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001270 if (favicon == null) {
1271 return;
1272 }
1273 if (!tab.isPrivateBrowsingEnabled()) {
1274 Bookmarks.updateFavicon(mActivity
1275 .getContentResolver(), originalUrl, url, favicon);
1276 }
1277 }
1278
Leon Scroggins4cd97792010-12-03 15:31:56 -05001279 @Override
1280 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001281 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001282 mUi.bookmarkedStatusHasChanged(tab);
1283 }
1284
Michael Kolb8233fac2010-10-26 16:08:53 -07001285 // end WebViewController
1286
1287 protected void pageUp() {
1288 getCurrentTopWebView().pageUp(false);
1289 }
1290
1291 protected void pageDown() {
1292 getCurrentTopWebView().pageDown(false);
1293 }
1294
1295 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001296 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001297 public void editUrl() {
1298 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001299 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001300 }
1301
John Reck9c35b9c2012-05-30 10:08:50 -07001302 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001303 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001304 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001305 if (tab.inForeground()) {
1306 if (mUi.isCustomViewShowing()) {
1307 callback.onCustomViewHidden();
1308 return;
1309 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001310 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001311 // Save the menu state and set it to empty while the custom
1312 // view is showing.
1313 mOldMenuState = mMenuState;
1314 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001315 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001316 }
1317 }
1318
1319 @Override
1320 public void hideCustomView() {
1321 if (mUi.isCustomViewShowing()) {
1322 mUi.onHideCustomView();
1323 // Reset the old menu state.
1324 mMenuState = mOldMenuState;
1325 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001326 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 }
1328 }
1329
John Reck9c35b9c2012-05-30 10:08:50 -07001330 @Override
1331 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001332 Intent intent) {
1333 if (getCurrentTopWebView() == null) return;
1334 switch (requestCode) {
1335 case PREFERENCES_PAGE:
1336 if (resultCode == Activity.RESULT_OK && intent != null) {
1337 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001338 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001339 mTabControl.removeParentChildRelationShips();
Pankaj Garg75273bb2015-08-20 11:43:49 -07001340 } else if (action.equals(PreferenceKeys.ACTION_RELOAD_PAGE)) {
1341 ArrayList<String> origins =
1342 intent.getStringArrayListExtra(EXTRA_UPDATED_URLS);
1343 if (origins.isEmpty()) {
1344 mTabControl.reloadLiveTabs();
1345 }
1346 else{
1347 for (String origin : origins){
1348 mTabControl.findAndReload(origin);
1349 }
1350 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001351 }
1352 }
1353 break;
1354 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001355 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001356 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001357 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001359 case AUTOFILL_SETUP:
1360 // Determine whether a profile was actually set up or not
1361 // and if so, send the message back to the WebTextView to
1362 // fill the form with the new profile.
1363 if (getSettings().getAutoFillProfile() != null) {
1364 mAutoFillSetupMessage.sendToTarget();
1365 mAutoFillSetupMessage = null;
1366 }
1367 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001368 case COMBO_VIEW:
1369 if (intent == null || resultCode != Activity.RESULT_OK) {
1370 break;
1371 }
John Reck3ba45532011-08-11 16:26:53 -07001372 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001373 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1374 Tab t = getCurrentTab();
1375 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001376 mUpdateMyNavThumbnail = true;
1377 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001378 loadUrl(t, uri.toString());
1379 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1380 String[] urls = intent.getStringArrayExtra(
1381 ComboViewActivity.EXTRA_OPEN_ALL);
1382 Tab parent = getCurrentTab();
1383 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001384 if (url != null) {
1385 parent = openTab(url, parent,
1386 !mSettings.openInBackground(), true);
1387 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001388 }
1389 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1390 long id = intent.getLongExtra(
1391 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1392 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001393 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001394 }
1395 }
1396 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001397 case VOICE_RESULT:
1398 if (resultCode == Activity.RESULT_OK && intent != null) {
1399 ArrayList<String> results = intent.getStringArrayListExtra(
1400 RecognizerIntent.EXTRA_RESULTS);
1401 if (results.size() >= 1) {
1402 mVoiceResult = results.get(0);
1403 }
1404 }
1405 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001406 case MY_NAVIGATION:
1407 if (intent == null || resultCode != Activity.RESULT_OK) {
1408 break;
1409 }
1410
1411 if (intent.getBooleanExtra("need_refresh", false) &&
1412 getCurrentTopWebView() != null) {
1413 getCurrentTopWebView().reload();
1414 }
1415 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 default:
1417 break;
1418 }
1419 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001420 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001421 }
1422
1423 /**
1424 * Open the Go page.
1425 * @param startWithHistory If true, open starting on the history tab.
1426 * Otherwise, start with the bookmarks tab.
1427 */
1428 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001429 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 if (mTabControl.getCurrentWebView() == null) {
1431 return;
1432 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001433 // clear action mode
1434 if (isInCustomActionMode()) {
1435 endActionMode();
1436 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001437 Bundle extras = new Bundle();
1438 // Disable opening in a new window if we have maxed out the windows
1439 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1440 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001441 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001442 }
1443
1444 // combo view callbacks
1445
Michael Kolb8233fac2010-10-26 16:08:53 -07001446 // key handling
1447 protected void onBackKey() {
1448 if (!mUi.onBackKey()) {
1449 WebView subwindow = mTabControl.getCurrentSubWindow();
1450 if (subwindow != null) {
1451 if (subwindow.canGoBack()) {
1452 subwindow.goBack();
1453 } else {
1454 dismissSubWindow(mTabControl.getCurrentTab());
1455 }
1456 } else {
1457 goBackOnePageOrQuit();
1458 }
1459 }
1460 }
1461
Michael Kolb4bd767d2011-05-27 11:33:55 -07001462 protected boolean onMenuKey() {
1463 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001464 }
1465
Michael Kolb8233fac2010-10-26 16:08:53 -07001466 // menu handling and state
1467 // TODO: maybe put into separate handler
1468
John Reck9c35b9c2012-05-30 10:08:50 -07001469 @Override
1470 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001471 if (mMenuState == EMPTY_MENU) {
1472 return false;
1473 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001474 MenuInflater inflater = mActivity.getMenuInflater();
1475 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001476 return true;
1477 }
1478
John Reck9c35b9c2012-05-30 10:08:50 -07001479 @Override
1480 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001481 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001482 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001483 return;
1484 }
1485 if (!(v instanceof WebView)) {
1486 return;
1487 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001488 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001489 WebView.HitTestResult result;
1490
1491 /* Determine whether the ContextMenu got triggered because
1492 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1493 * received. The mResult acts as a flag to identify, how it got trigerred
1494 */
1495 if (mResult == null){
1496 result = webview.getHitTestResult();
1497 } else {
1498 result = mResult;
1499 }
1500
Michael Kolb8233fac2010-10-26 16:08:53 -07001501 if (result == null) {
1502 return;
1503 }
1504
1505 int type = result.getType();
1506 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001507
1508 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1509 unknownTypeMap.put("webview", webview);
1510 final Message msg = mHandler.obtainMessage(
1511 UNKNOWN_TYPE_MSG, unknownTypeMap);
1512 /* As defined in android developers guide
1513 * when UNKNOWN_TYPE is received as a result of HitTest
1514 * you need to determing the type by invoking requestFocusNodeHref
1515 */
1516 webview.requestFocusNodeHref(msg);
1517
Michael Kolb8233fac2010-10-26 16:08:53 -07001518 Log.w(LOGTAG,
1519 "We should not show context menu when nothing is touched");
1520 return;
1521 }
1522 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1523 // let TextView handles context menu
1524 return;
1525 }
1526
1527 // Note, http://b/issue?id=1106666 is requesting that
1528 // an inflated menu can be used again. This is not available
1529 // yet, so inflate each time (yuk!)
1530 MenuInflater inflater = mActivity.getMenuInflater();
1531 inflater.inflate(R.menu.browsercontext, menu);
1532
1533 // Show the correct menu group
1534 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001535 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001536 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001537 menu.setGroupVisible(R.id.PHONE_MENU,
1538 type == WebView.HitTestResult.PHONE_TYPE);
1539 menu.setGroupVisible(R.id.EMAIL_MENU,
1540 type == WebView.HitTestResult.EMAIL_TYPE);
1541 menu.setGroupVisible(R.id.GEO_MENU,
1542 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001543 String itemUrl = null;
1544 String url = webview.getOriginalUrl();
1545 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1546 itemUrl = Uri.decode(navigationUrl);
1547 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1548 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1549 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1550 } else {
1551 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1552 }
1553 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1554 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1555 } else {
1556 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1557
1558 menu.setGroupVisible(R.id.IMAGE_MENU,
1559 type == WebView.HitTestResult.IMAGE_TYPE
1560 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1561 menu.setGroupVisible(R.id.ANCHOR_MENU,
1562 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1563 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001564
1565 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1566 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1567 UrlUtils.isDownloadableScheme(extra));
1568 }
1569 else {
1570 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1571 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001572 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001573 // Setup custom handling depending on the type
1574 switch (type) {
1575 case WebView.HitTestResult.PHONE_TYPE:
1576 menu.setHeaderTitle(Uri.decode(extra));
1577 menu.findItem(R.id.dial_context_menu_id).setIntent(
1578 new Intent(Intent.ACTION_VIEW, Uri
1579 .parse(WebView.SCHEME_TEL + extra)));
1580 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1581 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1582 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1583 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1584 addIntent);
1585 menu.findItem(R.id.copy_phone_context_menu_id)
1586 .setOnMenuItemClickListener(
1587 new Copy(extra));
1588 break;
1589
1590 case WebView.HitTestResult.EMAIL_TYPE:
1591 menu.setHeaderTitle(extra);
1592 menu.findItem(R.id.email_context_menu_id).setIntent(
1593 new Intent(Intent.ACTION_VIEW, Uri
1594 .parse(WebView.SCHEME_MAILTO + extra)));
1595 menu.findItem(R.id.copy_mail_context_menu_id)
1596 .setOnMenuItemClickListener(
1597 new Copy(extra));
1598 break;
1599
1600 case WebView.HitTestResult.GEO_TYPE:
1601 menu.setHeaderTitle(extra);
1602 menu.findItem(R.id.map_context_menu_id).setIntent(
1603 new Intent(Intent.ACTION_VIEW, Uri
1604 .parse(WebView.SCHEME_GEO
1605 + URLEncoder.encode(extra))));
1606 menu.findItem(R.id.copy_geo_context_menu_id)
1607 .setOnMenuItemClickListener(
1608 new Copy(extra));
1609 break;
1610
1611 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1612 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001613 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001614 // decide whether to show the open link in new tab option
1615 boolean showNewTab = mTabControl.canCreateNewTab();
1616 MenuItem newTabItem
1617 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001618 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001619 ? R.string.contextmenu_openlink_newwindow_background
1620 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 newTabItem.setVisible(showNewTab);
1622 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001623 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1624 newTabItem.setOnMenuItemClickListener(
1625 new MenuItem.OnMenuItemClickListener() {
1626 @Override
1627 public boolean onMenuItemClick(MenuItem item) {
1628 final HashMap<String, WebView> hrefMap =
1629 new HashMap<String, WebView>();
1630 hrefMap.put("webview", webview);
1631 final Message msg = mHandler.obtainMessage(
1632 FOCUS_NODE_HREF,
1633 R.id.open_newtab_context_menu_id,
1634 0, hrefMap);
1635 webview.requestFocusNodeHref(msg);
1636 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001637 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001638 });
1639 } else {
1640 newTabItem.setOnMenuItemClickListener(
1641 new MenuItem.OnMenuItemClickListener() {
1642 @Override
1643 public boolean onMenuItemClick(MenuItem item) {
1644 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001645 openTab(extra, parent,
1646 !mSettings.openInBackground(),
1647 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001648 return true;
1649 }
1650 });
1651 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001652 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001653 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1654 menu.setHeaderTitle(navigationUrl);
1655 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1656
1657 if (itemUrl != null) {
1658 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1659 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1660 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1661 @Override
1662 public boolean onMenuItemClick(MenuItem item) {
1663 final Intent intent = new Intent(Controller.this
1664 .getContext(),
1665 AddMyNavigationPage.class);
1666 Bundle bundle = new Bundle();
1667 String url = Uri.decode(navigationUrl);
1668 bundle.putBoolean("isAdding", false);
1669 bundle.putString("url", url);
1670 bundle.putString("name", getNameFromUrl(url));
1671 intent.putExtra("websites", bundle);
1672 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1673 return false;
1674 }
1675 });
1676 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1677 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1678 @Override
1679 public boolean onMenuItemClick(MenuItem item) {
1680 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1681 return false;
1682 }
1683 });
1684 }
1685 } else {
1686 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1687 }
1688 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001689 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1690 break;
1691 }
1692 // otherwise fall through to handle image part
1693 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001694 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1695 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001696 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1697 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001698 shareItem.setOnMenuItemClickListener(
1699 new MenuItem.OnMenuItemClickListener() {
1700 @Override
1701 public boolean onMenuItemClick(MenuItem item) {
1702 sharePage(mActivity, null, extra, null,
1703 null);
1704 return true;
1705 }
1706 }
1707 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001708 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001709 menu.findItem(R.id.view_image_context_menu_id)
1710 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1711 @Override
1712 public boolean onMenuItemClick(MenuItem item) {
1713 openTab(extra, mTabControl.getCurrentTab(), true, true);
1714 return false;
1715 }
1716 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001717 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1718 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1719 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001720 menu.findItem(R.id.set_wallpaper_context_menu_id).
1721 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1722 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001723 break;
1724
1725 default:
1726 Log.w(LOGTAG, "We should not get here.");
1727 break;
1728 }
1729 //update the ui
1730 mUi.onContextMenuCreated(menu);
1731 }
1732
kaiyiz6e5b3e02013-08-19 20:02:01 +08001733 public void startAddMyNavigation(String url) {
1734 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1735 Bundle bundle = new Bundle();
1736 bundle.putBoolean("isAdding", true);
1737 bundle.putString("url", url);
1738 bundle.putString("name", getNameFromUrl(url));
1739 intent.putExtra("websites", bundle);
1740 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1741 }
1742
1743 private void showMyNavigationDeleteDialog(final String itemUrl) {
1744 new AlertDialog.Builder(this.getContext())
1745 .setTitle(R.string.my_navigation_delete_label)
1746 .setIcon(android.R.drawable.ic_dialog_alert)
1747 .setMessage(R.string.my_navigation_delete_msg)
1748 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1749 @Override
1750 public void onClick(DialogInterface dialog, int whichButton) {
1751 deleteMyNavigationItem(itemUrl);
1752 }
1753 })
1754 .setNegativeButton(R.string.cancel, null)
1755 .show();
1756 }
1757
1758 private void deleteMyNavigationItem(final String itemUrl) {
1759 ContentResolver cr = this.getContext().getContentResolver();
1760 Cursor cursor = null;
1761
1762 try {
1763 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1764 new String[] {
1765 MyNavigationUtil.ID
1766 }, "url = ?", new String[] {
1767 itemUrl
1768 }, null);
1769 if (null != cursor && cursor.moveToFirst()) {
1770 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1771 cursor.getLong(0));
1772
1773 ContentValues values = new ContentValues();
1774 values.put(MyNavigationUtil.TITLE, "");
1775 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1776 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1777 ByteArrayOutputStream os = new ByteArrayOutputStream();
1778 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1779 R.raw.my_navigation_add);
1780 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1781 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1782 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1783 cr.update(uri, values, null, null);
1784 } else {
1785 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1786 }
1787 } catch (IllegalStateException e) {
1788 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1789 } finally {
1790 if (null != cursor) {
1791 cursor.close();
1792 }
1793 }
1794
1795 if (getCurrentTopWebView() != null) {
1796 getCurrentTopWebView().reload();
1797 }
1798 }
1799
1800 private String getNameFromUrl(String itemUrl) {
1801 ContentResolver cr = this.getContext().getContentResolver();
1802 Cursor cursor = null;
1803 String name = null;
1804
1805 try {
1806 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1807 new String[] {
1808 MyNavigationUtil.TITLE
1809 }, "url = ?", new String[] {
1810 itemUrl
1811 }, null);
1812 if (null != cursor && cursor.moveToFirst()) {
1813 name = cursor.getString(0);
1814 } else {
1815 Log.e(LOGTAG, "this item does not exist!");
1816 }
1817 } catch (IllegalStateException e) {
1818 Log.e(LOGTAG, "getNameFromUrl", e);
1819 } finally {
1820 if (null != cursor) {
1821 cursor.close();
1822 }
1823 }
1824 return name;
1825 }
1826
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001827 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001828 final ContentResolver cr = mActivity.getContentResolver();
1829 new AsyncTask<Void, Void, Void>() {
1830 @Override
1831 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001832 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1833 if(!isMyNavigationUrl)
1834 return null;
1835
kaiyiz6e5b3e02013-08-19 20:02:01 +08001836 ContentResolver cr = mActivity.getContentResolver();
1837 Cursor cursor = null;
1838 try {
1839 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1840 new String[] {
1841 MyNavigationUtil.ID
1842 }, "url = ?", new String[] {
1843 itemUrl
1844 }, null);
1845 if (null != cursor && cursor.moveToFirst()) {
1846 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001847 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001848
1849 ContentValues values = new ContentValues();
1850 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1851 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1852 cursor.getLong(0));
1853 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1854 cr.update(uri, values, null, null);
1855 os.close();
1856 }
1857 } catch (IllegalStateException e) {
1858 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1859 } catch (IOException e) {
1860 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1861 } finally {
1862 if (null != cursor) {
1863 cursor.close();
1864 }
1865 }
1866 return null;
1867 }
1868 }.execute();
1869 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001870 /**
1871 * As the menu can be open when loading state changes
1872 * we must manually update the state of the stop/reload menu
1873 * item
1874 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001875 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001876 if (menu == null) {
1877 return;
1878 }
1879 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001880 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001881 menu.findItem(R.id.stop_menu_id):
1882 menu.findItem(R.id.reload_menu_id);
1883 if (src != null) {
1884 dest.setIcon(src.getIcon());
1885 dest.setTitle(src.getTitle());
1886 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001887 mActivity.invalidateOptionsMenu();
1888 }
1889
1890 public void invalidateOptionsMenu() {
1891 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001892 }
1893
John Reck9c35b9c2012-05-30 10:08:50 -07001894 @Override
1895 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001896 // Software menu key (toolbar key)
1897 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1898 return true;
1899 }
1900
1901 @Override
1902 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001903 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001904 // hold on to the menu reference here; it is used by the page callbacks
1905 // to update the menu based on loading state
1906 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001907 // Note: setVisible will decide whether an item is visible; while
1908 // setEnabled() will decide whether an item is enabled, which also means
1909 // whether the matching shortcut key will function.
1910 switch (mMenuState) {
1911 case EMPTY_MENU:
1912 if (mCurrentMenuState != mMenuState) {
1913 menu.setGroupVisible(R.id.MAIN_MENU, false);
1914 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1915 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1916 }
1917 break;
1918 default:
1919 if (mCurrentMenuState != mMenuState) {
1920 menu.setGroupVisible(R.id.MAIN_MENU, true);
1921 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1922 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1923 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001924 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001925 break;
1926 }
1927 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001928 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001929
1930 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001931 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1932 EditBookmarksRestriction.getInstance()
1933 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001934 }
1935
1936 private void setMenuItemVisibility(Menu menu, int id,
1937 boolean visibility) {
1938 MenuItem item = menu.findItem(id);
1939 if (item != null) {
1940 item.setVisible(visibility);
1941 }
1942 }
1943
Sagar Dhawanef06f562015-09-07 15:23:22 +02001944 private int lookupBookmark( String url) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001945 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001946 int count = 0;
1947 Cursor cursor = null;
1948 try {
1949 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1950 BookmarksLoader.PROJECTION,
Sagar Dhawanef06f562015-09-07 15:23:22 +02001951 "url = ?",
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001952 new String[] {
Sagar Dhawanef06f562015-09-07 15:23:22 +02001953 url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001954 },
1955 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001956
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001957 if (cursor != null)
1958 count = cursor.getCount();
1959
1960 } catch (IllegalStateException e) {
1961 Log.e(LOGTAG, "lookupBookmark ", e);
1962 } finally {
1963 if (null != cursor) {
1964 cursor.close();
1965 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001966 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001967 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001968 }
1969
1970 private void resetMenuItems(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001971 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1972
1973 WebView w = getCurrentTopWebView();
1974 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1975
1976 String title = w.getTitle();
1977 String url = w.getUrl();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001978 mCurrentPageBookmarked = (lookupBookmark(url) > 0);
Pankaj Garg634bf432015-07-13 09:54:21 -07001979 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001980 bookmark_icon.setChecked(true);
1981 } else {
1982 bookmark_icon.setChecked(false);
1983 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001984
1985 // update reader mode checkbox
1986 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1987 readerSwitcher.setVisible(false);
1988 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001989 }
1990
Michael Kolb4bf79712011-07-14 14:18:12 -07001991 @Override
1992 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001993 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001994 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001995 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001996 // Following flag is used to identify schemes for which the LIVE_MENU
1997 // items defined in res/menu/browser.xml should be enabled
1998 boolean isLiveScheme = false;
1999 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002000 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07002001
2002 boolean isDistillable = false;
2003 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002004 resetMenuItems(menu);
2005
Michael Kolb4bf79712011-07-14 14:18:12 -07002006 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002007 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002008 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002009 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002010 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002011 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002012 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07002013
2014 isDistillable = tab.isDistillable();
2015 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07002016 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002017
2018 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2019 forward.setEnabled(canGoForward);
2020
Michael Kolb4bf79712011-07-14 14:18:12 -07002021 // decide whether to show the share link option
2022 PackageManager pm = mActivity.getPackageManager();
2023 Intent send = new Intent(Intent.ACTION_SEND);
2024 send.setType("text/plain");
2025 ResolveInfo ri = pm.resolveActivity(send,
2026 PackageManager.MATCH_DEFAULT_ONLY);
2027 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2028
2029 boolean isNavDump = mSettings.enableNavDump();
2030 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2031 nav.setVisible(isNavDump);
2032 nav.setEnabled(isNavDump);
2033
2034 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002035 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2036 uaSwitcher.setChecked(isDesktopUa);
Sagar Dhawanef06f562015-09-07 15:23:22 +02002037 setMenuItemVisibility(menu, R.id.find_menu_id, isLive);
2038 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002039 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002040 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002041 setMenuItemVisibility(menu, R.id.add_to_homescreen,
Sagar Dhawanef06f562015-09-07 15:23:22 +02002042 isLive && isPageFinished);
Pankaj Garg49b79252014-11-07 17:33:41 -08002043 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002044 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002045 // history and snapshots item are the members of COMBO menu group,
2046 // so if show history item, only make snapshots item invisible.
2047 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002048
Ze G Riande2a675c22015-06-03 11:15:24 -07002049
2050 // update reader mode checkbox
2051 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2052 // The reader mode checkbox is hidden only
2053 // when the current page is neither distillable nor distilled
2054 readerSwitcher.setVisible(isDistillable || isDistilled);
2055 readerSwitcher.setChecked(isDistilled);
2056
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002057 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002058 }
2059
Pankaj Gargf49e0222015-09-01 12:19:13 -07002060 private Bitmap cropAndScaleBitmap(Bitmap bm, int width, int height) {
2061 if (width == 0 || height == 0 || bm == null)
2062 return bm;
2063
2064 Bitmap cropped;
2065
2066 if (bm.getHeight() > bm.getWidth()) {
2067 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
2068 bm.getWidth() * height / width, null, true);
2069 } else {
2070 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getHeight() * width / height,
2071 bm.getHeight(), null, true);
2072 }
2073
2074 Bitmap scaled = Bitmap.createScaledBitmap(cropped, width, height, true);
2075 cropped.recycle();
2076 return scaled;
2077 }
2078
John Reck9c35b9c2012-05-30 10:08:50 -07002079 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 if (null == getCurrentTopWebView()) {
2082 return false;
2083 }
2084 if (mMenuIsDown) {
2085 // The shortcut action consumes the MENU. Even if it is still down,
2086 // it won't trigger the next shortcut action. In the case of the
2087 // shortcut action triggering a new activity, like Bookmarks, we
2088 // won't get onKeyUp for MENU. So it is important to reset it here.
2089 mMenuIsDown = false;
2090 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002091 if (mUi.onOptionsItemSelected(item)) {
2092 // ui callback handled it
2093 return true;
2094 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002095 switch (item.getItemId()) {
2096 // -- Main menu
2097 case R.id.new_tab_menu_id:
2098 openTabToHomePage();
2099 break;
2100
2101 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002102 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002103 break;
2104
2105 case R.id.goto_menu_id:
2106 editUrl();
2107 break;
2108
2109 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002110 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2111 break;
2112
Michael Kolb315d5022011-10-13 12:47:11 -07002113 case R.id.snapshots_menu_id:
2114 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002115 break;
2116
Pankaj Garg49b79252014-11-07 17:33:41 -08002117 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002118 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002119 break;
2120
2121 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002122 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002123 stopLoading();
2124 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002125 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 getCurrentTopWebView().reload();
2127 }
2128 break;
2129
Michael Kolb8233fac2010-10-26 16:08:53 -07002130 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002131 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002132 break;
2133
2134 case R.id.close_menu_id:
2135 // Close the subwindow if it exists.
2136 if (mTabControl.getCurrentSubWindow() != null) {
2137 dismissSubWindow(mTabControl.getCurrentTab());
2138 break;
2139 }
2140 closeCurrentTab();
2141 break;
2142
kaiyiza8b6dbb2013-07-29 18:11:22 +08002143 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002144 Object[] params = { new String("persist.debug.browsermonkeytest")};
2145 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002146 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002147 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002148 if (ret != null && ret.equals("enable"))
2149 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002150 if (BrowserConfig.getInstance(getContext())
2151 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2152 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002153 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 case R.id.homepage_menu_id:
2155 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002156 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002157 break;
2158
2159 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002160 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002161 break;
2162
2163 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002164 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002165 break;
2166
John Reck2bc80422011-06-30 15:11:49 -07002167 case R.id.save_snapshot_menu_id:
2168 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002169 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002170 createScreenshotAsync(
Pankaj Gargf49e0222015-09-01 12:19:13 -07002171 source,
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002172 new ValueCallback<Bitmap>() {
2173 @Override
2174 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002175 Bitmap bm = cropAndScaleBitmap(bitmap,
2176 getDesiredThumbnailWidth(mActivity),
2177 getDesiredThumbnailHeight(mActivity));
2178 new SaveSnapshotTask(source, bm).execute();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002179 }
2180 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002181 break;
2182
Michael Kolb8233fac2010-10-26 16:08:53 -07002183 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002184 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002185 break;
2186
John Recke1a03a32011-09-14 17:04:16 -07002187 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002188 // passing null to distinguish between
2189 // "go live" button and navigating a web page
2190 // on a snapshot tab
2191 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002192 case R.id.share_page_menu_id:
2193 Tab currentTab = mTabControl.getCurrentTab();
2194 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002195 return false;
2196 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002197 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002198 break;
2199
2200 case R.id.dump_nav_menu_id:
2201 getCurrentTopWebView().debugDump();
2202 break;
2203
Michael Kolb8233fac2010-10-26 16:08:53 -07002204 case R.id.zoom_in_menu_id:
2205 getCurrentTopWebView().zoomIn();
2206 break;
2207
2208 case R.id.zoom_out_menu_id:
2209 getCurrentTopWebView().zoomOut();
2210 break;
2211
2212 case R.id.view_downloads_menu_id:
2213 viewDownloads();
2214 break;
2215
John Reck42229bc2011-08-19 13:26:43 -07002216 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002217 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002218 break;
2219
Ze G Riande2a675c22015-06-03 11:15:24 -07002220 case R.id.reader_mode_menu_id:
2221 toggleReaderMode();
2222 break;
2223
Michael Kolb8233fac2010-10-26 16:08:53 -07002224 case R.id.window_one_menu_id:
2225 case R.id.window_two_menu_id:
2226 case R.id.window_three_menu_id:
2227 case R.id.window_four_menu_id:
2228 case R.id.window_five_menu_id:
2229 case R.id.window_six_menu_id:
2230 case R.id.window_seven_menu_id:
2231 case R.id.window_eight_menu_id:
2232 {
2233 int menuid = item.getItemId();
2234 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2235 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2236 Tab desiredTab = mTabControl.getTab(id);
2237 if (desiredTab != null &&
2238 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002239 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002240 }
2241 break;
2242 }
2243 }
2244 }
2245 break;
2246
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002247 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002248 Bundle bundle = new Bundle();
2249 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2250 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2251 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2252 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2253 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002254 break;
2255
Pankaj Garg49b79252014-11-07 17:33:41 -08002256 case R.id.add_to_homescreen:
2257 final WebView w = getCurrentTopWebView();
2258 final EditText input = new EditText(getContext());
2259 input.setText(w.getTitle());
2260 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002261 .setTitle(getContext().getResources().getString(
2262 R.string.add_to_homescreen))
2263 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002264 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002265 .setPositiveButton(getContext().getResources().getString(
2266 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002267 public void onClick(DialogInterface dialog, int whichButton) {
2268 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2269 getContext(),
2270 w.getUrl(),
2271 input.getText().toString(),
2272 w.getViewportBitmap(),
2273 w.getFavicon()));
2274
2275 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2276 .addCategory(Intent.CATEGORY_HOME));
2277 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002278 .setNegativeButton(getContext().getResources().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07002279 R.string.cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002280 public void onClick(DialogInterface dialog, int whichButton) {
2281 // Do nothing.
2282 }
2283 })
2284 .show();
2285 break;
2286
Michael Kolb8233fac2010-10-26 16:08:53 -07002287 default:
2288 return false;
2289 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002290 return true;
2291 }
2292
John Reck68234a92012-04-19 15:27:12 -07002293 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2294 implements OnCancelListener {
2295
2296 private Tab mTab;
2297 private Dialog mProgressDialog;
2298 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002299 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002300
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002301 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002302 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002303 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002304 }
2305
2306 @Override
2307 protected void onPreExecute() {
2308 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2309 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2310 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002311 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002312 }
2313
2314 @Override
2315 protected Long doInBackground(Void... params) {
2316 if (!mTab.saveViewState(mValues)) {
2317 return null;
2318 }
2319 if (isCancelled()) {
2320 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2321 File file = mActivity.getFileStreamPath(path);
2322 if (!file.delete()) {
2323 file.deleteOnExit();
2324 }
2325 return null;
2326 }
2327 final ContentResolver cr = mActivity.getContentResolver();
2328 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2329 if (result == null) {
2330 return null;
2331 }
2332 long id = ContentUris.parseId(result);
2333 return id;
2334 }
2335
2336 @Override
2337 protected void onPostExecute(Long id) {
2338 if (isCancelled()) {
2339 return;
2340 }
2341 mProgressDialog.dismiss();
2342 if (id == null) {
2343 Toast.makeText(mActivity, R.string.snapshot_failed,
2344 Toast.LENGTH_SHORT).show();
2345 return;
2346 }
2347 Bundle b = new Bundle();
2348 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2349 mUi.showComboView(ComboViews.Snapshots, b);
2350 }
2351
2352 @Override
2353 public void onCancel(DialogInterface dialog) {
2354 cancel(true);
2355 }
2356 }
2357
Michael Kolb80f75082012-04-10 10:50:06 -07002358 @Override
2359 public void toggleUserAgent() {
2360 WebView web = getCurrentWebView();
2361 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002362 }
2363
Ze G Riande2a675c22015-06-03 11:15:24 -07002364 // This function calls the method in the webview to enable/disable
2365 // the reader mode through the DOM distiller
2366 public void toggleReaderMode() {
2367 Tab t = mTabControl.getCurrentTab();
2368 if (t.isDistilled()) {
2369 closeTab(t);
2370 } else if (t.isDistillable()) {
2371 openTab(t.getDistilledUrl(), false, true, false, t);
2372 }
2373 }
2374
Michael Kolb80f75082012-04-10 10:50:06 -07002375 @Override
2376 public void findOnPage() {
2377 getCurrentTopWebView().showFindDialog(null, true);
2378 }
2379
2380 @Override
2381 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002382 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002383 }
2384
2385 @Override
2386 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002387 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002388 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2389 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002390 }
2391 else {
2392 WebView w = getCurrentTopWebView();
2393 if (w == null)
2394 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002395 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002396 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002397 }
Michael Kolb80f75082012-04-10 10:50:06 -07002398 }
2399
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002400 public boolean goLive(String url) {
2401 if (!getCurrentTab().isSnapshot())
2402 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002403 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002404
2405 if (url == null) { // "go live" button was clicked
2406 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002407 closeTab(t);
2408 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002409 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002410 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002411 return true;
John Recke1a03a32011-09-14 17:04:16 -07002412 }
2413
luxiaolb40014b2013-07-19 10:01:43 +08002414 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002415 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002416 new AlertDialog.Builder(activity)
2417 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002418 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002419 .setMessage(R.string.exit_browser_msg)
2420 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2421 public void onClick(DialogInterface dialog, int which) {
2422 activity.moveTaskToBack(true);
2423 dialog.dismiss();
2424 }
2425 })
2426 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2427 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002428 mCrashRecoveryHandler.clearState(true);
2429 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002430 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002431 dialog.dismiss();
2432 }
2433 })
2434 .show();
2435 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002436
Michael Kolb315d5022011-10-13 12:47:11 -07002437 @Override
2438 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002439 }
2440
John Reck9c35b9c2012-05-30 10:08:50 -07002441 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002442 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002443 // Let the History and Bookmark fragments handle menus they created.
2444 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2445 return false;
2446 }
2447
Michael Kolb8233fac2010-10-26 16:08:53 -07002448 int id = item.getItemId();
2449 boolean result = true;
2450 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002451 // -- Browser context menu
2452 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002453 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002454 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002455 case R.id.copy_link_context_menu_id:
2456 final WebView webView = getCurrentTopWebView();
2457 if (null == webView) {
2458 result = false;
2459 break;
2460 }
2461 final HashMap<String, WebView> hrefMap =
2462 new HashMap<String, WebView>();
2463 hrefMap.put("webview", webView);
2464 final Message msg = mHandler.obtainMessage(
2465 FOCUS_NODE_HREF, id, 0, hrefMap);
2466 webView.requestFocusNodeHref(msg);
2467 break;
2468
2469 default:
2470 // For other context menus
2471 result = onOptionsItemSelected(item);
2472 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002473 return result;
2474 }
2475
2476 /**
2477 * support programmatically opening the context menu
2478 */
2479 public void openContextMenu(View view) {
2480 mActivity.openContextMenu(view);
2481 }
2482
2483 /**
2484 * programmatically open the options menu
2485 */
2486 public void openOptionsMenu() {
2487 mActivity.openOptionsMenu();
2488 }
2489
John Reck9c35b9c2012-05-30 10:08:50 -07002490 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002491 public boolean onMenuOpened(int featureId, Menu menu) {
2492 if (mOptionsMenuOpen) {
2493 if (mConfigChanged) {
2494 // We do not need to make any changes to the state of the
2495 // title bar, since the only thing that happened was a
2496 // change in orientation
2497 mConfigChanged = false;
2498 } else {
2499 if (!mExtendedMenuOpen) {
2500 mExtendedMenuOpen = true;
2501 mUi.onExtendedMenuOpened();
2502 } else {
2503 // Switching the menu back to icon view, so show the
2504 // title bar once again.
2505 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002506 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002507 }
2508 }
2509 } else {
2510 // The options menu is closed, so open it, and show the title
2511 mOptionsMenuOpen = true;
2512 mConfigChanged = false;
2513 mExtendedMenuOpen = false;
2514 mUi.onOptionsMenuOpened();
2515 }
2516 return true;
2517 }
2518
John Reck9c35b9c2012-05-30 10:08:50 -07002519 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002520 public void onOptionsMenuClosed(Menu menu) {
2521 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002522 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002523 }
2524
John Reck9c35b9c2012-05-30 10:08:50 -07002525 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002526 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002527 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002528 }
2529
2530 // Helper method for getting the top window.
2531 @Override
2532 public WebView getCurrentTopWebView() {
2533 return mTabControl.getCurrentTopWebView();
2534 }
2535
2536 @Override
2537 public WebView getCurrentWebView() {
2538 return mTabControl.getCurrentWebView();
2539 }
2540
2541 /*
2542 * This method is called as a result of the user selecting the options
2543 * menu to see the download window. It shows the download window on top of
2544 * the current window.
2545 */
2546 void viewDownloads() {
2547 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2548 mActivity.startActivity(intent);
2549 }
2550
John Reck30b065e2011-07-19 10:58:05 -07002551 int getActionModeHeight() {
2552 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2553 new int[] { android.R.attr.actionBarSize });
2554 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2555 actionBarSizeTypedArray.recycle();
2556 return size;
2557 }
2558
Michael Kolb8233fac2010-10-26 16:08:53 -07002559 // action mode
2560
John Reck9c35b9c2012-05-30 10:08:50 -07002561 @Override
2562 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002563 mUi.onActionModeStarted(mode);
2564 mActionMode = mode;
2565 }
2566
2567 /*
2568 * True if a custom ActionMode (i.e. find or select) is in use.
2569 */
2570 @Override
2571 public boolean isInCustomActionMode() {
2572 return mActionMode != null;
2573 }
2574
2575 /*
2576 * End the current ActionMode.
2577 */
2578 @Override
2579 public void endActionMode() {
2580 if (mActionMode != null) {
2581 mActionMode.finish();
2582 }
2583 }
2584
2585 /*
2586 * Called by find and select when they are finished. Replace title bars
2587 * as necessary.
2588 */
John Reck9c35b9c2012-05-30 10:08:50 -07002589 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002590 public void onActionModeFinished(ActionMode mode) {
2591 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002592 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002593 mActionMode = null;
2594 }
2595
2596 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002597 final Tab tab = getCurrentTab();
2598 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 }
2600
2601 // bookmark handling
2602
2603 /**
2604 * add the current page as a bookmark to the given folder id
2605 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002606 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002607 * bookmarked, and if it is, edit that bookmark. If false, and
2608 * the site is already bookmarked, do not attempt to edit the
2609 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002610 */
2611 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002612 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002613 WebView w = getCurrentTopWebView();
2614 if (w == null) {
2615 return null;
2616 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002617 Intent i = new Intent(mActivity,
2618 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2620 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002621 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002622 if (touchIconUrl != null) {
2623 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2624 WebSettings settings = w.getSettings();
2625 if (settings != null) {
2626 i.putExtra(AddBookmarkPage.USER_AGENT,
2627 settings.getUserAgentString());
2628 }
2629 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002630 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002631 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002632 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002633 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2634 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002635 // Put the dialog at the upper right of the screen, covering the
2636 // star on the title bar.
2637 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002638 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002639 }
2640
2641 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002642 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002643 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002644 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002645 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002646 }
2647
Vivek Sekharb54614f2014-05-01 19:03:37 -07002648 @Override
2649 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2650 boolean capture) {
2651 mUploadHandler = new UploadHandler(this);
2652 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2653 }
2654
Michael Kolb8233fac2010-10-26 16:08:53 -07002655 // thumbnails
2656
2657 /**
2658 * Return the desired width for thumbnail screenshots, which are stored in
2659 * the database, and used on the bookmarks screen.
2660 * @param context Context for finding out the density of the screen.
2661 * @return desired width for thumbnail screenshot.
2662 */
2663 static int getDesiredThumbnailWidth(Context context) {
2664 return context.getResources().getDimensionPixelOffset(
2665 R.dimen.bookmarkThumbnailWidth);
2666 }
2667
2668 /**
2669 * Return the desired height for thumbnail screenshots, which are stored in
2670 * the database, and used on the bookmarks screen.
2671 * @param context Context for finding out the density of the screen.
2672 * @return desired height for thumbnail screenshot.
2673 */
2674 static int getDesiredThumbnailHeight(Context context) {
2675 return context.getResources().getDimensionPixelOffset(
2676 R.dimen.bookmarkThumbnailHeight);
2677 }
2678
Pankaj Gargf49e0222015-09-01 12:19:13 -07002679 void createScreenshotAsync(Tab tab, final ValueCallback<Bitmap> cb) {
2680 if (tab == null) {
2681 cb.onReceiveValue(null);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002682 return;
2683 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002684
2685 tab.capture();
2686
2687 synchronized (mThumbnailCbList) {
2688 mThumbnailCbList.add(cb);
2689 }
2690 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002691
Michael Kolb8233fac2010-10-26 16:08:53 -07002692 private class Copy implements OnMenuItemClickListener {
2693 private CharSequence mText;
2694
John Reck9c35b9c2012-05-30 10:08:50 -07002695 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002696 public boolean onMenuItemClick(MenuItem item) {
2697 copy(mText);
2698 return true;
2699 }
2700
2701 public Copy(CharSequence toCopy) {
2702 mText = toCopy;
2703 }
2704 }
2705
Leon Scroggins63c02662010-11-18 15:16:27 -05002706 private static class Download implements OnMenuItemClickListener {
2707 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002708 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002709 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002710 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002711 private static final String FALLBACK_EXTENSION = "dat";
2712 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002713
John Reck9c35b9c2012-05-30 10:08:50 -07002714 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002715 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002716 if (DataUri.isDataUri(mText)) {
2717 saveDataUri();
2718 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002719 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002720 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002721 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002722 return true;
2723 }
2724
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002725 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2726 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002727 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002728 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002729 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002730 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002731 }
George Mount387d45d2011-10-07 15:57:53 -07002732
2733 /**
2734 * Treats mText as a data URI and writes its contents to a file
2735 * based on the current time.
2736 */
2737 private void saveDataUri() {
2738 FileOutputStream outputStream = null;
2739 try {
2740 DataUri uri = new DataUri(mText);
2741 File target = getTarget(uri);
2742 outputStream = new FileOutputStream(target);
2743 outputStream.write(uri.getData());
2744 final DownloadManager manager =
2745 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2746 manager.addCompletedDownload(target.getName(),
2747 mActivity.getTitle().toString(), false,
2748 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002749 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002750 } catch (IOException e) {
2751 Log.e(LOGTAG, "Could not save data URL");
2752 } finally {
2753 if (outputStream != null) {
2754 try {
2755 outputStream.close();
2756 } catch (IOException e) {
2757 // ignore close errors
2758 }
2759 }
2760 }
2761 }
2762
2763 /**
2764 * Creates a File based on the current time stamp and uses
2765 * the mime type of the DataUri to get the extension.
2766 */
2767 private File getTarget(DataUri uri) throws IOException {
2768 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002769 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002770 String nameBase = format.format(new Date());
2771 String mimeType = uri.getMimeType();
2772 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2773 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2774 if (extension == null) {
2775 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2776 extension = FALLBACK_EXTENSION;
2777 }
2778 extension = "." + extension; // createTempFile needs the '.'
2779 File targetFile = File.createTempFile(nameBase, extension, dir);
2780 return targetFile;
2781 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002782 }
2783
Cary Clark8974d282010-11-22 10:46:05 -05002784 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002785 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002786
John Reck9c35b9c2012-05-30 10:08:50 -07002787 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002788 public boolean onMenuItemClick(MenuItem item) {
2789 if (mWebView != null) {
2790 return mWebView.selectText();
2791 }
2792 return false;
2793 }
2794
2795 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002796 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002797 }
2798
2799 }
2800
Michael Kolb8233fac2010-10-26 16:08:53 -07002801 /********************** TODO: UI stuff *****************************/
2802
2803 // these methods have been copied, they still need to be cleaned up
2804
2805 /****************** tabs ***************************************************/
2806
2807 // basic tab interactions:
2808
2809 // it is assumed that tabcontrol already knows about the tab
2810 protected void addTab(Tab tab) {
2811 mUi.addTab(tab);
2812 }
2813
2814 protected void removeTab(Tab tab) {
2815 mUi.removeTab(tab);
2816 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002817 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002818 }
2819
Michael Kolbf2055602011-04-09 17:20:03 -07002820 @Override
2821 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002822 // monkey protection against delayed start
2823 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002824
2825 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2826 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002827 mTabControl.setCurrentTab(tab);
2828 // the tab is guaranteed to have a webview after setCurrentTab
2829 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002830
2831
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002832 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002833 //Purge active tabs
2834 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002835 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002836 }
2837
John Reck8bcafc12011-08-29 16:43:02 -07002838 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002839 Tab current = mTabControl.getCurrentTab();
2840 if (current != null
2841 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002842 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002843 }
2844 }
2845
John Reck26b18322011-06-21 13:08:58 -07002846 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002847 //Cancel navscreen request
2848 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002849 // Dismiss the subwindow if applicable.
2850 dismissSubWindow(appTab);
2851 // Since we might kill the WebView, remove it from the
2852 // content view first.
2853 mUi.detachTab(appTab);
2854 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002855 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002856 // TODO: analyze why the remove and add are necessary
2857 mUi.attachTab(appTab);
2858 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002859 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002860 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002861 } else {
2862 // If the tab was the current tab, we have to attach
2863 // it to the view system again.
2864 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002865 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002866 }
2867 }
2868
2869 // Remove the sub window if it exists. Also called by TabControl when the
2870 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002871 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002872 public void dismissSubWindow(Tab tab) {
2873 removeSubWindow(tab);
2874 // dismiss the subwindow. This will destroy the WebView.
2875 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002876 WebView wv = getCurrentTopWebView();
2877 if (wv != null) {
2878 wv.requestFocus();
2879 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002880 }
2881
2882 @Override
2883 public void removeSubWindow(Tab t) {
2884 if (t.getSubWebView() != null) {
2885 mUi.removeSubWindow(t.getSubViewContainer());
2886 }
2887 }
2888
2889 @Override
2890 public void attachSubWindow(Tab tab) {
2891 if (tab.getSubWebView() != null) {
2892 mUi.attachSubWindow(tab.getSubViewContainer());
2893 getCurrentTopWebView().requestFocus();
2894 }
2895 }
2896
Mathew Inwood29721c22011-06-29 17:55:24 +01002897 private Tab showPreloadedTab(final UrlData urlData) {
2898 if (!urlData.isPreloaded()) {
2899 return null;
2900 }
2901 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2902 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2903 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002904 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002905 // Could not submit query. Fallback to regular tab creation
2906 tabControl.destroy();
2907 return null;
2908 }
2909 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002910 // check tab count and make room for new tab
2911 if (!mTabControl.canCreateNewTab()) {
2912 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2913 if (leastUsed != null) {
2914 closeTab(leastUsed);
2915 }
2916 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002917 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002918 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002919 mTabControl.addPreloadedTab(t);
2920 addTab(t);
2921 setActiveTab(t);
2922 return t;
2923 }
2924
Michael Kolb7bcafde2011-05-09 13:55:59 -07002925 // open a non inconito tab with the given url data
2926 // and set as active tab
2927 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002928 Tab tab = showPreloadedTab(urlData);
2929 if (tab == null) {
2930 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002931 if ((tab != null) && !urlData.isEmpty()) {
2932 loadUrlDataIn(tab, urlData);
2933 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002934 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002935 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002936 }
2937
Michael Kolb843510f2010-12-09 10:51:49 -08002938 @Override
2939 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002940 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002941 }
2942
Michael Kolb8233fac2010-10-26 16:08:53 -07002943 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002944 public Tab openIncognitoTab() {
2945 return openTab(INCOGNITO_URI, true, true, false);
2946 }
2947
2948 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002949 public Tab openTab(String url, boolean incognito, boolean setActive,
2950 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002951 return openTab(url, incognito, setActive, useCurrent, null);
2952 }
2953
2954 @Override
2955 public Tab openTab(String url, Tab parent, boolean setActive,
2956 boolean useCurrent) {
2957 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2958 setActive, useCurrent, parent);
2959 }
2960
2961 public Tab openTab(String url, boolean incognito, boolean setActive,
2962 boolean useCurrent, Tab parent) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002963 boolean change_tabs = false;
2964 if (setActive) {
2965 Tab currentTab = mTabControl.getCurrentTab();
2966 if (currentTab != null) {
2967 change_tabs = setActive;
2968 setActive = false;
Sagar Dhawan9a1d8e72015-09-11 17:14:38 -07002969 if (mUi instanceof PhoneUi)
2970 currentTab.capture();
Pankaj Gargf49e0222015-09-01 12:19:13 -07002971 }
2972 }
2973
Michael Kolb7bcafde2011-05-09 13:55:59 -07002974 Tab tab = createNewTab(incognito, setActive, useCurrent);
2975 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002976 if (parent instanceof SnapshotTab) {
2977 addTab(tab);
2978 if (setActive)
2979 setActiveTab(tab);
2980 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002981 parent.addChildTab(tab);
2982 }
Michael Kolb519d2282011-05-09 17:03:19 -07002983 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002984 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002985 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002986 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002987
2988 if (change_tabs) {
2989 synchronized (mThumbnailCbList) {
2990 startCaptureTimer();
2991 mLatestCreatedTab = tab;
2992 mThumbnailCbList.add(new ValueCallback<Bitmap>() {
2993 @Override
2994 public void onReceiveValue(Bitmap bitmap) {
2995 synchronized (mThumbnailCbList) {
2996 if (mLatestCreatedTab != null) {
2997 setActiveTab(mLatestCreatedTab);
2998 mLatestCreatedTab = null;
2999 }
3000 stopCaptureTimer();
3001 }
3002 }
3003 });
3004 }
3005 }
3006
Michael Kolb7bcafde2011-05-09 13:55:59 -07003007 return tab;
3008 }
3009
Pankaj Gargf49e0222015-09-01 12:19:13 -07003010 private void startCaptureTimer() {
3011 mCaptureTimer = new CountDownTimer(mCaptureMaxWaitMS, mCaptureMaxWaitMS) {
3012 @Override
3013 public void onTick(long millisUntilFinished) {
3014 // Do nothing
3015 }
3016
3017 @Override
3018 public void onFinish() {
3019 synchronized (mThumbnailCbList) {
3020 Log.e(LOGTAG, "Screen capture timed out while opening new tab");
3021 if (mLatestCreatedTab != null) {
3022 setActiveTab(mLatestCreatedTab);
3023 mLatestCreatedTab = null;
3024 }
3025 }
3026 }
3027 }.start();
3028 }
3029
3030 private void stopCaptureTimer() {
3031 if (mCaptureTimer != null) {
3032 mCaptureTimer.cancel();
3033 mCaptureTimer = null;
3034 }
3035 }
3036
3037
Michael Kolb7bcafde2011-05-09 13:55:59 -07003038 // this method will attempt to create a new tab
3039 // incognito: private browsing tab
3040 // setActive: ste tab as current tab
3041 // useCurrent: if no new tab can be created, return current tab
3042 private Tab createNewTab(boolean incognito, boolean setActive,
3043 boolean useCurrent) {
3044 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07003045 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
3046 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003047 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003048 if (mTabControl.canCreateNewTab()) {
3049 tab = mTabControl.createNewTab(incognito, !setActive);
3050 addTab(tab);
3051 if (setActive) {
3052 setActiveTab(tab);
3053 } else {
3054 tab.pause();
3055 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003056 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003057 if (useCurrent) {
3058 tab = mTabControl.getCurrentTab();
3059 reuseTab(tab, null);
3060 } else {
3061 mUi.showMaxTabsWarning();
3062 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003063 }
3064 }
3065 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003066 }
3067
John Reck2bc80422011-06-30 15:11:49 -07003068 @Override
3069 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3070 SnapshotTab tab = null;
3071 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07003072 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07003073 addTab(tab);
3074 if (setActive) {
3075 setActiveTab(tab);
3076 }
3077 } else {
3078 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003079 }
3080 return tab;
3081 }
3082
Michael Kolb8233fac2010-10-26 16:08:53 -07003083 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003084 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003085 * @return boolean True if we successfully switched to a different tab. If
3086 * the indexth tab is null, or if that tab is the same as
3087 * the current one, return false.
3088 */
3089 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003090 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003091 Tab currentTab = mTabControl.getCurrentTab();
3092 if (tab == null || tab == currentTab) {
3093 return false;
3094 }
3095 setActiveTab(tab);
3096 return true;
3097 }
3098
3099 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003100 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003101 closeCurrentTab(false);
3102 }
3103
3104 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003105 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003106 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003107 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003108 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003109 return;
3110 }
Michael Kolbc831b632011-05-11 09:30:34 -07003111 final Tab current = mTabControl.getCurrentTab();
3112 final int pos = mTabControl.getCurrentPosition();
3113 Tab newTab = current.getParent();
3114 if (newTab == null) {
3115 newTab = mTabControl.getTab(pos + 1);
3116 if (newTab == null) {
3117 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003118 }
3119 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003120 if (andQuit) {
3121 mTabControl.setCurrentTab(newTab);
3122 closeTab(current);
3123 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003124 // Close window
3125 closeTab(current);
3126 }
3127 }
3128
3129 /**
3130 * Close the tab, remove its associated title bar, and adjust mTabControl's
3131 * current tab to a valid value.
3132 */
3133 @Override
3134 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003135 if (tab == mTabControl.getCurrentTab()) {
3136 closeCurrentTab();
3137 } else {
3138 removeTab(tab);
3139 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003140 }
3141
Afzal Najamd4e33312012-04-26 01:54:01 -04003142 /**
3143 * Close all tabs except the current one
3144 */
3145 @Override
3146 public void closeOtherTabs() {
3147 int inactiveTabs = mTabControl.getTabCount() - 1;
3148 for (int i = inactiveTabs; i >= 0; i--) {
3149 Tab tab = mTabControl.getTab(i);
3150 if (tab != mTabControl.getCurrentTab()) {
3151 removeTab(tab);
3152 }
3153 }
3154 }
3155
Michael Kolb8233fac2010-10-26 16:08:53 -07003156 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003157 protected void loadUrlFromContext(String url) {
3158 Tab tab = getCurrentTab();
3159 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003160 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003161 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003162 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003163 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003164 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003165 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003166 }
3167 }
3168 }
3169
3170 /**
3171 * Load the URL into the given WebView and update the title bar
3172 * to reflect the new load. Call this instead of WebView.loadUrl
3173 * directly.
3174 * @param view The WebView used to load url.
3175 * @param url The URL to load.
3176 */
John Reck71e51422011-07-01 16:49:28 -07003177 @Override
3178 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003179 loadUrl(tab, url, null);
3180 }
3181
3182 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3183 if (tab != null) {
3184 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003185 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003186 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003187 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003188 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003189 }
3190
3191 /**
3192 * Load UrlData into a Tab and update the title bar to reflect the new
3193 * load. Call this instead of UrlData.loadIn directly.
3194 * @param t The Tab used to load.
3195 * @param data The UrlData being loaded.
3196 */
3197 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003198 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003199 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003200 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003201 } else {
John Reck38b39652012-06-05 09:22:59 -07003202 if (t != null && data.mDisableUrlOverride) {
3203 t.disableUrlOverridingForLoad();
3204 }
John Reck26b18322011-06-21 13:08:58 -07003205 loadUrl(t, data.mUrl, data.mHeaders);
3206 }
3207 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003208 }
3209
John Reck30c714c2010-12-16 17:30:34 -08003210 @Override
3211 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003212 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003213 //In case of tab can go back (aka tab has navigation entry) do nothing
3214 //else just load homepage in current tab.
3215 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003216 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003217 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003218 }
3219
3220 void goBackOnePageOrQuit() {
3221 Tab current = mTabControl.getCurrentTab();
3222 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003223 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3224 showExitDialog(mActivity);
3225 } else {
3226 /*
3227 * Instead of finishing the activity, simply push this to the back
3228 * of the stack and let ActivityManager to choose the foreground
3229 * activity. As BrowserActivity is singleTask, it will be always the
3230 * root of the task. So we can use either true or false for
3231 * moveTaskToBack().
3232 */
3233 mActivity.moveTaskToBack(true);
3234 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003235 return;
3236 }
John Reckef654f12011-07-12 16:42:08 -07003237 if (current.canGoBack()) {
3238 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003239 } else {
3240 // Check to see if we are closing a window that was created by
3241 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003242 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003243 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003244 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003245 // Now we close the other tab
3246 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003247 } else if (BrowserConfig.getInstance(getContext())
3248 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3249 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003250 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003251 /*
3252 * Instead of finishing the activity, simply push this to the back
3253 * of the stack and let ActivityManager to choose the foreground
3254 * activity. As BrowserActivity is singleTask, it will be always the
3255 * root of the task. So we can use either true or false for
3256 * moveTaskToBack().
3257 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003258 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003259 }
3260 }
3261 }
3262
3263 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003264 * helper method for key handler
3265 * returns the current tab if it can't advance
3266 */
Michael Kolbc831b632011-05-11 09:30:34 -07003267 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003268 int pos = mTabControl.getCurrentPosition() + 1;
3269 if (pos >= mTabControl.getTabCount()) {
3270 pos = 0;
3271 }
3272 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003273 }
3274
3275 /**
3276 * helper method for key handler
3277 * returns the current tab if it can't advance
3278 */
Michael Kolbc831b632011-05-11 09:30:34 -07003279 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003280 int pos = mTabControl.getCurrentPosition() - 1;
3281 if ( pos < 0) {
3282 pos = mTabControl.getTabCount() - 1;
3283 }
3284 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003285 }
3286
John Reckbcef87f2012-02-03 14:58:34 -08003287 boolean isMenuOrCtrlKey(int keyCode) {
3288 return (KeyEvent.KEYCODE_MENU == keyCode)
3289 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3290 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3291 }
3292
Michael Kolb0035fad2011-03-14 13:25:28 -07003293 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003294 * handle key events in browser
3295 *
3296 * @param keyCode
3297 * @param event
3298 * @return true if handled, false to pass to super
3299 */
John Reck9c35b9c2012-05-30 10:08:50 -07003300 @Override
3301 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003302 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3303 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003304 if (!mUi.isComboViewShowing()) {
3305 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3306 true, false);
3307 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003308 return true;
3309 }
3310
Cary Clark160bbb92011-01-10 11:17:07 -05003311 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003312 // Even if MENU is already held down, we need to call to super to open
3313 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003314 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003315 mMenuIsDown = true;
3316 return false;
3317 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003318
Cary Clark8ff8c662010-12-29 15:03:05 -05003319 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003320 Tab tab = getCurrentTab();
3321 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003322
Cary Clark160bbb92011-01-10 11:17:07 -05003323 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3324 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003325
Michael Kolb8233fac2010-10-26 16:08:53 -07003326 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003327 case KeyEvent.KEYCODE_TAB:
3328 if (event.isCtrlPressed()) {
3329 if (event.isShiftPressed()) {
3330 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003331 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003332 } else {
3333 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003334 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003335 }
3336 return true;
3337 }
3338 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003339 case KeyEvent.KEYCODE_SPACE:
3340 // WebView/WebTextView handle the keys in the KeyDown. As
3341 // the Activity's shortcut keys are only handled when WebView
3342 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003343 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003344 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003345 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003346 pageDown();
3347 }
3348 return true;
3349 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003350 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003351 event.startTracking();
3352 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003353 case KeyEvent.KEYCODE_FORWARD:
3354 if (!noModifiers) break;
3355 tab.goForward();
3356 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003357 case KeyEvent.KEYCODE_DPAD_LEFT:
3358 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003359 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003360 return true;
3361 }
3362 break;
3363 case KeyEvent.KEYCODE_DPAD_RIGHT:
3364 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003365 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003366 return true;
3367 }
3368 break;
3369 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003370 if (ctrl) {
3371 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003372 return true;
3373 }
3374 break;
Michael Kolba4183062011-01-16 10:43:21 -08003375// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003376 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003377 if (ctrl ) {
3378 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003379 return true;
3380 }
3381 break;
Michael Kolba4183062011-01-16 10:43:21 -08003382// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003383// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003384// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003385// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003386// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003387// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003388// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003389// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003390// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003391// case KeyEvent.KEYCODE_M: // unused
3392// case KeyEvent.KEYCODE_N: // in Chrome: new window
3393// case KeyEvent.KEYCODE_O: // in Chrome: open file
3394// case KeyEvent.KEYCODE_P: // in Chrome: print page
3395// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003396// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003397// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3398 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003399 // we can't use the ctrl/shift flags, they check for
3400 // exclusive use of a modifier
3401 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003402 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003403 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003404 } else {
3405 openTabToHomePage();
3406 }
3407 return true;
3408 }
3409 break;
3410// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3411// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003412// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003413// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3414// case KeyEvent.KEYCODE_Y: // unused
3415// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003416 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003417 // it is a regular key and webview is not null
3418 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003419 }
3420
John Reck9c35b9c2012-05-30 10:08:50 -07003421 @Override
3422 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003423 switch(keyCode) {
3424 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003425 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003426 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003427 return true;
3428 }
3429 break;
3430 }
3431 return false;
3432 }
3433
John Reck9c35b9c2012-05-30 10:08:50 -07003434 @Override
3435 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003436 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003437 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003438 if (KeyEvent.KEYCODE_MENU == keyCode
3439 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003440 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003441 }
3442 }
Cary Clark160bbb92011-01-10 11:17:07 -05003443 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003444 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003445 case KeyEvent.KEYCODE_BACK:
3446 if (event.isTracking() && !event.isCanceled()) {
3447 onBackKey();
3448 return true;
3449 }
3450 break;
3451 }
3452 return false;
3453 }
3454
3455 public boolean isMenuDown() {
3456 return mMenuIsDown;
3457 }
3458
John Reck9c35b9c2012-05-30 10:08:50 -07003459 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003460 public void setupAutoFill(Message message) {
3461 // Open the settings activity at the AutoFill profile fragment so that
3462 // the user can create a new profile. When they return, we will dispatch
3463 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003464 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003465 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3466 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003467 }
John Reckb3417f02011-01-14 11:01:05 -08003468
John Reck9c35b9c2012-05-30 10:08:50 -07003469 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003470 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003471 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003472 return true;
3473 }
3474
John Reck1cf4b792011-07-26 10:22:22 -07003475 @Override
3476 public boolean shouldCaptureThumbnails() {
3477 return mUi.shouldCaptureThumbnails();
3478 }
3479
Michael Kolbc3af0672011-08-09 10:24:41 -07003480 @Override
Pankaj Gargf49e0222015-09-01 12:19:13 -07003481 public void onThumbnailCapture(Bitmap bm) {
3482 synchronized (mThumbnailCbList) {
3483 int num_entries = mThumbnailCbList.size();
3484 while (num_entries > 0){
3485 num_entries--;
3486 ValueCallback<Bitmap> cb = mThumbnailCbList.get(num_entries);
3487 cb.onReceiveValue(bm);
3488 }
3489 mThumbnailCbList.clear();
3490 }
3491 }
3492
3493 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003494 public boolean supportsVoice() {
3495 PackageManager pm = mActivity.getPackageManager();
3496 List activities = pm.queryIntentActivities(new Intent(
3497 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3498 return activities.size() != 0;
3499 }
3500
3501 @Override
3502 public void startVoiceRecognizer() {
3503 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003504 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003505 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3506 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3507 mActivity.startActivityForResult(voice, VOICE_RESULT);
3508 }
3509
Pankaj Garg7b279f62014-08-12 14:47:18 -07003510 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003511 if (mLevel == level)
3512 return;
3513 mLevel = level;
3514 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003515 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3516 lp.dimAmount = level;
3517 mActivity.getWindow().setAttributes(lp);
3518 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3519 } else {
3520 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3521 }
3522 }
3523
Michael Kolb0b129122012-06-04 16:31:58 -07003524 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003525 public void setBlockEvents(boolean block) {
3526 mBlockEvents = block;
3527 }
3528
John Reck9c35b9c2012-05-30 10:08:50 -07003529 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003530 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003531 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003532 }
3533
John Reck9c35b9c2012-05-30 10:08:50 -07003534 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003535 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003536 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003537 }
3538
John Reck9c35b9c2012-05-30 10:08:50 -07003539 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003540 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003541 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003542 }
3543
John Reck9c35b9c2012-05-30 10:08:50 -07003544 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003545 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003546 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003547 }
3548
John Reck9c35b9c2012-05-30 10:08:50 -07003549 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003550 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003551 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003552 }
3553
Pankaj Garg49b79252014-11-07 17:33:41 -08003554 @Override
3555 public boolean shouldShowAppMenu() {
3556 return true;
3557 }
3558
3559 @Override
3560 public int getMenuThemeResourceId() {
3561 return R.style.OverflowMenuTheme;
3562 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003563}