blob: 0540e29dc4421e317c73b3807243edec355c9ffc [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;
kaiyiza016da12013-08-26 17:50:22 +080044import android.net.ConnectivityManager;
45import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.net.Uri;
47import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080048import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080049import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.os.AsyncTask;
51import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070052import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.os.Handler;
54import android.os.Message;
55import android.os.PowerManager;
56import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.provider.ContactsContract;
58import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080059import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070060import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070061import android.text.TextUtils;
62import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080063import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070064import android.view.ActionMode;
65import android.view.ContextMenu;
66import android.view.ContextMenu.ContextMenuInfo;
67import android.view.Gravity;
68import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070069import android.view.Menu;
70import android.view.MenuInflater;
71import android.view.MenuItem;
72import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070073import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070074import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070075import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070076import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070077import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080078import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080079import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050080import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070081
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080082import org.codeaurora.swe.CookieManager;
83import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080084import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080085import org.codeaurora.swe.HttpAuthHandler;
86import org.codeaurora.swe.SslErrorHandler;
87import org.codeaurora.swe.WebSettings;
88import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070089import org.codeaurora.swe.WebBackForwardList;
90import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080091
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070092import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070093import com.android.browser.R;
94import com.android.browser.IntentHandler.UrlData;
95import com.android.browser.UI.ComboViews;
96import com.android.browser.mynavigation.AddMyNavigationPage;
97import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070098import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070099import com.android.browser.platformsupport.BrowserContract;
100import com.android.browser.platformsupport.WebAddress;
101import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800102import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700103import com.android.browser.provider.BrowserProvider2.Thumbnails;
104import com.android.browser.provider.SnapshotProvider.Snapshots;
105import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800106import com.android.browser.appmenu.AppMenuHandler;
107import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700108
Michael Kolb8233fac2010-10-26 16:08:53 -0700109import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700110import java.io.File;
111import java.io.FileOutputStream;
112import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700113import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700114import java.text.DateFormat;
115import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700116import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700117import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700118import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800119import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200120import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700121import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700122
123/**
124 * Controller for browser
125 */
126public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800127 implements WebViewController, UiController, ActivityController,
128 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700129
130 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800131 private static final String SEND_APP_ID_EXTRA =
132 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700133 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800134
Vivek Sekharb54614f2014-05-01 19:03:37 -0700135 // Remind switch to data connection if wifi is unavailable
136 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800137
Michael Kolb8233fac2010-10-26 16:08:53 -0700138 // public message ids
139 public final static int LOAD_URL = 1001;
140 public final static int STOP_LOAD = 1002;
141
142 // Message Ids
143 private static final int FOCUS_NODE_HREF = 102;
144 private static final int RELEASE_WAKELOCK = 107;
145
146 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
147
148 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800149 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150
151 private static final int EMPTY_MENU = -1;
152
Michael Kolb8233fac2010-10-26 16:08:53 -0700153 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700154 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 final static int PREFERENCES_PAGE = 3;
156 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000157 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700158 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800159 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000160
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
162
163 // As the ids are dynamically created, we can't guarantee that they will
164 // be in sequence, so this static array maps ids to a window number.
165 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
166 { R.id.window_one_menu_id, R.id.window_two_menu_id,
167 R.id.window_three_menu_id, R.id.window_four_menu_id,
168 R.id.window_five_menu_id, R.id.window_six_menu_id,
169 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
170
171 // "source" parameter for Google search through search key
172 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
173 // "source" parameter for Google search through simplily type
174 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
175
George Mount387d45d2011-10-07 15:57:53 -0700176 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700177 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
178
John Reckd7dd9b22011-08-30 09:18:29 -0700179 // A bitmap that is re-used in createScreenshot as scratch space
180 private static Bitmap sThumbnailBitmap;
181
Michael Kolb8233fac2010-10-26 16:08:53 -0700182 private Activity mActivity;
183 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700184 private HomepageHandler mHomepageHandler;
185 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700186 private BrowserSettings mSettings;
187 private WebViewFactory mFactory;
188
189 private WakeLock mWakeLock;
190
191 private UrlHandler mUrlHandler;
192 private UploadHandler mUploadHandler;
193 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 private PageDialogsHandler mPageDialogsHandler;
195 private NetworkStateHandler mNetworkHandler;
196
Ben Murdoch8029a772010-11-16 11:58:21 +0000197 private Message mAutoFillSetupMessage;
198
Michael Kolb8233fac2010-10-26 16:08:53 -0700199 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800200 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700201
Michael Kolb8233fac2010-10-26 16:08:53 -0700202 // FIXME, temp address onPrepareMenu performance problem.
203 // When we move everything out of view, we should rewrite this.
204 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700205 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700206 private int mOldMenuState = EMPTY_MENU;
207 private Menu mCachedMenu;
208
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 private boolean mMenuIsDown;
210
Pankaj Garg49b79252014-11-07 17:33:41 -0800211 private boolean mWasInPageLoad = false;
212 private AppMenuHandler mAppMenuHandler;
213
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 // For select and find, we keep track of the ActionMode so that
215 // finish() can be called as desired.
216 private ActionMode mActionMode;
217
218 /**
219 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
220 * of whether the configuration has changed. The first onMenuOpened call
221 * after a configuration change is simply a reopening of the same menu
222 * (i.e. mIconView did not change).
223 */
224 private boolean mConfigChanged;
225
226 /**
227 * Keeps track of whether the options menu is open. This is important in
228 * determining whether to show or hide the title bar overlay
229 */
230 private boolean mOptionsMenuOpen;
231
232 /**
233 * Whether or not the options menu is in its bigger, popup menu form. When
234 * true, we want the title bar overlay to be gone. When false, we do not.
235 * Only meaningful if mOptionsMenuOpen is true.
236 */
237 private boolean mExtendedMenuOpen;
238
Michael Kolb8233fac2010-10-26 16:08:53 -0700239 private boolean mActivityPaused = true;
240 private boolean mLoadStopped;
241
242 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500243 // Checks to see when the bookmarks database has changed, and updates the
244 // Tabs' notion of whether they represent bookmarked sites.
245 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700246 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700247
Michael Kolbc3af0672011-08-09 10:24:41 -0700248 private boolean mBlockEvents;
249
Michael Kolb0b129122012-06-04 16:31:58 -0700250 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800251 private boolean mUpdateMyNavThumbnail;
252 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800253 private float mLevel = 0.0f;
Michael Kolb0b129122012-06-04 16:31:58 -0700254
George Mount3636d0a2011-11-21 09:08:21 -0800255 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700256 mActivity = browser;
257 mSettings = BrowserSettings.getInstance();
258 mTabControl = new TabControl(this);
259 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700260 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800261 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700262 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700263
264 mUrlHandler = new UrlHandler(this);
265 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700266 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
267
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500269 mBookmarksObserver = new ContentObserver(mHandler) {
270 @Override
271 public void onChange(boolean selfChange) {
272 int size = mTabControl.getTabCount();
273 for (int i = 0; i < size; i++) {
274 mTabControl.getTab(i).updateBookmarkedStatus();
275 }
276 }
277
278 };
279 browser.getContentResolver().registerContentObserver(
280 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700281
282 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700283 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800284 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700285 }
286
John Reck9c35b9c2012-05-30 10:08:50 -0700287 @Override
288 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700289 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800290 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800291 // mCrashRecoverHandler has any previously saved state.
292 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700293 }
294
George Mount3636d0a2011-11-21 09:08:21 -0800295 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800296 // we dont want to ever recover incognito tabs
297 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700298
Patrick Scott7d50a932011-02-04 09:27:26 -0500299 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700300 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500301 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000302
Michael Kolbc831b632011-05-11 09:30:34 -0700303 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500304 // Not able to restore so we go ahead and clear session cookies. We
305 // must do this before trying to login the user as we don't want to
306 // clear any session cookies set during login.
307 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700308 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800309 if (intent == null) {
310 // This won't happen under common scenarios. The icicle is
311 // not null, but there aren't any tabs to restore.
312 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700313 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800314 final Bundle extra = intent.getExtras();
315 // Create an initial tab.
316 // If the intent is ACTION_VIEW and data is not null, the Browser is
317 // invoked to view the content by another application. In this case,
318 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800319 UrlData urlData = null;
320 if (intent.getData() != null
321 && Intent.ACTION_VIEW.equals(intent.getAction())
322 && intent.getData().toString().startsWith("content://")) {
323 urlData = new UrlData(intent.getData().toString());
324 } else {
325 urlData = IntentHandler.getUrlDataFromIntent(intent);
326 }
George Mount3636d0a2011-11-21 09:08:21 -0800327 Tab t = null;
328 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700329 String landingPage = mActivity.getResources().getString(
330 R.string.def_landing_page);
331 if (!landingPage.isEmpty()) {
332 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800333 } else {
334 t = openTabToHomePage();
335 }
George Mount3636d0a2011-11-21 09:08:21 -0800336 } else {
337 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700338 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800339 }
340 if (t != null) {
341 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
342 }
343 WebView webView = t.getWebView();
344 if (extra != null) {
345 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
346 if (scale > 0 && scale <= 1000) {
347 webView.setInitialScale(scale);
348 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700349 }
350 }
John Reckd8c74522011-06-14 08:45:00 -0700351 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700353 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500354 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700355 List<Tab> tabs = mTabControl.getTabs();
356 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700357
John Reck1cf4b792011-07-26 10:22:22 -0700358 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700359 //handle restored pages that may require a JS interface
360 if (t.getWebView() != null) {
361 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
362 if (backForwardList != null) {
363 for (int i = 0; i < backForwardList.getSize(); i++) {
364 WebHistoryItem item = backForwardList.getItemAtIndex(i);
365 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
366 }
367 }
368 }
John Reck1cf4b792011-07-26 10:22:22 -0700369 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700370 if (t != mTabControl.getCurrentTab()) {
371 t.pause();
372 }
John Reck1cf4b792011-07-26 10:22:22 -0700373 }
374 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700375 if (tabs.size() == 0) {
376 openTabToHomePage();
377 }
John Reck1cf4b792011-07-26 10:22:22 -0700378 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 // TabControl.restoreState() will create a new tab even if
380 // restoring the state fails.
381 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800382 // Intent is non-null when framework thinks the browser should be
383 // launching with a new intent (icicle is null).
384 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700385 mIntentHandler.onNewIntent(intent);
386 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700387 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700389 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800390 if (jsFlags.trim().length() != 0) {
391 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700392 }
George Mount3636d0a2011-11-21 09:08:21 -0800393 if (intent != null
394 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700395 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800396 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 }
398
John Reck1cf4b792011-07-26 10:22:22 -0700399 private static class PruneThumbnails implements Runnable {
400 private Context mContext;
401 private List<Long> mIds;
402
403 PruneThumbnails(Context context, List<Long> preserveIds) {
404 mContext = context.getApplicationContext();
405 mIds = preserveIds;
406 }
407
408 @Override
409 public void run() {
410 ContentResolver cr = mContext.getContentResolver();
411 if (mIds == null || mIds.size() == 0) {
412 cr.delete(Thumbnails.CONTENT_URI, null, null);
413 } else {
414 int length = mIds.size();
415 StringBuilder where = new StringBuilder();
416 where.append(Thumbnails._ID);
417 where.append(" not in (");
418 for (int i = 0; i < length; i++) {
419 where.append(mIds.get(i));
420 if (i < (length - 1)) {
421 where.append(",");
422 }
423 }
424 where.append(")");
425 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
426 }
427 }
428
429 }
430
Michael Kolb1514bb72010-11-22 09:11:48 -0800431 @Override
432 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700433 return mFactory;
434 }
435
436 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800437 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800438 if (tab.hasCrashed)
439 tab.showCrashView();
440 else
441 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800442 }
443
444 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800445 public void createSubWindow(Tab tab) {
446 endActionMode();
447 WebView mainView = tab.getWebView();
448 WebView subView = mFactory.createWebView((mainView == null)
449 ? false
450 : mainView.isPrivateBrowsingEnabled());
451 mUi.createSubWindow(tab, subView);
452 }
453
454 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700455 public Context getContext() {
456 return mActivity;
457 }
458
459 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700460 public Activity getActivity() {
461 return mActivity;
462 }
463
464 void setUi(UI ui) {
465 mUi = ui;
466 }
467
Michael Kolbaf63dba2012-05-16 12:58:05 -0700468 @Override
469 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700470 return mSettings;
471 }
472
473 IntentHandler getIntentHandler() {
474 return mIntentHandler;
475 }
476
477 @Override
478 public UI getUi() {
479 return mUi;
480 }
481
482 int getMaxTabs() {
483 return mActivity.getResources().getInteger(R.integer.max_tabs);
484 }
485
486 @Override
487 public TabControl getTabControl() {
488 return mTabControl;
489 }
490
Michael Kolb1bf23132010-11-19 12:55:12 -0800491 @Override
492 public List<Tab> getTabs() {
493 return mTabControl.getTabs();
494 }
495
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 private void startHandler() {
497 mHandler = new Handler() {
498
499 @Override
500 public void handleMessage(Message msg) {
501 switch (msg.what) {
502 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700503 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 break;
505 case FOCUS_NODE_HREF:
506 {
507 String url = (String) msg.getData().get("url");
508 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500509 String src = (String) msg.getData().get("src");
510 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 if (TextUtils.isEmpty(url)) {
512 break;
513 }
514 HashMap focusNodeMap = (HashMap) msg.obj;
515 WebView view = (WebView) focusNodeMap.get("webview");
516 // Only apply the action if the top window did not change.
517 if (getCurrentTopWebView() != view) {
518 break;
519 }
520 switch (msg.arg1) {
521 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700522 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500524 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700525 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500526 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500527 case R.id.open_newtab_context_menu_id:
528 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700529 openTab(url, parent,
530 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500531 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700532 case R.id.copy_link_context_menu_id:
533 copy(url);
534 break;
535 case R.id.save_link_context_menu_id:
536 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500537 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800538 mActivity, url, view.getSettings().getUserAgentString(),
539 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700541 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700542 if(title == null || title == "")
543 title = url;
544
545 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
546 //SWE TODO: No thumbnail support for the url obtained via
547 //browser context menu as its not loaded in webview.
548 if (bookmarkIntent != null) {
549 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
550 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
551 mActivity.startActivity(bookmarkIntent);
552 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700553 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 }
555 break;
556 }
557
558 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700559 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 break;
561
562 case STOP_LOAD:
563 stopLoading();
564 break;
565
566 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700567 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700568 mWakeLock.release();
569 // if we reach here, Browser should be still in the
570 // background loading after WAKELOCK_TIMEOUT (5-min).
571 // To avoid burning the battery, stop loading.
572 mTabControl.stopAllLoading();
573 }
574 break;
575
576 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800577 Tab tab = (Tab) msg.obj;
578 if (tab != null) {
579 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 }
581 break;
kaiyiz591110b2013-08-06 17:11:06 +0800582 case OPEN_MENU:
583 if (!mOptionsMenuOpen && mActivity != null ) {
584 mActivity.openOptionsMenu();
585 }
586 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700587 }
588 }
589 };
590
591 }
592
John Reckef654f12011-07-12 16:42:08 -0700593 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200594 public Tab getCurrentTab() {
595 return mTabControl.getCurrentTab();
596 }
597
Michael Kolbba99c5d2010-11-29 14:57:41 -0800598 @Override
599 public void shareCurrentPage() {
600 shareCurrentPage(mTabControl.getCurrentTab());
601 }
602
603 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700604 if (tab == null || tab.getWebView() == null)
605 return;
606
607 final Tab mytab = tab;
608 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
609 @Override
610 public void onReceiveValue(Bitmap bitmap) {
611 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
612 mytab.getFavicon(), bitmap);
613 }
614 };
615
616 createScreenshotAsync(
617 tab.getWebView(),
618 getDesiredThumbnailWidth(mActivity),
619 getDesiredThumbnailHeight(mActivity),
620 new ValueCallback<Bitmap>() {
621 @Override
622 public void onReceiveValue(Bitmap bitmap) {
623 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
624 mytab.getFavicon(), bitmap);
625 }
626 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800627 }
628
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 /**
630 * Share a page, providing the title, url, favicon, and a screenshot. Uses
631 * an {@link Intent} to launch the Activity chooser.
632 * @param c Context used to launch a new Activity.
633 * @param title Title of the page. Stored in the Intent with
634 * {@link Intent#EXTRA_SUBJECT}
635 * @param url URL of the page. Stored in the Intent with
636 * {@link Intent#EXTRA_TEXT}
637 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
638 * with {@link Browser#EXTRA_SHARE_FAVICON}
639 * @param screenshot Bitmap of a screenshot of the page. Stored in the
640 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
641 */
642 static final void sharePage(Context c, String title, String url,
643 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700644
645 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
646 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
647 R.layout.app_row, sDialog.getApps());
648 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700649 }
650
651 private void copy(CharSequence text) {
652 ClipboardManager cm = (ClipboardManager) mActivity
653 .getSystemService(Context.CLIPBOARD_SERVICE);
654 cm.setText(text);
655 }
656
657 // lifecycle
658
John Reck9c35b9c2012-05-30 10:08:50 -0700659 @Override
660 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800662 // update the menu in case of a locale change
663 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800664 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800665 if (mOptionsMenuOpen) {
666 mActivity.closeOptionsMenu();
667 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
668 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 if (mPageDialogsHandler != null) {
670 mPageDialogsHandler.onConfigurationChanged(config);
671 }
672 mUi.onConfigurationChanged(config);
673 }
674
675 @Override
676 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700677 if (!mUi.isWebShowing()) {
678 mUi.showWeb(false);
679 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700680 mIntentHandler.onNewIntent(intent);
681 }
682
John Reck9c35b9c2012-05-30 10:08:50 -0700683 @Override
684 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800685 if (mUi.isCustomViewShowing()) {
686 hideCustomView();
687 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 if (mActivityPaused) {
689 Log.e(LOGTAG, "BrowserActivity is already paused.");
690 return;
691 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800693 Tab tab = mTabControl.getCurrentTab();
694 if (tab != null) {
695 tab.pause();
696 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700697 if (mWakeLock == null) {
698 PowerManager pm = (PowerManager) mActivity
699 .getSystemService(Context.POWER_SERVICE);
700 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
701 }
Michael Kolb70976932010-11-30 11:34:01 -0800702 mWakeLock.acquire();
703 mHandler.sendMessageDelayed(mHandler
704 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
705 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 }
707 mUi.onPause();
708 mNetworkHandler.onPause();
709
710 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100711 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 }
713
John Reck9c35b9c2012-05-30 10:08:50 -0700714 @Override
715 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700716 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800717 Bundle saveState = createSaveState();
718
719 // crash recovery manages all save & restore state
720 mCrashRecoveryHandler.writeState(saveState);
721 mSettings.setLastRunPaused(true);
722 }
723
724 /**
725 * Save the current state to outState. Does not write the state to
726 * disk.
727 * @return Bundle containing the current state of all tabs.
728 */
729 /* package */ Bundle createSaveState() {
730 Bundle saveState = new Bundle();
731 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800732 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 }
734
John Reck9c35b9c2012-05-30 10:08:50 -0700735 @Override
736 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 if (!mActivityPaused) {
738 Log.e(LOGTAG, "BrowserActivity is already resumed.");
739 return;
740 }
George Mount3636d0a2011-11-21 09:08:21 -0800741 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800743 Tab current = mTabControl.getCurrentTab();
744 if (current != null) {
745 current.resume();
746 resumeWebViewTimers(current);
747 }
John Reckf57c0292011-07-21 18:15:39 -0700748 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200749
Michael Kolb8233fac2010-10-26 16:08:53 -0700750 mUi.onResume();
751 mNetworkHandler.onResume();
752 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100753 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700754 if (mVoiceResult != null) {
755 mUi.onVoiceResult(mVoiceResult);
756 mVoiceResult = null;
757 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800758 if (current != null && current.hasCrashed) {
Vivek Sekhar2868b8d2014-12-03 17:22:50 -0800759 current.replaceCrashView(current.getWebView(), current.getViewContainer());
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800760 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700761 }
762
John Reckf57c0292011-07-21 18:15:39 -0700763 private void releaseWakeLock() {
764 if (mWakeLock != null && mWakeLock.isHeld()) {
765 mHandler.removeMessages(RELEASE_WAKELOCK);
766 mWakeLock.release();
767 }
768 }
769
Michael Kolb70976932010-11-30 11:34:01 -0800770 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800771 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800772 * @param tab guaranteed non-null
773 */
774 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700775 boolean inLoad = tab.inPageLoad();
776 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
777 CookieSyncManager.getInstance().startSync();
778 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100779 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700780 }
781 }
782
Michael Kolb70976932010-11-30 11:34:01 -0800783 /**
784 * Pause all WebView timers using the WebView of the given tab
785 * @param tab
786 * @return true if the timers are paused or tab is null
787 */
788 private boolean pauseWebViewTimers(Tab tab) {
789 if (tab == null) {
790 return true;
791 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100793 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 }
Michael Kolb70976932010-11-30 11:34:01 -0800796 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700797 }
798
John Reck9c35b9c2012-05-30 10:08:50 -0700799 @Override
800 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800801 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700802 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
803 mUploadHandler = null;
804 }
805 if (mTabControl == null) return;
806 mUi.onDestroy();
807 // Remove the current tab and sub window
808 Tab t = mTabControl.getCurrentTab();
809 if (t != null) {
810 dismissSubWindow(t);
811 removeTab(t);
812 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500813 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 // Destroy all the tabs
815 mTabControl.destroy();
Michael Kolb8233fac2010-10-26 16:08:53 -0700816 }
817
818 protected boolean isActivityPaused() {
819 return mActivityPaused;
820 }
821
John Reck9c35b9c2012-05-30 10:08:50 -0700822 @Override
823 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700824 mTabControl.freeMemory();
825 }
826
827 @Override
828 public boolean shouldShowErrorConsole() {
829 return mShouldShowErrorConsole;
830 }
831
832 protected void setShouldShowErrorConsole(boolean show) {
833 if (show == mShouldShowErrorConsole) {
834 // Nothing to do.
835 return;
836 }
837 mShouldShowErrorConsole = show;
838 Tab t = mTabControl.getCurrentTab();
839 if (t == null) {
840 // There is no current tab so we cannot toggle the error console
841 return;
842 }
843 mUi.setShouldShowErrorConsole(t, show);
844 }
845
846 @Override
847 public void stopLoading() {
848 mLoadStopped = true;
849 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700851 if (w != null) {
852 w.stopLoading();
853 mUi.onPageStopped(tab);
854 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 }
856
857 boolean didUserStopLoading() {
858 return mLoadStopped;
859 }
860
kaiyiza016da12013-08-26 17:50:22 +0800861 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700862 final String reminderType = getContext().getResources().getString(
863 R.string.def_wifi_browser_interaction_remind_type);
864 final String selectionConnnection = getContext().getResources().getString(
865 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700866 final String wifiSelection = getContext().getResources().getString(
867 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700868
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700869 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
870 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700871 return;
872
kaiyiza016da12013-08-26 17:50:22 +0800873 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700874 Context.CONNECTIVITY_SERVICE);
875 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700876 WifiManager wifiMgr = (WifiManager) this.getContext()
877 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700878 if (networkInfo == null
879 || (networkInfo != null && (networkInfo.getType() !=
880 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700881 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
882 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700883 List<ScanResult> list = wifiMgr.getScanResults();
884 // Have no AP's for Wifi's fall back to data
885 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700886 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700887 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
888 this.getContext().startActivity(intent);
889 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700890 // Request to select Wifi AP
891 try {
892 Intent intent = new Intent(wifiSelection);
893 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
894 this.getContext().startActivity(intent);
895 } catch (Exception e) {
896 String err_msg = this.getContext().getString(
897 R.string.acivity_not_found, wifiSelection);
898 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
899 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700900 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700901 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800902 }
903 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700904
Michael Kolb8233fac2010-10-26 16:08:53 -0700905 // WebViewController
906
907 @Override
John Reck324d4402011-01-11 16:56:42 -0800908 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700909
910 // We've started to load a new page. If there was a pending message
911 // to save a screenshot then we will now take the new page and save
912 // an incorrect screenshot. Therefore, remove any pending thumbnail
913 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700914 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700915
916 // reset sync timer to avoid sync starts during loading a page
917 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700918 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700919 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800920 boolean networkNotifier = BrowserConfig.getInstance(getContext())
921 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700922 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700923 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800924 } else {
925 if (!mNetworkHandler.isNetworkUp()) {
926 view.setNetworkAvailable(false);
927 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 }
929
930 // when BrowserActivity just starts, onPageStarted may be called before
931 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
932 // to start the timer. As we won't switch tabs while an activity is in
933 // pause state, we can ensure calling resume and pause in pair.
934 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800935 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 }
937 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 endActionMode();
939
John Reck30c714c2010-12-16 17:30:34 -0800940 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700941
John Reck324d4402011-01-11 16:56:42 -0800942 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 // update the bookmark database for favicon
944 maybeUpdateFavicon(tab, null, url, favicon);
945
946 Performance.tracePageStart(url);
947
948 // Performance probe
949 if (false) {
950 Performance.onPageStarted();
951 }
952
953 }
954
955 @Override
John Reck324d4402011-01-11 16:56:42 -0800956 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700957 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800958 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200959
Michael Kolb8233fac2010-10-26 16:08:53 -0700960 // Performance probe
961 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800962 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700963 }
964
Tarun Nainani8eb00912014-07-17 12:28:32 -0700965 tab.onPageFinished();
966
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 Performance.tracePageFinished();
968 }
969
970 @Override
John Reck30c714c2010-12-16 17:30:34 -0800971 public void onProgressChanged(Tab tab) {
972 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700973
974 if (newProgress == 100) {
975 CookieSyncManager.getInstance().sync();
976 // onProgressChanged() may continue to be called after the main
977 // frame has finished loading, as any remaining sub frames continue
978 // to load. We'll only get called once though with newProgress as
979 // 100 when everything is loaded. (onPageFinished is called once
980 // when the main frame completes loading regardless of the state of
981 // any sub frames so calls to onProgressChanges may continue after
982 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800983 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -0800984 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800985 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -0800986 } else if (mWasInPageLoad) {
987 mWasInPageLoad = false;
988 updateInLoadMenuItems(mCachedMenu, tab);
989 }
990
991 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +0200992 // pause the WebView timer and release the wake lock if it is
993 // finished while BrowserActivity is in pause state.
994 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700995 }
John Reckd9862372012-02-21 15:04:50 -0800996 if (!tab.isPrivateBrowsingEnabled()
997 && !TextUtils.isEmpty(tab.getUrl())
998 && !tab.isSnapshot()) {
999 // Only update the bookmark screenshot if the user did not
1000 // cancel the load early and there is not already
1001 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001002 if (tab.shouldUpdateThumbnail() &&
1003 (tab.inForeground() && !didUserStopLoading()
1004 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001005 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1006 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1007 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001008 1500);
John Reckd9862372012-02-21 15:04:50 -08001009 }
1010 }
1011 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001012 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001013 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001014 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001015 // still loading
1016 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001017 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001018 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001019 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001020 } else {
1021 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001022 }
1023 }
John Reck30c714c2010-12-16 17:30:34 -08001024 mUi.onProgressChanged(tab);
1025 }
1026
1027 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001028 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001029 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001030 }
1031
1032 @Override
1033 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001034 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001035 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001036 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1038 return;
1039 }
1040 // Update the title in the history database if not in private browsing mode
1041 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001042 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 }
1044 }
1045
1046 @Override
1047 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001048 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001049 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1050 }
1051
1052 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001053 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1054 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 }
1056
1057 @Override
1058 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1059 if (mMenuIsDown) {
1060 // only check shortcut key when MENU is held
1061 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1062 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001064 int keyCode = event.getKeyCode();
1065 // We need to send almost every key to WebKit. However:
1066 // 1. We don't want to block the device on the renderer for
1067 // some keys like menu, home, call.
1068 // 2. There are no WebKit equivalents for some of these keys
1069 // (see app/keyboard_codes_win.h)
1070 // Note that these are not the same set as KeyEvent.isSystemKey:
1071 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1072 if (keyCode == KeyEvent.KEYCODE_MENU ||
1073 keyCode == KeyEvent.KEYCODE_HOME ||
1074 keyCode == KeyEvent.KEYCODE_BACK ||
1075 keyCode == KeyEvent.KEYCODE_CALL ||
1076 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1077 keyCode == KeyEvent.KEYCODE_POWER ||
1078 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1079 keyCode == KeyEvent.KEYCODE_CAMERA ||
1080 keyCode == KeyEvent.KEYCODE_FOCUS ||
1081 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1082 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1083 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1084 return true;
1085 }
1086
1087 // We also have to intercept some shortcuts before we send them to the ContentView.
1088 if (event.isCtrlPressed() && (
1089 keyCode == KeyEvent.KEYCODE_TAB ||
1090 keyCode == KeyEvent.KEYCODE_W ||
1091 keyCode == KeyEvent.KEYCODE_F4)) {
1092 return true;
1093 }
1094
1095 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 }
1097
1098 @Override
John Reck997b1b72012-04-19 18:08:25 -07001099 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001100 if (!isActivityPaused()) {
1101 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001102 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 } else {
John Reck997b1b72012-04-19 18:08:25 -07001104 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 }
1106 }
John Reck997b1b72012-04-19 18:08:25 -07001107 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001108 }
1109
1110 @Override
John Reck324d4402011-01-11 16:56:42 -08001111 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001112 // Don't save anything in private browsing mode or when disabling history
1113 // for regular tabs is enabled
1114 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1115 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1116 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001117
John Reck49a603c2011-03-03 09:33:05 -08001118 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001119
John Reck324d4402011-01-11 16:56:42 -08001120 if (TextUtils.isEmpty(url)
1121 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 return;
1123 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001124
John Reckf57c0292011-07-21 18:15:39 -07001125 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001126 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001127 }
1128
1129 @Override
1130 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1131 AsyncTask<Void, Void, String[]> task =
1132 new AsyncTask<Void, Void, String[]>() {
1133 @Override
1134 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001135 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 }
1137 @Override
1138 public void onPostExecute(String[] result) {
1139 callback.onReceiveValue(result);
1140 }
1141 };
1142 task.execute();
1143 }
1144
1145 @Override
1146 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1147 final HttpAuthHandler handler, final String host,
1148 final String realm) {
1149 String username = null;
1150 String password = null;
1151
1152 boolean reuseHttpAuthUsernamePassword
1153 = handler.useHttpAuthUsernamePassword();
1154
1155 if (reuseHttpAuthUsernamePassword && view != null) {
1156 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1157 if (credentials != null && credentials.length == 2) {
1158 username = credentials[0];
1159 password = credentials[1];
1160 }
1161 }
1162
1163 if (username != null && password != null) {
1164 handler.proceed(username, password);
1165 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001166 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001167 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1168 } else {
1169 handler.cancel();
1170 }
1171 }
1172 }
1173
1174 @Override
1175 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001176 String contentDisposition, String mimetype, String referer,
1177 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001178 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001179 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001180 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001181 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001182 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001183 // This Tab was opened for the sole purpose of downloading a
1184 // file. Remove it.
1185 if (tab == mTabControl.getCurrentTab()) {
1186 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001187 if (tab.getDerivedFromIntent())
1188 closeTab(tab);
1189 else
1190 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001191 } else {
1192 // In this case, it is not.
1193 closeTab(tab);
1194 }
1195 }
1196 }
1197
1198 @Override
1199 public Bitmap getDefaultVideoPoster() {
1200 return mUi.getDefaultVideoPoster();
1201 }
1202
1203 @Override
1204 public View getVideoLoadingProgressView() {
1205 return mUi.getVideoLoadingProgressView();
1206 }
1207
1208 @Override
1209 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1210 SslError error) {
1211 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1212 }
1213
1214 // helper method
1215
1216 /*
1217 * Update the favorites icon if the private browsing isn't enabled and the
1218 * icon is valid.
1219 */
1220 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1221 final String url, Bitmap favicon) {
1222 if (favicon == null) {
1223 return;
1224 }
1225 if (!tab.isPrivateBrowsingEnabled()) {
1226 Bookmarks.updateFavicon(mActivity
1227 .getContentResolver(), originalUrl, url, favicon);
1228 }
1229 }
1230
Leon Scroggins4cd97792010-12-03 15:31:56 -05001231 @Override
1232 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001233 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001234 mUi.bookmarkedStatusHasChanged(tab);
1235 }
1236
Michael Kolb8233fac2010-10-26 16:08:53 -07001237 // end WebViewController
1238
1239 protected void pageUp() {
1240 getCurrentTopWebView().pageUp(false);
1241 }
1242
1243 protected void pageDown() {
1244 getCurrentTopWebView().pageDown(false);
1245 }
1246
1247 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001248 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001249 public void editUrl() {
1250 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001251 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001252 }
1253
John Reck9c35b9c2012-05-30 10:08:50 -07001254 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001255 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001256 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001257 if (tab.inForeground()) {
1258 if (mUi.isCustomViewShowing()) {
1259 callback.onCustomViewHidden();
1260 return;
1261 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001262 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001263 // Save the menu state and set it to empty while the custom
1264 // view is showing.
1265 mOldMenuState = mMenuState;
1266 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001267 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 }
1269 }
1270
1271 @Override
1272 public void hideCustomView() {
1273 if (mUi.isCustomViewShowing()) {
1274 mUi.onHideCustomView();
1275 // Reset the old menu state.
1276 mMenuState = mOldMenuState;
1277 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001278 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 }
1280 }
1281
John Reck9c35b9c2012-05-30 10:08:50 -07001282 @Override
1283 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001284 Intent intent) {
1285 if (getCurrentTopWebView() == null) return;
1286 switch (requestCode) {
1287 case PREFERENCES_PAGE:
1288 if (resultCode == Activity.RESULT_OK && intent != null) {
1289 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001290 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 mTabControl.removeParentChildRelationShips();
1292 }
1293 }
1294 break;
1295 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001296 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001297 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001298 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001299 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001300 case AUTOFILL_SETUP:
1301 // Determine whether a profile was actually set up or not
1302 // and if so, send the message back to the WebTextView to
1303 // fill the form with the new profile.
1304 if (getSettings().getAutoFillProfile() != null) {
1305 mAutoFillSetupMessage.sendToTarget();
1306 mAutoFillSetupMessage = null;
1307 }
1308 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001309 case COMBO_VIEW:
1310 if (intent == null || resultCode != Activity.RESULT_OK) {
1311 break;
1312 }
John Reck3ba45532011-08-11 16:26:53 -07001313 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001314 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1315 Tab t = getCurrentTab();
1316 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001317 mUpdateMyNavThumbnail = true;
1318 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001319 loadUrl(t, uri.toString());
1320 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1321 String[] urls = intent.getStringArrayExtra(
1322 ComboViewActivity.EXTRA_OPEN_ALL);
1323 Tab parent = getCurrentTab();
1324 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001325 if (url != null) {
1326 parent = openTab(url, parent,
1327 !mSettings.openInBackground(), true);
1328 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001329 }
1330 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1331 long id = intent.getLongExtra(
1332 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1333 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001334 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001335 }
1336 }
1337 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001338 case VOICE_RESULT:
1339 if (resultCode == Activity.RESULT_OK && intent != null) {
1340 ArrayList<String> results = intent.getStringArrayListExtra(
1341 RecognizerIntent.EXTRA_RESULTS);
1342 if (results.size() >= 1) {
1343 mVoiceResult = results.get(0);
1344 }
1345 }
1346 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001347 case MY_NAVIGATION:
1348 if (intent == null || resultCode != Activity.RESULT_OK) {
1349 break;
1350 }
1351
1352 if (intent.getBooleanExtra("need_refresh", false) &&
1353 getCurrentTopWebView() != null) {
1354 getCurrentTopWebView().reload();
1355 }
1356 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001357 default:
1358 break;
1359 }
1360 getCurrentTopWebView().requestFocus();
1361 }
1362
1363 /**
1364 * Open the Go page.
1365 * @param startWithHistory If true, open starting on the history tab.
1366 * Otherwise, start with the bookmarks tab.
1367 */
1368 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001369 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001370 if (mTabControl.getCurrentWebView() == null) {
1371 return;
1372 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001373 // clear action mode
1374 if (isInCustomActionMode()) {
1375 endActionMode();
1376 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001377 Bundle extras = new Bundle();
1378 // Disable opening in a new window if we have maxed out the windows
1379 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1380 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001381 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001382 }
1383
1384 // combo view callbacks
1385
Michael Kolb8233fac2010-10-26 16:08:53 -07001386 // key handling
1387 protected void onBackKey() {
1388 if (!mUi.onBackKey()) {
1389 WebView subwindow = mTabControl.getCurrentSubWindow();
1390 if (subwindow != null) {
1391 if (subwindow.canGoBack()) {
1392 subwindow.goBack();
1393 } else {
1394 dismissSubWindow(mTabControl.getCurrentTab());
1395 }
1396 } else {
1397 goBackOnePageOrQuit();
1398 }
1399 }
1400 }
1401
Michael Kolb4bd767d2011-05-27 11:33:55 -07001402 protected boolean onMenuKey() {
1403 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001404 }
1405
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 // menu handling and state
1407 // TODO: maybe put into separate handler
1408
John Reck9c35b9c2012-05-30 10:08:50 -07001409 @Override
1410 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001411 if (mMenuState == EMPTY_MENU) {
1412 return false;
1413 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 MenuInflater inflater = mActivity.getMenuInflater();
1415 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 return true;
1417 }
1418
John Reck9c35b9c2012-05-30 10:08:50 -07001419 @Override
1420 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001421 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001422 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 return;
1424 }
1425 if (!(v instanceof WebView)) {
1426 return;
1427 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001428 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001429 WebView.HitTestResult result = webview.getHitTestResult();
1430 if (result == null) {
1431 return;
1432 }
1433
1434 int type = result.getType();
1435 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1436 Log.w(LOGTAG,
1437 "We should not show context menu when nothing is touched");
1438 return;
1439 }
1440 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1441 // let TextView handles context menu
1442 return;
1443 }
1444
1445 // Note, http://b/issue?id=1106666 is requesting that
1446 // an inflated menu can be used again. This is not available
1447 // yet, so inflate each time (yuk!)
1448 MenuInflater inflater = mActivity.getMenuInflater();
1449 inflater.inflate(R.menu.browsercontext, menu);
1450
1451 // Show the correct menu group
1452 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001453 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001454 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001455 menu.setGroupVisible(R.id.PHONE_MENU,
1456 type == WebView.HitTestResult.PHONE_TYPE);
1457 menu.setGroupVisible(R.id.EMAIL_MENU,
1458 type == WebView.HitTestResult.EMAIL_TYPE);
1459 menu.setGroupVisible(R.id.GEO_MENU,
1460 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001461 String itemUrl = null;
1462 String url = webview.getOriginalUrl();
1463 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1464 itemUrl = Uri.decode(navigationUrl);
1465 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1466 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1467 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1468 } else {
1469 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1470 }
1471 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1472 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1473 } else {
1474 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1475
1476 menu.setGroupVisible(R.id.IMAGE_MENU,
1477 type == WebView.HitTestResult.IMAGE_TYPE
1478 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1479 menu.setGroupVisible(R.id.ANCHOR_MENU,
1480 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1481 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001482 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1483 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001484 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001485 // Setup custom handling depending on the type
1486 switch (type) {
1487 case WebView.HitTestResult.PHONE_TYPE:
1488 menu.setHeaderTitle(Uri.decode(extra));
1489 menu.findItem(R.id.dial_context_menu_id).setIntent(
1490 new Intent(Intent.ACTION_VIEW, Uri
1491 .parse(WebView.SCHEME_TEL + extra)));
1492 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1493 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1494 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1495 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1496 addIntent);
1497 menu.findItem(R.id.copy_phone_context_menu_id)
1498 .setOnMenuItemClickListener(
1499 new Copy(extra));
1500 break;
1501
1502 case WebView.HitTestResult.EMAIL_TYPE:
1503 menu.setHeaderTitle(extra);
1504 menu.findItem(R.id.email_context_menu_id).setIntent(
1505 new Intent(Intent.ACTION_VIEW, Uri
1506 .parse(WebView.SCHEME_MAILTO + extra)));
1507 menu.findItem(R.id.copy_mail_context_menu_id)
1508 .setOnMenuItemClickListener(
1509 new Copy(extra));
1510 break;
1511
1512 case WebView.HitTestResult.GEO_TYPE:
1513 menu.setHeaderTitle(extra);
1514 menu.findItem(R.id.map_context_menu_id).setIntent(
1515 new Intent(Intent.ACTION_VIEW, Uri
1516 .parse(WebView.SCHEME_GEO
1517 + URLEncoder.encode(extra))));
1518 menu.findItem(R.id.copy_geo_context_menu_id)
1519 .setOnMenuItemClickListener(
1520 new Copy(extra));
1521 break;
1522
1523 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1524 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001525 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001526 // decide whether to show the open link in new tab option
1527 boolean showNewTab = mTabControl.canCreateNewTab();
1528 MenuItem newTabItem
1529 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001530 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001531 ? R.string.contextmenu_openlink_newwindow_background
1532 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001533 newTabItem.setVisible(showNewTab);
1534 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001535 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1536 newTabItem.setOnMenuItemClickListener(
1537 new MenuItem.OnMenuItemClickListener() {
1538 @Override
1539 public boolean onMenuItemClick(MenuItem item) {
1540 final HashMap<String, WebView> hrefMap =
1541 new HashMap<String, WebView>();
1542 hrefMap.put("webview", webview);
1543 final Message msg = mHandler.obtainMessage(
1544 FOCUS_NODE_HREF,
1545 R.id.open_newtab_context_menu_id,
1546 0, hrefMap);
1547 webview.requestFocusNodeHref(msg);
1548 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001549 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001550 });
1551 } else {
1552 newTabItem.setOnMenuItemClickListener(
1553 new MenuItem.OnMenuItemClickListener() {
1554 @Override
1555 public boolean onMenuItemClick(MenuItem item) {
1556 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001557 openTab(extra, parent,
1558 !mSettings.openInBackground(),
1559 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001560 return true;
1561 }
1562 });
1563 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001564 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001565 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1566 menu.setHeaderTitle(navigationUrl);
1567 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1568
1569 if (itemUrl != null) {
1570 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1571 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1572 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1573 @Override
1574 public boolean onMenuItemClick(MenuItem item) {
1575 final Intent intent = new Intent(Controller.this
1576 .getContext(),
1577 AddMyNavigationPage.class);
1578 Bundle bundle = new Bundle();
1579 String url = Uri.decode(navigationUrl);
1580 bundle.putBoolean("isAdding", false);
1581 bundle.putString("url", url);
1582 bundle.putString("name", getNameFromUrl(url));
1583 intent.putExtra("websites", bundle);
1584 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1585 return false;
1586 }
1587 });
1588 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1589 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1590 @Override
1591 public boolean onMenuItemClick(MenuItem item) {
1592 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1593 return false;
1594 }
1595 });
1596 }
1597 } else {
1598 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1599 }
1600 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1602 break;
1603 }
1604 // otherwise fall through to handle image part
1605 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001606 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1607 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1609 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001610 shareItem.setOnMenuItemClickListener(
1611 new MenuItem.OnMenuItemClickListener() {
1612 @Override
1613 public boolean onMenuItemClick(MenuItem item) {
1614 sharePage(mActivity, null, extra, null,
1615 null);
1616 return true;
1617 }
1618 }
1619 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001620 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001621 menu.findItem(R.id.view_image_context_menu_id)
1622 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1623 @Override
1624 public boolean onMenuItemClick(MenuItem item) {
1625 openTab(extra, mTabControl.getCurrentTab(), true, true);
1626 return false;
1627 }
1628 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001629 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1630 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1631 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001632 menu.findItem(R.id.set_wallpaper_context_menu_id).
1633 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1634 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001635 break;
1636
1637 default:
1638 Log.w(LOGTAG, "We should not get here.");
1639 break;
1640 }
1641 //update the ui
1642 mUi.onContextMenuCreated(menu);
1643 }
1644
kaiyiz6e5b3e02013-08-19 20:02:01 +08001645 public void startAddMyNavigation(String url) {
1646 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1647 Bundle bundle = new Bundle();
1648 bundle.putBoolean("isAdding", true);
1649 bundle.putString("url", url);
1650 bundle.putString("name", getNameFromUrl(url));
1651 intent.putExtra("websites", bundle);
1652 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1653 }
1654
1655 private void showMyNavigationDeleteDialog(final String itemUrl) {
1656 new AlertDialog.Builder(this.getContext())
1657 .setTitle(R.string.my_navigation_delete_label)
1658 .setIcon(android.R.drawable.ic_dialog_alert)
1659 .setMessage(R.string.my_navigation_delete_msg)
1660 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1661 @Override
1662 public void onClick(DialogInterface dialog, int whichButton) {
1663 deleteMyNavigationItem(itemUrl);
1664 }
1665 })
1666 .setNegativeButton(R.string.cancel, null)
1667 .show();
1668 }
1669
1670 private void deleteMyNavigationItem(final String itemUrl) {
1671 ContentResolver cr = this.getContext().getContentResolver();
1672 Cursor cursor = null;
1673
1674 try {
1675 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1676 new String[] {
1677 MyNavigationUtil.ID
1678 }, "url = ?", new String[] {
1679 itemUrl
1680 }, null);
1681 if (null != cursor && cursor.moveToFirst()) {
1682 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1683 cursor.getLong(0));
1684
1685 ContentValues values = new ContentValues();
1686 values.put(MyNavigationUtil.TITLE, "");
1687 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1688 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1689 ByteArrayOutputStream os = new ByteArrayOutputStream();
1690 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1691 R.raw.my_navigation_add);
1692 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1693 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1694 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1695 cr.update(uri, values, null, null);
1696 } else {
1697 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1698 }
1699 } catch (IllegalStateException e) {
1700 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1701 } finally {
1702 if (null != cursor) {
1703 cursor.close();
1704 }
1705 }
1706
1707 if (getCurrentTopWebView() != null) {
1708 getCurrentTopWebView().reload();
1709 }
1710 }
1711
1712 private String getNameFromUrl(String itemUrl) {
1713 ContentResolver cr = this.getContext().getContentResolver();
1714 Cursor cursor = null;
1715 String name = null;
1716
1717 try {
1718 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1719 new String[] {
1720 MyNavigationUtil.TITLE
1721 }, "url = ?", new String[] {
1722 itemUrl
1723 }, null);
1724 if (null != cursor && cursor.moveToFirst()) {
1725 name = cursor.getString(0);
1726 } else {
1727 Log.e(LOGTAG, "this item does not exist!");
1728 }
1729 } catch (IllegalStateException e) {
1730 Log.e(LOGTAG, "getNameFromUrl", e);
1731 } finally {
1732 if (null != cursor) {
1733 cursor.close();
1734 }
1735 }
1736 return name;
1737 }
1738
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001739 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001740 final ContentResolver cr = mActivity.getContentResolver();
1741 new AsyncTask<Void, Void, Void>() {
1742 @Override
1743 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001744 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1745 if(!isMyNavigationUrl)
1746 return null;
1747
kaiyiz6e5b3e02013-08-19 20:02:01 +08001748 ContentResolver cr = mActivity.getContentResolver();
1749 Cursor cursor = null;
1750 try {
1751 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1752 new String[] {
1753 MyNavigationUtil.ID
1754 }, "url = ?", new String[] {
1755 itemUrl
1756 }, null);
1757 if (null != cursor && cursor.moveToFirst()) {
1758 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001759 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001760
1761 ContentValues values = new ContentValues();
1762 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1763 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1764 cursor.getLong(0));
1765 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1766 cr.update(uri, values, null, null);
1767 os.close();
1768 }
1769 } catch (IllegalStateException e) {
1770 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1771 } catch (IOException e) {
1772 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1773 } finally {
1774 if (null != cursor) {
1775 cursor.close();
1776 }
1777 }
1778 return null;
1779 }
1780 }.execute();
1781 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001782 /**
1783 * As the menu can be open when loading state changes
1784 * we must manually update the state of the stop/reload menu
1785 * item
1786 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001787 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001788 if (menu == null) {
1789 return;
1790 }
1791 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001792 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001793 menu.findItem(R.id.stop_menu_id):
1794 menu.findItem(R.id.reload_menu_id);
1795 if (src != null) {
1796 dest.setIcon(src.getIcon());
1797 dest.setTitle(src.getTitle());
1798 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001799 mActivity.invalidateOptionsMenu();
1800 }
1801
1802 public void invalidateOptionsMenu() {
1803 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001804 }
1805
John Reck9c35b9c2012-05-30 10:08:50 -07001806 @Override
1807 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001808 // Software menu key (toolbar key)
1809 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1810 return true;
1811 }
1812
1813 @Override
1814 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001815 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001816 // hold on to the menu reference here; it is used by the page callbacks
1817 // to update the menu based on loading state
1818 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001819 // Note: setVisible will decide whether an item is visible; while
1820 // setEnabled() will decide whether an item is enabled, which also means
1821 // whether the matching shortcut key will function.
1822 switch (mMenuState) {
1823 case EMPTY_MENU:
1824 if (mCurrentMenuState != mMenuState) {
1825 menu.setGroupVisible(R.id.MAIN_MENU, false);
1826 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1827 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1828 }
1829 break;
1830 default:
1831 if (mCurrentMenuState != mMenuState) {
1832 menu.setGroupVisible(R.id.MAIN_MENU, true);
1833 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1834 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1835 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001836 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001837 break;
1838 }
1839 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001840 mUi.onPrepareOptionsMenu(menu);
1841 }
1842
1843 private void setMenuItemVisibility(Menu menu, int id,
1844 boolean visibility) {
1845 MenuItem item = menu.findItem(id);
1846 if (item != null) {
1847 item.setVisible(visibility);
1848 }
1849 }
1850
1851 private int lookupBookmark(String title, String url) {
1852 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001853 int count = 0;
1854 Cursor cursor = null;
1855 try {
1856 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1857 BookmarksLoader.PROJECTION,
1858 "title = ? OR url = ?",
1859 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001860 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001861 },
1862 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001863
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001864 if (cursor != null)
1865 count = cursor.getCount();
1866
1867 } catch (IllegalStateException e) {
1868 Log.e(LOGTAG, "lookupBookmark ", e);
1869 } finally {
1870 if (null != cursor) {
1871 cursor.close();
1872 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001873 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001874 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001875 }
1876
1877 private void resetMenuItems(Menu menu) {
1878 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1879 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1880
1881 WebView w = getCurrentTopWebView();
1882 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1883
1884 String title = w.getTitle();
1885 String url = w.getUrl();
1886 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1887 bookmark_icon.setChecked(true);
1888 } else {
1889 bookmark_icon.setChecked(false);
1890 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001891 }
1892
Michael Kolb4bf79712011-07-14 14:18:12 -07001893 @Override
1894 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001895 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001896 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001897 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001898 // Following flag is used to identify schemes for which the LIVE_MENU
1899 // items defined in res/menu/browser.xml should be enabled
1900 boolean isLiveScheme = false;
1901 boolean isPageFinished = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001902
1903 resetMenuItems(menu);
1904
Michael Kolb4bf79712011-07-14 14:18:12 -07001905 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001906 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001907 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001908 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001909 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001910 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Michael Kolb4bf79712011-07-14 14:18:12 -07001911 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001912
1913 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1914 forward.setEnabled(canGoForward);
1915
Michael Kolb4bf79712011-07-14 14:18:12 -07001916 // decide whether to show the share link option
1917 PackageManager pm = mActivity.getPackageManager();
1918 Intent send = new Intent(Intent.ACTION_SEND);
1919 send.setType("text/plain");
1920 ResolveInfo ri = pm.resolveActivity(send,
1921 PackageManager.MATCH_DEFAULT_ONLY);
1922 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1923
1924 boolean isNavDump = mSettings.enableNavDump();
1925 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1926 nav.setVisible(isNavDump);
1927 nav.setEnabled(isNavDump);
1928
1929 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001930 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1931 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001932 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08001933 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
1934 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001935 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08001936 setMenuItemVisibility(menu, R.id.add_to_homescreen,
1937 isLive && isLiveScheme && isPageFinished);
1938 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
1939 isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001940 // history and snapshots item are the members of COMBO menu group,
1941 // so if show history item, only make snapshots item invisible.
1942 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001943
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001944 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001945 }
1946
John Reck9c35b9c2012-05-30 10:08:50 -07001947 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001948 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001949 if (null == getCurrentTopWebView()) {
1950 return false;
1951 }
1952 if (mMenuIsDown) {
1953 // The shortcut action consumes the MENU. Even if it is still down,
1954 // it won't trigger the next shortcut action. In the case of the
1955 // shortcut action triggering a new activity, like Bookmarks, we
1956 // won't get onKeyUp for MENU. So it is important to reset it here.
1957 mMenuIsDown = false;
1958 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001959 if (mUi.onOptionsItemSelected(item)) {
1960 // ui callback handled it
1961 return true;
1962 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 switch (item.getItemId()) {
1964 // -- Main menu
1965 case R.id.new_tab_menu_id:
1966 openTabToHomePage();
1967 break;
1968
1969 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001970 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001971 break;
1972
1973 case R.id.goto_menu_id:
1974 editUrl();
1975 break;
1976
1977 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001978 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1979 break;
1980
1981 case R.id.history_menu_id:
1982 bookmarksOrHistoryPicker(ComboViews.History);
1983 break;
1984
1985 case R.id.snapshots_menu_id:
1986 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001987 break;
1988
Pankaj Garg49b79252014-11-07 17:33:41 -08001989 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001990 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001991 break;
1992
1993 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001994 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001995 stopLoading();
1996 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001997 Tab currentTab = mTabControl.getCurrentTab();
1998 if (currentTab.hasCrashed) {
1999 currentTab.replaceCrashView(getCurrentTopWebView(),
2000 currentTab.getViewContainer());
2001 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002002 getCurrentTopWebView().reload();
2003 }
2004 break;
2005
Michael Kolb8233fac2010-10-26 16:08:53 -07002006 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002007 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002008 break;
2009
2010 case R.id.close_menu_id:
2011 // Close the subwindow if it exists.
2012 if (mTabControl.getCurrentSubWindow() != null) {
2013 dismissSubWindow(mTabControl.getCurrentTab());
2014 break;
2015 }
2016 closeCurrentTab();
2017 break;
2018
kaiyiza8b6dbb2013-07-29 18:11:22 +08002019 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002020 Object[] params = { new String("persist.debug.browsermonkeytest")};
2021 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002022 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002023 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002024 if (ret != null && ret.equals("enable"))
2025 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002026 if (BrowserConfig.getInstance(getContext())
2027 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2028 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002029 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002030 case R.id.homepage_menu_id:
2031 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002032 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002033 break;
2034
2035 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002036 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002037 break;
2038
2039 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002040 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002041 break;
2042
John Reck2bc80422011-06-30 15:11:49 -07002043 case R.id.save_snapshot_menu_id:
2044 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002045 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002046 createScreenshotAsync(
2047 source.getWebView(),
2048 getDesiredThumbnailWidth(mActivity),
2049 getDesiredThumbnailHeight(mActivity),
2050 new ValueCallback<Bitmap>() {
2051 @Override
2052 public void onReceiveValue(Bitmap bitmap) {
2053 new SaveSnapshotTask(source, bitmap).execute();
2054 }
2055 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002056 break;
2057
Michael Kolb8233fac2010-10-26 16:08:53 -07002058 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002059 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002060 break;
2061
John Recke1a03a32011-09-14 17:04:16 -07002062 case R.id.snapshot_go_live:
2063 goLive();
2064 return true;
2065
Michael Kolb8233fac2010-10-26 16:08:53 -07002066 case R.id.share_page_menu_id:
2067 Tab currentTab = mTabControl.getCurrentTab();
2068 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 return false;
2070 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002071 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002072 break;
2073
2074 case R.id.dump_nav_menu_id:
2075 getCurrentTopWebView().debugDump();
2076 break;
2077
Michael Kolb8233fac2010-10-26 16:08:53 -07002078 case R.id.zoom_in_menu_id:
2079 getCurrentTopWebView().zoomIn();
2080 break;
2081
2082 case R.id.zoom_out_menu_id:
2083 getCurrentTopWebView().zoomOut();
2084 break;
2085
2086 case R.id.view_downloads_menu_id:
2087 viewDownloads();
2088 break;
2089
John Reck42229bc2011-08-19 13:26:43 -07002090 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002091 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002092 break;
2093
Michael Kolb8233fac2010-10-26 16:08:53 -07002094 case R.id.window_one_menu_id:
2095 case R.id.window_two_menu_id:
2096 case R.id.window_three_menu_id:
2097 case R.id.window_four_menu_id:
2098 case R.id.window_five_menu_id:
2099 case R.id.window_six_menu_id:
2100 case R.id.window_seven_menu_id:
2101 case R.id.window_eight_menu_id:
2102 {
2103 int menuid = item.getItemId();
2104 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2105 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2106 Tab desiredTab = mTabControl.getTab(id);
2107 if (desiredTab != null &&
2108 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002109 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002110 }
2111 break;
2112 }
2113 }
2114 }
2115 break;
2116
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002117 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002118 Bundle bundle = new Bundle();
2119 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2120 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2121 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2122 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2123 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002124 break;
2125
Pankaj Garg49b79252014-11-07 17:33:41 -08002126 case R.id.add_to_homescreen:
2127 final WebView w = getCurrentTopWebView();
2128 final EditText input = new EditText(getContext());
2129 input.setText(w.getTitle());
2130 new AlertDialog.Builder(getContext())
2131 .setTitle("Add to homescreen")
2132 .setMessage("Title")
2133 .setView(input)
2134 .setPositiveButton("Add", new DialogInterface.OnClickListener() {
2135 public void onClick(DialogInterface dialog, int whichButton) {
2136 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2137 getContext(),
2138 w.getUrl(),
2139 input.getText().toString(),
2140 w.getViewportBitmap(),
2141 w.getFavicon()));
2142
2143 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2144 .addCategory(Intent.CATEGORY_HOME));
2145 }})
2146 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
2147 public void onClick(DialogInterface dialog, int whichButton) {
2148 // Do nothing.
2149 }
2150 })
2151 .show();
2152 break;
2153
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 default:
2155 return false;
2156 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002157 return true;
2158 }
2159
John Reck68234a92012-04-19 15:27:12 -07002160 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2161 implements OnCancelListener {
2162
2163 private Tab mTab;
2164 private Dialog mProgressDialog;
2165 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002166 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002167
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002168 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002169 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002170 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002171 }
2172
2173 @Override
2174 protected void onPreExecute() {
2175 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2176 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2177 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002178 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002179 }
2180
2181 @Override
2182 protected Long doInBackground(Void... params) {
2183 if (!mTab.saveViewState(mValues)) {
2184 return null;
2185 }
2186 if (isCancelled()) {
2187 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2188 File file = mActivity.getFileStreamPath(path);
2189 if (!file.delete()) {
2190 file.deleteOnExit();
2191 }
2192 return null;
2193 }
2194 final ContentResolver cr = mActivity.getContentResolver();
2195 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2196 if (result == null) {
2197 return null;
2198 }
2199 long id = ContentUris.parseId(result);
2200 return id;
2201 }
2202
2203 @Override
2204 protected void onPostExecute(Long id) {
2205 if (isCancelled()) {
2206 return;
2207 }
2208 mProgressDialog.dismiss();
2209 if (id == null) {
2210 Toast.makeText(mActivity, R.string.snapshot_failed,
2211 Toast.LENGTH_SHORT).show();
2212 return;
2213 }
2214 Bundle b = new Bundle();
2215 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2216 mUi.showComboView(ComboViews.Snapshots, b);
2217 }
2218
2219 @Override
2220 public void onCancel(DialogInterface dialog) {
2221 cancel(true);
2222 }
2223 }
2224
Michael Kolb80f75082012-04-10 10:50:06 -07002225 @Override
2226 public void toggleUserAgent() {
2227 WebView web = getCurrentWebView();
2228 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002229 }
2230
2231 @Override
2232 public void findOnPage() {
2233 getCurrentTopWebView().showFindDialog(null, true);
2234 }
2235
2236 @Override
2237 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002238 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002239 }
2240
2241 @Override
2242 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002243 WebView w = getCurrentTopWebView();
2244 if (w == null)
2245 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002246 final Intent i = createBookmarkCurrentPageIntent(false);
2247 createScreenshotAsync(
2248 w, getDesiredThumbnailWidth(mActivity),
2249 getDesiredThumbnailHeight(mActivity),
2250 new ValueCallback<Bitmap>() {
2251 @Override
2252 public void onReceiveValue(Bitmap bitmap) {
2253 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2254 mActivity.startActivity(i);
2255 }
2256 });
Michael Kolb80f75082012-04-10 10:50:06 -07002257 }
2258
John Recke1a03a32011-09-14 17:04:16 -07002259 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002260 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002261 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002262 boolean onlySingleTabRemaining = false;
2263 if (mTabControl.getTabCount() > 1) {
2264 // destroy the old snapshot tab
2265 closeCurrentTab();
2266 } else {
2267 onlySingleTabRemaining = true;
2268 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002269 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002270 if (onlySingleTabRemaining) {
2271 closeTab(t);
2272 }
2273
Tarun Nainani8eb00912014-07-17 12:28:32 -07002274 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002275 }
2276
luxiaolb40014b2013-07-19 10:01:43 +08002277 private void showExitDialog(final Activity activity) {
2278 new AlertDialog.Builder(activity)
2279 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002280 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002281 .setMessage(R.string.exit_browser_msg)
2282 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2283 public void onClick(DialogInterface dialog, int which) {
2284 activity.moveTaskToBack(true);
2285 dialog.dismiss();
2286 }
2287 })
2288 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2289 public void onClick(DialogInterface dialog, int which) {
2290 activity.finish();
2291 mHandler.postDelayed(new Runnable() {
2292 @Override
2293 public void run() {
luxiaolb40014b2013-07-19 10:01:43 +08002294 mCrashRecoveryHandler.clearState(true);
2295 int pid = android.os.Process.myPid();
2296 android.os.Process.killProcess(pid);
2297 }
2298 }, 300);
2299 dialog.dismiss();
2300 }
2301 })
2302 .show();
2303 }
Michael Kolb315d5022011-10-13 12:47:11 -07002304 @Override
2305 public void showPageInfo() {
2306 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2307 }
2308
John Reck9c35b9c2012-05-30 10:08:50 -07002309 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002310 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002311 // Let the History and Bookmark fragments handle menus they created.
2312 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2313 return false;
2314 }
2315
Michael Kolb8233fac2010-10-26 16:08:53 -07002316 int id = item.getItemId();
2317 boolean result = true;
2318 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002319 // -- Browser context menu
2320 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002321 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002322 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002323 case R.id.copy_link_context_menu_id:
2324 final WebView webView = getCurrentTopWebView();
2325 if (null == webView) {
2326 result = false;
2327 break;
2328 }
2329 final HashMap<String, WebView> hrefMap =
2330 new HashMap<String, WebView>();
2331 hrefMap.put("webview", webView);
2332 final Message msg = mHandler.obtainMessage(
2333 FOCUS_NODE_HREF, id, 0, hrefMap);
2334 webView.requestFocusNodeHref(msg);
2335 break;
2336
2337 default:
2338 // For other context menus
2339 result = onOptionsItemSelected(item);
2340 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002341 return result;
2342 }
2343
2344 /**
2345 * support programmatically opening the context menu
2346 */
2347 public void openContextMenu(View view) {
2348 mActivity.openContextMenu(view);
2349 }
2350
2351 /**
2352 * programmatically open the options menu
2353 */
2354 public void openOptionsMenu() {
2355 mActivity.openOptionsMenu();
2356 }
2357
John Reck9c35b9c2012-05-30 10:08:50 -07002358 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002359 public boolean onMenuOpened(int featureId, Menu menu) {
2360 if (mOptionsMenuOpen) {
2361 if (mConfigChanged) {
2362 // We do not need to make any changes to the state of the
2363 // title bar, since the only thing that happened was a
2364 // change in orientation
2365 mConfigChanged = false;
2366 } else {
2367 if (!mExtendedMenuOpen) {
2368 mExtendedMenuOpen = true;
2369 mUi.onExtendedMenuOpened();
2370 } else {
2371 // Switching the menu back to icon view, so show the
2372 // title bar once again.
2373 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002374 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002375 }
2376 }
2377 } else {
2378 // The options menu is closed, so open it, and show the title
2379 mOptionsMenuOpen = true;
2380 mConfigChanged = false;
2381 mExtendedMenuOpen = false;
2382 mUi.onOptionsMenuOpened();
2383 }
2384 return true;
2385 }
2386
John Reck9c35b9c2012-05-30 10:08:50 -07002387 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002388 public void onOptionsMenuClosed(Menu menu) {
2389 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002390 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002391 }
2392
John Reck9c35b9c2012-05-30 10:08:50 -07002393 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002394 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002395 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002396 }
2397
2398 // Helper method for getting the top window.
2399 @Override
2400 public WebView getCurrentTopWebView() {
2401 return mTabControl.getCurrentTopWebView();
2402 }
2403
2404 @Override
2405 public WebView getCurrentWebView() {
2406 return mTabControl.getCurrentWebView();
2407 }
2408
2409 /*
2410 * This method is called as a result of the user selecting the options
2411 * menu to see the download window. It shows the download window on top of
2412 * the current window.
2413 */
2414 void viewDownloads() {
2415 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2416 mActivity.startActivity(intent);
2417 }
2418
John Reck30b065e2011-07-19 10:58:05 -07002419 int getActionModeHeight() {
2420 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2421 new int[] { android.R.attr.actionBarSize });
2422 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2423 actionBarSizeTypedArray.recycle();
2424 return size;
2425 }
2426
Michael Kolb8233fac2010-10-26 16:08:53 -07002427 // action mode
2428
John Reck9c35b9c2012-05-30 10:08:50 -07002429 @Override
2430 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002431 mUi.onActionModeStarted(mode);
2432 mActionMode = mode;
2433 }
2434
2435 /*
2436 * True if a custom ActionMode (i.e. find or select) is in use.
2437 */
2438 @Override
2439 public boolean isInCustomActionMode() {
2440 return mActionMode != null;
2441 }
2442
2443 /*
2444 * End the current ActionMode.
2445 */
2446 @Override
2447 public void endActionMode() {
2448 if (mActionMode != null) {
2449 mActionMode.finish();
2450 }
2451 }
2452
2453 /*
2454 * Called by find and select when they are finished. Replace title bars
2455 * as necessary.
2456 */
John Reck9c35b9c2012-05-30 10:08:50 -07002457 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002458 public void onActionModeFinished(ActionMode mode) {
2459 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002460 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002461 mActionMode = null;
2462 }
2463
2464 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002465 final Tab tab = getCurrentTab();
2466 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002467 }
2468
2469 // bookmark handling
2470
2471 /**
2472 * add the current page as a bookmark to the given folder id
2473 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002474 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002475 * bookmarked, and if it is, edit that bookmark. If false, and
2476 * the site is already bookmarked, do not attempt to edit the
2477 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002478 */
2479 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002480 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002481 WebView w = getCurrentTopWebView();
2482 if (w == null) {
2483 return null;
2484 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002485 Intent i = new Intent(mActivity,
2486 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002487 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2488 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2489 String touchIconUrl = w.getTouchIconUrl();
2490 if (touchIconUrl != null) {
2491 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2492 WebSettings settings = w.getSettings();
2493 if (settings != null) {
2494 i.putExtra(AddBookmarkPage.USER_AGENT,
2495 settings.getUserAgentString());
2496 }
2497 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002498 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002499 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002500 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002501 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2502 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002503 // Put the dialog at the upper right of the screen, covering the
2504 // star on the title bar.
2505 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002506 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002507 }
2508
2509 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002510 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002511 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002512 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002513 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002514 }
2515
Vivek Sekharb54614f2014-05-01 19:03:37 -07002516 @Override
2517 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2518 boolean capture) {
2519 mUploadHandler = new UploadHandler(this);
2520 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2521 }
2522
Michael Kolb8233fac2010-10-26 16:08:53 -07002523 // thumbnails
2524
2525 /**
2526 * Return the desired width for thumbnail screenshots, which are stored in
2527 * the database, and used on the bookmarks screen.
2528 * @param context Context for finding out the density of the screen.
2529 * @return desired width for thumbnail screenshot.
2530 */
2531 static int getDesiredThumbnailWidth(Context context) {
2532 return context.getResources().getDimensionPixelOffset(
2533 R.dimen.bookmarkThumbnailWidth);
2534 }
2535
2536 /**
2537 * Return the desired height for thumbnail screenshots, which are stored in
2538 * the database, and used on the bookmarks screen.
2539 * @param context Context for finding out the density of the screen.
2540 * @return desired height for thumbnail screenshot.
2541 */
2542 static int getDesiredThumbnailHeight(Context context) {
2543 return context.getResources().getDimensionPixelOffset(
2544 R.dimen.bookmarkThumbnailHeight);
2545 }
2546
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002547 static void createScreenshotAsync(WebView view, int width, int height,
2548 final ValueCallback<Bitmap> cb) {
2549 if (view == null || width == 0 || height == 0) {
2550 return;
2551 }
2552
2553 view.getContentBitmapAsync(
2554 (float) width / view.getWidth(),
2555 new Rect(),
2556 new ValueCallback<Bitmap>() {
2557 @Override
2558 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002559 if (bitmap != null)
2560 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2561 cb.onReceiveValue(bitmap);
2562 }});
2563 }
2564
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002565 private void updateScreenshot(final Tab tab) {
2566 createScreenshotAsync(
2567 tab.getWebView(),
2568 getDesiredThumbnailWidth(mActivity),
2569 getDesiredThumbnailHeight(mActivity),
2570 new ValueCallback<Bitmap>() {
2571 @Override
2572 public void onReceiveValue(Bitmap bitmap) {
2573 updateScreenshot(tab, bitmap);
2574 }
2575 });
2576 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002577
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002578 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002579 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 // FIXME: Would like to make sure there is actually something to
2581 // draw, but the API for that (WebViewCore.pictureReady()) is not
2582 // currently accessible here.
2583
John Reck34ef2672011-02-10 11:30:55 -08002584 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002585 if (view == null) {
2586 // Tab was destroyed
2587 return;
2588 }
John Reck34ef2672011-02-10 11:30:55 -08002589 final String url = tab.getUrl();
2590 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002591 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002592 if (TextUtils.isEmpty(url)) {
2593 return;
2594 }
2595
kaiyiz6e5b3e02013-08-19 20:02:01 +08002596 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002597 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002598 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002599 }
John Reck34ef2672011-02-10 11:30:55 -08002600 // Only update thumbnails for web urls (http(s)://), not for
2601 // about:, javascript:, data:, etc...
2602 // Unless it is a bookmarked site, then always update
2603 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2604 return;
2605 }
2606
kaiyiz6e5b3e02013-08-19 20:02:01 +08002607 if (url != null && mUpdateMyNavThumbnailUrl != null
2608 && Patterns.WEB_URL.matcher(url).matches()
2609 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2610 String urlHost = (new WebAddress(url)).getHost();
2611 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2612 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2613 || bookmarkHost.length() == 0) {
2614 return;
2615 }
2616 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2617 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2618 bookmarkHost.length());
2619 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2620 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2621 if (!bookmarkDomain.equals(urlDomain)) {
2622 return;
2623 }
2624 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002625 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002626 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2627 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2628 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2629 500);
2630 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002631 return;
2632 }
2633
2634 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002635 new AsyncTask<Void, Void, Void>() {
2636 @Override
2637 protected Void doInBackground(Void... unused) {
2638 Cursor cursor = null;
2639 try {
2640 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002641 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2642 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2643 : url);
2644 if (mUpdateMyNavThumbnail) {
2645 mUpdateMyNavThumbnail = false;
2646 mUpdateMyNavThumbnailUrl = null;
2647 }
John Reck34ef2672011-02-10 11:30:55 -08002648 if (cursor != null && cursor.moveToFirst()) {
2649 final ByteArrayOutputStream os =
2650 new ByteArrayOutputStream();
2651 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002652
John Reck34ef2672011-02-10 11:30:55 -08002653 ContentValues values = new ContentValues();
2654 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002655
John Reck34ef2672011-02-10 11:30:55 -08002656 do {
John Reck617fd832011-02-16 14:35:59 -08002657 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002658 cr.update(Images.CONTENT_URI, values, null, null);
2659 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002660 }
John Reck34ef2672011-02-10 11:30:55 -08002661 } catch (IllegalStateException e) {
2662 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002663 } catch (SQLiteException s) {
2664 // Added for possible error when user tries to remove the same bookmark
2665 // that is being updated with a screen shot
2666 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002667 } finally {
2668 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002669 }
John Reck34ef2672011-02-10 11:30:55 -08002670 return null;
2671 }
2672 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002673 }
2674
2675 private class Copy implements OnMenuItemClickListener {
2676 private CharSequence mText;
2677
John Reck9c35b9c2012-05-30 10:08:50 -07002678 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002679 public boolean onMenuItemClick(MenuItem item) {
2680 copy(mText);
2681 return true;
2682 }
2683
2684 public Copy(CharSequence toCopy) {
2685 mText = toCopy;
2686 }
2687 }
2688
Leon Scroggins63c02662010-11-18 15:16:27 -05002689 private static class Download implements OnMenuItemClickListener {
2690 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002691 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002692 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002693 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002694 private static final String FALLBACK_EXTENSION = "dat";
2695 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002696
John Reck9c35b9c2012-05-30 10:08:50 -07002697 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002698 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002699 if (DataUri.isDataUri(mText)) {
2700 saveDataUri();
2701 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002702 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002703 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002704 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002705 return true;
2706 }
2707
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002708 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2709 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002710 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002711 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002712 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002713 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002714 }
George Mount387d45d2011-10-07 15:57:53 -07002715
2716 /**
2717 * Treats mText as a data URI and writes its contents to a file
2718 * based on the current time.
2719 */
2720 private void saveDataUri() {
2721 FileOutputStream outputStream = null;
2722 try {
2723 DataUri uri = new DataUri(mText);
2724 File target = getTarget(uri);
2725 outputStream = new FileOutputStream(target);
2726 outputStream.write(uri.getData());
2727 final DownloadManager manager =
2728 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2729 manager.addCompletedDownload(target.getName(),
2730 mActivity.getTitle().toString(), false,
2731 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002732 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002733 } catch (IOException e) {
2734 Log.e(LOGTAG, "Could not save data URL");
2735 } finally {
2736 if (outputStream != null) {
2737 try {
2738 outputStream.close();
2739 } catch (IOException e) {
2740 // ignore close errors
2741 }
2742 }
2743 }
2744 }
2745
2746 /**
2747 * Creates a File based on the current time stamp and uses
2748 * the mime type of the DataUri to get the extension.
2749 */
2750 private File getTarget(DataUri uri) throws IOException {
2751 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002752 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002753 String nameBase = format.format(new Date());
2754 String mimeType = uri.getMimeType();
2755 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2756 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2757 if (extension == null) {
2758 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2759 extension = FALLBACK_EXTENSION;
2760 }
2761 extension = "." + extension; // createTempFile needs the '.'
2762 File targetFile = File.createTempFile(nameBase, extension, dir);
2763 return targetFile;
2764 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002765 }
2766
Cary Clark8974d282010-11-22 10:46:05 -05002767 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002768 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002769
John Reck9c35b9c2012-05-30 10:08:50 -07002770 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002771 public boolean onMenuItemClick(MenuItem item) {
2772 if (mWebView != null) {
2773 return mWebView.selectText();
2774 }
2775 return false;
2776 }
2777
2778 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002779 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002780 }
2781
2782 }
2783
Michael Kolb8233fac2010-10-26 16:08:53 -07002784 /********************** TODO: UI stuff *****************************/
2785
2786 // these methods have been copied, they still need to be cleaned up
2787
2788 /****************** tabs ***************************************************/
2789
2790 // basic tab interactions:
2791
2792 // it is assumed that tabcontrol already knows about the tab
2793 protected void addTab(Tab tab) {
2794 mUi.addTab(tab);
2795 }
2796
2797 protected void removeTab(Tab tab) {
2798 mUi.removeTab(tab);
2799 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002800 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002801 }
2802
Michael Kolbf2055602011-04-09 17:20:03 -07002803 @Override
2804 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002805 // monkey protection against delayed start
2806 if (tab != null) {
2807 mTabControl.setCurrentTab(tab);
2808 // the tab is guaranteed to have a webview after setCurrentTab
2809 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002810 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002811 //Purge active tabs
2812 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002813 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002814 }
2815
John Reck8bcafc12011-08-29 16:43:02 -07002816 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002817 Tab current = mTabControl.getCurrentTab();
2818 if (current != null
2819 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002820 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002821 }
2822 }
2823
John Reck26b18322011-06-21 13:08:58 -07002824 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002825 // Dismiss the subwindow if applicable.
2826 dismissSubWindow(appTab);
2827 // Since we might kill the WebView, remove it from the
2828 // content view first.
2829 mUi.detachTab(appTab);
2830 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002831 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002832 // TODO: analyze why the remove and add are necessary
2833 mUi.attachTab(appTab);
2834 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002835 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002836 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002837 } else {
2838 // If the tab was the current tab, we have to attach
2839 // it to the view system again.
2840 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002841 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002842 }
2843 }
2844
2845 // Remove the sub window if it exists. Also called by TabControl when the
2846 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002847 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002848 public void dismissSubWindow(Tab tab) {
2849 removeSubWindow(tab);
2850 // dismiss the subwindow. This will destroy the WebView.
2851 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002852 WebView wv = getCurrentTopWebView();
2853 if (wv != null) {
2854 wv.requestFocus();
2855 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002856 }
2857
2858 @Override
2859 public void removeSubWindow(Tab t) {
2860 if (t.getSubWebView() != null) {
2861 mUi.removeSubWindow(t.getSubViewContainer());
2862 }
2863 }
2864
2865 @Override
2866 public void attachSubWindow(Tab tab) {
2867 if (tab.getSubWebView() != null) {
2868 mUi.attachSubWindow(tab.getSubViewContainer());
2869 getCurrentTopWebView().requestFocus();
2870 }
2871 }
2872
Mathew Inwood29721c22011-06-29 17:55:24 +01002873 private Tab showPreloadedTab(final UrlData urlData) {
2874 if (!urlData.isPreloaded()) {
2875 return null;
2876 }
2877 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2878 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2879 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002880 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002881 // Could not submit query. Fallback to regular tab creation
2882 tabControl.destroy();
2883 return null;
2884 }
2885 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002886 // check tab count and make room for new tab
2887 if (!mTabControl.canCreateNewTab()) {
2888 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2889 if (leastUsed != null) {
2890 closeTab(leastUsed);
2891 }
2892 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002893 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002894 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002895 mTabControl.addPreloadedTab(t);
2896 addTab(t);
2897 setActiveTab(t);
2898 return t;
2899 }
2900
Michael Kolb7bcafde2011-05-09 13:55:59 -07002901 // open a non inconito tab with the given url data
2902 // and set as active tab
2903 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002904 Tab tab = showPreloadedTab(urlData);
2905 if (tab == null) {
2906 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002907 if ((tab != null) && !urlData.isEmpty()) {
2908 loadUrlDataIn(tab, urlData);
2909 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002910 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002911 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002912 }
2913
Michael Kolb843510f2010-12-09 10:51:49 -08002914 @Override
2915 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002916 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002917 }
2918
Michael Kolb8233fac2010-10-26 16:08:53 -07002919 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002920 public Tab openIncognitoTab() {
2921 return openTab(INCOGNITO_URI, true, true, false);
2922 }
2923
2924 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002925 public Tab openTab(String url, boolean incognito, boolean setActive,
2926 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002927 return openTab(url, incognito, setActive, useCurrent, null);
2928 }
2929
2930 @Override
2931 public Tab openTab(String url, Tab parent, boolean setActive,
2932 boolean useCurrent) {
2933 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2934 setActive, useCurrent, parent);
2935 }
2936
2937 public Tab openTab(String url, boolean incognito, boolean setActive,
2938 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002939 Tab tab = createNewTab(incognito, setActive, useCurrent);
2940 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002941 if (parent instanceof SnapshotTab) {
2942 addTab(tab);
2943 if (setActive)
2944 setActiveTab(tab);
2945 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002946 parent.addChildTab(tab);
2947 }
Michael Kolb519d2282011-05-09 17:03:19 -07002948 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002949 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002950 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002951 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002952 return tab;
2953 }
2954
2955 // this method will attempt to create a new tab
2956 // incognito: private browsing tab
2957 // setActive: ste tab as current tab
2958 // useCurrent: if no new tab can be created, return current tab
2959 private Tab createNewTab(boolean incognito, boolean setActive,
2960 boolean useCurrent) {
2961 Tab tab = null;
2962 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05002963 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002964 addTab(tab);
2965 if (setActive) {
2966 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002967 } else {
2968 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002969 }
2970 } else {
2971 if (useCurrent) {
2972 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002973 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002974 } else {
2975 mUi.showMaxTabsWarning();
2976 }
2977 }
2978 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002979 }
2980
John Reck2bc80422011-06-30 15:11:49 -07002981 @Override
2982 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2983 SnapshotTab tab = null;
2984 if (mTabControl.canCreateNewTab()) {
2985 tab = mTabControl.createSnapshotTab(snapshotId);
2986 addTab(tab);
2987 if (setActive) {
2988 setActiveTab(tab);
2989 }
2990 } else {
2991 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002992 }
2993 return tab;
2994 }
2995
Michael Kolb8233fac2010-10-26 16:08:53 -07002996 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002997 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 * @return boolean True if we successfully switched to a different tab. If
2999 * the indexth tab is null, or if that tab is the same as
3000 * the current one, return false.
3001 */
3002 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003003 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003004 Tab currentTab = mTabControl.getCurrentTab();
3005 if (tab == null || tab == currentTab) {
3006 return false;
3007 }
3008 setActiveTab(tab);
3009 return true;
3010 }
3011
3012 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003013 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003014 closeCurrentTab(false);
3015 }
3016
3017 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003018 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003019 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003020 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003021 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003022 return;
3023 }
Michael Kolbc831b632011-05-11 09:30:34 -07003024 final Tab current = mTabControl.getCurrentTab();
3025 final int pos = mTabControl.getCurrentPosition();
3026 Tab newTab = current.getParent();
3027 if (newTab == null) {
3028 newTab = mTabControl.getTab(pos + 1);
3029 if (newTab == null) {
3030 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003031 }
3032 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003033 if (andQuit) {
3034 mTabControl.setCurrentTab(newTab);
3035 closeTab(current);
3036 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003037 // Close window
3038 closeTab(current);
3039 }
3040 }
3041
3042 /**
3043 * Close the tab, remove its associated title bar, and adjust mTabControl's
3044 * current tab to a valid value.
3045 */
3046 @Override
3047 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003048 if (tab == mTabControl.getCurrentTab()) {
3049 closeCurrentTab();
3050 } else {
3051 removeTab(tab);
3052 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003053 }
3054
Afzal Najamd4e33312012-04-26 01:54:01 -04003055 /**
3056 * Close all tabs except the current one
3057 */
3058 @Override
3059 public void closeOtherTabs() {
3060 int inactiveTabs = mTabControl.getTabCount() - 1;
3061 for (int i = inactiveTabs; i >= 0; i--) {
3062 Tab tab = mTabControl.getTab(i);
3063 if (tab != mTabControl.getCurrentTab()) {
3064 removeTab(tab);
3065 }
3066 }
3067 }
3068
Michael Kolb8233fac2010-10-26 16:08:53 -07003069 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003070 protected void loadUrlFromContext(String url) {
3071 Tab tab = getCurrentTab();
3072 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003073 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003074 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003076 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003077 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003078 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003079 }
3080 }
3081 }
3082
3083 /**
3084 * Load the URL into the given WebView and update the title bar
3085 * to reflect the new load. Call this instead of WebView.loadUrl
3086 * directly.
3087 * @param view The WebView used to load url.
3088 * @param url The URL to load.
3089 */
John Reck71e51422011-07-01 16:49:28 -07003090 @Override
3091 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003092 loadUrl(tab, url, null);
3093 }
3094
3095 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3096 if (tab != null) {
3097 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003098 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003099 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003100 if (tab.hasCrashed) {
3101 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3102 }
Michael Kolba53c9892011-10-05 13:31:40 -07003103 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003104 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003105 }
3106
3107 /**
3108 * Load UrlData into a Tab and update the title bar to reflect the new
3109 * load. Call this instead of UrlData.loadIn directly.
3110 * @param t The Tab used to load.
3111 * @param data The UrlData being loaded.
3112 */
3113 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003114 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003115 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003116 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003117 } else {
John Reck38b39652012-06-05 09:22:59 -07003118 if (t != null && data.mDisableUrlOverride) {
3119 t.disableUrlOverridingForLoad();
3120 }
John Reck26b18322011-06-21 13:08:58 -07003121 loadUrl(t, data.mUrl, data.mHeaders);
3122 }
3123 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003124 }
3125
John Reck30c714c2010-12-16 17:30:34 -08003126 @Override
3127 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003128 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003129 //In case of tab can go back (aka tab has navigation entry) do nothing
3130 //else just load homepage in current tab.
3131 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003132 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003133 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003134 }
3135
3136 void goBackOnePageOrQuit() {
3137 Tab current = mTabControl.getCurrentTab();
3138 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003139 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3140 showExitDialog(mActivity);
3141 } else {
3142 /*
3143 * Instead of finishing the activity, simply push this to the back
3144 * of the stack and let ActivityManager to choose the foreground
3145 * activity. As BrowserActivity is singleTask, it will be always the
3146 * root of the task. So we can use either true or false for
3147 * moveTaskToBack().
3148 */
3149 mActivity.moveTaskToBack(true);
3150 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003151 return;
3152 }
John Reckef654f12011-07-12 16:42:08 -07003153 if (current.canGoBack()) {
3154 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003155 } else {
3156 // Check to see if we are closing a window that was created by
3157 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003158 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003159 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003160 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003161 // Now we close the other tab
3162 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003163 } else if (BrowserConfig.getInstance(getContext())
3164 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3165 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003166 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003167 if ((current.getAppId() != null) || current.closeOnBack()) {
3168 closeCurrentTab(true);
3169 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003170 /*
3171 * Instead of finishing the activity, simply push this to the back
3172 * of the stack and let ActivityManager to choose the foreground
3173 * activity. As BrowserActivity is singleTask, it will be always the
3174 * root of the task. So we can use either true or false for
3175 * moveTaskToBack().
3176 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003177 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003178 }
3179 }
3180 }
3181
3182 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003183 * helper method for key handler
3184 * returns the current tab if it can't advance
3185 */
Michael Kolbc831b632011-05-11 09:30:34 -07003186 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003187 int pos = mTabControl.getCurrentPosition() + 1;
3188 if (pos >= mTabControl.getTabCount()) {
3189 pos = 0;
3190 }
3191 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003192 }
3193
3194 /**
3195 * helper method for key handler
3196 * returns the current tab if it can't advance
3197 */
Michael Kolbc831b632011-05-11 09:30:34 -07003198 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003199 int pos = mTabControl.getCurrentPosition() - 1;
3200 if ( pos < 0) {
3201 pos = mTabControl.getTabCount() - 1;
3202 }
3203 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003204 }
3205
John Reckbcef87f2012-02-03 14:58:34 -08003206 boolean isMenuOrCtrlKey(int keyCode) {
3207 return (KeyEvent.KEYCODE_MENU == keyCode)
3208 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3209 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3210 }
3211
Michael Kolb0035fad2011-03-14 13:25:28 -07003212 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003213 * handle key events in browser
3214 *
3215 * @param keyCode
3216 * @param event
3217 * @return true if handled, false to pass to super
3218 */
John Reck9c35b9c2012-05-30 10:08:50 -07003219 @Override
3220 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003221 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3222 // Hardware menu key
Enrico Ros1f5a0952014-11-18 20:15:48 -08003223 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
Pankaj Garg49b79252014-11-07 17:33:41 -08003224 true, false);
3225 return true;
3226 }
3227
Cary Clark160bbb92011-01-10 11:17:07 -05003228 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003229 // Even if MENU is already held down, we need to call to super to open
3230 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003231 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003232 mMenuIsDown = true;
3233 return false;
3234 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003235
Cary Clark8ff8c662010-12-29 15:03:05 -05003236 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003237 Tab tab = getCurrentTab();
3238 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003239
Cary Clark160bbb92011-01-10 11:17:07 -05003240 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3241 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003242
Michael Kolb8233fac2010-10-26 16:08:53 -07003243 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003244 case KeyEvent.KEYCODE_TAB:
3245 if (event.isCtrlPressed()) {
3246 if (event.isShiftPressed()) {
3247 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003248 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003249 } else {
3250 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003251 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003252 }
3253 return true;
3254 }
3255 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003256 case KeyEvent.KEYCODE_SPACE:
3257 // WebView/WebTextView handle the keys in the KeyDown. As
3258 // the Activity's shortcut keys are only handled when WebView
3259 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003260 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003261 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003262 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003263 pageDown();
3264 }
3265 return true;
3266 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003267 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003268 event.startTracking();
3269 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003270 case KeyEvent.KEYCODE_FORWARD:
3271 if (!noModifiers) break;
3272 tab.goForward();
3273 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003274 case KeyEvent.KEYCODE_DPAD_LEFT:
3275 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003276 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003277 return true;
3278 }
3279 break;
3280 case KeyEvent.KEYCODE_DPAD_RIGHT:
3281 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003282 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003283 return true;
3284 }
3285 break;
3286 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003287 if (ctrl) {
3288 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003289 return true;
3290 }
3291 break;
Michael Kolba4183062011-01-16 10:43:21 -08003292// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003293 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003294 if (ctrl ) {
3295 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003296 return true;
3297 }
3298 break;
Michael Kolba4183062011-01-16 10:43:21 -08003299// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003300// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003301// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003302// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003303// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003304// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003305// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003306// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003307// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003308// case KeyEvent.KEYCODE_M: // unused
3309// case KeyEvent.KEYCODE_N: // in Chrome: new window
3310// case KeyEvent.KEYCODE_O: // in Chrome: open file
3311// case KeyEvent.KEYCODE_P: // in Chrome: print page
3312// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003313// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003314// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3315 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003316 // we can't use the ctrl/shift flags, they check for
3317 // exclusive use of a modifier
3318 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003319 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003320 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003321 } else {
3322 openTabToHomePage();
3323 }
3324 return true;
3325 }
3326 break;
3327// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3328// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003329// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003330// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3331// case KeyEvent.KEYCODE_Y: // unused
3332// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003333 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003334 // it is a regular key and webview is not null
3335 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003336 }
3337
John Reck9c35b9c2012-05-30 10:08:50 -07003338 @Override
3339 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003340 switch(keyCode) {
3341 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003342 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003343 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003344 return true;
3345 }
3346 break;
3347 }
3348 return false;
3349 }
3350
John Reck9c35b9c2012-05-30 10:08:50 -07003351 @Override
3352 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003353 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003354 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003355 if (KeyEvent.KEYCODE_MENU == keyCode
3356 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003357 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003358 }
3359 }
Cary Clark160bbb92011-01-10 11:17:07 -05003360 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003361 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003362 case KeyEvent.KEYCODE_BACK:
3363 if (event.isTracking() && !event.isCanceled()) {
3364 onBackKey();
3365 return true;
3366 }
3367 break;
3368 }
3369 return false;
3370 }
3371
3372 public boolean isMenuDown() {
3373 return mMenuIsDown;
3374 }
3375
John Reck9c35b9c2012-05-30 10:08:50 -07003376 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003377 public void setupAutoFill(Message message) {
3378 // Open the settings activity at the AutoFill profile fragment so that
3379 // the user can create a new profile. When they return, we will dispatch
3380 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003381 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003382 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3383 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003384 }
John Reckb3417f02011-01-14 11:01:05 -08003385
John Reck9c35b9c2012-05-30 10:08:50 -07003386 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003387 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003388 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003389 return true;
3390 }
3391
John Reck1cf4b792011-07-26 10:22:22 -07003392 @Override
3393 public boolean shouldCaptureThumbnails() {
3394 return mUi.shouldCaptureThumbnails();
3395 }
3396
Michael Kolbc3af0672011-08-09 10:24:41 -07003397 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003398 public boolean supportsVoice() {
3399 PackageManager pm = mActivity.getPackageManager();
3400 List activities = pm.queryIntentActivities(new Intent(
3401 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3402 return activities.size() != 0;
3403 }
3404
3405 @Override
3406 public void startVoiceRecognizer() {
3407 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003408 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003409 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3410 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3411 mActivity.startActivityForResult(voice, VOICE_RESULT);
3412 }
3413
Pankaj Garg7b279f62014-08-12 14:47:18 -07003414 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003415 if (mLevel == level)
3416 return;
3417 mLevel = level;
3418 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003419 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3420 lp.dimAmount = level;
3421 mActivity.getWindow().setAttributes(lp);
3422 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3423 } else {
3424 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3425 }
3426 }
3427
Michael Kolb0b129122012-06-04 16:31:58 -07003428 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003429 public void setBlockEvents(boolean block) {
3430 mBlockEvents = block;
3431 }
3432
John Reck9c35b9c2012-05-30 10:08:50 -07003433 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003434 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003435 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003436 }
3437
John Reck9c35b9c2012-05-30 10:08:50 -07003438 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003439 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003440 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003441 }
3442
John Reck9c35b9c2012-05-30 10:08:50 -07003443 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003444 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003445 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003446 }
3447
John Reck9c35b9c2012-05-30 10:08:50 -07003448 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003449 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003450 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003451 }
3452
John Reck9c35b9c2012-05-30 10:08:50 -07003453 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003454 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003455 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003456 }
3457
Pankaj Garg49b79252014-11-07 17:33:41 -08003458 @Override
3459 public boolean shouldShowAppMenu() {
3460 return true;
3461 }
3462
3463 @Override
3464 public int getMenuThemeResourceId() {
3465 return R.style.OverflowMenuTheme;
3466 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003467}