blob: 39b422005652c88d8aa148091caba3d52f4e18fa [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
Dave Tharp851e8d52015-04-26 14:58:18 -07004 * Copyright (c) 2015 The Linux Foundation, All rights reserved.
Site Maoabb7bd32015-03-20 15:34:02 -07005 *
Michael Kolb8233fac2010-10-26 16:08:53 -07006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Bijan Amirzada41242f22014-03-21 12:12:18 -070019package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070020
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080022
luxiaolb40014b2013-07-19 10:01:43 +080023import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070025import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070026import android.app.ProgressDialog;
Vivek Sekharce2a4832014-03-26 13:26:53 -070027import android.content.Context;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070030import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070031import android.content.ContentValues;
John Reck68234a92012-04-19 15:27:12 -070032import android.content.DialogInterface;
33import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.content.Intent;
Site Maoabb7bd32015-03-20 15:34:02 -070035import android.content.IntentFilter;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
38import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070039import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050040import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.database.Cursor;
42import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080044import android.graphics.BitmapFactory;
Tarun Nainanif5563b62015-01-21 18:52:59 -080045import android.media.AudioManager;
kaiyiza016da12013-08-26 17:50:22 +080046import android.net.ConnectivityManager;
47import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.net.Uri;
kaiyiza016da12013-08-26 17:50:22 +080049import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080050import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.os.AsyncTask;
Tarun Nainanif5563b62015-01-21 18:52:59 -080052import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.os.Bundle;
Pankaj Gargf49e0222015-09-01 12:19:13 -070054import android.os.CountDownTimer;
George Mount387d45d2011-10-07 15:57:53 -070055import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.os.Handler;
57import android.os.Message;
58import android.os.PowerManager;
59import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.provider.ContactsContract;
61import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080062import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070063import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070064import android.text.TextUtils;
65import android.util.Log;
66import android.view.ActionMode;
67import android.view.ContextMenu;
68import android.view.ContextMenu.ContextMenuInfo;
69import android.view.Gravity;
70import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070071import android.view.Menu;
72import android.view.MenuInflater;
73import android.view.MenuItem;
74import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070075import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070076import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070077import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070078import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080081import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import org.codeaurora.swe.CookieManager;
85import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080086import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.HttpAuthHandler;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080088import org.codeaurora.swe.WebSettings;
89import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070090import org.codeaurora.swe.WebBackForwardList;
91import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080092
Bijan Amirzada41242f22014-03-21 12:12:18 -070093import com.android.browser.IntentHandler.UrlData;
94import com.android.browser.UI.ComboViews;
Dave Tharp13f3f7c2015-07-16 14:58:51 -070095import com.android.browser.mdm.DownloadDirRestriction;
Dave Tharp9a082b12015-06-19 08:46:57 -070096import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -070097import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -070098import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -070099import com.android.browser.mynavigation.AddMyNavigationPage;
100import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700101import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700102import com.android.browser.platformsupport.BrowserContract;
Pankaj Garg18902562014-12-05 16:18:51 -0800103import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700104import com.android.browser.provider.BrowserProvider2.Thumbnails;
105import com.android.browser.provider.SnapshotProvider.Snapshots;
106import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800107import com.android.browser.appmenu.AppMenuHandler;
108import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700109
Michael Kolb8233fac2010-10-26 16:08:53 -0700110import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700111import java.io.File;
112import java.io.FileOutputStream;
113import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700115import java.text.DateFormat;
116import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700117import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700118import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700119import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800120import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200121import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700122import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123
124/**
125 * Controller for browser
126 */
127public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800128 implements WebViewController, UiController, ActivityController,
129 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
131 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800132 private static final String SEND_APP_ID_EXTRA =
133 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700134 public static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800135 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
136 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Pankaj Garg75273bb2015-08-20 11:43:49 -0700137 public static final String EXTRA_UPDATED_URLS = "updated_urls";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800138
Vivek Sekharb54614f2014-05-01 19:03:37 -0700139 // Remind switch to data connection if wifi is unavailable
140 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800141
Michael Kolb8233fac2010-10-26 16:08:53 -0700142 // public message ids
143 public final static int LOAD_URL = 1001;
144 public final static int STOP_LOAD = 1002;
145
146 // Message Ids
147 private static final int FOCUS_NODE_HREF = 102;
148 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800149 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800152 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700153
154 private static final int EMPTY_MENU = -1;
155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700157 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 final static int PREFERENCES_PAGE = 3;
159 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700161 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800162 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000163
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
165
166 // As the ids are dynamically created, we can't guarantee that they will
167 // be in sequence, so this static array maps ids to a window number.
168 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
169 { R.id.window_one_menu_id, R.id.window_two_menu_id,
170 R.id.window_three_menu_id, R.id.window_four_menu_id,
171 R.id.window_five_menu_id, R.id.window_six_menu_id,
172 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
173
174 // "source" parameter for Google search through search key
175 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
176 // "source" parameter for Google search through simplily type
177 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
178
George Mount387d45d2011-10-07 15:57:53 -0700179 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700180 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
181
John Reckd7dd9b22011-08-30 09:18:29 -0700182 // A bitmap that is re-used in createScreenshot as scratch space
183 private static Bitmap sThumbnailBitmap;
184
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 private Activity mActivity;
186 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700187 private HomepageHandler mHomepageHandler;
188 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private BrowserSettings mSettings;
190 private WebViewFactory mFactory;
191
192 private WakeLock mWakeLock;
193
194 private UrlHandler mUrlHandler;
195 private UploadHandler mUploadHandler;
196 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 private NetworkStateHandler mNetworkHandler;
198
Ben Murdoch8029a772010-11-16 11:58:21 +0000199 private Message mAutoFillSetupMessage;
200
kaiyiza016da12013-08-26 17:50:22 +0800201 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700202
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 // FIXME, temp address onPrepareMenu performance problem.
204 // When we move everything out of view, we should rewrite this.
205 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700206 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 private int mOldMenuState = EMPTY_MENU;
208 private Menu mCachedMenu;
209
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 private boolean mMenuIsDown;
211
Pankaj Garg49b79252014-11-07 17:33:41 -0800212 private boolean mWasInPageLoad = false;
213 private AppMenuHandler mAppMenuHandler;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 // For select and find, we keep track of the ActionMode so that
216 // finish() can be called as desired.
217 private ActionMode mActionMode;
218
219 /**
220 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
221 * of whether the configuration has changed. The first onMenuOpened call
222 * after a configuration change is simply a reopening of the same menu
223 * (i.e. mIconView did not change).
224 */
225 private boolean mConfigChanged;
226
227 /**
228 * Keeps track of whether the options menu is open. This is important in
229 * determining whether to show or hide the title bar overlay
230 */
231 private boolean mOptionsMenuOpen;
232
233 /**
234 * Whether or not the options menu is in its bigger, popup menu form. When
235 * true, we want the title bar overlay to be gone. When false, we do not.
236 * Only meaningful if mOptionsMenuOpen is true.
237 */
238 private boolean mExtendedMenuOpen;
239
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 private boolean mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700241 private boolean mActivityStopped = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 private boolean mLoadStopped;
243
244 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500245 // Checks to see when the bookmarks database has changed, and updates the
246 // Tabs' notion of whether they represent bookmarked sites.
247 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700248 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700249
Michael Kolbc3af0672011-08-09 10:24:41 -0700250 private boolean mBlockEvents;
251
Michael Kolb0b129122012-06-04 16:31:58 -0700252 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800253 private boolean mUpdateMyNavThumbnail;
254 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800255 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800256 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700257 private PowerConnectionReceiver mLowPowerReceiver;
258 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700259
Pankaj Garg634bf432015-07-13 09:54:21 -0700260 private boolean mCurrentPageBookmarked;
261
Pankaj Gargf49e0222015-09-01 12:19:13 -0700262 private Tab mLatestCreatedTab;
263
264 private List<ValueCallback> mThumbnailCbList;
265
266 private CountDownTimer mCaptureTimer;
267 private static final int mCaptureMaxWaitMS = 200;
268
George Mount3636d0a2011-11-21 09:08:21 -0800269 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 mActivity = browser;
271 mSettings = BrowserSettings.getInstance();
272 mTabControl = new TabControl(this);
273 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700274 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800275 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700276 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700277
278 mUrlHandler = new UrlHandler(this);
279 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700280
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500282 mBookmarksObserver = new ContentObserver(mHandler) {
283 @Override
284 public void onChange(boolean selfChange) {
285 int size = mTabControl.getTabCount();
286 for (int i = 0; i < size; i++) {
287 mTabControl.getTab(i).updateBookmarkedStatus();
288 }
289 }
290
291 };
292 browser.getContentResolver().registerContentObserver(
293 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700294
295 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700296 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800297 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Pankaj Gargf49e0222015-09-01 12:19:13 -0700298 mThumbnailCbList = new ArrayList<ValueCallback>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700299 }
300
John Reck9c35b9c2012-05-30 10:08:50 -0700301 @Override
302 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700303 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800304 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800305 // mCrashRecoverHandler has any previously saved state.
306 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700307 }
308
George Mount3636d0a2011-11-21 09:08:21 -0800309 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800310 // we dont want to ever recover incognito tabs
311 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700312
Patrick Scott7d50a932011-02-04 09:27:26 -0500313 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700314 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500315 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000316
Michael Kolbc831b632011-05-11 09:30:34 -0700317 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500318 // Not able to restore so we go ahead and clear session cookies. We
319 // must do this before trying to login the user as we don't want to
320 // clear any session cookies set during login.
321 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700322 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800323 if (intent == null) {
324 // This won't happen under common scenarios. The icicle is
325 // not null, but there aren't any tabs to restore.
326 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700327 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800328 final Bundle extra = intent.getExtras();
329 // Create an initial tab.
330 // If the intent is ACTION_VIEW and data is not null, the Browser is
331 // invoked to view the content by another application. In this case,
332 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800333 UrlData urlData = null;
334 if (intent.getData() != null
335 && Intent.ACTION_VIEW.equals(intent.getAction())
336 && intent.getData().toString().startsWith("content://")) {
337 urlData = new UrlData(intent.getData().toString());
338 } else {
339 urlData = IntentHandler.getUrlDataFromIntent(intent);
340 }
George Mount3636d0a2011-11-21 09:08:21 -0800341 Tab t = null;
342 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700343 String landingPage = mActivity.getResources().getString(
344 R.string.def_landing_page);
345 if (!landingPage.isEmpty()) {
346 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800347 } else {
348 t = openTabToHomePage();
349 }
George Mount3636d0a2011-11-21 09:08:21 -0800350 } else {
351 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700352 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800353 }
354 if (t != null) {
355 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
356 }
357 WebView webView = t.getWebView();
358 if (extra != null) {
359 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
360 if (scale > 0 && scale <= 1000) {
361 webView.setInitialScale(scale);
362 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 }
364 }
John Reckd8c74522011-06-14 08:45:00 -0700365 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700367 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500368 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700369 List<Tab> tabs = mTabControl.getTabs();
370 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700371
John Reck1cf4b792011-07-26 10:22:22 -0700372 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700373 //handle restored pages that may require a JS interface
374 if (t.getWebView() != null) {
375 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
376 if (backForwardList != null) {
377 for (int i = 0; i < backForwardList.getSize(); i++) {
378 WebHistoryItem item = backForwardList.getItemAtIndex(i);
379 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
380 }
381 }
382 }
John Reck1cf4b792011-07-26 10:22:22 -0700383 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700384 if (t != mTabControl.getCurrentTab()) {
385 t.pause();
386 }
John Reck1cf4b792011-07-26 10:22:22 -0700387 }
388 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700389 if (tabs.size() == 0) {
390 openTabToHomePage();
391 }
John Reck1cf4b792011-07-26 10:22:22 -0700392 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 // TabControl.restoreState() will create a new tab even if
394 // restoring the state fails.
395 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800396 // Intent is non-null when framework thinks the browser should be
397 // launching with a new intent (icicle is null).
398 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700399 mIntentHandler.onNewIntent(intent);
400 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700403 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800404 if (jsFlags.trim().length() != 0) {
405 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700406 }
George Mount3636d0a2011-11-21 09:08:21 -0800407 if (intent != null
408 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700409 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800410 }
Site Maoabb7bd32015-03-20 15:34:02 -0700411 mLowPowerReceiver = new PowerConnectionReceiver();
412 mPowerChangeReceiver = new PowerConnectionReceiver();
413
414 //always track the android framework's power save mode
415 IntentFilter filter = new IntentFilter();
416 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
417 // Power save mode only exists in Lollipop and above
418 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
419 }
420 filter.addAction(Intent.ACTION_BATTERY_OKAY);
421 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700422 }
423
John Reck1cf4b792011-07-26 10:22:22 -0700424 private static class PruneThumbnails implements Runnable {
425 private Context mContext;
426 private List<Long> mIds;
427
428 PruneThumbnails(Context context, List<Long> preserveIds) {
429 mContext = context.getApplicationContext();
430 mIds = preserveIds;
431 }
432
433 @Override
434 public void run() {
435 ContentResolver cr = mContext.getContentResolver();
436 if (mIds == null || mIds.size() == 0) {
437 cr.delete(Thumbnails.CONTENT_URI, null, null);
438 } else {
439 int length = mIds.size();
440 StringBuilder where = new StringBuilder();
441 where.append(Thumbnails._ID);
442 where.append(" not in (");
443 for (int i = 0; i < length; i++) {
444 where.append(mIds.get(i));
445 if (i < (length - 1)) {
446 where.append(",");
447 }
448 }
449 where.append(")");
450 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
451 }
452 }
453
454 }
455
Michael Kolb1514bb72010-11-22 09:11:48 -0800456 @Override
457 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700458 return mFactory;
459 }
460
461 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800462 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700463 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700464 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800465 }
466
467 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800468 public void createSubWindow(Tab tab) {
469 endActionMode();
470 WebView mainView = tab.getWebView();
471 WebView subView = mFactory.createWebView((mainView == null)
472 ? false
473 : mainView.isPrivateBrowsingEnabled());
474 mUi.createSubWindow(tab, subView);
475 }
476
477 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700478 public Context getContext() {
479 return mActivity;
480 }
481
482 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700483 public Activity getActivity() {
484 return mActivity;
485 }
486
487 void setUi(UI ui) {
488 mUi = ui;
489 }
490
Michael Kolbaf63dba2012-05-16 12:58:05 -0700491 @Override
492 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 return mSettings;
494 }
495
496 IntentHandler getIntentHandler() {
497 return mIntentHandler;
498 }
499
500 @Override
501 public UI getUi() {
502 return mUi;
503 }
504
505 int getMaxTabs() {
506 return mActivity.getResources().getInteger(R.integer.max_tabs);
507 }
508
509 @Override
510 public TabControl getTabControl() {
511 return mTabControl;
512 }
513
Michael Kolb1bf23132010-11-19 12:55:12 -0800514 @Override
515 public List<Tab> getTabs() {
516 return mTabControl.getTabs();
517 }
518
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 private void startHandler() {
520 mHandler = new Handler() {
521
522 @Override
523 public void handleMessage(Message msg) {
524 switch (msg.what) {
525 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700526 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800528 case UNKNOWN_TYPE_MSG:
529 HashMap unknownTypeMap = (HashMap) msg.obj;
530 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
531 /*
532 * When the context menu is shown to the user
533 * we need to assure that its happening on the current webview
534 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
535 */
536 if (getCurrentWebView() != viewForUnknownType)
537 break;
538
539 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800540 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800541 WebView.HitTestResult result = new WebView.HitTestResult();
542
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800543 // Prevent unnecessary calls to context menu
544 // if url and image src are null
545 if (unknown_type_src == null && unknown_type_url == null)
546 break;
547
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800548 //setting the HitTestResult with new RESULT TYPE
549 if (!TextUtils.isEmpty(unknown_type_src)) {
550 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
551 result.setExtra(unknown_type_src);
552 } else {
553 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
554 result.setExtra("about:blank");
555 }
556
557 mResult = result;
558 openContextMenu(viewForUnknownType);
559 mResult = null;
560
561 break;
562
Michael Kolb8233fac2010-10-26 16:08:53 -0700563 case FOCUS_NODE_HREF:
564 {
565 String url = (String) msg.getData().get("url");
566 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500567 String src = (String) msg.getData().get("src");
568 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700569 if (TextUtils.isEmpty(url)) {
570 break;
571 }
572 HashMap focusNodeMap = (HashMap) msg.obj;
573 WebView view = (WebView) focusNodeMap.get("webview");
574 // Only apply the action if the top window did not change.
575 if (getCurrentTopWebView() != view) {
576 break;
577 }
578 switch (msg.arg1) {
579 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700580 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500582 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700583 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500584 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500585 case R.id.open_newtab_context_menu_id:
586 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700587 openTab(url, parent,
588 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500589 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 case R.id.copy_link_context_menu_id:
591 copy(url);
592 break;
593 case R.id.save_link_context_menu_id:
594 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500595 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800596 mActivity, url, view.getSettings().getUserAgentString(),
Pankaj Garg5762b362015-11-02 07:57:06 -0800597 null, null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700599 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700600 if(title == null || title == "")
601 title = url;
602
603 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
604 //SWE TODO: No thumbnail support for the url obtained via
605 //browser context menu as its not loaded in webview.
606 if (bookmarkIntent != null) {
607 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
608 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
609 mActivity.startActivity(bookmarkIntent);
610 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700611 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 }
613 break;
614 }
615
616 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700617 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 break;
619
620 case STOP_LOAD:
621 stopLoading();
622 break;
623
624 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700625 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 mWakeLock.release();
627 // if we reach here, Browser should be still in the
628 // background loading after WAKELOCK_TIMEOUT (5-min).
629 // To avoid burning the battery, stop loading.
630 mTabControl.stopAllLoading();
631 }
632 break;
633
kaiyiz591110b2013-08-06 17:11:06 +0800634 case OPEN_MENU:
635 if (!mOptionsMenuOpen && mActivity != null ) {
636 mActivity.openOptionsMenu();
637 }
638 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 }
640 }
641 };
642
643 }
644
John Reckef654f12011-07-12 16:42:08 -0700645 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200646 public Tab getCurrentTab() {
647 return mTabControl.getCurrentTab();
648 }
649
Michael Kolbba99c5d2010-11-29 14:57:41 -0800650 @Override
651 public void shareCurrentPage() {
652 shareCurrentPage(mTabControl.getCurrentTab());
653 }
654
655 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700656 if (tab == null || tab.getWebView() == null)
657 return;
658
659 final Tab mytab = tab;
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700660
661 createScreenshotAsync(
Pankaj Gargf49e0222015-09-01 12:19:13 -0700662 tab,
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700663 new ValueCallback<Bitmap>() {
664 @Override
665 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -0700666 Bitmap bm = cropAndScaleBitmap(bitmap,
667 getDesiredThumbnailWidth(mActivity),
668 getDesiredThumbnailHeight(mActivity));
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700669 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
Pankaj Gargf49e0222015-09-01 12:19:13 -0700670 mytab.getFavicon(), bm);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700671 }
672 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800673 }
674
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 /**
676 * Share a page, providing the title, url, favicon, and a screenshot. Uses
677 * an {@link Intent} to launch the Activity chooser.
678 * @param c Context used to launch a new Activity.
679 * @param title Title of the page. Stored in the Intent with
680 * {@link Intent#EXTRA_SUBJECT}
681 * @param url URL of the page. Stored in the Intent with
682 * {@link Intent#EXTRA_TEXT}
683 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
684 * with {@link Browser#EXTRA_SHARE_FAVICON}
685 * @param screenshot Bitmap of a screenshot of the page. Stored in the
686 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
687 */
688 static final void sharePage(Context c, String title, String url,
689 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700690
691 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
jrizzoli608c7a92016-04-26 15:16:59 +0200692
693 sDialog.sharePage();
Michael Kolb8233fac2010-10-26 16:08:53 -0700694 }
695
696 private void copy(CharSequence text) {
697 ClipboardManager cm = (ClipboardManager) mActivity
698 .getSystemService(Context.CLIPBOARD_SERVICE);
699 cm.setText(text);
700 }
701
702 // lifecycle
703
John Reck9c35b9c2012-05-30 10:08:50 -0700704 @Override
705 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800707 // update the menu in case of a locale change
708 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800709 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800710 if (mOptionsMenuOpen) {
711 mActivity.closeOptionsMenu();
712 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
713 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700714 mUi.onConfigurationChanged(config);
715 }
716
717 @Override
718 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700719 if (!mUi.isWebShowing()) {
720 mUi.showWeb(false);
721 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700722 mIntentHandler.onNewIntent(intent);
723 }
724
John Reck9c35b9c2012-05-30 10:08:50 -0700725 @Override
726 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800727 if (mUi.isCustomViewShowing()) {
728 hideCustomView();
729 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700730 if (mActivityPaused) {
731 Log.e(LOGTAG, "BrowserActivity is already paused.");
732 return;
733 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700734 mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700735
Michael Kolb8233fac2010-10-26 16:08:53 -0700736 }
737
John Reck9c35b9c2012-05-30 10:08:50 -0700738 @Override
739 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700740 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800741 Bundle saveState = createSaveState();
742
743 // crash recovery manages all save & restore state
744 mCrashRecoveryHandler.writeState(saveState);
745 mSettings.setLastRunPaused(true);
746 }
747
748 /**
749 * Save the current state to outState. Does not write the state to
750 * disk.
751 * @return Bundle containing the current state of all tabs.
752 */
753 /* package */ Bundle createSaveState() {
754 Bundle saveState = new Bundle();
755 mTabControl.saveState(saveState);
Vivek Sekhar2ce13da2015-12-15 11:23:29 -0800756 // This method is called multiple times.Need to
757 // guard against TabControl not having any tabs
758 // during the destroy cycles which looses all the
759 // existing saved information.
760 if (saveState.isEmpty())
761 return null;
George Mount3636d0a2011-11-21 09:08:21 -0800762 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700763 }
764
John Reck9c35b9c2012-05-30 10:08:50 -0700765 @Override
766 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700767 if (!mActivityPaused) {
768 Log.e(LOGTAG, "BrowserActivity is already resumed.");
769 return;
770 }
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700771 mActivityPaused = false;
772 if (mVoiceResult != null) {
773 mUi.onVoiceResult(mVoiceResult);
774 mVoiceResult = null;
775 }
776 }
777
778 private void releaseWakeLock() {
779 if (mWakeLock != null && mWakeLock.isHeld()) {
780 mHandler.removeMessages(RELEASE_WAKELOCK);
781 mWakeLock.release();
782 }
783 }
784
785 @Override
786 public void onStop() {
787 if (mActivityStopped) {
788 Log.e(LOGTAG, "BrowserActivity is already stoped.");
789 return;
790 }
791 mActivityStopped = true;
792 Tab tab = mTabControl.getCurrentTab();
793 if (tab != null) {
794 tab.pause();
795 if (!pauseWebViewTimers(tab)) {
796 if (mWakeLock == null) {
797 PowerManager pm = (PowerManager) mActivity
798 .getSystemService(Context.POWER_SERVICE);
799 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
800 }
801 mWakeLock.acquire();
802 mHandler.sendMessageDelayed(mHandler
803 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
804 }
805 }
806 mUi.onPause();
807 mNetworkHandler.onPause();
808 NfcHandler.unregister(mActivity);
Pankaj Garg5557aba2016-03-19 16:07:14 +0100809 if (mLowPowerReceiver != null)
810 mActivity.unregisterReceiver(mLowPowerReceiver);
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700811 }
812
813 @Override
814 public void onStart() {
815 if (!mActivityStopped) {
816 Log.e(LOGTAG, "BrowserActivity is already started.");
817 return;
818 }
819 mActivityStopped = false;
Vivek Sekharb53a5952015-07-17 14:14:38 -0700820 UpdateNotificationService.updateCheck(mActivity);
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700821 // reset the search engine based on locale
822 mSettings.updateSearchEngine(false);
George Mount3636d0a2011-11-21 09:08:21 -0800823 mSettings.setLastRunPaused(false);
Michael Kolb70976932010-11-30 11:34:01 -0800824 Tab current = mTabControl.getCurrentTab();
825 if (current != null) {
826 current.resume();
827 resumeWebViewTimers(current);
828 }
John Reckf57c0292011-07-21 18:15:39 -0700829 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200830
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 mUi.onResume();
832 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100833 NfcHandler.register(mActivity, this);
Vivek Sekharc70ae632015-04-08 17:54:05 -0700834 if (current != null && current.getWebView().isShowingCrashView())
835 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700836 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700837
John Reckf57c0292011-07-21 18:15:39 -0700838 }
839
Michael Kolb70976932010-11-30 11:34:01 -0800840 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800841 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800842 * @param tab guaranteed non-null
843 */
844 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 boolean inLoad = tab.inPageLoad();
Tarun Nainani7c762c82015-08-04 13:42:09 -0700846 if ((!mActivityStopped && !inLoad) || (mActivityStopped && inLoad)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 CookieSyncManager.getInstance().startSync();
848 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100849 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 }
851 }
852
Michael Kolb70976932010-11-30 11:34:01 -0800853 /**
854 * Pause all WebView timers using the WebView of the given tab
855 * @param tab
856 * @return true if the timers are paused or tab is null
857 */
858 private boolean pauseWebViewTimers(Tab tab) {
859 if (tab == null) {
860 return true;
861 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100863 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 }
Michael Kolb70976932010-11-30 11:34:01 -0800866 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700867 }
868
John Reck9c35b9c2012-05-30 10:08:50 -0700869 @Override
870 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800871 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
873 mUploadHandler = null;
874 }
875 if (mTabControl == null) return;
876 mUi.onDestroy();
877 // Remove the current tab and sub window
878 Tab t = mTabControl.getCurrentTab();
879 if (t != null) {
880 dismissSubWindow(t);
881 removeTab(t);
882 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500883 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 // Destroy all the tabs
885 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700886 // Unregister receiver
Pankaj Garg5557aba2016-03-19 16:07:14 +0100887 if (mPowerChangeReceiver != null)
888 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 }
890
891 protected boolean isActivityPaused() {
892 return mActivityPaused;
893 }
894
John Reck9c35b9c2012-05-30 10:08:50 -0700895 @Override
896 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 mTabControl.freeMemory();
898 }
899
900 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700901 public void stopLoading() {
902 mLoadStopped = true;
903 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700904 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700905 if (w != null) {
906 w.stopLoading();
907 mUi.onPageStopped(tab);
908 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 }
910
911 boolean didUserStopLoading() {
912 return mLoadStopped;
913 }
914
kaiyiza016da12013-08-26 17:50:22 +0800915 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700916 final String reminderType = getContext().getResources().getString(
917 R.string.def_wifi_browser_interaction_remind_type);
918 final String selectionConnnection = getContext().getResources().getString(
919 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700920 final String wifiSelection = getContext().getResources().getString(
921 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700922
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700923 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
924 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700925 return;
926
kaiyiza016da12013-08-26 17:50:22 +0800927 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700928 Context.CONNECTIVITY_SERVICE);
929 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700930 WifiManager wifiMgr = (WifiManager) this.getContext()
931 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700932 if (networkInfo == null
933 || (networkInfo != null && (networkInfo.getType() !=
934 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700935 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
936 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700937 List<ScanResult> list = wifiMgr.getScanResults();
938 // Have no AP's for Wifi's fall back to data
939 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700940 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700941 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
942 this.getContext().startActivity(intent);
943 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700944 // Request to select Wifi AP
945 try {
946 Intent intent = new Intent(wifiSelection);
947 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
948 this.getContext().startActivity(intent);
949 } catch (Exception e) {
950 String err_msg = this.getContext().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700951 R.string.activity_not_found, wifiSelection);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700952 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
953 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700954 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700955 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800956 }
957 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700958
Michael Kolb8233fac2010-10-26 16:08:53 -0700959 // WebViewController
960
961 @Override
John Reck324d4402011-01-11 16:56:42 -0800962 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700963
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 // reset sync timer to avoid sync starts during loading a page
965 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700966 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700967 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800968 boolean networkNotifier = BrowserConfig.getInstance(getContext())
969 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700970 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700971 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800972 } else {
973 if (!mNetworkHandler.isNetworkUp()) {
974 view.setNetworkAvailable(false);
975 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 }
977
978 // when BrowserActivity just starts, onPageStarted may be called before
979 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
980 // to start the timer. As we won't switch tabs while an activity is in
981 // pause state, we can ensure calling resume and pause in pair.
Tarun Nainani7c762c82015-08-04 13:42:09 -0700982 if (mActivityStopped) {
Michael Kolb70976932010-11-30 11:34:01 -0800983 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700984 }
985 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 endActionMode();
987
John Reck30c714c2010-12-16 17:30:34 -0800988 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700989
John Reck324d4402011-01-11 16:56:42 -0800990 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700991 // update the bookmark database for favicon
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700992 syncBookmarkFavicon(tab, null, url, favicon);
Michael Kolb8233fac2010-10-26 16:08:53 -0700993
994 Performance.tracePageStart(url);
995
996 // Performance probe
997 if (false) {
998 Performance.onPageStarted();
999 }
1000
1001 }
1002
1003 @Override
John Reck324d4402011-01-11 16:56:42 -08001004 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -07001005 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -08001006 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001007
Michael Kolb8233fac2010-10-26 16:08:53 -07001008 // Performance probe
1009 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001010 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001011 }
1012
Tarun Nainani8eb00912014-07-17 12:28:32 -07001013 tab.onPageFinished();
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001014 syncBookmarkFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -07001015
Michael Kolb8233fac2010-10-26 16:08:53 -07001016 Performance.tracePageFinished();
1017 }
1018
1019 @Override
John Reck30c714c2010-12-16 17:30:34 -08001020 public void onProgressChanged(Tab tab) {
1021 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001022
1023 if (newProgress == 100) {
1024 CookieSyncManager.getInstance().sync();
1025 // onProgressChanged() may continue to be called after the main
1026 // frame has finished loading, as any remaining sub frames continue
1027 // to load. We'll only get called once though with newProgress as
1028 // 100 when everything is loaded. (onPageFinished is called once
1029 // when the main frame completes loading regardless of the state of
1030 // any sub frames so calls to onProgressChanges may continue after
1031 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001032 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001033 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001034 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001035 } else if (mWasInPageLoad) {
1036 mWasInPageLoad = false;
1037 updateInLoadMenuItems(mCachedMenu, tab);
1038 }
1039
Tarun Nainani7c762c82015-08-04 13:42:09 -07001040 if (mActivityStopped && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001041 // pause the WebView timer and release the wake lock if it is
1042 // finished while BrowserActivity is in pause state.
1043 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 }
1045 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001046 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001047 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001048 // still loading
1049 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001051 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001052 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001053 } else {
1054 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 }
1056 }
Pankaj Gargf9040c82015-08-14 10:19:31 -07001057
1058 mUi.onProgressChanged(tab);
John Reck30c714c2010-12-16 17:30:34 -08001059 }
1060
1061 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001062 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001063 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 }
1065
1066 @Override
1067 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001068 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001069 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001070 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001071 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1072 return;
1073 }
1074 // Update the title in the history database if not in private browsing mode
1075 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001076 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 }
1078 }
1079
1080 @Override
1081 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001082 syncBookmarkFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1083 ((BaseUi)mUi).setFavicon(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 }
1085
1086 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001087 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001088 // if tab is snapshot tab we want to prevent navigation from occuring
1089 // since snapshot tab opens a new tab with the url
1090 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 }
1092
1093 @Override
1094 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1095 if (mMenuIsDown) {
1096 // only check shortcut key when MENU is held
1097 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1098 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001099 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001100 int keyCode = event.getKeyCode();
1101 // We need to send almost every key to WebKit. However:
1102 // 1. We don't want to block the device on the renderer for
1103 // some keys like menu, home, call.
1104 // 2. There are no WebKit equivalents for some of these keys
1105 // (see app/keyboard_codes_win.h)
1106 // Note that these are not the same set as KeyEvent.isSystemKey:
1107 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1108 if (keyCode == KeyEvent.KEYCODE_MENU ||
1109 keyCode == KeyEvent.KEYCODE_HOME ||
1110 keyCode == KeyEvent.KEYCODE_BACK ||
1111 keyCode == KeyEvent.KEYCODE_CALL ||
1112 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1113 keyCode == KeyEvent.KEYCODE_POWER ||
1114 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1115 keyCode == KeyEvent.KEYCODE_CAMERA ||
1116 keyCode == KeyEvent.KEYCODE_FOCUS ||
1117 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1118 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1119 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1120 return true;
1121 }
1122
1123 // We also have to intercept some shortcuts before we send them to the ContentView.
1124 if (event.isCtrlPressed() && (
1125 keyCode == KeyEvent.KEYCODE_TAB ||
1126 keyCode == KeyEvent.KEYCODE_W ||
1127 keyCode == KeyEvent.KEYCODE_F4)) {
1128 return true;
1129 }
1130
1131 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001132 }
1133
Tarun Nainanif5563b62015-01-21 18:52:59 -08001134 private void handleMediaKeyEvent(KeyEvent event) {
1135
1136 int keyCode = event.getKeyCode();
1137 // send media key events to audio manager
1138 if (Build.VERSION.SDK_INT >= 19) {
1139
1140 switch (keyCode) {
1141 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1142 case KeyEvent.KEYCODE_MEDIA_STOP:
1143 case KeyEvent.KEYCODE_MEDIA_NEXT:
1144 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1145 case KeyEvent.KEYCODE_MEDIA_REWIND:
1146 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1147 case KeyEvent.KEYCODE_MUTE:
1148 case KeyEvent.KEYCODE_MEDIA_PLAY:
1149 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1150 case KeyEvent.META_SHIFT_RIGHT_ON:
1151 case KeyEvent.KEYCODE_MEDIA_EJECT:
1152 case KeyEvent.KEYCODE_MEDIA_RECORD:
1153 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1154
1155 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1156 .getSystemService(Context.AUDIO_SERVICE);
1157 audioManager.dispatchMediaKeyEvent(event);
1158 }
1159 }
1160 }
1161
Michael Kolb8233fac2010-10-26 16:08:53 -07001162 @Override
John Reck997b1b72012-04-19 18:08:25 -07001163 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001165 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001167 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001168 } else {
John Reck997b1b72012-04-19 18:08:25 -07001169 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 }
1171 }
John Reck997b1b72012-04-19 18:08:25 -07001172 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001173 }
1174
1175 @Override
John Reck324d4402011-01-11 16:56:42 -08001176 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001177 // Don't save anything in private browsing mode or when disabling history
1178 // for regular tabs is enabled
1179 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1180 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1181 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001182
John Reck49a603c2011-03-03 09:33:05 -08001183 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001184
John Reck324d4402011-01-11 16:56:42 -08001185 if (TextUtils.isEmpty(url)
1186 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001187 return;
1188 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001189
John Reckf57c0292011-07-21 18:15:39 -07001190 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001191 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001192 }
1193
1194 @Override
1195 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1196 AsyncTask<Void, Void, String[]> task =
1197 new AsyncTask<Void, Void, String[]>() {
1198 @Override
1199 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001200 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001201 }
1202 @Override
1203 public void onPostExecute(String[] result) {
1204 callback.onReceiveValue(result);
1205 }
1206 };
1207 task.execute();
1208 }
1209
1210 @Override
1211 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1212 final HttpAuthHandler handler, final String host,
1213 final String realm) {
1214 String username = null;
1215 String password = null;
1216
1217 boolean reuseHttpAuthUsernamePassword
1218 = handler.useHttpAuthUsernamePassword();
1219
1220 if (reuseHttpAuthUsernamePassword && view != null) {
1221 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1222 if (credentials != null && credentials.length == 2) {
1223 username = credentials[0];
1224 password = credentials[1];
1225 }
1226 }
1227
1228 if (username != null && password != null) {
1229 handler.proceed(username, password);
1230 } else {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -07001231 if (!tab.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 handler.cancel();
1233 }
1234 }
1235 }
1236
1237 @Override
1238 public void onDownloadStart(Tab tab, String url, String userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001239 String contentDisposition, String mimetype, String referer, String auth,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001240 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001241 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001242 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001243 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001244 contentDisposition, mimetype, referer, auth,
1245 w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001246 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001247 // This Tab was opened for the sole purpose of downloading a
1248 // file. Remove it.
1249 if (tab == mTabControl.getCurrentTab()) {
1250 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001251 if (tab.getDerivedFromIntent())
1252 closeTab(tab);
1253 else
1254 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001255 } else {
1256 // In this case, it is not.
1257 closeTab(tab);
1258 }
1259 }
1260 }
1261
1262 @Override
1263 public Bitmap getDefaultVideoPoster() {
1264 return mUi.getDefaultVideoPoster();
1265 }
1266
1267 @Override
1268 public View getVideoLoadingProgressView() {
1269 return mUi.getVideoLoadingProgressView();
1270 }
1271
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 // helper method
1273
1274 /*
1275 * Update the favorites icon if the private browsing isn't enabled and the
1276 * icon is valid.
1277 */
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001278 private void syncBookmarkFavicon(Tab tab, final String originalUrl,
1279 final String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001280 if (favicon == null) {
1281 return;
1282 }
1283 if (!tab.isPrivateBrowsingEnabled()) {
1284 Bookmarks.updateFavicon(mActivity
1285 .getContentResolver(), originalUrl, url, favicon);
1286 }
1287 }
1288
Leon Scroggins4cd97792010-12-03 15:31:56 -05001289 @Override
1290 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001291 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001292 mUi.bookmarkedStatusHasChanged(tab);
1293 }
1294
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 // end WebViewController
1296
1297 protected void pageUp() {
1298 getCurrentTopWebView().pageUp(false);
1299 }
1300
1301 protected void pageDown() {
1302 getCurrentTopWebView().pageDown(false);
1303 }
1304
1305 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001306 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001307 public void editUrl() {
1308 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001309 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001310 }
1311
John Reck9c35b9c2012-05-30 10:08:50 -07001312 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001313 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001314 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001315 if (tab.inForeground()) {
1316 if (mUi.isCustomViewShowing()) {
1317 callback.onCustomViewHidden();
1318 return;
1319 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001320 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001321 // Save the menu state and set it to empty while the custom
1322 // view is showing.
1323 mOldMenuState = mMenuState;
1324 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001325 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001326 }
1327 }
1328
1329 @Override
1330 public void hideCustomView() {
1331 if (mUi.isCustomViewShowing()) {
1332 mUi.onHideCustomView();
1333 // Reset the old menu state.
1334 mMenuState = mOldMenuState;
1335 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001336 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 }
1338 }
1339
John Reck9c35b9c2012-05-30 10:08:50 -07001340 @Override
1341 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001342 Intent intent) {
1343 if (getCurrentTopWebView() == null) return;
1344 switch (requestCode) {
1345 case PREFERENCES_PAGE:
1346 if (resultCode == Activity.RESULT_OK && intent != null) {
1347 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001348 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001349 mTabControl.removeParentChildRelationShips();
Pankaj Garg75273bb2015-08-20 11:43:49 -07001350 } else if (action.equals(PreferenceKeys.ACTION_RELOAD_PAGE)) {
1351 ArrayList<String> origins =
1352 intent.getStringArrayListExtra(EXTRA_UPDATED_URLS);
1353 if (origins.isEmpty()) {
1354 mTabControl.reloadLiveTabs();
1355 }
1356 else{
1357 for (String origin : origins){
1358 mTabControl.findAndReload(origin);
1359 }
1360 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001361 }
1362 }
1363 break;
1364 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001365 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001366 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001367 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001369 case AUTOFILL_SETUP:
1370 // Determine whether a profile was actually set up or not
1371 // and if so, send the message back to the WebTextView to
1372 // fill the form with the new profile.
1373 if (getSettings().getAutoFillProfile() != null) {
1374 mAutoFillSetupMessage.sendToTarget();
1375 mAutoFillSetupMessage = null;
1376 }
1377 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001378 case COMBO_VIEW:
1379 if (intent == null || resultCode != Activity.RESULT_OK) {
1380 break;
1381 }
John Reck3ba45532011-08-11 16:26:53 -07001382 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001383 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1384 Tab t = getCurrentTab();
1385 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001386 mUpdateMyNavThumbnail = true;
1387 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001388 loadUrl(t, uri.toString());
1389 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1390 String[] urls = intent.getStringArrayExtra(
1391 ComboViewActivity.EXTRA_OPEN_ALL);
1392 Tab parent = getCurrentTab();
1393 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001394 if (url != null) {
1395 parent = openTab(url, parent,
1396 !mSettings.openInBackground(), true);
1397 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001398 }
1399 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1400 long id = intent.getLongExtra(
1401 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1402 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001403 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001404 }
1405 }
1406 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001407 case VOICE_RESULT:
1408 if (resultCode == Activity.RESULT_OK && intent != null) {
1409 ArrayList<String> results = intent.getStringArrayListExtra(
1410 RecognizerIntent.EXTRA_RESULTS);
1411 if (results.size() >= 1) {
1412 mVoiceResult = results.get(0);
1413 }
1414 }
1415 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001416 case MY_NAVIGATION:
1417 if (intent == null || resultCode != Activity.RESULT_OK) {
1418 break;
1419 }
1420
1421 if (intent.getBooleanExtra("need_refresh", false) &&
1422 getCurrentTopWebView() != null) {
1423 getCurrentTopWebView().reload();
1424 }
1425 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001426 default:
1427 break;
1428 }
1429 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001430 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001431 }
1432
1433 /**
1434 * Open the Go page.
1435 * @param startWithHistory If true, open starting on the history tab.
1436 * Otherwise, start with the bookmarks tab.
1437 */
1438 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001439 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001440 if (mTabControl.getCurrentWebView() == null) {
1441 return;
1442 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001443 // clear action mode
1444 if (isInCustomActionMode()) {
1445 endActionMode();
1446 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001447 Bundle extras = new Bundle();
1448 // Disable opening in a new window if we have maxed out the windows
1449 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1450 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001451 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001452 }
1453
1454 // combo view callbacks
1455
Michael Kolb8233fac2010-10-26 16:08:53 -07001456 // key handling
1457 protected void onBackKey() {
1458 if (!mUi.onBackKey()) {
1459 WebView subwindow = mTabControl.getCurrentSubWindow();
1460 if (subwindow != null) {
1461 if (subwindow.canGoBack()) {
1462 subwindow.goBack();
1463 } else {
1464 dismissSubWindow(mTabControl.getCurrentTab());
1465 }
1466 } else {
1467 goBackOnePageOrQuit();
1468 }
1469 }
1470 }
1471
Michael Kolb4bd767d2011-05-27 11:33:55 -07001472 protected boolean onMenuKey() {
1473 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001474 }
1475
Michael Kolb8233fac2010-10-26 16:08:53 -07001476 // menu handling and state
1477 // TODO: maybe put into separate handler
1478
John Reck9c35b9c2012-05-30 10:08:50 -07001479 @Override
1480 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001481 if (mMenuState == EMPTY_MENU) {
1482 return false;
1483 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001484 MenuInflater inflater = mActivity.getMenuInflater();
1485 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001486 return true;
1487 }
1488
John Reck9c35b9c2012-05-30 10:08:50 -07001489 @Override
1490 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001491 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001492 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001493 return;
1494 }
1495 if (!(v instanceof WebView)) {
1496 return;
1497 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001498 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001499 WebView.HitTestResult result;
1500
1501 /* Determine whether the ContextMenu got triggered because
1502 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1503 * received. The mResult acts as a flag to identify, how it got trigerred
1504 */
1505 if (mResult == null){
1506 result = webview.getHitTestResult();
1507 } else {
1508 result = mResult;
1509 }
1510
Michael Kolb8233fac2010-10-26 16:08:53 -07001511 if (result == null) {
1512 return;
1513 }
1514
1515 int type = result.getType();
1516 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001517
1518 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1519 unknownTypeMap.put("webview", webview);
1520 final Message msg = mHandler.obtainMessage(
1521 UNKNOWN_TYPE_MSG, unknownTypeMap);
1522 /* As defined in android developers guide
1523 * when UNKNOWN_TYPE is received as a result of HitTest
1524 * you need to determing the type by invoking requestFocusNodeHref
1525 */
1526 webview.requestFocusNodeHref(msg);
1527
Michael Kolb8233fac2010-10-26 16:08:53 -07001528 Log.w(LOGTAG,
1529 "We should not show context menu when nothing is touched");
1530 return;
1531 }
1532 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1533 // let TextView handles context menu
1534 return;
1535 }
1536
1537 // Note, http://b/issue?id=1106666 is requesting that
1538 // an inflated menu can be used again. This is not available
1539 // yet, so inflate each time (yuk!)
1540 MenuInflater inflater = mActivity.getMenuInflater();
1541 inflater.inflate(R.menu.browsercontext, menu);
1542
1543 // Show the correct menu group
1544 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001545 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001546 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001547 menu.setGroupVisible(R.id.PHONE_MENU,
1548 type == WebView.HitTestResult.PHONE_TYPE);
1549 menu.setGroupVisible(R.id.EMAIL_MENU,
1550 type == WebView.HitTestResult.EMAIL_TYPE);
1551 menu.setGroupVisible(R.id.GEO_MENU,
1552 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001553 String itemUrl = null;
1554 String url = webview.getOriginalUrl();
1555 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1556 itemUrl = Uri.decode(navigationUrl);
1557 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1558 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1559 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1560 } else {
1561 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1562 }
1563 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1564 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1565 } else {
1566 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1567
1568 menu.setGroupVisible(R.id.IMAGE_MENU,
1569 type == WebView.HitTestResult.IMAGE_TYPE
1570 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1571 menu.setGroupVisible(R.id.ANCHOR_MENU,
1572 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1573 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001574
1575 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1576 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1577 UrlUtils.isDownloadableScheme(extra));
1578 }
1579 else {
1580 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1581 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001582 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001583 // Setup custom handling depending on the type
1584 switch (type) {
1585 case WebView.HitTestResult.PHONE_TYPE:
1586 menu.setHeaderTitle(Uri.decode(extra));
1587 menu.findItem(R.id.dial_context_menu_id).setIntent(
1588 new Intent(Intent.ACTION_VIEW, Uri
Vivek Sekharcb3f7232015-12-10 20:02:46 -08001589 .parse(WebView.SCHEME_TEL + Uri.encode(extra))));
Michael Kolb8233fac2010-10-26 16:08:53 -07001590 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1591 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1592 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1593 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1594 addIntent);
1595 menu.findItem(R.id.copy_phone_context_menu_id)
1596 .setOnMenuItemClickListener(
1597 new Copy(extra));
1598 break;
1599
1600 case WebView.HitTestResult.EMAIL_TYPE:
1601 menu.setHeaderTitle(extra);
1602 menu.findItem(R.id.email_context_menu_id).setIntent(
1603 new Intent(Intent.ACTION_VIEW, Uri
1604 .parse(WebView.SCHEME_MAILTO + extra)));
1605 menu.findItem(R.id.copy_mail_context_menu_id)
1606 .setOnMenuItemClickListener(
1607 new Copy(extra));
1608 break;
1609
1610 case WebView.HitTestResult.GEO_TYPE:
1611 menu.setHeaderTitle(extra);
1612 menu.findItem(R.id.map_context_menu_id).setIntent(
1613 new Intent(Intent.ACTION_VIEW, Uri
1614 .parse(WebView.SCHEME_GEO
1615 + URLEncoder.encode(extra))));
1616 menu.findItem(R.id.copy_geo_context_menu_id)
1617 .setOnMenuItemClickListener(
1618 new Copy(extra));
1619 break;
1620
1621 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1622 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001623 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001624 // decide whether to show the open link in new tab option
1625 boolean showNewTab = mTabControl.canCreateNewTab();
1626 MenuItem newTabItem
1627 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001628 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001629 ? R.string.contextmenu_openlink_newwindow_background
1630 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001631 newTabItem.setVisible(showNewTab);
1632 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001633 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1634 newTabItem.setOnMenuItemClickListener(
1635 new MenuItem.OnMenuItemClickListener() {
1636 @Override
1637 public boolean onMenuItemClick(MenuItem item) {
1638 final HashMap<String, WebView> hrefMap =
1639 new HashMap<String, WebView>();
1640 hrefMap.put("webview", webview);
1641 final Message msg = mHandler.obtainMessage(
1642 FOCUS_NODE_HREF,
1643 R.id.open_newtab_context_menu_id,
1644 0, hrefMap);
1645 webview.requestFocusNodeHref(msg);
1646 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001647 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001648 });
1649 } else {
1650 newTabItem.setOnMenuItemClickListener(
1651 new MenuItem.OnMenuItemClickListener() {
1652 @Override
1653 public boolean onMenuItemClick(MenuItem item) {
1654 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001655 openTab(extra, parent,
1656 !mSettings.openInBackground(),
1657 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001658 return true;
1659 }
1660 });
1661 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001662 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001663 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1664 menu.setHeaderTitle(navigationUrl);
1665 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1666
1667 if (itemUrl != null) {
1668 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1669 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1670 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1671 @Override
1672 public boolean onMenuItemClick(MenuItem item) {
1673 final Intent intent = new Intent(Controller.this
1674 .getContext(),
1675 AddMyNavigationPage.class);
1676 Bundle bundle = new Bundle();
1677 String url = Uri.decode(navigationUrl);
1678 bundle.putBoolean("isAdding", false);
1679 bundle.putString("url", url);
1680 bundle.putString("name", getNameFromUrl(url));
1681 intent.putExtra("websites", bundle);
1682 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1683 return false;
1684 }
1685 });
1686 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1687 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1688 @Override
1689 public boolean onMenuItemClick(MenuItem item) {
1690 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1691 return false;
1692 }
1693 });
1694 }
1695 } else {
1696 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1697 }
1698 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001699 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1700 break;
1701 }
1702 // otherwise fall through to handle image part
1703 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001704 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1705 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001706 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1707 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001708 shareItem.setOnMenuItemClickListener(
1709 new MenuItem.OnMenuItemClickListener() {
1710 @Override
1711 public boolean onMenuItemClick(MenuItem item) {
1712 sharePage(mActivity, null, extra, null,
1713 null);
1714 return true;
1715 }
1716 }
1717 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001718 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001719 menu.findItem(R.id.view_image_context_menu_id)
1720 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1721 @Override
1722 public boolean onMenuItemClick(MenuItem item) {
1723 openTab(extra, mTabControl.getCurrentTab(), true, true);
1724 return false;
1725 }
1726 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001727 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1728 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1729 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001730 menu.findItem(R.id.set_wallpaper_context_menu_id).
1731 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1732 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001733 break;
1734
1735 default:
1736 Log.w(LOGTAG, "We should not get here.");
1737 break;
1738 }
1739 //update the ui
1740 mUi.onContextMenuCreated(menu);
1741 }
1742
kaiyiz6e5b3e02013-08-19 20:02:01 +08001743 public void startAddMyNavigation(String url) {
1744 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1745 Bundle bundle = new Bundle();
1746 bundle.putBoolean("isAdding", true);
1747 bundle.putString("url", url);
1748 bundle.putString("name", getNameFromUrl(url));
1749 intent.putExtra("websites", bundle);
1750 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1751 }
1752
1753 private void showMyNavigationDeleteDialog(final String itemUrl) {
1754 new AlertDialog.Builder(this.getContext())
1755 .setTitle(R.string.my_navigation_delete_label)
1756 .setIcon(android.R.drawable.ic_dialog_alert)
1757 .setMessage(R.string.my_navigation_delete_msg)
1758 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1759 @Override
1760 public void onClick(DialogInterface dialog, int whichButton) {
1761 deleteMyNavigationItem(itemUrl);
1762 }
1763 })
1764 .setNegativeButton(R.string.cancel, null)
1765 .show();
1766 }
1767
1768 private void deleteMyNavigationItem(final String itemUrl) {
1769 ContentResolver cr = this.getContext().getContentResolver();
1770 Cursor cursor = null;
1771
1772 try {
1773 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1774 new String[] {
1775 MyNavigationUtil.ID
1776 }, "url = ?", new String[] {
1777 itemUrl
1778 }, null);
1779 if (null != cursor && cursor.moveToFirst()) {
1780 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1781 cursor.getLong(0));
1782
1783 ContentValues values = new ContentValues();
1784 values.put(MyNavigationUtil.TITLE, "");
1785 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1786 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1787 ByteArrayOutputStream os = new ByteArrayOutputStream();
1788 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1789 R.raw.my_navigation_add);
1790 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1791 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1792 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1793 cr.update(uri, values, null, null);
1794 } else {
1795 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1796 }
1797 } catch (IllegalStateException e) {
1798 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1799 } finally {
1800 if (null != cursor) {
1801 cursor.close();
1802 }
1803 }
1804
1805 if (getCurrentTopWebView() != null) {
1806 getCurrentTopWebView().reload();
1807 }
1808 }
1809
1810 private String getNameFromUrl(String itemUrl) {
1811 ContentResolver cr = this.getContext().getContentResolver();
1812 Cursor cursor = null;
1813 String name = null;
1814
1815 try {
1816 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1817 new String[] {
1818 MyNavigationUtil.TITLE
1819 }, "url = ?", new String[] {
1820 itemUrl
1821 }, null);
1822 if (null != cursor && cursor.moveToFirst()) {
1823 name = cursor.getString(0);
1824 } else {
1825 Log.e(LOGTAG, "this item does not exist!");
1826 }
1827 } catch (IllegalStateException e) {
1828 Log.e(LOGTAG, "getNameFromUrl", e);
1829 } finally {
1830 if (null != cursor) {
1831 cursor.close();
1832 }
1833 }
1834 return name;
1835 }
1836
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001837 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001838 final ContentResolver cr = mActivity.getContentResolver();
1839 new AsyncTask<Void, Void, Void>() {
1840 @Override
1841 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001842 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1843 if(!isMyNavigationUrl)
1844 return null;
1845
kaiyiz6e5b3e02013-08-19 20:02:01 +08001846 ContentResolver cr = mActivity.getContentResolver();
1847 Cursor cursor = null;
1848 try {
1849 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1850 new String[] {
1851 MyNavigationUtil.ID
1852 }, "url = ?", new String[] {
1853 itemUrl
1854 }, null);
1855 if (null != cursor && cursor.moveToFirst()) {
1856 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001857 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001858
1859 ContentValues values = new ContentValues();
1860 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1861 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1862 cursor.getLong(0));
1863 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1864 cr.update(uri, values, null, null);
1865 os.close();
1866 }
1867 } catch (IllegalStateException e) {
1868 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1869 } catch (IOException e) {
1870 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1871 } finally {
1872 if (null != cursor) {
1873 cursor.close();
1874 }
1875 }
1876 return null;
1877 }
1878 }.execute();
1879 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001880 /**
1881 * As the menu can be open when loading state changes
1882 * we must manually update the state of the stop/reload menu
1883 * item
1884 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001885 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001886 if (menu == null) {
1887 return;
1888 }
1889 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001890 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001891 menu.findItem(R.id.stop_menu_id):
1892 menu.findItem(R.id.reload_menu_id);
1893 if (src != null) {
1894 dest.setIcon(src.getIcon());
1895 dest.setTitle(src.getTitle());
1896 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001897 mActivity.invalidateOptionsMenu();
1898 }
1899
1900 public void invalidateOptionsMenu() {
1901 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001902 }
1903
John Reck9c35b9c2012-05-30 10:08:50 -07001904 @Override
1905 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001906 // Software menu key (toolbar key)
Sagar Dhawan7fa89622016-01-04 17:58:02 -08001907 View overflowMenu = mActivity.findViewById(R.id.more_browser_settings);
1908 if (getCurrentTab() != null && getCurrentTab().isSnapshot()) {
1909 overflowMenu = mActivity.findViewById(R.id.more);
1910 }
1911 mAppMenuHandler.showAppMenu(overflowMenu, false, false);
Pankaj Garg49b79252014-11-07 17:33:41 -08001912 return true;
1913 }
1914
1915 @Override
1916 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001917 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001918 // hold on to the menu reference here; it is used by the page callbacks
1919 // to update the menu based on loading state
1920 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001921 // Note: setVisible will decide whether an item is visible; while
1922 // setEnabled() will decide whether an item is enabled, which also means
1923 // whether the matching shortcut key will function.
1924 switch (mMenuState) {
1925 case EMPTY_MENU:
1926 if (mCurrentMenuState != mMenuState) {
1927 menu.setGroupVisible(R.id.MAIN_MENU, false);
1928 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1929 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1930 }
1931 break;
1932 default:
1933 if (mCurrentMenuState != mMenuState) {
1934 menu.setGroupVisible(R.id.MAIN_MENU, true);
1935 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1936 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1937 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001938 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001939 break;
1940 }
1941 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001942 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001943
1944 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001945 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1946 EditBookmarksRestriction.getInstance()
1947 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001948 }
1949
1950 private void setMenuItemVisibility(Menu menu, int id,
1951 boolean visibility) {
1952 MenuItem item = menu.findItem(id);
1953 if (item != null) {
1954 item.setVisible(visibility);
1955 }
1956 }
1957
Sagar Dhawanef06f562015-09-07 15:23:22 +02001958 private int lookupBookmark( String url) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001959 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001960 int count = 0;
1961 Cursor cursor = null;
1962 try {
1963 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1964 BookmarksLoader.PROJECTION,
Sagar Dhawanef06f562015-09-07 15:23:22 +02001965 "url = ?",
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001966 new String[] {
Sagar Dhawanef06f562015-09-07 15:23:22 +02001967 url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001968 },
1969 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001970
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001971 if (cursor != null)
1972 count = cursor.getCount();
1973
1974 } catch (IllegalStateException e) {
1975 Log.e(LOGTAG, "lookupBookmark ", e);
1976 } finally {
1977 if (null != cursor) {
1978 cursor.close();
1979 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001980 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001981 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001982 }
1983
1984 private void resetMenuItems(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001985 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1986
1987 WebView w = getCurrentTopWebView();
1988 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1989
1990 String title = w.getTitle();
1991 String url = w.getUrl();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001992 mCurrentPageBookmarked = (lookupBookmark(url) > 0);
Pankaj Garg634bf432015-07-13 09:54:21 -07001993 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001994 bookmark_icon.setChecked(true);
1995 } else {
1996 bookmark_icon.setChecked(false);
1997 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001998
1999 // update reader mode checkbox
2000 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2001 readerSwitcher.setVisible(false);
2002 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002003 }
2004
Michael Kolb4bf79712011-07-14 14:18:12 -07002005 @Override
2006 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002007 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07002008 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07002009 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07002010 // Following flag is used to identify schemes for which the LIVE_MENU
2011 // items defined in res/menu/browser.xml should be enabled
2012 boolean isLiveScheme = false;
2013 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002014 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07002015
2016 boolean isDistillable = false;
2017 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002018 resetMenuItems(menu);
2019
Michael Kolb4bf79712011-07-14 14:18:12 -07002020 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002021 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002022 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002023 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002024 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002025 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002026 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07002027
2028 isDistillable = tab.isDistillable();
2029 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07002030 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002031
2032 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2033 forward.setEnabled(canGoForward);
2034
Michael Kolb4bf79712011-07-14 14:18:12 -07002035 // decide whether to show the share link option
2036 PackageManager pm = mActivity.getPackageManager();
2037 Intent send = new Intent(Intent.ACTION_SEND);
2038 send.setType("text/plain");
2039 ResolveInfo ri = pm.resolveActivity(send,
2040 PackageManager.MATCH_DEFAULT_ONLY);
2041 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2042
2043 boolean isNavDump = mSettings.enableNavDump();
2044 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2045 nav.setVisible(isNavDump);
2046 nav.setEnabled(isNavDump);
2047
2048 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002049 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2050 uaSwitcher.setChecked(isDesktopUa);
Sagar Dhawanef06f562015-09-07 15:23:22 +02002051 setMenuItemVisibility(menu, R.id.find_menu_id, isLive);
2052 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002053 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002054 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002055 setMenuItemVisibility(menu, R.id.add_to_homescreen,
Sagar Dhawanef06f562015-09-07 15:23:22 +02002056 isLive && isPageFinished);
Pankaj Garg49b79252014-11-07 17:33:41 -08002057 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002058 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002059 // history and snapshots item are the members of COMBO menu group,
2060 // so if show history item, only make snapshots item invisible.
2061 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002062
Ze G Riande2a675c22015-06-03 11:15:24 -07002063
2064 // update reader mode checkbox
2065 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2066 // The reader mode checkbox is hidden only
2067 // when the current page is neither distillable nor distilled
2068 readerSwitcher.setVisible(isDistillable || isDistilled);
2069 readerSwitcher.setChecked(isDistilled);
2070
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002071 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002072 }
2073
Pankaj Gargf49e0222015-09-01 12:19:13 -07002074 private Bitmap cropAndScaleBitmap(Bitmap bm, int width, int height) {
2075 if (width == 0 || height == 0 || bm == null)
2076 return bm;
2077
2078 Bitmap cropped;
2079
2080 if (bm.getHeight() > bm.getWidth()) {
2081 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
2082 bm.getWidth() * height / width, null, true);
2083 } else {
2084 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getHeight() * width / height,
2085 bm.getHeight(), null, true);
2086 }
2087
2088 Bitmap scaled = Bitmap.createScaledBitmap(cropped, width, height, true);
2089 cropped.recycle();
2090 return scaled;
2091 }
2092
John Reck9c35b9c2012-05-30 10:08:50 -07002093 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002094 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002095 if (null == getCurrentTopWebView()) {
2096 return false;
2097 }
2098 if (mMenuIsDown) {
2099 // The shortcut action consumes the MENU. Even if it is still down,
2100 // it won't trigger the next shortcut action. In the case of the
2101 // shortcut action triggering a new activity, like Bookmarks, we
2102 // won't get onKeyUp for MENU. So it is important to reset it here.
2103 mMenuIsDown = false;
2104 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002105 if (mUi.onOptionsItemSelected(item)) {
2106 // ui callback handled it
2107 return true;
2108 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002109 switch (item.getItemId()) {
2110 // -- Main menu
2111 case R.id.new_tab_menu_id:
2112 openTabToHomePage();
2113 break;
2114
2115 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002116 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002117 break;
2118
jrizzolidb51f632016-03-22 15:57:18 +01002119 case R.id.close_other_tabs_id:
2120 closeOtherTabs();
2121 break;
2122
Michael Kolb8233fac2010-10-26 16:08:53 -07002123 case R.id.goto_menu_id:
2124 editUrl();
2125 break;
2126
2127 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002128 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2129 break;
2130
Michael Kolb315d5022011-10-13 12:47:11 -07002131 case R.id.snapshots_menu_id:
2132 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 break;
2134
Pankaj Garg49b79252014-11-07 17:33:41 -08002135 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002136 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002137 break;
2138
2139 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002140 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002141 stopLoading();
2142 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002143 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002144 getCurrentTopWebView().reload();
2145 }
2146 break;
2147
Michael Kolb8233fac2010-10-26 16:08:53 -07002148 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002149 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002150 break;
2151
2152 case R.id.close_menu_id:
2153 // Close the subwindow if it exists.
2154 if (mTabControl.getCurrentSubWindow() != null) {
2155 dismissSubWindow(mTabControl.getCurrentTab());
2156 break;
2157 }
2158 closeCurrentTab();
2159 break;
2160
kaiyiza8b6dbb2013-07-29 18:11:22 +08002161 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002162 Object[] params = { new String("persist.debug.browsermonkeytest")};
2163 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002164 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002165 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002166 if (ret != null && ret.equals("enable"))
2167 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002168 if (BrowserConfig.getInstance(getContext())
2169 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2170 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002171 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002172 case R.id.homepage_menu_id:
2173 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002174 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002175 break;
2176
2177 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002178 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002179 break;
2180
2181 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002182 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002183 break;
2184
John Reck2bc80422011-06-30 15:11:49 -07002185 case R.id.save_snapshot_menu_id:
2186 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002187 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002188 createScreenshotAsync(
Pankaj Gargf49e0222015-09-01 12:19:13 -07002189 source,
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002190 new ValueCallback<Bitmap>() {
2191 @Override
2192 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002193 Bitmap bm = cropAndScaleBitmap(bitmap,
2194 getDesiredThumbnailWidth(mActivity),
2195 getDesiredThumbnailHeight(mActivity));
2196 new SaveSnapshotTask(source, bm).execute();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002197 }
2198 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002199 break;
2200
Michael Kolb8233fac2010-10-26 16:08:53 -07002201 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002202 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002203 break;
2204
John Recke1a03a32011-09-14 17:04:16 -07002205 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002206 // passing null to distinguish between
2207 // "go live" button and navigating a web page
2208 // on a snapshot tab
2209 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002210 case R.id.share_page_menu_id:
2211 Tab currentTab = mTabControl.getCurrentTab();
2212 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002213 return false;
2214 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002215 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002216 break;
2217
2218 case R.id.dump_nav_menu_id:
2219 getCurrentTopWebView().debugDump();
2220 break;
2221
Michael Kolb8233fac2010-10-26 16:08:53 -07002222 case R.id.zoom_in_menu_id:
2223 getCurrentTopWebView().zoomIn();
2224 break;
2225
2226 case R.id.zoom_out_menu_id:
2227 getCurrentTopWebView().zoomOut();
2228 break;
2229
2230 case R.id.view_downloads_menu_id:
2231 viewDownloads();
2232 break;
2233
John Reck42229bc2011-08-19 13:26:43 -07002234 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002235 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002236 break;
2237
Ze G Riande2a675c22015-06-03 11:15:24 -07002238 case R.id.reader_mode_menu_id:
2239 toggleReaderMode();
2240 break;
2241
Michael Kolb8233fac2010-10-26 16:08:53 -07002242 case R.id.window_one_menu_id:
2243 case R.id.window_two_menu_id:
2244 case R.id.window_three_menu_id:
2245 case R.id.window_four_menu_id:
2246 case R.id.window_five_menu_id:
2247 case R.id.window_six_menu_id:
2248 case R.id.window_seven_menu_id:
2249 case R.id.window_eight_menu_id:
2250 {
2251 int menuid = item.getItemId();
2252 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2253 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2254 Tab desiredTab = mTabControl.getTab(id);
2255 if (desiredTab != null &&
2256 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002257 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002258 }
2259 break;
2260 }
2261 }
2262 }
2263 break;
2264
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002265 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002266 Bundle bundle = new Bundle();
2267 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2268 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2269 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2270 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2271 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002272 break;
2273
Pankaj Garg49b79252014-11-07 17:33:41 -08002274 case R.id.add_to_homescreen:
2275 final WebView w = getCurrentTopWebView();
2276 final EditText input = new EditText(getContext());
2277 input.setText(w.getTitle());
2278 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002279 .setTitle(getContext().getResources().getString(
2280 R.string.add_to_homescreen))
2281 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002282 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002283 .setPositiveButton(getContext().getResources().getString(
2284 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002285 public void onClick(DialogInterface dialog, int whichButton) {
2286 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2287 getContext(),
2288 w.getUrl(),
2289 input.getText().toString(),
2290 w.getViewportBitmap(),
2291 w.getFavicon()));
2292
2293 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2294 .addCategory(Intent.CATEGORY_HOME));
2295 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002296 .setNegativeButton(getContext().getResources().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07002297 R.string.cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002298 public void onClick(DialogInterface dialog, int whichButton) {
2299 // Do nothing.
2300 }
2301 })
2302 .show();
2303 break;
2304
Michael Kolb8233fac2010-10-26 16:08:53 -07002305 default:
2306 return false;
2307 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 return true;
2309 }
2310
John Reck68234a92012-04-19 15:27:12 -07002311 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2312 implements OnCancelListener {
2313
2314 private Tab mTab;
2315 private Dialog mProgressDialog;
2316 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002317 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002318
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002319 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002320 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002321 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002322 }
2323
2324 @Override
2325 protected void onPreExecute() {
2326 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2327 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2328 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002329 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002330 }
2331
2332 @Override
2333 protected Long doInBackground(Void... params) {
2334 if (!mTab.saveViewState(mValues)) {
2335 return null;
2336 }
2337 if (isCancelled()) {
2338 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2339 File file = mActivity.getFileStreamPath(path);
2340 if (!file.delete()) {
2341 file.deleteOnExit();
2342 }
2343 return null;
2344 }
2345 final ContentResolver cr = mActivity.getContentResolver();
2346 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2347 if (result == null) {
2348 return null;
2349 }
2350 long id = ContentUris.parseId(result);
2351 return id;
2352 }
2353
2354 @Override
2355 protected void onPostExecute(Long id) {
2356 if (isCancelled()) {
2357 return;
2358 }
2359 mProgressDialog.dismiss();
2360 if (id == null) {
2361 Toast.makeText(mActivity, R.string.snapshot_failed,
2362 Toast.LENGTH_SHORT).show();
2363 return;
2364 }
2365 Bundle b = new Bundle();
2366 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2367 mUi.showComboView(ComboViews.Snapshots, b);
2368 }
2369
2370 @Override
2371 public void onCancel(DialogInterface dialog) {
2372 cancel(true);
2373 }
2374 }
2375
Michael Kolb80f75082012-04-10 10:50:06 -07002376 @Override
2377 public void toggleUserAgent() {
2378 WebView web = getCurrentWebView();
2379 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002380 }
2381
Ze G Riande2a675c22015-06-03 11:15:24 -07002382 // This function calls the method in the webview to enable/disable
2383 // the reader mode through the DOM distiller
2384 public void toggleReaderMode() {
2385 Tab t = mTabControl.getCurrentTab();
2386 if (t.isDistilled()) {
2387 closeTab(t);
2388 } else if (t.isDistillable()) {
2389 openTab(t.getDistilledUrl(), false, true, false, t);
2390 }
2391 }
2392
Michael Kolb80f75082012-04-10 10:50:06 -07002393 @Override
2394 public void findOnPage() {
2395 getCurrentTopWebView().showFindDialog(null, true);
2396 }
2397
2398 @Override
2399 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002400 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002401 }
2402
2403 @Override
2404 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002405 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002406 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2407 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002408 }
2409 else {
2410 WebView w = getCurrentTopWebView();
2411 if (w == null)
2412 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002413 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002414 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002415 }
Michael Kolb80f75082012-04-10 10:50:06 -07002416 }
2417
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002418 public boolean goLive(String url) {
2419 if (!getCurrentTab().isSnapshot())
2420 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002421 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002422
2423 if (url == null) { // "go live" button was clicked
2424 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002425 closeTab(t);
2426 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002427 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002428 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002429 return true;
John Recke1a03a32011-09-14 17:04:16 -07002430 }
2431
luxiaolb40014b2013-07-19 10:01:43 +08002432 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002433 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002434 new AlertDialog.Builder(activity)
2435 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002436 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002437 .setMessage(R.string.exit_browser_msg)
2438 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2439 public void onClick(DialogInterface dialog, int which) {
2440 activity.moveTaskToBack(true);
2441 dialog.dismiss();
2442 }
2443 })
2444 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2445 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002446 mCrashRecoveryHandler.clearState(true);
2447 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002448 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002449 dialog.dismiss();
2450 }
2451 })
2452 .show();
2453 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002454
Michael Kolb315d5022011-10-13 12:47:11 -07002455 @Override
2456 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002457 }
2458
John Reck9c35b9c2012-05-30 10:08:50 -07002459 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002460 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002461 // Let the History and Bookmark fragments handle menus they created.
2462 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2463 return false;
2464 }
2465
Michael Kolb8233fac2010-10-26 16:08:53 -07002466 int id = item.getItemId();
2467 boolean result = true;
2468 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002469 // -- Browser context menu
2470 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002471 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002472 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002473 case R.id.copy_link_context_menu_id:
2474 final WebView webView = getCurrentTopWebView();
2475 if (null == webView) {
2476 result = false;
2477 break;
2478 }
2479 final HashMap<String, WebView> hrefMap =
2480 new HashMap<String, WebView>();
2481 hrefMap.put("webview", webView);
2482 final Message msg = mHandler.obtainMessage(
2483 FOCUS_NODE_HREF, id, 0, hrefMap);
2484 webView.requestFocusNodeHref(msg);
2485 break;
2486
2487 default:
2488 // For other context menus
2489 result = onOptionsItemSelected(item);
2490 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002491 return result;
2492 }
2493
2494 /**
2495 * support programmatically opening the context menu
2496 */
2497 public void openContextMenu(View view) {
2498 mActivity.openContextMenu(view);
2499 }
2500
2501 /**
2502 * programmatically open the options menu
2503 */
2504 public void openOptionsMenu() {
2505 mActivity.openOptionsMenu();
2506 }
2507
John Reck9c35b9c2012-05-30 10:08:50 -07002508 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002509 public boolean onMenuOpened(int featureId, Menu menu) {
2510 if (mOptionsMenuOpen) {
2511 if (mConfigChanged) {
2512 // We do not need to make any changes to the state of the
2513 // title bar, since the only thing that happened was a
2514 // change in orientation
2515 mConfigChanged = false;
2516 } else {
2517 if (!mExtendedMenuOpen) {
2518 mExtendedMenuOpen = true;
2519 mUi.onExtendedMenuOpened();
2520 } else {
2521 // Switching the menu back to icon view, so show the
2522 // title bar once again.
2523 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002524 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 }
2526 }
2527 } else {
2528 // The options menu is closed, so open it, and show the title
2529 mOptionsMenuOpen = true;
2530 mConfigChanged = false;
2531 mExtendedMenuOpen = false;
2532 mUi.onOptionsMenuOpened();
2533 }
2534 return true;
2535 }
2536
John Reck9c35b9c2012-05-30 10:08:50 -07002537 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002538 public void onOptionsMenuClosed(Menu menu) {
2539 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002540 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 }
2542
John Reck9c35b9c2012-05-30 10:08:50 -07002543 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002544 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002545 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002546 }
2547
2548 // Helper method for getting the top window.
2549 @Override
2550 public WebView getCurrentTopWebView() {
2551 return mTabControl.getCurrentTopWebView();
2552 }
2553
2554 @Override
2555 public WebView getCurrentWebView() {
2556 return mTabControl.getCurrentWebView();
2557 }
2558
2559 /*
2560 * This method is called as a result of the user selecting the options
2561 * menu to see the download window. It shows the download window on top of
2562 * the current window.
2563 */
2564 void viewDownloads() {
2565 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2566 mActivity.startActivity(intent);
2567 }
2568
John Reck30b065e2011-07-19 10:58:05 -07002569 int getActionModeHeight() {
2570 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2571 new int[] { android.R.attr.actionBarSize });
2572 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2573 actionBarSizeTypedArray.recycle();
2574 return size;
2575 }
2576
Michael Kolb8233fac2010-10-26 16:08:53 -07002577 // action mode
2578
John Reck9c35b9c2012-05-30 10:08:50 -07002579 @Override
2580 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002581 mUi.onActionModeStarted(mode);
2582 mActionMode = mode;
2583 }
2584
2585 /*
2586 * True if a custom ActionMode (i.e. find or select) is in use.
2587 */
2588 @Override
2589 public boolean isInCustomActionMode() {
2590 return mActionMode != null;
2591 }
2592
2593 /*
2594 * End the current ActionMode.
2595 */
2596 @Override
2597 public void endActionMode() {
2598 if (mActionMode != null) {
2599 mActionMode.finish();
2600 }
2601 }
2602
2603 /*
2604 * Called by find and select when they are finished. Replace title bars
2605 * as necessary.
2606 */
John Reck9c35b9c2012-05-30 10:08:50 -07002607 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 public void onActionModeFinished(ActionMode mode) {
2609 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002610 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002611 mActionMode = null;
2612 }
2613
2614 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002615 final Tab tab = getCurrentTab();
2616 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002617 }
2618
2619 // bookmark handling
2620
2621 /**
2622 * add the current page as a bookmark to the given folder id
2623 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002624 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002625 * bookmarked, and if it is, edit that bookmark. If false, and
2626 * the site is already bookmarked, do not attempt to edit the
2627 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002628 */
2629 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002630 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002631 WebView w = getCurrentTopWebView();
2632 if (w == null) {
2633 return null;
2634 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002635 Intent i = new Intent(mActivity,
2636 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002637 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2638 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002639 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002640 if (touchIconUrl != null) {
2641 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2642 WebSettings settings = w.getSettings();
2643 if (settings != null) {
2644 i.putExtra(AddBookmarkPage.USER_AGENT,
2645 settings.getUserAgentString());
2646 }
2647 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002648 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002649 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002650 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002651 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2652 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002653 // Put the dialog at the upper right of the screen, covering the
2654 // star on the title bar.
2655 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002656 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002657 }
2658
2659 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002660 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002661 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002662 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002663 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002664 }
2665
Vivek Sekharb54614f2014-05-01 19:03:37 -07002666 @Override
2667 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2668 boolean capture) {
2669 mUploadHandler = new UploadHandler(this);
2670 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2671 }
2672
Michael Kolb8233fac2010-10-26 16:08:53 -07002673 // thumbnails
2674
2675 /**
2676 * Return the desired width for thumbnail screenshots, which are stored in
2677 * the database, and used on the bookmarks screen.
2678 * @param context Context for finding out the density of the screen.
2679 * @return desired width for thumbnail screenshot.
2680 */
2681 static int getDesiredThumbnailWidth(Context context) {
2682 return context.getResources().getDimensionPixelOffset(
2683 R.dimen.bookmarkThumbnailWidth);
2684 }
2685
2686 /**
2687 * Return the desired height for thumbnail screenshots, which are stored in
2688 * the database, and used on the bookmarks screen.
2689 * @param context Context for finding out the density of the screen.
2690 * @return desired height for thumbnail screenshot.
2691 */
2692 static int getDesiredThumbnailHeight(Context context) {
2693 return context.getResources().getDimensionPixelOffset(
2694 R.dimen.bookmarkThumbnailHeight);
2695 }
2696
Pankaj Gargf49e0222015-09-01 12:19:13 -07002697 void createScreenshotAsync(Tab tab, final ValueCallback<Bitmap> cb) {
2698 if (tab == null) {
2699 cb.onReceiveValue(null);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002700 return;
2701 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002702
2703 tab.capture();
2704
2705 synchronized (mThumbnailCbList) {
2706 mThumbnailCbList.add(cb);
2707 }
2708 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002709
Michael Kolb8233fac2010-10-26 16:08:53 -07002710 private class Copy implements OnMenuItemClickListener {
2711 private CharSequence mText;
2712
John Reck9c35b9c2012-05-30 10:08:50 -07002713 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002714 public boolean onMenuItemClick(MenuItem item) {
2715 copy(mText);
2716 return true;
2717 }
2718
2719 public Copy(CharSequence toCopy) {
2720 mText = toCopy;
2721 }
2722 }
2723
Leon Scroggins63c02662010-11-18 15:16:27 -05002724 private static class Download implements OnMenuItemClickListener {
2725 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002726 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002727 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002728 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002729 private static final String FALLBACK_EXTENSION = "dat";
2730 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002731
John Reck9c35b9c2012-05-30 10:08:50 -07002732 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002733 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002734 if (DataUri.isDataUri(mText)) {
2735 saveDataUri();
2736 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002737 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08002738 null, null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002739 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002740 return true;
2741 }
2742
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002743 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2744 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002745 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002746 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002747 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002748 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002749 }
George Mount387d45d2011-10-07 15:57:53 -07002750
2751 /**
2752 * Treats mText as a data URI and writes its contents to a file
2753 * based on the current time.
2754 */
2755 private void saveDataUri() {
2756 FileOutputStream outputStream = null;
2757 try {
2758 DataUri uri = new DataUri(mText);
2759 File target = getTarget(uri);
2760 outputStream = new FileOutputStream(target);
2761 outputStream.write(uri.getData());
2762 final DownloadManager manager =
2763 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2764 manager.addCompletedDownload(target.getName(),
2765 mActivity.getTitle().toString(), false,
2766 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002767 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002768 } catch (IOException e) {
2769 Log.e(LOGTAG, "Could not save data URL");
2770 } finally {
2771 if (outputStream != null) {
2772 try {
2773 outputStream.close();
2774 } catch (IOException e) {
2775 // ignore close errors
2776 }
2777 }
2778 }
2779 }
2780
2781 /**
2782 * Creates a File based on the current time stamp and uses
2783 * the mime type of the DataUri to get the extension.
2784 */
2785 private File getTarget(DataUri uri) throws IOException {
2786 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002787 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002788 String nameBase = format.format(new Date());
2789 String mimeType = uri.getMimeType();
2790 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2791 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2792 if (extension == null) {
2793 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2794 extension = FALLBACK_EXTENSION;
2795 }
2796 extension = "." + extension; // createTempFile needs the '.'
2797 File targetFile = File.createTempFile(nameBase, extension, dir);
2798 return targetFile;
2799 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002800 }
2801
Cary Clark8974d282010-11-22 10:46:05 -05002802 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002803 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002804
John Reck9c35b9c2012-05-30 10:08:50 -07002805 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002806 public boolean onMenuItemClick(MenuItem item) {
2807 if (mWebView != null) {
2808 return mWebView.selectText();
2809 }
2810 return false;
2811 }
2812
2813 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002814 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002815 }
2816
2817 }
2818
Michael Kolb8233fac2010-10-26 16:08:53 -07002819 /********************** TODO: UI stuff *****************************/
2820
2821 // these methods have been copied, they still need to be cleaned up
2822
2823 /****************** tabs ***************************************************/
2824
2825 // basic tab interactions:
2826
2827 // it is assumed that tabcontrol already knows about the tab
2828 protected void addTab(Tab tab) {
2829 mUi.addTab(tab);
2830 }
2831
2832 protected void removeTab(Tab tab) {
2833 mUi.removeTab(tab);
2834 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002835 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002836 }
2837
Michael Kolbf2055602011-04-09 17:20:03 -07002838 @Override
2839 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002840 // monkey protection against delayed start
2841 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002842
2843 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2844 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002845 mTabControl.setCurrentTab(tab);
2846 // the tab is guaranteed to have a webview after setCurrentTab
2847 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002848
2849
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002850 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002851 //Purge active tabs
2852 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002853 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002854 }
2855
John Reck8bcafc12011-08-29 16:43:02 -07002856 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002857 Tab current = mTabControl.getCurrentTab();
2858 if (current != null
2859 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002860 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002861 }
2862 }
2863
John Reck26b18322011-06-21 13:08:58 -07002864 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002865 //Cancel navscreen request
2866 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002867 // Dismiss the subwindow if applicable.
2868 dismissSubWindow(appTab);
2869 // Since we might kill the WebView, remove it from the
2870 // content view first.
2871 mUi.detachTab(appTab);
2872 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002873 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002874 // TODO: analyze why the remove and add are necessary
2875 mUi.attachTab(appTab);
2876 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002877 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002878 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002879 } else {
2880 // If the tab was the current tab, we have to attach
2881 // it to the view system again.
2882 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002883 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002884 }
2885 }
2886
2887 // Remove the sub window if it exists. Also called by TabControl when the
2888 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002889 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002890 public void dismissSubWindow(Tab tab) {
2891 removeSubWindow(tab);
2892 // dismiss the subwindow. This will destroy the WebView.
2893 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002894 WebView wv = getCurrentTopWebView();
2895 if (wv != null) {
2896 wv.requestFocus();
2897 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002898 }
2899
2900 @Override
2901 public void removeSubWindow(Tab t) {
2902 if (t.getSubWebView() != null) {
2903 mUi.removeSubWindow(t.getSubViewContainer());
2904 }
2905 }
2906
2907 @Override
2908 public void attachSubWindow(Tab tab) {
2909 if (tab.getSubWebView() != null) {
2910 mUi.attachSubWindow(tab.getSubViewContainer());
2911 getCurrentTopWebView().requestFocus();
2912 }
2913 }
2914
Mathew Inwood29721c22011-06-29 17:55:24 +01002915 private Tab showPreloadedTab(final UrlData urlData) {
2916 if (!urlData.isPreloaded()) {
2917 return null;
2918 }
2919 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2920 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2921 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002922 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002923 // Could not submit query. Fallback to regular tab creation
2924 tabControl.destroy();
2925 return null;
2926 }
2927 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002928 // check tab count and make room for new tab
2929 if (!mTabControl.canCreateNewTab()) {
2930 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2931 if (leastUsed != null) {
2932 closeTab(leastUsed);
2933 }
2934 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002935 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002936 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002937 mTabControl.addPreloadedTab(t);
2938 addTab(t);
2939 setActiveTab(t);
2940 return t;
2941 }
2942
Michael Kolb7bcafde2011-05-09 13:55:59 -07002943 // open a non inconito tab with the given url data
2944 // and set as active tab
2945 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002946 Tab tab = showPreloadedTab(urlData);
2947 if (tab == null) {
2948 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002949 if ((tab != null) && !urlData.isEmpty()) {
2950 loadUrlDataIn(tab, urlData);
2951 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002952 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002953 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002954 }
2955
Michael Kolb843510f2010-12-09 10:51:49 -08002956 @Override
2957 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002958 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002959 }
2960
Michael Kolb8233fac2010-10-26 16:08:53 -07002961 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002962 public Tab openIncognitoTab() {
2963 return openTab(INCOGNITO_URI, true, true, false);
2964 }
2965
2966 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002967 public Tab openTab(String url, boolean incognito, boolean setActive,
2968 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002969 return openTab(url, incognito, setActive, useCurrent, null);
2970 }
2971
2972 @Override
2973 public Tab openTab(String url, Tab parent, boolean setActive,
2974 boolean useCurrent) {
2975 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2976 setActive, useCurrent, parent);
2977 }
2978
2979 public Tab openTab(String url, boolean incognito, boolean setActive,
2980 boolean useCurrent, Tab parent) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002981 boolean change_tabs = false;
2982 if (setActive) {
2983 Tab currentTab = mTabControl.getCurrentTab();
2984 if (currentTab != null) {
2985 change_tabs = setActive;
2986 setActive = false;
Sagar Dhawan9a1d8e72015-09-11 17:14:38 -07002987 if (mUi instanceof PhoneUi)
2988 currentTab.capture();
Pankaj Gargf49e0222015-09-01 12:19:13 -07002989 }
2990 }
2991
Michael Kolb7bcafde2011-05-09 13:55:59 -07002992 Tab tab = createNewTab(incognito, setActive, useCurrent);
2993 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002994 if (parent instanceof SnapshotTab) {
2995 addTab(tab);
2996 if (setActive)
2997 setActiveTab(tab);
2998 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002999 parent.addChildTab(tab);
3000 }
Michael Kolb519d2282011-05-09 17:03:19 -07003001 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003002 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003003 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003004 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07003005
3006 if (change_tabs) {
Sagar Dhawan20b1deb2015-10-12 11:13:18 -07003007 setActiveTab(tab);
Pankaj Gargf49e0222015-09-01 12:19:13 -07003008 synchronized (mThumbnailCbList) {
3009 startCaptureTimer();
3010 mLatestCreatedTab = tab;
3011 mThumbnailCbList.add(new ValueCallback<Bitmap>() {
3012 @Override
3013 public void onReceiveValue(Bitmap bitmap) {
3014 synchronized (mThumbnailCbList) {
3015 if (mLatestCreatedTab != null) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07003016 mLatestCreatedTab = null;
3017 }
3018 stopCaptureTimer();
3019 }
3020 }
3021 });
3022 }
3023 }
3024
Michael Kolb7bcafde2011-05-09 13:55:59 -07003025 return tab;
3026 }
3027
Pankaj Gargf49e0222015-09-01 12:19:13 -07003028 private void startCaptureTimer() {
3029 mCaptureTimer = new CountDownTimer(mCaptureMaxWaitMS, mCaptureMaxWaitMS) {
3030 @Override
3031 public void onTick(long millisUntilFinished) {
3032 // Do nothing
3033 }
3034
3035 @Override
3036 public void onFinish() {
3037 synchronized (mThumbnailCbList) {
3038 Log.e(LOGTAG, "Screen capture timed out while opening new tab");
3039 if (mLatestCreatedTab != null) {
3040 setActiveTab(mLatestCreatedTab);
3041 mLatestCreatedTab = null;
3042 }
3043 }
3044 }
3045 }.start();
3046 }
3047
3048 private void stopCaptureTimer() {
3049 if (mCaptureTimer != null) {
3050 mCaptureTimer.cancel();
3051 mCaptureTimer = null;
3052 }
3053 }
3054
3055
Michael Kolb7bcafde2011-05-09 13:55:59 -07003056 // this method will attempt to create a new tab
3057 // incognito: private browsing tab
3058 // setActive: ste tab as current tab
3059 // useCurrent: if no new tab can be created, return current tab
3060 private Tab createNewTab(boolean incognito, boolean setActive,
3061 boolean useCurrent) {
3062 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07003063 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
3064 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003065 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003066 if (mTabControl.canCreateNewTab()) {
3067 tab = mTabControl.createNewTab(incognito, !setActive);
3068 addTab(tab);
3069 if (setActive) {
3070 setActiveTab(tab);
3071 } else {
3072 tab.pause();
3073 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003074 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003075 if (useCurrent) {
3076 tab = mTabControl.getCurrentTab();
3077 reuseTab(tab, null);
3078 } else {
3079 mUi.showMaxTabsWarning();
3080 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003081 }
3082 }
3083 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003084 }
3085
John Reck2bc80422011-06-30 15:11:49 -07003086 @Override
3087 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3088 SnapshotTab tab = null;
3089 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07003090 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07003091 addTab(tab);
3092 if (setActive) {
3093 setActiveTab(tab);
3094 }
3095 } else {
3096 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003097 }
3098 return tab;
3099 }
3100
Michael Kolb8233fac2010-10-26 16:08:53 -07003101 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003102 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003103 * @return boolean True if we successfully switched to a different tab. If
3104 * the indexth tab is null, or if that tab is the same as
3105 * the current one, return false.
3106 */
3107 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003108 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003109 Tab currentTab = mTabControl.getCurrentTab();
3110 if (tab == null || tab == currentTab) {
3111 return false;
3112 }
3113 setActiveTab(tab);
3114 return true;
3115 }
3116
3117 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003118 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003119 closeCurrentTab(false);
3120 }
3121
3122 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003123 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003124 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003125 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003126 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003127 return;
3128 }
Michael Kolbc831b632011-05-11 09:30:34 -07003129 final Tab current = mTabControl.getCurrentTab();
3130 final int pos = mTabControl.getCurrentPosition();
3131 Tab newTab = current.getParent();
3132 if (newTab == null) {
3133 newTab = mTabControl.getTab(pos + 1);
3134 if (newTab == null) {
3135 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003136 }
3137 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003138 if (andQuit) {
3139 mTabControl.setCurrentTab(newTab);
3140 closeTab(current);
3141 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003142 // Close window
3143 closeTab(current);
3144 }
3145 }
3146
3147 /**
3148 * Close the tab, remove its associated title bar, and adjust mTabControl's
3149 * current tab to a valid value.
3150 */
3151 @Override
3152 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003153 if (tab == mTabControl.getCurrentTab()) {
3154 closeCurrentTab();
3155 } else {
3156 removeTab(tab);
3157 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003158 }
3159
Afzal Najamd4e33312012-04-26 01:54:01 -04003160 /**
3161 * Close all tabs except the current one
3162 */
3163 @Override
3164 public void closeOtherTabs() {
3165 int inactiveTabs = mTabControl.getTabCount() - 1;
3166 for (int i = inactiveTabs; i >= 0; i--) {
3167 Tab tab = mTabControl.getTab(i);
3168 if (tab != mTabControl.getCurrentTab()) {
3169 removeTab(tab);
3170 }
3171 }
3172 }
3173
Michael Kolb8233fac2010-10-26 16:08:53 -07003174 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003175 protected void loadUrlFromContext(String url) {
3176 Tab tab = getCurrentTab();
3177 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003178 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003179 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003180 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003181 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003182 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003183 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003184 }
3185 }
3186 }
3187
3188 /**
3189 * Load the URL into the given WebView and update the title bar
3190 * to reflect the new load. Call this instead of WebView.loadUrl
3191 * directly.
3192 * @param view The WebView used to load url.
3193 * @param url The URL to load.
3194 */
John Reck71e51422011-07-01 16:49:28 -07003195 @Override
3196 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003197 loadUrl(tab, url, null);
3198 }
3199
3200 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3201 if (tab != null) {
3202 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003203 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003204 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003205 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003206 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003207 }
3208
3209 /**
3210 * Load UrlData into a Tab and update the title bar to reflect the new
3211 * load. Call this instead of UrlData.loadIn directly.
3212 * @param t The Tab used to load.
3213 * @param data The UrlData being loaded.
3214 */
3215 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003216 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003217 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003218 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003219 } else {
John Reck38b39652012-06-05 09:22:59 -07003220 if (t != null && data.mDisableUrlOverride) {
3221 t.disableUrlOverridingForLoad();
3222 }
John Reck26b18322011-06-21 13:08:58 -07003223 loadUrl(t, data.mUrl, data.mHeaders);
3224 }
3225 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003226 }
3227
John Reck30c714c2010-12-16 17:30:34 -08003228 @Override
3229 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003230 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003231 //In case of tab can go back (aka tab has navigation entry) do nothing
3232 //else just load homepage in current tab.
3233 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003234 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003235 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003236 }
3237
3238 void goBackOnePageOrQuit() {
3239 Tab current = mTabControl.getCurrentTab();
3240 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003241 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3242 showExitDialog(mActivity);
3243 } else {
3244 /*
3245 * Instead of finishing the activity, simply push this to the back
3246 * of the stack and let ActivityManager to choose the foreground
3247 * activity. As BrowserActivity is singleTask, it will be always the
3248 * root of the task. So we can use either true or false for
3249 * moveTaskToBack().
3250 */
3251 mActivity.moveTaskToBack(true);
3252 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003253 return;
3254 }
John Reckef654f12011-07-12 16:42:08 -07003255 if (current.canGoBack()) {
3256 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003257 } else {
3258 // Check to see if we are closing a window that was created by
3259 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003260 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003261 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003262 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003263 // Now we close the other tab
3264 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003265 } else if (BrowserConfig.getInstance(getContext())
3266 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3267 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003268 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003269 /*
3270 * Instead of finishing the activity, simply push this to the back
3271 * of the stack and let ActivityManager to choose the foreground
3272 * activity. As BrowserActivity is singleTask, it will be always the
3273 * root of the task. So we can use either true or false for
3274 * moveTaskToBack().
3275 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003276 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003277 }
3278 }
3279 }
3280
3281 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003282 * helper method for key handler
3283 * returns the current tab if it can't advance
3284 */
Michael Kolbc831b632011-05-11 09:30:34 -07003285 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003286 int pos = mTabControl.getCurrentPosition() + 1;
3287 if (pos >= mTabControl.getTabCount()) {
3288 pos = 0;
3289 }
3290 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003291 }
3292
3293 /**
3294 * helper method for key handler
3295 * returns the current tab if it can't advance
3296 */
Michael Kolbc831b632011-05-11 09:30:34 -07003297 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003298 int pos = mTabControl.getCurrentPosition() - 1;
3299 if ( pos < 0) {
3300 pos = mTabControl.getTabCount() - 1;
3301 }
3302 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003303 }
3304
John Reckbcef87f2012-02-03 14:58:34 -08003305 boolean isMenuOrCtrlKey(int keyCode) {
3306 return (KeyEvent.KEYCODE_MENU == keyCode)
3307 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3308 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3309 }
3310
Michael Kolb0035fad2011-03-14 13:25:28 -07003311 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003312 * handle key events in browser
3313 *
3314 * @param keyCode
3315 * @param event
3316 * @return true if handled, false to pass to super
3317 */
John Reck9c35b9c2012-05-30 10:08:50 -07003318 @Override
3319 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003320 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3321 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003322 if (!mUi.isComboViewShowing()) {
3323 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3324 true, false);
3325 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003326 return true;
3327 }
3328
Cary Clark160bbb92011-01-10 11:17:07 -05003329 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003330 // Even if MENU is already held down, we need to call to super to open
3331 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003332 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003333 mMenuIsDown = true;
3334 return false;
3335 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003336
Cary Clark8ff8c662010-12-29 15:03:05 -05003337 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003338 Tab tab = getCurrentTab();
3339 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003340
Cary Clark160bbb92011-01-10 11:17:07 -05003341 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3342 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003343
Michael Kolb8233fac2010-10-26 16:08:53 -07003344 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003345 case KeyEvent.KEYCODE_TAB:
3346 if (event.isCtrlPressed()) {
3347 if (event.isShiftPressed()) {
3348 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003349 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003350 } else {
3351 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003352 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003353 }
3354 return true;
3355 }
3356 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003357 case KeyEvent.KEYCODE_SPACE:
3358 // WebView/WebTextView handle the keys in the KeyDown. As
3359 // the Activity's shortcut keys are only handled when WebView
3360 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003361 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003362 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003363 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003364 pageDown();
3365 }
3366 return true;
3367 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003368 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003369 event.startTracking();
3370 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003371 case KeyEvent.KEYCODE_FORWARD:
3372 if (!noModifiers) break;
3373 tab.goForward();
3374 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003375 case KeyEvent.KEYCODE_DPAD_LEFT:
3376 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003377 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003378 return true;
3379 }
3380 break;
3381 case KeyEvent.KEYCODE_DPAD_RIGHT:
3382 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003383 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003384 return true;
3385 }
3386 break;
3387 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003388 if (ctrl) {
3389 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003390 return true;
3391 }
3392 break;
Michael Kolba4183062011-01-16 10:43:21 -08003393// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003394 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003395 if (ctrl ) {
3396 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003397 return true;
3398 }
3399 break;
Michael Kolba4183062011-01-16 10:43:21 -08003400// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003401// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003402// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003403// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003404// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003405// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003406// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003407// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003408// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003409// case KeyEvent.KEYCODE_M: // unused
3410// case KeyEvent.KEYCODE_N: // in Chrome: new window
3411// case KeyEvent.KEYCODE_O: // in Chrome: open file
3412// case KeyEvent.KEYCODE_P: // in Chrome: print page
3413// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003414// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003415// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3416 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003417 // we can't use the ctrl/shift flags, they check for
3418 // exclusive use of a modifier
3419 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003420 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003421 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003422 } else {
3423 openTabToHomePage();
3424 }
3425 return true;
3426 }
3427 break;
3428// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3429// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003430// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003431// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3432// case KeyEvent.KEYCODE_Y: // unused
3433// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003434 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003435 // it is a regular key and webview is not null
3436 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003437 }
3438
John Reck9c35b9c2012-05-30 10:08:50 -07003439 @Override
3440 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003441 switch(keyCode) {
3442 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003443 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003444 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003445 return true;
3446 }
3447 break;
3448 }
3449 return false;
3450 }
3451
John Reck9c35b9c2012-05-30 10:08:50 -07003452 @Override
3453 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003454 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003455 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003456 if (KeyEvent.KEYCODE_MENU == keyCode
3457 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003458 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003459 }
3460 }
Cary Clark160bbb92011-01-10 11:17:07 -05003461 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003462 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003463 case KeyEvent.KEYCODE_BACK:
3464 if (event.isTracking() && !event.isCanceled()) {
3465 onBackKey();
3466 return true;
3467 }
3468 break;
3469 }
3470 return false;
3471 }
3472
3473 public boolean isMenuDown() {
3474 return mMenuIsDown;
3475 }
3476
John Reck9c35b9c2012-05-30 10:08:50 -07003477 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003478 public void setupAutoFill(Message message) {
3479 // Open the settings activity at the AutoFill profile fragment so that
3480 // the user can create a new profile. When they return, we will dispatch
3481 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003482 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003483 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3484 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003485 }
John Reckb3417f02011-01-14 11:01:05 -08003486
John Reck9c35b9c2012-05-30 10:08:50 -07003487 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003488 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003489 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003490 return true;
3491 }
3492
John Reck1cf4b792011-07-26 10:22:22 -07003493 @Override
3494 public boolean shouldCaptureThumbnails() {
3495 return mUi.shouldCaptureThumbnails();
3496 }
3497
Michael Kolbc3af0672011-08-09 10:24:41 -07003498 @Override
Pankaj Gargf49e0222015-09-01 12:19:13 -07003499 public void onThumbnailCapture(Bitmap bm) {
3500 synchronized (mThumbnailCbList) {
3501 int num_entries = mThumbnailCbList.size();
3502 while (num_entries > 0){
3503 num_entries--;
3504 ValueCallback<Bitmap> cb = mThumbnailCbList.get(num_entries);
3505 cb.onReceiveValue(bm);
3506 }
3507 mThumbnailCbList.clear();
3508 }
3509 }
3510
3511 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003512 public boolean supportsVoice() {
3513 PackageManager pm = mActivity.getPackageManager();
3514 List activities = pm.queryIntentActivities(new Intent(
3515 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3516 return activities.size() != 0;
3517 }
3518
3519 @Override
3520 public void startVoiceRecognizer() {
3521 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003522 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003523 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3524 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3525 mActivity.startActivityForResult(voice, VOICE_RESULT);
3526 }
3527
Pankaj Garg7b279f62014-08-12 14:47:18 -07003528 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003529 if (mLevel == level)
3530 return;
3531 mLevel = level;
3532 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003533 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3534 lp.dimAmount = level;
3535 mActivity.getWindow().setAttributes(lp);
3536 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3537 } else {
3538 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3539 }
3540 }
3541
Michael Kolb0b129122012-06-04 16:31:58 -07003542 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003543 public void setBlockEvents(boolean block) {
3544 mBlockEvents = block;
3545 }
3546
John Reck9c35b9c2012-05-30 10:08:50 -07003547 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003548 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003549 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003550 }
3551
John Reck9c35b9c2012-05-30 10:08:50 -07003552 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003553 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003554 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003555 }
3556
John Reck9c35b9c2012-05-30 10:08:50 -07003557 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003558 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003559 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003560 }
3561
John Reck9c35b9c2012-05-30 10:08:50 -07003562 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003563 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003564 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003565 }
3566
John Reck9c35b9c2012-05-30 10:08:50 -07003567 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003568 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003569 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003570 }
3571
Pankaj Garg49b79252014-11-07 17:33:41 -08003572 @Override
3573 public boolean shouldShowAppMenu() {
3574 return true;
3575 }
3576
3577 @Override
3578 public int getMenuThemeResourceId() {
3579 return R.style.OverflowMenuTheme;
3580 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003581}