blob: d8953a25b8b912a2e5de8e4dcf166eba866af1fe [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);
810 mActivity.unregisterReceiver(mLowPowerReceiver);
811 }
812
813 @Override
814 public void onStart() {
815 if (!mActivityStopped) {
816 Log.e(LOGTAG, "BrowserActivity is already started.");
817 return;
818 }
819 mActivityStopped = false;
Vivek Sekharb53a5952015-07-17 14:14:38 -0700820 UpdateNotificationService.updateCheck(mActivity);
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700821 // reset the search engine based on locale
822 mSettings.updateSearchEngine(false);
George Mount3636d0a2011-11-21 09:08:21 -0800823 mSettings.setLastRunPaused(false);
Michael Kolb70976932010-11-30 11:34:01 -0800824 Tab current = mTabControl.getCurrentTab();
825 if (current != null) {
826 current.resume();
827 resumeWebViewTimers(current);
828 }
John Reckf57c0292011-07-21 18:15:39 -0700829 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200830
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 mUi.onResume();
832 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100833 NfcHandler.register(mActivity, this);
Vivek Sekharc70ae632015-04-08 17:54:05 -0700834 if (current != null && current.getWebView().isShowingCrashView())
835 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700836 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700837
John Reckf57c0292011-07-21 18:15:39 -0700838 }
839
Michael Kolb70976932010-11-30 11:34:01 -0800840 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800841 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800842 * @param tab guaranteed non-null
843 */
844 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 boolean inLoad = tab.inPageLoad();
Tarun Nainani7c762c82015-08-04 13:42:09 -0700846 if ((!mActivityStopped && !inLoad) || (mActivityStopped && inLoad)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 CookieSyncManager.getInstance().startSync();
848 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100849 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 }
851 }
852
Michael Kolb70976932010-11-30 11:34:01 -0800853 /**
854 * Pause all WebView timers using the WebView of the given tab
855 * @param tab
856 * @return true if the timers are paused or tab is null
857 */
858 private boolean pauseWebViewTimers(Tab tab) {
859 if (tab == null) {
860 return true;
861 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100863 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 }
Michael Kolb70976932010-11-30 11:34:01 -0800866 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700867 }
868
John Reck9c35b9c2012-05-30 10:08:50 -0700869 @Override
870 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800871 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
873 mUploadHandler = null;
874 }
875 if (mTabControl == null) return;
876 mUi.onDestroy();
877 // Remove the current tab and sub window
878 Tab t = mTabControl.getCurrentTab();
879 if (t != null) {
880 dismissSubWindow(t);
881 removeTab(t);
882 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500883 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 // Destroy all the tabs
885 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700886 // Unregister receiver
887 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700888 }
889
890 protected boolean isActivityPaused() {
891 return mActivityPaused;
892 }
893
John Reck9c35b9c2012-05-30 10:08:50 -0700894 @Override
895 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 mTabControl.freeMemory();
897 }
898
899 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700900 public void stopLoading() {
901 mLoadStopped = true;
902 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700904 if (w != null) {
905 w.stopLoading();
906 mUi.onPageStopped(tab);
907 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 }
909
910 boolean didUserStopLoading() {
911 return mLoadStopped;
912 }
913
kaiyiza016da12013-08-26 17:50:22 +0800914 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700915 final String reminderType = getContext().getResources().getString(
916 R.string.def_wifi_browser_interaction_remind_type);
917 final String selectionConnnection = getContext().getResources().getString(
918 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700919 final String wifiSelection = getContext().getResources().getString(
920 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700921
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700922 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
923 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700924 return;
925
kaiyiza016da12013-08-26 17:50:22 +0800926 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700927 Context.CONNECTIVITY_SERVICE);
928 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700929 WifiManager wifiMgr = (WifiManager) this.getContext()
930 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700931 if (networkInfo == null
932 || (networkInfo != null && (networkInfo.getType() !=
933 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700934 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
935 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700936 List<ScanResult> list = wifiMgr.getScanResults();
937 // Have no AP's for Wifi's fall back to data
938 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700939 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700940 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
941 this.getContext().startActivity(intent);
942 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700943 // Request to select Wifi AP
944 try {
945 Intent intent = new Intent(wifiSelection);
946 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
947 this.getContext().startActivity(intent);
948 } catch (Exception e) {
949 String err_msg = this.getContext().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700950 R.string.activity_not_found, wifiSelection);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700951 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
952 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700953 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700954 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800955 }
956 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700957
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 // WebViewController
959
960 @Override
John Reck324d4402011-01-11 16:56:42 -0800961 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700962
Michael Kolb8233fac2010-10-26 16:08:53 -0700963 // reset sync timer to avoid sync starts during loading a page
964 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700965 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700966 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800967 boolean networkNotifier = BrowserConfig.getInstance(getContext())
968 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700969 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700970 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800971 } else {
972 if (!mNetworkHandler.isNetworkUp()) {
973 view.setNetworkAvailable(false);
974 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700975 }
976
977 // when BrowserActivity just starts, onPageStarted may be called before
978 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
979 // to start the timer. As we won't switch tabs while an activity is in
980 // pause state, we can ensure calling resume and pause in pair.
Tarun Nainani7c762c82015-08-04 13:42:09 -0700981 if (mActivityStopped) {
Michael Kolb70976932010-11-30 11:34:01 -0800982 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 }
984 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 endActionMode();
986
John Reck30c714c2010-12-16 17:30:34 -0800987 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700988
John Reck324d4402011-01-11 16:56:42 -0800989 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 // update the bookmark database for favicon
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700991 syncBookmarkFavicon(tab, null, url, favicon);
Michael Kolb8233fac2010-10-26 16:08:53 -0700992
993 Performance.tracePageStart(url);
994
995 // Performance probe
996 if (false) {
997 Performance.onPageStarted();
998 }
999
1000 }
1001
1002 @Override
John Reck324d4402011-01-11 16:56:42 -08001003 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -07001004 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -08001005 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001006
Michael Kolb8233fac2010-10-26 16:08:53 -07001007 // Performance probe
1008 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001009 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001010 }
1011
Tarun Nainani8eb00912014-07-17 12:28:32 -07001012 tab.onPageFinished();
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001013 syncBookmarkFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -07001014
Michael Kolb8233fac2010-10-26 16:08:53 -07001015 Performance.tracePageFinished();
1016 }
1017
1018 @Override
John Reck30c714c2010-12-16 17:30:34 -08001019 public void onProgressChanged(Tab tab) {
1020 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001021
1022 if (newProgress == 100) {
1023 CookieSyncManager.getInstance().sync();
1024 // onProgressChanged() may continue to be called after the main
1025 // frame has finished loading, as any remaining sub frames continue
1026 // to load. We'll only get called once though with newProgress as
1027 // 100 when everything is loaded. (onPageFinished is called once
1028 // when the main frame completes loading regardless of the state of
1029 // any sub frames so calls to onProgressChanges may continue after
1030 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001031 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001032 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001033 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001034 } else if (mWasInPageLoad) {
1035 mWasInPageLoad = false;
1036 updateInLoadMenuItems(mCachedMenu, tab);
1037 }
1038
Tarun Nainani7c762c82015-08-04 13:42:09 -07001039 if (mActivityStopped && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001040 // pause the WebView timer and release the wake lock if it is
1041 // finished while BrowserActivity is in pause state.
1042 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 }
1044 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001045 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001047 // still loading
1048 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001049 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001050 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001051 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001052 } else {
1053 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001054 }
1055 }
Pankaj Gargf9040c82015-08-14 10:19:31 -07001056
1057 mUi.onProgressChanged(tab);
John Reck30c714c2010-12-16 17:30:34 -08001058 }
1059
1060 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001061 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001062 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 }
1064
1065 @Override
1066 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001067 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001068 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001069 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001070 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1071 return;
1072 }
1073 // Update the title in the history database if not in private browsing mode
1074 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001075 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001076 }
1077 }
1078
1079 @Override
1080 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001081 syncBookmarkFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1082 ((BaseUi)mUi).setFavicon(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001083 }
1084
1085 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001086 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001087 // if tab is snapshot tab we want to prevent navigation from occuring
1088 // since snapshot tab opens a new tab with the url
1089 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001090 }
1091
1092 @Override
1093 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1094 if (mMenuIsDown) {
1095 // only check shortcut key when MENU is held
1096 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1097 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001098 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001099 int keyCode = event.getKeyCode();
1100 // We need to send almost every key to WebKit. However:
1101 // 1. We don't want to block the device on the renderer for
1102 // some keys like menu, home, call.
1103 // 2. There are no WebKit equivalents for some of these keys
1104 // (see app/keyboard_codes_win.h)
1105 // Note that these are not the same set as KeyEvent.isSystemKey:
1106 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1107 if (keyCode == KeyEvent.KEYCODE_MENU ||
1108 keyCode == KeyEvent.KEYCODE_HOME ||
1109 keyCode == KeyEvent.KEYCODE_BACK ||
1110 keyCode == KeyEvent.KEYCODE_CALL ||
1111 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1112 keyCode == KeyEvent.KEYCODE_POWER ||
1113 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1114 keyCode == KeyEvent.KEYCODE_CAMERA ||
1115 keyCode == KeyEvent.KEYCODE_FOCUS ||
1116 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1117 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1118 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1119 return true;
1120 }
1121
1122 // We also have to intercept some shortcuts before we send them to the ContentView.
1123 if (event.isCtrlPressed() && (
1124 keyCode == KeyEvent.KEYCODE_TAB ||
1125 keyCode == KeyEvent.KEYCODE_W ||
1126 keyCode == KeyEvent.KEYCODE_F4)) {
1127 return true;
1128 }
1129
1130 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 }
1132
Tarun Nainanif5563b62015-01-21 18:52:59 -08001133 private void handleMediaKeyEvent(KeyEvent event) {
1134
1135 int keyCode = event.getKeyCode();
1136 // send media key events to audio manager
1137 if (Build.VERSION.SDK_INT >= 19) {
1138
1139 switch (keyCode) {
1140 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1141 case KeyEvent.KEYCODE_MEDIA_STOP:
1142 case KeyEvent.KEYCODE_MEDIA_NEXT:
1143 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1144 case KeyEvent.KEYCODE_MEDIA_REWIND:
1145 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1146 case KeyEvent.KEYCODE_MUTE:
1147 case KeyEvent.KEYCODE_MEDIA_PLAY:
1148 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1149 case KeyEvent.META_SHIFT_RIGHT_ON:
1150 case KeyEvent.KEYCODE_MEDIA_EJECT:
1151 case KeyEvent.KEYCODE_MEDIA_RECORD:
1152 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1153
1154 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1155 .getSystemService(Context.AUDIO_SERVICE);
1156 audioManager.dispatchMediaKeyEvent(event);
1157 }
1158 }
1159 }
1160
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 @Override
John Reck997b1b72012-04-19 18:08:25 -07001162 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001163 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001164 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001166 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001167 } else {
John Reck997b1b72012-04-19 18:08:25 -07001168 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 }
1170 }
John Reck997b1b72012-04-19 18:08:25 -07001171 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 }
1173
1174 @Override
John Reck324d4402011-01-11 16:56:42 -08001175 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001176 // Don't save anything in private browsing mode or when disabling history
1177 // for regular tabs is enabled
1178 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1179 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1180 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001181
John Reck49a603c2011-03-03 09:33:05 -08001182 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001183
John Reck324d4402011-01-11 16:56:42 -08001184 if (TextUtils.isEmpty(url)
1185 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001186 return;
1187 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001188
John Reckf57c0292011-07-21 18:15:39 -07001189 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001190 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001191 }
1192
1193 @Override
1194 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1195 AsyncTask<Void, Void, String[]> task =
1196 new AsyncTask<Void, Void, String[]>() {
1197 @Override
1198 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001199 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001200 }
1201 @Override
1202 public void onPostExecute(String[] result) {
1203 callback.onReceiveValue(result);
1204 }
1205 };
1206 task.execute();
1207 }
1208
1209 @Override
1210 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1211 final HttpAuthHandler handler, final String host,
1212 final String realm) {
1213 String username = null;
1214 String password = null;
1215
1216 boolean reuseHttpAuthUsernamePassword
1217 = handler.useHttpAuthUsernamePassword();
1218
1219 if (reuseHttpAuthUsernamePassword && view != null) {
1220 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1221 if (credentials != null && credentials.length == 2) {
1222 username = credentials[0];
1223 password = credentials[1];
1224 }
1225 }
1226
1227 if (username != null && password != null) {
1228 handler.proceed(username, password);
1229 } else {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -07001230 if (!tab.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 handler.cancel();
1232 }
1233 }
1234 }
1235
1236 @Override
1237 public void onDownloadStart(Tab tab, String url, String userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001238 String contentDisposition, String mimetype, String referer, String auth,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001239 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001240 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001241 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001242 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001243 contentDisposition, mimetype, referer, auth,
1244 w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001245 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 // This Tab was opened for the sole purpose of downloading a
1247 // file. Remove it.
1248 if (tab == mTabControl.getCurrentTab()) {
1249 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001250 if (tab.getDerivedFromIntent())
1251 closeTab(tab);
1252 else
1253 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001254 } else {
1255 // In this case, it is not.
1256 closeTab(tab);
1257 }
1258 }
1259 }
1260
1261 @Override
1262 public Bitmap getDefaultVideoPoster() {
1263 return mUi.getDefaultVideoPoster();
1264 }
1265
1266 @Override
1267 public View getVideoLoadingProgressView() {
1268 return mUi.getVideoLoadingProgressView();
1269 }
1270
Michael Kolb8233fac2010-10-26 16:08:53 -07001271 // helper method
1272
1273 /*
1274 * Update the favorites icon if the private browsing isn't enabled and the
1275 * icon is valid.
1276 */
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001277 private void syncBookmarkFavicon(Tab tab, final String originalUrl,
1278 final String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 if (favicon == null) {
1280 return;
1281 }
1282 if (!tab.isPrivateBrowsingEnabled()) {
1283 Bookmarks.updateFavicon(mActivity
1284 .getContentResolver(), originalUrl, url, favicon);
1285 }
1286 }
1287
Leon Scroggins4cd97792010-12-03 15:31:56 -05001288 @Override
1289 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001290 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001291 mUi.bookmarkedStatusHasChanged(tab);
1292 }
1293
Michael Kolb8233fac2010-10-26 16:08:53 -07001294 // end WebViewController
1295
1296 protected void pageUp() {
1297 getCurrentTopWebView().pageUp(false);
1298 }
1299
1300 protected void pageDown() {
1301 getCurrentTopWebView().pageDown(false);
1302 }
1303
1304 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001305 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001306 public void editUrl() {
1307 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001308 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001309 }
1310
John Reck9c35b9c2012-05-30 10:08:50 -07001311 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001312 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001313 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001314 if (tab.inForeground()) {
1315 if (mUi.isCustomViewShowing()) {
1316 callback.onCustomViewHidden();
1317 return;
1318 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001319 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001320 // Save the menu state and set it to empty while the custom
1321 // view is showing.
1322 mOldMenuState = mMenuState;
1323 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001324 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001325 }
1326 }
1327
1328 @Override
1329 public void hideCustomView() {
1330 if (mUi.isCustomViewShowing()) {
1331 mUi.onHideCustomView();
1332 // Reset the old menu state.
1333 mMenuState = mOldMenuState;
1334 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001335 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001336 }
1337 }
1338
John Reck9c35b9c2012-05-30 10:08:50 -07001339 @Override
1340 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001341 Intent intent) {
1342 if (getCurrentTopWebView() == null) return;
1343 switch (requestCode) {
1344 case PREFERENCES_PAGE:
1345 if (resultCode == Activity.RESULT_OK && intent != null) {
1346 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001347 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001348 mTabControl.removeParentChildRelationShips();
Pankaj Garg75273bb2015-08-20 11:43:49 -07001349 } else if (action.equals(PreferenceKeys.ACTION_RELOAD_PAGE)) {
1350 ArrayList<String> origins =
1351 intent.getStringArrayListExtra(EXTRA_UPDATED_URLS);
1352 if (origins.isEmpty()) {
1353 mTabControl.reloadLiveTabs();
1354 }
1355 else{
1356 for (String origin : origins){
1357 mTabControl.findAndReload(origin);
1358 }
1359 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 }
1361 }
1362 break;
1363 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001364 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001365 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001366 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001367 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001368 case AUTOFILL_SETUP:
1369 // Determine whether a profile was actually set up or not
1370 // and if so, send the message back to the WebTextView to
1371 // fill the form with the new profile.
1372 if (getSettings().getAutoFillProfile() != null) {
1373 mAutoFillSetupMessage.sendToTarget();
1374 mAutoFillSetupMessage = null;
1375 }
1376 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001377 case COMBO_VIEW:
1378 if (intent == null || resultCode != Activity.RESULT_OK) {
1379 break;
1380 }
John Reck3ba45532011-08-11 16:26:53 -07001381 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001382 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1383 Tab t = getCurrentTab();
1384 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001385 mUpdateMyNavThumbnail = true;
1386 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001387 loadUrl(t, uri.toString());
1388 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1389 String[] urls = intent.getStringArrayExtra(
1390 ComboViewActivity.EXTRA_OPEN_ALL);
1391 Tab parent = getCurrentTab();
1392 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001393 if (url != null) {
1394 parent = openTab(url, parent,
1395 !mSettings.openInBackground(), true);
1396 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001397 }
1398 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1399 long id = intent.getLongExtra(
1400 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1401 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001402 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001403 }
1404 }
1405 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001406 case VOICE_RESULT:
1407 if (resultCode == Activity.RESULT_OK && intent != null) {
1408 ArrayList<String> results = intent.getStringArrayListExtra(
1409 RecognizerIntent.EXTRA_RESULTS);
1410 if (results.size() >= 1) {
1411 mVoiceResult = results.get(0);
1412 }
1413 }
1414 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001415 case MY_NAVIGATION:
1416 if (intent == null || resultCode != Activity.RESULT_OK) {
1417 break;
1418 }
1419
1420 if (intent.getBooleanExtra("need_refresh", false) &&
1421 getCurrentTopWebView() != null) {
1422 getCurrentTopWebView().reload();
1423 }
1424 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001425 default:
1426 break;
1427 }
1428 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001429 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 }
1431
1432 /**
1433 * Open the Go page.
1434 * @param startWithHistory If true, open starting on the history tab.
1435 * Otherwise, start with the bookmarks tab.
1436 */
1437 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001438 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001439 if (mTabControl.getCurrentWebView() == null) {
1440 return;
1441 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001442 // clear action mode
1443 if (isInCustomActionMode()) {
1444 endActionMode();
1445 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001446 Bundle extras = new Bundle();
1447 // Disable opening in a new window if we have maxed out the windows
1448 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1449 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001450 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 }
1452
1453 // combo view callbacks
1454
Michael Kolb8233fac2010-10-26 16:08:53 -07001455 // key handling
1456 protected void onBackKey() {
1457 if (!mUi.onBackKey()) {
1458 WebView subwindow = mTabControl.getCurrentSubWindow();
1459 if (subwindow != null) {
1460 if (subwindow.canGoBack()) {
1461 subwindow.goBack();
1462 } else {
1463 dismissSubWindow(mTabControl.getCurrentTab());
1464 }
1465 } else {
1466 goBackOnePageOrQuit();
1467 }
1468 }
1469 }
1470
Michael Kolb4bd767d2011-05-27 11:33:55 -07001471 protected boolean onMenuKey() {
1472 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001473 }
1474
Michael Kolb8233fac2010-10-26 16:08:53 -07001475 // menu handling and state
1476 // TODO: maybe put into separate handler
1477
John Reck9c35b9c2012-05-30 10:08:50 -07001478 @Override
1479 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001480 if (mMenuState == EMPTY_MENU) {
1481 return false;
1482 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001483 MenuInflater inflater = mActivity.getMenuInflater();
1484 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001485 return true;
1486 }
1487
John Reck9c35b9c2012-05-30 10:08:50 -07001488 @Override
1489 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001490 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001491 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001492 return;
1493 }
1494 if (!(v instanceof WebView)) {
1495 return;
1496 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001497 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001498 WebView.HitTestResult result;
1499
1500 /* Determine whether the ContextMenu got triggered because
1501 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1502 * received. The mResult acts as a flag to identify, how it got trigerred
1503 */
1504 if (mResult == null){
1505 result = webview.getHitTestResult();
1506 } else {
1507 result = mResult;
1508 }
1509
Michael Kolb8233fac2010-10-26 16:08:53 -07001510 if (result == null) {
1511 return;
1512 }
1513
1514 int type = result.getType();
1515 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001516
1517 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1518 unknownTypeMap.put("webview", webview);
1519 final Message msg = mHandler.obtainMessage(
1520 UNKNOWN_TYPE_MSG, unknownTypeMap);
1521 /* As defined in android developers guide
1522 * when UNKNOWN_TYPE is received as a result of HitTest
1523 * you need to determing the type by invoking requestFocusNodeHref
1524 */
1525 webview.requestFocusNodeHref(msg);
1526
Michael Kolb8233fac2010-10-26 16:08:53 -07001527 Log.w(LOGTAG,
1528 "We should not show context menu when nothing is touched");
1529 return;
1530 }
1531 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1532 // let TextView handles context menu
1533 return;
1534 }
1535
1536 // Note, http://b/issue?id=1106666 is requesting that
1537 // an inflated menu can be used again. This is not available
1538 // yet, so inflate each time (yuk!)
1539 MenuInflater inflater = mActivity.getMenuInflater();
1540 inflater.inflate(R.menu.browsercontext, menu);
1541
1542 // Show the correct menu group
1543 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001544 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001545 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001546 menu.setGroupVisible(R.id.PHONE_MENU,
1547 type == WebView.HitTestResult.PHONE_TYPE);
1548 menu.setGroupVisible(R.id.EMAIL_MENU,
1549 type == WebView.HitTestResult.EMAIL_TYPE);
1550 menu.setGroupVisible(R.id.GEO_MENU,
1551 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001552 String itemUrl = null;
1553 String url = webview.getOriginalUrl();
1554 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1555 itemUrl = Uri.decode(navigationUrl);
1556 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1557 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1558 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1559 } else {
1560 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1561 }
1562 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1563 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1564 } else {
1565 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1566
1567 menu.setGroupVisible(R.id.IMAGE_MENU,
1568 type == WebView.HitTestResult.IMAGE_TYPE
1569 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1570 menu.setGroupVisible(R.id.ANCHOR_MENU,
1571 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1572 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001573
1574 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1575 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1576 UrlUtils.isDownloadableScheme(extra));
1577 }
1578 else {
1579 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1580 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001581 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001582 // Setup custom handling depending on the type
1583 switch (type) {
1584 case WebView.HitTestResult.PHONE_TYPE:
1585 menu.setHeaderTitle(Uri.decode(extra));
1586 menu.findItem(R.id.dial_context_menu_id).setIntent(
1587 new Intent(Intent.ACTION_VIEW, Uri
Vivek Sekharcb3f7232015-12-10 20:02:46 -08001588 .parse(WebView.SCHEME_TEL + Uri.encode(extra))));
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1590 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1591 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1592 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1593 addIntent);
1594 menu.findItem(R.id.copy_phone_context_menu_id)
1595 .setOnMenuItemClickListener(
1596 new Copy(extra));
1597 break;
1598
1599 case WebView.HitTestResult.EMAIL_TYPE:
1600 menu.setHeaderTitle(extra);
1601 menu.findItem(R.id.email_context_menu_id).setIntent(
1602 new Intent(Intent.ACTION_VIEW, Uri
1603 .parse(WebView.SCHEME_MAILTO + extra)));
1604 menu.findItem(R.id.copy_mail_context_menu_id)
1605 .setOnMenuItemClickListener(
1606 new Copy(extra));
1607 break;
1608
1609 case WebView.HitTestResult.GEO_TYPE:
1610 menu.setHeaderTitle(extra);
1611 menu.findItem(R.id.map_context_menu_id).setIntent(
1612 new Intent(Intent.ACTION_VIEW, Uri
1613 .parse(WebView.SCHEME_GEO
1614 + URLEncoder.encode(extra))));
1615 menu.findItem(R.id.copy_geo_context_menu_id)
1616 .setOnMenuItemClickListener(
1617 new Copy(extra));
1618 break;
1619
1620 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1621 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001622 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 // decide whether to show the open link in new tab option
1624 boolean showNewTab = mTabControl.canCreateNewTab();
1625 MenuItem newTabItem
1626 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001627 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001628 ? R.string.contextmenu_openlink_newwindow_background
1629 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001630 newTabItem.setVisible(showNewTab);
1631 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001632 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1633 newTabItem.setOnMenuItemClickListener(
1634 new MenuItem.OnMenuItemClickListener() {
1635 @Override
1636 public boolean onMenuItemClick(MenuItem item) {
1637 final HashMap<String, WebView> hrefMap =
1638 new HashMap<String, WebView>();
1639 hrefMap.put("webview", webview);
1640 final Message msg = mHandler.obtainMessage(
1641 FOCUS_NODE_HREF,
1642 R.id.open_newtab_context_menu_id,
1643 0, hrefMap);
1644 webview.requestFocusNodeHref(msg);
1645 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001646 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001647 });
1648 } else {
1649 newTabItem.setOnMenuItemClickListener(
1650 new MenuItem.OnMenuItemClickListener() {
1651 @Override
1652 public boolean onMenuItemClick(MenuItem item) {
1653 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001654 openTab(extra, parent,
1655 !mSettings.openInBackground(),
1656 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001657 return true;
1658 }
1659 });
1660 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001661 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001662 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1663 menu.setHeaderTitle(navigationUrl);
1664 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1665
1666 if (itemUrl != null) {
1667 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1668 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1669 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1670 @Override
1671 public boolean onMenuItemClick(MenuItem item) {
1672 final Intent intent = new Intent(Controller.this
1673 .getContext(),
1674 AddMyNavigationPage.class);
1675 Bundle bundle = new Bundle();
1676 String url = Uri.decode(navigationUrl);
1677 bundle.putBoolean("isAdding", false);
1678 bundle.putString("url", url);
1679 bundle.putString("name", getNameFromUrl(url));
1680 intent.putExtra("websites", bundle);
1681 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1682 return false;
1683 }
1684 });
1685 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1686 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1687 @Override
1688 public boolean onMenuItemClick(MenuItem item) {
1689 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1690 return false;
1691 }
1692 });
1693 }
1694 } else {
1695 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1696 }
1697 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001698 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1699 break;
1700 }
1701 // otherwise fall through to handle image part
1702 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001703 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1704 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001705 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1706 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001707 shareItem.setOnMenuItemClickListener(
1708 new MenuItem.OnMenuItemClickListener() {
1709 @Override
1710 public boolean onMenuItemClick(MenuItem item) {
1711 sharePage(mActivity, null, extra, null,
1712 null);
1713 return true;
1714 }
1715 }
1716 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001717 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001718 menu.findItem(R.id.view_image_context_menu_id)
1719 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1720 @Override
1721 public boolean onMenuItemClick(MenuItem item) {
1722 openTab(extra, mTabControl.getCurrentTab(), true, true);
1723 return false;
1724 }
1725 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001726 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1727 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1728 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001729 menu.findItem(R.id.set_wallpaper_context_menu_id).
1730 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1731 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001732 break;
1733
1734 default:
1735 Log.w(LOGTAG, "We should not get here.");
1736 break;
1737 }
1738 //update the ui
1739 mUi.onContextMenuCreated(menu);
1740 }
1741
kaiyiz6e5b3e02013-08-19 20:02:01 +08001742 public void startAddMyNavigation(String url) {
1743 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1744 Bundle bundle = new Bundle();
1745 bundle.putBoolean("isAdding", true);
1746 bundle.putString("url", url);
1747 bundle.putString("name", getNameFromUrl(url));
1748 intent.putExtra("websites", bundle);
1749 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1750 }
1751
1752 private void showMyNavigationDeleteDialog(final String itemUrl) {
1753 new AlertDialog.Builder(this.getContext())
1754 .setTitle(R.string.my_navigation_delete_label)
1755 .setIcon(android.R.drawable.ic_dialog_alert)
1756 .setMessage(R.string.my_navigation_delete_msg)
1757 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1758 @Override
1759 public void onClick(DialogInterface dialog, int whichButton) {
1760 deleteMyNavigationItem(itemUrl);
1761 }
1762 })
1763 .setNegativeButton(R.string.cancel, null)
1764 .show();
1765 }
1766
1767 private void deleteMyNavigationItem(final String itemUrl) {
1768 ContentResolver cr = this.getContext().getContentResolver();
1769 Cursor cursor = null;
1770
1771 try {
1772 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1773 new String[] {
1774 MyNavigationUtil.ID
1775 }, "url = ?", new String[] {
1776 itemUrl
1777 }, null);
1778 if (null != cursor && cursor.moveToFirst()) {
1779 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1780 cursor.getLong(0));
1781
1782 ContentValues values = new ContentValues();
1783 values.put(MyNavigationUtil.TITLE, "");
1784 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1785 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1786 ByteArrayOutputStream os = new ByteArrayOutputStream();
1787 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1788 R.raw.my_navigation_add);
1789 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1790 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1791 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1792 cr.update(uri, values, null, null);
1793 } else {
1794 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1795 }
1796 } catch (IllegalStateException e) {
1797 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1798 } finally {
1799 if (null != cursor) {
1800 cursor.close();
1801 }
1802 }
1803
1804 if (getCurrentTopWebView() != null) {
1805 getCurrentTopWebView().reload();
1806 }
1807 }
1808
1809 private String getNameFromUrl(String itemUrl) {
1810 ContentResolver cr = this.getContext().getContentResolver();
1811 Cursor cursor = null;
1812 String name = null;
1813
1814 try {
1815 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1816 new String[] {
1817 MyNavigationUtil.TITLE
1818 }, "url = ?", new String[] {
1819 itemUrl
1820 }, null);
1821 if (null != cursor && cursor.moveToFirst()) {
1822 name = cursor.getString(0);
1823 } else {
1824 Log.e(LOGTAG, "this item does not exist!");
1825 }
1826 } catch (IllegalStateException e) {
1827 Log.e(LOGTAG, "getNameFromUrl", e);
1828 } finally {
1829 if (null != cursor) {
1830 cursor.close();
1831 }
1832 }
1833 return name;
1834 }
1835
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001836 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001837 final ContentResolver cr = mActivity.getContentResolver();
1838 new AsyncTask<Void, Void, Void>() {
1839 @Override
1840 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001841 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1842 if(!isMyNavigationUrl)
1843 return null;
1844
kaiyiz6e5b3e02013-08-19 20:02:01 +08001845 ContentResolver cr = mActivity.getContentResolver();
1846 Cursor cursor = null;
1847 try {
1848 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1849 new String[] {
1850 MyNavigationUtil.ID
1851 }, "url = ?", new String[] {
1852 itemUrl
1853 }, null);
1854 if (null != cursor && cursor.moveToFirst()) {
1855 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001856 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001857
1858 ContentValues values = new ContentValues();
1859 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1860 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1861 cursor.getLong(0));
1862 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1863 cr.update(uri, values, null, null);
1864 os.close();
1865 }
1866 } catch (IllegalStateException e) {
1867 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1868 } catch (IOException e) {
1869 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1870 } finally {
1871 if (null != cursor) {
1872 cursor.close();
1873 }
1874 }
1875 return null;
1876 }
1877 }.execute();
1878 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001879 /**
1880 * As the menu can be open when loading state changes
1881 * we must manually update the state of the stop/reload menu
1882 * item
1883 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001884 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001885 if (menu == null) {
1886 return;
1887 }
1888 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001889 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001890 menu.findItem(R.id.stop_menu_id):
1891 menu.findItem(R.id.reload_menu_id);
1892 if (src != null) {
1893 dest.setIcon(src.getIcon());
1894 dest.setTitle(src.getTitle());
1895 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001896 mActivity.invalidateOptionsMenu();
1897 }
1898
1899 public void invalidateOptionsMenu() {
1900 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001901 }
1902
John Reck9c35b9c2012-05-30 10:08:50 -07001903 @Override
1904 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001905 // Software menu key (toolbar key)
Sagar Dhawan7fa89622016-01-04 17:58:02 -08001906 View overflowMenu = mActivity.findViewById(R.id.more_browser_settings);
1907 if (getCurrentTab() != null && getCurrentTab().isSnapshot()) {
1908 overflowMenu = mActivity.findViewById(R.id.more);
1909 }
1910 mAppMenuHandler.showAppMenu(overflowMenu, false, false);
Pankaj Garg49b79252014-11-07 17:33:41 -08001911 return true;
1912 }
1913
1914 @Override
1915 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001916 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001917 // hold on to the menu reference here; it is used by the page callbacks
1918 // to update the menu based on loading state
1919 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001920 // Note: setVisible will decide whether an item is visible; while
1921 // setEnabled() will decide whether an item is enabled, which also means
1922 // whether the matching shortcut key will function.
1923 switch (mMenuState) {
1924 case EMPTY_MENU:
1925 if (mCurrentMenuState != mMenuState) {
1926 menu.setGroupVisible(R.id.MAIN_MENU, false);
1927 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1928 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1929 }
1930 break;
1931 default:
1932 if (mCurrentMenuState != mMenuState) {
1933 menu.setGroupVisible(R.id.MAIN_MENU, true);
1934 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1935 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1936 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001937 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 break;
1939 }
1940 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001941 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001942
1943 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001944 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1945 EditBookmarksRestriction.getInstance()
1946 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001947 }
1948
1949 private void setMenuItemVisibility(Menu menu, int id,
1950 boolean visibility) {
1951 MenuItem item = menu.findItem(id);
1952 if (item != null) {
1953 item.setVisible(visibility);
1954 }
1955 }
1956
Sagar Dhawanef06f562015-09-07 15:23:22 +02001957 private int lookupBookmark( String url) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001958 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001959 int count = 0;
1960 Cursor cursor = null;
1961 try {
1962 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1963 BookmarksLoader.PROJECTION,
Sagar Dhawanef06f562015-09-07 15:23:22 +02001964 "url = ?",
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001965 new String[] {
Sagar Dhawanef06f562015-09-07 15:23:22 +02001966 url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001967 },
1968 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001969
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001970 if (cursor != null)
1971 count = cursor.getCount();
1972
1973 } catch (IllegalStateException e) {
1974 Log.e(LOGTAG, "lookupBookmark ", e);
1975 } finally {
1976 if (null != cursor) {
1977 cursor.close();
1978 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001979 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001980 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001981 }
1982
1983 private void resetMenuItems(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001984 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1985
1986 WebView w = getCurrentTopWebView();
1987 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1988
1989 String title = w.getTitle();
1990 String url = w.getUrl();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001991 mCurrentPageBookmarked = (lookupBookmark(url) > 0);
Pankaj Garg634bf432015-07-13 09:54:21 -07001992 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001993 bookmark_icon.setChecked(true);
1994 } else {
1995 bookmark_icon.setChecked(false);
1996 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001997
1998 // update reader mode checkbox
1999 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2000 readerSwitcher.setVisible(false);
2001 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002002 }
2003
Michael Kolb4bf79712011-07-14 14:18:12 -07002004 @Override
2005 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002006 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07002007 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07002008 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07002009 // Following flag is used to identify schemes for which the LIVE_MENU
2010 // items defined in res/menu/browser.xml should be enabled
2011 boolean isLiveScheme = false;
2012 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002013 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07002014
2015 boolean isDistillable = false;
2016 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002017 resetMenuItems(menu);
2018
Michael Kolb4bf79712011-07-14 14:18:12 -07002019 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002020 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002021 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002022 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002023 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002024 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002025 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07002026
2027 isDistillable = tab.isDistillable();
2028 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07002029 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002030
2031 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2032 forward.setEnabled(canGoForward);
2033
Michael Kolb4bf79712011-07-14 14:18:12 -07002034 // decide whether to show the share link option
2035 PackageManager pm = mActivity.getPackageManager();
2036 Intent send = new Intent(Intent.ACTION_SEND);
2037 send.setType("text/plain");
2038 ResolveInfo ri = pm.resolveActivity(send,
2039 PackageManager.MATCH_DEFAULT_ONLY);
2040 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2041
2042 boolean isNavDump = mSettings.enableNavDump();
2043 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2044 nav.setVisible(isNavDump);
2045 nav.setEnabled(isNavDump);
2046
2047 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002048 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2049 uaSwitcher.setChecked(isDesktopUa);
Sagar Dhawanef06f562015-09-07 15:23:22 +02002050 setMenuItemVisibility(menu, R.id.find_menu_id, isLive);
2051 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002052 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002053 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002054 setMenuItemVisibility(menu, R.id.add_to_homescreen,
Sagar Dhawanef06f562015-09-07 15:23:22 +02002055 isLive && isPageFinished);
Pankaj Garg49b79252014-11-07 17:33:41 -08002056 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002057 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002058 // history and snapshots item are the members of COMBO menu group,
2059 // so if show history item, only make snapshots item invisible.
2060 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002061
Ze G Riande2a675c22015-06-03 11:15:24 -07002062
2063 // update reader mode checkbox
2064 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2065 // The reader mode checkbox is hidden only
2066 // when the current page is neither distillable nor distilled
2067 readerSwitcher.setVisible(isDistillable || isDistilled);
2068 readerSwitcher.setChecked(isDistilled);
2069
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002070 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002071 }
2072
Pankaj Gargf49e0222015-09-01 12:19:13 -07002073 private Bitmap cropAndScaleBitmap(Bitmap bm, int width, int height) {
2074 if (width == 0 || height == 0 || bm == null)
2075 return bm;
2076
2077 Bitmap cropped;
2078
2079 if (bm.getHeight() > bm.getWidth()) {
2080 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
2081 bm.getWidth() * height / width, null, true);
2082 } else {
2083 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getHeight() * width / height,
2084 bm.getHeight(), null, true);
2085 }
2086
2087 Bitmap scaled = Bitmap.createScaledBitmap(cropped, width, height, true);
2088 cropped.recycle();
2089 return scaled;
2090 }
2091
John Reck9c35b9c2012-05-30 10:08:50 -07002092 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002093 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002094 if (null == getCurrentTopWebView()) {
2095 return false;
2096 }
2097 if (mMenuIsDown) {
2098 // The shortcut action consumes the MENU. Even if it is still down,
2099 // it won't trigger the next shortcut action. In the case of the
2100 // shortcut action triggering a new activity, like Bookmarks, we
2101 // won't get onKeyUp for MENU. So it is important to reset it here.
2102 mMenuIsDown = false;
2103 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002104 if (mUi.onOptionsItemSelected(item)) {
2105 // ui callback handled it
2106 return true;
2107 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002108 switch (item.getItemId()) {
2109 // -- Main menu
2110 case R.id.new_tab_menu_id:
2111 openTabToHomePage();
2112 break;
2113
2114 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002115 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002116 break;
2117
2118 case R.id.goto_menu_id:
2119 editUrl();
2120 break;
2121
2122 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002123 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2124 break;
2125
Michael Kolb315d5022011-10-13 12:47:11 -07002126 case R.id.snapshots_menu_id:
2127 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002128 break;
2129
Pankaj Garg49b79252014-11-07 17:33:41 -08002130 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002131 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002132 break;
2133
2134 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002135 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002136 stopLoading();
2137 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002138 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 getCurrentTopWebView().reload();
2140 }
2141 break;
2142
Michael Kolb8233fac2010-10-26 16:08:53 -07002143 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002144 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 break;
2146
2147 case R.id.close_menu_id:
2148 // Close the subwindow if it exists.
2149 if (mTabControl.getCurrentSubWindow() != null) {
2150 dismissSubWindow(mTabControl.getCurrentTab());
2151 break;
2152 }
2153 closeCurrentTab();
2154 break;
2155
kaiyiza8b6dbb2013-07-29 18:11:22 +08002156 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002157 Object[] params = { new String("persist.debug.browsermonkeytest")};
2158 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002159 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002160 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002161 if (ret != null && ret.equals("enable"))
2162 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002163 if (BrowserConfig.getInstance(getContext())
2164 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2165 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002166 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002167 case R.id.homepage_menu_id:
2168 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002169 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002170 break;
2171
2172 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002173 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002174 break;
2175
2176 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002177 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002178 break;
2179
John Reck2bc80422011-06-30 15:11:49 -07002180 case R.id.save_snapshot_menu_id:
2181 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002182 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002183 createScreenshotAsync(
Pankaj Gargf49e0222015-09-01 12:19:13 -07002184 source,
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002185 new ValueCallback<Bitmap>() {
2186 @Override
2187 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002188 Bitmap bm = cropAndScaleBitmap(bitmap,
2189 getDesiredThumbnailWidth(mActivity),
2190 getDesiredThumbnailHeight(mActivity));
2191 new SaveSnapshotTask(source, bm).execute();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002192 }
2193 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002194 break;
2195
Michael Kolb8233fac2010-10-26 16:08:53 -07002196 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002197 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002198 break;
2199
John Recke1a03a32011-09-14 17:04:16 -07002200 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002201 // passing null to distinguish between
2202 // "go live" button and navigating a web page
2203 // on a snapshot tab
2204 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002205 case R.id.share_page_menu_id:
2206 Tab currentTab = mTabControl.getCurrentTab();
2207 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002208 return false;
2209 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002210 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002211 break;
2212
2213 case R.id.dump_nav_menu_id:
2214 getCurrentTopWebView().debugDump();
2215 break;
2216
Michael Kolb8233fac2010-10-26 16:08:53 -07002217 case R.id.zoom_in_menu_id:
2218 getCurrentTopWebView().zoomIn();
2219 break;
2220
2221 case R.id.zoom_out_menu_id:
2222 getCurrentTopWebView().zoomOut();
2223 break;
2224
2225 case R.id.view_downloads_menu_id:
2226 viewDownloads();
2227 break;
2228
John Reck42229bc2011-08-19 13:26:43 -07002229 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002230 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002231 break;
2232
Ze G Riande2a675c22015-06-03 11:15:24 -07002233 case R.id.reader_mode_menu_id:
2234 toggleReaderMode();
2235 break;
2236
Michael Kolb8233fac2010-10-26 16:08:53 -07002237 case R.id.window_one_menu_id:
2238 case R.id.window_two_menu_id:
2239 case R.id.window_three_menu_id:
2240 case R.id.window_four_menu_id:
2241 case R.id.window_five_menu_id:
2242 case R.id.window_six_menu_id:
2243 case R.id.window_seven_menu_id:
2244 case R.id.window_eight_menu_id:
2245 {
2246 int menuid = item.getItemId();
2247 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2248 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2249 Tab desiredTab = mTabControl.getTab(id);
2250 if (desiredTab != null &&
2251 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002252 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002253 }
2254 break;
2255 }
2256 }
2257 }
2258 break;
2259
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002260 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002261 Bundle bundle = new Bundle();
2262 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2263 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2264 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2265 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2266 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002267 break;
2268
Pankaj Garg49b79252014-11-07 17:33:41 -08002269 case R.id.add_to_homescreen:
2270 final WebView w = getCurrentTopWebView();
2271 final EditText input = new EditText(getContext());
2272 input.setText(w.getTitle());
2273 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002274 .setTitle(getContext().getResources().getString(
2275 R.string.add_to_homescreen))
2276 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002277 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002278 .setPositiveButton(getContext().getResources().getString(
2279 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002280 public void onClick(DialogInterface dialog, int whichButton) {
2281 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2282 getContext(),
2283 w.getUrl(),
2284 input.getText().toString(),
2285 w.getViewportBitmap(),
2286 w.getFavicon()));
2287
2288 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2289 .addCategory(Intent.CATEGORY_HOME));
2290 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002291 .setNegativeButton(getContext().getResources().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07002292 R.string.cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002293 public void onClick(DialogInterface dialog, int whichButton) {
2294 // Do nothing.
2295 }
2296 })
2297 .show();
2298 break;
2299
Michael Kolb8233fac2010-10-26 16:08:53 -07002300 default:
2301 return false;
2302 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002303 return true;
2304 }
2305
John Reck68234a92012-04-19 15:27:12 -07002306 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2307 implements OnCancelListener {
2308
2309 private Tab mTab;
2310 private Dialog mProgressDialog;
2311 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002312 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002313
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002314 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002315 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002316 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002317 }
2318
2319 @Override
2320 protected void onPreExecute() {
2321 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2322 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2323 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002324 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002325 }
2326
2327 @Override
2328 protected Long doInBackground(Void... params) {
2329 if (!mTab.saveViewState(mValues)) {
2330 return null;
2331 }
2332 if (isCancelled()) {
2333 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2334 File file = mActivity.getFileStreamPath(path);
2335 if (!file.delete()) {
2336 file.deleteOnExit();
2337 }
2338 return null;
2339 }
2340 final ContentResolver cr = mActivity.getContentResolver();
2341 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2342 if (result == null) {
2343 return null;
2344 }
2345 long id = ContentUris.parseId(result);
2346 return id;
2347 }
2348
2349 @Override
2350 protected void onPostExecute(Long id) {
2351 if (isCancelled()) {
2352 return;
2353 }
2354 mProgressDialog.dismiss();
2355 if (id == null) {
2356 Toast.makeText(mActivity, R.string.snapshot_failed,
2357 Toast.LENGTH_SHORT).show();
2358 return;
2359 }
2360 Bundle b = new Bundle();
2361 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2362 mUi.showComboView(ComboViews.Snapshots, b);
2363 }
2364
2365 @Override
2366 public void onCancel(DialogInterface dialog) {
2367 cancel(true);
2368 }
2369 }
2370
Michael Kolb80f75082012-04-10 10:50:06 -07002371 @Override
2372 public void toggleUserAgent() {
2373 WebView web = getCurrentWebView();
2374 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002375 }
2376
Ze G Riande2a675c22015-06-03 11:15:24 -07002377 // This function calls the method in the webview to enable/disable
2378 // the reader mode through the DOM distiller
2379 public void toggleReaderMode() {
2380 Tab t = mTabControl.getCurrentTab();
2381 if (t.isDistilled()) {
2382 closeTab(t);
2383 } else if (t.isDistillable()) {
2384 openTab(t.getDistilledUrl(), false, true, false, t);
2385 }
2386 }
2387
Michael Kolb80f75082012-04-10 10:50:06 -07002388 @Override
2389 public void findOnPage() {
2390 getCurrentTopWebView().showFindDialog(null, true);
2391 }
2392
2393 @Override
2394 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002395 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002396 }
2397
2398 @Override
2399 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002400 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002401 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2402 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002403 }
2404 else {
2405 WebView w = getCurrentTopWebView();
2406 if (w == null)
2407 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002408 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002409 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002410 }
Michael Kolb80f75082012-04-10 10:50:06 -07002411 }
2412
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002413 public boolean goLive(String url) {
2414 if (!getCurrentTab().isSnapshot())
2415 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002416 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002417
2418 if (url == null) { // "go live" button was clicked
2419 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002420 closeTab(t);
2421 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002422 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002423 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002424 return true;
John Recke1a03a32011-09-14 17:04:16 -07002425 }
2426
luxiaolb40014b2013-07-19 10:01:43 +08002427 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002428 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002429 new AlertDialog.Builder(activity)
2430 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002431 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002432 .setMessage(R.string.exit_browser_msg)
2433 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2434 public void onClick(DialogInterface dialog, int which) {
2435 activity.moveTaskToBack(true);
2436 dialog.dismiss();
2437 }
2438 })
2439 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2440 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002441 mCrashRecoveryHandler.clearState(true);
2442 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002443 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002444 dialog.dismiss();
2445 }
2446 })
2447 .show();
2448 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002449
Michael Kolb315d5022011-10-13 12:47:11 -07002450 @Override
2451 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002452 }
2453
John Reck9c35b9c2012-05-30 10:08:50 -07002454 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002455 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002456 // Let the History and Bookmark fragments handle menus they created.
2457 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2458 return false;
2459 }
2460
Michael Kolb8233fac2010-10-26 16:08:53 -07002461 int id = item.getItemId();
2462 boolean result = true;
2463 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002464 // -- Browser context menu
2465 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002466 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002467 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002468 case R.id.copy_link_context_menu_id:
2469 final WebView webView = getCurrentTopWebView();
2470 if (null == webView) {
2471 result = false;
2472 break;
2473 }
2474 final HashMap<String, WebView> hrefMap =
2475 new HashMap<String, WebView>();
2476 hrefMap.put("webview", webView);
2477 final Message msg = mHandler.obtainMessage(
2478 FOCUS_NODE_HREF, id, 0, hrefMap);
2479 webView.requestFocusNodeHref(msg);
2480 break;
2481
2482 default:
2483 // For other context menus
2484 result = onOptionsItemSelected(item);
2485 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002486 return result;
2487 }
2488
2489 /**
2490 * support programmatically opening the context menu
2491 */
2492 public void openContextMenu(View view) {
2493 mActivity.openContextMenu(view);
2494 }
2495
2496 /**
2497 * programmatically open the options menu
2498 */
2499 public void openOptionsMenu() {
2500 mActivity.openOptionsMenu();
2501 }
2502
John Reck9c35b9c2012-05-30 10:08:50 -07002503 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002504 public boolean onMenuOpened(int featureId, Menu menu) {
2505 if (mOptionsMenuOpen) {
2506 if (mConfigChanged) {
2507 // We do not need to make any changes to the state of the
2508 // title bar, since the only thing that happened was a
2509 // change in orientation
2510 mConfigChanged = false;
2511 } else {
2512 if (!mExtendedMenuOpen) {
2513 mExtendedMenuOpen = true;
2514 mUi.onExtendedMenuOpened();
2515 } else {
2516 // Switching the menu back to icon view, so show the
2517 // title bar once again.
2518 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002519 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002520 }
2521 }
2522 } else {
2523 // The options menu is closed, so open it, and show the title
2524 mOptionsMenuOpen = true;
2525 mConfigChanged = false;
2526 mExtendedMenuOpen = false;
2527 mUi.onOptionsMenuOpened();
2528 }
2529 return true;
2530 }
2531
John Reck9c35b9c2012-05-30 10:08:50 -07002532 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002533 public void onOptionsMenuClosed(Menu menu) {
2534 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002535 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002536 }
2537
John Reck9c35b9c2012-05-30 10:08:50 -07002538 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002539 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002540 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 }
2542
2543 // Helper method for getting the top window.
2544 @Override
2545 public WebView getCurrentTopWebView() {
2546 return mTabControl.getCurrentTopWebView();
2547 }
2548
2549 @Override
2550 public WebView getCurrentWebView() {
2551 return mTabControl.getCurrentWebView();
2552 }
2553
2554 /*
2555 * This method is called as a result of the user selecting the options
2556 * menu to see the download window. It shows the download window on top of
2557 * the current window.
2558 */
2559 void viewDownloads() {
2560 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2561 mActivity.startActivity(intent);
2562 }
2563
John Reck30b065e2011-07-19 10:58:05 -07002564 int getActionModeHeight() {
2565 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2566 new int[] { android.R.attr.actionBarSize });
2567 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2568 actionBarSizeTypedArray.recycle();
2569 return size;
2570 }
2571
Michael Kolb8233fac2010-10-26 16:08:53 -07002572 // action mode
2573
John Reck9c35b9c2012-05-30 10:08:50 -07002574 @Override
2575 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002576 mUi.onActionModeStarted(mode);
2577 mActionMode = mode;
2578 }
2579
2580 /*
2581 * True if a custom ActionMode (i.e. find or select) is in use.
2582 */
2583 @Override
2584 public boolean isInCustomActionMode() {
2585 return mActionMode != null;
2586 }
2587
2588 /*
2589 * End the current ActionMode.
2590 */
2591 @Override
2592 public void endActionMode() {
2593 if (mActionMode != null) {
2594 mActionMode.finish();
2595 }
2596 }
2597
2598 /*
2599 * Called by find and select when they are finished. Replace title bars
2600 * as necessary.
2601 */
John Reck9c35b9c2012-05-30 10:08:50 -07002602 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002603 public void onActionModeFinished(ActionMode mode) {
2604 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002605 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 mActionMode = null;
2607 }
2608
2609 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002610 final Tab tab = getCurrentTab();
2611 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 }
2613
2614 // bookmark handling
2615
2616 /**
2617 * add the current page as a bookmark to the given folder id
2618 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002619 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002620 * bookmarked, and if it is, edit that bookmark. If false, and
2621 * the site is already bookmarked, do not attempt to edit the
2622 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002623 */
2624 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002625 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002626 WebView w = getCurrentTopWebView();
2627 if (w == null) {
2628 return null;
2629 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002630 Intent i = new Intent(mActivity,
2631 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002632 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2633 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002634 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002635 if (touchIconUrl != null) {
2636 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2637 WebSettings settings = w.getSettings();
2638 if (settings != null) {
2639 i.putExtra(AddBookmarkPage.USER_AGENT,
2640 settings.getUserAgentString());
2641 }
2642 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002643 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002644 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002645 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002646 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2647 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002648 // Put the dialog at the upper right of the screen, covering the
2649 // star on the title bar.
2650 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002651 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002652 }
2653
2654 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002655 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002656 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002657 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002658 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002659 }
2660
Vivek Sekharb54614f2014-05-01 19:03:37 -07002661 @Override
2662 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2663 boolean capture) {
2664 mUploadHandler = new UploadHandler(this);
2665 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2666 }
2667
Michael Kolb8233fac2010-10-26 16:08:53 -07002668 // thumbnails
2669
2670 /**
2671 * Return the desired width for thumbnail screenshots, which are stored in
2672 * the database, and used on the bookmarks screen.
2673 * @param context Context for finding out the density of the screen.
2674 * @return desired width for thumbnail screenshot.
2675 */
2676 static int getDesiredThumbnailWidth(Context context) {
2677 return context.getResources().getDimensionPixelOffset(
2678 R.dimen.bookmarkThumbnailWidth);
2679 }
2680
2681 /**
2682 * Return the desired height for thumbnail screenshots, which are stored in
2683 * the database, and used on the bookmarks screen.
2684 * @param context Context for finding out the density of the screen.
2685 * @return desired height for thumbnail screenshot.
2686 */
2687 static int getDesiredThumbnailHeight(Context context) {
2688 return context.getResources().getDimensionPixelOffset(
2689 R.dimen.bookmarkThumbnailHeight);
2690 }
2691
Pankaj Gargf49e0222015-09-01 12:19:13 -07002692 void createScreenshotAsync(Tab tab, final ValueCallback<Bitmap> cb) {
2693 if (tab == null) {
2694 cb.onReceiveValue(null);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002695 return;
2696 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002697
2698 tab.capture();
2699
2700 synchronized (mThumbnailCbList) {
2701 mThumbnailCbList.add(cb);
2702 }
2703 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002704
Michael Kolb8233fac2010-10-26 16:08:53 -07002705 private class Copy implements OnMenuItemClickListener {
2706 private CharSequence mText;
2707
John Reck9c35b9c2012-05-30 10:08:50 -07002708 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002709 public boolean onMenuItemClick(MenuItem item) {
2710 copy(mText);
2711 return true;
2712 }
2713
2714 public Copy(CharSequence toCopy) {
2715 mText = toCopy;
2716 }
2717 }
2718
Leon Scroggins63c02662010-11-18 15:16:27 -05002719 private static class Download implements OnMenuItemClickListener {
2720 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002721 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002722 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002723 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002724 private static final String FALLBACK_EXTENSION = "dat";
2725 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002726
John Reck9c35b9c2012-05-30 10:08:50 -07002727 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002728 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002729 if (DataUri.isDataUri(mText)) {
2730 saveDataUri();
2731 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002732 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08002733 null, null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002734 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002735 return true;
2736 }
2737
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002738 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2739 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002740 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002741 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002742 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002743 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002744 }
George Mount387d45d2011-10-07 15:57:53 -07002745
2746 /**
2747 * Treats mText as a data URI and writes its contents to a file
2748 * based on the current time.
2749 */
2750 private void saveDataUri() {
2751 FileOutputStream outputStream = null;
2752 try {
2753 DataUri uri = new DataUri(mText);
2754 File target = getTarget(uri);
2755 outputStream = new FileOutputStream(target);
2756 outputStream.write(uri.getData());
2757 final DownloadManager manager =
2758 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2759 manager.addCompletedDownload(target.getName(),
2760 mActivity.getTitle().toString(), false,
2761 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002762 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002763 } catch (IOException e) {
2764 Log.e(LOGTAG, "Could not save data URL");
2765 } finally {
2766 if (outputStream != null) {
2767 try {
2768 outputStream.close();
2769 } catch (IOException e) {
2770 // ignore close errors
2771 }
2772 }
2773 }
2774 }
2775
2776 /**
2777 * Creates a File based on the current time stamp and uses
2778 * the mime type of the DataUri to get the extension.
2779 */
2780 private File getTarget(DataUri uri) throws IOException {
2781 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002782 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002783 String nameBase = format.format(new Date());
2784 String mimeType = uri.getMimeType();
2785 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2786 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2787 if (extension == null) {
2788 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2789 extension = FALLBACK_EXTENSION;
2790 }
2791 extension = "." + extension; // createTempFile needs the '.'
2792 File targetFile = File.createTempFile(nameBase, extension, dir);
2793 return targetFile;
2794 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002795 }
2796
Cary Clark8974d282010-11-22 10:46:05 -05002797 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002798 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002799
John Reck9c35b9c2012-05-30 10:08:50 -07002800 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002801 public boolean onMenuItemClick(MenuItem item) {
2802 if (mWebView != null) {
2803 return mWebView.selectText();
2804 }
2805 return false;
2806 }
2807
2808 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002809 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002810 }
2811
2812 }
2813
Michael Kolb8233fac2010-10-26 16:08:53 -07002814 /********************** TODO: UI stuff *****************************/
2815
2816 // these methods have been copied, they still need to be cleaned up
2817
2818 /****************** tabs ***************************************************/
2819
2820 // basic tab interactions:
2821
2822 // it is assumed that tabcontrol already knows about the tab
2823 protected void addTab(Tab tab) {
2824 mUi.addTab(tab);
2825 }
2826
2827 protected void removeTab(Tab tab) {
2828 mUi.removeTab(tab);
2829 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002830 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002831 }
2832
Michael Kolbf2055602011-04-09 17:20:03 -07002833 @Override
2834 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002835 // monkey protection against delayed start
2836 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002837
2838 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2839 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002840 mTabControl.setCurrentTab(tab);
2841 // the tab is guaranteed to have a webview after setCurrentTab
2842 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002843
2844
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002845 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002846 //Purge active tabs
2847 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002848 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002849 }
2850
John Reck8bcafc12011-08-29 16:43:02 -07002851 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002852 Tab current = mTabControl.getCurrentTab();
2853 if (current != null
2854 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002855 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002856 }
2857 }
2858
John Reck26b18322011-06-21 13:08:58 -07002859 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002860 //Cancel navscreen request
2861 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002862 // Dismiss the subwindow if applicable.
2863 dismissSubWindow(appTab);
2864 // Since we might kill the WebView, remove it from the
2865 // content view first.
2866 mUi.detachTab(appTab);
2867 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002868 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002869 // TODO: analyze why the remove and add are necessary
2870 mUi.attachTab(appTab);
2871 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002872 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002873 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002874 } else {
2875 // If the tab was the current tab, we have to attach
2876 // it to the view system again.
2877 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002878 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002879 }
2880 }
2881
2882 // Remove the sub window if it exists. Also called by TabControl when the
2883 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002884 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002885 public void dismissSubWindow(Tab tab) {
2886 removeSubWindow(tab);
2887 // dismiss the subwindow. This will destroy the WebView.
2888 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002889 WebView wv = getCurrentTopWebView();
2890 if (wv != null) {
2891 wv.requestFocus();
2892 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002893 }
2894
2895 @Override
2896 public void removeSubWindow(Tab t) {
2897 if (t.getSubWebView() != null) {
2898 mUi.removeSubWindow(t.getSubViewContainer());
2899 }
2900 }
2901
2902 @Override
2903 public void attachSubWindow(Tab tab) {
2904 if (tab.getSubWebView() != null) {
2905 mUi.attachSubWindow(tab.getSubViewContainer());
2906 getCurrentTopWebView().requestFocus();
2907 }
2908 }
2909
Mathew Inwood29721c22011-06-29 17:55:24 +01002910 private Tab showPreloadedTab(final UrlData urlData) {
2911 if (!urlData.isPreloaded()) {
2912 return null;
2913 }
2914 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2915 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2916 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002917 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002918 // Could not submit query. Fallback to regular tab creation
2919 tabControl.destroy();
2920 return null;
2921 }
2922 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002923 // check tab count and make room for new tab
2924 if (!mTabControl.canCreateNewTab()) {
2925 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2926 if (leastUsed != null) {
2927 closeTab(leastUsed);
2928 }
2929 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002930 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002931 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002932 mTabControl.addPreloadedTab(t);
2933 addTab(t);
2934 setActiveTab(t);
2935 return t;
2936 }
2937
Michael Kolb7bcafde2011-05-09 13:55:59 -07002938 // open a non inconito tab with the given url data
2939 // and set as active tab
2940 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002941 Tab tab = showPreloadedTab(urlData);
2942 if (tab == null) {
2943 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002944 if ((tab != null) && !urlData.isEmpty()) {
2945 loadUrlDataIn(tab, urlData);
2946 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002947 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002948 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002949 }
2950
Michael Kolb843510f2010-12-09 10:51:49 -08002951 @Override
2952 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002953 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002954 }
2955
Michael Kolb8233fac2010-10-26 16:08:53 -07002956 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002957 public Tab openIncognitoTab() {
2958 return openTab(INCOGNITO_URI, true, true, false);
2959 }
2960
2961 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002962 public Tab openTab(String url, boolean incognito, boolean setActive,
2963 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002964 return openTab(url, incognito, setActive, useCurrent, null);
2965 }
2966
2967 @Override
2968 public Tab openTab(String url, Tab parent, boolean setActive,
2969 boolean useCurrent) {
2970 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2971 setActive, useCurrent, parent);
2972 }
2973
2974 public Tab openTab(String url, boolean incognito, boolean setActive,
2975 boolean useCurrent, Tab parent) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002976 boolean change_tabs = false;
2977 if (setActive) {
2978 Tab currentTab = mTabControl.getCurrentTab();
2979 if (currentTab != null) {
2980 change_tabs = setActive;
2981 setActive = false;
Sagar Dhawan9a1d8e72015-09-11 17:14:38 -07002982 if (mUi instanceof PhoneUi)
2983 currentTab.capture();
Pankaj Gargf49e0222015-09-01 12:19:13 -07002984 }
2985 }
2986
Michael Kolb7bcafde2011-05-09 13:55:59 -07002987 Tab tab = createNewTab(incognito, setActive, useCurrent);
2988 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002989 if (parent instanceof SnapshotTab) {
2990 addTab(tab);
2991 if (setActive)
2992 setActiveTab(tab);
2993 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002994 parent.addChildTab(tab);
2995 }
Michael Kolb519d2282011-05-09 17:03:19 -07002996 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002997 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002998 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002999 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07003000
3001 if (change_tabs) {
Sagar Dhawan20b1deb2015-10-12 11:13:18 -07003002 setActiveTab(tab);
Pankaj Gargf49e0222015-09-01 12:19:13 -07003003 synchronized (mThumbnailCbList) {
3004 startCaptureTimer();
3005 mLatestCreatedTab = tab;
3006 mThumbnailCbList.add(new ValueCallback<Bitmap>() {
3007 @Override
3008 public void onReceiveValue(Bitmap bitmap) {
3009 synchronized (mThumbnailCbList) {
3010 if (mLatestCreatedTab != null) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07003011 mLatestCreatedTab = null;
3012 }
3013 stopCaptureTimer();
3014 }
3015 }
3016 });
3017 }
3018 }
3019
Michael Kolb7bcafde2011-05-09 13:55:59 -07003020 return tab;
3021 }
3022
Pankaj Gargf49e0222015-09-01 12:19:13 -07003023 private void startCaptureTimer() {
3024 mCaptureTimer = new CountDownTimer(mCaptureMaxWaitMS, mCaptureMaxWaitMS) {
3025 @Override
3026 public void onTick(long millisUntilFinished) {
3027 // Do nothing
3028 }
3029
3030 @Override
3031 public void onFinish() {
3032 synchronized (mThumbnailCbList) {
3033 Log.e(LOGTAG, "Screen capture timed out while opening new tab");
3034 if (mLatestCreatedTab != null) {
3035 setActiveTab(mLatestCreatedTab);
3036 mLatestCreatedTab = null;
3037 }
3038 }
3039 }
3040 }.start();
3041 }
3042
3043 private void stopCaptureTimer() {
3044 if (mCaptureTimer != null) {
3045 mCaptureTimer.cancel();
3046 mCaptureTimer = null;
3047 }
3048 }
3049
3050
Michael Kolb7bcafde2011-05-09 13:55:59 -07003051 // this method will attempt to create a new tab
3052 // incognito: private browsing tab
3053 // setActive: ste tab as current tab
3054 // useCurrent: if no new tab can be created, return current tab
3055 private Tab createNewTab(boolean incognito, boolean setActive,
3056 boolean useCurrent) {
3057 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07003058 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
3059 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003060 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003061 if (mTabControl.canCreateNewTab()) {
3062 tab = mTabControl.createNewTab(incognito, !setActive);
3063 addTab(tab);
3064 if (setActive) {
3065 setActiveTab(tab);
3066 } else {
3067 tab.pause();
3068 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003069 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003070 if (useCurrent) {
3071 tab = mTabControl.getCurrentTab();
3072 reuseTab(tab, null);
3073 } else {
3074 mUi.showMaxTabsWarning();
3075 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003076 }
3077 }
3078 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003079 }
3080
John Reck2bc80422011-06-30 15:11:49 -07003081 @Override
3082 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3083 SnapshotTab tab = null;
3084 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07003085 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07003086 addTab(tab);
3087 if (setActive) {
3088 setActiveTab(tab);
3089 }
3090 } else {
3091 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003092 }
3093 return tab;
3094 }
3095
Michael Kolb8233fac2010-10-26 16:08:53 -07003096 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003097 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003098 * @return boolean True if we successfully switched to a different tab. If
3099 * the indexth tab is null, or if that tab is the same as
3100 * the current one, return false.
3101 */
3102 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003103 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003104 Tab currentTab = mTabControl.getCurrentTab();
3105 if (tab == null || tab == currentTab) {
3106 return false;
3107 }
3108 setActiveTab(tab);
3109 return true;
3110 }
3111
3112 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003113 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003114 closeCurrentTab(false);
3115 }
3116
3117 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003118 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003119 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003120 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003121 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003122 return;
3123 }
Michael Kolbc831b632011-05-11 09:30:34 -07003124 final Tab current = mTabControl.getCurrentTab();
3125 final int pos = mTabControl.getCurrentPosition();
3126 Tab newTab = current.getParent();
3127 if (newTab == null) {
3128 newTab = mTabControl.getTab(pos + 1);
3129 if (newTab == null) {
3130 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003131 }
3132 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003133 if (andQuit) {
3134 mTabControl.setCurrentTab(newTab);
3135 closeTab(current);
3136 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003137 // Close window
3138 closeTab(current);
3139 }
3140 }
3141
3142 /**
3143 * Close the tab, remove its associated title bar, and adjust mTabControl's
3144 * current tab to a valid value.
3145 */
3146 @Override
3147 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003148 if (tab == mTabControl.getCurrentTab()) {
3149 closeCurrentTab();
3150 } else {
3151 removeTab(tab);
3152 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003153 }
3154
Afzal Najamd4e33312012-04-26 01:54:01 -04003155 /**
3156 * Close all tabs except the current one
3157 */
3158 @Override
3159 public void closeOtherTabs() {
3160 int inactiveTabs = mTabControl.getTabCount() - 1;
3161 for (int i = inactiveTabs; i >= 0; i--) {
3162 Tab tab = mTabControl.getTab(i);
3163 if (tab != mTabControl.getCurrentTab()) {
3164 removeTab(tab);
3165 }
3166 }
3167 }
3168
Michael Kolb8233fac2010-10-26 16:08:53 -07003169 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003170 protected void loadUrlFromContext(String url) {
3171 Tab tab = getCurrentTab();
3172 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003173 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003174 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003175 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003176 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003177 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003178 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003179 }
3180 }
3181 }
3182
3183 /**
3184 * Load the URL into the given WebView and update the title bar
3185 * to reflect the new load. Call this instead of WebView.loadUrl
3186 * directly.
3187 * @param view The WebView used to load url.
3188 * @param url The URL to load.
3189 */
John Reck71e51422011-07-01 16:49:28 -07003190 @Override
3191 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003192 loadUrl(tab, url, null);
3193 }
3194
3195 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3196 if (tab != null) {
3197 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003198 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003199 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003200 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003201 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003202 }
3203
3204 /**
3205 * Load UrlData into a Tab and update the title bar to reflect the new
3206 * load. Call this instead of UrlData.loadIn directly.
3207 * @param t The Tab used to load.
3208 * @param data The UrlData being loaded.
3209 */
3210 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003211 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003212 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003213 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003214 } else {
John Reck38b39652012-06-05 09:22:59 -07003215 if (t != null && data.mDisableUrlOverride) {
3216 t.disableUrlOverridingForLoad();
3217 }
John Reck26b18322011-06-21 13:08:58 -07003218 loadUrl(t, data.mUrl, data.mHeaders);
3219 }
3220 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003221 }
3222
John Reck30c714c2010-12-16 17:30:34 -08003223 @Override
3224 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003225 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003226 //In case of tab can go back (aka tab has navigation entry) do nothing
3227 //else just load homepage in current tab.
3228 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003229 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003230 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003231 }
3232
3233 void goBackOnePageOrQuit() {
3234 Tab current = mTabControl.getCurrentTab();
3235 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003236 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3237 showExitDialog(mActivity);
3238 } else {
3239 /*
3240 * Instead of finishing the activity, simply push this to the back
3241 * of the stack and let ActivityManager to choose the foreground
3242 * activity. As BrowserActivity is singleTask, it will be always the
3243 * root of the task. So we can use either true or false for
3244 * moveTaskToBack().
3245 */
3246 mActivity.moveTaskToBack(true);
3247 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003248 return;
3249 }
John Reckef654f12011-07-12 16:42:08 -07003250 if (current.canGoBack()) {
3251 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003252 } else {
3253 // Check to see if we are closing a window that was created by
3254 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003255 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003256 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003257 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003258 // Now we close the other tab
3259 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003260 } else if (BrowserConfig.getInstance(getContext())
3261 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3262 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003263 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003264 /*
3265 * Instead of finishing the activity, simply push this to the back
3266 * of the stack and let ActivityManager to choose the foreground
3267 * activity. As BrowserActivity is singleTask, it will be always the
3268 * root of the task. So we can use either true or false for
3269 * moveTaskToBack().
3270 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003271 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003272 }
3273 }
3274 }
3275
3276 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003277 * helper method for key handler
3278 * returns the current tab if it can't advance
3279 */
Michael Kolbc831b632011-05-11 09:30:34 -07003280 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003281 int pos = mTabControl.getCurrentPosition() + 1;
3282 if (pos >= mTabControl.getTabCount()) {
3283 pos = 0;
3284 }
3285 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003286 }
3287
3288 /**
3289 * helper method for key handler
3290 * returns the current tab if it can't advance
3291 */
Michael Kolbc831b632011-05-11 09:30:34 -07003292 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003293 int pos = mTabControl.getCurrentPosition() - 1;
3294 if ( pos < 0) {
3295 pos = mTabControl.getTabCount() - 1;
3296 }
3297 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003298 }
3299
John Reckbcef87f2012-02-03 14:58:34 -08003300 boolean isMenuOrCtrlKey(int keyCode) {
3301 return (KeyEvent.KEYCODE_MENU == keyCode)
3302 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3303 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3304 }
3305
Michael Kolb0035fad2011-03-14 13:25:28 -07003306 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003307 * handle key events in browser
3308 *
3309 * @param keyCode
3310 * @param event
3311 * @return true if handled, false to pass to super
3312 */
John Reck9c35b9c2012-05-30 10:08:50 -07003313 @Override
3314 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003315 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3316 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003317 if (!mUi.isComboViewShowing()) {
3318 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3319 true, false);
3320 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003321 return true;
3322 }
3323
Cary Clark160bbb92011-01-10 11:17:07 -05003324 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003325 // Even if MENU is already held down, we need to call to super to open
3326 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003327 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003328 mMenuIsDown = true;
3329 return false;
3330 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003331
Cary Clark8ff8c662010-12-29 15:03:05 -05003332 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003333 Tab tab = getCurrentTab();
3334 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003335
Cary Clark160bbb92011-01-10 11:17:07 -05003336 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3337 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003338
Michael Kolb8233fac2010-10-26 16:08:53 -07003339 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003340 case KeyEvent.KEYCODE_TAB:
3341 if (event.isCtrlPressed()) {
3342 if (event.isShiftPressed()) {
3343 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003344 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003345 } else {
3346 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003347 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003348 }
3349 return true;
3350 }
3351 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003352 case KeyEvent.KEYCODE_SPACE:
3353 // WebView/WebTextView handle the keys in the KeyDown. As
3354 // the Activity's shortcut keys are only handled when WebView
3355 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003356 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003357 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003358 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003359 pageDown();
3360 }
3361 return true;
3362 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003363 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003364 event.startTracking();
3365 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003366 case KeyEvent.KEYCODE_FORWARD:
3367 if (!noModifiers) break;
3368 tab.goForward();
3369 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003370 case KeyEvent.KEYCODE_DPAD_LEFT:
3371 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003372 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003373 return true;
3374 }
3375 break;
3376 case KeyEvent.KEYCODE_DPAD_RIGHT:
3377 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003378 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003379 return true;
3380 }
3381 break;
3382 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003383 if (ctrl) {
3384 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003385 return true;
3386 }
3387 break;
Michael Kolba4183062011-01-16 10:43:21 -08003388// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003389 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003390 if (ctrl ) {
3391 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003392 return true;
3393 }
3394 break;
Michael Kolba4183062011-01-16 10:43:21 -08003395// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003396// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003397// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003398// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003399// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003400// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003401// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003402// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003403// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003404// case KeyEvent.KEYCODE_M: // unused
3405// case KeyEvent.KEYCODE_N: // in Chrome: new window
3406// case KeyEvent.KEYCODE_O: // in Chrome: open file
3407// case KeyEvent.KEYCODE_P: // in Chrome: print page
3408// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003409// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003410// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3411 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003412 // we can't use the ctrl/shift flags, they check for
3413 // exclusive use of a modifier
3414 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003415 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003416 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003417 } else {
3418 openTabToHomePage();
3419 }
3420 return true;
3421 }
3422 break;
3423// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3424// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003425// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003426// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3427// case KeyEvent.KEYCODE_Y: // unused
3428// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003429 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003430 // it is a regular key and webview is not null
3431 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003432 }
3433
John Reck9c35b9c2012-05-30 10:08:50 -07003434 @Override
3435 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003436 switch(keyCode) {
3437 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003438 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003439 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003440 return true;
3441 }
3442 break;
3443 }
3444 return false;
3445 }
3446
John Reck9c35b9c2012-05-30 10:08:50 -07003447 @Override
3448 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003449 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003450 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003451 if (KeyEvent.KEYCODE_MENU == keyCode
3452 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003453 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003454 }
3455 }
Cary Clark160bbb92011-01-10 11:17:07 -05003456 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003457 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003458 case KeyEvent.KEYCODE_BACK:
3459 if (event.isTracking() && !event.isCanceled()) {
3460 onBackKey();
3461 return true;
3462 }
3463 break;
3464 }
3465 return false;
3466 }
3467
3468 public boolean isMenuDown() {
3469 return mMenuIsDown;
3470 }
3471
John Reck9c35b9c2012-05-30 10:08:50 -07003472 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003473 public void setupAutoFill(Message message) {
3474 // Open the settings activity at the AutoFill profile fragment so that
3475 // the user can create a new profile. When they return, we will dispatch
3476 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003477 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003478 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3479 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003480 }
John Reckb3417f02011-01-14 11:01:05 -08003481
John Reck9c35b9c2012-05-30 10:08:50 -07003482 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003483 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003484 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003485 return true;
3486 }
3487
John Reck1cf4b792011-07-26 10:22:22 -07003488 @Override
3489 public boolean shouldCaptureThumbnails() {
3490 return mUi.shouldCaptureThumbnails();
3491 }
3492
Michael Kolbc3af0672011-08-09 10:24:41 -07003493 @Override
Pankaj Gargf49e0222015-09-01 12:19:13 -07003494 public void onThumbnailCapture(Bitmap bm) {
3495 synchronized (mThumbnailCbList) {
3496 int num_entries = mThumbnailCbList.size();
3497 while (num_entries > 0){
3498 num_entries--;
3499 ValueCallback<Bitmap> cb = mThumbnailCbList.get(num_entries);
3500 cb.onReceiveValue(bm);
3501 }
3502 mThumbnailCbList.clear();
3503 }
3504 }
3505
3506 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003507 public boolean supportsVoice() {
3508 PackageManager pm = mActivity.getPackageManager();
3509 List activities = pm.queryIntentActivities(new Intent(
3510 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3511 return activities.size() != 0;
3512 }
3513
3514 @Override
3515 public void startVoiceRecognizer() {
3516 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003517 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003518 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3519 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3520 mActivity.startActivityForResult(voice, VOICE_RESULT);
3521 }
3522
Pankaj Garg7b279f62014-08-12 14:47:18 -07003523 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003524 if (mLevel == level)
3525 return;
3526 mLevel = level;
3527 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003528 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3529 lp.dimAmount = level;
3530 mActivity.getWindow().setAttributes(lp);
3531 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3532 } else {
3533 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3534 }
3535 }
3536
Michael Kolb0b129122012-06-04 16:31:58 -07003537 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003538 public void setBlockEvents(boolean block) {
3539 mBlockEvents = block;
3540 }
3541
John Reck9c35b9c2012-05-30 10:08:50 -07003542 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003543 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003544 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003545 }
3546
John Reck9c35b9c2012-05-30 10:08:50 -07003547 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003548 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003549 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003550 }
3551
John Reck9c35b9c2012-05-30 10:08:50 -07003552 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003553 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003554 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003555 }
3556
John Reck9c35b9c2012-05-30 10:08:50 -07003557 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003558 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003559 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003560 }
3561
John Reck9c35b9c2012-05-30 10:08:50 -07003562 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003563 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003564 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003565 }
3566
Pankaj Garg49b79252014-11-07 17:33:41 -08003567 @Override
3568 public boolean shouldShowAppMenu() {
3569 return true;
3570 }
3571
3572 @Override
3573 public int getMenuThemeResourceId() {
3574 return R.style.OverflowMenuTheme;
3575 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003576}