blob: dd3d76c9ba1e32a13503178f9416979b7b987af7 [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";
Michael Kolba4261fd2011-05-05 11:27:37 -0700135 private static final String INCOGNITO_URI = "browser: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();
1409 }
1410
1411 /**
1412 * Open the Go page.
1413 * @param startWithHistory If true, open starting on the history tab.
1414 * Otherwise, start with the bookmarks tab.
1415 */
1416 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001417 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001418 if (mTabControl.getCurrentWebView() == null) {
1419 return;
1420 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001421 // clear action mode
1422 if (isInCustomActionMode()) {
1423 endActionMode();
1424 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001425 Bundle extras = new Bundle();
1426 // Disable opening in a new window if we have maxed out the windows
1427 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1428 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001429 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 }
1431
1432 // combo view callbacks
1433
Michael Kolb8233fac2010-10-26 16:08:53 -07001434 // key handling
1435 protected void onBackKey() {
1436 if (!mUi.onBackKey()) {
1437 WebView subwindow = mTabControl.getCurrentSubWindow();
1438 if (subwindow != null) {
1439 if (subwindow.canGoBack()) {
1440 subwindow.goBack();
1441 } else {
1442 dismissSubWindow(mTabControl.getCurrentTab());
1443 }
1444 } else {
1445 goBackOnePageOrQuit();
1446 }
1447 }
1448 }
1449
Michael Kolb4bd767d2011-05-27 11:33:55 -07001450 protected boolean onMenuKey() {
1451 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001452 }
1453
Michael Kolb8233fac2010-10-26 16:08:53 -07001454 // menu handling and state
1455 // TODO: maybe put into separate handler
1456
John Reck9c35b9c2012-05-30 10:08:50 -07001457 @Override
1458 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001459 if (mMenuState == EMPTY_MENU) {
1460 return false;
1461 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 MenuInflater inflater = mActivity.getMenuInflater();
1463 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001464 return true;
1465 }
1466
John Reck9c35b9c2012-05-30 10:08:50 -07001467 @Override
1468 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001469 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001470 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001471 return;
1472 }
1473 if (!(v instanceof WebView)) {
1474 return;
1475 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001476 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001477 WebView.HitTestResult result;
1478
1479 /* Determine whether the ContextMenu got triggered because
1480 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1481 * received. The mResult acts as a flag to identify, how it got trigerred
1482 */
1483 if (mResult == null){
1484 result = webview.getHitTestResult();
1485 } else {
1486 result = mResult;
1487 }
1488
Michael Kolb8233fac2010-10-26 16:08:53 -07001489 if (result == null) {
1490 return;
1491 }
1492
1493 int type = result.getType();
1494 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001495
1496 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1497 unknownTypeMap.put("webview", webview);
1498 final Message msg = mHandler.obtainMessage(
1499 UNKNOWN_TYPE_MSG, unknownTypeMap);
1500 /* As defined in android developers guide
1501 * when UNKNOWN_TYPE is received as a result of HitTest
1502 * you need to determing the type by invoking requestFocusNodeHref
1503 */
1504 webview.requestFocusNodeHref(msg);
1505
Michael Kolb8233fac2010-10-26 16:08:53 -07001506 Log.w(LOGTAG,
1507 "We should not show context menu when nothing is touched");
1508 return;
1509 }
1510 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1511 // let TextView handles context menu
1512 return;
1513 }
1514
1515 // Note, http://b/issue?id=1106666 is requesting that
1516 // an inflated menu can be used again. This is not available
1517 // yet, so inflate each time (yuk!)
1518 MenuInflater inflater = mActivity.getMenuInflater();
1519 inflater.inflate(R.menu.browsercontext, menu);
1520
1521 // Show the correct menu group
1522 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001523 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001524 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001525 menu.setGroupVisible(R.id.PHONE_MENU,
1526 type == WebView.HitTestResult.PHONE_TYPE);
1527 menu.setGroupVisible(R.id.EMAIL_MENU,
1528 type == WebView.HitTestResult.EMAIL_TYPE);
1529 menu.setGroupVisible(R.id.GEO_MENU,
1530 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001531 String itemUrl = null;
1532 String url = webview.getOriginalUrl();
1533 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1534 itemUrl = Uri.decode(navigationUrl);
1535 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1536 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1537 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1538 } else {
1539 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1540 }
1541 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1542 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1543 } else {
1544 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1545
1546 menu.setGroupVisible(R.id.IMAGE_MENU,
1547 type == WebView.HitTestResult.IMAGE_TYPE
1548 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1549 menu.setGroupVisible(R.id.ANCHOR_MENU,
1550 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1551 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001552 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1553 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001554 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001555 // Setup custom handling depending on the type
1556 switch (type) {
1557 case WebView.HitTestResult.PHONE_TYPE:
1558 menu.setHeaderTitle(Uri.decode(extra));
1559 menu.findItem(R.id.dial_context_menu_id).setIntent(
1560 new Intent(Intent.ACTION_VIEW, Uri
1561 .parse(WebView.SCHEME_TEL + extra)));
1562 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1563 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1564 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1565 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1566 addIntent);
1567 menu.findItem(R.id.copy_phone_context_menu_id)
1568 .setOnMenuItemClickListener(
1569 new Copy(extra));
1570 break;
1571
1572 case WebView.HitTestResult.EMAIL_TYPE:
1573 menu.setHeaderTitle(extra);
1574 menu.findItem(R.id.email_context_menu_id).setIntent(
1575 new Intent(Intent.ACTION_VIEW, Uri
1576 .parse(WebView.SCHEME_MAILTO + extra)));
1577 menu.findItem(R.id.copy_mail_context_menu_id)
1578 .setOnMenuItemClickListener(
1579 new Copy(extra));
1580 break;
1581
1582 case WebView.HitTestResult.GEO_TYPE:
1583 menu.setHeaderTitle(extra);
1584 menu.findItem(R.id.map_context_menu_id).setIntent(
1585 new Intent(Intent.ACTION_VIEW, Uri
1586 .parse(WebView.SCHEME_GEO
1587 + URLEncoder.encode(extra))));
1588 menu.findItem(R.id.copy_geo_context_menu_id)
1589 .setOnMenuItemClickListener(
1590 new Copy(extra));
1591 break;
1592
1593 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1594 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001595 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001596 // decide whether to show the open link in new tab option
1597 boolean showNewTab = mTabControl.canCreateNewTab();
1598 MenuItem newTabItem
1599 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001600 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001601 ? R.string.contextmenu_openlink_newwindow_background
1602 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 newTabItem.setVisible(showNewTab);
1604 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001605 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1606 newTabItem.setOnMenuItemClickListener(
1607 new MenuItem.OnMenuItemClickListener() {
1608 @Override
1609 public boolean onMenuItemClick(MenuItem item) {
1610 final HashMap<String, WebView> hrefMap =
1611 new HashMap<String, WebView>();
1612 hrefMap.put("webview", webview);
1613 final Message msg = mHandler.obtainMessage(
1614 FOCUS_NODE_HREF,
1615 R.id.open_newtab_context_menu_id,
1616 0, hrefMap);
1617 webview.requestFocusNodeHref(msg);
1618 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001619 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001620 });
1621 } else {
1622 newTabItem.setOnMenuItemClickListener(
1623 new MenuItem.OnMenuItemClickListener() {
1624 @Override
1625 public boolean onMenuItemClick(MenuItem item) {
1626 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001627 openTab(extra, parent,
1628 !mSettings.openInBackground(),
1629 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001630 return true;
1631 }
1632 });
1633 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001634 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001635 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1636 menu.setHeaderTitle(navigationUrl);
1637 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1638
1639 if (itemUrl != null) {
1640 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1641 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1642 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1643 @Override
1644 public boolean onMenuItemClick(MenuItem item) {
1645 final Intent intent = new Intent(Controller.this
1646 .getContext(),
1647 AddMyNavigationPage.class);
1648 Bundle bundle = new Bundle();
1649 String url = Uri.decode(navigationUrl);
1650 bundle.putBoolean("isAdding", false);
1651 bundle.putString("url", url);
1652 bundle.putString("name", getNameFromUrl(url));
1653 intent.putExtra("websites", bundle);
1654 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1655 return false;
1656 }
1657 });
1658 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1659 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1660 @Override
1661 public boolean onMenuItemClick(MenuItem item) {
1662 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1663 return false;
1664 }
1665 });
1666 }
1667 } else {
1668 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1669 }
1670 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001671 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1672 break;
1673 }
1674 // otherwise fall through to handle image part
1675 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001676 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1677 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001678 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1679 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001680 shareItem.setOnMenuItemClickListener(
1681 new MenuItem.OnMenuItemClickListener() {
1682 @Override
1683 public boolean onMenuItemClick(MenuItem item) {
1684 sharePage(mActivity, null, extra, null,
1685 null);
1686 return true;
1687 }
1688 }
1689 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001690 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001691 menu.findItem(R.id.view_image_context_menu_id)
1692 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1693 @Override
1694 public boolean onMenuItemClick(MenuItem item) {
1695 openTab(extra, mTabControl.getCurrentTab(), true, true);
1696 return false;
1697 }
1698 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001699 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1700 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1701 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001702 menu.findItem(R.id.set_wallpaper_context_menu_id).
1703 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1704 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001705 break;
1706
1707 default:
1708 Log.w(LOGTAG, "We should not get here.");
1709 break;
1710 }
1711 //update the ui
1712 mUi.onContextMenuCreated(menu);
1713 }
1714
kaiyiz6e5b3e02013-08-19 20:02:01 +08001715 public void startAddMyNavigation(String url) {
1716 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1717 Bundle bundle = new Bundle();
1718 bundle.putBoolean("isAdding", true);
1719 bundle.putString("url", url);
1720 bundle.putString("name", getNameFromUrl(url));
1721 intent.putExtra("websites", bundle);
1722 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1723 }
1724
1725 private void showMyNavigationDeleteDialog(final String itemUrl) {
1726 new AlertDialog.Builder(this.getContext())
1727 .setTitle(R.string.my_navigation_delete_label)
1728 .setIcon(android.R.drawable.ic_dialog_alert)
1729 .setMessage(R.string.my_navigation_delete_msg)
1730 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1731 @Override
1732 public void onClick(DialogInterface dialog, int whichButton) {
1733 deleteMyNavigationItem(itemUrl);
1734 }
1735 })
1736 .setNegativeButton(R.string.cancel, null)
1737 .show();
1738 }
1739
1740 private void deleteMyNavigationItem(final String itemUrl) {
1741 ContentResolver cr = this.getContext().getContentResolver();
1742 Cursor cursor = null;
1743
1744 try {
1745 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1746 new String[] {
1747 MyNavigationUtil.ID
1748 }, "url = ?", new String[] {
1749 itemUrl
1750 }, null);
1751 if (null != cursor && cursor.moveToFirst()) {
1752 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1753 cursor.getLong(0));
1754
1755 ContentValues values = new ContentValues();
1756 values.put(MyNavigationUtil.TITLE, "");
1757 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1758 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1759 ByteArrayOutputStream os = new ByteArrayOutputStream();
1760 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1761 R.raw.my_navigation_add);
1762 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1763 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1764 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1765 cr.update(uri, values, null, null);
1766 } else {
1767 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1768 }
1769 } catch (IllegalStateException e) {
1770 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1771 } finally {
1772 if (null != cursor) {
1773 cursor.close();
1774 }
1775 }
1776
1777 if (getCurrentTopWebView() != null) {
1778 getCurrentTopWebView().reload();
1779 }
1780 }
1781
1782 private String getNameFromUrl(String itemUrl) {
1783 ContentResolver cr = this.getContext().getContentResolver();
1784 Cursor cursor = null;
1785 String name = null;
1786
1787 try {
1788 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1789 new String[] {
1790 MyNavigationUtil.TITLE
1791 }, "url = ?", new String[] {
1792 itemUrl
1793 }, null);
1794 if (null != cursor && cursor.moveToFirst()) {
1795 name = cursor.getString(0);
1796 } else {
1797 Log.e(LOGTAG, "this item does not exist!");
1798 }
1799 } catch (IllegalStateException e) {
1800 Log.e(LOGTAG, "getNameFromUrl", e);
1801 } finally {
1802 if (null != cursor) {
1803 cursor.close();
1804 }
1805 }
1806 return name;
1807 }
1808
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001809 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001810 final ContentResolver cr = mActivity.getContentResolver();
1811 new AsyncTask<Void, Void, Void>() {
1812 @Override
1813 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001814 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1815 if(!isMyNavigationUrl)
1816 return null;
1817
kaiyiz6e5b3e02013-08-19 20:02:01 +08001818 ContentResolver cr = mActivity.getContentResolver();
1819 Cursor cursor = null;
1820 try {
1821 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1822 new String[] {
1823 MyNavigationUtil.ID
1824 }, "url = ?", new String[] {
1825 itemUrl
1826 }, null);
1827 if (null != cursor && cursor.moveToFirst()) {
1828 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001829 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001830
1831 ContentValues values = new ContentValues();
1832 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1833 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1834 cursor.getLong(0));
1835 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1836 cr.update(uri, values, null, null);
1837 os.close();
1838 }
1839 } catch (IllegalStateException e) {
1840 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1841 } catch (IOException e) {
1842 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1843 } finally {
1844 if (null != cursor) {
1845 cursor.close();
1846 }
1847 }
1848 return null;
1849 }
1850 }.execute();
1851 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001852 /**
1853 * As the menu can be open when loading state changes
1854 * we must manually update the state of the stop/reload menu
1855 * item
1856 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001857 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001858 if (menu == null) {
1859 return;
1860 }
1861 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001862 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001863 menu.findItem(R.id.stop_menu_id):
1864 menu.findItem(R.id.reload_menu_id);
1865 if (src != null) {
1866 dest.setIcon(src.getIcon());
1867 dest.setTitle(src.getTitle());
1868 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001869 mActivity.invalidateOptionsMenu();
1870 }
1871
1872 public void invalidateOptionsMenu() {
1873 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001874 }
1875
John Reck9c35b9c2012-05-30 10:08:50 -07001876 @Override
1877 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001878 // Software menu key (toolbar key)
1879 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1880 return true;
1881 }
1882
1883 @Override
1884 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001885 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001886 // hold on to the menu reference here; it is used by the page callbacks
1887 // to update the menu based on loading state
1888 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001889 // Note: setVisible will decide whether an item is visible; while
1890 // setEnabled() will decide whether an item is enabled, which also means
1891 // whether the matching shortcut key will function.
1892 switch (mMenuState) {
1893 case EMPTY_MENU:
1894 if (mCurrentMenuState != mMenuState) {
1895 menu.setGroupVisible(R.id.MAIN_MENU, false);
1896 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1897 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1898 }
1899 break;
1900 default:
1901 if (mCurrentMenuState != mMenuState) {
1902 menu.setGroupVisible(R.id.MAIN_MENU, true);
1903 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1904 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1905 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001906 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001907 break;
1908 }
1909 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001910 mUi.onPrepareOptionsMenu(menu);
1911 }
1912
1913 private void setMenuItemVisibility(Menu menu, int id,
1914 boolean visibility) {
1915 MenuItem item = menu.findItem(id);
1916 if (item != null) {
1917 item.setVisible(visibility);
1918 }
1919 }
1920
1921 private int lookupBookmark(String title, String url) {
1922 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001923 int count = 0;
1924 Cursor cursor = null;
1925 try {
1926 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1927 BookmarksLoader.PROJECTION,
1928 "title = ? OR url = ?",
1929 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001930 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001931 },
1932 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001933
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001934 if (cursor != null)
1935 count = cursor.getCount();
1936
1937 } catch (IllegalStateException e) {
1938 Log.e(LOGTAG, "lookupBookmark ", e);
1939 } finally {
1940 if (null != cursor) {
1941 cursor.close();
1942 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001943 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001944 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001945 }
1946
1947 private void resetMenuItems(Menu menu) {
1948 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1949 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1950
1951 WebView w = getCurrentTopWebView();
1952 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1953
1954 String title = w.getTitle();
1955 String url = w.getUrl();
1956 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1957 bookmark_icon.setChecked(true);
1958 } else {
1959 bookmark_icon.setChecked(false);
1960 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001961 }
1962
Michael Kolb4bf79712011-07-14 14:18:12 -07001963 @Override
1964 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001965 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001966 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001967 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001968 // Following flag is used to identify schemes for which the LIVE_MENU
1969 // items defined in res/menu/browser.xml should be enabled
1970 boolean isLiveScheme = false;
1971 boolean isPageFinished = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001972
1973 resetMenuItems(menu);
1974
Michael Kolb4bf79712011-07-14 14:18:12 -07001975 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001976 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001977 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001978 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001979 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001980 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Michael Kolb4bf79712011-07-14 14:18:12 -07001981 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001982
1983 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1984 forward.setEnabled(canGoForward);
1985
Michael Kolb4bf79712011-07-14 14:18:12 -07001986 // decide whether to show the share link option
1987 PackageManager pm = mActivity.getPackageManager();
1988 Intent send = new Intent(Intent.ACTION_SEND);
1989 send.setType("text/plain");
1990 ResolveInfo ri = pm.resolveActivity(send,
1991 PackageManager.MATCH_DEFAULT_ONLY);
1992 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1993
1994 boolean isNavDump = mSettings.enableNavDump();
1995 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1996 nav.setVisible(isNavDump);
1997 nav.setEnabled(isNavDump);
1998
1999 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002000 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2001 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002002 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002003 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2004 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002005 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002006 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2007 isLive && isLiveScheme && isPageFinished);
2008 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
2009 isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002010 // history and snapshots item are the members of COMBO menu group,
2011 // so if show history item, only make snapshots item invisible.
2012 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002013
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002014 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002015 }
2016
John Reck9c35b9c2012-05-30 10:08:50 -07002017 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002018 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002019 if (null == getCurrentTopWebView()) {
2020 return false;
2021 }
2022 if (mMenuIsDown) {
2023 // The shortcut action consumes the MENU. Even if it is still down,
2024 // it won't trigger the next shortcut action. In the case of the
2025 // shortcut action triggering a new activity, like Bookmarks, we
2026 // won't get onKeyUp for MENU. So it is important to reset it here.
2027 mMenuIsDown = false;
2028 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002029 if (mUi.onOptionsItemSelected(item)) {
2030 // ui callback handled it
2031 return true;
2032 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002033 switch (item.getItemId()) {
2034 // -- Main menu
2035 case R.id.new_tab_menu_id:
2036 openTabToHomePage();
2037 break;
2038
2039 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002040 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002041 break;
2042
2043 case R.id.goto_menu_id:
2044 editUrl();
2045 break;
2046
2047 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002048 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2049 break;
2050
2051 case R.id.history_menu_id:
2052 bookmarksOrHistoryPicker(ComboViews.History);
2053 break;
2054
2055 case R.id.snapshots_menu_id:
2056 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002057 break;
2058
Pankaj Garg49b79252014-11-07 17:33:41 -08002059 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002060 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002061 break;
2062
2063 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002064 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002065 stopLoading();
2066 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002067 Tab currentTab = mTabControl.getCurrentTab();
2068 if (currentTab.hasCrashed) {
2069 currentTab.replaceCrashView(getCurrentTopWebView(),
2070 currentTab.getViewContainer());
2071 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002072 getCurrentTopWebView().reload();
2073 }
2074 break;
2075
Michael Kolb8233fac2010-10-26 16:08:53 -07002076 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002077 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002078 break;
2079
2080 case R.id.close_menu_id:
2081 // Close the subwindow if it exists.
2082 if (mTabControl.getCurrentSubWindow() != null) {
2083 dismissSubWindow(mTabControl.getCurrentTab());
2084 break;
2085 }
2086 closeCurrentTab();
2087 break;
2088
kaiyiza8b6dbb2013-07-29 18:11:22 +08002089 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002090 Object[] params = { new String("persist.debug.browsermonkeytest")};
2091 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002092 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002093 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002094 if (ret != null && ret.equals("enable"))
2095 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002096 if (BrowserConfig.getInstance(getContext())
2097 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2098 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002099 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002100 case R.id.homepage_menu_id:
2101 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002102 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002103 break;
2104
2105 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002106 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002107 break;
2108
2109 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002110 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002111 break;
2112
John Reck2bc80422011-06-30 15:11:49 -07002113 case R.id.save_snapshot_menu_id:
2114 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002115 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002116 createScreenshotAsync(
2117 source.getWebView(),
2118 getDesiredThumbnailWidth(mActivity),
2119 getDesiredThumbnailHeight(mActivity),
2120 new ValueCallback<Bitmap>() {
2121 @Override
2122 public void onReceiveValue(Bitmap bitmap) {
2123 new SaveSnapshotTask(source, bitmap).execute();
2124 }
2125 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002126 break;
2127
Michael Kolb8233fac2010-10-26 16:08:53 -07002128 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002129 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002130 break;
2131
John Recke1a03a32011-09-14 17:04:16 -07002132 case R.id.snapshot_go_live:
2133 goLive();
2134 return true;
2135
Michael Kolb8233fac2010-10-26 16:08:53 -07002136 case R.id.share_page_menu_id:
2137 Tab currentTab = mTabControl.getCurrentTab();
2138 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 return false;
2140 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002141 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002142 break;
2143
2144 case R.id.dump_nav_menu_id:
2145 getCurrentTopWebView().debugDump();
2146 break;
2147
Michael Kolb8233fac2010-10-26 16:08:53 -07002148 case R.id.zoom_in_menu_id:
2149 getCurrentTopWebView().zoomIn();
2150 break;
2151
2152 case R.id.zoom_out_menu_id:
2153 getCurrentTopWebView().zoomOut();
2154 break;
2155
2156 case R.id.view_downloads_menu_id:
2157 viewDownloads();
2158 break;
2159
John Reck42229bc2011-08-19 13:26:43 -07002160 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002161 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002162 break;
2163
Michael Kolb8233fac2010-10-26 16:08:53 -07002164 case R.id.window_one_menu_id:
2165 case R.id.window_two_menu_id:
2166 case R.id.window_three_menu_id:
2167 case R.id.window_four_menu_id:
2168 case R.id.window_five_menu_id:
2169 case R.id.window_six_menu_id:
2170 case R.id.window_seven_menu_id:
2171 case R.id.window_eight_menu_id:
2172 {
2173 int menuid = item.getItemId();
2174 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2175 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2176 Tab desiredTab = mTabControl.getTab(id);
2177 if (desiredTab != null &&
2178 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002179 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002180 }
2181 break;
2182 }
2183 }
2184 }
2185 break;
2186
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002187 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002188 Bundle bundle = new Bundle();
2189 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2190 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2191 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2192 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2193 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002194 break;
2195
Pankaj Garg49b79252014-11-07 17:33:41 -08002196 case R.id.add_to_homescreen:
2197 final WebView w = getCurrentTopWebView();
2198 final EditText input = new EditText(getContext());
2199 input.setText(w.getTitle());
2200 new AlertDialog.Builder(getContext())
2201 .setTitle("Add to homescreen")
2202 .setMessage("Title")
2203 .setView(input)
2204 .setPositiveButton("Add", new DialogInterface.OnClickListener() {
2205 public void onClick(DialogInterface dialog, int whichButton) {
2206 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2207 getContext(),
2208 w.getUrl(),
2209 input.getText().toString(),
2210 w.getViewportBitmap(),
2211 w.getFavicon()));
2212
2213 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2214 .addCategory(Intent.CATEGORY_HOME));
2215 }})
2216 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
2217 public void onClick(DialogInterface dialog, int whichButton) {
2218 // Do nothing.
2219 }
2220 })
2221 .show();
2222 break;
2223
Michael Kolb8233fac2010-10-26 16:08:53 -07002224 default:
2225 return false;
2226 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002227 return true;
2228 }
2229
John Reck68234a92012-04-19 15:27:12 -07002230 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2231 implements OnCancelListener {
2232
2233 private Tab mTab;
2234 private Dialog mProgressDialog;
2235 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002236 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002237
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002238 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002239 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002240 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002241 }
2242
2243 @Override
2244 protected void onPreExecute() {
2245 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2246 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2247 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002248 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002249 }
2250
2251 @Override
2252 protected Long doInBackground(Void... params) {
2253 if (!mTab.saveViewState(mValues)) {
2254 return null;
2255 }
2256 if (isCancelled()) {
2257 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2258 File file = mActivity.getFileStreamPath(path);
2259 if (!file.delete()) {
2260 file.deleteOnExit();
2261 }
2262 return null;
2263 }
2264 final ContentResolver cr = mActivity.getContentResolver();
2265 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2266 if (result == null) {
2267 return null;
2268 }
2269 long id = ContentUris.parseId(result);
2270 return id;
2271 }
2272
2273 @Override
2274 protected void onPostExecute(Long id) {
2275 if (isCancelled()) {
2276 return;
2277 }
2278 mProgressDialog.dismiss();
2279 if (id == null) {
2280 Toast.makeText(mActivity, R.string.snapshot_failed,
2281 Toast.LENGTH_SHORT).show();
2282 return;
2283 }
2284 Bundle b = new Bundle();
2285 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2286 mUi.showComboView(ComboViews.Snapshots, b);
2287 }
2288
2289 @Override
2290 public void onCancel(DialogInterface dialog) {
2291 cancel(true);
2292 }
2293 }
2294
Michael Kolb80f75082012-04-10 10:50:06 -07002295 @Override
2296 public void toggleUserAgent() {
2297 WebView web = getCurrentWebView();
2298 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002299 }
2300
2301 @Override
2302 public void findOnPage() {
2303 getCurrentTopWebView().showFindDialog(null, true);
2304 }
2305
2306 @Override
2307 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002308 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002309 }
2310
2311 @Override
2312 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002313 WebView w = getCurrentTopWebView();
2314 if (w == null)
2315 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002316 final Intent i = createBookmarkCurrentPageIntent(false);
2317 createScreenshotAsync(
2318 w, getDesiredThumbnailWidth(mActivity),
2319 getDesiredThumbnailHeight(mActivity),
2320 new ValueCallback<Bitmap>() {
2321 @Override
2322 public void onReceiveValue(Bitmap bitmap) {
2323 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2324 mActivity.startActivity(i);
2325 }
2326 });
Michael Kolb80f75082012-04-10 10:50:06 -07002327 }
2328
John Recke1a03a32011-09-14 17:04:16 -07002329 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002330 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002331 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002332 boolean onlySingleTabRemaining = false;
2333 if (mTabControl.getTabCount() > 1) {
2334 // destroy the old snapshot tab
2335 closeCurrentTab();
2336 } else {
2337 onlySingleTabRemaining = true;
2338 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002339 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002340 if (onlySingleTabRemaining) {
2341 closeTab(t);
2342 }
2343
Tarun Nainani8eb00912014-07-17 12:28:32 -07002344 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002345 }
2346
luxiaolb40014b2013-07-19 10:01:43 +08002347 private void showExitDialog(final Activity activity) {
2348 new AlertDialog.Builder(activity)
2349 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002350 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002351 .setMessage(R.string.exit_browser_msg)
2352 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2353 public void onClick(DialogInterface dialog, int which) {
2354 activity.moveTaskToBack(true);
2355 dialog.dismiss();
2356 }
2357 })
2358 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2359 public void onClick(DialogInterface dialog, int which) {
2360 activity.finish();
2361 mHandler.postDelayed(new Runnable() {
2362 @Override
2363 public void run() {
luxiaolb40014b2013-07-19 10:01:43 +08002364 mCrashRecoveryHandler.clearState(true);
2365 int pid = android.os.Process.myPid();
2366 android.os.Process.killProcess(pid);
2367 }
2368 }, 300);
2369 dialog.dismiss();
2370 }
2371 })
2372 .show();
2373 }
Michael Kolb315d5022011-10-13 12:47:11 -07002374 @Override
2375 public void showPageInfo() {
2376 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2377 }
2378
John Reck9c35b9c2012-05-30 10:08:50 -07002379 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002380 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002381 // Let the History and Bookmark fragments handle menus they created.
2382 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2383 return false;
2384 }
2385
Michael Kolb8233fac2010-10-26 16:08:53 -07002386 int id = item.getItemId();
2387 boolean result = true;
2388 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002389 // -- Browser context menu
2390 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002391 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002392 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002393 case R.id.copy_link_context_menu_id:
2394 final WebView webView = getCurrentTopWebView();
2395 if (null == webView) {
2396 result = false;
2397 break;
2398 }
2399 final HashMap<String, WebView> hrefMap =
2400 new HashMap<String, WebView>();
2401 hrefMap.put("webview", webView);
2402 final Message msg = mHandler.obtainMessage(
2403 FOCUS_NODE_HREF, id, 0, hrefMap);
2404 webView.requestFocusNodeHref(msg);
2405 break;
2406
2407 default:
2408 // For other context menus
2409 result = onOptionsItemSelected(item);
2410 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002411 return result;
2412 }
2413
2414 /**
2415 * support programmatically opening the context menu
2416 */
2417 public void openContextMenu(View view) {
2418 mActivity.openContextMenu(view);
2419 }
2420
2421 /**
2422 * programmatically open the options menu
2423 */
2424 public void openOptionsMenu() {
2425 mActivity.openOptionsMenu();
2426 }
2427
John Reck9c35b9c2012-05-30 10:08:50 -07002428 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002429 public boolean onMenuOpened(int featureId, Menu menu) {
2430 if (mOptionsMenuOpen) {
2431 if (mConfigChanged) {
2432 // We do not need to make any changes to the state of the
2433 // title bar, since the only thing that happened was a
2434 // change in orientation
2435 mConfigChanged = false;
2436 } else {
2437 if (!mExtendedMenuOpen) {
2438 mExtendedMenuOpen = true;
2439 mUi.onExtendedMenuOpened();
2440 } else {
2441 // Switching the menu back to icon view, so show the
2442 // title bar once again.
2443 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002444 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002445 }
2446 }
2447 } else {
2448 // The options menu is closed, so open it, and show the title
2449 mOptionsMenuOpen = true;
2450 mConfigChanged = false;
2451 mExtendedMenuOpen = false;
2452 mUi.onOptionsMenuOpened();
2453 }
2454 return true;
2455 }
2456
John Reck9c35b9c2012-05-30 10:08:50 -07002457 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002458 public void onOptionsMenuClosed(Menu menu) {
2459 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002460 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002461 }
2462
John Reck9c35b9c2012-05-30 10:08:50 -07002463 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002464 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002465 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002466 }
2467
2468 // Helper method for getting the top window.
2469 @Override
2470 public WebView getCurrentTopWebView() {
2471 return mTabControl.getCurrentTopWebView();
2472 }
2473
2474 @Override
2475 public WebView getCurrentWebView() {
2476 return mTabControl.getCurrentWebView();
2477 }
2478
2479 /*
2480 * This method is called as a result of the user selecting the options
2481 * menu to see the download window. It shows the download window on top of
2482 * the current window.
2483 */
2484 void viewDownloads() {
2485 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2486 mActivity.startActivity(intent);
2487 }
2488
John Reck30b065e2011-07-19 10:58:05 -07002489 int getActionModeHeight() {
2490 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2491 new int[] { android.R.attr.actionBarSize });
2492 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2493 actionBarSizeTypedArray.recycle();
2494 return size;
2495 }
2496
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 // action mode
2498
John Reck9c35b9c2012-05-30 10:08:50 -07002499 @Override
2500 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002501 mUi.onActionModeStarted(mode);
2502 mActionMode = mode;
2503 }
2504
2505 /*
2506 * True if a custom ActionMode (i.e. find or select) is in use.
2507 */
2508 @Override
2509 public boolean isInCustomActionMode() {
2510 return mActionMode != null;
2511 }
2512
2513 /*
2514 * End the current ActionMode.
2515 */
2516 @Override
2517 public void endActionMode() {
2518 if (mActionMode != null) {
2519 mActionMode.finish();
2520 }
2521 }
2522
2523 /*
2524 * Called by find and select when they are finished. Replace title bars
2525 * as necessary.
2526 */
John Reck9c35b9c2012-05-30 10:08:50 -07002527 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002528 public void onActionModeFinished(ActionMode mode) {
2529 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002530 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002531 mActionMode = null;
2532 }
2533
2534 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002535 final Tab tab = getCurrentTab();
2536 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002537 }
2538
2539 // bookmark handling
2540
2541 /**
2542 * add the current page as a bookmark to the given folder id
2543 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002544 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002545 * bookmarked, and if it is, edit that bookmark. If false, and
2546 * the site is already bookmarked, do not attempt to edit the
2547 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002548 */
2549 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002550 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002551 WebView w = getCurrentTopWebView();
2552 if (w == null) {
2553 return null;
2554 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002555 Intent i = new Intent(mActivity,
2556 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002557 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2558 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2559 String touchIconUrl = w.getTouchIconUrl();
2560 if (touchIconUrl != null) {
2561 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2562 WebSettings settings = w.getSettings();
2563 if (settings != null) {
2564 i.putExtra(AddBookmarkPage.USER_AGENT,
2565 settings.getUserAgentString());
2566 }
2567 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002568 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002569 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002570 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002571 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2572 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002573 // Put the dialog at the upper right of the screen, covering the
2574 // star on the title bar.
2575 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002576 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002577 }
2578
2579 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002580 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002581 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002582 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002583 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002584 }
2585
Vivek Sekharb54614f2014-05-01 19:03:37 -07002586 @Override
2587 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2588 boolean capture) {
2589 mUploadHandler = new UploadHandler(this);
2590 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2591 }
2592
Michael Kolb8233fac2010-10-26 16:08:53 -07002593 // thumbnails
2594
2595 /**
2596 * Return the desired width for thumbnail screenshots, which are stored in
2597 * the database, and used on the bookmarks screen.
2598 * @param context Context for finding out the density of the screen.
2599 * @return desired width for thumbnail screenshot.
2600 */
2601 static int getDesiredThumbnailWidth(Context context) {
2602 return context.getResources().getDimensionPixelOffset(
2603 R.dimen.bookmarkThumbnailWidth);
2604 }
2605
2606 /**
2607 * Return the desired height for thumbnail screenshots, which are stored in
2608 * the database, and used on the bookmarks screen.
2609 * @param context Context for finding out the density of the screen.
2610 * @return desired height for thumbnail screenshot.
2611 */
2612 static int getDesiredThumbnailHeight(Context context) {
2613 return context.getResources().getDimensionPixelOffset(
2614 R.dimen.bookmarkThumbnailHeight);
2615 }
2616
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002617 static void createScreenshotAsync(WebView view, int width, int height,
2618 final ValueCallback<Bitmap> cb) {
2619 if (view == null || width == 0 || height == 0) {
2620 return;
2621 }
2622
2623 view.getContentBitmapAsync(
2624 (float) width / view.getWidth(),
2625 new Rect(),
2626 new ValueCallback<Bitmap>() {
2627 @Override
2628 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002629 if (bitmap != null)
2630 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2631 cb.onReceiveValue(bitmap);
2632 }});
2633 }
2634
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002635 private void updateScreenshot(final Tab tab) {
2636 createScreenshotAsync(
2637 tab.getWebView(),
2638 getDesiredThumbnailWidth(mActivity),
2639 getDesiredThumbnailHeight(mActivity),
2640 new ValueCallback<Bitmap>() {
2641 @Override
2642 public void onReceiveValue(Bitmap bitmap) {
2643 updateScreenshot(tab, bitmap);
2644 }
2645 });
2646 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002647
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002648 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002649 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002650 // FIXME: Would like to make sure there is actually something to
2651 // draw, but the API for that (WebViewCore.pictureReady()) is not
2652 // currently accessible here.
2653
John Reck34ef2672011-02-10 11:30:55 -08002654 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002655 if (view == null) {
2656 // Tab was destroyed
2657 return;
2658 }
John Reck34ef2672011-02-10 11:30:55 -08002659 final String url = tab.getUrl();
2660 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002661 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002662 if (TextUtils.isEmpty(url)) {
2663 return;
2664 }
2665
kaiyiz6e5b3e02013-08-19 20:02:01 +08002666 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002667 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002668 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002669 }
John Reck34ef2672011-02-10 11:30:55 -08002670 // Only update thumbnails for web urls (http(s)://), not for
2671 // about:, javascript:, data:, etc...
2672 // Unless it is a bookmarked site, then always update
2673 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2674 return;
2675 }
2676
kaiyiz6e5b3e02013-08-19 20:02:01 +08002677 if (url != null && mUpdateMyNavThumbnailUrl != null
2678 && Patterns.WEB_URL.matcher(url).matches()
2679 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2680 String urlHost = (new WebAddress(url)).getHost();
2681 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2682 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2683 || bookmarkHost.length() == 0) {
2684 return;
2685 }
2686 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2687 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2688 bookmarkHost.length());
2689 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2690 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2691 if (!bookmarkDomain.equals(urlDomain)) {
2692 return;
2693 }
2694 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002695 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002696 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2697 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2698 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2699 500);
2700 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002701 return;
2702 }
2703
2704 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002705 new AsyncTask<Void, Void, Void>() {
2706 @Override
2707 protected Void doInBackground(Void... unused) {
2708 Cursor cursor = null;
2709 try {
2710 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002711 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2712 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2713 : url);
2714 if (mUpdateMyNavThumbnail) {
2715 mUpdateMyNavThumbnail = false;
2716 mUpdateMyNavThumbnailUrl = null;
2717 }
John Reck34ef2672011-02-10 11:30:55 -08002718 if (cursor != null && cursor.moveToFirst()) {
2719 final ByteArrayOutputStream os =
2720 new ByteArrayOutputStream();
2721 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002722
John Reck34ef2672011-02-10 11:30:55 -08002723 ContentValues values = new ContentValues();
2724 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002725
John Reck34ef2672011-02-10 11:30:55 -08002726 do {
John Reck617fd832011-02-16 14:35:59 -08002727 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002728 cr.update(Images.CONTENT_URI, values, null, null);
2729 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 }
John Reck34ef2672011-02-10 11:30:55 -08002731 } catch (IllegalStateException e) {
2732 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002733 } catch (SQLiteException s) {
2734 // Added for possible error when user tries to remove the same bookmark
2735 // that is being updated with a screen shot
2736 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002737 } finally {
2738 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002739 }
John Reck34ef2672011-02-10 11:30:55 -08002740 return null;
2741 }
2742 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002743 }
2744
2745 private class Copy implements OnMenuItemClickListener {
2746 private CharSequence mText;
2747
John Reck9c35b9c2012-05-30 10:08:50 -07002748 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002749 public boolean onMenuItemClick(MenuItem item) {
2750 copy(mText);
2751 return true;
2752 }
2753
2754 public Copy(CharSequence toCopy) {
2755 mText = toCopy;
2756 }
2757 }
2758
Leon Scroggins63c02662010-11-18 15:16:27 -05002759 private static class Download implements OnMenuItemClickListener {
2760 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002761 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002762 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002763 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002764 private static final String FALLBACK_EXTENSION = "dat";
2765 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002766
John Reck9c35b9c2012-05-30 10:08:50 -07002767 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002768 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002769 if (DataUri.isDataUri(mText)) {
2770 saveDataUri();
2771 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002772 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002773 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002774 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002775 return true;
2776 }
2777
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002778 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2779 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002780 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002781 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002782 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002783 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002784 }
George Mount387d45d2011-10-07 15:57:53 -07002785
2786 /**
2787 * Treats mText as a data URI and writes its contents to a file
2788 * based on the current time.
2789 */
2790 private void saveDataUri() {
2791 FileOutputStream outputStream = null;
2792 try {
2793 DataUri uri = new DataUri(mText);
2794 File target = getTarget(uri);
2795 outputStream = new FileOutputStream(target);
2796 outputStream.write(uri.getData());
2797 final DownloadManager manager =
2798 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2799 manager.addCompletedDownload(target.getName(),
2800 mActivity.getTitle().toString(), false,
2801 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002802 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002803 } catch (IOException e) {
2804 Log.e(LOGTAG, "Could not save data URL");
2805 } finally {
2806 if (outputStream != null) {
2807 try {
2808 outputStream.close();
2809 } catch (IOException e) {
2810 // ignore close errors
2811 }
2812 }
2813 }
2814 }
2815
2816 /**
2817 * Creates a File based on the current time stamp and uses
2818 * the mime type of the DataUri to get the extension.
2819 */
2820 private File getTarget(DataUri uri) throws IOException {
2821 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002822 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002823 String nameBase = format.format(new Date());
2824 String mimeType = uri.getMimeType();
2825 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2826 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2827 if (extension == null) {
2828 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2829 extension = FALLBACK_EXTENSION;
2830 }
2831 extension = "." + extension; // createTempFile needs the '.'
2832 File targetFile = File.createTempFile(nameBase, extension, dir);
2833 return targetFile;
2834 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002835 }
2836
Cary Clark8974d282010-11-22 10:46:05 -05002837 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002838 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002839
John Reck9c35b9c2012-05-30 10:08:50 -07002840 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002841 public boolean onMenuItemClick(MenuItem item) {
2842 if (mWebView != null) {
2843 return mWebView.selectText();
2844 }
2845 return false;
2846 }
2847
2848 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002849 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002850 }
2851
2852 }
2853
Michael Kolb8233fac2010-10-26 16:08:53 -07002854 /********************** TODO: UI stuff *****************************/
2855
2856 // these methods have been copied, they still need to be cleaned up
2857
2858 /****************** tabs ***************************************************/
2859
2860 // basic tab interactions:
2861
2862 // it is assumed that tabcontrol already knows about the tab
2863 protected void addTab(Tab tab) {
2864 mUi.addTab(tab);
2865 }
2866
2867 protected void removeTab(Tab tab) {
2868 mUi.removeTab(tab);
2869 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002870 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002871 }
2872
Michael Kolbf2055602011-04-09 17:20:03 -07002873 @Override
2874 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002875 // monkey protection against delayed start
2876 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002877
2878 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2879 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002880 mTabControl.setCurrentTab(tab);
2881 // the tab is guaranteed to have a webview after setCurrentTab
2882 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002883
2884
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002885 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002886 //Purge active tabs
2887 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002888 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002889 }
2890
John Reck8bcafc12011-08-29 16:43:02 -07002891 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002892 Tab current = mTabControl.getCurrentTab();
2893 if (current != null
2894 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002895 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002896 }
2897 }
2898
John Reck26b18322011-06-21 13:08:58 -07002899 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002900 //Cancel navscreen request
2901 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002902 // Dismiss the subwindow if applicable.
2903 dismissSubWindow(appTab);
2904 // Since we might kill the WebView, remove it from the
2905 // content view first.
2906 mUi.detachTab(appTab);
2907 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002908 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002909 // TODO: analyze why the remove and add are necessary
2910 mUi.attachTab(appTab);
2911 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002912 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002913 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002914 } else {
2915 // If the tab was the current tab, we have to attach
2916 // it to the view system again.
2917 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002918 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002919 }
2920 }
2921
2922 // Remove the sub window if it exists. Also called by TabControl when the
2923 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002924 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002925 public void dismissSubWindow(Tab tab) {
2926 removeSubWindow(tab);
2927 // dismiss the subwindow. This will destroy the WebView.
2928 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002929 WebView wv = getCurrentTopWebView();
2930 if (wv != null) {
2931 wv.requestFocus();
2932 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002933 }
2934
2935 @Override
2936 public void removeSubWindow(Tab t) {
2937 if (t.getSubWebView() != null) {
2938 mUi.removeSubWindow(t.getSubViewContainer());
2939 }
2940 }
2941
2942 @Override
2943 public void attachSubWindow(Tab tab) {
2944 if (tab.getSubWebView() != null) {
2945 mUi.attachSubWindow(tab.getSubViewContainer());
2946 getCurrentTopWebView().requestFocus();
2947 }
2948 }
2949
Mathew Inwood29721c22011-06-29 17:55:24 +01002950 private Tab showPreloadedTab(final UrlData urlData) {
2951 if (!urlData.isPreloaded()) {
2952 return null;
2953 }
2954 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2955 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2956 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002957 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002958 // Could not submit query. Fallback to regular tab creation
2959 tabControl.destroy();
2960 return null;
2961 }
2962 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002963 // check tab count and make room for new tab
2964 if (!mTabControl.canCreateNewTab()) {
2965 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2966 if (leastUsed != null) {
2967 closeTab(leastUsed);
2968 }
2969 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002970 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002971 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002972 mTabControl.addPreloadedTab(t);
2973 addTab(t);
2974 setActiveTab(t);
2975 return t;
2976 }
2977
Michael Kolb7bcafde2011-05-09 13:55:59 -07002978 // open a non inconito tab with the given url data
2979 // and set as active tab
2980 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002981 Tab tab = showPreloadedTab(urlData);
2982 if (tab == null) {
2983 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002984 if ((tab != null) && !urlData.isEmpty()) {
2985 loadUrlDataIn(tab, urlData);
2986 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002987 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002988 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002989 }
2990
Michael Kolb843510f2010-12-09 10:51:49 -08002991 @Override
2992 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002993 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002994 }
2995
Michael Kolb8233fac2010-10-26 16:08:53 -07002996 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002997 public Tab openIncognitoTab() {
2998 return openTab(INCOGNITO_URI, true, true, false);
2999 }
3000
3001 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003002 public Tab openTab(String url, boolean incognito, boolean setActive,
3003 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003004 return openTab(url, incognito, setActive, useCurrent, null);
3005 }
3006
3007 @Override
3008 public Tab openTab(String url, Tab parent, boolean setActive,
3009 boolean useCurrent) {
3010 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3011 setActive, useCurrent, parent);
3012 }
3013
3014 public Tab openTab(String url, boolean incognito, boolean setActive,
3015 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003016 Tab tab = createNewTab(incognito, setActive, useCurrent);
3017 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003018 if (parent instanceof SnapshotTab) {
3019 addTab(tab);
3020 if (setActive)
3021 setActiveTab(tab);
3022 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003023 parent.addChildTab(tab);
3024 }
Michael Kolb519d2282011-05-09 17:03:19 -07003025 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003026 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003027 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003028 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003029 return tab;
3030 }
3031
3032 // this method will attempt to create a new tab
3033 // incognito: private browsing tab
3034 // setActive: ste tab as current tab
3035 // useCurrent: if no new tab can be created, return current tab
3036 private Tab createNewTab(boolean incognito, boolean setActive,
3037 boolean useCurrent) {
3038 Tab tab = null;
3039 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05003040 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003041 addTab(tab);
3042 if (setActive) {
3043 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07003044 } else {
3045 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003046 }
3047 } else {
3048 if (useCurrent) {
3049 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07003050 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003051 } else {
3052 mUi.showMaxTabsWarning();
3053 }
3054 }
3055 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003056 }
3057
John Reck2bc80422011-06-30 15:11:49 -07003058 @Override
3059 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3060 SnapshotTab tab = null;
3061 if (mTabControl.canCreateNewTab()) {
3062 tab = mTabControl.createSnapshotTab(snapshotId);
3063 addTab(tab);
3064 if (setActive) {
3065 setActiveTab(tab);
3066 }
3067 } else {
3068 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003069 }
3070 return tab;
3071 }
3072
Michael Kolb8233fac2010-10-26 16:08:53 -07003073 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003074 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 * @return boolean True if we successfully switched to a different tab. If
3076 * the indexth tab is null, or if that tab is the same as
3077 * the current one, return false.
3078 */
3079 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003080 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003081 Tab currentTab = mTabControl.getCurrentTab();
3082 if (tab == null || tab == currentTab) {
3083 return false;
3084 }
3085 setActiveTab(tab);
3086 return true;
3087 }
3088
3089 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003090 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003091 closeCurrentTab(false);
3092 }
3093
3094 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003095 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003096 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003097 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003098 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003099 return;
3100 }
Michael Kolbc831b632011-05-11 09:30:34 -07003101 final Tab current = mTabControl.getCurrentTab();
3102 final int pos = mTabControl.getCurrentPosition();
3103 Tab newTab = current.getParent();
3104 if (newTab == null) {
3105 newTab = mTabControl.getTab(pos + 1);
3106 if (newTab == null) {
3107 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003108 }
3109 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003110 if (andQuit) {
3111 mTabControl.setCurrentTab(newTab);
3112 closeTab(current);
3113 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003114 // Close window
3115 closeTab(current);
3116 }
3117 }
3118
3119 /**
3120 * Close the tab, remove its associated title bar, and adjust mTabControl's
3121 * current tab to a valid value.
3122 */
3123 @Override
3124 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003125 if (tab == mTabControl.getCurrentTab()) {
3126 closeCurrentTab();
3127 } else {
3128 removeTab(tab);
3129 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003130 }
3131
Afzal Najamd4e33312012-04-26 01:54:01 -04003132 /**
3133 * Close all tabs except the current one
3134 */
3135 @Override
3136 public void closeOtherTabs() {
3137 int inactiveTabs = mTabControl.getTabCount() - 1;
3138 for (int i = inactiveTabs; i >= 0; i--) {
3139 Tab tab = mTabControl.getTab(i);
3140 if (tab != mTabControl.getCurrentTab()) {
3141 removeTab(tab);
3142 }
3143 }
3144 }
3145
Michael Kolb8233fac2010-10-26 16:08:53 -07003146 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003147 protected void loadUrlFromContext(String url) {
3148 Tab tab = getCurrentTab();
3149 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003150 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003151 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003152 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003153 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003154 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003155 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003156 }
3157 }
3158 }
3159
3160 /**
3161 * Load the URL into the given WebView and update the title bar
3162 * to reflect the new load. Call this instead of WebView.loadUrl
3163 * directly.
3164 * @param view The WebView used to load url.
3165 * @param url The URL to load.
3166 */
John Reck71e51422011-07-01 16:49:28 -07003167 @Override
3168 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003169 loadUrl(tab, url, null);
3170 }
3171
3172 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3173 if (tab != null) {
3174 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003175 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003176 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003177 if (tab.hasCrashed) {
3178 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3179 }
Michael Kolba53c9892011-10-05 13:31:40 -07003180 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003181 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003182 }
3183
3184 /**
3185 * Load UrlData into a Tab and update the title bar to reflect the new
3186 * load. Call this instead of UrlData.loadIn directly.
3187 * @param t The Tab used to load.
3188 * @param data The UrlData being loaded.
3189 */
3190 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003191 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003192 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003193 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003194 } else {
John Reck38b39652012-06-05 09:22:59 -07003195 if (t != null && data.mDisableUrlOverride) {
3196 t.disableUrlOverridingForLoad();
3197 }
John Reck26b18322011-06-21 13:08:58 -07003198 loadUrl(t, data.mUrl, data.mHeaders);
3199 }
3200 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003201 }
3202
John Reck30c714c2010-12-16 17:30:34 -08003203 @Override
3204 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003205 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003206 //In case of tab can go back (aka tab has navigation entry) do nothing
3207 //else just load homepage in current tab.
3208 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003209 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003210 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003211 }
3212
3213 void goBackOnePageOrQuit() {
3214 Tab current = mTabControl.getCurrentTab();
3215 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003216 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3217 showExitDialog(mActivity);
3218 } else {
3219 /*
3220 * Instead of finishing the activity, simply push this to the back
3221 * of the stack and let ActivityManager to choose the foreground
3222 * activity. As BrowserActivity is singleTask, it will be always the
3223 * root of the task. So we can use either true or false for
3224 * moveTaskToBack().
3225 */
3226 mActivity.moveTaskToBack(true);
3227 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003228 return;
3229 }
John Reckef654f12011-07-12 16:42:08 -07003230 if (current.canGoBack()) {
3231 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003232 } else {
3233 // Check to see if we are closing a window that was created by
3234 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003235 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003236 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003237 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003238 // Now we close the other tab
3239 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003240 } else if (BrowserConfig.getInstance(getContext())
3241 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3242 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003243 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003244 if ((current.getAppId() != null) || current.closeOnBack()) {
3245 closeCurrentTab(true);
3246 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003247 /*
3248 * Instead of finishing the activity, simply push this to the back
3249 * of the stack and let ActivityManager to choose the foreground
3250 * activity. As BrowserActivity is singleTask, it will be always the
3251 * root of the task. So we can use either true or false for
3252 * moveTaskToBack().
3253 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003254 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003255 }
3256 }
3257 }
3258
3259 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003260 * helper method for key handler
3261 * returns the current tab if it can't advance
3262 */
Michael Kolbc831b632011-05-11 09:30:34 -07003263 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003264 int pos = mTabControl.getCurrentPosition() + 1;
3265 if (pos >= mTabControl.getTabCount()) {
3266 pos = 0;
3267 }
3268 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003269 }
3270
3271 /**
3272 * helper method for key handler
3273 * returns the current tab if it can't advance
3274 */
Michael Kolbc831b632011-05-11 09:30:34 -07003275 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003276 int pos = mTabControl.getCurrentPosition() - 1;
3277 if ( pos < 0) {
3278 pos = mTabControl.getTabCount() - 1;
3279 }
3280 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003281 }
3282
John Reckbcef87f2012-02-03 14:58:34 -08003283 boolean isMenuOrCtrlKey(int keyCode) {
3284 return (KeyEvent.KEYCODE_MENU == keyCode)
3285 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3286 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3287 }
3288
Michael Kolb0035fad2011-03-14 13:25:28 -07003289 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003290 * handle key events in browser
3291 *
3292 * @param keyCode
3293 * @param event
3294 * @return true if handled, false to pass to super
3295 */
John Reck9c35b9c2012-05-30 10:08:50 -07003296 @Override
3297 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003298 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3299 // Hardware menu key
Enrico Ros1f5a0952014-11-18 20:15:48 -08003300 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
Pankaj Garg49b79252014-11-07 17:33:41 -08003301 true, false);
3302 return true;
3303 }
3304
Cary Clark160bbb92011-01-10 11:17:07 -05003305 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003306 // Even if MENU is already held down, we need to call to super to open
3307 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003308 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003309 mMenuIsDown = true;
3310 return false;
3311 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003312
Cary Clark8ff8c662010-12-29 15:03:05 -05003313 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003314 Tab tab = getCurrentTab();
3315 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003316
Cary Clark160bbb92011-01-10 11:17:07 -05003317 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3318 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003319
Michael Kolb8233fac2010-10-26 16:08:53 -07003320 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003321 case KeyEvent.KEYCODE_TAB:
3322 if (event.isCtrlPressed()) {
3323 if (event.isShiftPressed()) {
3324 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003325 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003326 } else {
3327 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003328 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003329 }
3330 return true;
3331 }
3332 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003333 case KeyEvent.KEYCODE_SPACE:
3334 // WebView/WebTextView handle the keys in the KeyDown. As
3335 // the Activity's shortcut keys are only handled when WebView
3336 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003337 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003338 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003339 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003340 pageDown();
3341 }
3342 return true;
3343 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003344 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003345 event.startTracking();
3346 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003347 case KeyEvent.KEYCODE_FORWARD:
3348 if (!noModifiers) break;
3349 tab.goForward();
3350 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003351 case KeyEvent.KEYCODE_DPAD_LEFT:
3352 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003353 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003354 return true;
3355 }
3356 break;
3357 case KeyEvent.KEYCODE_DPAD_RIGHT:
3358 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003359 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003360 return true;
3361 }
3362 break;
3363 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003364 if (ctrl) {
3365 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003366 return true;
3367 }
3368 break;
Michael Kolba4183062011-01-16 10:43:21 -08003369// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003370 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003371 if (ctrl ) {
3372 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003373 return true;
3374 }
3375 break;
Michael Kolba4183062011-01-16 10:43:21 -08003376// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003377// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003378// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003379// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003380// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003381// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003382// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003383// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003384// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003385// case KeyEvent.KEYCODE_M: // unused
3386// case KeyEvent.KEYCODE_N: // in Chrome: new window
3387// case KeyEvent.KEYCODE_O: // in Chrome: open file
3388// case KeyEvent.KEYCODE_P: // in Chrome: print page
3389// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003390// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003391// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3392 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003393 // we can't use the ctrl/shift flags, they check for
3394 // exclusive use of a modifier
3395 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003396 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003397 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003398 } else {
3399 openTabToHomePage();
3400 }
3401 return true;
3402 }
3403 break;
3404// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3405// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003406// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003407// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3408// case KeyEvent.KEYCODE_Y: // unused
3409// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003410 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003411 // it is a regular key and webview is not null
3412 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003413 }
3414
John Reck9c35b9c2012-05-30 10:08:50 -07003415 @Override
3416 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003417 switch(keyCode) {
3418 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003419 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003420 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003421 return true;
3422 }
3423 break;
3424 }
3425 return false;
3426 }
3427
John Reck9c35b9c2012-05-30 10:08:50 -07003428 @Override
3429 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003430 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003431 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003432 if (KeyEvent.KEYCODE_MENU == keyCode
3433 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003434 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003435 }
3436 }
Cary Clark160bbb92011-01-10 11:17:07 -05003437 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003438 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003439 case KeyEvent.KEYCODE_BACK:
3440 if (event.isTracking() && !event.isCanceled()) {
3441 onBackKey();
3442 return true;
3443 }
3444 break;
3445 }
3446 return false;
3447 }
3448
3449 public boolean isMenuDown() {
3450 return mMenuIsDown;
3451 }
3452
John Reck9c35b9c2012-05-30 10:08:50 -07003453 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003454 public void setupAutoFill(Message message) {
3455 // Open the settings activity at the AutoFill profile fragment so that
3456 // the user can create a new profile. When they return, we will dispatch
3457 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003458 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003459 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3460 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003461 }
John Reckb3417f02011-01-14 11:01:05 -08003462
John Reck9c35b9c2012-05-30 10:08:50 -07003463 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003464 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003465 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003466 return true;
3467 }
3468
John Reck1cf4b792011-07-26 10:22:22 -07003469 @Override
3470 public boolean shouldCaptureThumbnails() {
3471 return mUi.shouldCaptureThumbnails();
3472 }
3473
Michael Kolbc3af0672011-08-09 10:24:41 -07003474 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003475 public boolean supportsVoice() {
3476 PackageManager pm = mActivity.getPackageManager();
3477 List activities = pm.queryIntentActivities(new Intent(
3478 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3479 return activities.size() != 0;
3480 }
3481
3482 @Override
3483 public void startVoiceRecognizer() {
3484 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003485 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003486 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3487 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3488 mActivity.startActivityForResult(voice, VOICE_RESULT);
3489 }
3490
Pankaj Garg7b279f62014-08-12 14:47:18 -07003491 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003492 if (mLevel == level)
3493 return;
3494 mLevel = level;
3495 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003496 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3497 lp.dimAmount = level;
3498 mActivity.getWindow().setAttributes(lp);
3499 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3500 } else {
3501 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3502 }
3503 }
3504
Michael Kolb0b129122012-06-04 16:31:58 -07003505 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003506 public void setBlockEvents(boolean block) {
3507 mBlockEvents = block;
3508 }
3509
John Reck9c35b9c2012-05-30 10:08:50 -07003510 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003511 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003512 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003513 }
3514
John Reck9c35b9c2012-05-30 10:08:50 -07003515 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003516 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003517 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003518 }
3519
John Reck9c35b9c2012-05-30 10:08:50 -07003520 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003521 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003522 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003523 }
3524
John Reck9c35b9c2012-05-30 10:08:50 -07003525 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003526 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003527 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003528 }
3529
John Reck9c35b9c2012-05-30 10:08:50 -07003530 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003531 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003532 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003533 }
3534
Pankaj Garg49b79252014-11-07 17:33:41 -08003535 @Override
3536 public boolean shouldShowAppMenu() {
3537 return true;
3538 }
3539
3540 @Override
3541 public int getMenuThemeResourceId() {
3542 return R.style.OverflowMenuTheme;
3543 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003544}