blob: 747b7790c165dabed50227f677b968172d01589d [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;
Michael Kolb0b129122012-06-04 16:31:58 -0700257
George Mount3636d0a2011-11-21 09:08:21 -0800258 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 mActivity = browser;
260 mSettings = BrowserSettings.getInstance();
261 mTabControl = new TabControl(this);
262 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700263 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800264 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700265 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700266
267 mUrlHandler = new UrlHandler(this);
268 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
270
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500272 mBookmarksObserver = new ContentObserver(mHandler) {
273 @Override
274 public void onChange(boolean selfChange) {
275 int size = mTabControl.getTabCount();
276 for (int i = 0; i < size; i++) {
277 mTabControl.getTab(i).updateBookmarkedStatus();
278 }
279 }
280
281 };
282 browser.getContentResolver().registerContentObserver(
283 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700284
285 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700286 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800287 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700288 }
289
John Reck9c35b9c2012-05-30 10:08:50 -0700290 @Override
291 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700292 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800293 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800294 // mCrashRecoverHandler has any previously saved state.
295 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700296 }
297
George Mount3636d0a2011-11-21 09:08:21 -0800298 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800299 // we dont want to ever recover incognito tabs
300 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700301
Patrick Scott7d50a932011-02-04 09:27:26 -0500302 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700303 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500304 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000305
Michael Kolbc831b632011-05-11 09:30:34 -0700306 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500307 // Not able to restore so we go ahead and clear session cookies. We
308 // must do this before trying to login the user as we don't want to
309 // clear any session cookies set during login.
310 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700311 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800312 if (intent == null) {
313 // This won't happen under common scenarios. The icicle is
314 // not null, but there aren't any tabs to restore.
315 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700316 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800317 final Bundle extra = intent.getExtras();
318 // Create an initial tab.
319 // If the intent is ACTION_VIEW and data is not null, the Browser is
320 // invoked to view the content by another application. In this case,
321 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800322 UrlData urlData = null;
323 if (intent.getData() != null
324 && Intent.ACTION_VIEW.equals(intent.getAction())
325 && intent.getData().toString().startsWith("content://")) {
326 urlData = new UrlData(intent.getData().toString());
327 } else {
328 urlData = IntentHandler.getUrlDataFromIntent(intent);
329 }
George Mount3636d0a2011-11-21 09:08:21 -0800330 Tab t = null;
331 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700332 String landingPage = mActivity.getResources().getString(
333 R.string.def_landing_page);
334 if (!landingPage.isEmpty()) {
335 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800336 } else {
337 t = openTabToHomePage();
338 }
George Mount3636d0a2011-11-21 09:08:21 -0800339 } else {
340 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700341 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800342 }
343 if (t != null) {
344 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
345 }
346 WebView webView = t.getWebView();
347 if (extra != null) {
348 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
349 if (scale > 0 && scale <= 1000) {
350 webView.setInitialScale(scale);
351 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 }
353 }
John Reckd8c74522011-06-14 08:45:00 -0700354 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700356 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500357 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700358 List<Tab> tabs = mTabControl.getTabs();
359 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700360
John Reck1cf4b792011-07-26 10:22:22 -0700361 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700362 //handle restored pages that may require a JS interface
363 if (t.getWebView() != null) {
364 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
365 if (backForwardList != null) {
366 for (int i = 0; i < backForwardList.getSize(); i++) {
367 WebHistoryItem item = backForwardList.getItemAtIndex(i);
368 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
369 }
370 }
371 }
John Reck1cf4b792011-07-26 10:22:22 -0700372 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700373 if (t != mTabControl.getCurrentTab()) {
374 t.pause();
375 }
John Reck1cf4b792011-07-26 10:22:22 -0700376 }
377 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700378 if (tabs.size() == 0) {
379 openTabToHomePage();
380 }
John Reck1cf4b792011-07-26 10:22:22 -0700381 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700382 // TabControl.restoreState() will create a new tab even if
383 // restoring the state fails.
384 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800385 // Intent is non-null when framework thinks the browser should be
386 // launching with a new intent (icicle is null).
387 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700388 mIntentHandler.onNewIntent(intent);
389 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700390 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700391 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700392 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800393 if (jsFlags.trim().length() != 0) {
394 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700395 }
George Mount3636d0a2011-11-21 09:08:21 -0800396 if (intent != null
397 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700398 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800399 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700400 }
401
John Reck1cf4b792011-07-26 10:22:22 -0700402 private static class PruneThumbnails implements Runnable {
403 private Context mContext;
404 private List<Long> mIds;
405
406 PruneThumbnails(Context context, List<Long> preserveIds) {
407 mContext = context.getApplicationContext();
408 mIds = preserveIds;
409 }
410
411 @Override
412 public void run() {
413 ContentResolver cr = mContext.getContentResolver();
414 if (mIds == null || mIds.size() == 0) {
415 cr.delete(Thumbnails.CONTENT_URI, null, null);
416 } else {
417 int length = mIds.size();
418 StringBuilder where = new StringBuilder();
419 where.append(Thumbnails._ID);
420 where.append(" not in (");
421 for (int i = 0; i < length; i++) {
422 where.append(mIds.get(i));
423 if (i < (length - 1)) {
424 where.append(",");
425 }
426 }
427 where.append(")");
428 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
429 }
430 }
431
432 }
433
Michael Kolb1514bb72010-11-22 09:11:48 -0800434 @Override
435 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700436 return mFactory;
437 }
438
439 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800440 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800441 if (tab.hasCrashed)
442 tab.showCrashView();
443 else
444 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800445 }
446
447 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800448 public void createSubWindow(Tab tab) {
449 endActionMode();
450 WebView mainView = tab.getWebView();
451 WebView subView = mFactory.createWebView((mainView == null)
452 ? false
453 : mainView.isPrivateBrowsingEnabled());
454 mUi.createSubWindow(tab, subView);
455 }
456
457 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700458 public Context getContext() {
459 return mActivity;
460 }
461
462 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700463 public Activity getActivity() {
464 return mActivity;
465 }
466
467 void setUi(UI ui) {
468 mUi = ui;
469 }
470
Michael Kolbaf63dba2012-05-16 12:58:05 -0700471 @Override
472 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700473 return mSettings;
474 }
475
476 IntentHandler getIntentHandler() {
477 return mIntentHandler;
478 }
479
480 @Override
481 public UI getUi() {
482 return mUi;
483 }
484
485 int getMaxTabs() {
486 return mActivity.getResources().getInteger(R.integer.max_tabs);
487 }
488
489 @Override
490 public TabControl getTabControl() {
491 return mTabControl;
492 }
493
Michael Kolb1bf23132010-11-19 12:55:12 -0800494 @Override
495 public List<Tab> getTabs() {
496 return mTabControl.getTabs();
497 }
498
Michael Kolb8233fac2010-10-26 16:08:53 -0700499 private void startHandler() {
500 mHandler = new Handler() {
501
502 @Override
503 public void handleMessage(Message msg) {
504 switch (msg.what) {
505 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700506 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700507 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800508 case UNKNOWN_TYPE_MSG:
509 HashMap unknownTypeMap = (HashMap) msg.obj;
510 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
511 /*
512 * When the context menu is shown to the user
513 * we need to assure that its happening on the current webview
514 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
515 */
516 if (getCurrentWebView() != viewForUnknownType)
517 break;
518
519 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800520 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800521 WebView.HitTestResult result = new WebView.HitTestResult();
522
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800523 // Prevent unnecessary calls to context menu
524 // if url and image src are null
525 if (unknown_type_src == null && unknown_type_url == null)
526 break;
527
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800528 //setting the HitTestResult with new RESULT TYPE
529 if (!TextUtils.isEmpty(unknown_type_src)) {
530 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
531 result.setExtra(unknown_type_src);
532 } else {
533 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
534 result.setExtra("about:blank");
535 }
536
537 mResult = result;
538 openContextMenu(viewForUnknownType);
539 mResult = null;
540
541 break;
542
Michael Kolb8233fac2010-10-26 16:08:53 -0700543 case FOCUS_NODE_HREF:
544 {
545 String url = (String) msg.getData().get("url");
546 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500547 String src = (String) msg.getData().get("src");
548 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700549 if (TextUtils.isEmpty(url)) {
550 break;
551 }
552 HashMap focusNodeMap = (HashMap) msg.obj;
553 WebView view = (WebView) focusNodeMap.get("webview");
554 // Only apply the action if the top window did not change.
555 if (getCurrentTopWebView() != view) {
556 break;
557 }
558 switch (msg.arg1) {
559 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700560 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500562 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700563 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500564 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500565 case R.id.open_newtab_context_menu_id:
566 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700567 openTab(url, parent,
568 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500569 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700570 case R.id.copy_link_context_menu_id:
571 copy(url);
572 break;
573 case R.id.save_link_context_menu_id:
574 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500575 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800576 mActivity, url, view.getSettings().getUserAgentString(),
577 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700578 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700579 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700580 if(title == null || title == "")
581 title = url;
582
583 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
584 //SWE TODO: No thumbnail support for the url obtained via
585 //browser context menu as its not loaded in webview.
586 if (bookmarkIntent != null) {
587 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
588 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
589 mActivity.startActivity(bookmarkIntent);
590 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700591 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700592 }
593 break;
594 }
595
596 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700597 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 break;
599
600 case STOP_LOAD:
601 stopLoading();
602 break;
603
604 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700605 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700606 mWakeLock.release();
607 // if we reach here, Browser should be still in the
608 // background loading after WAKELOCK_TIMEOUT (5-min).
609 // To avoid burning the battery, stop loading.
610 mTabControl.stopAllLoading();
611 }
612 break;
613
614 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800615 Tab tab = (Tab) msg.obj;
616 if (tab != null) {
617 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 }
619 break;
kaiyiz591110b2013-08-06 17:11:06 +0800620 case OPEN_MENU:
621 if (!mOptionsMenuOpen && mActivity != null ) {
622 mActivity.openOptionsMenu();
623 }
624 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 }
626 }
627 };
628
629 }
630
John Reckef654f12011-07-12 16:42:08 -0700631 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200632 public Tab getCurrentTab() {
633 return mTabControl.getCurrentTab();
634 }
635
Michael Kolbba99c5d2010-11-29 14:57:41 -0800636 @Override
637 public void shareCurrentPage() {
638 shareCurrentPage(mTabControl.getCurrentTab());
639 }
640
641 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700642 if (tab == null || tab.getWebView() == null)
643 return;
644
645 final Tab mytab = tab;
646 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
647 @Override
648 public void onReceiveValue(Bitmap bitmap) {
649 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
650 mytab.getFavicon(), bitmap);
651 }
652 };
653
654 createScreenshotAsync(
655 tab.getWebView(),
656 getDesiredThumbnailWidth(mActivity),
657 getDesiredThumbnailHeight(mActivity),
658 new ValueCallback<Bitmap>() {
659 @Override
660 public void onReceiveValue(Bitmap bitmap) {
661 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
662 mytab.getFavicon(), bitmap);
663 }
664 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800665 }
666
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 /**
668 * Share a page, providing the title, url, favicon, and a screenshot. Uses
669 * an {@link Intent} to launch the Activity chooser.
670 * @param c Context used to launch a new Activity.
671 * @param title Title of the page. Stored in the Intent with
672 * {@link Intent#EXTRA_SUBJECT}
673 * @param url URL of the page. Stored in the Intent with
674 * {@link Intent#EXTRA_TEXT}
675 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
676 * with {@link Browser#EXTRA_SHARE_FAVICON}
677 * @param screenshot Bitmap of a screenshot of the page. Stored in the
678 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
679 */
680 static final void sharePage(Context c, String title, String url,
681 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700682
683 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
684 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
685 R.layout.app_row, sDialog.getApps());
686 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 }
688
689 private void copy(CharSequence text) {
690 ClipboardManager cm = (ClipboardManager) mActivity
691 .getSystemService(Context.CLIPBOARD_SERVICE);
692 cm.setText(text);
693 }
694
695 // lifecycle
696
John Reck9c35b9c2012-05-30 10:08:50 -0700697 @Override
698 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700699 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800700 // update the menu in case of a locale change
701 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800702 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800703 if (mOptionsMenuOpen) {
704 mActivity.closeOptionsMenu();
705 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
706 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 if (mPageDialogsHandler != null) {
708 mPageDialogsHandler.onConfigurationChanged(config);
709 }
710 mUi.onConfigurationChanged(config);
711 }
712
713 @Override
714 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700715 if (!mUi.isWebShowing()) {
716 mUi.showWeb(false);
717 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700718 mIntentHandler.onNewIntent(intent);
719 }
720
John Reck9c35b9c2012-05-30 10:08:50 -0700721 @Override
722 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800723 if (mUi.isCustomViewShowing()) {
724 hideCustomView();
725 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700726 if (mActivityPaused) {
727 Log.e(LOGTAG, "BrowserActivity is already paused.");
728 return;
729 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700730 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800731 Tab tab = mTabControl.getCurrentTab();
732 if (tab != null) {
733 tab.pause();
734 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700735 if (mWakeLock == null) {
736 PowerManager pm = (PowerManager) mActivity
737 .getSystemService(Context.POWER_SERVICE);
738 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
739 }
Michael Kolb70976932010-11-30 11:34:01 -0800740 mWakeLock.acquire();
741 mHandler.sendMessageDelayed(mHandler
742 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
743 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 }
745 mUi.onPause();
746 mNetworkHandler.onPause();
747
748 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100749 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700750 }
751
John Reck9c35b9c2012-05-30 10:08:50 -0700752 @Override
753 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800755 Bundle saveState = createSaveState();
756
757 // crash recovery manages all save & restore state
758 mCrashRecoveryHandler.writeState(saveState);
759 mSettings.setLastRunPaused(true);
760 }
761
762 /**
763 * Save the current state to outState. Does not write the state to
764 * disk.
765 * @return Bundle containing the current state of all tabs.
766 */
767 /* package */ Bundle createSaveState() {
768 Bundle saveState = new Bundle();
769 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800770 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700771 }
772
John Reck9c35b9c2012-05-30 10:08:50 -0700773 @Override
774 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700775 if (!mActivityPaused) {
776 Log.e(LOGTAG, "BrowserActivity is already resumed.");
777 return;
778 }
George Mount3636d0a2011-11-21 09:08:21 -0800779 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700780 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800781 Tab current = mTabControl.getCurrentTab();
782 if (current != null) {
783 current.resume();
784 resumeWebViewTimers(current);
785 }
John Reckf57c0292011-07-21 18:15:39 -0700786 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200787
Michael Kolb8233fac2010-10-26 16:08:53 -0700788 mUi.onResume();
789 mNetworkHandler.onResume();
790 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100791 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700792 if (mVoiceResult != null) {
793 mUi.onVoiceResult(mVoiceResult);
794 mVoiceResult = null;
795 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800796 if (current != null && current.hasCrashed) {
Vivek Sekhar2868b8d2014-12-03 17:22:50 -0800797 current.replaceCrashView(current.getWebView(), current.getViewContainer());
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800798 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 }
800
John Reckf57c0292011-07-21 18:15:39 -0700801 private void releaseWakeLock() {
802 if (mWakeLock != null && mWakeLock.isHeld()) {
803 mHandler.removeMessages(RELEASE_WAKELOCK);
804 mWakeLock.release();
805 }
806 }
807
Michael Kolb70976932010-11-30 11:34:01 -0800808 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800809 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800810 * @param tab guaranteed non-null
811 */
812 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700813 boolean inLoad = tab.inPageLoad();
814 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
815 CookieSyncManager.getInstance().startSync();
816 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100817 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700818 }
819 }
820
Michael Kolb70976932010-11-30 11:34:01 -0800821 /**
822 * Pause all WebView timers using the WebView of the given tab
823 * @param tab
824 * @return true if the timers are paused or tab is null
825 */
826 private boolean pauseWebViewTimers(Tab tab) {
827 if (tab == null) {
828 return true;
829 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700830 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100831 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700833 }
Michael Kolb70976932010-11-30 11:34:01 -0800834 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700835 }
836
John Reck9c35b9c2012-05-30 10:08:50 -0700837 @Override
838 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800839 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
841 mUploadHandler = null;
842 }
843 if (mTabControl == null) return;
844 mUi.onDestroy();
845 // Remove the current tab and sub window
846 Tab t = mTabControl.getCurrentTab();
847 if (t != null) {
848 dismissSubWindow(t);
849 removeTab(t);
850 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500851 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 // Destroy all the tabs
853 mTabControl.destroy();
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 }
855
856 protected boolean isActivityPaused() {
857 return mActivityPaused;
858 }
859
John Reck9c35b9c2012-05-30 10:08:50 -0700860 @Override
861 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 mTabControl.freeMemory();
863 }
864
865 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 public void stopLoading() {
867 mLoadStopped = true;
868 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700870 if (w != null) {
871 w.stopLoading();
872 mUi.onPageStopped(tab);
873 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700874 }
875
876 boolean didUserStopLoading() {
877 return mLoadStopped;
878 }
879
kaiyiza016da12013-08-26 17:50:22 +0800880 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700881 final String reminderType = getContext().getResources().getString(
882 R.string.def_wifi_browser_interaction_remind_type);
883 final String selectionConnnection = getContext().getResources().getString(
884 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700885 final String wifiSelection = getContext().getResources().getString(
886 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700887
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700888 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
889 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700890 return;
891
kaiyiza016da12013-08-26 17:50:22 +0800892 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700893 Context.CONNECTIVITY_SERVICE);
894 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700895 WifiManager wifiMgr = (WifiManager) this.getContext()
896 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700897 if (networkInfo == null
898 || (networkInfo != null && (networkInfo.getType() !=
899 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700900 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
901 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700902 List<ScanResult> list = wifiMgr.getScanResults();
903 // Have no AP's for Wifi's fall back to data
904 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700905 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700906 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
907 this.getContext().startActivity(intent);
908 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700909 // Request to select Wifi AP
910 try {
911 Intent intent = new Intent(wifiSelection);
912 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
913 this.getContext().startActivity(intent);
914 } catch (Exception e) {
915 String err_msg = this.getContext().getString(
916 R.string.acivity_not_found, wifiSelection);
917 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
918 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700919 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700920 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800921 }
922 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700923
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 // WebViewController
925
926 @Override
John Reck324d4402011-01-11 16:56:42 -0800927 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700928
929 // We've started to load a new page. If there was a pending message
930 // to save a screenshot then we will now take the new page and save
931 // an incorrect screenshot. Therefore, remove any pending thumbnail
932 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700933 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700934
935 // reset sync timer to avoid sync starts during loading a page
936 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700937 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700938 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800939 boolean networkNotifier = BrowserConfig.getInstance(getContext())
940 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700941 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700942 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800943 } else {
944 if (!mNetworkHandler.isNetworkUp()) {
945 view.setNetworkAvailable(false);
946 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700947 }
948
949 // when BrowserActivity just starts, onPageStarted may be called before
950 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
951 // to start the timer. As we won't switch tabs while an activity is in
952 // pause state, we can ensure calling resume and pause in pair.
953 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800954 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700955 }
956 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700957 endActionMode();
958
John Reck30c714c2010-12-16 17:30:34 -0800959 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700960
John Reck324d4402011-01-11 16:56:42 -0800961 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700962 // update the bookmark database for favicon
963 maybeUpdateFavicon(tab, null, url, favicon);
964
965 Performance.tracePageStart(url);
966
967 // Performance probe
968 if (false) {
969 Performance.onPageStarted();
970 }
971
972 }
973
974 @Override
John Reck324d4402011-01-11 16:56:42 -0800975 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700976 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800977 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200978
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 // Performance probe
980 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800981 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 }
983
Tarun Nainani8eb00912014-07-17 12:28:32 -0700984 tab.onPageFinished();
985
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 Performance.tracePageFinished();
987 }
988
989 @Override
John Reck30c714c2010-12-16 17:30:34 -0800990 public void onProgressChanged(Tab tab) {
991 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700992
993 if (newProgress == 100) {
994 CookieSyncManager.getInstance().sync();
995 // onProgressChanged() may continue to be called after the main
996 // frame has finished loading, as any remaining sub frames continue
997 // to load. We'll only get called once though with newProgress as
998 // 100 when everything is loaded. (onPageFinished is called once
999 // when the main frame completes loading regardless of the state of
1000 // any sub frames so calls to onProgressChanges may continue after
1001 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001002 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001003 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001004 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001005 } else if (mWasInPageLoad) {
1006 mWasInPageLoad = false;
1007 updateInLoadMenuItems(mCachedMenu, tab);
1008 }
1009
1010 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001011 // pause the WebView timer and release the wake lock if it is
1012 // finished while BrowserActivity is in pause state.
1013 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001014 }
John Reckd9862372012-02-21 15:04:50 -08001015 if (!tab.isPrivateBrowsingEnabled()
1016 && !TextUtils.isEmpty(tab.getUrl())
1017 && !tab.isSnapshot()) {
1018 // Only update the bookmark screenshot if the user did not
1019 // cancel the load early and there is not already
1020 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001021 if (tab.shouldUpdateThumbnail() &&
1022 (tab.inForeground() && !didUserStopLoading()
1023 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001024 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1025 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1026 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001027 1500);
John Reckd9862372012-02-21 15:04:50 -08001028 }
1029 }
1030 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001032 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001033 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001034 // still loading
1035 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001036 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001037 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001038 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001039 } else {
1040 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 }
1042 }
John Reck30c714c2010-12-16 17:30:34 -08001043 mUi.onProgressChanged(tab);
1044 }
1045
1046 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001047 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001048 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001049 }
1050
1051 @Override
1052 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001053 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001054 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001055 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1057 return;
1058 }
1059 // Update the title in the history database if not in private browsing mode
1060 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001061 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 }
1063 }
1064
1065 @Override
1066 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001067 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001068 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1069 }
1070
1071 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001072 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1073 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001074 }
1075
1076 @Override
1077 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1078 if (mMenuIsDown) {
1079 // only check shortcut key when MENU is held
1080 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1081 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001083 int keyCode = event.getKeyCode();
1084 // We need to send almost every key to WebKit. However:
1085 // 1. We don't want to block the device on the renderer for
1086 // some keys like menu, home, call.
1087 // 2. There are no WebKit equivalents for some of these keys
1088 // (see app/keyboard_codes_win.h)
1089 // Note that these are not the same set as KeyEvent.isSystemKey:
1090 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1091 if (keyCode == KeyEvent.KEYCODE_MENU ||
1092 keyCode == KeyEvent.KEYCODE_HOME ||
1093 keyCode == KeyEvent.KEYCODE_BACK ||
1094 keyCode == KeyEvent.KEYCODE_CALL ||
1095 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1096 keyCode == KeyEvent.KEYCODE_POWER ||
1097 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1098 keyCode == KeyEvent.KEYCODE_CAMERA ||
1099 keyCode == KeyEvent.KEYCODE_FOCUS ||
1100 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1101 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1102 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1103 return true;
1104 }
1105
1106 // We also have to intercept some shortcuts before we send them to the ContentView.
1107 if (event.isCtrlPressed() && (
1108 keyCode == KeyEvent.KEYCODE_TAB ||
1109 keyCode == KeyEvent.KEYCODE_W ||
1110 keyCode == KeyEvent.KEYCODE_F4)) {
1111 return true;
1112 }
1113
1114 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 }
1116
Tarun Nainanif5563b62015-01-21 18:52:59 -08001117 private void handleMediaKeyEvent(KeyEvent event) {
1118
1119 int keyCode = event.getKeyCode();
1120 // send media key events to audio manager
1121 if (Build.VERSION.SDK_INT >= 19) {
1122
1123 switch (keyCode) {
1124 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1125 case KeyEvent.KEYCODE_MEDIA_STOP:
1126 case KeyEvent.KEYCODE_MEDIA_NEXT:
1127 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1128 case KeyEvent.KEYCODE_MEDIA_REWIND:
1129 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1130 case KeyEvent.KEYCODE_MUTE:
1131 case KeyEvent.KEYCODE_MEDIA_PLAY:
1132 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1133 case KeyEvent.META_SHIFT_RIGHT_ON:
1134 case KeyEvent.KEYCODE_MEDIA_EJECT:
1135 case KeyEvent.KEYCODE_MEDIA_RECORD:
1136 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1137
1138 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1139 .getSystemService(Context.AUDIO_SERVICE);
1140 audioManager.dispatchMediaKeyEvent(event);
1141 }
1142 }
1143 }
1144
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 @Override
John Reck997b1b72012-04-19 18:08:25 -07001146 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001147 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001148 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001149 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001150 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 } else {
John Reck997b1b72012-04-19 18:08:25 -07001152 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001153 }
1154 }
John Reck997b1b72012-04-19 18:08:25 -07001155 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 }
1157
1158 @Override
John Reck324d4402011-01-11 16:56:42 -08001159 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001160 // Don't save anything in private browsing mode or when disabling history
1161 // for regular tabs is enabled
1162 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1163 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1164 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001165
John Reck49a603c2011-03-03 09:33:05 -08001166 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001167
John Reck324d4402011-01-11 16:56:42 -08001168 if (TextUtils.isEmpty(url)
1169 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 return;
1171 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001172
John Reckf57c0292011-07-21 18:15:39 -07001173 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001174 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001175 }
1176
1177 @Override
1178 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1179 AsyncTask<Void, Void, String[]> task =
1180 new AsyncTask<Void, Void, String[]>() {
1181 @Override
1182 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001183 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001184 }
1185 @Override
1186 public void onPostExecute(String[] result) {
1187 callback.onReceiveValue(result);
1188 }
1189 };
1190 task.execute();
1191 }
1192
1193 @Override
1194 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1195 final HttpAuthHandler handler, final String host,
1196 final String realm) {
1197 String username = null;
1198 String password = null;
1199
1200 boolean reuseHttpAuthUsernamePassword
1201 = handler.useHttpAuthUsernamePassword();
1202
1203 if (reuseHttpAuthUsernamePassword && view != null) {
1204 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1205 if (credentials != null && credentials.length == 2) {
1206 username = credentials[0];
1207 password = credentials[1];
1208 }
1209 }
1210
1211 if (username != null && password != null) {
1212 handler.proceed(username, password);
1213 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001214 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001215 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1216 } else {
1217 handler.cancel();
1218 }
1219 }
1220 }
1221
1222 @Override
1223 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001224 String contentDisposition, String mimetype, String referer,
1225 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001226 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001227 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001228 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001229 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001230 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 // This Tab was opened for the sole purpose of downloading a
1232 // file. Remove it.
1233 if (tab == mTabControl.getCurrentTab()) {
1234 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001235 if (tab.getDerivedFromIntent())
1236 closeTab(tab);
1237 else
1238 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 } else {
1240 // In this case, it is not.
1241 closeTab(tab);
1242 }
1243 }
1244 }
1245
1246 @Override
1247 public Bitmap getDefaultVideoPoster() {
1248 return mUi.getDefaultVideoPoster();
1249 }
1250
1251 @Override
1252 public View getVideoLoadingProgressView() {
1253 return mUi.getVideoLoadingProgressView();
1254 }
1255
1256 @Override
1257 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1258 SslError error) {
1259 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1260 }
1261
1262 // helper method
1263
1264 /*
1265 * Update the favorites icon if the private browsing isn't enabled and the
1266 * icon is valid.
1267 */
1268 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1269 final String url, Bitmap favicon) {
1270 if (favicon == null) {
1271 return;
1272 }
1273 if (!tab.isPrivateBrowsingEnabled()) {
1274 Bookmarks.updateFavicon(mActivity
1275 .getContentResolver(), originalUrl, url, favicon);
1276 }
1277 }
1278
Leon Scroggins4cd97792010-12-03 15:31:56 -05001279 @Override
1280 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001281 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001282 mUi.bookmarkedStatusHasChanged(tab);
1283 }
1284
Michael Kolb8233fac2010-10-26 16:08:53 -07001285 // end WebViewController
1286
1287 protected void pageUp() {
1288 getCurrentTopWebView().pageUp(false);
1289 }
1290
1291 protected void pageDown() {
1292 getCurrentTopWebView().pageDown(false);
1293 }
1294
1295 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001296 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001297 public void editUrl() {
1298 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001299 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001300 }
1301
John Reck9c35b9c2012-05-30 10:08:50 -07001302 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001303 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001304 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001305 if (tab.inForeground()) {
1306 if (mUi.isCustomViewShowing()) {
1307 callback.onCustomViewHidden();
1308 return;
1309 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001310 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001311 // Save the menu state and set it to empty while the custom
1312 // view is showing.
1313 mOldMenuState = mMenuState;
1314 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001315 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001316 }
1317 }
1318
1319 @Override
1320 public void hideCustomView() {
1321 if (mUi.isCustomViewShowing()) {
1322 mUi.onHideCustomView();
1323 // Reset the old menu state.
1324 mMenuState = mOldMenuState;
1325 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001326 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 }
1328 }
1329
John Reck9c35b9c2012-05-30 10:08:50 -07001330 @Override
1331 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001332 Intent intent) {
1333 if (getCurrentTopWebView() == null) return;
1334 switch (requestCode) {
1335 case PREFERENCES_PAGE:
1336 if (resultCode == Activity.RESULT_OK && intent != null) {
1337 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001338 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001339 mTabControl.removeParentChildRelationShips();
1340 }
1341 }
1342 break;
1343 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001344 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001345 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001346 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001347 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001348 case AUTOFILL_SETUP:
1349 // Determine whether a profile was actually set up or not
1350 // and if so, send the message back to the WebTextView to
1351 // fill the form with the new profile.
1352 if (getSettings().getAutoFillProfile() != null) {
1353 mAutoFillSetupMessage.sendToTarget();
1354 mAutoFillSetupMessage = null;
1355 }
1356 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001357 case COMBO_VIEW:
1358 if (intent == null || resultCode != Activity.RESULT_OK) {
1359 break;
1360 }
John Reck3ba45532011-08-11 16:26:53 -07001361 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001362 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1363 Tab t = getCurrentTab();
1364 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001365 mUpdateMyNavThumbnail = true;
1366 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001367 loadUrl(t, uri.toString());
1368 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1369 String[] urls = intent.getStringArrayExtra(
1370 ComboViewActivity.EXTRA_OPEN_ALL);
1371 Tab parent = getCurrentTab();
1372 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001373 if (url != null) {
1374 parent = openTab(url, parent,
1375 !mSettings.openInBackground(), true);
1376 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001377 }
1378 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1379 long id = intent.getLongExtra(
1380 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1381 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001382 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001383 }
1384 }
1385 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001386 case VOICE_RESULT:
1387 if (resultCode == Activity.RESULT_OK && intent != null) {
1388 ArrayList<String> results = intent.getStringArrayListExtra(
1389 RecognizerIntent.EXTRA_RESULTS);
1390 if (results.size() >= 1) {
1391 mVoiceResult = results.get(0);
1392 }
1393 }
1394 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001395 case MY_NAVIGATION:
1396 if (intent == null || resultCode != Activity.RESULT_OK) {
1397 break;
1398 }
1399
1400 if (intent.getBooleanExtra("need_refresh", false) &&
1401 getCurrentTopWebView() != null) {
1402 getCurrentTopWebView().reload();
1403 }
1404 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001405 default:
1406 break;
1407 }
1408 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001409 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 }
1411
1412 /**
1413 * Open the Go page.
1414 * @param startWithHistory If true, open starting on the history tab.
1415 * Otherwise, start with the bookmarks tab.
1416 */
1417 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001418 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 if (mTabControl.getCurrentWebView() == null) {
1420 return;
1421 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001422 // clear action mode
1423 if (isInCustomActionMode()) {
1424 endActionMode();
1425 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001426 Bundle extras = new Bundle();
1427 // Disable opening in a new window if we have maxed out the windows
1428 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1429 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001430 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001431 }
1432
1433 // combo view callbacks
1434
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 // key handling
1436 protected void onBackKey() {
1437 if (!mUi.onBackKey()) {
1438 WebView subwindow = mTabControl.getCurrentSubWindow();
1439 if (subwindow != null) {
1440 if (subwindow.canGoBack()) {
1441 subwindow.goBack();
1442 } else {
1443 dismissSubWindow(mTabControl.getCurrentTab());
1444 }
1445 } else {
1446 goBackOnePageOrQuit();
1447 }
1448 }
1449 }
1450
Michael Kolb4bd767d2011-05-27 11:33:55 -07001451 protected boolean onMenuKey() {
1452 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001453 }
1454
Michael Kolb8233fac2010-10-26 16:08:53 -07001455 // menu handling and state
1456 // TODO: maybe put into separate handler
1457
John Reck9c35b9c2012-05-30 10:08:50 -07001458 @Override
1459 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001460 if (mMenuState == EMPTY_MENU) {
1461 return false;
1462 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 MenuInflater inflater = mActivity.getMenuInflater();
1464 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001465 return true;
1466 }
1467
John Reck9c35b9c2012-05-30 10:08:50 -07001468 @Override
1469 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001470 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001471 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001472 return;
1473 }
1474 if (!(v instanceof WebView)) {
1475 return;
1476 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001477 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001478 WebView.HitTestResult result;
1479
1480 /* Determine whether the ContextMenu got triggered because
1481 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1482 * received. The mResult acts as a flag to identify, how it got trigerred
1483 */
1484 if (mResult == null){
1485 result = webview.getHitTestResult();
1486 } else {
1487 result = mResult;
1488 }
1489
Michael Kolb8233fac2010-10-26 16:08:53 -07001490 if (result == null) {
1491 return;
1492 }
1493
1494 int type = result.getType();
1495 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001496
1497 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1498 unknownTypeMap.put("webview", webview);
1499 final Message msg = mHandler.obtainMessage(
1500 UNKNOWN_TYPE_MSG, unknownTypeMap);
1501 /* As defined in android developers guide
1502 * when UNKNOWN_TYPE is received as a result of HitTest
1503 * you need to determing the type by invoking requestFocusNodeHref
1504 */
1505 webview.requestFocusNodeHref(msg);
1506
Michael Kolb8233fac2010-10-26 16:08:53 -07001507 Log.w(LOGTAG,
1508 "We should not show context menu when nothing is touched");
1509 return;
1510 }
1511 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1512 // let TextView handles context menu
1513 return;
1514 }
1515
1516 // Note, http://b/issue?id=1106666 is requesting that
1517 // an inflated menu can be used again. This is not available
1518 // yet, so inflate each time (yuk!)
1519 MenuInflater inflater = mActivity.getMenuInflater();
1520 inflater.inflate(R.menu.browsercontext, menu);
1521
1522 // Show the correct menu group
1523 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001524 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001525 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001526 menu.setGroupVisible(R.id.PHONE_MENU,
1527 type == WebView.HitTestResult.PHONE_TYPE);
1528 menu.setGroupVisible(R.id.EMAIL_MENU,
1529 type == WebView.HitTestResult.EMAIL_TYPE);
1530 menu.setGroupVisible(R.id.GEO_MENU,
1531 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001532 String itemUrl = null;
1533 String url = webview.getOriginalUrl();
1534 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1535 itemUrl = Uri.decode(navigationUrl);
1536 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1537 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1538 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1539 } else {
1540 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1541 }
1542 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1543 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1544 } else {
1545 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1546
1547 menu.setGroupVisible(R.id.IMAGE_MENU,
1548 type == WebView.HitTestResult.IMAGE_TYPE
1549 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1550 menu.setGroupVisible(R.id.ANCHOR_MENU,
1551 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1552 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001553 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1554 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001555 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001556 // Setup custom handling depending on the type
1557 switch (type) {
1558 case WebView.HitTestResult.PHONE_TYPE:
1559 menu.setHeaderTitle(Uri.decode(extra));
1560 menu.findItem(R.id.dial_context_menu_id).setIntent(
1561 new Intent(Intent.ACTION_VIEW, Uri
1562 .parse(WebView.SCHEME_TEL + extra)));
1563 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1564 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1565 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1566 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1567 addIntent);
1568 menu.findItem(R.id.copy_phone_context_menu_id)
1569 .setOnMenuItemClickListener(
1570 new Copy(extra));
1571 break;
1572
1573 case WebView.HitTestResult.EMAIL_TYPE:
1574 menu.setHeaderTitle(extra);
1575 menu.findItem(R.id.email_context_menu_id).setIntent(
1576 new Intent(Intent.ACTION_VIEW, Uri
1577 .parse(WebView.SCHEME_MAILTO + extra)));
1578 menu.findItem(R.id.copy_mail_context_menu_id)
1579 .setOnMenuItemClickListener(
1580 new Copy(extra));
1581 break;
1582
1583 case WebView.HitTestResult.GEO_TYPE:
1584 menu.setHeaderTitle(extra);
1585 menu.findItem(R.id.map_context_menu_id).setIntent(
1586 new Intent(Intent.ACTION_VIEW, Uri
1587 .parse(WebView.SCHEME_GEO
1588 + URLEncoder.encode(extra))));
1589 menu.findItem(R.id.copy_geo_context_menu_id)
1590 .setOnMenuItemClickListener(
1591 new Copy(extra));
1592 break;
1593
1594 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1595 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001596 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001597 // decide whether to show the open link in new tab option
1598 boolean showNewTab = mTabControl.canCreateNewTab();
1599 MenuItem newTabItem
1600 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001601 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001602 ? R.string.contextmenu_openlink_newwindow_background
1603 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001604 newTabItem.setVisible(showNewTab);
1605 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001606 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1607 newTabItem.setOnMenuItemClickListener(
1608 new MenuItem.OnMenuItemClickListener() {
1609 @Override
1610 public boolean onMenuItemClick(MenuItem item) {
1611 final HashMap<String, WebView> hrefMap =
1612 new HashMap<String, WebView>();
1613 hrefMap.put("webview", webview);
1614 final Message msg = mHandler.obtainMessage(
1615 FOCUS_NODE_HREF,
1616 R.id.open_newtab_context_menu_id,
1617 0, hrefMap);
1618 webview.requestFocusNodeHref(msg);
1619 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001620 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001621 });
1622 } else {
1623 newTabItem.setOnMenuItemClickListener(
1624 new MenuItem.OnMenuItemClickListener() {
1625 @Override
1626 public boolean onMenuItemClick(MenuItem item) {
1627 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001628 openTab(extra, parent,
1629 !mSettings.openInBackground(),
1630 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001631 return true;
1632 }
1633 });
1634 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001635 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001636 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1637 menu.setHeaderTitle(navigationUrl);
1638 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1639
1640 if (itemUrl != null) {
1641 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1642 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1643 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1644 @Override
1645 public boolean onMenuItemClick(MenuItem item) {
1646 final Intent intent = new Intent(Controller.this
1647 .getContext(),
1648 AddMyNavigationPage.class);
1649 Bundle bundle = new Bundle();
1650 String url = Uri.decode(navigationUrl);
1651 bundle.putBoolean("isAdding", false);
1652 bundle.putString("url", url);
1653 bundle.putString("name", getNameFromUrl(url));
1654 intent.putExtra("websites", bundle);
1655 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1656 return false;
1657 }
1658 });
1659 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1660 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1661 @Override
1662 public boolean onMenuItemClick(MenuItem item) {
1663 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1664 return false;
1665 }
1666 });
1667 }
1668 } else {
1669 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1670 }
1671 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001672 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1673 break;
1674 }
1675 // otherwise fall through to handle image part
1676 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001677 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1678 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001679 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1680 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001681 shareItem.setOnMenuItemClickListener(
1682 new MenuItem.OnMenuItemClickListener() {
1683 @Override
1684 public boolean onMenuItemClick(MenuItem item) {
1685 sharePage(mActivity, null, extra, null,
1686 null);
1687 return true;
1688 }
1689 }
1690 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001691 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001692 menu.findItem(R.id.view_image_context_menu_id)
1693 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1694 @Override
1695 public boolean onMenuItemClick(MenuItem item) {
1696 openTab(extra, mTabControl.getCurrentTab(), true, true);
1697 return false;
1698 }
1699 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001700 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1701 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1702 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001703 menu.findItem(R.id.set_wallpaper_context_menu_id).
1704 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1705 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001706 break;
1707
1708 default:
1709 Log.w(LOGTAG, "We should not get here.");
1710 break;
1711 }
1712 //update the ui
1713 mUi.onContextMenuCreated(menu);
1714 }
1715
kaiyiz6e5b3e02013-08-19 20:02:01 +08001716 public void startAddMyNavigation(String url) {
1717 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1718 Bundle bundle = new Bundle();
1719 bundle.putBoolean("isAdding", true);
1720 bundle.putString("url", url);
1721 bundle.putString("name", getNameFromUrl(url));
1722 intent.putExtra("websites", bundle);
1723 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1724 }
1725
1726 private void showMyNavigationDeleteDialog(final String itemUrl) {
1727 new AlertDialog.Builder(this.getContext())
1728 .setTitle(R.string.my_navigation_delete_label)
1729 .setIcon(android.R.drawable.ic_dialog_alert)
1730 .setMessage(R.string.my_navigation_delete_msg)
1731 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1732 @Override
1733 public void onClick(DialogInterface dialog, int whichButton) {
1734 deleteMyNavigationItem(itemUrl);
1735 }
1736 })
1737 .setNegativeButton(R.string.cancel, null)
1738 .show();
1739 }
1740
1741 private void deleteMyNavigationItem(final String itemUrl) {
1742 ContentResolver cr = this.getContext().getContentResolver();
1743 Cursor cursor = null;
1744
1745 try {
1746 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1747 new String[] {
1748 MyNavigationUtil.ID
1749 }, "url = ?", new String[] {
1750 itemUrl
1751 }, null);
1752 if (null != cursor && cursor.moveToFirst()) {
1753 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1754 cursor.getLong(0));
1755
1756 ContentValues values = new ContentValues();
1757 values.put(MyNavigationUtil.TITLE, "");
1758 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1759 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1760 ByteArrayOutputStream os = new ByteArrayOutputStream();
1761 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1762 R.raw.my_navigation_add);
1763 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1764 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1765 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1766 cr.update(uri, values, null, null);
1767 } else {
1768 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1769 }
1770 } catch (IllegalStateException e) {
1771 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1772 } finally {
1773 if (null != cursor) {
1774 cursor.close();
1775 }
1776 }
1777
1778 if (getCurrentTopWebView() != null) {
1779 getCurrentTopWebView().reload();
1780 }
1781 }
1782
1783 private String getNameFromUrl(String itemUrl) {
1784 ContentResolver cr = this.getContext().getContentResolver();
1785 Cursor cursor = null;
1786 String name = null;
1787
1788 try {
1789 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1790 new String[] {
1791 MyNavigationUtil.TITLE
1792 }, "url = ?", new String[] {
1793 itemUrl
1794 }, null);
1795 if (null != cursor && cursor.moveToFirst()) {
1796 name = cursor.getString(0);
1797 } else {
1798 Log.e(LOGTAG, "this item does not exist!");
1799 }
1800 } catch (IllegalStateException e) {
1801 Log.e(LOGTAG, "getNameFromUrl", e);
1802 } finally {
1803 if (null != cursor) {
1804 cursor.close();
1805 }
1806 }
1807 return name;
1808 }
1809
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001810 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001811 final ContentResolver cr = mActivity.getContentResolver();
1812 new AsyncTask<Void, Void, Void>() {
1813 @Override
1814 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001815 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1816 if(!isMyNavigationUrl)
1817 return null;
1818
kaiyiz6e5b3e02013-08-19 20:02:01 +08001819 ContentResolver cr = mActivity.getContentResolver();
1820 Cursor cursor = null;
1821 try {
1822 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1823 new String[] {
1824 MyNavigationUtil.ID
1825 }, "url = ?", new String[] {
1826 itemUrl
1827 }, null);
1828 if (null != cursor && cursor.moveToFirst()) {
1829 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001830 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001831
1832 ContentValues values = new ContentValues();
1833 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1834 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1835 cursor.getLong(0));
1836 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1837 cr.update(uri, values, null, null);
1838 os.close();
1839 }
1840 } catch (IllegalStateException e) {
1841 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1842 } catch (IOException e) {
1843 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1844 } finally {
1845 if (null != cursor) {
1846 cursor.close();
1847 }
1848 }
1849 return null;
1850 }
1851 }.execute();
1852 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001853 /**
1854 * As the menu can be open when loading state changes
1855 * we must manually update the state of the stop/reload menu
1856 * item
1857 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001858 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001859 if (menu == null) {
1860 return;
1861 }
1862 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001863 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001864 menu.findItem(R.id.stop_menu_id):
1865 menu.findItem(R.id.reload_menu_id);
1866 if (src != null) {
1867 dest.setIcon(src.getIcon());
1868 dest.setTitle(src.getTitle());
1869 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001870 mActivity.invalidateOptionsMenu();
1871 }
1872
1873 public void invalidateOptionsMenu() {
1874 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001875 }
1876
John Reck9c35b9c2012-05-30 10:08:50 -07001877 @Override
1878 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001879 // Software menu key (toolbar key)
1880 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1881 return true;
1882 }
1883
1884 @Override
1885 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001886 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001887 // hold on to the menu reference here; it is used by the page callbacks
1888 // to update the menu based on loading state
1889 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001890 // Note: setVisible will decide whether an item is visible; while
1891 // setEnabled() will decide whether an item is enabled, which also means
1892 // whether the matching shortcut key will function.
1893 switch (mMenuState) {
1894 case EMPTY_MENU:
1895 if (mCurrentMenuState != mMenuState) {
1896 menu.setGroupVisible(R.id.MAIN_MENU, false);
1897 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1898 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1899 }
1900 break;
1901 default:
1902 if (mCurrentMenuState != mMenuState) {
1903 menu.setGroupVisible(R.id.MAIN_MENU, true);
1904 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1905 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1906 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001907 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001908 break;
1909 }
1910 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001911 mUi.onPrepareOptionsMenu(menu);
1912 }
1913
1914 private void setMenuItemVisibility(Menu menu, int id,
1915 boolean visibility) {
1916 MenuItem item = menu.findItem(id);
1917 if (item != null) {
1918 item.setVisible(visibility);
1919 }
1920 }
1921
1922 private int lookupBookmark(String title, String url) {
1923 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001924 int count = 0;
1925 Cursor cursor = null;
1926 try {
1927 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1928 BookmarksLoader.PROJECTION,
1929 "title = ? OR url = ?",
1930 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001931 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001932 },
1933 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001934
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001935 if (cursor != null)
1936 count = cursor.getCount();
1937
1938 } catch (IllegalStateException e) {
1939 Log.e(LOGTAG, "lookupBookmark ", e);
1940 } finally {
1941 if (null != cursor) {
1942 cursor.close();
1943 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001944 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001945 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001946 }
1947
1948 private void resetMenuItems(Menu menu) {
1949 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1950 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1951
1952 WebView w = getCurrentTopWebView();
1953 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1954
1955 String title = w.getTitle();
1956 String url = w.getUrl();
1957 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1958 bookmark_icon.setChecked(true);
1959 } else {
1960 bookmark_icon.setChecked(false);
1961 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001962 }
1963
Michael Kolb4bf79712011-07-14 14:18:12 -07001964 @Override
1965 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001966 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001967 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001968 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001969 // Following flag is used to identify schemes for which the LIVE_MENU
1970 // items defined in res/menu/browser.xml should be enabled
1971 boolean isLiveScheme = false;
1972 boolean isPageFinished = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001973
1974 resetMenuItems(menu);
1975
Michael Kolb4bf79712011-07-14 14:18:12 -07001976 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001977 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001978 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001979 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001980 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001981 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Michael Kolb4bf79712011-07-14 14:18:12 -07001982 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001983
1984 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1985 forward.setEnabled(canGoForward);
1986
Michael Kolb4bf79712011-07-14 14:18:12 -07001987 // decide whether to show the share link option
1988 PackageManager pm = mActivity.getPackageManager();
1989 Intent send = new Intent(Intent.ACTION_SEND);
1990 send.setType("text/plain");
1991 ResolveInfo ri = pm.resolveActivity(send,
1992 PackageManager.MATCH_DEFAULT_ONLY);
1993 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1994
1995 boolean isNavDump = mSettings.enableNavDump();
1996 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1997 nav.setVisible(isNavDump);
1998 nav.setEnabled(isNavDump);
1999
2000 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002001 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2002 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002003 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002004 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2005 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002006 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002007 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2008 isLive && isLiveScheme && isPageFinished);
2009 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
2010 isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002011 // history and snapshots item are the members of COMBO menu group,
2012 // so if show history item, only make snapshots item invisible.
2013 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002014
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002015 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002016 }
2017
John Reck9c35b9c2012-05-30 10:08:50 -07002018 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002019 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002020 if (null == getCurrentTopWebView()) {
2021 return false;
2022 }
2023 if (mMenuIsDown) {
2024 // The shortcut action consumes the MENU. Even if it is still down,
2025 // it won't trigger the next shortcut action. In the case of the
2026 // shortcut action triggering a new activity, like Bookmarks, we
2027 // won't get onKeyUp for MENU. So it is important to reset it here.
2028 mMenuIsDown = false;
2029 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002030 if (mUi.onOptionsItemSelected(item)) {
2031 // ui callback handled it
2032 return true;
2033 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002034 switch (item.getItemId()) {
2035 // -- Main menu
2036 case R.id.new_tab_menu_id:
2037 openTabToHomePage();
2038 break;
2039
2040 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002041 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 break;
2043
2044 case R.id.goto_menu_id:
2045 editUrl();
2046 break;
2047
2048 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002049 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2050 break;
2051
2052 case R.id.history_menu_id:
2053 bookmarksOrHistoryPicker(ComboViews.History);
2054 break;
2055
2056 case R.id.snapshots_menu_id:
2057 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002058 break;
2059
Pankaj Garg49b79252014-11-07 17:33:41 -08002060 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002061 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002062 break;
2063
2064 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002065 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002066 stopLoading();
2067 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002068 Tab currentTab = mTabControl.getCurrentTab();
2069 if (currentTab.hasCrashed) {
2070 currentTab.replaceCrashView(getCurrentTopWebView(),
2071 currentTab.getViewContainer());
2072 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002073 getCurrentTopWebView().reload();
2074 }
2075 break;
2076
Michael Kolb8233fac2010-10-26 16:08:53 -07002077 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002078 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002079 break;
2080
2081 case R.id.close_menu_id:
2082 // Close the subwindow if it exists.
2083 if (mTabControl.getCurrentSubWindow() != null) {
2084 dismissSubWindow(mTabControl.getCurrentTab());
2085 break;
2086 }
2087 closeCurrentTab();
2088 break;
2089
kaiyiza8b6dbb2013-07-29 18:11:22 +08002090 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002091 Object[] params = { new String("persist.debug.browsermonkeytest")};
2092 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002093 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002094 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002095 if (ret != null && ret.equals("enable"))
2096 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002097 if (BrowserConfig.getInstance(getContext())
2098 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2099 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002100 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002101 case R.id.homepage_menu_id:
2102 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002103 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002104 break;
2105
2106 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002107 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002108 break;
2109
2110 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002111 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002112 break;
2113
John Reck2bc80422011-06-30 15:11:49 -07002114 case R.id.save_snapshot_menu_id:
2115 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002116 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002117 createScreenshotAsync(
2118 source.getWebView(),
2119 getDesiredThumbnailWidth(mActivity),
2120 getDesiredThumbnailHeight(mActivity),
2121 new ValueCallback<Bitmap>() {
2122 @Override
2123 public void onReceiveValue(Bitmap bitmap) {
2124 new SaveSnapshotTask(source, bitmap).execute();
2125 }
2126 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002127 break;
2128
Michael Kolb8233fac2010-10-26 16:08:53 -07002129 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002130 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002131 break;
2132
John Recke1a03a32011-09-14 17:04:16 -07002133 case R.id.snapshot_go_live:
2134 goLive();
2135 return true;
2136
Michael Kolb8233fac2010-10-26 16:08:53 -07002137 case R.id.share_page_menu_id:
2138 Tab currentTab = mTabControl.getCurrentTab();
2139 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002140 return false;
2141 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002142 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002143 break;
2144
2145 case R.id.dump_nav_menu_id:
2146 getCurrentTopWebView().debugDump();
2147 break;
2148
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 case R.id.zoom_in_menu_id:
2150 getCurrentTopWebView().zoomIn();
2151 break;
2152
2153 case R.id.zoom_out_menu_id:
2154 getCurrentTopWebView().zoomOut();
2155 break;
2156
2157 case R.id.view_downloads_menu_id:
2158 viewDownloads();
2159 break;
2160
John Reck42229bc2011-08-19 13:26:43 -07002161 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002162 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002163 break;
2164
Michael Kolb8233fac2010-10-26 16:08:53 -07002165 case R.id.window_one_menu_id:
2166 case R.id.window_two_menu_id:
2167 case R.id.window_three_menu_id:
2168 case R.id.window_four_menu_id:
2169 case R.id.window_five_menu_id:
2170 case R.id.window_six_menu_id:
2171 case R.id.window_seven_menu_id:
2172 case R.id.window_eight_menu_id:
2173 {
2174 int menuid = item.getItemId();
2175 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2176 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2177 Tab desiredTab = mTabControl.getTab(id);
2178 if (desiredTab != null &&
2179 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002180 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002181 }
2182 break;
2183 }
2184 }
2185 }
2186 break;
2187
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002188 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002189 Bundle bundle = new Bundle();
2190 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2191 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2192 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2193 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2194 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002195 break;
2196
Pankaj Garg49b79252014-11-07 17:33:41 -08002197 case R.id.add_to_homescreen:
2198 final WebView w = getCurrentTopWebView();
2199 final EditText input = new EditText(getContext());
2200 input.setText(w.getTitle());
2201 new AlertDialog.Builder(getContext())
2202 .setTitle("Add to homescreen")
2203 .setMessage("Title")
2204 .setView(input)
2205 .setPositiveButton("Add", new DialogInterface.OnClickListener() {
2206 public void onClick(DialogInterface dialog, int whichButton) {
2207 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2208 getContext(),
2209 w.getUrl(),
2210 input.getText().toString(),
2211 w.getViewportBitmap(),
2212 w.getFavicon()));
2213
2214 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2215 .addCategory(Intent.CATEGORY_HOME));
2216 }})
2217 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
2218 public void onClick(DialogInterface dialog, int whichButton) {
2219 // Do nothing.
2220 }
2221 })
2222 .show();
2223 break;
2224
Michael Kolb8233fac2010-10-26 16:08:53 -07002225 default:
2226 return false;
2227 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002228 return true;
2229 }
2230
John Reck68234a92012-04-19 15:27:12 -07002231 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2232 implements OnCancelListener {
2233
2234 private Tab mTab;
2235 private Dialog mProgressDialog;
2236 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002237 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002238
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002239 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002240 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002241 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002242 }
2243
2244 @Override
2245 protected void onPreExecute() {
2246 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2247 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2248 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002249 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002250 }
2251
2252 @Override
2253 protected Long doInBackground(Void... params) {
2254 if (!mTab.saveViewState(mValues)) {
2255 return null;
2256 }
2257 if (isCancelled()) {
2258 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2259 File file = mActivity.getFileStreamPath(path);
2260 if (!file.delete()) {
2261 file.deleteOnExit();
2262 }
2263 return null;
2264 }
2265 final ContentResolver cr = mActivity.getContentResolver();
2266 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2267 if (result == null) {
2268 return null;
2269 }
2270 long id = ContentUris.parseId(result);
2271 return id;
2272 }
2273
2274 @Override
2275 protected void onPostExecute(Long id) {
2276 if (isCancelled()) {
2277 return;
2278 }
2279 mProgressDialog.dismiss();
2280 if (id == null) {
2281 Toast.makeText(mActivity, R.string.snapshot_failed,
2282 Toast.LENGTH_SHORT).show();
2283 return;
2284 }
2285 Bundle b = new Bundle();
2286 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2287 mUi.showComboView(ComboViews.Snapshots, b);
2288 }
2289
2290 @Override
2291 public void onCancel(DialogInterface dialog) {
2292 cancel(true);
2293 }
2294 }
2295
Michael Kolb80f75082012-04-10 10:50:06 -07002296 @Override
2297 public void toggleUserAgent() {
2298 WebView web = getCurrentWebView();
2299 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002300 }
2301
2302 @Override
2303 public void findOnPage() {
2304 getCurrentTopWebView().showFindDialog(null, true);
2305 }
2306
2307 @Override
2308 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002309 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002310 }
2311
2312 @Override
2313 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002314 WebView w = getCurrentTopWebView();
2315 if (w == null)
2316 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002317 final Intent i = createBookmarkCurrentPageIntent(false);
2318 createScreenshotAsync(
2319 w, getDesiredThumbnailWidth(mActivity),
2320 getDesiredThumbnailHeight(mActivity),
2321 new ValueCallback<Bitmap>() {
2322 @Override
2323 public void onReceiveValue(Bitmap bitmap) {
2324 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2325 mActivity.startActivity(i);
2326 }
2327 });
Michael Kolb80f75082012-04-10 10:50:06 -07002328 }
2329
John Recke1a03a32011-09-14 17:04:16 -07002330 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002331 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002332 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002333 boolean onlySingleTabRemaining = false;
2334 if (mTabControl.getTabCount() > 1) {
2335 // destroy the old snapshot tab
2336 closeCurrentTab();
2337 } else {
2338 onlySingleTabRemaining = true;
2339 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002340 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002341 if (onlySingleTabRemaining) {
2342 closeTab(t);
2343 }
2344
Tarun Nainani8eb00912014-07-17 12:28:32 -07002345 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002346 }
2347
luxiaolb40014b2013-07-19 10:01:43 +08002348 private void showExitDialog(final Activity activity) {
2349 new AlertDialog.Builder(activity)
2350 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002351 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002352 .setMessage(R.string.exit_browser_msg)
2353 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2354 public void onClick(DialogInterface dialog, int which) {
2355 activity.moveTaskToBack(true);
2356 dialog.dismiss();
2357 }
2358 })
2359 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2360 public void onClick(DialogInterface dialog, int which) {
2361 activity.finish();
2362 mHandler.postDelayed(new Runnable() {
2363 @Override
2364 public void run() {
luxiaolb40014b2013-07-19 10:01:43 +08002365 mCrashRecoveryHandler.clearState(true);
2366 int pid = android.os.Process.myPid();
2367 android.os.Process.killProcess(pid);
2368 }
2369 }, 300);
2370 dialog.dismiss();
2371 }
2372 })
2373 .show();
2374 }
Michael Kolb315d5022011-10-13 12:47:11 -07002375 @Override
2376 public void showPageInfo() {
2377 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2378 }
2379
John Reck9c35b9c2012-05-30 10:08:50 -07002380 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002381 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002382 // Let the History and Bookmark fragments handle menus they created.
2383 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2384 return false;
2385 }
2386
Michael Kolb8233fac2010-10-26 16:08:53 -07002387 int id = item.getItemId();
2388 boolean result = true;
2389 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002390 // -- Browser context menu
2391 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002392 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002393 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002394 case R.id.copy_link_context_menu_id:
2395 final WebView webView = getCurrentTopWebView();
2396 if (null == webView) {
2397 result = false;
2398 break;
2399 }
2400 final HashMap<String, WebView> hrefMap =
2401 new HashMap<String, WebView>();
2402 hrefMap.put("webview", webView);
2403 final Message msg = mHandler.obtainMessage(
2404 FOCUS_NODE_HREF, id, 0, hrefMap);
2405 webView.requestFocusNodeHref(msg);
2406 break;
2407
2408 default:
2409 // For other context menus
2410 result = onOptionsItemSelected(item);
2411 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002412 return result;
2413 }
2414
2415 /**
2416 * support programmatically opening the context menu
2417 */
2418 public void openContextMenu(View view) {
2419 mActivity.openContextMenu(view);
2420 }
2421
2422 /**
2423 * programmatically open the options menu
2424 */
2425 public void openOptionsMenu() {
2426 mActivity.openOptionsMenu();
2427 }
2428
John Reck9c35b9c2012-05-30 10:08:50 -07002429 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002430 public boolean onMenuOpened(int featureId, Menu menu) {
2431 if (mOptionsMenuOpen) {
2432 if (mConfigChanged) {
2433 // We do not need to make any changes to the state of the
2434 // title bar, since the only thing that happened was a
2435 // change in orientation
2436 mConfigChanged = false;
2437 } else {
2438 if (!mExtendedMenuOpen) {
2439 mExtendedMenuOpen = true;
2440 mUi.onExtendedMenuOpened();
2441 } else {
2442 // Switching the menu back to icon view, so show the
2443 // title bar once again.
2444 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002445 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002446 }
2447 }
2448 } else {
2449 // The options menu is closed, so open it, and show the title
2450 mOptionsMenuOpen = true;
2451 mConfigChanged = false;
2452 mExtendedMenuOpen = false;
2453 mUi.onOptionsMenuOpened();
2454 }
2455 return true;
2456 }
2457
John Reck9c35b9c2012-05-30 10:08:50 -07002458 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002459 public void onOptionsMenuClosed(Menu menu) {
2460 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002461 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002462 }
2463
John Reck9c35b9c2012-05-30 10:08:50 -07002464 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002465 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002466 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002467 }
2468
2469 // Helper method for getting the top window.
2470 @Override
2471 public WebView getCurrentTopWebView() {
2472 return mTabControl.getCurrentTopWebView();
2473 }
2474
2475 @Override
2476 public WebView getCurrentWebView() {
2477 return mTabControl.getCurrentWebView();
2478 }
2479
2480 /*
2481 * This method is called as a result of the user selecting the options
2482 * menu to see the download window. It shows the download window on top of
2483 * the current window.
2484 */
2485 void viewDownloads() {
2486 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2487 mActivity.startActivity(intent);
2488 }
2489
John Reck30b065e2011-07-19 10:58:05 -07002490 int getActionModeHeight() {
2491 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2492 new int[] { android.R.attr.actionBarSize });
2493 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2494 actionBarSizeTypedArray.recycle();
2495 return size;
2496 }
2497
Michael Kolb8233fac2010-10-26 16:08:53 -07002498 // action mode
2499
John Reck9c35b9c2012-05-30 10:08:50 -07002500 @Override
2501 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002502 mUi.onActionModeStarted(mode);
2503 mActionMode = mode;
2504 }
2505
2506 /*
2507 * True if a custom ActionMode (i.e. find or select) is in use.
2508 */
2509 @Override
2510 public boolean isInCustomActionMode() {
2511 return mActionMode != null;
2512 }
2513
2514 /*
2515 * End the current ActionMode.
2516 */
2517 @Override
2518 public void endActionMode() {
2519 if (mActionMode != null) {
2520 mActionMode.finish();
2521 }
2522 }
2523
2524 /*
2525 * Called by find and select when they are finished. Replace title bars
2526 * as necessary.
2527 */
John Reck9c35b9c2012-05-30 10:08:50 -07002528 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002529 public void onActionModeFinished(ActionMode mode) {
2530 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002531 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 mActionMode = null;
2533 }
2534
2535 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002536 final Tab tab = getCurrentTab();
2537 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002538 }
2539
2540 // bookmark handling
2541
2542 /**
2543 * add the current page as a bookmark to the given folder id
2544 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002545 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002546 * bookmarked, and if it is, edit that bookmark. If false, and
2547 * the site is already bookmarked, do not attempt to edit the
2548 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002549 */
2550 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002551 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002552 WebView w = getCurrentTopWebView();
2553 if (w == null) {
2554 return null;
2555 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002556 Intent i = new Intent(mActivity,
2557 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002558 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2559 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2560 String touchIconUrl = w.getTouchIconUrl();
2561 if (touchIconUrl != null) {
2562 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2563 WebSettings settings = w.getSettings();
2564 if (settings != null) {
2565 i.putExtra(AddBookmarkPage.USER_AGENT,
2566 settings.getUserAgentString());
2567 }
2568 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002569 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002570 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002571 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002572 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2573 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002574 // Put the dialog at the upper right of the screen, covering the
2575 // star on the title bar.
2576 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002577 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 }
2579
2580 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002581 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002582 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002583 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002584 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002585 }
2586
Vivek Sekharb54614f2014-05-01 19:03:37 -07002587 @Override
2588 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2589 boolean capture) {
2590 mUploadHandler = new UploadHandler(this);
2591 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2592 }
2593
Michael Kolb8233fac2010-10-26 16:08:53 -07002594 // thumbnails
2595
2596 /**
2597 * Return the desired width for thumbnail screenshots, which are stored in
2598 * the database, and used on the bookmarks screen.
2599 * @param context Context for finding out the density of the screen.
2600 * @return desired width for thumbnail screenshot.
2601 */
2602 static int getDesiredThumbnailWidth(Context context) {
2603 return context.getResources().getDimensionPixelOffset(
2604 R.dimen.bookmarkThumbnailWidth);
2605 }
2606
2607 /**
2608 * Return the desired height 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 height for thumbnail screenshot.
2612 */
2613 static int getDesiredThumbnailHeight(Context context) {
2614 return context.getResources().getDimensionPixelOffset(
2615 R.dimen.bookmarkThumbnailHeight);
2616 }
2617
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002618 static void createScreenshotAsync(WebView view, int width, int height,
2619 final ValueCallback<Bitmap> cb) {
2620 if (view == null || width == 0 || height == 0) {
2621 return;
2622 }
2623
2624 view.getContentBitmapAsync(
2625 (float) width / view.getWidth(),
2626 new Rect(),
2627 new ValueCallback<Bitmap>() {
2628 @Override
2629 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002630 if (bitmap != null)
2631 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2632 cb.onReceiveValue(bitmap);
2633 }});
2634 }
2635
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002636 private void updateScreenshot(final Tab tab) {
2637 createScreenshotAsync(
2638 tab.getWebView(),
2639 getDesiredThumbnailWidth(mActivity),
2640 getDesiredThumbnailHeight(mActivity),
2641 new ValueCallback<Bitmap>() {
2642 @Override
2643 public void onReceiveValue(Bitmap bitmap) {
2644 updateScreenshot(tab, bitmap);
2645 }
2646 });
2647 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002648
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002649 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002650 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002651 // FIXME: Would like to make sure there is actually something to
2652 // draw, but the API for that (WebViewCore.pictureReady()) is not
2653 // currently accessible here.
2654
John Reck34ef2672011-02-10 11:30:55 -08002655 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002656 if (view == null) {
2657 // Tab was destroyed
2658 return;
2659 }
John Reck34ef2672011-02-10 11:30:55 -08002660 final String url = tab.getUrl();
2661 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002662 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002663 if (TextUtils.isEmpty(url)) {
2664 return;
2665 }
2666
kaiyiz6e5b3e02013-08-19 20:02:01 +08002667 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002668 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002669 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002670 }
John Reck34ef2672011-02-10 11:30:55 -08002671 // Only update thumbnails for web urls (http(s)://), not for
2672 // about:, javascript:, data:, etc...
2673 // Unless it is a bookmarked site, then always update
2674 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2675 return;
2676 }
2677
kaiyiz6e5b3e02013-08-19 20:02:01 +08002678 if (url != null && mUpdateMyNavThumbnailUrl != null
2679 && Patterns.WEB_URL.matcher(url).matches()
2680 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2681 String urlHost = (new WebAddress(url)).getHost();
2682 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2683 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2684 || bookmarkHost.length() == 0) {
2685 return;
2686 }
2687 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2688 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2689 bookmarkHost.length());
2690 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2691 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2692 if (!bookmarkDomain.equals(urlDomain)) {
2693 return;
2694 }
2695 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002696 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002697 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2698 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2699 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2700 500);
2701 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002702 return;
2703 }
2704
2705 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002706 new AsyncTask<Void, Void, Void>() {
2707 @Override
2708 protected Void doInBackground(Void... unused) {
2709 Cursor cursor = null;
2710 try {
2711 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002712 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2713 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2714 : url);
2715 if (mUpdateMyNavThumbnail) {
2716 mUpdateMyNavThumbnail = false;
2717 mUpdateMyNavThumbnailUrl = null;
2718 }
John Reck34ef2672011-02-10 11:30:55 -08002719 if (cursor != null && cursor.moveToFirst()) {
2720 final ByteArrayOutputStream os =
2721 new ByteArrayOutputStream();
2722 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002723
John Reck34ef2672011-02-10 11:30:55 -08002724 ContentValues values = new ContentValues();
2725 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002726
John Reck34ef2672011-02-10 11:30:55 -08002727 do {
John Reck617fd832011-02-16 14:35:59 -08002728 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002729 cr.update(Images.CONTENT_URI, values, null, null);
2730 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002731 }
John Reck34ef2672011-02-10 11:30:55 -08002732 } catch (IllegalStateException e) {
2733 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002734 } catch (SQLiteException s) {
2735 // Added for possible error when user tries to remove the same bookmark
2736 // that is being updated with a screen shot
2737 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002738 } finally {
2739 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002740 }
John Reck34ef2672011-02-10 11:30:55 -08002741 return null;
2742 }
2743 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002744 }
2745
2746 private class Copy implements OnMenuItemClickListener {
2747 private CharSequence mText;
2748
John Reck9c35b9c2012-05-30 10:08:50 -07002749 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002750 public boolean onMenuItemClick(MenuItem item) {
2751 copy(mText);
2752 return true;
2753 }
2754
2755 public Copy(CharSequence toCopy) {
2756 mText = toCopy;
2757 }
2758 }
2759
Leon Scroggins63c02662010-11-18 15:16:27 -05002760 private static class Download implements OnMenuItemClickListener {
2761 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002762 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002763 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002764 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002765 private static final String FALLBACK_EXTENSION = "dat";
2766 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002767
John Reck9c35b9c2012-05-30 10:08:50 -07002768 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002769 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002770 if (DataUri.isDataUri(mText)) {
2771 saveDataUri();
2772 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002773 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002774 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002775 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002776 return true;
2777 }
2778
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002779 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2780 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002781 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002782 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002783 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002784 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002785 }
George Mount387d45d2011-10-07 15:57:53 -07002786
2787 /**
2788 * Treats mText as a data URI and writes its contents to a file
2789 * based on the current time.
2790 */
2791 private void saveDataUri() {
2792 FileOutputStream outputStream = null;
2793 try {
2794 DataUri uri = new DataUri(mText);
2795 File target = getTarget(uri);
2796 outputStream = new FileOutputStream(target);
2797 outputStream.write(uri.getData());
2798 final DownloadManager manager =
2799 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2800 manager.addCompletedDownload(target.getName(),
2801 mActivity.getTitle().toString(), false,
2802 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002803 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002804 } catch (IOException e) {
2805 Log.e(LOGTAG, "Could not save data URL");
2806 } finally {
2807 if (outputStream != null) {
2808 try {
2809 outputStream.close();
2810 } catch (IOException e) {
2811 // ignore close errors
2812 }
2813 }
2814 }
2815 }
2816
2817 /**
2818 * Creates a File based on the current time stamp and uses
2819 * the mime type of the DataUri to get the extension.
2820 */
2821 private File getTarget(DataUri uri) throws IOException {
2822 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002823 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002824 String nameBase = format.format(new Date());
2825 String mimeType = uri.getMimeType();
2826 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2827 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2828 if (extension == null) {
2829 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2830 extension = FALLBACK_EXTENSION;
2831 }
2832 extension = "." + extension; // createTempFile needs the '.'
2833 File targetFile = File.createTempFile(nameBase, extension, dir);
2834 return targetFile;
2835 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002836 }
2837
Cary Clark8974d282010-11-22 10:46:05 -05002838 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002839 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002840
John Reck9c35b9c2012-05-30 10:08:50 -07002841 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002842 public boolean onMenuItemClick(MenuItem item) {
2843 if (mWebView != null) {
2844 return mWebView.selectText();
2845 }
2846 return false;
2847 }
2848
2849 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002850 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002851 }
2852
2853 }
2854
Michael Kolb8233fac2010-10-26 16:08:53 -07002855 /********************** TODO: UI stuff *****************************/
2856
2857 // these methods have been copied, they still need to be cleaned up
2858
2859 /****************** tabs ***************************************************/
2860
2861 // basic tab interactions:
2862
2863 // it is assumed that tabcontrol already knows about the tab
2864 protected void addTab(Tab tab) {
2865 mUi.addTab(tab);
2866 }
2867
2868 protected void removeTab(Tab tab) {
2869 mUi.removeTab(tab);
2870 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002871 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002872 }
2873
Michael Kolbf2055602011-04-09 17:20:03 -07002874 @Override
2875 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002876 // monkey protection against delayed start
2877 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002878
2879 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2880 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002881 mTabControl.setCurrentTab(tab);
2882 // the tab is guaranteed to have a webview after setCurrentTab
2883 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002884
2885
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002886 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002887 //Purge active tabs
2888 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002889 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002890 }
2891
John Reck8bcafc12011-08-29 16:43:02 -07002892 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002893 Tab current = mTabControl.getCurrentTab();
2894 if (current != null
2895 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002896 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002897 }
2898 }
2899
John Reck26b18322011-06-21 13:08:58 -07002900 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002901 //Cancel navscreen request
2902 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002903 // Dismiss the subwindow if applicable.
2904 dismissSubWindow(appTab);
2905 // Since we might kill the WebView, remove it from the
2906 // content view first.
2907 mUi.detachTab(appTab);
2908 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002909 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002910 // TODO: analyze why the remove and add are necessary
2911 mUi.attachTab(appTab);
2912 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002913 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002914 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002915 } else {
2916 // If the tab was the current tab, we have to attach
2917 // it to the view system again.
2918 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002919 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002920 }
2921 }
2922
2923 // Remove the sub window if it exists. Also called by TabControl when the
2924 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002925 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002926 public void dismissSubWindow(Tab tab) {
2927 removeSubWindow(tab);
2928 // dismiss the subwindow. This will destroy the WebView.
2929 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002930 WebView wv = getCurrentTopWebView();
2931 if (wv != null) {
2932 wv.requestFocus();
2933 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002934 }
2935
2936 @Override
2937 public void removeSubWindow(Tab t) {
2938 if (t.getSubWebView() != null) {
2939 mUi.removeSubWindow(t.getSubViewContainer());
2940 }
2941 }
2942
2943 @Override
2944 public void attachSubWindow(Tab tab) {
2945 if (tab.getSubWebView() != null) {
2946 mUi.attachSubWindow(tab.getSubViewContainer());
2947 getCurrentTopWebView().requestFocus();
2948 }
2949 }
2950
Mathew Inwood29721c22011-06-29 17:55:24 +01002951 private Tab showPreloadedTab(final UrlData urlData) {
2952 if (!urlData.isPreloaded()) {
2953 return null;
2954 }
2955 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2956 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2957 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002958 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002959 // Could not submit query. Fallback to regular tab creation
2960 tabControl.destroy();
2961 return null;
2962 }
2963 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002964 // check tab count and make room for new tab
2965 if (!mTabControl.canCreateNewTab()) {
2966 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2967 if (leastUsed != null) {
2968 closeTab(leastUsed);
2969 }
2970 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002971 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002972 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002973 mTabControl.addPreloadedTab(t);
2974 addTab(t);
2975 setActiveTab(t);
2976 return t;
2977 }
2978
Michael Kolb7bcafde2011-05-09 13:55:59 -07002979 // open a non inconito tab with the given url data
2980 // and set as active tab
2981 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002982 Tab tab = showPreloadedTab(urlData);
2983 if (tab == null) {
2984 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002985 if ((tab != null) && !urlData.isEmpty()) {
2986 loadUrlDataIn(tab, urlData);
2987 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002988 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002989 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002990 }
2991
Michael Kolb843510f2010-12-09 10:51:49 -08002992 @Override
2993 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002994 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002995 }
2996
Michael Kolb8233fac2010-10-26 16:08:53 -07002997 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002998 public Tab openIncognitoTab() {
2999 return openTab(INCOGNITO_URI, true, true, false);
3000 }
3001
3002 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003003 public Tab openTab(String url, boolean incognito, boolean setActive,
3004 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003005 return openTab(url, incognito, setActive, useCurrent, null);
3006 }
3007
3008 @Override
3009 public Tab openTab(String url, Tab parent, boolean setActive,
3010 boolean useCurrent) {
3011 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3012 setActive, useCurrent, parent);
3013 }
3014
3015 public Tab openTab(String url, boolean incognito, boolean setActive,
3016 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003017 Tab tab = createNewTab(incognito, setActive, useCurrent);
3018 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003019 if (parent instanceof SnapshotTab) {
3020 addTab(tab);
3021 if (setActive)
3022 setActiveTab(tab);
3023 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003024 parent.addChildTab(tab);
3025 }
Michael Kolb519d2282011-05-09 17:03:19 -07003026 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003027 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003028 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003029 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003030 return tab;
3031 }
3032
3033 // this method will attempt to create a new tab
3034 // incognito: private browsing tab
3035 // setActive: ste tab as current tab
3036 // useCurrent: if no new tab can be created, return current tab
3037 private Tab createNewTab(boolean incognito, boolean setActive,
3038 boolean useCurrent) {
3039 Tab tab = null;
3040 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05003041 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003042 addTab(tab);
3043 if (setActive) {
3044 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07003045 } else {
3046 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003047 }
3048 } else {
3049 if (useCurrent) {
3050 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07003051 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003052 } else {
3053 mUi.showMaxTabsWarning();
3054 }
3055 }
3056 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003057 }
3058
John Reck2bc80422011-06-30 15:11:49 -07003059 @Override
3060 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3061 SnapshotTab tab = null;
3062 if (mTabControl.canCreateNewTab()) {
3063 tab = mTabControl.createSnapshotTab(snapshotId);
3064 addTab(tab);
3065 if (setActive) {
3066 setActiveTab(tab);
3067 }
3068 } else {
3069 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003070 }
3071 return tab;
3072 }
3073
Michael Kolb8233fac2010-10-26 16:08:53 -07003074 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003075 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003076 * @return boolean True if we successfully switched to a different tab. If
3077 * the indexth tab is null, or if that tab is the same as
3078 * the current one, return false.
3079 */
3080 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003081 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003082 Tab currentTab = mTabControl.getCurrentTab();
3083 if (tab == null || tab == currentTab) {
3084 return false;
3085 }
3086 setActiveTab(tab);
3087 return true;
3088 }
3089
3090 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003091 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003092 closeCurrentTab(false);
3093 }
3094
3095 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003096 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003097 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003098 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003099 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003100 return;
3101 }
Michael Kolbc831b632011-05-11 09:30:34 -07003102 final Tab current = mTabControl.getCurrentTab();
3103 final int pos = mTabControl.getCurrentPosition();
3104 Tab newTab = current.getParent();
3105 if (newTab == null) {
3106 newTab = mTabControl.getTab(pos + 1);
3107 if (newTab == null) {
3108 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003109 }
3110 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003111 if (andQuit) {
3112 mTabControl.setCurrentTab(newTab);
3113 closeTab(current);
3114 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003115 // Close window
3116 closeTab(current);
3117 }
3118 }
3119
3120 /**
3121 * Close the tab, remove its associated title bar, and adjust mTabControl's
3122 * current tab to a valid value.
3123 */
3124 @Override
3125 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003126 if (tab == mTabControl.getCurrentTab()) {
3127 closeCurrentTab();
3128 } else {
3129 removeTab(tab);
3130 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003131 }
3132
Afzal Najamd4e33312012-04-26 01:54:01 -04003133 /**
3134 * Close all tabs except the current one
3135 */
3136 @Override
3137 public void closeOtherTabs() {
3138 int inactiveTabs = mTabControl.getTabCount() - 1;
3139 for (int i = inactiveTabs; i >= 0; i--) {
3140 Tab tab = mTabControl.getTab(i);
3141 if (tab != mTabControl.getCurrentTab()) {
3142 removeTab(tab);
3143 }
3144 }
3145 }
3146
Michael Kolb8233fac2010-10-26 16:08:53 -07003147 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003148 protected void loadUrlFromContext(String url) {
3149 Tab tab = getCurrentTab();
3150 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003151 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003152 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003153 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003154 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003155 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003156 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 }
3158 }
3159 }
3160
3161 /**
3162 * Load the URL into the given WebView and update the title bar
3163 * to reflect the new load. Call this instead of WebView.loadUrl
3164 * directly.
3165 * @param view The WebView used to load url.
3166 * @param url The URL to load.
3167 */
John Reck71e51422011-07-01 16:49:28 -07003168 @Override
3169 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003170 loadUrl(tab, url, null);
3171 }
3172
3173 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3174 if (tab != null) {
3175 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003176 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003177 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003178 if (tab.hasCrashed) {
3179 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3180 }
Michael Kolba53c9892011-10-05 13:31:40 -07003181 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003182 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003183 }
3184
3185 /**
3186 * Load UrlData into a Tab and update the title bar to reflect the new
3187 * load. Call this instead of UrlData.loadIn directly.
3188 * @param t The Tab used to load.
3189 * @param data The UrlData being loaded.
3190 */
3191 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003192 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003193 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003194 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003195 } else {
John Reck38b39652012-06-05 09:22:59 -07003196 if (t != null && data.mDisableUrlOverride) {
3197 t.disableUrlOverridingForLoad();
3198 }
John Reck26b18322011-06-21 13:08:58 -07003199 loadUrl(t, data.mUrl, data.mHeaders);
3200 }
3201 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003202 }
3203
John Reck30c714c2010-12-16 17:30:34 -08003204 @Override
3205 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003206 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003207 //In case of tab can go back (aka tab has navigation entry) do nothing
3208 //else just load homepage in current tab.
3209 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003210 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003211 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003212 }
3213
3214 void goBackOnePageOrQuit() {
3215 Tab current = mTabControl.getCurrentTab();
3216 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003217 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3218 showExitDialog(mActivity);
3219 } else {
3220 /*
3221 * Instead of finishing the activity, simply push this to the back
3222 * of the stack and let ActivityManager to choose the foreground
3223 * activity. As BrowserActivity is singleTask, it will be always the
3224 * root of the task. So we can use either true or false for
3225 * moveTaskToBack().
3226 */
3227 mActivity.moveTaskToBack(true);
3228 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003229 return;
3230 }
John Reckef654f12011-07-12 16:42:08 -07003231 if (current.canGoBack()) {
3232 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003233 } else {
3234 // Check to see if we are closing a window that was created by
3235 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003236 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003237 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003238 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003239 // Now we close the other tab
3240 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003241 } else if (BrowserConfig.getInstance(getContext())
3242 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3243 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003244 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003245 if ((current.getAppId() != null) || current.closeOnBack()) {
3246 closeCurrentTab(true);
3247 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003248 /*
3249 * Instead of finishing the activity, simply push this to the back
3250 * of the stack and let ActivityManager to choose the foreground
3251 * activity. As BrowserActivity is singleTask, it will be always the
3252 * root of the task. So we can use either true or false for
3253 * moveTaskToBack().
3254 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003255 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003256 }
3257 }
3258 }
3259
3260 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003261 * helper method for key handler
3262 * returns the current tab if it can't advance
3263 */
Michael Kolbc831b632011-05-11 09:30:34 -07003264 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003265 int pos = mTabControl.getCurrentPosition() + 1;
3266 if (pos >= mTabControl.getTabCount()) {
3267 pos = 0;
3268 }
3269 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003270 }
3271
3272 /**
3273 * helper method for key handler
3274 * returns the current tab if it can't advance
3275 */
Michael Kolbc831b632011-05-11 09:30:34 -07003276 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003277 int pos = mTabControl.getCurrentPosition() - 1;
3278 if ( pos < 0) {
3279 pos = mTabControl.getTabCount() - 1;
3280 }
3281 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003282 }
3283
John Reckbcef87f2012-02-03 14:58:34 -08003284 boolean isMenuOrCtrlKey(int keyCode) {
3285 return (KeyEvent.KEYCODE_MENU == keyCode)
3286 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3287 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3288 }
3289
Michael Kolb0035fad2011-03-14 13:25:28 -07003290 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003291 * handle key events in browser
3292 *
3293 * @param keyCode
3294 * @param event
3295 * @return true if handled, false to pass to super
3296 */
John Reck9c35b9c2012-05-30 10:08:50 -07003297 @Override
3298 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003299 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3300 // Hardware menu key
Enrico Ros1f5a0952014-11-18 20:15:48 -08003301 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
Pankaj Garg49b79252014-11-07 17:33:41 -08003302 true, false);
3303 return true;
3304 }
3305
Cary Clark160bbb92011-01-10 11:17:07 -05003306 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003307 // Even if MENU is already held down, we need to call to super to open
3308 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003309 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003310 mMenuIsDown = true;
3311 return false;
3312 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003313
Cary Clark8ff8c662010-12-29 15:03:05 -05003314 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003315 Tab tab = getCurrentTab();
3316 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003317
Cary Clark160bbb92011-01-10 11:17:07 -05003318 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3319 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003320
Michael Kolb8233fac2010-10-26 16:08:53 -07003321 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003322 case KeyEvent.KEYCODE_TAB:
3323 if (event.isCtrlPressed()) {
3324 if (event.isShiftPressed()) {
3325 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003326 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003327 } else {
3328 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003329 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003330 }
3331 return true;
3332 }
3333 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003334 case KeyEvent.KEYCODE_SPACE:
3335 // WebView/WebTextView handle the keys in the KeyDown. As
3336 // the Activity's shortcut keys are only handled when WebView
3337 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003338 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003339 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003340 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003341 pageDown();
3342 }
3343 return true;
3344 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003345 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003346 event.startTracking();
3347 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003348 case KeyEvent.KEYCODE_FORWARD:
3349 if (!noModifiers) break;
3350 tab.goForward();
3351 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003352 case KeyEvent.KEYCODE_DPAD_LEFT:
3353 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003354 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003355 return true;
3356 }
3357 break;
3358 case KeyEvent.KEYCODE_DPAD_RIGHT:
3359 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003360 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003361 return true;
3362 }
3363 break;
3364 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003365 if (ctrl) {
3366 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003367 return true;
3368 }
3369 break;
Michael Kolba4183062011-01-16 10:43:21 -08003370// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003371 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003372 if (ctrl ) {
3373 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003374 return true;
3375 }
3376 break;
Michael Kolba4183062011-01-16 10:43:21 -08003377// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003378// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003379// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003380// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003381// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003382// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003383// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003384// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003385// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003386// case KeyEvent.KEYCODE_M: // unused
3387// case KeyEvent.KEYCODE_N: // in Chrome: new window
3388// case KeyEvent.KEYCODE_O: // in Chrome: open file
3389// case KeyEvent.KEYCODE_P: // in Chrome: print page
3390// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003391// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003392// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3393 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003394 // we can't use the ctrl/shift flags, they check for
3395 // exclusive use of a modifier
3396 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003397 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003398 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003399 } else {
3400 openTabToHomePage();
3401 }
3402 return true;
3403 }
3404 break;
3405// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3406// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003407// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003408// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3409// case KeyEvent.KEYCODE_Y: // unused
3410// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003411 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003412 // it is a regular key and webview is not null
3413 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003414 }
3415
John Reck9c35b9c2012-05-30 10:08:50 -07003416 @Override
3417 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003418 switch(keyCode) {
3419 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003420 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003421 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003422 return true;
3423 }
3424 break;
3425 }
3426 return false;
3427 }
3428
John Reck9c35b9c2012-05-30 10:08:50 -07003429 @Override
3430 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003431 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003432 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003433 if (KeyEvent.KEYCODE_MENU == keyCode
3434 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003435 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003436 }
3437 }
Cary Clark160bbb92011-01-10 11:17:07 -05003438 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003439 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003440 case KeyEvent.KEYCODE_BACK:
3441 if (event.isTracking() && !event.isCanceled()) {
3442 onBackKey();
3443 return true;
3444 }
3445 break;
3446 }
3447 return false;
3448 }
3449
3450 public boolean isMenuDown() {
3451 return mMenuIsDown;
3452 }
3453
John Reck9c35b9c2012-05-30 10:08:50 -07003454 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003455 public void setupAutoFill(Message message) {
3456 // Open the settings activity at the AutoFill profile fragment so that
3457 // the user can create a new profile. When they return, we will dispatch
3458 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003459 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003460 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3461 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003462 }
John Reckb3417f02011-01-14 11:01:05 -08003463
John Reck9c35b9c2012-05-30 10:08:50 -07003464 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003465 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003466 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003467 return true;
3468 }
3469
John Reck1cf4b792011-07-26 10:22:22 -07003470 @Override
3471 public boolean shouldCaptureThumbnails() {
3472 return mUi.shouldCaptureThumbnails();
3473 }
3474
Michael Kolbc3af0672011-08-09 10:24:41 -07003475 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003476 public boolean supportsVoice() {
3477 PackageManager pm = mActivity.getPackageManager();
3478 List activities = pm.queryIntentActivities(new Intent(
3479 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3480 return activities.size() != 0;
3481 }
3482
3483 @Override
3484 public void startVoiceRecognizer() {
3485 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003486 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003487 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3488 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3489 mActivity.startActivityForResult(voice, VOICE_RESULT);
3490 }
3491
Pankaj Garg7b279f62014-08-12 14:47:18 -07003492 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003493 if (mLevel == level)
3494 return;
3495 mLevel = level;
3496 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003497 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3498 lp.dimAmount = level;
3499 mActivity.getWindow().setAttributes(lp);
3500 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3501 } else {
3502 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3503 }
3504 }
3505
Michael Kolb0b129122012-06-04 16:31:58 -07003506 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003507 public void setBlockEvents(boolean block) {
3508 mBlockEvents = block;
3509 }
3510
John Reck9c35b9c2012-05-30 10:08:50 -07003511 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003512 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003513 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003514 }
3515
John Reck9c35b9c2012-05-30 10:08:50 -07003516 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003517 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003518 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003519 }
3520
John Reck9c35b9c2012-05-30 10:08:50 -07003521 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003522 public boolean dispatchTouchEvent(MotionEvent ev) {
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 dispatchTrackballEvent(MotionEvent ev) {
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 dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003533 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003534 }
3535
Pankaj Garg49b79252014-11-07 17:33:41 -08003536 @Override
3537 public boolean shouldShowAppMenu() {
3538 return true;
3539 }
3540
3541 @Override
3542 public int getMenuThemeResourceId() {
3543 return R.style.OverflowMenuTheme;
3544 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003545}