blob: 353fea9055e7447b6d720c7d413c1642ddee955e [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080020
luxiaolb40014b2013-07-19 10:01:43 +080021import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.ProgressDialog;
Vivek Sekharce2a4832014-03-26 13:26:53 -070025import android.content.Context;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070028import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.content.ContentValues;
John Reck68234a92012-04-19 15:27:12 -070030import android.content.DialogInterface;
31import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070032import android.content.Intent;
33import android.content.pm.PackageManager;
34import android.content.pm.ResolveInfo;
35import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070036import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050037import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.database.Cursor;
39import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020040import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080042import android.graphics.BitmapFactory;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080043import android.graphics.Rect;
Tarun Nainanif5563b62015-01-21 18:52:59 -080044import android.media.AudioManager;
kaiyiza016da12013-08-26 17:50:22 +080045import android.net.ConnectivityManager;
46import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070047import android.net.Uri;
48import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080049import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080050import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.os.AsyncTask;
Tarun Nainanif5563b62015-01-21 18:52:59 -080052import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070054import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.os.Handler;
56import android.os.Message;
57import android.os.PowerManager;
58import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070059import android.provider.ContactsContract;
60import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080061import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070062import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070063import android.text.TextUtils;
64import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080065import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070066import android.view.ActionMode;
67import android.view.ContextMenu;
68import android.view.ContextMenu.ContextMenuInfo;
69import android.view.Gravity;
70import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070071import android.view.Menu;
72import android.view.MenuInflater;
73import android.view.MenuItem;
74import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070075import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070076import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070077import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070078import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080081import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import org.codeaurora.swe.CookieManager;
85import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080086import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.HttpAuthHandler;
88import org.codeaurora.swe.SslErrorHandler;
89import org.codeaurora.swe.WebSettings;
90import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070091import org.codeaurora.swe.WebBackForwardList;
92import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080093
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070094import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070095import com.android.browser.R;
96import com.android.browser.IntentHandler.UrlData;
97import com.android.browser.UI.ComboViews;
98import com.android.browser.mynavigation.AddMyNavigationPage;
99import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700100import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700101import com.android.browser.platformsupport.BrowserContract;
102import com.android.browser.platformsupport.WebAddress;
103import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800104import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700105import com.android.browser.provider.BrowserProvider2.Thumbnails;
106import com.android.browser.provider.SnapshotProvider.Snapshots;
107import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800108import com.android.browser.appmenu.AppMenuHandler;
109import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700110
Michael Kolb8233fac2010-10-26 16:08:53 -0700111import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700112import java.io.File;
113import java.io.FileOutputStream;
114import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700116import java.text.DateFormat;
117import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700118import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700119import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700120import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800121import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200122import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700123import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700124
125/**
126 * Controller for browser
127 */
128public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800129 implements WebViewController, UiController, ActivityController,
130 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700131
132 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800133 private static final String SEND_APP_ID_EXTRA =
134 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Vivek Sekhared791da2015-02-22 12:39:05 -0800135 private static final String INCOGNITO_URI = "chrome://incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800136
Vivek Sekharb54614f2014-05-01 19:03:37 -0700137 // Remind switch to data connection if wifi is unavailable
138 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800139
Michael Kolb8233fac2010-10-26 16:08:53 -0700140 // public message ids
141 public final static int LOAD_URL = 1001;
142 public final static int STOP_LOAD = 1002;
143
144 // Message Ids
145 private static final int FOCUS_NODE_HREF = 102;
146 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800147 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700148
149 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
150
151 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800152 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700153
154 private static final int EMPTY_MENU = -1;
155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700157 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 final static int PREFERENCES_PAGE = 3;
159 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700161 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800162 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000163
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
165
166 // As the ids are dynamically created, we can't guarantee that they will
167 // be in sequence, so this static array maps ids to a window number.
168 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
169 { R.id.window_one_menu_id, R.id.window_two_menu_id,
170 R.id.window_three_menu_id, R.id.window_four_menu_id,
171 R.id.window_five_menu_id, R.id.window_six_menu_id,
172 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
173
174 // "source" parameter for Google search through search key
175 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
176 // "source" parameter for Google search through simplily type
177 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
178
George Mount387d45d2011-10-07 15:57:53 -0700179 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700180 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
181
John Reckd7dd9b22011-08-30 09:18:29 -0700182 // A bitmap that is re-used in createScreenshot as scratch space
183 private static Bitmap sThumbnailBitmap;
184
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 private Activity mActivity;
186 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700187 private HomepageHandler mHomepageHandler;
188 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private BrowserSettings mSettings;
190 private WebViewFactory mFactory;
191
192 private WakeLock mWakeLock;
193
194 private UrlHandler mUrlHandler;
195 private UploadHandler mUploadHandler;
196 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 private PageDialogsHandler mPageDialogsHandler;
198 private NetworkStateHandler mNetworkHandler;
199
Ben Murdoch8029a772010-11-16 11:58:21 +0000200 private Message mAutoFillSetupMessage;
201
kaiyiza016da12013-08-26 17:50:22 +0800202 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700203
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 // FIXME, temp address onPrepareMenu performance problem.
205 // When we move everything out of view, we should rewrite this.
206 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700207 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 private int mOldMenuState = EMPTY_MENU;
209 private Menu mCachedMenu;
210
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 private boolean mMenuIsDown;
212
Pankaj Garg49b79252014-11-07 17:33:41 -0800213 private boolean mWasInPageLoad = false;
214 private AppMenuHandler mAppMenuHandler;
215
Michael Kolb8233fac2010-10-26 16:08:53 -0700216 // For select and find, we keep track of the ActionMode so that
217 // finish() can be called as desired.
218 private ActionMode mActionMode;
219
220 /**
221 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
222 * of whether the configuration has changed. The first onMenuOpened call
223 * after a configuration change is simply a reopening of the same menu
224 * (i.e. mIconView did not change).
225 */
226 private boolean mConfigChanged;
227
228 /**
229 * Keeps track of whether the options menu is open. This is important in
230 * determining whether to show or hide the title bar overlay
231 */
232 private boolean mOptionsMenuOpen;
233
234 /**
235 * Whether or not the options menu is in its bigger, popup menu form. When
236 * true, we want the title bar overlay to be gone. When false, we do not.
237 * Only meaningful if mOptionsMenuOpen is true.
238 */
239 private boolean mExtendedMenuOpen;
240
Michael Kolb8233fac2010-10-26 16:08:53 -0700241 private boolean mActivityPaused = true;
242 private boolean mLoadStopped;
243
244 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500245 // Checks to see when the bookmarks database has changed, and updates the
246 // Tabs' notion of whether they represent bookmarked sites.
247 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700248 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700249
Michael Kolbc3af0672011-08-09 10:24:41 -0700250 private boolean mBlockEvents;
251
Michael Kolb0b129122012-06-04 16:31:58 -0700252 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800253 private boolean mUpdateMyNavThumbnail;
254 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800255 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800256 private WebView.HitTestResult mResult;
Pankaj Gargeba076f2015-03-25 13:40:59 -0700257 private static Bitmap mBookmarkBitmap;
Michael Kolb0b129122012-06-04 16:31:58 -0700258
George Mount3636d0a2011-11-21 09:08:21 -0800259 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 mActivity = browser;
261 mSettings = BrowserSettings.getInstance();
262 mTabControl = new TabControl(this);
263 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700264 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800265 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700266 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700267
268 mUrlHandler = new UrlHandler(this);
269 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
271
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500273 mBookmarksObserver = new ContentObserver(mHandler) {
274 @Override
275 public void onChange(boolean selfChange) {
276 int size = mTabControl.getTabCount();
277 for (int i = 0; i < size; i++) {
278 mTabControl.getTab(i).updateBookmarkedStatus();
279 }
280 }
281
282 };
283 browser.getContentResolver().registerContentObserver(
284 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700285
286 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700287 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800288 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700289 }
290
John Reck9c35b9c2012-05-30 10:08:50 -0700291 @Override
292 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700293 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800294 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800295 // mCrashRecoverHandler has any previously saved state.
296 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700297 }
298
George Mount3636d0a2011-11-21 09:08:21 -0800299 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800300 // we dont want to ever recover incognito tabs
301 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700302
Patrick Scott7d50a932011-02-04 09:27:26 -0500303 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700304 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500305 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000306
Michael Kolbc831b632011-05-11 09:30:34 -0700307 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500308 // Not able to restore so we go ahead and clear session cookies. We
309 // must do this before trying to login the user as we don't want to
310 // clear any session cookies set during login.
311 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700312 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800313 if (intent == null) {
314 // This won't happen under common scenarios. The icicle is
315 // not null, but there aren't any tabs to restore.
316 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700317 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800318 final Bundle extra = intent.getExtras();
319 // Create an initial tab.
320 // If the intent is ACTION_VIEW and data is not null, the Browser is
321 // invoked to view the content by another application. In this case,
322 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800323 UrlData urlData = null;
324 if (intent.getData() != null
325 && Intent.ACTION_VIEW.equals(intent.getAction())
326 && intent.getData().toString().startsWith("content://")) {
327 urlData = new UrlData(intent.getData().toString());
328 } else {
329 urlData = IntentHandler.getUrlDataFromIntent(intent);
330 }
George Mount3636d0a2011-11-21 09:08:21 -0800331 Tab t = null;
332 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700333 String landingPage = mActivity.getResources().getString(
334 R.string.def_landing_page);
335 if (!landingPage.isEmpty()) {
336 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800337 } else {
338 t = openTabToHomePage();
339 }
George Mount3636d0a2011-11-21 09:08:21 -0800340 } else {
341 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700342 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800343 }
344 if (t != null) {
345 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
346 }
347 WebView webView = t.getWebView();
348 if (extra != null) {
349 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
350 if (scale > 0 && scale <= 1000) {
351 webView.setInitialScale(scale);
352 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700353 }
354 }
John Reckd8c74522011-06-14 08:45:00 -0700355 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700357 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500358 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700359 List<Tab> tabs = mTabControl.getTabs();
360 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700361
John Reck1cf4b792011-07-26 10:22:22 -0700362 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700363 //handle restored pages that may require a JS interface
364 if (t.getWebView() != null) {
365 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
366 if (backForwardList != null) {
367 for (int i = 0; i < backForwardList.getSize(); i++) {
368 WebHistoryItem item = backForwardList.getItemAtIndex(i);
369 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
370 }
371 }
372 }
John Reck1cf4b792011-07-26 10:22:22 -0700373 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700374 if (t != mTabControl.getCurrentTab()) {
375 t.pause();
376 }
John Reck1cf4b792011-07-26 10:22:22 -0700377 }
378 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700379 if (tabs.size() == 0) {
380 openTabToHomePage();
381 }
John Reck1cf4b792011-07-26 10:22:22 -0700382 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700383 // TabControl.restoreState() will create a new tab even if
384 // restoring the state fails.
385 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800386 // Intent is non-null when framework thinks the browser should be
387 // launching with a new intent (icicle is null).
388 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700389 mIntentHandler.onNewIntent(intent);
390 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700391 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700392 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700393 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800394 if (jsFlags.trim().length() != 0) {
395 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 }
George Mount3636d0a2011-11-21 09:08:21 -0800397 if (intent != null
398 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700399 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800400 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 }
402
John Reck1cf4b792011-07-26 10:22:22 -0700403 private static class PruneThumbnails implements Runnable {
404 private Context mContext;
405 private List<Long> mIds;
406
407 PruneThumbnails(Context context, List<Long> preserveIds) {
408 mContext = context.getApplicationContext();
409 mIds = preserveIds;
410 }
411
412 @Override
413 public void run() {
414 ContentResolver cr = mContext.getContentResolver();
415 if (mIds == null || mIds.size() == 0) {
416 cr.delete(Thumbnails.CONTENT_URI, null, null);
417 } else {
418 int length = mIds.size();
419 StringBuilder where = new StringBuilder();
420 where.append(Thumbnails._ID);
421 where.append(" not in (");
422 for (int i = 0; i < length; i++) {
423 where.append(mIds.get(i));
424 if (i < (length - 1)) {
425 where.append(",");
426 }
427 }
428 where.append(")");
429 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
430 }
431 }
432
433 }
434
Michael Kolb1514bb72010-11-22 09:11:48 -0800435 @Override
436 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700437 return mFactory;
438 }
439
440 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800441 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800442 if (tab.hasCrashed)
443 tab.showCrashView();
444 else
445 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800446 }
447
448 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800449 public void createSubWindow(Tab tab) {
450 endActionMode();
451 WebView mainView = tab.getWebView();
452 WebView subView = mFactory.createWebView((mainView == null)
453 ? false
454 : mainView.isPrivateBrowsingEnabled());
455 mUi.createSubWindow(tab, subView);
456 }
457
458 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700459 public Context getContext() {
460 return mActivity;
461 }
462
463 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700464 public Activity getActivity() {
465 return mActivity;
466 }
467
468 void setUi(UI ui) {
469 mUi = ui;
470 }
471
Michael Kolbaf63dba2012-05-16 12:58:05 -0700472 @Override
473 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700474 return mSettings;
475 }
476
477 IntentHandler getIntentHandler() {
478 return mIntentHandler;
479 }
480
481 @Override
482 public UI getUi() {
483 return mUi;
484 }
485
486 int getMaxTabs() {
487 return mActivity.getResources().getInteger(R.integer.max_tabs);
488 }
489
490 @Override
491 public TabControl getTabControl() {
492 return mTabControl;
493 }
494
Michael Kolb1bf23132010-11-19 12:55:12 -0800495 @Override
496 public List<Tab> getTabs() {
497 return mTabControl.getTabs();
498 }
499
Michael Kolb8233fac2010-10-26 16:08:53 -0700500 private void startHandler() {
501 mHandler = new Handler() {
502
503 @Override
504 public void handleMessage(Message msg) {
505 switch (msg.what) {
506 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700507 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800509 case UNKNOWN_TYPE_MSG:
510 HashMap unknownTypeMap = (HashMap) msg.obj;
511 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
512 /*
513 * When the context menu is shown to the user
514 * we need to assure that its happening on the current webview
515 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
516 */
517 if (getCurrentWebView() != viewForUnknownType)
518 break;
519
520 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800521 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800522 WebView.HitTestResult result = new WebView.HitTestResult();
523
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800524 // Prevent unnecessary calls to context menu
525 // if url and image src are null
526 if (unknown_type_src == null && unknown_type_url == null)
527 break;
528
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800529 //setting the HitTestResult with new RESULT TYPE
530 if (!TextUtils.isEmpty(unknown_type_src)) {
531 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
532 result.setExtra(unknown_type_src);
533 } else {
534 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
535 result.setExtra("about:blank");
536 }
537
538 mResult = result;
539 openContextMenu(viewForUnknownType);
540 mResult = null;
541
542 break;
543
Michael Kolb8233fac2010-10-26 16:08:53 -0700544 case FOCUS_NODE_HREF:
545 {
546 String url = (String) msg.getData().get("url");
547 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500548 String src = (String) msg.getData().get("src");
549 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700550 if (TextUtils.isEmpty(url)) {
551 break;
552 }
553 HashMap focusNodeMap = (HashMap) msg.obj;
554 WebView view = (WebView) focusNodeMap.get("webview");
555 // Only apply the action if the top window did not change.
556 if (getCurrentTopWebView() != view) {
557 break;
558 }
559 switch (msg.arg1) {
560 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700561 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700562 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500563 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700564 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500565 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500566 case R.id.open_newtab_context_menu_id:
567 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700568 openTab(url, parent,
569 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500570 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700571 case R.id.copy_link_context_menu_id:
572 copy(url);
573 break;
574 case R.id.save_link_context_menu_id:
575 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500576 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800577 mActivity, url, view.getSettings().getUserAgentString(),
578 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700580 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700581 if(title == null || title == "")
582 title = url;
583
584 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
585 //SWE TODO: No thumbnail support for the url obtained via
586 //browser context menu as its not loaded in webview.
587 if (bookmarkIntent != null) {
588 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
589 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
590 mActivity.startActivity(bookmarkIntent);
591 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700592 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 }
594 break;
595 }
596
597 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700598 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 break;
600
601 case STOP_LOAD:
602 stopLoading();
603 break;
604
605 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700606 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700607 mWakeLock.release();
608 // if we reach here, Browser should be still in the
609 // background loading after WAKELOCK_TIMEOUT (5-min).
610 // To avoid burning the battery, stop loading.
611 mTabControl.stopAllLoading();
612 }
613 break;
614
615 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800616 Tab tab = (Tab) msg.obj;
617 if (tab != null) {
618 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700619 }
620 break;
kaiyiz591110b2013-08-06 17:11:06 +0800621 case OPEN_MENU:
622 if (!mOptionsMenuOpen && mActivity != null ) {
623 mActivity.openOptionsMenu();
624 }
625 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 }
627 }
628 };
629
630 }
631
John Reckef654f12011-07-12 16:42:08 -0700632 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200633 public Tab getCurrentTab() {
634 return mTabControl.getCurrentTab();
635 }
636
Michael Kolbba99c5d2010-11-29 14:57:41 -0800637 @Override
638 public void shareCurrentPage() {
639 shareCurrentPage(mTabControl.getCurrentTab());
640 }
641
642 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700643 if (tab == null || tab.getWebView() == null)
644 return;
645
646 final Tab mytab = tab;
647 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
648 @Override
649 public void onReceiveValue(Bitmap bitmap) {
650 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
651 mytab.getFavicon(), bitmap);
652 }
653 };
654
655 createScreenshotAsync(
656 tab.getWebView(),
657 getDesiredThumbnailWidth(mActivity),
658 getDesiredThumbnailHeight(mActivity),
659 new ValueCallback<Bitmap>() {
660 @Override
661 public void onReceiveValue(Bitmap bitmap) {
662 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
663 mytab.getFavicon(), bitmap);
664 }
665 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800666 }
667
Michael Kolb8233fac2010-10-26 16:08:53 -0700668 /**
669 * Share a page, providing the title, url, favicon, and a screenshot. Uses
670 * an {@link Intent} to launch the Activity chooser.
671 * @param c Context used to launch a new Activity.
672 * @param title Title of the page. Stored in the Intent with
673 * {@link Intent#EXTRA_SUBJECT}
674 * @param url URL of the page. Stored in the Intent with
675 * {@link Intent#EXTRA_TEXT}
676 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
677 * with {@link Browser#EXTRA_SHARE_FAVICON}
678 * @param screenshot Bitmap of a screenshot of the page. Stored in the
679 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
680 */
681 static final void sharePage(Context c, String title, String url,
682 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700683
684 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
685 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
686 R.layout.app_row, sDialog.getApps());
687 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 }
689
690 private void copy(CharSequence text) {
691 ClipboardManager cm = (ClipboardManager) mActivity
692 .getSystemService(Context.CLIPBOARD_SERVICE);
693 cm.setText(text);
694 }
695
696 // lifecycle
697
John Reck9c35b9c2012-05-30 10:08:50 -0700698 @Override
699 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700700 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800701 // update the menu in case of a locale change
702 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800703 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800704 if (mOptionsMenuOpen) {
705 mActivity.closeOptionsMenu();
706 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
707 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700708 if (mPageDialogsHandler != null) {
709 mPageDialogsHandler.onConfigurationChanged(config);
710 }
711 mUi.onConfigurationChanged(config);
712 }
713
714 @Override
715 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700716 if (!mUi.isWebShowing()) {
717 mUi.showWeb(false);
718 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700719 mIntentHandler.onNewIntent(intent);
720 }
721
John Reck9c35b9c2012-05-30 10:08:50 -0700722 @Override
723 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800724 if (mUi.isCustomViewShowing()) {
725 hideCustomView();
726 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 if (mActivityPaused) {
728 Log.e(LOGTAG, "BrowserActivity is already paused.");
729 return;
730 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700731 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800732 Tab tab = mTabControl.getCurrentTab();
733 if (tab != null) {
734 tab.pause();
735 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700736 if (mWakeLock == null) {
737 PowerManager pm = (PowerManager) mActivity
738 .getSystemService(Context.POWER_SERVICE);
739 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
740 }
Michael Kolb70976932010-11-30 11:34:01 -0800741 mWakeLock.acquire();
742 mHandler.sendMessageDelayed(mHandler
743 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
744 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 }
746 mUi.onPause();
747 mNetworkHandler.onPause();
748
749 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100750 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700751 }
752
John Reck9c35b9c2012-05-30 10:08:50 -0700753 @Override
754 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700755 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800756 Bundle saveState = createSaveState();
757
758 // crash recovery manages all save & restore state
759 mCrashRecoveryHandler.writeState(saveState);
760 mSettings.setLastRunPaused(true);
761 }
762
763 /**
764 * Save the current state to outState. Does not write the state to
765 * disk.
766 * @return Bundle containing the current state of all tabs.
767 */
768 /* package */ Bundle createSaveState() {
769 Bundle saveState = new Bundle();
770 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800771 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700772 }
773
John Reck9c35b9c2012-05-30 10:08:50 -0700774 @Override
775 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700776 if (!mActivityPaused) {
777 Log.e(LOGTAG, "BrowserActivity is already resumed.");
778 return;
779 }
George Mount3636d0a2011-11-21 09:08:21 -0800780 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700781 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800782 Tab current = mTabControl.getCurrentTab();
783 if (current != null) {
784 current.resume();
785 resumeWebViewTimers(current);
786 }
John Reckf57c0292011-07-21 18:15:39 -0700787 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200788
Michael Kolb8233fac2010-10-26 16:08:53 -0700789 mUi.onResume();
790 mNetworkHandler.onResume();
791 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100792 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700793 if (mVoiceResult != null) {
794 mUi.onVoiceResult(mVoiceResult);
795 mVoiceResult = null;
796 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800797 if (current != null && current.hasCrashed) {
Vivek Sekhar2868b8d2014-12-03 17:22:50 -0800798 current.replaceCrashView(current.getWebView(), current.getViewContainer());
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800799 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 }
801
John Reckf57c0292011-07-21 18:15:39 -0700802 private void releaseWakeLock() {
803 if (mWakeLock != null && mWakeLock.isHeld()) {
804 mHandler.removeMessages(RELEASE_WAKELOCK);
805 mWakeLock.release();
806 }
807 }
808
Michael Kolb70976932010-11-30 11:34:01 -0800809 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800810 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800811 * @param tab guaranteed non-null
812 */
813 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 boolean inLoad = tab.inPageLoad();
815 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
816 CookieSyncManager.getInstance().startSync();
817 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100818 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 }
820 }
821
Michael Kolb70976932010-11-30 11:34:01 -0800822 /**
823 * Pause all WebView timers using the WebView of the given tab
824 * @param tab
825 * @return true if the timers are paused or tab is null
826 */
827 private boolean pauseWebViewTimers(Tab tab) {
828 if (tab == null) {
829 return true;
830 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100832 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700833 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700834 }
Michael Kolb70976932010-11-30 11:34:01 -0800835 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700836 }
837
John Reck9c35b9c2012-05-30 10:08:50 -0700838 @Override
839 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800840 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
842 mUploadHandler = null;
843 }
844 if (mTabControl == null) return;
845 mUi.onDestroy();
846 // Remove the current tab and sub window
847 Tab t = mTabControl.getCurrentTab();
848 if (t != null) {
849 dismissSubWindow(t);
850 removeTab(t);
851 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500852 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 // Destroy all the tabs
854 mTabControl.destroy();
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 }
856
857 protected boolean isActivityPaused() {
858 return mActivityPaused;
859 }
860
John Reck9c35b9c2012-05-30 10:08:50 -0700861 @Override
862 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 mTabControl.freeMemory();
864 }
865
866 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700867 public void stopLoading() {
868 mLoadStopped = true;
869 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700870 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700871 if (w != null) {
872 w.stopLoading();
873 mUi.onPageStopped(tab);
874 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700875 }
876
877 boolean didUserStopLoading() {
878 return mLoadStopped;
879 }
880
kaiyiza016da12013-08-26 17:50:22 +0800881 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700882 final String reminderType = getContext().getResources().getString(
883 R.string.def_wifi_browser_interaction_remind_type);
884 final String selectionConnnection = getContext().getResources().getString(
885 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700886 final String wifiSelection = getContext().getResources().getString(
887 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700888
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700889 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
890 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700891 return;
892
kaiyiza016da12013-08-26 17:50:22 +0800893 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700894 Context.CONNECTIVITY_SERVICE);
895 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700896 WifiManager wifiMgr = (WifiManager) this.getContext()
897 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700898 if (networkInfo == null
899 || (networkInfo != null && (networkInfo.getType() !=
900 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700901 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
902 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700903 List<ScanResult> list = wifiMgr.getScanResults();
904 // Have no AP's for Wifi's fall back to data
905 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700906 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700907 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
908 this.getContext().startActivity(intent);
909 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700910 // Request to select Wifi AP
911 try {
912 Intent intent = new Intent(wifiSelection);
913 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
914 this.getContext().startActivity(intent);
915 } catch (Exception e) {
916 String err_msg = this.getContext().getString(
917 R.string.acivity_not_found, wifiSelection);
918 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
919 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700920 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700921 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800922 }
923 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700924
Michael Kolb8233fac2010-10-26 16:08:53 -0700925 // WebViewController
926
927 @Override
John Reck324d4402011-01-11 16:56:42 -0800928 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700929
930 // We've started to load a new page. If there was a pending message
931 // to save a screenshot then we will now take the new page and save
932 // an incorrect screenshot. Therefore, remove any pending thumbnail
933 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700934 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700935
936 // reset sync timer to avoid sync starts during loading a page
937 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700938 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700939 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800940 boolean networkNotifier = BrowserConfig.getInstance(getContext())
941 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700942 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700943 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800944 } else {
945 if (!mNetworkHandler.isNetworkUp()) {
946 view.setNetworkAvailable(false);
947 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 }
949
950 // when BrowserActivity just starts, onPageStarted may be called before
951 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
952 // to start the timer. As we won't switch tabs while an activity is in
953 // pause state, we can ensure calling resume and pause in pair.
954 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800955 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700956 }
957 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 endActionMode();
959
John Reck30c714c2010-12-16 17:30:34 -0800960 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700961
John Reck324d4402011-01-11 16:56:42 -0800962 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700963 // update the bookmark database for favicon
964 maybeUpdateFavicon(tab, null, url, favicon);
965
966 Performance.tracePageStart(url);
967
968 // Performance probe
969 if (false) {
970 Performance.onPageStarted();
971 }
972
973 }
974
975 @Override
John Reck324d4402011-01-11 16:56:42 -0800976 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700977 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800978 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200979
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 // Performance probe
981 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800982 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 }
984
Tarun Nainani8eb00912014-07-17 12:28:32 -0700985 tab.onPageFinished();
986
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 Performance.tracePageFinished();
988 }
989
990 @Override
John Reck30c714c2010-12-16 17:30:34 -0800991 public void onProgressChanged(Tab tab) {
992 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700993
994 if (newProgress == 100) {
995 CookieSyncManager.getInstance().sync();
996 // onProgressChanged() may continue to be called after the main
997 // frame has finished loading, as any remaining sub frames continue
998 // to load. We'll only get called once though with newProgress as
999 // 100 when everything is loaded. (onPageFinished is called once
1000 // when the main frame completes loading regardless of the state of
1001 // any sub frames so calls to onProgressChanges may continue after
1002 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001003 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001004 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001005 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001006 } else if (mWasInPageLoad) {
1007 mWasInPageLoad = false;
1008 updateInLoadMenuItems(mCachedMenu, tab);
1009 }
1010
1011 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001012 // pause the WebView timer and release the wake lock if it is
1013 // finished while BrowserActivity is in pause state.
1014 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001015 }
John Reckd9862372012-02-21 15:04:50 -08001016 if (!tab.isPrivateBrowsingEnabled()
1017 && !TextUtils.isEmpty(tab.getUrl())
1018 && !tab.isSnapshot()) {
1019 // Only update the bookmark screenshot if the user did not
1020 // cancel the load early and there is not already
1021 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001022 if (tab.shouldUpdateThumbnail() &&
1023 (tab.inForeground() && !didUserStopLoading()
1024 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001025 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1026 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1027 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001028 1500);
John Reckd9862372012-02-21 15:04:50 -08001029 }
1030 }
1031 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001032 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001033 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001034 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001035 // still loading
1036 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001038 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001039 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001040 } else {
1041 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 }
1043 }
John Reck30c714c2010-12-16 17:30:34 -08001044 mUi.onProgressChanged(tab);
1045 }
1046
1047 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001048 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001049 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 }
1051
1052 @Override
1053 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001054 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001055 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001056 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001057 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1058 return;
1059 }
1060 // Update the title in the history database if not in private browsing mode
1061 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001062 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 }
1064 }
1065
1066 @Override
1067 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001068 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001069 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1070 }
1071
1072 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001073 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1074 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001075 }
1076
1077 @Override
1078 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1079 if (mMenuIsDown) {
1080 // only check shortcut key when MENU is held
1081 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1082 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001083 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001084 int keyCode = event.getKeyCode();
1085 // We need to send almost every key to WebKit. However:
1086 // 1. We don't want to block the device on the renderer for
1087 // some keys like menu, home, call.
1088 // 2. There are no WebKit equivalents for some of these keys
1089 // (see app/keyboard_codes_win.h)
1090 // Note that these are not the same set as KeyEvent.isSystemKey:
1091 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1092 if (keyCode == KeyEvent.KEYCODE_MENU ||
1093 keyCode == KeyEvent.KEYCODE_HOME ||
1094 keyCode == KeyEvent.KEYCODE_BACK ||
1095 keyCode == KeyEvent.KEYCODE_CALL ||
1096 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1097 keyCode == KeyEvent.KEYCODE_POWER ||
1098 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1099 keyCode == KeyEvent.KEYCODE_CAMERA ||
1100 keyCode == KeyEvent.KEYCODE_FOCUS ||
1101 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1102 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1103 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1104 return true;
1105 }
1106
1107 // We also have to intercept some shortcuts before we send them to the ContentView.
1108 if (event.isCtrlPressed() && (
1109 keyCode == KeyEvent.KEYCODE_TAB ||
1110 keyCode == KeyEvent.KEYCODE_W ||
1111 keyCode == KeyEvent.KEYCODE_F4)) {
1112 return true;
1113 }
1114
1115 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 }
1117
Tarun Nainanif5563b62015-01-21 18:52:59 -08001118 private void handleMediaKeyEvent(KeyEvent event) {
1119
1120 int keyCode = event.getKeyCode();
1121 // send media key events to audio manager
1122 if (Build.VERSION.SDK_INT >= 19) {
1123
1124 switch (keyCode) {
1125 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1126 case KeyEvent.KEYCODE_MEDIA_STOP:
1127 case KeyEvent.KEYCODE_MEDIA_NEXT:
1128 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1129 case KeyEvent.KEYCODE_MEDIA_REWIND:
1130 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1131 case KeyEvent.KEYCODE_MUTE:
1132 case KeyEvent.KEYCODE_MEDIA_PLAY:
1133 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1134 case KeyEvent.META_SHIFT_RIGHT_ON:
1135 case KeyEvent.KEYCODE_MEDIA_EJECT:
1136 case KeyEvent.KEYCODE_MEDIA_RECORD:
1137 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1138
1139 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1140 .getSystemService(Context.AUDIO_SERVICE);
1141 audioManager.dispatchMediaKeyEvent(event);
1142 }
1143 }
1144 }
1145
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 @Override
John Reck997b1b72012-04-19 18:08:25 -07001147 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001149 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001151 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 } else {
John Reck997b1b72012-04-19 18:08:25 -07001153 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001154 }
1155 }
John Reck997b1b72012-04-19 18:08:25 -07001156 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 }
1158
1159 @Override
John Reck324d4402011-01-11 16:56:42 -08001160 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001161 // Don't save anything in private browsing mode or when disabling history
1162 // for regular tabs is enabled
1163 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1164 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1165 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001166
John Reck49a603c2011-03-03 09:33:05 -08001167 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001168
John Reck324d4402011-01-11 16:56:42 -08001169 if (TextUtils.isEmpty(url)
1170 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001171 return;
1172 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001173
John Reckf57c0292011-07-21 18:15:39 -07001174 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001175 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001176 }
1177
1178 @Override
1179 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1180 AsyncTask<Void, Void, String[]> task =
1181 new AsyncTask<Void, Void, String[]>() {
1182 @Override
1183 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001184 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 }
1186 @Override
1187 public void onPostExecute(String[] result) {
1188 callback.onReceiveValue(result);
1189 }
1190 };
1191 task.execute();
1192 }
1193
1194 @Override
1195 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1196 final HttpAuthHandler handler, final String host,
1197 final String realm) {
1198 String username = null;
1199 String password = null;
1200
1201 boolean reuseHttpAuthUsernamePassword
1202 = handler.useHttpAuthUsernamePassword();
1203
1204 if (reuseHttpAuthUsernamePassword && view != null) {
1205 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1206 if (credentials != null && credentials.length == 2) {
1207 username = credentials[0];
1208 password = credentials[1];
1209 }
1210 }
1211
1212 if (username != null && password != null) {
1213 handler.proceed(username, password);
1214 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001215 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001216 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1217 } else {
1218 handler.cancel();
1219 }
1220 }
1221 }
1222
1223 @Override
1224 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001225 String contentDisposition, String mimetype, String referer,
1226 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001227 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001228 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001229 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001230 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001231 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 // This Tab was opened for the sole purpose of downloading a
1233 // file. Remove it.
1234 if (tab == mTabControl.getCurrentTab()) {
1235 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001236 if (tab.getDerivedFromIntent())
1237 closeTab(tab);
1238 else
1239 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 } else {
1241 // In this case, it is not.
1242 closeTab(tab);
1243 }
1244 }
1245 }
1246
1247 @Override
1248 public Bitmap getDefaultVideoPoster() {
1249 return mUi.getDefaultVideoPoster();
1250 }
1251
1252 @Override
1253 public View getVideoLoadingProgressView() {
1254 return mUi.getVideoLoadingProgressView();
1255 }
1256
1257 @Override
1258 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1259 SslError error) {
1260 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1261 }
1262
1263 // helper method
1264
1265 /*
1266 * Update the favorites icon if the private browsing isn't enabled and the
1267 * icon is valid.
1268 */
1269 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1270 final String url, Bitmap favicon) {
1271 if (favicon == null) {
1272 return;
1273 }
1274 if (!tab.isPrivateBrowsingEnabled()) {
1275 Bookmarks.updateFavicon(mActivity
1276 .getContentResolver(), originalUrl, url, favicon);
1277 }
1278 }
1279
Leon Scroggins4cd97792010-12-03 15:31:56 -05001280 @Override
1281 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001282 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001283 mUi.bookmarkedStatusHasChanged(tab);
1284 }
1285
Michael Kolb8233fac2010-10-26 16:08:53 -07001286 // end WebViewController
1287
1288 protected void pageUp() {
1289 getCurrentTopWebView().pageUp(false);
1290 }
1291
1292 protected void pageDown() {
1293 getCurrentTopWebView().pageDown(false);
1294 }
1295
1296 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001297 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001298 public void editUrl() {
1299 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001300 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001301 }
1302
John Reck9c35b9c2012-05-30 10:08:50 -07001303 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001304 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001305 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001306 if (tab.inForeground()) {
1307 if (mUi.isCustomViewShowing()) {
1308 callback.onCustomViewHidden();
1309 return;
1310 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001311 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001312 // Save the menu state and set it to empty while the custom
1313 // view is showing.
1314 mOldMenuState = mMenuState;
1315 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001316 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001317 }
1318 }
1319
1320 @Override
1321 public void hideCustomView() {
1322 if (mUi.isCustomViewShowing()) {
1323 mUi.onHideCustomView();
1324 // Reset the old menu state.
1325 mMenuState = mOldMenuState;
1326 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001327 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001328 }
1329 }
1330
John Reck9c35b9c2012-05-30 10:08:50 -07001331 @Override
1332 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001333 Intent intent) {
1334 if (getCurrentTopWebView() == null) return;
1335 switch (requestCode) {
1336 case PREFERENCES_PAGE:
1337 if (resultCode == Activity.RESULT_OK && intent != null) {
1338 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001339 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001340 mTabControl.removeParentChildRelationShips();
1341 }
1342 }
1343 break;
1344 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001345 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001346 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001347 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001348 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001349 case AUTOFILL_SETUP:
1350 // Determine whether a profile was actually set up or not
1351 // and if so, send the message back to the WebTextView to
1352 // fill the form with the new profile.
1353 if (getSettings().getAutoFillProfile() != null) {
1354 mAutoFillSetupMessage.sendToTarget();
1355 mAutoFillSetupMessage = null;
1356 }
1357 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001358 case COMBO_VIEW:
1359 if (intent == null || resultCode != Activity.RESULT_OK) {
1360 break;
1361 }
John Reck3ba45532011-08-11 16:26:53 -07001362 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001363 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1364 Tab t = getCurrentTab();
1365 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001366 mUpdateMyNavThumbnail = true;
1367 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001368 loadUrl(t, uri.toString());
1369 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1370 String[] urls = intent.getStringArrayExtra(
1371 ComboViewActivity.EXTRA_OPEN_ALL);
1372 Tab parent = getCurrentTab();
1373 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001374 if (url != null) {
1375 parent = openTab(url, parent,
1376 !mSettings.openInBackground(), true);
1377 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001378 }
1379 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1380 long id = intent.getLongExtra(
1381 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1382 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001383 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001384 }
1385 }
1386 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001387 case VOICE_RESULT:
1388 if (resultCode == Activity.RESULT_OK && intent != null) {
1389 ArrayList<String> results = intent.getStringArrayListExtra(
1390 RecognizerIntent.EXTRA_RESULTS);
1391 if (results.size() >= 1) {
1392 mVoiceResult = results.get(0);
1393 }
1394 }
1395 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001396 case MY_NAVIGATION:
1397 if (intent == null || resultCode != Activity.RESULT_OK) {
1398 break;
1399 }
1400
1401 if (intent.getBooleanExtra("need_refresh", false) &&
1402 getCurrentTopWebView() != null) {
1403 getCurrentTopWebView().reload();
1404 }
1405 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 default:
1407 break;
1408 }
1409 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001410 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001411 }
1412
1413 /**
1414 * Open the Go page.
1415 * @param startWithHistory If true, open starting on the history tab.
1416 * Otherwise, start with the bookmarks tab.
1417 */
1418 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001419 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001420 if (mTabControl.getCurrentWebView() == null) {
1421 return;
1422 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001423 // clear action mode
1424 if (isInCustomActionMode()) {
1425 endActionMode();
1426 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001427 Bundle extras = new Bundle();
1428 // Disable opening in a new window if we have maxed out the windows
1429 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1430 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001431 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001432 }
1433
1434 // combo view callbacks
1435
Michael Kolb8233fac2010-10-26 16:08:53 -07001436 // key handling
1437 protected void onBackKey() {
1438 if (!mUi.onBackKey()) {
1439 WebView subwindow = mTabControl.getCurrentSubWindow();
1440 if (subwindow != null) {
1441 if (subwindow.canGoBack()) {
1442 subwindow.goBack();
1443 } else {
1444 dismissSubWindow(mTabControl.getCurrentTab());
1445 }
1446 } else {
1447 goBackOnePageOrQuit();
1448 }
1449 }
1450 }
1451
Michael Kolb4bd767d2011-05-27 11:33:55 -07001452 protected boolean onMenuKey() {
1453 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001454 }
1455
Michael Kolb8233fac2010-10-26 16:08:53 -07001456 // menu handling and state
1457 // TODO: maybe put into separate handler
1458
John Reck9c35b9c2012-05-30 10:08:50 -07001459 @Override
1460 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001461 if (mMenuState == EMPTY_MENU) {
1462 return false;
1463 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001464 MenuInflater inflater = mActivity.getMenuInflater();
1465 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001466 return true;
1467 }
1468
John Reck9c35b9c2012-05-30 10:08:50 -07001469 @Override
1470 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001471 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001472 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001473 return;
1474 }
1475 if (!(v instanceof WebView)) {
1476 return;
1477 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001478 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001479 WebView.HitTestResult result;
1480
1481 /* Determine whether the ContextMenu got triggered because
1482 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1483 * received. The mResult acts as a flag to identify, how it got trigerred
1484 */
1485 if (mResult == null){
1486 result = webview.getHitTestResult();
1487 } else {
1488 result = mResult;
1489 }
1490
Michael Kolb8233fac2010-10-26 16:08:53 -07001491 if (result == null) {
1492 return;
1493 }
1494
1495 int type = result.getType();
1496 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001497
1498 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1499 unknownTypeMap.put("webview", webview);
1500 final Message msg = mHandler.obtainMessage(
1501 UNKNOWN_TYPE_MSG, unknownTypeMap);
1502 /* As defined in android developers guide
1503 * when UNKNOWN_TYPE is received as a result of HitTest
1504 * you need to determing the type by invoking requestFocusNodeHref
1505 */
1506 webview.requestFocusNodeHref(msg);
1507
Michael Kolb8233fac2010-10-26 16:08:53 -07001508 Log.w(LOGTAG,
1509 "We should not show context menu when nothing is touched");
1510 return;
1511 }
1512 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1513 // let TextView handles context menu
1514 return;
1515 }
1516
1517 // Note, http://b/issue?id=1106666 is requesting that
1518 // an inflated menu can be used again. This is not available
1519 // yet, so inflate each time (yuk!)
1520 MenuInflater inflater = mActivity.getMenuInflater();
1521 inflater.inflate(R.menu.browsercontext, menu);
1522
1523 // Show the correct menu group
1524 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001525 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001526 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001527 menu.setGroupVisible(R.id.PHONE_MENU,
1528 type == WebView.HitTestResult.PHONE_TYPE);
1529 menu.setGroupVisible(R.id.EMAIL_MENU,
1530 type == WebView.HitTestResult.EMAIL_TYPE);
1531 menu.setGroupVisible(R.id.GEO_MENU,
1532 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001533 String itemUrl = null;
1534 String url = webview.getOriginalUrl();
1535 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1536 itemUrl = Uri.decode(navigationUrl);
1537 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1538 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1539 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1540 } else {
1541 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1542 }
1543 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1544 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1545 } else {
1546 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1547
1548 menu.setGroupVisible(R.id.IMAGE_MENU,
1549 type == WebView.HitTestResult.IMAGE_TYPE
1550 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1551 menu.setGroupVisible(R.id.ANCHOR_MENU,
1552 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1553 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001554 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1555 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001556 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001557 // Setup custom handling depending on the type
1558 switch (type) {
1559 case WebView.HitTestResult.PHONE_TYPE:
1560 menu.setHeaderTitle(Uri.decode(extra));
1561 menu.findItem(R.id.dial_context_menu_id).setIntent(
1562 new Intent(Intent.ACTION_VIEW, Uri
1563 .parse(WebView.SCHEME_TEL + extra)));
1564 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1565 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1566 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1567 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1568 addIntent);
1569 menu.findItem(R.id.copy_phone_context_menu_id)
1570 .setOnMenuItemClickListener(
1571 new Copy(extra));
1572 break;
1573
1574 case WebView.HitTestResult.EMAIL_TYPE:
1575 menu.setHeaderTitle(extra);
1576 menu.findItem(R.id.email_context_menu_id).setIntent(
1577 new Intent(Intent.ACTION_VIEW, Uri
1578 .parse(WebView.SCHEME_MAILTO + extra)));
1579 menu.findItem(R.id.copy_mail_context_menu_id)
1580 .setOnMenuItemClickListener(
1581 new Copy(extra));
1582 break;
1583
1584 case WebView.HitTestResult.GEO_TYPE:
1585 menu.setHeaderTitle(extra);
1586 menu.findItem(R.id.map_context_menu_id).setIntent(
1587 new Intent(Intent.ACTION_VIEW, Uri
1588 .parse(WebView.SCHEME_GEO
1589 + URLEncoder.encode(extra))));
1590 menu.findItem(R.id.copy_geo_context_menu_id)
1591 .setOnMenuItemClickListener(
1592 new Copy(extra));
1593 break;
1594
1595 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1596 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001597 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001598 // decide whether to show the open link in new tab option
1599 boolean showNewTab = mTabControl.canCreateNewTab();
1600 MenuItem newTabItem
1601 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001602 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001603 ? R.string.contextmenu_openlink_newwindow_background
1604 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001605 newTabItem.setVisible(showNewTab);
1606 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001607 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1608 newTabItem.setOnMenuItemClickListener(
1609 new MenuItem.OnMenuItemClickListener() {
1610 @Override
1611 public boolean onMenuItemClick(MenuItem item) {
1612 final HashMap<String, WebView> hrefMap =
1613 new HashMap<String, WebView>();
1614 hrefMap.put("webview", webview);
1615 final Message msg = mHandler.obtainMessage(
1616 FOCUS_NODE_HREF,
1617 R.id.open_newtab_context_menu_id,
1618 0, hrefMap);
1619 webview.requestFocusNodeHref(msg);
1620 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001622 });
1623 } else {
1624 newTabItem.setOnMenuItemClickListener(
1625 new MenuItem.OnMenuItemClickListener() {
1626 @Override
1627 public boolean onMenuItemClick(MenuItem item) {
1628 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001629 openTab(extra, parent,
1630 !mSettings.openInBackground(),
1631 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001632 return true;
1633 }
1634 });
1635 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001636 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001637 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1638 menu.setHeaderTitle(navigationUrl);
1639 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1640
1641 if (itemUrl != null) {
1642 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1643 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1644 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1645 @Override
1646 public boolean onMenuItemClick(MenuItem item) {
1647 final Intent intent = new Intent(Controller.this
1648 .getContext(),
1649 AddMyNavigationPage.class);
1650 Bundle bundle = new Bundle();
1651 String url = Uri.decode(navigationUrl);
1652 bundle.putBoolean("isAdding", false);
1653 bundle.putString("url", url);
1654 bundle.putString("name", getNameFromUrl(url));
1655 intent.putExtra("websites", bundle);
1656 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1657 return false;
1658 }
1659 });
1660 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1661 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1662 @Override
1663 public boolean onMenuItemClick(MenuItem item) {
1664 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1665 return false;
1666 }
1667 });
1668 }
1669 } else {
1670 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1671 }
1672 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001673 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1674 break;
1675 }
1676 // otherwise fall through to handle image part
1677 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001678 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1679 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001680 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1681 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001682 shareItem.setOnMenuItemClickListener(
1683 new MenuItem.OnMenuItemClickListener() {
1684 @Override
1685 public boolean onMenuItemClick(MenuItem item) {
1686 sharePage(mActivity, null, extra, null,
1687 null);
1688 return true;
1689 }
1690 }
1691 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001692 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001693 menu.findItem(R.id.view_image_context_menu_id)
1694 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1695 @Override
1696 public boolean onMenuItemClick(MenuItem item) {
1697 openTab(extra, mTabControl.getCurrentTab(), true, true);
1698 return false;
1699 }
1700 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001701 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1702 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1703 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001704 menu.findItem(R.id.set_wallpaper_context_menu_id).
1705 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1706 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001707 break;
1708
1709 default:
1710 Log.w(LOGTAG, "We should not get here.");
1711 break;
1712 }
1713 //update the ui
1714 mUi.onContextMenuCreated(menu);
1715 }
1716
kaiyiz6e5b3e02013-08-19 20:02:01 +08001717 public void startAddMyNavigation(String url) {
1718 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1719 Bundle bundle = new Bundle();
1720 bundle.putBoolean("isAdding", true);
1721 bundle.putString("url", url);
1722 bundle.putString("name", getNameFromUrl(url));
1723 intent.putExtra("websites", bundle);
1724 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1725 }
1726
1727 private void showMyNavigationDeleteDialog(final String itemUrl) {
1728 new AlertDialog.Builder(this.getContext())
1729 .setTitle(R.string.my_navigation_delete_label)
1730 .setIcon(android.R.drawable.ic_dialog_alert)
1731 .setMessage(R.string.my_navigation_delete_msg)
1732 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1733 @Override
1734 public void onClick(DialogInterface dialog, int whichButton) {
1735 deleteMyNavigationItem(itemUrl);
1736 }
1737 })
1738 .setNegativeButton(R.string.cancel, null)
1739 .show();
1740 }
1741
1742 private void deleteMyNavigationItem(final String itemUrl) {
1743 ContentResolver cr = this.getContext().getContentResolver();
1744 Cursor cursor = null;
1745
1746 try {
1747 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1748 new String[] {
1749 MyNavigationUtil.ID
1750 }, "url = ?", new String[] {
1751 itemUrl
1752 }, null);
1753 if (null != cursor && cursor.moveToFirst()) {
1754 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1755 cursor.getLong(0));
1756
1757 ContentValues values = new ContentValues();
1758 values.put(MyNavigationUtil.TITLE, "");
1759 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1760 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1761 ByteArrayOutputStream os = new ByteArrayOutputStream();
1762 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1763 R.raw.my_navigation_add);
1764 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1765 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1766 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1767 cr.update(uri, values, null, null);
1768 } else {
1769 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1770 }
1771 } catch (IllegalStateException e) {
1772 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1773 } finally {
1774 if (null != cursor) {
1775 cursor.close();
1776 }
1777 }
1778
1779 if (getCurrentTopWebView() != null) {
1780 getCurrentTopWebView().reload();
1781 }
1782 }
1783
1784 private String getNameFromUrl(String itemUrl) {
1785 ContentResolver cr = this.getContext().getContentResolver();
1786 Cursor cursor = null;
1787 String name = null;
1788
1789 try {
1790 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1791 new String[] {
1792 MyNavigationUtil.TITLE
1793 }, "url = ?", new String[] {
1794 itemUrl
1795 }, null);
1796 if (null != cursor && cursor.moveToFirst()) {
1797 name = cursor.getString(0);
1798 } else {
1799 Log.e(LOGTAG, "this item does not exist!");
1800 }
1801 } catch (IllegalStateException e) {
1802 Log.e(LOGTAG, "getNameFromUrl", e);
1803 } finally {
1804 if (null != cursor) {
1805 cursor.close();
1806 }
1807 }
1808 return name;
1809 }
1810
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001811 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001812 final ContentResolver cr = mActivity.getContentResolver();
1813 new AsyncTask<Void, Void, Void>() {
1814 @Override
1815 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001816 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1817 if(!isMyNavigationUrl)
1818 return null;
1819
kaiyiz6e5b3e02013-08-19 20:02:01 +08001820 ContentResolver cr = mActivity.getContentResolver();
1821 Cursor cursor = null;
1822 try {
1823 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1824 new String[] {
1825 MyNavigationUtil.ID
1826 }, "url = ?", new String[] {
1827 itemUrl
1828 }, null);
1829 if (null != cursor && cursor.moveToFirst()) {
1830 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001831 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001832
1833 ContentValues values = new ContentValues();
1834 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1835 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1836 cursor.getLong(0));
1837 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1838 cr.update(uri, values, null, null);
1839 os.close();
1840 }
1841 } catch (IllegalStateException e) {
1842 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1843 } catch (IOException e) {
1844 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1845 } finally {
1846 if (null != cursor) {
1847 cursor.close();
1848 }
1849 }
1850 return null;
1851 }
1852 }.execute();
1853 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001854 /**
1855 * As the menu can be open when loading state changes
1856 * we must manually update the state of the stop/reload menu
1857 * item
1858 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001859 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001860 if (menu == null) {
1861 return;
1862 }
1863 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001864 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001865 menu.findItem(R.id.stop_menu_id):
1866 menu.findItem(R.id.reload_menu_id);
1867 if (src != null) {
1868 dest.setIcon(src.getIcon());
1869 dest.setTitle(src.getTitle());
1870 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001871 mActivity.invalidateOptionsMenu();
1872 }
1873
1874 public void invalidateOptionsMenu() {
1875 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001876 }
1877
John Reck9c35b9c2012-05-30 10:08:50 -07001878 @Override
1879 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001880 // Software menu key (toolbar key)
1881 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1882 return true;
1883 }
1884
1885 @Override
1886 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001887 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001888 // hold on to the menu reference here; it is used by the page callbacks
1889 // to update the menu based on loading state
1890 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001891 // Note: setVisible will decide whether an item is visible; while
1892 // setEnabled() will decide whether an item is enabled, which also means
1893 // whether the matching shortcut key will function.
1894 switch (mMenuState) {
1895 case EMPTY_MENU:
1896 if (mCurrentMenuState != mMenuState) {
1897 menu.setGroupVisible(R.id.MAIN_MENU, false);
1898 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1899 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1900 }
1901 break;
1902 default:
1903 if (mCurrentMenuState != mMenuState) {
1904 menu.setGroupVisible(R.id.MAIN_MENU, true);
1905 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1906 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1907 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001908 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001909 break;
1910 }
1911 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001912 mUi.onPrepareOptionsMenu(menu);
1913 }
1914
1915 private void setMenuItemVisibility(Menu menu, int id,
1916 boolean visibility) {
1917 MenuItem item = menu.findItem(id);
1918 if (item != null) {
1919 item.setVisible(visibility);
1920 }
1921 }
1922
1923 private int lookupBookmark(String title, String url) {
1924 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001925 int count = 0;
1926 Cursor cursor = null;
1927 try {
1928 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1929 BookmarksLoader.PROJECTION,
1930 "title = ? OR url = ?",
1931 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001932 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001933 },
1934 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001935
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001936 if (cursor != null)
1937 count = cursor.getCount();
1938
1939 } catch (IllegalStateException e) {
1940 Log.e(LOGTAG, "lookupBookmark ", e);
1941 } finally {
1942 if (null != cursor) {
1943 cursor.close();
1944 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001945 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001946 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001947 }
1948
1949 private void resetMenuItems(Menu menu) {
1950 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1951 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1952
1953 WebView w = getCurrentTopWebView();
1954 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1955
1956 String title = w.getTitle();
1957 String url = w.getUrl();
1958 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1959 bookmark_icon.setChecked(true);
1960 } else {
1961 bookmark_icon.setChecked(false);
1962 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 }
1964
Michael Kolb4bf79712011-07-14 14:18:12 -07001965 @Override
1966 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001967 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001968 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001969 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001970 // Following flag is used to identify schemes for which the LIVE_MENU
1971 // items defined in res/menu/browser.xml should be enabled
1972 boolean isLiveScheme = false;
1973 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001974 boolean isSavable = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001975 resetMenuItems(menu);
1976
Michael Kolb4bf79712011-07-14 14:18:12 -07001977 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001978 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001979 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001980 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001981 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001982 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001983 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07001984 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001985
1986 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1987 forward.setEnabled(canGoForward);
1988
Michael Kolb4bf79712011-07-14 14:18:12 -07001989 // decide whether to show the share link option
1990 PackageManager pm = mActivity.getPackageManager();
1991 Intent send = new Intent(Intent.ACTION_SEND);
1992 send.setType("text/plain");
1993 ResolveInfo ri = pm.resolveActivity(send,
1994 PackageManager.MATCH_DEFAULT_ONLY);
1995 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1996
1997 boolean isNavDump = mSettings.enableNavDump();
1998 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1999 nav.setVisible(isNavDump);
2000 nav.setEnabled(isNavDump);
2001
2002 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002003 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2004 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002005 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002006 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2007 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002008 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002009 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2010 isLive && isLiveScheme && isPageFinished);
2011 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002012 isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002013 // history and snapshots item are the members of COMBO menu group,
2014 // so if show history item, only make snapshots item invisible.
2015 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002016
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002017 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002018 }
2019
John Reck9c35b9c2012-05-30 10:08:50 -07002020 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002021 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002022 if (null == getCurrentTopWebView()) {
2023 return false;
2024 }
2025 if (mMenuIsDown) {
2026 // The shortcut action consumes the MENU. Even if it is still down,
2027 // it won't trigger the next shortcut action. In the case of the
2028 // shortcut action triggering a new activity, like Bookmarks, we
2029 // won't get onKeyUp for MENU. So it is important to reset it here.
2030 mMenuIsDown = false;
2031 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002032 if (mUi.onOptionsItemSelected(item)) {
2033 // ui callback handled it
2034 return true;
2035 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002036 switch (item.getItemId()) {
2037 // -- Main menu
2038 case R.id.new_tab_menu_id:
2039 openTabToHomePage();
2040 break;
2041
2042 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002043 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002044 break;
2045
2046 case R.id.goto_menu_id:
2047 editUrl();
2048 break;
2049
2050 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002051 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2052 break;
2053
2054 case R.id.history_menu_id:
2055 bookmarksOrHistoryPicker(ComboViews.History);
2056 break;
2057
2058 case R.id.snapshots_menu_id:
2059 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002060 break;
2061
Pankaj Garg49b79252014-11-07 17:33:41 -08002062 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002063 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002064 break;
2065
2066 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002067 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002068 stopLoading();
2069 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002070 Tab currentTab = mTabControl.getCurrentTab();
2071 if (currentTab.hasCrashed) {
2072 currentTab.replaceCrashView(getCurrentTopWebView(),
2073 currentTab.getViewContainer());
2074 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002075 getCurrentTopWebView().reload();
2076 }
2077 break;
2078
Michael Kolb8233fac2010-10-26 16:08:53 -07002079 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002080 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 break;
2082
2083 case R.id.close_menu_id:
2084 // Close the subwindow if it exists.
2085 if (mTabControl.getCurrentSubWindow() != null) {
2086 dismissSubWindow(mTabControl.getCurrentTab());
2087 break;
2088 }
2089 closeCurrentTab();
2090 break;
2091
kaiyiza8b6dbb2013-07-29 18:11:22 +08002092 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002093 Object[] params = { new String("persist.debug.browsermonkeytest")};
2094 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002095 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002096 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002097 if (ret != null && ret.equals("enable"))
2098 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002099 if (BrowserConfig.getInstance(getContext())
2100 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2101 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002102 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002103 case R.id.homepage_menu_id:
2104 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002105 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002106 break;
2107
2108 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002109 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002110 break;
2111
2112 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002113 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002114 break;
2115
John Reck2bc80422011-06-30 15:11:49 -07002116 case R.id.save_snapshot_menu_id:
2117 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002118 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002119 createScreenshotAsync(
2120 source.getWebView(),
2121 getDesiredThumbnailWidth(mActivity),
2122 getDesiredThumbnailHeight(mActivity),
2123 new ValueCallback<Bitmap>() {
2124 @Override
2125 public void onReceiveValue(Bitmap bitmap) {
2126 new SaveSnapshotTask(source, bitmap).execute();
2127 }
2128 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002129 break;
2130
Michael Kolb8233fac2010-10-26 16:08:53 -07002131 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002132 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 break;
2134
John Recke1a03a32011-09-14 17:04:16 -07002135 case R.id.snapshot_go_live:
2136 goLive();
2137 return true;
2138
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 case R.id.share_page_menu_id:
2140 Tab currentTab = mTabControl.getCurrentTab();
2141 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002142 return false;
2143 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002144 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 break;
2146
2147 case R.id.dump_nav_menu_id:
2148 getCurrentTopWebView().debugDump();
2149 break;
2150
Michael Kolb8233fac2010-10-26 16:08:53 -07002151 case R.id.zoom_in_menu_id:
2152 getCurrentTopWebView().zoomIn();
2153 break;
2154
2155 case R.id.zoom_out_menu_id:
2156 getCurrentTopWebView().zoomOut();
2157 break;
2158
2159 case R.id.view_downloads_menu_id:
2160 viewDownloads();
2161 break;
2162
John Reck42229bc2011-08-19 13:26:43 -07002163 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002164 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002165 break;
2166
Michael Kolb8233fac2010-10-26 16:08:53 -07002167 case R.id.window_one_menu_id:
2168 case R.id.window_two_menu_id:
2169 case R.id.window_three_menu_id:
2170 case R.id.window_four_menu_id:
2171 case R.id.window_five_menu_id:
2172 case R.id.window_six_menu_id:
2173 case R.id.window_seven_menu_id:
2174 case R.id.window_eight_menu_id:
2175 {
2176 int menuid = item.getItemId();
2177 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2178 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2179 Tab desiredTab = mTabControl.getTab(id);
2180 if (desiredTab != null &&
2181 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002182 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002183 }
2184 break;
2185 }
2186 }
2187 }
2188 break;
2189
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002190 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002191 Bundle bundle = new Bundle();
2192 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2193 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2194 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2195 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2196 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002197 break;
2198
Pankaj Garg49b79252014-11-07 17:33:41 -08002199 case R.id.add_to_homescreen:
2200 final WebView w = getCurrentTopWebView();
2201 final EditText input = new EditText(getContext());
2202 input.setText(w.getTitle());
2203 new AlertDialog.Builder(getContext())
2204 .setTitle("Add to homescreen")
2205 .setMessage("Title")
2206 .setView(input)
2207 .setPositiveButton("Add", new DialogInterface.OnClickListener() {
2208 public void onClick(DialogInterface dialog, int whichButton) {
2209 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2210 getContext(),
2211 w.getUrl(),
2212 input.getText().toString(),
2213 w.getViewportBitmap(),
2214 w.getFavicon()));
2215
2216 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2217 .addCategory(Intent.CATEGORY_HOME));
2218 }})
2219 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
2220 public void onClick(DialogInterface dialog, int whichButton) {
2221 // Do nothing.
2222 }
2223 })
2224 .show();
2225 break;
2226
Michael Kolb8233fac2010-10-26 16:08:53 -07002227 default:
2228 return false;
2229 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002230 return true;
2231 }
2232
John Reck68234a92012-04-19 15:27:12 -07002233 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2234 implements OnCancelListener {
2235
2236 private Tab mTab;
2237 private Dialog mProgressDialog;
2238 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002239 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002240
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002241 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002242 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002243 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002244 }
2245
2246 @Override
2247 protected void onPreExecute() {
2248 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2249 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2250 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002251 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002252 }
2253
2254 @Override
2255 protected Long doInBackground(Void... params) {
2256 if (!mTab.saveViewState(mValues)) {
2257 return null;
2258 }
2259 if (isCancelled()) {
2260 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2261 File file = mActivity.getFileStreamPath(path);
2262 if (!file.delete()) {
2263 file.deleteOnExit();
2264 }
2265 return null;
2266 }
2267 final ContentResolver cr = mActivity.getContentResolver();
2268 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2269 if (result == null) {
2270 return null;
2271 }
2272 long id = ContentUris.parseId(result);
2273 return id;
2274 }
2275
2276 @Override
2277 protected void onPostExecute(Long id) {
2278 if (isCancelled()) {
2279 return;
2280 }
2281 mProgressDialog.dismiss();
2282 if (id == null) {
2283 Toast.makeText(mActivity, R.string.snapshot_failed,
2284 Toast.LENGTH_SHORT).show();
2285 return;
2286 }
2287 Bundle b = new Bundle();
2288 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2289 mUi.showComboView(ComboViews.Snapshots, b);
2290 }
2291
2292 @Override
2293 public void onCancel(DialogInterface dialog) {
2294 cancel(true);
2295 }
2296 }
2297
Michael Kolb80f75082012-04-10 10:50:06 -07002298 @Override
2299 public void toggleUserAgent() {
2300 WebView web = getCurrentWebView();
2301 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002302 }
2303
2304 @Override
2305 public void findOnPage() {
2306 getCurrentTopWebView().showFindDialog(null, true);
2307 }
2308
2309 @Override
2310 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002311 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002312 }
2313
Pankaj Gargeba076f2015-03-25 13:40:59 -07002314 // This function is specifically used from AddBookmark Activity.
2315 // The bookmark activity clears the bitmap after retrieving it.
2316 // The function usage elsewhere will result in breaking bookmark
2317 // functionality.
2318 public static Bitmap getAndReleaseLastBookmarkBitmapFromIntent() {
2319 Bitmap bitmap = mBookmarkBitmap;
2320 mBookmarkBitmap = null;
2321 return bitmap;
2322 }
2323
Michael Kolb80f75082012-04-10 10:50:06 -07002324 @Override
2325 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002326 WebView w = getCurrentTopWebView();
2327 if (w == null)
2328 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002329 final Intent i = createBookmarkCurrentPageIntent(false);
2330 createScreenshotAsync(
2331 w, getDesiredThumbnailWidth(mActivity),
2332 getDesiredThumbnailHeight(mActivity),
2333 new ValueCallback<Bitmap>() {
2334 @Override
2335 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargeba076f2015-03-25 13:40:59 -07002336 mBookmarkBitmap = bitmap;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002337 mActivity.startActivity(i);
2338 }
2339 });
Michael Kolb80f75082012-04-10 10:50:06 -07002340 }
2341
John Recke1a03a32011-09-14 17:04:16 -07002342 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002343 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002344 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002345 boolean onlySingleTabRemaining = false;
2346 if (mTabControl.getTabCount() > 1) {
2347 // destroy the old snapshot tab
2348 closeCurrentTab();
2349 } else {
2350 onlySingleTabRemaining = true;
2351 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002352 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002353 if (onlySingleTabRemaining) {
2354 closeTab(t);
2355 }
2356
Tarun Nainani8eb00912014-07-17 12:28:32 -07002357 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002358 }
2359
luxiaolb40014b2013-07-19 10:01:43 +08002360 private void showExitDialog(final Activity activity) {
2361 new AlertDialog.Builder(activity)
2362 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002363 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002364 .setMessage(R.string.exit_browser_msg)
2365 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2366 public void onClick(DialogInterface dialog, int which) {
2367 activity.moveTaskToBack(true);
2368 dialog.dismiss();
2369 }
2370 })
2371 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2372 public void onClick(DialogInterface dialog, int which) {
2373 activity.finish();
2374 mHandler.postDelayed(new Runnable() {
2375 @Override
2376 public void run() {
luxiaolb40014b2013-07-19 10:01:43 +08002377 mCrashRecoveryHandler.clearState(true);
2378 int pid = android.os.Process.myPid();
2379 android.os.Process.killProcess(pid);
2380 }
2381 }, 300);
2382 dialog.dismiss();
2383 }
2384 })
2385 .show();
2386 }
Michael Kolb315d5022011-10-13 12:47:11 -07002387 @Override
2388 public void showPageInfo() {
2389 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2390 }
2391
John Reck9c35b9c2012-05-30 10:08:50 -07002392 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002393 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002394 // Let the History and Bookmark fragments handle menus they created.
2395 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2396 return false;
2397 }
2398
Michael Kolb8233fac2010-10-26 16:08:53 -07002399 int id = item.getItemId();
2400 boolean result = true;
2401 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002402 // -- Browser context menu
2403 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002404 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002405 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002406 case R.id.copy_link_context_menu_id:
2407 final WebView webView = getCurrentTopWebView();
2408 if (null == webView) {
2409 result = false;
2410 break;
2411 }
2412 final HashMap<String, WebView> hrefMap =
2413 new HashMap<String, WebView>();
2414 hrefMap.put("webview", webView);
2415 final Message msg = mHandler.obtainMessage(
2416 FOCUS_NODE_HREF, id, 0, hrefMap);
2417 webView.requestFocusNodeHref(msg);
2418 break;
2419
2420 default:
2421 // For other context menus
2422 result = onOptionsItemSelected(item);
2423 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002424 return result;
2425 }
2426
2427 /**
2428 * support programmatically opening the context menu
2429 */
2430 public void openContextMenu(View view) {
2431 mActivity.openContextMenu(view);
2432 }
2433
2434 /**
2435 * programmatically open the options menu
2436 */
2437 public void openOptionsMenu() {
2438 mActivity.openOptionsMenu();
2439 }
2440
John Reck9c35b9c2012-05-30 10:08:50 -07002441 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002442 public boolean onMenuOpened(int featureId, Menu menu) {
2443 if (mOptionsMenuOpen) {
2444 if (mConfigChanged) {
2445 // We do not need to make any changes to the state of the
2446 // title bar, since the only thing that happened was a
2447 // change in orientation
2448 mConfigChanged = false;
2449 } else {
2450 if (!mExtendedMenuOpen) {
2451 mExtendedMenuOpen = true;
2452 mUi.onExtendedMenuOpened();
2453 } else {
2454 // Switching the menu back to icon view, so show the
2455 // title bar once again.
2456 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002457 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002458 }
2459 }
2460 } else {
2461 // The options menu is closed, so open it, and show the title
2462 mOptionsMenuOpen = true;
2463 mConfigChanged = false;
2464 mExtendedMenuOpen = false;
2465 mUi.onOptionsMenuOpened();
2466 }
2467 return true;
2468 }
2469
John Reck9c35b9c2012-05-30 10:08:50 -07002470 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002471 public void onOptionsMenuClosed(Menu menu) {
2472 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002473 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002474 }
2475
John Reck9c35b9c2012-05-30 10:08:50 -07002476 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002477 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002478 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002479 }
2480
2481 // Helper method for getting the top window.
2482 @Override
2483 public WebView getCurrentTopWebView() {
2484 return mTabControl.getCurrentTopWebView();
2485 }
2486
2487 @Override
2488 public WebView getCurrentWebView() {
2489 return mTabControl.getCurrentWebView();
2490 }
2491
2492 /*
2493 * This method is called as a result of the user selecting the options
2494 * menu to see the download window. It shows the download window on top of
2495 * the current window.
2496 */
2497 void viewDownloads() {
2498 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2499 mActivity.startActivity(intent);
2500 }
2501
John Reck30b065e2011-07-19 10:58:05 -07002502 int getActionModeHeight() {
2503 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2504 new int[] { android.R.attr.actionBarSize });
2505 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2506 actionBarSizeTypedArray.recycle();
2507 return size;
2508 }
2509
Michael Kolb8233fac2010-10-26 16:08:53 -07002510 // action mode
2511
John Reck9c35b9c2012-05-30 10:08:50 -07002512 @Override
2513 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002514 mUi.onActionModeStarted(mode);
2515 mActionMode = mode;
2516 }
2517
2518 /*
2519 * True if a custom ActionMode (i.e. find or select) is in use.
2520 */
2521 @Override
2522 public boolean isInCustomActionMode() {
2523 return mActionMode != null;
2524 }
2525
2526 /*
2527 * End the current ActionMode.
2528 */
2529 @Override
2530 public void endActionMode() {
2531 if (mActionMode != null) {
2532 mActionMode.finish();
2533 }
2534 }
2535
2536 /*
2537 * Called by find and select when they are finished. Replace title bars
2538 * as necessary.
2539 */
John Reck9c35b9c2012-05-30 10:08:50 -07002540 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 public void onActionModeFinished(ActionMode mode) {
2542 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002543 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002544 mActionMode = null;
2545 }
2546
2547 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002548 final Tab tab = getCurrentTab();
2549 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002550 }
2551
2552 // bookmark handling
2553
2554 /**
2555 * add the current page as a bookmark to the given folder id
2556 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002557 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002558 * bookmarked, and if it is, edit that bookmark. If false, and
2559 * the site is already bookmarked, do not attempt to edit the
2560 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002561 */
2562 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002563 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002564 WebView w = getCurrentTopWebView();
2565 if (w == null) {
2566 return null;
2567 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002568 Intent i = new Intent(mActivity,
2569 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002570 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2571 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2572 String touchIconUrl = w.getTouchIconUrl();
2573 if (touchIconUrl != null) {
2574 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2575 WebSettings settings = w.getSettings();
2576 if (settings != null) {
2577 i.putExtra(AddBookmarkPage.USER_AGENT,
2578 settings.getUserAgentString());
2579 }
2580 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002581 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002582 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002583 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002584 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2585 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002586 // Put the dialog at the upper right of the screen, covering the
2587 // star on the title bar.
2588 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002589 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002590 }
2591
2592 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002593 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002594 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002595 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002596 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002597 }
2598
Vivek Sekharb54614f2014-05-01 19:03:37 -07002599 @Override
2600 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2601 boolean capture) {
2602 mUploadHandler = new UploadHandler(this);
2603 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2604 }
2605
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 // thumbnails
2607
2608 /**
2609 * Return the desired width for thumbnail screenshots, which are stored in
2610 * the database, and used on the bookmarks screen.
2611 * @param context Context for finding out the density of the screen.
2612 * @return desired width for thumbnail screenshot.
2613 */
2614 static int getDesiredThumbnailWidth(Context context) {
2615 return context.getResources().getDimensionPixelOffset(
2616 R.dimen.bookmarkThumbnailWidth);
2617 }
2618
2619 /**
2620 * Return the desired height for thumbnail screenshots, which are stored in
2621 * the database, and used on the bookmarks screen.
2622 * @param context Context for finding out the density of the screen.
2623 * @return desired height for thumbnail screenshot.
2624 */
2625 static int getDesiredThumbnailHeight(Context context) {
2626 return context.getResources().getDimensionPixelOffset(
2627 R.dimen.bookmarkThumbnailHeight);
2628 }
2629
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002630 static void createScreenshotAsync(WebView view, int width, int height,
2631 final ValueCallback<Bitmap> cb) {
2632 if (view == null || width == 0 || height == 0) {
2633 return;
2634 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002635 view.getContentBitmapAsync(
2636 (float) width / view.getWidth(),
2637 new Rect(),
2638 new ValueCallback<Bitmap>() {
2639 @Override
2640 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002641 if (bitmap != null)
2642 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2643 cb.onReceiveValue(bitmap);
2644 }});
2645 }
2646
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002647 private void updateScreenshot(final Tab tab) {
2648 createScreenshotAsync(
2649 tab.getWebView(),
2650 getDesiredThumbnailWidth(mActivity),
2651 getDesiredThumbnailHeight(mActivity),
2652 new ValueCallback<Bitmap>() {
2653 @Override
2654 public void onReceiveValue(Bitmap bitmap) {
2655 updateScreenshot(tab, bitmap);
2656 }
2657 });
2658 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002659
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002660 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002661 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002662 // FIXME: Would like to make sure there is actually something to
2663 // draw, but the API for that (WebViewCore.pictureReady()) is not
2664 // currently accessible here.
2665
John Reck34ef2672011-02-10 11:30:55 -08002666 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002667 if (view == null) {
2668 // Tab was destroyed
2669 return;
2670 }
John Reck34ef2672011-02-10 11:30:55 -08002671 final String url = tab.getUrl();
2672 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002673 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002674 if (TextUtils.isEmpty(url)) {
2675 return;
2676 }
2677
kaiyiz6e5b3e02013-08-19 20:02:01 +08002678 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002679 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002680 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002681 }
John Reck34ef2672011-02-10 11:30:55 -08002682 // Only update thumbnails for web urls (http(s)://), not for
2683 // about:, javascript:, data:, etc...
2684 // Unless it is a bookmarked site, then always update
2685 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2686 return;
2687 }
2688
kaiyiz6e5b3e02013-08-19 20:02:01 +08002689 if (url != null && mUpdateMyNavThumbnailUrl != null
2690 && Patterns.WEB_URL.matcher(url).matches()
2691 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2692 String urlHost = (new WebAddress(url)).getHost();
2693 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2694 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2695 || bookmarkHost.length() == 0) {
2696 return;
2697 }
2698 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2699 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2700 bookmarkHost.length());
2701 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2702 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2703 if (!bookmarkDomain.equals(urlDomain)) {
2704 return;
2705 }
2706 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002707 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002708 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2709 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2710 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2711 500);
2712 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002713 return;
2714 }
2715
2716 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002717 new AsyncTask<Void, Void, Void>() {
2718 @Override
2719 protected Void doInBackground(Void... unused) {
2720 Cursor cursor = null;
2721 try {
2722 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002723 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2724 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2725 : url);
2726 if (mUpdateMyNavThumbnail) {
2727 mUpdateMyNavThumbnail = false;
2728 mUpdateMyNavThumbnailUrl = null;
2729 }
John Reck34ef2672011-02-10 11:30:55 -08002730 if (cursor != null && cursor.moveToFirst()) {
2731 final ByteArrayOutputStream os =
2732 new ByteArrayOutputStream();
2733 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002734
John Reck34ef2672011-02-10 11:30:55 -08002735 ContentValues values = new ContentValues();
2736 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002737
John Reck34ef2672011-02-10 11:30:55 -08002738 do {
John Reck617fd832011-02-16 14:35:59 -08002739 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002740 cr.update(Images.CONTENT_URI, values, null, null);
2741 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002742 }
John Reck34ef2672011-02-10 11:30:55 -08002743 } catch (IllegalStateException e) {
2744 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002745 } catch (SQLiteException s) {
2746 // Added for possible error when user tries to remove the same bookmark
2747 // that is being updated with a screen shot
2748 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002749 } finally {
2750 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002751 }
John Reck34ef2672011-02-10 11:30:55 -08002752 return null;
2753 }
2754 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002755 }
2756
2757 private class Copy implements OnMenuItemClickListener {
2758 private CharSequence mText;
2759
John Reck9c35b9c2012-05-30 10:08:50 -07002760 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002761 public boolean onMenuItemClick(MenuItem item) {
2762 copy(mText);
2763 return true;
2764 }
2765
2766 public Copy(CharSequence toCopy) {
2767 mText = toCopy;
2768 }
2769 }
2770
Leon Scroggins63c02662010-11-18 15:16:27 -05002771 private static class Download implements OnMenuItemClickListener {
2772 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002773 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002774 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002775 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002776 private static final String FALLBACK_EXTENSION = "dat";
2777 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002778
John Reck9c35b9c2012-05-30 10:08:50 -07002779 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002780 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002781 if (DataUri.isDataUri(mText)) {
2782 saveDataUri();
2783 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002784 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002785 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002786 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002787 return true;
2788 }
2789
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002790 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2791 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002792 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002793 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002794 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002795 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002796 }
George Mount387d45d2011-10-07 15:57:53 -07002797
2798 /**
2799 * Treats mText as a data URI and writes its contents to a file
2800 * based on the current time.
2801 */
2802 private void saveDataUri() {
2803 FileOutputStream outputStream = null;
2804 try {
2805 DataUri uri = new DataUri(mText);
2806 File target = getTarget(uri);
2807 outputStream = new FileOutputStream(target);
2808 outputStream.write(uri.getData());
2809 final DownloadManager manager =
2810 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2811 manager.addCompletedDownload(target.getName(),
2812 mActivity.getTitle().toString(), false,
2813 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002814 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002815 } catch (IOException e) {
2816 Log.e(LOGTAG, "Could not save data URL");
2817 } finally {
2818 if (outputStream != null) {
2819 try {
2820 outputStream.close();
2821 } catch (IOException e) {
2822 // ignore close errors
2823 }
2824 }
2825 }
2826 }
2827
2828 /**
2829 * Creates a File based on the current time stamp and uses
2830 * the mime type of the DataUri to get the extension.
2831 */
2832 private File getTarget(DataUri uri) throws IOException {
2833 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002834 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002835 String nameBase = format.format(new Date());
2836 String mimeType = uri.getMimeType();
2837 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2838 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2839 if (extension == null) {
2840 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2841 extension = FALLBACK_EXTENSION;
2842 }
2843 extension = "." + extension; // createTempFile needs the '.'
2844 File targetFile = File.createTempFile(nameBase, extension, dir);
2845 return targetFile;
2846 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002847 }
2848
Cary Clark8974d282010-11-22 10:46:05 -05002849 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002850 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002851
John Reck9c35b9c2012-05-30 10:08:50 -07002852 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002853 public boolean onMenuItemClick(MenuItem item) {
2854 if (mWebView != null) {
2855 return mWebView.selectText();
2856 }
2857 return false;
2858 }
2859
2860 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002861 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002862 }
2863
2864 }
2865
Michael Kolb8233fac2010-10-26 16:08:53 -07002866 /********************** TODO: UI stuff *****************************/
2867
2868 // these methods have been copied, they still need to be cleaned up
2869
2870 /****************** tabs ***************************************************/
2871
2872 // basic tab interactions:
2873
2874 // it is assumed that tabcontrol already knows about the tab
2875 protected void addTab(Tab tab) {
2876 mUi.addTab(tab);
2877 }
2878
2879 protected void removeTab(Tab tab) {
2880 mUi.removeTab(tab);
2881 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002882 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002883 }
2884
Michael Kolbf2055602011-04-09 17:20:03 -07002885 @Override
2886 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002887 // monkey protection against delayed start
2888 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002889
2890 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2891 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002892 mTabControl.setCurrentTab(tab);
2893 // the tab is guaranteed to have a webview after setCurrentTab
2894 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002895
2896
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002897 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002898 //Purge active tabs
2899 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002900 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002901 }
2902
John Reck8bcafc12011-08-29 16:43:02 -07002903 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002904 Tab current = mTabControl.getCurrentTab();
2905 if (current != null
2906 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002907 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002908 }
2909 }
2910
John Reck26b18322011-06-21 13:08:58 -07002911 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002912 //Cancel navscreen request
2913 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002914 // Dismiss the subwindow if applicable.
2915 dismissSubWindow(appTab);
2916 // Since we might kill the WebView, remove it from the
2917 // content view first.
2918 mUi.detachTab(appTab);
2919 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002920 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002921 // TODO: analyze why the remove and add are necessary
2922 mUi.attachTab(appTab);
2923 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002924 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002925 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002926 } else {
2927 // If the tab was the current tab, we have to attach
2928 // it to the view system again.
2929 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002930 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002931 }
2932 }
2933
2934 // Remove the sub window if it exists. Also called by TabControl when the
2935 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002936 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002937 public void dismissSubWindow(Tab tab) {
2938 removeSubWindow(tab);
2939 // dismiss the subwindow. This will destroy the WebView.
2940 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002941 WebView wv = getCurrentTopWebView();
2942 if (wv != null) {
2943 wv.requestFocus();
2944 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002945 }
2946
2947 @Override
2948 public void removeSubWindow(Tab t) {
2949 if (t.getSubWebView() != null) {
2950 mUi.removeSubWindow(t.getSubViewContainer());
2951 }
2952 }
2953
2954 @Override
2955 public void attachSubWindow(Tab tab) {
2956 if (tab.getSubWebView() != null) {
2957 mUi.attachSubWindow(tab.getSubViewContainer());
2958 getCurrentTopWebView().requestFocus();
2959 }
2960 }
2961
Mathew Inwood29721c22011-06-29 17:55:24 +01002962 private Tab showPreloadedTab(final UrlData urlData) {
2963 if (!urlData.isPreloaded()) {
2964 return null;
2965 }
2966 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2967 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2968 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002969 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002970 // Could not submit query. Fallback to regular tab creation
2971 tabControl.destroy();
2972 return null;
2973 }
2974 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002975 // check tab count and make room for new tab
2976 if (!mTabControl.canCreateNewTab()) {
2977 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2978 if (leastUsed != null) {
2979 closeTab(leastUsed);
2980 }
2981 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002982 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002983 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002984 mTabControl.addPreloadedTab(t);
2985 addTab(t);
2986 setActiveTab(t);
2987 return t;
2988 }
2989
Michael Kolb7bcafde2011-05-09 13:55:59 -07002990 // open a non inconito tab with the given url data
2991 // and set as active tab
2992 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002993 Tab tab = showPreloadedTab(urlData);
2994 if (tab == null) {
2995 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002996 if ((tab != null) && !urlData.isEmpty()) {
2997 loadUrlDataIn(tab, urlData);
2998 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002999 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003000 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07003001 }
3002
Michael Kolb843510f2010-12-09 10:51:49 -08003003 @Override
3004 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003005 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07003006 }
3007
Michael Kolb8233fac2010-10-26 16:08:53 -07003008 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07003009 public Tab openIncognitoTab() {
3010 return openTab(INCOGNITO_URI, true, true, false);
3011 }
3012
3013 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003014 public Tab openTab(String url, boolean incognito, boolean setActive,
3015 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003016 return openTab(url, incognito, setActive, useCurrent, null);
3017 }
3018
3019 @Override
3020 public Tab openTab(String url, Tab parent, boolean setActive,
3021 boolean useCurrent) {
3022 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3023 setActive, useCurrent, parent);
3024 }
3025
3026 public Tab openTab(String url, boolean incognito, boolean setActive,
3027 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003028 Tab tab = createNewTab(incognito, setActive, useCurrent);
3029 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003030 if (parent instanceof SnapshotTab) {
3031 addTab(tab);
3032 if (setActive)
3033 setActiveTab(tab);
3034 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003035 parent.addChildTab(tab);
3036 }
Michael Kolb519d2282011-05-09 17:03:19 -07003037 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003038 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003039 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003040 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003041 return tab;
3042 }
3043
3044 // this method will attempt to create a new tab
3045 // incognito: private browsing tab
3046 // setActive: ste tab as current tab
3047 // useCurrent: if no new tab can be created, return current tab
3048 private Tab createNewTab(boolean incognito, boolean setActive,
3049 boolean useCurrent) {
3050 Tab tab = null;
3051 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05003052 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003053 addTab(tab);
3054 if (setActive) {
3055 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07003056 } else {
3057 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003058 }
3059 } else {
3060 if (useCurrent) {
3061 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07003062 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003063 } else {
3064 mUi.showMaxTabsWarning();
3065 }
3066 }
3067 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003068 }
3069
John Reck2bc80422011-06-30 15:11:49 -07003070 @Override
3071 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3072 SnapshotTab tab = null;
3073 if (mTabControl.canCreateNewTab()) {
3074 tab = mTabControl.createSnapshotTab(snapshotId);
3075 addTab(tab);
3076 if (setActive) {
3077 setActiveTab(tab);
3078 }
3079 } else {
3080 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003081 }
3082 return tab;
3083 }
3084
Michael Kolb8233fac2010-10-26 16:08:53 -07003085 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003086 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003087 * @return boolean True if we successfully switched to a different tab. If
3088 * the indexth tab is null, or if that tab is the same as
3089 * the current one, return false.
3090 */
3091 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003092 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003093 Tab currentTab = mTabControl.getCurrentTab();
3094 if (tab == null || tab == currentTab) {
3095 return false;
3096 }
3097 setActiveTab(tab);
3098 return true;
3099 }
3100
3101 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003102 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003103 closeCurrentTab(false);
3104 }
3105
3106 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003107 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003108 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003109 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003110 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003111 return;
3112 }
Michael Kolbc831b632011-05-11 09:30:34 -07003113 final Tab current = mTabControl.getCurrentTab();
3114 final int pos = mTabControl.getCurrentPosition();
3115 Tab newTab = current.getParent();
3116 if (newTab == null) {
3117 newTab = mTabControl.getTab(pos + 1);
3118 if (newTab == null) {
3119 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003120 }
3121 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003122 if (andQuit) {
3123 mTabControl.setCurrentTab(newTab);
3124 closeTab(current);
3125 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003126 // Close window
3127 closeTab(current);
3128 }
3129 }
3130
3131 /**
3132 * Close the tab, remove its associated title bar, and adjust mTabControl's
3133 * current tab to a valid value.
3134 */
3135 @Override
3136 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003137 if (tab == mTabControl.getCurrentTab()) {
3138 closeCurrentTab();
3139 } else {
3140 removeTab(tab);
3141 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003142 }
3143
Afzal Najamd4e33312012-04-26 01:54:01 -04003144 /**
3145 * Close all tabs except the current one
3146 */
3147 @Override
3148 public void closeOtherTabs() {
3149 int inactiveTabs = mTabControl.getTabCount() - 1;
3150 for (int i = inactiveTabs; i >= 0; i--) {
3151 Tab tab = mTabControl.getTab(i);
3152 if (tab != mTabControl.getCurrentTab()) {
3153 removeTab(tab);
3154 }
3155 }
3156 }
3157
Michael Kolb8233fac2010-10-26 16:08:53 -07003158 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003159 protected void loadUrlFromContext(String url) {
3160 Tab tab = getCurrentTab();
3161 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003162 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003163 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003164 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003165 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003166 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003167 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003168 }
3169 }
3170 }
3171
3172 /**
3173 * Load the URL into the given WebView and update the title bar
3174 * to reflect the new load. Call this instead of WebView.loadUrl
3175 * directly.
3176 * @param view The WebView used to load url.
3177 * @param url The URL to load.
3178 */
John Reck71e51422011-07-01 16:49:28 -07003179 @Override
3180 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003181 loadUrl(tab, url, null);
3182 }
3183
3184 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3185 if (tab != null) {
3186 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003187 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003188 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003189 if (tab.hasCrashed) {
3190 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3191 }
Michael Kolba53c9892011-10-05 13:31:40 -07003192 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003193 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003194 }
3195
3196 /**
3197 * Load UrlData into a Tab and update the title bar to reflect the new
3198 * load. Call this instead of UrlData.loadIn directly.
3199 * @param t The Tab used to load.
3200 * @param data The UrlData being loaded.
3201 */
3202 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003203 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003204 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003205 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003206 } else {
John Reck38b39652012-06-05 09:22:59 -07003207 if (t != null && data.mDisableUrlOverride) {
3208 t.disableUrlOverridingForLoad();
3209 }
John Reck26b18322011-06-21 13:08:58 -07003210 loadUrl(t, data.mUrl, data.mHeaders);
3211 }
3212 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003213 }
3214
John Reck30c714c2010-12-16 17:30:34 -08003215 @Override
3216 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003217 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003218 //In case of tab can go back (aka tab has navigation entry) do nothing
3219 //else just load homepage in current tab.
3220 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003221 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003222 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003223 }
3224
3225 void goBackOnePageOrQuit() {
3226 Tab current = mTabControl.getCurrentTab();
3227 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003228 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3229 showExitDialog(mActivity);
3230 } else {
3231 /*
3232 * Instead of finishing the activity, simply push this to the back
3233 * of the stack and let ActivityManager to choose the foreground
3234 * activity. As BrowserActivity is singleTask, it will be always the
3235 * root of the task. So we can use either true or false for
3236 * moveTaskToBack().
3237 */
3238 mActivity.moveTaskToBack(true);
3239 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003240 return;
3241 }
John Reckef654f12011-07-12 16:42:08 -07003242 if (current.canGoBack()) {
3243 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003244 } else {
3245 // Check to see if we are closing a window that was created by
3246 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003247 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003248 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003249 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003250 // Now we close the other tab
3251 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003252 } else if (BrowserConfig.getInstance(getContext())
3253 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3254 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003255 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003256 if ((current.getAppId() != null) || current.closeOnBack()) {
3257 closeCurrentTab(true);
3258 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003259 /*
3260 * Instead of finishing the activity, simply push this to the back
3261 * of the stack and let ActivityManager to choose the foreground
3262 * activity. As BrowserActivity is singleTask, it will be always the
3263 * root of the task. So we can use either true or false for
3264 * moveTaskToBack().
3265 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003266 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003267 }
3268 }
3269 }
3270
3271 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003272 * helper method for key handler
3273 * returns the current tab if it can't advance
3274 */
Michael Kolbc831b632011-05-11 09:30:34 -07003275 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003276 int pos = mTabControl.getCurrentPosition() + 1;
3277 if (pos >= mTabControl.getTabCount()) {
3278 pos = 0;
3279 }
3280 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003281 }
3282
3283 /**
3284 * helper method for key handler
3285 * returns the current tab if it can't advance
3286 */
Michael Kolbc831b632011-05-11 09:30:34 -07003287 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003288 int pos = mTabControl.getCurrentPosition() - 1;
3289 if ( pos < 0) {
3290 pos = mTabControl.getTabCount() - 1;
3291 }
3292 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003293 }
3294
John Reckbcef87f2012-02-03 14:58:34 -08003295 boolean isMenuOrCtrlKey(int keyCode) {
3296 return (KeyEvent.KEYCODE_MENU == keyCode)
3297 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3298 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3299 }
3300
Michael Kolb0035fad2011-03-14 13:25:28 -07003301 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003302 * handle key events in browser
3303 *
3304 * @param keyCode
3305 * @param event
3306 * @return true if handled, false to pass to super
3307 */
John Reck9c35b9c2012-05-30 10:08:50 -07003308 @Override
3309 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003310 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3311 // Hardware menu key
Enrico Ros1f5a0952014-11-18 20:15:48 -08003312 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
Pankaj Garg49b79252014-11-07 17:33:41 -08003313 true, false);
3314 return true;
3315 }
3316
Cary Clark160bbb92011-01-10 11:17:07 -05003317 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003318 // Even if MENU is already held down, we need to call to super to open
3319 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003320 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003321 mMenuIsDown = true;
3322 return false;
3323 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003324
Cary Clark8ff8c662010-12-29 15:03:05 -05003325 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003326 Tab tab = getCurrentTab();
3327 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003328
Cary Clark160bbb92011-01-10 11:17:07 -05003329 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3330 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003331
Michael Kolb8233fac2010-10-26 16:08:53 -07003332 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003333 case KeyEvent.KEYCODE_TAB:
3334 if (event.isCtrlPressed()) {
3335 if (event.isShiftPressed()) {
3336 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003337 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003338 } else {
3339 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003340 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003341 }
3342 return true;
3343 }
3344 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003345 case KeyEvent.KEYCODE_SPACE:
3346 // WebView/WebTextView handle the keys in the KeyDown. As
3347 // the Activity's shortcut keys are only handled when WebView
3348 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003349 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003350 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003351 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003352 pageDown();
3353 }
3354 return true;
3355 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003356 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003357 event.startTracking();
3358 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003359 case KeyEvent.KEYCODE_FORWARD:
3360 if (!noModifiers) break;
3361 tab.goForward();
3362 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003363 case KeyEvent.KEYCODE_DPAD_LEFT:
3364 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003365 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003366 return true;
3367 }
3368 break;
3369 case KeyEvent.KEYCODE_DPAD_RIGHT:
3370 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003371 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003372 return true;
3373 }
3374 break;
3375 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003376 if (ctrl) {
3377 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003378 return true;
3379 }
3380 break;
Michael Kolba4183062011-01-16 10:43:21 -08003381// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003382 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003383 if (ctrl ) {
3384 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003385 return true;
3386 }
3387 break;
Michael Kolba4183062011-01-16 10:43:21 -08003388// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003389// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003390// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003391// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003392// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003393// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003394// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003395// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003396// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003397// case KeyEvent.KEYCODE_M: // unused
3398// case KeyEvent.KEYCODE_N: // in Chrome: new window
3399// case KeyEvent.KEYCODE_O: // in Chrome: open file
3400// case KeyEvent.KEYCODE_P: // in Chrome: print page
3401// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003402// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003403// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3404 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003405 // we can't use the ctrl/shift flags, they check for
3406 // exclusive use of a modifier
3407 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003408 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003409 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003410 } else {
3411 openTabToHomePage();
3412 }
3413 return true;
3414 }
3415 break;
3416// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3417// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003418// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003419// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3420// case KeyEvent.KEYCODE_Y: // unused
3421// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003422 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003423 // it is a regular key and webview is not null
3424 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003425 }
3426
John Reck9c35b9c2012-05-30 10:08:50 -07003427 @Override
3428 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003429 switch(keyCode) {
3430 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003431 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003432 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003433 return true;
3434 }
3435 break;
3436 }
3437 return false;
3438 }
3439
John Reck9c35b9c2012-05-30 10:08:50 -07003440 @Override
3441 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003442 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003443 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003444 if (KeyEvent.KEYCODE_MENU == keyCode
3445 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003446 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003447 }
3448 }
Cary Clark160bbb92011-01-10 11:17:07 -05003449 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003450 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003451 case KeyEvent.KEYCODE_BACK:
3452 if (event.isTracking() && !event.isCanceled()) {
3453 onBackKey();
3454 return true;
3455 }
3456 break;
3457 }
3458 return false;
3459 }
3460
3461 public boolean isMenuDown() {
3462 return mMenuIsDown;
3463 }
3464
John Reck9c35b9c2012-05-30 10:08:50 -07003465 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003466 public void setupAutoFill(Message message) {
3467 // Open the settings activity at the AutoFill profile fragment so that
3468 // the user can create a new profile. When they return, we will dispatch
3469 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003470 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003471 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3472 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003473 }
John Reckb3417f02011-01-14 11:01:05 -08003474
John Reck9c35b9c2012-05-30 10:08:50 -07003475 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003476 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003477 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003478 return true;
3479 }
3480
John Reck1cf4b792011-07-26 10:22:22 -07003481 @Override
3482 public boolean shouldCaptureThumbnails() {
3483 return mUi.shouldCaptureThumbnails();
3484 }
3485
Michael Kolbc3af0672011-08-09 10:24:41 -07003486 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003487 public boolean supportsVoice() {
3488 PackageManager pm = mActivity.getPackageManager();
3489 List activities = pm.queryIntentActivities(new Intent(
3490 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3491 return activities.size() != 0;
3492 }
3493
3494 @Override
3495 public void startVoiceRecognizer() {
3496 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003497 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003498 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3499 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3500 mActivity.startActivityForResult(voice, VOICE_RESULT);
3501 }
3502
Pankaj Garg7b279f62014-08-12 14:47:18 -07003503 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003504 if (mLevel == level)
3505 return;
3506 mLevel = level;
3507 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003508 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3509 lp.dimAmount = level;
3510 mActivity.getWindow().setAttributes(lp);
3511 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3512 } else {
3513 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3514 }
3515 }
3516
Michael Kolb0b129122012-06-04 16:31:58 -07003517 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003518 public void setBlockEvents(boolean block) {
3519 mBlockEvents = block;
3520 }
3521
John Reck9c35b9c2012-05-30 10:08:50 -07003522 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003523 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003524 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003525 }
3526
John Reck9c35b9c2012-05-30 10:08:50 -07003527 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003528 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003529 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003530 }
3531
John Reck9c35b9c2012-05-30 10:08:50 -07003532 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003533 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003534 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003535 }
3536
John Reck9c35b9c2012-05-30 10:08:50 -07003537 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003538 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003539 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003540 }
3541
John Reck9c35b9c2012-05-30 10:08:50 -07003542 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003543 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003544 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003545 }
3546
Pankaj Garg49b79252014-11-07 17:33:41 -08003547 @Override
3548 public boolean shouldShowAppMenu() {
3549 return true;
3550 }
3551
3552 @Override
3553 public int getMenuThemeResourceId() {
3554 return R.style.OverflowMenuTheme;
3555 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003556}