blob: 07f2d12c13e11dbbb2da45c0560012856d760933 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
Site Maoabb7bd32015-03-20 15:34:02 -07004 * Copyright (C) 2015 The Linux Foundation
5 *
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;
101import com.android.browser.mynavigation.AddMyNavigationPage;
102import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700103import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700104import com.android.browser.platformsupport.BrowserContract;
105import com.android.browser.platformsupport.WebAddress;
106import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800107import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700108import com.android.browser.provider.BrowserProvider2.Thumbnails;
109import com.android.browser.provider.SnapshotProvider.Snapshots;
110import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800111import com.android.browser.appmenu.AppMenuHandler;
112import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700113
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700115import java.io.File;
116import java.io.FileOutputStream;
117import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700118import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700119import java.text.DateFormat;
120import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700121import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700122import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800124import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200125import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700126import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700127
128/**
129 * Controller for browser
130 */
131public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800132 implements WebViewController, UiController, ActivityController,
133 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700134
135 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800136 private static final String SEND_APP_ID_EXTRA =
137 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Vivek Sekhared791da2015-02-22 12:39:05 -0800138 private static final String INCOGNITO_URI = "chrome://incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800139
Vivek Sekharb54614f2014-05-01 19:03:37 -0700140 // Remind switch to data connection if wifi is unavailable
141 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800142
Michael Kolb8233fac2010-10-26 16:08:53 -0700143 // public message ids
144 public final static int LOAD_URL = 1001;
145 public final static int STOP_LOAD = 1002;
146
147 // Message Ids
148 private static final int FOCUS_NODE_HREF = 102;
149 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800150 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700151
152 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
153
154 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800155 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156
157 private static final int EMPTY_MENU = -1;
158
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700160 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 final static int PREFERENCES_PAGE = 3;
162 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000163 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700164 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800165 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000166
Michael Kolb8233fac2010-10-26 16:08:53 -0700167 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
168
169 // As the ids are dynamically created, we can't guarantee that they will
170 // be in sequence, so this static array maps ids to a window number.
171 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
172 { R.id.window_one_menu_id, R.id.window_two_menu_id,
173 R.id.window_three_menu_id, R.id.window_four_menu_id,
174 R.id.window_five_menu_id, R.id.window_six_menu_id,
175 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
176
177 // "source" parameter for Google search through search key
178 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
179 // "source" parameter for Google search through simplily type
180 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
181
George Mount387d45d2011-10-07 15:57:53 -0700182 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700183 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
184
John Reckd7dd9b22011-08-30 09:18:29 -0700185 // A bitmap that is re-used in createScreenshot as scratch space
186 private static Bitmap sThumbnailBitmap;
187
Michael Kolb8233fac2010-10-26 16:08:53 -0700188 private Activity mActivity;
189 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700190 private HomepageHandler mHomepageHandler;
191 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 private BrowserSettings mSettings;
193 private WebViewFactory mFactory;
194
195 private WakeLock mWakeLock;
196
197 private UrlHandler mUrlHandler;
198 private UploadHandler mUploadHandler;
199 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700200 private PageDialogsHandler mPageDialogsHandler;
201 private NetworkStateHandler mNetworkHandler;
202
Ben Murdoch8029a772010-11-16 11:58:21 +0000203 private Message mAutoFillSetupMessage;
204
kaiyiza016da12013-08-26 17:50:22 +0800205 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700206
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 // FIXME, temp address onPrepareMenu performance problem.
208 // When we move everything out of view, we should rewrite this.
209 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700210 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 private int mOldMenuState = EMPTY_MENU;
212 private Menu mCachedMenu;
213
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 private boolean mMenuIsDown;
215
Pankaj Garg49b79252014-11-07 17:33:41 -0800216 private boolean mWasInPageLoad = false;
217 private AppMenuHandler mAppMenuHandler;
218
Michael Kolb8233fac2010-10-26 16:08:53 -0700219 // For select and find, we keep track of the ActionMode so that
220 // finish() can be called as desired.
221 private ActionMode mActionMode;
222
223 /**
224 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
225 * of whether the configuration has changed. The first onMenuOpened call
226 * after a configuration change is simply a reopening of the same menu
227 * (i.e. mIconView did not change).
228 */
229 private boolean mConfigChanged;
230
231 /**
232 * Keeps track of whether the options menu is open. This is important in
233 * determining whether to show or hide the title bar overlay
234 */
235 private boolean mOptionsMenuOpen;
236
237 /**
238 * Whether or not the options menu is in its bigger, popup menu form. When
239 * true, we want the title bar overlay to be gone. When false, we do not.
240 * Only meaningful if mOptionsMenuOpen is true.
241 */
242 private boolean mExtendedMenuOpen;
243
Michael Kolb8233fac2010-10-26 16:08:53 -0700244 private boolean mActivityPaused = true;
245 private boolean mLoadStopped;
246
247 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500248 // Checks to see when the bookmarks database has changed, and updates the
249 // Tabs' notion of whether they represent bookmarked sites.
250 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700251 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700252
Michael Kolbc3af0672011-08-09 10:24:41 -0700253 private boolean mBlockEvents;
254
Michael Kolb0b129122012-06-04 16:31:58 -0700255 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800256 private boolean mUpdateMyNavThumbnail;
257 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800258 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800259 private WebView.HitTestResult mResult;
Pankaj Gargeba076f2015-03-25 13:40:59 -0700260 private static Bitmap mBookmarkBitmap;
Site Maoabb7bd32015-03-20 15:34:02 -0700261 private PowerConnectionReceiver mLowPowerReceiver;
262 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700263
George Mount3636d0a2011-11-21 09:08:21 -0800264 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 mActivity = browser;
266 mSettings = BrowserSettings.getInstance();
267 mTabControl = new TabControl(this);
268 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700269 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800270 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700271 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700272
273 mUrlHandler = new UrlHandler(this);
274 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
276
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500278 mBookmarksObserver = new ContentObserver(mHandler) {
279 @Override
280 public void onChange(boolean selfChange) {
281 int size = mTabControl.getTabCount();
282 for (int i = 0; i < size; i++) {
283 mTabControl.getTab(i).updateBookmarkedStatus();
284 }
285 }
286
287 };
288 browser.getContentResolver().registerContentObserver(
289 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700290
291 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700292 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800293 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700294 }
295
John Reck9c35b9c2012-05-30 10:08:50 -0700296 @Override
297 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700298 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800299 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800300 // mCrashRecoverHandler has any previously saved state.
301 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700302 }
303
George Mount3636d0a2011-11-21 09:08:21 -0800304 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800305 // we dont want to ever recover incognito tabs
306 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700307
Patrick Scott7d50a932011-02-04 09:27:26 -0500308 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700309 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000311
Michael Kolbc831b632011-05-11 09:30:34 -0700312 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500313 // Not able to restore so we go ahead and clear session cookies. We
314 // must do this before trying to login the user as we don't want to
315 // clear any session cookies set during login.
316 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700317 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800318 if (intent == null) {
319 // This won't happen under common scenarios. The icicle is
320 // not null, but there aren't any tabs to restore.
321 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700322 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800323 final Bundle extra = intent.getExtras();
324 // Create an initial tab.
325 // If the intent is ACTION_VIEW and data is not null, the Browser is
326 // invoked to view the content by another application. In this case,
327 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800328 UrlData urlData = null;
329 if (intent.getData() != null
330 && Intent.ACTION_VIEW.equals(intent.getAction())
331 && intent.getData().toString().startsWith("content://")) {
332 urlData = new UrlData(intent.getData().toString());
333 } else {
334 urlData = IntentHandler.getUrlDataFromIntent(intent);
335 }
George Mount3636d0a2011-11-21 09:08:21 -0800336 Tab t = null;
337 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700338 String landingPage = mActivity.getResources().getString(
339 R.string.def_landing_page);
340 if (!landingPage.isEmpty()) {
341 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800342 } else {
343 t = openTabToHomePage();
344 }
George Mount3636d0a2011-11-21 09:08:21 -0800345 } else {
346 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700347 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800348 }
349 if (t != null) {
350 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
351 }
352 WebView webView = t.getWebView();
353 if (extra != null) {
354 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
355 if (scale > 0 && scale <= 1000) {
356 webView.setInitialScale(scale);
357 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 }
359 }
John Reckd8c74522011-06-14 08:45:00 -0700360 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700362 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500363 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700364 List<Tab> tabs = mTabControl.getTabs();
365 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700366
John Reck1cf4b792011-07-26 10:22:22 -0700367 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700368 //handle restored pages that may require a JS interface
369 if (t.getWebView() != null) {
370 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
371 if (backForwardList != null) {
372 for (int i = 0; i < backForwardList.getSize(); i++) {
373 WebHistoryItem item = backForwardList.getItemAtIndex(i);
374 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
375 }
376 }
377 }
John Reck1cf4b792011-07-26 10:22:22 -0700378 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700379 if (t != mTabControl.getCurrentTab()) {
380 t.pause();
381 }
John Reck1cf4b792011-07-26 10:22:22 -0700382 }
383 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700384 if (tabs.size() == 0) {
385 openTabToHomePage();
386 }
John Reck1cf4b792011-07-26 10:22:22 -0700387 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 // TabControl.restoreState() will create a new tab even if
389 // restoring the state fails.
390 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800391 // Intent is non-null when framework thinks the browser should be
392 // launching with a new intent (icicle is null).
393 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700394 mIntentHandler.onNewIntent(intent);
395 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700398 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800399 if (jsFlags.trim().length() != 0) {
400 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 }
George Mount3636d0a2011-11-21 09:08:21 -0800402 if (intent != null
403 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700404 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800405 }
Site Maoabb7bd32015-03-20 15:34:02 -0700406
407 mLowPowerReceiver = new PowerConnectionReceiver();
408 mPowerChangeReceiver = new PowerConnectionReceiver();
409
410 //always track the android framework's power save mode
411 IntentFilter filter = new IntentFilter();
412 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
413 // Power save mode only exists in Lollipop and above
414 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
415 }
416 filter.addAction(Intent.ACTION_BATTERY_OKAY);
417 mActivity.registerReceiver(mPowerChangeReceiver, filter);
418
Michael Kolb8233fac2010-10-26 16:08:53 -0700419 }
420
John Reck1cf4b792011-07-26 10:22:22 -0700421 private static class PruneThumbnails implements Runnable {
422 private Context mContext;
423 private List<Long> mIds;
424
425 PruneThumbnails(Context context, List<Long> preserveIds) {
426 mContext = context.getApplicationContext();
427 mIds = preserveIds;
428 }
429
430 @Override
431 public void run() {
432 ContentResolver cr = mContext.getContentResolver();
433 if (mIds == null || mIds.size() == 0) {
434 cr.delete(Thumbnails.CONTENT_URI, null, null);
435 } else {
436 int length = mIds.size();
437 StringBuilder where = new StringBuilder();
438 where.append(Thumbnails._ID);
439 where.append(" not in (");
440 for (int i = 0; i < length; i++) {
441 where.append(mIds.get(i));
442 if (i < (length - 1)) {
443 where.append(",");
444 }
445 }
446 where.append(")");
447 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
448 }
449 }
450
451 }
452
Michael Kolb1514bb72010-11-22 09:11:48 -0800453 @Override
454 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700455 return mFactory;
456 }
457
458 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800459 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800460 if (tab.hasCrashed)
461 tab.showCrashView();
462 else
463 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800464 }
465
466 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800467 public void createSubWindow(Tab tab) {
468 endActionMode();
469 WebView mainView = tab.getWebView();
470 WebView subView = mFactory.createWebView((mainView == null)
471 ? false
472 : mainView.isPrivateBrowsingEnabled());
473 mUi.createSubWindow(tab, subView);
474 }
475
476 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700477 public Context getContext() {
478 return mActivity;
479 }
480
481 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700482 public Activity getActivity() {
483 return mActivity;
484 }
485
486 void setUi(UI ui) {
487 mUi = ui;
488 }
489
Michael Kolbaf63dba2012-05-16 12:58:05 -0700490 @Override
491 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700492 return mSettings;
493 }
494
495 IntentHandler getIntentHandler() {
496 return mIntentHandler;
497 }
498
499 @Override
500 public UI getUi() {
501 return mUi;
502 }
503
504 int getMaxTabs() {
505 return mActivity.getResources().getInteger(R.integer.max_tabs);
506 }
507
508 @Override
509 public TabControl getTabControl() {
510 return mTabControl;
511 }
512
Michael Kolb1bf23132010-11-19 12:55:12 -0800513 @Override
514 public List<Tab> getTabs() {
515 return mTabControl.getTabs();
516 }
517
Michael Kolb8233fac2010-10-26 16:08:53 -0700518 private void startHandler() {
519 mHandler = new Handler() {
520
521 @Override
522 public void handleMessage(Message msg) {
523 switch (msg.what) {
524 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700525 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700526 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800527 case UNKNOWN_TYPE_MSG:
528 HashMap unknownTypeMap = (HashMap) msg.obj;
529 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
530 /*
531 * When the context menu is shown to the user
532 * we need to assure that its happening on the current webview
533 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
534 */
535 if (getCurrentWebView() != viewForUnknownType)
536 break;
537
538 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800539 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800540 WebView.HitTestResult result = new WebView.HitTestResult();
541
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800542 // Prevent unnecessary calls to context menu
543 // if url and image src are null
544 if (unknown_type_src == null && unknown_type_url == null)
545 break;
546
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800547 //setting the HitTestResult with new RESULT TYPE
548 if (!TextUtils.isEmpty(unknown_type_src)) {
549 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
550 result.setExtra(unknown_type_src);
551 } else {
552 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
553 result.setExtra("about:blank");
554 }
555
556 mResult = result;
557 openContextMenu(viewForUnknownType);
558 mResult = null;
559
560 break;
561
Michael Kolb8233fac2010-10-26 16:08:53 -0700562 case FOCUS_NODE_HREF:
563 {
564 String url = (String) msg.getData().get("url");
565 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500566 String src = (String) msg.getData().get("src");
567 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700568 if (TextUtils.isEmpty(url)) {
569 break;
570 }
571 HashMap focusNodeMap = (HashMap) msg.obj;
572 WebView view = (WebView) focusNodeMap.get("webview");
573 // Only apply the action if the top window did not change.
574 if (getCurrentTopWebView() != view) {
575 break;
576 }
577 switch (msg.arg1) {
578 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700579 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500581 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700582 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500583 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500584 case R.id.open_newtab_context_menu_id:
585 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700586 openTab(url, parent,
587 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500588 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700589 case R.id.copy_link_context_menu_id:
590 copy(url);
591 break;
592 case R.id.save_link_context_menu_id:
593 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500594 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800595 mActivity, url, view.getSettings().getUserAgentString(),
596 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700597 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700598 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700599 if(title == null || title == "")
600 title = url;
601
602 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
603 //SWE TODO: No thumbnail support for the url obtained via
604 //browser context menu as its not loaded in webview.
605 if (bookmarkIntent != null) {
606 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
607 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
608 mActivity.startActivity(bookmarkIntent);
609 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700610 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700611 }
612 break;
613 }
614
615 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700616 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 break;
618
619 case STOP_LOAD:
620 stopLoading();
621 break;
622
623 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700624 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 mWakeLock.release();
626 // if we reach here, Browser should be still in the
627 // background loading after WAKELOCK_TIMEOUT (5-min).
628 // To avoid burning the battery, stop loading.
629 mTabControl.stopAllLoading();
630 }
631 break;
632
633 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800634 Tab tab = (Tab) msg.obj;
635 if (tab != null) {
636 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 }
638 break;
kaiyiz591110b2013-08-06 17:11:06 +0800639 case OPEN_MENU:
640 if (!mOptionsMenuOpen && mActivity != null ) {
641 mActivity.openOptionsMenu();
642 }
643 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700644 }
645 }
646 };
647
648 }
649
John Reckef654f12011-07-12 16:42:08 -0700650 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200651 public Tab getCurrentTab() {
652 return mTabControl.getCurrentTab();
653 }
654
Michael Kolbba99c5d2010-11-29 14:57:41 -0800655 @Override
656 public void shareCurrentPage() {
657 shareCurrentPage(mTabControl.getCurrentTab());
658 }
659
660 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700661 if (tab == null || tab.getWebView() == null)
662 return;
663
664 final Tab mytab = tab;
665 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
666 @Override
667 public void onReceiveValue(Bitmap bitmap) {
668 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
669 mytab.getFavicon(), bitmap);
670 }
671 };
672
673 createScreenshotAsync(
674 tab.getWebView(),
675 getDesiredThumbnailWidth(mActivity),
676 getDesiredThumbnailHeight(mActivity),
677 new ValueCallback<Bitmap>() {
678 @Override
679 public void onReceiveValue(Bitmap bitmap) {
680 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
681 mytab.getFavicon(), bitmap);
682 }
683 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800684 }
685
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 /**
687 * Share a page, providing the title, url, favicon, and a screenshot. Uses
688 * an {@link Intent} to launch the Activity chooser.
689 * @param c Context used to launch a new Activity.
690 * @param title Title of the page. Stored in the Intent with
691 * {@link Intent#EXTRA_SUBJECT}
692 * @param url URL of the page. Stored in the Intent with
693 * {@link Intent#EXTRA_TEXT}
694 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
695 * with {@link Browser#EXTRA_SHARE_FAVICON}
696 * @param screenshot Bitmap of a screenshot of the page. Stored in the
697 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
698 */
699 static final void sharePage(Context c, String title, String url,
700 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700701
702 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
703 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
704 R.layout.app_row, sDialog.getApps());
705 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 }
707
708 private void copy(CharSequence text) {
709 ClipboardManager cm = (ClipboardManager) mActivity
710 .getSystemService(Context.CLIPBOARD_SERVICE);
711 cm.setText(text);
712 }
713
714 // lifecycle
715
John Reck9c35b9c2012-05-30 10:08:50 -0700716 @Override
717 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700718 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800719 // update the menu in case of a locale change
720 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800721 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800722 if (mOptionsMenuOpen) {
723 mActivity.closeOptionsMenu();
724 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
725 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700726 if (mPageDialogsHandler != null) {
727 mPageDialogsHandler.onConfigurationChanged(config);
728 }
729 mUi.onConfigurationChanged(config);
730 }
731
732 @Override
733 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700734 if (!mUi.isWebShowing()) {
735 mUi.showWeb(false);
736 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 mIntentHandler.onNewIntent(intent);
738 }
739
John Reck9c35b9c2012-05-30 10:08:50 -0700740 @Override
741 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800742 if (mUi.isCustomViewShowing()) {
743 hideCustomView();
744 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 if (mActivityPaused) {
746 Log.e(LOGTAG, "BrowserActivity is already paused.");
747 return;
748 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700749 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800750 Tab tab = mTabControl.getCurrentTab();
751 if (tab != null) {
752 tab.pause();
753 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700754 if (mWakeLock == null) {
755 PowerManager pm = (PowerManager) mActivity
756 .getSystemService(Context.POWER_SERVICE);
757 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
758 }
Michael Kolb70976932010-11-30 11:34:01 -0800759 mWakeLock.acquire();
760 mHandler.sendMessageDelayed(mHandler
761 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
762 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700763 }
764 mUi.onPause();
765 mNetworkHandler.onPause();
766
767 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100768 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700769
770 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700771 }
772
John Reck9c35b9c2012-05-30 10:08:50 -0700773 @Override
774 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700775 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800776 Bundle saveState = createSaveState();
777
778 // crash recovery manages all save & restore state
779 mCrashRecoveryHandler.writeState(saveState);
780 mSettings.setLastRunPaused(true);
781 }
782
783 /**
784 * Save the current state to outState. Does not write the state to
785 * disk.
786 * @return Bundle containing the current state of all tabs.
787 */
788 /* package */ Bundle createSaveState() {
789 Bundle saveState = new Bundle();
790 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800791 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 }
793
John Reck9c35b9c2012-05-30 10:08:50 -0700794 @Override
795 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700796 if (!mActivityPaused) {
797 Log.e(LOGTAG, "BrowserActivity is already resumed.");
798 return;
799 }
George Mount3636d0a2011-11-21 09:08:21 -0800800 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700801 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800802 Tab current = mTabControl.getCurrentTab();
803 if (current != null) {
804 current.resume();
805 resumeWebViewTimers(current);
806 }
John Reckf57c0292011-07-21 18:15:39 -0700807 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200808
Michael Kolb8233fac2010-10-26 16:08:53 -0700809 mUi.onResume();
810 mNetworkHandler.onResume();
811 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100812 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700813 if (mVoiceResult != null) {
814 mUi.onVoiceResult(mVoiceResult);
815 mVoiceResult = null;
816 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800817 if (current != null && current.hasCrashed) {
Vivek Sekhar2868b8d2014-12-03 17:22:50 -0800818 current.replaceCrashView(current.getWebView(), current.getViewContainer());
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800819 }
Site Maoabb7bd32015-03-20 15:34:02 -0700820
821 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700822 }
823
John Reckf57c0292011-07-21 18:15:39 -0700824 private void releaseWakeLock() {
825 if (mWakeLock != null && mWakeLock.isHeld()) {
826 mHandler.removeMessages(RELEASE_WAKELOCK);
827 mWakeLock.release();
828 }
829 }
830
Michael Kolb70976932010-11-30 11:34:01 -0800831 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800832 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800833 * @param tab guaranteed non-null
834 */
835 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700836 boolean inLoad = tab.inPageLoad();
837 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
838 CookieSyncManager.getInstance().startSync();
839 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100840 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 }
842 }
843
Michael Kolb70976932010-11-30 11:34:01 -0800844 /**
845 * Pause all WebView timers using the WebView of the given tab
846 * @param tab
847 * @return true if the timers are paused or tab is null
848 */
849 private boolean pauseWebViewTimers(Tab tab) {
850 if (tab == null) {
851 return true;
852 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100854 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 }
Michael Kolb70976932010-11-30 11:34:01 -0800857 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 }
859
John Reck9c35b9c2012-05-30 10:08:50 -0700860 @Override
861 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800862 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
864 mUploadHandler = null;
865 }
866 if (mTabControl == null) return;
867 mUi.onDestroy();
868 // Remove the current tab and sub window
869 Tab t = mTabControl.getCurrentTab();
870 if (t != null) {
871 dismissSubWindow(t);
872 removeTab(t);
873 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500874 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700875 // Destroy all the tabs
876 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700877 // Unregister receiver
878 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 }
880
881 protected boolean isActivityPaused() {
882 return mActivityPaused;
883 }
884
John Reck9c35b9c2012-05-30 10:08:50 -0700885 @Override
886 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700887 mTabControl.freeMemory();
888 }
889
890 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700891 public void stopLoading() {
892 mLoadStopped = true;
893 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700894 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700895 if (w != null) {
896 w.stopLoading();
897 mUi.onPageStopped(tab);
898 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700899 }
900
901 boolean didUserStopLoading() {
902 return mLoadStopped;
903 }
904
kaiyiza016da12013-08-26 17:50:22 +0800905 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700906 final String reminderType = getContext().getResources().getString(
907 R.string.def_wifi_browser_interaction_remind_type);
908 final String selectionConnnection = getContext().getResources().getString(
909 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700910 final String wifiSelection = getContext().getResources().getString(
911 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700912
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700913 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
914 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700915 return;
916
kaiyiza016da12013-08-26 17:50:22 +0800917 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700918 Context.CONNECTIVITY_SERVICE);
919 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700920 WifiManager wifiMgr = (WifiManager) this.getContext()
921 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700922 if (networkInfo == null
923 || (networkInfo != null && (networkInfo.getType() !=
924 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700925 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
926 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700927 List<ScanResult> list = wifiMgr.getScanResults();
928 // Have no AP's for Wifi's fall back to data
929 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700930 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700931 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
932 this.getContext().startActivity(intent);
933 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700934 // Request to select Wifi AP
935 try {
936 Intent intent = new Intent(wifiSelection);
937 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
938 this.getContext().startActivity(intent);
939 } catch (Exception e) {
940 String err_msg = this.getContext().getString(
941 R.string.acivity_not_found, wifiSelection);
942 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
943 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700944 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700945 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800946 }
947 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700948
Michael Kolb8233fac2010-10-26 16:08:53 -0700949 // WebViewController
950
951 @Override
John Reck324d4402011-01-11 16:56:42 -0800952 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700953
954 // We've started to load a new page. If there was a pending message
955 // to save a screenshot then we will now take the new page and save
956 // an incorrect screenshot. Therefore, remove any pending thumbnail
957 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700958 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700959
960 // reset sync timer to avoid sync starts during loading a page
961 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700962 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700963 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800964 boolean networkNotifier = BrowserConfig.getInstance(getContext())
965 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700966 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700967 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800968 } else {
969 if (!mNetworkHandler.isNetworkUp()) {
970 view.setNetworkAvailable(false);
971 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 }
973
974 // when BrowserActivity just starts, onPageStarted may be called before
975 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
976 // to start the timer. As we won't switch tabs while an activity is in
977 // pause state, we can ensure calling resume and pause in pair.
978 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800979 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 }
981 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 endActionMode();
983
John Reck30c714c2010-12-16 17:30:34 -0800984 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700985
John Reck324d4402011-01-11 16:56:42 -0800986 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 // update the bookmark database for favicon
988 maybeUpdateFavicon(tab, null, url, favicon);
989
990 Performance.tracePageStart(url);
991
992 // Performance probe
993 if (false) {
994 Performance.onPageStarted();
995 }
996
997 }
998
999 @Override
John Reck324d4402011-01-11 16:56:42 -08001000 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -07001001 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -08001002 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001003
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 // Performance probe
1005 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001006 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001007 }
1008
Tarun Nainani8eb00912014-07-17 12:28:32 -07001009 tab.onPageFinished();
1010
Michael Kolb8233fac2010-10-26 16:08:53 -07001011 Performance.tracePageFinished();
1012 }
1013
1014 @Override
John Reck30c714c2010-12-16 17:30:34 -08001015 public void onProgressChanged(Tab tab) {
1016 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001017
1018 if (newProgress == 100) {
1019 CookieSyncManager.getInstance().sync();
1020 // onProgressChanged() may continue to be called after the main
1021 // frame has finished loading, as any remaining sub frames continue
1022 // to load. We'll only get called once though with newProgress as
1023 // 100 when everything is loaded. (onPageFinished is called once
1024 // when the main frame completes loading regardless of the state of
1025 // any sub frames so calls to onProgressChanges may continue after
1026 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001027 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001028 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001029 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001030 } else if (mWasInPageLoad) {
1031 mWasInPageLoad = false;
1032 updateInLoadMenuItems(mCachedMenu, tab);
1033 }
1034
1035 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001036 // pause the WebView timer and release the wake lock if it is
1037 // finished while BrowserActivity is in pause state.
1038 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 }
John Reckd9862372012-02-21 15:04:50 -08001040 if (!tab.isPrivateBrowsingEnabled()
1041 && !TextUtils.isEmpty(tab.getUrl())
1042 && !tab.isSnapshot()) {
1043 // Only update the bookmark screenshot if the user did not
1044 // cancel the load early and there is not already
1045 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001046 if (tab.shouldUpdateThumbnail() &&
1047 (tab.inForeground() && !didUserStopLoading()
1048 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001049 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1050 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1051 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001052 1500);
John Reckd9862372012-02-21 15:04:50 -08001053 }
1054 }
1055 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001057 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001058 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001059 // still loading
1060 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001061 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001062 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001063 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001064 } else {
1065 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001066 }
1067 }
John Reck30c714c2010-12-16 17:30:34 -08001068 mUi.onProgressChanged(tab);
1069 }
1070
1071 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001072 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001073 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001074 }
1075
1076 @Override
1077 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001078 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001079 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001080 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1082 return;
1083 }
1084 // Update the title in the history database if not in private browsing mode
1085 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001086 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001087 }
1088 }
1089
1090 @Override
1091 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001092 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001093 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1094 }
1095
1096 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001097 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1098 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001099 }
1100
1101 @Override
1102 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1103 if (mMenuIsDown) {
1104 // only check shortcut key when MENU is held
1105 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1106 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001107 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001108 int keyCode = event.getKeyCode();
1109 // We need to send almost every key to WebKit. However:
1110 // 1. We don't want to block the device on the renderer for
1111 // some keys like menu, home, call.
1112 // 2. There are no WebKit equivalents for some of these keys
1113 // (see app/keyboard_codes_win.h)
1114 // Note that these are not the same set as KeyEvent.isSystemKey:
1115 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1116 if (keyCode == KeyEvent.KEYCODE_MENU ||
1117 keyCode == KeyEvent.KEYCODE_HOME ||
1118 keyCode == KeyEvent.KEYCODE_BACK ||
1119 keyCode == KeyEvent.KEYCODE_CALL ||
1120 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1121 keyCode == KeyEvent.KEYCODE_POWER ||
1122 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1123 keyCode == KeyEvent.KEYCODE_CAMERA ||
1124 keyCode == KeyEvent.KEYCODE_FOCUS ||
1125 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1126 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1127 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1128 return true;
1129 }
1130
1131 // We also have to intercept some shortcuts before we send them to the ContentView.
1132 if (event.isCtrlPressed() && (
1133 keyCode == KeyEvent.KEYCODE_TAB ||
1134 keyCode == KeyEvent.KEYCODE_W ||
1135 keyCode == KeyEvent.KEYCODE_F4)) {
1136 return true;
1137 }
1138
1139 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001140 }
1141
Tarun Nainanif5563b62015-01-21 18:52:59 -08001142 private void handleMediaKeyEvent(KeyEvent event) {
1143
1144 int keyCode = event.getKeyCode();
1145 // send media key events to audio manager
1146 if (Build.VERSION.SDK_INT >= 19) {
1147
1148 switch (keyCode) {
1149 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1150 case KeyEvent.KEYCODE_MEDIA_STOP:
1151 case KeyEvent.KEYCODE_MEDIA_NEXT:
1152 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1153 case KeyEvent.KEYCODE_MEDIA_REWIND:
1154 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1155 case KeyEvent.KEYCODE_MUTE:
1156 case KeyEvent.KEYCODE_MEDIA_PLAY:
1157 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1158 case KeyEvent.META_SHIFT_RIGHT_ON:
1159 case KeyEvent.KEYCODE_MEDIA_EJECT:
1160 case KeyEvent.KEYCODE_MEDIA_RECORD:
1161 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1162
1163 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1164 .getSystemService(Context.AUDIO_SERVICE);
1165 audioManager.dispatchMediaKeyEvent(event);
1166 }
1167 }
1168 }
1169
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 @Override
John Reck997b1b72012-04-19 18:08:25 -07001171 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001173 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001175 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001176 } else {
John Reck997b1b72012-04-19 18:08:25 -07001177 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001178 }
1179 }
John Reck997b1b72012-04-19 18:08:25 -07001180 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001181 }
1182
1183 @Override
John Reck324d4402011-01-11 16:56:42 -08001184 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001185 // Don't save anything in private browsing mode or when disabling history
1186 // for regular tabs is enabled
1187 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1188 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1189 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001190
John Reck49a603c2011-03-03 09:33:05 -08001191 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001192
John Reck324d4402011-01-11 16:56:42 -08001193 if (TextUtils.isEmpty(url)
1194 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001195 return;
1196 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001197
John Reckf57c0292011-07-21 18:15:39 -07001198 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001199 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001200 }
1201
1202 @Override
1203 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1204 AsyncTask<Void, Void, String[]> task =
1205 new AsyncTask<Void, Void, String[]>() {
1206 @Override
1207 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001208 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 }
1210 @Override
1211 public void onPostExecute(String[] result) {
1212 callback.onReceiveValue(result);
1213 }
1214 };
1215 task.execute();
1216 }
1217
1218 @Override
1219 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1220 final HttpAuthHandler handler, final String host,
1221 final String realm) {
1222 String username = null;
1223 String password = null;
1224
1225 boolean reuseHttpAuthUsernamePassword
1226 = handler.useHttpAuthUsernamePassword();
1227
1228 if (reuseHttpAuthUsernamePassword && view != null) {
1229 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1230 if (credentials != null && credentials.length == 2) {
1231 username = credentials[0];
1232 password = credentials[1];
1233 }
1234 }
1235
1236 if (username != null && password != null) {
1237 handler.proceed(username, password);
1238 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001239 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1241 } else {
1242 handler.cancel();
1243 }
1244 }
1245 }
1246
1247 @Override
1248 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001249 String contentDisposition, String mimetype, String referer,
1250 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001251 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001252 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001253 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001254 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001255 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001256 // This Tab was opened for the sole purpose of downloading a
1257 // file. Remove it.
1258 if (tab == mTabControl.getCurrentTab()) {
1259 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001260 if (tab.getDerivedFromIntent())
1261 closeTab(tab);
1262 else
1263 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 } else {
1265 // In this case, it is not.
1266 closeTab(tab);
1267 }
1268 }
1269 }
1270
1271 @Override
1272 public Bitmap getDefaultVideoPoster() {
1273 return mUi.getDefaultVideoPoster();
1274 }
1275
1276 @Override
1277 public View getVideoLoadingProgressView() {
1278 return mUi.getVideoLoadingProgressView();
1279 }
1280
1281 @Override
1282 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1283 SslError error) {
1284 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1285 }
1286
1287 // helper method
1288
1289 /*
1290 * Update the favorites icon if the private browsing isn't enabled and the
1291 * icon is valid.
1292 */
1293 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1294 final String url, Bitmap favicon) {
1295 if (favicon == null) {
1296 return;
1297 }
1298 if (!tab.isPrivateBrowsingEnabled()) {
1299 Bookmarks.updateFavicon(mActivity
1300 .getContentResolver(), originalUrl, url, favicon);
1301 }
1302 }
1303
Leon Scroggins4cd97792010-12-03 15:31:56 -05001304 @Override
1305 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001306 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001307 mUi.bookmarkedStatusHasChanged(tab);
1308 }
1309
Michael Kolb8233fac2010-10-26 16:08:53 -07001310 // end WebViewController
1311
1312 protected void pageUp() {
1313 getCurrentTopWebView().pageUp(false);
1314 }
1315
1316 protected void pageDown() {
1317 getCurrentTopWebView().pageDown(false);
1318 }
1319
1320 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001321 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001322 public void editUrl() {
1323 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001324 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001325 }
1326
John Reck9c35b9c2012-05-30 10:08:50 -07001327 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001328 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001329 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001330 if (tab.inForeground()) {
1331 if (mUi.isCustomViewShowing()) {
1332 callback.onCustomViewHidden();
1333 return;
1334 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001335 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001336 // Save the menu state and set it to empty while the custom
1337 // view is showing.
1338 mOldMenuState = mMenuState;
1339 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001340 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001341 }
1342 }
1343
1344 @Override
1345 public void hideCustomView() {
1346 if (mUi.isCustomViewShowing()) {
1347 mUi.onHideCustomView();
1348 // Reset the old menu state.
1349 mMenuState = mOldMenuState;
1350 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001351 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001352 }
1353 }
1354
John Reck9c35b9c2012-05-30 10:08:50 -07001355 @Override
1356 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001357 Intent intent) {
1358 if (getCurrentTopWebView() == null) return;
1359 switch (requestCode) {
1360 case PREFERENCES_PAGE:
1361 if (resultCode == Activity.RESULT_OK && intent != null) {
1362 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001363 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 mTabControl.removeParentChildRelationShips();
1365 }
1366 }
1367 break;
1368 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001369 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001370 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001371 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001372 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001373 case AUTOFILL_SETUP:
1374 // Determine whether a profile was actually set up or not
1375 // and if so, send the message back to the WebTextView to
1376 // fill the form with the new profile.
1377 if (getSettings().getAutoFillProfile() != null) {
1378 mAutoFillSetupMessage.sendToTarget();
1379 mAutoFillSetupMessage = null;
1380 }
1381 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001382 case COMBO_VIEW:
1383 if (intent == null || resultCode != Activity.RESULT_OK) {
1384 break;
1385 }
John Reck3ba45532011-08-11 16:26:53 -07001386 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001387 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1388 Tab t = getCurrentTab();
1389 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001390 mUpdateMyNavThumbnail = true;
1391 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001392 loadUrl(t, uri.toString());
1393 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1394 String[] urls = intent.getStringArrayExtra(
1395 ComboViewActivity.EXTRA_OPEN_ALL);
1396 Tab parent = getCurrentTab();
1397 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001398 if (url != null) {
1399 parent = openTab(url, parent,
1400 !mSettings.openInBackground(), true);
1401 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001402 }
1403 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1404 long id = intent.getLongExtra(
1405 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1406 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001407 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001408 }
1409 }
1410 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001411 case VOICE_RESULT:
1412 if (resultCode == Activity.RESULT_OK && intent != null) {
1413 ArrayList<String> results = intent.getStringArrayListExtra(
1414 RecognizerIntent.EXTRA_RESULTS);
1415 if (results.size() >= 1) {
1416 mVoiceResult = results.get(0);
1417 }
1418 }
1419 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001420 case MY_NAVIGATION:
1421 if (intent == null || resultCode != Activity.RESULT_OK) {
1422 break;
1423 }
1424
1425 if (intent.getBooleanExtra("need_refresh", false) &&
1426 getCurrentTopWebView() != null) {
1427 getCurrentTopWebView().reload();
1428 }
1429 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 default:
1431 break;
1432 }
1433 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001434 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 }
1436
1437 /**
1438 * Open the Go page.
1439 * @param startWithHistory If true, open starting on the history tab.
1440 * Otherwise, start with the bookmarks tab.
1441 */
1442 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001443 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001444 if (mTabControl.getCurrentWebView() == null) {
1445 return;
1446 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001447 // clear action mode
1448 if (isInCustomActionMode()) {
1449 endActionMode();
1450 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 Bundle extras = new Bundle();
1452 // Disable opening in a new window if we have maxed out the windows
1453 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1454 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001455 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001456 }
1457
1458 // combo view callbacks
1459
Michael Kolb8233fac2010-10-26 16:08:53 -07001460 // key handling
1461 protected void onBackKey() {
1462 if (!mUi.onBackKey()) {
1463 WebView subwindow = mTabControl.getCurrentSubWindow();
1464 if (subwindow != null) {
1465 if (subwindow.canGoBack()) {
1466 subwindow.goBack();
1467 } else {
1468 dismissSubWindow(mTabControl.getCurrentTab());
1469 }
1470 } else {
1471 goBackOnePageOrQuit();
1472 }
1473 }
1474 }
1475
Michael Kolb4bd767d2011-05-27 11:33:55 -07001476 protected boolean onMenuKey() {
1477 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001478 }
1479
Michael Kolb8233fac2010-10-26 16:08:53 -07001480 // menu handling and state
1481 // TODO: maybe put into separate handler
1482
John Reck9c35b9c2012-05-30 10:08:50 -07001483 @Override
1484 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001485 if (mMenuState == EMPTY_MENU) {
1486 return false;
1487 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001488 MenuInflater inflater = mActivity.getMenuInflater();
1489 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001490 return true;
1491 }
1492
John Reck9c35b9c2012-05-30 10:08:50 -07001493 @Override
1494 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001495 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001496 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001497 return;
1498 }
1499 if (!(v instanceof WebView)) {
1500 return;
1501 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001502 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001503 WebView.HitTestResult result;
1504
1505 /* Determine whether the ContextMenu got triggered because
1506 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1507 * received. The mResult acts as a flag to identify, how it got trigerred
1508 */
1509 if (mResult == null){
1510 result = webview.getHitTestResult();
1511 } else {
1512 result = mResult;
1513 }
1514
Michael Kolb8233fac2010-10-26 16:08:53 -07001515 if (result == null) {
1516 return;
1517 }
1518
1519 int type = result.getType();
1520 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001521
1522 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1523 unknownTypeMap.put("webview", webview);
1524 final Message msg = mHandler.obtainMessage(
1525 UNKNOWN_TYPE_MSG, unknownTypeMap);
1526 /* As defined in android developers guide
1527 * when UNKNOWN_TYPE is received as a result of HitTest
1528 * you need to determing the type by invoking requestFocusNodeHref
1529 */
1530 webview.requestFocusNodeHref(msg);
1531
Michael Kolb8233fac2010-10-26 16:08:53 -07001532 Log.w(LOGTAG,
1533 "We should not show context menu when nothing is touched");
1534 return;
1535 }
1536 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1537 // let TextView handles context menu
1538 return;
1539 }
1540
1541 // Note, http://b/issue?id=1106666 is requesting that
1542 // an inflated menu can be used again. This is not available
1543 // yet, so inflate each time (yuk!)
1544 MenuInflater inflater = mActivity.getMenuInflater();
1545 inflater.inflate(R.menu.browsercontext, menu);
1546
1547 // Show the correct menu group
1548 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001549 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001550 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001551 menu.setGroupVisible(R.id.PHONE_MENU,
1552 type == WebView.HitTestResult.PHONE_TYPE);
1553 menu.setGroupVisible(R.id.EMAIL_MENU,
1554 type == WebView.HitTestResult.EMAIL_TYPE);
1555 menu.setGroupVisible(R.id.GEO_MENU,
1556 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001557 String itemUrl = null;
1558 String url = webview.getOriginalUrl();
1559 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1560 itemUrl = Uri.decode(navigationUrl);
1561 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1562 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1563 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1564 } else {
1565 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1566 }
1567 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1568 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1569 } else {
1570 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1571
1572 menu.setGroupVisible(R.id.IMAGE_MENU,
1573 type == WebView.HitTestResult.IMAGE_TYPE
1574 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1575 menu.setGroupVisible(R.id.ANCHOR_MENU,
1576 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1577 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001578 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1579 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001580 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 // Setup custom handling depending on the type
1582 switch (type) {
1583 case WebView.HitTestResult.PHONE_TYPE:
1584 menu.setHeaderTitle(Uri.decode(extra));
1585 menu.findItem(R.id.dial_context_menu_id).setIntent(
1586 new Intent(Intent.ACTION_VIEW, Uri
1587 .parse(WebView.SCHEME_TEL + extra)));
1588 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1589 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1590 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1591 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1592 addIntent);
1593 menu.findItem(R.id.copy_phone_context_menu_id)
1594 .setOnMenuItemClickListener(
1595 new Copy(extra));
1596 break;
1597
1598 case WebView.HitTestResult.EMAIL_TYPE:
1599 menu.setHeaderTitle(extra);
1600 menu.findItem(R.id.email_context_menu_id).setIntent(
1601 new Intent(Intent.ACTION_VIEW, Uri
1602 .parse(WebView.SCHEME_MAILTO + extra)));
1603 menu.findItem(R.id.copy_mail_context_menu_id)
1604 .setOnMenuItemClickListener(
1605 new Copy(extra));
1606 break;
1607
1608 case WebView.HitTestResult.GEO_TYPE:
1609 menu.setHeaderTitle(extra);
1610 menu.findItem(R.id.map_context_menu_id).setIntent(
1611 new Intent(Intent.ACTION_VIEW, Uri
1612 .parse(WebView.SCHEME_GEO
1613 + URLEncoder.encode(extra))));
1614 menu.findItem(R.id.copy_geo_context_menu_id)
1615 .setOnMenuItemClickListener(
1616 new Copy(extra));
1617 break;
1618
1619 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1620 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001621 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001622 // decide whether to show the open link in new tab option
1623 boolean showNewTab = mTabControl.canCreateNewTab();
1624 MenuItem newTabItem
1625 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001626 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001627 ? R.string.contextmenu_openlink_newwindow_background
1628 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 newTabItem.setVisible(showNewTab);
1630 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001631 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1632 newTabItem.setOnMenuItemClickListener(
1633 new MenuItem.OnMenuItemClickListener() {
1634 @Override
1635 public boolean onMenuItemClick(MenuItem item) {
1636 final HashMap<String, WebView> hrefMap =
1637 new HashMap<String, WebView>();
1638 hrefMap.put("webview", webview);
1639 final Message msg = mHandler.obtainMessage(
1640 FOCUS_NODE_HREF,
1641 R.id.open_newtab_context_menu_id,
1642 0, hrefMap);
1643 webview.requestFocusNodeHref(msg);
1644 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001645 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001646 });
1647 } else {
1648 newTabItem.setOnMenuItemClickListener(
1649 new MenuItem.OnMenuItemClickListener() {
1650 @Override
1651 public boolean onMenuItemClick(MenuItem item) {
1652 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001653 openTab(extra, parent,
1654 !mSettings.openInBackground(),
1655 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001656 return true;
1657 }
1658 });
1659 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001660 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001661 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1662 menu.setHeaderTitle(navigationUrl);
1663 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1664
1665 if (itemUrl != null) {
1666 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1667 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1668 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1669 @Override
1670 public boolean onMenuItemClick(MenuItem item) {
1671 final Intent intent = new Intent(Controller.this
1672 .getContext(),
1673 AddMyNavigationPage.class);
1674 Bundle bundle = new Bundle();
1675 String url = Uri.decode(navigationUrl);
1676 bundle.putBoolean("isAdding", false);
1677 bundle.putString("url", url);
1678 bundle.putString("name", getNameFromUrl(url));
1679 intent.putExtra("websites", bundle);
1680 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1681 return false;
1682 }
1683 });
1684 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1685 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1686 @Override
1687 public boolean onMenuItemClick(MenuItem item) {
1688 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1689 return false;
1690 }
1691 });
1692 }
1693 } else {
1694 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1695 }
1696 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001697 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1698 break;
1699 }
1700 // otherwise fall through to handle image part
1701 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001702 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1703 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001704 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1705 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001706 shareItem.setOnMenuItemClickListener(
1707 new MenuItem.OnMenuItemClickListener() {
1708 @Override
1709 public boolean onMenuItemClick(MenuItem item) {
1710 sharePage(mActivity, null, extra, null,
1711 null);
1712 return true;
1713 }
1714 }
1715 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001716 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001717 menu.findItem(R.id.view_image_context_menu_id)
1718 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1719 @Override
1720 public boolean onMenuItemClick(MenuItem item) {
1721 openTab(extra, mTabControl.getCurrentTab(), true, true);
1722 return false;
1723 }
1724 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001725 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1726 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1727 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001728 menu.findItem(R.id.set_wallpaper_context_menu_id).
1729 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1730 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001731 break;
1732
1733 default:
1734 Log.w(LOGTAG, "We should not get here.");
1735 break;
1736 }
1737 //update the ui
1738 mUi.onContextMenuCreated(menu);
1739 }
1740
kaiyiz6e5b3e02013-08-19 20:02:01 +08001741 public void startAddMyNavigation(String url) {
1742 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1743 Bundle bundle = new Bundle();
1744 bundle.putBoolean("isAdding", true);
1745 bundle.putString("url", url);
1746 bundle.putString("name", getNameFromUrl(url));
1747 intent.putExtra("websites", bundle);
1748 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1749 }
1750
1751 private void showMyNavigationDeleteDialog(final String itemUrl) {
1752 new AlertDialog.Builder(this.getContext())
1753 .setTitle(R.string.my_navigation_delete_label)
1754 .setIcon(android.R.drawable.ic_dialog_alert)
1755 .setMessage(R.string.my_navigation_delete_msg)
1756 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1757 @Override
1758 public void onClick(DialogInterface dialog, int whichButton) {
1759 deleteMyNavigationItem(itemUrl);
1760 }
1761 })
1762 .setNegativeButton(R.string.cancel, null)
1763 .show();
1764 }
1765
1766 private void deleteMyNavigationItem(final String itemUrl) {
1767 ContentResolver cr = this.getContext().getContentResolver();
1768 Cursor cursor = null;
1769
1770 try {
1771 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1772 new String[] {
1773 MyNavigationUtil.ID
1774 }, "url = ?", new String[] {
1775 itemUrl
1776 }, null);
1777 if (null != cursor && cursor.moveToFirst()) {
1778 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1779 cursor.getLong(0));
1780
1781 ContentValues values = new ContentValues();
1782 values.put(MyNavigationUtil.TITLE, "");
1783 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1784 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1785 ByteArrayOutputStream os = new ByteArrayOutputStream();
1786 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1787 R.raw.my_navigation_add);
1788 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1789 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1790 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1791 cr.update(uri, values, null, null);
1792 } else {
1793 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1794 }
1795 } catch (IllegalStateException e) {
1796 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1797 } finally {
1798 if (null != cursor) {
1799 cursor.close();
1800 }
1801 }
1802
1803 if (getCurrentTopWebView() != null) {
1804 getCurrentTopWebView().reload();
1805 }
1806 }
1807
1808 private String getNameFromUrl(String itemUrl) {
1809 ContentResolver cr = this.getContext().getContentResolver();
1810 Cursor cursor = null;
1811 String name = null;
1812
1813 try {
1814 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1815 new String[] {
1816 MyNavigationUtil.TITLE
1817 }, "url = ?", new String[] {
1818 itemUrl
1819 }, null);
1820 if (null != cursor && cursor.moveToFirst()) {
1821 name = cursor.getString(0);
1822 } else {
1823 Log.e(LOGTAG, "this item does not exist!");
1824 }
1825 } catch (IllegalStateException e) {
1826 Log.e(LOGTAG, "getNameFromUrl", e);
1827 } finally {
1828 if (null != cursor) {
1829 cursor.close();
1830 }
1831 }
1832 return name;
1833 }
1834
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001835 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001836 final ContentResolver cr = mActivity.getContentResolver();
1837 new AsyncTask<Void, Void, Void>() {
1838 @Override
1839 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001840 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1841 if(!isMyNavigationUrl)
1842 return null;
1843
kaiyiz6e5b3e02013-08-19 20:02:01 +08001844 ContentResolver cr = mActivity.getContentResolver();
1845 Cursor cursor = null;
1846 try {
1847 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1848 new String[] {
1849 MyNavigationUtil.ID
1850 }, "url = ?", new String[] {
1851 itemUrl
1852 }, null);
1853 if (null != cursor && cursor.moveToFirst()) {
1854 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001855 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001856
1857 ContentValues values = new ContentValues();
1858 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1859 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1860 cursor.getLong(0));
1861 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1862 cr.update(uri, values, null, null);
1863 os.close();
1864 }
1865 } catch (IllegalStateException e) {
1866 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1867 } catch (IOException e) {
1868 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1869 } finally {
1870 if (null != cursor) {
1871 cursor.close();
1872 }
1873 }
1874 return null;
1875 }
1876 }.execute();
1877 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001878 /**
1879 * As the menu can be open when loading state changes
1880 * we must manually update the state of the stop/reload menu
1881 * item
1882 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001883 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001884 if (menu == null) {
1885 return;
1886 }
1887 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001888 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001889 menu.findItem(R.id.stop_menu_id):
1890 menu.findItem(R.id.reload_menu_id);
1891 if (src != null) {
1892 dest.setIcon(src.getIcon());
1893 dest.setTitle(src.getTitle());
1894 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001895 mActivity.invalidateOptionsMenu();
1896 }
1897
1898 public void invalidateOptionsMenu() {
1899 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001900 }
1901
John Reck9c35b9c2012-05-30 10:08:50 -07001902 @Override
1903 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001904 // Software menu key (toolbar key)
1905 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1906 return true;
1907 }
1908
1909 @Override
1910 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001911 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001912 // hold on to the menu reference here; it is used by the page callbacks
1913 // to update the menu based on loading state
1914 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001915 // Note: setVisible will decide whether an item is visible; while
1916 // setEnabled() will decide whether an item is enabled, which also means
1917 // whether the matching shortcut key will function.
1918 switch (mMenuState) {
1919 case EMPTY_MENU:
1920 if (mCurrentMenuState != mMenuState) {
1921 menu.setGroupVisible(R.id.MAIN_MENU, false);
1922 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1923 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1924 }
1925 break;
1926 default:
1927 if (mCurrentMenuState != mMenuState) {
1928 menu.setGroupVisible(R.id.MAIN_MENU, true);
1929 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1930 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1931 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001932 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 break;
1934 }
1935 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001936 mUi.onPrepareOptionsMenu(menu);
1937 }
1938
1939 private void setMenuItemVisibility(Menu menu, int id,
1940 boolean visibility) {
1941 MenuItem item = menu.findItem(id);
1942 if (item != null) {
1943 item.setVisible(visibility);
1944 }
1945 }
1946
1947 private int lookupBookmark(String title, String url) {
1948 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001949 int count = 0;
1950 Cursor cursor = null;
1951 try {
1952 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1953 BookmarksLoader.PROJECTION,
1954 "title = ? OR url = ?",
1955 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001956 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001957 },
1958 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001959
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001960 if (cursor != null)
1961 count = cursor.getCount();
1962
1963 } catch (IllegalStateException e) {
1964 Log.e(LOGTAG, "lookupBookmark ", e);
1965 } finally {
1966 if (null != cursor) {
1967 cursor.close();
1968 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001969 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001970 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001971 }
1972
1973 private void resetMenuItems(Menu menu) {
1974 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1975 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1976
1977 WebView w = getCurrentTopWebView();
1978 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1979
1980 String title = w.getTitle();
1981 String url = w.getUrl();
1982 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1983 bookmark_icon.setChecked(true);
1984 } else {
1985 bookmark_icon.setChecked(false);
1986 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001987 }
1988
Michael Kolb4bf79712011-07-14 14:18:12 -07001989 @Override
1990 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001991 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001992 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001993 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001994 // Following flag is used to identify schemes for which the LIVE_MENU
1995 // items defined in res/menu/browser.xml should be enabled
1996 boolean isLiveScheme = false;
1997 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001998 boolean isSavable = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001999 resetMenuItems(menu);
2000
Michael Kolb4bf79712011-07-14 14:18:12 -07002001 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002002 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002003 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002004 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002005 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002006 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002007 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07002008 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002009
2010 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2011 forward.setEnabled(canGoForward);
2012
Michael Kolb4bf79712011-07-14 14:18:12 -07002013 // decide whether to show the share link option
2014 PackageManager pm = mActivity.getPackageManager();
2015 Intent send = new Intent(Intent.ACTION_SEND);
2016 send.setType("text/plain");
2017 ResolveInfo ri = pm.resolveActivity(send,
2018 PackageManager.MATCH_DEFAULT_ONLY);
2019 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2020
2021 boolean isNavDump = mSettings.enableNavDump();
2022 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2023 nav.setVisible(isNavDump);
2024 nav.setEnabled(isNavDump);
2025
2026 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002027 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2028 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002029 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002030 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2031 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002032 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002033 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2034 isLive && isLiveScheme && isPageFinished);
2035 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002036 isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002037 // history and snapshots item are the members of COMBO menu group,
2038 // so if show history item, only make snapshots item invisible.
2039 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002040
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002041 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002042 }
2043
John Reck9c35b9c2012-05-30 10:08:50 -07002044 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002045 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002046 if (null == getCurrentTopWebView()) {
2047 return false;
2048 }
2049 if (mMenuIsDown) {
2050 // The shortcut action consumes the MENU. Even if it is still down,
2051 // it won't trigger the next shortcut action. In the case of the
2052 // shortcut action triggering a new activity, like Bookmarks, we
2053 // won't get onKeyUp for MENU. So it is important to reset it here.
2054 mMenuIsDown = false;
2055 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002056 if (mUi.onOptionsItemSelected(item)) {
2057 // ui callback handled it
2058 return true;
2059 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002060 switch (item.getItemId()) {
2061 // -- Main menu
2062 case R.id.new_tab_menu_id:
2063 openTabToHomePage();
2064 break;
2065
2066 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002067 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002068 break;
2069
2070 case R.id.goto_menu_id:
2071 editUrl();
2072 break;
2073
2074 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002075 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2076 break;
2077
2078 case R.id.history_menu_id:
2079 bookmarksOrHistoryPicker(ComboViews.History);
2080 break;
2081
2082 case R.id.snapshots_menu_id:
2083 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 break;
2085
Pankaj Garg49b79252014-11-07 17:33:41 -08002086 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002087 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002088 break;
2089
2090 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002091 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002092 stopLoading();
2093 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002094 Tab currentTab = mTabControl.getCurrentTab();
2095 if (currentTab.hasCrashed) {
2096 currentTab.replaceCrashView(getCurrentTopWebView(),
2097 currentTab.getViewContainer());
2098 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002099 getCurrentTopWebView().reload();
2100 }
2101 break;
2102
Michael Kolb8233fac2010-10-26 16:08:53 -07002103 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002104 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002105 break;
2106
2107 case R.id.close_menu_id:
2108 // Close the subwindow if it exists.
2109 if (mTabControl.getCurrentSubWindow() != null) {
2110 dismissSubWindow(mTabControl.getCurrentTab());
2111 break;
2112 }
2113 closeCurrentTab();
2114 break;
2115
kaiyiza8b6dbb2013-07-29 18:11:22 +08002116 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002117 Object[] params = { new String("persist.debug.browsermonkeytest")};
2118 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002119 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002120 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002121 if (ret != null && ret.equals("enable"))
2122 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002123 if (BrowserConfig.getInstance(getContext())
2124 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2125 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002126 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002127 case R.id.homepage_menu_id:
2128 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002129 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002130 break;
2131
2132 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002133 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002134 break;
2135
2136 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002137 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002138 break;
2139
John Reck2bc80422011-06-30 15:11:49 -07002140 case R.id.save_snapshot_menu_id:
2141 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002142 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002143 createScreenshotAsync(
2144 source.getWebView(),
2145 getDesiredThumbnailWidth(mActivity),
2146 getDesiredThumbnailHeight(mActivity),
2147 new ValueCallback<Bitmap>() {
2148 @Override
2149 public void onReceiveValue(Bitmap bitmap) {
2150 new SaveSnapshotTask(source, bitmap).execute();
2151 }
2152 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002153 break;
2154
Michael Kolb8233fac2010-10-26 16:08:53 -07002155 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002156 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002157 break;
2158
John Recke1a03a32011-09-14 17:04:16 -07002159 case R.id.snapshot_go_live:
2160 goLive();
2161 return true;
2162
Michael Kolb8233fac2010-10-26 16:08:53 -07002163 case R.id.share_page_menu_id:
2164 Tab currentTab = mTabControl.getCurrentTab();
2165 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002166 return false;
2167 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002168 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002169 break;
2170
2171 case R.id.dump_nav_menu_id:
2172 getCurrentTopWebView().debugDump();
2173 break;
2174
Michael Kolb8233fac2010-10-26 16:08:53 -07002175 case R.id.zoom_in_menu_id:
2176 getCurrentTopWebView().zoomIn();
2177 break;
2178
2179 case R.id.zoom_out_menu_id:
2180 getCurrentTopWebView().zoomOut();
2181 break;
2182
2183 case R.id.view_downloads_menu_id:
2184 viewDownloads();
2185 break;
2186
John Reck42229bc2011-08-19 13:26:43 -07002187 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002188 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002189 break;
2190
Michael Kolb8233fac2010-10-26 16:08:53 -07002191 case R.id.window_one_menu_id:
2192 case R.id.window_two_menu_id:
2193 case R.id.window_three_menu_id:
2194 case R.id.window_four_menu_id:
2195 case R.id.window_five_menu_id:
2196 case R.id.window_six_menu_id:
2197 case R.id.window_seven_menu_id:
2198 case R.id.window_eight_menu_id:
2199 {
2200 int menuid = item.getItemId();
2201 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2202 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2203 Tab desiredTab = mTabControl.getTab(id);
2204 if (desiredTab != null &&
2205 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002206 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002207 }
2208 break;
2209 }
2210 }
2211 }
2212 break;
2213
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002214 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002215 Bundle bundle = new Bundle();
2216 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2217 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2218 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2219 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2220 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002221 break;
2222
Pankaj Garg49b79252014-11-07 17:33:41 -08002223 case R.id.add_to_homescreen:
2224 final WebView w = getCurrentTopWebView();
2225 final EditText input = new EditText(getContext());
2226 input.setText(w.getTitle());
2227 new AlertDialog.Builder(getContext())
2228 .setTitle("Add to homescreen")
2229 .setMessage("Title")
2230 .setView(input)
2231 .setPositiveButton("Add", new DialogInterface.OnClickListener() {
2232 public void onClick(DialogInterface dialog, int whichButton) {
2233 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2234 getContext(),
2235 w.getUrl(),
2236 input.getText().toString(),
2237 w.getViewportBitmap(),
2238 w.getFavicon()));
2239
2240 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2241 .addCategory(Intent.CATEGORY_HOME));
2242 }})
2243 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
2244 public void onClick(DialogInterface dialog, int whichButton) {
2245 // Do nothing.
2246 }
2247 })
2248 .show();
2249 break;
2250
Michael Kolb8233fac2010-10-26 16:08:53 -07002251 default:
2252 return false;
2253 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002254 return true;
2255 }
2256
John Reck68234a92012-04-19 15:27:12 -07002257 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2258 implements OnCancelListener {
2259
2260 private Tab mTab;
2261 private Dialog mProgressDialog;
2262 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002263 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002264
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002265 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002266 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002267 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002268 }
2269
2270 @Override
2271 protected void onPreExecute() {
2272 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2273 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2274 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002275 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002276 }
2277
2278 @Override
2279 protected Long doInBackground(Void... params) {
2280 if (!mTab.saveViewState(mValues)) {
2281 return null;
2282 }
2283 if (isCancelled()) {
2284 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2285 File file = mActivity.getFileStreamPath(path);
2286 if (!file.delete()) {
2287 file.deleteOnExit();
2288 }
2289 return null;
2290 }
2291 final ContentResolver cr = mActivity.getContentResolver();
2292 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2293 if (result == null) {
2294 return null;
2295 }
2296 long id = ContentUris.parseId(result);
2297 return id;
2298 }
2299
2300 @Override
2301 protected void onPostExecute(Long id) {
2302 if (isCancelled()) {
2303 return;
2304 }
2305 mProgressDialog.dismiss();
2306 if (id == null) {
2307 Toast.makeText(mActivity, R.string.snapshot_failed,
2308 Toast.LENGTH_SHORT).show();
2309 return;
2310 }
2311 Bundle b = new Bundle();
2312 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2313 mUi.showComboView(ComboViews.Snapshots, b);
2314 }
2315
2316 @Override
2317 public void onCancel(DialogInterface dialog) {
2318 cancel(true);
2319 }
2320 }
2321
Michael Kolb80f75082012-04-10 10:50:06 -07002322 @Override
2323 public void toggleUserAgent() {
2324 WebView web = getCurrentWebView();
2325 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002326 }
2327
2328 @Override
2329 public void findOnPage() {
2330 getCurrentTopWebView().showFindDialog(null, true);
2331 }
2332
2333 @Override
2334 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002335 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002336 }
2337
Pankaj Gargeba076f2015-03-25 13:40:59 -07002338 // This function is specifically used from AddBookmark Activity.
2339 // The bookmark activity clears the bitmap after retrieving it.
2340 // The function usage elsewhere will result in breaking bookmark
2341 // functionality.
2342 public static Bitmap getAndReleaseLastBookmarkBitmapFromIntent() {
2343 Bitmap bitmap = mBookmarkBitmap;
2344 mBookmarkBitmap = null;
2345 return bitmap;
2346 }
2347
Michael Kolb80f75082012-04-10 10:50:06 -07002348 @Override
2349 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002350 WebView w = getCurrentTopWebView();
2351 if (w == null)
2352 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002353 final Intent i = createBookmarkCurrentPageIntent(false);
2354 createScreenshotAsync(
2355 w, getDesiredThumbnailWidth(mActivity),
2356 getDesiredThumbnailHeight(mActivity),
2357 new ValueCallback<Bitmap>() {
2358 @Override
2359 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargeba076f2015-03-25 13:40:59 -07002360 mBookmarkBitmap = bitmap;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002361 mActivity.startActivity(i);
2362 }
2363 });
Michael Kolb80f75082012-04-10 10:50:06 -07002364 }
2365
John Recke1a03a32011-09-14 17:04:16 -07002366 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002367 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002368 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002369 boolean onlySingleTabRemaining = false;
2370 if (mTabControl.getTabCount() > 1) {
2371 // destroy the old snapshot tab
2372 closeCurrentTab();
2373 } else {
2374 onlySingleTabRemaining = true;
2375 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002376 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002377 if (onlySingleTabRemaining) {
2378 closeTab(t);
2379 }
2380
Tarun Nainani8eb00912014-07-17 12:28:32 -07002381 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002382 }
2383
luxiaolb40014b2013-07-19 10:01:43 +08002384 private void showExitDialog(final Activity activity) {
2385 new AlertDialog.Builder(activity)
2386 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002387 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002388 .setMessage(R.string.exit_browser_msg)
2389 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2390 public void onClick(DialogInterface dialog, int which) {
2391 activity.moveTaskToBack(true);
2392 dialog.dismiss();
2393 }
2394 })
2395 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2396 public void onClick(DialogInterface dialog, int which) {
2397 activity.finish();
2398 mHandler.postDelayed(new Runnable() {
2399 @Override
2400 public void run() {
luxiaolb40014b2013-07-19 10:01:43 +08002401 mCrashRecoveryHandler.clearState(true);
2402 int pid = android.os.Process.myPid();
2403 android.os.Process.killProcess(pid);
2404 }
2405 }, 300);
2406 dialog.dismiss();
2407 }
2408 })
2409 .show();
2410 }
Michael Kolb315d5022011-10-13 12:47:11 -07002411 @Override
2412 public void showPageInfo() {
2413 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2414 }
2415
John Reck9c35b9c2012-05-30 10:08:50 -07002416 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002417 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002418 // Let the History and Bookmark fragments handle menus they created.
2419 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2420 return false;
2421 }
2422
Michael Kolb8233fac2010-10-26 16:08:53 -07002423 int id = item.getItemId();
2424 boolean result = true;
2425 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002426 // -- Browser context menu
2427 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002428 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002429 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002430 case R.id.copy_link_context_menu_id:
2431 final WebView webView = getCurrentTopWebView();
2432 if (null == webView) {
2433 result = false;
2434 break;
2435 }
2436 final HashMap<String, WebView> hrefMap =
2437 new HashMap<String, WebView>();
2438 hrefMap.put("webview", webView);
2439 final Message msg = mHandler.obtainMessage(
2440 FOCUS_NODE_HREF, id, 0, hrefMap);
2441 webView.requestFocusNodeHref(msg);
2442 break;
2443
2444 default:
2445 // For other context menus
2446 result = onOptionsItemSelected(item);
2447 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002448 return result;
2449 }
2450
2451 /**
2452 * support programmatically opening the context menu
2453 */
2454 public void openContextMenu(View view) {
2455 mActivity.openContextMenu(view);
2456 }
2457
2458 /**
2459 * programmatically open the options menu
2460 */
2461 public void openOptionsMenu() {
2462 mActivity.openOptionsMenu();
2463 }
2464
John Reck9c35b9c2012-05-30 10:08:50 -07002465 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002466 public boolean onMenuOpened(int featureId, Menu menu) {
2467 if (mOptionsMenuOpen) {
2468 if (mConfigChanged) {
2469 // We do not need to make any changes to the state of the
2470 // title bar, since the only thing that happened was a
2471 // change in orientation
2472 mConfigChanged = false;
2473 } else {
2474 if (!mExtendedMenuOpen) {
2475 mExtendedMenuOpen = true;
2476 mUi.onExtendedMenuOpened();
2477 } else {
2478 // Switching the menu back to icon view, so show the
2479 // title bar once again.
2480 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002481 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002482 }
2483 }
2484 } else {
2485 // The options menu is closed, so open it, and show the title
2486 mOptionsMenuOpen = true;
2487 mConfigChanged = false;
2488 mExtendedMenuOpen = false;
2489 mUi.onOptionsMenuOpened();
2490 }
2491 return true;
2492 }
2493
John Reck9c35b9c2012-05-30 10:08:50 -07002494 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002495 public void onOptionsMenuClosed(Menu menu) {
2496 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002497 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002498 }
2499
John Reck9c35b9c2012-05-30 10:08:50 -07002500 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002501 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002502 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002503 }
2504
2505 // Helper method for getting the top window.
2506 @Override
2507 public WebView getCurrentTopWebView() {
2508 return mTabControl.getCurrentTopWebView();
2509 }
2510
2511 @Override
2512 public WebView getCurrentWebView() {
2513 return mTabControl.getCurrentWebView();
2514 }
2515
2516 /*
2517 * This method is called as a result of the user selecting the options
2518 * menu to see the download window. It shows the download window on top of
2519 * the current window.
2520 */
2521 void viewDownloads() {
2522 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2523 mActivity.startActivity(intent);
2524 }
2525
John Reck30b065e2011-07-19 10:58:05 -07002526 int getActionModeHeight() {
2527 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2528 new int[] { android.R.attr.actionBarSize });
2529 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2530 actionBarSizeTypedArray.recycle();
2531 return size;
2532 }
2533
Michael Kolb8233fac2010-10-26 16:08:53 -07002534 // action mode
2535
John Reck9c35b9c2012-05-30 10:08:50 -07002536 @Override
2537 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002538 mUi.onActionModeStarted(mode);
2539 mActionMode = mode;
2540 }
2541
2542 /*
2543 * True if a custom ActionMode (i.e. find or select) is in use.
2544 */
2545 @Override
2546 public boolean isInCustomActionMode() {
2547 return mActionMode != null;
2548 }
2549
2550 /*
2551 * End the current ActionMode.
2552 */
2553 @Override
2554 public void endActionMode() {
2555 if (mActionMode != null) {
2556 mActionMode.finish();
2557 }
2558 }
2559
2560 /*
2561 * Called by find and select when they are finished. Replace title bars
2562 * as necessary.
2563 */
John Reck9c35b9c2012-05-30 10:08:50 -07002564 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002565 public void onActionModeFinished(ActionMode mode) {
2566 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002567 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002568 mActionMode = null;
2569 }
2570
2571 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002572 final Tab tab = getCurrentTab();
2573 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002574 }
2575
2576 // bookmark handling
2577
2578 /**
2579 * add the current page as a bookmark to the given folder id
2580 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002581 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002582 * bookmarked, and if it is, edit that bookmark. If false, and
2583 * the site is already bookmarked, do not attempt to edit the
2584 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002585 */
2586 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002587 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002588 WebView w = getCurrentTopWebView();
2589 if (w == null) {
2590 return null;
2591 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002592 Intent i = new Intent(mActivity,
2593 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002594 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2595 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2596 String touchIconUrl = w.getTouchIconUrl();
2597 if (touchIconUrl != null) {
2598 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2599 WebSettings settings = w.getSettings();
2600 if (settings != null) {
2601 i.putExtra(AddBookmarkPage.USER_AGENT,
2602 settings.getUserAgentString());
2603 }
2604 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002605 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002607 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002608 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2609 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002610 // Put the dialog at the upper right of the screen, covering the
2611 // star on the title bar.
2612 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002613 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002614 }
2615
2616 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002617 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002618 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002620 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002621 }
2622
Vivek Sekharb54614f2014-05-01 19:03:37 -07002623 @Override
2624 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2625 boolean capture) {
2626 mUploadHandler = new UploadHandler(this);
2627 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2628 }
2629
Michael Kolb8233fac2010-10-26 16:08:53 -07002630 // thumbnails
2631
2632 /**
2633 * Return the desired width for thumbnail screenshots, which are stored in
2634 * the database, and used on the bookmarks screen.
2635 * @param context Context for finding out the density of the screen.
2636 * @return desired width for thumbnail screenshot.
2637 */
2638 static int getDesiredThumbnailWidth(Context context) {
2639 return context.getResources().getDimensionPixelOffset(
2640 R.dimen.bookmarkThumbnailWidth);
2641 }
2642
2643 /**
2644 * Return the desired height for thumbnail screenshots, which are stored in
2645 * the database, and used on the bookmarks screen.
2646 * @param context Context for finding out the density of the screen.
2647 * @return desired height for thumbnail screenshot.
2648 */
2649 static int getDesiredThumbnailHeight(Context context) {
2650 return context.getResources().getDimensionPixelOffset(
2651 R.dimen.bookmarkThumbnailHeight);
2652 }
2653
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002654 static void createScreenshotAsync(WebView view, int width, int height,
2655 final ValueCallback<Bitmap> cb) {
2656 if (view == null || width == 0 || height == 0) {
2657 return;
2658 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002659 view.getContentBitmapAsync(
2660 (float) width / view.getWidth(),
2661 new Rect(),
2662 new ValueCallback<Bitmap>() {
2663 @Override
2664 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002665 if (bitmap != null)
2666 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2667 cb.onReceiveValue(bitmap);
2668 }});
2669 }
2670
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002671 private void updateScreenshot(final Tab tab) {
2672 createScreenshotAsync(
2673 tab.getWebView(),
2674 getDesiredThumbnailWidth(mActivity),
2675 getDesiredThumbnailHeight(mActivity),
2676 new ValueCallback<Bitmap>() {
2677 @Override
2678 public void onReceiveValue(Bitmap bitmap) {
2679 updateScreenshot(tab, bitmap);
2680 }
2681 });
2682 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002683
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002684 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002685 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002686 // FIXME: Would like to make sure there is actually something to
2687 // draw, but the API for that (WebViewCore.pictureReady()) is not
2688 // currently accessible here.
2689
John Reck34ef2672011-02-10 11:30:55 -08002690 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002691 if (view == null) {
2692 // Tab was destroyed
2693 return;
2694 }
John Reck34ef2672011-02-10 11:30:55 -08002695 final String url = tab.getUrl();
2696 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002697 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002698 if (TextUtils.isEmpty(url)) {
2699 return;
2700 }
2701
kaiyiz6e5b3e02013-08-19 20:02:01 +08002702 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002703 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002704 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002705 }
John Reck34ef2672011-02-10 11:30:55 -08002706 // Only update thumbnails for web urls (http(s)://), not for
2707 // about:, javascript:, data:, etc...
2708 // Unless it is a bookmarked site, then always update
2709 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2710 return;
2711 }
2712
kaiyiz6e5b3e02013-08-19 20:02:01 +08002713 if (url != null && mUpdateMyNavThumbnailUrl != null
2714 && Patterns.WEB_URL.matcher(url).matches()
2715 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2716 String urlHost = (new WebAddress(url)).getHost();
2717 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2718 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2719 || bookmarkHost.length() == 0) {
2720 return;
2721 }
2722 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2723 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2724 bookmarkHost.length());
2725 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2726 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2727 if (!bookmarkDomain.equals(urlDomain)) {
2728 return;
2729 }
2730 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002731 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002732 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2733 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2734 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2735 500);
2736 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 return;
2738 }
2739
2740 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002741 new AsyncTask<Void, Void, Void>() {
2742 @Override
2743 protected Void doInBackground(Void... unused) {
2744 Cursor cursor = null;
2745 try {
2746 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002747 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2748 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2749 : url);
2750 if (mUpdateMyNavThumbnail) {
2751 mUpdateMyNavThumbnail = false;
2752 mUpdateMyNavThumbnailUrl = null;
2753 }
John Reck34ef2672011-02-10 11:30:55 -08002754 if (cursor != null && cursor.moveToFirst()) {
2755 final ByteArrayOutputStream os =
2756 new ByteArrayOutputStream();
2757 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002758
John Reck34ef2672011-02-10 11:30:55 -08002759 ContentValues values = new ContentValues();
2760 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002761
John Reck34ef2672011-02-10 11:30:55 -08002762 do {
John Reck617fd832011-02-16 14:35:59 -08002763 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002764 cr.update(Images.CONTENT_URI, values, null, null);
2765 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002766 }
John Reck34ef2672011-02-10 11:30:55 -08002767 } catch (IllegalStateException e) {
2768 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002769 } catch (SQLiteException s) {
2770 // Added for possible error when user tries to remove the same bookmark
2771 // that is being updated with a screen shot
2772 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002773 } finally {
2774 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002775 }
John Reck34ef2672011-02-10 11:30:55 -08002776 return null;
2777 }
2778 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002779 }
2780
2781 private class Copy implements OnMenuItemClickListener {
2782 private CharSequence mText;
2783
John Reck9c35b9c2012-05-30 10:08:50 -07002784 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002785 public boolean onMenuItemClick(MenuItem item) {
2786 copy(mText);
2787 return true;
2788 }
2789
2790 public Copy(CharSequence toCopy) {
2791 mText = toCopy;
2792 }
2793 }
2794
Leon Scroggins63c02662010-11-18 15:16:27 -05002795 private static class Download implements OnMenuItemClickListener {
2796 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002797 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002798 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002799 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002800 private static final String FALLBACK_EXTENSION = "dat";
2801 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002802
John Reck9c35b9c2012-05-30 10:08:50 -07002803 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002804 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002805 if (DataUri.isDataUri(mText)) {
2806 saveDataUri();
2807 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002808 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002809 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002810 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002811 return true;
2812 }
2813
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002814 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2815 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002816 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002817 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002818 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002819 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002820 }
George Mount387d45d2011-10-07 15:57:53 -07002821
2822 /**
2823 * Treats mText as a data URI and writes its contents to a file
2824 * based on the current time.
2825 */
2826 private void saveDataUri() {
2827 FileOutputStream outputStream = null;
2828 try {
2829 DataUri uri = new DataUri(mText);
2830 File target = getTarget(uri);
2831 outputStream = new FileOutputStream(target);
2832 outputStream.write(uri.getData());
2833 final DownloadManager manager =
2834 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2835 manager.addCompletedDownload(target.getName(),
2836 mActivity.getTitle().toString(), false,
2837 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002838 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002839 } catch (IOException e) {
2840 Log.e(LOGTAG, "Could not save data URL");
2841 } finally {
2842 if (outputStream != null) {
2843 try {
2844 outputStream.close();
2845 } catch (IOException e) {
2846 // ignore close errors
2847 }
2848 }
2849 }
2850 }
2851
2852 /**
2853 * Creates a File based on the current time stamp and uses
2854 * the mime type of the DataUri to get the extension.
2855 */
2856 private File getTarget(DataUri uri) throws IOException {
2857 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002858 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002859 String nameBase = format.format(new Date());
2860 String mimeType = uri.getMimeType();
2861 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2862 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2863 if (extension == null) {
2864 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2865 extension = FALLBACK_EXTENSION;
2866 }
2867 extension = "." + extension; // createTempFile needs the '.'
2868 File targetFile = File.createTempFile(nameBase, extension, dir);
2869 return targetFile;
2870 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002871 }
2872
Cary Clark8974d282010-11-22 10:46:05 -05002873 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002874 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002875
John Reck9c35b9c2012-05-30 10:08:50 -07002876 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002877 public boolean onMenuItemClick(MenuItem item) {
2878 if (mWebView != null) {
2879 return mWebView.selectText();
2880 }
2881 return false;
2882 }
2883
2884 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002885 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002886 }
2887
2888 }
2889
Michael Kolb8233fac2010-10-26 16:08:53 -07002890 /********************** TODO: UI stuff *****************************/
2891
2892 // these methods have been copied, they still need to be cleaned up
2893
2894 /****************** tabs ***************************************************/
2895
2896 // basic tab interactions:
2897
2898 // it is assumed that tabcontrol already knows about the tab
2899 protected void addTab(Tab tab) {
2900 mUi.addTab(tab);
2901 }
2902
2903 protected void removeTab(Tab tab) {
2904 mUi.removeTab(tab);
2905 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002906 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002907 }
2908
Michael Kolbf2055602011-04-09 17:20:03 -07002909 @Override
2910 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002911 // monkey protection against delayed start
2912 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002913
2914 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2915 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002916 mTabControl.setCurrentTab(tab);
2917 // the tab is guaranteed to have a webview after setCurrentTab
2918 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002919
2920
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002921 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002922 //Purge active tabs
2923 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002924 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002925 }
2926
John Reck8bcafc12011-08-29 16:43:02 -07002927 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002928 Tab current = mTabControl.getCurrentTab();
2929 if (current != null
2930 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002931 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002932 }
2933 }
2934
John Reck26b18322011-06-21 13:08:58 -07002935 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002936 //Cancel navscreen request
2937 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002938 // Dismiss the subwindow if applicable.
2939 dismissSubWindow(appTab);
2940 // Since we might kill the WebView, remove it from the
2941 // content view first.
2942 mUi.detachTab(appTab);
2943 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002944 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002945 // TODO: analyze why the remove and add are necessary
2946 mUi.attachTab(appTab);
2947 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002948 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002949 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002950 } else {
2951 // If the tab was the current tab, we have to attach
2952 // it to the view system again.
2953 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002954 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002955 }
2956 }
2957
2958 // Remove the sub window if it exists. Also called by TabControl when the
2959 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002960 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002961 public void dismissSubWindow(Tab tab) {
2962 removeSubWindow(tab);
2963 // dismiss the subwindow. This will destroy the WebView.
2964 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002965 WebView wv = getCurrentTopWebView();
2966 if (wv != null) {
2967 wv.requestFocus();
2968 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002969 }
2970
2971 @Override
2972 public void removeSubWindow(Tab t) {
2973 if (t.getSubWebView() != null) {
2974 mUi.removeSubWindow(t.getSubViewContainer());
2975 }
2976 }
2977
2978 @Override
2979 public void attachSubWindow(Tab tab) {
2980 if (tab.getSubWebView() != null) {
2981 mUi.attachSubWindow(tab.getSubViewContainer());
2982 getCurrentTopWebView().requestFocus();
2983 }
2984 }
2985
Mathew Inwood29721c22011-06-29 17:55:24 +01002986 private Tab showPreloadedTab(final UrlData urlData) {
2987 if (!urlData.isPreloaded()) {
2988 return null;
2989 }
2990 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2991 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2992 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002993 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002994 // Could not submit query. Fallback to regular tab creation
2995 tabControl.destroy();
2996 return null;
2997 }
2998 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002999 // check tab count and make room for new tab
3000 if (!mTabControl.canCreateNewTab()) {
3001 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
3002 if (leastUsed != null) {
3003 closeTab(leastUsed);
3004 }
3005 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003006 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01003007 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01003008 mTabControl.addPreloadedTab(t);
3009 addTab(t);
3010 setActiveTab(t);
3011 return t;
3012 }
3013
Michael Kolb7bcafde2011-05-09 13:55:59 -07003014 // open a non inconito tab with the given url data
3015 // and set as active tab
3016 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01003017 Tab tab = showPreloadedTab(urlData);
3018 if (tab == null) {
3019 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07003020 if ((tab != null) && !urlData.isEmpty()) {
3021 loadUrlDataIn(tab, urlData);
3022 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003023 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003024 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07003025 }
3026
Michael Kolb843510f2010-12-09 10:51:49 -08003027 @Override
3028 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003029 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07003030 }
3031
Michael Kolb8233fac2010-10-26 16:08:53 -07003032 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07003033 public Tab openIncognitoTab() {
3034 return openTab(INCOGNITO_URI, true, true, false);
3035 }
3036
3037 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003038 public Tab openTab(String url, boolean incognito, boolean setActive,
3039 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003040 return openTab(url, incognito, setActive, useCurrent, null);
3041 }
3042
3043 @Override
3044 public Tab openTab(String url, Tab parent, boolean setActive,
3045 boolean useCurrent) {
3046 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3047 setActive, useCurrent, parent);
3048 }
3049
3050 public Tab openTab(String url, boolean incognito, boolean setActive,
3051 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003052 Tab tab = createNewTab(incognito, setActive, useCurrent);
3053 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003054 if (parent instanceof SnapshotTab) {
3055 addTab(tab);
3056 if (setActive)
3057 setActiveTab(tab);
3058 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003059 parent.addChildTab(tab);
3060 }
Michael Kolb519d2282011-05-09 17:03:19 -07003061 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003062 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003063 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003064 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003065 return tab;
3066 }
3067
3068 // this method will attempt to create a new tab
3069 // incognito: private browsing tab
3070 // setActive: ste tab as current tab
3071 // useCurrent: if no new tab can be created, return current tab
3072 private Tab createNewTab(boolean incognito, boolean setActive,
3073 boolean useCurrent) {
3074 Tab tab = null;
3075 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05003076 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003077 addTab(tab);
3078 if (setActive) {
3079 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07003080 } else {
3081 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003082 }
3083 } else {
3084 if (useCurrent) {
3085 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07003086 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003087 } else {
3088 mUi.showMaxTabsWarning();
3089 }
3090 }
3091 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003092 }
3093
John Reck2bc80422011-06-30 15:11:49 -07003094 @Override
3095 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3096 SnapshotTab tab = null;
3097 if (mTabControl.canCreateNewTab()) {
3098 tab = mTabControl.createSnapshotTab(snapshotId);
3099 addTab(tab);
3100 if (setActive) {
3101 setActiveTab(tab);
3102 }
3103 } else {
3104 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003105 }
3106 return tab;
3107 }
3108
Michael Kolb8233fac2010-10-26 16:08:53 -07003109 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003110 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003111 * @return boolean True if we successfully switched to a different tab. If
3112 * the indexth tab is null, or if that tab is the same as
3113 * the current one, return false.
3114 */
3115 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003116 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003117 Tab currentTab = mTabControl.getCurrentTab();
3118 if (tab == null || tab == currentTab) {
3119 return false;
3120 }
3121 setActiveTab(tab);
3122 return true;
3123 }
3124
3125 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003126 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003127 closeCurrentTab(false);
3128 }
3129
3130 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003131 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003132 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003133 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003134 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003135 return;
3136 }
Michael Kolbc831b632011-05-11 09:30:34 -07003137 final Tab current = mTabControl.getCurrentTab();
3138 final int pos = mTabControl.getCurrentPosition();
3139 Tab newTab = current.getParent();
3140 if (newTab == null) {
3141 newTab = mTabControl.getTab(pos + 1);
3142 if (newTab == null) {
3143 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003144 }
3145 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003146 if (andQuit) {
3147 mTabControl.setCurrentTab(newTab);
3148 closeTab(current);
3149 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003150 // Close window
3151 closeTab(current);
3152 }
3153 }
3154
3155 /**
3156 * Close the tab, remove its associated title bar, and adjust mTabControl's
3157 * current tab to a valid value.
3158 */
3159 @Override
3160 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003161 if (tab == mTabControl.getCurrentTab()) {
3162 closeCurrentTab();
3163 } else {
3164 removeTab(tab);
3165 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003166 }
3167
Afzal Najamd4e33312012-04-26 01:54:01 -04003168 /**
3169 * Close all tabs except the current one
3170 */
3171 @Override
3172 public void closeOtherTabs() {
3173 int inactiveTabs = mTabControl.getTabCount() - 1;
3174 for (int i = inactiveTabs; i >= 0; i--) {
3175 Tab tab = mTabControl.getTab(i);
3176 if (tab != mTabControl.getCurrentTab()) {
3177 removeTab(tab);
3178 }
3179 }
3180 }
3181
Michael Kolb8233fac2010-10-26 16:08:53 -07003182 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003183 protected void loadUrlFromContext(String url) {
3184 Tab tab = getCurrentTab();
3185 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003186 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003187 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003188 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003189 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003190 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003191 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003192 }
3193 }
3194 }
3195
3196 /**
3197 * Load the URL into the given WebView and update the title bar
3198 * to reflect the new load. Call this instead of WebView.loadUrl
3199 * directly.
3200 * @param view The WebView used to load url.
3201 * @param url The URL to load.
3202 */
John Reck71e51422011-07-01 16:49:28 -07003203 @Override
3204 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003205 loadUrl(tab, url, null);
3206 }
3207
3208 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3209 if (tab != null) {
3210 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003211 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003212 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003213 if (tab.hasCrashed) {
3214 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3215 }
Michael Kolba53c9892011-10-05 13:31:40 -07003216 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003217 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003218 }
3219
3220 /**
3221 * Load UrlData into a Tab and update the title bar to reflect the new
3222 * load. Call this instead of UrlData.loadIn directly.
3223 * @param t The Tab used to load.
3224 * @param data The UrlData being loaded.
3225 */
3226 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003227 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003228 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003229 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003230 } else {
John Reck38b39652012-06-05 09:22:59 -07003231 if (t != null && data.mDisableUrlOverride) {
3232 t.disableUrlOverridingForLoad();
3233 }
John Reck26b18322011-06-21 13:08:58 -07003234 loadUrl(t, data.mUrl, data.mHeaders);
3235 }
3236 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003237 }
3238
John Reck30c714c2010-12-16 17:30:34 -08003239 @Override
3240 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003241 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003242 //In case of tab can go back (aka tab has navigation entry) do nothing
3243 //else just load homepage in current tab.
3244 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003245 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003246 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003247 }
3248
3249 void goBackOnePageOrQuit() {
3250 Tab current = mTabControl.getCurrentTab();
3251 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003252 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3253 showExitDialog(mActivity);
3254 } else {
3255 /*
3256 * Instead of finishing the activity, simply push this to the back
3257 * of the stack and let ActivityManager to choose the foreground
3258 * activity. As BrowserActivity is singleTask, it will be always the
3259 * root of the task. So we can use either true or false for
3260 * moveTaskToBack().
3261 */
3262 mActivity.moveTaskToBack(true);
3263 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003264 return;
3265 }
John Reckef654f12011-07-12 16:42:08 -07003266 if (current.canGoBack()) {
3267 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003268 } else {
3269 // Check to see if we are closing a window that was created by
3270 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003271 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003272 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003273 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003274 // Now we close the other tab
3275 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003276 } else if (BrowserConfig.getInstance(getContext())
3277 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3278 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003279 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003280 if ((current.getAppId() != null) || current.closeOnBack()) {
3281 closeCurrentTab(true);
3282 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003283 /*
3284 * Instead of finishing the activity, simply push this to the back
3285 * of the stack and let ActivityManager to choose the foreground
3286 * activity. As BrowserActivity is singleTask, it will be always the
3287 * root of the task. So we can use either true or false for
3288 * moveTaskToBack().
3289 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003290 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003291 }
3292 }
3293 }
3294
3295 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003296 * 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 getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003300 int pos = mTabControl.getCurrentPosition() + 1;
3301 if (pos >= mTabControl.getTabCount()) {
3302 pos = 0;
3303 }
3304 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003305 }
3306
3307 /**
3308 * helper method for key handler
3309 * returns the current tab if it can't advance
3310 */
Michael Kolbc831b632011-05-11 09:30:34 -07003311 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003312 int pos = mTabControl.getCurrentPosition() - 1;
3313 if ( pos < 0) {
3314 pos = mTabControl.getTabCount() - 1;
3315 }
3316 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003317 }
3318
John Reckbcef87f2012-02-03 14:58:34 -08003319 boolean isMenuOrCtrlKey(int keyCode) {
3320 return (KeyEvent.KEYCODE_MENU == keyCode)
3321 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3322 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3323 }
3324
Michael Kolb0035fad2011-03-14 13:25:28 -07003325 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003326 * handle key events in browser
3327 *
3328 * @param keyCode
3329 * @param event
3330 * @return true if handled, false to pass to super
3331 */
John Reck9c35b9c2012-05-30 10:08:50 -07003332 @Override
3333 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003334 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3335 // Hardware menu key
Enrico Ros1f5a0952014-11-18 20:15:48 -08003336 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
Pankaj Garg49b79252014-11-07 17:33:41 -08003337 true, false);
3338 return true;
3339 }
3340
Cary Clark160bbb92011-01-10 11:17:07 -05003341 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003342 // Even if MENU is already held down, we need to call to super to open
3343 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003344 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003345 mMenuIsDown = true;
3346 return false;
3347 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003348
Cary Clark8ff8c662010-12-29 15:03:05 -05003349 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003350 Tab tab = getCurrentTab();
3351 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003352
Cary Clark160bbb92011-01-10 11:17:07 -05003353 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3354 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003355
Michael Kolb8233fac2010-10-26 16:08:53 -07003356 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003357 case KeyEvent.KEYCODE_TAB:
3358 if (event.isCtrlPressed()) {
3359 if (event.isShiftPressed()) {
3360 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003361 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003362 } else {
3363 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003364 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003365 }
3366 return true;
3367 }
3368 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003369 case KeyEvent.KEYCODE_SPACE:
3370 // WebView/WebTextView handle the keys in the KeyDown. As
3371 // the Activity's shortcut keys are only handled when WebView
3372 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003373 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003374 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003375 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003376 pageDown();
3377 }
3378 return true;
3379 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003380 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003381 event.startTracking();
3382 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003383 case KeyEvent.KEYCODE_FORWARD:
3384 if (!noModifiers) break;
3385 tab.goForward();
3386 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003387 case KeyEvent.KEYCODE_DPAD_LEFT:
3388 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003389 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003390 return true;
3391 }
3392 break;
3393 case KeyEvent.KEYCODE_DPAD_RIGHT:
3394 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003395 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003396 return true;
3397 }
3398 break;
3399 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003400 if (ctrl) {
3401 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003402 return true;
3403 }
3404 break;
Michael Kolba4183062011-01-16 10:43:21 -08003405// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003406 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003407 if (ctrl ) {
3408 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003409 return true;
3410 }
3411 break;
Michael Kolba4183062011-01-16 10:43:21 -08003412// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003413// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003414// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003415// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003416// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003417// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003418// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003419// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003420// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003421// case KeyEvent.KEYCODE_M: // unused
3422// case KeyEvent.KEYCODE_N: // in Chrome: new window
3423// case KeyEvent.KEYCODE_O: // in Chrome: open file
3424// case KeyEvent.KEYCODE_P: // in Chrome: print page
3425// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003426// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003427// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3428 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003429 // we can't use the ctrl/shift flags, they check for
3430 // exclusive use of a modifier
3431 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003432 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003433 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003434 } else {
3435 openTabToHomePage();
3436 }
3437 return true;
3438 }
3439 break;
3440// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3441// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003442// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003443// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3444// case KeyEvent.KEYCODE_Y: // unused
3445// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003446 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003447 // it is a regular key and webview is not null
3448 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003449 }
3450
John Reck9c35b9c2012-05-30 10:08:50 -07003451 @Override
3452 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003453 switch(keyCode) {
3454 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003455 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003456 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003457 return true;
3458 }
3459 break;
3460 }
3461 return false;
3462 }
3463
John Reck9c35b9c2012-05-30 10:08:50 -07003464 @Override
3465 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003466 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003467 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003468 if (KeyEvent.KEYCODE_MENU == keyCode
3469 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003470 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003471 }
3472 }
Cary Clark160bbb92011-01-10 11:17:07 -05003473 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003474 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003475 case KeyEvent.KEYCODE_BACK:
3476 if (event.isTracking() && !event.isCanceled()) {
3477 onBackKey();
3478 return true;
3479 }
3480 break;
3481 }
3482 return false;
3483 }
3484
3485 public boolean isMenuDown() {
3486 return mMenuIsDown;
3487 }
3488
John Reck9c35b9c2012-05-30 10:08:50 -07003489 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003490 public void setupAutoFill(Message message) {
3491 // Open the settings activity at the AutoFill profile fragment so that
3492 // the user can create a new profile. When they return, we will dispatch
3493 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003494 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003495 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3496 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003497 }
John Reckb3417f02011-01-14 11:01:05 -08003498
John Reck9c35b9c2012-05-30 10:08:50 -07003499 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003500 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003501 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003502 return true;
3503 }
3504
John Reck1cf4b792011-07-26 10:22:22 -07003505 @Override
3506 public boolean shouldCaptureThumbnails() {
3507 return mUi.shouldCaptureThumbnails();
3508 }
3509
Michael Kolbc3af0672011-08-09 10:24:41 -07003510 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003511 public boolean supportsVoice() {
3512 PackageManager pm = mActivity.getPackageManager();
3513 List activities = pm.queryIntentActivities(new Intent(
3514 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3515 return activities.size() != 0;
3516 }
3517
3518 @Override
3519 public void startVoiceRecognizer() {
3520 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003521 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003522 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3523 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3524 mActivity.startActivityForResult(voice, VOICE_RESULT);
3525 }
3526
Pankaj Garg7b279f62014-08-12 14:47:18 -07003527 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003528 if (mLevel == level)
3529 return;
3530 mLevel = level;
3531 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003532 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3533 lp.dimAmount = level;
3534 mActivity.getWindow().setAttributes(lp);
3535 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3536 } else {
3537 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3538 }
3539 }
3540
Michael Kolb0b129122012-06-04 16:31:58 -07003541 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003542 public void setBlockEvents(boolean block) {
3543 mBlockEvents = block;
3544 }
3545
John Reck9c35b9c2012-05-30 10:08:50 -07003546 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003547 public boolean dispatchKeyEvent(KeyEvent event) {
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 dispatchKeyShortcutEvent(KeyEvent event) {
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 dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003558 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003559 }
3560
John Reck9c35b9c2012-05-30 10:08:50 -07003561 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003562 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003563 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003564 }
3565
John Reck9c35b9c2012-05-30 10:08:50 -07003566 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003567 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003568 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003569 }
3570
Pankaj Garg49b79252014-11-07 17:33:41 -08003571 @Override
3572 public boolean shouldShowAppMenu() {
3573 return true;
3574 }
3575
3576 @Override
3577 public int getMenuThemeResourceId() {
3578 return R.style.OverflowMenuTheme;
3579 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003580}