blob: f6e8cbfe011c1813339714ef45d2a05e5cbbb730 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
Site Maoabb7bd32015-03-20 15:34:02 -07004 * Copyright (C) 2015 The Linux Foundation
5 *
Michael Kolb8233fac2010-10-26 16:08:53 -07006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Bijan Amirzada41242f22014-03-21 12:12:18 -070019package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070020
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080022
luxiaolb40014b2013-07-19 10:01:43 +080023import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070025import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070026import android.app.ProgressDialog;
Vivek Sekharce2a4832014-03-26 13:26:53 -070027import android.content.Context;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070030import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070031import android.content.ContentValues;
John Reck68234a92012-04-19 15:27:12 -070032import android.content.DialogInterface;
33import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.content.Intent;
Site Maoabb7bd32015-03-20 15:34:02 -070035import android.content.IntentFilter;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
38import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070039import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050040import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.database.Cursor;
42import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020043import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070044import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080045import android.graphics.BitmapFactory;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080046import android.graphics.Rect;
Tarun Nainanif5563b62015-01-21 18:52:59 -080047import android.media.AudioManager;
kaiyiza016da12013-08-26 17:50:22 +080048import android.net.ConnectivityManager;
49import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.net.Uri;
51import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080052import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080053import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.os.AsyncTask;
Tarun Nainanif5563b62015-01-21 18:52:59 -080055import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070057import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.os.Handler;
59import android.os.Message;
60import android.os.PowerManager;
61import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.provider.ContactsContract;
63import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080064import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070065import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070066import android.text.TextUtils;
67import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080068import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070069import android.view.ActionMode;
70import android.view.ContextMenu;
71import android.view.ContextMenu.ContextMenuInfo;
72import android.view.Gravity;
73import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070074import android.view.Menu;
75import android.view.MenuInflater;
76import android.view.MenuItem;
77import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070078import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070080import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070081import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070082import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080083import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080084import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050085import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070086
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.CookieManager;
88import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080089import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080090import org.codeaurora.swe.HttpAuthHandler;
91import org.codeaurora.swe.SslErrorHandler;
92import org.codeaurora.swe.WebSettings;
93import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070094import org.codeaurora.swe.WebBackForwardList;
95import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080096
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070097import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070098import com.android.browser.R;
99import com.android.browser.IntentHandler.UrlData;
100import com.android.browser.UI.ComboViews;
101import com.android.browser.mynavigation.AddMyNavigationPage;
102import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700103import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700104import com.android.browser.platformsupport.BrowserContract;
105import com.android.browser.platformsupport.WebAddress;
106import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800107import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700108import com.android.browser.provider.BrowserProvider2.Thumbnails;
109import com.android.browser.provider.SnapshotProvider.Snapshots;
110import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800111import com.android.browser.appmenu.AppMenuHandler;
112import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700113
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700115import java.io.File;
116import java.io.FileOutputStream;
117import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700118import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700119import java.text.DateFormat;
120import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700121import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700122import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800124import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200125import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700126import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700127
128/**
129 * Controller for browser
130 */
131public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800132 implements WebViewController, UiController, ActivityController,
133 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700134
135 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800136 private static final String SEND_APP_ID_EXTRA =
137 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Vivek Sekhared791da2015-02-22 12:39:05 -0800138 private static final String INCOGNITO_URI = "chrome://incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800139
Vivek Sekharb54614f2014-05-01 19:03:37 -0700140 // Remind switch to data connection if wifi is unavailable
141 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800142
Michael Kolb8233fac2010-10-26 16:08:53 -0700143 // public message ids
144 public final static int LOAD_URL = 1001;
145 public final static int STOP_LOAD = 1002;
146
147 // Message Ids
148 private static final int FOCUS_NODE_HREF = 102;
149 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800150 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700151
152 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
153
154 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800155 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156
157 private static final int EMPTY_MENU = -1;
158
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700160 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 final static int PREFERENCES_PAGE = 3;
162 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000163 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700164 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800165 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000166
Michael Kolb8233fac2010-10-26 16:08:53 -0700167 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
168
169 // As the ids are dynamically created, we can't guarantee that they will
170 // be in sequence, so this static array maps ids to a window number.
171 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
172 { R.id.window_one_menu_id, R.id.window_two_menu_id,
173 R.id.window_three_menu_id, R.id.window_four_menu_id,
174 R.id.window_five_menu_id, R.id.window_six_menu_id,
175 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
176
177 // "source" parameter for Google search through search key
178 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
179 // "source" parameter for Google search through simplily type
180 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
181
George Mount387d45d2011-10-07 15:57:53 -0700182 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700183 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
184
John Reckd7dd9b22011-08-30 09:18:29 -0700185 // A bitmap that is re-used in createScreenshot as scratch space
186 private static Bitmap sThumbnailBitmap;
187
Michael Kolb8233fac2010-10-26 16:08:53 -0700188 private Activity mActivity;
189 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700190 private HomepageHandler mHomepageHandler;
191 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 private BrowserSettings mSettings;
193 private WebViewFactory mFactory;
194
195 private WakeLock mWakeLock;
196
197 private UrlHandler mUrlHandler;
198 private UploadHandler mUploadHandler;
199 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700200 private PageDialogsHandler mPageDialogsHandler;
201 private NetworkStateHandler mNetworkHandler;
202
Ben Murdoch8029a772010-11-16 11:58:21 +0000203 private Message mAutoFillSetupMessage;
204
kaiyiza016da12013-08-26 17:50:22 +0800205 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700206
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 // FIXME, temp address onPrepareMenu performance problem.
208 // When we move everything out of view, we should rewrite this.
209 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700210 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 private int mOldMenuState = EMPTY_MENU;
212 private Menu mCachedMenu;
213
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 private boolean mMenuIsDown;
215
Pankaj Garg49b79252014-11-07 17:33:41 -0800216 private boolean mWasInPageLoad = false;
217 private AppMenuHandler mAppMenuHandler;
218
Michael Kolb8233fac2010-10-26 16:08:53 -0700219 // For select and find, we keep track of the ActionMode so that
220 // finish() can be called as desired.
221 private ActionMode mActionMode;
222
223 /**
224 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
225 * of whether the configuration has changed. The first onMenuOpened call
226 * after a configuration change is simply a reopening of the same menu
227 * (i.e. mIconView did not change).
228 */
229 private boolean mConfigChanged;
230
231 /**
232 * Keeps track of whether the options menu is open. This is important in
233 * determining whether to show or hide the title bar overlay
234 */
235 private boolean mOptionsMenuOpen;
236
237 /**
238 * Whether or not the options menu is in its bigger, popup menu form. When
239 * true, we want the title bar overlay to be gone. When false, we do not.
240 * Only meaningful if mOptionsMenuOpen is true.
241 */
242 private boolean mExtendedMenuOpen;
243
Michael Kolb8233fac2010-10-26 16:08:53 -0700244 private boolean mActivityPaused = true;
245 private boolean mLoadStopped;
246
247 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500248 // Checks to see when the bookmarks database has changed, and updates the
249 // Tabs' notion of whether they represent bookmarked sites.
250 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700251 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700252
Michael Kolbc3af0672011-08-09 10:24:41 -0700253 private boolean mBlockEvents;
254
Michael Kolb0b129122012-06-04 16:31:58 -0700255 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800256 private boolean mUpdateMyNavThumbnail;
257 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800258 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800259 private WebView.HitTestResult mResult;
Pankaj Gargeba076f2015-03-25 13:40:59 -0700260 private static Bitmap mBookmarkBitmap;
Site Maoabb7bd32015-03-20 15:34:02 -0700261 private PowerConnectionReceiver mLowPowerReceiver;
262 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700263
George Mount3636d0a2011-11-21 09:08:21 -0800264 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 mActivity = browser;
266 mSettings = BrowserSettings.getInstance();
267 mTabControl = new TabControl(this);
268 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700269 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800270 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700271 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700272
273 mUrlHandler = new UrlHandler(this);
274 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
276
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500278 mBookmarksObserver = new ContentObserver(mHandler) {
279 @Override
280 public void onChange(boolean selfChange) {
281 int size = mTabControl.getTabCount();
282 for (int i = 0; i < size; i++) {
283 mTabControl.getTab(i).updateBookmarkedStatus();
284 }
285 }
286
287 };
288 browser.getContentResolver().registerContentObserver(
289 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700290
291 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700292 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800293 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700294 }
295
John Reck9c35b9c2012-05-30 10:08:50 -0700296 @Override
297 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700298 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800299 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800300 // mCrashRecoverHandler has any previously saved state.
301 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700302 }
303
George Mount3636d0a2011-11-21 09:08:21 -0800304 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800305 // we dont want to ever recover incognito tabs
306 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700307
Patrick Scott7d50a932011-02-04 09:27:26 -0500308 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700309 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000311
Michael Kolbc831b632011-05-11 09:30:34 -0700312 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500313 // Not able to restore so we go ahead and clear session cookies. We
314 // must do this before trying to login the user as we don't want to
315 // clear any session cookies set during login.
316 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700317 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800318 if (intent == null) {
319 // This won't happen under common scenarios. The icicle is
320 // not null, but there aren't any tabs to restore.
321 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700322 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800323 final Bundle extra = intent.getExtras();
324 // Create an initial tab.
325 // If the intent is ACTION_VIEW and data is not null, the Browser is
326 // invoked to view the content by another application. In this case,
327 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800328 UrlData urlData = null;
329 if (intent.getData() != null
330 && Intent.ACTION_VIEW.equals(intent.getAction())
331 && intent.getData().toString().startsWith("content://")) {
332 urlData = new UrlData(intent.getData().toString());
333 } else {
334 urlData = IntentHandler.getUrlDataFromIntent(intent);
335 }
George Mount3636d0a2011-11-21 09:08:21 -0800336 Tab t = null;
337 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700338 String landingPage = mActivity.getResources().getString(
339 R.string.def_landing_page);
340 if (!landingPage.isEmpty()) {
341 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800342 } else {
343 t = openTabToHomePage();
344 }
George Mount3636d0a2011-11-21 09:08:21 -0800345 } else {
346 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700347 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800348 }
349 if (t != null) {
350 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
351 }
352 WebView webView = t.getWebView();
353 if (extra != null) {
354 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
355 if (scale > 0 && scale <= 1000) {
356 webView.setInitialScale(scale);
357 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 }
359 }
John Reckd8c74522011-06-14 08:45:00 -0700360 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700362 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500363 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700364 List<Tab> tabs = mTabControl.getTabs();
365 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700366
John Reck1cf4b792011-07-26 10:22:22 -0700367 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700368 //handle restored pages that may require a JS interface
369 if (t.getWebView() != null) {
370 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
371 if (backForwardList != null) {
372 for (int i = 0; i < backForwardList.getSize(); i++) {
373 WebHistoryItem item = backForwardList.getItemAtIndex(i);
374 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
375 }
376 }
377 }
John Reck1cf4b792011-07-26 10:22:22 -0700378 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700379 if (t != mTabControl.getCurrentTab()) {
380 t.pause();
381 }
John Reck1cf4b792011-07-26 10:22:22 -0700382 }
383 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700384 if (tabs.size() == 0) {
385 openTabToHomePage();
386 }
John Reck1cf4b792011-07-26 10:22:22 -0700387 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 // TabControl.restoreState() will create a new tab even if
389 // restoring the state fails.
390 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800391 // Intent is non-null when framework thinks the browser should be
392 // launching with a new intent (icicle is null).
393 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700394 mIntentHandler.onNewIntent(intent);
395 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700398 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800399 if (jsFlags.trim().length() != 0) {
400 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 }
George Mount3636d0a2011-11-21 09:08:21 -0800402 if (intent != null
403 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700404 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800405 }
Site Maoabb7bd32015-03-20 15:34:02 -0700406 mLowPowerReceiver = new PowerConnectionReceiver();
407 mPowerChangeReceiver = new PowerConnectionReceiver();
408
409 //always track the android framework's power save mode
410 IntentFilter filter = new IntentFilter();
411 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
412 // Power save mode only exists in Lollipop and above
413 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
414 }
415 filter.addAction(Intent.ACTION_BATTERY_OKAY);
416 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700417 }
418
John Reck1cf4b792011-07-26 10:22:22 -0700419 private static class PruneThumbnails implements Runnable {
420 private Context mContext;
421 private List<Long> mIds;
422
423 PruneThumbnails(Context context, List<Long> preserveIds) {
424 mContext = context.getApplicationContext();
425 mIds = preserveIds;
426 }
427
428 @Override
429 public void run() {
430 ContentResolver cr = mContext.getContentResolver();
431 if (mIds == null || mIds.size() == 0) {
432 cr.delete(Thumbnails.CONTENT_URI, null, null);
433 } else {
434 int length = mIds.size();
435 StringBuilder where = new StringBuilder();
436 where.append(Thumbnails._ID);
437 where.append(" not in (");
438 for (int i = 0; i < length; i++) {
439 where.append(mIds.get(i));
440 if (i < (length - 1)) {
441 where.append(",");
442 }
443 }
444 where.append(")");
445 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
446 }
447 }
448
449 }
450
Michael Kolb1514bb72010-11-22 09:11:48 -0800451 @Override
452 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 return mFactory;
454 }
455
456 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800457 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700458 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800459 }
460
461 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800462 public void createSubWindow(Tab tab) {
463 endActionMode();
464 WebView mainView = tab.getWebView();
465 WebView subView = mFactory.createWebView((mainView == null)
466 ? false
467 : mainView.isPrivateBrowsingEnabled());
468 mUi.createSubWindow(tab, subView);
469 }
470
471 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700472 public Context getContext() {
473 return mActivity;
474 }
475
476 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700477 public Activity getActivity() {
478 return mActivity;
479 }
480
481 void setUi(UI ui) {
482 mUi = ui;
483 }
484
Michael Kolbaf63dba2012-05-16 12:58:05 -0700485 @Override
486 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 return mSettings;
488 }
489
490 IntentHandler getIntentHandler() {
491 return mIntentHandler;
492 }
493
494 @Override
495 public UI getUi() {
496 return mUi;
497 }
498
499 int getMaxTabs() {
500 return mActivity.getResources().getInteger(R.integer.max_tabs);
501 }
502
503 @Override
504 public TabControl getTabControl() {
505 return mTabControl;
506 }
507
Michael Kolb1bf23132010-11-19 12:55:12 -0800508 @Override
509 public List<Tab> getTabs() {
510 return mTabControl.getTabs();
511 }
512
Michael Kolb8233fac2010-10-26 16:08:53 -0700513 private void startHandler() {
514 mHandler = new Handler() {
515
516 @Override
517 public void handleMessage(Message msg) {
518 switch (msg.what) {
519 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700520 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700521 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800522 case UNKNOWN_TYPE_MSG:
523 HashMap unknownTypeMap = (HashMap) msg.obj;
524 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
525 /*
526 * When the context menu is shown to the user
527 * we need to assure that its happening on the current webview
528 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
529 */
530 if (getCurrentWebView() != viewForUnknownType)
531 break;
532
533 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800534 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800535 WebView.HitTestResult result = new WebView.HitTestResult();
536
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800537 // Prevent unnecessary calls to context menu
538 // if url and image src are null
539 if (unknown_type_src == null && unknown_type_url == null)
540 break;
541
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800542 //setting the HitTestResult with new RESULT TYPE
543 if (!TextUtils.isEmpty(unknown_type_src)) {
544 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
545 result.setExtra(unknown_type_src);
546 } else {
547 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
548 result.setExtra("about:blank");
549 }
550
551 mResult = result;
552 openContextMenu(viewForUnknownType);
553 mResult = null;
554
555 break;
556
Michael Kolb8233fac2010-10-26 16:08:53 -0700557 case FOCUS_NODE_HREF:
558 {
559 String url = (String) msg.getData().get("url");
560 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500561 String src = (String) msg.getData().get("src");
562 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700563 if (TextUtils.isEmpty(url)) {
564 break;
565 }
566 HashMap focusNodeMap = (HashMap) msg.obj;
567 WebView view = (WebView) focusNodeMap.get("webview");
568 // Only apply the action if the top window did not change.
569 if (getCurrentTopWebView() != view) {
570 break;
571 }
572 switch (msg.arg1) {
573 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700574 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700575 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500576 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700577 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500578 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500579 case R.id.open_newtab_context_menu_id:
580 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700581 openTab(url, parent,
582 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500583 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700584 case R.id.copy_link_context_menu_id:
585 copy(url);
586 break;
587 case R.id.save_link_context_menu_id:
588 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500589 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800590 mActivity, url, view.getSettings().getUserAgentString(),
591 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700592 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700593 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700594 if(title == null || title == "")
595 title = url;
596
597 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
598 //SWE TODO: No thumbnail support for the url obtained via
599 //browser context menu as its not loaded in webview.
600 if (bookmarkIntent != null) {
601 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
602 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
603 mActivity.startActivity(bookmarkIntent);
604 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700605 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700606 }
607 break;
608 }
609
610 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700611 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 break;
613
614 case STOP_LOAD:
615 stopLoading();
616 break;
617
618 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700619 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 mWakeLock.release();
621 // if we reach here, Browser should be still in the
622 // background loading after WAKELOCK_TIMEOUT (5-min).
623 // To avoid burning the battery, stop loading.
624 mTabControl.stopAllLoading();
625 }
626 break;
627
628 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800629 Tab tab = (Tab) msg.obj;
630 if (tab != null) {
631 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700632 }
633 break;
kaiyiz591110b2013-08-06 17:11:06 +0800634 case OPEN_MENU:
635 if (!mOptionsMenuOpen && mActivity != null ) {
636 mActivity.openOptionsMenu();
637 }
638 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 }
640 }
641 };
642
643 }
644
John Reckef654f12011-07-12 16:42:08 -0700645 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200646 public Tab getCurrentTab() {
647 return mTabControl.getCurrentTab();
648 }
649
Michael Kolbba99c5d2010-11-29 14:57:41 -0800650 @Override
651 public void shareCurrentPage() {
652 shareCurrentPage(mTabControl.getCurrentTab());
653 }
654
655 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700656 if (tab == null || tab.getWebView() == null)
657 return;
658
659 final Tab mytab = tab;
660 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
661 @Override
662 public void onReceiveValue(Bitmap bitmap) {
663 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
664 mytab.getFavicon(), bitmap);
665 }
666 };
667
668 createScreenshotAsync(
669 tab.getWebView(),
670 getDesiredThumbnailWidth(mActivity),
671 getDesiredThumbnailHeight(mActivity),
672 new ValueCallback<Bitmap>() {
673 @Override
674 public void onReceiveValue(Bitmap bitmap) {
675 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
676 mytab.getFavicon(), bitmap);
677 }
678 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800679 }
680
Michael Kolb8233fac2010-10-26 16:08:53 -0700681 /**
682 * Share a page, providing the title, url, favicon, and a screenshot. Uses
683 * an {@link Intent} to launch the Activity chooser.
684 * @param c Context used to launch a new Activity.
685 * @param title Title of the page. Stored in the Intent with
686 * {@link Intent#EXTRA_SUBJECT}
687 * @param url URL of the page. Stored in the Intent with
688 * {@link Intent#EXTRA_TEXT}
689 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
690 * with {@link Browser#EXTRA_SHARE_FAVICON}
691 * @param screenshot Bitmap of a screenshot of the page. Stored in the
692 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
693 */
694 static final void sharePage(Context c, String title, String url,
695 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700696
697 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
698 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
699 R.layout.app_row, sDialog.getApps());
700 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 }
702
703 private void copy(CharSequence text) {
704 ClipboardManager cm = (ClipboardManager) mActivity
705 .getSystemService(Context.CLIPBOARD_SERVICE);
706 cm.setText(text);
707 }
708
709 // lifecycle
710
John Reck9c35b9c2012-05-30 10:08:50 -0700711 @Override
712 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800714 // update the menu in case of a locale change
715 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800716 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800717 if (mOptionsMenuOpen) {
718 mActivity.closeOptionsMenu();
719 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
720 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700721 if (mPageDialogsHandler != null) {
722 mPageDialogsHandler.onConfigurationChanged(config);
723 }
724 mUi.onConfigurationChanged(config);
725 }
726
727 @Override
728 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700729 if (!mUi.isWebShowing()) {
730 mUi.showWeb(false);
731 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700732 mIntentHandler.onNewIntent(intent);
733 }
734
John Reck9c35b9c2012-05-30 10:08:50 -0700735 @Override
736 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800737 if (mUi.isCustomViewShowing()) {
738 hideCustomView();
739 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700740 if (mActivityPaused) {
741 Log.e(LOGTAG, "BrowserActivity is already paused.");
742 return;
743 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800745 Tab tab = mTabControl.getCurrentTab();
746 if (tab != null) {
747 tab.pause();
748 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700749 if (mWakeLock == null) {
750 PowerManager pm = (PowerManager) mActivity
751 .getSystemService(Context.POWER_SERVICE);
752 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
753 }
Michael Kolb70976932010-11-30 11:34:01 -0800754 mWakeLock.acquire();
755 mHandler.sendMessageDelayed(mHandler
756 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
757 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 }
759 mUi.onPause();
760 mNetworkHandler.onPause();
761
762 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100763 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700764 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700765 }
766
John Reck9c35b9c2012-05-30 10:08:50 -0700767 @Override
768 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700769 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800770 Bundle saveState = createSaveState();
771
772 // crash recovery manages all save & restore state
773 mCrashRecoveryHandler.writeState(saveState);
774 mSettings.setLastRunPaused(true);
775 }
776
777 /**
778 * Save the current state to outState. Does not write the state to
779 * disk.
780 * @return Bundle containing the current state of all tabs.
781 */
782 /* package */ Bundle createSaveState() {
783 Bundle saveState = new Bundle();
784 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800785 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 }
787
John Reck9c35b9c2012-05-30 10:08:50 -0700788 @Override
789 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700790 if (!mActivityPaused) {
791 Log.e(LOGTAG, "BrowserActivity is already resumed.");
792 return;
793 }
George Mount3636d0a2011-11-21 09:08:21 -0800794 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800796 Tab current = mTabControl.getCurrentTab();
797 if (current != null) {
798 current.resume();
799 resumeWebViewTimers(current);
800 }
John Reckf57c0292011-07-21 18:15:39 -0700801 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200802
Michael Kolb8233fac2010-10-26 16:08:53 -0700803 mUi.onResume();
804 mNetworkHandler.onResume();
805 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100806 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700807 if (mVoiceResult != null) {
808 mUi.onVoiceResult(mVoiceResult);
809 mVoiceResult = null;
810 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700811 if (current != null && current.getWebView().isShowingCrashView())
812 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700813 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 }
815
John Reckf57c0292011-07-21 18:15:39 -0700816 private void releaseWakeLock() {
817 if (mWakeLock != null && mWakeLock.isHeld()) {
818 mHandler.removeMessages(RELEASE_WAKELOCK);
819 mWakeLock.release();
820 }
821 }
822
Michael Kolb70976932010-11-30 11:34:01 -0800823 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800824 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800825 * @param tab guaranteed non-null
826 */
827 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700828 boolean inLoad = tab.inPageLoad();
829 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
830 CookieSyncManager.getInstance().startSync();
831 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100832 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700833 }
834 }
835
Michael Kolb70976932010-11-30 11:34:01 -0800836 /**
837 * Pause all WebView timers using the WebView of the given tab
838 * @param tab
839 * @return true if the timers are paused or tab is null
840 */
841 private boolean pauseWebViewTimers(Tab tab) {
842 if (tab == null) {
843 return true;
844 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100846 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700848 }
Michael Kolb70976932010-11-30 11:34:01 -0800849 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 }
851
John Reck9c35b9c2012-05-30 10:08:50 -0700852 @Override
853 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800854 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
856 mUploadHandler = null;
857 }
858 if (mTabControl == null) return;
859 mUi.onDestroy();
860 // Remove the current tab and sub window
861 Tab t = mTabControl.getCurrentTab();
862 if (t != null) {
863 dismissSubWindow(t);
864 removeTab(t);
865 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500866 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700867 // Destroy all the tabs
868 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700869 // Unregister receiver
870 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 }
872
873 protected boolean isActivityPaused() {
874 return mActivityPaused;
875 }
876
John Reck9c35b9c2012-05-30 10:08:50 -0700877 @Override
878 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 mTabControl.freeMemory();
880 }
881
882 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 public void stopLoading() {
884 mLoadStopped = true;
885 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700887 if (w != null) {
888 w.stopLoading();
889 mUi.onPageStopped(tab);
890 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700891 }
892
893 boolean didUserStopLoading() {
894 return mLoadStopped;
895 }
896
kaiyiza016da12013-08-26 17:50:22 +0800897 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700898 final String reminderType = getContext().getResources().getString(
899 R.string.def_wifi_browser_interaction_remind_type);
900 final String selectionConnnection = getContext().getResources().getString(
901 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700902 final String wifiSelection = getContext().getResources().getString(
903 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700904
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700905 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
906 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700907 return;
908
kaiyiza016da12013-08-26 17:50:22 +0800909 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700910 Context.CONNECTIVITY_SERVICE);
911 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700912 WifiManager wifiMgr = (WifiManager) this.getContext()
913 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700914 if (networkInfo == null
915 || (networkInfo != null && (networkInfo.getType() !=
916 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700917 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
918 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700919 List<ScanResult> list = wifiMgr.getScanResults();
920 // Have no AP's for Wifi's fall back to data
921 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700922 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700923 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
924 this.getContext().startActivity(intent);
925 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700926 // Request to select Wifi AP
927 try {
928 Intent intent = new Intent(wifiSelection);
929 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
930 this.getContext().startActivity(intent);
931 } catch (Exception e) {
932 String err_msg = this.getContext().getString(
933 R.string.acivity_not_found, wifiSelection);
934 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
935 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700936 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700937 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800938 }
939 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700940
Michael Kolb8233fac2010-10-26 16:08:53 -0700941 // WebViewController
942
943 @Override
John Reck324d4402011-01-11 16:56:42 -0800944 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700945
946 // We've started to load a new page. If there was a pending message
947 // to save a screenshot then we will now take the new page and save
948 // an incorrect screenshot. Therefore, remove any pending thumbnail
949 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700950 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700951
952 // reset sync timer to avoid sync starts during loading a page
953 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700954 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700955 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800956 boolean networkNotifier = BrowserConfig.getInstance(getContext())
957 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700958 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700959 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800960 } else {
961 if (!mNetworkHandler.isNetworkUp()) {
962 view.setNetworkAvailable(false);
963 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 }
965
966 // when BrowserActivity just starts, onPageStarted may be called before
967 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
968 // to start the timer. As we won't switch tabs while an activity is in
969 // pause state, we can ensure calling resume and pause in pair.
970 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800971 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 }
973 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700974 endActionMode();
975
John Reck30c714c2010-12-16 17:30:34 -0800976 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700977
John Reck324d4402011-01-11 16:56:42 -0800978 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 // update the bookmark database for favicon
980 maybeUpdateFavicon(tab, null, url, favicon);
981
982 Performance.tracePageStart(url);
983
984 // Performance probe
985 if (false) {
986 Performance.onPageStarted();
987 }
988
989 }
990
991 @Override
John Reck324d4402011-01-11 16:56:42 -0800992 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700993 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800994 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200995
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 // Performance probe
997 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800998 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700999 }
1000
Tarun Nainani8eb00912014-07-17 12:28:32 -07001001 tab.onPageFinished();
1002
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 Performance.tracePageFinished();
1004 }
1005
1006 @Override
John Reck30c714c2010-12-16 17:30:34 -08001007 public void onProgressChanged(Tab tab) {
1008 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001009
1010 if (newProgress == 100) {
1011 CookieSyncManager.getInstance().sync();
1012 // onProgressChanged() may continue to be called after the main
1013 // frame has finished loading, as any remaining sub frames continue
1014 // to load. We'll only get called once though with newProgress as
1015 // 100 when everything is loaded. (onPageFinished is called once
1016 // when the main frame completes loading regardless of the state of
1017 // any sub frames so calls to onProgressChanges may continue after
1018 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001019 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001020 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001021 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001022 } else if (mWasInPageLoad) {
1023 mWasInPageLoad = false;
1024 updateInLoadMenuItems(mCachedMenu, tab);
1025 }
1026
1027 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001028 // pause the WebView timer and release the wake lock if it is
1029 // finished while BrowserActivity is in pause state.
1030 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 }
John Reckd9862372012-02-21 15:04:50 -08001032 if (!tab.isPrivateBrowsingEnabled()
1033 && !TextUtils.isEmpty(tab.getUrl())
1034 && !tab.isSnapshot()) {
1035 // Only update the bookmark screenshot if the user did not
1036 // cancel the load early and there is not already
1037 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001038 if (tab.shouldUpdateThumbnail() &&
1039 (tab.inForeground() && !didUserStopLoading()
1040 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001041 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1042 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1043 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001044 1500);
John Reckd9862372012-02-21 15:04:50 -08001045 }
1046 }
1047 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001048 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001049 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001051 // still loading
1052 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001053 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001054 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001055 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001056 } else {
1057 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001058 }
1059 }
John Reck30c714c2010-12-16 17:30:34 -08001060 mUi.onProgressChanged(tab);
1061 }
1062
1063 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001064 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001065 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001066 }
1067
1068 @Override
1069 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001070 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001071 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001072 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001073 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1074 return;
1075 }
1076 // Update the title in the history database if not in private browsing mode
1077 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001078 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 }
1080 }
1081
1082 @Override
1083 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001084 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1086 }
1087
1088 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001089 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1090 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 }
1092
1093 @Override
1094 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1095 if (mMenuIsDown) {
1096 // only check shortcut key when MENU is held
1097 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1098 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001099 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001100 int keyCode = event.getKeyCode();
1101 // We need to send almost every key to WebKit. However:
1102 // 1. We don't want to block the device on the renderer for
1103 // some keys like menu, home, call.
1104 // 2. There are no WebKit equivalents for some of these keys
1105 // (see app/keyboard_codes_win.h)
1106 // Note that these are not the same set as KeyEvent.isSystemKey:
1107 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1108 if (keyCode == KeyEvent.KEYCODE_MENU ||
1109 keyCode == KeyEvent.KEYCODE_HOME ||
1110 keyCode == KeyEvent.KEYCODE_BACK ||
1111 keyCode == KeyEvent.KEYCODE_CALL ||
1112 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1113 keyCode == KeyEvent.KEYCODE_POWER ||
1114 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1115 keyCode == KeyEvent.KEYCODE_CAMERA ||
1116 keyCode == KeyEvent.KEYCODE_FOCUS ||
1117 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1118 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1119 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1120 return true;
1121 }
1122
1123 // We also have to intercept some shortcuts before we send them to the ContentView.
1124 if (event.isCtrlPressed() && (
1125 keyCode == KeyEvent.KEYCODE_TAB ||
1126 keyCode == KeyEvent.KEYCODE_W ||
1127 keyCode == KeyEvent.KEYCODE_F4)) {
1128 return true;
1129 }
1130
1131 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001132 }
1133
Tarun Nainanif5563b62015-01-21 18:52:59 -08001134 private void handleMediaKeyEvent(KeyEvent event) {
1135
1136 int keyCode = event.getKeyCode();
1137 // send media key events to audio manager
1138 if (Build.VERSION.SDK_INT >= 19) {
1139
1140 switch (keyCode) {
1141 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1142 case KeyEvent.KEYCODE_MEDIA_STOP:
1143 case KeyEvent.KEYCODE_MEDIA_NEXT:
1144 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1145 case KeyEvent.KEYCODE_MEDIA_REWIND:
1146 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1147 case KeyEvent.KEYCODE_MUTE:
1148 case KeyEvent.KEYCODE_MEDIA_PLAY:
1149 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1150 case KeyEvent.META_SHIFT_RIGHT_ON:
1151 case KeyEvent.KEYCODE_MEDIA_EJECT:
1152 case KeyEvent.KEYCODE_MEDIA_RECORD:
1153 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1154
1155 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1156 .getSystemService(Context.AUDIO_SERVICE);
1157 audioManager.dispatchMediaKeyEvent(event);
1158 }
1159 }
1160 }
1161
Michael Kolb8233fac2010-10-26 16:08:53 -07001162 @Override
John Reck997b1b72012-04-19 18:08:25 -07001163 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001165 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001167 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001168 } else {
John Reck997b1b72012-04-19 18:08:25 -07001169 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 }
1171 }
John Reck997b1b72012-04-19 18:08:25 -07001172 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001173 }
1174
1175 @Override
John Reck324d4402011-01-11 16:56:42 -08001176 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001177 // Don't save anything in private browsing mode or when disabling history
1178 // for regular tabs is enabled
1179 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1180 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1181 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001182
John Reck49a603c2011-03-03 09:33:05 -08001183 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001184
John Reck324d4402011-01-11 16:56:42 -08001185 if (TextUtils.isEmpty(url)
1186 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001187 return;
1188 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001189
John Reckf57c0292011-07-21 18:15:39 -07001190 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001191 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001192 }
1193
1194 @Override
1195 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1196 AsyncTask<Void, Void, String[]> task =
1197 new AsyncTask<Void, Void, String[]>() {
1198 @Override
1199 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001200 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001201 }
1202 @Override
1203 public void onPostExecute(String[] result) {
1204 callback.onReceiveValue(result);
1205 }
1206 };
1207 task.execute();
1208 }
1209
1210 @Override
1211 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1212 final HttpAuthHandler handler, final String host,
1213 final String realm) {
1214 String username = null;
1215 String password = null;
1216
1217 boolean reuseHttpAuthUsernamePassword
1218 = handler.useHttpAuthUsernamePassword();
1219
1220 if (reuseHttpAuthUsernamePassword && view != null) {
1221 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1222 if (credentials != null && credentials.length == 2) {
1223 username = credentials[0];
1224 password = credentials[1];
1225 }
1226 }
1227
1228 if (username != null && password != null) {
1229 handler.proceed(username, password);
1230 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001231 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1233 } else {
1234 handler.cancel();
1235 }
1236 }
1237 }
1238
1239 @Override
1240 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001241 String contentDisposition, String mimetype, String referer,
1242 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001243 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001244 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001245 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001246 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001247 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001248 // This Tab was opened for the sole purpose of downloading a
1249 // file. Remove it.
1250 if (tab == mTabControl.getCurrentTab()) {
1251 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001252 if (tab.getDerivedFromIntent())
1253 closeTab(tab);
1254 else
1255 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001256 } else {
1257 // In this case, it is not.
1258 closeTab(tab);
1259 }
1260 }
1261 }
1262
1263 @Override
1264 public Bitmap getDefaultVideoPoster() {
1265 return mUi.getDefaultVideoPoster();
1266 }
1267
1268 @Override
1269 public View getVideoLoadingProgressView() {
1270 return mUi.getVideoLoadingProgressView();
1271 }
1272
1273 @Override
1274 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1275 SslError error) {
1276 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1277 }
1278
1279 // helper method
1280
1281 /*
1282 * Update the favorites icon if the private browsing isn't enabled and the
1283 * icon is valid.
1284 */
1285 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1286 final String url, Bitmap favicon) {
1287 if (favicon == null) {
1288 return;
1289 }
1290 if (!tab.isPrivateBrowsingEnabled()) {
1291 Bookmarks.updateFavicon(mActivity
1292 .getContentResolver(), originalUrl, url, favicon);
1293 }
1294 }
1295
Leon Scroggins4cd97792010-12-03 15:31:56 -05001296 @Override
1297 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001298 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001299 mUi.bookmarkedStatusHasChanged(tab);
1300 }
1301
Michael Kolb8233fac2010-10-26 16:08:53 -07001302 // end WebViewController
1303
1304 protected void pageUp() {
1305 getCurrentTopWebView().pageUp(false);
1306 }
1307
1308 protected void pageDown() {
1309 getCurrentTopWebView().pageDown(false);
1310 }
1311
1312 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001313 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001314 public void editUrl() {
1315 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001316 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001317 }
1318
John Reck9c35b9c2012-05-30 10:08:50 -07001319 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001320 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001321 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001322 if (tab.inForeground()) {
1323 if (mUi.isCustomViewShowing()) {
1324 callback.onCustomViewHidden();
1325 return;
1326 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001327 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001328 // Save the menu state and set it to empty while the custom
1329 // view is showing.
1330 mOldMenuState = mMenuState;
1331 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001332 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001333 }
1334 }
1335
1336 @Override
1337 public void hideCustomView() {
1338 if (mUi.isCustomViewShowing()) {
1339 mUi.onHideCustomView();
1340 // Reset the old menu state.
1341 mMenuState = mOldMenuState;
1342 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001343 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001344 }
1345 }
1346
John Reck9c35b9c2012-05-30 10:08:50 -07001347 @Override
1348 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001349 Intent intent) {
1350 if (getCurrentTopWebView() == null) return;
1351 switch (requestCode) {
1352 case PREFERENCES_PAGE:
1353 if (resultCode == Activity.RESULT_OK && intent != null) {
1354 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001355 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001356 mTabControl.removeParentChildRelationShips();
1357 }
1358 }
1359 break;
1360 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001361 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001362 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001363 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001365 case AUTOFILL_SETUP:
1366 // Determine whether a profile was actually set up or not
1367 // and if so, send the message back to the WebTextView to
1368 // fill the form with the new profile.
1369 if (getSettings().getAutoFillProfile() != null) {
1370 mAutoFillSetupMessage.sendToTarget();
1371 mAutoFillSetupMessage = null;
1372 }
1373 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001374 case COMBO_VIEW:
1375 if (intent == null || resultCode != Activity.RESULT_OK) {
1376 break;
1377 }
John Reck3ba45532011-08-11 16:26:53 -07001378 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001379 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1380 Tab t = getCurrentTab();
1381 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001382 mUpdateMyNavThumbnail = true;
1383 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001384 loadUrl(t, uri.toString());
1385 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1386 String[] urls = intent.getStringArrayExtra(
1387 ComboViewActivity.EXTRA_OPEN_ALL);
1388 Tab parent = getCurrentTab();
1389 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001390 if (url != null) {
1391 parent = openTab(url, parent,
1392 !mSettings.openInBackground(), true);
1393 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001394 }
1395 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1396 long id = intent.getLongExtra(
1397 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1398 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001399 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001400 }
1401 }
1402 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001403 case VOICE_RESULT:
1404 if (resultCode == Activity.RESULT_OK && intent != null) {
1405 ArrayList<String> results = intent.getStringArrayListExtra(
1406 RecognizerIntent.EXTRA_RESULTS);
1407 if (results.size() >= 1) {
1408 mVoiceResult = results.get(0);
1409 }
1410 }
1411 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001412 case MY_NAVIGATION:
1413 if (intent == null || resultCode != Activity.RESULT_OK) {
1414 break;
1415 }
1416
1417 if (intent.getBooleanExtra("need_refresh", false) &&
1418 getCurrentTopWebView() != null) {
1419 getCurrentTopWebView().reload();
1420 }
1421 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001422 default:
1423 break;
1424 }
1425 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001426 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001427 }
1428
1429 /**
1430 * Open the Go page.
1431 * @param startWithHistory If true, open starting on the history tab.
1432 * Otherwise, start with the bookmarks tab.
1433 */
1434 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001435 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001436 if (mTabControl.getCurrentWebView() == null) {
1437 return;
1438 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001439 // clear action mode
1440 if (isInCustomActionMode()) {
1441 endActionMode();
1442 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 Bundle extras = new Bundle();
1444 // Disable opening in a new window if we have maxed out the windows
1445 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1446 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001447 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001448 }
1449
1450 // combo view callbacks
1451
Michael Kolb8233fac2010-10-26 16:08:53 -07001452 // key handling
1453 protected void onBackKey() {
1454 if (!mUi.onBackKey()) {
1455 WebView subwindow = mTabControl.getCurrentSubWindow();
1456 if (subwindow != null) {
1457 if (subwindow.canGoBack()) {
1458 subwindow.goBack();
1459 } else {
1460 dismissSubWindow(mTabControl.getCurrentTab());
1461 }
1462 } else {
1463 goBackOnePageOrQuit();
1464 }
1465 }
1466 }
1467
Michael Kolb4bd767d2011-05-27 11:33:55 -07001468 protected boolean onMenuKey() {
1469 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001470 }
1471
Michael Kolb8233fac2010-10-26 16:08:53 -07001472 // menu handling and state
1473 // TODO: maybe put into separate handler
1474
John Reck9c35b9c2012-05-30 10:08:50 -07001475 @Override
1476 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001477 if (mMenuState == EMPTY_MENU) {
1478 return false;
1479 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001480 MenuInflater inflater = mActivity.getMenuInflater();
1481 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001482 return true;
1483 }
1484
John Reck9c35b9c2012-05-30 10:08:50 -07001485 @Override
1486 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001487 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001488 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001489 return;
1490 }
1491 if (!(v instanceof WebView)) {
1492 return;
1493 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001494 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001495 WebView.HitTestResult result;
1496
1497 /* Determine whether the ContextMenu got triggered because
1498 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1499 * received. The mResult acts as a flag to identify, how it got trigerred
1500 */
1501 if (mResult == null){
1502 result = webview.getHitTestResult();
1503 } else {
1504 result = mResult;
1505 }
1506
Michael Kolb8233fac2010-10-26 16:08:53 -07001507 if (result == null) {
1508 return;
1509 }
1510
1511 int type = result.getType();
1512 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001513
1514 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1515 unknownTypeMap.put("webview", webview);
1516 final Message msg = mHandler.obtainMessage(
1517 UNKNOWN_TYPE_MSG, unknownTypeMap);
1518 /* As defined in android developers guide
1519 * when UNKNOWN_TYPE is received as a result of HitTest
1520 * you need to determing the type by invoking requestFocusNodeHref
1521 */
1522 webview.requestFocusNodeHref(msg);
1523
Michael Kolb8233fac2010-10-26 16:08:53 -07001524 Log.w(LOGTAG,
1525 "We should not show context menu when nothing is touched");
1526 return;
1527 }
1528 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1529 // let TextView handles context menu
1530 return;
1531 }
1532
1533 // Note, http://b/issue?id=1106666 is requesting that
1534 // an inflated menu can be used again. This is not available
1535 // yet, so inflate each time (yuk!)
1536 MenuInflater inflater = mActivity.getMenuInflater();
1537 inflater.inflate(R.menu.browsercontext, menu);
1538
1539 // Show the correct menu group
1540 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001541 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001542 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001543 menu.setGroupVisible(R.id.PHONE_MENU,
1544 type == WebView.HitTestResult.PHONE_TYPE);
1545 menu.setGroupVisible(R.id.EMAIL_MENU,
1546 type == WebView.HitTestResult.EMAIL_TYPE);
1547 menu.setGroupVisible(R.id.GEO_MENU,
1548 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001549 String itemUrl = null;
1550 String url = webview.getOriginalUrl();
1551 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1552 itemUrl = Uri.decode(navigationUrl);
1553 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1554 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1555 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1556 } else {
1557 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1558 }
1559 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1560 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1561 } else {
1562 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1563
1564 menu.setGroupVisible(R.id.IMAGE_MENU,
1565 type == WebView.HitTestResult.IMAGE_TYPE
1566 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1567 menu.setGroupVisible(R.id.ANCHOR_MENU,
1568 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1569 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001570 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1571 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001572 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001573 // Setup custom handling depending on the type
1574 switch (type) {
1575 case WebView.HitTestResult.PHONE_TYPE:
1576 menu.setHeaderTitle(Uri.decode(extra));
1577 menu.findItem(R.id.dial_context_menu_id).setIntent(
1578 new Intent(Intent.ACTION_VIEW, Uri
1579 .parse(WebView.SCHEME_TEL + extra)));
1580 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1581 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1582 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1583 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1584 addIntent);
1585 menu.findItem(R.id.copy_phone_context_menu_id)
1586 .setOnMenuItemClickListener(
1587 new Copy(extra));
1588 break;
1589
1590 case WebView.HitTestResult.EMAIL_TYPE:
1591 menu.setHeaderTitle(extra);
1592 menu.findItem(R.id.email_context_menu_id).setIntent(
1593 new Intent(Intent.ACTION_VIEW, Uri
1594 .parse(WebView.SCHEME_MAILTO + extra)));
1595 menu.findItem(R.id.copy_mail_context_menu_id)
1596 .setOnMenuItemClickListener(
1597 new Copy(extra));
1598 break;
1599
1600 case WebView.HitTestResult.GEO_TYPE:
1601 menu.setHeaderTitle(extra);
1602 menu.findItem(R.id.map_context_menu_id).setIntent(
1603 new Intent(Intent.ACTION_VIEW, Uri
1604 .parse(WebView.SCHEME_GEO
1605 + URLEncoder.encode(extra))));
1606 menu.findItem(R.id.copy_geo_context_menu_id)
1607 .setOnMenuItemClickListener(
1608 new Copy(extra));
1609 break;
1610
1611 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1612 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001613 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001614 // decide whether to show the open link in new tab option
1615 boolean showNewTab = mTabControl.canCreateNewTab();
1616 MenuItem newTabItem
1617 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001618 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001619 ? R.string.contextmenu_openlink_newwindow_background
1620 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 newTabItem.setVisible(showNewTab);
1622 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001623 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1624 newTabItem.setOnMenuItemClickListener(
1625 new MenuItem.OnMenuItemClickListener() {
1626 @Override
1627 public boolean onMenuItemClick(MenuItem item) {
1628 final HashMap<String, WebView> hrefMap =
1629 new HashMap<String, WebView>();
1630 hrefMap.put("webview", webview);
1631 final Message msg = mHandler.obtainMessage(
1632 FOCUS_NODE_HREF,
1633 R.id.open_newtab_context_menu_id,
1634 0, hrefMap);
1635 webview.requestFocusNodeHref(msg);
1636 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001637 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001638 });
1639 } else {
1640 newTabItem.setOnMenuItemClickListener(
1641 new MenuItem.OnMenuItemClickListener() {
1642 @Override
1643 public boolean onMenuItemClick(MenuItem item) {
1644 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001645 openTab(extra, parent,
1646 !mSettings.openInBackground(),
1647 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001648 return true;
1649 }
1650 });
1651 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001652 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001653 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1654 menu.setHeaderTitle(navigationUrl);
1655 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1656
1657 if (itemUrl != null) {
1658 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1659 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1660 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1661 @Override
1662 public boolean onMenuItemClick(MenuItem item) {
1663 final Intent intent = new Intent(Controller.this
1664 .getContext(),
1665 AddMyNavigationPage.class);
1666 Bundle bundle = new Bundle();
1667 String url = Uri.decode(navigationUrl);
1668 bundle.putBoolean("isAdding", false);
1669 bundle.putString("url", url);
1670 bundle.putString("name", getNameFromUrl(url));
1671 intent.putExtra("websites", bundle);
1672 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1673 return false;
1674 }
1675 });
1676 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1677 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1678 @Override
1679 public boolean onMenuItemClick(MenuItem item) {
1680 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1681 return false;
1682 }
1683 });
1684 }
1685 } else {
1686 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1687 }
1688 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001689 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1690 break;
1691 }
1692 // otherwise fall through to handle image part
1693 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001694 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1695 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001696 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1697 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001698 shareItem.setOnMenuItemClickListener(
1699 new MenuItem.OnMenuItemClickListener() {
1700 @Override
1701 public boolean onMenuItemClick(MenuItem item) {
1702 sharePage(mActivity, null, extra, null,
1703 null);
1704 return true;
1705 }
1706 }
1707 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001708 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001709 menu.findItem(R.id.view_image_context_menu_id)
1710 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1711 @Override
1712 public boolean onMenuItemClick(MenuItem item) {
1713 openTab(extra, mTabControl.getCurrentTab(), true, true);
1714 return false;
1715 }
1716 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001717 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1718 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1719 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001720 menu.findItem(R.id.set_wallpaper_context_menu_id).
1721 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1722 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001723 break;
1724
1725 default:
1726 Log.w(LOGTAG, "We should not get here.");
1727 break;
1728 }
1729 //update the ui
1730 mUi.onContextMenuCreated(menu);
1731 }
1732
kaiyiz6e5b3e02013-08-19 20:02:01 +08001733 public void startAddMyNavigation(String url) {
1734 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1735 Bundle bundle = new Bundle();
1736 bundle.putBoolean("isAdding", true);
1737 bundle.putString("url", url);
1738 bundle.putString("name", getNameFromUrl(url));
1739 intent.putExtra("websites", bundle);
1740 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1741 }
1742
1743 private void showMyNavigationDeleteDialog(final String itemUrl) {
1744 new AlertDialog.Builder(this.getContext())
1745 .setTitle(R.string.my_navigation_delete_label)
1746 .setIcon(android.R.drawable.ic_dialog_alert)
1747 .setMessage(R.string.my_navigation_delete_msg)
1748 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1749 @Override
1750 public void onClick(DialogInterface dialog, int whichButton) {
1751 deleteMyNavigationItem(itemUrl);
1752 }
1753 })
1754 .setNegativeButton(R.string.cancel, null)
1755 .show();
1756 }
1757
1758 private void deleteMyNavigationItem(final String itemUrl) {
1759 ContentResolver cr = this.getContext().getContentResolver();
1760 Cursor cursor = null;
1761
1762 try {
1763 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1764 new String[] {
1765 MyNavigationUtil.ID
1766 }, "url = ?", new String[] {
1767 itemUrl
1768 }, null);
1769 if (null != cursor && cursor.moveToFirst()) {
1770 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1771 cursor.getLong(0));
1772
1773 ContentValues values = new ContentValues();
1774 values.put(MyNavigationUtil.TITLE, "");
1775 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1776 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1777 ByteArrayOutputStream os = new ByteArrayOutputStream();
1778 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1779 R.raw.my_navigation_add);
1780 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1781 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1782 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1783 cr.update(uri, values, null, null);
1784 } else {
1785 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1786 }
1787 } catch (IllegalStateException e) {
1788 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1789 } finally {
1790 if (null != cursor) {
1791 cursor.close();
1792 }
1793 }
1794
1795 if (getCurrentTopWebView() != null) {
1796 getCurrentTopWebView().reload();
1797 }
1798 }
1799
1800 private String getNameFromUrl(String itemUrl) {
1801 ContentResolver cr = this.getContext().getContentResolver();
1802 Cursor cursor = null;
1803 String name = null;
1804
1805 try {
1806 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1807 new String[] {
1808 MyNavigationUtil.TITLE
1809 }, "url = ?", new String[] {
1810 itemUrl
1811 }, null);
1812 if (null != cursor && cursor.moveToFirst()) {
1813 name = cursor.getString(0);
1814 } else {
1815 Log.e(LOGTAG, "this item does not exist!");
1816 }
1817 } catch (IllegalStateException e) {
1818 Log.e(LOGTAG, "getNameFromUrl", e);
1819 } finally {
1820 if (null != cursor) {
1821 cursor.close();
1822 }
1823 }
1824 return name;
1825 }
1826
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001827 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001828 final ContentResolver cr = mActivity.getContentResolver();
1829 new AsyncTask<Void, Void, Void>() {
1830 @Override
1831 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001832 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1833 if(!isMyNavigationUrl)
1834 return null;
1835
kaiyiz6e5b3e02013-08-19 20:02:01 +08001836 ContentResolver cr = mActivity.getContentResolver();
1837 Cursor cursor = null;
1838 try {
1839 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1840 new String[] {
1841 MyNavigationUtil.ID
1842 }, "url = ?", new String[] {
1843 itemUrl
1844 }, null);
1845 if (null != cursor && cursor.moveToFirst()) {
1846 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001847 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001848
1849 ContentValues values = new ContentValues();
1850 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1851 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1852 cursor.getLong(0));
1853 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1854 cr.update(uri, values, null, null);
1855 os.close();
1856 }
1857 } catch (IllegalStateException e) {
1858 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1859 } catch (IOException e) {
1860 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1861 } finally {
1862 if (null != cursor) {
1863 cursor.close();
1864 }
1865 }
1866 return null;
1867 }
1868 }.execute();
1869 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001870 /**
1871 * As the menu can be open when loading state changes
1872 * we must manually update the state of the stop/reload menu
1873 * item
1874 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001875 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001876 if (menu == null) {
1877 return;
1878 }
1879 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001880 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001881 menu.findItem(R.id.stop_menu_id):
1882 menu.findItem(R.id.reload_menu_id);
1883 if (src != null) {
1884 dest.setIcon(src.getIcon());
1885 dest.setTitle(src.getTitle());
1886 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001887 mActivity.invalidateOptionsMenu();
1888 }
1889
1890 public void invalidateOptionsMenu() {
1891 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001892 }
1893
John Reck9c35b9c2012-05-30 10:08:50 -07001894 @Override
1895 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001896 // Software menu key (toolbar key)
1897 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1898 return true;
1899 }
1900
1901 @Override
1902 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001903 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001904 // hold on to the menu reference here; it is used by the page callbacks
1905 // to update the menu based on loading state
1906 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001907 // Note: setVisible will decide whether an item is visible; while
1908 // setEnabled() will decide whether an item is enabled, which also means
1909 // whether the matching shortcut key will function.
1910 switch (mMenuState) {
1911 case EMPTY_MENU:
1912 if (mCurrentMenuState != mMenuState) {
1913 menu.setGroupVisible(R.id.MAIN_MENU, false);
1914 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1915 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1916 }
1917 break;
1918 default:
1919 if (mCurrentMenuState != mMenuState) {
1920 menu.setGroupVisible(R.id.MAIN_MENU, true);
1921 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1922 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1923 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001924 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001925 break;
1926 }
1927 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001928 mUi.onPrepareOptionsMenu(menu);
1929 }
1930
1931 private void setMenuItemVisibility(Menu menu, int id,
1932 boolean visibility) {
1933 MenuItem item = menu.findItem(id);
1934 if (item != null) {
1935 item.setVisible(visibility);
1936 }
1937 }
1938
1939 private int lookupBookmark(String title, String url) {
1940 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001941 int count = 0;
1942 Cursor cursor = null;
1943 try {
1944 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1945 BookmarksLoader.PROJECTION,
1946 "title = ? OR url = ?",
1947 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001948 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001949 },
1950 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001951
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001952 if (cursor != null)
1953 count = cursor.getCount();
1954
1955 } catch (IllegalStateException e) {
1956 Log.e(LOGTAG, "lookupBookmark ", e);
1957 } finally {
1958 if (null != cursor) {
1959 cursor.close();
1960 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001961 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001962 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001963 }
1964
1965 private void resetMenuItems(Menu menu) {
1966 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1967 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1968
1969 WebView w = getCurrentTopWebView();
1970 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1971
1972 String title = w.getTitle();
1973 String url = w.getUrl();
1974 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1975 bookmark_icon.setChecked(true);
1976 } else {
1977 bookmark_icon.setChecked(false);
1978 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001979 }
1980
Michael Kolb4bf79712011-07-14 14:18:12 -07001981 @Override
1982 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001983 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001984 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001985 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001986 // Following flag is used to identify schemes for which the LIVE_MENU
1987 // items defined in res/menu/browser.xml should be enabled
1988 boolean isLiveScheme = false;
1989 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001990 boolean isSavable = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001991 resetMenuItems(menu);
1992
Michael Kolb4bf79712011-07-14 14:18:12 -07001993 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001994 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001995 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001996 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001997 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001998 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001999 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07002000 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002001
2002 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2003 forward.setEnabled(canGoForward);
2004
Michael Kolb4bf79712011-07-14 14:18:12 -07002005 // decide whether to show the share link option
2006 PackageManager pm = mActivity.getPackageManager();
2007 Intent send = new Intent(Intent.ACTION_SEND);
2008 send.setType("text/plain");
2009 ResolveInfo ri = pm.resolveActivity(send,
2010 PackageManager.MATCH_DEFAULT_ONLY);
2011 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2012
2013 boolean isNavDump = mSettings.enableNavDump();
2014 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2015 nav.setVisible(isNavDump);
2016 nav.setEnabled(isNavDump);
2017
2018 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002019 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2020 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002021 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002022 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2023 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002024 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002025 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2026 isLive && isLiveScheme && isPageFinished);
2027 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002028 isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002029 // history and snapshots item are the members of COMBO menu group,
2030 // so if show history item, only make snapshots item invisible.
2031 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002032
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002033 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002034 }
2035
John Reck9c35b9c2012-05-30 10:08:50 -07002036 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002037 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002038 if (null == getCurrentTopWebView()) {
2039 return false;
2040 }
2041 if (mMenuIsDown) {
2042 // The shortcut action consumes the MENU. Even if it is still down,
2043 // it won't trigger the next shortcut action. In the case of the
2044 // shortcut action triggering a new activity, like Bookmarks, we
2045 // won't get onKeyUp for MENU. So it is important to reset it here.
2046 mMenuIsDown = false;
2047 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002048 if (mUi.onOptionsItemSelected(item)) {
2049 // ui callback handled it
2050 return true;
2051 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002052 switch (item.getItemId()) {
2053 // -- Main menu
2054 case R.id.new_tab_menu_id:
2055 openTabToHomePage();
2056 break;
2057
2058 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002059 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002060 break;
2061
2062 case R.id.goto_menu_id:
2063 editUrl();
2064 break;
2065
2066 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002067 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2068 break;
2069
2070 case R.id.history_menu_id:
2071 bookmarksOrHistoryPicker(ComboViews.History);
2072 break;
2073
2074 case R.id.snapshots_menu_id:
2075 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002076 break;
2077
Pankaj Garg49b79252014-11-07 17:33:41 -08002078 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002079 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 break;
2081
2082 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002083 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 stopLoading();
2085 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002086 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002087 getCurrentTopWebView().reload();
2088 }
2089 break;
2090
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002092 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002093 break;
2094
2095 case R.id.close_menu_id:
2096 // Close the subwindow if it exists.
2097 if (mTabControl.getCurrentSubWindow() != null) {
2098 dismissSubWindow(mTabControl.getCurrentTab());
2099 break;
2100 }
2101 closeCurrentTab();
2102 break;
2103
kaiyiza8b6dbb2013-07-29 18:11:22 +08002104 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002105 Object[] params = { new String("persist.debug.browsermonkeytest")};
2106 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002107 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002108 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002109 if (ret != null && ret.equals("enable"))
2110 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002111 if (BrowserConfig.getInstance(getContext())
2112 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2113 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002114 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002115 case R.id.homepage_menu_id:
2116 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002117 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002118 break;
2119
2120 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002121 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002122 break;
2123
2124 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002125 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 break;
2127
John Reck2bc80422011-06-30 15:11:49 -07002128 case R.id.save_snapshot_menu_id:
2129 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002130 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002131 createScreenshotAsync(
2132 source.getWebView(),
2133 getDesiredThumbnailWidth(mActivity),
2134 getDesiredThumbnailHeight(mActivity),
2135 new ValueCallback<Bitmap>() {
2136 @Override
2137 public void onReceiveValue(Bitmap bitmap) {
2138 new SaveSnapshotTask(source, bitmap).execute();
2139 }
2140 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002141 break;
2142
Michael Kolb8233fac2010-10-26 16:08:53 -07002143 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002144 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 break;
2146
John Recke1a03a32011-09-14 17:04:16 -07002147 case R.id.snapshot_go_live:
2148 goLive();
2149 return true;
2150
Michael Kolb8233fac2010-10-26 16:08:53 -07002151 case R.id.share_page_menu_id:
2152 Tab currentTab = mTabControl.getCurrentTab();
2153 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 return false;
2155 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002156 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002157 break;
2158
2159 case R.id.dump_nav_menu_id:
2160 getCurrentTopWebView().debugDump();
2161 break;
2162
Michael Kolb8233fac2010-10-26 16:08:53 -07002163 case R.id.zoom_in_menu_id:
2164 getCurrentTopWebView().zoomIn();
2165 break;
2166
2167 case R.id.zoom_out_menu_id:
2168 getCurrentTopWebView().zoomOut();
2169 break;
2170
2171 case R.id.view_downloads_menu_id:
2172 viewDownloads();
2173 break;
2174
John Reck42229bc2011-08-19 13:26:43 -07002175 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002176 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002177 break;
2178
Michael Kolb8233fac2010-10-26 16:08:53 -07002179 case R.id.window_one_menu_id:
2180 case R.id.window_two_menu_id:
2181 case R.id.window_three_menu_id:
2182 case R.id.window_four_menu_id:
2183 case R.id.window_five_menu_id:
2184 case R.id.window_six_menu_id:
2185 case R.id.window_seven_menu_id:
2186 case R.id.window_eight_menu_id:
2187 {
2188 int menuid = item.getItemId();
2189 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2190 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2191 Tab desiredTab = mTabControl.getTab(id);
2192 if (desiredTab != null &&
2193 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002194 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002195 }
2196 break;
2197 }
2198 }
2199 }
2200 break;
2201
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002202 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002203 Bundle bundle = new Bundle();
2204 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2205 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2206 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2207 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2208 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002209 break;
2210
Pankaj Garg49b79252014-11-07 17:33:41 -08002211 case R.id.add_to_homescreen:
2212 final WebView w = getCurrentTopWebView();
2213 final EditText input = new EditText(getContext());
2214 input.setText(w.getTitle());
2215 new AlertDialog.Builder(getContext())
2216 .setTitle("Add to homescreen")
2217 .setMessage("Title")
2218 .setView(input)
2219 .setPositiveButton("Add", new DialogInterface.OnClickListener() {
2220 public void onClick(DialogInterface dialog, int whichButton) {
2221 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2222 getContext(),
2223 w.getUrl(),
2224 input.getText().toString(),
2225 w.getViewportBitmap(),
2226 w.getFavicon()));
2227
2228 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2229 .addCategory(Intent.CATEGORY_HOME));
2230 }})
2231 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
2232 public void onClick(DialogInterface dialog, int whichButton) {
2233 // Do nothing.
2234 }
2235 })
2236 .show();
2237 break;
2238
Michael Kolb8233fac2010-10-26 16:08:53 -07002239 default:
2240 return false;
2241 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002242 return true;
2243 }
2244
John Reck68234a92012-04-19 15:27:12 -07002245 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2246 implements OnCancelListener {
2247
2248 private Tab mTab;
2249 private Dialog mProgressDialog;
2250 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002251 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002252
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002253 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002254 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002255 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002256 }
2257
2258 @Override
2259 protected void onPreExecute() {
2260 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2261 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2262 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002263 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002264 }
2265
2266 @Override
2267 protected Long doInBackground(Void... params) {
2268 if (!mTab.saveViewState(mValues)) {
2269 return null;
2270 }
2271 if (isCancelled()) {
2272 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2273 File file = mActivity.getFileStreamPath(path);
2274 if (!file.delete()) {
2275 file.deleteOnExit();
2276 }
2277 return null;
2278 }
2279 final ContentResolver cr = mActivity.getContentResolver();
2280 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2281 if (result == null) {
2282 return null;
2283 }
2284 long id = ContentUris.parseId(result);
2285 return id;
2286 }
2287
2288 @Override
2289 protected void onPostExecute(Long id) {
2290 if (isCancelled()) {
2291 return;
2292 }
2293 mProgressDialog.dismiss();
2294 if (id == null) {
2295 Toast.makeText(mActivity, R.string.snapshot_failed,
2296 Toast.LENGTH_SHORT).show();
2297 return;
2298 }
2299 Bundle b = new Bundle();
2300 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2301 mUi.showComboView(ComboViews.Snapshots, b);
2302 }
2303
2304 @Override
2305 public void onCancel(DialogInterface dialog) {
2306 cancel(true);
2307 }
2308 }
2309
Michael Kolb80f75082012-04-10 10:50:06 -07002310 @Override
2311 public void toggleUserAgent() {
2312 WebView web = getCurrentWebView();
2313 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002314 }
2315
2316 @Override
2317 public void findOnPage() {
2318 getCurrentTopWebView().showFindDialog(null, true);
2319 }
2320
2321 @Override
2322 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002323 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002324 }
2325
Pankaj Gargeba076f2015-03-25 13:40:59 -07002326 // This function is specifically used from AddBookmark Activity.
2327 // The bookmark activity clears the bitmap after retrieving it.
2328 // The function usage elsewhere will result in breaking bookmark
2329 // functionality.
2330 public static Bitmap getAndReleaseLastBookmarkBitmapFromIntent() {
2331 Bitmap bitmap = mBookmarkBitmap;
2332 mBookmarkBitmap = null;
2333 return bitmap;
2334 }
2335
Michael Kolb80f75082012-04-10 10:50:06 -07002336 @Override
2337 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002338 WebView w = getCurrentTopWebView();
2339 if (w == null)
2340 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002341 final Intent i = createBookmarkCurrentPageIntent(false);
2342 createScreenshotAsync(
2343 w, getDesiredThumbnailWidth(mActivity),
2344 getDesiredThumbnailHeight(mActivity),
2345 new ValueCallback<Bitmap>() {
2346 @Override
2347 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargeba076f2015-03-25 13:40:59 -07002348 mBookmarkBitmap = bitmap;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002349 mActivity.startActivity(i);
2350 }
2351 });
Michael Kolb80f75082012-04-10 10:50:06 -07002352 }
2353
John Recke1a03a32011-09-14 17:04:16 -07002354 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002355 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002356 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002357 boolean onlySingleTabRemaining = false;
2358 if (mTabControl.getTabCount() > 1) {
2359 // destroy the old snapshot tab
2360 closeCurrentTab();
2361 } else {
2362 onlySingleTabRemaining = true;
2363 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002364 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002365 if (onlySingleTabRemaining) {
2366 closeTab(t);
2367 }
2368
Tarun Nainani8eb00912014-07-17 12:28:32 -07002369 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002370 }
2371
luxiaolb40014b2013-07-19 10:01:43 +08002372 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002373 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002374 new AlertDialog.Builder(activity)
2375 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002376 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002377 .setMessage(R.string.exit_browser_msg)
2378 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2379 public void onClick(DialogInterface dialog, int which) {
2380 activity.moveTaskToBack(true);
2381 dialog.dismiss();
2382 }
2383 })
2384 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2385 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002386 mCrashRecoveryHandler.clearState(true);
2387 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002388 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002389 dialog.dismiss();
2390 }
2391 })
2392 .show();
2393 }
Michael Kolb315d5022011-10-13 12:47:11 -07002394 @Override
2395 public void showPageInfo() {
2396 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2397 }
2398
John Reck9c35b9c2012-05-30 10:08:50 -07002399 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002400 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002401 // Let the History and Bookmark fragments handle menus they created.
2402 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2403 return false;
2404 }
2405
Michael Kolb8233fac2010-10-26 16:08:53 -07002406 int id = item.getItemId();
2407 boolean result = true;
2408 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002409 // -- Browser context menu
2410 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002411 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002412 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002413 case R.id.copy_link_context_menu_id:
2414 final WebView webView = getCurrentTopWebView();
2415 if (null == webView) {
2416 result = false;
2417 break;
2418 }
2419 final HashMap<String, WebView> hrefMap =
2420 new HashMap<String, WebView>();
2421 hrefMap.put("webview", webView);
2422 final Message msg = mHandler.obtainMessage(
2423 FOCUS_NODE_HREF, id, 0, hrefMap);
2424 webView.requestFocusNodeHref(msg);
2425 break;
2426
2427 default:
2428 // For other context menus
2429 result = onOptionsItemSelected(item);
2430 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002431 return result;
2432 }
2433
2434 /**
2435 * support programmatically opening the context menu
2436 */
2437 public void openContextMenu(View view) {
2438 mActivity.openContextMenu(view);
2439 }
2440
2441 /**
2442 * programmatically open the options menu
2443 */
2444 public void openOptionsMenu() {
2445 mActivity.openOptionsMenu();
2446 }
2447
John Reck9c35b9c2012-05-30 10:08:50 -07002448 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002449 public boolean onMenuOpened(int featureId, Menu menu) {
2450 if (mOptionsMenuOpen) {
2451 if (mConfigChanged) {
2452 // We do not need to make any changes to the state of the
2453 // title bar, since the only thing that happened was a
2454 // change in orientation
2455 mConfigChanged = false;
2456 } else {
2457 if (!mExtendedMenuOpen) {
2458 mExtendedMenuOpen = true;
2459 mUi.onExtendedMenuOpened();
2460 } else {
2461 // Switching the menu back to icon view, so show the
2462 // title bar once again.
2463 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002464 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002465 }
2466 }
2467 } else {
2468 // The options menu is closed, so open it, and show the title
2469 mOptionsMenuOpen = true;
2470 mConfigChanged = false;
2471 mExtendedMenuOpen = false;
2472 mUi.onOptionsMenuOpened();
2473 }
2474 return true;
2475 }
2476
John Reck9c35b9c2012-05-30 10:08:50 -07002477 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002478 public void onOptionsMenuClosed(Menu menu) {
2479 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002480 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002481 }
2482
John Reck9c35b9c2012-05-30 10:08:50 -07002483 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002484 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002485 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002486 }
2487
2488 // Helper method for getting the top window.
2489 @Override
2490 public WebView getCurrentTopWebView() {
2491 return mTabControl.getCurrentTopWebView();
2492 }
2493
2494 @Override
2495 public WebView getCurrentWebView() {
2496 return mTabControl.getCurrentWebView();
2497 }
2498
2499 /*
2500 * This method is called as a result of the user selecting the options
2501 * menu to see the download window. It shows the download window on top of
2502 * the current window.
2503 */
2504 void viewDownloads() {
2505 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2506 mActivity.startActivity(intent);
2507 }
2508
John Reck30b065e2011-07-19 10:58:05 -07002509 int getActionModeHeight() {
2510 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2511 new int[] { android.R.attr.actionBarSize });
2512 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2513 actionBarSizeTypedArray.recycle();
2514 return size;
2515 }
2516
Michael Kolb8233fac2010-10-26 16:08:53 -07002517 // action mode
2518
John Reck9c35b9c2012-05-30 10:08:50 -07002519 @Override
2520 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002521 mUi.onActionModeStarted(mode);
2522 mActionMode = mode;
2523 }
2524
2525 /*
2526 * True if a custom ActionMode (i.e. find or select) is in use.
2527 */
2528 @Override
2529 public boolean isInCustomActionMode() {
2530 return mActionMode != null;
2531 }
2532
2533 /*
2534 * End the current ActionMode.
2535 */
2536 @Override
2537 public void endActionMode() {
2538 if (mActionMode != null) {
2539 mActionMode.finish();
2540 }
2541 }
2542
2543 /*
2544 * Called by find and select when they are finished. Replace title bars
2545 * as necessary.
2546 */
John Reck9c35b9c2012-05-30 10:08:50 -07002547 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002548 public void onActionModeFinished(ActionMode mode) {
2549 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002550 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002551 mActionMode = null;
2552 }
2553
2554 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002555 final Tab tab = getCurrentTab();
2556 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002557 }
2558
2559 // bookmark handling
2560
2561 /**
2562 * add the current page as a bookmark to the given folder id
2563 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002564 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002565 * bookmarked, and if it is, edit that bookmark. If false, and
2566 * the site is already bookmarked, do not attempt to edit the
2567 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002568 */
2569 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002570 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002571 WebView w = getCurrentTopWebView();
2572 if (w == null) {
2573 return null;
2574 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002575 Intent i = new Intent(mActivity,
2576 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002577 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2578 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2579 String touchIconUrl = w.getTouchIconUrl();
2580 if (touchIconUrl != null) {
2581 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2582 WebSettings settings = w.getSettings();
2583 if (settings != null) {
2584 i.putExtra(AddBookmarkPage.USER_AGENT,
2585 settings.getUserAgentString());
2586 }
2587 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002588 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002589 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002590 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002591 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2592 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002593 // Put the dialog at the upper right of the screen, covering the
2594 // star on the title bar.
2595 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002596 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002597 }
2598
2599 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002600 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002601 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002603 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002604 }
2605
Vivek Sekharb54614f2014-05-01 19:03:37 -07002606 @Override
2607 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2608 boolean capture) {
2609 mUploadHandler = new UploadHandler(this);
2610 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2611 }
2612
Michael Kolb8233fac2010-10-26 16:08:53 -07002613 // thumbnails
2614
2615 /**
2616 * Return the desired width for thumbnail screenshots, which are stored in
2617 * the database, and used on the bookmarks screen.
2618 * @param context Context for finding out the density of the screen.
2619 * @return desired width for thumbnail screenshot.
2620 */
2621 static int getDesiredThumbnailWidth(Context context) {
2622 return context.getResources().getDimensionPixelOffset(
2623 R.dimen.bookmarkThumbnailWidth);
2624 }
2625
2626 /**
2627 * Return the desired height for thumbnail screenshots, which are stored in
2628 * the database, and used on the bookmarks screen.
2629 * @param context Context for finding out the density of the screen.
2630 * @return desired height for thumbnail screenshot.
2631 */
2632 static int getDesiredThumbnailHeight(Context context) {
2633 return context.getResources().getDimensionPixelOffset(
2634 R.dimen.bookmarkThumbnailHeight);
2635 }
2636
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002637 static void createScreenshotAsync(WebView view, int width, int height,
2638 final ValueCallback<Bitmap> cb) {
2639 if (view == null || width == 0 || height == 0) {
2640 return;
2641 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002642 view.getContentBitmapAsync(
2643 (float) width / view.getWidth(),
2644 new Rect(),
2645 new ValueCallback<Bitmap>() {
2646 @Override
2647 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002648 if (bitmap != null)
2649 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2650 cb.onReceiveValue(bitmap);
2651 }});
2652 }
2653
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002654 private void updateScreenshot(final Tab tab) {
2655 createScreenshotAsync(
2656 tab.getWebView(),
2657 getDesiredThumbnailWidth(mActivity),
2658 getDesiredThumbnailHeight(mActivity),
2659 new ValueCallback<Bitmap>() {
2660 @Override
2661 public void onReceiveValue(Bitmap bitmap) {
2662 updateScreenshot(tab, bitmap);
2663 }
2664 });
2665 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002666
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002667 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002668 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002669 // FIXME: Would like to make sure there is actually something to
2670 // draw, but the API for that (WebViewCore.pictureReady()) is not
2671 // currently accessible here.
2672
John Reck34ef2672011-02-10 11:30:55 -08002673 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002674 if (view == null) {
2675 // Tab was destroyed
2676 return;
2677 }
John Reck34ef2672011-02-10 11:30:55 -08002678 final String url = tab.getUrl();
2679 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002680 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002681 if (TextUtils.isEmpty(url)) {
2682 return;
2683 }
2684
kaiyiz6e5b3e02013-08-19 20:02:01 +08002685 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002686 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002687 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002688 }
John Reck34ef2672011-02-10 11:30:55 -08002689 // Only update thumbnails for web urls (http(s)://), not for
2690 // about:, javascript:, data:, etc...
2691 // Unless it is a bookmarked site, then always update
2692 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2693 return;
2694 }
2695
kaiyiz6e5b3e02013-08-19 20:02:01 +08002696 if (url != null && mUpdateMyNavThumbnailUrl != null
2697 && Patterns.WEB_URL.matcher(url).matches()
2698 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2699 String urlHost = (new WebAddress(url)).getHost();
2700 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2701 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2702 || bookmarkHost.length() == 0) {
2703 return;
2704 }
2705 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2706 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2707 bookmarkHost.length());
2708 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2709 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2710 if (!bookmarkDomain.equals(urlDomain)) {
2711 return;
2712 }
2713 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002714 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002715 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2716 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2717 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2718 500);
2719 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002720 return;
2721 }
2722
2723 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002724 new AsyncTask<Void, Void, Void>() {
2725 @Override
2726 protected Void doInBackground(Void... unused) {
2727 Cursor cursor = null;
2728 try {
2729 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002730 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2731 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2732 : url);
2733 if (mUpdateMyNavThumbnail) {
2734 mUpdateMyNavThumbnail = false;
2735 mUpdateMyNavThumbnailUrl = null;
2736 }
John Reck34ef2672011-02-10 11:30:55 -08002737 if (cursor != null && cursor.moveToFirst()) {
2738 final ByteArrayOutputStream os =
2739 new ByteArrayOutputStream();
2740 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002741
John Reck34ef2672011-02-10 11:30:55 -08002742 ContentValues values = new ContentValues();
2743 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002744
John Reck34ef2672011-02-10 11:30:55 -08002745 do {
John Reck617fd832011-02-16 14:35:59 -08002746 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002747 cr.update(Images.CONTENT_URI, values, null, null);
2748 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002749 }
John Reck34ef2672011-02-10 11:30:55 -08002750 } catch (IllegalStateException e) {
2751 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002752 } catch (SQLiteException s) {
2753 // Added for possible error when user tries to remove the same bookmark
2754 // that is being updated with a screen shot
2755 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002756 } finally {
2757 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002758 }
John Reck34ef2672011-02-10 11:30:55 -08002759 return null;
2760 }
2761 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002762 }
2763
2764 private class Copy implements OnMenuItemClickListener {
2765 private CharSequence mText;
2766
John Reck9c35b9c2012-05-30 10:08:50 -07002767 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002768 public boolean onMenuItemClick(MenuItem item) {
2769 copy(mText);
2770 return true;
2771 }
2772
2773 public Copy(CharSequence toCopy) {
2774 mText = toCopy;
2775 }
2776 }
2777
Leon Scroggins63c02662010-11-18 15:16:27 -05002778 private static class Download implements OnMenuItemClickListener {
2779 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002780 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002781 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002782 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002783 private static final String FALLBACK_EXTENSION = "dat";
2784 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002785
John Reck9c35b9c2012-05-30 10:08:50 -07002786 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002787 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002788 if (DataUri.isDataUri(mText)) {
2789 saveDataUri();
2790 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002791 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002792 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002793 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002794 return true;
2795 }
2796
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002797 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2798 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002799 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002800 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002801 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002802 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002803 }
George Mount387d45d2011-10-07 15:57:53 -07002804
2805 /**
2806 * Treats mText as a data URI and writes its contents to a file
2807 * based on the current time.
2808 */
2809 private void saveDataUri() {
2810 FileOutputStream outputStream = null;
2811 try {
2812 DataUri uri = new DataUri(mText);
2813 File target = getTarget(uri);
2814 outputStream = new FileOutputStream(target);
2815 outputStream.write(uri.getData());
2816 final DownloadManager manager =
2817 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2818 manager.addCompletedDownload(target.getName(),
2819 mActivity.getTitle().toString(), false,
2820 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002821 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002822 } catch (IOException e) {
2823 Log.e(LOGTAG, "Could not save data URL");
2824 } finally {
2825 if (outputStream != null) {
2826 try {
2827 outputStream.close();
2828 } catch (IOException e) {
2829 // ignore close errors
2830 }
2831 }
2832 }
2833 }
2834
2835 /**
2836 * Creates a File based on the current time stamp and uses
2837 * the mime type of the DataUri to get the extension.
2838 */
2839 private File getTarget(DataUri uri) throws IOException {
2840 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002841 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002842 String nameBase = format.format(new Date());
2843 String mimeType = uri.getMimeType();
2844 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2845 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2846 if (extension == null) {
2847 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2848 extension = FALLBACK_EXTENSION;
2849 }
2850 extension = "." + extension; // createTempFile needs the '.'
2851 File targetFile = File.createTempFile(nameBase, extension, dir);
2852 return targetFile;
2853 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002854 }
2855
Cary Clark8974d282010-11-22 10:46:05 -05002856 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002857 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002858
John Reck9c35b9c2012-05-30 10:08:50 -07002859 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002860 public boolean onMenuItemClick(MenuItem item) {
2861 if (mWebView != null) {
2862 return mWebView.selectText();
2863 }
2864 return false;
2865 }
2866
2867 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002868 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002869 }
2870
2871 }
2872
Michael Kolb8233fac2010-10-26 16:08:53 -07002873 /********************** TODO: UI stuff *****************************/
2874
2875 // these methods have been copied, they still need to be cleaned up
2876
2877 /****************** tabs ***************************************************/
2878
2879 // basic tab interactions:
2880
2881 // it is assumed that tabcontrol already knows about the tab
2882 protected void addTab(Tab tab) {
2883 mUi.addTab(tab);
2884 }
2885
2886 protected void removeTab(Tab tab) {
2887 mUi.removeTab(tab);
2888 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002889 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002890 }
2891
Michael Kolbf2055602011-04-09 17:20:03 -07002892 @Override
2893 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002894 // monkey protection against delayed start
2895 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002896
2897 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2898 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002899 mTabControl.setCurrentTab(tab);
2900 // the tab is guaranteed to have a webview after setCurrentTab
2901 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002902
2903
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002904 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002905 //Purge active tabs
2906 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002907 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002908 }
2909
John Reck8bcafc12011-08-29 16:43:02 -07002910 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002911 Tab current = mTabControl.getCurrentTab();
2912 if (current != null
2913 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002914 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002915 }
2916 }
2917
John Reck26b18322011-06-21 13:08:58 -07002918 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002919 //Cancel navscreen request
2920 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002921 // Dismiss the subwindow if applicable.
2922 dismissSubWindow(appTab);
2923 // Since we might kill the WebView, remove it from the
2924 // content view first.
2925 mUi.detachTab(appTab);
2926 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002927 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002928 // TODO: analyze why the remove and add are necessary
2929 mUi.attachTab(appTab);
2930 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002931 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002932 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002933 } else {
2934 // If the tab was the current tab, we have to attach
2935 // it to the view system again.
2936 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002937 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002938 }
2939 }
2940
2941 // Remove the sub window if it exists. Also called by TabControl when the
2942 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002943 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002944 public void dismissSubWindow(Tab tab) {
2945 removeSubWindow(tab);
2946 // dismiss the subwindow. This will destroy the WebView.
2947 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002948 WebView wv = getCurrentTopWebView();
2949 if (wv != null) {
2950 wv.requestFocus();
2951 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002952 }
2953
2954 @Override
2955 public void removeSubWindow(Tab t) {
2956 if (t.getSubWebView() != null) {
2957 mUi.removeSubWindow(t.getSubViewContainer());
2958 }
2959 }
2960
2961 @Override
2962 public void attachSubWindow(Tab tab) {
2963 if (tab.getSubWebView() != null) {
2964 mUi.attachSubWindow(tab.getSubViewContainer());
2965 getCurrentTopWebView().requestFocus();
2966 }
2967 }
2968
Mathew Inwood29721c22011-06-29 17:55:24 +01002969 private Tab showPreloadedTab(final UrlData urlData) {
2970 if (!urlData.isPreloaded()) {
2971 return null;
2972 }
2973 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2974 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2975 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002976 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002977 // Could not submit query. Fallback to regular tab creation
2978 tabControl.destroy();
2979 return null;
2980 }
2981 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002982 // check tab count and make room for new tab
2983 if (!mTabControl.canCreateNewTab()) {
2984 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2985 if (leastUsed != null) {
2986 closeTab(leastUsed);
2987 }
2988 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002989 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002990 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002991 mTabControl.addPreloadedTab(t);
2992 addTab(t);
2993 setActiveTab(t);
2994 return t;
2995 }
2996
Michael Kolb7bcafde2011-05-09 13:55:59 -07002997 // open a non inconito tab with the given url data
2998 // and set as active tab
2999 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01003000 Tab tab = showPreloadedTab(urlData);
3001 if (tab == null) {
3002 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07003003 if ((tab != null) && !urlData.isEmpty()) {
3004 loadUrlDataIn(tab, urlData);
3005 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003006 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003007 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07003008 }
3009
Michael Kolb843510f2010-12-09 10:51:49 -08003010 @Override
3011 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003012 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07003013 }
3014
Michael Kolb8233fac2010-10-26 16:08:53 -07003015 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07003016 public Tab openIncognitoTab() {
3017 return openTab(INCOGNITO_URI, true, true, false);
3018 }
3019
3020 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003021 public Tab openTab(String url, boolean incognito, boolean setActive,
3022 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003023 return openTab(url, incognito, setActive, useCurrent, null);
3024 }
3025
3026 @Override
3027 public Tab openTab(String url, Tab parent, boolean setActive,
3028 boolean useCurrent) {
3029 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3030 setActive, useCurrent, parent);
3031 }
3032
3033 public Tab openTab(String url, boolean incognito, boolean setActive,
3034 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003035 Tab tab = createNewTab(incognito, setActive, useCurrent);
3036 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003037 if (parent instanceof SnapshotTab) {
3038 addTab(tab);
3039 if (setActive)
3040 setActiveTab(tab);
3041 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003042 parent.addChildTab(tab);
3043 }
Michael Kolb519d2282011-05-09 17:03:19 -07003044 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003045 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003046 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003047 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003048 return tab;
3049 }
3050
3051 // this method will attempt to create a new tab
3052 // incognito: private browsing tab
3053 // setActive: ste tab as current tab
3054 // useCurrent: if no new tab can be created, return current tab
3055 private Tab createNewTab(boolean incognito, boolean setActive,
3056 boolean useCurrent) {
3057 Tab tab = null;
3058 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05003059 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003060 addTab(tab);
3061 if (setActive) {
3062 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07003063 } else {
3064 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003065 }
3066 } else {
3067 if (useCurrent) {
3068 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07003069 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003070 } else {
3071 mUi.showMaxTabsWarning();
3072 }
3073 }
3074 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 }
3076
John Reck2bc80422011-06-30 15:11:49 -07003077 @Override
3078 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3079 SnapshotTab tab = null;
3080 if (mTabControl.canCreateNewTab()) {
3081 tab = mTabControl.createSnapshotTab(snapshotId);
3082 addTab(tab);
3083 if (setActive) {
3084 setActiveTab(tab);
3085 }
3086 } else {
3087 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003088 }
3089 return tab;
3090 }
3091
Michael Kolb8233fac2010-10-26 16:08:53 -07003092 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003093 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003094 * @return boolean True if we successfully switched to a different tab. If
3095 * the indexth tab is null, or if that tab is the same as
3096 * the current one, return false.
3097 */
3098 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003099 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003100 Tab currentTab = mTabControl.getCurrentTab();
3101 if (tab == null || tab == currentTab) {
3102 return false;
3103 }
3104 setActiveTab(tab);
3105 return true;
3106 }
3107
3108 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003109 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003110 closeCurrentTab(false);
3111 }
3112
3113 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003114 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003115 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003116 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003117 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003118 return;
3119 }
Michael Kolbc831b632011-05-11 09:30:34 -07003120 final Tab current = mTabControl.getCurrentTab();
3121 final int pos = mTabControl.getCurrentPosition();
3122 Tab newTab = current.getParent();
3123 if (newTab == null) {
3124 newTab = mTabControl.getTab(pos + 1);
3125 if (newTab == null) {
3126 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003127 }
3128 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003129 if (andQuit) {
3130 mTabControl.setCurrentTab(newTab);
3131 closeTab(current);
3132 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003133 // Close window
3134 closeTab(current);
3135 }
3136 }
3137
3138 /**
3139 * Close the tab, remove its associated title bar, and adjust mTabControl's
3140 * current tab to a valid value.
3141 */
3142 @Override
3143 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003144 if (tab == mTabControl.getCurrentTab()) {
3145 closeCurrentTab();
3146 } else {
3147 removeTab(tab);
3148 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003149 }
3150
Afzal Najamd4e33312012-04-26 01:54:01 -04003151 /**
3152 * Close all tabs except the current one
3153 */
3154 @Override
3155 public void closeOtherTabs() {
3156 int inactiveTabs = mTabControl.getTabCount() - 1;
3157 for (int i = inactiveTabs; i >= 0; i--) {
3158 Tab tab = mTabControl.getTab(i);
3159 if (tab != mTabControl.getCurrentTab()) {
3160 removeTab(tab);
3161 }
3162 }
3163 }
3164
Michael Kolb8233fac2010-10-26 16:08:53 -07003165 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003166 protected void loadUrlFromContext(String url) {
3167 Tab tab = getCurrentTab();
3168 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003169 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003170 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003171 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003172 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003173 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003174 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003175 }
3176 }
3177 }
3178
3179 /**
3180 * Load the URL into the given WebView and update the title bar
3181 * to reflect the new load. Call this instead of WebView.loadUrl
3182 * directly.
3183 * @param view The WebView used to load url.
3184 * @param url The URL to load.
3185 */
John Reck71e51422011-07-01 16:49:28 -07003186 @Override
3187 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003188 loadUrl(tab, url, null);
3189 }
3190
3191 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3192 if (tab != null) {
3193 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003194 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003195 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003196 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003197 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003198 }
3199
3200 /**
3201 * Load UrlData into a Tab and update the title bar to reflect the new
3202 * load. Call this instead of UrlData.loadIn directly.
3203 * @param t The Tab used to load.
3204 * @param data The UrlData being loaded.
3205 */
3206 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003207 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003208 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003209 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003210 } else {
John Reck38b39652012-06-05 09:22:59 -07003211 if (t != null && data.mDisableUrlOverride) {
3212 t.disableUrlOverridingForLoad();
3213 }
John Reck26b18322011-06-21 13:08:58 -07003214 loadUrl(t, data.mUrl, data.mHeaders);
3215 }
3216 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003217 }
3218
John Reck30c714c2010-12-16 17:30:34 -08003219 @Override
3220 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003221 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003222 //In case of tab can go back (aka tab has navigation entry) do nothing
3223 //else just load homepage in current tab.
3224 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003225 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003226 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003227 }
3228
3229 void goBackOnePageOrQuit() {
3230 Tab current = mTabControl.getCurrentTab();
3231 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003232 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3233 showExitDialog(mActivity);
3234 } else {
3235 /*
3236 * Instead of finishing the activity, simply push this to the back
3237 * of the stack and let ActivityManager to choose the foreground
3238 * activity. As BrowserActivity is singleTask, it will be always the
3239 * root of the task. So we can use either true or false for
3240 * moveTaskToBack().
3241 */
3242 mActivity.moveTaskToBack(true);
3243 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003244 return;
3245 }
John Reckef654f12011-07-12 16:42:08 -07003246 if (current.canGoBack()) {
3247 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003248 } else {
3249 // Check to see if we are closing a window that was created by
3250 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003251 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003252 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003253 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003254 // Now we close the other tab
3255 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003256 } else if (BrowserConfig.getInstance(getContext())
3257 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3258 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003259 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003260 if ((current.getAppId() != null) || current.closeOnBack()) {
3261 closeCurrentTab(true);
3262 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003263 /*
3264 * Instead of finishing the activity, simply push this to the back
3265 * of the stack and let ActivityManager to choose the foreground
3266 * activity. As BrowserActivity is singleTask, it will be always the
3267 * root of the task. So we can use either true or false for
3268 * moveTaskToBack().
3269 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003270 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003271 }
3272 }
3273 }
3274
3275 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003276 * helper method for key handler
3277 * returns the current tab if it can't advance
3278 */
Michael Kolbc831b632011-05-11 09:30:34 -07003279 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003280 int pos = mTabControl.getCurrentPosition() + 1;
3281 if (pos >= mTabControl.getTabCount()) {
3282 pos = 0;
3283 }
3284 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003285 }
3286
3287 /**
3288 * helper method for key handler
3289 * returns the current tab if it can't advance
3290 */
Michael Kolbc831b632011-05-11 09:30:34 -07003291 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003292 int pos = mTabControl.getCurrentPosition() - 1;
3293 if ( pos < 0) {
3294 pos = mTabControl.getTabCount() - 1;
3295 }
3296 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003297 }
3298
John Reckbcef87f2012-02-03 14:58:34 -08003299 boolean isMenuOrCtrlKey(int keyCode) {
3300 return (KeyEvent.KEYCODE_MENU == keyCode)
3301 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3302 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3303 }
3304
Michael Kolb0035fad2011-03-14 13:25:28 -07003305 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003306 * handle key events in browser
3307 *
3308 * @param keyCode
3309 * @param event
3310 * @return true if handled, false to pass to super
3311 */
John Reck9c35b9c2012-05-30 10:08:50 -07003312 @Override
3313 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003314 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3315 // Hardware menu key
Enrico Ros1f5a0952014-11-18 20:15:48 -08003316 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
Pankaj Garg49b79252014-11-07 17:33:41 -08003317 true, false);
3318 return true;
3319 }
3320
Cary Clark160bbb92011-01-10 11:17:07 -05003321 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003322 // Even if MENU is already held down, we need to call to super to open
3323 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003324 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003325 mMenuIsDown = true;
3326 return false;
3327 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003328
Cary Clark8ff8c662010-12-29 15:03:05 -05003329 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003330 Tab tab = getCurrentTab();
3331 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003332
Cary Clark160bbb92011-01-10 11:17:07 -05003333 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3334 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003335
Michael Kolb8233fac2010-10-26 16:08:53 -07003336 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003337 case KeyEvent.KEYCODE_TAB:
3338 if (event.isCtrlPressed()) {
3339 if (event.isShiftPressed()) {
3340 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003341 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003342 } else {
3343 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003344 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003345 }
3346 return true;
3347 }
3348 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003349 case KeyEvent.KEYCODE_SPACE:
3350 // WebView/WebTextView handle the keys in the KeyDown. As
3351 // the Activity's shortcut keys are only handled when WebView
3352 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003353 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003354 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003355 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003356 pageDown();
3357 }
3358 return true;
3359 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003360 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003361 event.startTracking();
3362 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003363 case KeyEvent.KEYCODE_FORWARD:
3364 if (!noModifiers) break;
3365 tab.goForward();
3366 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003367 case KeyEvent.KEYCODE_DPAD_LEFT:
3368 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003369 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003370 return true;
3371 }
3372 break;
3373 case KeyEvent.KEYCODE_DPAD_RIGHT:
3374 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003375 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003376 return true;
3377 }
3378 break;
3379 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003380 if (ctrl) {
3381 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003382 return true;
3383 }
3384 break;
Michael Kolba4183062011-01-16 10:43:21 -08003385// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003386 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003387 if (ctrl ) {
3388 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003389 return true;
3390 }
3391 break;
Michael Kolba4183062011-01-16 10:43:21 -08003392// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003393// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003394// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003395// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003396// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003397// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003398// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003399// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003400// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003401// case KeyEvent.KEYCODE_M: // unused
3402// case KeyEvent.KEYCODE_N: // in Chrome: new window
3403// case KeyEvent.KEYCODE_O: // in Chrome: open file
3404// case KeyEvent.KEYCODE_P: // in Chrome: print page
3405// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003406// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003407// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3408 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003409 // we can't use the ctrl/shift flags, they check for
3410 // exclusive use of a modifier
3411 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003412 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003413 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003414 } else {
3415 openTabToHomePage();
3416 }
3417 return true;
3418 }
3419 break;
3420// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3421// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003422// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003423// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3424// case KeyEvent.KEYCODE_Y: // unused
3425// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003426 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003427 // it is a regular key and webview is not null
3428 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003429 }
3430
John Reck9c35b9c2012-05-30 10:08:50 -07003431 @Override
3432 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003433 switch(keyCode) {
3434 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003435 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003436 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003437 return true;
3438 }
3439 break;
3440 }
3441 return false;
3442 }
3443
John Reck9c35b9c2012-05-30 10:08:50 -07003444 @Override
3445 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003446 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003447 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003448 if (KeyEvent.KEYCODE_MENU == keyCode
3449 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003450 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003451 }
3452 }
Cary Clark160bbb92011-01-10 11:17:07 -05003453 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003454 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003455 case KeyEvent.KEYCODE_BACK:
3456 if (event.isTracking() && !event.isCanceled()) {
3457 onBackKey();
3458 return true;
3459 }
3460 break;
3461 }
3462 return false;
3463 }
3464
3465 public boolean isMenuDown() {
3466 return mMenuIsDown;
3467 }
3468
John Reck9c35b9c2012-05-30 10:08:50 -07003469 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003470 public void setupAutoFill(Message message) {
3471 // Open the settings activity at the AutoFill profile fragment so that
3472 // the user can create a new profile. When they return, we will dispatch
3473 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003474 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003475 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3476 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003477 }
John Reckb3417f02011-01-14 11:01:05 -08003478
John Reck9c35b9c2012-05-30 10:08:50 -07003479 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003480 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003481 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003482 return true;
3483 }
3484
John Reck1cf4b792011-07-26 10:22:22 -07003485 @Override
3486 public boolean shouldCaptureThumbnails() {
3487 return mUi.shouldCaptureThumbnails();
3488 }
3489
Michael Kolbc3af0672011-08-09 10:24:41 -07003490 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003491 public boolean supportsVoice() {
3492 PackageManager pm = mActivity.getPackageManager();
3493 List activities = pm.queryIntentActivities(new Intent(
3494 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3495 return activities.size() != 0;
3496 }
3497
3498 @Override
3499 public void startVoiceRecognizer() {
3500 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003501 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003502 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3503 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3504 mActivity.startActivityForResult(voice, VOICE_RESULT);
3505 }
3506
Pankaj Garg7b279f62014-08-12 14:47:18 -07003507 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003508 if (mLevel == level)
3509 return;
3510 mLevel = level;
3511 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003512 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3513 lp.dimAmount = level;
3514 mActivity.getWindow().setAttributes(lp);
3515 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3516 } else {
3517 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3518 }
3519 }
3520
Michael Kolb0b129122012-06-04 16:31:58 -07003521 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003522 public void setBlockEvents(boolean block) {
3523 mBlockEvents = block;
3524 }
3525
John Reck9c35b9c2012-05-30 10:08:50 -07003526 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003527 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003528 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003529 }
3530
John Reck9c35b9c2012-05-30 10:08:50 -07003531 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003532 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003533 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003534 }
3535
John Reck9c35b9c2012-05-30 10:08:50 -07003536 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003537 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003538 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003539 }
3540
John Reck9c35b9c2012-05-30 10:08:50 -07003541 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003542 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003543 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003544 }
3545
John Reck9c35b9c2012-05-30 10:08:50 -07003546 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003547 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003548 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003549 }
3550
Pankaj Garg49b79252014-11-07 17:33:41 -08003551 @Override
3552 public boolean shouldShowAppMenu() {
3553 return true;
3554 }
3555
3556 @Override
3557 public int getMenuThemeResourceId() {
3558 return R.style.OverflowMenuTheme;
3559 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003560}