blob: f6a9aa8bf617a6e54cba25de868cf8fbef194f95 [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
jrizzolidb51f632016-03-22 15:57:18 +01002120 case R.id.close_other_tabs_id:
2121 closeOtherTabs();
2122 break;
2123
Michael Kolb8233fac2010-10-26 16:08:53 -07002124 case R.id.goto_menu_id:
2125 editUrl();
2126 break;
2127
2128 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002129 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2130 break;
2131
Michael Kolb315d5022011-10-13 12:47:11 -07002132 case R.id.snapshots_menu_id:
2133 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002134 break;
2135
Pankaj Garg49b79252014-11-07 17:33:41 -08002136 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002137 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002138 break;
2139
2140 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002141 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002142 stopLoading();
2143 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002144 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 getCurrentTopWebView().reload();
2146 }
2147 break;
2148
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002150 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002151 break;
2152
2153 case R.id.close_menu_id:
2154 // Close the subwindow if it exists.
2155 if (mTabControl.getCurrentSubWindow() != null) {
2156 dismissSubWindow(mTabControl.getCurrentTab());
2157 break;
2158 }
2159 closeCurrentTab();
2160 break;
2161
kaiyiza8b6dbb2013-07-29 18:11:22 +08002162 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002163 Object[] params = { new String("persist.debug.browsermonkeytest")};
2164 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002165 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002166 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002167 if (ret != null && ret.equals("enable"))
2168 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002169 if (BrowserConfig.getInstance(getContext())
2170 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2171 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002172 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002173 case R.id.homepage_menu_id:
2174 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002175 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002176 break;
2177
2178 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002179 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002180 break;
2181
2182 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002183 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002184 break;
2185
John Reck2bc80422011-06-30 15:11:49 -07002186 case R.id.save_snapshot_menu_id:
2187 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002188 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002189 createScreenshotAsync(
Pankaj Gargf49e0222015-09-01 12:19:13 -07002190 source,
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002191 new ValueCallback<Bitmap>() {
2192 @Override
2193 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002194 Bitmap bm = cropAndScaleBitmap(bitmap,
2195 getDesiredThumbnailWidth(mActivity),
2196 getDesiredThumbnailHeight(mActivity));
2197 new SaveSnapshotTask(source, bm).execute();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002198 }
2199 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002200 break;
2201
Michael Kolb8233fac2010-10-26 16:08:53 -07002202 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002203 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002204 break;
2205
John Recke1a03a32011-09-14 17:04:16 -07002206 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002207 // passing null to distinguish between
2208 // "go live" button and navigating a web page
2209 // on a snapshot tab
2210 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002211 case R.id.share_page_menu_id:
2212 Tab currentTab = mTabControl.getCurrentTab();
2213 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002214 return false;
2215 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002216 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002217 break;
2218
2219 case R.id.dump_nav_menu_id:
2220 getCurrentTopWebView().debugDump();
2221 break;
2222
Michael Kolb8233fac2010-10-26 16:08:53 -07002223 case R.id.zoom_in_menu_id:
2224 getCurrentTopWebView().zoomIn();
2225 break;
2226
2227 case R.id.zoom_out_menu_id:
2228 getCurrentTopWebView().zoomOut();
2229 break;
2230
2231 case R.id.view_downloads_menu_id:
2232 viewDownloads();
2233 break;
2234
John Reck42229bc2011-08-19 13:26:43 -07002235 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002236 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002237 break;
2238
Ze G Riande2a675c22015-06-03 11:15:24 -07002239 case R.id.reader_mode_menu_id:
2240 toggleReaderMode();
2241 break;
2242
Michael Kolb8233fac2010-10-26 16:08:53 -07002243 case R.id.window_one_menu_id:
2244 case R.id.window_two_menu_id:
2245 case R.id.window_three_menu_id:
2246 case R.id.window_four_menu_id:
2247 case R.id.window_five_menu_id:
2248 case R.id.window_six_menu_id:
2249 case R.id.window_seven_menu_id:
2250 case R.id.window_eight_menu_id:
2251 {
2252 int menuid = item.getItemId();
2253 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2254 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2255 Tab desiredTab = mTabControl.getTab(id);
2256 if (desiredTab != null &&
2257 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002258 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002259 }
2260 break;
2261 }
2262 }
2263 }
2264 break;
2265
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002266 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002267 Bundle bundle = new Bundle();
2268 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2269 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2270 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2271 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2272 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002273 break;
2274
Pankaj Garg49b79252014-11-07 17:33:41 -08002275 case R.id.add_to_homescreen:
2276 final WebView w = getCurrentTopWebView();
2277 final EditText input = new EditText(getContext());
2278 input.setText(w.getTitle());
2279 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002280 .setTitle(getContext().getResources().getString(
2281 R.string.add_to_homescreen))
2282 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002283 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002284 .setPositiveButton(getContext().getResources().getString(
2285 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002286 public void onClick(DialogInterface dialog, int whichButton) {
2287 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2288 getContext(),
2289 w.getUrl(),
2290 input.getText().toString(),
2291 w.getViewportBitmap(),
2292 w.getFavicon()));
2293
2294 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2295 .addCategory(Intent.CATEGORY_HOME));
2296 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002297 .setNegativeButton(getContext().getResources().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07002298 R.string.cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002299 public void onClick(DialogInterface dialog, int whichButton) {
2300 // Do nothing.
2301 }
2302 })
2303 .show();
2304 break;
2305
Michael Kolb8233fac2010-10-26 16:08:53 -07002306 default:
2307 return false;
2308 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002309 return true;
2310 }
2311
John Reck68234a92012-04-19 15:27:12 -07002312 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2313 implements OnCancelListener {
2314
2315 private Tab mTab;
2316 private Dialog mProgressDialog;
2317 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002318 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002319
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002320 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002321 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002322 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002323 }
2324
2325 @Override
2326 protected void onPreExecute() {
2327 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2328 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2329 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002330 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002331 }
2332
2333 @Override
2334 protected Long doInBackground(Void... params) {
2335 if (!mTab.saveViewState(mValues)) {
2336 return null;
2337 }
2338 if (isCancelled()) {
2339 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2340 File file = mActivity.getFileStreamPath(path);
2341 if (!file.delete()) {
2342 file.deleteOnExit();
2343 }
2344 return null;
2345 }
2346 final ContentResolver cr = mActivity.getContentResolver();
2347 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2348 if (result == null) {
2349 return null;
2350 }
2351 long id = ContentUris.parseId(result);
2352 return id;
2353 }
2354
2355 @Override
2356 protected void onPostExecute(Long id) {
2357 if (isCancelled()) {
2358 return;
2359 }
2360 mProgressDialog.dismiss();
2361 if (id == null) {
2362 Toast.makeText(mActivity, R.string.snapshot_failed,
2363 Toast.LENGTH_SHORT).show();
2364 return;
2365 }
2366 Bundle b = new Bundle();
2367 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2368 mUi.showComboView(ComboViews.Snapshots, b);
2369 }
2370
2371 @Override
2372 public void onCancel(DialogInterface dialog) {
2373 cancel(true);
2374 }
2375 }
2376
Michael Kolb80f75082012-04-10 10:50:06 -07002377 @Override
2378 public void toggleUserAgent() {
2379 WebView web = getCurrentWebView();
2380 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002381 }
2382
Ze G Riande2a675c22015-06-03 11:15:24 -07002383 // This function calls the method in the webview to enable/disable
2384 // the reader mode through the DOM distiller
2385 public void toggleReaderMode() {
2386 Tab t = mTabControl.getCurrentTab();
2387 if (t.isDistilled()) {
2388 closeTab(t);
2389 } else if (t.isDistillable()) {
2390 openTab(t.getDistilledUrl(), false, true, false, t);
2391 }
2392 }
2393
Michael Kolb80f75082012-04-10 10:50:06 -07002394 @Override
2395 public void findOnPage() {
2396 getCurrentTopWebView().showFindDialog(null, true);
2397 }
2398
2399 @Override
2400 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002401 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002402 }
2403
2404 @Override
2405 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002406 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002407 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2408 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002409 }
2410 else {
2411 WebView w = getCurrentTopWebView();
2412 if (w == null)
2413 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002414 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002415 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002416 }
Michael Kolb80f75082012-04-10 10:50:06 -07002417 }
2418
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002419 public boolean goLive(String url) {
2420 if (!getCurrentTab().isSnapshot())
2421 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002422 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002423
2424 if (url == null) { // "go live" button was clicked
2425 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002426 closeTab(t);
2427 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002428 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002429 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002430 return true;
John Recke1a03a32011-09-14 17:04:16 -07002431 }
2432
luxiaolb40014b2013-07-19 10:01:43 +08002433 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002434 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002435 new AlertDialog.Builder(activity)
2436 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002437 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002438 .setMessage(R.string.exit_browser_msg)
2439 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2440 public void onClick(DialogInterface dialog, int which) {
2441 activity.moveTaskToBack(true);
2442 dialog.dismiss();
2443 }
2444 })
2445 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2446 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002447 mCrashRecoveryHandler.clearState(true);
2448 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002449 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002450 dialog.dismiss();
2451 }
2452 })
2453 .show();
2454 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002455
Michael Kolb315d5022011-10-13 12:47:11 -07002456 @Override
2457 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002458 }
2459
John Reck9c35b9c2012-05-30 10:08:50 -07002460 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002461 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002462 // Let the History and Bookmark fragments handle menus they created.
2463 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2464 return false;
2465 }
2466
Michael Kolb8233fac2010-10-26 16:08:53 -07002467 int id = item.getItemId();
2468 boolean result = true;
2469 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002470 // -- Browser context menu
2471 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002472 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002473 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002474 case R.id.copy_link_context_menu_id:
2475 final WebView webView = getCurrentTopWebView();
2476 if (null == webView) {
2477 result = false;
2478 break;
2479 }
2480 final HashMap<String, WebView> hrefMap =
2481 new HashMap<String, WebView>();
2482 hrefMap.put("webview", webView);
2483 final Message msg = mHandler.obtainMessage(
2484 FOCUS_NODE_HREF, id, 0, hrefMap);
2485 webView.requestFocusNodeHref(msg);
2486 break;
2487
2488 default:
2489 // For other context menus
2490 result = onOptionsItemSelected(item);
2491 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002492 return result;
2493 }
2494
2495 /**
2496 * support programmatically opening the context menu
2497 */
2498 public void openContextMenu(View view) {
2499 mActivity.openContextMenu(view);
2500 }
2501
2502 /**
2503 * programmatically open the options menu
2504 */
2505 public void openOptionsMenu() {
2506 mActivity.openOptionsMenu();
2507 }
2508
John Reck9c35b9c2012-05-30 10:08:50 -07002509 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002510 public boolean onMenuOpened(int featureId, Menu menu) {
2511 if (mOptionsMenuOpen) {
2512 if (mConfigChanged) {
2513 // We do not need to make any changes to the state of the
2514 // title bar, since the only thing that happened was a
2515 // change in orientation
2516 mConfigChanged = false;
2517 } else {
2518 if (!mExtendedMenuOpen) {
2519 mExtendedMenuOpen = true;
2520 mUi.onExtendedMenuOpened();
2521 } else {
2522 // Switching the menu back to icon view, so show the
2523 // title bar once again.
2524 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002525 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002526 }
2527 }
2528 } else {
2529 // The options menu is closed, so open it, and show the title
2530 mOptionsMenuOpen = true;
2531 mConfigChanged = false;
2532 mExtendedMenuOpen = false;
2533 mUi.onOptionsMenuOpened();
2534 }
2535 return true;
2536 }
2537
John Reck9c35b9c2012-05-30 10:08:50 -07002538 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002539 public void onOptionsMenuClosed(Menu menu) {
2540 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002541 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002542 }
2543
John Reck9c35b9c2012-05-30 10:08:50 -07002544 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002545 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002546 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002547 }
2548
2549 // Helper method for getting the top window.
2550 @Override
2551 public WebView getCurrentTopWebView() {
2552 return mTabControl.getCurrentTopWebView();
2553 }
2554
2555 @Override
2556 public WebView getCurrentWebView() {
2557 return mTabControl.getCurrentWebView();
2558 }
2559
2560 /*
2561 * This method is called as a result of the user selecting the options
2562 * menu to see the download window. It shows the download window on top of
2563 * the current window.
2564 */
2565 void viewDownloads() {
2566 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2567 mActivity.startActivity(intent);
2568 }
2569
John Reck30b065e2011-07-19 10:58:05 -07002570 int getActionModeHeight() {
2571 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2572 new int[] { android.R.attr.actionBarSize });
2573 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2574 actionBarSizeTypedArray.recycle();
2575 return size;
2576 }
2577
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 // action mode
2579
John Reck9c35b9c2012-05-30 10:08:50 -07002580 @Override
2581 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002582 mUi.onActionModeStarted(mode);
2583 mActionMode = mode;
2584 }
2585
2586 /*
2587 * True if a custom ActionMode (i.e. find or select) is in use.
2588 */
2589 @Override
2590 public boolean isInCustomActionMode() {
2591 return mActionMode != null;
2592 }
2593
2594 /*
2595 * End the current ActionMode.
2596 */
2597 @Override
2598 public void endActionMode() {
2599 if (mActionMode != null) {
2600 mActionMode.finish();
2601 }
2602 }
2603
2604 /*
2605 * Called by find and select when they are finished. Replace title bars
2606 * as necessary.
2607 */
John Reck9c35b9c2012-05-30 10:08:50 -07002608 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002609 public void onActionModeFinished(ActionMode mode) {
2610 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002611 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 mActionMode = null;
2613 }
2614
2615 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002616 final Tab tab = getCurrentTab();
2617 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002618 }
2619
2620 // bookmark handling
2621
2622 /**
2623 * add the current page as a bookmark to the given folder id
2624 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002625 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002626 * bookmarked, and if it is, edit that bookmark. If false, and
2627 * the site is already bookmarked, do not attempt to edit the
2628 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002629 */
2630 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002631 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002632 WebView w = getCurrentTopWebView();
2633 if (w == null) {
2634 return null;
2635 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002636 Intent i = new Intent(mActivity,
2637 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002638 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2639 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002640 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002641 if (touchIconUrl != null) {
2642 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2643 WebSettings settings = w.getSettings();
2644 if (settings != null) {
2645 i.putExtra(AddBookmarkPage.USER_AGENT,
2646 settings.getUserAgentString());
2647 }
2648 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002649 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002650 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002651 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002652 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2653 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002654 // Put the dialog at the upper right of the screen, covering the
2655 // star on the title bar.
2656 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002657 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002658 }
2659
2660 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002661 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002662 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002663 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002664 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002665 }
2666
Vivek Sekharb54614f2014-05-01 19:03:37 -07002667 @Override
2668 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2669 boolean capture) {
2670 mUploadHandler = new UploadHandler(this);
2671 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2672 }
2673
Michael Kolb8233fac2010-10-26 16:08:53 -07002674 // thumbnails
2675
2676 /**
2677 * Return the desired width for thumbnail screenshots, which are stored in
2678 * the database, and used on the bookmarks screen.
2679 * @param context Context for finding out the density of the screen.
2680 * @return desired width for thumbnail screenshot.
2681 */
2682 static int getDesiredThumbnailWidth(Context context) {
2683 return context.getResources().getDimensionPixelOffset(
2684 R.dimen.bookmarkThumbnailWidth);
2685 }
2686
2687 /**
2688 * Return the desired height for thumbnail screenshots, which are stored in
2689 * the database, and used on the bookmarks screen.
2690 * @param context Context for finding out the density of the screen.
2691 * @return desired height for thumbnail screenshot.
2692 */
2693 static int getDesiredThumbnailHeight(Context context) {
2694 return context.getResources().getDimensionPixelOffset(
2695 R.dimen.bookmarkThumbnailHeight);
2696 }
2697
Pankaj Gargf49e0222015-09-01 12:19:13 -07002698 void createScreenshotAsync(Tab tab, final ValueCallback<Bitmap> cb) {
2699 if (tab == null) {
2700 cb.onReceiveValue(null);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002701 return;
2702 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002703
2704 tab.capture();
2705
2706 synchronized (mThumbnailCbList) {
2707 mThumbnailCbList.add(cb);
2708 }
2709 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002710
Michael Kolb8233fac2010-10-26 16:08:53 -07002711 private class Copy implements OnMenuItemClickListener {
2712 private CharSequence mText;
2713
John Reck9c35b9c2012-05-30 10:08:50 -07002714 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002715 public boolean onMenuItemClick(MenuItem item) {
2716 copy(mText);
2717 return true;
2718 }
2719
2720 public Copy(CharSequence toCopy) {
2721 mText = toCopy;
2722 }
2723 }
2724
Leon Scroggins63c02662010-11-18 15:16:27 -05002725 private static class Download implements OnMenuItemClickListener {
2726 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002727 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002728 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002729 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002730 private static final String FALLBACK_EXTENSION = "dat";
2731 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002732
John Reck9c35b9c2012-05-30 10:08:50 -07002733 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002734 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002735 if (DataUri.isDataUri(mText)) {
2736 saveDataUri();
2737 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002738 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08002739 null, null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002740 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002741 return true;
2742 }
2743
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002744 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2745 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002746 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002747 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002748 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002749 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002750 }
George Mount387d45d2011-10-07 15:57:53 -07002751
2752 /**
2753 * Treats mText as a data URI and writes its contents to a file
2754 * based on the current time.
2755 */
2756 private void saveDataUri() {
2757 FileOutputStream outputStream = null;
2758 try {
2759 DataUri uri = new DataUri(mText);
2760 File target = getTarget(uri);
2761 outputStream = new FileOutputStream(target);
2762 outputStream.write(uri.getData());
2763 final DownloadManager manager =
2764 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2765 manager.addCompletedDownload(target.getName(),
2766 mActivity.getTitle().toString(), false,
2767 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002768 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002769 } catch (IOException e) {
2770 Log.e(LOGTAG, "Could not save data URL");
2771 } finally {
2772 if (outputStream != null) {
2773 try {
2774 outputStream.close();
2775 } catch (IOException e) {
2776 // ignore close errors
2777 }
2778 }
2779 }
2780 }
2781
2782 /**
2783 * Creates a File based on the current time stamp and uses
2784 * the mime type of the DataUri to get the extension.
2785 */
2786 private File getTarget(DataUri uri) throws IOException {
2787 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002788 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002789 String nameBase = format.format(new Date());
2790 String mimeType = uri.getMimeType();
2791 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2792 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2793 if (extension == null) {
2794 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2795 extension = FALLBACK_EXTENSION;
2796 }
2797 extension = "." + extension; // createTempFile needs the '.'
2798 File targetFile = File.createTempFile(nameBase, extension, dir);
2799 return targetFile;
2800 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002801 }
2802
Cary Clark8974d282010-11-22 10:46:05 -05002803 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002804 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002805
John Reck9c35b9c2012-05-30 10:08:50 -07002806 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002807 public boolean onMenuItemClick(MenuItem item) {
2808 if (mWebView != null) {
2809 return mWebView.selectText();
2810 }
2811 return false;
2812 }
2813
2814 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002815 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002816 }
2817
2818 }
2819
Michael Kolb8233fac2010-10-26 16:08:53 -07002820 /********************** TODO: UI stuff *****************************/
2821
2822 // these methods have been copied, they still need to be cleaned up
2823
2824 /****************** tabs ***************************************************/
2825
2826 // basic tab interactions:
2827
2828 // it is assumed that tabcontrol already knows about the tab
2829 protected void addTab(Tab tab) {
2830 mUi.addTab(tab);
2831 }
2832
2833 protected void removeTab(Tab tab) {
2834 mUi.removeTab(tab);
2835 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002836 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002837 }
2838
Michael Kolbf2055602011-04-09 17:20:03 -07002839 @Override
2840 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002841 // monkey protection against delayed start
2842 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002843
2844 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2845 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002846 mTabControl.setCurrentTab(tab);
2847 // the tab is guaranteed to have a webview after setCurrentTab
2848 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002849
2850
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002851 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002852 //Purge active tabs
2853 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002854 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002855 }
2856
John Reck8bcafc12011-08-29 16:43:02 -07002857 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002858 Tab current = mTabControl.getCurrentTab();
2859 if (current != null
2860 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002861 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002862 }
2863 }
2864
John Reck26b18322011-06-21 13:08:58 -07002865 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002866 //Cancel navscreen request
2867 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002868 // Dismiss the subwindow if applicable.
2869 dismissSubWindow(appTab);
2870 // Since we might kill the WebView, remove it from the
2871 // content view first.
2872 mUi.detachTab(appTab);
2873 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002874 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002875 // TODO: analyze why the remove and add are necessary
2876 mUi.attachTab(appTab);
2877 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002878 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002879 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002880 } else {
2881 // If the tab was the current tab, we have to attach
2882 // it to the view system again.
2883 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002884 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002885 }
2886 }
2887
2888 // Remove the sub window if it exists. Also called by TabControl when the
2889 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002890 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002891 public void dismissSubWindow(Tab tab) {
2892 removeSubWindow(tab);
2893 // dismiss the subwindow. This will destroy the WebView.
2894 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002895 WebView wv = getCurrentTopWebView();
2896 if (wv != null) {
2897 wv.requestFocus();
2898 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002899 }
2900
2901 @Override
2902 public void removeSubWindow(Tab t) {
2903 if (t.getSubWebView() != null) {
2904 mUi.removeSubWindow(t.getSubViewContainer());
2905 }
2906 }
2907
2908 @Override
2909 public void attachSubWindow(Tab tab) {
2910 if (tab.getSubWebView() != null) {
2911 mUi.attachSubWindow(tab.getSubViewContainer());
2912 getCurrentTopWebView().requestFocus();
2913 }
2914 }
2915
Mathew Inwood29721c22011-06-29 17:55:24 +01002916 private Tab showPreloadedTab(final UrlData urlData) {
2917 if (!urlData.isPreloaded()) {
2918 return null;
2919 }
2920 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2921 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2922 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002923 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002924 // Could not submit query. Fallback to regular tab creation
2925 tabControl.destroy();
2926 return null;
2927 }
2928 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002929 // check tab count and make room for new tab
2930 if (!mTabControl.canCreateNewTab()) {
2931 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2932 if (leastUsed != null) {
2933 closeTab(leastUsed);
2934 }
2935 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002936 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002937 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002938 mTabControl.addPreloadedTab(t);
2939 addTab(t);
2940 setActiveTab(t);
2941 return t;
2942 }
2943
Michael Kolb7bcafde2011-05-09 13:55:59 -07002944 // open a non inconito tab with the given url data
2945 // and set as active tab
2946 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002947 Tab tab = showPreloadedTab(urlData);
2948 if (tab == null) {
2949 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002950 if ((tab != null) && !urlData.isEmpty()) {
2951 loadUrlDataIn(tab, urlData);
2952 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002953 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002954 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002955 }
2956
Michael Kolb843510f2010-12-09 10:51:49 -08002957 @Override
2958 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002959 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002960 }
2961
Michael Kolb8233fac2010-10-26 16:08:53 -07002962 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002963 public Tab openIncognitoTab() {
2964 return openTab(INCOGNITO_URI, true, true, false);
2965 }
2966
2967 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002968 public Tab openTab(String url, boolean incognito, boolean setActive,
2969 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002970 return openTab(url, incognito, setActive, useCurrent, null);
2971 }
2972
2973 @Override
2974 public Tab openTab(String url, Tab parent, boolean setActive,
2975 boolean useCurrent) {
2976 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2977 setActive, useCurrent, parent);
2978 }
2979
2980 public Tab openTab(String url, boolean incognito, boolean setActive,
2981 boolean useCurrent, Tab parent) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002982 boolean change_tabs = false;
2983 if (setActive) {
2984 Tab currentTab = mTabControl.getCurrentTab();
2985 if (currentTab != null) {
2986 change_tabs = setActive;
2987 setActive = false;
Sagar Dhawan9a1d8e72015-09-11 17:14:38 -07002988 if (mUi instanceof PhoneUi)
2989 currentTab.capture();
Pankaj Gargf49e0222015-09-01 12:19:13 -07002990 }
2991 }
2992
Michael Kolb7bcafde2011-05-09 13:55:59 -07002993 Tab tab = createNewTab(incognito, setActive, useCurrent);
2994 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002995 if (parent instanceof SnapshotTab) {
2996 addTab(tab);
2997 if (setActive)
2998 setActiveTab(tab);
2999 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003000 parent.addChildTab(tab);
3001 }
Michael Kolb519d2282011-05-09 17:03:19 -07003002 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003003 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003004 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003005 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07003006
3007 if (change_tabs) {
Sagar Dhawan20b1deb2015-10-12 11:13:18 -07003008 setActiveTab(tab);
Pankaj Gargf49e0222015-09-01 12:19:13 -07003009 synchronized (mThumbnailCbList) {
3010 startCaptureTimer();
3011 mLatestCreatedTab = tab;
3012 mThumbnailCbList.add(new ValueCallback<Bitmap>() {
3013 @Override
3014 public void onReceiveValue(Bitmap bitmap) {
3015 synchronized (mThumbnailCbList) {
3016 if (mLatestCreatedTab != null) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07003017 mLatestCreatedTab = null;
3018 }
3019 stopCaptureTimer();
3020 }
3021 }
3022 });
3023 }
3024 }
3025
Michael Kolb7bcafde2011-05-09 13:55:59 -07003026 return tab;
3027 }
3028
Pankaj Gargf49e0222015-09-01 12:19:13 -07003029 private void startCaptureTimer() {
3030 mCaptureTimer = new CountDownTimer(mCaptureMaxWaitMS, mCaptureMaxWaitMS) {
3031 @Override
3032 public void onTick(long millisUntilFinished) {
3033 // Do nothing
3034 }
3035
3036 @Override
3037 public void onFinish() {
3038 synchronized (mThumbnailCbList) {
3039 Log.e(LOGTAG, "Screen capture timed out while opening new tab");
3040 if (mLatestCreatedTab != null) {
3041 setActiveTab(mLatestCreatedTab);
3042 mLatestCreatedTab = null;
3043 }
3044 }
3045 }
3046 }.start();
3047 }
3048
3049 private void stopCaptureTimer() {
3050 if (mCaptureTimer != null) {
3051 mCaptureTimer.cancel();
3052 mCaptureTimer = null;
3053 }
3054 }
3055
3056
Michael Kolb7bcafde2011-05-09 13:55:59 -07003057 // this method will attempt to create a new tab
3058 // incognito: private browsing tab
3059 // setActive: ste tab as current tab
3060 // useCurrent: if no new tab can be created, return current tab
3061 private Tab createNewTab(boolean incognito, boolean setActive,
3062 boolean useCurrent) {
3063 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07003064 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
3065 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003066 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003067 if (mTabControl.canCreateNewTab()) {
3068 tab = mTabControl.createNewTab(incognito, !setActive);
3069 addTab(tab);
3070 if (setActive) {
3071 setActiveTab(tab);
3072 } else {
3073 tab.pause();
3074 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003075 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003076 if (useCurrent) {
3077 tab = mTabControl.getCurrentTab();
3078 reuseTab(tab, null);
3079 } else {
3080 mUi.showMaxTabsWarning();
3081 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003082 }
3083 }
3084 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003085 }
3086
John Reck2bc80422011-06-30 15:11:49 -07003087 @Override
3088 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3089 SnapshotTab tab = null;
3090 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07003091 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07003092 addTab(tab);
3093 if (setActive) {
3094 setActiveTab(tab);
3095 }
3096 } else {
3097 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003098 }
3099 return tab;
3100 }
3101
Michael Kolb8233fac2010-10-26 16:08:53 -07003102 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003103 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003104 * @return boolean True if we successfully switched to a different tab. If
3105 * the indexth tab is null, or if that tab is the same as
3106 * the current one, return false.
3107 */
3108 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003109 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003110 Tab currentTab = mTabControl.getCurrentTab();
3111 if (tab == null || tab == currentTab) {
3112 return false;
3113 }
3114 setActiveTab(tab);
3115 return true;
3116 }
3117
3118 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003119 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003120 closeCurrentTab(false);
3121 }
3122
3123 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003124 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003125 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003126 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003127 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003128 return;
3129 }
Michael Kolbc831b632011-05-11 09:30:34 -07003130 final Tab current = mTabControl.getCurrentTab();
3131 final int pos = mTabControl.getCurrentPosition();
3132 Tab newTab = current.getParent();
3133 if (newTab == null) {
3134 newTab = mTabControl.getTab(pos + 1);
3135 if (newTab == null) {
3136 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003137 }
3138 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003139 if (andQuit) {
3140 mTabControl.setCurrentTab(newTab);
3141 closeTab(current);
3142 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003143 // Close window
3144 closeTab(current);
3145 }
3146 }
3147
3148 /**
3149 * Close the tab, remove its associated title bar, and adjust mTabControl's
3150 * current tab to a valid value.
3151 */
3152 @Override
3153 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003154 if (tab == mTabControl.getCurrentTab()) {
3155 closeCurrentTab();
3156 } else {
3157 removeTab(tab);
3158 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003159 }
3160
Afzal Najamd4e33312012-04-26 01:54:01 -04003161 /**
3162 * Close all tabs except the current one
3163 */
3164 @Override
3165 public void closeOtherTabs() {
3166 int inactiveTabs = mTabControl.getTabCount() - 1;
3167 for (int i = inactiveTabs; i >= 0; i--) {
3168 Tab tab = mTabControl.getTab(i);
3169 if (tab != mTabControl.getCurrentTab()) {
3170 removeTab(tab);
3171 }
3172 }
3173 }
3174
Michael Kolb8233fac2010-10-26 16:08:53 -07003175 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003176 protected void loadUrlFromContext(String url) {
3177 Tab tab = getCurrentTab();
3178 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003179 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003180 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003181 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003182 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003183 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003184 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003185 }
3186 }
3187 }
3188
3189 /**
3190 * Load the URL into the given WebView and update the title bar
3191 * to reflect the new load. Call this instead of WebView.loadUrl
3192 * directly.
3193 * @param view The WebView used to load url.
3194 * @param url The URL to load.
3195 */
John Reck71e51422011-07-01 16:49:28 -07003196 @Override
3197 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003198 loadUrl(tab, url, null);
3199 }
3200
3201 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3202 if (tab != null) {
3203 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003204 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003205 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003206 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003207 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003208 }
3209
3210 /**
3211 * Load UrlData into a Tab and update the title bar to reflect the new
3212 * load. Call this instead of UrlData.loadIn directly.
3213 * @param t The Tab used to load.
3214 * @param data The UrlData being loaded.
3215 */
3216 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003217 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003218 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003219 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003220 } else {
John Reck38b39652012-06-05 09:22:59 -07003221 if (t != null && data.mDisableUrlOverride) {
3222 t.disableUrlOverridingForLoad();
3223 }
John Reck26b18322011-06-21 13:08:58 -07003224 loadUrl(t, data.mUrl, data.mHeaders);
3225 }
3226 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003227 }
3228
John Reck30c714c2010-12-16 17:30:34 -08003229 @Override
3230 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003231 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003232 //In case of tab can go back (aka tab has navigation entry) do nothing
3233 //else just load homepage in current tab.
3234 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003235 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003236 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003237 }
3238
3239 void goBackOnePageOrQuit() {
3240 Tab current = mTabControl.getCurrentTab();
3241 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003242 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3243 showExitDialog(mActivity);
3244 } else {
3245 /*
3246 * Instead of finishing the activity, simply push this to the back
3247 * of the stack and let ActivityManager to choose the foreground
3248 * activity. As BrowserActivity is singleTask, it will be always the
3249 * root of the task. So we can use either true or false for
3250 * moveTaskToBack().
3251 */
3252 mActivity.moveTaskToBack(true);
3253 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003254 return;
3255 }
John Reckef654f12011-07-12 16:42:08 -07003256 if (current.canGoBack()) {
3257 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003258 } else {
3259 // Check to see if we are closing a window that was created by
3260 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003261 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003262 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003263 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003264 // Now we close the other tab
3265 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003266 } else if (BrowserConfig.getInstance(getContext())
3267 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3268 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003269 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003270 /*
3271 * Instead of finishing the activity, simply push this to the back
3272 * of the stack and let ActivityManager to choose the foreground
3273 * activity. As BrowserActivity is singleTask, it will be always the
3274 * root of the task. So we can use either true or false for
3275 * moveTaskToBack().
3276 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003277 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003278 }
3279 }
3280 }
3281
3282 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003283 * helper method for key handler
3284 * returns the current tab if it can't advance
3285 */
Michael Kolbc831b632011-05-11 09:30:34 -07003286 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003287 int pos = mTabControl.getCurrentPosition() + 1;
3288 if (pos >= mTabControl.getTabCount()) {
3289 pos = 0;
3290 }
3291 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003292 }
3293
3294 /**
3295 * helper method for key handler
3296 * returns the current tab if it can't advance
3297 */
Michael Kolbc831b632011-05-11 09:30:34 -07003298 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003299 int pos = mTabControl.getCurrentPosition() - 1;
3300 if ( pos < 0) {
3301 pos = mTabControl.getTabCount() - 1;
3302 }
3303 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003304 }
3305
John Reckbcef87f2012-02-03 14:58:34 -08003306 boolean isMenuOrCtrlKey(int keyCode) {
3307 return (KeyEvent.KEYCODE_MENU == keyCode)
3308 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3309 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3310 }
3311
Michael Kolb0035fad2011-03-14 13:25:28 -07003312 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003313 * handle key events in browser
3314 *
3315 * @param keyCode
3316 * @param event
3317 * @return true if handled, false to pass to super
3318 */
John Reck9c35b9c2012-05-30 10:08:50 -07003319 @Override
3320 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003321 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3322 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003323 if (!mUi.isComboViewShowing()) {
3324 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3325 true, false);
3326 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003327 return true;
3328 }
3329
Cary Clark160bbb92011-01-10 11:17:07 -05003330 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003331 // Even if MENU is already held down, we need to call to super to open
3332 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003333 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003334 mMenuIsDown = true;
3335 return false;
3336 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003337
Cary Clark8ff8c662010-12-29 15:03:05 -05003338 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003339 Tab tab = getCurrentTab();
3340 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003341
Cary Clark160bbb92011-01-10 11:17:07 -05003342 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3343 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003344
Michael Kolb8233fac2010-10-26 16:08:53 -07003345 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003346 case KeyEvent.KEYCODE_TAB:
3347 if (event.isCtrlPressed()) {
3348 if (event.isShiftPressed()) {
3349 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003350 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003351 } else {
3352 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003353 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003354 }
3355 return true;
3356 }
3357 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003358 case KeyEvent.KEYCODE_SPACE:
3359 // WebView/WebTextView handle the keys in the KeyDown. As
3360 // the Activity's shortcut keys are only handled when WebView
3361 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003362 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003363 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003364 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003365 pageDown();
3366 }
3367 return true;
3368 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003369 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003370 event.startTracking();
3371 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003372 case KeyEvent.KEYCODE_FORWARD:
3373 if (!noModifiers) break;
3374 tab.goForward();
3375 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003376 case KeyEvent.KEYCODE_DPAD_LEFT:
3377 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003378 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003379 return true;
3380 }
3381 break;
3382 case KeyEvent.KEYCODE_DPAD_RIGHT:
3383 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003384 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003385 return true;
3386 }
3387 break;
3388 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003389 if (ctrl) {
3390 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003391 return true;
3392 }
3393 break;
Michael Kolba4183062011-01-16 10:43:21 -08003394// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003395 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003396 if (ctrl ) {
3397 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003398 return true;
3399 }
3400 break;
Michael Kolba4183062011-01-16 10:43:21 -08003401// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003402// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003403// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003404// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003405// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003406// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003407// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003408// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003409// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003410// case KeyEvent.KEYCODE_M: // unused
3411// case KeyEvent.KEYCODE_N: // in Chrome: new window
3412// case KeyEvent.KEYCODE_O: // in Chrome: open file
3413// case KeyEvent.KEYCODE_P: // in Chrome: print page
3414// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003415// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003416// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3417 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003418 // we can't use the ctrl/shift flags, they check for
3419 // exclusive use of a modifier
3420 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003421 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003422 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003423 } else {
3424 openTabToHomePage();
3425 }
3426 return true;
3427 }
3428 break;
3429// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3430// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003431// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003432// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3433// case KeyEvent.KEYCODE_Y: // unused
3434// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003435 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003436 // it is a regular key and webview is not null
3437 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003438 }
3439
John Reck9c35b9c2012-05-30 10:08:50 -07003440 @Override
3441 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003442 switch(keyCode) {
3443 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003444 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003445 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003446 return true;
3447 }
3448 break;
3449 }
3450 return false;
3451 }
3452
John Reck9c35b9c2012-05-30 10:08:50 -07003453 @Override
3454 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003455 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003456 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003457 if (KeyEvent.KEYCODE_MENU == keyCode
3458 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003459 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003460 }
3461 }
Cary Clark160bbb92011-01-10 11:17:07 -05003462 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003463 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003464 case KeyEvent.KEYCODE_BACK:
3465 if (event.isTracking() && !event.isCanceled()) {
3466 onBackKey();
3467 return true;
3468 }
3469 break;
3470 }
3471 return false;
3472 }
3473
3474 public boolean isMenuDown() {
3475 return mMenuIsDown;
3476 }
3477
John Reck9c35b9c2012-05-30 10:08:50 -07003478 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003479 public void setupAutoFill(Message message) {
3480 // Open the settings activity at the AutoFill profile fragment so that
3481 // the user can create a new profile. When they return, we will dispatch
3482 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003483 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003484 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3485 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003486 }
John Reckb3417f02011-01-14 11:01:05 -08003487
John Reck9c35b9c2012-05-30 10:08:50 -07003488 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003489 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003490 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003491 return true;
3492 }
3493
John Reck1cf4b792011-07-26 10:22:22 -07003494 @Override
3495 public boolean shouldCaptureThumbnails() {
3496 return mUi.shouldCaptureThumbnails();
3497 }
3498
Michael Kolbc3af0672011-08-09 10:24:41 -07003499 @Override
Pankaj Gargf49e0222015-09-01 12:19:13 -07003500 public void onThumbnailCapture(Bitmap bm) {
3501 synchronized (mThumbnailCbList) {
3502 int num_entries = mThumbnailCbList.size();
3503 while (num_entries > 0){
3504 num_entries--;
3505 ValueCallback<Bitmap> cb = mThumbnailCbList.get(num_entries);
3506 cb.onReceiveValue(bm);
3507 }
3508 mThumbnailCbList.clear();
3509 }
3510 }
3511
3512 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003513 public boolean supportsVoice() {
3514 PackageManager pm = mActivity.getPackageManager();
3515 List activities = pm.queryIntentActivities(new Intent(
3516 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3517 return activities.size() != 0;
3518 }
3519
3520 @Override
3521 public void startVoiceRecognizer() {
3522 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003523 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003524 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3525 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3526 mActivity.startActivityForResult(voice, VOICE_RESULT);
3527 }
3528
Pankaj Garg7b279f62014-08-12 14:47:18 -07003529 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003530 if (mLevel == level)
3531 return;
3532 mLevel = level;
3533 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003534 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3535 lp.dimAmount = level;
3536 mActivity.getWindow().setAttributes(lp);
3537 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3538 } else {
3539 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3540 }
3541 }
3542
Michael Kolb0b129122012-06-04 16:31:58 -07003543 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003544 public void setBlockEvents(boolean block) {
3545 mBlockEvents = block;
3546 }
3547
John Reck9c35b9c2012-05-30 10:08:50 -07003548 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003549 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003550 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003551 }
3552
John Reck9c35b9c2012-05-30 10:08:50 -07003553 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003554 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003555 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003556 }
3557
John Reck9c35b9c2012-05-30 10:08:50 -07003558 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003559 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003560 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003561 }
3562
John Reck9c35b9c2012-05-30 10:08:50 -07003563 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003564 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003565 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003566 }
3567
John Reck9c35b9c2012-05-30 10:08:50 -07003568 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003569 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003570 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003571 }
3572
Pankaj Garg49b79252014-11-07 17:33:41 -08003573 @Override
3574 public boolean shouldShowAppMenu() {
3575 return true;
3576 }
3577
3578 @Override
3579 public int getMenuThemeResourceId() {
3580 return R.style.OverflowMenuTheme;
3581 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003582}