blob: 454b25a81aea03f8918bce5f715cdc84e365da9e [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)
1906 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1907 return true;
1908 }
1909
1910 @Override
1911 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001912 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001913 // hold on to the menu reference here; it is used by the page callbacks
1914 // to update the menu based on loading state
1915 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001916 // Note: setVisible will decide whether an item is visible; while
1917 // setEnabled() will decide whether an item is enabled, which also means
1918 // whether the matching shortcut key will function.
1919 switch (mMenuState) {
1920 case EMPTY_MENU:
1921 if (mCurrentMenuState != mMenuState) {
1922 menu.setGroupVisible(R.id.MAIN_MENU, false);
1923 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1924 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1925 }
1926 break;
1927 default:
1928 if (mCurrentMenuState != mMenuState) {
1929 menu.setGroupVisible(R.id.MAIN_MENU, true);
1930 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1931 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1932 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001933 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001934 break;
1935 }
1936 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001937 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001938
1939 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001940 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1941 EditBookmarksRestriction.getInstance()
1942 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001943 }
1944
1945 private void setMenuItemVisibility(Menu menu, int id,
1946 boolean visibility) {
1947 MenuItem item = menu.findItem(id);
1948 if (item != null) {
1949 item.setVisible(visibility);
1950 }
1951 }
1952
Sagar Dhawanef06f562015-09-07 15:23:22 +02001953 private int lookupBookmark( String url) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001954 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001955 int count = 0;
1956 Cursor cursor = null;
1957 try {
1958 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1959 BookmarksLoader.PROJECTION,
Sagar Dhawanef06f562015-09-07 15:23:22 +02001960 "url = ?",
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001961 new String[] {
Sagar Dhawanef06f562015-09-07 15:23:22 +02001962 url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001963 },
1964 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001965
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001966 if (cursor != null)
1967 count = cursor.getCount();
1968
1969 } catch (IllegalStateException e) {
1970 Log.e(LOGTAG, "lookupBookmark ", e);
1971 } finally {
1972 if (null != cursor) {
1973 cursor.close();
1974 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001975 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001976 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001977 }
1978
1979 private void resetMenuItems(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001980 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1981
1982 WebView w = getCurrentTopWebView();
1983 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1984
1985 String title = w.getTitle();
1986 String url = w.getUrl();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001987 mCurrentPageBookmarked = (lookupBookmark(url) > 0);
Pankaj Garg634bf432015-07-13 09:54:21 -07001988 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001989 bookmark_icon.setChecked(true);
1990 } else {
1991 bookmark_icon.setChecked(false);
1992 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001993
1994 // update reader mode checkbox
1995 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1996 readerSwitcher.setVisible(false);
1997 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001998 }
1999
Michael Kolb4bf79712011-07-14 14:18:12 -07002000 @Override
2001 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002002 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07002003 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07002004 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07002005 // Following flag is used to identify schemes for which the LIVE_MENU
2006 // items defined in res/menu/browser.xml should be enabled
2007 boolean isLiveScheme = false;
2008 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002009 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07002010
2011 boolean isDistillable = false;
2012 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002013 resetMenuItems(menu);
2014
Michael Kolb4bf79712011-07-14 14:18:12 -07002015 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002016 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002017 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002018 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002019 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002020 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002021 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07002022
2023 isDistillable = tab.isDistillable();
2024 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07002025 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002026
2027 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2028 forward.setEnabled(canGoForward);
2029
Michael Kolb4bf79712011-07-14 14:18:12 -07002030 // decide whether to show the share link option
2031 PackageManager pm = mActivity.getPackageManager();
2032 Intent send = new Intent(Intent.ACTION_SEND);
2033 send.setType("text/plain");
2034 ResolveInfo ri = pm.resolveActivity(send,
2035 PackageManager.MATCH_DEFAULT_ONLY);
2036 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2037
2038 boolean isNavDump = mSettings.enableNavDump();
2039 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2040 nav.setVisible(isNavDump);
2041 nav.setEnabled(isNavDump);
2042
2043 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002044 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2045 uaSwitcher.setChecked(isDesktopUa);
Sagar Dhawanef06f562015-09-07 15:23:22 +02002046 setMenuItemVisibility(menu, R.id.find_menu_id, isLive);
2047 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002048 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002049 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002050 setMenuItemVisibility(menu, R.id.add_to_homescreen,
Sagar Dhawanef06f562015-09-07 15:23:22 +02002051 isLive && isPageFinished);
Pankaj Garg49b79252014-11-07 17:33:41 -08002052 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002053 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002054 // history and snapshots item are the members of COMBO menu group,
2055 // so if show history item, only make snapshots item invisible.
2056 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002057
Ze G Riande2a675c22015-06-03 11:15:24 -07002058
2059 // update reader mode checkbox
2060 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2061 // The reader mode checkbox is hidden only
2062 // when the current page is neither distillable nor distilled
2063 readerSwitcher.setVisible(isDistillable || isDistilled);
2064 readerSwitcher.setChecked(isDistilled);
2065
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002066 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002067 }
2068
Pankaj Gargf49e0222015-09-01 12:19:13 -07002069 private Bitmap cropAndScaleBitmap(Bitmap bm, int width, int height) {
2070 if (width == 0 || height == 0 || bm == null)
2071 return bm;
2072
2073 Bitmap cropped;
2074
2075 if (bm.getHeight() > bm.getWidth()) {
2076 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
2077 bm.getWidth() * height / width, null, true);
2078 } else {
2079 cropped = Bitmap.createBitmap(bm, 0, 0, bm.getHeight() * width / height,
2080 bm.getHeight(), null, true);
2081 }
2082
2083 Bitmap scaled = Bitmap.createScaledBitmap(cropped, width, height, true);
2084 cropped.recycle();
2085 return scaled;
2086 }
2087
John Reck9c35b9c2012-05-30 10:08:50 -07002088 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002089 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002090 if (null == getCurrentTopWebView()) {
2091 return false;
2092 }
2093 if (mMenuIsDown) {
2094 // The shortcut action consumes the MENU. Even if it is still down,
2095 // it won't trigger the next shortcut action. In the case of the
2096 // shortcut action triggering a new activity, like Bookmarks, we
2097 // won't get onKeyUp for MENU. So it is important to reset it here.
2098 mMenuIsDown = false;
2099 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002100 if (mUi.onOptionsItemSelected(item)) {
2101 // ui callback handled it
2102 return true;
2103 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002104 switch (item.getItemId()) {
2105 // -- Main menu
2106 case R.id.new_tab_menu_id:
2107 openTabToHomePage();
2108 break;
2109
2110 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002111 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002112 break;
2113
2114 case R.id.goto_menu_id:
2115 editUrl();
2116 break;
2117
2118 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002119 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2120 break;
2121
Michael Kolb315d5022011-10-13 12:47:11 -07002122 case R.id.snapshots_menu_id:
2123 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002124 break;
2125
Pankaj Garg49b79252014-11-07 17:33:41 -08002126 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002127 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002128 break;
2129
2130 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002131 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002132 stopLoading();
2133 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002134 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002135 getCurrentTopWebView().reload();
2136 }
2137 break;
2138
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002140 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002141 break;
2142
2143 case R.id.close_menu_id:
2144 // Close the subwindow if it exists.
2145 if (mTabControl.getCurrentSubWindow() != null) {
2146 dismissSubWindow(mTabControl.getCurrentTab());
2147 break;
2148 }
2149 closeCurrentTab();
2150 break;
2151
kaiyiza8b6dbb2013-07-29 18:11:22 +08002152 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002153 Object[] params = { new String("persist.debug.browsermonkeytest")};
2154 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002155 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002156 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002157 if (ret != null && ret.equals("enable"))
2158 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002159 if (BrowserConfig.getInstance(getContext())
2160 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2161 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002162 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002163 case R.id.homepage_menu_id:
2164 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002165 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002166 break;
2167
2168 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002169 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002170 break;
2171
2172 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002173 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002174 break;
2175
John Reck2bc80422011-06-30 15:11:49 -07002176 case R.id.save_snapshot_menu_id:
2177 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002178 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002179 createScreenshotAsync(
Pankaj Gargf49e0222015-09-01 12:19:13 -07002180 source,
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002181 new ValueCallback<Bitmap>() {
2182 @Override
2183 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002184 Bitmap bm = cropAndScaleBitmap(bitmap,
2185 getDesiredThumbnailWidth(mActivity),
2186 getDesiredThumbnailHeight(mActivity));
2187 new SaveSnapshotTask(source, bm).execute();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002188 }
2189 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002190 break;
2191
Michael Kolb8233fac2010-10-26 16:08:53 -07002192 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002193 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002194 break;
2195
John Recke1a03a32011-09-14 17:04:16 -07002196 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002197 // passing null to distinguish between
2198 // "go live" button and navigating a web page
2199 // on a snapshot tab
2200 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002201 case R.id.share_page_menu_id:
2202 Tab currentTab = mTabControl.getCurrentTab();
2203 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002204 return false;
2205 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002206 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002207 break;
2208
2209 case R.id.dump_nav_menu_id:
2210 getCurrentTopWebView().debugDump();
2211 break;
2212
Michael Kolb8233fac2010-10-26 16:08:53 -07002213 case R.id.zoom_in_menu_id:
2214 getCurrentTopWebView().zoomIn();
2215 break;
2216
2217 case R.id.zoom_out_menu_id:
2218 getCurrentTopWebView().zoomOut();
2219 break;
2220
2221 case R.id.view_downloads_menu_id:
2222 viewDownloads();
2223 break;
2224
John Reck42229bc2011-08-19 13:26:43 -07002225 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002226 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002227 break;
2228
Ze G Riande2a675c22015-06-03 11:15:24 -07002229 case R.id.reader_mode_menu_id:
2230 toggleReaderMode();
2231 break;
2232
Michael Kolb8233fac2010-10-26 16:08:53 -07002233 case R.id.window_one_menu_id:
2234 case R.id.window_two_menu_id:
2235 case R.id.window_three_menu_id:
2236 case R.id.window_four_menu_id:
2237 case R.id.window_five_menu_id:
2238 case R.id.window_six_menu_id:
2239 case R.id.window_seven_menu_id:
2240 case R.id.window_eight_menu_id:
2241 {
2242 int menuid = item.getItemId();
2243 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2244 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2245 Tab desiredTab = mTabControl.getTab(id);
2246 if (desiredTab != null &&
2247 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002248 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002249 }
2250 break;
2251 }
2252 }
2253 }
2254 break;
2255
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002256 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002257 Bundle bundle = new Bundle();
2258 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2259 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2260 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2261 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2262 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002263 break;
2264
Pankaj Garg49b79252014-11-07 17:33:41 -08002265 case R.id.add_to_homescreen:
2266 final WebView w = getCurrentTopWebView();
2267 final EditText input = new EditText(getContext());
2268 input.setText(w.getTitle());
2269 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002270 .setTitle(getContext().getResources().getString(
2271 R.string.add_to_homescreen))
2272 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002273 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002274 .setPositiveButton(getContext().getResources().getString(
2275 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002276 public void onClick(DialogInterface dialog, int whichButton) {
2277 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2278 getContext(),
2279 w.getUrl(),
2280 input.getText().toString(),
2281 w.getViewportBitmap(),
2282 w.getFavicon()));
2283
2284 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2285 .addCategory(Intent.CATEGORY_HOME));
2286 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002287 .setNegativeButton(getContext().getResources().getString(
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07002288 R.string.cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002289 public void onClick(DialogInterface dialog, int whichButton) {
2290 // Do nothing.
2291 }
2292 })
2293 .show();
2294 break;
2295
Michael Kolb8233fac2010-10-26 16:08:53 -07002296 default:
2297 return false;
2298 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002299 return true;
2300 }
2301
John Reck68234a92012-04-19 15:27:12 -07002302 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2303 implements OnCancelListener {
2304
2305 private Tab mTab;
2306 private Dialog mProgressDialog;
2307 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002308 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002309
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002310 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002311 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002312 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002313 }
2314
2315 @Override
2316 protected void onPreExecute() {
2317 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2318 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2319 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002320 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002321 }
2322
2323 @Override
2324 protected Long doInBackground(Void... params) {
2325 if (!mTab.saveViewState(mValues)) {
2326 return null;
2327 }
2328 if (isCancelled()) {
2329 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2330 File file = mActivity.getFileStreamPath(path);
2331 if (!file.delete()) {
2332 file.deleteOnExit();
2333 }
2334 return null;
2335 }
2336 final ContentResolver cr = mActivity.getContentResolver();
2337 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2338 if (result == null) {
2339 return null;
2340 }
2341 long id = ContentUris.parseId(result);
2342 return id;
2343 }
2344
2345 @Override
2346 protected void onPostExecute(Long id) {
2347 if (isCancelled()) {
2348 return;
2349 }
2350 mProgressDialog.dismiss();
2351 if (id == null) {
2352 Toast.makeText(mActivity, R.string.snapshot_failed,
2353 Toast.LENGTH_SHORT).show();
2354 return;
2355 }
2356 Bundle b = new Bundle();
2357 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2358 mUi.showComboView(ComboViews.Snapshots, b);
2359 }
2360
2361 @Override
2362 public void onCancel(DialogInterface dialog) {
2363 cancel(true);
2364 }
2365 }
2366
Michael Kolb80f75082012-04-10 10:50:06 -07002367 @Override
2368 public void toggleUserAgent() {
2369 WebView web = getCurrentWebView();
2370 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002371 }
2372
Ze G Riande2a675c22015-06-03 11:15:24 -07002373 // This function calls the method in the webview to enable/disable
2374 // the reader mode through the DOM distiller
2375 public void toggleReaderMode() {
2376 Tab t = mTabControl.getCurrentTab();
2377 if (t.isDistilled()) {
2378 closeTab(t);
2379 } else if (t.isDistillable()) {
2380 openTab(t.getDistilledUrl(), false, true, false, t);
2381 }
2382 }
2383
Michael Kolb80f75082012-04-10 10:50:06 -07002384 @Override
2385 public void findOnPage() {
2386 getCurrentTopWebView().showFindDialog(null, true);
2387 }
2388
2389 @Override
2390 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002391 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002392 }
2393
2394 @Override
2395 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002396 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002397 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2398 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002399 }
2400 else {
2401 WebView w = getCurrentTopWebView();
2402 if (w == null)
2403 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002404 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002405 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002406 }
Michael Kolb80f75082012-04-10 10:50:06 -07002407 }
2408
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002409 public boolean goLive(String url) {
2410 if (!getCurrentTab().isSnapshot())
2411 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002412 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002413
2414 if (url == null) { // "go live" button was clicked
2415 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002416 closeTab(t);
2417 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002418 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002419 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002420 return true;
John Recke1a03a32011-09-14 17:04:16 -07002421 }
2422
luxiaolb40014b2013-07-19 10:01:43 +08002423 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002424 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002425 new AlertDialog.Builder(activity)
2426 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002427 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002428 .setMessage(R.string.exit_browser_msg)
2429 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2430 public void onClick(DialogInterface dialog, int which) {
2431 activity.moveTaskToBack(true);
2432 dialog.dismiss();
2433 }
2434 })
2435 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2436 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002437 mCrashRecoveryHandler.clearState(true);
2438 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002439 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002440 dialog.dismiss();
2441 }
2442 })
2443 .show();
2444 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002445
Michael Kolb315d5022011-10-13 12:47:11 -07002446 @Override
2447 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002448 }
2449
John Reck9c35b9c2012-05-30 10:08:50 -07002450 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002451 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002452 // Let the History and Bookmark fragments handle menus they created.
2453 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2454 return false;
2455 }
2456
Michael Kolb8233fac2010-10-26 16:08:53 -07002457 int id = item.getItemId();
2458 boolean result = true;
2459 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002460 // -- Browser context menu
2461 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002462 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002463 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002464 case R.id.copy_link_context_menu_id:
2465 final WebView webView = getCurrentTopWebView();
2466 if (null == webView) {
2467 result = false;
2468 break;
2469 }
2470 final HashMap<String, WebView> hrefMap =
2471 new HashMap<String, WebView>();
2472 hrefMap.put("webview", webView);
2473 final Message msg = mHandler.obtainMessage(
2474 FOCUS_NODE_HREF, id, 0, hrefMap);
2475 webView.requestFocusNodeHref(msg);
2476 break;
2477
2478 default:
2479 // For other context menus
2480 result = onOptionsItemSelected(item);
2481 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002482 return result;
2483 }
2484
2485 /**
2486 * support programmatically opening the context menu
2487 */
2488 public void openContextMenu(View view) {
2489 mActivity.openContextMenu(view);
2490 }
2491
2492 /**
2493 * programmatically open the options menu
2494 */
2495 public void openOptionsMenu() {
2496 mActivity.openOptionsMenu();
2497 }
2498
John Reck9c35b9c2012-05-30 10:08:50 -07002499 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002500 public boolean onMenuOpened(int featureId, Menu menu) {
2501 if (mOptionsMenuOpen) {
2502 if (mConfigChanged) {
2503 // We do not need to make any changes to the state of the
2504 // title bar, since the only thing that happened was a
2505 // change in orientation
2506 mConfigChanged = false;
2507 } else {
2508 if (!mExtendedMenuOpen) {
2509 mExtendedMenuOpen = true;
2510 mUi.onExtendedMenuOpened();
2511 } else {
2512 // Switching the menu back to icon view, so show the
2513 // title bar once again.
2514 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002515 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002516 }
2517 }
2518 } else {
2519 // The options menu is closed, so open it, and show the title
2520 mOptionsMenuOpen = true;
2521 mConfigChanged = false;
2522 mExtendedMenuOpen = false;
2523 mUi.onOptionsMenuOpened();
2524 }
2525 return true;
2526 }
2527
John Reck9c35b9c2012-05-30 10:08:50 -07002528 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002529 public void onOptionsMenuClosed(Menu menu) {
2530 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002531 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 }
2533
John Reck9c35b9c2012-05-30 10:08:50 -07002534 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002535 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002536 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002537 }
2538
2539 // Helper method for getting the top window.
2540 @Override
2541 public WebView getCurrentTopWebView() {
2542 return mTabControl.getCurrentTopWebView();
2543 }
2544
2545 @Override
2546 public WebView getCurrentWebView() {
2547 return mTabControl.getCurrentWebView();
2548 }
2549
2550 /*
2551 * This method is called as a result of the user selecting the options
2552 * menu to see the download window. It shows the download window on top of
2553 * the current window.
2554 */
2555 void viewDownloads() {
2556 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2557 mActivity.startActivity(intent);
2558 }
2559
John Reck30b065e2011-07-19 10:58:05 -07002560 int getActionModeHeight() {
2561 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2562 new int[] { android.R.attr.actionBarSize });
2563 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2564 actionBarSizeTypedArray.recycle();
2565 return size;
2566 }
2567
Michael Kolb8233fac2010-10-26 16:08:53 -07002568 // action mode
2569
John Reck9c35b9c2012-05-30 10:08:50 -07002570 @Override
2571 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002572 mUi.onActionModeStarted(mode);
2573 mActionMode = mode;
2574 }
2575
2576 /*
2577 * True if a custom ActionMode (i.e. find or select) is in use.
2578 */
2579 @Override
2580 public boolean isInCustomActionMode() {
2581 return mActionMode != null;
2582 }
2583
2584 /*
2585 * End the current ActionMode.
2586 */
2587 @Override
2588 public void endActionMode() {
2589 if (mActionMode != null) {
2590 mActionMode.finish();
2591 }
2592 }
2593
2594 /*
2595 * Called by find and select when they are finished. Replace title bars
2596 * as necessary.
2597 */
John Reck9c35b9c2012-05-30 10:08:50 -07002598 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 public void onActionModeFinished(ActionMode mode) {
2600 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002601 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 mActionMode = null;
2603 }
2604
2605 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002606 final Tab tab = getCurrentTab();
2607 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 }
2609
2610 // bookmark handling
2611
2612 /**
2613 * add the current page as a bookmark to the given folder id
2614 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002615 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002616 * bookmarked, and if it is, edit that bookmark. If false, and
2617 * the site is already bookmarked, do not attempt to edit the
2618 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 */
2620 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002621 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002622 WebView w = getCurrentTopWebView();
2623 if (w == null) {
2624 return null;
2625 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002626 Intent i = new Intent(mActivity,
2627 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002628 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2629 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002630 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002631 if (touchIconUrl != null) {
2632 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2633 WebSettings settings = w.getSettings();
2634 if (settings != null) {
2635 i.putExtra(AddBookmarkPage.USER_AGENT,
2636 settings.getUserAgentString());
2637 }
2638 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002639 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002640 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002641 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002642 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2643 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002644 // Put the dialog at the upper right of the screen, covering the
2645 // star on the title bar.
2646 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002647 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002648 }
2649
2650 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002651 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002652 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002653 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002654 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002655 }
2656
Vivek Sekharb54614f2014-05-01 19:03:37 -07002657 @Override
2658 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2659 boolean capture) {
2660 mUploadHandler = new UploadHandler(this);
2661 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2662 }
2663
Michael Kolb8233fac2010-10-26 16:08:53 -07002664 // thumbnails
2665
2666 /**
2667 * Return the desired width for thumbnail screenshots, which are stored in
2668 * the database, and used on the bookmarks screen.
2669 * @param context Context for finding out the density of the screen.
2670 * @return desired width for thumbnail screenshot.
2671 */
2672 static int getDesiredThumbnailWidth(Context context) {
2673 return context.getResources().getDimensionPixelOffset(
2674 R.dimen.bookmarkThumbnailWidth);
2675 }
2676
2677 /**
2678 * Return the desired height for thumbnail screenshots, which are stored in
2679 * the database, and used on the bookmarks screen.
2680 * @param context Context for finding out the density of the screen.
2681 * @return desired height for thumbnail screenshot.
2682 */
2683 static int getDesiredThumbnailHeight(Context context) {
2684 return context.getResources().getDimensionPixelOffset(
2685 R.dimen.bookmarkThumbnailHeight);
2686 }
2687
Pankaj Gargf49e0222015-09-01 12:19:13 -07002688 void createScreenshotAsync(Tab tab, final ValueCallback<Bitmap> cb) {
2689 if (tab == null) {
2690 cb.onReceiveValue(null);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002691 return;
2692 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002693
2694 tab.capture();
2695
2696 synchronized (mThumbnailCbList) {
2697 mThumbnailCbList.add(cb);
2698 }
2699 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002700
Michael Kolb8233fac2010-10-26 16:08:53 -07002701 private class Copy implements OnMenuItemClickListener {
2702 private CharSequence mText;
2703
John Reck9c35b9c2012-05-30 10:08:50 -07002704 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002705 public boolean onMenuItemClick(MenuItem item) {
2706 copy(mText);
2707 return true;
2708 }
2709
2710 public Copy(CharSequence toCopy) {
2711 mText = toCopy;
2712 }
2713 }
2714
Leon Scroggins63c02662010-11-18 15:16:27 -05002715 private static class Download implements OnMenuItemClickListener {
2716 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002717 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002718 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002719 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002720 private static final String FALLBACK_EXTENSION = "dat";
2721 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002722
John Reck9c35b9c2012-05-30 10:08:50 -07002723 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002724 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002725 if (DataUri.isDataUri(mText)) {
2726 saveDataUri();
2727 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002728 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08002729 null, null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002730 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002731 return true;
2732 }
2733
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002734 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2735 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002736 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002738 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002739 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002740 }
George Mount387d45d2011-10-07 15:57:53 -07002741
2742 /**
2743 * Treats mText as a data URI and writes its contents to a file
2744 * based on the current time.
2745 */
2746 private void saveDataUri() {
2747 FileOutputStream outputStream = null;
2748 try {
2749 DataUri uri = new DataUri(mText);
2750 File target = getTarget(uri);
2751 outputStream = new FileOutputStream(target);
2752 outputStream.write(uri.getData());
2753 final DownloadManager manager =
2754 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2755 manager.addCompletedDownload(target.getName(),
2756 mActivity.getTitle().toString(), false,
2757 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002758 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002759 } catch (IOException e) {
2760 Log.e(LOGTAG, "Could not save data URL");
2761 } finally {
2762 if (outputStream != null) {
2763 try {
2764 outputStream.close();
2765 } catch (IOException e) {
2766 // ignore close errors
2767 }
2768 }
2769 }
2770 }
2771
2772 /**
2773 * Creates a File based on the current time stamp and uses
2774 * the mime type of the DataUri to get the extension.
2775 */
2776 private File getTarget(DataUri uri) throws IOException {
2777 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002778 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002779 String nameBase = format.format(new Date());
2780 String mimeType = uri.getMimeType();
2781 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2782 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2783 if (extension == null) {
2784 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2785 extension = FALLBACK_EXTENSION;
2786 }
2787 extension = "." + extension; // createTempFile needs the '.'
2788 File targetFile = File.createTempFile(nameBase, extension, dir);
2789 return targetFile;
2790 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002791 }
2792
Cary Clark8974d282010-11-22 10:46:05 -05002793 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002794 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002795
John Reck9c35b9c2012-05-30 10:08:50 -07002796 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002797 public boolean onMenuItemClick(MenuItem item) {
2798 if (mWebView != null) {
2799 return mWebView.selectText();
2800 }
2801 return false;
2802 }
2803
2804 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002805 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002806 }
2807
2808 }
2809
Michael Kolb8233fac2010-10-26 16:08:53 -07002810 /********************** TODO: UI stuff *****************************/
2811
2812 // these methods have been copied, they still need to be cleaned up
2813
2814 /****************** tabs ***************************************************/
2815
2816 // basic tab interactions:
2817
2818 // it is assumed that tabcontrol already knows about the tab
2819 protected void addTab(Tab tab) {
2820 mUi.addTab(tab);
2821 }
2822
2823 protected void removeTab(Tab tab) {
2824 mUi.removeTab(tab);
2825 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002826 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002827 }
2828
Michael Kolbf2055602011-04-09 17:20:03 -07002829 @Override
2830 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002831 // monkey protection against delayed start
2832 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002833
2834 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2835 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002836 mTabControl.setCurrentTab(tab);
2837 // the tab is guaranteed to have a webview after setCurrentTab
2838 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002839
2840
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002841 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002842 //Purge active tabs
2843 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002844 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002845 }
2846
John Reck8bcafc12011-08-29 16:43:02 -07002847 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002848 Tab current = mTabControl.getCurrentTab();
2849 if (current != null
2850 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002851 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002852 }
2853 }
2854
John Reck26b18322011-06-21 13:08:58 -07002855 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002856 //Cancel navscreen request
2857 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002858 // Dismiss the subwindow if applicable.
2859 dismissSubWindow(appTab);
2860 // Since we might kill the WebView, remove it from the
2861 // content view first.
2862 mUi.detachTab(appTab);
2863 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002864 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002865 // TODO: analyze why the remove and add are necessary
2866 mUi.attachTab(appTab);
2867 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002868 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002869 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002870 } else {
2871 // If the tab was the current tab, we have to attach
2872 // it to the view system again.
2873 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002874 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002875 }
2876 }
2877
2878 // Remove the sub window if it exists. Also called by TabControl when the
2879 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002880 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002881 public void dismissSubWindow(Tab tab) {
2882 removeSubWindow(tab);
2883 // dismiss the subwindow. This will destroy the WebView.
2884 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002885 WebView wv = getCurrentTopWebView();
2886 if (wv != null) {
2887 wv.requestFocus();
2888 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002889 }
2890
2891 @Override
2892 public void removeSubWindow(Tab t) {
2893 if (t.getSubWebView() != null) {
2894 mUi.removeSubWindow(t.getSubViewContainer());
2895 }
2896 }
2897
2898 @Override
2899 public void attachSubWindow(Tab tab) {
2900 if (tab.getSubWebView() != null) {
2901 mUi.attachSubWindow(tab.getSubViewContainer());
2902 getCurrentTopWebView().requestFocus();
2903 }
2904 }
2905
Mathew Inwood29721c22011-06-29 17:55:24 +01002906 private Tab showPreloadedTab(final UrlData urlData) {
2907 if (!urlData.isPreloaded()) {
2908 return null;
2909 }
2910 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2911 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2912 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002913 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002914 // Could not submit query. Fallback to regular tab creation
2915 tabControl.destroy();
2916 return null;
2917 }
2918 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002919 // check tab count and make room for new tab
2920 if (!mTabControl.canCreateNewTab()) {
2921 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2922 if (leastUsed != null) {
2923 closeTab(leastUsed);
2924 }
2925 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002926 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002927 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002928 mTabControl.addPreloadedTab(t);
2929 addTab(t);
2930 setActiveTab(t);
2931 return t;
2932 }
2933
Michael Kolb7bcafde2011-05-09 13:55:59 -07002934 // open a non inconito tab with the given url data
2935 // and set as active tab
2936 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002937 Tab tab = showPreloadedTab(urlData);
2938 if (tab == null) {
2939 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002940 if ((tab != null) && !urlData.isEmpty()) {
2941 loadUrlDataIn(tab, urlData);
2942 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002943 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002944 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002945 }
2946
Michael Kolb843510f2010-12-09 10:51:49 -08002947 @Override
2948 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002949 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002950 }
2951
Michael Kolb8233fac2010-10-26 16:08:53 -07002952 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002953 public Tab openIncognitoTab() {
2954 return openTab(INCOGNITO_URI, true, true, false);
2955 }
2956
2957 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002958 public Tab openTab(String url, boolean incognito, boolean setActive,
2959 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002960 return openTab(url, incognito, setActive, useCurrent, null);
2961 }
2962
2963 @Override
2964 public Tab openTab(String url, Tab parent, boolean setActive,
2965 boolean useCurrent) {
2966 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2967 setActive, useCurrent, parent);
2968 }
2969
2970 public Tab openTab(String url, boolean incognito, boolean setActive,
2971 boolean useCurrent, Tab parent) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07002972 boolean change_tabs = false;
2973 if (setActive) {
2974 Tab currentTab = mTabControl.getCurrentTab();
2975 if (currentTab != null) {
2976 change_tabs = setActive;
2977 setActive = false;
Sagar Dhawan9a1d8e72015-09-11 17:14:38 -07002978 if (mUi instanceof PhoneUi)
2979 currentTab.capture();
Pankaj Gargf49e0222015-09-01 12:19:13 -07002980 }
2981 }
2982
Michael Kolb7bcafde2011-05-09 13:55:59 -07002983 Tab tab = createNewTab(incognito, setActive, useCurrent);
2984 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002985 if (parent instanceof SnapshotTab) {
2986 addTab(tab);
2987 if (setActive)
2988 setActiveTab(tab);
2989 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002990 parent.addChildTab(tab);
2991 }
Michael Kolb519d2282011-05-09 17:03:19 -07002992 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002993 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002994 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002995 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07002996
2997 if (change_tabs) {
Sagar Dhawan20b1deb2015-10-12 11:13:18 -07002998 setActiveTab(tab);
Pankaj Gargf49e0222015-09-01 12:19:13 -07002999 synchronized (mThumbnailCbList) {
3000 startCaptureTimer();
3001 mLatestCreatedTab = tab;
3002 mThumbnailCbList.add(new ValueCallback<Bitmap>() {
3003 @Override
3004 public void onReceiveValue(Bitmap bitmap) {
3005 synchronized (mThumbnailCbList) {
3006 if (mLatestCreatedTab != null) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07003007 mLatestCreatedTab = null;
3008 }
3009 stopCaptureTimer();
3010 }
3011 }
3012 });
3013 }
3014 }
3015
Michael Kolb7bcafde2011-05-09 13:55:59 -07003016 return tab;
3017 }
3018
Pankaj Gargf49e0222015-09-01 12:19:13 -07003019 private void startCaptureTimer() {
3020 mCaptureTimer = new CountDownTimer(mCaptureMaxWaitMS, mCaptureMaxWaitMS) {
3021 @Override
3022 public void onTick(long millisUntilFinished) {
3023 // Do nothing
3024 }
3025
3026 @Override
3027 public void onFinish() {
3028 synchronized (mThumbnailCbList) {
3029 Log.e(LOGTAG, "Screen capture timed out while opening new tab");
3030 if (mLatestCreatedTab != null) {
3031 setActiveTab(mLatestCreatedTab);
3032 mLatestCreatedTab = null;
3033 }
3034 }
3035 }
3036 }.start();
3037 }
3038
3039 private void stopCaptureTimer() {
3040 if (mCaptureTimer != null) {
3041 mCaptureTimer.cancel();
3042 mCaptureTimer = null;
3043 }
3044 }
3045
3046
Michael Kolb7bcafde2011-05-09 13:55:59 -07003047 // this method will attempt to create a new tab
3048 // incognito: private browsing tab
3049 // setActive: ste tab as current tab
3050 // useCurrent: if no new tab can be created, return current tab
3051 private Tab createNewTab(boolean incognito, boolean setActive,
3052 boolean useCurrent) {
3053 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07003054 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
3055 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003056 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003057 if (mTabControl.canCreateNewTab()) {
3058 tab = mTabControl.createNewTab(incognito, !setActive);
3059 addTab(tab);
3060 if (setActive) {
3061 setActiveTab(tab);
3062 } else {
3063 tab.pause();
3064 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003065 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003066 if (useCurrent) {
3067 tab = mTabControl.getCurrentTab();
3068 reuseTab(tab, null);
3069 } else {
3070 mUi.showMaxTabsWarning();
3071 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003072 }
3073 }
3074 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 }
3076
John Reck2bc80422011-06-30 15:11:49 -07003077 @Override
3078 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3079 SnapshotTab tab = null;
3080 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07003081 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07003082 addTab(tab);
3083 if (setActive) {
3084 setActiveTab(tab);
3085 }
3086 } else {
3087 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003088 }
3089 return tab;
3090 }
3091
Michael Kolb8233fac2010-10-26 16:08:53 -07003092 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003093 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003094 * @return boolean True if we successfully switched to a different tab. If
3095 * the indexth tab is null, or if that tab is the same as
3096 * the current one, return false.
3097 */
3098 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003099 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003100 Tab currentTab = mTabControl.getCurrentTab();
3101 if (tab == null || tab == currentTab) {
3102 return false;
3103 }
3104 setActiveTab(tab);
3105 return true;
3106 }
3107
3108 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003109 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003110 closeCurrentTab(false);
3111 }
3112
3113 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003114 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003115 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003116 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003117 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003118 return;
3119 }
Michael Kolbc831b632011-05-11 09:30:34 -07003120 final Tab current = mTabControl.getCurrentTab();
3121 final int pos = mTabControl.getCurrentPosition();
3122 Tab newTab = current.getParent();
3123 if (newTab == null) {
3124 newTab = mTabControl.getTab(pos + 1);
3125 if (newTab == null) {
3126 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003127 }
3128 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003129 if (andQuit) {
3130 mTabControl.setCurrentTab(newTab);
3131 closeTab(current);
3132 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003133 // Close window
3134 closeTab(current);
3135 }
3136 }
3137
3138 /**
3139 * Close the tab, remove its associated title bar, and adjust mTabControl's
3140 * current tab to a valid value.
3141 */
3142 @Override
3143 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003144 if (tab == mTabControl.getCurrentTab()) {
3145 closeCurrentTab();
3146 } else {
3147 removeTab(tab);
3148 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003149 }
3150
Afzal Najamd4e33312012-04-26 01:54:01 -04003151 /**
3152 * Close all tabs except the current one
3153 */
3154 @Override
3155 public void closeOtherTabs() {
3156 int inactiveTabs = mTabControl.getTabCount() - 1;
3157 for (int i = inactiveTabs; i >= 0; i--) {
3158 Tab tab = mTabControl.getTab(i);
3159 if (tab != mTabControl.getCurrentTab()) {
3160 removeTab(tab);
3161 }
3162 }
3163 }
3164
Michael Kolb8233fac2010-10-26 16:08:53 -07003165 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003166 protected void loadUrlFromContext(String url) {
3167 Tab tab = getCurrentTab();
3168 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003169 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003170 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003171 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003172 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003173 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003174 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003175 }
3176 }
3177 }
3178
3179 /**
3180 * Load the URL into the given WebView and update the title bar
3181 * to reflect the new load. Call this instead of WebView.loadUrl
3182 * directly.
3183 * @param view The WebView used to load url.
3184 * @param url The URL to load.
3185 */
John Reck71e51422011-07-01 16:49:28 -07003186 @Override
3187 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003188 loadUrl(tab, url, null);
3189 }
3190
3191 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3192 if (tab != null) {
3193 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003194 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003195 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003196 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003197 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003198 }
3199
3200 /**
3201 * Load UrlData into a Tab and update the title bar to reflect the new
3202 * load. Call this instead of UrlData.loadIn directly.
3203 * @param t The Tab used to load.
3204 * @param data The UrlData being loaded.
3205 */
3206 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003207 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003208 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003209 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003210 } else {
John Reck38b39652012-06-05 09:22:59 -07003211 if (t != null && data.mDisableUrlOverride) {
3212 t.disableUrlOverridingForLoad();
3213 }
John Reck26b18322011-06-21 13:08:58 -07003214 loadUrl(t, data.mUrl, data.mHeaders);
3215 }
3216 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003217 }
3218
John Reck30c714c2010-12-16 17:30:34 -08003219 @Override
3220 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003221 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003222 //In case of tab can go back (aka tab has navigation entry) do nothing
3223 //else just load homepage in current tab.
3224 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003225 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003226 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003227 }
3228
3229 void goBackOnePageOrQuit() {
3230 Tab current = mTabControl.getCurrentTab();
3231 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003232 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3233 showExitDialog(mActivity);
3234 } else {
3235 /*
3236 * Instead of finishing the activity, simply push this to the back
3237 * of the stack and let ActivityManager to choose the foreground
3238 * activity. As BrowserActivity is singleTask, it will be always the
3239 * root of the task. So we can use either true or false for
3240 * moveTaskToBack().
3241 */
3242 mActivity.moveTaskToBack(true);
3243 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003244 return;
3245 }
John Reckef654f12011-07-12 16:42:08 -07003246 if (current.canGoBack()) {
3247 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003248 } else {
3249 // Check to see if we are closing a window that was created by
3250 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003251 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003252 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003253 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003254 // Now we close the other tab
3255 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003256 } else if (BrowserConfig.getInstance(getContext())
3257 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3258 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003259 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003260 /*
3261 * Instead of finishing the activity, simply push this to the back
3262 * of the stack and let ActivityManager to choose the foreground
3263 * activity. As BrowserActivity is singleTask, it will be always the
3264 * root of the task. So we can use either true or false for
3265 * moveTaskToBack().
3266 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003267 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003268 }
3269 }
3270 }
3271
3272 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003273 * helper method for key handler
3274 * returns the current tab if it can't advance
3275 */
Michael Kolbc831b632011-05-11 09:30:34 -07003276 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003277 int pos = mTabControl.getCurrentPosition() + 1;
3278 if (pos >= mTabControl.getTabCount()) {
3279 pos = 0;
3280 }
3281 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003282 }
3283
3284 /**
3285 * helper method for key handler
3286 * returns the current tab if it can't advance
3287 */
Michael Kolbc831b632011-05-11 09:30:34 -07003288 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003289 int pos = mTabControl.getCurrentPosition() - 1;
3290 if ( pos < 0) {
3291 pos = mTabControl.getTabCount() - 1;
3292 }
3293 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003294 }
3295
John Reckbcef87f2012-02-03 14:58:34 -08003296 boolean isMenuOrCtrlKey(int keyCode) {
3297 return (KeyEvent.KEYCODE_MENU == keyCode)
3298 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3299 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3300 }
3301
Michael Kolb0035fad2011-03-14 13:25:28 -07003302 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003303 * handle key events in browser
3304 *
3305 * @param keyCode
3306 * @param event
3307 * @return true if handled, false to pass to super
3308 */
John Reck9c35b9c2012-05-30 10:08:50 -07003309 @Override
3310 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003311 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3312 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003313 if (!mUi.isComboViewShowing()) {
3314 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3315 true, false);
3316 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003317 return true;
3318 }
3319
Cary Clark160bbb92011-01-10 11:17:07 -05003320 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003321 // Even if MENU is already held down, we need to call to super to open
3322 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003323 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003324 mMenuIsDown = true;
3325 return false;
3326 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003327
Cary Clark8ff8c662010-12-29 15:03:05 -05003328 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003329 Tab tab = getCurrentTab();
3330 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003331
Cary Clark160bbb92011-01-10 11:17:07 -05003332 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3333 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003334
Michael Kolb8233fac2010-10-26 16:08:53 -07003335 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003336 case KeyEvent.KEYCODE_TAB:
3337 if (event.isCtrlPressed()) {
3338 if (event.isShiftPressed()) {
3339 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003340 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003341 } else {
3342 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003343 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003344 }
3345 return true;
3346 }
3347 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003348 case KeyEvent.KEYCODE_SPACE:
3349 // WebView/WebTextView handle the keys in the KeyDown. As
3350 // the Activity's shortcut keys are only handled when WebView
3351 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003352 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003353 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003354 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003355 pageDown();
3356 }
3357 return true;
3358 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003359 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003360 event.startTracking();
3361 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003362 case KeyEvent.KEYCODE_FORWARD:
3363 if (!noModifiers) break;
3364 tab.goForward();
3365 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003366 case KeyEvent.KEYCODE_DPAD_LEFT:
3367 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003368 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003369 return true;
3370 }
3371 break;
3372 case KeyEvent.KEYCODE_DPAD_RIGHT:
3373 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003374 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003375 return true;
3376 }
3377 break;
3378 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003379 if (ctrl) {
3380 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003381 return true;
3382 }
3383 break;
Michael Kolba4183062011-01-16 10:43:21 -08003384// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003385 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003386 if (ctrl ) {
3387 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003388 return true;
3389 }
3390 break;
Michael Kolba4183062011-01-16 10:43:21 -08003391// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003392// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003393// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003394// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003395// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003396// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003397// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003398// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003399// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003400// case KeyEvent.KEYCODE_M: // unused
3401// case KeyEvent.KEYCODE_N: // in Chrome: new window
3402// case KeyEvent.KEYCODE_O: // in Chrome: open file
3403// case KeyEvent.KEYCODE_P: // in Chrome: print page
3404// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003405// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003406// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3407 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003408 // we can't use the ctrl/shift flags, they check for
3409 // exclusive use of a modifier
3410 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003411 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003412 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003413 } else {
3414 openTabToHomePage();
3415 }
3416 return true;
3417 }
3418 break;
3419// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3420// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003421// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003422// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3423// case KeyEvent.KEYCODE_Y: // unused
3424// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003425 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003426 // it is a regular key and webview is not null
3427 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003428 }
3429
John Reck9c35b9c2012-05-30 10:08:50 -07003430 @Override
3431 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003432 switch(keyCode) {
3433 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003434 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003435 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003436 return true;
3437 }
3438 break;
3439 }
3440 return false;
3441 }
3442
John Reck9c35b9c2012-05-30 10:08:50 -07003443 @Override
3444 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003445 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003446 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003447 if (KeyEvent.KEYCODE_MENU == keyCode
3448 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003449 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003450 }
3451 }
Cary Clark160bbb92011-01-10 11:17:07 -05003452 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003453 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003454 case KeyEvent.KEYCODE_BACK:
3455 if (event.isTracking() && !event.isCanceled()) {
3456 onBackKey();
3457 return true;
3458 }
3459 break;
3460 }
3461 return false;
3462 }
3463
3464 public boolean isMenuDown() {
3465 return mMenuIsDown;
3466 }
3467
John Reck9c35b9c2012-05-30 10:08:50 -07003468 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003469 public void setupAutoFill(Message message) {
3470 // Open the settings activity at the AutoFill profile fragment so that
3471 // the user can create a new profile. When they return, we will dispatch
3472 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003473 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003474 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3475 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003476 }
John Reckb3417f02011-01-14 11:01:05 -08003477
John Reck9c35b9c2012-05-30 10:08:50 -07003478 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003479 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003480 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003481 return true;
3482 }
3483
John Reck1cf4b792011-07-26 10:22:22 -07003484 @Override
3485 public boolean shouldCaptureThumbnails() {
3486 return mUi.shouldCaptureThumbnails();
3487 }
3488
Michael Kolbc3af0672011-08-09 10:24:41 -07003489 @Override
Pankaj Gargf49e0222015-09-01 12:19:13 -07003490 public void onThumbnailCapture(Bitmap bm) {
3491 synchronized (mThumbnailCbList) {
3492 int num_entries = mThumbnailCbList.size();
3493 while (num_entries > 0){
3494 num_entries--;
3495 ValueCallback<Bitmap> cb = mThumbnailCbList.get(num_entries);
3496 cb.onReceiveValue(bm);
3497 }
3498 mThumbnailCbList.clear();
3499 }
3500 }
3501
3502 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003503 public boolean supportsVoice() {
3504 PackageManager pm = mActivity.getPackageManager();
3505 List activities = pm.queryIntentActivities(new Intent(
3506 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3507 return activities.size() != 0;
3508 }
3509
3510 @Override
3511 public void startVoiceRecognizer() {
3512 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003513 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003514 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3515 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3516 mActivity.startActivityForResult(voice, VOICE_RESULT);
3517 }
3518
Pankaj Garg7b279f62014-08-12 14:47:18 -07003519 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003520 if (mLevel == level)
3521 return;
3522 mLevel = level;
3523 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003524 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3525 lp.dimAmount = level;
3526 mActivity.getWindow().setAttributes(lp);
3527 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3528 } else {
3529 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3530 }
3531 }
3532
Michael Kolb0b129122012-06-04 16:31:58 -07003533 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003534 public void setBlockEvents(boolean block) {
3535 mBlockEvents = block;
3536 }
3537
John Reck9c35b9c2012-05-30 10:08:50 -07003538 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003539 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003540 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003541 }
3542
John Reck9c35b9c2012-05-30 10:08:50 -07003543 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003544 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003545 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003546 }
3547
John Reck9c35b9c2012-05-30 10:08:50 -07003548 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003549 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003550 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003551 }
3552
John Reck9c35b9c2012-05-30 10:08:50 -07003553 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003554 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003555 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003556 }
3557
John Reck9c35b9c2012-05-30 10:08:50 -07003558 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003559 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003560 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003561 }
3562
Pankaj Garg49b79252014-11-07 17:33:41 -08003563 @Override
3564 public boolean shouldShowAppMenu() {
3565 return true;
3566 }
3567
3568 @Override
3569 public int getMenuThemeResourceId() {
3570 return R.style.OverflowMenuTheme;
3571 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003572}