blob: ccc1c59e6de71a1acef2078f8f98857f04ab053f [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) {
1740 if (bitmap == null) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001741 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1742 return;
1743 }
1744
1745 final ContentResolver cr = mActivity.getContentResolver();
1746 new AsyncTask<Void, Void, Void>() {
1747 @Override
1748 protected Void doInBackground(Void... unused) {
1749 ContentResolver cr = mActivity.getContentResolver();
1750 Cursor cursor = null;
1751 try {
1752 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1753 new String[] {
1754 MyNavigationUtil.ID
1755 }, "url = ?", new String[] {
1756 itemUrl
1757 }, null);
1758 if (null != cursor && cursor.moveToFirst()) {
1759 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001760 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001761
1762 ContentValues values = new ContentValues();
1763 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1764 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1765 cursor.getLong(0));
1766 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1767 cr.update(uri, values, null, null);
1768 os.close();
1769 }
1770 } catch (IllegalStateException e) {
1771 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1772 } catch (IOException e) {
1773 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1774 } finally {
1775 if (null != cursor) {
1776 cursor.close();
1777 }
1778 }
1779 return null;
1780 }
1781 }.execute();
1782 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001783 /**
1784 * As the menu can be open when loading state changes
1785 * we must manually update the state of the stop/reload menu
1786 * item
1787 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001788 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001789 if (menu == null) {
1790 return;
1791 }
1792 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001793 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001794 menu.findItem(R.id.stop_menu_id):
1795 menu.findItem(R.id.reload_menu_id);
1796 if (src != null) {
1797 dest.setIcon(src.getIcon());
1798 dest.setTitle(src.getTitle());
1799 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001800 mActivity.invalidateOptionsMenu();
1801 }
1802
1803 public void invalidateOptionsMenu() {
1804 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001805 }
1806
John Reck9c35b9c2012-05-30 10:08:50 -07001807 @Override
1808 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001809 // Software menu key (toolbar key)
1810 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1811 return true;
1812 }
1813
1814 @Override
1815 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001816 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001817 // hold on to the menu reference here; it is used by the page callbacks
1818 // to update the menu based on loading state
1819 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001820 // Note: setVisible will decide whether an item is visible; while
1821 // setEnabled() will decide whether an item is enabled, which also means
1822 // whether the matching shortcut key will function.
1823 switch (mMenuState) {
1824 case EMPTY_MENU:
1825 if (mCurrentMenuState != mMenuState) {
1826 menu.setGroupVisible(R.id.MAIN_MENU, false);
1827 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1828 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1829 }
1830 break;
1831 default:
1832 if (mCurrentMenuState != mMenuState) {
1833 menu.setGroupVisible(R.id.MAIN_MENU, true);
1834 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1835 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1836 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001837 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001838 break;
1839 }
1840 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001841 mUi.onPrepareOptionsMenu(menu);
1842 }
1843
1844 private void setMenuItemVisibility(Menu menu, int id,
1845 boolean visibility) {
1846 MenuItem item = menu.findItem(id);
1847 if (item != null) {
1848 item.setVisible(visibility);
1849 }
1850 }
1851
1852 private int lookupBookmark(String title, String url) {
1853 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001854 int count = 0;
1855 Cursor cursor = null;
1856 try {
1857 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1858 BookmarksLoader.PROJECTION,
1859 "title = ? OR url = ?",
1860 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001861 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001862 },
1863 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001864
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001865 if (cursor != null)
1866 count = cursor.getCount();
1867
1868 } catch (IllegalStateException e) {
1869 Log.e(LOGTAG, "lookupBookmark ", e);
1870 } finally {
1871 if (null != cursor) {
1872 cursor.close();
1873 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001874 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001875 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001876 }
1877
1878 private void resetMenuItems(Menu menu) {
1879 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1880 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1881
1882 WebView w = getCurrentTopWebView();
1883 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1884
1885 String title = w.getTitle();
1886 String url = w.getUrl();
1887 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1888 bookmark_icon.setChecked(true);
1889 } else {
1890 bookmark_icon.setChecked(false);
1891 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001892 }
1893
Michael Kolb4bf79712011-07-14 14:18:12 -07001894 @Override
1895 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001896 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001897 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001898 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001899 // Following flag is used to identify schemes for which the LIVE_MENU
1900 // items defined in res/menu/browser.xml should be enabled
1901 boolean isLiveScheme = false;
1902 boolean isPageFinished = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001903
1904 resetMenuItems(menu);
1905
Michael Kolb4bf79712011-07-14 14:18:12 -07001906 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001907 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001908 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001909 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001910 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001911 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Michael Kolb4bf79712011-07-14 14:18:12 -07001912 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001913
1914 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1915 forward.setEnabled(canGoForward);
1916
Michael Kolb4bf79712011-07-14 14:18:12 -07001917 // decide whether to show the share link option
1918 PackageManager pm = mActivity.getPackageManager();
1919 Intent send = new Intent(Intent.ACTION_SEND);
1920 send.setType("text/plain");
1921 ResolveInfo ri = pm.resolveActivity(send,
1922 PackageManager.MATCH_DEFAULT_ONLY);
1923 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1924
1925 boolean isNavDump = mSettings.enableNavDump();
1926 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1927 nav.setVisible(isNavDump);
1928 nav.setEnabled(isNavDump);
1929
1930 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001931 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1932 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001933 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08001934 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
1935 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001936 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08001937 setMenuItemVisibility(menu, R.id.add_to_homescreen,
1938 isLive && isLiveScheme && isPageFinished);
1939 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
1940 isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001941 // history and snapshots item are the members of COMBO menu group,
1942 // so if show history item, only make snapshots item invisible.
1943 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001944
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001945 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001946 }
1947
John Reck9c35b9c2012-05-30 10:08:50 -07001948 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001949 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001950 if (null == getCurrentTopWebView()) {
1951 return false;
1952 }
1953 if (mMenuIsDown) {
1954 // The shortcut action consumes the MENU. Even if it is still down,
1955 // it won't trigger the next shortcut action. In the case of the
1956 // shortcut action triggering a new activity, like Bookmarks, we
1957 // won't get onKeyUp for MENU. So it is important to reset it here.
1958 mMenuIsDown = false;
1959 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001960 if (mUi.onOptionsItemSelected(item)) {
1961 // ui callback handled it
1962 return true;
1963 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001964 switch (item.getItemId()) {
1965 // -- Main menu
1966 case R.id.new_tab_menu_id:
1967 openTabToHomePage();
1968 break;
1969
1970 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001971 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001972 break;
1973
1974 case R.id.goto_menu_id:
1975 editUrl();
1976 break;
1977
1978 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001979 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1980 break;
1981
1982 case R.id.history_menu_id:
1983 bookmarksOrHistoryPicker(ComboViews.History);
1984 break;
1985
1986 case R.id.snapshots_menu_id:
1987 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001988 break;
1989
Pankaj Garg49b79252014-11-07 17:33:41 -08001990 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001991 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 break;
1993
1994 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001995 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001996 stopLoading();
1997 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001998 Tab currentTab = mTabControl.getCurrentTab();
1999 if (currentTab.hasCrashed) {
2000 currentTab.replaceCrashView(getCurrentTopWebView(),
2001 currentTab.getViewContainer());
2002 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002003 getCurrentTopWebView().reload();
2004 }
2005 break;
2006
Michael Kolb8233fac2010-10-26 16:08:53 -07002007 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002008 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002009 break;
2010
2011 case R.id.close_menu_id:
2012 // Close the subwindow if it exists.
2013 if (mTabControl.getCurrentSubWindow() != null) {
2014 dismissSubWindow(mTabControl.getCurrentTab());
2015 break;
2016 }
2017 closeCurrentTab();
2018 break;
2019
kaiyiza8b6dbb2013-07-29 18:11:22 +08002020 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002021 Object[] params = { new String("persist.debug.browsermonkeytest")};
2022 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002023 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002024 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002025 if (ret != null && ret.equals("enable"))
2026 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002027 if (BrowserConfig.getInstance(getContext())
2028 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2029 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002030 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002031 case R.id.homepage_menu_id:
2032 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002033 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002034 break;
2035
2036 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002037 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002038 break;
2039
2040 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002041 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 break;
2043
John Reck2bc80422011-06-30 15:11:49 -07002044 case R.id.save_snapshot_menu_id:
2045 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002046 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002047 createScreenshotAsync(
2048 source.getWebView(),
2049 getDesiredThumbnailWidth(mActivity),
2050 getDesiredThumbnailHeight(mActivity),
2051 new ValueCallback<Bitmap>() {
2052 @Override
2053 public void onReceiveValue(Bitmap bitmap) {
2054 new SaveSnapshotTask(source, bitmap).execute();
2055 }
2056 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002057 break;
2058
Michael Kolb8233fac2010-10-26 16:08:53 -07002059 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002060 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002061 break;
2062
John Recke1a03a32011-09-14 17:04:16 -07002063 case R.id.snapshot_go_live:
2064 goLive();
2065 return true;
2066
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 case R.id.share_page_menu_id:
2068 Tab currentTab = mTabControl.getCurrentTab();
2069 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002070 return false;
2071 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002072 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002073 break;
2074
2075 case R.id.dump_nav_menu_id:
2076 getCurrentTopWebView().debugDump();
2077 break;
2078
Michael Kolb8233fac2010-10-26 16:08:53 -07002079 case R.id.zoom_in_menu_id:
2080 getCurrentTopWebView().zoomIn();
2081 break;
2082
2083 case R.id.zoom_out_menu_id:
2084 getCurrentTopWebView().zoomOut();
2085 break;
2086
2087 case R.id.view_downloads_menu_id:
2088 viewDownloads();
2089 break;
2090
John Reck42229bc2011-08-19 13:26:43 -07002091 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002092 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002093 break;
2094
Michael Kolb8233fac2010-10-26 16:08:53 -07002095 case R.id.window_one_menu_id:
2096 case R.id.window_two_menu_id:
2097 case R.id.window_three_menu_id:
2098 case R.id.window_four_menu_id:
2099 case R.id.window_five_menu_id:
2100 case R.id.window_six_menu_id:
2101 case R.id.window_seven_menu_id:
2102 case R.id.window_eight_menu_id:
2103 {
2104 int menuid = item.getItemId();
2105 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2106 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2107 Tab desiredTab = mTabControl.getTab(id);
2108 if (desiredTab != null &&
2109 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002110 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002111 }
2112 break;
2113 }
2114 }
2115 }
2116 break;
2117
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002118 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002119 Bundle bundle = new Bundle();
2120 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2121 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2122 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2123 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2124 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002125 break;
2126
Pankaj Garg49b79252014-11-07 17:33:41 -08002127 case R.id.add_to_homescreen:
2128 final WebView w = getCurrentTopWebView();
2129 final EditText input = new EditText(getContext());
2130 input.setText(w.getTitle());
2131 new AlertDialog.Builder(getContext())
2132 .setTitle("Add to homescreen")
2133 .setMessage("Title")
2134 .setView(input)
2135 .setPositiveButton("Add", new DialogInterface.OnClickListener() {
2136 public void onClick(DialogInterface dialog, int whichButton) {
2137 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2138 getContext(),
2139 w.getUrl(),
2140 input.getText().toString(),
2141 w.getViewportBitmap(),
2142 w.getFavicon()));
2143
2144 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2145 .addCategory(Intent.CATEGORY_HOME));
2146 }})
2147 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
2148 public void onClick(DialogInterface dialog, int whichButton) {
2149 // Do nothing.
2150 }
2151 })
2152 .show();
2153 break;
2154
Michael Kolb8233fac2010-10-26 16:08:53 -07002155 default:
2156 return false;
2157 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002158 return true;
2159 }
2160
John Reck68234a92012-04-19 15:27:12 -07002161 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2162 implements OnCancelListener {
2163
2164 private Tab mTab;
2165 private Dialog mProgressDialog;
2166 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002167 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002168
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002169 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002170 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002171 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002172 }
2173
2174 @Override
2175 protected void onPreExecute() {
2176 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2177 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2178 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002179 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002180 }
2181
2182 @Override
2183 protected Long doInBackground(Void... params) {
2184 if (!mTab.saveViewState(mValues)) {
2185 return null;
2186 }
2187 if (isCancelled()) {
2188 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2189 File file = mActivity.getFileStreamPath(path);
2190 if (!file.delete()) {
2191 file.deleteOnExit();
2192 }
2193 return null;
2194 }
2195 final ContentResolver cr = mActivity.getContentResolver();
2196 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2197 if (result == null) {
2198 return null;
2199 }
2200 long id = ContentUris.parseId(result);
2201 return id;
2202 }
2203
2204 @Override
2205 protected void onPostExecute(Long id) {
2206 if (isCancelled()) {
2207 return;
2208 }
2209 mProgressDialog.dismiss();
2210 if (id == null) {
2211 Toast.makeText(mActivity, R.string.snapshot_failed,
2212 Toast.LENGTH_SHORT).show();
2213 return;
2214 }
2215 Bundle b = new Bundle();
2216 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2217 mUi.showComboView(ComboViews.Snapshots, b);
2218 }
2219
2220 @Override
2221 public void onCancel(DialogInterface dialog) {
2222 cancel(true);
2223 }
2224 }
2225
Michael Kolb80f75082012-04-10 10:50:06 -07002226 @Override
2227 public void toggleUserAgent() {
2228 WebView web = getCurrentWebView();
2229 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002230 }
2231
2232 @Override
2233 public void findOnPage() {
2234 getCurrentTopWebView().showFindDialog(null, true);
2235 }
2236
2237 @Override
2238 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002239 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002240 }
2241
2242 @Override
2243 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002244 WebView w = getCurrentTopWebView();
2245 if (w == null)
2246 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002247 final Intent i = createBookmarkCurrentPageIntent(false);
2248 createScreenshotAsync(
2249 w, getDesiredThumbnailWidth(mActivity),
2250 getDesiredThumbnailHeight(mActivity),
2251 new ValueCallback<Bitmap>() {
2252 @Override
2253 public void onReceiveValue(Bitmap bitmap) {
2254 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2255 mActivity.startActivity(i);
2256 }
2257 });
Michael Kolb80f75082012-04-10 10:50:06 -07002258 }
2259
John Recke1a03a32011-09-14 17:04:16 -07002260 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002261 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002262 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002263 boolean onlySingleTabRemaining = false;
2264 if (mTabControl.getTabCount() > 1) {
2265 // destroy the old snapshot tab
2266 closeCurrentTab();
2267 } else {
2268 onlySingleTabRemaining = true;
2269 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002270 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002271 if (onlySingleTabRemaining) {
2272 closeTab(t);
2273 }
2274
Tarun Nainani8eb00912014-07-17 12:28:32 -07002275 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002276 }
2277
luxiaolb40014b2013-07-19 10:01:43 +08002278 private void showExitDialog(final Activity activity) {
2279 new AlertDialog.Builder(activity)
2280 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002281 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002282 .setMessage(R.string.exit_browser_msg)
2283 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2284 public void onClick(DialogInterface dialog, int which) {
2285 activity.moveTaskToBack(true);
2286 dialog.dismiss();
2287 }
2288 })
2289 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2290 public void onClick(DialogInterface dialog, int which) {
2291 activity.finish();
2292 mHandler.postDelayed(new Runnable() {
2293 @Override
2294 public void run() {
luxiaolb40014b2013-07-19 10:01:43 +08002295 mCrashRecoveryHandler.clearState(true);
2296 int pid = android.os.Process.myPid();
2297 android.os.Process.killProcess(pid);
2298 }
2299 }, 300);
2300 dialog.dismiss();
2301 }
2302 })
2303 .show();
2304 }
Michael Kolb315d5022011-10-13 12:47:11 -07002305 @Override
2306 public void showPageInfo() {
2307 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2308 }
2309
John Reck9c35b9c2012-05-30 10:08:50 -07002310 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002311 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002312 // Let the History and Bookmark fragments handle menus they created.
2313 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2314 return false;
2315 }
2316
Michael Kolb8233fac2010-10-26 16:08:53 -07002317 int id = item.getItemId();
2318 boolean result = true;
2319 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002320 // -- Browser context menu
2321 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002322 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002323 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002324 case R.id.copy_link_context_menu_id:
2325 final WebView webView = getCurrentTopWebView();
2326 if (null == webView) {
2327 result = false;
2328 break;
2329 }
2330 final HashMap<String, WebView> hrefMap =
2331 new HashMap<String, WebView>();
2332 hrefMap.put("webview", webView);
2333 final Message msg = mHandler.obtainMessage(
2334 FOCUS_NODE_HREF, id, 0, hrefMap);
2335 webView.requestFocusNodeHref(msg);
2336 break;
2337
2338 default:
2339 // For other context menus
2340 result = onOptionsItemSelected(item);
2341 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002342 return result;
2343 }
2344
2345 /**
2346 * support programmatically opening the context menu
2347 */
2348 public void openContextMenu(View view) {
2349 mActivity.openContextMenu(view);
2350 }
2351
2352 /**
2353 * programmatically open the options menu
2354 */
2355 public void openOptionsMenu() {
2356 mActivity.openOptionsMenu();
2357 }
2358
John Reck9c35b9c2012-05-30 10:08:50 -07002359 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002360 public boolean onMenuOpened(int featureId, Menu menu) {
2361 if (mOptionsMenuOpen) {
2362 if (mConfigChanged) {
2363 // We do not need to make any changes to the state of the
2364 // title bar, since the only thing that happened was a
2365 // change in orientation
2366 mConfigChanged = false;
2367 } else {
2368 if (!mExtendedMenuOpen) {
2369 mExtendedMenuOpen = true;
2370 mUi.onExtendedMenuOpened();
2371 } else {
2372 // Switching the menu back to icon view, so show the
2373 // title bar once again.
2374 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002375 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002376 }
2377 }
2378 } else {
2379 // The options menu is closed, so open it, and show the title
2380 mOptionsMenuOpen = true;
2381 mConfigChanged = false;
2382 mExtendedMenuOpen = false;
2383 mUi.onOptionsMenuOpened();
2384 }
2385 return true;
2386 }
2387
John Reck9c35b9c2012-05-30 10:08:50 -07002388 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002389 public void onOptionsMenuClosed(Menu menu) {
2390 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002391 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002392 }
2393
John Reck9c35b9c2012-05-30 10:08:50 -07002394 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002395 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002396 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002397 }
2398
2399 // Helper method for getting the top window.
2400 @Override
2401 public WebView getCurrentTopWebView() {
2402 return mTabControl.getCurrentTopWebView();
2403 }
2404
2405 @Override
2406 public WebView getCurrentWebView() {
2407 return mTabControl.getCurrentWebView();
2408 }
2409
2410 /*
2411 * This method is called as a result of the user selecting the options
2412 * menu to see the download window. It shows the download window on top of
2413 * the current window.
2414 */
2415 void viewDownloads() {
2416 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2417 mActivity.startActivity(intent);
2418 }
2419
John Reck30b065e2011-07-19 10:58:05 -07002420 int getActionModeHeight() {
2421 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2422 new int[] { android.R.attr.actionBarSize });
2423 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2424 actionBarSizeTypedArray.recycle();
2425 return size;
2426 }
2427
Michael Kolb8233fac2010-10-26 16:08:53 -07002428 // action mode
2429
John Reck9c35b9c2012-05-30 10:08:50 -07002430 @Override
2431 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002432 mUi.onActionModeStarted(mode);
2433 mActionMode = mode;
2434 }
2435
2436 /*
2437 * True if a custom ActionMode (i.e. find or select) is in use.
2438 */
2439 @Override
2440 public boolean isInCustomActionMode() {
2441 return mActionMode != null;
2442 }
2443
2444 /*
2445 * End the current ActionMode.
2446 */
2447 @Override
2448 public void endActionMode() {
2449 if (mActionMode != null) {
2450 mActionMode.finish();
2451 }
2452 }
2453
2454 /*
2455 * Called by find and select when they are finished. Replace title bars
2456 * as necessary.
2457 */
John Reck9c35b9c2012-05-30 10:08:50 -07002458 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002459 public void onActionModeFinished(ActionMode mode) {
2460 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002461 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002462 mActionMode = null;
2463 }
2464
2465 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002466 final Tab tab = getCurrentTab();
2467 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002468 }
2469
2470 // bookmark handling
2471
2472 /**
2473 * add the current page as a bookmark to the given folder id
2474 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002475 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002476 * bookmarked, and if it is, edit that bookmark. If false, and
2477 * the site is already bookmarked, do not attempt to edit the
2478 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002479 */
2480 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002481 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002482 WebView w = getCurrentTopWebView();
2483 if (w == null) {
2484 return null;
2485 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002486 Intent i = new Intent(mActivity,
2487 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002488 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2489 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2490 String touchIconUrl = w.getTouchIconUrl();
2491 if (touchIconUrl != null) {
2492 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2493 WebSettings settings = w.getSettings();
2494 if (settings != null) {
2495 i.putExtra(AddBookmarkPage.USER_AGENT,
2496 settings.getUserAgentString());
2497 }
2498 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002499 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002500 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002501 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002502 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2503 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002504 // Put the dialog at the upper right of the screen, covering the
2505 // star on the title bar.
2506 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002507 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002508 }
2509
2510 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002511 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002512 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002513 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002514 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002515 }
2516
Vivek Sekharb54614f2014-05-01 19:03:37 -07002517 @Override
2518 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2519 boolean capture) {
2520 mUploadHandler = new UploadHandler(this);
2521 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2522 }
2523
Michael Kolb8233fac2010-10-26 16:08:53 -07002524 // thumbnails
2525
2526 /**
2527 * Return the desired width for thumbnail screenshots, which are stored in
2528 * the database, and used on the bookmarks screen.
2529 * @param context Context for finding out the density of the screen.
2530 * @return desired width for thumbnail screenshot.
2531 */
2532 static int getDesiredThumbnailWidth(Context context) {
2533 return context.getResources().getDimensionPixelOffset(
2534 R.dimen.bookmarkThumbnailWidth);
2535 }
2536
2537 /**
2538 * Return the desired height for thumbnail screenshots, which are stored in
2539 * the database, and used on the bookmarks screen.
2540 * @param context Context for finding out the density of the screen.
2541 * @return desired height for thumbnail screenshot.
2542 */
2543 static int getDesiredThumbnailHeight(Context context) {
2544 return context.getResources().getDimensionPixelOffset(
2545 R.dimen.bookmarkThumbnailHeight);
2546 }
2547
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002548 static void createScreenshotAsync(WebView view, int width, int height,
2549 final ValueCallback<Bitmap> cb) {
2550 if (view == null || width == 0 || height == 0) {
2551 return;
2552 }
2553
2554 view.getContentBitmapAsync(
2555 (float) width / view.getWidth(),
2556 new Rect(),
2557 new ValueCallback<Bitmap>() {
2558 @Override
2559 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002560 if (bitmap != null)
2561 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2562 cb.onReceiveValue(bitmap);
2563 }});
2564 }
2565
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002566 private void updateScreenshot(final Tab tab) {
2567 createScreenshotAsync(
2568 tab.getWebView(),
2569 getDesiredThumbnailWidth(mActivity),
2570 getDesiredThumbnailHeight(mActivity),
2571 new ValueCallback<Bitmap>() {
2572 @Override
2573 public void onReceiveValue(Bitmap bitmap) {
2574 updateScreenshot(tab, bitmap);
2575 }
2576 });
2577 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002578
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002579 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002581 // FIXME: Would like to make sure there is actually something to
2582 // draw, but the API for that (WebViewCore.pictureReady()) is not
2583 // currently accessible here.
2584
John Reck34ef2672011-02-10 11:30:55 -08002585 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002586 if (view == null) {
2587 // Tab was destroyed
2588 return;
2589 }
John Reck34ef2672011-02-10 11:30:55 -08002590 final String url = tab.getUrl();
2591 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002592 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002593 if (TextUtils.isEmpty(url)) {
2594 return;
2595 }
2596
kaiyiz6e5b3e02013-08-19 20:02:01 +08002597 //update My Navigation Thumbnails
2598 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2599 if (isMyNavigationUrl) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002600 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002601 }
John Reck34ef2672011-02-10 11:30:55 -08002602 // Only update thumbnails for web urls (http(s)://), not for
2603 // about:, javascript:, data:, etc...
2604 // Unless it is a bookmarked site, then always update
2605 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2606 return;
2607 }
2608
kaiyiz6e5b3e02013-08-19 20:02:01 +08002609 if (url != null && mUpdateMyNavThumbnailUrl != null
2610 && Patterns.WEB_URL.matcher(url).matches()
2611 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2612 String urlHost = (new WebAddress(url)).getHost();
2613 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2614 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2615 || bookmarkHost.length() == 0) {
2616 return;
2617 }
2618 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2619 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2620 bookmarkHost.length());
2621 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2622 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2623 if (!bookmarkDomain.equals(urlDomain)) {
2624 return;
2625 }
2626 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002627 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002628 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2629 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2630 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2631 500);
2632 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002633 return;
2634 }
2635
2636 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002637 new AsyncTask<Void, Void, Void>() {
2638 @Override
2639 protected Void doInBackground(Void... unused) {
2640 Cursor cursor = null;
2641 try {
2642 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002643 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2644 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2645 : url);
2646 if (mUpdateMyNavThumbnail) {
2647 mUpdateMyNavThumbnail = false;
2648 mUpdateMyNavThumbnailUrl = null;
2649 }
John Reck34ef2672011-02-10 11:30:55 -08002650 if (cursor != null && cursor.moveToFirst()) {
2651 final ByteArrayOutputStream os =
2652 new ByteArrayOutputStream();
2653 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002654
John Reck34ef2672011-02-10 11:30:55 -08002655 ContentValues values = new ContentValues();
2656 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002657
John Reck34ef2672011-02-10 11:30:55 -08002658 do {
John Reck617fd832011-02-16 14:35:59 -08002659 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002660 cr.update(Images.CONTENT_URI, values, null, null);
2661 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002662 }
John Reck34ef2672011-02-10 11:30:55 -08002663 } catch (IllegalStateException e) {
2664 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002665 } catch (SQLiteException s) {
2666 // Added for possible error when user tries to remove the same bookmark
2667 // that is being updated with a screen shot
2668 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002669 } finally {
2670 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002671 }
John Reck34ef2672011-02-10 11:30:55 -08002672 return null;
2673 }
2674 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002675 }
2676
2677 private class Copy implements OnMenuItemClickListener {
2678 private CharSequence mText;
2679
John Reck9c35b9c2012-05-30 10:08:50 -07002680 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002681 public boolean onMenuItemClick(MenuItem item) {
2682 copy(mText);
2683 return true;
2684 }
2685
2686 public Copy(CharSequence toCopy) {
2687 mText = toCopy;
2688 }
2689 }
2690
Leon Scroggins63c02662010-11-18 15:16:27 -05002691 private static class Download implements OnMenuItemClickListener {
2692 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002693 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002694 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002695 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002696 private static final String FALLBACK_EXTENSION = "dat";
2697 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002698
John Reck9c35b9c2012-05-30 10:08:50 -07002699 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002700 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002701 if (DataUri.isDataUri(mText)) {
2702 saveDataUri();
2703 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002704 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002705 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002706 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002707 return true;
2708 }
2709
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002710 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2711 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002712 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002713 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002714 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002715 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002716 }
George Mount387d45d2011-10-07 15:57:53 -07002717
2718 /**
2719 * Treats mText as a data URI and writes its contents to a file
2720 * based on the current time.
2721 */
2722 private void saveDataUri() {
2723 FileOutputStream outputStream = null;
2724 try {
2725 DataUri uri = new DataUri(mText);
2726 File target = getTarget(uri);
2727 outputStream = new FileOutputStream(target);
2728 outputStream.write(uri.getData());
2729 final DownloadManager manager =
2730 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2731 manager.addCompletedDownload(target.getName(),
2732 mActivity.getTitle().toString(), false,
2733 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002734 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002735 } catch (IOException e) {
2736 Log.e(LOGTAG, "Could not save data URL");
2737 } finally {
2738 if (outputStream != null) {
2739 try {
2740 outputStream.close();
2741 } catch (IOException e) {
2742 // ignore close errors
2743 }
2744 }
2745 }
2746 }
2747
2748 /**
2749 * Creates a File based on the current time stamp and uses
2750 * the mime type of the DataUri to get the extension.
2751 */
2752 private File getTarget(DataUri uri) throws IOException {
2753 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002754 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002755 String nameBase = format.format(new Date());
2756 String mimeType = uri.getMimeType();
2757 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2758 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2759 if (extension == null) {
2760 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2761 extension = FALLBACK_EXTENSION;
2762 }
2763 extension = "." + extension; // createTempFile needs the '.'
2764 File targetFile = File.createTempFile(nameBase, extension, dir);
2765 return targetFile;
2766 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002767 }
2768
Cary Clark8974d282010-11-22 10:46:05 -05002769 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002770 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002771
John Reck9c35b9c2012-05-30 10:08:50 -07002772 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002773 public boolean onMenuItemClick(MenuItem item) {
2774 if (mWebView != null) {
2775 return mWebView.selectText();
2776 }
2777 return false;
2778 }
2779
2780 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002781 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002782 }
2783
2784 }
2785
Michael Kolb8233fac2010-10-26 16:08:53 -07002786 /********************** TODO: UI stuff *****************************/
2787
2788 // these methods have been copied, they still need to be cleaned up
2789
2790 /****************** tabs ***************************************************/
2791
2792 // basic tab interactions:
2793
2794 // it is assumed that tabcontrol already knows about the tab
2795 protected void addTab(Tab tab) {
2796 mUi.addTab(tab);
2797 }
2798
2799 protected void removeTab(Tab tab) {
2800 mUi.removeTab(tab);
2801 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002802 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002803 }
2804
Michael Kolbf2055602011-04-09 17:20:03 -07002805 @Override
2806 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002807 // monkey protection against delayed start
2808 if (tab != null) {
2809 mTabControl.setCurrentTab(tab);
2810 // the tab is guaranteed to have a webview after setCurrentTab
2811 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002812 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002813 //Purge active tabs
2814 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002815 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002816 }
2817
John Reck8bcafc12011-08-29 16:43:02 -07002818 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002819 Tab current = mTabControl.getCurrentTab();
2820 if (current != null
2821 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002822 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002823 }
2824 }
2825
John Reck26b18322011-06-21 13:08:58 -07002826 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002827 // Dismiss the subwindow if applicable.
2828 dismissSubWindow(appTab);
2829 // Since we might kill the WebView, remove it from the
2830 // content view first.
2831 mUi.detachTab(appTab);
2832 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002833 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002834 // TODO: analyze why the remove and add are necessary
2835 mUi.attachTab(appTab);
2836 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002837 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002838 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002839 } else {
2840 // If the tab was the current tab, we have to attach
2841 // it to the view system again.
2842 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002843 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002844 }
2845 }
2846
2847 // Remove the sub window if it exists. Also called by TabControl when the
2848 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002849 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002850 public void dismissSubWindow(Tab tab) {
2851 removeSubWindow(tab);
2852 // dismiss the subwindow. This will destroy the WebView.
2853 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002854 WebView wv = getCurrentTopWebView();
2855 if (wv != null) {
2856 wv.requestFocus();
2857 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002858 }
2859
2860 @Override
2861 public void removeSubWindow(Tab t) {
2862 if (t.getSubWebView() != null) {
2863 mUi.removeSubWindow(t.getSubViewContainer());
2864 }
2865 }
2866
2867 @Override
2868 public void attachSubWindow(Tab tab) {
2869 if (tab.getSubWebView() != null) {
2870 mUi.attachSubWindow(tab.getSubViewContainer());
2871 getCurrentTopWebView().requestFocus();
2872 }
2873 }
2874
Mathew Inwood29721c22011-06-29 17:55:24 +01002875 private Tab showPreloadedTab(final UrlData urlData) {
2876 if (!urlData.isPreloaded()) {
2877 return null;
2878 }
2879 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2880 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2881 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002882 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002883 // Could not submit query. Fallback to regular tab creation
2884 tabControl.destroy();
2885 return null;
2886 }
2887 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002888 // check tab count and make room for new tab
2889 if (!mTabControl.canCreateNewTab()) {
2890 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2891 if (leastUsed != null) {
2892 closeTab(leastUsed);
2893 }
2894 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002895 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002896 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002897 mTabControl.addPreloadedTab(t);
2898 addTab(t);
2899 setActiveTab(t);
2900 return t;
2901 }
2902
Michael Kolb7bcafde2011-05-09 13:55:59 -07002903 // open a non inconito tab with the given url data
2904 // and set as active tab
2905 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002906 Tab tab = showPreloadedTab(urlData);
2907 if (tab == null) {
2908 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002909 if ((tab != null) && !urlData.isEmpty()) {
2910 loadUrlDataIn(tab, urlData);
2911 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002912 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002913 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002914 }
2915
Michael Kolb843510f2010-12-09 10:51:49 -08002916 @Override
2917 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002918 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002919 }
2920
Michael Kolb8233fac2010-10-26 16:08:53 -07002921 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002922 public Tab openIncognitoTab() {
2923 return openTab(INCOGNITO_URI, true, true, false);
2924 }
2925
2926 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002927 public Tab openTab(String url, boolean incognito, boolean setActive,
2928 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002929 return openTab(url, incognito, setActive, useCurrent, null);
2930 }
2931
2932 @Override
2933 public Tab openTab(String url, Tab parent, boolean setActive,
2934 boolean useCurrent) {
2935 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2936 setActive, useCurrent, parent);
2937 }
2938
2939 public Tab openTab(String url, boolean incognito, boolean setActive,
2940 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002941 Tab tab = createNewTab(incognito, setActive, useCurrent);
2942 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002943 if (parent instanceof SnapshotTab) {
2944 addTab(tab);
2945 if (setActive)
2946 setActiveTab(tab);
2947 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002948 parent.addChildTab(tab);
2949 }
Michael Kolb519d2282011-05-09 17:03:19 -07002950 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002951 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002952 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002953 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002954 return tab;
2955 }
2956
2957 // this method will attempt to create a new tab
2958 // incognito: private browsing tab
2959 // setActive: ste tab as current tab
2960 // useCurrent: if no new tab can be created, return current tab
2961 private Tab createNewTab(boolean incognito, boolean setActive,
2962 boolean useCurrent) {
2963 Tab tab = null;
2964 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05002965 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002966 addTab(tab);
2967 if (setActive) {
2968 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002969 } else {
2970 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002971 }
2972 } else {
2973 if (useCurrent) {
2974 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002975 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002976 } else {
2977 mUi.showMaxTabsWarning();
2978 }
2979 }
2980 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002981 }
2982
John Reck2bc80422011-06-30 15:11:49 -07002983 @Override
2984 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2985 SnapshotTab tab = null;
2986 if (mTabControl.canCreateNewTab()) {
2987 tab = mTabControl.createSnapshotTab(snapshotId);
2988 addTab(tab);
2989 if (setActive) {
2990 setActiveTab(tab);
2991 }
2992 } else {
2993 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002994 }
2995 return tab;
2996 }
2997
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002999 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003000 * @return boolean True if we successfully switched to a different tab. If
3001 * the indexth tab is null, or if that tab is the same as
3002 * the current one, return false.
3003 */
3004 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003005 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003006 Tab currentTab = mTabControl.getCurrentTab();
3007 if (tab == null || tab == currentTab) {
3008 return false;
3009 }
3010 setActiveTab(tab);
3011 return true;
3012 }
3013
3014 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003015 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003016 closeCurrentTab(false);
3017 }
3018
3019 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003020 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003021 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003022 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003023 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003024 return;
3025 }
Michael Kolbc831b632011-05-11 09:30:34 -07003026 final Tab current = mTabControl.getCurrentTab();
3027 final int pos = mTabControl.getCurrentPosition();
3028 Tab newTab = current.getParent();
3029 if (newTab == null) {
3030 newTab = mTabControl.getTab(pos + 1);
3031 if (newTab == null) {
3032 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003033 }
3034 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003035 if (andQuit) {
3036 mTabControl.setCurrentTab(newTab);
3037 closeTab(current);
3038 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003039 // Close window
3040 closeTab(current);
3041 }
3042 }
3043
3044 /**
3045 * Close the tab, remove its associated title bar, and adjust mTabControl's
3046 * current tab to a valid value.
3047 */
3048 @Override
3049 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003050 if (tab == mTabControl.getCurrentTab()) {
3051 closeCurrentTab();
3052 } else {
3053 removeTab(tab);
3054 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003055 }
3056
Afzal Najamd4e33312012-04-26 01:54:01 -04003057 /**
3058 * Close all tabs except the current one
3059 */
3060 @Override
3061 public void closeOtherTabs() {
3062 int inactiveTabs = mTabControl.getTabCount() - 1;
3063 for (int i = inactiveTabs; i >= 0; i--) {
3064 Tab tab = mTabControl.getTab(i);
3065 if (tab != mTabControl.getCurrentTab()) {
3066 removeTab(tab);
3067 }
3068 }
3069 }
3070
Michael Kolb8233fac2010-10-26 16:08:53 -07003071 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003072 protected void loadUrlFromContext(String url) {
3073 Tab tab = getCurrentTab();
3074 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003076 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003077 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003078 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003079 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003080 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003081 }
3082 }
3083 }
3084
3085 /**
3086 * Load the URL into the given WebView and update the title bar
3087 * to reflect the new load. Call this instead of WebView.loadUrl
3088 * directly.
3089 * @param view The WebView used to load url.
3090 * @param url The URL to load.
3091 */
John Reck71e51422011-07-01 16:49:28 -07003092 @Override
3093 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003094 loadUrl(tab, url, null);
3095 }
3096
3097 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3098 if (tab != null) {
3099 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003100 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003101 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003102 if (tab.hasCrashed) {
3103 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3104 }
Michael Kolba53c9892011-10-05 13:31:40 -07003105 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003106 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003107 }
3108
3109 /**
3110 * Load UrlData into a Tab and update the title bar to reflect the new
3111 * load. Call this instead of UrlData.loadIn directly.
3112 * @param t The Tab used to load.
3113 * @param data The UrlData being loaded.
3114 */
3115 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003116 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003117 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003118 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003119 } else {
John Reck38b39652012-06-05 09:22:59 -07003120 if (t != null && data.mDisableUrlOverride) {
3121 t.disableUrlOverridingForLoad();
3122 }
John Reck26b18322011-06-21 13:08:58 -07003123 loadUrl(t, data.mUrl, data.mHeaders);
3124 }
3125 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003126 }
3127
John Reck30c714c2010-12-16 17:30:34 -08003128 @Override
3129 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003130 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003131 if (tab.canGoBack()) {
3132 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003133 } else {
John Reckef654f12011-07-12 16:42:08 -07003134 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003135 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003136 }
3137
3138 void goBackOnePageOrQuit() {
3139 Tab current = mTabControl.getCurrentTab();
3140 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003141 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3142 showExitDialog(mActivity);
3143 } else {
3144 /*
3145 * Instead of finishing the activity, simply push this to the back
3146 * of the stack and let ActivityManager to choose the foreground
3147 * activity. As BrowserActivity is singleTask, it will be always the
3148 * root of the task. So we can use either true or false for
3149 * moveTaskToBack().
3150 */
3151 mActivity.moveTaskToBack(true);
3152 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003153 return;
3154 }
John Reckef654f12011-07-12 16:42:08 -07003155 if (current.canGoBack()) {
3156 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 } else {
3158 // Check to see if we are closing a window that was created by
3159 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003160 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003161 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003162 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003163 // Now we close the other tab
3164 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003165 } else if (BrowserConfig.getInstance(getContext())
3166 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3167 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003168 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003169 if ((current.getAppId() != null) || current.closeOnBack()) {
3170 closeCurrentTab(true);
3171 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003172 /*
3173 * Instead of finishing the activity, simply push this to the back
3174 * of the stack and let ActivityManager to choose the foreground
3175 * activity. As BrowserActivity is singleTask, it will be always the
3176 * root of the task. So we can use either true or false for
3177 * moveTaskToBack().
3178 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003179 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003180 }
3181 }
3182 }
3183
3184 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003185 * helper method for key handler
3186 * returns the current tab if it can't advance
3187 */
Michael Kolbc831b632011-05-11 09:30:34 -07003188 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003189 int pos = mTabControl.getCurrentPosition() + 1;
3190 if (pos >= mTabControl.getTabCount()) {
3191 pos = 0;
3192 }
3193 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003194 }
3195
3196 /**
3197 * helper method for key handler
3198 * returns the current tab if it can't advance
3199 */
Michael Kolbc831b632011-05-11 09:30:34 -07003200 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003201 int pos = mTabControl.getCurrentPosition() - 1;
3202 if ( pos < 0) {
3203 pos = mTabControl.getTabCount() - 1;
3204 }
3205 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003206 }
3207
John Reckbcef87f2012-02-03 14:58:34 -08003208 boolean isMenuOrCtrlKey(int keyCode) {
3209 return (KeyEvent.KEYCODE_MENU == keyCode)
3210 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3211 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3212 }
3213
Michael Kolb0035fad2011-03-14 13:25:28 -07003214 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003215 * handle key events in browser
3216 *
3217 * @param keyCode
3218 * @param event
3219 * @return true if handled, false to pass to super
3220 */
John Reck9c35b9c2012-05-30 10:08:50 -07003221 @Override
3222 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003223 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3224 // Hardware menu key
Enrico Ros1f5a0952014-11-18 20:15:48 -08003225 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
Pankaj Garg49b79252014-11-07 17:33:41 -08003226 true, false);
3227 return true;
3228 }
3229
Cary Clark160bbb92011-01-10 11:17:07 -05003230 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003231 // Even if MENU is already held down, we need to call to super to open
3232 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003233 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003234 mMenuIsDown = true;
3235 return false;
3236 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003237
Cary Clark8ff8c662010-12-29 15:03:05 -05003238 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003239 Tab tab = getCurrentTab();
3240 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003241
Cary Clark160bbb92011-01-10 11:17:07 -05003242 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3243 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003244
Michael Kolb8233fac2010-10-26 16:08:53 -07003245 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003246 case KeyEvent.KEYCODE_TAB:
3247 if (event.isCtrlPressed()) {
3248 if (event.isShiftPressed()) {
3249 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003250 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003251 } else {
3252 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003253 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003254 }
3255 return true;
3256 }
3257 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003258 case KeyEvent.KEYCODE_SPACE:
3259 // WebView/WebTextView handle the keys in the KeyDown. As
3260 // the Activity's shortcut keys are only handled when WebView
3261 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003262 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003263 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003264 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003265 pageDown();
3266 }
3267 return true;
3268 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003269 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003270 event.startTracking();
3271 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003272 case KeyEvent.KEYCODE_FORWARD:
3273 if (!noModifiers) break;
3274 tab.goForward();
3275 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003276 case KeyEvent.KEYCODE_DPAD_LEFT:
3277 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003278 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003279 return true;
3280 }
3281 break;
3282 case KeyEvent.KEYCODE_DPAD_RIGHT:
3283 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003284 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003285 return true;
3286 }
3287 break;
3288 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003289 if (ctrl) {
3290 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003291 return true;
3292 }
3293 break;
Michael Kolba4183062011-01-16 10:43:21 -08003294// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003295 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003296 if (ctrl ) {
3297 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003298 return true;
3299 }
3300 break;
Michael Kolba4183062011-01-16 10:43:21 -08003301// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003302// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003303// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003304// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003305// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003306// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003307// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003308// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003309// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003310// case KeyEvent.KEYCODE_M: // unused
3311// case KeyEvent.KEYCODE_N: // in Chrome: new window
3312// case KeyEvent.KEYCODE_O: // in Chrome: open file
3313// case KeyEvent.KEYCODE_P: // in Chrome: print page
3314// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003315// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003316// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3317 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003318 // we can't use the ctrl/shift flags, they check for
3319 // exclusive use of a modifier
3320 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003321 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003322 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003323 } else {
3324 openTabToHomePage();
3325 }
3326 return true;
3327 }
3328 break;
3329// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3330// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003331// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003332// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3333// case KeyEvent.KEYCODE_Y: // unused
3334// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003335 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003336 // it is a regular key and webview is not null
3337 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003338 }
3339
John Reck9c35b9c2012-05-30 10:08:50 -07003340 @Override
3341 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003342 switch(keyCode) {
3343 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003344 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003345 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003346 return true;
3347 }
3348 break;
3349 }
3350 return false;
3351 }
3352
John Reck9c35b9c2012-05-30 10:08:50 -07003353 @Override
3354 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003355 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003356 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003357 if (KeyEvent.KEYCODE_MENU == keyCode
3358 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003359 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003360 }
3361 }
Cary Clark160bbb92011-01-10 11:17:07 -05003362 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003363 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003364 case KeyEvent.KEYCODE_BACK:
3365 if (event.isTracking() && !event.isCanceled()) {
3366 onBackKey();
3367 return true;
3368 }
3369 break;
3370 }
3371 return false;
3372 }
3373
3374 public boolean isMenuDown() {
3375 return mMenuIsDown;
3376 }
3377
John Reck9c35b9c2012-05-30 10:08:50 -07003378 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003379 public void setupAutoFill(Message message) {
3380 // Open the settings activity at the AutoFill profile fragment so that
3381 // the user can create a new profile. When they return, we will dispatch
3382 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003383 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003384 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3385 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003386 }
John Reckb3417f02011-01-14 11:01:05 -08003387
John Reck9c35b9c2012-05-30 10:08:50 -07003388 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003389 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003390 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003391 return true;
3392 }
3393
John Reck1cf4b792011-07-26 10:22:22 -07003394 @Override
3395 public boolean shouldCaptureThumbnails() {
3396 return mUi.shouldCaptureThumbnails();
3397 }
3398
Michael Kolbc3af0672011-08-09 10:24:41 -07003399 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003400 public boolean supportsVoice() {
3401 PackageManager pm = mActivity.getPackageManager();
3402 List activities = pm.queryIntentActivities(new Intent(
3403 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3404 return activities.size() != 0;
3405 }
3406
3407 @Override
3408 public void startVoiceRecognizer() {
3409 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003410 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003411 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3412 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3413 mActivity.startActivityForResult(voice, VOICE_RESULT);
3414 }
3415
Pankaj Garg7b279f62014-08-12 14:47:18 -07003416 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003417 if (mLevel == level)
3418 return;
3419 mLevel = level;
3420 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003421 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3422 lp.dimAmount = level;
3423 mActivity.getWindow().setAttributes(lp);
3424 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3425 } else {
3426 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3427 }
3428 }
3429
Michael Kolb0b129122012-06-04 16:31:58 -07003430 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003431 public void setBlockEvents(boolean block) {
3432 mBlockEvents = block;
3433 }
3434
John Reck9c35b9c2012-05-30 10:08:50 -07003435 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003436 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003437 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003438 }
3439
John Reck9c35b9c2012-05-30 10:08:50 -07003440 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003441 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003442 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003443 }
3444
John Reck9c35b9c2012-05-30 10:08:50 -07003445 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003446 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003447 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003448 }
3449
John Reck9c35b9c2012-05-30 10:08:50 -07003450 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003451 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003452 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003453 }
3454
John Reck9c35b9c2012-05-30 10:08:50 -07003455 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003456 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003457 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003458 }
3459
Pankaj Garg49b79252014-11-07 17:33:41 -08003460 @Override
3461 public boolean shouldShowAppMenu() {
3462 return true;
3463 }
3464
3465 @Override
3466 public int getMenuThemeResourceId() {
3467 return R.style.OverflowMenuTheme;
3468 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003469}