blob: 80a5a424032fe2e18f16d1ee2cffb3fe8b9de5bc [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;
Pankaj Garg49b79252014-11-07 17:33:41 -08001974
1975 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());
Michael Kolb4bf79712011-07-14 14:18:12 -07001983 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001984
1985 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1986 forward.setEnabled(canGoForward);
1987
Michael Kolb4bf79712011-07-14 14:18:12 -07001988 // decide whether to show the share link option
1989 PackageManager pm = mActivity.getPackageManager();
1990 Intent send = new Intent(Intent.ACTION_SEND);
1991 send.setType("text/plain");
1992 ResolveInfo ri = pm.resolveActivity(send,
1993 PackageManager.MATCH_DEFAULT_ONLY);
1994 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1995
1996 boolean isNavDump = mSettings.enableNavDump();
1997 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1998 nav.setVisible(isNavDump);
1999 nav.setEnabled(isNavDump);
2000
2001 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002002 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2003 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002004 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002005 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2006 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002007 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002008 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2009 isLive && isLiveScheme && isPageFinished);
2010 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
2011 isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002012 // history and snapshots item are the members of COMBO menu group,
2013 // so if show history item, only make snapshots item invisible.
2014 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002015
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002016 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002017 }
2018
John Reck9c35b9c2012-05-30 10:08:50 -07002019 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002020 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002021 if (null == getCurrentTopWebView()) {
2022 return false;
2023 }
2024 if (mMenuIsDown) {
2025 // The shortcut action consumes the MENU. Even if it is still down,
2026 // it won't trigger the next shortcut action. In the case of the
2027 // shortcut action triggering a new activity, like Bookmarks, we
2028 // won't get onKeyUp for MENU. So it is important to reset it here.
2029 mMenuIsDown = false;
2030 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002031 if (mUi.onOptionsItemSelected(item)) {
2032 // ui callback handled it
2033 return true;
2034 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002035 switch (item.getItemId()) {
2036 // -- Main menu
2037 case R.id.new_tab_menu_id:
2038 openTabToHomePage();
2039 break;
2040
2041 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002042 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002043 break;
2044
2045 case R.id.goto_menu_id:
2046 editUrl();
2047 break;
2048
2049 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002050 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2051 break;
2052
2053 case R.id.history_menu_id:
2054 bookmarksOrHistoryPicker(ComboViews.History);
2055 break;
2056
2057 case R.id.snapshots_menu_id:
2058 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002059 break;
2060
Pankaj Garg49b79252014-11-07 17:33:41 -08002061 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002062 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002063 break;
2064
2065 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002066 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 stopLoading();
2068 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002069 Tab currentTab = mTabControl.getCurrentTab();
2070 if (currentTab.hasCrashed) {
2071 currentTab.replaceCrashView(getCurrentTopWebView(),
2072 currentTab.getViewContainer());
2073 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002074 getCurrentTopWebView().reload();
2075 }
2076 break;
2077
Michael Kolb8233fac2010-10-26 16:08:53 -07002078 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002079 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 break;
2081
2082 case R.id.close_menu_id:
2083 // Close the subwindow if it exists.
2084 if (mTabControl.getCurrentSubWindow() != null) {
2085 dismissSubWindow(mTabControl.getCurrentTab());
2086 break;
2087 }
2088 closeCurrentTab();
2089 break;
2090
kaiyiza8b6dbb2013-07-29 18:11:22 +08002091 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002092 Object[] params = { new String("persist.debug.browsermonkeytest")};
2093 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002094 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002095 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002096 if (ret != null && ret.equals("enable"))
2097 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002098 if (BrowserConfig.getInstance(getContext())
2099 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2100 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002101 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002102 case R.id.homepage_menu_id:
2103 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002104 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002105 break;
2106
2107 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002108 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002109 break;
2110
2111 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002112 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002113 break;
2114
John Reck2bc80422011-06-30 15:11:49 -07002115 case R.id.save_snapshot_menu_id:
2116 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002117 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002118 createScreenshotAsync(
2119 source.getWebView(),
2120 getDesiredThumbnailWidth(mActivity),
2121 getDesiredThumbnailHeight(mActivity),
2122 new ValueCallback<Bitmap>() {
2123 @Override
2124 public void onReceiveValue(Bitmap bitmap) {
2125 new SaveSnapshotTask(source, bitmap).execute();
2126 }
2127 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002128 break;
2129
Michael Kolb8233fac2010-10-26 16:08:53 -07002130 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002131 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002132 break;
2133
John Recke1a03a32011-09-14 17:04:16 -07002134 case R.id.snapshot_go_live:
2135 goLive();
2136 return true;
2137
Michael Kolb8233fac2010-10-26 16:08:53 -07002138 case R.id.share_page_menu_id:
2139 Tab currentTab = mTabControl.getCurrentTab();
2140 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002141 return false;
2142 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002143 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002144 break;
2145
2146 case R.id.dump_nav_menu_id:
2147 getCurrentTopWebView().debugDump();
2148 break;
2149
Michael Kolb8233fac2010-10-26 16:08:53 -07002150 case R.id.zoom_in_menu_id:
2151 getCurrentTopWebView().zoomIn();
2152 break;
2153
2154 case R.id.zoom_out_menu_id:
2155 getCurrentTopWebView().zoomOut();
2156 break;
2157
2158 case R.id.view_downloads_menu_id:
2159 viewDownloads();
2160 break;
2161
John Reck42229bc2011-08-19 13:26:43 -07002162 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002163 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002164 break;
2165
Michael Kolb8233fac2010-10-26 16:08:53 -07002166 case R.id.window_one_menu_id:
2167 case R.id.window_two_menu_id:
2168 case R.id.window_three_menu_id:
2169 case R.id.window_four_menu_id:
2170 case R.id.window_five_menu_id:
2171 case R.id.window_six_menu_id:
2172 case R.id.window_seven_menu_id:
2173 case R.id.window_eight_menu_id:
2174 {
2175 int menuid = item.getItemId();
2176 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2177 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2178 Tab desiredTab = mTabControl.getTab(id);
2179 if (desiredTab != null &&
2180 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002181 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002182 }
2183 break;
2184 }
2185 }
2186 }
2187 break;
2188
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002189 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002190 Bundle bundle = new Bundle();
2191 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2192 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2193 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2194 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2195 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002196 break;
2197
Pankaj Garg49b79252014-11-07 17:33:41 -08002198 case R.id.add_to_homescreen:
2199 final WebView w = getCurrentTopWebView();
2200 final EditText input = new EditText(getContext());
2201 input.setText(w.getTitle());
2202 new AlertDialog.Builder(getContext())
2203 .setTitle("Add to homescreen")
2204 .setMessage("Title")
2205 .setView(input)
2206 .setPositiveButton("Add", new DialogInterface.OnClickListener() {
2207 public void onClick(DialogInterface dialog, int whichButton) {
2208 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2209 getContext(),
2210 w.getUrl(),
2211 input.getText().toString(),
2212 w.getViewportBitmap(),
2213 w.getFavicon()));
2214
2215 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2216 .addCategory(Intent.CATEGORY_HOME));
2217 }})
2218 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
2219 public void onClick(DialogInterface dialog, int whichButton) {
2220 // Do nothing.
2221 }
2222 })
2223 .show();
2224 break;
2225
Michael Kolb8233fac2010-10-26 16:08:53 -07002226 default:
2227 return false;
2228 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002229 return true;
2230 }
2231
John Reck68234a92012-04-19 15:27:12 -07002232 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2233 implements OnCancelListener {
2234
2235 private Tab mTab;
2236 private Dialog mProgressDialog;
2237 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002238 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002239
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002240 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002241 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002242 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002243 }
2244
2245 @Override
2246 protected void onPreExecute() {
2247 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2248 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2249 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002250 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002251 }
2252
2253 @Override
2254 protected Long doInBackground(Void... params) {
2255 if (!mTab.saveViewState(mValues)) {
2256 return null;
2257 }
2258 if (isCancelled()) {
2259 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2260 File file = mActivity.getFileStreamPath(path);
2261 if (!file.delete()) {
2262 file.deleteOnExit();
2263 }
2264 return null;
2265 }
2266 final ContentResolver cr = mActivity.getContentResolver();
2267 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2268 if (result == null) {
2269 return null;
2270 }
2271 long id = ContentUris.parseId(result);
2272 return id;
2273 }
2274
2275 @Override
2276 protected void onPostExecute(Long id) {
2277 if (isCancelled()) {
2278 return;
2279 }
2280 mProgressDialog.dismiss();
2281 if (id == null) {
2282 Toast.makeText(mActivity, R.string.snapshot_failed,
2283 Toast.LENGTH_SHORT).show();
2284 return;
2285 }
2286 Bundle b = new Bundle();
2287 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2288 mUi.showComboView(ComboViews.Snapshots, b);
2289 }
2290
2291 @Override
2292 public void onCancel(DialogInterface dialog) {
2293 cancel(true);
2294 }
2295 }
2296
Michael Kolb80f75082012-04-10 10:50:06 -07002297 @Override
2298 public void toggleUserAgent() {
2299 WebView web = getCurrentWebView();
2300 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002301 }
2302
2303 @Override
2304 public void findOnPage() {
2305 getCurrentTopWebView().showFindDialog(null, true);
2306 }
2307
2308 @Override
2309 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002310 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002311 }
2312
Pankaj Gargeba076f2015-03-25 13:40:59 -07002313 // This function is specifically used from AddBookmark Activity.
2314 // The bookmark activity clears the bitmap after retrieving it.
2315 // The function usage elsewhere will result in breaking bookmark
2316 // functionality.
2317 public static Bitmap getAndReleaseLastBookmarkBitmapFromIntent() {
2318 Bitmap bitmap = mBookmarkBitmap;
2319 mBookmarkBitmap = null;
2320 return bitmap;
2321 }
2322
Michael Kolb80f75082012-04-10 10:50:06 -07002323 @Override
2324 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002325 WebView w = getCurrentTopWebView();
2326 if (w == null)
2327 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002328 final Intent i = createBookmarkCurrentPageIntent(false);
2329 createScreenshotAsync(
2330 w, getDesiredThumbnailWidth(mActivity),
2331 getDesiredThumbnailHeight(mActivity),
2332 new ValueCallback<Bitmap>() {
2333 @Override
2334 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargeba076f2015-03-25 13:40:59 -07002335 mBookmarkBitmap = bitmap;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002336 mActivity.startActivity(i);
2337 }
2338 });
Michael Kolb80f75082012-04-10 10:50:06 -07002339 }
2340
John Recke1a03a32011-09-14 17:04:16 -07002341 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002342 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002343 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002344 boolean onlySingleTabRemaining = false;
2345 if (mTabControl.getTabCount() > 1) {
2346 // destroy the old snapshot tab
2347 closeCurrentTab();
2348 } else {
2349 onlySingleTabRemaining = true;
2350 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002351 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002352 if (onlySingleTabRemaining) {
2353 closeTab(t);
2354 }
2355
Tarun Nainani8eb00912014-07-17 12:28:32 -07002356 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002357 }
2358
luxiaolb40014b2013-07-19 10:01:43 +08002359 private void showExitDialog(final Activity activity) {
2360 new AlertDialog.Builder(activity)
2361 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002362 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002363 .setMessage(R.string.exit_browser_msg)
2364 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2365 public void onClick(DialogInterface dialog, int which) {
2366 activity.moveTaskToBack(true);
2367 dialog.dismiss();
2368 }
2369 })
2370 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2371 public void onClick(DialogInterface dialog, int which) {
2372 activity.finish();
2373 mHandler.postDelayed(new Runnable() {
2374 @Override
2375 public void run() {
luxiaolb40014b2013-07-19 10:01:43 +08002376 mCrashRecoveryHandler.clearState(true);
2377 int pid = android.os.Process.myPid();
2378 android.os.Process.killProcess(pid);
2379 }
2380 }, 300);
2381 dialog.dismiss();
2382 }
2383 })
2384 .show();
2385 }
Michael Kolb315d5022011-10-13 12:47:11 -07002386 @Override
2387 public void showPageInfo() {
2388 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2389 }
2390
John Reck9c35b9c2012-05-30 10:08:50 -07002391 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002392 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002393 // Let the History and Bookmark fragments handle menus they created.
2394 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2395 return false;
2396 }
2397
Michael Kolb8233fac2010-10-26 16:08:53 -07002398 int id = item.getItemId();
2399 boolean result = true;
2400 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002401 // -- Browser context menu
2402 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002403 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002404 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002405 case R.id.copy_link_context_menu_id:
2406 final WebView webView = getCurrentTopWebView();
2407 if (null == webView) {
2408 result = false;
2409 break;
2410 }
2411 final HashMap<String, WebView> hrefMap =
2412 new HashMap<String, WebView>();
2413 hrefMap.put("webview", webView);
2414 final Message msg = mHandler.obtainMessage(
2415 FOCUS_NODE_HREF, id, 0, hrefMap);
2416 webView.requestFocusNodeHref(msg);
2417 break;
2418
2419 default:
2420 // For other context menus
2421 result = onOptionsItemSelected(item);
2422 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002423 return result;
2424 }
2425
2426 /**
2427 * support programmatically opening the context menu
2428 */
2429 public void openContextMenu(View view) {
2430 mActivity.openContextMenu(view);
2431 }
2432
2433 /**
2434 * programmatically open the options menu
2435 */
2436 public void openOptionsMenu() {
2437 mActivity.openOptionsMenu();
2438 }
2439
John Reck9c35b9c2012-05-30 10:08:50 -07002440 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002441 public boolean onMenuOpened(int featureId, Menu menu) {
2442 if (mOptionsMenuOpen) {
2443 if (mConfigChanged) {
2444 // We do not need to make any changes to the state of the
2445 // title bar, since the only thing that happened was a
2446 // change in orientation
2447 mConfigChanged = false;
2448 } else {
2449 if (!mExtendedMenuOpen) {
2450 mExtendedMenuOpen = true;
2451 mUi.onExtendedMenuOpened();
2452 } else {
2453 // Switching the menu back to icon view, so show the
2454 // title bar once again.
2455 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002456 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002457 }
2458 }
2459 } else {
2460 // The options menu is closed, so open it, and show the title
2461 mOptionsMenuOpen = true;
2462 mConfigChanged = false;
2463 mExtendedMenuOpen = false;
2464 mUi.onOptionsMenuOpened();
2465 }
2466 return true;
2467 }
2468
John Reck9c35b9c2012-05-30 10:08:50 -07002469 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002470 public void onOptionsMenuClosed(Menu menu) {
2471 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002472 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002473 }
2474
John Reck9c35b9c2012-05-30 10:08:50 -07002475 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002476 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002477 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002478 }
2479
2480 // Helper method for getting the top window.
2481 @Override
2482 public WebView getCurrentTopWebView() {
2483 return mTabControl.getCurrentTopWebView();
2484 }
2485
2486 @Override
2487 public WebView getCurrentWebView() {
2488 return mTabControl.getCurrentWebView();
2489 }
2490
2491 /*
2492 * This method is called as a result of the user selecting the options
2493 * menu to see the download window. It shows the download window on top of
2494 * the current window.
2495 */
2496 void viewDownloads() {
2497 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2498 mActivity.startActivity(intent);
2499 }
2500
John Reck30b065e2011-07-19 10:58:05 -07002501 int getActionModeHeight() {
2502 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2503 new int[] { android.R.attr.actionBarSize });
2504 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2505 actionBarSizeTypedArray.recycle();
2506 return size;
2507 }
2508
Michael Kolb8233fac2010-10-26 16:08:53 -07002509 // action mode
2510
John Reck9c35b9c2012-05-30 10:08:50 -07002511 @Override
2512 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002513 mUi.onActionModeStarted(mode);
2514 mActionMode = mode;
2515 }
2516
2517 /*
2518 * True if a custom ActionMode (i.e. find or select) is in use.
2519 */
2520 @Override
2521 public boolean isInCustomActionMode() {
2522 return mActionMode != null;
2523 }
2524
2525 /*
2526 * End the current ActionMode.
2527 */
2528 @Override
2529 public void endActionMode() {
2530 if (mActionMode != null) {
2531 mActionMode.finish();
2532 }
2533 }
2534
2535 /*
2536 * Called by find and select when they are finished. Replace title bars
2537 * as necessary.
2538 */
John Reck9c35b9c2012-05-30 10:08:50 -07002539 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002540 public void onActionModeFinished(ActionMode mode) {
2541 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002542 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002543 mActionMode = null;
2544 }
2545
2546 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002547 final Tab tab = getCurrentTab();
2548 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002549 }
2550
2551 // bookmark handling
2552
2553 /**
2554 * add the current page as a bookmark to the given folder id
2555 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002556 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002557 * bookmarked, and if it is, edit that bookmark. If false, and
2558 * the site is already bookmarked, do not attempt to edit the
2559 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002560 */
2561 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002562 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002563 WebView w = getCurrentTopWebView();
2564 if (w == null) {
2565 return null;
2566 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002567 Intent i = new Intent(mActivity,
2568 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002569 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2570 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2571 String touchIconUrl = w.getTouchIconUrl();
2572 if (touchIconUrl != null) {
2573 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2574 WebSettings settings = w.getSettings();
2575 if (settings != null) {
2576 i.putExtra(AddBookmarkPage.USER_AGENT,
2577 settings.getUserAgentString());
2578 }
2579 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002580 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002581 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002582 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002583 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2584 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002585 // Put the dialog at the upper right of the screen, covering the
2586 // star on the title bar.
2587 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002588 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002589 }
2590
2591 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002592 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002593 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002594 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002595 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002596 }
2597
Vivek Sekharb54614f2014-05-01 19:03:37 -07002598 @Override
2599 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2600 boolean capture) {
2601 mUploadHandler = new UploadHandler(this);
2602 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2603 }
2604
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 // thumbnails
2606
2607 /**
2608 * Return the desired width for thumbnail screenshots, which are stored in
2609 * the database, and used on the bookmarks screen.
2610 * @param context Context for finding out the density of the screen.
2611 * @return desired width for thumbnail screenshot.
2612 */
2613 static int getDesiredThumbnailWidth(Context context) {
2614 return context.getResources().getDimensionPixelOffset(
2615 R.dimen.bookmarkThumbnailWidth);
2616 }
2617
2618 /**
2619 * Return the desired height for thumbnail screenshots, which are stored in
2620 * the database, and used on the bookmarks screen.
2621 * @param context Context for finding out the density of the screen.
2622 * @return desired height for thumbnail screenshot.
2623 */
2624 static int getDesiredThumbnailHeight(Context context) {
2625 return context.getResources().getDimensionPixelOffset(
2626 R.dimen.bookmarkThumbnailHeight);
2627 }
2628
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002629 static void createScreenshotAsync(WebView view, int width, int height,
2630 final ValueCallback<Bitmap> cb) {
2631 if (view == null || width == 0 || height == 0) {
2632 return;
2633 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002634 view.getContentBitmapAsync(
2635 (float) width / view.getWidth(),
2636 new Rect(),
2637 new ValueCallback<Bitmap>() {
2638 @Override
2639 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002640 if (bitmap != null)
2641 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2642 cb.onReceiveValue(bitmap);
2643 }});
2644 }
2645
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002646 private void updateScreenshot(final Tab tab) {
2647 createScreenshotAsync(
2648 tab.getWebView(),
2649 getDesiredThumbnailWidth(mActivity),
2650 getDesiredThumbnailHeight(mActivity),
2651 new ValueCallback<Bitmap>() {
2652 @Override
2653 public void onReceiveValue(Bitmap bitmap) {
2654 updateScreenshot(tab, bitmap);
2655 }
2656 });
2657 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002658
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002659 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002660 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002661 // FIXME: Would like to make sure there is actually something to
2662 // draw, but the API for that (WebViewCore.pictureReady()) is not
2663 // currently accessible here.
2664
John Reck34ef2672011-02-10 11:30:55 -08002665 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002666 if (view == null) {
2667 // Tab was destroyed
2668 return;
2669 }
John Reck34ef2672011-02-10 11:30:55 -08002670 final String url = tab.getUrl();
2671 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002672 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002673 if (TextUtils.isEmpty(url)) {
2674 return;
2675 }
2676
kaiyiz6e5b3e02013-08-19 20:02:01 +08002677 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002678 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002679 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002680 }
John Reck34ef2672011-02-10 11:30:55 -08002681 // Only update thumbnails for web urls (http(s)://), not for
2682 // about:, javascript:, data:, etc...
2683 // Unless it is a bookmarked site, then always update
2684 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2685 return;
2686 }
2687
kaiyiz6e5b3e02013-08-19 20:02:01 +08002688 if (url != null && mUpdateMyNavThumbnailUrl != null
2689 && Patterns.WEB_URL.matcher(url).matches()
2690 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2691 String urlHost = (new WebAddress(url)).getHost();
2692 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2693 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2694 || bookmarkHost.length() == 0) {
2695 return;
2696 }
2697 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2698 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2699 bookmarkHost.length());
2700 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2701 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2702 if (!bookmarkDomain.equals(urlDomain)) {
2703 return;
2704 }
2705 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002706 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002707 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2708 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2709 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2710 500);
2711 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002712 return;
2713 }
2714
2715 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002716 new AsyncTask<Void, Void, Void>() {
2717 @Override
2718 protected Void doInBackground(Void... unused) {
2719 Cursor cursor = null;
2720 try {
2721 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002722 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2723 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2724 : url);
2725 if (mUpdateMyNavThumbnail) {
2726 mUpdateMyNavThumbnail = false;
2727 mUpdateMyNavThumbnailUrl = null;
2728 }
John Reck34ef2672011-02-10 11:30:55 -08002729 if (cursor != null && cursor.moveToFirst()) {
2730 final ByteArrayOutputStream os =
2731 new ByteArrayOutputStream();
2732 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002733
John Reck34ef2672011-02-10 11:30:55 -08002734 ContentValues values = new ContentValues();
2735 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002736
John Reck34ef2672011-02-10 11:30:55 -08002737 do {
John Reck617fd832011-02-16 14:35:59 -08002738 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002739 cr.update(Images.CONTENT_URI, values, null, null);
2740 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002741 }
John Reck34ef2672011-02-10 11:30:55 -08002742 } catch (IllegalStateException e) {
2743 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002744 } catch (SQLiteException s) {
2745 // Added for possible error when user tries to remove the same bookmark
2746 // that is being updated with a screen shot
2747 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002748 } finally {
2749 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002750 }
John Reck34ef2672011-02-10 11:30:55 -08002751 return null;
2752 }
2753 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002754 }
2755
2756 private class Copy implements OnMenuItemClickListener {
2757 private CharSequence mText;
2758
John Reck9c35b9c2012-05-30 10:08:50 -07002759 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002760 public boolean onMenuItemClick(MenuItem item) {
2761 copy(mText);
2762 return true;
2763 }
2764
2765 public Copy(CharSequence toCopy) {
2766 mText = toCopy;
2767 }
2768 }
2769
Leon Scroggins63c02662010-11-18 15:16:27 -05002770 private static class Download implements OnMenuItemClickListener {
2771 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002772 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002773 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002774 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002775 private static final String FALLBACK_EXTENSION = "dat";
2776 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002777
John Reck9c35b9c2012-05-30 10:08:50 -07002778 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002779 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002780 if (DataUri.isDataUri(mText)) {
2781 saveDataUri();
2782 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002783 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002784 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002785 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002786 return true;
2787 }
2788
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002789 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2790 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002791 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002792 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002793 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002794 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002795 }
George Mount387d45d2011-10-07 15:57:53 -07002796
2797 /**
2798 * Treats mText as a data URI and writes its contents to a file
2799 * based on the current time.
2800 */
2801 private void saveDataUri() {
2802 FileOutputStream outputStream = null;
2803 try {
2804 DataUri uri = new DataUri(mText);
2805 File target = getTarget(uri);
2806 outputStream = new FileOutputStream(target);
2807 outputStream.write(uri.getData());
2808 final DownloadManager manager =
2809 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2810 manager.addCompletedDownload(target.getName(),
2811 mActivity.getTitle().toString(), false,
2812 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002813 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002814 } catch (IOException e) {
2815 Log.e(LOGTAG, "Could not save data URL");
2816 } finally {
2817 if (outputStream != null) {
2818 try {
2819 outputStream.close();
2820 } catch (IOException e) {
2821 // ignore close errors
2822 }
2823 }
2824 }
2825 }
2826
2827 /**
2828 * Creates a File based on the current time stamp and uses
2829 * the mime type of the DataUri to get the extension.
2830 */
2831 private File getTarget(DataUri uri) throws IOException {
2832 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002833 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002834 String nameBase = format.format(new Date());
2835 String mimeType = uri.getMimeType();
2836 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2837 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2838 if (extension == null) {
2839 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2840 extension = FALLBACK_EXTENSION;
2841 }
2842 extension = "." + extension; // createTempFile needs the '.'
2843 File targetFile = File.createTempFile(nameBase, extension, dir);
2844 return targetFile;
2845 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002846 }
2847
Cary Clark8974d282010-11-22 10:46:05 -05002848 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002849 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002850
John Reck9c35b9c2012-05-30 10:08:50 -07002851 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002852 public boolean onMenuItemClick(MenuItem item) {
2853 if (mWebView != null) {
2854 return mWebView.selectText();
2855 }
2856 return false;
2857 }
2858
2859 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002860 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002861 }
2862
2863 }
2864
Michael Kolb8233fac2010-10-26 16:08:53 -07002865 /********************** TODO: UI stuff *****************************/
2866
2867 // these methods have been copied, they still need to be cleaned up
2868
2869 /****************** tabs ***************************************************/
2870
2871 // basic tab interactions:
2872
2873 // it is assumed that tabcontrol already knows about the tab
2874 protected void addTab(Tab tab) {
2875 mUi.addTab(tab);
2876 }
2877
2878 protected void removeTab(Tab tab) {
2879 mUi.removeTab(tab);
2880 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002881 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002882 }
2883
Michael Kolbf2055602011-04-09 17:20:03 -07002884 @Override
2885 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002886 // monkey protection against delayed start
2887 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002888
2889 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2890 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002891 mTabControl.setCurrentTab(tab);
2892 // the tab is guaranteed to have a webview after setCurrentTab
2893 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002894
2895
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002896 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002897 //Purge active tabs
2898 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002899 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002900 }
2901
John Reck8bcafc12011-08-29 16:43:02 -07002902 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002903 Tab current = mTabControl.getCurrentTab();
2904 if (current != null
2905 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002906 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002907 }
2908 }
2909
John Reck26b18322011-06-21 13:08:58 -07002910 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002911 //Cancel navscreen request
2912 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002913 // Dismiss the subwindow if applicable.
2914 dismissSubWindow(appTab);
2915 // Since we might kill the WebView, remove it from the
2916 // content view first.
2917 mUi.detachTab(appTab);
2918 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002919 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002920 // TODO: analyze why the remove and add are necessary
2921 mUi.attachTab(appTab);
2922 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002923 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002924 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002925 } else {
2926 // If the tab was the current tab, we have to attach
2927 // it to the view system again.
2928 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002929 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002930 }
2931 }
2932
2933 // Remove the sub window if it exists. Also called by TabControl when the
2934 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002935 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002936 public void dismissSubWindow(Tab tab) {
2937 removeSubWindow(tab);
2938 // dismiss the subwindow. This will destroy the WebView.
2939 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002940 WebView wv = getCurrentTopWebView();
2941 if (wv != null) {
2942 wv.requestFocus();
2943 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002944 }
2945
2946 @Override
2947 public void removeSubWindow(Tab t) {
2948 if (t.getSubWebView() != null) {
2949 mUi.removeSubWindow(t.getSubViewContainer());
2950 }
2951 }
2952
2953 @Override
2954 public void attachSubWindow(Tab tab) {
2955 if (tab.getSubWebView() != null) {
2956 mUi.attachSubWindow(tab.getSubViewContainer());
2957 getCurrentTopWebView().requestFocus();
2958 }
2959 }
2960
Mathew Inwood29721c22011-06-29 17:55:24 +01002961 private Tab showPreloadedTab(final UrlData urlData) {
2962 if (!urlData.isPreloaded()) {
2963 return null;
2964 }
2965 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2966 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2967 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002968 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002969 // Could not submit query. Fallback to regular tab creation
2970 tabControl.destroy();
2971 return null;
2972 }
2973 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002974 // check tab count and make room for new tab
2975 if (!mTabControl.canCreateNewTab()) {
2976 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2977 if (leastUsed != null) {
2978 closeTab(leastUsed);
2979 }
2980 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002981 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002982 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002983 mTabControl.addPreloadedTab(t);
2984 addTab(t);
2985 setActiveTab(t);
2986 return t;
2987 }
2988
Michael Kolb7bcafde2011-05-09 13:55:59 -07002989 // open a non inconito tab with the given url data
2990 // and set as active tab
2991 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002992 Tab tab = showPreloadedTab(urlData);
2993 if (tab == null) {
2994 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002995 if ((tab != null) && !urlData.isEmpty()) {
2996 loadUrlDataIn(tab, urlData);
2997 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002998 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002999 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07003000 }
3001
Michael Kolb843510f2010-12-09 10:51:49 -08003002 @Override
3003 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003004 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07003005 }
3006
Michael Kolb8233fac2010-10-26 16:08:53 -07003007 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07003008 public Tab openIncognitoTab() {
3009 return openTab(INCOGNITO_URI, true, true, false);
3010 }
3011
3012 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003013 public Tab openTab(String url, boolean incognito, boolean setActive,
3014 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003015 return openTab(url, incognito, setActive, useCurrent, null);
3016 }
3017
3018 @Override
3019 public Tab openTab(String url, Tab parent, boolean setActive,
3020 boolean useCurrent) {
3021 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3022 setActive, useCurrent, parent);
3023 }
3024
3025 public Tab openTab(String url, boolean incognito, boolean setActive,
3026 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003027 Tab tab = createNewTab(incognito, setActive, useCurrent);
3028 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003029 if (parent instanceof SnapshotTab) {
3030 addTab(tab);
3031 if (setActive)
3032 setActiveTab(tab);
3033 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003034 parent.addChildTab(tab);
3035 }
Michael Kolb519d2282011-05-09 17:03:19 -07003036 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003037 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003038 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003039 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003040 return tab;
3041 }
3042
3043 // this method will attempt to create a new tab
3044 // incognito: private browsing tab
3045 // setActive: ste tab as current tab
3046 // useCurrent: if no new tab can be created, return current tab
3047 private Tab createNewTab(boolean incognito, boolean setActive,
3048 boolean useCurrent) {
3049 Tab tab = null;
3050 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05003051 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003052 addTab(tab);
3053 if (setActive) {
3054 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07003055 } else {
3056 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003057 }
3058 } else {
3059 if (useCurrent) {
3060 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07003061 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003062 } else {
3063 mUi.showMaxTabsWarning();
3064 }
3065 }
3066 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003067 }
3068
John Reck2bc80422011-06-30 15:11:49 -07003069 @Override
3070 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3071 SnapshotTab tab = null;
3072 if (mTabControl.canCreateNewTab()) {
3073 tab = mTabControl.createSnapshotTab(snapshotId);
3074 addTab(tab);
3075 if (setActive) {
3076 setActiveTab(tab);
3077 }
3078 } else {
3079 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003080 }
3081 return tab;
3082 }
3083
Michael Kolb8233fac2010-10-26 16:08:53 -07003084 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003085 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003086 * @return boolean True if we successfully switched to a different tab. If
3087 * the indexth tab is null, or if that tab is the same as
3088 * the current one, return false.
3089 */
3090 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003091 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003092 Tab currentTab = mTabControl.getCurrentTab();
3093 if (tab == null || tab == currentTab) {
3094 return false;
3095 }
3096 setActiveTab(tab);
3097 return true;
3098 }
3099
3100 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003101 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003102 closeCurrentTab(false);
3103 }
3104
3105 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003106 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003107 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003108 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003109 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003110 return;
3111 }
Michael Kolbc831b632011-05-11 09:30:34 -07003112 final Tab current = mTabControl.getCurrentTab();
3113 final int pos = mTabControl.getCurrentPosition();
3114 Tab newTab = current.getParent();
3115 if (newTab == null) {
3116 newTab = mTabControl.getTab(pos + 1);
3117 if (newTab == null) {
3118 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003119 }
3120 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003121 if (andQuit) {
3122 mTabControl.setCurrentTab(newTab);
3123 closeTab(current);
3124 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003125 // Close window
3126 closeTab(current);
3127 }
3128 }
3129
3130 /**
3131 * Close the tab, remove its associated title bar, and adjust mTabControl's
3132 * current tab to a valid value.
3133 */
3134 @Override
3135 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003136 if (tab == mTabControl.getCurrentTab()) {
3137 closeCurrentTab();
3138 } else {
3139 removeTab(tab);
3140 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003141 }
3142
Afzal Najamd4e33312012-04-26 01:54:01 -04003143 /**
3144 * Close all tabs except the current one
3145 */
3146 @Override
3147 public void closeOtherTabs() {
3148 int inactiveTabs = mTabControl.getTabCount() - 1;
3149 for (int i = inactiveTabs; i >= 0; i--) {
3150 Tab tab = mTabControl.getTab(i);
3151 if (tab != mTabControl.getCurrentTab()) {
3152 removeTab(tab);
3153 }
3154 }
3155 }
3156
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003158 protected void loadUrlFromContext(String url) {
3159 Tab tab = getCurrentTab();
3160 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003161 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003162 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003163 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003164 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003165 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003166 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003167 }
3168 }
3169 }
3170
3171 /**
3172 * Load the URL into the given WebView and update the title bar
3173 * to reflect the new load. Call this instead of WebView.loadUrl
3174 * directly.
3175 * @param view The WebView used to load url.
3176 * @param url The URL to load.
3177 */
John Reck71e51422011-07-01 16:49:28 -07003178 @Override
3179 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003180 loadUrl(tab, url, null);
3181 }
3182
3183 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3184 if (tab != null) {
3185 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003186 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003187 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003188 if (tab.hasCrashed) {
3189 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3190 }
Michael Kolba53c9892011-10-05 13:31:40 -07003191 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003192 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003193 }
3194
3195 /**
3196 * Load UrlData into a Tab and update the title bar to reflect the new
3197 * load. Call this instead of UrlData.loadIn directly.
3198 * @param t The Tab used to load.
3199 * @param data The UrlData being loaded.
3200 */
3201 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003202 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003203 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003204 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003205 } else {
John Reck38b39652012-06-05 09:22:59 -07003206 if (t != null && data.mDisableUrlOverride) {
3207 t.disableUrlOverridingForLoad();
3208 }
John Reck26b18322011-06-21 13:08:58 -07003209 loadUrl(t, data.mUrl, data.mHeaders);
3210 }
3211 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003212 }
3213
John Reck30c714c2010-12-16 17:30:34 -08003214 @Override
3215 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003216 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003217 //In case of tab can go back (aka tab has navigation entry) do nothing
3218 //else just load homepage in current tab.
3219 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003220 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003221 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003222 }
3223
3224 void goBackOnePageOrQuit() {
3225 Tab current = mTabControl.getCurrentTab();
3226 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003227 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3228 showExitDialog(mActivity);
3229 } else {
3230 /*
3231 * Instead of finishing the activity, simply push this to the back
3232 * of the stack and let ActivityManager to choose the foreground
3233 * activity. As BrowserActivity is singleTask, it will be always the
3234 * root of the task. So we can use either true or false for
3235 * moveTaskToBack().
3236 */
3237 mActivity.moveTaskToBack(true);
3238 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003239 return;
3240 }
John Reckef654f12011-07-12 16:42:08 -07003241 if (current.canGoBack()) {
3242 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003243 } else {
3244 // Check to see if we are closing a window that was created by
3245 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003246 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003247 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003248 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003249 // Now we close the other tab
3250 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003251 } else if (BrowserConfig.getInstance(getContext())
3252 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3253 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003254 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003255 if ((current.getAppId() != null) || current.closeOnBack()) {
3256 closeCurrentTab(true);
3257 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003258 /*
3259 * Instead of finishing the activity, simply push this to the back
3260 * of the stack and let ActivityManager to choose the foreground
3261 * activity. As BrowserActivity is singleTask, it will be always the
3262 * root of the task. So we can use either true or false for
3263 * moveTaskToBack().
3264 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003265 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003266 }
3267 }
3268 }
3269
3270 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003271 * helper method for key handler
3272 * returns the current tab if it can't advance
3273 */
Michael Kolbc831b632011-05-11 09:30:34 -07003274 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003275 int pos = mTabControl.getCurrentPosition() + 1;
3276 if (pos >= mTabControl.getTabCount()) {
3277 pos = 0;
3278 }
3279 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003280 }
3281
3282 /**
3283 * helper method for key handler
3284 * returns the current tab if it can't advance
3285 */
Michael Kolbc831b632011-05-11 09:30:34 -07003286 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003287 int pos = mTabControl.getCurrentPosition() - 1;
3288 if ( pos < 0) {
3289 pos = mTabControl.getTabCount() - 1;
3290 }
3291 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003292 }
3293
John Reckbcef87f2012-02-03 14:58:34 -08003294 boolean isMenuOrCtrlKey(int keyCode) {
3295 return (KeyEvent.KEYCODE_MENU == keyCode)
3296 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3297 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3298 }
3299
Michael Kolb0035fad2011-03-14 13:25:28 -07003300 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003301 * handle key events in browser
3302 *
3303 * @param keyCode
3304 * @param event
3305 * @return true if handled, false to pass to super
3306 */
John Reck9c35b9c2012-05-30 10:08:50 -07003307 @Override
3308 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003309 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3310 // Hardware menu key
Enrico Ros1f5a0952014-11-18 20:15:48 -08003311 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
Pankaj Garg49b79252014-11-07 17:33:41 -08003312 true, false);
3313 return true;
3314 }
3315
Cary Clark160bbb92011-01-10 11:17:07 -05003316 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003317 // Even if MENU is already held down, we need to call to super to open
3318 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003319 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003320 mMenuIsDown = true;
3321 return false;
3322 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003323
Cary Clark8ff8c662010-12-29 15:03:05 -05003324 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003325 Tab tab = getCurrentTab();
3326 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003327
Cary Clark160bbb92011-01-10 11:17:07 -05003328 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3329 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003330
Michael Kolb8233fac2010-10-26 16:08:53 -07003331 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003332 case KeyEvent.KEYCODE_TAB:
3333 if (event.isCtrlPressed()) {
3334 if (event.isShiftPressed()) {
3335 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003336 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003337 } else {
3338 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003339 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003340 }
3341 return true;
3342 }
3343 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003344 case KeyEvent.KEYCODE_SPACE:
3345 // WebView/WebTextView handle the keys in the KeyDown. As
3346 // the Activity's shortcut keys are only handled when WebView
3347 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003348 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003349 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003350 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003351 pageDown();
3352 }
3353 return true;
3354 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003355 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003356 event.startTracking();
3357 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003358 case KeyEvent.KEYCODE_FORWARD:
3359 if (!noModifiers) break;
3360 tab.goForward();
3361 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003362 case KeyEvent.KEYCODE_DPAD_LEFT:
3363 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003364 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003365 return true;
3366 }
3367 break;
3368 case KeyEvent.KEYCODE_DPAD_RIGHT:
3369 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003370 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003371 return true;
3372 }
3373 break;
3374 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003375 if (ctrl) {
3376 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003377 return true;
3378 }
3379 break;
Michael Kolba4183062011-01-16 10:43:21 -08003380// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003381 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003382 if (ctrl ) {
3383 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003384 return true;
3385 }
3386 break;
Michael Kolba4183062011-01-16 10:43:21 -08003387// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003388// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003389// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003390// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003391// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003392// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003393// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003394// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003395// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003396// case KeyEvent.KEYCODE_M: // unused
3397// case KeyEvent.KEYCODE_N: // in Chrome: new window
3398// case KeyEvent.KEYCODE_O: // in Chrome: open file
3399// case KeyEvent.KEYCODE_P: // in Chrome: print page
3400// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003401// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003402// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3403 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003404 // we can't use the ctrl/shift flags, they check for
3405 // exclusive use of a modifier
3406 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003407 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003408 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003409 } else {
3410 openTabToHomePage();
3411 }
3412 return true;
3413 }
3414 break;
3415// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3416// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003417// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003418// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3419// case KeyEvent.KEYCODE_Y: // unused
3420// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003421 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003422 // it is a regular key and webview is not null
3423 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003424 }
3425
John Reck9c35b9c2012-05-30 10:08:50 -07003426 @Override
3427 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003428 switch(keyCode) {
3429 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003430 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003431 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003432 return true;
3433 }
3434 break;
3435 }
3436 return false;
3437 }
3438
John Reck9c35b9c2012-05-30 10:08:50 -07003439 @Override
3440 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003441 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003442 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003443 if (KeyEvent.KEYCODE_MENU == keyCode
3444 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003445 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003446 }
3447 }
Cary Clark160bbb92011-01-10 11:17:07 -05003448 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003449 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003450 case KeyEvent.KEYCODE_BACK:
3451 if (event.isTracking() && !event.isCanceled()) {
3452 onBackKey();
3453 return true;
3454 }
3455 break;
3456 }
3457 return false;
3458 }
3459
3460 public boolean isMenuDown() {
3461 return mMenuIsDown;
3462 }
3463
John Reck9c35b9c2012-05-30 10:08:50 -07003464 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003465 public void setupAutoFill(Message message) {
3466 // Open the settings activity at the AutoFill profile fragment so that
3467 // the user can create a new profile. When they return, we will dispatch
3468 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003469 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003470 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3471 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003472 }
John Reckb3417f02011-01-14 11:01:05 -08003473
John Reck9c35b9c2012-05-30 10:08:50 -07003474 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003475 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003476 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003477 return true;
3478 }
3479
John Reck1cf4b792011-07-26 10:22:22 -07003480 @Override
3481 public boolean shouldCaptureThumbnails() {
3482 return mUi.shouldCaptureThumbnails();
3483 }
3484
Michael Kolbc3af0672011-08-09 10:24:41 -07003485 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003486 public boolean supportsVoice() {
3487 PackageManager pm = mActivity.getPackageManager();
3488 List activities = pm.queryIntentActivities(new Intent(
3489 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3490 return activities.size() != 0;
3491 }
3492
3493 @Override
3494 public void startVoiceRecognizer() {
3495 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003496 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003497 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3498 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3499 mActivity.startActivityForResult(voice, VOICE_RESULT);
3500 }
3501
Pankaj Garg7b279f62014-08-12 14:47:18 -07003502 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003503 if (mLevel == level)
3504 return;
3505 mLevel = level;
3506 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003507 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3508 lp.dimAmount = level;
3509 mActivity.getWindow().setAttributes(lp);
3510 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3511 } else {
3512 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3513 }
3514 }
3515
Michael Kolb0b129122012-06-04 16:31:58 -07003516 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003517 public void setBlockEvents(boolean block) {
3518 mBlockEvents = block;
3519 }
3520
John Reck9c35b9c2012-05-30 10:08:50 -07003521 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003522 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003523 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003524 }
3525
John Reck9c35b9c2012-05-30 10:08:50 -07003526 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003527 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003528 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003529 }
3530
John Reck9c35b9c2012-05-30 10:08:50 -07003531 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003532 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003533 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003534 }
3535
John Reck9c35b9c2012-05-30 10:08:50 -07003536 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003537 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003538 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003539 }
3540
John Reck9c35b9c2012-05-30 10:08:50 -07003541 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003542 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003543 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003544 }
3545
Pankaj Garg49b79252014-11-07 17:33:41 -08003546 @Override
3547 public boolean shouldShowAppMenu() {
3548 return true;
3549 }
3550
3551 @Override
3552 public int getMenuThemeResourceId() {
3553 return R.style.OverflowMenuTheme;
3554 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003555}