blob: d70f5856beb57582ced13f73ab1197211879509b [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
Dave Tharp851e8d52015-04-26 14:58:18 -07004 * Copyright (c) 2015 The Linux Foundation, All rights reserved.
Site Maoabb7bd32015-03-20 15:34:02 -07005 *
Michael Kolb8233fac2010-10-26 16:08:53 -07006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Bijan Amirzada41242f22014-03-21 12:12:18 -070019package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070020
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080022
luxiaolb40014b2013-07-19 10:01:43 +080023import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070025import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070026import android.app.ProgressDialog;
Vivek Sekharce2a4832014-03-26 13:26:53 -070027import android.content.Context;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070030import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070031import android.content.ContentValues;
John Reck68234a92012-04-19 15:27:12 -070032import android.content.DialogInterface;
33import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.content.Intent;
Site Maoabb7bd32015-03-20 15:34:02 -070035import android.content.IntentFilter;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
38import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070039import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050040import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.database.Cursor;
42import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080044import android.graphics.BitmapFactory;
Tarun Nainanif5563b62015-01-21 18:52:59 -080045import android.media.AudioManager;
kaiyiza016da12013-08-26 17:50:22 +080046import android.net.ConnectivityManager;
47import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.net.Uri;
kaiyiza016da12013-08-26 17:50:22 +080049import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080050import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.os.AsyncTask;
Tarun Nainanif5563b62015-01-21 18:52:59 -080052import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.os.Bundle;
Pankaj Gargf49e0222015-09-01 12:19:13 -070054import android.os.CountDownTimer;
George Mount387d45d2011-10-07 15:57:53 -070055import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.os.Handler;
57import android.os.Message;
58import android.os.PowerManager;
59import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.provider.ContactsContract;
61import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080062import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070063import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070064import android.text.TextUtils;
65import android.util.Log;
66import android.view.ActionMode;
67import android.view.ContextMenu;
68import android.view.ContextMenu.ContextMenuInfo;
69import android.view.Gravity;
70import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070071import android.view.Menu;
72import android.view.MenuInflater;
73import android.view.MenuItem;
74import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070075import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070076import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070077import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070078import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080081import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import org.codeaurora.swe.CookieManager;
85import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080086import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.HttpAuthHandler;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080088import org.codeaurora.swe.WebSettings;
89import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070090import org.codeaurora.swe.WebBackForwardList;
91import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080092
Bijan Amirzada41242f22014-03-21 12:12:18 -070093import com.android.browser.IntentHandler.UrlData;
94import com.android.browser.UI.ComboViews;
Dave Tharp13f3f7c2015-07-16 14:58:51 -070095import com.android.browser.mdm.DownloadDirRestriction;
Dave Tharp9a082b12015-06-19 08:46:57 -070096import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -070097import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -070098import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -070099import com.android.browser.mynavigation.AddMyNavigationPage;
100import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700101import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700102import com.android.browser.platformsupport.BrowserContract;
Pankaj Garg18902562014-12-05 16:18:51 -0800103import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700104import com.android.browser.provider.BrowserProvider2.Thumbnails;
105import com.android.browser.provider.SnapshotProvider.Snapshots;
106import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800107import com.android.browser.appmenu.AppMenuHandler;
108import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700109
Michael Kolb8233fac2010-10-26 16:08:53 -0700110import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700111import java.io.File;
112import java.io.FileOutputStream;
113import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700115import java.text.DateFormat;
116import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700117import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700118import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700119import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800120import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200121import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700122import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123
124/**
125 * Controller for browser
126 */
127public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800128 implements WebViewController, UiController, ActivityController,
129 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
131 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800132 private static final String SEND_APP_ID_EXTRA =
133 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700134 public static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800135 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
136 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Pankaj Garg75273bb2015-08-20 11:43:49 -0700137 public static final String EXTRA_UPDATED_URLS = "updated_urls";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800138
Vivek Sekharb54614f2014-05-01 19:03:37 -0700139 // Remind switch to data connection if wifi is unavailable
140 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800141
Michael Kolb8233fac2010-10-26 16:08:53 -0700142 // public message ids
143 public final static int LOAD_URL = 1001;
144 public final static int STOP_LOAD = 1002;
145
146 // Message Ids
147 private static final int FOCUS_NODE_HREF = 102;
148 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800149 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800152 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700153
154 private static final int EMPTY_MENU = -1;
155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700157 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 final static int PREFERENCES_PAGE = 3;
159 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700161 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800162 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000163
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
165
166 // As the ids are dynamically created, we can't guarantee that they will
167 // be in sequence, so this static array maps ids to a window number.
168 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
169 { R.id.window_one_menu_id, R.id.window_two_menu_id,
170 R.id.window_three_menu_id, R.id.window_four_menu_id,
171 R.id.window_five_menu_id, R.id.window_six_menu_id,
172 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
173
174 // "source" parameter for Google search through search key
175 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
176 // "source" parameter for Google search through simplily type
177 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
178
George Mount387d45d2011-10-07 15:57:53 -0700179 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700180 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
181
John Reckd7dd9b22011-08-30 09:18:29 -0700182 // A bitmap that is re-used in createScreenshot as scratch space
183 private static Bitmap sThumbnailBitmap;
184
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 private Activity mActivity;
186 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700187 private HomepageHandler mHomepageHandler;
188 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private BrowserSettings mSettings;
190 private WebViewFactory mFactory;
191
192 private WakeLock mWakeLock;
193
194 private UrlHandler mUrlHandler;
195 private UploadHandler mUploadHandler;
196 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 private NetworkStateHandler mNetworkHandler;
198
Ben Murdoch8029a772010-11-16 11:58:21 +0000199 private Message mAutoFillSetupMessage;
200
kaiyiza016da12013-08-26 17:50:22 +0800201 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700202
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 // FIXME, temp address onPrepareMenu performance problem.
204 // When we move everything out of view, we should rewrite this.
205 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700206 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 private int mOldMenuState = EMPTY_MENU;
208 private Menu mCachedMenu;
209
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 private boolean mMenuIsDown;
211
Pankaj Garg49b79252014-11-07 17:33:41 -0800212 private boolean mWasInPageLoad = false;
213 private AppMenuHandler mAppMenuHandler;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 // For select and find, we keep track of the ActionMode so that
216 // finish() can be called as desired.
217 private ActionMode mActionMode;
218
219 /**
220 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
221 * of whether the configuration has changed. The first onMenuOpened call
222 * after a configuration change is simply a reopening of the same menu
223 * (i.e. mIconView did not change).
224 */
225 private boolean mConfigChanged;
226
227 /**
228 * Keeps track of whether the options menu is open. This is important in
229 * determining whether to show or hide the title bar overlay
230 */
231 private boolean mOptionsMenuOpen;
232
233 /**
234 * Whether or not the options menu is in its bigger, popup menu form. When
235 * true, we want the title bar overlay to be gone. When false, we do not.
236 * Only meaningful if mOptionsMenuOpen is true.
237 */
238 private boolean mExtendedMenuOpen;
239
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 private boolean mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700241 private boolean mActivityStopped = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 private boolean mLoadStopped;
243
244 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500245 // Checks to see when the bookmarks database has changed, and updates the
246 // Tabs' notion of whether they represent bookmarked sites.
247 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700248 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700249
Michael Kolbc3af0672011-08-09 10:24:41 -0700250 private boolean mBlockEvents;
251
Michael Kolb0b129122012-06-04 16:31:58 -0700252 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800253 private boolean mUpdateMyNavThumbnail;
254 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800255 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800256 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700257 private PowerConnectionReceiver mLowPowerReceiver;
258 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700259
Pankaj Garg634bf432015-07-13 09:54:21 -0700260 private boolean mCurrentPageBookmarked;
261
Pankaj Gargf49e0222015-09-01 12:19:13 -0700262 private Tab mLatestCreatedTab;
263
264 private List<ValueCallback> mThumbnailCbList;
265
266 private CountDownTimer mCaptureTimer;
267 private static final int mCaptureMaxWaitMS = 200;
268
George Mount3636d0a2011-11-21 09:08:21 -0800269 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 mActivity = browser;
271 mSettings = BrowserSettings.getInstance();
272 mTabControl = new TabControl(this);
273 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700274 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800275 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700276 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700277
278 mUrlHandler = new UrlHandler(this);
279 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700280
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500282 mBookmarksObserver = new ContentObserver(mHandler) {
283 @Override
284 public void onChange(boolean selfChange) {
285 int size = mTabControl.getTabCount();
286 for (int i = 0; i < size; i++) {
287 mTabControl.getTab(i).updateBookmarkedStatus();
288 }
289 }
290
291 };
292 browser.getContentResolver().registerContentObserver(
293 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700294
295 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700296 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800297 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Pankaj Gargf49e0222015-09-01 12:19:13 -0700298 mThumbnailCbList = new ArrayList<ValueCallback>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700299 }
300
John Reck9c35b9c2012-05-30 10:08:50 -0700301 @Override
302 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700303 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800304 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800305 // mCrashRecoverHandler has any previously saved state.
306 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700307 }
308
George Mount3636d0a2011-11-21 09:08:21 -0800309 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800310 // we dont want to ever recover incognito tabs
311 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700312
Patrick Scott7d50a932011-02-04 09:27:26 -0500313 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700314 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500315 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000316
Michael Kolbc831b632011-05-11 09:30:34 -0700317 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500318 // Not able to restore so we go ahead and clear session cookies. We
319 // must do this before trying to login the user as we don't want to
320 // clear any session cookies set during login.
321 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700322 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800323 if (intent == null) {
324 // This won't happen under common scenarios. The icicle is
325 // not null, but there aren't any tabs to restore.
326 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700327 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800328 final Bundle extra = intent.getExtras();
329 // Create an initial tab.
330 // If the intent is ACTION_VIEW and data is not null, the Browser is
331 // invoked to view the content by another application. In this case,
332 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800333 UrlData urlData = null;
334 if (intent.getData() != null
335 && Intent.ACTION_VIEW.equals(intent.getAction())
336 && intent.getData().toString().startsWith("content://")) {
337 urlData = new UrlData(intent.getData().toString());
338 } else {
339 urlData = IntentHandler.getUrlDataFromIntent(intent);
340 }
George Mount3636d0a2011-11-21 09:08:21 -0800341 Tab t = null;
342 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700343 String landingPage = mActivity.getResources().getString(
344 R.string.def_landing_page);
345 if (!landingPage.isEmpty()) {
346 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800347 } else {
348 t = openTabToHomePage();
349 }
George Mount3636d0a2011-11-21 09:08:21 -0800350 } else {
351 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700352 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800353 }
354 if (t != null) {
355 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
356 }
357 WebView webView = t.getWebView();
358 if (extra != null) {
359 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
360 if (scale > 0 && scale <= 1000) {
361 webView.setInitialScale(scale);
362 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 }
364 }
John Reckd8c74522011-06-14 08:45:00 -0700365 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700367 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500368 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700369 List<Tab> tabs = mTabControl.getTabs();
370 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700371
John Reck1cf4b792011-07-26 10:22:22 -0700372 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700373 //handle restored pages that may require a JS interface
374 if (t.getWebView() != null) {
375 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
376 if (backForwardList != null) {
377 for (int i = 0; i < backForwardList.getSize(); i++) {
378 WebHistoryItem item = backForwardList.getItemAtIndex(i);
379 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
380 }
381 }
382 }
John Reck1cf4b792011-07-26 10:22:22 -0700383 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700384 if (t != mTabControl.getCurrentTab()) {
385 t.pause();
386 }
John Reck1cf4b792011-07-26 10:22:22 -0700387 }
388 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700389 if (tabs.size() == 0) {
390 openTabToHomePage();
391 }
John Reck1cf4b792011-07-26 10:22:22 -0700392 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 // TabControl.restoreState() will create a new tab even if
394 // restoring the state fails.
395 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800396 // Intent is non-null when framework thinks the browser should be
397 // launching with a new intent (icicle is null).
398 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700399 mIntentHandler.onNewIntent(intent);
400 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700403 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800404 if (jsFlags.trim().length() != 0) {
405 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700406 }
George Mount3636d0a2011-11-21 09:08:21 -0800407 if (intent != null
408 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700409 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800410 }
Site Maoabb7bd32015-03-20 15:34:02 -0700411 mLowPowerReceiver = new PowerConnectionReceiver();
412 mPowerChangeReceiver = new PowerConnectionReceiver();
413
414 //always track the android framework's power save mode
415 IntentFilter filter = new IntentFilter();
416 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
417 // Power save mode only exists in Lollipop and above
418 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
419 }
420 filter.addAction(Intent.ACTION_BATTERY_OKAY);
421 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700422 }
423
John Reck1cf4b792011-07-26 10:22:22 -0700424 private static class PruneThumbnails implements Runnable {
425 private Context mContext;
426 private List<Long> mIds;
427
428 PruneThumbnails(Context context, List<Long> preserveIds) {
429 mContext = context.getApplicationContext();
430 mIds = preserveIds;
431 }
432
433 @Override
434 public void run() {
435 ContentResolver cr = mContext.getContentResolver();
436 if (mIds == null || mIds.size() == 0) {
437 cr.delete(Thumbnails.CONTENT_URI, null, null);
438 } else {
439 int length = mIds.size();
440 StringBuilder where = new StringBuilder();
441 where.append(Thumbnails._ID);
442 where.append(" not in (");
443 for (int i = 0; i < length; i++) {
444 where.append(mIds.get(i));
445 if (i < (length - 1)) {
446 where.append(",");
447 }
448 }
449 where.append(")");
450 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
451 }
452 }
453
454 }
455
Michael Kolb1514bb72010-11-22 09:11:48 -0800456 @Override
457 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700458 return mFactory;
459 }
460
461 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800462 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700463 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700464 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800465 }
466
467 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800468 public void createSubWindow(Tab tab) {
469 endActionMode();
470 WebView mainView = tab.getWebView();
471 WebView subView = mFactory.createWebView((mainView == null)
472 ? false
473 : mainView.isPrivateBrowsingEnabled());
474 mUi.createSubWindow(tab, subView);
475 }
476
477 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700478 public Context getContext() {
479 return mActivity;
480 }
481
482 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700483 public Activity getActivity() {
484 return mActivity;
485 }
486
487 void setUi(UI ui) {
488 mUi = ui;
489 }
490
Michael Kolbaf63dba2012-05-16 12:58:05 -0700491 @Override
492 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 return mSettings;
494 }
495
496 IntentHandler getIntentHandler() {
497 return mIntentHandler;
498 }
499
500 @Override
501 public UI getUi() {
502 return mUi;
503 }
504
505 int getMaxTabs() {
506 return mActivity.getResources().getInteger(R.integer.max_tabs);
507 }
508
509 @Override
510 public TabControl getTabControl() {
511 return mTabControl;
512 }
513
Michael Kolb1bf23132010-11-19 12:55:12 -0800514 @Override
515 public List<Tab> getTabs() {
516 return mTabControl.getTabs();
517 }
518
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 private void startHandler() {
520 mHandler = new Handler() {
521
522 @Override
523 public void handleMessage(Message msg) {
524 switch (msg.what) {
525 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700526 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800528 case UNKNOWN_TYPE_MSG:
529 HashMap unknownTypeMap = (HashMap) msg.obj;
530 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
531 /*
532 * When the context menu is shown to the user
533 * we need to assure that its happening on the current webview
534 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
535 */
536 if (getCurrentWebView() != viewForUnknownType)
537 break;
538
539 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800540 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800541 WebView.HitTestResult result = new WebView.HitTestResult();
542
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800543 // Prevent unnecessary calls to context menu
544 // if url and image src are null
545 if (unknown_type_src == null && unknown_type_url == null)
546 break;
547
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800548 //setting the HitTestResult with new RESULT TYPE
549 if (!TextUtils.isEmpty(unknown_type_src)) {
550 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
551 result.setExtra(unknown_type_src);
552 } else {
553 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
554 result.setExtra("about:blank");
555 }
556
557 mResult = result;
558 openContextMenu(viewForUnknownType);
559 mResult = null;
560
561 break;
562
Michael Kolb8233fac2010-10-26 16:08:53 -0700563 case FOCUS_NODE_HREF:
564 {
565 String url = (String) msg.getData().get("url");
566 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500567 String src = (String) msg.getData().get("src");
568 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700569 if (TextUtils.isEmpty(url)) {
570 break;
571 }
572 HashMap focusNodeMap = (HashMap) msg.obj;
573 WebView view = (WebView) focusNodeMap.get("webview");
574 // Only apply the action if the top window did not change.
575 if (getCurrentTopWebView() != view) {
576 break;
577 }
578 switch (msg.arg1) {
579 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700580 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500582 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700583 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500584 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500585 case R.id.open_newtab_context_menu_id:
586 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700587 openTab(url, parent,
588 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500589 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 case R.id.copy_link_context_menu_id:
591 copy(url);
592 break;
593 case R.id.save_link_context_menu_id:
594 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500595 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800596 mActivity, url, view.getSettings().getUserAgentString(),
Pankaj Garg5762b362015-11-02 07:57:06 -0800597 null, null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700599 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700600 if(title == null || title == "")
601 title = url;
602
603 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
604 //SWE TODO: No thumbnail support for the url obtained via
605 //browser context menu as its not loaded in webview.
606 if (bookmarkIntent != null) {
607 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
608 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
609 mActivity.startActivity(bookmarkIntent);
610 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700611 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 }
613 break;
614 }
615
616 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700617 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 break;
619
620 case STOP_LOAD:
621 stopLoading();
622 break;
623
624 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700625 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 mWakeLock.release();
627 // if we reach here, Browser should be still in the
628 // background loading after WAKELOCK_TIMEOUT (5-min).
629 // To avoid burning the battery, stop loading.
630 mTabControl.stopAllLoading();
631 }
632 break;
633
kaiyiz591110b2013-08-06 17:11:06 +0800634 case OPEN_MENU:
635 if (!mOptionsMenuOpen && mActivity != null ) {
636 mActivity.openOptionsMenu();
637 }
638 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 }
640 }
641 };
642
643 }
644
John Reckef654f12011-07-12 16:42:08 -0700645 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200646 public Tab getCurrentTab() {
647 return mTabControl.getCurrentTab();
648 }
649
Michael Kolbba99c5d2010-11-29 14:57:41 -0800650 @Override
651 public void shareCurrentPage() {
652 shareCurrentPage(mTabControl.getCurrentTab());
653 }
654
655 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700656 if (tab == null || tab.getWebView() == null)
657 return;
658
659 final Tab mytab = tab;
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700660
661 createScreenshotAsync(
Pankaj Gargf49e0222015-09-01 12:19:13 -0700662 tab,
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700663 new ValueCallback<Bitmap>() {
664 @Override
665 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -0700666 Bitmap bm = cropAndScaleBitmap(bitmap,
667 getDesiredThumbnailWidth(mActivity),
668 getDesiredThumbnailHeight(mActivity));
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700669 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
Pankaj Gargf49e0222015-09-01 12:19:13 -0700670 mytab.getFavicon(), bm);
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700671 }
672 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800673 }
674
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 /**
676 * Share a page, providing the title, url, favicon, and a screenshot. Uses
677 * an {@link Intent} to launch the Activity chooser.
678 * @param c Context used to launch a new Activity.
679 * @param title Title of the page. Stored in the Intent with
680 * {@link Intent#EXTRA_SUBJECT}
681 * @param url URL of the page. Stored in the Intent with
682 * {@link Intent#EXTRA_TEXT}
683 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
684 * with {@link Browser#EXTRA_SHARE_FAVICON}
685 * @param screenshot Bitmap of a screenshot of the page. Stored in the
686 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
687 */
688 static final void sharePage(Context c, String title, String url,
689 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700690
691 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
692 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
693 R.layout.app_row, sDialog.getApps());
694 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700695 }
696
697 private void copy(CharSequence text) {
698 ClipboardManager cm = (ClipboardManager) mActivity
699 .getSystemService(Context.CLIPBOARD_SERVICE);
700 cm.setText(text);
701 }
702
703 // lifecycle
704
John Reck9c35b9c2012-05-30 10:08:50 -0700705 @Override
706 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800708 // update the menu in case of a locale change
709 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800710 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800711 if (mOptionsMenuOpen) {
712 mActivity.closeOptionsMenu();
713 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
714 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 mUi.onConfigurationChanged(config);
716 }
717
718 @Override
719 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700720 if (!mUi.isWebShowing()) {
721 mUi.showWeb(false);
722 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700723 mIntentHandler.onNewIntent(intent);
724 }
725
John Reck9c35b9c2012-05-30 10:08:50 -0700726 @Override
727 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800728 if (mUi.isCustomViewShowing()) {
729 hideCustomView();
730 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700731 if (mActivityPaused) {
732 Log.e(LOGTAG, "BrowserActivity is already paused.");
733 return;
734 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700735 mActivityPaused = true;
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700736
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 }
738
John Reck9c35b9c2012-05-30 10:08:50 -0700739 @Override
740 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700741 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800742 Bundle saveState = createSaveState();
743
744 // crash recovery manages all save & restore state
745 mCrashRecoveryHandler.writeState(saveState);
746 mSettings.setLastRunPaused(true);
747 }
748
749 /**
750 * Save the current state to outState. Does not write the state to
751 * disk.
752 * @return Bundle containing the current state of all tabs.
753 */
754 /* package */ Bundle createSaveState() {
755 Bundle saveState = new Bundle();
756 mTabControl.saveState(saveState);
Vivek Sekhar2ce13da2015-12-15 11:23:29 -0800757 // This method is called multiple times.Need to
758 // guard against TabControl not having any tabs
759 // during the destroy cycles which looses all the
760 // existing saved information.
761 if (saveState.isEmpty())
762 return null;
George Mount3636d0a2011-11-21 09:08:21 -0800763 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 }
765
John Reck9c35b9c2012-05-30 10:08:50 -0700766 @Override
767 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700768 if (!mActivityPaused) {
769 Log.e(LOGTAG, "BrowserActivity is already resumed.");
770 return;
771 }
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700772 mActivityPaused = false;
773 if (mVoiceResult != null) {
774 mUi.onVoiceResult(mVoiceResult);
775 mVoiceResult = null;
776 }
777 }
778
779 private void releaseWakeLock() {
780 if (mWakeLock != null && mWakeLock.isHeld()) {
781 mHandler.removeMessages(RELEASE_WAKELOCK);
782 mWakeLock.release();
783 }
784 }
785
786 @Override
787 public void onStop() {
788 if (mActivityStopped) {
789 Log.e(LOGTAG, "BrowserActivity is already stoped.");
790 return;
791 }
792 mActivityStopped = true;
793 Tab tab = mTabControl.getCurrentTab();
794 if (tab != null) {
795 tab.pause();
796 if (!pauseWebViewTimers(tab)) {
797 if (mWakeLock == null) {
798 PowerManager pm = (PowerManager) mActivity
799 .getSystemService(Context.POWER_SERVICE);
800 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
801 }
802 mWakeLock.acquire();
803 mHandler.sendMessageDelayed(mHandler
804 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
805 }
806 }
807 mUi.onPause();
808 mNetworkHandler.onPause();
809 NfcHandler.unregister(mActivity);
Pankaj Garg5557aba2016-03-19 16:07:14 +0100810 if (mLowPowerReceiver != null)
811 mActivity.unregisterReceiver(mLowPowerReceiver);
Tarun Nainani32bc29c2015-04-29 15:33:45 -0700812 }
813
814 @Override
815 public void onStart() {
816 if (!mActivityStopped) {
817 Log.e(LOGTAG, "BrowserActivity is already started.");
818 return;
819 }
820 mActivityStopped = false;
Vivek Sekharb53a5952015-07-17 14:14:38 -0700821 UpdateNotificationService.updateCheck(mActivity);
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700822 // reset the search engine based on locale
823 mSettings.updateSearchEngine(false);
George Mount3636d0a2011-11-21 09:08:21 -0800824 mSettings.setLastRunPaused(false);
Michael Kolb70976932010-11-30 11:34:01 -0800825 Tab current = mTabControl.getCurrentTab();
826 if (current != null) {
827 current.resume();
828 resumeWebViewTimers(current);
829 }
John Reckf57c0292011-07-21 18:15:39 -0700830 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200831
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 mUi.onResume();
833 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100834 NfcHandler.register(mActivity, this);
Vivek Sekharc70ae632015-04-08 17:54:05 -0700835 if (current != null && current.getWebView().isShowingCrashView())
836 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700837 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700838
John Reckf57c0292011-07-21 18:15:39 -0700839 }
840
Michael Kolb70976932010-11-30 11:34:01 -0800841 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800842 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800843 * @param tab guaranteed non-null
844 */
845 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700846 boolean inLoad = tab.inPageLoad();
Tarun Nainani7c762c82015-08-04 13:42:09 -0700847 if ((!mActivityStopped && !inLoad) || (mActivityStopped && inLoad)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700848 CookieSyncManager.getInstance().startSync();
849 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100850 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700851 }
852 }
853
Michael Kolb70976932010-11-30 11:34:01 -0800854 /**
855 * Pause all WebView timers using the WebView of the given tab
856 * @param tab
857 * @return true if the timers are paused or tab is null
858 */
859 private boolean pauseWebViewTimers(Tab tab) {
860 if (tab == null) {
861 return true;
862 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100864 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 }
Michael Kolb70976932010-11-30 11:34:01 -0800867 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700868 }
869
John Reck9c35b9c2012-05-30 10:08:50 -0700870 @Override
871 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800872 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700873 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
874 mUploadHandler = null;
875 }
876 if (mTabControl == null) return;
877 mUi.onDestroy();
878 // Remove the current tab and sub window
879 Tab t = mTabControl.getCurrentTab();
880 if (t != null) {
881 dismissSubWindow(t);
882 removeTab(t);
883 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500884 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 // Destroy all the tabs
886 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700887 // Unregister receiver
Pankaj Garg5557aba2016-03-19 16:07:14 +0100888 if (mPowerChangeReceiver != null)
889 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 }
891
892 protected boolean isActivityPaused() {
893 return mActivityPaused;
894 }
895
John Reck9c35b9c2012-05-30 10:08:50 -0700896 @Override
897 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700898 mTabControl.freeMemory();
899 }
900
901 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700902 public void stopLoading() {
903 mLoadStopped = true;
904 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700905 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700906 if (w != null) {
907 w.stopLoading();
908 mUi.onPageStopped(tab);
909 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700910 }
911
912 boolean didUserStopLoading() {
913 return mLoadStopped;
914 }
915
kaiyiza016da12013-08-26 17:50:22 +0800916 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700917 final String reminderType = getContext().getResources().getString(
918 R.string.def_wifi_browser_interaction_remind_type);
919 final String selectionConnnection = getContext().getResources().getString(
920 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700921 final String wifiSelection = getContext().getResources().getString(
922 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700923
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700924 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
925 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700926 return;
927
kaiyiza016da12013-08-26 17:50:22 +0800928 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700929 Context.CONNECTIVITY_SERVICE);
930 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700931 WifiManager wifiMgr = (WifiManager) this.getContext()
932 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700933 if (networkInfo == null
934 || (networkInfo != null && (networkInfo.getType() !=
935 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700936 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
937 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700938 List<ScanResult> list = wifiMgr.getScanResults();
939 // Have no AP's for Wifi's fall back to data
940 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700941 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700942 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
943 this.getContext().startActivity(intent);
944 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700945 // Request to select Wifi AP
946 try {
947 Intent intent = new Intent(wifiSelection);
948 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
949 this.getContext().startActivity(intent);
950 } catch (Exception e) {
951 String err_msg = this.getContext().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700952 R.string.activity_not_found, wifiSelection);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700953 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
954 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700955 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700956 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800957 }
958 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700959
Michael Kolb8233fac2010-10-26 16:08:53 -0700960 // WebViewController
961
962 @Override
John Reck324d4402011-01-11 16:56:42 -0800963 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700964
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 // reset sync timer to avoid sync starts during loading a page
966 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700967 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700968 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800969 boolean networkNotifier = BrowserConfig.getInstance(getContext())
970 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700971 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700972 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800973 } else {
974 if (!mNetworkHandler.isNetworkUp()) {
975 view.setNetworkAvailable(false);
976 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 }
978
979 // when BrowserActivity just starts, onPageStarted may be called before
980 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
981 // to start the timer. As we won't switch tabs while an activity is in
982 // pause state, we can ensure calling resume and pause in pair.
Tarun Nainani7c762c82015-08-04 13:42:09 -0700983 if (mActivityStopped) {
Michael Kolb70976932010-11-30 11:34:01 -0800984 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 }
986 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 endActionMode();
988
John Reck30c714c2010-12-16 17:30:34 -0800989 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700990
John Reck324d4402011-01-11 16:56:42 -0800991 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700992 // update the bookmark database for favicon
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700993 syncBookmarkFavicon(tab, null, url, favicon);
Michael Kolb8233fac2010-10-26 16:08:53 -0700994
995 Performance.tracePageStart(url);
996
997 // Performance probe
998 if (false) {
999 Performance.onPageStarted();
1000 }
1001
1002 }
1003
1004 @Override
John Reck324d4402011-01-11 16:56:42 -08001005 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -07001006 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -08001007 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001008
Michael Kolb8233fac2010-10-26 16:08:53 -07001009 // Performance probe
1010 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001011 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001012 }
1013
Tarun Nainani8eb00912014-07-17 12:28:32 -07001014 tab.onPageFinished();
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001015 syncBookmarkFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -07001016
Michael Kolb8233fac2010-10-26 16:08:53 -07001017 Performance.tracePageFinished();
1018 }
1019
1020 @Override
John Reck30c714c2010-12-16 17:30:34 -08001021 public void onProgressChanged(Tab tab) {
1022 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001023
1024 if (newProgress == 100) {
1025 CookieSyncManager.getInstance().sync();
1026 // onProgressChanged() may continue to be called after the main
1027 // frame has finished loading, as any remaining sub frames continue
1028 // to load. We'll only get called once though with newProgress as
1029 // 100 when everything is loaded. (onPageFinished is called once
1030 // when the main frame completes loading regardless of the state of
1031 // any sub frames so calls to onProgressChanges may continue after
1032 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001033 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001034 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001035 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001036 } else if (mWasInPageLoad) {
1037 mWasInPageLoad = false;
1038 updateInLoadMenuItems(mCachedMenu, tab);
1039 }
1040
Tarun Nainani7c762c82015-08-04 13:42:09 -07001041 if (mActivityStopped && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001042 // pause the WebView timer and release the wake lock if it is
1043 // finished while BrowserActivity is in pause state.
1044 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001045 }
1046 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001047 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001048 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001049 // still loading
1050 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001051 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001052 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001053 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001054 } else {
1055 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 }
1057 }
Pankaj Gargf9040c82015-08-14 10:19:31 -07001058
1059 mUi.onProgressChanged(tab);
John Reck30c714c2010-12-16 17:30:34 -08001060 }
1061
1062 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001063 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001064 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001065 }
1066
1067 @Override
1068 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001069 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001070 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001071 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001072 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1073 return;
1074 }
1075 // Update the title in the history database if not in private browsing mode
1076 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001077 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001078 }
1079 }
1080
1081 @Override
1082 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001083 syncBookmarkFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1084 ((BaseUi)mUi).setFavicon(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 }
1086
1087 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001088 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001089 // if tab is snapshot tab we want to prevent navigation from occuring
1090 // since snapshot tab opens a new tab with the url
1091 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 }
1093
1094 @Override
1095 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1096 if (mMenuIsDown) {
1097 // only check shortcut key when MENU is held
1098 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1099 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001100 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001101 int keyCode = event.getKeyCode();
1102 // We need to send almost every key to WebKit. However:
1103 // 1. We don't want to block the device on the renderer for
1104 // some keys like menu, home, call.
1105 // 2. There are no WebKit equivalents for some of these keys
1106 // (see app/keyboard_codes_win.h)
1107 // Note that these are not the same set as KeyEvent.isSystemKey:
1108 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1109 if (keyCode == KeyEvent.KEYCODE_MENU ||
1110 keyCode == KeyEvent.KEYCODE_HOME ||
1111 keyCode == KeyEvent.KEYCODE_BACK ||
1112 keyCode == KeyEvent.KEYCODE_CALL ||
1113 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1114 keyCode == KeyEvent.KEYCODE_POWER ||
1115 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1116 keyCode == KeyEvent.KEYCODE_CAMERA ||
1117 keyCode == KeyEvent.KEYCODE_FOCUS ||
1118 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1119 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1120 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1121 return true;
1122 }
1123
1124 // We also have to intercept some shortcuts before we send them to the ContentView.
1125 if (event.isCtrlPressed() && (
1126 keyCode == KeyEvent.KEYCODE_TAB ||
1127 keyCode == KeyEvent.KEYCODE_W ||
1128 keyCode == KeyEvent.KEYCODE_F4)) {
1129 return true;
1130 }
1131
1132 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001133 }
1134
Tarun Nainanif5563b62015-01-21 18:52:59 -08001135 private void handleMediaKeyEvent(KeyEvent event) {
1136
1137 int keyCode = event.getKeyCode();
1138 // send media key events to audio manager
1139 if (Build.VERSION.SDK_INT >= 19) {
1140
1141 switch (keyCode) {
1142 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1143 case KeyEvent.KEYCODE_MEDIA_STOP:
1144 case KeyEvent.KEYCODE_MEDIA_NEXT:
1145 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1146 case KeyEvent.KEYCODE_MEDIA_REWIND:
1147 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1148 case KeyEvent.KEYCODE_MUTE:
1149 case KeyEvent.KEYCODE_MEDIA_PLAY:
1150 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1151 case KeyEvent.META_SHIFT_RIGHT_ON:
1152 case KeyEvent.KEYCODE_MEDIA_EJECT:
1153 case KeyEvent.KEYCODE_MEDIA_RECORD:
1154 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1155
1156 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1157 .getSystemService(Context.AUDIO_SERVICE);
1158 audioManager.dispatchMediaKeyEvent(event);
1159 }
1160 }
1161 }
1162
Michael Kolb8233fac2010-10-26 16:08:53 -07001163 @Override
John Reck997b1b72012-04-19 18:08:25 -07001164 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001166 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001167 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001168 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 } else {
John Reck997b1b72012-04-19 18:08:25 -07001170 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001171 }
1172 }
John Reck997b1b72012-04-19 18:08:25 -07001173 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 }
1175
1176 @Override
John Reck324d4402011-01-11 16:56:42 -08001177 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001178 // Don't save anything in private browsing mode or when disabling history
1179 // for regular tabs is enabled
1180 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1181 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1182 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001183
John Reck49a603c2011-03-03 09:33:05 -08001184 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001185
John Reck324d4402011-01-11 16:56:42 -08001186 if (TextUtils.isEmpty(url)
1187 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001188 return;
1189 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001190
John Reckf57c0292011-07-21 18:15:39 -07001191 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001192 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001193 }
1194
1195 @Override
1196 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1197 AsyncTask<Void, Void, String[]> task =
1198 new AsyncTask<Void, Void, String[]>() {
1199 @Override
1200 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001201 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001202 }
1203 @Override
1204 public void onPostExecute(String[] result) {
1205 callback.onReceiveValue(result);
1206 }
1207 };
1208 task.execute();
1209 }
1210
1211 @Override
1212 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1213 final HttpAuthHandler handler, final String host,
1214 final String realm) {
1215 String username = null;
1216 String password = null;
1217
1218 boolean reuseHttpAuthUsernamePassword
1219 = handler.useHttpAuthUsernamePassword();
1220
1221 if (reuseHttpAuthUsernamePassword && view != null) {
1222 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1223 if (credentials != null && credentials.length == 2) {
1224 username = credentials[0];
1225 password = credentials[1];
1226 }
1227 }
1228
1229 if (username != null && password != null) {
1230 handler.proceed(username, password);
1231 } else {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -07001232 if (!tab.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001233 handler.cancel();
1234 }
1235 }
1236 }
1237
1238 @Override
1239 public void onDownloadStart(Tab tab, String url, String userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001240 String contentDisposition, String mimetype, String referer, String auth,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001241 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001242 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001243 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001244 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001245 contentDisposition, mimetype, referer, auth,
1246 w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001247 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001248 // This Tab was opened for the sole purpose of downloading a
1249 // file. Remove it.
1250 if (tab == mTabControl.getCurrentTab()) {
1251 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001252 if (tab.getDerivedFromIntent())
1253 closeTab(tab);
1254 else
1255 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001256 } else {
1257 // In this case, it is not.
1258 closeTab(tab);
1259 }
1260 }
1261 }
1262
1263 @Override
1264 public Bitmap getDefaultVideoPoster() {
1265 return mUi.getDefaultVideoPoster();
1266 }
1267
1268 @Override
1269 public View getVideoLoadingProgressView() {
1270 return mUi.getVideoLoadingProgressView();
1271 }
1272
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 // helper method
1274
1275 /*
1276 * Update the favorites icon if the private browsing isn't enabled and the
1277 * icon is valid.
1278 */
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001279 private void syncBookmarkFavicon(Tab tab, final String originalUrl,
1280 final String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 if (favicon == null) {
1282 return;
1283 }
1284 if (!tab.isPrivateBrowsingEnabled()) {
1285 Bookmarks.updateFavicon(mActivity
1286 .getContentResolver(), originalUrl, url, favicon);
1287 }
1288 }
1289
Leon Scroggins4cd97792010-12-03 15:31:56 -05001290 @Override
1291 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001292 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001293 mUi.bookmarkedStatusHasChanged(tab);
1294 }
1295
Michael Kolb8233fac2010-10-26 16:08:53 -07001296 // end WebViewController
1297
1298 protected void pageUp() {
1299 getCurrentTopWebView().pageUp(false);
1300 }
1301
1302 protected void pageDown() {
1303 getCurrentTopWebView().pageDown(false);
1304 }
1305
1306 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001307 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001308 public void editUrl() {
1309 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001310 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001311 }
1312
John Reck9c35b9c2012-05-30 10:08:50 -07001313 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001314 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001315 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001316 if (tab.inForeground()) {
1317 if (mUi.isCustomViewShowing()) {
1318 callback.onCustomViewHidden();
1319 return;
1320 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001321 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001322 // Save the menu state and set it to empty while the custom
1323 // view is showing.
1324 mOldMenuState = mMenuState;
1325 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001326 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 }
1328 }
1329
1330 @Override
1331 public void hideCustomView() {
1332 if (mUi.isCustomViewShowing()) {
1333 mUi.onHideCustomView();
1334 // Reset the old menu state.
1335 mMenuState = mOldMenuState;
1336 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001337 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001338 }
1339 }
1340
John Reck9c35b9c2012-05-30 10:08:50 -07001341 @Override
1342 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001343 Intent intent) {
1344 if (getCurrentTopWebView() == null) return;
1345 switch (requestCode) {
1346 case PREFERENCES_PAGE:
1347 if (resultCode == Activity.RESULT_OK && intent != null) {
1348 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001349 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001350 mTabControl.removeParentChildRelationShips();
Pankaj Garg75273bb2015-08-20 11:43:49 -07001351 } else if (action.equals(PreferenceKeys.ACTION_RELOAD_PAGE)) {
1352 ArrayList<String> origins =
1353 intent.getStringArrayListExtra(EXTRA_UPDATED_URLS);
1354 if (origins.isEmpty()) {
1355 mTabControl.reloadLiveTabs();
1356 }
1357 else{
1358 for (String origin : origins){
1359 mTabControl.findAndReload(origin);
1360 }
1361 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001362 }
1363 }
1364 break;
1365 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001366 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001367 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001370 case AUTOFILL_SETUP:
1371 // Determine whether a profile was actually set up or not
1372 // and if so, send the message back to the WebTextView to
1373 // fill the form with the new profile.
1374 if (getSettings().getAutoFillProfile() != null) {
1375 mAutoFillSetupMessage.sendToTarget();
1376 mAutoFillSetupMessage = null;
1377 }
1378 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001379 case COMBO_VIEW:
1380 if (intent == null || resultCode != Activity.RESULT_OK) {
1381 break;
1382 }
John Reck3ba45532011-08-11 16:26:53 -07001383 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001384 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1385 Tab t = getCurrentTab();
1386 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001387 mUpdateMyNavThumbnail = true;
1388 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001389 loadUrl(t, uri.toString());
1390 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1391 String[] urls = intent.getStringArrayExtra(
1392 ComboViewActivity.EXTRA_OPEN_ALL);
1393 Tab parent = getCurrentTab();
1394 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001395 if (url != null) {
1396 parent = openTab(url, parent,
1397 !mSettings.openInBackground(), true);
1398 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001399 }
1400 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1401 long id = intent.getLongExtra(
1402 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1403 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001404 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001405 }
1406 }
1407 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001408 case VOICE_RESULT:
1409 if (resultCode == Activity.RESULT_OK && intent != null) {
1410 ArrayList<String> results = intent.getStringArrayListExtra(
1411 RecognizerIntent.EXTRA_RESULTS);
1412 if (results.size() >= 1) {
1413 mVoiceResult = results.get(0);
1414 }
1415 }
1416 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001417 case MY_NAVIGATION:
1418 if (intent == null || resultCode != Activity.RESULT_OK) {
1419 break;
1420 }
1421
1422 if (intent.getBooleanExtra("need_refresh", false) &&
1423 getCurrentTopWebView() != null) {
1424 getCurrentTopWebView().reload();
1425 }
1426 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001427 default:
1428 break;
1429 }
1430 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001431 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001432 }
1433
1434 /**
1435 * Open the Go page.
1436 * @param startWithHistory If true, open starting on the history tab.
1437 * Otherwise, start with the bookmarks tab.
1438 */
1439 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001440 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001441 if (mTabControl.getCurrentWebView() == null) {
1442 return;
1443 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001444 // clear action mode
1445 if (isInCustomActionMode()) {
1446 endActionMode();
1447 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001448 Bundle extras = new Bundle();
1449 // Disable opening in a new window if we have maxed out the windows
1450 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1451 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001452 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 }
1454
1455 // combo view callbacks
1456
Michael Kolb8233fac2010-10-26 16:08:53 -07001457 // key handling
1458 protected void onBackKey() {
1459 if (!mUi.onBackKey()) {
1460 WebView subwindow = mTabControl.getCurrentSubWindow();
1461 if (subwindow != null) {
1462 if (subwindow.canGoBack()) {
1463 subwindow.goBack();
1464 } else {
1465 dismissSubWindow(mTabControl.getCurrentTab());
1466 }
1467 } else {
1468 goBackOnePageOrQuit();
1469 }
1470 }
1471 }
1472
Michael Kolb4bd767d2011-05-27 11:33:55 -07001473 protected boolean onMenuKey() {
1474 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001475 }
1476
Michael Kolb8233fac2010-10-26 16:08:53 -07001477 // menu handling and state
1478 // TODO: maybe put into separate handler
1479
John Reck9c35b9c2012-05-30 10:08:50 -07001480 @Override
1481 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001482 if (mMenuState == EMPTY_MENU) {
1483 return false;
1484 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001485 MenuInflater inflater = mActivity.getMenuInflater();
1486 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001487 return true;
1488 }
1489
John Reck9c35b9c2012-05-30 10:08:50 -07001490 @Override
1491 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001492 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001493 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001494 return;
1495 }
1496 if (!(v instanceof WebView)) {
1497 return;
1498 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001499 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001500 WebView.HitTestResult result;
1501
1502 /* Determine whether the ContextMenu got triggered because
1503 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1504 * received. The mResult acts as a flag to identify, how it got trigerred
1505 */
1506 if (mResult == null){
1507 result = webview.getHitTestResult();
1508 } else {
1509 result = mResult;
1510 }
1511
Michael Kolb8233fac2010-10-26 16:08:53 -07001512 if (result == null) {
1513 return;
1514 }
1515
1516 int type = result.getType();
1517 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001518
1519 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1520 unknownTypeMap.put("webview", webview);
1521 final Message msg = mHandler.obtainMessage(
1522 UNKNOWN_TYPE_MSG, unknownTypeMap);
1523 /* As defined in android developers guide
1524 * when UNKNOWN_TYPE is received as a result of HitTest
1525 * you need to determing the type by invoking requestFocusNodeHref
1526 */
1527 webview.requestFocusNodeHref(msg);
1528
Michael Kolb8233fac2010-10-26 16:08:53 -07001529 Log.w(LOGTAG,
1530 "We should not show context menu when nothing is touched");
1531 return;
1532 }
1533 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1534 // let TextView handles context menu
1535 return;
1536 }
1537
1538 // Note, http://b/issue?id=1106666 is requesting that
1539 // an inflated menu can be used again. This is not available
1540 // yet, so inflate each time (yuk!)
1541 MenuInflater inflater = mActivity.getMenuInflater();
1542 inflater.inflate(R.menu.browsercontext, menu);
1543
1544 // Show the correct menu group
1545 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001546 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001547 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001548 menu.setGroupVisible(R.id.PHONE_MENU,
1549 type == WebView.HitTestResult.PHONE_TYPE);
1550 menu.setGroupVisible(R.id.EMAIL_MENU,
1551 type == WebView.HitTestResult.EMAIL_TYPE);
1552 menu.setGroupVisible(R.id.GEO_MENU,
1553 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001554 String itemUrl = null;
1555 String url = webview.getOriginalUrl();
1556 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1557 itemUrl = Uri.decode(navigationUrl);
1558 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1559 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1560 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1561 } else {
1562 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1563 }
1564 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1565 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1566 } else {
1567 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1568
1569 menu.setGroupVisible(R.id.IMAGE_MENU,
1570 type == WebView.HitTestResult.IMAGE_TYPE
1571 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1572 menu.setGroupVisible(R.id.ANCHOR_MENU,
1573 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1574 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001575
1576 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1577 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1578 UrlUtils.isDownloadableScheme(extra));
1579 }
1580 else {
1581 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1582 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001583 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001584 // Setup custom handling depending on the type
1585 switch (type) {
1586 case WebView.HitTestResult.PHONE_TYPE:
1587 menu.setHeaderTitle(Uri.decode(extra));
1588 menu.findItem(R.id.dial_context_menu_id).setIntent(
1589 new Intent(Intent.ACTION_VIEW, Uri
Vivek Sekharcb3f7232015-12-10 20:02:46 -08001590 .parse(WebView.SCHEME_TEL + Uri.encode(extra))));
Michael Kolb8233fac2010-10-26 16:08:53 -07001591 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1592 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1593 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1594 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1595 addIntent);
1596 menu.findItem(R.id.copy_phone_context_menu_id)
1597 .setOnMenuItemClickListener(
1598 new Copy(extra));
1599 break;
1600
1601 case WebView.HitTestResult.EMAIL_TYPE:
1602 menu.setHeaderTitle(extra);
1603 menu.findItem(R.id.email_context_menu_id).setIntent(
1604 new Intent(Intent.ACTION_VIEW, Uri
1605 .parse(WebView.SCHEME_MAILTO + extra)));
1606 menu.findItem(R.id.copy_mail_context_menu_id)
1607 .setOnMenuItemClickListener(
1608 new Copy(extra));
1609 break;
1610
1611 case WebView.HitTestResult.GEO_TYPE:
1612 menu.setHeaderTitle(extra);
1613 menu.findItem(R.id.map_context_menu_id).setIntent(
1614 new Intent(Intent.ACTION_VIEW, Uri
1615 .parse(WebView.SCHEME_GEO
1616 + URLEncoder.encode(extra))));
1617 menu.findItem(R.id.copy_geo_context_menu_id)
1618 .setOnMenuItemClickListener(
1619 new Copy(extra));
1620 break;
1621
1622 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1623 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001624 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001625 // decide whether to show the open link in new tab option
1626 boolean showNewTab = mTabControl.canCreateNewTab();
1627 MenuItem newTabItem
1628 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001629 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001630 ? R.string.contextmenu_openlink_newwindow_background
1631 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001632 newTabItem.setVisible(showNewTab);
1633 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001634 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1635 newTabItem.setOnMenuItemClickListener(
1636 new MenuItem.OnMenuItemClickListener() {
1637 @Override
1638 public boolean onMenuItemClick(MenuItem item) {
1639 final HashMap<String, WebView> hrefMap =
1640 new HashMap<String, WebView>();
1641 hrefMap.put("webview", webview);
1642 final Message msg = mHandler.obtainMessage(
1643 FOCUS_NODE_HREF,
1644 R.id.open_newtab_context_menu_id,
1645 0, hrefMap);
1646 webview.requestFocusNodeHref(msg);
1647 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001648 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001649 });
1650 } else {
1651 newTabItem.setOnMenuItemClickListener(
1652 new MenuItem.OnMenuItemClickListener() {
1653 @Override
1654 public boolean onMenuItemClick(MenuItem item) {
1655 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001656 openTab(extra, parent,
1657 !mSettings.openInBackground(),
1658 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001659 return true;
1660 }
1661 });
1662 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001663 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001664 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1665 menu.setHeaderTitle(navigationUrl);
1666 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1667
1668 if (itemUrl != null) {
1669 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1670 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1671 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1672 @Override
1673 public boolean onMenuItemClick(MenuItem item) {
1674 final Intent intent = new Intent(Controller.this
1675 .getContext(),
1676 AddMyNavigationPage.class);
1677 Bundle bundle = new Bundle();
1678 String url = Uri.decode(navigationUrl);
1679 bundle.putBoolean("isAdding", false);
1680 bundle.putString("url", url);
1681 bundle.putString("name", getNameFromUrl(url));
1682 intent.putExtra("websites", bundle);
1683 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1684 return false;
1685 }
1686 });
1687 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1688 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1689 @Override
1690 public boolean onMenuItemClick(MenuItem item) {
1691 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1692 return false;
1693 }
1694 });
1695 }
1696 } else {
1697 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1698 }
1699 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001700 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1701 break;
1702 }
1703 // otherwise fall through to handle image part
1704 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001705 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1706 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001707 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1708 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001709 shareItem.setOnMenuItemClickListener(
1710 new MenuItem.OnMenuItemClickListener() {
1711 @Override
1712 public boolean onMenuItemClick(MenuItem item) {
1713 sharePage(mActivity, null, extra, null,
1714 null);
1715 return true;
1716 }
1717 }
1718 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001719 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001720 menu.findItem(R.id.view_image_context_menu_id)
1721 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1722 @Override
1723 public boolean onMenuItemClick(MenuItem item) {
1724 openTab(extra, mTabControl.getCurrentTab(), true, true);
1725 return false;
1726 }
1727 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001728 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1729 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1730 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001731 menu.findItem(R.id.set_wallpaper_context_menu_id).
1732 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1733 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001734 break;
1735
1736 default:
1737 Log.w(LOGTAG, "We should not get here.");
1738 break;
1739 }
1740 //update the ui
1741 mUi.onContextMenuCreated(menu);
1742 }
1743
kaiyiz6e5b3e02013-08-19 20:02:01 +08001744 public void startAddMyNavigation(String url) {
1745 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1746 Bundle bundle = new Bundle();
1747 bundle.putBoolean("isAdding", true);
1748 bundle.putString("url", url);
1749 bundle.putString("name", getNameFromUrl(url));
1750 intent.putExtra("websites", bundle);
1751 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1752 }
1753
1754 private void showMyNavigationDeleteDialog(final String itemUrl) {
1755 new AlertDialog.Builder(this.getContext())
1756 .setTitle(R.string.my_navigation_delete_label)
1757 .setIcon(android.R.drawable.ic_dialog_alert)
1758 .setMessage(R.string.my_navigation_delete_msg)
1759 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1760 @Override
1761 public void onClick(DialogInterface dialog, int whichButton) {
1762 deleteMyNavigationItem(itemUrl);
1763 }
1764 })
1765 .setNegativeButton(R.string.cancel, null)
1766 .show();
1767 }
1768
1769 private void deleteMyNavigationItem(final String itemUrl) {
1770 ContentResolver cr = this.getContext().getContentResolver();
1771 Cursor cursor = null;
1772
1773 try {
1774 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1775 new String[] {
1776 MyNavigationUtil.ID
1777 }, "url = ?", new String[] {
1778 itemUrl
1779 }, null);
1780 if (null != cursor && cursor.moveToFirst()) {
1781 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1782 cursor.getLong(0));
1783
1784 ContentValues values = new ContentValues();
1785 values.put(MyNavigationUtil.TITLE, "");
1786 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1787 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1788 ByteArrayOutputStream os = new ByteArrayOutputStream();
1789 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1790 R.raw.my_navigation_add);
1791 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1792 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1793 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1794 cr.update(uri, values, null, null);
1795 } else {
1796 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1797 }
1798 } catch (IllegalStateException e) {
1799 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1800 } finally {
1801 if (null != cursor) {
1802 cursor.close();
1803 }
1804 }
1805
1806 if (getCurrentTopWebView() != null) {
1807 getCurrentTopWebView().reload();
1808 }
1809 }
1810
1811 private String getNameFromUrl(String itemUrl) {
1812 ContentResolver cr = this.getContext().getContentResolver();
1813 Cursor cursor = null;
1814 String name = null;
1815
1816 try {
1817 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1818 new String[] {
1819 MyNavigationUtil.TITLE
1820 }, "url = ?", new String[] {
1821 itemUrl
1822 }, null);
1823 if (null != cursor && cursor.moveToFirst()) {
1824 name = cursor.getString(0);
1825 } else {
1826 Log.e(LOGTAG, "this item does not exist!");
1827 }
1828 } catch (IllegalStateException e) {
1829 Log.e(LOGTAG, "getNameFromUrl", e);
1830 } finally {
1831 if (null != cursor) {
1832 cursor.close();
1833 }
1834 }
1835 return name;
1836 }
1837
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001838 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001839 final ContentResolver cr = mActivity.getContentResolver();
1840 new AsyncTask<Void, Void, Void>() {
1841 @Override
1842 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001843 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1844 if(!isMyNavigationUrl)
1845 return null;
1846
kaiyiz6e5b3e02013-08-19 20:02:01 +08001847 ContentResolver cr = mActivity.getContentResolver();
1848 Cursor cursor = null;
1849 try {
1850 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1851 new String[] {
1852 MyNavigationUtil.ID
1853 }, "url = ?", new String[] {
1854 itemUrl
1855 }, null);
1856 if (null != cursor && cursor.moveToFirst()) {
1857 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001858 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001859
1860 ContentValues values = new ContentValues();
1861 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1862 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1863 cursor.getLong(0));
1864 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1865 cr.update(uri, values, null, null);
1866 os.close();
1867 }
1868 } catch (IllegalStateException e) {
1869 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1870 } catch (IOException e) {
1871 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1872 } finally {
1873 if (null != cursor) {
1874 cursor.close();
1875 }
1876 }
1877 return null;
1878 }
1879 }.execute();
1880 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001881 /**
1882 * As the menu can be open when loading state changes
1883 * we must manually update the state of the stop/reload menu
1884 * item
1885 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001886 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 if (menu == null) {
1888 return;
1889 }
1890 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001891 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001892 menu.findItem(R.id.stop_menu_id):
1893 menu.findItem(R.id.reload_menu_id);
1894 if (src != null) {
1895 dest.setIcon(src.getIcon());
1896 dest.setTitle(src.getTitle());
1897 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001898 mActivity.invalidateOptionsMenu();
1899 }
1900
1901 public void invalidateOptionsMenu() {
1902 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001903 }
1904
John Reck9c35b9c2012-05-30 10:08:50 -07001905 @Override
1906 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001907 // Software menu key (toolbar key)
Sagar Dhawan7fa89622016-01-04 17:58:02 -08001908 View overflowMenu = mActivity.findViewById(R.id.more_browser_settings);
1909 if (getCurrentTab() != null && getCurrentTab().isSnapshot()) {
1910 overflowMenu = mActivity.findViewById(R.id.more);
1911 }
1912 mAppMenuHandler.showAppMenu(overflowMenu, false, false);
Pankaj Garg49b79252014-11-07 17:33:41 -08001913 return true;
1914 }
1915
1916 @Override
1917 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001918 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001919 // hold on to the menu reference here; it is used by the page callbacks
1920 // to update the menu based on loading state
1921 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001922 // Note: setVisible will decide whether an item is visible; while
1923 // setEnabled() will decide whether an item is enabled, which also means
1924 // whether the matching shortcut key will function.
1925 switch (mMenuState) {
1926 case EMPTY_MENU:
1927 if (mCurrentMenuState != mMenuState) {
1928 menu.setGroupVisible(R.id.MAIN_MENU, false);
1929 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1930 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1931 }
1932 break;
1933 default:
1934 if (mCurrentMenuState != mMenuState) {
1935 menu.setGroupVisible(R.id.MAIN_MENU, true);
1936 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1937 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1938 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001939 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001940 break;
1941 }
1942 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001943 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001944
1945 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001946 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1947 EditBookmarksRestriction.getInstance()
1948 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001949 }
1950
1951 private void setMenuItemVisibility(Menu menu, int id,
1952 boolean visibility) {
1953 MenuItem item = menu.findItem(id);
1954 if (item != null) {
1955 item.setVisible(visibility);
1956 }
1957 }
1958
Sagar Dhawanef06f562015-09-07 15:23:22 +02001959 private int lookupBookmark( String url) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001960 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001961 int count = 0;
1962 Cursor cursor = null;
1963 try {
1964 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1965 BookmarksLoader.PROJECTION,
Sagar Dhawanef06f562015-09-07 15:23:22 +02001966 "url = ?",
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001967 new String[] {
Sagar Dhawanef06f562015-09-07 15:23:22 +02001968 url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001969 },
1970 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001971
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001972 if (cursor != null)
1973 count = cursor.getCount();
1974
1975 } catch (IllegalStateException e) {
1976 Log.e(LOGTAG, "lookupBookmark ", e);
1977 } finally {
1978 if (null != cursor) {
1979 cursor.close();
1980 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001981 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001982 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001983 }
1984
1985 private void resetMenuItems(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001986 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1987
1988 WebView w = getCurrentTopWebView();
1989 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1990
1991 String title = w.getTitle();
1992 String url = w.getUrl();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001993 mCurrentPageBookmarked = (lookupBookmark(url) > 0);
Pankaj Garg634bf432015-07-13 09:54:21 -07001994 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001995 bookmark_icon.setChecked(true);
1996 } else {
1997 bookmark_icon.setChecked(false);
1998 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001999
2000 // update reader mode checkbox
2001 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2002 readerSwitcher.setVisible(false);
2003 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002004 }
2005
Michael Kolb4bf79712011-07-14 14:18:12 -07002006 @Override
2007 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002008 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07002009 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07002010 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07002011 // Following flag is used to identify schemes for which the LIVE_MENU
2012 // items defined in res/menu/browser.xml should be enabled
2013 boolean isLiveScheme = false;
2014 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002015 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07002016
2017 boolean isDistillable = false;
2018 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002019 resetMenuItems(menu);
2020
Michael Kolb4bf79712011-07-14 14:18:12 -07002021 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002022 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002023 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002024 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002025 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002026 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002027 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07002028
2029 isDistillable = tab.isDistillable();
2030 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07002031 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002032
2033 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2034 forward.setEnabled(canGoForward);
2035
Michael Kolb4bf79712011-07-14 14:18:12 -07002036 // decide whether to show the share link option
2037 PackageManager pm = mActivity.getPackageManager();
2038 Intent send = new Intent(Intent.ACTION_SEND);
2039 send.setType("text/plain");
2040 ResolveInfo ri = pm.resolveActivity(send,
2041 PackageManager.MATCH_DEFAULT_ONLY);
2042 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2043
2044 boolean isNavDump = mSettings.enableNavDump();
2045 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2046 nav.setVisible(isNavDump);
2047 nav.setEnabled(isNavDump);
2048
2049 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002050 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2051 uaSwitcher.setChecked(isDesktopUa);
Sagar Dhawanef06f562015-09-07 15:23:22 +02002052 setMenuItemVisibility(menu, R.id.find_menu_id, isLive);
2053 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002054 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002055 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002056 setMenuItemVisibility(menu, R.id.add_to_homescreen,
Sagar Dhawanef06f562015-09-07 15:23:22 +02002057 isLive && isPageFinished);
Pankaj Garg49b79252014-11-07 17:33:41 -08002058 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002059 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002060 // history and snapshots item are the members of COMBO menu group,
2061 // so if show history item, only make snapshots item invisible.
2062 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002063
Ze G Riande2a675c22015-06-03 11:15:24 -07002064
2065 // update reader mode checkbox
2066 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2067 // The reader mode checkbox is hidden only
2068 // when the current page is neither distillable nor distilled
2069 readerSwitcher.setVisible(isDistillable || isDistilled);
2070 readerSwitcher.setChecked(isDistilled);
2071
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002072 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002073 }
2074
Pankaj Gargf49e0222015-09-01 12:19:13 -07002075 private Bitmap cropAndScaleBitmap(Bitmap bm, int width, int height) {
2076 if (width == 0 || height == 0 || bm == null)
2077 return bm;
2078
2079 Bitmap cropped;
2080
2081 if (bm.getHeight() > bm.getWidth()) {
2082 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
2083 bm.getWidth() * height / width, null, true);
2084 } else {
2085 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getHeight() * width / height,
2086 bm.getHeight(), null, true);
2087 }
2088
2089 Bitmap scaled = Bitmap.createScaledBitmap(cropped, width, height, true);
2090 cropped.recycle();
2091 return scaled;
2092 }
2093
John Reck9c35b9c2012-05-30 10:08:50 -07002094 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002095 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002096 if (null == getCurrentTopWebView()) {
2097 return false;
2098 }
2099 if (mMenuIsDown) {
2100 // The shortcut action consumes the MENU. Even if it is still down,
2101 // it won't trigger the next shortcut action. In the case of the
2102 // shortcut action triggering a new activity, like Bookmarks, we
2103 // won't get onKeyUp for MENU. So it is important to reset it here.
2104 mMenuIsDown = false;
2105 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002106 if (mUi.onOptionsItemSelected(item)) {
2107 // ui callback handled it
2108 return true;
2109 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002110 switch (item.getItemId()) {
2111 // -- Main menu
2112 case R.id.new_tab_menu_id:
2113 openTabToHomePage();
2114 break;
2115
2116 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002117 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002118 break;
2119
2120 case R.id.goto_menu_id:
2121 editUrl();
2122 break;
2123
2124 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002125 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2126 break;
2127
Michael Kolb315d5022011-10-13 12:47:11 -07002128 case R.id.snapshots_menu_id:
2129 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002130 break;
2131
Pankaj Garg49b79252014-11-07 17:33:41 -08002132 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002133 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002134 break;
2135
2136 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002137 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002138 stopLoading();
2139 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002140 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002141 getCurrentTopWebView().reload();
2142 }
2143 break;
2144
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002146 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002147 break;
2148
2149 case R.id.close_menu_id:
2150 // Close the subwindow if it exists.
2151 if (mTabControl.getCurrentSubWindow() != null) {
2152 dismissSubWindow(mTabControl.getCurrentTab());
2153 break;
2154 }
2155 closeCurrentTab();
2156 break;
2157
kaiyiza8b6dbb2013-07-29 18:11:22 +08002158 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002159 Object[] params = { new String("persist.debug.browsermonkeytest")};
2160 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002161 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002162 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002163 if (ret != null && ret.equals("enable"))
2164 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002165 if (BrowserConfig.getInstance(getContext())
2166 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2167 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002168 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002169 case R.id.homepage_menu_id:
2170 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002171 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002172 break;
2173
2174 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002175 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002176 break;
2177
2178 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002179 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002180 break;
2181
John Reck2bc80422011-06-30 15:11:49 -07002182 case R.id.save_snapshot_menu_id:
2183 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002184 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002185 createScreenshotAsync(
Pankaj Gargf49e0222015-09-01 12:19:13 -07002186 source,
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002187 new ValueCallback<Bitmap>() {
2188 @Override
2189 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002190 Bitmap bm = cropAndScaleBitmap(bitmap,
2191 getDesiredThumbnailWidth(mActivity),
2192 getDesiredThumbnailHeight(mActivity));
2193 new SaveSnapshotTask(source, bm).execute();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002194 }
2195 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002196 break;
2197
Michael Kolb8233fac2010-10-26 16:08:53 -07002198 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002199 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002200 break;
2201
John Recke1a03a32011-09-14 17:04:16 -07002202 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002203 // passing null to distinguish between
2204 // "go live" button and navigating a web page
2205 // on a snapshot tab
2206 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002207 case R.id.share_page_menu_id:
2208 Tab currentTab = mTabControl.getCurrentTab();
2209 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002210 return false;
2211 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002212 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002213 break;
2214
2215 case R.id.dump_nav_menu_id:
2216 getCurrentTopWebView().debugDump();
2217 break;
2218
Michael Kolb8233fac2010-10-26 16:08:53 -07002219 case R.id.zoom_in_menu_id:
2220 getCurrentTopWebView().zoomIn();
2221 break;
2222
2223 case R.id.zoom_out_menu_id:
2224 getCurrentTopWebView().zoomOut();
2225 break;
2226
2227 case R.id.view_downloads_menu_id:
2228 viewDownloads();
2229 break;
2230
John Reck42229bc2011-08-19 13:26:43 -07002231 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002232 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002233 break;
2234
Ze G Riande2a675c22015-06-03 11:15:24 -07002235 case R.id.reader_mode_menu_id:
2236 toggleReaderMode();
2237 break;
2238
Michael Kolb8233fac2010-10-26 16:08:53 -07002239 case R.id.window_one_menu_id:
2240 case R.id.window_two_menu_id:
2241 case R.id.window_three_menu_id:
2242 case R.id.window_four_menu_id:
2243 case R.id.window_five_menu_id:
2244 case R.id.window_six_menu_id:
2245 case R.id.window_seven_menu_id:
2246 case R.id.window_eight_menu_id:
2247 {
2248 int menuid = item.getItemId();
2249 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2250 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2251 Tab desiredTab = mTabControl.getTab(id);
2252 if (desiredTab != null &&
2253 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002254 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002255 }
2256 break;
2257 }
2258 }
2259 }
2260 break;
2261
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002262 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002263 Bundle bundle = new Bundle();
2264 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2265 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2266 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2267 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2268 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002269 break;
2270
Pankaj Garg49b79252014-11-07 17:33:41 -08002271 case R.id.add_to_homescreen:
2272 final WebView w = getCurrentTopWebView();
2273 final EditText input = new EditText(getContext());
2274 input.setText(w.getTitle());
2275 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002276 .setTitle(getContext().getResources().getString(
2277 R.string.add_to_homescreen))
2278 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002279 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002280 .setPositiveButton(getContext().getResources().getString(
2281 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002282 public void onClick(DialogInterface dialog, int whichButton) {
2283 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2284 getContext(),
2285 w.getUrl(),
2286 input.getText().toString(),
2287 w.getViewportBitmap(),
2288 w.getFavicon()));
2289
2290 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2291 .addCategory(Intent.CATEGORY_HOME));
2292 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002293 .setNegativeButton(getContext().getResources().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07002294 R.string.cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002295 public void onClick(DialogInterface dialog, int whichButton) {
2296 // Do nothing.
2297 }
2298 })
2299 .show();
2300 break;
2301
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 default:
2303 return false;
2304 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002305 return true;
2306 }
2307
John Reck68234a92012-04-19 15:27:12 -07002308 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2309 implements OnCancelListener {
2310
2311 private Tab mTab;
2312 private Dialog mProgressDialog;
2313 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002314 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002315
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002316 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002317 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002318 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002319 }
2320
2321 @Override
2322 protected void onPreExecute() {
2323 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2324 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2325 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002326 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002327 }
2328
2329 @Override
2330 protected Long doInBackground(Void... params) {
2331 if (!mTab.saveViewState(mValues)) {
2332 return null;
2333 }
2334 if (isCancelled()) {
2335 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2336 File file = mActivity.getFileStreamPath(path);
2337 if (!file.delete()) {
2338 file.deleteOnExit();
2339 }
2340 return null;
2341 }
2342 final ContentResolver cr = mActivity.getContentResolver();
2343 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2344 if (result == null) {
2345 return null;
2346 }
2347 long id = ContentUris.parseId(result);
2348 return id;
2349 }
2350
2351 @Override
2352 protected void onPostExecute(Long id) {
2353 if (isCancelled()) {
2354 return;
2355 }
2356 mProgressDialog.dismiss();
2357 if (id == null) {
2358 Toast.makeText(mActivity, R.string.snapshot_failed,
2359 Toast.LENGTH_SHORT).show();
2360 return;
2361 }
2362 Bundle b = new Bundle();
2363 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2364 mUi.showComboView(ComboViews.Snapshots, b);
2365 }
2366
2367 @Override
2368 public void onCancel(DialogInterface dialog) {
2369 cancel(true);
2370 }
2371 }
2372
Michael Kolb80f75082012-04-10 10:50:06 -07002373 @Override
2374 public void toggleUserAgent() {
2375 WebView web = getCurrentWebView();
2376 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002377 }
2378
Ze G Riande2a675c22015-06-03 11:15:24 -07002379 // This function calls the method in the webview to enable/disable
2380 // the reader mode through the DOM distiller
2381 public void toggleReaderMode() {
2382 Tab t = mTabControl.getCurrentTab();
2383 if (t.isDistilled()) {
2384 closeTab(t);
2385 } else if (t.isDistillable()) {
2386 openTab(t.getDistilledUrl(), false, true, false, t);
2387 }
2388 }
2389
Michael Kolb80f75082012-04-10 10:50:06 -07002390 @Override
2391 public void findOnPage() {
2392 getCurrentTopWebView().showFindDialog(null, true);
2393 }
2394
2395 @Override
2396 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002397 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002398 }
2399
2400 @Override
2401 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002402 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002403 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2404 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002405 }
2406 else {
2407 WebView w = getCurrentTopWebView();
2408 if (w == null)
2409 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002410 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002411 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002412 }
Michael Kolb80f75082012-04-10 10:50:06 -07002413 }
2414
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002415 public boolean goLive(String url) {
2416 if (!getCurrentTab().isSnapshot())
2417 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002418 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002419
2420 if (url == null) { // "go live" button was clicked
2421 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002422 closeTab(t);
2423 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002424 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002425 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002426 return true;
John Recke1a03a32011-09-14 17:04:16 -07002427 }
2428
luxiaolb40014b2013-07-19 10:01:43 +08002429 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002430 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002431 new AlertDialog.Builder(activity)
2432 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002433 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002434 .setMessage(R.string.exit_browser_msg)
2435 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2436 public void onClick(DialogInterface dialog, int which) {
2437 activity.moveTaskToBack(true);
2438 dialog.dismiss();
2439 }
2440 })
2441 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2442 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002443 mCrashRecoveryHandler.clearState(true);
2444 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002445 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002446 dialog.dismiss();
2447 }
2448 })
2449 .show();
2450 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002451
Michael Kolb315d5022011-10-13 12:47:11 -07002452 @Override
2453 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002454 }
2455
John Reck9c35b9c2012-05-30 10:08:50 -07002456 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002457 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002458 // Let the History and Bookmark fragments handle menus they created.
2459 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2460 return false;
2461 }
2462
Michael Kolb8233fac2010-10-26 16:08:53 -07002463 int id = item.getItemId();
2464 boolean result = true;
2465 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002466 // -- Browser context menu
2467 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002468 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002469 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002470 case R.id.copy_link_context_menu_id:
2471 final WebView webView = getCurrentTopWebView();
2472 if (null == webView) {
2473 result = false;
2474 break;
2475 }
2476 final HashMap<String, WebView> hrefMap =
2477 new HashMap<String, WebView>();
2478 hrefMap.put("webview", webView);
2479 final Message msg = mHandler.obtainMessage(
2480 FOCUS_NODE_HREF, id, 0, hrefMap);
2481 webView.requestFocusNodeHref(msg);
2482 break;
2483
2484 default:
2485 // For other context menus
2486 result = onOptionsItemSelected(item);
2487 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002488 return result;
2489 }
2490
2491 /**
2492 * support programmatically opening the context menu
2493 */
2494 public void openContextMenu(View view) {
2495 mActivity.openContextMenu(view);
2496 }
2497
2498 /**
2499 * programmatically open the options menu
2500 */
2501 public void openOptionsMenu() {
2502 mActivity.openOptionsMenu();
2503 }
2504
John Reck9c35b9c2012-05-30 10:08:50 -07002505 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002506 public boolean onMenuOpened(int featureId, Menu menu) {
2507 if (mOptionsMenuOpen) {
2508 if (mConfigChanged) {
2509 // We do not need to make any changes to the state of the
2510 // title bar, since the only thing that happened was a
2511 // change in orientation
2512 mConfigChanged = false;
2513 } else {
2514 if (!mExtendedMenuOpen) {
2515 mExtendedMenuOpen = true;
2516 mUi.onExtendedMenuOpened();
2517 } else {
2518 // Switching the menu back to icon view, so show the
2519 // title bar once again.
2520 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002521 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002522 }
2523 }
2524 } else {
2525 // The options menu is closed, so open it, and show the title
2526 mOptionsMenuOpen = true;
2527 mConfigChanged = false;
2528 mExtendedMenuOpen = false;
2529 mUi.onOptionsMenuOpened();
2530 }
2531 return true;
2532 }
2533
John Reck9c35b9c2012-05-30 10:08:50 -07002534 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002535 public void onOptionsMenuClosed(Menu menu) {
2536 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002537 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002538 }
2539
John Reck9c35b9c2012-05-30 10:08:50 -07002540 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002542 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002543 }
2544
2545 // Helper method for getting the top window.
2546 @Override
2547 public WebView getCurrentTopWebView() {
2548 return mTabControl.getCurrentTopWebView();
2549 }
2550
2551 @Override
2552 public WebView getCurrentWebView() {
2553 return mTabControl.getCurrentWebView();
2554 }
2555
2556 /*
2557 * This method is called as a result of the user selecting the options
2558 * menu to see the download window. It shows the download window on top of
2559 * the current window.
2560 */
2561 void viewDownloads() {
2562 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2563 mActivity.startActivity(intent);
2564 }
2565
John Reck30b065e2011-07-19 10:58:05 -07002566 int getActionModeHeight() {
2567 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2568 new int[] { android.R.attr.actionBarSize });
2569 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2570 actionBarSizeTypedArray.recycle();
2571 return size;
2572 }
2573
Michael Kolb8233fac2010-10-26 16:08:53 -07002574 // action mode
2575
John Reck9c35b9c2012-05-30 10:08:50 -07002576 @Override
2577 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 mUi.onActionModeStarted(mode);
2579 mActionMode = mode;
2580 }
2581
2582 /*
2583 * True if a custom ActionMode (i.e. find or select) is in use.
2584 */
2585 @Override
2586 public boolean isInCustomActionMode() {
2587 return mActionMode != null;
2588 }
2589
2590 /*
2591 * End the current ActionMode.
2592 */
2593 @Override
2594 public void endActionMode() {
2595 if (mActionMode != null) {
2596 mActionMode.finish();
2597 }
2598 }
2599
2600 /*
2601 * Called by find and select when they are finished. Replace title bars
2602 * as necessary.
2603 */
John Reck9c35b9c2012-05-30 10:08:50 -07002604 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 public void onActionModeFinished(ActionMode mode) {
2606 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002607 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 mActionMode = null;
2609 }
2610
2611 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002612 final Tab tab = getCurrentTab();
2613 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002614 }
2615
2616 // bookmark handling
2617
2618 /**
2619 * add the current page as a bookmark to the given folder id
2620 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002621 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002622 * bookmarked, and if it is, edit that bookmark. If false, and
2623 * the site is already bookmarked, do not attempt to edit the
2624 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002625 */
2626 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002627 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002628 WebView w = getCurrentTopWebView();
2629 if (w == null) {
2630 return null;
2631 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002632 Intent i = new Intent(mActivity,
2633 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002634 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2635 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002636 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002637 if (touchIconUrl != null) {
2638 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2639 WebSettings settings = w.getSettings();
2640 if (settings != null) {
2641 i.putExtra(AddBookmarkPage.USER_AGENT,
2642 settings.getUserAgentString());
2643 }
2644 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002645 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002646 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002647 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002648 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2649 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002650 // Put the dialog at the upper right of the screen, covering the
2651 // star on the title bar.
2652 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002653 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002654 }
2655
2656 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002657 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002658 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002659 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002660 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002661 }
2662
Vivek Sekharb54614f2014-05-01 19:03:37 -07002663 @Override
2664 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2665 boolean capture) {
2666 mUploadHandler = new UploadHandler(this);
2667 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2668 }
2669
Michael Kolb8233fac2010-10-26 16:08:53 -07002670 // thumbnails
2671
2672 /**
2673 * Return the desired width for thumbnail screenshots, which are stored in
2674 * the database, and used on the bookmarks screen.
2675 * @param context Context for finding out the density of the screen.
2676 * @return desired width for thumbnail screenshot.
2677 */
2678 static int getDesiredThumbnailWidth(Context context) {
2679 return context.getResources().getDimensionPixelOffset(
2680 R.dimen.bookmarkThumbnailWidth);
2681 }
2682
2683 /**
2684 * Return the desired height for thumbnail screenshots, which are stored in
2685 * the database, and used on the bookmarks screen.
2686 * @param context Context for finding out the density of the screen.
2687 * @return desired height for thumbnail screenshot.
2688 */
2689 static int getDesiredThumbnailHeight(Context context) {
2690 return context.getResources().getDimensionPixelOffset(
2691 R.dimen.bookmarkThumbnailHeight);
2692 }
2693
Pankaj Gargf49e0222015-09-01 12:19:13 -07002694 void createScreenshotAsync(Tab tab, final ValueCallback<Bitmap> cb) {
2695 if (tab == null) {
2696 cb.onReceiveValue(null);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002697 return;
2698 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002699
2700 tab.capture();
2701
2702 synchronized (mThumbnailCbList) {
2703 mThumbnailCbList.add(cb);
2704 }
2705 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002706
Michael Kolb8233fac2010-10-26 16:08:53 -07002707 private class Copy implements OnMenuItemClickListener {
2708 private CharSequence mText;
2709
John Reck9c35b9c2012-05-30 10:08:50 -07002710 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002711 public boolean onMenuItemClick(MenuItem item) {
2712 copy(mText);
2713 return true;
2714 }
2715
2716 public Copy(CharSequence toCopy) {
2717 mText = toCopy;
2718 }
2719 }
2720
Leon Scroggins63c02662010-11-18 15:16:27 -05002721 private static class Download implements OnMenuItemClickListener {
2722 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002723 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002724 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002725 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002726 private static final String FALLBACK_EXTENSION = "dat";
2727 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002728
John Reck9c35b9c2012-05-30 10:08:50 -07002729 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002731 if (DataUri.isDataUri(mText)) {
2732 saveDataUri();
2733 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002734 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08002735 null, null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002736 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 return true;
2738 }
2739
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002740 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2741 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002742 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002743 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002744 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002745 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002746 }
George Mount387d45d2011-10-07 15:57:53 -07002747
2748 /**
2749 * Treats mText as a data URI and writes its contents to a file
2750 * based on the current time.
2751 */
2752 private void saveDataUri() {
2753 FileOutputStream outputStream = null;
2754 try {
2755 DataUri uri = new DataUri(mText);
2756 File target = getTarget(uri);
2757 outputStream = new FileOutputStream(target);
2758 outputStream.write(uri.getData());
2759 final DownloadManager manager =
2760 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2761 manager.addCompletedDownload(target.getName(),
2762 mActivity.getTitle().toString(), false,
2763 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002764 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002765 } catch (IOException e) {
2766 Log.e(LOGTAG, "Could not save data URL");
2767 } finally {
2768 if (outputStream != null) {
2769 try {
2770 outputStream.close();
2771 } catch (IOException e) {
2772 // ignore close errors
2773 }
2774 }
2775 }
2776 }
2777
2778 /**
2779 * Creates a File based on the current time stamp and uses
2780 * the mime type of the DataUri to get the extension.
2781 */
2782 private File getTarget(DataUri uri) throws IOException {
2783 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002784 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002785 String nameBase = format.format(new Date());
2786 String mimeType = uri.getMimeType();
2787 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2788 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2789 if (extension == null) {
2790 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2791 extension = FALLBACK_EXTENSION;
2792 }
2793 extension = "." + extension; // createTempFile needs the '.'
2794 File targetFile = File.createTempFile(nameBase, extension, dir);
2795 return targetFile;
2796 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002797 }
2798
Cary Clark8974d282010-11-22 10:46:05 -05002799 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002800 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002801
John Reck9c35b9c2012-05-30 10:08:50 -07002802 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002803 public boolean onMenuItemClick(MenuItem item) {
2804 if (mWebView != null) {
2805 return mWebView.selectText();
2806 }
2807 return false;
2808 }
2809
2810 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002811 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002812 }
2813
2814 }
2815
Michael Kolb8233fac2010-10-26 16:08:53 -07002816 /********************** TODO: UI stuff *****************************/
2817
2818 // these methods have been copied, they still need to be cleaned up
2819
2820 /****************** tabs ***************************************************/
2821
2822 // basic tab interactions:
2823
2824 // it is assumed that tabcontrol already knows about the tab
2825 protected void addTab(Tab tab) {
2826 mUi.addTab(tab);
2827 }
2828
2829 protected void removeTab(Tab tab) {
2830 mUi.removeTab(tab);
2831 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002832 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002833 }
2834
Michael Kolbf2055602011-04-09 17:20:03 -07002835 @Override
2836 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002837 // monkey protection against delayed start
2838 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002839
2840 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2841 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002842 mTabControl.setCurrentTab(tab);
2843 // the tab is guaranteed to have a webview after setCurrentTab
2844 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002845
2846
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002847 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002848 //Purge active tabs
2849 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002850 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002851 }
2852
John Reck8bcafc12011-08-29 16:43:02 -07002853 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002854 Tab current = mTabControl.getCurrentTab();
2855 if (current != null
2856 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002857 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002858 }
2859 }
2860
John Reck26b18322011-06-21 13:08:58 -07002861 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002862 //Cancel navscreen request
2863 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002864 // Dismiss the subwindow if applicable.
2865 dismissSubWindow(appTab);
2866 // Since we might kill the WebView, remove it from the
2867 // content view first.
2868 mUi.detachTab(appTab);
2869 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002870 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002871 // TODO: analyze why the remove and add are necessary
2872 mUi.attachTab(appTab);
2873 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002874 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002875 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002876 } else {
2877 // If the tab was the current tab, we have to attach
2878 // it to the view system again.
2879 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002880 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002881 }
2882 }
2883
2884 // Remove the sub window if it exists. Also called by TabControl when the
2885 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002886 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002887 public void dismissSubWindow(Tab tab) {
2888 removeSubWindow(tab);
2889 // dismiss the subwindow. This will destroy the WebView.
2890 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002891 WebView wv = getCurrentTopWebView();
2892 if (wv != null) {
2893 wv.requestFocus();
2894 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002895 }
2896
2897 @Override
2898 public void removeSubWindow(Tab t) {
2899 if (t.getSubWebView() != null) {
2900 mUi.removeSubWindow(t.getSubViewContainer());
2901 }
2902 }
2903
2904 @Override
2905 public void attachSubWindow(Tab tab) {
2906 if (tab.getSubWebView() != null) {
2907 mUi.attachSubWindow(tab.getSubViewContainer());
2908 getCurrentTopWebView().requestFocus();
2909 }
2910 }
2911
Mathew Inwood29721c22011-06-29 17:55:24 +01002912 private Tab showPreloadedTab(final UrlData urlData) {
2913 if (!urlData.isPreloaded()) {
2914 return null;
2915 }
2916 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2917 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2918 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002919 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002920 // Could not submit query. Fallback to regular tab creation
2921 tabControl.destroy();
2922 return null;
2923 }
2924 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002925 // check tab count and make room for new tab
2926 if (!mTabControl.canCreateNewTab()) {
2927 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2928 if (leastUsed != null) {
2929 closeTab(leastUsed);
2930 }
2931 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002932 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002933 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002934 mTabControl.addPreloadedTab(t);
2935 addTab(t);
2936 setActiveTab(t);
2937 return t;
2938 }
2939
Michael Kolb7bcafde2011-05-09 13:55:59 -07002940 // open a non inconito tab with the given url data
2941 // and set as active tab
2942 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002943 Tab tab = showPreloadedTab(urlData);
2944 if (tab == null) {
2945 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002946 if ((tab != null) && !urlData.isEmpty()) {
2947 loadUrlDataIn(tab, urlData);
2948 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002949 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002950 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002951 }
2952
Michael Kolb843510f2010-12-09 10:51:49 -08002953 @Override
2954 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002955 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002956 }
2957
Michael Kolb8233fac2010-10-26 16:08:53 -07002958 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002959 public Tab openIncognitoTab() {
2960 return openTab(INCOGNITO_URI, true, true, false);
2961 }
2962
2963 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002964 public Tab openTab(String url, boolean incognito, boolean setActive,
2965 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002966 return openTab(url, incognito, setActive, useCurrent, null);
2967 }
2968
2969 @Override
2970 public Tab openTab(String url, Tab parent, boolean setActive,
2971 boolean useCurrent) {
2972 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2973 setActive, useCurrent, parent);
2974 }
2975
2976 public Tab openTab(String url, boolean incognito, boolean setActive,
2977 boolean useCurrent, Tab parent) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002978 boolean change_tabs = false;
2979 if (setActive) {
2980 Tab currentTab = mTabControl.getCurrentTab();
2981 if (currentTab != null) {
2982 change_tabs = setActive;
2983 setActive = false;
Sagar Dhawan9a1d8e72015-09-11 17:14:38 -07002984 if (mUi instanceof PhoneUi)
2985 currentTab.capture();
Pankaj Gargf49e0222015-09-01 12:19:13 -07002986 }
2987 }
2988
Michael Kolb7bcafde2011-05-09 13:55:59 -07002989 Tab tab = createNewTab(incognito, setActive, useCurrent);
2990 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002991 if (parent instanceof SnapshotTab) {
2992 addTab(tab);
2993 if (setActive)
2994 setActiveTab(tab);
2995 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002996 parent.addChildTab(tab);
2997 }
Michael Kolb519d2282011-05-09 17:03:19 -07002998 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002999 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003000 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003001 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07003002
3003 if (change_tabs) {
Sagar Dhawan20b1deb2015-10-12 11:13:18 -07003004 setActiveTab(tab);
Pankaj Gargf49e0222015-09-01 12:19:13 -07003005 synchronized (mThumbnailCbList) {
3006 startCaptureTimer();
3007 mLatestCreatedTab = tab;
3008 mThumbnailCbList.add(new ValueCallback<Bitmap>() {
3009 @Override
3010 public void onReceiveValue(Bitmap bitmap) {
3011 synchronized (mThumbnailCbList) {
3012 if (mLatestCreatedTab != null) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07003013 mLatestCreatedTab = null;
3014 }
3015 stopCaptureTimer();
3016 }
3017 }
3018 });
3019 }
3020 }
3021
Michael Kolb7bcafde2011-05-09 13:55:59 -07003022 return tab;
3023 }
3024
Pankaj Gargf49e0222015-09-01 12:19:13 -07003025 private void startCaptureTimer() {
3026 mCaptureTimer = new CountDownTimer(mCaptureMaxWaitMS, mCaptureMaxWaitMS) {
3027 @Override
3028 public void onTick(long millisUntilFinished) {
3029 // Do nothing
3030 }
3031
3032 @Override
3033 public void onFinish() {
3034 synchronized (mThumbnailCbList) {
3035 Log.e(LOGTAG, "Screen capture timed out while opening new tab");
3036 if (mLatestCreatedTab != null) {
3037 setActiveTab(mLatestCreatedTab);
3038 mLatestCreatedTab = null;
3039 }
3040 }
3041 }
3042 }.start();
3043 }
3044
3045 private void stopCaptureTimer() {
3046 if (mCaptureTimer != null) {
3047 mCaptureTimer.cancel();
3048 mCaptureTimer = null;
3049 }
3050 }
3051
3052
Michael Kolb7bcafde2011-05-09 13:55:59 -07003053 // this method will attempt to create a new tab
3054 // incognito: private browsing tab
3055 // setActive: ste tab as current tab
3056 // useCurrent: if no new tab can be created, return current tab
3057 private Tab createNewTab(boolean incognito, boolean setActive,
3058 boolean useCurrent) {
3059 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07003060 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
3061 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003062 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003063 if (mTabControl.canCreateNewTab()) {
3064 tab = mTabControl.createNewTab(incognito, !setActive);
3065 addTab(tab);
3066 if (setActive) {
3067 setActiveTab(tab);
3068 } else {
3069 tab.pause();
3070 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003071 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003072 if (useCurrent) {
3073 tab = mTabControl.getCurrentTab();
3074 reuseTab(tab, null);
3075 } else {
3076 mUi.showMaxTabsWarning();
3077 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003078 }
3079 }
3080 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003081 }
3082
John Reck2bc80422011-06-30 15:11:49 -07003083 @Override
3084 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3085 SnapshotTab tab = null;
3086 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07003087 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07003088 addTab(tab);
3089 if (setActive) {
3090 setActiveTab(tab);
3091 }
3092 } else {
3093 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003094 }
3095 return tab;
3096 }
3097
Michael Kolb8233fac2010-10-26 16:08:53 -07003098 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003099 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003100 * @return boolean True if we successfully switched to a different tab. If
3101 * the indexth tab is null, or if that tab is the same as
3102 * the current one, return false.
3103 */
3104 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003105 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003106 Tab currentTab = mTabControl.getCurrentTab();
3107 if (tab == null || tab == currentTab) {
3108 return false;
3109 }
3110 setActiveTab(tab);
3111 return true;
3112 }
3113
3114 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003115 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003116 closeCurrentTab(false);
3117 }
3118
3119 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003120 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003121 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003122 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003123 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003124 return;
3125 }
Michael Kolbc831b632011-05-11 09:30:34 -07003126 final Tab current = mTabControl.getCurrentTab();
3127 final int pos = mTabControl.getCurrentPosition();
3128 Tab newTab = current.getParent();
3129 if (newTab == null) {
3130 newTab = mTabControl.getTab(pos + 1);
3131 if (newTab == null) {
3132 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003133 }
3134 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003135 if (andQuit) {
3136 mTabControl.setCurrentTab(newTab);
3137 closeTab(current);
3138 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003139 // Close window
3140 closeTab(current);
3141 }
3142 }
3143
3144 /**
3145 * Close the tab, remove its associated title bar, and adjust mTabControl's
3146 * current tab to a valid value.
3147 */
3148 @Override
3149 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003150 if (tab == mTabControl.getCurrentTab()) {
3151 closeCurrentTab();
3152 } else {
3153 removeTab(tab);
3154 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003155 }
3156
Afzal Najamd4e33312012-04-26 01:54:01 -04003157 /**
3158 * Close all tabs except the current one
3159 */
3160 @Override
3161 public void closeOtherTabs() {
3162 int inactiveTabs = mTabControl.getTabCount() - 1;
3163 for (int i = inactiveTabs; i >= 0; i--) {
3164 Tab tab = mTabControl.getTab(i);
3165 if (tab != mTabControl.getCurrentTab()) {
3166 removeTab(tab);
3167 }
3168 }
3169 }
3170
Michael Kolb8233fac2010-10-26 16:08:53 -07003171 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003172 protected void loadUrlFromContext(String url) {
3173 Tab tab = getCurrentTab();
3174 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003175 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003176 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003177 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003178 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003179 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003180 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003181 }
3182 }
3183 }
3184
3185 /**
3186 * Load the URL into the given WebView and update the title bar
3187 * to reflect the new load. Call this instead of WebView.loadUrl
3188 * directly.
3189 * @param view The WebView used to load url.
3190 * @param url The URL to load.
3191 */
John Reck71e51422011-07-01 16:49:28 -07003192 @Override
3193 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003194 loadUrl(tab, url, null);
3195 }
3196
3197 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3198 if (tab != null) {
3199 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003200 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003201 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003202 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003203 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003204 }
3205
3206 /**
3207 * Load UrlData into a Tab and update the title bar to reflect the new
3208 * load. Call this instead of UrlData.loadIn directly.
3209 * @param t The Tab used to load.
3210 * @param data The UrlData being loaded.
3211 */
3212 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003213 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003214 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003215 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003216 } else {
John Reck38b39652012-06-05 09:22:59 -07003217 if (t != null && data.mDisableUrlOverride) {
3218 t.disableUrlOverridingForLoad();
3219 }
John Reck26b18322011-06-21 13:08:58 -07003220 loadUrl(t, data.mUrl, data.mHeaders);
3221 }
3222 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003223 }
3224
John Reck30c714c2010-12-16 17:30:34 -08003225 @Override
3226 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003227 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003228 //In case of tab can go back (aka tab has navigation entry) do nothing
3229 //else just load homepage in current tab.
3230 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003231 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003232 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003233 }
3234
3235 void goBackOnePageOrQuit() {
3236 Tab current = mTabControl.getCurrentTab();
3237 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003238 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3239 showExitDialog(mActivity);
3240 } else {
3241 /*
3242 * Instead of finishing the activity, simply push this to the back
3243 * of the stack and let ActivityManager to choose the foreground
3244 * activity. As BrowserActivity is singleTask, it will be always the
3245 * root of the task. So we can use either true or false for
3246 * moveTaskToBack().
3247 */
3248 mActivity.moveTaskToBack(true);
3249 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003250 return;
3251 }
John Reckef654f12011-07-12 16:42:08 -07003252 if (current.canGoBack()) {
3253 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003254 } else {
3255 // Check to see if we are closing a window that was created by
3256 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003257 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003258 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003259 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003260 // Now we close the other tab
3261 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003262 } else if (BrowserConfig.getInstance(getContext())
3263 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3264 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003265 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003266 /*
3267 * Instead of finishing the activity, simply push this to the back
3268 * of the stack and let ActivityManager to choose the foreground
3269 * activity. As BrowserActivity is singleTask, it will be always the
3270 * root of the task. So we can use either true or false for
3271 * moveTaskToBack().
3272 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003273 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003274 }
3275 }
3276 }
3277
3278 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003279 * helper method for key handler
3280 * returns the current tab if it can't advance
3281 */
Michael Kolbc831b632011-05-11 09:30:34 -07003282 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003283 int pos = mTabControl.getCurrentPosition() + 1;
3284 if (pos >= mTabControl.getTabCount()) {
3285 pos = 0;
3286 }
3287 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003288 }
3289
3290 /**
3291 * helper method for key handler
3292 * returns the current tab if it can't advance
3293 */
Michael Kolbc831b632011-05-11 09:30:34 -07003294 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003295 int pos = mTabControl.getCurrentPosition() - 1;
3296 if ( pos < 0) {
3297 pos = mTabControl.getTabCount() - 1;
3298 }
3299 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003300 }
3301
John Reckbcef87f2012-02-03 14:58:34 -08003302 boolean isMenuOrCtrlKey(int keyCode) {
3303 return (KeyEvent.KEYCODE_MENU == keyCode)
3304 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3305 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3306 }
3307
Michael Kolb0035fad2011-03-14 13:25:28 -07003308 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003309 * handle key events in browser
3310 *
3311 * @param keyCode
3312 * @param event
3313 * @return true if handled, false to pass to super
3314 */
John Reck9c35b9c2012-05-30 10:08:50 -07003315 @Override
3316 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003317 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3318 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003319 if (!mUi.isComboViewShowing()) {
3320 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3321 true, false);
3322 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003323 return true;
3324 }
3325
Cary Clark160bbb92011-01-10 11:17:07 -05003326 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003327 // Even if MENU is already held down, we need to call to super to open
3328 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003329 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003330 mMenuIsDown = true;
3331 return false;
3332 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003333
Cary Clark8ff8c662010-12-29 15:03:05 -05003334 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003335 Tab tab = getCurrentTab();
3336 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003337
Cary Clark160bbb92011-01-10 11:17:07 -05003338 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3339 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003340
Michael Kolb8233fac2010-10-26 16:08:53 -07003341 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003342 case KeyEvent.KEYCODE_TAB:
3343 if (event.isCtrlPressed()) {
3344 if (event.isShiftPressed()) {
3345 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003346 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003347 } else {
3348 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003349 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003350 }
3351 return true;
3352 }
3353 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003354 case KeyEvent.KEYCODE_SPACE:
3355 // WebView/WebTextView handle the keys in the KeyDown. As
3356 // the Activity's shortcut keys are only handled when WebView
3357 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003358 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003359 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003360 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003361 pageDown();
3362 }
3363 return true;
3364 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003365 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003366 event.startTracking();
3367 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003368 case KeyEvent.KEYCODE_FORWARD:
3369 if (!noModifiers) break;
3370 tab.goForward();
3371 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003372 case KeyEvent.KEYCODE_DPAD_LEFT:
3373 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003374 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003375 return true;
3376 }
3377 break;
3378 case KeyEvent.KEYCODE_DPAD_RIGHT:
3379 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003380 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003381 return true;
3382 }
3383 break;
3384 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003385 if (ctrl) {
3386 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003387 return true;
3388 }
3389 break;
Michael Kolba4183062011-01-16 10:43:21 -08003390// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003391 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003392 if (ctrl ) {
3393 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003394 return true;
3395 }
3396 break;
Michael Kolba4183062011-01-16 10:43:21 -08003397// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003398// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003399// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003400// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003401// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003402// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003403// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003404// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003405// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003406// case KeyEvent.KEYCODE_M: // unused
3407// case KeyEvent.KEYCODE_N: // in Chrome: new window
3408// case KeyEvent.KEYCODE_O: // in Chrome: open file
3409// case KeyEvent.KEYCODE_P: // in Chrome: print page
3410// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003411// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003412// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3413 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003414 // we can't use the ctrl/shift flags, they check for
3415 // exclusive use of a modifier
3416 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003417 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003418 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003419 } else {
3420 openTabToHomePage();
3421 }
3422 return true;
3423 }
3424 break;
3425// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3426// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003427// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003428// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3429// case KeyEvent.KEYCODE_Y: // unused
3430// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003431 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003432 // it is a regular key and webview is not null
3433 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003434 }
3435
John Reck9c35b9c2012-05-30 10:08:50 -07003436 @Override
3437 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003438 switch(keyCode) {
3439 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003440 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003441 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003442 return true;
3443 }
3444 break;
3445 }
3446 return false;
3447 }
3448
John Reck9c35b9c2012-05-30 10:08:50 -07003449 @Override
3450 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003451 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003452 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003453 if (KeyEvent.KEYCODE_MENU == keyCode
3454 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003455 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003456 }
3457 }
Cary Clark160bbb92011-01-10 11:17:07 -05003458 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003459 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003460 case KeyEvent.KEYCODE_BACK:
3461 if (event.isTracking() && !event.isCanceled()) {
3462 onBackKey();
3463 return true;
3464 }
3465 break;
3466 }
3467 return false;
3468 }
3469
3470 public boolean isMenuDown() {
3471 return mMenuIsDown;
3472 }
3473
John Reck9c35b9c2012-05-30 10:08:50 -07003474 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003475 public void setupAutoFill(Message message) {
3476 // Open the settings activity at the AutoFill profile fragment so that
3477 // the user can create a new profile. When they return, we will dispatch
3478 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003479 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003480 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3481 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003482 }
John Reckb3417f02011-01-14 11:01:05 -08003483
John Reck9c35b9c2012-05-30 10:08:50 -07003484 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003485 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003486 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003487 return true;
3488 }
3489
John Reck1cf4b792011-07-26 10:22:22 -07003490 @Override
3491 public boolean shouldCaptureThumbnails() {
3492 return mUi.shouldCaptureThumbnails();
3493 }
3494
Michael Kolbc3af0672011-08-09 10:24:41 -07003495 @Override
Pankaj Gargf49e0222015-09-01 12:19:13 -07003496 public void onThumbnailCapture(Bitmap bm) {
3497 synchronized (mThumbnailCbList) {
3498 int num_entries = mThumbnailCbList.size();
3499 while (num_entries > 0){
3500 num_entries--;
3501 ValueCallback<Bitmap> cb = mThumbnailCbList.get(num_entries);
3502 cb.onReceiveValue(bm);
3503 }
3504 mThumbnailCbList.clear();
3505 }
3506 }
3507
3508 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003509 public boolean supportsVoice() {
3510 PackageManager pm = mActivity.getPackageManager();
3511 List activities = pm.queryIntentActivities(new Intent(
3512 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3513 return activities.size() != 0;
3514 }
3515
3516 @Override
3517 public void startVoiceRecognizer() {
3518 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003519 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003520 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3521 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3522 mActivity.startActivityForResult(voice, VOICE_RESULT);
3523 }
3524
Pankaj Garg7b279f62014-08-12 14:47:18 -07003525 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003526 if (mLevel == level)
3527 return;
3528 mLevel = level;
3529 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003530 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3531 lp.dimAmount = level;
3532 mActivity.getWindow().setAttributes(lp);
3533 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3534 } else {
3535 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3536 }
3537 }
3538
Michael Kolb0b129122012-06-04 16:31:58 -07003539 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003540 public void setBlockEvents(boolean block) {
3541 mBlockEvents = block;
3542 }
3543
John Reck9c35b9c2012-05-30 10:08:50 -07003544 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003545 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003546 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003547 }
3548
John Reck9c35b9c2012-05-30 10:08:50 -07003549 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003550 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003551 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003552 }
3553
John Reck9c35b9c2012-05-30 10:08:50 -07003554 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003555 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003556 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003557 }
3558
John Reck9c35b9c2012-05-30 10:08:50 -07003559 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003560 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003561 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003562 }
3563
John Reck9c35b9c2012-05-30 10:08:50 -07003564 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003565 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003566 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003567 }
3568
Pankaj Garg49b79252014-11-07 17:33:41 -08003569 @Override
3570 public boolean shouldShowAppMenu() {
3571 return true;
3572 }
3573
3574 @Override
3575 public int getMenuThemeResourceId() {
3576 return R.style.OverflowMenuTheme;
3577 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003578}