blob: 12b8b2978cee3f9779c826f5519bd61d6c6c4426 [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;
Mattias Nilsson561d1952011-10-04 10:18:50 +020043import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070044import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080045import android.graphics.BitmapFactory;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080046import android.graphics.Rect;
Tarun Nainanif5563b62015-01-21 18:52:59 -080047import android.media.AudioManager;
kaiyiza016da12013-08-26 17:50:22 +080048import android.net.ConnectivityManager;
49import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.net.Uri;
51import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080052import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080053import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.os.AsyncTask;
Tarun Nainanif5563b62015-01-21 18:52:59 -080055import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070057import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.os.Handler;
59import android.os.Message;
60import android.os.PowerManager;
61import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.provider.ContactsContract;
63import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080064import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070065import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070066import android.text.TextUtils;
67import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080068import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070069import android.view.ActionMode;
70import android.view.ContextMenu;
71import android.view.ContextMenu.ContextMenuInfo;
72import android.view.Gravity;
73import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070074import android.view.Menu;
75import android.view.MenuInflater;
76import android.view.MenuItem;
77import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070078import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070080import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070081import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070082import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080083import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080084import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050085import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070086
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.CookieManager;
88import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080089import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080090import org.codeaurora.swe.HttpAuthHandler;
91import org.codeaurora.swe.SslErrorHandler;
92import org.codeaurora.swe.WebSettings;
93import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070094import org.codeaurora.swe.WebBackForwardList;
95import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080096
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070097import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070098import com.android.browser.R;
99import com.android.browser.IntentHandler.UrlData;
100import com.android.browser.UI.ComboViews;
Dave Tharp851e8d52015-04-26 14:58:18 -0700101import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700102import com.android.browser.mynavigation.AddMyNavigationPage;
103import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700104import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700105import com.android.browser.platformsupport.BrowserContract;
106import com.android.browser.platformsupport.WebAddress;
107import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800108import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700109import com.android.browser.provider.BrowserProvider2.Thumbnails;
110import com.android.browser.provider.SnapshotProvider.Snapshots;
111import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800112import com.android.browser.appmenu.AppMenuHandler;
113import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700114
Michael Kolb8233fac2010-10-26 16:08:53 -0700115import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700116import java.io.File;
117import java.io.FileOutputStream;
118import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700119import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700120import java.text.DateFormat;
121import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700122import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700123import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700124import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800125import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200126import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700127import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700128
129/**
130 * Controller for browser
131 */
132public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800133 implements WebViewController, UiController, ActivityController,
134 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700135
136 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800137 private static final String SEND_APP_ID_EXTRA =
138 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Vivek Sekhared791da2015-02-22 12:39:05 -0800139 private static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800140 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
141 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800142
Vivek Sekharb54614f2014-05-01 19:03:37 -0700143 // Remind switch to data connection if wifi is unavailable
144 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800145
Michael Kolb8233fac2010-10-26 16:08:53 -0700146 // public message ids
147 public final static int LOAD_URL = 1001;
148 public final static int STOP_LOAD = 1002;
149
150 // Message Ids
151 private static final int FOCUS_NODE_HREF = 102;
152 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800153 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700154
155 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
156
157 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800158 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700159
160 private static final int EMPTY_MENU = -1;
161
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700163 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 final static int PREFERENCES_PAGE = 3;
165 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000166 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700167 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800168 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000169
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
171
172 // As the ids are dynamically created, we can't guarantee that they will
173 // be in sequence, so this static array maps ids to a window number.
174 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
175 { R.id.window_one_menu_id, R.id.window_two_menu_id,
176 R.id.window_three_menu_id, R.id.window_four_menu_id,
177 R.id.window_five_menu_id, R.id.window_six_menu_id,
178 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
179
180 // "source" parameter for Google search through search key
181 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
182 // "source" parameter for Google search through simplily type
183 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
184
George Mount387d45d2011-10-07 15:57:53 -0700185 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700186 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
187
John Reckd7dd9b22011-08-30 09:18:29 -0700188 // A bitmap that is re-used in createScreenshot as scratch space
189 private static Bitmap sThumbnailBitmap;
190
Michael Kolb8233fac2010-10-26 16:08:53 -0700191 private Activity mActivity;
192 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700193 private HomepageHandler mHomepageHandler;
194 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 private BrowserSettings mSettings;
196 private WebViewFactory mFactory;
197
198 private WakeLock mWakeLock;
199
200 private UrlHandler mUrlHandler;
201 private UploadHandler mUploadHandler;
202 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 private PageDialogsHandler mPageDialogsHandler;
204 private NetworkStateHandler mNetworkHandler;
205
Ben Murdoch8029a772010-11-16 11:58:21 +0000206 private Message mAutoFillSetupMessage;
207
kaiyiza016da12013-08-26 17:50:22 +0800208 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700209
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 // FIXME, temp address onPrepareMenu performance problem.
211 // When we move everything out of view, we should rewrite this.
212 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700213 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 private int mOldMenuState = EMPTY_MENU;
215 private Menu mCachedMenu;
216
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 private boolean mMenuIsDown;
218
Pankaj Garg49b79252014-11-07 17:33:41 -0800219 private boolean mWasInPageLoad = false;
220 private AppMenuHandler mAppMenuHandler;
221
Michael Kolb8233fac2010-10-26 16:08:53 -0700222 // For select and find, we keep track of the ActionMode so that
223 // finish() can be called as desired.
224 private ActionMode mActionMode;
225
226 /**
227 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
228 * of whether the configuration has changed. The first onMenuOpened call
229 * after a configuration change is simply a reopening of the same menu
230 * (i.e. mIconView did not change).
231 */
232 private boolean mConfigChanged;
233
234 /**
235 * Keeps track of whether the options menu is open. This is important in
236 * determining whether to show or hide the title bar overlay
237 */
238 private boolean mOptionsMenuOpen;
239
240 /**
241 * Whether or not the options menu is in its bigger, popup menu form. When
242 * true, we want the title bar overlay to be gone. When false, we do not.
243 * Only meaningful if mOptionsMenuOpen is true.
244 */
245 private boolean mExtendedMenuOpen;
246
Michael Kolb8233fac2010-10-26 16:08:53 -0700247 private boolean mActivityPaused = true;
248 private boolean mLoadStopped;
249
250 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500251 // Checks to see when the bookmarks database has changed, and updates the
252 // Tabs' notion of whether they represent bookmarked sites.
253 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700254 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700255
Michael Kolbc3af0672011-08-09 10:24:41 -0700256 private boolean mBlockEvents;
257
Michael Kolb0b129122012-06-04 16:31:58 -0700258 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800259 private boolean mUpdateMyNavThumbnail;
260 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800261 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800262 private WebView.HitTestResult mResult;
Pankaj Gargeba076f2015-03-25 13:40:59 -0700263 private static Bitmap mBookmarkBitmap;
Site Maoabb7bd32015-03-20 15:34:02 -0700264 private PowerConnectionReceiver mLowPowerReceiver;
265 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700266
George Mount3636d0a2011-11-21 09:08:21 -0800267 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 mActivity = browser;
269 mSettings = BrowserSettings.getInstance();
270 mTabControl = new TabControl(this);
271 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700272 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800273 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700274 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700275
276 mUrlHandler = new UrlHandler(this);
277 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
279
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500281 mBookmarksObserver = new ContentObserver(mHandler) {
282 @Override
283 public void onChange(boolean selfChange) {
284 int size = mTabControl.getTabCount();
285 for (int i = 0; i < size; i++) {
286 mTabControl.getTab(i).updateBookmarkedStatus();
287 }
288 }
289
290 };
291 browser.getContentResolver().registerContentObserver(
292 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700293
294 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700295 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800296 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700297 }
298
John Reck9c35b9c2012-05-30 10:08:50 -0700299 @Override
300 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700301 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800302 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800303 // mCrashRecoverHandler has any previously saved state.
304 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700305 }
306
George Mount3636d0a2011-11-21 09:08:21 -0800307 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800308 // we dont want to ever recover incognito tabs
309 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700310
Patrick Scott7d50a932011-02-04 09:27:26 -0500311 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700312 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500313 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000314
Michael Kolbc831b632011-05-11 09:30:34 -0700315 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500316 // Not able to restore so we go ahead and clear session cookies. We
317 // must do this before trying to login the user as we don't want to
318 // clear any session cookies set during login.
319 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700320 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800321 if (intent == null) {
322 // This won't happen under common scenarios. The icicle is
323 // not null, but there aren't any tabs to restore.
324 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700325 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800326 final Bundle extra = intent.getExtras();
327 // Create an initial tab.
328 // If the intent is ACTION_VIEW and data is not null, the Browser is
329 // invoked to view the content by another application. In this case,
330 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800331 UrlData urlData = null;
332 if (intent.getData() != null
333 && Intent.ACTION_VIEW.equals(intent.getAction())
334 && intent.getData().toString().startsWith("content://")) {
335 urlData = new UrlData(intent.getData().toString());
336 } else {
337 urlData = IntentHandler.getUrlDataFromIntent(intent);
338 }
George Mount3636d0a2011-11-21 09:08:21 -0800339 Tab t = null;
340 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700341 String landingPage = mActivity.getResources().getString(
342 R.string.def_landing_page);
343 if (!landingPage.isEmpty()) {
344 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800345 } else {
346 t = openTabToHomePage();
347 }
George Mount3636d0a2011-11-21 09:08:21 -0800348 } else {
349 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700350 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800351 }
352 if (t != null) {
353 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
354 }
355 WebView webView = t.getWebView();
356 if (extra != null) {
357 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
358 if (scale > 0 && scale <= 1000) {
359 webView.setInitialScale(scale);
360 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 }
362 }
John Reckd8c74522011-06-14 08:45:00 -0700363 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700364 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700365 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500366 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700367 List<Tab> tabs = mTabControl.getTabs();
368 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700369
John Reck1cf4b792011-07-26 10:22:22 -0700370 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700371 //handle restored pages that may require a JS interface
372 if (t.getWebView() != null) {
373 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
374 if (backForwardList != null) {
375 for (int i = 0; i < backForwardList.getSize(); i++) {
376 WebHistoryItem item = backForwardList.getItemAtIndex(i);
377 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
378 }
379 }
380 }
John Reck1cf4b792011-07-26 10:22:22 -0700381 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700382 if (t != mTabControl.getCurrentTab()) {
383 t.pause();
384 }
John Reck1cf4b792011-07-26 10:22:22 -0700385 }
386 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700387 if (tabs.size() == 0) {
388 openTabToHomePage();
389 }
John Reck1cf4b792011-07-26 10:22:22 -0700390 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700391 // TabControl.restoreState() will create a new tab even if
392 // restoring the state fails.
393 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800394 // Intent is non-null when framework thinks the browser should be
395 // launching with a new intent (icicle is null).
396 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700397 mIntentHandler.onNewIntent(intent);
398 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700399 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700400 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700401 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800402 if (jsFlags.trim().length() != 0) {
403 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700404 }
George Mount3636d0a2011-11-21 09:08:21 -0800405 if (intent != null
406 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700407 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800408 }
Site Maoabb7bd32015-03-20 15:34:02 -0700409 mLowPowerReceiver = new PowerConnectionReceiver();
410 mPowerChangeReceiver = new PowerConnectionReceiver();
411
412 //always track the android framework's power save mode
413 IntentFilter filter = new IntentFilter();
414 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
415 // Power save mode only exists in Lollipop and above
416 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
417 }
418 filter.addAction(Intent.ACTION_BATTERY_OKAY);
419 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700420 }
421
John Reck1cf4b792011-07-26 10:22:22 -0700422 private static class PruneThumbnails implements Runnable {
423 private Context mContext;
424 private List<Long> mIds;
425
426 PruneThumbnails(Context context, List<Long> preserveIds) {
427 mContext = context.getApplicationContext();
428 mIds = preserveIds;
429 }
430
431 @Override
432 public void run() {
433 ContentResolver cr = mContext.getContentResolver();
434 if (mIds == null || mIds.size() == 0) {
435 cr.delete(Thumbnails.CONTENT_URI, null, null);
436 } else {
437 int length = mIds.size();
438 StringBuilder where = new StringBuilder();
439 where.append(Thumbnails._ID);
440 where.append(" not in (");
441 for (int i = 0; i < length; i++) {
442 where.append(mIds.get(i));
443 if (i < (length - 1)) {
444 where.append(",");
445 }
446 }
447 where.append(")");
448 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
449 }
450 }
451
452 }
453
Michael Kolb1514bb72010-11-22 09:11:48 -0800454 @Override
455 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700456 return mFactory;
457 }
458
459 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800460 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700461 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700462 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800463 }
464
465 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800466 public void createSubWindow(Tab tab) {
467 endActionMode();
468 WebView mainView = tab.getWebView();
469 WebView subView = mFactory.createWebView((mainView == null)
470 ? false
471 : mainView.isPrivateBrowsingEnabled());
472 mUi.createSubWindow(tab, subView);
473 }
474
475 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700476 public Context getContext() {
477 return mActivity;
478 }
479
480 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700481 public Activity getActivity() {
482 return mActivity;
483 }
484
485 void setUi(UI ui) {
486 mUi = ui;
487 }
488
Michael Kolbaf63dba2012-05-16 12:58:05 -0700489 @Override
490 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700491 return mSettings;
492 }
493
494 IntentHandler getIntentHandler() {
495 return mIntentHandler;
496 }
497
498 @Override
499 public UI getUi() {
500 return mUi;
501 }
502
503 int getMaxTabs() {
504 return mActivity.getResources().getInteger(R.integer.max_tabs);
505 }
506
507 @Override
508 public TabControl getTabControl() {
509 return mTabControl;
510 }
511
Michael Kolb1bf23132010-11-19 12:55:12 -0800512 @Override
513 public List<Tab> getTabs() {
514 return mTabControl.getTabs();
515 }
516
Michael Kolb8233fac2010-10-26 16:08:53 -0700517 private void startHandler() {
518 mHandler = new Handler() {
519
520 @Override
521 public void handleMessage(Message msg) {
522 switch (msg.what) {
523 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700524 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800526 case UNKNOWN_TYPE_MSG:
527 HashMap unknownTypeMap = (HashMap) msg.obj;
528 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
529 /*
530 * When the context menu is shown to the user
531 * we need to assure that its happening on the current webview
532 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
533 */
534 if (getCurrentWebView() != viewForUnknownType)
535 break;
536
537 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800538 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800539 WebView.HitTestResult result = new WebView.HitTestResult();
540
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800541 // Prevent unnecessary calls to context menu
542 // if url and image src are null
543 if (unknown_type_src == null && unknown_type_url == null)
544 break;
545
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800546 //setting the HitTestResult with new RESULT TYPE
547 if (!TextUtils.isEmpty(unknown_type_src)) {
548 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
549 result.setExtra(unknown_type_src);
550 } else {
551 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
552 result.setExtra("about:blank");
553 }
554
555 mResult = result;
556 openContextMenu(viewForUnknownType);
557 mResult = null;
558
559 break;
560
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 case FOCUS_NODE_HREF:
562 {
563 String url = (String) msg.getData().get("url");
564 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500565 String src = (String) msg.getData().get("src");
566 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700567 if (TextUtils.isEmpty(url)) {
568 break;
569 }
570 HashMap focusNodeMap = (HashMap) msg.obj;
571 WebView view = (WebView) focusNodeMap.get("webview");
572 // Only apply the action if the top window did not change.
573 if (getCurrentTopWebView() != view) {
574 break;
575 }
576 switch (msg.arg1) {
577 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700578 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500580 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700581 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500582 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500583 case R.id.open_newtab_context_menu_id:
584 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700585 openTab(url, parent,
586 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500587 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700588 case R.id.copy_link_context_menu_id:
589 copy(url);
590 break;
591 case R.id.save_link_context_menu_id:
592 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500593 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800594 mActivity, url, view.getSettings().getUserAgentString(),
595 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700596 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700597 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700598 if(title == null || title == "")
599 title = url;
600
601 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
602 //SWE TODO: No thumbnail support for the url obtained via
603 //browser context menu as its not loaded in webview.
604 if (bookmarkIntent != null) {
605 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
606 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
607 mActivity.startActivity(bookmarkIntent);
608 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700609 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700610 }
611 break;
612 }
613
614 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700615 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 break;
617
618 case STOP_LOAD:
619 stopLoading();
620 break;
621
622 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700623 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700624 mWakeLock.release();
625 // if we reach here, Browser should be still in the
626 // background loading after WAKELOCK_TIMEOUT (5-min).
627 // To avoid burning the battery, stop loading.
628 mTabControl.stopAllLoading();
629 }
630 break;
631
632 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800633 Tab tab = (Tab) msg.obj;
634 if (tab != null) {
635 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 }
637 break;
kaiyiz591110b2013-08-06 17:11:06 +0800638 case OPEN_MENU:
639 if (!mOptionsMenuOpen && mActivity != null ) {
640 mActivity.openOptionsMenu();
641 }
642 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700643 }
644 }
645 };
646
647 }
648
John Reckef654f12011-07-12 16:42:08 -0700649 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200650 public Tab getCurrentTab() {
651 return mTabControl.getCurrentTab();
652 }
653
Michael Kolbba99c5d2010-11-29 14:57:41 -0800654 @Override
655 public void shareCurrentPage() {
656 shareCurrentPage(mTabControl.getCurrentTab());
657 }
658
659 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700660 if (tab == null || tab.getWebView() == null)
661 return;
662
663 final Tab mytab = tab;
664 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
665 @Override
666 public void onReceiveValue(Bitmap bitmap) {
667 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
668 mytab.getFavicon(), bitmap);
669 }
670 };
671
672 createScreenshotAsync(
673 tab.getWebView(),
674 getDesiredThumbnailWidth(mActivity),
675 getDesiredThumbnailHeight(mActivity),
676 new ValueCallback<Bitmap>() {
677 @Override
678 public void onReceiveValue(Bitmap bitmap) {
679 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
680 mytab.getFavicon(), bitmap);
681 }
682 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800683 }
684
Michael Kolb8233fac2010-10-26 16:08:53 -0700685 /**
686 * Share a page, providing the title, url, favicon, and a screenshot. Uses
687 * an {@link Intent} to launch the Activity chooser.
688 * @param c Context used to launch a new Activity.
689 * @param title Title of the page. Stored in the Intent with
690 * {@link Intent#EXTRA_SUBJECT}
691 * @param url URL of the page. Stored in the Intent with
692 * {@link Intent#EXTRA_TEXT}
693 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
694 * with {@link Browser#EXTRA_SHARE_FAVICON}
695 * @param screenshot Bitmap of a screenshot of the page. Stored in the
696 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
697 */
698 static final void sharePage(Context c, String title, String url,
699 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700700
701 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
702 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
703 R.layout.app_row, sDialog.getApps());
704 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 }
706
707 private void copy(CharSequence text) {
708 ClipboardManager cm = (ClipboardManager) mActivity
709 .getSystemService(Context.CLIPBOARD_SERVICE);
710 cm.setText(text);
711 }
712
713 // lifecycle
714
John Reck9c35b9c2012-05-30 10:08:50 -0700715 @Override
716 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700717 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800718 // update the menu in case of a locale change
719 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800720 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800721 if (mOptionsMenuOpen) {
722 mActivity.closeOptionsMenu();
723 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
724 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 if (mPageDialogsHandler != null) {
726 mPageDialogsHandler.onConfigurationChanged(config);
727 }
728 mUi.onConfigurationChanged(config);
729 }
730
731 @Override
732 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700733 if (!mUi.isWebShowing()) {
734 mUi.showWeb(false);
735 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700736 mIntentHandler.onNewIntent(intent);
737 }
738
John Reck9c35b9c2012-05-30 10:08:50 -0700739 @Override
740 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800741 if (mUi.isCustomViewShowing()) {
742 hideCustomView();
743 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 if (mActivityPaused) {
745 Log.e(LOGTAG, "BrowserActivity is already paused.");
746 return;
747 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700748 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800749 Tab tab = mTabControl.getCurrentTab();
750 if (tab != null) {
751 tab.pause();
752 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700753 if (mWakeLock == null) {
754 PowerManager pm = (PowerManager) mActivity
755 .getSystemService(Context.POWER_SERVICE);
756 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
757 }
Michael Kolb70976932010-11-30 11:34:01 -0800758 mWakeLock.acquire();
759 mHandler.sendMessageDelayed(mHandler
760 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
761 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 }
763 mUi.onPause();
764 mNetworkHandler.onPause();
765
766 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100767 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700768 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700769 }
770
John Reck9c35b9c2012-05-30 10:08:50 -0700771 @Override
772 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700773 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800774 Bundle saveState = createSaveState();
775
776 // crash recovery manages all save & restore state
777 mCrashRecoveryHandler.writeState(saveState);
778 mSettings.setLastRunPaused(true);
779 }
780
781 /**
782 * Save the current state to outState. Does not write the state to
783 * disk.
784 * @return Bundle containing the current state of all tabs.
785 */
786 /* package */ Bundle createSaveState() {
787 Bundle saveState = new Bundle();
788 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800789 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700790 }
791
John Reck9c35b9c2012-05-30 10:08:50 -0700792 @Override
793 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 if (!mActivityPaused) {
795 Log.e(LOGTAG, "BrowserActivity is already resumed.");
796 return;
797 }
George Mount3636d0a2011-11-21 09:08:21 -0800798 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800800 Tab current = mTabControl.getCurrentTab();
801 if (current != null) {
802 current.resume();
803 resumeWebViewTimers(current);
804 }
John Reckf57c0292011-07-21 18:15:39 -0700805 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200806
Michael Kolb8233fac2010-10-26 16:08:53 -0700807 mUi.onResume();
808 mNetworkHandler.onResume();
809 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100810 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700811 if (mVoiceResult != null) {
812 mUi.onVoiceResult(mVoiceResult);
813 mVoiceResult = null;
814 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700815 if (current != null && current.getWebView().isShowingCrashView())
816 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700817 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700818 }
819
John Reckf57c0292011-07-21 18:15:39 -0700820 private void releaseWakeLock() {
821 if (mWakeLock != null && mWakeLock.isHeld()) {
822 mHandler.removeMessages(RELEASE_WAKELOCK);
823 mWakeLock.release();
824 }
825 }
826
Michael Kolb70976932010-11-30 11:34:01 -0800827 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800828 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800829 * @param tab guaranteed non-null
830 */
831 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 boolean inLoad = tab.inPageLoad();
833 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
834 CookieSyncManager.getInstance().startSync();
835 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100836 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 }
838 }
839
Michael Kolb70976932010-11-30 11:34:01 -0800840 /**
841 * Pause all WebView timers using the WebView of the given tab
842 * @param tab
843 * @return true if the timers are paused or tab is null
844 */
845 private boolean pauseWebViewTimers(Tab tab) {
846 if (tab == null) {
847 return true;
848 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100850 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700851 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 }
Michael Kolb70976932010-11-30 11:34:01 -0800853 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 }
855
John Reck9c35b9c2012-05-30 10:08:50 -0700856 @Override
857 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800858 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
860 mUploadHandler = null;
861 }
862 if (mTabControl == null) return;
863 mUi.onDestroy();
864 // Remove the current tab and sub window
865 Tab t = mTabControl.getCurrentTab();
866 if (t != null) {
867 dismissSubWindow(t);
868 removeTab(t);
869 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500870 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 // Destroy all the tabs
872 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700873 // Unregister receiver
874 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700875 }
876
877 protected boolean isActivityPaused() {
878 return mActivityPaused;
879 }
880
John Reck9c35b9c2012-05-30 10:08:50 -0700881 @Override
882 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 mTabControl.freeMemory();
884 }
885
886 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700887 public void stopLoading() {
888 mLoadStopped = true;
889 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700891 if (w != null) {
892 w.stopLoading();
893 mUi.onPageStopped(tab);
894 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 }
896
897 boolean didUserStopLoading() {
898 return mLoadStopped;
899 }
900
kaiyiza016da12013-08-26 17:50:22 +0800901 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700902 final String reminderType = getContext().getResources().getString(
903 R.string.def_wifi_browser_interaction_remind_type);
904 final String selectionConnnection = getContext().getResources().getString(
905 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700906 final String wifiSelection = getContext().getResources().getString(
907 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700908
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700909 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
910 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700911 return;
912
kaiyiza016da12013-08-26 17:50:22 +0800913 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700914 Context.CONNECTIVITY_SERVICE);
915 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700916 WifiManager wifiMgr = (WifiManager) this.getContext()
917 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700918 if (networkInfo == null
919 || (networkInfo != null && (networkInfo.getType() !=
920 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700921 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
922 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700923 List<ScanResult> list = wifiMgr.getScanResults();
924 // Have no AP's for Wifi's fall back to data
925 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700926 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700927 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
928 this.getContext().startActivity(intent);
929 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700930 // Request to select Wifi AP
931 try {
932 Intent intent = new Intent(wifiSelection);
933 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
934 this.getContext().startActivity(intent);
935 } catch (Exception e) {
936 String err_msg = this.getContext().getString(
937 R.string.acivity_not_found, wifiSelection);
938 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
939 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700940 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700941 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800942 }
943 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700944
Michael Kolb8233fac2010-10-26 16:08:53 -0700945 // WebViewController
946
947 @Override
John Reck324d4402011-01-11 16:56:42 -0800948 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700949
950 // We've started to load a new page. If there was a pending message
951 // to save a screenshot then we will now take the new page and save
952 // an incorrect screenshot. Therefore, remove any pending thumbnail
953 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700954 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700955
956 // reset sync timer to avoid sync starts during loading a page
957 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700958 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700959 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800960 boolean networkNotifier = BrowserConfig.getInstance(getContext())
961 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700962 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700963 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800964 } else {
965 if (!mNetworkHandler.isNetworkUp()) {
966 view.setNetworkAvailable(false);
967 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700968 }
969
970 // when BrowserActivity just starts, onPageStarted may be called before
971 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
972 // to start the timer. As we won't switch tabs while an activity is in
973 // pause state, we can ensure calling resume and pause in pair.
974 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800975 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 }
977 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700978 endActionMode();
979
John Reck30c714c2010-12-16 17:30:34 -0800980 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700981
John Reck324d4402011-01-11 16:56:42 -0800982 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 // update the bookmark database for favicon
984 maybeUpdateFavicon(tab, null, url, favicon);
985
986 Performance.tracePageStart(url);
987
988 // Performance probe
989 if (false) {
990 Performance.onPageStarted();
991 }
992
993 }
994
995 @Override
John Reck324d4402011-01-11 16:56:42 -0800996 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700997 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800998 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200999
Michael Kolb8233fac2010-10-26 16:08:53 -07001000 // Performance probe
1001 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001002 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 }
1004
Tarun Nainani8eb00912014-07-17 12:28:32 -07001005 tab.onPageFinished();
1006
Michael Kolb8233fac2010-10-26 16:08:53 -07001007 Performance.tracePageFinished();
1008 }
1009
1010 @Override
John Reck30c714c2010-12-16 17:30:34 -08001011 public void onProgressChanged(Tab tab) {
1012 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001013
1014 if (newProgress == 100) {
1015 CookieSyncManager.getInstance().sync();
1016 // onProgressChanged() may continue to be called after the main
1017 // frame has finished loading, as any remaining sub frames continue
1018 // to load. We'll only get called once though with newProgress as
1019 // 100 when everything is loaded. (onPageFinished is called once
1020 // when the main frame completes loading regardless of the state of
1021 // any sub frames so calls to onProgressChanges may continue after
1022 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001023 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001024 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001025 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001026 } else if (mWasInPageLoad) {
1027 mWasInPageLoad = false;
1028 updateInLoadMenuItems(mCachedMenu, tab);
1029 }
1030
1031 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001032 // pause the WebView timer and release the wake lock if it is
1033 // finished while BrowserActivity is in pause state.
1034 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001035 }
John Reckd9862372012-02-21 15:04:50 -08001036 if (!tab.isPrivateBrowsingEnabled()
1037 && !TextUtils.isEmpty(tab.getUrl())
1038 && !tab.isSnapshot()) {
1039 // Only update the bookmark screenshot if the user did not
1040 // cancel the load early and there is not already
1041 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001042 if (tab.shouldUpdateThumbnail() &&
1043 (tab.inForeground() && !didUserStopLoading()
1044 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001045 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1046 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1047 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001048 1500);
John Reckd9862372012-02-21 15:04:50 -08001049 }
1050 }
1051 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001053 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001054 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001055 // still loading
1056 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001057 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001058 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001059 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001060 } else {
1061 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 }
1063 }
John Reck30c714c2010-12-16 17:30:34 -08001064 mUi.onProgressChanged(tab);
1065 }
1066
1067 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001068 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001069 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001070 }
1071
1072 @Override
1073 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001074 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001075 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001076 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1078 return;
1079 }
1080 // Update the title in the history database if not in private browsing mode
1081 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001082 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001083 }
1084 }
1085
1086 @Override
1087 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001088 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001089 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1090 }
1091
1092 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001093 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1094 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001095 }
1096
1097 @Override
1098 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1099 if (mMenuIsDown) {
1100 // only check shortcut key when MENU is held
1101 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1102 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001104 int keyCode = event.getKeyCode();
1105 // We need to send almost every key to WebKit. However:
1106 // 1. We don't want to block the device on the renderer for
1107 // some keys like menu, home, call.
1108 // 2. There are no WebKit equivalents for some of these keys
1109 // (see app/keyboard_codes_win.h)
1110 // Note that these are not the same set as KeyEvent.isSystemKey:
1111 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1112 if (keyCode == KeyEvent.KEYCODE_MENU ||
1113 keyCode == KeyEvent.KEYCODE_HOME ||
1114 keyCode == KeyEvent.KEYCODE_BACK ||
1115 keyCode == KeyEvent.KEYCODE_CALL ||
1116 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1117 keyCode == KeyEvent.KEYCODE_POWER ||
1118 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1119 keyCode == KeyEvent.KEYCODE_CAMERA ||
1120 keyCode == KeyEvent.KEYCODE_FOCUS ||
1121 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1122 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1123 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1124 return true;
1125 }
1126
1127 // We also have to intercept some shortcuts before we send them to the ContentView.
1128 if (event.isCtrlPressed() && (
1129 keyCode == KeyEvent.KEYCODE_TAB ||
1130 keyCode == KeyEvent.KEYCODE_W ||
1131 keyCode == KeyEvent.KEYCODE_F4)) {
1132 return true;
1133 }
1134
1135 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 }
1137
Tarun Nainanif5563b62015-01-21 18:52:59 -08001138 private void handleMediaKeyEvent(KeyEvent event) {
1139
1140 int keyCode = event.getKeyCode();
1141 // send media key events to audio manager
1142 if (Build.VERSION.SDK_INT >= 19) {
1143
1144 switch (keyCode) {
1145 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1146 case KeyEvent.KEYCODE_MEDIA_STOP:
1147 case KeyEvent.KEYCODE_MEDIA_NEXT:
1148 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1149 case KeyEvent.KEYCODE_MEDIA_REWIND:
1150 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1151 case KeyEvent.KEYCODE_MUTE:
1152 case KeyEvent.KEYCODE_MEDIA_PLAY:
1153 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1154 case KeyEvent.META_SHIFT_RIGHT_ON:
1155 case KeyEvent.KEYCODE_MEDIA_EJECT:
1156 case KeyEvent.KEYCODE_MEDIA_RECORD:
1157 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1158
1159 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1160 .getSystemService(Context.AUDIO_SERVICE);
1161 audioManager.dispatchMediaKeyEvent(event);
1162 }
1163 }
1164 }
1165
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 @Override
John Reck997b1b72012-04-19 18:08:25 -07001167 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001168 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001169 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001171 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 } else {
John Reck997b1b72012-04-19 18:08:25 -07001173 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 }
1175 }
John Reck997b1b72012-04-19 18:08:25 -07001176 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001177 }
1178
1179 @Override
John Reck324d4402011-01-11 16:56:42 -08001180 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001181 // Don't save anything in private browsing mode or when disabling history
1182 // for regular tabs is enabled
1183 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1184 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1185 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001186
John Reck49a603c2011-03-03 09:33:05 -08001187 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001188
John Reck324d4402011-01-11 16:56:42 -08001189 if (TextUtils.isEmpty(url)
1190 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001191 return;
1192 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001193
John Reckf57c0292011-07-21 18:15:39 -07001194 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001195 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001196 }
1197
1198 @Override
1199 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1200 AsyncTask<Void, Void, String[]> task =
1201 new AsyncTask<Void, Void, String[]>() {
1202 @Override
1203 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001204 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001205 }
1206 @Override
1207 public void onPostExecute(String[] result) {
1208 callback.onReceiveValue(result);
1209 }
1210 };
1211 task.execute();
1212 }
1213
1214 @Override
1215 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1216 final HttpAuthHandler handler, final String host,
1217 final String realm) {
1218 String username = null;
1219 String password = null;
1220
1221 boolean reuseHttpAuthUsernamePassword
1222 = handler.useHttpAuthUsernamePassword();
1223
1224 if (reuseHttpAuthUsernamePassword && view != null) {
1225 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1226 if (credentials != null && credentials.length == 2) {
1227 username = credentials[0];
1228 password = credentials[1];
1229 }
1230 }
1231
1232 if (username != null && password != null) {
1233 handler.proceed(username, password);
1234 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001235 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001236 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1237 } else {
1238 handler.cancel();
1239 }
1240 }
1241 }
1242
1243 @Override
1244 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001245 String contentDisposition, String mimetype, String referer,
1246 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001247 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001248 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001249 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001250 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001251 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001252 // This Tab was opened for the sole purpose of downloading a
1253 // file. Remove it.
1254 if (tab == mTabControl.getCurrentTab()) {
1255 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001256 if (tab.getDerivedFromIntent())
1257 closeTab(tab);
1258 else
1259 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001260 } else {
1261 // In this case, it is not.
1262 closeTab(tab);
1263 }
1264 }
1265 }
1266
1267 @Override
1268 public Bitmap getDefaultVideoPoster() {
1269 return mUi.getDefaultVideoPoster();
1270 }
1271
1272 @Override
1273 public View getVideoLoadingProgressView() {
1274 return mUi.getVideoLoadingProgressView();
1275 }
1276
1277 @Override
1278 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1279 SslError error) {
1280 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1281 }
1282
1283 // helper method
1284
1285 /*
1286 * Update the favorites icon if the private browsing isn't enabled and the
1287 * icon is valid.
1288 */
1289 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1290 final String url, Bitmap favicon) {
1291 if (favicon == null) {
1292 return;
1293 }
1294 if (!tab.isPrivateBrowsingEnabled()) {
1295 Bookmarks.updateFavicon(mActivity
1296 .getContentResolver(), originalUrl, url, favicon);
1297 }
1298 }
1299
Leon Scroggins4cd97792010-12-03 15:31:56 -05001300 @Override
1301 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001302 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001303 mUi.bookmarkedStatusHasChanged(tab);
1304 }
1305
Michael Kolb8233fac2010-10-26 16:08:53 -07001306 // end WebViewController
1307
1308 protected void pageUp() {
1309 getCurrentTopWebView().pageUp(false);
1310 }
1311
1312 protected void pageDown() {
1313 getCurrentTopWebView().pageDown(false);
1314 }
1315
1316 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001317 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001318 public void editUrl() {
1319 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001320 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001321 }
1322
John Reck9c35b9c2012-05-30 10:08:50 -07001323 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001324 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001325 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001326 if (tab.inForeground()) {
1327 if (mUi.isCustomViewShowing()) {
1328 callback.onCustomViewHidden();
1329 return;
1330 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001331 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001332 // Save the menu state and set it to empty while the custom
1333 // view is showing.
1334 mOldMenuState = mMenuState;
1335 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001336 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 }
1338 }
1339
1340 @Override
1341 public void hideCustomView() {
1342 if (mUi.isCustomViewShowing()) {
1343 mUi.onHideCustomView();
1344 // Reset the old menu state.
1345 mMenuState = mOldMenuState;
1346 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001347 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001348 }
1349 }
1350
John Reck9c35b9c2012-05-30 10:08:50 -07001351 @Override
1352 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001353 Intent intent) {
1354 if (getCurrentTopWebView() == null) return;
1355 switch (requestCode) {
1356 case PREFERENCES_PAGE:
1357 if (resultCode == Activity.RESULT_OK && intent != null) {
1358 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001359 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 mTabControl.removeParentChildRelationShips();
1361 }
1362 }
1363 break;
1364 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001365 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001366 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001367 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001369 case AUTOFILL_SETUP:
1370 // Determine whether a profile was actually set up or not
1371 // and if so, send the message back to the WebTextView to
1372 // fill the form with the new profile.
1373 if (getSettings().getAutoFillProfile() != null) {
1374 mAutoFillSetupMessage.sendToTarget();
1375 mAutoFillSetupMessage = null;
1376 }
1377 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001378 case COMBO_VIEW:
1379 if (intent == null || resultCode != Activity.RESULT_OK) {
1380 break;
1381 }
John Reck3ba45532011-08-11 16:26:53 -07001382 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001383 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1384 Tab t = getCurrentTab();
1385 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001386 mUpdateMyNavThumbnail = true;
1387 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001388 loadUrl(t, uri.toString());
1389 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1390 String[] urls = intent.getStringArrayExtra(
1391 ComboViewActivity.EXTRA_OPEN_ALL);
1392 Tab parent = getCurrentTab();
1393 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001394 if (url != null) {
1395 parent = openTab(url, parent,
1396 !mSettings.openInBackground(), true);
1397 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001398 }
1399 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1400 long id = intent.getLongExtra(
1401 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1402 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001403 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001404 }
1405 }
1406 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001407 case VOICE_RESULT:
1408 if (resultCode == Activity.RESULT_OK && intent != null) {
1409 ArrayList<String> results = intent.getStringArrayListExtra(
1410 RecognizerIntent.EXTRA_RESULTS);
1411 if (results.size() >= 1) {
1412 mVoiceResult = results.get(0);
1413 }
1414 }
1415 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001416 case MY_NAVIGATION:
1417 if (intent == null || resultCode != Activity.RESULT_OK) {
1418 break;
1419 }
1420
1421 if (intent.getBooleanExtra("need_refresh", false) &&
1422 getCurrentTopWebView() != null) {
1423 getCurrentTopWebView().reload();
1424 }
1425 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001426 default:
1427 break;
1428 }
1429 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001430 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001431 }
1432
1433 /**
1434 * Open the Go page.
1435 * @param startWithHistory If true, open starting on the history tab.
1436 * Otherwise, start with the bookmarks tab.
1437 */
1438 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001439 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001440 if (mTabControl.getCurrentWebView() == null) {
1441 return;
1442 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001443 // clear action mode
1444 if (isInCustomActionMode()) {
1445 endActionMode();
1446 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001447 Bundle extras = new Bundle();
1448 // Disable opening in a new window if we have maxed out the windows
1449 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1450 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001451 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001452 }
1453
1454 // combo view callbacks
1455
Michael Kolb8233fac2010-10-26 16:08:53 -07001456 // key handling
1457 protected void onBackKey() {
1458 if (!mUi.onBackKey()) {
1459 WebView subwindow = mTabControl.getCurrentSubWindow();
1460 if (subwindow != null) {
1461 if (subwindow.canGoBack()) {
1462 subwindow.goBack();
1463 } else {
1464 dismissSubWindow(mTabControl.getCurrentTab());
1465 }
1466 } else {
1467 goBackOnePageOrQuit();
1468 }
1469 }
1470 }
1471
Michael Kolb4bd767d2011-05-27 11:33:55 -07001472 protected boolean onMenuKey() {
1473 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001474 }
1475
Michael Kolb8233fac2010-10-26 16:08:53 -07001476 // menu handling and state
1477 // TODO: maybe put into separate handler
1478
John Reck9c35b9c2012-05-30 10:08:50 -07001479 @Override
1480 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001481 if (mMenuState == EMPTY_MENU) {
1482 return false;
1483 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001484 MenuInflater inflater = mActivity.getMenuInflater();
1485 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001486 return true;
1487 }
1488
John Reck9c35b9c2012-05-30 10:08:50 -07001489 @Override
1490 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001491 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001492 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001493 return;
1494 }
1495 if (!(v instanceof WebView)) {
1496 return;
1497 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001498 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001499 WebView.HitTestResult result;
1500
1501 /* Determine whether the ContextMenu got triggered because
1502 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1503 * received. The mResult acts as a flag to identify, how it got trigerred
1504 */
1505 if (mResult == null){
1506 result = webview.getHitTestResult();
1507 } else {
1508 result = mResult;
1509 }
1510
Michael Kolb8233fac2010-10-26 16:08:53 -07001511 if (result == null) {
1512 return;
1513 }
1514
1515 int type = result.getType();
1516 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001517
1518 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1519 unknownTypeMap.put("webview", webview);
1520 final Message msg = mHandler.obtainMessage(
1521 UNKNOWN_TYPE_MSG, unknownTypeMap);
1522 /* As defined in android developers guide
1523 * when UNKNOWN_TYPE is received as a result of HitTest
1524 * you need to determing the type by invoking requestFocusNodeHref
1525 */
1526 webview.requestFocusNodeHref(msg);
1527
Michael Kolb8233fac2010-10-26 16:08:53 -07001528 Log.w(LOGTAG,
1529 "We should not show context menu when nothing is touched");
1530 return;
1531 }
1532 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1533 // let TextView handles context menu
1534 return;
1535 }
1536
1537 // Note, http://b/issue?id=1106666 is requesting that
1538 // an inflated menu can be used again. This is not available
1539 // yet, so inflate each time (yuk!)
1540 MenuInflater inflater = mActivity.getMenuInflater();
1541 inflater.inflate(R.menu.browsercontext, menu);
1542
1543 // Show the correct menu group
1544 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001545 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001546 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001547 menu.setGroupVisible(R.id.PHONE_MENU,
1548 type == WebView.HitTestResult.PHONE_TYPE);
1549 menu.setGroupVisible(R.id.EMAIL_MENU,
1550 type == WebView.HitTestResult.EMAIL_TYPE);
1551 menu.setGroupVisible(R.id.GEO_MENU,
1552 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001553 String itemUrl = null;
1554 String url = webview.getOriginalUrl();
1555 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1556 itemUrl = Uri.decode(navigationUrl);
1557 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1558 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1559 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1560 } else {
1561 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1562 }
1563 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1564 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1565 } else {
1566 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1567
1568 menu.setGroupVisible(R.id.IMAGE_MENU,
1569 type == WebView.HitTestResult.IMAGE_TYPE
1570 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1571 menu.setGroupVisible(R.id.ANCHOR_MENU,
1572 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1573 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001574 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1575 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001576 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001577 // Setup custom handling depending on the type
1578 switch (type) {
1579 case WebView.HitTestResult.PHONE_TYPE:
1580 menu.setHeaderTitle(Uri.decode(extra));
1581 menu.findItem(R.id.dial_context_menu_id).setIntent(
1582 new Intent(Intent.ACTION_VIEW, Uri
1583 .parse(WebView.SCHEME_TEL + extra)));
1584 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1585 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1586 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1587 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1588 addIntent);
1589 menu.findItem(R.id.copy_phone_context_menu_id)
1590 .setOnMenuItemClickListener(
1591 new Copy(extra));
1592 break;
1593
1594 case WebView.HitTestResult.EMAIL_TYPE:
1595 menu.setHeaderTitle(extra);
1596 menu.findItem(R.id.email_context_menu_id).setIntent(
1597 new Intent(Intent.ACTION_VIEW, Uri
1598 .parse(WebView.SCHEME_MAILTO + extra)));
1599 menu.findItem(R.id.copy_mail_context_menu_id)
1600 .setOnMenuItemClickListener(
1601 new Copy(extra));
1602 break;
1603
1604 case WebView.HitTestResult.GEO_TYPE:
1605 menu.setHeaderTitle(extra);
1606 menu.findItem(R.id.map_context_menu_id).setIntent(
1607 new Intent(Intent.ACTION_VIEW, Uri
1608 .parse(WebView.SCHEME_GEO
1609 + URLEncoder.encode(extra))));
1610 menu.findItem(R.id.copy_geo_context_menu_id)
1611 .setOnMenuItemClickListener(
1612 new Copy(extra));
1613 break;
1614
1615 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1616 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001617 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 // decide whether to show the open link in new tab option
1619 boolean showNewTab = mTabControl.canCreateNewTab();
1620 MenuItem newTabItem
1621 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001622 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001623 ? R.string.contextmenu_openlink_newwindow_background
1624 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001625 newTabItem.setVisible(showNewTab);
1626 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001627 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1628 newTabItem.setOnMenuItemClickListener(
1629 new MenuItem.OnMenuItemClickListener() {
1630 @Override
1631 public boolean onMenuItemClick(MenuItem item) {
1632 final HashMap<String, WebView> hrefMap =
1633 new HashMap<String, WebView>();
1634 hrefMap.put("webview", webview);
1635 final Message msg = mHandler.obtainMessage(
1636 FOCUS_NODE_HREF,
1637 R.id.open_newtab_context_menu_id,
1638 0, hrefMap);
1639 webview.requestFocusNodeHref(msg);
1640 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001641 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001642 });
1643 } else {
1644 newTabItem.setOnMenuItemClickListener(
1645 new MenuItem.OnMenuItemClickListener() {
1646 @Override
1647 public boolean onMenuItemClick(MenuItem item) {
1648 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001649 openTab(extra, parent,
1650 !mSettings.openInBackground(),
1651 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001652 return true;
1653 }
1654 });
1655 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001656 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001657 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1658 menu.setHeaderTitle(navigationUrl);
1659 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1660
1661 if (itemUrl != null) {
1662 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1663 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1664 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1665 @Override
1666 public boolean onMenuItemClick(MenuItem item) {
1667 final Intent intent = new Intent(Controller.this
1668 .getContext(),
1669 AddMyNavigationPage.class);
1670 Bundle bundle = new Bundle();
1671 String url = Uri.decode(navigationUrl);
1672 bundle.putBoolean("isAdding", false);
1673 bundle.putString("url", url);
1674 bundle.putString("name", getNameFromUrl(url));
1675 intent.putExtra("websites", bundle);
1676 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1677 return false;
1678 }
1679 });
1680 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1681 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1682 @Override
1683 public boolean onMenuItemClick(MenuItem item) {
1684 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1685 return false;
1686 }
1687 });
1688 }
1689 } else {
1690 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1691 }
1692 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001693 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1694 break;
1695 }
1696 // otherwise fall through to handle image part
1697 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001698 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1699 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001700 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1701 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001702 shareItem.setOnMenuItemClickListener(
1703 new MenuItem.OnMenuItemClickListener() {
1704 @Override
1705 public boolean onMenuItemClick(MenuItem item) {
1706 sharePage(mActivity, null, extra, null,
1707 null);
1708 return true;
1709 }
1710 }
1711 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001712 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001713 menu.findItem(R.id.view_image_context_menu_id)
1714 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1715 @Override
1716 public boolean onMenuItemClick(MenuItem item) {
1717 openTab(extra, mTabControl.getCurrentTab(), true, true);
1718 return false;
1719 }
1720 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001721 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1722 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1723 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001724 menu.findItem(R.id.set_wallpaper_context_menu_id).
1725 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1726 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001727 break;
1728
1729 default:
1730 Log.w(LOGTAG, "We should not get here.");
1731 break;
1732 }
1733 //update the ui
1734 mUi.onContextMenuCreated(menu);
1735 }
1736
kaiyiz6e5b3e02013-08-19 20:02:01 +08001737 public void startAddMyNavigation(String url) {
1738 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1739 Bundle bundle = new Bundle();
1740 bundle.putBoolean("isAdding", true);
1741 bundle.putString("url", url);
1742 bundle.putString("name", getNameFromUrl(url));
1743 intent.putExtra("websites", bundle);
1744 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1745 }
1746
1747 private void showMyNavigationDeleteDialog(final String itemUrl) {
1748 new AlertDialog.Builder(this.getContext())
1749 .setTitle(R.string.my_navigation_delete_label)
1750 .setIcon(android.R.drawable.ic_dialog_alert)
1751 .setMessage(R.string.my_navigation_delete_msg)
1752 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1753 @Override
1754 public void onClick(DialogInterface dialog, int whichButton) {
1755 deleteMyNavigationItem(itemUrl);
1756 }
1757 })
1758 .setNegativeButton(R.string.cancel, null)
1759 .show();
1760 }
1761
1762 private void deleteMyNavigationItem(final String itemUrl) {
1763 ContentResolver cr = this.getContext().getContentResolver();
1764 Cursor cursor = null;
1765
1766 try {
1767 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1768 new String[] {
1769 MyNavigationUtil.ID
1770 }, "url = ?", new String[] {
1771 itemUrl
1772 }, null);
1773 if (null != cursor && cursor.moveToFirst()) {
1774 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1775 cursor.getLong(0));
1776
1777 ContentValues values = new ContentValues();
1778 values.put(MyNavigationUtil.TITLE, "");
1779 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1780 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1781 ByteArrayOutputStream os = new ByteArrayOutputStream();
1782 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1783 R.raw.my_navigation_add);
1784 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1785 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1786 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1787 cr.update(uri, values, null, null);
1788 } else {
1789 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1790 }
1791 } catch (IllegalStateException e) {
1792 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1793 } finally {
1794 if (null != cursor) {
1795 cursor.close();
1796 }
1797 }
1798
1799 if (getCurrentTopWebView() != null) {
1800 getCurrentTopWebView().reload();
1801 }
1802 }
1803
1804 private String getNameFromUrl(String itemUrl) {
1805 ContentResolver cr = this.getContext().getContentResolver();
1806 Cursor cursor = null;
1807 String name = null;
1808
1809 try {
1810 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1811 new String[] {
1812 MyNavigationUtil.TITLE
1813 }, "url = ?", new String[] {
1814 itemUrl
1815 }, null);
1816 if (null != cursor && cursor.moveToFirst()) {
1817 name = cursor.getString(0);
1818 } else {
1819 Log.e(LOGTAG, "this item does not exist!");
1820 }
1821 } catch (IllegalStateException e) {
1822 Log.e(LOGTAG, "getNameFromUrl", e);
1823 } finally {
1824 if (null != cursor) {
1825 cursor.close();
1826 }
1827 }
1828 return name;
1829 }
1830
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001831 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001832 final ContentResolver cr = mActivity.getContentResolver();
1833 new AsyncTask<Void, Void, Void>() {
1834 @Override
1835 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001836 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1837 if(!isMyNavigationUrl)
1838 return null;
1839
kaiyiz6e5b3e02013-08-19 20:02:01 +08001840 ContentResolver cr = mActivity.getContentResolver();
1841 Cursor cursor = null;
1842 try {
1843 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1844 new String[] {
1845 MyNavigationUtil.ID
1846 }, "url = ?", new String[] {
1847 itemUrl
1848 }, null);
1849 if (null != cursor && cursor.moveToFirst()) {
1850 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001851 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001852
1853 ContentValues values = new ContentValues();
1854 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1855 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1856 cursor.getLong(0));
1857 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1858 cr.update(uri, values, null, null);
1859 os.close();
1860 }
1861 } catch (IllegalStateException e) {
1862 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1863 } catch (IOException e) {
1864 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1865 } finally {
1866 if (null != cursor) {
1867 cursor.close();
1868 }
1869 }
1870 return null;
1871 }
1872 }.execute();
1873 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001874 /**
1875 * As the menu can be open when loading state changes
1876 * we must manually update the state of the stop/reload menu
1877 * item
1878 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001879 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001880 if (menu == null) {
1881 return;
1882 }
1883 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001884 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001885 menu.findItem(R.id.stop_menu_id):
1886 menu.findItem(R.id.reload_menu_id);
1887 if (src != null) {
1888 dest.setIcon(src.getIcon());
1889 dest.setTitle(src.getTitle());
1890 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001891 mActivity.invalidateOptionsMenu();
1892 }
1893
1894 public void invalidateOptionsMenu() {
1895 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001896 }
1897
John Reck9c35b9c2012-05-30 10:08:50 -07001898 @Override
1899 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001900 // Software menu key (toolbar key)
1901 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1902 return true;
1903 }
1904
1905 @Override
1906 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001907 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001908 // hold on to the menu reference here; it is used by the page callbacks
1909 // to update the menu based on loading state
1910 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001911 // Note: setVisible will decide whether an item is visible; while
1912 // setEnabled() will decide whether an item is enabled, which also means
1913 // whether the matching shortcut key will function.
1914 switch (mMenuState) {
1915 case EMPTY_MENU:
1916 if (mCurrentMenuState != mMenuState) {
1917 menu.setGroupVisible(R.id.MAIN_MENU, false);
1918 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1919 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1920 }
1921 break;
1922 default:
1923 if (mCurrentMenuState != mMenuState) {
1924 menu.setGroupVisible(R.id.MAIN_MENU, true);
1925 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1926 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1927 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001928 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001929 break;
1930 }
1931 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001932 mUi.onPrepareOptionsMenu(menu);
1933 }
1934
1935 private void setMenuItemVisibility(Menu menu, int id,
1936 boolean visibility) {
1937 MenuItem item = menu.findItem(id);
1938 if (item != null) {
1939 item.setVisible(visibility);
1940 }
1941 }
1942
1943 private int lookupBookmark(String title, String url) {
1944 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001945 int count = 0;
1946 Cursor cursor = null;
1947 try {
1948 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1949 BookmarksLoader.PROJECTION,
1950 "title = ? OR url = ?",
1951 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001952 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001953 },
1954 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001955
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001956 if (cursor != null)
1957 count = cursor.getCount();
1958
1959 } catch (IllegalStateException e) {
1960 Log.e(LOGTAG, "lookupBookmark ", e);
1961 } finally {
1962 if (null != cursor) {
1963 cursor.close();
1964 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001965 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001966 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001967 }
1968
1969 private void resetMenuItems(Menu menu) {
1970 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1971 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1972
1973 WebView w = getCurrentTopWebView();
1974 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1975
1976 String title = w.getTitle();
1977 String url = w.getUrl();
1978 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1979 bookmark_icon.setChecked(true);
1980 } else {
1981 bookmark_icon.setChecked(false);
1982 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 }
1984
Michael Kolb4bf79712011-07-14 14:18:12 -07001985 @Override
1986 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001987 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001988 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001989 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001990 // Following flag is used to identify schemes for which the LIVE_MENU
1991 // items defined in res/menu/browser.xml should be enabled
1992 boolean isLiveScheme = false;
1993 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001994 boolean isSavable = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001995 resetMenuItems(menu);
1996
Michael Kolb4bf79712011-07-14 14:18:12 -07001997 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001998 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001999 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002000 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002001 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002002 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002003 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07002004 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002005
2006 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2007 forward.setEnabled(canGoForward);
2008
Michael Kolb4bf79712011-07-14 14:18:12 -07002009 // decide whether to show the share link option
2010 PackageManager pm = mActivity.getPackageManager();
2011 Intent send = new Intent(Intent.ACTION_SEND);
2012 send.setType("text/plain");
2013 ResolveInfo ri = pm.resolveActivity(send,
2014 PackageManager.MATCH_DEFAULT_ONLY);
2015 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2016
2017 boolean isNavDump = mSettings.enableNavDump();
2018 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2019 nav.setVisible(isNavDump);
2020 nav.setEnabled(isNavDump);
2021
2022 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002023 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2024 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002025 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002026 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2027 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002028 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002029 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2030 isLive && isLiveScheme && isPageFinished);
2031 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002032 isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002033 // history and snapshots item are the members of COMBO menu group,
2034 // so if show history item, only make snapshots item invisible.
2035 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002036
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002037 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002038 }
2039
John Reck9c35b9c2012-05-30 10:08:50 -07002040 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002041 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 if (null == getCurrentTopWebView()) {
2043 return false;
2044 }
2045 if (mMenuIsDown) {
2046 // The shortcut action consumes the MENU. Even if it is still down,
2047 // it won't trigger the next shortcut action. In the case of the
2048 // shortcut action triggering a new activity, like Bookmarks, we
2049 // won't get onKeyUp for MENU. So it is important to reset it here.
2050 mMenuIsDown = false;
2051 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002052 if (mUi.onOptionsItemSelected(item)) {
2053 // ui callback handled it
2054 return true;
2055 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002056 switch (item.getItemId()) {
2057 // -- Main menu
2058 case R.id.new_tab_menu_id:
2059 openTabToHomePage();
2060 break;
2061
2062 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002063 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002064 break;
2065
2066 case R.id.goto_menu_id:
2067 editUrl();
2068 break;
2069
2070 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002071 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2072 break;
2073
2074 case R.id.history_menu_id:
2075 bookmarksOrHistoryPicker(ComboViews.History);
2076 break;
2077
2078 case R.id.snapshots_menu_id:
2079 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 break;
2081
Pankaj Garg49b79252014-11-07 17:33:41 -08002082 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002083 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 break;
2085
2086 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002087 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002088 stopLoading();
2089 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002090 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 getCurrentTopWebView().reload();
2092 }
2093 break;
2094
Michael Kolb8233fac2010-10-26 16:08:53 -07002095 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002096 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002097 break;
2098
2099 case R.id.close_menu_id:
2100 // Close the subwindow if it exists.
2101 if (mTabControl.getCurrentSubWindow() != null) {
2102 dismissSubWindow(mTabControl.getCurrentTab());
2103 break;
2104 }
2105 closeCurrentTab();
2106 break;
2107
kaiyiza8b6dbb2013-07-29 18:11:22 +08002108 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002109 Object[] params = { new String("persist.debug.browsermonkeytest")};
2110 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002111 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002112 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002113 if (ret != null && ret.equals("enable"))
2114 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002115 if (BrowserConfig.getInstance(getContext())
2116 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2117 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002118 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002119 case R.id.homepage_menu_id:
2120 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002121 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002122 break;
2123
2124 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002125 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 break;
2127
2128 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002129 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002130 break;
2131
John Reck2bc80422011-06-30 15:11:49 -07002132 case R.id.save_snapshot_menu_id:
2133 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002134 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002135 createScreenshotAsync(
2136 source.getWebView(),
2137 getDesiredThumbnailWidth(mActivity),
2138 getDesiredThumbnailHeight(mActivity),
2139 new ValueCallback<Bitmap>() {
2140 @Override
2141 public void onReceiveValue(Bitmap bitmap) {
2142 new SaveSnapshotTask(source, bitmap).execute();
2143 }
2144 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002145 break;
2146
Michael Kolb8233fac2010-10-26 16:08:53 -07002147 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002148 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 break;
2150
John Recke1a03a32011-09-14 17:04:16 -07002151 case R.id.snapshot_go_live:
2152 goLive();
2153 return true;
2154
Michael Kolb8233fac2010-10-26 16:08:53 -07002155 case R.id.share_page_menu_id:
2156 Tab currentTab = mTabControl.getCurrentTab();
2157 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002158 return false;
2159 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002160 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002161 break;
2162
2163 case R.id.dump_nav_menu_id:
2164 getCurrentTopWebView().debugDump();
2165 break;
2166
Michael Kolb8233fac2010-10-26 16:08:53 -07002167 case R.id.zoom_in_menu_id:
2168 getCurrentTopWebView().zoomIn();
2169 break;
2170
2171 case R.id.zoom_out_menu_id:
2172 getCurrentTopWebView().zoomOut();
2173 break;
2174
2175 case R.id.view_downloads_menu_id:
2176 viewDownloads();
2177 break;
2178
John Reck42229bc2011-08-19 13:26:43 -07002179 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002180 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002181 break;
2182
Michael Kolb8233fac2010-10-26 16:08:53 -07002183 case R.id.window_one_menu_id:
2184 case R.id.window_two_menu_id:
2185 case R.id.window_three_menu_id:
2186 case R.id.window_four_menu_id:
2187 case R.id.window_five_menu_id:
2188 case R.id.window_six_menu_id:
2189 case R.id.window_seven_menu_id:
2190 case R.id.window_eight_menu_id:
2191 {
2192 int menuid = item.getItemId();
2193 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2194 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2195 Tab desiredTab = mTabControl.getTab(id);
2196 if (desiredTab != null &&
2197 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002198 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002199 }
2200 break;
2201 }
2202 }
2203 }
2204 break;
2205
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002206 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002207 Bundle bundle = new Bundle();
2208 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2209 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2210 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2211 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2212 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002213 break;
2214
Pankaj Garg49b79252014-11-07 17:33:41 -08002215 case R.id.add_to_homescreen:
2216 final WebView w = getCurrentTopWebView();
2217 final EditText input = new EditText(getContext());
2218 input.setText(w.getTitle());
2219 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002220 .setTitle(getContext().getResources().getString(
2221 R.string.add_to_homescreen))
2222 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002223 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002224 .setPositiveButton(getContext().getResources().getString(
2225 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002226 public void onClick(DialogInterface dialog, int whichButton) {
2227 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2228 getContext(),
2229 w.getUrl(),
2230 input.getText().toString(),
2231 w.getViewportBitmap(),
2232 w.getFavicon()));
2233
2234 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2235 .addCategory(Intent.CATEGORY_HOME));
2236 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002237 .setNegativeButton(getContext().getResources().getString(
2238 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002239 public void onClick(DialogInterface dialog, int whichButton) {
2240 // Do nothing.
2241 }
2242 })
2243 .show();
2244 break;
2245
Michael Kolb8233fac2010-10-26 16:08:53 -07002246 default:
2247 return false;
2248 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002249 return true;
2250 }
2251
John Reck68234a92012-04-19 15:27:12 -07002252 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2253 implements OnCancelListener {
2254
2255 private Tab mTab;
2256 private Dialog mProgressDialog;
2257 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002258 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002259
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002260 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002261 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002262 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002263 }
2264
2265 @Override
2266 protected void onPreExecute() {
2267 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2268 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2269 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002270 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002271 }
2272
2273 @Override
2274 protected Long doInBackground(Void... params) {
2275 if (!mTab.saveViewState(mValues)) {
2276 return null;
2277 }
2278 if (isCancelled()) {
2279 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2280 File file = mActivity.getFileStreamPath(path);
2281 if (!file.delete()) {
2282 file.deleteOnExit();
2283 }
2284 return null;
2285 }
2286 final ContentResolver cr = mActivity.getContentResolver();
2287 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2288 if (result == null) {
2289 return null;
2290 }
2291 long id = ContentUris.parseId(result);
2292 return id;
2293 }
2294
2295 @Override
2296 protected void onPostExecute(Long id) {
2297 if (isCancelled()) {
2298 return;
2299 }
2300 mProgressDialog.dismiss();
2301 if (id == null) {
2302 Toast.makeText(mActivity, R.string.snapshot_failed,
2303 Toast.LENGTH_SHORT).show();
2304 return;
2305 }
2306 Bundle b = new Bundle();
2307 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2308 mUi.showComboView(ComboViews.Snapshots, b);
2309 }
2310
2311 @Override
2312 public void onCancel(DialogInterface dialog) {
2313 cancel(true);
2314 }
2315 }
2316
Michael Kolb80f75082012-04-10 10:50:06 -07002317 @Override
2318 public void toggleUserAgent() {
2319 WebView web = getCurrentWebView();
2320 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002321 }
2322
2323 @Override
2324 public void findOnPage() {
2325 getCurrentTopWebView().showFindDialog(null, true);
2326 }
2327
2328 @Override
2329 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002330 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002331 }
2332
Pankaj Gargeba076f2015-03-25 13:40:59 -07002333 // This function is specifically used from AddBookmark Activity.
2334 // The bookmark activity clears the bitmap after retrieving it.
2335 // The function usage elsewhere will result in breaking bookmark
2336 // functionality.
2337 public static Bitmap getAndReleaseLastBookmarkBitmapFromIntent() {
2338 Bitmap bitmap = mBookmarkBitmap;
2339 mBookmarkBitmap = null;
2340 return bitmap;
2341 }
2342
Michael Kolb80f75082012-04-10 10:50:06 -07002343 @Override
2344 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002345 WebView w = getCurrentTopWebView();
2346 if (w == null)
2347 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002348 final Intent i = createBookmarkCurrentPageIntent(false);
2349 createScreenshotAsync(
2350 w, getDesiredThumbnailWidth(mActivity),
2351 getDesiredThumbnailHeight(mActivity),
2352 new ValueCallback<Bitmap>() {
2353 @Override
2354 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargeba076f2015-03-25 13:40:59 -07002355 mBookmarkBitmap = bitmap;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002356 mActivity.startActivity(i);
2357 }
2358 });
Michael Kolb80f75082012-04-10 10:50:06 -07002359 }
2360
John Recke1a03a32011-09-14 17:04:16 -07002361 private void goLive() {
Axesh R. Ajmera89cf4d82015-05-19 11:26:18 -07002362 if (!getCurrentTab().isSnapshot()) return;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002363 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002364 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002365 boolean onlySingleTabRemaining = false;
2366 if (mTabControl.getTabCount() > 1) {
2367 // destroy the old snapshot tab
2368 closeCurrentTab();
2369 } else {
2370 onlySingleTabRemaining = true;
2371 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002372 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002373 if (onlySingleTabRemaining) {
2374 closeTab(t);
2375 }
2376
Tarun Nainani8eb00912014-07-17 12:28:32 -07002377 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002378 }
2379
luxiaolb40014b2013-07-19 10:01:43 +08002380 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002381 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002382 new AlertDialog.Builder(activity)
2383 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002384 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002385 .setMessage(R.string.exit_browser_msg)
2386 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2387 public void onClick(DialogInterface dialog, int which) {
2388 activity.moveTaskToBack(true);
2389 dialog.dismiss();
2390 }
2391 })
2392 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2393 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002394 mCrashRecoveryHandler.clearState(true);
2395 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002396 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002397 dialog.dismiss();
2398 }
2399 })
2400 .show();
2401 }
Michael Kolb315d5022011-10-13 12:47:11 -07002402 @Override
2403 public void showPageInfo() {
2404 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2405 }
2406
John Reck9c35b9c2012-05-30 10:08:50 -07002407 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002408 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002409 // Let the History and Bookmark fragments handle menus they created.
2410 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2411 return false;
2412 }
2413
Michael Kolb8233fac2010-10-26 16:08:53 -07002414 int id = item.getItemId();
2415 boolean result = true;
2416 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002417 // -- Browser context menu
2418 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002419 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002420 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002421 case R.id.copy_link_context_menu_id:
2422 final WebView webView = getCurrentTopWebView();
2423 if (null == webView) {
2424 result = false;
2425 break;
2426 }
2427 final HashMap<String, WebView> hrefMap =
2428 new HashMap<String, WebView>();
2429 hrefMap.put("webview", webView);
2430 final Message msg = mHandler.obtainMessage(
2431 FOCUS_NODE_HREF, id, 0, hrefMap);
2432 webView.requestFocusNodeHref(msg);
2433 break;
2434
2435 default:
2436 // For other context menus
2437 result = onOptionsItemSelected(item);
2438 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002439 return result;
2440 }
2441
2442 /**
2443 * support programmatically opening the context menu
2444 */
2445 public void openContextMenu(View view) {
2446 mActivity.openContextMenu(view);
2447 }
2448
2449 /**
2450 * programmatically open the options menu
2451 */
2452 public void openOptionsMenu() {
2453 mActivity.openOptionsMenu();
2454 }
2455
John Reck9c35b9c2012-05-30 10:08:50 -07002456 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002457 public boolean onMenuOpened(int featureId, Menu menu) {
2458 if (mOptionsMenuOpen) {
2459 if (mConfigChanged) {
2460 // We do not need to make any changes to the state of the
2461 // title bar, since the only thing that happened was a
2462 // change in orientation
2463 mConfigChanged = false;
2464 } else {
2465 if (!mExtendedMenuOpen) {
2466 mExtendedMenuOpen = true;
2467 mUi.onExtendedMenuOpened();
2468 } else {
2469 // Switching the menu back to icon view, so show the
2470 // title bar once again.
2471 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002472 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002473 }
2474 }
2475 } else {
2476 // The options menu is closed, so open it, and show the title
2477 mOptionsMenuOpen = true;
2478 mConfigChanged = false;
2479 mExtendedMenuOpen = false;
2480 mUi.onOptionsMenuOpened();
2481 }
2482 return true;
2483 }
2484
John Reck9c35b9c2012-05-30 10:08:50 -07002485 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002486 public void onOptionsMenuClosed(Menu menu) {
2487 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002488 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002489 }
2490
John Reck9c35b9c2012-05-30 10:08:50 -07002491 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002492 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002493 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002494 }
2495
2496 // Helper method for getting the top window.
2497 @Override
2498 public WebView getCurrentTopWebView() {
2499 return mTabControl.getCurrentTopWebView();
2500 }
2501
2502 @Override
2503 public WebView getCurrentWebView() {
2504 return mTabControl.getCurrentWebView();
2505 }
2506
2507 /*
2508 * This method is called as a result of the user selecting the options
2509 * menu to see the download window. It shows the download window on top of
2510 * the current window.
2511 */
2512 void viewDownloads() {
2513 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2514 mActivity.startActivity(intent);
2515 }
2516
John Reck30b065e2011-07-19 10:58:05 -07002517 int getActionModeHeight() {
2518 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2519 new int[] { android.R.attr.actionBarSize });
2520 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2521 actionBarSizeTypedArray.recycle();
2522 return size;
2523 }
2524
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 // action mode
2526
John Reck9c35b9c2012-05-30 10:08:50 -07002527 @Override
2528 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002529 mUi.onActionModeStarted(mode);
2530 mActionMode = mode;
2531 }
2532
2533 /*
2534 * True if a custom ActionMode (i.e. find or select) is in use.
2535 */
2536 @Override
2537 public boolean isInCustomActionMode() {
2538 return mActionMode != null;
2539 }
2540
2541 /*
2542 * End the current ActionMode.
2543 */
2544 @Override
2545 public void endActionMode() {
2546 if (mActionMode != null) {
2547 mActionMode.finish();
2548 }
2549 }
2550
2551 /*
2552 * Called by find and select when they are finished. Replace title bars
2553 * as necessary.
2554 */
John Reck9c35b9c2012-05-30 10:08:50 -07002555 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002556 public void onActionModeFinished(ActionMode mode) {
2557 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002558 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002559 mActionMode = null;
2560 }
2561
2562 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002563 final Tab tab = getCurrentTab();
2564 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002565 }
2566
2567 // bookmark handling
2568
2569 /**
2570 * add the current page as a bookmark to the given folder id
2571 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002572 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002573 * bookmarked, and if it is, edit that bookmark. If false, and
2574 * the site is already bookmarked, do not attempt to edit the
2575 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002576 */
2577 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002578 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002579 WebView w = getCurrentTopWebView();
2580 if (w == null) {
2581 return null;
2582 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002583 Intent i = new Intent(mActivity,
2584 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002585 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2586 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2587 String touchIconUrl = w.getTouchIconUrl();
2588 if (touchIconUrl != null) {
2589 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2590 WebSettings settings = w.getSettings();
2591 if (settings != null) {
2592 i.putExtra(AddBookmarkPage.USER_AGENT,
2593 settings.getUserAgentString());
2594 }
2595 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002596 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002597 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002598 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002599 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2600 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002601 // Put the dialog at the upper right of the screen, covering the
2602 // star on the title bar.
2603 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002604 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 }
2606
2607 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002608 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002609 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002610 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002611 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 }
2613
Vivek Sekharb54614f2014-05-01 19:03:37 -07002614 @Override
2615 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2616 boolean capture) {
2617 mUploadHandler = new UploadHandler(this);
2618 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2619 }
2620
Michael Kolb8233fac2010-10-26 16:08:53 -07002621 // thumbnails
2622
2623 /**
2624 * Return the desired width for thumbnail screenshots, which are stored in
2625 * the database, and used on the bookmarks screen.
2626 * @param context Context for finding out the density of the screen.
2627 * @return desired width for thumbnail screenshot.
2628 */
2629 static int getDesiredThumbnailWidth(Context context) {
2630 return context.getResources().getDimensionPixelOffset(
2631 R.dimen.bookmarkThumbnailWidth);
2632 }
2633
2634 /**
2635 * Return the desired height for thumbnail screenshots, which are stored in
2636 * the database, and used on the bookmarks screen.
2637 * @param context Context for finding out the density of the screen.
2638 * @return desired height for thumbnail screenshot.
2639 */
2640 static int getDesiredThumbnailHeight(Context context) {
2641 return context.getResources().getDimensionPixelOffset(
2642 R.dimen.bookmarkThumbnailHeight);
2643 }
2644
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002645 static void createScreenshotAsync(WebView view, int width, int height,
2646 final ValueCallback<Bitmap> cb) {
2647 if (view == null || width == 0 || height == 0) {
2648 return;
2649 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002650 view.getContentBitmapAsync(
2651 (float) width / view.getWidth(),
2652 new Rect(),
2653 new ValueCallback<Bitmap>() {
2654 @Override
2655 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002656 if (bitmap != null)
2657 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2658 cb.onReceiveValue(bitmap);
2659 }});
2660 }
2661
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002662 private void updateScreenshot(final Tab tab) {
2663 createScreenshotAsync(
2664 tab.getWebView(),
2665 getDesiredThumbnailWidth(mActivity),
2666 getDesiredThumbnailHeight(mActivity),
2667 new ValueCallback<Bitmap>() {
2668 @Override
2669 public void onReceiveValue(Bitmap bitmap) {
2670 updateScreenshot(tab, bitmap);
2671 }
2672 });
2673 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002674
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002675 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002676 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002677 // FIXME: Would like to make sure there is actually something to
2678 // draw, but the API for that (WebViewCore.pictureReady()) is not
2679 // currently accessible here.
2680
John Reck34ef2672011-02-10 11:30:55 -08002681 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002682 if (view == null) {
2683 // Tab was destroyed
2684 return;
2685 }
John Reck34ef2672011-02-10 11:30:55 -08002686 final String url = tab.getUrl();
2687 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002688 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002689 if (TextUtils.isEmpty(url)) {
2690 return;
2691 }
2692
kaiyiz6e5b3e02013-08-19 20:02:01 +08002693 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002694 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002695 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002696 }
John Reck34ef2672011-02-10 11:30:55 -08002697 // Only update thumbnails for web urls (http(s)://), not for
2698 // about:, javascript:, data:, etc...
2699 // Unless it is a bookmarked site, then always update
2700 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2701 return;
2702 }
2703
kaiyiz6e5b3e02013-08-19 20:02:01 +08002704 if (url != null && mUpdateMyNavThumbnailUrl != null
2705 && Patterns.WEB_URL.matcher(url).matches()
2706 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2707 String urlHost = (new WebAddress(url)).getHost();
2708 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2709 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2710 || bookmarkHost.length() == 0) {
2711 return;
2712 }
2713 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2714 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2715 bookmarkHost.length());
2716 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2717 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2718 if (!bookmarkDomain.equals(urlDomain)) {
2719 return;
2720 }
2721 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002722 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002723 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2724 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2725 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2726 500);
2727 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002728 return;
2729 }
2730
2731 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002732 new AsyncTask<Void, Void, Void>() {
2733 @Override
2734 protected Void doInBackground(Void... unused) {
2735 Cursor cursor = null;
2736 try {
2737 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002738 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2739 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2740 : url);
2741 if (mUpdateMyNavThumbnail) {
2742 mUpdateMyNavThumbnail = false;
2743 mUpdateMyNavThumbnailUrl = null;
2744 }
John Reck34ef2672011-02-10 11:30:55 -08002745 if (cursor != null && cursor.moveToFirst()) {
2746 final ByteArrayOutputStream os =
2747 new ByteArrayOutputStream();
2748 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002749
John Reck34ef2672011-02-10 11:30:55 -08002750 ContentValues values = new ContentValues();
2751 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002752
John Reck34ef2672011-02-10 11:30:55 -08002753 do {
John Reck617fd832011-02-16 14:35:59 -08002754 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002755 cr.update(Images.CONTENT_URI, values, null, null);
2756 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002757 }
John Reck34ef2672011-02-10 11:30:55 -08002758 } catch (IllegalStateException e) {
2759 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002760 } catch (SQLiteException s) {
2761 // Added for possible error when user tries to remove the same bookmark
2762 // that is being updated with a screen shot
2763 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002764 } finally {
2765 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002766 }
John Reck34ef2672011-02-10 11:30:55 -08002767 return null;
2768 }
2769 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002770 }
2771
2772 private class Copy implements OnMenuItemClickListener {
2773 private CharSequence mText;
2774
John Reck9c35b9c2012-05-30 10:08:50 -07002775 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002776 public boolean onMenuItemClick(MenuItem item) {
2777 copy(mText);
2778 return true;
2779 }
2780
2781 public Copy(CharSequence toCopy) {
2782 mText = toCopy;
2783 }
2784 }
2785
Leon Scroggins63c02662010-11-18 15:16:27 -05002786 private static class Download implements OnMenuItemClickListener {
2787 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002788 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002789 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002790 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002791 private static final String FALLBACK_EXTENSION = "dat";
2792 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002793
John Reck9c35b9c2012-05-30 10:08:50 -07002794 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002795 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002796 if (DataUri.isDataUri(mText)) {
2797 saveDataUri();
2798 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002799 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002800 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002801 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002802 return true;
2803 }
2804
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002805 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2806 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002807 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002808 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002809 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002810 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002811 }
George Mount387d45d2011-10-07 15:57:53 -07002812
2813 /**
2814 * Treats mText as a data URI and writes its contents to a file
2815 * based on the current time.
2816 */
2817 private void saveDataUri() {
2818 FileOutputStream outputStream = null;
2819 try {
2820 DataUri uri = new DataUri(mText);
2821 File target = getTarget(uri);
2822 outputStream = new FileOutputStream(target);
2823 outputStream.write(uri.getData());
2824 final DownloadManager manager =
2825 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2826 manager.addCompletedDownload(target.getName(),
2827 mActivity.getTitle().toString(), false,
2828 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002829 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002830 } catch (IOException e) {
2831 Log.e(LOGTAG, "Could not save data URL");
2832 } finally {
2833 if (outputStream != null) {
2834 try {
2835 outputStream.close();
2836 } catch (IOException e) {
2837 // ignore close errors
2838 }
2839 }
2840 }
2841 }
2842
2843 /**
2844 * Creates a File based on the current time stamp and uses
2845 * the mime type of the DataUri to get the extension.
2846 */
2847 private File getTarget(DataUri uri) throws IOException {
2848 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002849 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002850 String nameBase = format.format(new Date());
2851 String mimeType = uri.getMimeType();
2852 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2853 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2854 if (extension == null) {
2855 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2856 extension = FALLBACK_EXTENSION;
2857 }
2858 extension = "." + extension; // createTempFile needs the '.'
2859 File targetFile = File.createTempFile(nameBase, extension, dir);
2860 return targetFile;
2861 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002862 }
2863
Cary Clark8974d282010-11-22 10:46:05 -05002864 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002865 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002866
John Reck9c35b9c2012-05-30 10:08:50 -07002867 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002868 public boolean onMenuItemClick(MenuItem item) {
2869 if (mWebView != null) {
2870 return mWebView.selectText();
2871 }
2872 return false;
2873 }
2874
2875 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002876 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002877 }
2878
2879 }
2880
Michael Kolb8233fac2010-10-26 16:08:53 -07002881 /********************** TODO: UI stuff *****************************/
2882
2883 // these methods have been copied, they still need to be cleaned up
2884
2885 /****************** tabs ***************************************************/
2886
2887 // basic tab interactions:
2888
2889 // it is assumed that tabcontrol already knows about the tab
2890 protected void addTab(Tab tab) {
2891 mUi.addTab(tab);
2892 }
2893
2894 protected void removeTab(Tab tab) {
2895 mUi.removeTab(tab);
2896 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002897 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002898 }
2899
Michael Kolbf2055602011-04-09 17:20:03 -07002900 @Override
2901 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002902 // monkey protection against delayed start
2903 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002904
2905 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2906 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002907 mTabControl.setCurrentTab(tab);
2908 // the tab is guaranteed to have a webview after setCurrentTab
2909 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002910
2911
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002912 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002913 //Purge active tabs
2914 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002915 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002916 }
2917
John Reck8bcafc12011-08-29 16:43:02 -07002918 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002919 Tab current = mTabControl.getCurrentTab();
2920 if (current != null
2921 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002922 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002923 }
2924 }
2925
John Reck26b18322011-06-21 13:08:58 -07002926 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002927 //Cancel navscreen request
2928 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002929 // Dismiss the subwindow if applicable.
2930 dismissSubWindow(appTab);
2931 // Since we might kill the WebView, remove it from the
2932 // content view first.
2933 mUi.detachTab(appTab);
2934 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002935 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002936 // TODO: analyze why the remove and add are necessary
2937 mUi.attachTab(appTab);
2938 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002939 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002940 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002941 } else {
2942 // If the tab was the current tab, we have to attach
2943 // it to the view system again.
2944 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002945 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002946 }
2947 }
2948
2949 // Remove the sub window if it exists. Also called by TabControl when the
2950 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002951 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002952 public void dismissSubWindow(Tab tab) {
2953 removeSubWindow(tab);
2954 // dismiss the subwindow. This will destroy the WebView.
2955 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002956 WebView wv = getCurrentTopWebView();
2957 if (wv != null) {
2958 wv.requestFocus();
2959 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002960 }
2961
2962 @Override
2963 public void removeSubWindow(Tab t) {
2964 if (t.getSubWebView() != null) {
2965 mUi.removeSubWindow(t.getSubViewContainer());
2966 }
2967 }
2968
2969 @Override
2970 public void attachSubWindow(Tab tab) {
2971 if (tab.getSubWebView() != null) {
2972 mUi.attachSubWindow(tab.getSubViewContainer());
2973 getCurrentTopWebView().requestFocus();
2974 }
2975 }
2976
Mathew Inwood29721c22011-06-29 17:55:24 +01002977 private Tab showPreloadedTab(final UrlData urlData) {
2978 if (!urlData.isPreloaded()) {
2979 return null;
2980 }
2981 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2982 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2983 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002984 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002985 // Could not submit query. Fallback to regular tab creation
2986 tabControl.destroy();
2987 return null;
2988 }
2989 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002990 // check tab count and make room for new tab
2991 if (!mTabControl.canCreateNewTab()) {
2992 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2993 if (leastUsed != null) {
2994 closeTab(leastUsed);
2995 }
2996 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002997 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002998 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002999 mTabControl.addPreloadedTab(t);
3000 addTab(t);
3001 setActiveTab(t);
3002 return t;
3003 }
3004
Michael Kolb7bcafde2011-05-09 13:55:59 -07003005 // open a non inconito tab with the given url data
3006 // and set as active tab
3007 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01003008 Tab tab = showPreloadedTab(urlData);
3009 if (tab == null) {
3010 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07003011 if ((tab != null) && !urlData.isEmpty()) {
3012 loadUrlDataIn(tab, urlData);
3013 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003014 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003015 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07003016 }
3017
Michael Kolb843510f2010-12-09 10:51:49 -08003018 @Override
3019 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003020 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07003021 }
3022
Michael Kolb8233fac2010-10-26 16:08:53 -07003023 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07003024 public Tab openIncognitoTab() {
3025 return openTab(INCOGNITO_URI, true, true, false);
3026 }
3027
3028 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003029 public Tab openTab(String url, boolean incognito, boolean setActive,
3030 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003031 return openTab(url, incognito, setActive, useCurrent, null);
3032 }
3033
3034 @Override
3035 public Tab openTab(String url, Tab parent, boolean setActive,
3036 boolean useCurrent) {
3037 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3038 setActive, useCurrent, parent);
3039 }
3040
3041 public Tab openTab(String url, boolean incognito, boolean setActive,
3042 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003043 Tab tab = createNewTab(incognito, setActive, useCurrent);
3044 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003045 if (parent instanceof SnapshotTab) {
3046 addTab(tab);
3047 if (setActive)
3048 setActiveTab(tab);
3049 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003050 parent.addChildTab(tab);
3051 }
Michael Kolb519d2282011-05-09 17:03:19 -07003052 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003053 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003054 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003055 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003056 return tab;
3057 }
3058
3059 // this method will attempt to create a new tab
3060 // incognito: private browsing tab
3061 // setActive: ste tab as current tab
3062 // useCurrent: if no new tab can be created, return current tab
3063 private Tab createNewTab(boolean incognito, boolean setActive,
3064 boolean useCurrent) {
3065 Tab tab = null;
3066 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05003067 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003068 addTab(tab);
3069 if (setActive) {
3070 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07003071 } else {
3072 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003073 }
3074 } else {
3075 if (useCurrent) {
3076 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07003077 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003078 } else {
3079 mUi.showMaxTabsWarning();
3080 }
3081 }
3082 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003083 }
3084
John Reck2bc80422011-06-30 15:11:49 -07003085 @Override
3086 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3087 SnapshotTab tab = null;
3088 if (mTabControl.canCreateNewTab()) {
3089 tab = mTabControl.createSnapshotTab(snapshotId);
3090 addTab(tab);
3091 if (setActive) {
3092 setActiveTab(tab);
3093 }
3094 } else {
3095 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003096 }
3097 return tab;
3098 }
3099
Michael Kolb8233fac2010-10-26 16:08:53 -07003100 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003101 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003102 * @return boolean True if we successfully switched to a different tab. If
3103 * the indexth tab is null, or if that tab is the same as
3104 * the current one, return false.
3105 */
3106 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003107 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003108 Tab currentTab = mTabControl.getCurrentTab();
3109 if (tab == null || tab == currentTab) {
3110 return false;
3111 }
3112 setActiveTab(tab);
3113 return true;
3114 }
3115
3116 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003117 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003118 closeCurrentTab(false);
3119 }
3120
3121 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003122 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003123 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003124 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003125 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003126 return;
3127 }
Michael Kolbc831b632011-05-11 09:30:34 -07003128 final Tab current = mTabControl.getCurrentTab();
3129 final int pos = mTabControl.getCurrentPosition();
3130 Tab newTab = current.getParent();
3131 if (newTab == null) {
3132 newTab = mTabControl.getTab(pos + 1);
3133 if (newTab == null) {
3134 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003135 }
3136 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003137 if (andQuit) {
3138 mTabControl.setCurrentTab(newTab);
3139 closeTab(current);
3140 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003141 // Close window
3142 closeTab(current);
3143 }
3144 }
3145
3146 /**
3147 * Close the tab, remove its associated title bar, and adjust mTabControl's
3148 * current tab to a valid value.
3149 */
3150 @Override
3151 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003152 if (tab == mTabControl.getCurrentTab()) {
3153 closeCurrentTab();
3154 } else {
3155 removeTab(tab);
3156 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 }
3158
Afzal Najamd4e33312012-04-26 01:54:01 -04003159 /**
3160 * Close all tabs except the current one
3161 */
3162 @Override
3163 public void closeOtherTabs() {
3164 int inactiveTabs = mTabControl.getTabCount() - 1;
3165 for (int i = inactiveTabs; i >= 0; i--) {
3166 Tab tab = mTabControl.getTab(i);
3167 if (tab != mTabControl.getCurrentTab()) {
3168 removeTab(tab);
3169 }
3170 }
3171 }
3172
Michael Kolb8233fac2010-10-26 16:08:53 -07003173 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003174 protected void loadUrlFromContext(String url) {
3175 Tab tab = getCurrentTab();
3176 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003177 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003178 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003179 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003180 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003181 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003182 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003183 }
3184 }
3185 }
3186
3187 /**
3188 * Load the URL into the given WebView and update the title bar
3189 * to reflect the new load. Call this instead of WebView.loadUrl
3190 * directly.
3191 * @param view The WebView used to load url.
3192 * @param url The URL to load.
3193 */
John Reck71e51422011-07-01 16:49:28 -07003194 @Override
3195 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003196 loadUrl(tab, url, null);
3197 }
3198
3199 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3200 if (tab != null) {
3201 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003202 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003203 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003204 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003205 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003206 }
3207
3208 /**
3209 * Load UrlData into a Tab and update the title bar to reflect the new
3210 * load. Call this instead of UrlData.loadIn directly.
3211 * @param t The Tab used to load.
3212 * @param data The UrlData being loaded.
3213 */
3214 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003215 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003216 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003217 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003218 } else {
John Reck38b39652012-06-05 09:22:59 -07003219 if (t != null && data.mDisableUrlOverride) {
3220 t.disableUrlOverridingForLoad();
3221 }
John Reck26b18322011-06-21 13:08:58 -07003222 loadUrl(t, data.mUrl, data.mHeaders);
3223 }
3224 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003225 }
3226
John Reck30c714c2010-12-16 17:30:34 -08003227 @Override
3228 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003229 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003230 //In case of tab can go back (aka tab has navigation entry) do nothing
3231 //else just load homepage in current tab.
3232 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003233 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003234 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003235 }
3236
3237 void goBackOnePageOrQuit() {
3238 Tab current = mTabControl.getCurrentTab();
3239 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003240 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3241 showExitDialog(mActivity);
3242 } else {
3243 /*
3244 * Instead of finishing the activity, simply push this to the back
3245 * of the stack and let ActivityManager to choose the foreground
3246 * activity. As BrowserActivity is singleTask, it will be always the
3247 * root of the task. So we can use either true or false for
3248 * moveTaskToBack().
3249 */
3250 mActivity.moveTaskToBack(true);
3251 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003252 return;
3253 }
John Reckef654f12011-07-12 16:42:08 -07003254 if (current.canGoBack()) {
3255 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003256 } else {
3257 // Check to see if we are closing a window that was created by
3258 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003259 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003260 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003261 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003262 // Now we close the other tab
3263 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003264 } else if (BrowserConfig.getInstance(getContext())
3265 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3266 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003267 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003268 if ((current.getAppId() != null) || current.closeOnBack()) {
3269 closeCurrentTab(true);
3270 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003271 /*
3272 * Instead of finishing the activity, simply push this to the back
3273 * of the stack and let ActivityManager to choose the foreground
3274 * activity. As BrowserActivity is singleTask, it will be always the
3275 * root of the task. So we can use either true or false for
3276 * moveTaskToBack().
3277 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003278 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003279 }
3280 }
3281 }
3282
3283 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003284 * helper method for key handler
3285 * returns the current tab if it can't advance
3286 */
Michael Kolbc831b632011-05-11 09:30:34 -07003287 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003288 int pos = mTabControl.getCurrentPosition() + 1;
3289 if (pos >= mTabControl.getTabCount()) {
3290 pos = 0;
3291 }
3292 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003293 }
3294
3295 /**
3296 * helper method for key handler
3297 * returns the current tab if it can't advance
3298 */
Michael Kolbc831b632011-05-11 09:30:34 -07003299 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003300 int pos = mTabControl.getCurrentPosition() - 1;
3301 if ( pos < 0) {
3302 pos = mTabControl.getTabCount() - 1;
3303 }
3304 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003305 }
3306
John Reckbcef87f2012-02-03 14:58:34 -08003307 boolean isMenuOrCtrlKey(int keyCode) {
3308 return (KeyEvent.KEYCODE_MENU == keyCode)
3309 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3310 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3311 }
3312
Michael Kolb0035fad2011-03-14 13:25:28 -07003313 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003314 * handle key events in browser
3315 *
3316 * @param keyCode
3317 * @param event
3318 * @return true if handled, false to pass to super
3319 */
John Reck9c35b9c2012-05-30 10:08:50 -07003320 @Override
3321 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003322 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3323 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003324 if (!mUi.isComboViewShowing()) {
3325 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3326 true, false);
3327 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003328 return true;
3329 }
3330
Cary Clark160bbb92011-01-10 11:17:07 -05003331 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003332 // Even if MENU is already held down, we need to call to super to open
3333 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003334 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003335 mMenuIsDown = true;
3336 return false;
3337 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003338
Cary Clark8ff8c662010-12-29 15:03:05 -05003339 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003340 Tab tab = getCurrentTab();
3341 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003342
Cary Clark160bbb92011-01-10 11:17:07 -05003343 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3344 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003345
Michael Kolb8233fac2010-10-26 16:08:53 -07003346 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003347 case KeyEvent.KEYCODE_TAB:
3348 if (event.isCtrlPressed()) {
3349 if (event.isShiftPressed()) {
3350 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003351 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003352 } else {
3353 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003354 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003355 }
3356 return true;
3357 }
3358 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003359 case KeyEvent.KEYCODE_SPACE:
3360 // WebView/WebTextView handle the keys in the KeyDown. As
3361 // the Activity's shortcut keys are only handled when WebView
3362 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003363 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003364 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003365 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003366 pageDown();
3367 }
3368 return true;
3369 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003370 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003371 event.startTracking();
3372 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003373 case KeyEvent.KEYCODE_FORWARD:
3374 if (!noModifiers) break;
3375 tab.goForward();
3376 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003377 case KeyEvent.KEYCODE_DPAD_LEFT:
3378 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003379 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003380 return true;
3381 }
3382 break;
3383 case KeyEvent.KEYCODE_DPAD_RIGHT:
3384 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003385 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003386 return true;
3387 }
3388 break;
3389 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003390 if (ctrl) {
3391 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003392 return true;
3393 }
3394 break;
Michael Kolba4183062011-01-16 10:43:21 -08003395// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003396 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003397 if (ctrl ) {
3398 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003399 return true;
3400 }
3401 break;
Michael Kolba4183062011-01-16 10:43:21 -08003402// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003403// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003404// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003405// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003406// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003407// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003408// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003409// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003410// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003411// case KeyEvent.KEYCODE_M: // unused
3412// case KeyEvent.KEYCODE_N: // in Chrome: new window
3413// case KeyEvent.KEYCODE_O: // in Chrome: open file
3414// case KeyEvent.KEYCODE_P: // in Chrome: print page
3415// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003416// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003417// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3418 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003419 // we can't use the ctrl/shift flags, they check for
3420 // exclusive use of a modifier
3421 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003422 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003423 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003424 } else {
3425 openTabToHomePage();
3426 }
3427 return true;
3428 }
3429 break;
3430// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3431// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003432// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003433// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3434// case KeyEvent.KEYCODE_Y: // unused
3435// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003436 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003437 // it is a regular key and webview is not null
3438 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003439 }
3440
John Reck9c35b9c2012-05-30 10:08:50 -07003441 @Override
3442 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003443 switch(keyCode) {
3444 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003445 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003446 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003447 return true;
3448 }
3449 break;
3450 }
3451 return false;
3452 }
3453
John Reck9c35b9c2012-05-30 10:08:50 -07003454 @Override
3455 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003456 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003457 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003458 if (KeyEvent.KEYCODE_MENU == keyCode
3459 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003460 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003461 }
3462 }
Cary Clark160bbb92011-01-10 11:17:07 -05003463 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003464 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003465 case KeyEvent.KEYCODE_BACK:
3466 if (event.isTracking() && !event.isCanceled()) {
3467 onBackKey();
3468 return true;
3469 }
3470 break;
3471 }
3472 return false;
3473 }
3474
3475 public boolean isMenuDown() {
3476 return mMenuIsDown;
3477 }
3478
John Reck9c35b9c2012-05-30 10:08:50 -07003479 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003480 public void setupAutoFill(Message message) {
3481 // Open the settings activity at the AutoFill profile fragment so that
3482 // the user can create a new profile. When they return, we will dispatch
3483 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003484 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003485 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3486 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003487 }
John Reckb3417f02011-01-14 11:01:05 -08003488
John Reck9c35b9c2012-05-30 10:08:50 -07003489 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003490 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003491 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003492 return true;
3493 }
3494
John Reck1cf4b792011-07-26 10:22:22 -07003495 @Override
3496 public boolean shouldCaptureThumbnails() {
3497 return mUi.shouldCaptureThumbnails();
3498 }
3499
Michael Kolbc3af0672011-08-09 10:24:41 -07003500 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003501 public boolean supportsVoice() {
3502 PackageManager pm = mActivity.getPackageManager();
3503 List activities = pm.queryIntentActivities(new Intent(
3504 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3505 return activities.size() != 0;
3506 }
3507
3508 @Override
3509 public void startVoiceRecognizer() {
3510 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003511 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003512 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3513 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3514 mActivity.startActivityForResult(voice, VOICE_RESULT);
3515 }
3516
Pankaj Garg7b279f62014-08-12 14:47:18 -07003517 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003518 if (mLevel == level)
3519 return;
3520 mLevel = level;
3521 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003522 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3523 lp.dimAmount = level;
3524 mActivity.getWindow().setAttributes(lp);
3525 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3526 } else {
3527 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3528 }
3529 }
3530
Michael Kolb0b129122012-06-04 16:31:58 -07003531 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003532 public void setBlockEvents(boolean block) {
3533 mBlockEvents = block;
3534 }
3535
John Reck9c35b9c2012-05-30 10:08:50 -07003536 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003537 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003538 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003539 }
3540
John Reck9c35b9c2012-05-30 10:08:50 -07003541 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003542 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003543 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003544 }
3545
John Reck9c35b9c2012-05-30 10:08:50 -07003546 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003547 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003548 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003549 }
3550
John Reck9c35b9c2012-05-30 10:08:50 -07003551 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003552 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003553 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003554 }
3555
John Reck9c35b9c2012-05-30 10:08:50 -07003556 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003557 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003558 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003559 }
3560
Pankaj Garg49b79252014-11-07 17:33:41 -08003561 @Override
3562 public boolean shouldShowAppMenu() {
3563 return true;
3564 }
3565
3566 @Override
3567 public int getMenuThemeResourceId() {
3568 return R.style.OverflowMenuTheme;
3569 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003570}