blob: 53c7e34b2fb00b6dd554e775d5afd5229003bf30 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
Dave Tharp851e8d52015-04-26 14:58:18 -07004 * Copyright (c) 2015 The Linux Foundation, All rights reserved.
Site Maoabb7bd32015-03-20 15:34:02 -07005 *
Michael Kolb8233fac2010-10-26 16:08:53 -07006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Bijan Amirzada41242f22014-03-21 12:12:18 -070019package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070020
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080022
luxiaolb40014b2013-07-19 10:01:43 +080023import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070025import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070026import android.app.ProgressDialog;
Vivek Sekharce2a4832014-03-26 13:26:53 -070027import android.content.Context;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070030import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070031import android.content.ContentValues;
John Reck68234a92012-04-19 15:27:12 -070032import android.content.DialogInterface;
33import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.content.Intent;
Site Maoabb7bd32015-03-20 15:34:02 -070035import android.content.IntentFilter;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
38import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070039import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050040import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.database.Cursor;
42import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020043import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070044import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080045import android.graphics.BitmapFactory;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080046import android.graphics.Rect;
Tarun Nainanif5563b62015-01-21 18:52:59 -080047import android.media.AudioManager;
kaiyiza016da12013-08-26 17:50:22 +080048import android.net.ConnectivityManager;
49import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.net.Uri;
51import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080052import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080053import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.os.AsyncTask;
Tarun Nainanif5563b62015-01-21 18:52:59 -080055import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070057import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.os.Handler;
59import android.os.Message;
60import android.os.PowerManager;
61import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.provider.ContactsContract;
63import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080064import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070065import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070066import android.text.TextUtils;
67import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080068import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070069import android.view.ActionMode;
70import android.view.ContextMenu;
71import android.view.ContextMenu.ContextMenuInfo;
72import android.view.Gravity;
73import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070074import android.view.Menu;
75import android.view.MenuInflater;
76import android.view.MenuItem;
77import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070078import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070080import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070081import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070082import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080083import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080084import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050085import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070086
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.CookieManager;
88import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080089import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080090import org.codeaurora.swe.HttpAuthHandler;
91import org.codeaurora.swe.SslErrorHandler;
92import org.codeaurora.swe.WebSettings;
93import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070094import org.codeaurora.swe.WebBackForwardList;
95import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080096
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070097import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070098import com.android.browser.R;
99import com.android.browser.IntentHandler.UrlData;
100import com.android.browser.UI.ComboViews;
Dave Tharp851e8d52015-04-26 14:58:18 -0700101import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700102import com.android.browser.mynavigation.AddMyNavigationPage;
103import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700104import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700105import com.android.browser.platformsupport.BrowserContract;
106import com.android.browser.platformsupport.WebAddress;
107import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800108import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700109import com.android.browser.provider.BrowserProvider2.Thumbnails;
110import com.android.browser.provider.SnapshotProvider.Snapshots;
111import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800112import com.android.browser.appmenu.AppMenuHandler;
113import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700114
Michael Kolb8233fac2010-10-26 16:08:53 -0700115import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700116import java.io.File;
117import java.io.FileOutputStream;
118import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700119import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700120import java.text.DateFormat;
121import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700122import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700123import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700124import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800125import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200126import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700127import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700128
129/**
130 * Controller for browser
131 */
132public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800133 implements WebViewController, UiController, ActivityController,
134 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700135
136 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800137 private static final String SEND_APP_ID_EXTRA =
138 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Vivek Sekhared791da2015-02-22 12:39:05 -0800139 private static final String INCOGNITO_URI = "chrome://incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800140
Vivek Sekharb54614f2014-05-01 19:03:37 -0700141 // Remind switch to data connection if wifi is unavailable
142 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800143
Michael Kolb8233fac2010-10-26 16:08:53 -0700144 // public message ids
145 public final static int LOAD_URL = 1001;
146 public final static int STOP_LOAD = 1002;
147
148 // Message Ids
149 private static final int FOCUS_NODE_HREF = 102;
150 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800151 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700152
153 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
154
155 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800156 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700157
158 private static final int EMPTY_MENU = -1;
159
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700161 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 final static int PREFERENCES_PAGE = 3;
163 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000164 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700165 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800166 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000167
Michael Kolb8233fac2010-10-26 16:08:53 -0700168 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
169
170 // As the ids are dynamically created, we can't guarantee that they will
171 // be in sequence, so this static array maps ids to a window number.
172 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
173 { R.id.window_one_menu_id, R.id.window_two_menu_id,
174 R.id.window_three_menu_id, R.id.window_four_menu_id,
175 R.id.window_five_menu_id, R.id.window_six_menu_id,
176 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
177
178 // "source" parameter for Google search through search key
179 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
180 // "source" parameter for Google search through simplily type
181 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
182
George Mount387d45d2011-10-07 15:57:53 -0700183 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700184 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
185
John Reckd7dd9b22011-08-30 09:18:29 -0700186 // A bitmap that is re-used in createScreenshot as scratch space
187 private static Bitmap sThumbnailBitmap;
188
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private Activity mActivity;
190 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700191 private HomepageHandler mHomepageHandler;
192 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 private BrowserSettings mSettings;
194 private WebViewFactory mFactory;
195
196 private WakeLock mWakeLock;
197
198 private UrlHandler mUrlHandler;
199 private UploadHandler mUploadHandler;
200 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700201 private PageDialogsHandler mPageDialogsHandler;
202 private NetworkStateHandler mNetworkHandler;
203
Ben Murdoch8029a772010-11-16 11:58:21 +0000204 private Message mAutoFillSetupMessage;
205
kaiyiza016da12013-08-26 17:50:22 +0800206 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700207
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 // FIXME, temp address onPrepareMenu performance problem.
209 // When we move everything out of view, we should rewrite this.
210 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700211 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 private int mOldMenuState = EMPTY_MENU;
213 private Menu mCachedMenu;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 private boolean mMenuIsDown;
216
Pankaj Garg49b79252014-11-07 17:33:41 -0800217 private boolean mWasInPageLoad = false;
218 private AppMenuHandler mAppMenuHandler;
219
Michael Kolb8233fac2010-10-26 16:08:53 -0700220 // For select and find, we keep track of the ActionMode so that
221 // finish() can be called as desired.
222 private ActionMode mActionMode;
223
224 /**
225 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
226 * of whether the configuration has changed. The first onMenuOpened call
227 * after a configuration change is simply a reopening of the same menu
228 * (i.e. mIconView did not change).
229 */
230 private boolean mConfigChanged;
231
232 /**
233 * Keeps track of whether the options menu is open. This is important in
234 * determining whether to show or hide the title bar overlay
235 */
236 private boolean mOptionsMenuOpen;
237
238 /**
239 * Whether or not the options menu is in its bigger, popup menu form. When
240 * true, we want the title bar overlay to be gone. When false, we do not.
241 * Only meaningful if mOptionsMenuOpen is true.
242 */
243 private boolean mExtendedMenuOpen;
244
Michael Kolb8233fac2010-10-26 16:08:53 -0700245 private boolean mActivityPaused = true;
246 private boolean mLoadStopped;
247
248 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500249 // Checks to see when the bookmarks database has changed, and updates the
250 // Tabs' notion of whether they represent bookmarked sites.
251 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700252 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700253
Michael Kolbc3af0672011-08-09 10:24:41 -0700254 private boolean mBlockEvents;
255
Michael Kolb0b129122012-06-04 16:31:58 -0700256 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800257 private boolean mUpdateMyNavThumbnail;
258 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800259 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800260 private WebView.HitTestResult mResult;
Pankaj Gargeba076f2015-03-25 13:40:59 -0700261 private static Bitmap mBookmarkBitmap;
Site Maoabb7bd32015-03-20 15:34:02 -0700262 private PowerConnectionReceiver mLowPowerReceiver;
263 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700264
George Mount3636d0a2011-11-21 09:08:21 -0800265 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700266 mActivity = browser;
267 mSettings = BrowserSettings.getInstance();
268 mTabControl = new TabControl(this);
269 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700270 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800271 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700272 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700273
274 mUrlHandler = new UrlHandler(this);
275 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
277
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500279 mBookmarksObserver = new ContentObserver(mHandler) {
280 @Override
281 public void onChange(boolean selfChange) {
282 int size = mTabControl.getTabCount();
283 for (int i = 0; i < size; i++) {
284 mTabControl.getTab(i).updateBookmarkedStatus();
285 }
286 }
287
288 };
289 browser.getContentResolver().registerContentObserver(
290 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700291
292 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700293 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800294 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700295 }
296
John Reck9c35b9c2012-05-30 10:08:50 -0700297 @Override
298 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700299 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800300 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800301 // mCrashRecoverHandler has any previously saved state.
302 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700303 }
304
George Mount3636d0a2011-11-21 09:08:21 -0800305 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800306 // we dont want to ever recover incognito tabs
307 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700308
Patrick Scott7d50a932011-02-04 09:27:26 -0500309 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700310 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500311 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000312
Michael Kolbc831b632011-05-11 09:30:34 -0700313 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500314 // Not able to restore so we go ahead and clear session cookies. We
315 // must do this before trying to login the user as we don't want to
316 // clear any session cookies set during login.
317 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700318 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800319 if (intent == null) {
320 // This won't happen under common scenarios. The icicle is
321 // not null, but there aren't any tabs to restore.
322 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700323 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800324 final Bundle extra = intent.getExtras();
325 // Create an initial tab.
326 // If the intent is ACTION_VIEW and data is not null, the Browser is
327 // invoked to view the content by another application. In this case,
328 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800329 UrlData urlData = null;
330 if (intent.getData() != null
331 && Intent.ACTION_VIEW.equals(intent.getAction())
332 && intent.getData().toString().startsWith("content://")) {
333 urlData = new UrlData(intent.getData().toString());
334 } else {
335 urlData = IntentHandler.getUrlDataFromIntent(intent);
336 }
George Mount3636d0a2011-11-21 09:08:21 -0800337 Tab t = null;
338 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700339 String landingPage = mActivity.getResources().getString(
340 R.string.def_landing_page);
341 if (!landingPage.isEmpty()) {
342 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800343 } else {
344 t = openTabToHomePage();
345 }
George Mount3636d0a2011-11-21 09:08:21 -0800346 } else {
347 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700348 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800349 }
350 if (t != null) {
351 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
352 }
353 WebView webView = t.getWebView();
354 if (extra != null) {
355 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
356 if (scale > 0 && scale <= 1000) {
357 webView.setInitialScale(scale);
358 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 }
360 }
John Reckd8c74522011-06-14 08:45:00 -0700361 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700362 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700363 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500364 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700365 List<Tab> tabs = mTabControl.getTabs();
366 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700367
John Reck1cf4b792011-07-26 10:22:22 -0700368 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700369 //handle restored pages that may require a JS interface
370 if (t.getWebView() != null) {
371 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
372 if (backForwardList != null) {
373 for (int i = 0; i < backForwardList.getSize(); i++) {
374 WebHistoryItem item = backForwardList.getItemAtIndex(i);
375 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
376 }
377 }
378 }
John Reck1cf4b792011-07-26 10:22:22 -0700379 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700380 if (t != mTabControl.getCurrentTab()) {
381 t.pause();
382 }
John Reck1cf4b792011-07-26 10:22:22 -0700383 }
384 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700385 if (tabs.size() == 0) {
386 openTabToHomePage();
387 }
John Reck1cf4b792011-07-26 10:22:22 -0700388 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700389 // TabControl.restoreState() will create a new tab even if
390 // restoring the state fails.
391 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800392 // Intent is non-null when framework thinks the browser should be
393 // launching with a new intent (icicle is null).
394 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700395 mIntentHandler.onNewIntent(intent);
396 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700398 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700399 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800400 if (jsFlags.trim().length() != 0) {
401 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 }
George Mount3636d0a2011-11-21 09:08:21 -0800403 if (intent != null
404 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700405 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800406 }
Site Maoabb7bd32015-03-20 15:34:02 -0700407 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);
Michael Kolb8233fac2010-10-26 16:08:53 -0700418 }
419
John Reck1cf4b792011-07-26 10:22:22 -0700420 private static class PruneThumbnails implements Runnable {
421 private Context mContext;
422 private List<Long> mIds;
423
424 PruneThumbnails(Context context, List<Long> preserveIds) {
425 mContext = context.getApplicationContext();
426 mIds = preserveIds;
427 }
428
429 @Override
430 public void run() {
431 ContentResolver cr = mContext.getContentResolver();
432 if (mIds == null || mIds.size() == 0) {
433 cr.delete(Thumbnails.CONTENT_URI, null, null);
434 } else {
435 int length = mIds.size();
436 StringBuilder where = new StringBuilder();
437 where.append(Thumbnails._ID);
438 where.append(" not in (");
439 for (int i = 0; i < length; i++) {
440 where.append(mIds.get(i));
441 if (i < (length - 1)) {
442 where.append(",");
443 }
444 }
445 where.append(")");
446 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
447 }
448 }
449
450 }
451
Michael Kolb1514bb72010-11-22 09:11:48 -0800452 @Override
453 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700454 return mFactory;
455 }
456
457 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800458 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700459 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700460 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800461 }
462
463 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800464 public void createSubWindow(Tab tab) {
465 endActionMode();
466 WebView mainView = tab.getWebView();
467 WebView subView = mFactory.createWebView((mainView == null)
468 ? false
469 : mainView.isPrivateBrowsingEnabled());
470 mUi.createSubWindow(tab, subView);
471 }
472
473 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700474 public Context getContext() {
475 return mActivity;
476 }
477
478 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700479 public Activity getActivity() {
480 return mActivity;
481 }
482
483 void setUi(UI ui) {
484 mUi = ui;
485 }
486
Michael Kolbaf63dba2012-05-16 12:58:05 -0700487 @Override
488 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700489 return mSettings;
490 }
491
492 IntentHandler getIntentHandler() {
493 return mIntentHandler;
494 }
495
496 @Override
497 public UI getUi() {
498 return mUi;
499 }
500
501 int getMaxTabs() {
502 return mActivity.getResources().getInteger(R.integer.max_tabs);
503 }
504
505 @Override
506 public TabControl getTabControl() {
507 return mTabControl;
508 }
509
Michael Kolb1bf23132010-11-19 12:55:12 -0800510 @Override
511 public List<Tab> getTabs() {
512 return mTabControl.getTabs();
513 }
514
Michael Kolb8233fac2010-10-26 16:08:53 -0700515 private void startHandler() {
516 mHandler = new Handler() {
517
518 @Override
519 public void handleMessage(Message msg) {
520 switch (msg.what) {
521 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700522 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800524 case UNKNOWN_TYPE_MSG:
525 HashMap unknownTypeMap = (HashMap) msg.obj;
526 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
527 /*
528 * When the context menu is shown to the user
529 * we need to assure that its happening on the current webview
530 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
531 */
532 if (getCurrentWebView() != viewForUnknownType)
533 break;
534
535 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800536 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800537 WebView.HitTestResult result = new WebView.HitTestResult();
538
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800539 // Prevent unnecessary calls to context menu
540 // if url and image src are null
541 if (unknown_type_src == null && unknown_type_url == null)
542 break;
543
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800544 //setting the HitTestResult with new RESULT TYPE
545 if (!TextUtils.isEmpty(unknown_type_src)) {
546 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
547 result.setExtra(unknown_type_src);
548 } else {
549 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
550 result.setExtra("about:blank");
551 }
552
553 mResult = result;
554 openContextMenu(viewForUnknownType);
555 mResult = null;
556
557 break;
558
Michael Kolb8233fac2010-10-26 16:08:53 -0700559 case FOCUS_NODE_HREF:
560 {
561 String url = (String) msg.getData().get("url");
562 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500563 String src = (String) msg.getData().get("src");
564 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700565 if (TextUtils.isEmpty(url)) {
566 break;
567 }
568 HashMap focusNodeMap = (HashMap) msg.obj;
569 WebView view = (WebView) focusNodeMap.get("webview");
570 // Only apply the action if the top window did not change.
571 if (getCurrentTopWebView() != view) {
572 break;
573 }
574 switch (msg.arg1) {
575 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700576 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700577 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500578 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700579 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500580 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500581 case R.id.open_newtab_context_menu_id:
582 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700583 openTab(url, parent,
584 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500585 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700586 case R.id.copy_link_context_menu_id:
587 copy(url);
588 break;
589 case R.id.save_link_context_menu_id:
590 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500591 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800592 mActivity, url, view.getSettings().getUserAgentString(),
593 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700594 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700595 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700596 if(title == null || title == "")
597 title = url;
598
599 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
600 //SWE TODO: No thumbnail support for the url obtained via
601 //browser context menu as its not loaded in webview.
602 if (bookmarkIntent != null) {
603 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
604 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
605 mActivity.startActivity(bookmarkIntent);
606 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700607 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700608 }
609 break;
610 }
611
612 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700613 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700614 break;
615
616 case STOP_LOAD:
617 stopLoading();
618 break;
619
620 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700621 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700622 mWakeLock.release();
623 // if we reach here, Browser should be still in the
624 // background loading after WAKELOCK_TIMEOUT (5-min).
625 // To avoid burning the battery, stop loading.
626 mTabControl.stopAllLoading();
627 }
628 break;
629
630 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800631 Tab tab = (Tab) msg.obj;
632 if (tab != null) {
633 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700634 }
635 break;
kaiyiz591110b2013-08-06 17:11:06 +0800636 case OPEN_MENU:
637 if (!mOptionsMenuOpen && mActivity != null ) {
638 mActivity.openOptionsMenu();
639 }
640 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700641 }
642 }
643 };
644
645 }
646
John Reckef654f12011-07-12 16:42:08 -0700647 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200648 public Tab getCurrentTab() {
649 return mTabControl.getCurrentTab();
650 }
651
Michael Kolbba99c5d2010-11-29 14:57:41 -0800652 @Override
653 public void shareCurrentPage() {
654 shareCurrentPage(mTabControl.getCurrentTab());
655 }
656
657 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700658 if (tab == null || tab.getWebView() == null)
659 return;
660
661 final Tab mytab = tab;
662 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
663 @Override
664 public void onReceiveValue(Bitmap bitmap) {
665 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
666 mytab.getFavicon(), bitmap);
667 }
668 };
669
670 createScreenshotAsync(
671 tab.getWebView(),
672 getDesiredThumbnailWidth(mActivity),
673 getDesiredThumbnailHeight(mActivity),
674 new ValueCallback<Bitmap>() {
675 @Override
676 public void onReceiveValue(Bitmap bitmap) {
677 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
678 mytab.getFavicon(), bitmap);
679 }
680 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800681 }
682
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 /**
684 * Share a page, providing the title, url, favicon, and a screenshot. Uses
685 * an {@link Intent} to launch the Activity chooser.
686 * @param c Context used to launch a new Activity.
687 * @param title Title of the page. Stored in the Intent with
688 * {@link Intent#EXTRA_SUBJECT}
689 * @param url URL of the page. Stored in the Intent with
690 * {@link Intent#EXTRA_TEXT}
691 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
692 * with {@link Browser#EXTRA_SHARE_FAVICON}
693 * @param screenshot Bitmap of a screenshot of the page. Stored in the
694 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
695 */
696 static final void sharePage(Context c, String title, String url,
697 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700698
699 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
700 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
701 R.layout.app_row, sDialog.getApps());
702 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 }
704
705 private void copy(CharSequence text) {
706 ClipboardManager cm = (ClipboardManager) mActivity
707 .getSystemService(Context.CLIPBOARD_SERVICE);
708 cm.setText(text);
709 }
710
711 // lifecycle
712
John Reck9c35b9c2012-05-30 10:08:50 -0700713 @Override
714 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800716 // update the menu in case of a locale change
717 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800718 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800719 if (mOptionsMenuOpen) {
720 mActivity.closeOptionsMenu();
721 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
722 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700723 if (mPageDialogsHandler != null) {
724 mPageDialogsHandler.onConfigurationChanged(config);
725 }
726 mUi.onConfigurationChanged(config);
727 }
728
729 @Override
730 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700731 if (!mUi.isWebShowing()) {
732 mUi.showWeb(false);
733 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700734 mIntentHandler.onNewIntent(intent);
735 }
736
John Reck9c35b9c2012-05-30 10:08:50 -0700737 @Override
738 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800739 if (mUi.isCustomViewShowing()) {
740 hideCustomView();
741 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 if (mActivityPaused) {
743 Log.e(LOGTAG, "BrowserActivity is already paused.");
744 return;
745 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700746 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800747 Tab tab = mTabControl.getCurrentTab();
748 if (tab != null) {
749 tab.pause();
750 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700751 if (mWakeLock == null) {
752 PowerManager pm = (PowerManager) mActivity
753 .getSystemService(Context.POWER_SERVICE);
754 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
755 }
Michael Kolb70976932010-11-30 11:34:01 -0800756 mWakeLock.acquire();
757 mHandler.sendMessageDelayed(mHandler
758 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
759 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700760 }
761 mUi.onPause();
762 mNetworkHandler.onPause();
763
764 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100765 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700766 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700767 }
768
John Reck9c35b9c2012-05-30 10:08:50 -0700769 @Override
770 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700771 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800772 Bundle saveState = createSaveState();
773
774 // crash recovery manages all save & restore state
775 mCrashRecoveryHandler.writeState(saveState);
776 mSettings.setLastRunPaused(true);
777 }
778
779 /**
780 * Save the current state to outState. Does not write the state to
781 * disk.
782 * @return Bundle containing the current state of all tabs.
783 */
784 /* package */ Bundle createSaveState() {
785 Bundle saveState = new Bundle();
786 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800787 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700788 }
789
John Reck9c35b9c2012-05-30 10:08:50 -0700790 @Override
791 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 if (!mActivityPaused) {
793 Log.e(LOGTAG, "BrowserActivity is already resumed.");
794 return;
795 }
George Mount3636d0a2011-11-21 09:08:21 -0800796 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700797 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800798 Tab current = mTabControl.getCurrentTab();
799 if (current != null) {
800 current.resume();
801 resumeWebViewTimers(current);
802 }
John Reckf57c0292011-07-21 18:15:39 -0700803 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200804
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 mUi.onResume();
806 mNetworkHandler.onResume();
807 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100808 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700809 if (mVoiceResult != null) {
810 mUi.onVoiceResult(mVoiceResult);
811 mVoiceResult = null;
812 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700813 if (current != null && current.getWebView().isShowingCrashView())
814 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700815 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700816 }
817
John Reckf57c0292011-07-21 18:15:39 -0700818 private void releaseWakeLock() {
819 if (mWakeLock != null && mWakeLock.isHeld()) {
820 mHandler.removeMessages(RELEASE_WAKELOCK);
821 mWakeLock.release();
822 }
823 }
824
Michael Kolb70976932010-11-30 11:34:01 -0800825 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800826 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800827 * @param tab guaranteed non-null
828 */
829 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700830 boolean inLoad = tab.inPageLoad();
831 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
832 CookieSyncManager.getInstance().startSync();
833 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100834 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700835 }
836 }
837
Michael Kolb70976932010-11-30 11:34:01 -0800838 /**
839 * Pause all WebView timers using the WebView of the given tab
840 * @param tab
841 * @return true if the timers are paused or tab is null
842 */
843 private boolean pauseWebViewTimers(Tab tab) {
844 if (tab == null) {
845 return true;
846 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100848 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 }
Michael Kolb70976932010-11-30 11:34:01 -0800851 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 }
853
John Reck9c35b9c2012-05-30 10:08:50 -0700854 @Override
855 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800856 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700857 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
858 mUploadHandler = null;
859 }
860 if (mTabControl == null) return;
861 mUi.onDestroy();
862 // Remove the current tab and sub window
863 Tab t = mTabControl.getCurrentTab();
864 if (t != null) {
865 dismissSubWindow(t);
866 removeTab(t);
867 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500868 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 // Destroy all the tabs
870 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700871 // Unregister receiver
872 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700873 }
874
875 protected boolean isActivityPaused() {
876 return mActivityPaused;
877 }
878
John Reck9c35b9c2012-05-30 10:08:50 -0700879 @Override
880 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700881 mTabControl.freeMemory();
882 }
883
884 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 public void stopLoading() {
886 mLoadStopped = true;
887 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700888 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700889 if (w != null) {
890 w.stopLoading();
891 mUi.onPageStopped(tab);
892 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 }
894
895 boolean didUserStopLoading() {
896 return mLoadStopped;
897 }
898
kaiyiza016da12013-08-26 17:50:22 +0800899 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700900 final String reminderType = getContext().getResources().getString(
901 R.string.def_wifi_browser_interaction_remind_type);
902 final String selectionConnnection = getContext().getResources().getString(
903 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700904 final String wifiSelection = getContext().getResources().getString(
905 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700906
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700907 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
908 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700909 return;
910
kaiyiza016da12013-08-26 17:50:22 +0800911 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700912 Context.CONNECTIVITY_SERVICE);
913 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700914 WifiManager wifiMgr = (WifiManager) this.getContext()
915 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700916 if (networkInfo == null
917 || (networkInfo != null && (networkInfo.getType() !=
918 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700919 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
920 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700921 List<ScanResult> list = wifiMgr.getScanResults();
922 // Have no AP's for Wifi's fall back to data
923 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700924 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700925 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
926 this.getContext().startActivity(intent);
927 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700928 // Request to select Wifi AP
929 try {
930 Intent intent = new Intent(wifiSelection);
931 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
932 this.getContext().startActivity(intent);
933 } catch (Exception e) {
934 String err_msg = this.getContext().getString(
935 R.string.acivity_not_found, wifiSelection);
936 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
937 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700938 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700939 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800940 }
941 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700942
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 // WebViewController
944
945 @Override
John Reck324d4402011-01-11 16:56:42 -0800946 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700947
948 // We've started to load a new page. If there was a pending message
949 // to save a screenshot then we will now take the new page and save
950 // an incorrect screenshot. Therefore, remove any pending thumbnail
951 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700952 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700953
954 // reset sync timer to avoid sync starts during loading a page
955 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700956 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700957 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800958 boolean networkNotifier = BrowserConfig.getInstance(getContext())
959 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700960 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700961 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800962 } else {
963 if (!mNetworkHandler.isNetworkUp()) {
964 view.setNetworkAvailable(false);
965 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700966 }
967
968 // when BrowserActivity just starts, onPageStarted may be called before
969 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
970 // to start the timer. As we won't switch tabs while an activity is in
971 // pause state, we can ensure calling resume and pause in pair.
972 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800973 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700974 }
975 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 endActionMode();
977
John Reck30c714c2010-12-16 17:30:34 -0800978 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700979
John Reck324d4402011-01-11 16:56:42 -0800980 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700981 // update the bookmark database for favicon
982 maybeUpdateFavicon(tab, null, url, favicon);
983
984 Performance.tracePageStart(url);
985
986 // Performance probe
987 if (false) {
988 Performance.onPageStarted();
989 }
990
991 }
992
993 @Override
John Reck324d4402011-01-11 16:56:42 -0800994 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700995 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800996 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200997
Michael Kolb8233fac2010-10-26 16:08:53 -0700998 // Performance probe
999 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001000 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001001 }
1002
Tarun Nainani8eb00912014-07-17 12:28:32 -07001003 tab.onPageFinished();
1004
Michael Kolb8233fac2010-10-26 16:08:53 -07001005 Performance.tracePageFinished();
1006 }
1007
1008 @Override
John Reck30c714c2010-12-16 17:30:34 -08001009 public void onProgressChanged(Tab tab) {
1010 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001011
1012 if (newProgress == 100) {
1013 CookieSyncManager.getInstance().sync();
1014 // onProgressChanged() may continue to be called after the main
1015 // frame has finished loading, as any remaining sub frames continue
1016 // to load. We'll only get called once though with newProgress as
1017 // 100 when everything is loaded. (onPageFinished is called once
1018 // when the main frame completes loading regardless of the state of
1019 // any sub frames so calls to onProgressChanges may continue after
1020 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001021 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001022 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001023 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001024 } else if (mWasInPageLoad) {
1025 mWasInPageLoad = false;
1026 updateInLoadMenuItems(mCachedMenu, tab);
1027 }
1028
1029 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001030 // pause the WebView timer and release the wake lock if it is
1031 // finished while BrowserActivity is in pause state.
1032 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001033 }
John Reckd9862372012-02-21 15:04:50 -08001034 if (!tab.isPrivateBrowsingEnabled()
1035 && !TextUtils.isEmpty(tab.getUrl())
1036 && !tab.isSnapshot()) {
1037 // Only update the bookmark screenshot if the user did not
1038 // cancel the load early and there is not already
1039 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001040 if (tab.shouldUpdateThumbnail() &&
1041 (tab.inForeground() && !didUserStopLoading()
1042 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001043 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1044 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1045 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001046 1500);
John Reckd9862372012-02-21 15:04:50 -08001047 }
1048 }
1049 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001051 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001053 // still loading
1054 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001056 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001057 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001058 } else {
1059 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 }
1061 }
John Reck30c714c2010-12-16 17:30:34 -08001062 mUi.onProgressChanged(tab);
1063 }
1064
1065 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001066 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001067 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001068 }
1069
1070 @Override
1071 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001072 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001073 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001074 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001075 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1076 return;
1077 }
1078 // Update the title in the history database if not in private browsing mode
1079 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001080 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 }
1082 }
1083
1084 @Override
1085 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001086 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001087 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1088 }
1089
1090 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001091 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1092 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001093 }
1094
1095 @Override
1096 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1097 if (mMenuIsDown) {
1098 // only check shortcut key when MENU is held
1099 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1100 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001101 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001102 int keyCode = event.getKeyCode();
1103 // We need to send almost every key to WebKit. However:
1104 // 1. We don't want to block the device on the renderer for
1105 // some keys like menu, home, call.
1106 // 2. There are no WebKit equivalents for some of these keys
1107 // (see app/keyboard_codes_win.h)
1108 // Note that these are not the same set as KeyEvent.isSystemKey:
1109 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1110 if (keyCode == KeyEvent.KEYCODE_MENU ||
1111 keyCode == KeyEvent.KEYCODE_HOME ||
1112 keyCode == KeyEvent.KEYCODE_BACK ||
1113 keyCode == KeyEvent.KEYCODE_CALL ||
1114 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1115 keyCode == KeyEvent.KEYCODE_POWER ||
1116 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1117 keyCode == KeyEvent.KEYCODE_CAMERA ||
1118 keyCode == KeyEvent.KEYCODE_FOCUS ||
1119 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1120 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1121 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1122 return true;
1123 }
1124
1125 // We also have to intercept some shortcuts before we send them to the ContentView.
1126 if (event.isCtrlPressed() && (
1127 keyCode == KeyEvent.KEYCODE_TAB ||
1128 keyCode == KeyEvent.KEYCODE_W ||
1129 keyCode == KeyEvent.KEYCODE_F4)) {
1130 return true;
1131 }
1132
1133 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001134 }
1135
Tarun Nainanif5563b62015-01-21 18:52:59 -08001136 private void handleMediaKeyEvent(KeyEvent event) {
1137
1138 int keyCode = event.getKeyCode();
1139 // send media key events to audio manager
1140 if (Build.VERSION.SDK_INT >= 19) {
1141
1142 switch (keyCode) {
1143 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1144 case KeyEvent.KEYCODE_MEDIA_STOP:
1145 case KeyEvent.KEYCODE_MEDIA_NEXT:
1146 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1147 case KeyEvent.KEYCODE_MEDIA_REWIND:
1148 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1149 case KeyEvent.KEYCODE_MUTE:
1150 case KeyEvent.KEYCODE_MEDIA_PLAY:
1151 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1152 case KeyEvent.META_SHIFT_RIGHT_ON:
1153 case KeyEvent.KEYCODE_MEDIA_EJECT:
1154 case KeyEvent.KEYCODE_MEDIA_RECORD:
1155 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1156
1157 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1158 .getSystemService(Context.AUDIO_SERVICE);
1159 audioManager.dispatchMediaKeyEvent(event);
1160 }
1161 }
1162 }
1163
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 @Override
John Reck997b1b72012-04-19 18:08:25 -07001165 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001167 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001168 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001169 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 } else {
John Reck997b1b72012-04-19 18:08:25 -07001171 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 }
1173 }
John Reck997b1b72012-04-19 18:08:25 -07001174 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001175 }
1176
1177 @Override
John Reck324d4402011-01-11 16:56:42 -08001178 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001179 // Don't save anything in private browsing mode or when disabling history
1180 // for regular tabs is enabled
1181 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1182 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1183 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001184
John Reck49a603c2011-03-03 09:33:05 -08001185 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001186
John Reck324d4402011-01-11 16:56:42 -08001187 if (TextUtils.isEmpty(url)
1188 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001189 return;
1190 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001191
John Reckf57c0292011-07-21 18:15:39 -07001192 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001193 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001194 }
1195
1196 @Override
1197 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1198 AsyncTask<Void, Void, String[]> task =
1199 new AsyncTask<Void, Void, String[]>() {
1200 @Override
1201 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001202 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 }
1204 @Override
1205 public void onPostExecute(String[] result) {
1206 callback.onReceiveValue(result);
1207 }
1208 };
1209 task.execute();
1210 }
1211
1212 @Override
1213 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1214 final HttpAuthHandler handler, final String host,
1215 final String realm) {
1216 String username = null;
1217 String password = null;
1218
1219 boolean reuseHttpAuthUsernamePassword
1220 = handler.useHttpAuthUsernamePassword();
1221
1222 if (reuseHttpAuthUsernamePassword && view != null) {
1223 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1224 if (credentials != null && credentials.length == 2) {
1225 username = credentials[0];
1226 password = credentials[1];
1227 }
1228 }
1229
1230 if (username != null && password != null) {
1231 handler.proceed(username, password);
1232 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001233 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001234 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1235 } else {
1236 handler.cancel();
1237 }
1238 }
1239 }
1240
1241 @Override
1242 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001243 String contentDisposition, String mimetype, String referer,
1244 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001245 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001246 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001247 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001248 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001249 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001250 // This Tab was opened for the sole purpose of downloading a
1251 // file. Remove it.
1252 if (tab == mTabControl.getCurrentTab()) {
1253 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001254 if (tab.getDerivedFromIntent())
1255 closeTab(tab);
1256 else
1257 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001258 } else {
1259 // In this case, it is not.
1260 closeTab(tab);
1261 }
1262 }
1263 }
1264
1265 @Override
1266 public Bitmap getDefaultVideoPoster() {
1267 return mUi.getDefaultVideoPoster();
1268 }
1269
1270 @Override
1271 public View getVideoLoadingProgressView() {
1272 return mUi.getVideoLoadingProgressView();
1273 }
1274
1275 @Override
1276 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1277 SslError error) {
1278 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1279 }
1280
1281 // helper method
1282
1283 /*
1284 * Update the favorites icon if the private browsing isn't enabled and the
1285 * icon is valid.
1286 */
1287 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1288 final String url, Bitmap favicon) {
1289 if (favicon == null) {
1290 return;
1291 }
1292 if (!tab.isPrivateBrowsingEnabled()) {
1293 Bookmarks.updateFavicon(mActivity
1294 .getContentResolver(), originalUrl, url, favicon);
1295 }
1296 }
1297
Leon Scroggins4cd97792010-12-03 15:31:56 -05001298 @Override
1299 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001300 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001301 mUi.bookmarkedStatusHasChanged(tab);
1302 }
1303
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 // end WebViewController
1305
1306 protected void pageUp() {
1307 getCurrentTopWebView().pageUp(false);
1308 }
1309
1310 protected void pageDown() {
1311 getCurrentTopWebView().pageDown(false);
1312 }
1313
1314 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001315 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001316 public void editUrl() {
1317 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001318 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001319 }
1320
John Reck9c35b9c2012-05-30 10:08:50 -07001321 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001322 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001323 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001324 if (tab.inForeground()) {
1325 if (mUi.isCustomViewShowing()) {
1326 callback.onCustomViewHidden();
1327 return;
1328 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001329 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001330 // Save the menu state and set it to empty while the custom
1331 // view is showing.
1332 mOldMenuState = mMenuState;
1333 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001334 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 }
1336 }
1337
1338 @Override
1339 public void hideCustomView() {
1340 if (mUi.isCustomViewShowing()) {
1341 mUi.onHideCustomView();
1342 // Reset the old menu state.
1343 mMenuState = mOldMenuState;
1344 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001345 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001346 }
1347 }
1348
John Reck9c35b9c2012-05-30 10:08:50 -07001349 @Override
1350 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001351 Intent intent) {
1352 if (getCurrentTopWebView() == null) return;
1353 switch (requestCode) {
1354 case PREFERENCES_PAGE:
1355 if (resultCode == Activity.RESULT_OK && intent != null) {
1356 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001357 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 mTabControl.removeParentChildRelationShips();
1359 }
1360 }
1361 break;
1362 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001363 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001364 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001365 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001366 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001367 case AUTOFILL_SETUP:
1368 // Determine whether a profile was actually set up or not
1369 // and if so, send the message back to the WebTextView to
1370 // fill the form with the new profile.
1371 if (getSettings().getAutoFillProfile() != null) {
1372 mAutoFillSetupMessage.sendToTarget();
1373 mAutoFillSetupMessage = null;
1374 }
1375 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001376 case COMBO_VIEW:
1377 if (intent == null || resultCode != Activity.RESULT_OK) {
1378 break;
1379 }
John Reck3ba45532011-08-11 16:26:53 -07001380 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001381 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1382 Tab t = getCurrentTab();
1383 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001384 mUpdateMyNavThumbnail = true;
1385 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001386 loadUrl(t, uri.toString());
1387 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1388 String[] urls = intent.getStringArrayExtra(
1389 ComboViewActivity.EXTRA_OPEN_ALL);
1390 Tab parent = getCurrentTab();
1391 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001392 if (url != null) {
1393 parent = openTab(url, parent,
1394 !mSettings.openInBackground(), true);
1395 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001396 }
1397 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1398 long id = intent.getLongExtra(
1399 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1400 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001401 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001402 }
1403 }
1404 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001405 case VOICE_RESULT:
1406 if (resultCode == Activity.RESULT_OK && intent != null) {
1407 ArrayList<String> results = intent.getStringArrayListExtra(
1408 RecognizerIntent.EXTRA_RESULTS);
1409 if (results.size() >= 1) {
1410 mVoiceResult = results.get(0);
1411 }
1412 }
1413 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001414 case MY_NAVIGATION:
1415 if (intent == null || resultCode != Activity.RESULT_OK) {
1416 break;
1417 }
1418
1419 if (intent.getBooleanExtra("need_refresh", false) &&
1420 getCurrentTopWebView() != null) {
1421 getCurrentTopWebView().reload();
1422 }
1423 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001424 default:
1425 break;
1426 }
1427 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001428 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001429 }
1430
1431 /**
1432 * Open the Go page.
1433 * @param startWithHistory If true, open starting on the history tab.
1434 * Otherwise, start with the bookmarks tab.
1435 */
1436 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001437 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001438 if (mTabControl.getCurrentWebView() == null) {
1439 return;
1440 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001441 // clear action mode
1442 if (isInCustomActionMode()) {
1443 endActionMode();
1444 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001445 Bundle extras = new Bundle();
1446 // Disable opening in a new window if we have maxed out the windows
1447 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1448 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001449 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001450 }
1451
1452 // combo view callbacks
1453
Michael Kolb8233fac2010-10-26 16:08:53 -07001454 // key handling
1455 protected void onBackKey() {
1456 if (!mUi.onBackKey()) {
1457 WebView subwindow = mTabControl.getCurrentSubWindow();
1458 if (subwindow != null) {
1459 if (subwindow.canGoBack()) {
1460 subwindow.goBack();
1461 } else {
1462 dismissSubWindow(mTabControl.getCurrentTab());
1463 }
1464 } else {
1465 goBackOnePageOrQuit();
1466 }
1467 }
1468 }
1469
Michael Kolb4bd767d2011-05-27 11:33:55 -07001470 protected boolean onMenuKey() {
1471 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001472 }
1473
Michael Kolb8233fac2010-10-26 16:08:53 -07001474 // menu handling and state
1475 // TODO: maybe put into separate handler
1476
John Reck9c35b9c2012-05-30 10:08:50 -07001477 @Override
1478 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001479 if (mMenuState == EMPTY_MENU) {
1480 return false;
1481 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001482 MenuInflater inflater = mActivity.getMenuInflater();
1483 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001484 return true;
1485 }
1486
John Reck9c35b9c2012-05-30 10:08:50 -07001487 @Override
1488 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001489 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001490 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001491 return;
1492 }
1493 if (!(v instanceof WebView)) {
1494 return;
1495 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001496 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001497 WebView.HitTestResult result;
1498
1499 /* Determine whether the ContextMenu got triggered because
1500 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1501 * received. The mResult acts as a flag to identify, how it got trigerred
1502 */
1503 if (mResult == null){
1504 result = webview.getHitTestResult();
1505 } else {
1506 result = mResult;
1507 }
1508
Michael Kolb8233fac2010-10-26 16:08:53 -07001509 if (result == null) {
1510 return;
1511 }
1512
1513 int type = result.getType();
1514 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001515
1516 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1517 unknownTypeMap.put("webview", webview);
1518 final Message msg = mHandler.obtainMessage(
1519 UNKNOWN_TYPE_MSG, unknownTypeMap);
1520 /* As defined in android developers guide
1521 * when UNKNOWN_TYPE is received as a result of HitTest
1522 * you need to determing the type by invoking requestFocusNodeHref
1523 */
1524 webview.requestFocusNodeHref(msg);
1525
Michael Kolb8233fac2010-10-26 16:08:53 -07001526 Log.w(LOGTAG,
1527 "We should not show context menu when nothing is touched");
1528 return;
1529 }
1530 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1531 // let TextView handles context menu
1532 return;
1533 }
1534
1535 // Note, http://b/issue?id=1106666 is requesting that
1536 // an inflated menu can be used again. This is not available
1537 // yet, so inflate each time (yuk!)
1538 MenuInflater inflater = mActivity.getMenuInflater();
1539 inflater.inflate(R.menu.browsercontext, menu);
1540
1541 // Show the correct menu group
1542 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001543 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001544 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001545 menu.setGroupVisible(R.id.PHONE_MENU,
1546 type == WebView.HitTestResult.PHONE_TYPE);
1547 menu.setGroupVisible(R.id.EMAIL_MENU,
1548 type == WebView.HitTestResult.EMAIL_TYPE);
1549 menu.setGroupVisible(R.id.GEO_MENU,
1550 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001551 String itemUrl = null;
1552 String url = webview.getOriginalUrl();
1553 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1554 itemUrl = Uri.decode(navigationUrl);
1555 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1556 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1557 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1558 } else {
1559 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1560 }
1561 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1562 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1563 } else {
1564 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1565
1566 menu.setGroupVisible(R.id.IMAGE_MENU,
1567 type == WebView.HitTestResult.IMAGE_TYPE
1568 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1569 menu.setGroupVisible(R.id.ANCHOR_MENU,
1570 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1571 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001572 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1573 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001574 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001575 // Setup custom handling depending on the type
1576 switch (type) {
1577 case WebView.HitTestResult.PHONE_TYPE:
1578 menu.setHeaderTitle(Uri.decode(extra));
1579 menu.findItem(R.id.dial_context_menu_id).setIntent(
1580 new Intent(Intent.ACTION_VIEW, Uri
1581 .parse(WebView.SCHEME_TEL + extra)));
1582 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1583 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1584 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1585 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1586 addIntent);
1587 menu.findItem(R.id.copy_phone_context_menu_id)
1588 .setOnMenuItemClickListener(
1589 new Copy(extra));
1590 break;
1591
1592 case WebView.HitTestResult.EMAIL_TYPE:
1593 menu.setHeaderTitle(extra);
1594 menu.findItem(R.id.email_context_menu_id).setIntent(
1595 new Intent(Intent.ACTION_VIEW, Uri
1596 .parse(WebView.SCHEME_MAILTO + extra)));
1597 menu.findItem(R.id.copy_mail_context_menu_id)
1598 .setOnMenuItemClickListener(
1599 new Copy(extra));
1600 break;
1601
1602 case WebView.HitTestResult.GEO_TYPE:
1603 menu.setHeaderTitle(extra);
1604 menu.findItem(R.id.map_context_menu_id).setIntent(
1605 new Intent(Intent.ACTION_VIEW, Uri
1606 .parse(WebView.SCHEME_GEO
1607 + URLEncoder.encode(extra))));
1608 menu.findItem(R.id.copy_geo_context_menu_id)
1609 .setOnMenuItemClickListener(
1610 new Copy(extra));
1611 break;
1612
1613 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1614 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001615 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 // decide whether to show the open link in new tab option
1617 boolean showNewTab = mTabControl.canCreateNewTab();
1618 MenuItem newTabItem
1619 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001620 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001621 ? R.string.contextmenu_openlink_newwindow_background
1622 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 newTabItem.setVisible(showNewTab);
1624 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001625 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1626 newTabItem.setOnMenuItemClickListener(
1627 new MenuItem.OnMenuItemClickListener() {
1628 @Override
1629 public boolean onMenuItemClick(MenuItem item) {
1630 final HashMap<String, WebView> hrefMap =
1631 new HashMap<String, WebView>();
1632 hrefMap.put("webview", webview);
1633 final Message msg = mHandler.obtainMessage(
1634 FOCUS_NODE_HREF,
1635 R.id.open_newtab_context_menu_id,
1636 0, hrefMap);
1637 webview.requestFocusNodeHref(msg);
1638 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001639 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001640 });
1641 } else {
1642 newTabItem.setOnMenuItemClickListener(
1643 new MenuItem.OnMenuItemClickListener() {
1644 @Override
1645 public boolean onMenuItemClick(MenuItem item) {
1646 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001647 openTab(extra, parent,
1648 !mSettings.openInBackground(),
1649 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001650 return true;
1651 }
1652 });
1653 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001654 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001655 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1656 menu.setHeaderTitle(navigationUrl);
1657 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1658
1659 if (itemUrl != null) {
1660 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1661 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1662 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1663 @Override
1664 public boolean onMenuItemClick(MenuItem item) {
1665 final Intent intent = new Intent(Controller.this
1666 .getContext(),
1667 AddMyNavigationPage.class);
1668 Bundle bundle = new Bundle();
1669 String url = Uri.decode(navigationUrl);
1670 bundle.putBoolean("isAdding", false);
1671 bundle.putString("url", url);
1672 bundle.putString("name", getNameFromUrl(url));
1673 intent.putExtra("websites", bundle);
1674 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1675 return false;
1676 }
1677 });
1678 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1679 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1680 @Override
1681 public boolean onMenuItemClick(MenuItem item) {
1682 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1683 return false;
1684 }
1685 });
1686 }
1687 } else {
1688 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1689 }
1690 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001691 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1692 break;
1693 }
1694 // otherwise fall through to handle image part
1695 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001696 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1697 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001698 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1699 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001700 shareItem.setOnMenuItemClickListener(
1701 new MenuItem.OnMenuItemClickListener() {
1702 @Override
1703 public boolean onMenuItemClick(MenuItem item) {
1704 sharePage(mActivity, null, extra, null,
1705 null);
1706 return true;
1707 }
1708 }
1709 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001710 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001711 menu.findItem(R.id.view_image_context_menu_id)
1712 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1713 @Override
1714 public boolean onMenuItemClick(MenuItem item) {
1715 openTab(extra, mTabControl.getCurrentTab(), true, true);
1716 return false;
1717 }
1718 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001719 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1720 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1721 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001722 menu.findItem(R.id.set_wallpaper_context_menu_id).
1723 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1724 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001725 break;
1726
1727 default:
1728 Log.w(LOGTAG, "We should not get here.");
1729 break;
1730 }
1731 //update the ui
1732 mUi.onContextMenuCreated(menu);
1733 }
1734
kaiyiz6e5b3e02013-08-19 20:02:01 +08001735 public void startAddMyNavigation(String url) {
1736 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1737 Bundle bundle = new Bundle();
1738 bundle.putBoolean("isAdding", true);
1739 bundle.putString("url", url);
1740 bundle.putString("name", getNameFromUrl(url));
1741 intent.putExtra("websites", bundle);
1742 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1743 }
1744
1745 private void showMyNavigationDeleteDialog(final String itemUrl) {
1746 new AlertDialog.Builder(this.getContext())
1747 .setTitle(R.string.my_navigation_delete_label)
1748 .setIcon(android.R.drawable.ic_dialog_alert)
1749 .setMessage(R.string.my_navigation_delete_msg)
1750 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1751 @Override
1752 public void onClick(DialogInterface dialog, int whichButton) {
1753 deleteMyNavigationItem(itemUrl);
1754 }
1755 })
1756 .setNegativeButton(R.string.cancel, null)
1757 .show();
1758 }
1759
1760 private void deleteMyNavigationItem(final String itemUrl) {
1761 ContentResolver cr = this.getContext().getContentResolver();
1762 Cursor cursor = null;
1763
1764 try {
1765 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1766 new String[] {
1767 MyNavigationUtil.ID
1768 }, "url = ?", new String[] {
1769 itemUrl
1770 }, null);
1771 if (null != cursor && cursor.moveToFirst()) {
1772 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1773 cursor.getLong(0));
1774
1775 ContentValues values = new ContentValues();
1776 values.put(MyNavigationUtil.TITLE, "");
1777 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1778 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1779 ByteArrayOutputStream os = new ByteArrayOutputStream();
1780 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1781 R.raw.my_navigation_add);
1782 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1783 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1784 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1785 cr.update(uri, values, null, null);
1786 } else {
1787 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1788 }
1789 } catch (IllegalStateException e) {
1790 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1791 } finally {
1792 if (null != cursor) {
1793 cursor.close();
1794 }
1795 }
1796
1797 if (getCurrentTopWebView() != null) {
1798 getCurrentTopWebView().reload();
1799 }
1800 }
1801
1802 private String getNameFromUrl(String itemUrl) {
1803 ContentResolver cr = this.getContext().getContentResolver();
1804 Cursor cursor = null;
1805 String name = null;
1806
1807 try {
1808 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1809 new String[] {
1810 MyNavigationUtil.TITLE
1811 }, "url = ?", new String[] {
1812 itemUrl
1813 }, null);
1814 if (null != cursor && cursor.moveToFirst()) {
1815 name = cursor.getString(0);
1816 } else {
1817 Log.e(LOGTAG, "this item does not exist!");
1818 }
1819 } catch (IllegalStateException e) {
1820 Log.e(LOGTAG, "getNameFromUrl", e);
1821 } finally {
1822 if (null != cursor) {
1823 cursor.close();
1824 }
1825 }
1826 return name;
1827 }
1828
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001829 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001830 final ContentResolver cr = mActivity.getContentResolver();
1831 new AsyncTask<Void, Void, Void>() {
1832 @Override
1833 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001834 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1835 if(!isMyNavigationUrl)
1836 return null;
1837
kaiyiz6e5b3e02013-08-19 20:02:01 +08001838 ContentResolver cr = mActivity.getContentResolver();
1839 Cursor cursor = null;
1840 try {
1841 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1842 new String[] {
1843 MyNavigationUtil.ID
1844 }, "url = ?", new String[] {
1845 itemUrl
1846 }, null);
1847 if (null != cursor && cursor.moveToFirst()) {
1848 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001849 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001850
1851 ContentValues values = new ContentValues();
1852 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1853 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1854 cursor.getLong(0));
1855 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1856 cr.update(uri, values, null, null);
1857 os.close();
1858 }
1859 } catch (IllegalStateException e) {
1860 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1861 } catch (IOException e) {
1862 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1863 } finally {
1864 if (null != cursor) {
1865 cursor.close();
1866 }
1867 }
1868 return null;
1869 }
1870 }.execute();
1871 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001872 /**
1873 * As the menu can be open when loading state changes
1874 * we must manually update the state of the stop/reload menu
1875 * item
1876 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001877 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001878 if (menu == null) {
1879 return;
1880 }
1881 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001882 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001883 menu.findItem(R.id.stop_menu_id):
1884 menu.findItem(R.id.reload_menu_id);
1885 if (src != null) {
1886 dest.setIcon(src.getIcon());
1887 dest.setTitle(src.getTitle());
1888 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001889 mActivity.invalidateOptionsMenu();
1890 }
1891
1892 public void invalidateOptionsMenu() {
1893 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001894 }
1895
John Reck9c35b9c2012-05-30 10:08:50 -07001896 @Override
1897 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001898 // Software menu key (toolbar key)
1899 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1900 return true;
1901 }
1902
1903 @Override
1904 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001905 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001906 // hold on to the menu reference here; it is used by the page callbacks
1907 // to update the menu based on loading state
1908 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001909 // Note: setVisible will decide whether an item is visible; while
1910 // setEnabled() will decide whether an item is enabled, which also means
1911 // whether the matching shortcut key will function.
1912 switch (mMenuState) {
1913 case EMPTY_MENU:
1914 if (mCurrentMenuState != mMenuState) {
1915 menu.setGroupVisible(R.id.MAIN_MENU, false);
1916 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1917 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1918 }
1919 break;
1920 default:
1921 if (mCurrentMenuState != mMenuState) {
1922 menu.setGroupVisible(R.id.MAIN_MENU, true);
1923 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1924 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1925 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001926 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001927 break;
1928 }
1929 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001930 mUi.onPrepareOptionsMenu(menu);
1931 }
1932
1933 private void setMenuItemVisibility(Menu menu, int id,
1934 boolean visibility) {
1935 MenuItem item = menu.findItem(id);
1936 if (item != null) {
1937 item.setVisible(visibility);
1938 }
1939 }
1940
1941 private int lookupBookmark(String title, String url) {
1942 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001943 int count = 0;
1944 Cursor cursor = null;
1945 try {
1946 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1947 BookmarksLoader.PROJECTION,
1948 "title = ? OR url = ?",
1949 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001950 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001951 },
1952 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001953
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001954 if (cursor != null)
1955 count = cursor.getCount();
1956
1957 } catch (IllegalStateException e) {
1958 Log.e(LOGTAG, "lookupBookmark ", e);
1959 } finally {
1960 if (null != cursor) {
1961 cursor.close();
1962 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001963 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001964 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001965 }
1966
1967 private void resetMenuItems(Menu menu) {
1968 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1969 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1970
1971 WebView w = getCurrentTopWebView();
1972 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1973
1974 String title = w.getTitle();
1975 String url = w.getUrl();
1976 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1977 bookmark_icon.setChecked(true);
1978 } else {
1979 bookmark_icon.setChecked(false);
1980 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001981 }
1982
Michael Kolb4bf79712011-07-14 14:18:12 -07001983 @Override
1984 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001985 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001986 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001987 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001988 // Following flag is used to identify schemes for which the LIVE_MENU
1989 // items defined in res/menu/browser.xml should be enabled
1990 boolean isLiveScheme = false;
1991 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001992 boolean isSavable = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001993 resetMenuItems(menu);
1994
Michael Kolb4bf79712011-07-14 14:18:12 -07001995 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001996 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001997 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001998 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001999 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002000 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002001 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07002002 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002003
2004 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2005 forward.setEnabled(canGoForward);
2006
Michael Kolb4bf79712011-07-14 14:18:12 -07002007 // decide whether to show the share link option
2008 PackageManager pm = mActivity.getPackageManager();
2009 Intent send = new Intent(Intent.ACTION_SEND);
2010 send.setType("text/plain");
2011 ResolveInfo ri = pm.resolveActivity(send,
2012 PackageManager.MATCH_DEFAULT_ONLY);
2013 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2014
2015 boolean isNavDump = mSettings.enableNavDump();
2016 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2017 nav.setVisible(isNavDump);
2018 nav.setEnabled(isNavDump);
2019
2020 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002021 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2022 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002023 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002024 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2025 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002026 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002027 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2028 isLive && isLiveScheme && isPageFinished);
2029 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002030 isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002031 // history and snapshots item are the members of COMBO menu group,
2032 // so if show history item, only make snapshots item invisible.
2033 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002034
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002035 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002036 }
2037
John Reck9c35b9c2012-05-30 10:08:50 -07002038 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002039 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002040 if (null == getCurrentTopWebView()) {
2041 return false;
2042 }
2043 if (mMenuIsDown) {
2044 // The shortcut action consumes the MENU. Even if it is still down,
2045 // it won't trigger the next shortcut action. In the case of the
2046 // shortcut action triggering a new activity, like Bookmarks, we
2047 // won't get onKeyUp for MENU. So it is important to reset it here.
2048 mMenuIsDown = false;
2049 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002050 if (mUi.onOptionsItemSelected(item)) {
2051 // ui callback handled it
2052 return true;
2053 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002054 switch (item.getItemId()) {
2055 // -- Main menu
2056 case R.id.new_tab_menu_id:
2057 openTabToHomePage();
2058 break;
2059
2060 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002061 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002062 break;
2063
2064 case R.id.goto_menu_id:
2065 editUrl();
2066 break;
2067
2068 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002069 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2070 break;
2071
2072 case R.id.history_menu_id:
2073 bookmarksOrHistoryPicker(ComboViews.History);
2074 break;
2075
2076 case R.id.snapshots_menu_id:
2077 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002078 break;
2079
Pankaj Garg49b79252014-11-07 17:33:41 -08002080 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002081 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002082 break;
2083
2084 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002085 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002086 stopLoading();
2087 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002088 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002089 getCurrentTopWebView().reload();
2090 }
2091 break;
2092
Michael Kolb8233fac2010-10-26 16:08:53 -07002093 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002094 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002095 break;
2096
2097 case R.id.close_menu_id:
2098 // Close the subwindow if it exists.
2099 if (mTabControl.getCurrentSubWindow() != null) {
2100 dismissSubWindow(mTabControl.getCurrentTab());
2101 break;
2102 }
2103 closeCurrentTab();
2104 break;
2105
kaiyiza8b6dbb2013-07-29 18:11:22 +08002106 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002107 Object[] params = { new String("persist.debug.browsermonkeytest")};
2108 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002109 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002110 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002111 if (ret != null && ret.equals("enable"))
2112 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002113 if (BrowserConfig.getInstance(getContext())
2114 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2115 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002116 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002117 case R.id.homepage_menu_id:
2118 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002119 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002120 break;
2121
2122 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002123 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002124 break;
2125
2126 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002127 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002128 break;
2129
John Reck2bc80422011-06-30 15:11:49 -07002130 case R.id.save_snapshot_menu_id:
2131 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002132 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002133 createScreenshotAsync(
2134 source.getWebView(),
2135 getDesiredThumbnailWidth(mActivity),
2136 getDesiredThumbnailHeight(mActivity),
2137 new ValueCallback<Bitmap>() {
2138 @Override
2139 public void onReceiveValue(Bitmap bitmap) {
2140 new SaveSnapshotTask(source, bitmap).execute();
2141 }
2142 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002143 break;
2144
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002146 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002147 break;
2148
John Recke1a03a32011-09-14 17:04:16 -07002149 case R.id.snapshot_go_live:
2150 goLive();
2151 return true;
2152
Michael Kolb8233fac2010-10-26 16:08:53 -07002153 case R.id.share_page_menu_id:
2154 Tab currentTab = mTabControl.getCurrentTab();
2155 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002156 return false;
2157 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002158 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002159 break;
2160
2161 case R.id.dump_nav_menu_id:
2162 getCurrentTopWebView().debugDump();
2163 break;
2164
Michael Kolb8233fac2010-10-26 16:08:53 -07002165 case R.id.zoom_in_menu_id:
2166 getCurrentTopWebView().zoomIn();
2167 break;
2168
2169 case R.id.zoom_out_menu_id:
2170 getCurrentTopWebView().zoomOut();
2171 break;
2172
2173 case R.id.view_downloads_menu_id:
2174 viewDownloads();
2175 break;
2176
John Reck42229bc2011-08-19 13:26:43 -07002177 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002178 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002179 break;
2180
Michael Kolb8233fac2010-10-26 16:08:53 -07002181 case R.id.window_one_menu_id:
2182 case R.id.window_two_menu_id:
2183 case R.id.window_three_menu_id:
2184 case R.id.window_four_menu_id:
2185 case R.id.window_five_menu_id:
2186 case R.id.window_six_menu_id:
2187 case R.id.window_seven_menu_id:
2188 case R.id.window_eight_menu_id:
2189 {
2190 int menuid = item.getItemId();
2191 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2192 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2193 Tab desiredTab = mTabControl.getTab(id);
2194 if (desiredTab != null &&
2195 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002196 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002197 }
2198 break;
2199 }
2200 }
2201 }
2202 break;
2203
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002204 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002205 Bundle bundle = new Bundle();
2206 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2207 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2208 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2209 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2210 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002211 break;
2212
Pankaj Garg49b79252014-11-07 17:33:41 -08002213 case R.id.add_to_homescreen:
2214 final WebView w = getCurrentTopWebView();
2215 final EditText input = new EditText(getContext());
2216 input.setText(w.getTitle());
2217 new AlertDialog.Builder(getContext())
2218 .setTitle("Add to homescreen")
2219 .setMessage("Title")
2220 .setView(input)
2221 .setPositiveButton("Add", new DialogInterface.OnClickListener() {
2222 public void onClick(DialogInterface dialog, int whichButton) {
2223 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2224 getContext(),
2225 w.getUrl(),
2226 input.getText().toString(),
2227 w.getViewportBitmap(),
2228 w.getFavicon()));
2229
2230 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2231 .addCategory(Intent.CATEGORY_HOME));
2232 }})
2233 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
2234 public void onClick(DialogInterface dialog, int whichButton) {
2235 // Do nothing.
2236 }
2237 })
2238 .show();
2239 break;
2240
Michael Kolb8233fac2010-10-26 16:08:53 -07002241 default:
2242 return false;
2243 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002244 return true;
2245 }
2246
John Reck68234a92012-04-19 15:27:12 -07002247 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2248 implements OnCancelListener {
2249
2250 private Tab mTab;
2251 private Dialog mProgressDialog;
2252 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002253 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002254
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002255 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002256 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002257 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002258 }
2259
2260 @Override
2261 protected void onPreExecute() {
2262 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2263 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2264 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002265 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002266 }
2267
2268 @Override
2269 protected Long doInBackground(Void... params) {
2270 if (!mTab.saveViewState(mValues)) {
2271 return null;
2272 }
2273 if (isCancelled()) {
2274 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2275 File file = mActivity.getFileStreamPath(path);
2276 if (!file.delete()) {
2277 file.deleteOnExit();
2278 }
2279 return null;
2280 }
2281 final ContentResolver cr = mActivity.getContentResolver();
2282 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2283 if (result == null) {
2284 return null;
2285 }
2286 long id = ContentUris.parseId(result);
2287 return id;
2288 }
2289
2290 @Override
2291 protected void onPostExecute(Long id) {
2292 if (isCancelled()) {
2293 return;
2294 }
2295 mProgressDialog.dismiss();
2296 if (id == null) {
2297 Toast.makeText(mActivity, R.string.snapshot_failed,
2298 Toast.LENGTH_SHORT).show();
2299 return;
2300 }
2301 Bundle b = new Bundle();
2302 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2303 mUi.showComboView(ComboViews.Snapshots, b);
2304 }
2305
2306 @Override
2307 public void onCancel(DialogInterface dialog) {
2308 cancel(true);
2309 }
2310 }
2311
Michael Kolb80f75082012-04-10 10:50:06 -07002312 @Override
2313 public void toggleUserAgent() {
2314 WebView web = getCurrentWebView();
2315 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002316 }
2317
2318 @Override
2319 public void findOnPage() {
2320 getCurrentTopWebView().showFindDialog(null, true);
2321 }
2322
2323 @Override
2324 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002325 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002326 }
2327
Pankaj Gargeba076f2015-03-25 13:40:59 -07002328 // This function is specifically used from AddBookmark Activity.
2329 // The bookmark activity clears the bitmap after retrieving it.
2330 // The function usage elsewhere will result in breaking bookmark
2331 // functionality.
2332 public static Bitmap getAndReleaseLastBookmarkBitmapFromIntent() {
2333 Bitmap bitmap = mBookmarkBitmap;
2334 mBookmarkBitmap = null;
2335 return bitmap;
2336 }
2337
Michael Kolb80f75082012-04-10 10:50:06 -07002338 @Override
2339 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002340 WebView w = getCurrentTopWebView();
2341 if (w == null)
2342 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002343 final Intent i = createBookmarkCurrentPageIntent(false);
2344 createScreenshotAsync(
2345 w, getDesiredThumbnailWidth(mActivity),
2346 getDesiredThumbnailHeight(mActivity),
2347 new ValueCallback<Bitmap>() {
2348 @Override
2349 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargeba076f2015-03-25 13:40:59 -07002350 mBookmarkBitmap = bitmap;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002351 mActivity.startActivity(i);
2352 }
2353 });
Michael Kolb80f75082012-04-10 10:50:06 -07002354 }
2355
John Recke1a03a32011-09-14 17:04:16 -07002356 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002357 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002358 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002359 boolean onlySingleTabRemaining = false;
2360 if (mTabControl.getTabCount() > 1) {
2361 // destroy the old snapshot tab
2362 closeCurrentTab();
2363 } else {
2364 onlySingleTabRemaining = true;
2365 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002366 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002367 if (onlySingleTabRemaining) {
2368 closeTab(t);
2369 }
2370
Tarun Nainani8eb00912014-07-17 12:28:32 -07002371 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002372 }
2373
luxiaolb40014b2013-07-19 10:01:43 +08002374 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002375 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002376 new AlertDialog.Builder(activity)
2377 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002378 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002379 .setMessage(R.string.exit_browser_msg)
2380 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2381 public void onClick(DialogInterface dialog, int which) {
2382 activity.moveTaskToBack(true);
2383 dialog.dismiss();
2384 }
2385 })
2386 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2387 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002388 mCrashRecoveryHandler.clearState(true);
2389 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002390 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002391 dialog.dismiss();
2392 }
2393 })
2394 .show();
2395 }
Michael Kolb315d5022011-10-13 12:47:11 -07002396 @Override
2397 public void showPageInfo() {
2398 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2399 }
2400
John Reck9c35b9c2012-05-30 10:08:50 -07002401 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002402 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002403 // Let the History and Bookmark fragments handle menus they created.
2404 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2405 return false;
2406 }
2407
Michael Kolb8233fac2010-10-26 16:08:53 -07002408 int id = item.getItemId();
2409 boolean result = true;
2410 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002411 // -- Browser context menu
2412 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002413 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002414 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002415 case R.id.copy_link_context_menu_id:
2416 final WebView webView = getCurrentTopWebView();
2417 if (null == webView) {
2418 result = false;
2419 break;
2420 }
2421 final HashMap<String, WebView> hrefMap =
2422 new HashMap<String, WebView>();
2423 hrefMap.put("webview", webView);
2424 final Message msg = mHandler.obtainMessage(
2425 FOCUS_NODE_HREF, id, 0, hrefMap);
2426 webView.requestFocusNodeHref(msg);
2427 break;
2428
2429 default:
2430 // For other context menus
2431 result = onOptionsItemSelected(item);
2432 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 return result;
2434 }
2435
2436 /**
2437 * support programmatically opening the context menu
2438 */
2439 public void openContextMenu(View view) {
2440 mActivity.openContextMenu(view);
2441 }
2442
2443 /**
2444 * programmatically open the options menu
2445 */
2446 public void openOptionsMenu() {
2447 mActivity.openOptionsMenu();
2448 }
2449
John Reck9c35b9c2012-05-30 10:08:50 -07002450 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002451 public boolean onMenuOpened(int featureId, Menu menu) {
2452 if (mOptionsMenuOpen) {
2453 if (mConfigChanged) {
2454 // We do not need to make any changes to the state of the
2455 // title bar, since the only thing that happened was a
2456 // change in orientation
2457 mConfigChanged = false;
2458 } else {
2459 if (!mExtendedMenuOpen) {
2460 mExtendedMenuOpen = true;
2461 mUi.onExtendedMenuOpened();
2462 } else {
2463 // Switching the menu back to icon view, so show the
2464 // title bar once again.
2465 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002466 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002467 }
2468 }
2469 } else {
2470 // The options menu is closed, so open it, and show the title
2471 mOptionsMenuOpen = true;
2472 mConfigChanged = false;
2473 mExtendedMenuOpen = false;
2474 mUi.onOptionsMenuOpened();
2475 }
2476 return true;
2477 }
2478
John Reck9c35b9c2012-05-30 10:08:50 -07002479 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002480 public void onOptionsMenuClosed(Menu menu) {
2481 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002482 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002483 }
2484
John Reck9c35b9c2012-05-30 10:08:50 -07002485 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002486 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002487 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002488 }
2489
2490 // Helper method for getting the top window.
2491 @Override
2492 public WebView getCurrentTopWebView() {
2493 return mTabControl.getCurrentTopWebView();
2494 }
2495
2496 @Override
2497 public WebView getCurrentWebView() {
2498 return mTabControl.getCurrentWebView();
2499 }
2500
2501 /*
2502 * This method is called as a result of the user selecting the options
2503 * menu to see the download window. It shows the download window on top of
2504 * the current window.
2505 */
2506 void viewDownloads() {
2507 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2508 mActivity.startActivity(intent);
2509 }
2510
John Reck30b065e2011-07-19 10:58:05 -07002511 int getActionModeHeight() {
2512 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2513 new int[] { android.R.attr.actionBarSize });
2514 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2515 actionBarSizeTypedArray.recycle();
2516 return size;
2517 }
2518
Michael Kolb8233fac2010-10-26 16:08:53 -07002519 // action mode
2520
John Reck9c35b9c2012-05-30 10:08:50 -07002521 @Override
2522 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002523 mUi.onActionModeStarted(mode);
2524 mActionMode = mode;
2525 }
2526
2527 /*
2528 * True if a custom ActionMode (i.e. find or select) is in use.
2529 */
2530 @Override
2531 public boolean isInCustomActionMode() {
2532 return mActionMode != null;
2533 }
2534
2535 /*
2536 * End the current ActionMode.
2537 */
2538 @Override
2539 public void endActionMode() {
2540 if (mActionMode != null) {
2541 mActionMode.finish();
2542 }
2543 }
2544
2545 /*
2546 * Called by find and select when they are finished. Replace title bars
2547 * as necessary.
2548 */
John Reck9c35b9c2012-05-30 10:08:50 -07002549 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002550 public void onActionModeFinished(ActionMode mode) {
2551 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002552 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002553 mActionMode = null;
2554 }
2555
2556 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002557 final Tab tab = getCurrentTab();
2558 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002559 }
2560
2561 // bookmark handling
2562
2563 /**
2564 * add the current page as a bookmark to the given folder id
2565 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002566 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002567 * bookmarked, and if it is, edit that bookmark. If false, and
2568 * the site is already bookmarked, do not attempt to edit the
2569 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002570 */
2571 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002572 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002573 WebView w = getCurrentTopWebView();
2574 if (w == null) {
2575 return null;
2576 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002577 Intent i = new Intent(mActivity,
2578 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002579 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2580 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2581 String touchIconUrl = w.getTouchIconUrl();
2582 if (touchIconUrl != null) {
2583 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2584 WebSettings settings = w.getSettings();
2585 if (settings != null) {
2586 i.putExtra(AddBookmarkPage.USER_AGENT,
2587 settings.getUserAgentString());
2588 }
2589 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002590 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002591 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002592 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002593 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2594 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002595 // Put the dialog at the upper right of the screen, covering the
2596 // star on the title bar.
2597 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002598 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 }
2600
2601 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002602 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002603 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002604 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002605 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 }
2607
Vivek Sekharb54614f2014-05-01 19:03:37 -07002608 @Override
2609 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2610 boolean capture) {
2611 mUploadHandler = new UploadHandler(this);
2612 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2613 }
2614
Michael Kolb8233fac2010-10-26 16:08:53 -07002615 // thumbnails
2616
2617 /**
2618 * Return the desired width for thumbnail screenshots, which are stored in
2619 * the database, and used on the bookmarks screen.
2620 * @param context Context for finding out the density of the screen.
2621 * @return desired width for thumbnail screenshot.
2622 */
2623 static int getDesiredThumbnailWidth(Context context) {
2624 return context.getResources().getDimensionPixelOffset(
2625 R.dimen.bookmarkThumbnailWidth);
2626 }
2627
2628 /**
2629 * Return the desired height for thumbnail screenshots, which are stored in
2630 * the database, and used on the bookmarks screen.
2631 * @param context Context for finding out the density of the screen.
2632 * @return desired height for thumbnail screenshot.
2633 */
2634 static int getDesiredThumbnailHeight(Context context) {
2635 return context.getResources().getDimensionPixelOffset(
2636 R.dimen.bookmarkThumbnailHeight);
2637 }
2638
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002639 static void createScreenshotAsync(WebView view, int width, int height,
2640 final ValueCallback<Bitmap> cb) {
2641 if (view == null || width == 0 || height == 0) {
2642 return;
2643 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002644 view.getContentBitmapAsync(
2645 (float) width / view.getWidth(),
2646 new Rect(),
2647 new ValueCallback<Bitmap>() {
2648 @Override
2649 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002650 if (bitmap != null)
2651 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2652 cb.onReceiveValue(bitmap);
2653 }});
2654 }
2655
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002656 private void updateScreenshot(final Tab tab) {
2657 createScreenshotAsync(
2658 tab.getWebView(),
2659 getDesiredThumbnailWidth(mActivity),
2660 getDesiredThumbnailHeight(mActivity),
2661 new ValueCallback<Bitmap>() {
2662 @Override
2663 public void onReceiveValue(Bitmap bitmap) {
2664 updateScreenshot(tab, bitmap);
2665 }
2666 });
2667 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002668
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002669 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002670 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002671 // FIXME: Would like to make sure there is actually something to
2672 // draw, but the API for that (WebViewCore.pictureReady()) is not
2673 // currently accessible here.
2674
John Reck34ef2672011-02-10 11:30:55 -08002675 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002676 if (view == null) {
2677 // Tab was destroyed
2678 return;
2679 }
John Reck34ef2672011-02-10 11:30:55 -08002680 final String url = tab.getUrl();
2681 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002682 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002683 if (TextUtils.isEmpty(url)) {
2684 return;
2685 }
2686
kaiyiz6e5b3e02013-08-19 20:02:01 +08002687 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002688 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002689 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002690 }
John Reck34ef2672011-02-10 11:30:55 -08002691 // Only update thumbnails for web urls (http(s)://), not for
2692 // about:, javascript:, data:, etc...
2693 // Unless it is a bookmarked site, then always update
2694 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2695 return;
2696 }
2697
kaiyiz6e5b3e02013-08-19 20:02:01 +08002698 if (url != null && mUpdateMyNavThumbnailUrl != null
2699 && Patterns.WEB_URL.matcher(url).matches()
2700 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2701 String urlHost = (new WebAddress(url)).getHost();
2702 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2703 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2704 || bookmarkHost.length() == 0) {
2705 return;
2706 }
2707 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2708 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2709 bookmarkHost.length());
2710 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2711 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2712 if (!bookmarkDomain.equals(urlDomain)) {
2713 return;
2714 }
2715 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002716 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002717 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2718 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2719 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2720 500);
2721 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002722 return;
2723 }
2724
2725 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002726 new AsyncTask<Void, Void, Void>() {
2727 @Override
2728 protected Void doInBackground(Void... unused) {
2729 Cursor cursor = null;
2730 try {
2731 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002732 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2733 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2734 : url);
2735 if (mUpdateMyNavThumbnail) {
2736 mUpdateMyNavThumbnail = false;
2737 mUpdateMyNavThumbnailUrl = null;
2738 }
John Reck34ef2672011-02-10 11:30:55 -08002739 if (cursor != null && cursor.moveToFirst()) {
2740 final ByteArrayOutputStream os =
2741 new ByteArrayOutputStream();
2742 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002743
John Reck34ef2672011-02-10 11:30:55 -08002744 ContentValues values = new ContentValues();
2745 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002746
John Reck34ef2672011-02-10 11:30:55 -08002747 do {
John Reck617fd832011-02-16 14:35:59 -08002748 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002749 cr.update(Images.CONTENT_URI, values, null, null);
2750 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002751 }
John Reck34ef2672011-02-10 11:30:55 -08002752 } catch (IllegalStateException e) {
2753 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002754 } catch (SQLiteException s) {
2755 // Added for possible error when user tries to remove the same bookmark
2756 // that is being updated with a screen shot
2757 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002758 } finally {
2759 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002760 }
John Reck34ef2672011-02-10 11:30:55 -08002761 return null;
2762 }
2763 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002764 }
2765
2766 private class Copy implements OnMenuItemClickListener {
2767 private CharSequence mText;
2768
John Reck9c35b9c2012-05-30 10:08:50 -07002769 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002770 public boolean onMenuItemClick(MenuItem item) {
2771 copy(mText);
2772 return true;
2773 }
2774
2775 public Copy(CharSequence toCopy) {
2776 mText = toCopy;
2777 }
2778 }
2779
Leon Scroggins63c02662010-11-18 15:16:27 -05002780 private static class Download implements OnMenuItemClickListener {
2781 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002782 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002783 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002784 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002785 private static final String FALLBACK_EXTENSION = "dat";
2786 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002787
John Reck9c35b9c2012-05-30 10:08:50 -07002788 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002789 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002790 if (DataUri.isDataUri(mText)) {
2791 saveDataUri();
2792 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002793 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002794 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002795 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002796 return true;
2797 }
2798
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002799 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2800 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002801 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002802 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002803 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002804 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002805 }
George Mount387d45d2011-10-07 15:57:53 -07002806
2807 /**
2808 * Treats mText as a data URI and writes its contents to a file
2809 * based on the current time.
2810 */
2811 private void saveDataUri() {
2812 FileOutputStream outputStream = null;
2813 try {
2814 DataUri uri = new DataUri(mText);
2815 File target = getTarget(uri);
2816 outputStream = new FileOutputStream(target);
2817 outputStream.write(uri.getData());
2818 final DownloadManager manager =
2819 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2820 manager.addCompletedDownload(target.getName(),
2821 mActivity.getTitle().toString(), false,
2822 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002823 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002824 } catch (IOException e) {
2825 Log.e(LOGTAG, "Could not save data URL");
2826 } finally {
2827 if (outputStream != null) {
2828 try {
2829 outputStream.close();
2830 } catch (IOException e) {
2831 // ignore close errors
2832 }
2833 }
2834 }
2835 }
2836
2837 /**
2838 * Creates a File based on the current time stamp and uses
2839 * the mime type of the DataUri to get the extension.
2840 */
2841 private File getTarget(DataUri uri) throws IOException {
2842 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002843 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002844 String nameBase = format.format(new Date());
2845 String mimeType = uri.getMimeType();
2846 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2847 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2848 if (extension == null) {
2849 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2850 extension = FALLBACK_EXTENSION;
2851 }
2852 extension = "." + extension; // createTempFile needs the '.'
2853 File targetFile = File.createTempFile(nameBase, extension, dir);
2854 return targetFile;
2855 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002856 }
2857
Cary Clark8974d282010-11-22 10:46:05 -05002858 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002859 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002860
John Reck9c35b9c2012-05-30 10:08:50 -07002861 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002862 public boolean onMenuItemClick(MenuItem item) {
2863 if (mWebView != null) {
2864 return mWebView.selectText();
2865 }
2866 return false;
2867 }
2868
2869 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002870 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002871 }
2872
2873 }
2874
Michael Kolb8233fac2010-10-26 16:08:53 -07002875 /********************** TODO: UI stuff *****************************/
2876
2877 // these methods have been copied, they still need to be cleaned up
2878
2879 /****************** tabs ***************************************************/
2880
2881 // basic tab interactions:
2882
2883 // it is assumed that tabcontrol already knows about the tab
2884 protected void addTab(Tab tab) {
2885 mUi.addTab(tab);
2886 }
2887
2888 protected void removeTab(Tab tab) {
2889 mUi.removeTab(tab);
2890 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002891 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002892 }
2893
Michael Kolbf2055602011-04-09 17:20:03 -07002894 @Override
2895 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002896 // monkey protection against delayed start
2897 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002898
2899 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2900 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002901 mTabControl.setCurrentTab(tab);
2902 // the tab is guaranteed to have a webview after setCurrentTab
2903 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002904
2905
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002906 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002907 //Purge active tabs
2908 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002909 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002910 }
2911
John Reck8bcafc12011-08-29 16:43:02 -07002912 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002913 Tab current = mTabControl.getCurrentTab();
2914 if (current != null
2915 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002916 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002917 }
2918 }
2919
John Reck26b18322011-06-21 13:08:58 -07002920 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002921 //Cancel navscreen request
2922 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002923 // Dismiss the subwindow if applicable.
2924 dismissSubWindow(appTab);
2925 // Since we might kill the WebView, remove it from the
2926 // content view first.
2927 mUi.detachTab(appTab);
2928 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002929 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002930 // TODO: analyze why the remove and add are necessary
2931 mUi.attachTab(appTab);
2932 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002933 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002934 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002935 } else {
2936 // If the tab was the current tab, we have to attach
2937 // it to the view system again.
2938 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002939 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002940 }
2941 }
2942
2943 // Remove the sub window if it exists. Also called by TabControl when the
2944 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002945 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002946 public void dismissSubWindow(Tab tab) {
2947 removeSubWindow(tab);
2948 // dismiss the subwindow. This will destroy the WebView.
2949 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002950 WebView wv = getCurrentTopWebView();
2951 if (wv != null) {
2952 wv.requestFocus();
2953 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002954 }
2955
2956 @Override
2957 public void removeSubWindow(Tab t) {
2958 if (t.getSubWebView() != null) {
2959 mUi.removeSubWindow(t.getSubViewContainer());
2960 }
2961 }
2962
2963 @Override
2964 public void attachSubWindow(Tab tab) {
2965 if (tab.getSubWebView() != null) {
2966 mUi.attachSubWindow(tab.getSubViewContainer());
2967 getCurrentTopWebView().requestFocus();
2968 }
2969 }
2970
Mathew Inwood29721c22011-06-29 17:55:24 +01002971 private Tab showPreloadedTab(final UrlData urlData) {
2972 if (!urlData.isPreloaded()) {
2973 return null;
2974 }
2975 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2976 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2977 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002978 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002979 // Could not submit query. Fallback to regular tab creation
2980 tabControl.destroy();
2981 return null;
2982 }
2983 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002984 // check tab count and make room for new tab
2985 if (!mTabControl.canCreateNewTab()) {
2986 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2987 if (leastUsed != null) {
2988 closeTab(leastUsed);
2989 }
2990 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002991 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002992 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002993 mTabControl.addPreloadedTab(t);
2994 addTab(t);
2995 setActiveTab(t);
2996 return t;
2997 }
2998
Michael Kolb7bcafde2011-05-09 13:55:59 -07002999 // open a non inconito tab with the given url data
3000 // and set as active tab
3001 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01003002 Tab tab = showPreloadedTab(urlData);
3003 if (tab == null) {
3004 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07003005 if ((tab != null) && !urlData.isEmpty()) {
3006 loadUrlDataIn(tab, urlData);
3007 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003008 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003009 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07003010 }
3011
Michael Kolb843510f2010-12-09 10:51:49 -08003012 @Override
3013 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003014 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07003015 }
3016
Michael Kolb8233fac2010-10-26 16:08:53 -07003017 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07003018 public Tab openIncognitoTab() {
3019 return openTab(INCOGNITO_URI, true, true, false);
3020 }
3021
3022 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003023 public Tab openTab(String url, boolean incognito, boolean setActive,
3024 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003025 return openTab(url, incognito, setActive, useCurrent, null);
3026 }
3027
3028 @Override
3029 public Tab openTab(String url, Tab parent, boolean setActive,
3030 boolean useCurrent) {
3031 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3032 setActive, useCurrent, parent);
3033 }
3034
3035 public Tab openTab(String url, boolean incognito, boolean setActive,
3036 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003037 Tab tab = createNewTab(incognito, setActive, useCurrent);
3038 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003039 if (parent instanceof SnapshotTab) {
3040 addTab(tab);
3041 if (setActive)
3042 setActiveTab(tab);
3043 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003044 parent.addChildTab(tab);
3045 }
Michael Kolb519d2282011-05-09 17:03:19 -07003046 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003047 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003048 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003049 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003050 return tab;
3051 }
3052
3053 // this method will attempt to create a new tab
3054 // incognito: private browsing tab
3055 // setActive: ste tab as current tab
3056 // useCurrent: if no new tab can be created, return current tab
3057 private Tab createNewTab(boolean incognito, boolean setActive,
3058 boolean useCurrent) {
3059 Tab tab = null;
3060 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05003061 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003062 addTab(tab);
3063 if (setActive) {
3064 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07003065 } else {
3066 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003067 }
3068 } else {
3069 if (useCurrent) {
3070 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07003071 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003072 } else {
3073 mUi.showMaxTabsWarning();
3074 }
3075 }
3076 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003077 }
3078
John Reck2bc80422011-06-30 15:11:49 -07003079 @Override
3080 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3081 SnapshotTab tab = null;
3082 if (mTabControl.canCreateNewTab()) {
3083 tab = mTabControl.createSnapshotTab(snapshotId);
3084 addTab(tab);
3085 if (setActive) {
3086 setActiveTab(tab);
3087 }
3088 } else {
3089 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003090 }
3091 return tab;
3092 }
3093
Michael Kolb8233fac2010-10-26 16:08:53 -07003094 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003095 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003096 * @return boolean True if we successfully switched to a different tab. If
3097 * the indexth tab is null, or if that tab is the same as
3098 * the current one, return false.
3099 */
3100 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003101 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003102 Tab currentTab = mTabControl.getCurrentTab();
3103 if (tab == null || tab == currentTab) {
3104 return false;
3105 }
3106 setActiveTab(tab);
3107 return true;
3108 }
3109
3110 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003111 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003112 closeCurrentTab(false);
3113 }
3114
3115 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003116 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003117 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003118 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003119 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003120 return;
3121 }
Michael Kolbc831b632011-05-11 09:30:34 -07003122 final Tab current = mTabControl.getCurrentTab();
3123 final int pos = mTabControl.getCurrentPosition();
3124 Tab newTab = current.getParent();
3125 if (newTab == null) {
3126 newTab = mTabControl.getTab(pos + 1);
3127 if (newTab == null) {
3128 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003129 }
3130 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003131 if (andQuit) {
3132 mTabControl.setCurrentTab(newTab);
3133 closeTab(current);
3134 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003135 // Close window
3136 closeTab(current);
3137 }
3138 }
3139
3140 /**
3141 * Close the tab, remove its associated title bar, and adjust mTabControl's
3142 * current tab to a valid value.
3143 */
3144 @Override
3145 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003146 if (tab == mTabControl.getCurrentTab()) {
3147 closeCurrentTab();
3148 } else {
3149 removeTab(tab);
3150 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003151 }
3152
Afzal Najamd4e33312012-04-26 01:54:01 -04003153 /**
3154 * Close all tabs except the current one
3155 */
3156 @Override
3157 public void closeOtherTabs() {
3158 int inactiveTabs = mTabControl.getTabCount() - 1;
3159 for (int i = inactiveTabs; i >= 0; i--) {
3160 Tab tab = mTabControl.getTab(i);
3161 if (tab != mTabControl.getCurrentTab()) {
3162 removeTab(tab);
3163 }
3164 }
3165 }
3166
Michael Kolb8233fac2010-10-26 16:08:53 -07003167 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003168 protected void loadUrlFromContext(String url) {
3169 Tab tab = getCurrentTab();
3170 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003171 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003172 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003173 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003174 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003175 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003176 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003177 }
3178 }
3179 }
3180
3181 /**
3182 * Load the URL into the given WebView and update the title bar
3183 * to reflect the new load. Call this instead of WebView.loadUrl
3184 * directly.
3185 * @param view The WebView used to load url.
3186 * @param url The URL to load.
3187 */
John Reck71e51422011-07-01 16:49:28 -07003188 @Override
3189 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003190 loadUrl(tab, url, null);
3191 }
3192
3193 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3194 if (tab != null) {
3195 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003196 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003197 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003198 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003199 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003200 }
3201
3202 /**
3203 * Load UrlData into a Tab and update the title bar to reflect the new
3204 * load. Call this instead of UrlData.loadIn directly.
3205 * @param t The Tab used to load.
3206 * @param data The UrlData being loaded.
3207 */
3208 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003209 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003210 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003211 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003212 } else {
John Reck38b39652012-06-05 09:22:59 -07003213 if (t != null && data.mDisableUrlOverride) {
3214 t.disableUrlOverridingForLoad();
3215 }
John Reck26b18322011-06-21 13:08:58 -07003216 loadUrl(t, data.mUrl, data.mHeaders);
3217 }
3218 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003219 }
3220
John Reck30c714c2010-12-16 17:30:34 -08003221 @Override
3222 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003223 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003224 //In case of tab can go back (aka tab has navigation entry) do nothing
3225 //else just load homepage in current tab.
3226 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003227 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003228 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003229 }
3230
3231 void goBackOnePageOrQuit() {
3232 Tab current = mTabControl.getCurrentTab();
3233 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003234 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3235 showExitDialog(mActivity);
3236 } else {
3237 /*
3238 * Instead of finishing the activity, simply push this to the back
3239 * of the stack and let ActivityManager to choose the foreground
3240 * activity. As BrowserActivity is singleTask, it will be always the
3241 * root of the task. So we can use either true or false for
3242 * moveTaskToBack().
3243 */
3244 mActivity.moveTaskToBack(true);
3245 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003246 return;
3247 }
John Reckef654f12011-07-12 16:42:08 -07003248 if (current.canGoBack()) {
3249 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003250 } else {
3251 // Check to see if we are closing a window that was created by
3252 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003253 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003254 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003255 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003256 // Now we close the other tab
3257 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003258 } else if (BrowserConfig.getInstance(getContext())
3259 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3260 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003261 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003262 if ((current.getAppId() != null) || current.closeOnBack()) {
3263 closeCurrentTab(true);
3264 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003265 /*
3266 * Instead of finishing the activity, simply push this to the back
3267 * of the stack and let ActivityManager to choose the foreground
3268 * activity. As BrowserActivity is singleTask, it will be always the
3269 * root of the task. So we can use either true or false for
3270 * moveTaskToBack().
3271 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003272 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003273 }
3274 }
3275 }
3276
3277 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003278 * helper method for key handler
3279 * returns the current tab if it can't advance
3280 */
Michael Kolbc831b632011-05-11 09:30:34 -07003281 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003282 int pos = mTabControl.getCurrentPosition() + 1;
3283 if (pos >= mTabControl.getTabCount()) {
3284 pos = 0;
3285 }
3286 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003287 }
3288
3289 /**
3290 * helper method for key handler
3291 * returns the current tab if it can't advance
3292 */
Michael Kolbc831b632011-05-11 09:30:34 -07003293 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003294 int pos = mTabControl.getCurrentPosition() - 1;
3295 if ( pos < 0) {
3296 pos = mTabControl.getTabCount() - 1;
3297 }
3298 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003299 }
3300
John Reckbcef87f2012-02-03 14:58:34 -08003301 boolean isMenuOrCtrlKey(int keyCode) {
3302 return (KeyEvent.KEYCODE_MENU == keyCode)
3303 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3304 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3305 }
3306
Michael Kolb0035fad2011-03-14 13:25:28 -07003307 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003308 * handle key events in browser
3309 *
3310 * @param keyCode
3311 * @param event
3312 * @return true if handled, false to pass to super
3313 */
John Reck9c35b9c2012-05-30 10:08:50 -07003314 @Override
3315 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003316 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3317 // Hardware menu key
Enrico Ros1f5a0952014-11-18 20:15:48 -08003318 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
Pankaj Garg49b79252014-11-07 17:33:41 -08003319 true, false);
3320 return true;
3321 }
3322
Cary Clark160bbb92011-01-10 11:17:07 -05003323 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003324 // Even if MENU is already held down, we need to call to super to open
3325 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003326 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003327 mMenuIsDown = true;
3328 return false;
3329 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003330
Cary Clark8ff8c662010-12-29 15:03:05 -05003331 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003332 Tab tab = getCurrentTab();
3333 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003334
Cary Clark160bbb92011-01-10 11:17:07 -05003335 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3336 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003337
Michael Kolb8233fac2010-10-26 16:08:53 -07003338 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003339 case KeyEvent.KEYCODE_TAB:
3340 if (event.isCtrlPressed()) {
3341 if (event.isShiftPressed()) {
3342 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003343 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003344 } else {
3345 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003346 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003347 }
3348 return true;
3349 }
3350 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003351 case KeyEvent.KEYCODE_SPACE:
3352 // WebView/WebTextView handle the keys in the KeyDown. As
3353 // the Activity's shortcut keys are only handled when WebView
3354 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003355 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003356 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003357 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003358 pageDown();
3359 }
3360 return true;
3361 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003362 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003363 event.startTracking();
3364 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003365 case KeyEvent.KEYCODE_FORWARD:
3366 if (!noModifiers) break;
3367 tab.goForward();
3368 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003369 case KeyEvent.KEYCODE_DPAD_LEFT:
3370 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003371 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003372 return true;
3373 }
3374 break;
3375 case KeyEvent.KEYCODE_DPAD_RIGHT:
3376 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003377 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003378 return true;
3379 }
3380 break;
3381 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003382 if (ctrl) {
3383 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003384 return true;
3385 }
3386 break;
Michael Kolba4183062011-01-16 10:43:21 -08003387// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003388 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003389 if (ctrl ) {
3390 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003391 return true;
3392 }
3393 break;
Michael Kolba4183062011-01-16 10:43:21 -08003394// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003395// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003396// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003397// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003398// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003399// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003400// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003401// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003402// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003403// case KeyEvent.KEYCODE_M: // unused
3404// case KeyEvent.KEYCODE_N: // in Chrome: new window
3405// case KeyEvent.KEYCODE_O: // in Chrome: open file
3406// case KeyEvent.KEYCODE_P: // in Chrome: print page
3407// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003408// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003409// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3410 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003411 // we can't use the ctrl/shift flags, they check for
3412 // exclusive use of a modifier
3413 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003414 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003415 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003416 } else {
3417 openTabToHomePage();
3418 }
3419 return true;
3420 }
3421 break;
3422// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3423// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003424// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003425// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3426// case KeyEvent.KEYCODE_Y: // unused
3427// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003428 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003429 // it is a regular key and webview is not null
3430 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003431 }
3432
John Reck9c35b9c2012-05-30 10:08:50 -07003433 @Override
3434 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003435 switch(keyCode) {
3436 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003437 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003438 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003439 return true;
3440 }
3441 break;
3442 }
3443 return false;
3444 }
3445
John Reck9c35b9c2012-05-30 10:08:50 -07003446 @Override
3447 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003448 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003449 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003450 if (KeyEvent.KEYCODE_MENU == keyCode
3451 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003452 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003453 }
3454 }
Cary Clark160bbb92011-01-10 11:17:07 -05003455 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003456 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003457 case KeyEvent.KEYCODE_BACK:
3458 if (event.isTracking() && !event.isCanceled()) {
3459 onBackKey();
3460 return true;
3461 }
3462 break;
3463 }
3464 return false;
3465 }
3466
3467 public boolean isMenuDown() {
3468 return mMenuIsDown;
3469 }
3470
John Reck9c35b9c2012-05-30 10:08:50 -07003471 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003472 public void setupAutoFill(Message message) {
3473 // Open the settings activity at the AutoFill profile fragment so that
3474 // the user can create a new profile. When they return, we will dispatch
3475 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003476 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003477 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3478 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003479 }
John Reckb3417f02011-01-14 11:01:05 -08003480
John Reck9c35b9c2012-05-30 10:08:50 -07003481 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003482 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003483 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003484 return true;
3485 }
3486
John Reck1cf4b792011-07-26 10:22:22 -07003487 @Override
3488 public boolean shouldCaptureThumbnails() {
3489 return mUi.shouldCaptureThumbnails();
3490 }
3491
Michael Kolbc3af0672011-08-09 10:24:41 -07003492 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003493 public boolean supportsVoice() {
3494 PackageManager pm = mActivity.getPackageManager();
3495 List activities = pm.queryIntentActivities(new Intent(
3496 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3497 return activities.size() != 0;
3498 }
3499
3500 @Override
3501 public void startVoiceRecognizer() {
3502 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003503 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003504 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3505 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3506 mActivity.startActivityForResult(voice, VOICE_RESULT);
3507 }
3508
Pankaj Garg7b279f62014-08-12 14:47:18 -07003509 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003510 if (mLevel == level)
3511 return;
3512 mLevel = level;
3513 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003514 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3515 lp.dimAmount = level;
3516 mActivity.getWindow().setAttributes(lp);
3517 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3518 } else {
3519 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3520 }
3521 }
3522
Michael Kolb0b129122012-06-04 16:31:58 -07003523 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003524 public void setBlockEvents(boolean block) {
3525 mBlockEvents = block;
3526 }
3527
John Reck9c35b9c2012-05-30 10:08:50 -07003528 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003529 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003530 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003531 }
3532
John Reck9c35b9c2012-05-30 10:08:50 -07003533 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003534 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003535 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003536 }
3537
John Reck9c35b9c2012-05-30 10:08:50 -07003538 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003539 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003540 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003541 }
3542
John Reck9c35b9c2012-05-30 10:08:50 -07003543 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003544 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003545 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003546 }
3547
John Reck9c35b9c2012-05-30 10:08:50 -07003548 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003549 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003550 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003551 }
3552
Pankaj Garg49b79252014-11-07 17:33:41 -08003553 @Override
3554 public boolean shouldShowAppMenu() {
3555 return true;
3556 }
3557
3558 @Override
3559 public int getMenuThemeResourceId() {
3560 return R.style.OverflowMenuTheme;
3561 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003562}