blob: 4c059067df3086e4abdc2ec67c1d9aebb6ce7bc5 [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);
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700815 // reset the search engine based on locale
816 mSettings.updateSearchEngine(false);
George Mount3636d0a2011-11-21 09:08:21 -0800817 mSettings.setLastRunPaused(false);
Michael Kolb70976932010-11-30 11:34:01 -0800818 Tab current = mTabControl.getCurrentTab();
819 if (current != null) {
820 current.resume();
821 resumeWebViewTimers(current);
822 }
John Reckf57c0292011-07-21 18:15:39 -0700823 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200824
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 mUi.onResume();
826 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100827 NfcHandler.register(mActivity, this);
Vivek Sekharc70ae632015-04-08 17:54:05 -0700828 if (current != null && current.getWebView().isShowingCrashView())
829 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700830 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700831
John Reckf57c0292011-07-21 18:15:39 -0700832 }
833
Michael Kolb70976932010-11-30 11:34:01 -0800834 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800835 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800836 * @param tab guaranteed non-null
837 */
838 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 boolean inLoad = tab.inPageLoad();
Tarun Nainani7c762c82015-08-04 13:42:09 -0700840 if ((!mActivityStopped && !inLoad) || (mActivityStopped && inLoad)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 CookieSyncManager.getInstance().startSync();
842 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100843 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 }
845 }
846
Michael Kolb70976932010-11-30 11:34:01 -0800847 /**
848 * Pause all WebView timers using the WebView of the given tab
849 * @param tab
850 * @return true if the timers are paused or tab is null
851 */
852 private boolean pauseWebViewTimers(Tab tab) {
853 if (tab == null) {
854 return true;
855 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100857 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 }
Michael Kolb70976932010-11-30 11:34:01 -0800860 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 }
862
John Reck9c35b9c2012-05-30 10:08:50 -0700863 @Override
864 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800865 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
867 mUploadHandler = null;
868 }
869 if (mTabControl == null) return;
870 mUi.onDestroy();
871 // Remove the current tab and sub window
872 Tab t = mTabControl.getCurrentTab();
873 if (t != null) {
874 dismissSubWindow(t);
875 removeTab(t);
876 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500877 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 // Destroy all the tabs
879 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700880 // Unregister receiver
881 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 }
883
884 protected boolean isActivityPaused() {
885 return mActivityPaused;
886 }
887
John Reck9c35b9c2012-05-30 10:08:50 -0700888 @Override
889 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 mTabControl.freeMemory();
891 }
892
893 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700894 public void stopLoading() {
895 mLoadStopped = true;
896 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700898 if (w != null) {
899 w.stopLoading();
900 mUi.onPageStopped(tab);
901 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700902 }
903
904 boolean didUserStopLoading() {
905 return mLoadStopped;
906 }
907
kaiyiza016da12013-08-26 17:50:22 +0800908 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700909 final String reminderType = getContext().getResources().getString(
910 R.string.def_wifi_browser_interaction_remind_type);
911 final String selectionConnnection = getContext().getResources().getString(
912 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700913 final String wifiSelection = getContext().getResources().getString(
914 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700915
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700916 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
917 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700918 return;
919
kaiyiza016da12013-08-26 17:50:22 +0800920 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700921 Context.CONNECTIVITY_SERVICE);
922 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700923 WifiManager wifiMgr = (WifiManager) this.getContext()
924 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700925 if (networkInfo == null
926 || (networkInfo != null && (networkInfo.getType() !=
927 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700928 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
929 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700930 List<ScanResult> list = wifiMgr.getScanResults();
931 // Have no AP's for Wifi's fall back to data
932 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700933 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700934 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
935 this.getContext().startActivity(intent);
936 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700937 // Request to select Wifi AP
938 try {
939 Intent intent = new Intent(wifiSelection);
940 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
941 this.getContext().startActivity(intent);
942 } catch (Exception e) {
943 String err_msg = this.getContext().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700944 R.string.activity_not_found, wifiSelection);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700945 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
946 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700947 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700948 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800949 }
950 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700951
Michael Kolb8233fac2010-10-26 16:08:53 -0700952 // WebViewController
953
954 @Override
John Reck324d4402011-01-11 16:56:42 -0800955 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700956
Michael Kolb8233fac2010-10-26 16:08:53 -0700957 // reset sync timer to avoid sync starts during loading a page
958 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700959 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700960 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800961 boolean networkNotifier = BrowserConfig.getInstance(getContext())
962 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700963 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700964 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800965 } else {
966 if (!mNetworkHandler.isNetworkUp()) {
967 view.setNetworkAvailable(false);
968 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700969 }
970
971 // when BrowserActivity just starts, onPageStarted may be called before
972 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
973 // to start the timer. As we won't switch tabs while an activity is in
974 // pause state, we can ensure calling resume and pause in pair.
Tarun Nainani7c762c82015-08-04 13:42:09 -0700975 if (mActivityStopped) {
Michael Kolb70976932010-11-30 11:34:01 -0800976 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 }
978 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 endActionMode();
980
John Reck30c714c2010-12-16 17:30:34 -0800981 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700982
John Reck324d4402011-01-11 16:56:42 -0800983 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700984 // update the bookmark database for favicon
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700985 syncBookmarkFavicon(tab, null, url, favicon);
Michael Kolb8233fac2010-10-26 16:08:53 -0700986
987 Performance.tracePageStart(url);
988
989 // Performance probe
990 if (false) {
991 Performance.onPageStarted();
992 }
993
994 }
995
996 @Override
John Reck324d4402011-01-11 16:56:42 -0800997 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700998 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800999 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001000
Michael Kolb8233fac2010-10-26 16:08:53 -07001001 // Performance probe
1002 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001003 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 }
1005
Tarun Nainani8eb00912014-07-17 12:28:32 -07001006 tab.onPageFinished();
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001007 syncBookmarkFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -07001008
Michael Kolb8233fac2010-10-26 16:08:53 -07001009 Performance.tracePageFinished();
1010 }
1011
1012 @Override
John Reck30c714c2010-12-16 17:30:34 -08001013 public void onProgressChanged(Tab tab) {
1014 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001015
1016 if (newProgress == 100) {
1017 CookieSyncManager.getInstance().sync();
1018 // onProgressChanged() may continue to be called after the main
1019 // frame has finished loading, as any remaining sub frames continue
1020 // to load. We'll only get called once though with newProgress as
1021 // 100 when everything is loaded. (onPageFinished is called once
1022 // when the main frame completes loading regardless of the state of
1023 // any sub frames so calls to onProgressChanges may continue after
1024 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001025 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001026 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001027 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001028 } else if (mWasInPageLoad) {
1029 mWasInPageLoad = false;
1030 updateInLoadMenuItems(mCachedMenu, tab);
1031 }
1032
Tarun Nainani7c762c82015-08-04 13:42:09 -07001033 if (mActivityStopped && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001034 // pause the WebView timer and release the wake lock if it is
1035 // finished while BrowserActivity is in pause state.
1036 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 }
1038 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001039 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001041 // still loading
1042 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001044 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001045 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001046 } else {
1047 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001048 }
1049 }
Pankaj Gargf9040c82015-08-14 10:19:31 -07001050
1051 mUi.onProgressChanged(tab);
John Reck30c714c2010-12-16 17:30:34 -08001052 }
1053
1054 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001055 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001056 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001057 }
1058
1059 @Override
1060 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001061 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001062 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001063 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1065 return;
1066 }
1067 // Update the title in the history database if not in private browsing mode
1068 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001069 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001070 }
1071 }
1072
1073 @Override
1074 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001075 syncBookmarkFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1076 ((BaseUi)mUi).setFavicon(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 }
1078
1079 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001080 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001081 // if tab is snapshot tab we want to prevent navigation from occuring
1082 // since snapshot tab opens a new tab with the url
1083 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 }
1085
1086 @Override
1087 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1088 if (mMenuIsDown) {
1089 // only check shortcut key when MENU is held
1090 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1091 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001093 int keyCode = event.getKeyCode();
1094 // We need to send almost every key to WebKit. However:
1095 // 1. We don't want to block the device on the renderer for
1096 // some keys like menu, home, call.
1097 // 2. There are no WebKit equivalents for some of these keys
1098 // (see app/keyboard_codes_win.h)
1099 // Note that these are not the same set as KeyEvent.isSystemKey:
1100 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1101 if (keyCode == KeyEvent.KEYCODE_MENU ||
1102 keyCode == KeyEvent.KEYCODE_HOME ||
1103 keyCode == KeyEvent.KEYCODE_BACK ||
1104 keyCode == KeyEvent.KEYCODE_CALL ||
1105 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1106 keyCode == KeyEvent.KEYCODE_POWER ||
1107 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1108 keyCode == KeyEvent.KEYCODE_CAMERA ||
1109 keyCode == KeyEvent.KEYCODE_FOCUS ||
1110 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1111 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1112 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1113 return true;
1114 }
1115
1116 // We also have to intercept some shortcuts before we send them to the ContentView.
1117 if (event.isCtrlPressed() && (
1118 keyCode == KeyEvent.KEYCODE_TAB ||
1119 keyCode == KeyEvent.KEYCODE_W ||
1120 keyCode == KeyEvent.KEYCODE_F4)) {
1121 return true;
1122 }
1123
1124 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 }
1126
Tarun Nainanif5563b62015-01-21 18:52:59 -08001127 private void handleMediaKeyEvent(KeyEvent event) {
1128
1129 int keyCode = event.getKeyCode();
1130 // send media key events to audio manager
1131 if (Build.VERSION.SDK_INT >= 19) {
1132
1133 switch (keyCode) {
1134 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1135 case KeyEvent.KEYCODE_MEDIA_STOP:
1136 case KeyEvent.KEYCODE_MEDIA_NEXT:
1137 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1138 case KeyEvent.KEYCODE_MEDIA_REWIND:
1139 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1140 case KeyEvent.KEYCODE_MUTE:
1141 case KeyEvent.KEYCODE_MEDIA_PLAY:
1142 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1143 case KeyEvent.META_SHIFT_RIGHT_ON:
1144 case KeyEvent.KEYCODE_MEDIA_EJECT:
1145 case KeyEvent.KEYCODE_MEDIA_RECORD:
1146 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1147
1148 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1149 .getSystemService(Context.AUDIO_SERVICE);
1150 audioManager.dispatchMediaKeyEvent(event);
1151 }
1152 }
1153 }
1154
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 @Override
John Reck997b1b72012-04-19 18:08:25 -07001156 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001158 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001160 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 } else {
John Reck997b1b72012-04-19 18:08:25 -07001162 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001163 }
1164 }
John Reck997b1b72012-04-19 18:08:25 -07001165 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 }
1167
1168 @Override
John Reck324d4402011-01-11 16:56:42 -08001169 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001170 // Don't save anything in private browsing mode or when disabling history
1171 // for regular tabs is enabled
1172 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1173 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1174 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001175
John Reck49a603c2011-03-03 09:33:05 -08001176 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001177
John Reck324d4402011-01-11 16:56:42 -08001178 if (TextUtils.isEmpty(url)
1179 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 return;
1181 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001182
John Reckf57c0292011-07-21 18:15:39 -07001183 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001184 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 }
1186
1187 @Override
1188 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1189 AsyncTask<Void, Void, String[]> task =
1190 new AsyncTask<Void, Void, String[]>() {
1191 @Override
1192 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001193 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001194 }
1195 @Override
1196 public void onPostExecute(String[] result) {
1197 callback.onReceiveValue(result);
1198 }
1199 };
1200 task.execute();
1201 }
1202
1203 @Override
1204 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1205 final HttpAuthHandler handler, final String host,
1206 final String realm) {
1207 String username = null;
1208 String password = null;
1209
1210 boolean reuseHttpAuthUsernamePassword
1211 = handler.useHttpAuthUsernamePassword();
1212
1213 if (reuseHttpAuthUsernamePassword && view != null) {
1214 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1215 if (credentials != null && credentials.length == 2) {
1216 username = credentials[0];
1217 password = credentials[1];
1218 }
1219 }
1220
1221 if (username != null && password != null) {
1222 handler.proceed(username, password);
1223 } else {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -07001224 if (!tab.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001225 handler.cancel();
1226 }
1227 }
1228 }
1229
1230 @Override
1231 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001232 String contentDisposition, String mimetype, String referer,
1233 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001234 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001235 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001236 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001237 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001238 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 // This Tab was opened for the sole purpose of downloading a
1240 // file. Remove it.
1241 if (tab == mTabControl.getCurrentTab()) {
1242 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001243 if (tab.getDerivedFromIntent())
1244 closeTab(tab);
1245 else
1246 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001247 } else {
1248 // In this case, it is not.
1249 closeTab(tab);
1250 }
1251 }
1252 }
1253
1254 @Override
1255 public Bitmap getDefaultVideoPoster() {
1256 return mUi.getDefaultVideoPoster();
1257 }
1258
1259 @Override
1260 public View getVideoLoadingProgressView() {
1261 return mUi.getVideoLoadingProgressView();
1262 }
1263
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 // helper method
1265
1266 /*
1267 * Update the favorites icon if the private browsing isn't enabled and the
1268 * icon is valid.
1269 */
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001270 private void syncBookmarkFavicon(Tab tab, final String originalUrl,
1271 final String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 if (favicon == null) {
1273 return;
1274 }
1275 if (!tab.isPrivateBrowsingEnabled()) {
1276 Bookmarks.updateFavicon(mActivity
1277 .getContentResolver(), originalUrl, url, favicon);
1278 }
1279 }
1280
Leon Scroggins4cd97792010-12-03 15:31:56 -05001281 @Override
1282 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001283 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001284 mUi.bookmarkedStatusHasChanged(tab);
1285 }
1286
Michael Kolb8233fac2010-10-26 16:08:53 -07001287 // end WebViewController
1288
1289 protected void pageUp() {
1290 getCurrentTopWebView().pageUp(false);
1291 }
1292
1293 protected void pageDown() {
1294 getCurrentTopWebView().pageDown(false);
1295 }
1296
1297 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001298 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001299 public void editUrl() {
1300 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001301 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001302 }
1303
John Reck9c35b9c2012-05-30 10:08:50 -07001304 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001305 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001306 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001307 if (tab.inForeground()) {
1308 if (mUi.isCustomViewShowing()) {
1309 callback.onCustomViewHidden();
1310 return;
1311 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001312 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001313 // Save the menu state and set it to empty while the custom
1314 // view is showing.
1315 mOldMenuState = mMenuState;
1316 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001317 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001318 }
1319 }
1320
1321 @Override
1322 public void hideCustomView() {
1323 if (mUi.isCustomViewShowing()) {
1324 mUi.onHideCustomView();
1325 // Reset the old menu state.
1326 mMenuState = mOldMenuState;
1327 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001328 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001329 }
1330 }
1331
John Reck9c35b9c2012-05-30 10:08:50 -07001332 @Override
1333 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001334 Intent intent) {
1335 if (getCurrentTopWebView() == null) return;
1336 switch (requestCode) {
1337 case PREFERENCES_PAGE:
1338 if (resultCode == Activity.RESULT_OK && intent != null) {
1339 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001340 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001341 mTabControl.removeParentChildRelationShips();
Pankaj Garg75273bb2015-08-20 11:43:49 -07001342 } else if (action.equals(PreferenceKeys.ACTION_RELOAD_PAGE)) {
1343 ArrayList<String> origins =
1344 intent.getStringArrayListExtra(EXTRA_UPDATED_URLS);
1345 if (origins.isEmpty()) {
1346 mTabControl.reloadLiveTabs();
1347 }
1348 else{
1349 for (String origin : origins){
1350 mTabControl.findAndReload(origin);
1351 }
1352 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001353 }
1354 }
1355 break;
1356 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001357 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001358 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001359 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001361 case AUTOFILL_SETUP:
1362 // Determine whether a profile was actually set up or not
1363 // and if so, send the message back to the WebTextView to
1364 // fill the form with the new profile.
1365 if (getSettings().getAutoFillProfile() != null) {
1366 mAutoFillSetupMessage.sendToTarget();
1367 mAutoFillSetupMessage = null;
1368 }
1369 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001370 case COMBO_VIEW:
1371 if (intent == null || resultCode != Activity.RESULT_OK) {
1372 break;
1373 }
John Reck3ba45532011-08-11 16:26:53 -07001374 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001375 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1376 Tab t = getCurrentTab();
1377 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001378 mUpdateMyNavThumbnail = true;
1379 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001380 loadUrl(t, uri.toString());
1381 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1382 String[] urls = intent.getStringArrayExtra(
1383 ComboViewActivity.EXTRA_OPEN_ALL);
1384 Tab parent = getCurrentTab();
1385 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001386 if (url != null) {
1387 parent = openTab(url, parent,
1388 !mSettings.openInBackground(), true);
1389 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001390 }
1391 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1392 long id = intent.getLongExtra(
1393 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1394 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001395 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001396 }
1397 }
1398 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001399 case VOICE_RESULT:
1400 if (resultCode == Activity.RESULT_OK && intent != null) {
1401 ArrayList<String> results = intent.getStringArrayListExtra(
1402 RecognizerIntent.EXTRA_RESULTS);
1403 if (results.size() >= 1) {
1404 mVoiceResult = results.get(0);
1405 }
1406 }
1407 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001408 case MY_NAVIGATION:
1409 if (intent == null || resultCode != Activity.RESULT_OK) {
1410 break;
1411 }
1412
1413 if (intent.getBooleanExtra("need_refresh", false) &&
1414 getCurrentTopWebView() != null) {
1415 getCurrentTopWebView().reload();
1416 }
1417 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001418 default:
1419 break;
1420 }
1421 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001422 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 }
1424
1425 /**
1426 * Open the Go page.
1427 * @param startWithHistory If true, open starting on the history tab.
1428 * Otherwise, start with the bookmarks tab.
1429 */
1430 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001431 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001432 if (mTabControl.getCurrentWebView() == null) {
1433 return;
1434 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001435 // clear action mode
1436 if (isInCustomActionMode()) {
1437 endActionMode();
1438 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001439 Bundle extras = new Bundle();
1440 // Disable opening in a new window if we have maxed out the windows
1441 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1442 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001443 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001444 }
1445
1446 // combo view callbacks
1447
Michael Kolb8233fac2010-10-26 16:08:53 -07001448 // key handling
1449 protected void onBackKey() {
1450 if (!mUi.onBackKey()) {
1451 WebView subwindow = mTabControl.getCurrentSubWindow();
1452 if (subwindow != null) {
1453 if (subwindow.canGoBack()) {
1454 subwindow.goBack();
1455 } else {
1456 dismissSubWindow(mTabControl.getCurrentTab());
1457 }
1458 } else {
1459 goBackOnePageOrQuit();
1460 }
1461 }
1462 }
1463
Michael Kolb4bd767d2011-05-27 11:33:55 -07001464 protected boolean onMenuKey() {
1465 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001466 }
1467
Michael Kolb8233fac2010-10-26 16:08:53 -07001468 // menu handling and state
1469 // TODO: maybe put into separate handler
1470
John Reck9c35b9c2012-05-30 10:08:50 -07001471 @Override
1472 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001473 if (mMenuState == EMPTY_MENU) {
1474 return false;
1475 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001476 MenuInflater inflater = mActivity.getMenuInflater();
1477 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 return true;
1479 }
1480
John Reck9c35b9c2012-05-30 10:08:50 -07001481 @Override
1482 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001483 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001484 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001485 return;
1486 }
1487 if (!(v instanceof WebView)) {
1488 return;
1489 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001490 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001491 WebView.HitTestResult result;
1492
1493 /* Determine whether the ContextMenu got triggered because
1494 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1495 * received. The mResult acts as a flag to identify, how it got trigerred
1496 */
1497 if (mResult == null){
1498 result = webview.getHitTestResult();
1499 } else {
1500 result = mResult;
1501 }
1502
Michael Kolb8233fac2010-10-26 16:08:53 -07001503 if (result == null) {
1504 return;
1505 }
1506
1507 int type = result.getType();
1508 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001509
1510 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1511 unknownTypeMap.put("webview", webview);
1512 final Message msg = mHandler.obtainMessage(
1513 UNKNOWN_TYPE_MSG, unknownTypeMap);
1514 /* As defined in android developers guide
1515 * when UNKNOWN_TYPE is received as a result of HitTest
1516 * you need to determing the type by invoking requestFocusNodeHref
1517 */
1518 webview.requestFocusNodeHref(msg);
1519
Michael Kolb8233fac2010-10-26 16:08:53 -07001520 Log.w(LOGTAG,
1521 "We should not show context menu when nothing is touched");
1522 return;
1523 }
1524 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1525 // let TextView handles context menu
1526 return;
1527 }
1528
1529 // Note, http://b/issue?id=1106666 is requesting that
1530 // an inflated menu can be used again. This is not available
1531 // yet, so inflate each time (yuk!)
1532 MenuInflater inflater = mActivity.getMenuInflater();
1533 inflater.inflate(R.menu.browsercontext, menu);
1534
1535 // Show the correct menu group
1536 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001537 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001538 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001539 menu.setGroupVisible(R.id.PHONE_MENU,
1540 type == WebView.HitTestResult.PHONE_TYPE);
1541 menu.setGroupVisible(R.id.EMAIL_MENU,
1542 type == WebView.HitTestResult.EMAIL_TYPE);
1543 menu.setGroupVisible(R.id.GEO_MENU,
1544 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001545 String itemUrl = null;
1546 String url = webview.getOriginalUrl();
1547 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1548 itemUrl = Uri.decode(navigationUrl);
1549 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1550 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1551 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1552 } else {
1553 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1554 }
1555 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1556 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1557 } else {
1558 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1559
1560 menu.setGroupVisible(R.id.IMAGE_MENU,
1561 type == WebView.HitTestResult.IMAGE_TYPE
1562 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1563 menu.setGroupVisible(R.id.ANCHOR_MENU,
1564 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1565 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001566
1567 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1568 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1569 UrlUtils.isDownloadableScheme(extra));
1570 }
1571 else {
1572 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1573 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001574 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001575 // Setup custom handling depending on the type
1576 switch (type) {
1577 case WebView.HitTestResult.PHONE_TYPE:
1578 menu.setHeaderTitle(Uri.decode(extra));
1579 menu.findItem(R.id.dial_context_menu_id).setIntent(
1580 new Intent(Intent.ACTION_VIEW, Uri
1581 .parse(WebView.SCHEME_TEL + extra)));
1582 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1583 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1584 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1585 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1586 addIntent);
1587 menu.findItem(R.id.copy_phone_context_menu_id)
1588 .setOnMenuItemClickListener(
1589 new Copy(extra));
1590 break;
1591
1592 case WebView.HitTestResult.EMAIL_TYPE:
1593 menu.setHeaderTitle(extra);
1594 menu.findItem(R.id.email_context_menu_id).setIntent(
1595 new Intent(Intent.ACTION_VIEW, Uri
1596 .parse(WebView.SCHEME_MAILTO + extra)));
1597 menu.findItem(R.id.copy_mail_context_menu_id)
1598 .setOnMenuItemClickListener(
1599 new Copy(extra));
1600 break;
1601
1602 case WebView.HitTestResult.GEO_TYPE:
1603 menu.setHeaderTitle(extra);
1604 menu.findItem(R.id.map_context_menu_id).setIntent(
1605 new Intent(Intent.ACTION_VIEW, Uri
1606 .parse(WebView.SCHEME_GEO
1607 + URLEncoder.encode(extra))));
1608 menu.findItem(R.id.copy_geo_context_menu_id)
1609 .setOnMenuItemClickListener(
1610 new Copy(extra));
1611 break;
1612
1613 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1614 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001615 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 // decide whether to show the open link in new tab option
1617 boolean showNewTab = mTabControl.canCreateNewTab();
1618 MenuItem newTabItem
1619 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001620 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001621 ? R.string.contextmenu_openlink_newwindow_background
1622 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 newTabItem.setVisible(showNewTab);
1624 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001625 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1626 newTabItem.setOnMenuItemClickListener(
1627 new MenuItem.OnMenuItemClickListener() {
1628 @Override
1629 public boolean onMenuItemClick(MenuItem item) {
1630 final HashMap<String, WebView> hrefMap =
1631 new HashMap<String, WebView>();
1632 hrefMap.put("webview", webview);
1633 final Message msg = mHandler.obtainMessage(
1634 FOCUS_NODE_HREF,
1635 R.id.open_newtab_context_menu_id,
1636 0, hrefMap);
1637 webview.requestFocusNodeHref(msg);
1638 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001639 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001640 });
1641 } else {
1642 newTabItem.setOnMenuItemClickListener(
1643 new MenuItem.OnMenuItemClickListener() {
1644 @Override
1645 public boolean onMenuItemClick(MenuItem item) {
1646 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001647 openTab(extra, parent,
1648 !mSettings.openInBackground(),
1649 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001650 return true;
1651 }
1652 });
1653 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001654 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001655 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1656 menu.setHeaderTitle(navigationUrl);
1657 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1658
1659 if (itemUrl != null) {
1660 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1661 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1662 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1663 @Override
1664 public boolean onMenuItemClick(MenuItem item) {
1665 final Intent intent = new Intent(Controller.this
1666 .getContext(),
1667 AddMyNavigationPage.class);
1668 Bundle bundle = new Bundle();
1669 String url = Uri.decode(navigationUrl);
1670 bundle.putBoolean("isAdding", false);
1671 bundle.putString("url", url);
1672 bundle.putString("name", getNameFromUrl(url));
1673 intent.putExtra("websites", bundle);
1674 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1675 return false;
1676 }
1677 });
1678 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1679 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1680 @Override
1681 public boolean onMenuItemClick(MenuItem item) {
1682 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1683 return false;
1684 }
1685 });
1686 }
1687 } else {
1688 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1689 }
1690 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001691 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1692 break;
1693 }
1694 // otherwise fall through to handle image part
1695 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001696 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1697 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001698 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1699 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001700 shareItem.setOnMenuItemClickListener(
1701 new MenuItem.OnMenuItemClickListener() {
1702 @Override
1703 public boolean onMenuItemClick(MenuItem item) {
1704 sharePage(mActivity, null, extra, null,
1705 null);
1706 return true;
1707 }
1708 }
1709 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001710 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001711 menu.findItem(R.id.view_image_context_menu_id)
1712 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1713 @Override
1714 public boolean onMenuItemClick(MenuItem item) {
1715 openTab(extra, mTabControl.getCurrentTab(), true, true);
1716 return false;
1717 }
1718 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001719 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1720 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1721 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001722 menu.findItem(R.id.set_wallpaper_context_menu_id).
1723 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1724 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001725 break;
1726
1727 default:
1728 Log.w(LOGTAG, "We should not get here.");
1729 break;
1730 }
1731 //update the ui
1732 mUi.onContextMenuCreated(menu);
1733 }
1734
kaiyiz6e5b3e02013-08-19 20:02:01 +08001735 public void startAddMyNavigation(String url) {
1736 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1737 Bundle bundle = new Bundle();
1738 bundle.putBoolean("isAdding", true);
1739 bundle.putString("url", url);
1740 bundle.putString("name", getNameFromUrl(url));
1741 intent.putExtra("websites", bundle);
1742 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1743 }
1744
1745 private void showMyNavigationDeleteDialog(final String itemUrl) {
1746 new AlertDialog.Builder(this.getContext())
1747 .setTitle(R.string.my_navigation_delete_label)
1748 .setIcon(android.R.drawable.ic_dialog_alert)
1749 .setMessage(R.string.my_navigation_delete_msg)
1750 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1751 @Override
1752 public void onClick(DialogInterface dialog, int whichButton) {
1753 deleteMyNavigationItem(itemUrl);
1754 }
1755 })
1756 .setNegativeButton(R.string.cancel, null)
1757 .show();
1758 }
1759
1760 private void deleteMyNavigationItem(final String itemUrl) {
1761 ContentResolver cr = this.getContext().getContentResolver();
1762 Cursor cursor = null;
1763
1764 try {
1765 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1766 new String[] {
1767 MyNavigationUtil.ID
1768 }, "url = ?", new String[] {
1769 itemUrl
1770 }, null);
1771 if (null != cursor && cursor.moveToFirst()) {
1772 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1773 cursor.getLong(0));
1774
1775 ContentValues values = new ContentValues();
1776 values.put(MyNavigationUtil.TITLE, "");
1777 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1778 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1779 ByteArrayOutputStream os = new ByteArrayOutputStream();
1780 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1781 R.raw.my_navigation_add);
1782 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1783 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1784 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1785 cr.update(uri, values, null, null);
1786 } else {
1787 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1788 }
1789 } catch (IllegalStateException e) {
1790 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1791 } finally {
1792 if (null != cursor) {
1793 cursor.close();
1794 }
1795 }
1796
1797 if (getCurrentTopWebView() != null) {
1798 getCurrentTopWebView().reload();
1799 }
1800 }
1801
1802 private String getNameFromUrl(String itemUrl) {
1803 ContentResolver cr = this.getContext().getContentResolver();
1804 Cursor cursor = null;
1805 String name = null;
1806
1807 try {
1808 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1809 new String[] {
1810 MyNavigationUtil.TITLE
1811 }, "url = ?", new String[] {
1812 itemUrl
1813 }, null);
1814 if (null != cursor && cursor.moveToFirst()) {
1815 name = cursor.getString(0);
1816 } else {
1817 Log.e(LOGTAG, "this item does not exist!");
1818 }
1819 } catch (IllegalStateException e) {
1820 Log.e(LOGTAG, "getNameFromUrl", e);
1821 } finally {
1822 if (null != cursor) {
1823 cursor.close();
1824 }
1825 }
1826 return name;
1827 }
1828
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001829 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001830 final ContentResolver cr = mActivity.getContentResolver();
1831 new AsyncTask<Void, Void, Void>() {
1832 @Override
1833 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001834 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1835 if(!isMyNavigationUrl)
1836 return null;
1837
kaiyiz6e5b3e02013-08-19 20:02:01 +08001838 ContentResolver cr = mActivity.getContentResolver();
1839 Cursor cursor = null;
1840 try {
1841 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1842 new String[] {
1843 MyNavigationUtil.ID
1844 }, "url = ?", new String[] {
1845 itemUrl
1846 }, null);
1847 if (null != cursor && cursor.moveToFirst()) {
1848 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001849 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001850
1851 ContentValues values = new ContentValues();
1852 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1853 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1854 cursor.getLong(0));
1855 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1856 cr.update(uri, values, null, null);
1857 os.close();
1858 }
1859 } catch (IllegalStateException e) {
1860 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1861 } catch (IOException e) {
1862 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1863 } finally {
1864 if (null != cursor) {
1865 cursor.close();
1866 }
1867 }
1868 return null;
1869 }
1870 }.execute();
1871 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001872 /**
1873 * As the menu can be open when loading state changes
1874 * we must manually update the state of the stop/reload menu
1875 * item
1876 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001877 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001878 if (menu == null) {
1879 return;
1880 }
1881 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001882 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001883 menu.findItem(R.id.stop_menu_id):
1884 menu.findItem(R.id.reload_menu_id);
1885 if (src != null) {
1886 dest.setIcon(src.getIcon());
1887 dest.setTitle(src.getTitle());
1888 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001889 mActivity.invalidateOptionsMenu();
1890 }
1891
1892 public void invalidateOptionsMenu() {
1893 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001894 }
1895
John Reck9c35b9c2012-05-30 10:08:50 -07001896 @Override
1897 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001898 // Software menu key (toolbar key)
1899 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1900 return true;
1901 }
1902
1903 @Override
1904 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001905 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001906 // hold on to the menu reference here; it is used by the page callbacks
1907 // to update the menu based on loading state
1908 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001909 // Note: setVisible will decide whether an item is visible; while
1910 // setEnabled() will decide whether an item is enabled, which also means
1911 // whether the matching shortcut key will function.
1912 switch (mMenuState) {
1913 case EMPTY_MENU:
1914 if (mCurrentMenuState != mMenuState) {
1915 menu.setGroupVisible(R.id.MAIN_MENU, false);
1916 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1917 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1918 }
1919 break;
1920 default:
1921 if (mCurrentMenuState != mMenuState) {
1922 menu.setGroupVisible(R.id.MAIN_MENU, true);
1923 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1924 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1925 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001926 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001927 break;
1928 }
1929 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001930 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001931
1932 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001933 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1934 EditBookmarksRestriction.getInstance()
1935 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001936 }
1937
1938 private void setMenuItemVisibility(Menu menu, int id,
1939 boolean visibility) {
1940 MenuItem item = menu.findItem(id);
1941 if (item != null) {
1942 item.setVisible(visibility);
1943 }
1944 }
1945
Sagar Dhawanef06f562015-09-07 15:23:22 +02001946 private int lookupBookmark( String url) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001947 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001948 int count = 0;
1949 Cursor cursor = null;
1950 try {
1951 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1952 BookmarksLoader.PROJECTION,
Sagar Dhawanef06f562015-09-07 15:23:22 +02001953 "url = ?",
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001954 new String[] {
Sagar Dhawanef06f562015-09-07 15:23:22 +02001955 url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001956 },
1957 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001958
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001959 if (cursor != null)
1960 count = cursor.getCount();
1961
1962 } catch (IllegalStateException e) {
1963 Log.e(LOGTAG, "lookupBookmark ", e);
1964 } finally {
1965 if (null != cursor) {
1966 cursor.close();
1967 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001968 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001969 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001970 }
1971
1972 private void resetMenuItems(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001973 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1974
1975 WebView w = getCurrentTopWebView();
1976 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1977
1978 String title = w.getTitle();
1979 String url = w.getUrl();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001980 mCurrentPageBookmarked = (lookupBookmark(url) > 0);
Pankaj Garg634bf432015-07-13 09:54:21 -07001981 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001982 bookmark_icon.setChecked(true);
1983 } else {
1984 bookmark_icon.setChecked(false);
1985 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001986
1987 // update reader mode checkbox
1988 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1989 readerSwitcher.setVisible(false);
1990 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001991 }
1992
Michael Kolb4bf79712011-07-14 14:18:12 -07001993 @Override
1994 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001995 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001996 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001997 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001998 // Following flag is used to identify schemes for which the LIVE_MENU
1999 // items defined in res/menu/browser.xml should be enabled
2000 boolean isLiveScheme = false;
2001 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002002 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07002003
2004 boolean isDistillable = false;
2005 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002006 resetMenuItems(menu);
2007
Michael Kolb4bf79712011-07-14 14:18:12 -07002008 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002009 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002010 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002011 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002012 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002013 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002014 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07002015
2016 isDistillable = tab.isDistillable();
2017 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07002018 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002019
2020 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2021 forward.setEnabled(canGoForward);
2022
Michael Kolb4bf79712011-07-14 14:18:12 -07002023 // decide whether to show the share link option
2024 PackageManager pm = mActivity.getPackageManager();
2025 Intent send = new Intent(Intent.ACTION_SEND);
2026 send.setType("text/plain");
2027 ResolveInfo ri = pm.resolveActivity(send,
2028 PackageManager.MATCH_DEFAULT_ONLY);
2029 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2030
2031 boolean isNavDump = mSettings.enableNavDump();
2032 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2033 nav.setVisible(isNavDump);
2034 nav.setEnabled(isNavDump);
2035
2036 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002037 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2038 uaSwitcher.setChecked(isDesktopUa);
Sagar Dhawanef06f562015-09-07 15:23:22 +02002039 setMenuItemVisibility(menu, R.id.find_menu_id, isLive);
2040 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002041 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002042 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002043 setMenuItemVisibility(menu, R.id.add_to_homescreen,
Sagar Dhawanef06f562015-09-07 15:23:22 +02002044 isLive && isPageFinished);
Pankaj Garg49b79252014-11-07 17:33:41 -08002045 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002046 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002047 // history and snapshots item are the members of COMBO menu group,
2048 // so if show history item, only make snapshots item invisible.
2049 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002050
Ze G Riande2a675c22015-06-03 11:15:24 -07002051
2052 // update reader mode checkbox
2053 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2054 // The reader mode checkbox is hidden only
2055 // when the current page is neither distillable nor distilled
2056 readerSwitcher.setVisible(isDistillable || isDistilled);
2057 readerSwitcher.setChecked(isDistilled);
2058
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002059 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002060 }
2061
Pankaj Gargf49e0222015-09-01 12:19:13 -07002062 private Bitmap cropAndScaleBitmap(Bitmap bm, int width, int height) {
2063 if (width == 0 || height == 0 || bm == null)
2064 return bm;
2065
2066 Bitmap cropped;
2067
2068 if (bm.getHeight() > bm.getWidth()) {
2069 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
2070 bm.getWidth() * height / width, null, true);
2071 } else {
2072 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getHeight() * width / height,
2073 bm.getHeight(), null, true);
2074 }
2075
2076 Bitmap scaled = Bitmap.createScaledBitmap(cropped, width, height, true);
2077 cropped.recycle();
2078 return scaled;
2079 }
2080
John Reck9c35b9c2012-05-30 10:08:50 -07002081 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002082 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002083 if (null == getCurrentTopWebView()) {
2084 return false;
2085 }
2086 if (mMenuIsDown) {
2087 // The shortcut action consumes the MENU. Even if it is still down,
2088 // it won't trigger the next shortcut action. In the case of the
2089 // shortcut action triggering a new activity, like Bookmarks, we
2090 // won't get onKeyUp for MENU. So it is important to reset it here.
2091 mMenuIsDown = false;
2092 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002093 if (mUi.onOptionsItemSelected(item)) {
2094 // ui callback handled it
2095 return true;
2096 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002097 switch (item.getItemId()) {
2098 // -- Main menu
2099 case R.id.new_tab_menu_id:
2100 openTabToHomePage();
2101 break;
2102
2103 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002104 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002105 break;
2106
2107 case R.id.goto_menu_id:
2108 editUrl();
2109 break;
2110
2111 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002112 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2113 break;
2114
Michael Kolb315d5022011-10-13 12:47:11 -07002115 case R.id.snapshots_menu_id:
2116 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002117 break;
2118
Pankaj Garg49b79252014-11-07 17:33:41 -08002119 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002120 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002121 break;
2122
2123 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002124 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002125 stopLoading();
2126 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002127 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002128 getCurrentTopWebView().reload();
2129 }
2130 break;
2131
Michael Kolb8233fac2010-10-26 16:08:53 -07002132 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002133 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002134 break;
2135
2136 case R.id.close_menu_id:
2137 // Close the subwindow if it exists.
2138 if (mTabControl.getCurrentSubWindow() != null) {
2139 dismissSubWindow(mTabControl.getCurrentTab());
2140 break;
2141 }
2142 closeCurrentTab();
2143 break;
2144
kaiyiza8b6dbb2013-07-29 18:11:22 +08002145 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002146 Object[] params = { new String("persist.debug.browsermonkeytest")};
2147 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002148 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002149 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002150 if (ret != null && ret.equals("enable"))
2151 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002152 if (BrowserConfig.getInstance(getContext())
2153 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2154 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002155 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002156 case R.id.homepage_menu_id:
2157 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002158 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002159 break;
2160
2161 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002162 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002163 break;
2164
2165 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002166 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002167 break;
2168
John Reck2bc80422011-06-30 15:11:49 -07002169 case R.id.save_snapshot_menu_id:
2170 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002171 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002172 createScreenshotAsync(
Pankaj Gargf49e0222015-09-01 12:19:13 -07002173 source,
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002174 new ValueCallback<Bitmap>() {
2175 @Override
2176 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002177 Bitmap bm = cropAndScaleBitmap(bitmap,
2178 getDesiredThumbnailWidth(mActivity),
2179 getDesiredThumbnailHeight(mActivity));
2180 new SaveSnapshotTask(source, bm).execute();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002181 }
2182 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002183 break;
2184
Michael Kolb8233fac2010-10-26 16:08:53 -07002185 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002186 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002187 break;
2188
John Recke1a03a32011-09-14 17:04:16 -07002189 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002190 // passing null to distinguish between
2191 // "go live" button and navigating a web page
2192 // on a snapshot tab
2193 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002194 case R.id.share_page_menu_id:
2195 Tab currentTab = mTabControl.getCurrentTab();
2196 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002197 return false;
2198 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002199 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002200 break;
2201
2202 case R.id.dump_nav_menu_id:
2203 getCurrentTopWebView().debugDump();
2204 break;
2205
Michael Kolb8233fac2010-10-26 16:08:53 -07002206 case R.id.zoom_in_menu_id:
2207 getCurrentTopWebView().zoomIn();
2208 break;
2209
2210 case R.id.zoom_out_menu_id:
2211 getCurrentTopWebView().zoomOut();
2212 break;
2213
2214 case R.id.view_downloads_menu_id:
2215 viewDownloads();
2216 break;
2217
John Reck42229bc2011-08-19 13:26:43 -07002218 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002219 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002220 break;
2221
Ze G Riande2a675c22015-06-03 11:15:24 -07002222 case R.id.reader_mode_menu_id:
2223 toggleReaderMode();
2224 break;
2225
Michael Kolb8233fac2010-10-26 16:08:53 -07002226 case R.id.window_one_menu_id:
2227 case R.id.window_two_menu_id:
2228 case R.id.window_three_menu_id:
2229 case R.id.window_four_menu_id:
2230 case R.id.window_five_menu_id:
2231 case R.id.window_six_menu_id:
2232 case R.id.window_seven_menu_id:
2233 case R.id.window_eight_menu_id:
2234 {
2235 int menuid = item.getItemId();
2236 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2237 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2238 Tab desiredTab = mTabControl.getTab(id);
2239 if (desiredTab != null &&
2240 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002241 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002242 }
2243 break;
2244 }
2245 }
2246 }
2247 break;
2248
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002249 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002250 Bundle bundle = new Bundle();
2251 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2252 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2253 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2254 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2255 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002256 break;
2257
Pankaj Garg49b79252014-11-07 17:33:41 -08002258 case R.id.add_to_homescreen:
2259 final WebView w = getCurrentTopWebView();
2260 final EditText input = new EditText(getContext());
2261 input.setText(w.getTitle());
2262 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002263 .setTitle(getContext().getResources().getString(
2264 R.string.add_to_homescreen))
2265 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002266 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002267 .setPositiveButton(getContext().getResources().getString(
2268 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002269 public void onClick(DialogInterface dialog, int whichButton) {
2270 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2271 getContext(),
2272 w.getUrl(),
2273 input.getText().toString(),
2274 w.getViewportBitmap(),
2275 w.getFavicon()));
2276
2277 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2278 .addCategory(Intent.CATEGORY_HOME));
2279 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002280 .setNegativeButton(getContext().getResources().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07002281 R.string.cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002282 public void onClick(DialogInterface dialog, int whichButton) {
2283 // Do nothing.
2284 }
2285 })
2286 .show();
2287 break;
2288
Michael Kolb8233fac2010-10-26 16:08:53 -07002289 default:
2290 return false;
2291 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002292 return true;
2293 }
2294
John Reck68234a92012-04-19 15:27:12 -07002295 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2296 implements OnCancelListener {
2297
2298 private Tab mTab;
2299 private Dialog mProgressDialog;
2300 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002301 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002302
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002303 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002304 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002305 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002306 }
2307
2308 @Override
2309 protected void onPreExecute() {
2310 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2311 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2312 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002313 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002314 }
2315
2316 @Override
2317 protected Long doInBackground(Void... params) {
2318 if (!mTab.saveViewState(mValues)) {
2319 return null;
2320 }
2321 if (isCancelled()) {
2322 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2323 File file = mActivity.getFileStreamPath(path);
2324 if (!file.delete()) {
2325 file.deleteOnExit();
2326 }
2327 return null;
2328 }
2329 final ContentResolver cr = mActivity.getContentResolver();
2330 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2331 if (result == null) {
2332 return null;
2333 }
2334 long id = ContentUris.parseId(result);
2335 return id;
2336 }
2337
2338 @Override
2339 protected void onPostExecute(Long id) {
2340 if (isCancelled()) {
2341 return;
2342 }
2343 mProgressDialog.dismiss();
2344 if (id == null) {
2345 Toast.makeText(mActivity, R.string.snapshot_failed,
2346 Toast.LENGTH_SHORT).show();
2347 return;
2348 }
2349 Bundle b = new Bundle();
2350 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2351 mUi.showComboView(ComboViews.Snapshots, b);
2352 }
2353
2354 @Override
2355 public void onCancel(DialogInterface dialog) {
2356 cancel(true);
2357 }
2358 }
2359
Michael Kolb80f75082012-04-10 10:50:06 -07002360 @Override
2361 public void toggleUserAgent() {
2362 WebView web = getCurrentWebView();
2363 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002364 }
2365
Ze G Riande2a675c22015-06-03 11:15:24 -07002366 // This function calls the method in the webview to enable/disable
2367 // the reader mode through the DOM distiller
2368 public void toggleReaderMode() {
2369 Tab t = mTabControl.getCurrentTab();
2370 if (t.isDistilled()) {
2371 closeTab(t);
2372 } else if (t.isDistillable()) {
2373 openTab(t.getDistilledUrl(), false, true, false, t);
2374 }
2375 }
2376
Michael Kolb80f75082012-04-10 10:50:06 -07002377 @Override
2378 public void findOnPage() {
2379 getCurrentTopWebView().showFindDialog(null, true);
2380 }
2381
2382 @Override
2383 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002384 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002385 }
2386
2387 @Override
2388 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002389 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002390 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2391 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002392 }
2393 else {
2394 WebView w = getCurrentTopWebView();
2395 if (w == null)
2396 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002397 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002398 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002399 }
Michael Kolb80f75082012-04-10 10:50:06 -07002400 }
2401
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002402 public boolean goLive(String url) {
2403 if (!getCurrentTab().isSnapshot())
2404 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002405 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002406
2407 if (url == null) { // "go live" button was clicked
2408 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002409 closeTab(t);
2410 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002411 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002412 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002413 return true;
John Recke1a03a32011-09-14 17:04:16 -07002414 }
2415
luxiaolb40014b2013-07-19 10:01:43 +08002416 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002417 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002418 new AlertDialog.Builder(activity)
2419 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002420 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002421 .setMessage(R.string.exit_browser_msg)
2422 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2423 public void onClick(DialogInterface dialog, int which) {
2424 activity.moveTaskToBack(true);
2425 dialog.dismiss();
2426 }
2427 })
2428 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2429 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002430 mCrashRecoveryHandler.clearState(true);
2431 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002432 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002433 dialog.dismiss();
2434 }
2435 })
2436 .show();
2437 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002438
Michael Kolb315d5022011-10-13 12:47:11 -07002439 @Override
2440 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002441 }
2442
John Reck9c35b9c2012-05-30 10:08:50 -07002443 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002444 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002445 // Let the History and Bookmark fragments handle menus they created.
2446 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2447 return false;
2448 }
2449
Michael Kolb8233fac2010-10-26 16:08:53 -07002450 int id = item.getItemId();
2451 boolean result = true;
2452 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002453 // -- Browser context menu
2454 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002455 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002456 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002457 case R.id.copy_link_context_menu_id:
2458 final WebView webView = getCurrentTopWebView();
2459 if (null == webView) {
2460 result = false;
2461 break;
2462 }
2463 final HashMap<String, WebView> hrefMap =
2464 new HashMap<String, WebView>();
2465 hrefMap.put("webview", webView);
2466 final Message msg = mHandler.obtainMessage(
2467 FOCUS_NODE_HREF, id, 0, hrefMap);
2468 webView.requestFocusNodeHref(msg);
2469 break;
2470
2471 default:
2472 // For other context menus
2473 result = onOptionsItemSelected(item);
2474 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002475 return result;
2476 }
2477
2478 /**
2479 * support programmatically opening the context menu
2480 */
2481 public void openContextMenu(View view) {
2482 mActivity.openContextMenu(view);
2483 }
2484
2485 /**
2486 * programmatically open the options menu
2487 */
2488 public void openOptionsMenu() {
2489 mActivity.openOptionsMenu();
2490 }
2491
John Reck9c35b9c2012-05-30 10:08:50 -07002492 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002493 public boolean onMenuOpened(int featureId, Menu menu) {
2494 if (mOptionsMenuOpen) {
2495 if (mConfigChanged) {
2496 // We do not need to make any changes to the state of the
2497 // title bar, since the only thing that happened was a
2498 // change in orientation
2499 mConfigChanged = false;
2500 } else {
2501 if (!mExtendedMenuOpen) {
2502 mExtendedMenuOpen = true;
2503 mUi.onExtendedMenuOpened();
2504 } else {
2505 // Switching the menu back to icon view, so show the
2506 // title bar once again.
2507 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002508 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002509 }
2510 }
2511 } else {
2512 // The options menu is closed, so open it, and show the title
2513 mOptionsMenuOpen = true;
2514 mConfigChanged = false;
2515 mExtendedMenuOpen = false;
2516 mUi.onOptionsMenuOpened();
2517 }
2518 return true;
2519 }
2520
John Reck9c35b9c2012-05-30 10:08:50 -07002521 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002522 public void onOptionsMenuClosed(Menu menu) {
2523 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002524 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 }
2526
John Reck9c35b9c2012-05-30 10:08:50 -07002527 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002528 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002529 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002530 }
2531
2532 // Helper method for getting the top window.
2533 @Override
2534 public WebView getCurrentTopWebView() {
2535 return mTabControl.getCurrentTopWebView();
2536 }
2537
2538 @Override
2539 public WebView getCurrentWebView() {
2540 return mTabControl.getCurrentWebView();
2541 }
2542
2543 /*
2544 * This method is called as a result of the user selecting the options
2545 * menu to see the download window. It shows the download window on top of
2546 * the current window.
2547 */
2548 void viewDownloads() {
2549 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2550 mActivity.startActivity(intent);
2551 }
2552
John Reck30b065e2011-07-19 10:58:05 -07002553 int getActionModeHeight() {
2554 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2555 new int[] { android.R.attr.actionBarSize });
2556 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2557 actionBarSizeTypedArray.recycle();
2558 return size;
2559 }
2560
Michael Kolb8233fac2010-10-26 16:08:53 -07002561 // action mode
2562
John Reck9c35b9c2012-05-30 10:08:50 -07002563 @Override
2564 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002565 mUi.onActionModeStarted(mode);
2566 mActionMode = mode;
2567 }
2568
2569 /*
2570 * True if a custom ActionMode (i.e. find or select) is in use.
2571 */
2572 @Override
2573 public boolean isInCustomActionMode() {
2574 return mActionMode != null;
2575 }
2576
2577 /*
2578 * End the current ActionMode.
2579 */
2580 @Override
2581 public void endActionMode() {
2582 if (mActionMode != null) {
2583 mActionMode.finish();
2584 }
2585 }
2586
2587 /*
2588 * Called by find and select when they are finished. Replace title bars
2589 * as necessary.
2590 */
John Reck9c35b9c2012-05-30 10:08:50 -07002591 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002592 public void onActionModeFinished(ActionMode mode) {
2593 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002594 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002595 mActionMode = null;
2596 }
2597
2598 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002599 final Tab tab = getCurrentTab();
2600 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002601 }
2602
2603 // bookmark handling
2604
2605 /**
2606 * add the current page as a bookmark to the given folder id
2607 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002608 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002609 * bookmarked, and if it is, edit that bookmark. If false, and
2610 * the site is already bookmarked, do not attempt to edit the
2611 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 */
2613 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002614 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002615 WebView w = getCurrentTopWebView();
2616 if (w == null) {
2617 return null;
2618 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 Intent i = new Intent(mActivity,
2620 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002621 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2622 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002623 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002624 if (touchIconUrl != null) {
2625 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2626 WebSettings settings = w.getSettings();
2627 if (settings != null) {
2628 i.putExtra(AddBookmarkPage.USER_AGENT,
2629 settings.getUserAgentString());
2630 }
2631 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002632 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002633 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002634 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002635 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2636 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002637 // Put the dialog at the upper right of the screen, covering the
2638 // star on the title bar.
2639 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002640 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002641 }
2642
2643 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002644 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002645 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002646 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002647 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002648 }
2649
Vivek Sekharb54614f2014-05-01 19:03:37 -07002650 @Override
2651 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2652 boolean capture) {
2653 mUploadHandler = new UploadHandler(this);
2654 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2655 }
2656
Michael Kolb8233fac2010-10-26 16:08:53 -07002657 // thumbnails
2658
2659 /**
2660 * Return the desired width for thumbnail screenshots, which are stored in
2661 * the database, and used on the bookmarks screen.
2662 * @param context Context for finding out the density of the screen.
2663 * @return desired width for thumbnail screenshot.
2664 */
2665 static int getDesiredThumbnailWidth(Context context) {
2666 return context.getResources().getDimensionPixelOffset(
2667 R.dimen.bookmarkThumbnailWidth);
2668 }
2669
2670 /**
2671 * Return the desired height for thumbnail screenshots, which are stored in
2672 * the database, and used on the bookmarks screen.
2673 * @param context Context for finding out the density of the screen.
2674 * @return desired height for thumbnail screenshot.
2675 */
2676 static int getDesiredThumbnailHeight(Context context) {
2677 return context.getResources().getDimensionPixelOffset(
2678 R.dimen.bookmarkThumbnailHeight);
2679 }
2680
Pankaj Gargf49e0222015-09-01 12:19:13 -07002681 void createScreenshotAsync(Tab tab, final ValueCallback<Bitmap> cb) {
2682 if (tab == null) {
2683 cb.onReceiveValue(null);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002684 return;
2685 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002686
2687 tab.capture();
2688
2689 synchronized (mThumbnailCbList) {
2690 mThumbnailCbList.add(cb);
2691 }
2692 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002693
Michael Kolb8233fac2010-10-26 16:08:53 -07002694 private class Copy implements OnMenuItemClickListener {
2695 private CharSequence mText;
2696
John Reck9c35b9c2012-05-30 10:08:50 -07002697 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002698 public boolean onMenuItemClick(MenuItem item) {
2699 copy(mText);
2700 return true;
2701 }
2702
2703 public Copy(CharSequence toCopy) {
2704 mText = toCopy;
2705 }
2706 }
2707
Leon Scroggins63c02662010-11-18 15:16:27 -05002708 private static class Download implements OnMenuItemClickListener {
2709 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002710 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002711 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002712 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002713 private static final String FALLBACK_EXTENSION = "dat";
2714 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002715
John Reck9c35b9c2012-05-30 10:08:50 -07002716 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002717 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002718 if (DataUri.isDataUri(mText)) {
2719 saveDataUri();
2720 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002721 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002722 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002723 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002724 return true;
2725 }
2726
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002727 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2728 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002729 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002731 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002732 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002733 }
George Mount387d45d2011-10-07 15:57:53 -07002734
2735 /**
2736 * Treats mText as a data URI and writes its contents to a file
2737 * based on the current time.
2738 */
2739 private void saveDataUri() {
2740 FileOutputStream outputStream = null;
2741 try {
2742 DataUri uri = new DataUri(mText);
2743 File target = getTarget(uri);
2744 outputStream = new FileOutputStream(target);
2745 outputStream.write(uri.getData());
2746 final DownloadManager manager =
2747 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2748 manager.addCompletedDownload(target.getName(),
2749 mActivity.getTitle().toString(), false,
2750 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002751 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002752 } catch (IOException e) {
2753 Log.e(LOGTAG, "Could not save data URL");
2754 } finally {
2755 if (outputStream != null) {
2756 try {
2757 outputStream.close();
2758 } catch (IOException e) {
2759 // ignore close errors
2760 }
2761 }
2762 }
2763 }
2764
2765 /**
2766 * Creates a File based on the current time stamp and uses
2767 * the mime type of the DataUri to get the extension.
2768 */
2769 private File getTarget(DataUri uri) throws IOException {
2770 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002771 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002772 String nameBase = format.format(new Date());
2773 String mimeType = uri.getMimeType();
2774 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2775 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2776 if (extension == null) {
2777 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2778 extension = FALLBACK_EXTENSION;
2779 }
2780 extension = "." + extension; // createTempFile needs the '.'
2781 File targetFile = File.createTempFile(nameBase, extension, dir);
2782 return targetFile;
2783 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002784 }
2785
Cary Clark8974d282010-11-22 10:46:05 -05002786 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002787 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002788
John Reck9c35b9c2012-05-30 10:08:50 -07002789 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002790 public boolean onMenuItemClick(MenuItem item) {
2791 if (mWebView != null) {
2792 return mWebView.selectText();
2793 }
2794 return false;
2795 }
2796
2797 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002798 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002799 }
2800
2801 }
2802
Michael Kolb8233fac2010-10-26 16:08:53 -07002803 /********************** TODO: UI stuff *****************************/
2804
2805 // these methods have been copied, they still need to be cleaned up
2806
2807 /****************** tabs ***************************************************/
2808
2809 // basic tab interactions:
2810
2811 // it is assumed that tabcontrol already knows about the tab
2812 protected void addTab(Tab tab) {
2813 mUi.addTab(tab);
2814 }
2815
2816 protected void removeTab(Tab tab) {
2817 mUi.removeTab(tab);
2818 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002819 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002820 }
2821
Michael Kolbf2055602011-04-09 17:20:03 -07002822 @Override
2823 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002824 // monkey protection against delayed start
2825 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002826
2827 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2828 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002829 mTabControl.setCurrentTab(tab);
2830 // the tab is guaranteed to have a webview after setCurrentTab
2831 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002832
2833
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002834 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002835 //Purge active tabs
2836 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002837 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002838 }
2839
John Reck8bcafc12011-08-29 16:43:02 -07002840 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002841 Tab current = mTabControl.getCurrentTab();
2842 if (current != null
2843 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002844 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002845 }
2846 }
2847
John Reck26b18322011-06-21 13:08:58 -07002848 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002849 //Cancel navscreen request
2850 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002851 // Dismiss the subwindow if applicable.
2852 dismissSubWindow(appTab);
2853 // Since we might kill the WebView, remove it from the
2854 // content view first.
2855 mUi.detachTab(appTab);
2856 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002857 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002858 // TODO: analyze why the remove and add are necessary
2859 mUi.attachTab(appTab);
2860 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002861 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002862 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002863 } else {
2864 // If the tab was the current tab, we have to attach
2865 // it to the view system again.
2866 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002867 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002868 }
2869 }
2870
2871 // Remove the sub window if it exists. Also called by TabControl when the
2872 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002873 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002874 public void dismissSubWindow(Tab tab) {
2875 removeSubWindow(tab);
2876 // dismiss the subwindow. This will destroy the WebView.
2877 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002878 WebView wv = getCurrentTopWebView();
2879 if (wv != null) {
2880 wv.requestFocus();
2881 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002882 }
2883
2884 @Override
2885 public void removeSubWindow(Tab t) {
2886 if (t.getSubWebView() != null) {
2887 mUi.removeSubWindow(t.getSubViewContainer());
2888 }
2889 }
2890
2891 @Override
2892 public void attachSubWindow(Tab tab) {
2893 if (tab.getSubWebView() != null) {
2894 mUi.attachSubWindow(tab.getSubViewContainer());
2895 getCurrentTopWebView().requestFocus();
2896 }
2897 }
2898
Mathew Inwood29721c22011-06-29 17:55:24 +01002899 private Tab showPreloadedTab(final UrlData urlData) {
2900 if (!urlData.isPreloaded()) {
2901 return null;
2902 }
2903 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2904 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2905 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002906 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002907 // Could not submit query. Fallback to regular tab creation
2908 tabControl.destroy();
2909 return null;
2910 }
2911 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002912 // check tab count and make room for new tab
2913 if (!mTabControl.canCreateNewTab()) {
2914 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2915 if (leastUsed != null) {
2916 closeTab(leastUsed);
2917 }
2918 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002919 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002920 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002921 mTabControl.addPreloadedTab(t);
2922 addTab(t);
2923 setActiveTab(t);
2924 return t;
2925 }
2926
Michael Kolb7bcafde2011-05-09 13:55:59 -07002927 // open a non inconito tab with the given url data
2928 // and set as active tab
2929 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002930 Tab tab = showPreloadedTab(urlData);
2931 if (tab == null) {
2932 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002933 if ((tab != null) && !urlData.isEmpty()) {
2934 loadUrlDataIn(tab, urlData);
2935 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002936 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002937 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002938 }
2939
Michael Kolb843510f2010-12-09 10:51:49 -08002940 @Override
2941 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002942 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002943 }
2944
Michael Kolb8233fac2010-10-26 16:08:53 -07002945 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002946 public Tab openIncognitoTab() {
2947 return openTab(INCOGNITO_URI, true, true, false);
2948 }
2949
2950 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002951 public Tab openTab(String url, boolean incognito, boolean setActive,
2952 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002953 return openTab(url, incognito, setActive, useCurrent, null);
2954 }
2955
2956 @Override
2957 public Tab openTab(String url, Tab parent, boolean setActive,
2958 boolean useCurrent) {
2959 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2960 setActive, useCurrent, parent);
2961 }
2962
2963 public Tab openTab(String url, boolean incognito, boolean setActive,
2964 boolean useCurrent, Tab parent) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002965 boolean change_tabs = false;
2966 if (setActive) {
2967 Tab currentTab = mTabControl.getCurrentTab();
2968 if (currentTab != null) {
2969 change_tabs = setActive;
2970 setActive = false;
Sagar Dhawan9a1d8e72015-09-11 17:14:38 -07002971 if (mUi instanceof PhoneUi)
2972 currentTab.capture();
Pankaj Gargf49e0222015-09-01 12:19:13 -07002973 }
2974 }
2975
Michael Kolb7bcafde2011-05-09 13:55:59 -07002976 Tab tab = createNewTab(incognito, setActive, useCurrent);
2977 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002978 if (parent instanceof SnapshotTab) {
2979 addTab(tab);
2980 if (setActive)
2981 setActiveTab(tab);
2982 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002983 parent.addChildTab(tab);
2984 }
Michael Kolb519d2282011-05-09 17:03:19 -07002985 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002986 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002987 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002988 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002989
2990 if (change_tabs) {
Sagar Dhawan20b1deb2015-10-12 11:13:18 -07002991 setActiveTab(tab);
Pankaj Gargf49e0222015-09-01 12:19:13 -07002992 synchronized (mThumbnailCbList) {
2993 startCaptureTimer();
2994 mLatestCreatedTab = tab;
2995 mThumbnailCbList.add(new ValueCallback<Bitmap>() {
2996 @Override
2997 public void onReceiveValue(Bitmap bitmap) {
2998 synchronized (mThumbnailCbList) {
2999 if (mLatestCreatedTab != null) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07003000 mLatestCreatedTab = null;
3001 }
3002 stopCaptureTimer();
3003 }
3004 }
3005 });
3006 }
3007 }
3008
Michael Kolb7bcafde2011-05-09 13:55:59 -07003009 return tab;
3010 }
3011
Pankaj Gargf49e0222015-09-01 12:19:13 -07003012 private void startCaptureTimer() {
3013 mCaptureTimer = new CountDownTimer(mCaptureMaxWaitMS, mCaptureMaxWaitMS) {
3014 @Override
3015 public void onTick(long millisUntilFinished) {
3016 // Do nothing
3017 }
3018
3019 @Override
3020 public void onFinish() {
3021 synchronized (mThumbnailCbList) {
3022 Log.e(LOGTAG, "Screen capture timed out while opening new tab");
3023 if (mLatestCreatedTab != null) {
3024 setActiveTab(mLatestCreatedTab);
3025 mLatestCreatedTab = null;
3026 }
3027 }
3028 }
3029 }.start();
3030 }
3031
3032 private void stopCaptureTimer() {
3033 if (mCaptureTimer != null) {
3034 mCaptureTimer.cancel();
3035 mCaptureTimer = null;
3036 }
3037 }
3038
3039
Michael Kolb7bcafde2011-05-09 13:55:59 -07003040 // this method will attempt to create a new tab
3041 // incognito: private browsing tab
3042 // setActive: ste tab as current tab
3043 // useCurrent: if no new tab can be created, return current tab
3044 private Tab createNewTab(boolean incognito, boolean setActive,
3045 boolean useCurrent) {
3046 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07003047 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
3048 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003049 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003050 if (mTabControl.canCreateNewTab()) {
3051 tab = mTabControl.createNewTab(incognito, !setActive);
3052 addTab(tab);
3053 if (setActive) {
3054 setActiveTab(tab);
3055 } else {
3056 tab.pause();
3057 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003058 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003059 if (useCurrent) {
3060 tab = mTabControl.getCurrentTab();
3061 reuseTab(tab, null);
3062 } else {
3063 mUi.showMaxTabsWarning();
3064 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003065 }
3066 }
3067 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003068 }
3069
John Reck2bc80422011-06-30 15:11:49 -07003070 @Override
3071 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3072 SnapshotTab tab = null;
3073 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07003074 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07003075 addTab(tab);
3076 if (setActive) {
3077 setActiveTab(tab);
3078 }
3079 } else {
3080 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003081 }
3082 return tab;
3083 }
3084
Michael Kolb8233fac2010-10-26 16:08:53 -07003085 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003086 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003087 * @return boolean True if we successfully switched to a different tab. If
3088 * the indexth tab is null, or if that tab is the same as
3089 * the current one, return false.
3090 */
3091 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003092 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003093 Tab currentTab = mTabControl.getCurrentTab();
3094 if (tab == null || tab == currentTab) {
3095 return false;
3096 }
3097 setActiveTab(tab);
3098 return true;
3099 }
3100
3101 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003102 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003103 closeCurrentTab(false);
3104 }
3105
3106 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003107 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003108 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003109 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003110 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003111 return;
3112 }
Michael Kolbc831b632011-05-11 09:30:34 -07003113 final Tab current = mTabControl.getCurrentTab();
3114 final int pos = mTabControl.getCurrentPosition();
3115 Tab newTab = current.getParent();
3116 if (newTab == null) {
3117 newTab = mTabControl.getTab(pos + 1);
3118 if (newTab == null) {
3119 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003120 }
3121 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003122 if (andQuit) {
3123 mTabControl.setCurrentTab(newTab);
3124 closeTab(current);
3125 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003126 // Close window
3127 closeTab(current);
3128 }
3129 }
3130
3131 /**
3132 * Close the tab, remove its associated title bar, and adjust mTabControl's
3133 * current tab to a valid value.
3134 */
3135 @Override
3136 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003137 if (tab == mTabControl.getCurrentTab()) {
3138 closeCurrentTab();
3139 } else {
3140 removeTab(tab);
3141 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003142 }
3143
Afzal Najamd4e33312012-04-26 01:54:01 -04003144 /**
3145 * Close all tabs except the current one
3146 */
3147 @Override
3148 public void closeOtherTabs() {
3149 int inactiveTabs = mTabControl.getTabCount() - 1;
3150 for (int i = inactiveTabs; i >= 0; i--) {
3151 Tab tab = mTabControl.getTab(i);
3152 if (tab != mTabControl.getCurrentTab()) {
3153 removeTab(tab);
3154 }
3155 }
3156 }
3157
Michael Kolb8233fac2010-10-26 16:08:53 -07003158 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003159 protected void loadUrlFromContext(String url) {
3160 Tab tab = getCurrentTab();
3161 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003162 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003163 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003164 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003165 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003166 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003167 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003168 }
3169 }
3170 }
3171
3172 /**
3173 * Load the URL into the given WebView and update the title bar
3174 * to reflect the new load. Call this instead of WebView.loadUrl
3175 * directly.
3176 * @param view The WebView used to load url.
3177 * @param url The URL to load.
3178 */
John Reck71e51422011-07-01 16:49:28 -07003179 @Override
3180 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003181 loadUrl(tab, url, null);
3182 }
3183
3184 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3185 if (tab != null) {
3186 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003187 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003188 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003189 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003190 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003191 }
3192
3193 /**
3194 * Load UrlData into a Tab and update the title bar to reflect the new
3195 * load. Call this instead of UrlData.loadIn directly.
3196 * @param t The Tab used to load.
3197 * @param data The UrlData being loaded.
3198 */
3199 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003200 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003201 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003202 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003203 } else {
John Reck38b39652012-06-05 09:22:59 -07003204 if (t != null && data.mDisableUrlOverride) {
3205 t.disableUrlOverridingForLoad();
3206 }
John Reck26b18322011-06-21 13:08:58 -07003207 loadUrl(t, data.mUrl, data.mHeaders);
3208 }
3209 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003210 }
3211
John Reck30c714c2010-12-16 17:30:34 -08003212 @Override
3213 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003214 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003215 //In case of tab can go back (aka tab has navigation entry) do nothing
3216 //else just load homepage in current tab.
3217 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003218 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003219 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003220 }
3221
3222 void goBackOnePageOrQuit() {
3223 Tab current = mTabControl.getCurrentTab();
3224 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003225 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3226 showExitDialog(mActivity);
3227 } else {
3228 /*
3229 * Instead of finishing the activity, simply push this to the back
3230 * of the stack and let ActivityManager to choose the foreground
3231 * activity. As BrowserActivity is singleTask, it will be always the
3232 * root of the task. So we can use either true or false for
3233 * moveTaskToBack().
3234 */
3235 mActivity.moveTaskToBack(true);
3236 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003237 return;
3238 }
John Reckef654f12011-07-12 16:42:08 -07003239 if (current.canGoBack()) {
3240 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003241 } else {
3242 // Check to see if we are closing a window that was created by
3243 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003244 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003245 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003246 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003247 // Now we close the other tab
3248 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003249 } else if (BrowserConfig.getInstance(getContext())
3250 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3251 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003252 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003253 /*
3254 * Instead of finishing the activity, simply push this to the back
3255 * of the stack and let ActivityManager to choose the foreground
3256 * activity. As BrowserActivity is singleTask, it will be always the
3257 * root of the task. So we can use either true or false for
3258 * moveTaskToBack().
3259 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003260 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003261 }
3262 }
3263 }
3264
3265 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003266 * helper method for key handler
3267 * returns the current tab if it can't advance
3268 */
Michael Kolbc831b632011-05-11 09:30:34 -07003269 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003270 int pos = mTabControl.getCurrentPosition() + 1;
3271 if (pos >= mTabControl.getTabCount()) {
3272 pos = 0;
3273 }
3274 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003275 }
3276
3277 /**
3278 * helper method for key handler
3279 * returns the current tab if it can't advance
3280 */
Michael Kolbc831b632011-05-11 09:30:34 -07003281 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003282 int pos = mTabControl.getCurrentPosition() - 1;
3283 if ( pos < 0) {
3284 pos = mTabControl.getTabCount() - 1;
3285 }
3286 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003287 }
3288
John Reckbcef87f2012-02-03 14:58:34 -08003289 boolean isMenuOrCtrlKey(int keyCode) {
3290 return (KeyEvent.KEYCODE_MENU == keyCode)
3291 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3292 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3293 }
3294
Michael Kolb0035fad2011-03-14 13:25:28 -07003295 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003296 * handle key events in browser
3297 *
3298 * @param keyCode
3299 * @param event
3300 * @return true if handled, false to pass to super
3301 */
John Reck9c35b9c2012-05-30 10:08:50 -07003302 @Override
3303 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003304 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3305 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003306 if (!mUi.isComboViewShowing()) {
3307 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3308 true, false);
3309 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003310 return true;
3311 }
3312
Cary Clark160bbb92011-01-10 11:17:07 -05003313 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003314 // Even if MENU is already held down, we need to call to super to open
3315 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003316 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003317 mMenuIsDown = true;
3318 return false;
3319 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003320
Cary Clark8ff8c662010-12-29 15:03:05 -05003321 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003322 Tab tab = getCurrentTab();
3323 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003324
Cary Clark160bbb92011-01-10 11:17:07 -05003325 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3326 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003327
Michael Kolb8233fac2010-10-26 16:08:53 -07003328 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003329 case KeyEvent.KEYCODE_TAB:
3330 if (event.isCtrlPressed()) {
3331 if (event.isShiftPressed()) {
3332 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003333 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003334 } else {
3335 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003336 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003337 }
3338 return true;
3339 }
3340 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003341 case KeyEvent.KEYCODE_SPACE:
3342 // WebView/WebTextView handle the keys in the KeyDown. As
3343 // the Activity's shortcut keys are only handled when WebView
3344 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003345 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003346 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003347 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003348 pageDown();
3349 }
3350 return true;
3351 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003352 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003353 event.startTracking();
3354 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003355 case KeyEvent.KEYCODE_FORWARD:
3356 if (!noModifiers) break;
3357 tab.goForward();
3358 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003359 case KeyEvent.KEYCODE_DPAD_LEFT:
3360 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003361 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003362 return true;
3363 }
3364 break;
3365 case KeyEvent.KEYCODE_DPAD_RIGHT:
3366 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003367 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003368 return true;
3369 }
3370 break;
3371 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003372 if (ctrl) {
3373 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003374 return true;
3375 }
3376 break;
Michael Kolba4183062011-01-16 10:43:21 -08003377// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003378 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003379 if (ctrl ) {
3380 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003381 return true;
3382 }
3383 break;
Michael Kolba4183062011-01-16 10:43:21 -08003384// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003385// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003386// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003387// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003388// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003389// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003390// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003391// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003392// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003393// case KeyEvent.KEYCODE_M: // unused
3394// case KeyEvent.KEYCODE_N: // in Chrome: new window
3395// case KeyEvent.KEYCODE_O: // in Chrome: open file
3396// case KeyEvent.KEYCODE_P: // in Chrome: print page
3397// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003398// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003399// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3400 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003401 // we can't use the ctrl/shift flags, they check for
3402 // exclusive use of a modifier
3403 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003404 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003405 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003406 } else {
3407 openTabToHomePage();
3408 }
3409 return true;
3410 }
3411 break;
3412// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3413// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003414// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003415// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3416// case KeyEvent.KEYCODE_Y: // unused
3417// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003418 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003419 // it is a regular key and webview is not null
3420 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003421 }
3422
John Reck9c35b9c2012-05-30 10:08:50 -07003423 @Override
3424 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003425 switch(keyCode) {
3426 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003427 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003428 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003429 return true;
3430 }
3431 break;
3432 }
3433 return false;
3434 }
3435
John Reck9c35b9c2012-05-30 10:08:50 -07003436 @Override
3437 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003438 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003439 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003440 if (KeyEvent.KEYCODE_MENU == keyCode
3441 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003442 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003443 }
3444 }
Cary Clark160bbb92011-01-10 11:17:07 -05003445 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003446 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003447 case KeyEvent.KEYCODE_BACK:
3448 if (event.isTracking() && !event.isCanceled()) {
3449 onBackKey();
3450 return true;
3451 }
3452 break;
3453 }
3454 return false;
3455 }
3456
3457 public boolean isMenuDown() {
3458 return mMenuIsDown;
3459 }
3460
John Reck9c35b9c2012-05-30 10:08:50 -07003461 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003462 public void setupAutoFill(Message message) {
3463 // Open the settings activity at the AutoFill profile fragment so that
3464 // the user can create a new profile. When they return, we will dispatch
3465 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003466 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003467 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3468 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003469 }
John Reckb3417f02011-01-14 11:01:05 -08003470
John Reck9c35b9c2012-05-30 10:08:50 -07003471 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003472 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003473 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003474 return true;
3475 }
3476
John Reck1cf4b792011-07-26 10:22:22 -07003477 @Override
3478 public boolean shouldCaptureThumbnails() {
3479 return mUi.shouldCaptureThumbnails();
3480 }
3481
Michael Kolbc3af0672011-08-09 10:24:41 -07003482 @Override
Pankaj Gargf49e0222015-09-01 12:19:13 -07003483 public void onThumbnailCapture(Bitmap bm) {
3484 synchronized (mThumbnailCbList) {
3485 int num_entries = mThumbnailCbList.size();
3486 while (num_entries > 0){
3487 num_entries--;
3488 ValueCallback<Bitmap> cb = mThumbnailCbList.get(num_entries);
3489 cb.onReceiveValue(bm);
3490 }
3491 mThumbnailCbList.clear();
3492 }
3493 }
3494
3495 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003496 public boolean supportsVoice() {
3497 PackageManager pm = mActivity.getPackageManager();
3498 List activities = pm.queryIntentActivities(new Intent(
3499 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3500 return activities.size() != 0;
3501 }
3502
3503 @Override
3504 public void startVoiceRecognizer() {
3505 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003506 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003507 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3508 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3509 mActivity.startActivityForResult(voice, VOICE_RESULT);
3510 }
3511
Pankaj Garg7b279f62014-08-12 14:47:18 -07003512 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003513 if (mLevel == level)
3514 return;
3515 mLevel = level;
3516 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003517 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3518 lp.dimAmount = level;
3519 mActivity.getWindow().setAttributes(lp);
3520 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3521 } else {
3522 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3523 }
3524 }
3525
Michael Kolb0b129122012-06-04 16:31:58 -07003526 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003527 public void setBlockEvents(boolean block) {
3528 mBlockEvents = block;
3529 }
3530
John Reck9c35b9c2012-05-30 10:08:50 -07003531 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003532 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003533 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003534 }
3535
John Reck9c35b9c2012-05-30 10:08:50 -07003536 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003537 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003538 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003539 }
3540
John Reck9c35b9c2012-05-30 10:08:50 -07003541 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003542 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003543 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003544 }
3545
John Reck9c35b9c2012-05-30 10:08:50 -07003546 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003547 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003548 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003549 }
3550
John Reck9c35b9c2012-05-30 10:08:50 -07003551 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003552 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003553 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003554 }
3555
Pankaj Garg49b79252014-11-07 17:33:41 -08003556 @Override
3557 public boolean shouldShowAppMenu() {
3558 return true;
3559 }
3560
3561 @Override
3562 public int getMenuThemeResourceId() {
3563 return R.style.OverflowMenuTheme;
3564 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003565}