blob: 1138700ee3f0aa9e25f8682cef1c4dc794469fd7 [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) {
2373 new AlertDialog.Builder(activity)
2374 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002375 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002376 .setMessage(R.string.exit_browser_msg)
2377 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2378 public void onClick(DialogInterface dialog, int which) {
2379 activity.moveTaskToBack(true);
2380 dialog.dismiss();
2381 }
2382 })
2383 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2384 public void onClick(DialogInterface dialog, int which) {
2385 activity.finish();
2386 mHandler.postDelayed(new Runnable() {
2387 @Override
2388 public void run() {
luxiaolb40014b2013-07-19 10:01:43 +08002389 mCrashRecoveryHandler.clearState(true);
2390 int pid = android.os.Process.myPid();
2391 android.os.Process.killProcess(pid);
2392 }
2393 }, 300);
2394 dialog.dismiss();
2395 }
2396 })
2397 .show();
2398 }
Michael Kolb315d5022011-10-13 12:47:11 -07002399 @Override
2400 public void showPageInfo() {
2401 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2402 }
2403
John Reck9c35b9c2012-05-30 10:08:50 -07002404 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002405 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002406 // Let the History and Bookmark fragments handle menus they created.
2407 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2408 return false;
2409 }
2410
Michael Kolb8233fac2010-10-26 16:08:53 -07002411 int id = item.getItemId();
2412 boolean result = true;
2413 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002414 // -- Browser context menu
2415 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002416 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002417 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002418 case R.id.copy_link_context_menu_id:
2419 final WebView webView = getCurrentTopWebView();
2420 if (null == webView) {
2421 result = false;
2422 break;
2423 }
2424 final HashMap<String, WebView> hrefMap =
2425 new HashMap<String, WebView>();
2426 hrefMap.put("webview", webView);
2427 final Message msg = mHandler.obtainMessage(
2428 FOCUS_NODE_HREF, id, 0, hrefMap);
2429 webView.requestFocusNodeHref(msg);
2430 break;
2431
2432 default:
2433 // For other context menus
2434 result = onOptionsItemSelected(item);
2435 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002436 return result;
2437 }
2438
2439 /**
2440 * support programmatically opening the context menu
2441 */
2442 public void openContextMenu(View view) {
2443 mActivity.openContextMenu(view);
2444 }
2445
2446 /**
2447 * programmatically open the options menu
2448 */
2449 public void openOptionsMenu() {
2450 mActivity.openOptionsMenu();
2451 }
2452
John Reck9c35b9c2012-05-30 10:08:50 -07002453 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002454 public boolean onMenuOpened(int featureId, Menu menu) {
2455 if (mOptionsMenuOpen) {
2456 if (mConfigChanged) {
2457 // We do not need to make any changes to the state of the
2458 // title bar, since the only thing that happened was a
2459 // change in orientation
2460 mConfigChanged = false;
2461 } else {
2462 if (!mExtendedMenuOpen) {
2463 mExtendedMenuOpen = true;
2464 mUi.onExtendedMenuOpened();
2465 } else {
2466 // Switching the menu back to icon view, so show the
2467 // title bar once again.
2468 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002469 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002470 }
2471 }
2472 } else {
2473 // The options menu is closed, so open it, and show the title
2474 mOptionsMenuOpen = true;
2475 mConfigChanged = false;
2476 mExtendedMenuOpen = false;
2477 mUi.onOptionsMenuOpened();
2478 }
2479 return true;
2480 }
2481
John Reck9c35b9c2012-05-30 10:08:50 -07002482 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002483 public void onOptionsMenuClosed(Menu menu) {
2484 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002485 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002486 }
2487
John Reck9c35b9c2012-05-30 10:08:50 -07002488 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002489 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002490 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002491 }
2492
2493 // Helper method for getting the top window.
2494 @Override
2495 public WebView getCurrentTopWebView() {
2496 return mTabControl.getCurrentTopWebView();
2497 }
2498
2499 @Override
2500 public WebView getCurrentWebView() {
2501 return mTabControl.getCurrentWebView();
2502 }
2503
2504 /*
2505 * This method is called as a result of the user selecting the options
2506 * menu to see the download window. It shows the download window on top of
2507 * the current window.
2508 */
2509 void viewDownloads() {
2510 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2511 mActivity.startActivity(intent);
2512 }
2513
John Reck30b065e2011-07-19 10:58:05 -07002514 int getActionModeHeight() {
2515 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2516 new int[] { android.R.attr.actionBarSize });
2517 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2518 actionBarSizeTypedArray.recycle();
2519 return size;
2520 }
2521
Michael Kolb8233fac2010-10-26 16:08:53 -07002522 // action mode
2523
John Reck9c35b9c2012-05-30 10:08:50 -07002524 @Override
2525 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002526 mUi.onActionModeStarted(mode);
2527 mActionMode = mode;
2528 }
2529
2530 /*
2531 * True if a custom ActionMode (i.e. find or select) is in use.
2532 */
2533 @Override
2534 public boolean isInCustomActionMode() {
2535 return mActionMode != null;
2536 }
2537
2538 /*
2539 * End the current ActionMode.
2540 */
2541 @Override
2542 public void endActionMode() {
2543 if (mActionMode != null) {
2544 mActionMode.finish();
2545 }
2546 }
2547
2548 /*
2549 * Called by find and select when they are finished. Replace title bars
2550 * as necessary.
2551 */
John Reck9c35b9c2012-05-30 10:08:50 -07002552 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002553 public void onActionModeFinished(ActionMode mode) {
2554 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002555 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002556 mActionMode = null;
2557 }
2558
2559 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002560 final Tab tab = getCurrentTab();
2561 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002562 }
2563
2564 // bookmark handling
2565
2566 /**
2567 * add the current page as a bookmark to the given folder id
2568 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002569 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002570 * bookmarked, and if it is, edit that bookmark. If false, and
2571 * the site is already bookmarked, do not attempt to edit the
2572 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002573 */
2574 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002575 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002576 WebView w = getCurrentTopWebView();
2577 if (w == null) {
2578 return null;
2579 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 Intent i = new Intent(mActivity,
2581 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002582 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2583 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2584 String touchIconUrl = w.getTouchIconUrl();
2585 if (touchIconUrl != null) {
2586 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2587 WebSettings settings = w.getSettings();
2588 if (settings != null) {
2589 i.putExtra(AddBookmarkPage.USER_AGENT,
2590 settings.getUserAgentString());
2591 }
2592 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002593 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002594 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002595 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002596 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2597 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002598 // Put the dialog at the upper right of the screen, covering the
2599 // star on the title bar.
2600 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002601 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 }
2603
2604 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002605 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002606 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002607 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002608 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002609 }
2610
Vivek Sekharb54614f2014-05-01 19:03:37 -07002611 @Override
2612 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2613 boolean capture) {
2614 mUploadHandler = new UploadHandler(this);
2615 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2616 }
2617
Michael Kolb8233fac2010-10-26 16:08:53 -07002618 // thumbnails
2619
2620 /**
2621 * Return the desired width for thumbnail screenshots, which are stored in
2622 * the database, and used on the bookmarks screen.
2623 * @param context Context for finding out the density of the screen.
2624 * @return desired width for thumbnail screenshot.
2625 */
2626 static int getDesiredThumbnailWidth(Context context) {
2627 return context.getResources().getDimensionPixelOffset(
2628 R.dimen.bookmarkThumbnailWidth);
2629 }
2630
2631 /**
2632 * Return the desired height for thumbnail screenshots, which are stored in
2633 * the database, and used on the bookmarks screen.
2634 * @param context Context for finding out the density of the screen.
2635 * @return desired height for thumbnail screenshot.
2636 */
2637 static int getDesiredThumbnailHeight(Context context) {
2638 return context.getResources().getDimensionPixelOffset(
2639 R.dimen.bookmarkThumbnailHeight);
2640 }
2641
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002642 static void createScreenshotAsync(WebView view, int width, int height,
2643 final ValueCallback<Bitmap> cb) {
2644 if (view == null || width == 0 || height == 0) {
2645 return;
2646 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002647 view.getContentBitmapAsync(
2648 (float) width / view.getWidth(),
2649 new Rect(),
2650 new ValueCallback<Bitmap>() {
2651 @Override
2652 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002653 if (bitmap != null)
2654 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2655 cb.onReceiveValue(bitmap);
2656 }});
2657 }
2658
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002659 private void updateScreenshot(final Tab tab) {
2660 createScreenshotAsync(
2661 tab.getWebView(),
2662 getDesiredThumbnailWidth(mActivity),
2663 getDesiredThumbnailHeight(mActivity),
2664 new ValueCallback<Bitmap>() {
2665 @Override
2666 public void onReceiveValue(Bitmap bitmap) {
2667 updateScreenshot(tab, bitmap);
2668 }
2669 });
2670 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002671
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002672 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002673 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002674 // FIXME: Would like to make sure there is actually something to
2675 // draw, but the API for that (WebViewCore.pictureReady()) is not
2676 // currently accessible here.
2677
John Reck34ef2672011-02-10 11:30:55 -08002678 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002679 if (view == null) {
2680 // Tab was destroyed
2681 return;
2682 }
John Reck34ef2672011-02-10 11:30:55 -08002683 final String url = tab.getUrl();
2684 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002685 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002686 if (TextUtils.isEmpty(url)) {
2687 return;
2688 }
2689
kaiyiz6e5b3e02013-08-19 20:02:01 +08002690 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002691 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002692 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002693 }
John Reck34ef2672011-02-10 11:30:55 -08002694 // Only update thumbnails for web urls (http(s)://), not for
2695 // about:, javascript:, data:, etc...
2696 // Unless it is a bookmarked site, then always update
2697 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2698 return;
2699 }
2700
kaiyiz6e5b3e02013-08-19 20:02:01 +08002701 if (url != null && mUpdateMyNavThumbnailUrl != null
2702 && Patterns.WEB_URL.matcher(url).matches()
2703 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2704 String urlHost = (new WebAddress(url)).getHost();
2705 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2706 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2707 || bookmarkHost.length() == 0) {
2708 return;
2709 }
2710 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2711 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2712 bookmarkHost.length());
2713 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2714 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2715 if (!bookmarkDomain.equals(urlDomain)) {
2716 return;
2717 }
2718 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002719 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002720 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2721 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2722 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2723 500);
2724 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002725 return;
2726 }
2727
2728 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002729 new AsyncTask<Void, Void, Void>() {
2730 @Override
2731 protected Void doInBackground(Void... unused) {
2732 Cursor cursor = null;
2733 try {
2734 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002735 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2736 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2737 : url);
2738 if (mUpdateMyNavThumbnail) {
2739 mUpdateMyNavThumbnail = false;
2740 mUpdateMyNavThumbnailUrl = null;
2741 }
John Reck34ef2672011-02-10 11:30:55 -08002742 if (cursor != null && cursor.moveToFirst()) {
2743 final ByteArrayOutputStream os =
2744 new ByteArrayOutputStream();
2745 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002746
John Reck34ef2672011-02-10 11:30:55 -08002747 ContentValues values = new ContentValues();
2748 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002749
John Reck34ef2672011-02-10 11:30:55 -08002750 do {
John Reck617fd832011-02-16 14:35:59 -08002751 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002752 cr.update(Images.CONTENT_URI, values, null, null);
2753 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002754 }
John Reck34ef2672011-02-10 11:30:55 -08002755 } catch (IllegalStateException e) {
2756 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002757 } catch (SQLiteException s) {
2758 // Added for possible error when user tries to remove the same bookmark
2759 // that is being updated with a screen shot
2760 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002761 } finally {
2762 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002763 }
John Reck34ef2672011-02-10 11:30:55 -08002764 return null;
2765 }
2766 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002767 }
2768
2769 private class Copy implements OnMenuItemClickListener {
2770 private CharSequence mText;
2771
John Reck9c35b9c2012-05-30 10:08:50 -07002772 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002773 public boolean onMenuItemClick(MenuItem item) {
2774 copy(mText);
2775 return true;
2776 }
2777
2778 public Copy(CharSequence toCopy) {
2779 mText = toCopy;
2780 }
2781 }
2782
Leon Scroggins63c02662010-11-18 15:16:27 -05002783 private static class Download implements OnMenuItemClickListener {
2784 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002785 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002786 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002787 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002788 private static final String FALLBACK_EXTENSION = "dat";
2789 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002790
John Reck9c35b9c2012-05-30 10:08:50 -07002791 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002792 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002793 if (DataUri.isDataUri(mText)) {
2794 saveDataUri();
2795 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002796 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002797 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002798 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002799 return true;
2800 }
2801
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002802 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2803 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002804 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002805 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002806 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002807 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002808 }
George Mount387d45d2011-10-07 15:57:53 -07002809
2810 /**
2811 * Treats mText as a data URI and writes its contents to a file
2812 * based on the current time.
2813 */
2814 private void saveDataUri() {
2815 FileOutputStream outputStream = null;
2816 try {
2817 DataUri uri = new DataUri(mText);
2818 File target = getTarget(uri);
2819 outputStream = new FileOutputStream(target);
2820 outputStream.write(uri.getData());
2821 final DownloadManager manager =
2822 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2823 manager.addCompletedDownload(target.getName(),
2824 mActivity.getTitle().toString(), false,
2825 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002826 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002827 } catch (IOException e) {
2828 Log.e(LOGTAG, "Could not save data URL");
2829 } finally {
2830 if (outputStream != null) {
2831 try {
2832 outputStream.close();
2833 } catch (IOException e) {
2834 // ignore close errors
2835 }
2836 }
2837 }
2838 }
2839
2840 /**
2841 * Creates a File based on the current time stamp and uses
2842 * the mime type of the DataUri to get the extension.
2843 */
2844 private File getTarget(DataUri uri) throws IOException {
2845 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002846 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002847 String nameBase = format.format(new Date());
2848 String mimeType = uri.getMimeType();
2849 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2850 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2851 if (extension == null) {
2852 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2853 extension = FALLBACK_EXTENSION;
2854 }
2855 extension = "." + extension; // createTempFile needs the '.'
2856 File targetFile = File.createTempFile(nameBase, extension, dir);
2857 return targetFile;
2858 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002859 }
2860
Cary Clark8974d282010-11-22 10:46:05 -05002861 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002862 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002863
John Reck9c35b9c2012-05-30 10:08:50 -07002864 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002865 public boolean onMenuItemClick(MenuItem item) {
2866 if (mWebView != null) {
2867 return mWebView.selectText();
2868 }
2869 return false;
2870 }
2871
2872 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002873 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002874 }
2875
2876 }
2877
Michael Kolb8233fac2010-10-26 16:08:53 -07002878 /********************** TODO: UI stuff *****************************/
2879
2880 // these methods have been copied, they still need to be cleaned up
2881
2882 /****************** tabs ***************************************************/
2883
2884 // basic tab interactions:
2885
2886 // it is assumed that tabcontrol already knows about the tab
2887 protected void addTab(Tab tab) {
2888 mUi.addTab(tab);
2889 }
2890
2891 protected void removeTab(Tab tab) {
2892 mUi.removeTab(tab);
2893 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002894 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002895 }
2896
Michael Kolbf2055602011-04-09 17:20:03 -07002897 @Override
2898 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002899 // monkey protection against delayed start
2900 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002901
2902 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2903 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002904 mTabControl.setCurrentTab(tab);
2905 // the tab is guaranteed to have a webview after setCurrentTab
2906 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002907
2908
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002909 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002910 //Purge active tabs
2911 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002912 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002913 }
2914
John Reck8bcafc12011-08-29 16:43:02 -07002915 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002916 Tab current = mTabControl.getCurrentTab();
2917 if (current != null
2918 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002919 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002920 }
2921 }
2922
John Reck26b18322011-06-21 13:08:58 -07002923 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002924 //Cancel navscreen request
2925 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002926 // Dismiss the subwindow if applicable.
2927 dismissSubWindow(appTab);
2928 // Since we might kill the WebView, remove it from the
2929 // content view first.
2930 mUi.detachTab(appTab);
2931 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002932 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002933 // TODO: analyze why the remove and add are necessary
2934 mUi.attachTab(appTab);
2935 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002936 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002937 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002938 } else {
2939 // If the tab was the current tab, we have to attach
2940 // it to the view system again.
2941 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002942 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002943 }
2944 }
2945
2946 // Remove the sub window if it exists. Also called by TabControl when the
2947 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002948 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002949 public void dismissSubWindow(Tab tab) {
2950 removeSubWindow(tab);
2951 // dismiss the subwindow. This will destroy the WebView.
2952 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002953 WebView wv = getCurrentTopWebView();
2954 if (wv != null) {
2955 wv.requestFocus();
2956 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002957 }
2958
2959 @Override
2960 public void removeSubWindow(Tab t) {
2961 if (t.getSubWebView() != null) {
2962 mUi.removeSubWindow(t.getSubViewContainer());
2963 }
2964 }
2965
2966 @Override
2967 public void attachSubWindow(Tab tab) {
2968 if (tab.getSubWebView() != null) {
2969 mUi.attachSubWindow(tab.getSubViewContainer());
2970 getCurrentTopWebView().requestFocus();
2971 }
2972 }
2973
Mathew Inwood29721c22011-06-29 17:55:24 +01002974 private Tab showPreloadedTab(final UrlData urlData) {
2975 if (!urlData.isPreloaded()) {
2976 return null;
2977 }
2978 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2979 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2980 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002981 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002982 // Could not submit query. Fallback to regular tab creation
2983 tabControl.destroy();
2984 return null;
2985 }
2986 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002987 // check tab count and make room for new tab
2988 if (!mTabControl.canCreateNewTab()) {
2989 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2990 if (leastUsed != null) {
2991 closeTab(leastUsed);
2992 }
2993 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002994 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002995 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002996 mTabControl.addPreloadedTab(t);
2997 addTab(t);
2998 setActiveTab(t);
2999 return t;
3000 }
3001
Michael Kolb7bcafde2011-05-09 13:55:59 -07003002 // open a non inconito tab with the given url data
3003 // and set as active tab
3004 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01003005 Tab tab = showPreloadedTab(urlData);
3006 if (tab == null) {
3007 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07003008 if ((tab != null) && !urlData.isEmpty()) {
3009 loadUrlDataIn(tab, urlData);
3010 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003011 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003012 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07003013 }
3014
Michael Kolb843510f2010-12-09 10:51:49 -08003015 @Override
3016 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003017 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07003018 }
3019
Michael Kolb8233fac2010-10-26 16:08:53 -07003020 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07003021 public Tab openIncognitoTab() {
3022 return openTab(INCOGNITO_URI, true, true, false);
3023 }
3024
3025 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003026 public Tab openTab(String url, boolean incognito, boolean setActive,
3027 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003028 return openTab(url, incognito, setActive, useCurrent, null);
3029 }
3030
3031 @Override
3032 public Tab openTab(String url, Tab parent, boolean setActive,
3033 boolean useCurrent) {
3034 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3035 setActive, useCurrent, parent);
3036 }
3037
3038 public Tab openTab(String url, boolean incognito, boolean setActive,
3039 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003040 Tab tab = createNewTab(incognito, setActive, useCurrent);
3041 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003042 if (parent instanceof SnapshotTab) {
3043 addTab(tab);
3044 if (setActive)
3045 setActiveTab(tab);
3046 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003047 parent.addChildTab(tab);
3048 }
Michael Kolb519d2282011-05-09 17:03:19 -07003049 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003050 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003051 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003052 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003053 return tab;
3054 }
3055
3056 // this method will attempt to create a new tab
3057 // incognito: private browsing tab
3058 // setActive: ste tab as current tab
3059 // useCurrent: if no new tab can be created, return current tab
3060 private Tab createNewTab(boolean incognito, boolean setActive,
3061 boolean useCurrent) {
3062 Tab tab = null;
3063 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05003064 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003065 addTab(tab);
3066 if (setActive) {
3067 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07003068 } else {
3069 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003070 }
3071 } else {
3072 if (useCurrent) {
3073 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07003074 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003075 } else {
3076 mUi.showMaxTabsWarning();
3077 }
3078 }
3079 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003080 }
3081
John Reck2bc80422011-06-30 15:11:49 -07003082 @Override
3083 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3084 SnapshotTab tab = null;
3085 if (mTabControl.canCreateNewTab()) {
3086 tab = mTabControl.createSnapshotTab(snapshotId);
3087 addTab(tab);
3088 if (setActive) {
3089 setActiveTab(tab);
3090 }
3091 } else {
3092 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003093 }
3094 return tab;
3095 }
3096
Michael Kolb8233fac2010-10-26 16:08:53 -07003097 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003098 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003099 * @return boolean True if we successfully switched to a different tab. If
3100 * the indexth tab is null, or if that tab is the same as
3101 * the current one, return false.
3102 */
3103 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003104 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003105 Tab currentTab = mTabControl.getCurrentTab();
3106 if (tab == null || tab == currentTab) {
3107 return false;
3108 }
3109 setActiveTab(tab);
3110 return true;
3111 }
3112
3113 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003114 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003115 closeCurrentTab(false);
3116 }
3117
3118 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003119 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003120 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003121 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003122 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003123 return;
3124 }
Michael Kolbc831b632011-05-11 09:30:34 -07003125 final Tab current = mTabControl.getCurrentTab();
3126 final int pos = mTabControl.getCurrentPosition();
3127 Tab newTab = current.getParent();
3128 if (newTab == null) {
3129 newTab = mTabControl.getTab(pos + 1);
3130 if (newTab == null) {
3131 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003132 }
3133 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003134 if (andQuit) {
3135 mTabControl.setCurrentTab(newTab);
3136 closeTab(current);
3137 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003138 // Close window
3139 closeTab(current);
3140 }
3141 }
3142
3143 /**
3144 * Close the tab, remove its associated title bar, and adjust mTabControl's
3145 * current tab to a valid value.
3146 */
3147 @Override
3148 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003149 if (tab == mTabControl.getCurrentTab()) {
3150 closeCurrentTab();
3151 } else {
3152 removeTab(tab);
3153 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003154 }
3155
Afzal Najamd4e33312012-04-26 01:54:01 -04003156 /**
3157 * Close all tabs except the current one
3158 */
3159 @Override
3160 public void closeOtherTabs() {
3161 int inactiveTabs = mTabControl.getTabCount() - 1;
3162 for (int i = inactiveTabs; i >= 0; i--) {
3163 Tab tab = mTabControl.getTab(i);
3164 if (tab != mTabControl.getCurrentTab()) {
3165 removeTab(tab);
3166 }
3167 }
3168 }
3169
Michael Kolb8233fac2010-10-26 16:08:53 -07003170 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003171 protected void loadUrlFromContext(String url) {
3172 Tab tab = getCurrentTab();
3173 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003174 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003175 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003176 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003177 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003178 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003179 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003180 }
3181 }
3182 }
3183
3184 /**
3185 * Load the URL into the given WebView and update the title bar
3186 * to reflect the new load. Call this instead of WebView.loadUrl
3187 * directly.
3188 * @param view The WebView used to load url.
3189 * @param url The URL to load.
3190 */
John Reck71e51422011-07-01 16:49:28 -07003191 @Override
3192 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003193 loadUrl(tab, url, null);
3194 }
3195
3196 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3197 if (tab != null) {
3198 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003199 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003200 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003201 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003202 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003203 }
3204
3205 /**
3206 * Load UrlData into a Tab and update the title bar to reflect the new
3207 * load. Call this instead of UrlData.loadIn directly.
3208 * @param t The Tab used to load.
3209 * @param data The UrlData being loaded.
3210 */
3211 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003212 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003213 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003214 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003215 } else {
John Reck38b39652012-06-05 09:22:59 -07003216 if (t != null && data.mDisableUrlOverride) {
3217 t.disableUrlOverridingForLoad();
3218 }
John Reck26b18322011-06-21 13:08:58 -07003219 loadUrl(t, data.mUrl, data.mHeaders);
3220 }
3221 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003222 }
3223
John Reck30c714c2010-12-16 17:30:34 -08003224 @Override
3225 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003226 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003227 //In case of tab can go back (aka tab has navigation entry) do nothing
3228 //else just load homepage in current tab.
3229 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003230 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003231 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003232 }
3233
3234 void goBackOnePageOrQuit() {
3235 Tab current = mTabControl.getCurrentTab();
3236 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003237 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3238 showExitDialog(mActivity);
3239 } else {
3240 /*
3241 * Instead of finishing the activity, simply push this to the back
3242 * of the stack and let ActivityManager to choose the foreground
3243 * activity. As BrowserActivity is singleTask, it will be always the
3244 * root of the task. So we can use either true or false for
3245 * moveTaskToBack().
3246 */
3247 mActivity.moveTaskToBack(true);
3248 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003249 return;
3250 }
John Reckef654f12011-07-12 16:42:08 -07003251 if (current.canGoBack()) {
3252 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003253 } else {
3254 // Check to see if we are closing a window that was created by
3255 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003256 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003257 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003258 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003259 // Now we close the other tab
3260 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003261 } else if (BrowserConfig.getInstance(getContext())
3262 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3263 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003264 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003265 if ((current.getAppId() != null) || current.closeOnBack()) {
3266 closeCurrentTab(true);
3267 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003268 /*
3269 * Instead of finishing the activity, simply push this to the back
3270 * of the stack and let ActivityManager to choose the foreground
3271 * activity. As BrowserActivity is singleTask, it will be always the
3272 * root of the task. So we can use either true or false for
3273 * moveTaskToBack().
3274 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003275 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003276 }
3277 }
3278 }
3279
3280 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003281 * helper method for key handler
3282 * returns the current tab if it can't advance
3283 */
Michael Kolbc831b632011-05-11 09:30:34 -07003284 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003285 int pos = mTabControl.getCurrentPosition() + 1;
3286 if (pos >= mTabControl.getTabCount()) {
3287 pos = 0;
3288 }
3289 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003290 }
3291
3292 /**
3293 * helper method for key handler
3294 * returns the current tab if it can't advance
3295 */
Michael Kolbc831b632011-05-11 09:30:34 -07003296 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003297 int pos = mTabControl.getCurrentPosition() - 1;
3298 if ( pos < 0) {
3299 pos = mTabControl.getTabCount() - 1;
3300 }
3301 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003302 }
3303
John Reckbcef87f2012-02-03 14:58:34 -08003304 boolean isMenuOrCtrlKey(int keyCode) {
3305 return (KeyEvent.KEYCODE_MENU == keyCode)
3306 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3307 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3308 }
3309
Michael Kolb0035fad2011-03-14 13:25:28 -07003310 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003311 * handle key events in browser
3312 *
3313 * @param keyCode
3314 * @param event
3315 * @return true if handled, false to pass to super
3316 */
John Reck9c35b9c2012-05-30 10:08:50 -07003317 @Override
3318 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003319 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3320 // Hardware menu key
Enrico Ros1f5a0952014-11-18 20:15:48 -08003321 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
Pankaj Garg49b79252014-11-07 17:33:41 -08003322 true, false);
3323 return true;
3324 }
3325
Cary Clark160bbb92011-01-10 11:17:07 -05003326 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003327 // Even if MENU is already held down, we need to call to super to open
3328 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003329 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003330 mMenuIsDown = true;
3331 return false;
3332 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003333
Cary Clark8ff8c662010-12-29 15:03:05 -05003334 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003335 Tab tab = getCurrentTab();
3336 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003337
Cary Clark160bbb92011-01-10 11:17:07 -05003338 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3339 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003340
Michael Kolb8233fac2010-10-26 16:08:53 -07003341 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003342 case KeyEvent.KEYCODE_TAB:
3343 if (event.isCtrlPressed()) {
3344 if (event.isShiftPressed()) {
3345 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003346 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003347 } else {
3348 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003349 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003350 }
3351 return true;
3352 }
3353 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003354 case KeyEvent.KEYCODE_SPACE:
3355 // WebView/WebTextView handle the keys in the KeyDown. As
3356 // the Activity's shortcut keys are only handled when WebView
3357 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003358 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003359 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003360 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003361 pageDown();
3362 }
3363 return true;
3364 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003365 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003366 event.startTracking();
3367 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003368 case KeyEvent.KEYCODE_FORWARD:
3369 if (!noModifiers) break;
3370 tab.goForward();
3371 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003372 case KeyEvent.KEYCODE_DPAD_LEFT:
3373 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003374 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003375 return true;
3376 }
3377 break;
3378 case KeyEvent.KEYCODE_DPAD_RIGHT:
3379 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003380 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003381 return true;
3382 }
3383 break;
3384 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003385 if (ctrl) {
3386 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003387 return true;
3388 }
3389 break;
Michael Kolba4183062011-01-16 10:43:21 -08003390// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003391 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003392 if (ctrl ) {
3393 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003394 return true;
3395 }
3396 break;
Michael Kolba4183062011-01-16 10:43:21 -08003397// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003398// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003399// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003400// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003401// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003402// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003403// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003404// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003405// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003406// case KeyEvent.KEYCODE_M: // unused
3407// case KeyEvent.KEYCODE_N: // in Chrome: new window
3408// case KeyEvent.KEYCODE_O: // in Chrome: open file
3409// case KeyEvent.KEYCODE_P: // in Chrome: print page
3410// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003411// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003412// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3413 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003414 // we can't use the ctrl/shift flags, they check for
3415 // exclusive use of a modifier
3416 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003417 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003418 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003419 } else {
3420 openTabToHomePage();
3421 }
3422 return true;
3423 }
3424 break;
3425// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3426// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003427// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003428// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3429// case KeyEvent.KEYCODE_Y: // unused
3430// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003431 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003432 // it is a regular key and webview is not null
3433 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003434 }
3435
John Reck9c35b9c2012-05-30 10:08:50 -07003436 @Override
3437 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003438 switch(keyCode) {
3439 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003440 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003441 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003442 return true;
3443 }
3444 break;
3445 }
3446 return false;
3447 }
3448
John Reck9c35b9c2012-05-30 10:08:50 -07003449 @Override
3450 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003451 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003452 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003453 if (KeyEvent.KEYCODE_MENU == keyCode
3454 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003455 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003456 }
3457 }
Cary Clark160bbb92011-01-10 11:17:07 -05003458 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003459 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003460 case KeyEvent.KEYCODE_BACK:
3461 if (event.isTracking() && !event.isCanceled()) {
3462 onBackKey();
3463 return true;
3464 }
3465 break;
3466 }
3467 return false;
3468 }
3469
3470 public boolean isMenuDown() {
3471 return mMenuIsDown;
3472 }
3473
John Reck9c35b9c2012-05-30 10:08:50 -07003474 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003475 public void setupAutoFill(Message message) {
3476 // Open the settings activity at the AutoFill profile fragment so that
3477 // the user can create a new profile. When they return, we will dispatch
3478 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003479 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003480 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3481 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003482 }
John Reckb3417f02011-01-14 11:01:05 -08003483
John Reck9c35b9c2012-05-30 10:08:50 -07003484 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003485 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003486 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003487 return true;
3488 }
3489
John Reck1cf4b792011-07-26 10:22:22 -07003490 @Override
3491 public boolean shouldCaptureThumbnails() {
3492 return mUi.shouldCaptureThumbnails();
3493 }
3494
Michael Kolbc3af0672011-08-09 10:24:41 -07003495 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003496 public boolean supportsVoice() {
3497 PackageManager pm = mActivity.getPackageManager();
3498 List activities = pm.queryIntentActivities(new Intent(
3499 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3500 return activities.size() != 0;
3501 }
3502
3503 @Override
3504 public void startVoiceRecognizer() {
3505 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003506 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003507 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3508 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3509 mActivity.startActivityForResult(voice, VOICE_RESULT);
3510 }
3511
Pankaj Garg7b279f62014-08-12 14:47:18 -07003512 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003513 if (mLevel == level)
3514 return;
3515 mLevel = level;
3516 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003517 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3518 lp.dimAmount = level;
3519 mActivity.getWindow().setAttributes(lp);
3520 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3521 } else {
3522 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3523 }
3524 }
3525
Michael Kolb0b129122012-06-04 16:31:58 -07003526 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003527 public void setBlockEvents(boolean block) {
3528 mBlockEvents = block;
3529 }
3530
John Reck9c35b9c2012-05-30 10:08:50 -07003531 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003532 public boolean dispatchKeyEvent(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 dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003538 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003539 }
3540
John Reck9c35b9c2012-05-30 10:08:50 -07003541 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003542 public boolean dispatchTouchEvent(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 dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003548 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003549 }
3550
John Reck9c35b9c2012-05-30 10:08:50 -07003551 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003552 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003553 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003554 }
3555
Pankaj Garg49b79252014-11-07 17:33:41 -08003556 @Override
3557 public boolean shouldShowAppMenu() {
3558 return true;
3559 }
3560
3561 @Override
3562 public int getMenuThemeResourceId() {
3563 return R.style.OverflowMenuTheme;
3564 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003565}