blob: 397528dfcb370f70e7b5011d5c32df6868f3e47c [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;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800145 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700146
147 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
148
149 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800150 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700151
152 private static final int EMPTY_MENU = -1;
153
Michael Kolb8233fac2010-10-26 16:08:53 -0700154 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700155 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 final static int PREFERENCES_PAGE = 3;
157 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000158 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700159 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800160 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000161
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
163
164 // As the ids are dynamically created, we can't guarantee that they will
165 // be in sequence, so this static array maps ids to a window number.
166 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
167 { R.id.window_one_menu_id, R.id.window_two_menu_id,
168 R.id.window_three_menu_id, R.id.window_four_menu_id,
169 R.id.window_five_menu_id, R.id.window_six_menu_id,
170 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
171
172 // "source" parameter for Google search through search key
173 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
174 // "source" parameter for Google search through simplily type
175 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
176
George Mount387d45d2011-10-07 15:57:53 -0700177 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700178 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
179
John Reckd7dd9b22011-08-30 09:18:29 -0700180 // A bitmap that is re-used in createScreenshot as scratch space
181 private static Bitmap sThumbnailBitmap;
182
Michael Kolb8233fac2010-10-26 16:08:53 -0700183 private Activity mActivity;
184 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700185 private HomepageHandler mHomepageHandler;
186 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700187 private BrowserSettings mSettings;
188 private WebViewFactory mFactory;
189
190 private WakeLock mWakeLock;
191
192 private UrlHandler mUrlHandler;
193 private UploadHandler mUploadHandler;
194 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 private PageDialogsHandler mPageDialogsHandler;
196 private NetworkStateHandler mNetworkHandler;
197
Ben Murdoch8029a772010-11-16 11:58:21 +0000198 private Message mAutoFillSetupMessage;
199
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;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800254 private WebView.HitTestResult mResult;
Michael Kolb0b129122012-06-04 16:31:58 -0700255
George Mount3636d0a2011-11-21 09:08:21 -0800256 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700257 mActivity = browser;
258 mSettings = BrowserSettings.getInstance();
259 mTabControl = new TabControl(this);
260 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700261 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800262 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700263 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700264
265 mUrlHandler = new UrlHandler(this);
266 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
268
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500270 mBookmarksObserver = new ContentObserver(mHandler) {
271 @Override
272 public void onChange(boolean selfChange) {
273 int size = mTabControl.getTabCount();
274 for (int i = 0; i < size; i++) {
275 mTabControl.getTab(i).updateBookmarkedStatus();
276 }
277 }
278
279 };
280 browser.getContentResolver().registerContentObserver(
281 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700282
283 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700284 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800285 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700286 }
287
John Reck9c35b9c2012-05-30 10:08:50 -0700288 @Override
289 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700290 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800291 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800292 // mCrashRecoverHandler has any previously saved state.
293 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700294 }
295
George Mount3636d0a2011-11-21 09:08:21 -0800296 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800297 // we dont want to ever recover incognito tabs
298 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700299
Patrick Scott7d50a932011-02-04 09:27:26 -0500300 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700301 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500302 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000303
Michael Kolbc831b632011-05-11 09:30:34 -0700304 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500305 // Not able to restore so we go ahead and clear session cookies. We
306 // must do this before trying to login the user as we don't want to
307 // clear any session cookies set during login.
308 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700309 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800310 if (intent == null) {
311 // This won't happen under common scenarios. The icicle is
312 // not null, but there aren't any tabs to restore.
313 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700314 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800315 final Bundle extra = intent.getExtras();
316 // Create an initial tab.
317 // If the intent is ACTION_VIEW and data is not null, the Browser is
318 // invoked to view the content by another application. In this case,
319 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800320 UrlData urlData = null;
321 if (intent.getData() != null
322 && Intent.ACTION_VIEW.equals(intent.getAction())
323 && intent.getData().toString().startsWith("content://")) {
324 urlData = new UrlData(intent.getData().toString());
325 } else {
326 urlData = IntentHandler.getUrlDataFromIntent(intent);
327 }
George Mount3636d0a2011-11-21 09:08:21 -0800328 Tab t = null;
329 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700330 String landingPage = mActivity.getResources().getString(
331 R.string.def_landing_page);
332 if (!landingPage.isEmpty()) {
333 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800334 } else {
335 t = openTabToHomePage();
336 }
George Mount3636d0a2011-11-21 09:08:21 -0800337 } else {
338 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700339 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800340 }
341 if (t != null) {
342 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
343 }
344 WebView webView = t.getWebView();
345 if (extra != null) {
346 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
347 if (scale > 0 && scale <= 1000) {
348 webView.setInitialScale(scale);
349 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700350 }
351 }
John Reckd8c74522011-06-14 08:45:00 -0700352 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700353 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700354 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500355 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700356 List<Tab> tabs = mTabControl.getTabs();
357 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700358
John Reck1cf4b792011-07-26 10:22:22 -0700359 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700360 //handle restored pages that may require a JS interface
361 if (t.getWebView() != null) {
362 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
363 if (backForwardList != null) {
364 for (int i = 0; i < backForwardList.getSize(); i++) {
365 WebHistoryItem item = backForwardList.getItemAtIndex(i);
366 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
367 }
368 }
369 }
John Reck1cf4b792011-07-26 10:22:22 -0700370 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700371 if (t != mTabControl.getCurrentTab()) {
372 t.pause();
373 }
John Reck1cf4b792011-07-26 10:22:22 -0700374 }
375 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700376 if (tabs.size() == 0) {
377 openTabToHomePage();
378 }
John Reck1cf4b792011-07-26 10:22:22 -0700379 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700380 // TabControl.restoreState() will create a new tab even if
381 // restoring the state fails.
382 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800383 // Intent is non-null when framework thinks the browser should be
384 // launching with a new intent (icicle is null).
385 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700386 mIntentHandler.onNewIntent(intent);
387 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700389 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700390 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800391 if (jsFlags.trim().length() != 0) {
392 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 }
George Mount3636d0a2011-11-21 09:08:21 -0800394 if (intent != null
395 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700396 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800397 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700398 }
399
John Reck1cf4b792011-07-26 10:22:22 -0700400 private static class PruneThumbnails implements Runnable {
401 private Context mContext;
402 private List<Long> mIds;
403
404 PruneThumbnails(Context context, List<Long> preserveIds) {
405 mContext = context.getApplicationContext();
406 mIds = preserveIds;
407 }
408
409 @Override
410 public void run() {
411 ContentResolver cr = mContext.getContentResolver();
412 if (mIds == null || mIds.size() == 0) {
413 cr.delete(Thumbnails.CONTENT_URI, null, null);
414 } else {
415 int length = mIds.size();
416 StringBuilder where = new StringBuilder();
417 where.append(Thumbnails._ID);
418 where.append(" not in (");
419 for (int i = 0; i < length; i++) {
420 where.append(mIds.get(i));
421 if (i < (length - 1)) {
422 where.append(",");
423 }
424 }
425 where.append(")");
426 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
427 }
428 }
429
430 }
431
Michael Kolb1514bb72010-11-22 09:11:48 -0800432 @Override
433 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700434 return mFactory;
435 }
436
437 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800438 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800439 if (tab.hasCrashed)
440 tab.showCrashView();
441 else
442 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800443 }
444
445 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800446 public void createSubWindow(Tab tab) {
447 endActionMode();
448 WebView mainView = tab.getWebView();
449 WebView subView = mFactory.createWebView((mainView == null)
450 ? false
451 : mainView.isPrivateBrowsingEnabled());
452 mUi.createSubWindow(tab, subView);
453 }
454
455 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700456 public Context getContext() {
457 return mActivity;
458 }
459
460 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700461 public Activity getActivity() {
462 return mActivity;
463 }
464
465 void setUi(UI ui) {
466 mUi = ui;
467 }
468
Michael Kolbaf63dba2012-05-16 12:58:05 -0700469 @Override
470 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700471 return mSettings;
472 }
473
474 IntentHandler getIntentHandler() {
475 return mIntentHandler;
476 }
477
478 @Override
479 public UI getUi() {
480 return mUi;
481 }
482
483 int getMaxTabs() {
484 return mActivity.getResources().getInteger(R.integer.max_tabs);
485 }
486
487 @Override
488 public TabControl getTabControl() {
489 return mTabControl;
490 }
491
Michael Kolb1bf23132010-11-19 12:55:12 -0800492 @Override
493 public List<Tab> getTabs() {
494 return mTabControl.getTabs();
495 }
496
Michael Kolb8233fac2010-10-26 16:08:53 -0700497 private void startHandler() {
498 mHandler = new Handler() {
499
500 @Override
501 public void handleMessage(Message msg) {
502 switch (msg.what) {
503 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700504 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700505 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800506 case UNKNOWN_TYPE_MSG:
507 HashMap unknownTypeMap = (HashMap) msg.obj;
508 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
509 /*
510 * When the context menu is shown to the user
511 * we need to assure that its happening on the current webview
512 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
513 */
514 if (getCurrentWebView() != viewForUnknownType)
515 break;
516
517 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800518 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800519 WebView.HitTestResult result = new WebView.HitTestResult();
520
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800521 // Prevent unnecessary calls to context menu
522 // if url and image src are null
523 if (unknown_type_src == null && unknown_type_url == null)
524 break;
525
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800526 //setting the HitTestResult with new RESULT TYPE
527 if (!TextUtils.isEmpty(unknown_type_src)) {
528 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
529 result.setExtra(unknown_type_src);
530 } else {
531 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
532 result.setExtra("about:blank");
533 }
534
535 mResult = result;
536 openContextMenu(viewForUnknownType);
537 mResult = null;
538
539 break;
540
Michael Kolb8233fac2010-10-26 16:08:53 -0700541 case FOCUS_NODE_HREF:
542 {
543 String url = (String) msg.getData().get("url");
544 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500545 String src = (String) msg.getData().get("src");
546 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700547 if (TextUtils.isEmpty(url)) {
548 break;
549 }
550 HashMap focusNodeMap = (HashMap) msg.obj;
551 WebView view = (WebView) focusNodeMap.get("webview");
552 // Only apply the action if the top window did not change.
553 if (getCurrentTopWebView() != view) {
554 break;
555 }
556 switch (msg.arg1) {
557 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700558 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700559 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500560 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700561 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500562 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500563 case R.id.open_newtab_context_menu_id:
564 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700565 openTab(url, parent,
566 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500567 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700568 case R.id.copy_link_context_menu_id:
569 copy(url);
570 break;
571 case R.id.save_link_context_menu_id:
572 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500573 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800574 mActivity, url, view.getSettings().getUserAgentString(),
575 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700576 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700577 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700578 if(title == null || title == "")
579 title = url;
580
581 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
582 //SWE TODO: No thumbnail support for the url obtained via
583 //browser context menu as its not loaded in webview.
584 if (bookmarkIntent != null) {
585 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
586 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
587 mActivity.startActivity(bookmarkIntent);
588 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700589 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 }
591 break;
592 }
593
594 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700595 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700596 break;
597
598 case STOP_LOAD:
599 stopLoading();
600 break;
601
602 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700603 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700604 mWakeLock.release();
605 // if we reach here, Browser should be still in the
606 // background loading after WAKELOCK_TIMEOUT (5-min).
607 // To avoid burning the battery, stop loading.
608 mTabControl.stopAllLoading();
609 }
610 break;
611
612 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800613 Tab tab = (Tab) msg.obj;
614 if (tab != null) {
615 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 }
617 break;
kaiyiz591110b2013-08-06 17:11:06 +0800618 case OPEN_MENU:
619 if (!mOptionsMenuOpen && mActivity != null ) {
620 mActivity.openOptionsMenu();
621 }
622 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700623 }
624 }
625 };
626
627 }
628
John Reckef654f12011-07-12 16:42:08 -0700629 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200630 public Tab getCurrentTab() {
631 return mTabControl.getCurrentTab();
632 }
633
Michael Kolbba99c5d2010-11-29 14:57:41 -0800634 @Override
635 public void shareCurrentPage() {
636 shareCurrentPage(mTabControl.getCurrentTab());
637 }
638
639 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700640 if (tab == null || tab.getWebView() == null)
641 return;
642
643 final Tab mytab = tab;
644 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
645 @Override
646 public void onReceiveValue(Bitmap bitmap) {
647 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
648 mytab.getFavicon(), bitmap);
649 }
650 };
651
652 createScreenshotAsync(
653 tab.getWebView(),
654 getDesiredThumbnailWidth(mActivity),
655 getDesiredThumbnailHeight(mActivity),
656 new ValueCallback<Bitmap>() {
657 @Override
658 public void onReceiveValue(Bitmap bitmap) {
659 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
660 mytab.getFavicon(), bitmap);
661 }
662 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800663 }
664
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 /**
666 * Share a page, providing the title, url, favicon, and a screenshot. Uses
667 * an {@link Intent} to launch the Activity chooser.
668 * @param c Context used to launch a new Activity.
669 * @param title Title of the page. Stored in the Intent with
670 * {@link Intent#EXTRA_SUBJECT}
671 * @param url URL of the page. Stored in the Intent with
672 * {@link Intent#EXTRA_TEXT}
673 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
674 * with {@link Browser#EXTRA_SHARE_FAVICON}
675 * @param screenshot Bitmap of a screenshot of the page. Stored in the
676 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
677 */
678 static final void sharePage(Context c, String title, String url,
679 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700680
681 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
682 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
683 R.layout.app_row, sDialog.getApps());
684 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700685 }
686
687 private void copy(CharSequence text) {
688 ClipboardManager cm = (ClipboardManager) mActivity
689 .getSystemService(Context.CLIPBOARD_SERVICE);
690 cm.setText(text);
691 }
692
693 // lifecycle
694
John Reck9c35b9c2012-05-30 10:08:50 -0700695 @Override
696 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800698 // update the menu in case of a locale change
699 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800700 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800701 if (mOptionsMenuOpen) {
702 mActivity.closeOptionsMenu();
703 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
704 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 if (mPageDialogsHandler != null) {
706 mPageDialogsHandler.onConfigurationChanged(config);
707 }
708 mUi.onConfigurationChanged(config);
709 }
710
711 @Override
712 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700713 if (!mUi.isWebShowing()) {
714 mUi.showWeb(false);
715 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700716 mIntentHandler.onNewIntent(intent);
717 }
718
John Reck9c35b9c2012-05-30 10:08:50 -0700719 @Override
720 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800721 if (mUi.isCustomViewShowing()) {
722 hideCustomView();
723 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700724 if (mActivityPaused) {
725 Log.e(LOGTAG, "BrowserActivity is already paused.");
726 return;
727 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700728 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800729 Tab tab = mTabControl.getCurrentTab();
730 if (tab != null) {
731 tab.pause();
732 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700733 if (mWakeLock == null) {
734 PowerManager pm = (PowerManager) mActivity
735 .getSystemService(Context.POWER_SERVICE);
736 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
737 }
Michael Kolb70976932010-11-30 11:34:01 -0800738 mWakeLock.acquire();
739 mHandler.sendMessageDelayed(mHandler
740 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
741 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 }
743 mUi.onPause();
744 mNetworkHandler.onPause();
745
746 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100747 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700748 }
749
John Reck9c35b9c2012-05-30 10:08:50 -0700750 @Override
751 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700752 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800753 Bundle saveState = createSaveState();
754
755 // crash recovery manages all save & restore state
756 mCrashRecoveryHandler.writeState(saveState);
757 mSettings.setLastRunPaused(true);
758 }
759
760 /**
761 * Save the current state to outState. Does not write the state to
762 * disk.
763 * @return Bundle containing the current state of all tabs.
764 */
765 /* package */ Bundle createSaveState() {
766 Bundle saveState = new Bundle();
767 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800768 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700769 }
770
John Reck9c35b9c2012-05-30 10:08:50 -0700771 @Override
772 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700773 if (!mActivityPaused) {
774 Log.e(LOGTAG, "BrowserActivity is already resumed.");
775 return;
776 }
George Mount3636d0a2011-11-21 09:08:21 -0800777 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700778 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800779 Tab current = mTabControl.getCurrentTab();
780 if (current != null) {
781 current.resume();
782 resumeWebViewTimers(current);
783 }
John Reckf57c0292011-07-21 18:15:39 -0700784 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200785
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 mUi.onResume();
787 mNetworkHandler.onResume();
788 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100789 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700790 if (mVoiceResult != null) {
791 mUi.onVoiceResult(mVoiceResult);
792 mVoiceResult = null;
793 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800794 if (current != null && current.hasCrashed) {
Vivek Sekhar2868b8d2014-12-03 17:22:50 -0800795 current.replaceCrashView(current.getWebView(), current.getViewContainer());
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800796 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700797 }
798
John Reckf57c0292011-07-21 18:15:39 -0700799 private void releaseWakeLock() {
800 if (mWakeLock != null && mWakeLock.isHeld()) {
801 mHandler.removeMessages(RELEASE_WAKELOCK);
802 mWakeLock.release();
803 }
804 }
805
Michael Kolb70976932010-11-30 11:34:01 -0800806 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800807 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800808 * @param tab guaranteed non-null
809 */
810 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 boolean inLoad = tab.inPageLoad();
812 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
813 CookieSyncManager.getInstance().startSync();
814 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100815 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700816 }
817 }
818
Michael Kolb70976932010-11-30 11:34:01 -0800819 /**
820 * Pause all WebView timers using the WebView of the given tab
821 * @param tab
822 * @return true if the timers are paused or tab is null
823 */
824 private boolean pauseWebViewTimers(Tab tab) {
825 if (tab == null) {
826 return true;
827 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700828 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100829 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700830 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 }
Michael Kolb70976932010-11-30 11:34:01 -0800832 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700833 }
834
John Reck9c35b9c2012-05-30 10:08:50 -0700835 @Override
836 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800837 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700838 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
839 mUploadHandler = null;
840 }
841 if (mTabControl == null) return;
842 mUi.onDestroy();
843 // Remove the current tab and sub window
844 Tab t = mTabControl.getCurrentTab();
845 if (t != null) {
846 dismissSubWindow(t);
847 removeTab(t);
848 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500849 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 // Destroy all the tabs
851 mTabControl.destroy();
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 }
853
854 protected boolean isActivityPaused() {
855 return mActivityPaused;
856 }
857
John Reck9c35b9c2012-05-30 10:08:50 -0700858 @Override
859 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700860 mTabControl.freeMemory();
861 }
862
863 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 public void stopLoading() {
865 mLoadStopped = true;
866 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700867 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700868 if (w != null) {
869 w.stopLoading();
870 mUi.onPageStopped(tab);
871 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 }
873
874 boolean didUserStopLoading() {
875 return mLoadStopped;
876 }
877
kaiyiza016da12013-08-26 17:50:22 +0800878 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700879 final String reminderType = getContext().getResources().getString(
880 R.string.def_wifi_browser_interaction_remind_type);
881 final String selectionConnnection = getContext().getResources().getString(
882 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700883 final String wifiSelection = getContext().getResources().getString(
884 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700885
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700886 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
887 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700888 return;
889
kaiyiza016da12013-08-26 17:50:22 +0800890 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700891 Context.CONNECTIVITY_SERVICE);
892 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700893 WifiManager wifiMgr = (WifiManager) this.getContext()
894 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700895 if (networkInfo == null
896 || (networkInfo != null && (networkInfo.getType() !=
897 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700898 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
899 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700900 List<ScanResult> list = wifiMgr.getScanResults();
901 // Have no AP's for Wifi's fall back to data
902 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700903 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700904 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
905 this.getContext().startActivity(intent);
906 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700907 // Request to select Wifi AP
908 try {
909 Intent intent = new Intent(wifiSelection);
910 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
911 this.getContext().startActivity(intent);
912 } catch (Exception e) {
913 String err_msg = this.getContext().getString(
914 R.string.acivity_not_found, wifiSelection);
915 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
916 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700917 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700918 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800919 }
920 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700921
Michael Kolb8233fac2010-10-26 16:08:53 -0700922 // WebViewController
923
924 @Override
John Reck324d4402011-01-11 16:56:42 -0800925 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700926
927 // We've started to load a new page. If there was a pending message
928 // to save a screenshot then we will now take the new page and save
929 // an incorrect screenshot. Therefore, remove any pending thumbnail
930 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700931 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700932
933 // reset sync timer to avoid sync starts during loading a page
934 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700935 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700936 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800937 boolean networkNotifier = BrowserConfig.getInstance(getContext())
938 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700939 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700940 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800941 } else {
942 if (!mNetworkHandler.isNetworkUp()) {
943 view.setNetworkAvailable(false);
944 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700945 }
946
947 // when BrowserActivity just starts, onPageStarted may be called before
948 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
949 // to start the timer. As we won't switch tabs while an activity is in
950 // pause state, we can ensure calling resume and pause in pair.
951 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800952 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700953 }
954 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700955 endActionMode();
956
John Reck30c714c2010-12-16 17:30:34 -0800957 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700958
John Reck324d4402011-01-11 16:56:42 -0800959 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700960 // update the bookmark database for favicon
961 maybeUpdateFavicon(tab, null, url, favicon);
962
963 Performance.tracePageStart(url);
964
965 // Performance probe
966 if (false) {
967 Performance.onPageStarted();
968 }
969
970 }
971
972 @Override
John Reck324d4402011-01-11 16:56:42 -0800973 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700974 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800975 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200976
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 // Performance probe
978 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800979 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 }
981
Tarun Nainani8eb00912014-07-17 12:28:32 -0700982 tab.onPageFinished();
983
Michael Kolb8233fac2010-10-26 16:08:53 -0700984 Performance.tracePageFinished();
985 }
986
987 @Override
John Reck30c714c2010-12-16 17:30:34 -0800988 public void onProgressChanged(Tab tab) {
989 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700990
991 if (newProgress == 100) {
992 CookieSyncManager.getInstance().sync();
993 // onProgressChanged() may continue to be called after the main
994 // frame has finished loading, as any remaining sub frames continue
995 // to load. We'll only get called once though with newProgress as
996 // 100 when everything is loaded. (onPageFinished is called once
997 // when the main frame completes loading regardless of the state of
998 // any sub frames so calls to onProgressChanges may continue after
999 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001000 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001001 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001002 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001003 } else if (mWasInPageLoad) {
1004 mWasInPageLoad = false;
1005 updateInLoadMenuItems(mCachedMenu, tab);
1006 }
1007
1008 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001009 // pause the WebView timer and release the wake lock if it is
1010 // finished while BrowserActivity is in pause state.
1011 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001012 }
John Reckd9862372012-02-21 15:04:50 -08001013 if (!tab.isPrivateBrowsingEnabled()
1014 && !TextUtils.isEmpty(tab.getUrl())
1015 && !tab.isSnapshot()) {
1016 // Only update the bookmark screenshot if the user did not
1017 // cancel the load early and there is not already
1018 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001019 if (tab.shouldUpdateThumbnail() &&
1020 (tab.inForeground() && !didUserStopLoading()
1021 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001022 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1023 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1024 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001025 1500);
John Reckd9862372012-02-21 15:04:50 -08001026 }
1027 }
1028 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001029 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001030 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001032 // still loading
1033 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001034 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001035 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001036 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001037 } else {
1038 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 }
1040 }
John Reck30c714c2010-12-16 17:30:34 -08001041 mUi.onProgressChanged(tab);
1042 }
1043
1044 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001045 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001046 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001047 }
1048
1049 @Override
1050 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001051 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001052 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001053 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001054 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1055 return;
1056 }
1057 // Update the title in the history database if not in private browsing mode
1058 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001059 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 }
1061 }
1062
1063 @Override
1064 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001065 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001066 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1067 }
1068
1069 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001070 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1071 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001072 }
1073
1074 @Override
1075 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1076 if (mMenuIsDown) {
1077 // only check shortcut key when MENU is held
1078 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1079 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001080 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001081 int keyCode = event.getKeyCode();
1082 // We need to send almost every key to WebKit. However:
1083 // 1. We don't want to block the device on the renderer for
1084 // some keys like menu, home, call.
1085 // 2. There are no WebKit equivalents for some of these keys
1086 // (see app/keyboard_codes_win.h)
1087 // Note that these are not the same set as KeyEvent.isSystemKey:
1088 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1089 if (keyCode == KeyEvent.KEYCODE_MENU ||
1090 keyCode == KeyEvent.KEYCODE_HOME ||
1091 keyCode == KeyEvent.KEYCODE_BACK ||
1092 keyCode == KeyEvent.KEYCODE_CALL ||
1093 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1094 keyCode == KeyEvent.KEYCODE_POWER ||
1095 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1096 keyCode == KeyEvent.KEYCODE_CAMERA ||
1097 keyCode == KeyEvent.KEYCODE_FOCUS ||
1098 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1099 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1100 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1101 return true;
1102 }
1103
1104 // We also have to intercept some shortcuts before we send them to the ContentView.
1105 if (event.isCtrlPressed() && (
1106 keyCode == KeyEvent.KEYCODE_TAB ||
1107 keyCode == KeyEvent.KEYCODE_W ||
1108 keyCode == KeyEvent.KEYCODE_F4)) {
1109 return true;
1110 }
1111
1112 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001113 }
1114
1115 @Override
John Reck997b1b72012-04-19 18:08:25 -07001116 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 if (!isActivityPaused()) {
1118 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001119 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 } else {
John Reck997b1b72012-04-19 18:08:25 -07001121 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 }
1123 }
John Reck997b1b72012-04-19 18:08:25 -07001124 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 }
1126
1127 @Override
John Reck324d4402011-01-11 16:56:42 -08001128 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001129 // Don't save anything in private browsing mode or when disabling history
1130 // for regular tabs is enabled
1131 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1132 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1133 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001134
John Reck49a603c2011-03-03 09:33:05 -08001135 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001136
John Reck324d4402011-01-11 16:56:42 -08001137 if (TextUtils.isEmpty(url)
1138 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 return;
1140 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001141
John Reckf57c0292011-07-21 18:15:39 -07001142 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001143 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 }
1145
1146 @Override
1147 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1148 AsyncTask<Void, Void, String[]> task =
1149 new AsyncTask<Void, Void, String[]>() {
1150 @Override
1151 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001152 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001153 }
1154 @Override
1155 public void onPostExecute(String[] result) {
1156 callback.onReceiveValue(result);
1157 }
1158 };
1159 task.execute();
1160 }
1161
1162 @Override
1163 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1164 final HttpAuthHandler handler, final String host,
1165 final String realm) {
1166 String username = null;
1167 String password = null;
1168
1169 boolean reuseHttpAuthUsernamePassword
1170 = handler.useHttpAuthUsernamePassword();
1171
1172 if (reuseHttpAuthUsernamePassword && view != null) {
1173 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1174 if (credentials != null && credentials.length == 2) {
1175 username = credentials[0];
1176 password = credentials[1];
1177 }
1178 }
1179
1180 if (username != null && password != null) {
1181 handler.proceed(username, password);
1182 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001183 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001184 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1185 } else {
1186 handler.cancel();
1187 }
1188 }
1189 }
1190
1191 @Override
1192 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001193 String contentDisposition, String mimetype, String referer,
1194 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001195 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001196 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001197 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001198 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001199 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001200 // This Tab was opened for the sole purpose of downloading a
1201 // file. Remove it.
1202 if (tab == mTabControl.getCurrentTab()) {
1203 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001204 if (tab.getDerivedFromIntent())
1205 closeTab(tab);
1206 else
1207 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 } else {
1209 // In this case, it is not.
1210 closeTab(tab);
1211 }
1212 }
1213 }
1214
1215 @Override
1216 public Bitmap getDefaultVideoPoster() {
1217 return mUi.getDefaultVideoPoster();
1218 }
1219
1220 @Override
1221 public View getVideoLoadingProgressView() {
1222 return mUi.getVideoLoadingProgressView();
1223 }
1224
1225 @Override
1226 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1227 SslError error) {
1228 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1229 }
1230
1231 // helper method
1232
1233 /*
1234 * Update the favorites icon if the private browsing isn't enabled and the
1235 * icon is valid.
1236 */
1237 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1238 final String url, Bitmap favicon) {
1239 if (favicon == null) {
1240 return;
1241 }
1242 if (!tab.isPrivateBrowsingEnabled()) {
1243 Bookmarks.updateFavicon(mActivity
1244 .getContentResolver(), originalUrl, url, favicon);
1245 }
1246 }
1247
Leon Scroggins4cd97792010-12-03 15:31:56 -05001248 @Override
1249 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001250 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001251 mUi.bookmarkedStatusHasChanged(tab);
1252 }
1253
Michael Kolb8233fac2010-10-26 16:08:53 -07001254 // end WebViewController
1255
1256 protected void pageUp() {
1257 getCurrentTopWebView().pageUp(false);
1258 }
1259
1260 protected void pageDown() {
1261 getCurrentTopWebView().pageDown(false);
1262 }
1263
1264 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001265 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001266 public void editUrl() {
1267 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001268 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001269 }
1270
John Reck9c35b9c2012-05-30 10:08:50 -07001271 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001272 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001273 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 if (tab.inForeground()) {
1275 if (mUi.isCustomViewShowing()) {
1276 callback.onCustomViewHidden();
1277 return;
1278 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001279 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001280 // Save the menu state and set it to empty while the custom
1281 // view is showing.
1282 mOldMenuState = mMenuState;
1283 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001284 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001285 }
1286 }
1287
1288 @Override
1289 public void hideCustomView() {
1290 if (mUi.isCustomViewShowing()) {
1291 mUi.onHideCustomView();
1292 // Reset the old menu state.
1293 mMenuState = mOldMenuState;
1294 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001295 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001296 }
1297 }
1298
John Reck9c35b9c2012-05-30 10:08:50 -07001299 @Override
1300 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001301 Intent intent) {
1302 if (getCurrentTopWebView() == null) return;
1303 switch (requestCode) {
1304 case PREFERENCES_PAGE:
1305 if (resultCode == Activity.RESULT_OK && intent != null) {
1306 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001307 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001308 mTabControl.removeParentChildRelationShips();
1309 }
1310 }
1311 break;
1312 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001313 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001314 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001315 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001316 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001317 case AUTOFILL_SETUP:
1318 // Determine whether a profile was actually set up or not
1319 // and if so, send the message back to the WebTextView to
1320 // fill the form with the new profile.
1321 if (getSettings().getAutoFillProfile() != null) {
1322 mAutoFillSetupMessage.sendToTarget();
1323 mAutoFillSetupMessage = null;
1324 }
1325 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001326 case COMBO_VIEW:
1327 if (intent == null || resultCode != Activity.RESULT_OK) {
1328 break;
1329 }
John Reck3ba45532011-08-11 16:26:53 -07001330 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001331 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1332 Tab t = getCurrentTab();
1333 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001334 mUpdateMyNavThumbnail = true;
1335 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001336 loadUrl(t, uri.toString());
1337 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1338 String[] urls = intent.getStringArrayExtra(
1339 ComboViewActivity.EXTRA_OPEN_ALL);
1340 Tab parent = getCurrentTab();
1341 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001342 if (url != null) {
1343 parent = openTab(url, parent,
1344 !mSettings.openInBackground(), true);
1345 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001346 }
1347 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1348 long id = intent.getLongExtra(
1349 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1350 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001351 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001352 }
1353 }
1354 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001355 case VOICE_RESULT:
1356 if (resultCode == Activity.RESULT_OK && intent != null) {
1357 ArrayList<String> results = intent.getStringArrayListExtra(
1358 RecognizerIntent.EXTRA_RESULTS);
1359 if (results.size() >= 1) {
1360 mVoiceResult = results.get(0);
1361 }
1362 }
1363 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001364 case MY_NAVIGATION:
1365 if (intent == null || resultCode != Activity.RESULT_OK) {
1366 break;
1367 }
1368
1369 if (intent.getBooleanExtra("need_refresh", false) &&
1370 getCurrentTopWebView() != null) {
1371 getCurrentTopWebView().reload();
1372 }
1373 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001374 default:
1375 break;
1376 }
1377 getCurrentTopWebView().requestFocus();
1378 }
1379
1380 /**
1381 * Open the Go page.
1382 * @param startWithHistory If true, open starting on the history tab.
1383 * Otherwise, start with the bookmarks tab.
1384 */
1385 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001386 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001387 if (mTabControl.getCurrentWebView() == null) {
1388 return;
1389 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001390 // clear action mode
1391 if (isInCustomActionMode()) {
1392 endActionMode();
1393 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 Bundle extras = new Bundle();
1395 // Disable opening in a new window if we have maxed out the windows
1396 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1397 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001398 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001399 }
1400
1401 // combo view callbacks
1402
Michael Kolb8233fac2010-10-26 16:08:53 -07001403 // key handling
1404 protected void onBackKey() {
1405 if (!mUi.onBackKey()) {
1406 WebView subwindow = mTabControl.getCurrentSubWindow();
1407 if (subwindow != null) {
1408 if (subwindow.canGoBack()) {
1409 subwindow.goBack();
1410 } else {
1411 dismissSubWindow(mTabControl.getCurrentTab());
1412 }
1413 } else {
1414 goBackOnePageOrQuit();
1415 }
1416 }
1417 }
1418
Michael Kolb4bd767d2011-05-27 11:33:55 -07001419 protected boolean onMenuKey() {
1420 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001421 }
1422
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 // menu handling and state
1424 // TODO: maybe put into separate handler
1425
John Reck9c35b9c2012-05-30 10:08:50 -07001426 @Override
1427 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001428 if (mMenuState == EMPTY_MENU) {
1429 return false;
1430 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001431 MenuInflater inflater = mActivity.getMenuInflater();
1432 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001433 return true;
1434 }
1435
John Reck9c35b9c2012-05-30 10:08:50 -07001436 @Override
1437 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001438 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001439 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001440 return;
1441 }
1442 if (!(v instanceof WebView)) {
1443 return;
1444 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001445 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001446 WebView.HitTestResult result;
1447
1448 /* Determine whether the ContextMenu got triggered because
1449 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1450 * received. The mResult acts as a flag to identify, how it got trigerred
1451 */
1452 if (mResult == null){
1453 result = webview.getHitTestResult();
1454 } else {
1455 result = mResult;
1456 }
1457
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 if (result == null) {
1459 return;
1460 }
1461
1462 int type = result.getType();
1463 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001464
1465 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1466 unknownTypeMap.put("webview", webview);
1467 final Message msg = mHandler.obtainMessage(
1468 UNKNOWN_TYPE_MSG, unknownTypeMap);
1469 /* As defined in android developers guide
1470 * when UNKNOWN_TYPE is received as a result of HitTest
1471 * you need to determing the type by invoking requestFocusNodeHref
1472 */
1473 webview.requestFocusNodeHref(msg);
1474
Michael Kolb8233fac2010-10-26 16:08:53 -07001475 Log.w(LOGTAG,
1476 "We should not show context menu when nothing is touched");
1477 return;
1478 }
1479 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1480 // let TextView handles context menu
1481 return;
1482 }
1483
1484 // Note, http://b/issue?id=1106666 is requesting that
1485 // an inflated menu can be used again. This is not available
1486 // yet, so inflate each time (yuk!)
1487 MenuInflater inflater = mActivity.getMenuInflater();
1488 inflater.inflate(R.menu.browsercontext, menu);
1489
1490 // Show the correct menu group
1491 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001492 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001493 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001494 menu.setGroupVisible(R.id.PHONE_MENU,
1495 type == WebView.HitTestResult.PHONE_TYPE);
1496 menu.setGroupVisible(R.id.EMAIL_MENU,
1497 type == WebView.HitTestResult.EMAIL_TYPE);
1498 menu.setGroupVisible(R.id.GEO_MENU,
1499 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001500 String itemUrl = null;
1501 String url = webview.getOriginalUrl();
1502 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1503 itemUrl = Uri.decode(navigationUrl);
1504 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1505 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1506 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1507 } else {
1508 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1509 }
1510 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1511 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1512 } else {
1513 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1514
1515 menu.setGroupVisible(R.id.IMAGE_MENU,
1516 type == WebView.HitTestResult.IMAGE_TYPE
1517 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1518 menu.setGroupVisible(R.id.ANCHOR_MENU,
1519 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1520 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001521 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1522 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001523 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001524 // Setup custom handling depending on the type
1525 switch (type) {
1526 case WebView.HitTestResult.PHONE_TYPE:
1527 menu.setHeaderTitle(Uri.decode(extra));
1528 menu.findItem(R.id.dial_context_menu_id).setIntent(
1529 new Intent(Intent.ACTION_VIEW, Uri
1530 .parse(WebView.SCHEME_TEL + extra)));
1531 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1532 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1533 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1534 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1535 addIntent);
1536 menu.findItem(R.id.copy_phone_context_menu_id)
1537 .setOnMenuItemClickListener(
1538 new Copy(extra));
1539 break;
1540
1541 case WebView.HitTestResult.EMAIL_TYPE:
1542 menu.setHeaderTitle(extra);
1543 menu.findItem(R.id.email_context_menu_id).setIntent(
1544 new Intent(Intent.ACTION_VIEW, Uri
1545 .parse(WebView.SCHEME_MAILTO + extra)));
1546 menu.findItem(R.id.copy_mail_context_menu_id)
1547 .setOnMenuItemClickListener(
1548 new Copy(extra));
1549 break;
1550
1551 case WebView.HitTestResult.GEO_TYPE:
1552 menu.setHeaderTitle(extra);
1553 menu.findItem(R.id.map_context_menu_id).setIntent(
1554 new Intent(Intent.ACTION_VIEW, Uri
1555 .parse(WebView.SCHEME_GEO
1556 + URLEncoder.encode(extra))));
1557 menu.findItem(R.id.copy_geo_context_menu_id)
1558 .setOnMenuItemClickListener(
1559 new Copy(extra));
1560 break;
1561
1562 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1563 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001564 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001565 // decide whether to show the open link in new tab option
1566 boolean showNewTab = mTabControl.canCreateNewTab();
1567 MenuItem newTabItem
1568 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001569 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001570 ? R.string.contextmenu_openlink_newwindow_background
1571 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001572 newTabItem.setVisible(showNewTab);
1573 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001574 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1575 newTabItem.setOnMenuItemClickListener(
1576 new MenuItem.OnMenuItemClickListener() {
1577 @Override
1578 public boolean onMenuItemClick(MenuItem item) {
1579 final HashMap<String, WebView> hrefMap =
1580 new HashMap<String, WebView>();
1581 hrefMap.put("webview", webview);
1582 final Message msg = mHandler.obtainMessage(
1583 FOCUS_NODE_HREF,
1584 R.id.open_newtab_context_menu_id,
1585 0, hrefMap);
1586 webview.requestFocusNodeHref(msg);
1587 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001588 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001589 });
1590 } else {
1591 newTabItem.setOnMenuItemClickListener(
1592 new MenuItem.OnMenuItemClickListener() {
1593 @Override
1594 public boolean onMenuItemClick(MenuItem item) {
1595 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001596 openTab(extra, parent,
1597 !mSettings.openInBackground(),
1598 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001599 return true;
1600 }
1601 });
1602 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001604 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1605 menu.setHeaderTitle(navigationUrl);
1606 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1607
1608 if (itemUrl != null) {
1609 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1610 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1611 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1612 @Override
1613 public boolean onMenuItemClick(MenuItem item) {
1614 final Intent intent = new Intent(Controller.this
1615 .getContext(),
1616 AddMyNavigationPage.class);
1617 Bundle bundle = new Bundle();
1618 String url = Uri.decode(navigationUrl);
1619 bundle.putBoolean("isAdding", false);
1620 bundle.putString("url", url);
1621 bundle.putString("name", getNameFromUrl(url));
1622 intent.putExtra("websites", bundle);
1623 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1624 return false;
1625 }
1626 });
1627 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1628 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1629 @Override
1630 public boolean onMenuItemClick(MenuItem item) {
1631 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1632 return false;
1633 }
1634 });
1635 }
1636 } else {
1637 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1638 }
1639 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001640 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1641 break;
1642 }
1643 // otherwise fall through to handle image part
1644 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001645 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1646 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001647 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1648 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001649 shareItem.setOnMenuItemClickListener(
1650 new MenuItem.OnMenuItemClickListener() {
1651 @Override
1652 public boolean onMenuItemClick(MenuItem item) {
1653 sharePage(mActivity, null, extra, null,
1654 null);
1655 return true;
1656 }
1657 }
1658 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001659 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001660 menu.findItem(R.id.view_image_context_menu_id)
1661 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1662 @Override
1663 public boolean onMenuItemClick(MenuItem item) {
1664 openTab(extra, mTabControl.getCurrentTab(), true, true);
1665 return false;
1666 }
1667 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001668 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1669 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1670 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001671 menu.findItem(R.id.set_wallpaper_context_menu_id).
1672 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1673 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001674 break;
1675
1676 default:
1677 Log.w(LOGTAG, "We should not get here.");
1678 break;
1679 }
1680 //update the ui
1681 mUi.onContextMenuCreated(menu);
1682 }
1683
kaiyiz6e5b3e02013-08-19 20:02:01 +08001684 public void startAddMyNavigation(String url) {
1685 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1686 Bundle bundle = new Bundle();
1687 bundle.putBoolean("isAdding", true);
1688 bundle.putString("url", url);
1689 bundle.putString("name", getNameFromUrl(url));
1690 intent.putExtra("websites", bundle);
1691 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1692 }
1693
1694 private void showMyNavigationDeleteDialog(final String itemUrl) {
1695 new AlertDialog.Builder(this.getContext())
1696 .setTitle(R.string.my_navigation_delete_label)
1697 .setIcon(android.R.drawable.ic_dialog_alert)
1698 .setMessage(R.string.my_navigation_delete_msg)
1699 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1700 @Override
1701 public void onClick(DialogInterface dialog, int whichButton) {
1702 deleteMyNavigationItem(itemUrl);
1703 }
1704 })
1705 .setNegativeButton(R.string.cancel, null)
1706 .show();
1707 }
1708
1709 private void deleteMyNavigationItem(final String itemUrl) {
1710 ContentResolver cr = this.getContext().getContentResolver();
1711 Cursor cursor = null;
1712
1713 try {
1714 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1715 new String[] {
1716 MyNavigationUtil.ID
1717 }, "url = ?", new String[] {
1718 itemUrl
1719 }, null);
1720 if (null != cursor && cursor.moveToFirst()) {
1721 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1722 cursor.getLong(0));
1723
1724 ContentValues values = new ContentValues();
1725 values.put(MyNavigationUtil.TITLE, "");
1726 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1727 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1728 ByteArrayOutputStream os = new ByteArrayOutputStream();
1729 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1730 R.raw.my_navigation_add);
1731 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1732 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1733 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1734 cr.update(uri, values, null, null);
1735 } else {
1736 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1737 }
1738 } catch (IllegalStateException e) {
1739 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1740 } finally {
1741 if (null != cursor) {
1742 cursor.close();
1743 }
1744 }
1745
1746 if (getCurrentTopWebView() != null) {
1747 getCurrentTopWebView().reload();
1748 }
1749 }
1750
1751 private String getNameFromUrl(String itemUrl) {
1752 ContentResolver cr = this.getContext().getContentResolver();
1753 Cursor cursor = null;
1754 String name = null;
1755
1756 try {
1757 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1758 new String[] {
1759 MyNavigationUtil.TITLE
1760 }, "url = ?", new String[] {
1761 itemUrl
1762 }, null);
1763 if (null != cursor && cursor.moveToFirst()) {
1764 name = cursor.getString(0);
1765 } else {
1766 Log.e(LOGTAG, "this item does not exist!");
1767 }
1768 } catch (IllegalStateException e) {
1769 Log.e(LOGTAG, "getNameFromUrl", e);
1770 } finally {
1771 if (null != cursor) {
1772 cursor.close();
1773 }
1774 }
1775 return name;
1776 }
1777
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001778 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001779 final ContentResolver cr = mActivity.getContentResolver();
1780 new AsyncTask<Void, Void, Void>() {
1781 @Override
1782 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001783 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1784 if(!isMyNavigationUrl)
1785 return null;
1786
kaiyiz6e5b3e02013-08-19 20:02:01 +08001787 ContentResolver cr = mActivity.getContentResolver();
1788 Cursor cursor = null;
1789 try {
1790 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1791 new String[] {
1792 MyNavigationUtil.ID
1793 }, "url = ?", new String[] {
1794 itemUrl
1795 }, null);
1796 if (null != cursor && cursor.moveToFirst()) {
1797 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001798 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001799
1800 ContentValues values = new ContentValues();
1801 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1802 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1803 cursor.getLong(0));
1804 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1805 cr.update(uri, values, null, null);
1806 os.close();
1807 }
1808 } catch (IllegalStateException e) {
1809 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1810 } catch (IOException e) {
1811 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1812 } finally {
1813 if (null != cursor) {
1814 cursor.close();
1815 }
1816 }
1817 return null;
1818 }
1819 }.execute();
1820 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001821 /**
1822 * As the menu can be open when loading state changes
1823 * we must manually update the state of the stop/reload menu
1824 * item
1825 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001826 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001827 if (menu == null) {
1828 return;
1829 }
1830 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001831 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001832 menu.findItem(R.id.stop_menu_id):
1833 menu.findItem(R.id.reload_menu_id);
1834 if (src != null) {
1835 dest.setIcon(src.getIcon());
1836 dest.setTitle(src.getTitle());
1837 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001838 mActivity.invalidateOptionsMenu();
1839 }
1840
1841 public void invalidateOptionsMenu() {
1842 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001843 }
1844
John Reck9c35b9c2012-05-30 10:08:50 -07001845 @Override
1846 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001847 // Software menu key (toolbar key)
1848 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1849 return true;
1850 }
1851
1852 @Override
1853 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001854 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001855 // hold on to the menu reference here; it is used by the page callbacks
1856 // to update the menu based on loading state
1857 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001858 // Note: setVisible will decide whether an item is visible; while
1859 // setEnabled() will decide whether an item is enabled, which also means
1860 // whether the matching shortcut key will function.
1861 switch (mMenuState) {
1862 case EMPTY_MENU:
1863 if (mCurrentMenuState != mMenuState) {
1864 menu.setGroupVisible(R.id.MAIN_MENU, false);
1865 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1866 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1867 }
1868 break;
1869 default:
1870 if (mCurrentMenuState != mMenuState) {
1871 menu.setGroupVisible(R.id.MAIN_MENU, true);
1872 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1873 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1874 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001875 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001876 break;
1877 }
1878 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001879 mUi.onPrepareOptionsMenu(menu);
1880 }
1881
1882 private void setMenuItemVisibility(Menu menu, int id,
1883 boolean visibility) {
1884 MenuItem item = menu.findItem(id);
1885 if (item != null) {
1886 item.setVisible(visibility);
1887 }
1888 }
1889
1890 private int lookupBookmark(String title, String url) {
1891 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001892 int count = 0;
1893 Cursor cursor = null;
1894 try {
1895 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1896 BookmarksLoader.PROJECTION,
1897 "title = ? OR url = ?",
1898 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001899 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001900 },
1901 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001902
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001903 if (cursor != null)
1904 count = cursor.getCount();
1905
1906 } catch (IllegalStateException e) {
1907 Log.e(LOGTAG, "lookupBookmark ", e);
1908 } finally {
1909 if (null != cursor) {
1910 cursor.close();
1911 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001912 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001913 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001914 }
1915
1916 private void resetMenuItems(Menu menu) {
1917 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1918 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1919
1920 WebView w = getCurrentTopWebView();
1921 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1922
1923 String title = w.getTitle();
1924 String url = w.getUrl();
1925 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1926 bookmark_icon.setChecked(true);
1927 } else {
1928 bookmark_icon.setChecked(false);
1929 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001930 }
1931
Michael Kolb4bf79712011-07-14 14:18:12 -07001932 @Override
1933 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001934 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001935 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001936 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001937 // Following flag is used to identify schemes for which the LIVE_MENU
1938 // items defined in res/menu/browser.xml should be enabled
1939 boolean isLiveScheme = false;
1940 boolean isPageFinished = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001941
1942 resetMenuItems(menu);
1943
Michael Kolb4bf79712011-07-14 14:18:12 -07001944 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001945 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001946 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001947 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001948 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001949 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Michael Kolb4bf79712011-07-14 14:18:12 -07001950 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001951
1952 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1953 forward.setEnabled(canGoForward);
1954
Michael Kolb4bf79712011-07-14 14:18:12 -07001955 // decide whether to show the share link option
1956 PackageManager pm = mActivity.getPackageManager();
1957 Intent send = new Intent(Intent.ACTION_SEND);
1958 send.setType("text/plain");
1959 ResolveInfo ri = pm.resolveActivity(send,
1960 PackageManager.MATCH_DEFAULT_ONLY);
1961 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1962
1963 boolean isNavDump = mSettings.enableNavDump();
1964 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1965 nav.setVisible(isNavDump);
1966 nav.setEnabled(isNavDump);
1967
1968 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001969 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1970 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001971 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08001972 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
1973 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001974 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08001975 setMenuItemVisibility(menu, R.id.add_to_homescreen,
1976 isLive && isLiveScheme && isPageFinished);
1977 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
1978 isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001979 // history and snapshots item are the members of COMBO menu group,
1980 // so if show history item, only make snapshots item invisible.
1981 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001982
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001983 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001984 }
1985
John Reck9c35b9c2012-05-30 10:08:50 -07001986 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001987 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001988 if (null == getCurrentTopWebView()) {
1989 return false;
1990 }
1991 if (mMenuIsDown) {
1992 // The shortcut action consumes the MENU. Even if it is still down,
1993 // it won't trigger the next shortcut action. In the case of the
1994 // shortcut action triggering a new activity, like Bookmarks, we
1995 // won't get onKeyUp for MENU. So it is important to reset it here.
1996 mMenuIsDown = false;
1997 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001998 if (mUi.onOptionsItemSelected(item)) {
1999 // ui callback handled it
2000 return true;
2001 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002002 switch (item.getItemId()) {
2003 // -- Main menu
2004 case R.id.new_tab_menu_id:
2005 openTabToHomePage();
2006 break;
2007
2008 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002009 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002010 break;
2011
2012 case R.id.goto_menu_id:
2013 editUrl();
2014 break;
2015
2016 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002017 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2018 break;
2019
2020 case R.id.history_menu_id:
2021 bookmarksOrHistoryPicker(ComboViews.History);
2022 break;
2023
2024 case R.id.snapshots_menu_id:
2025 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002026 break;
2027
Pankaj Garg49b79252014-11-07 17:33:41 -08002028 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002029 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002030 break;
2031
2032 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002033 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002034 stopLoading();
2035 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002036 Tab currentTab = mTabControl.getCurrentTab();
2037 if (currentTab.hasCrashed) {
2038 currentTab.replaceCrashView(getCurrentTopWebView(),
2039 currentTab.getViewContainer());
2040 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002041 getCurrentTopWebView().reload();
2042 }
2043 break;
2044
Michael Kolb8233fac2010-10-26 16:08:53 -07002045 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002046 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002047 break;
2048
2049 case R.id.close_menu_id:
2050 // Close the subwindow if it exists.
2051 if (mTabControl.getCurrentSubWindow() != null) {
2052 dismissSubWindow(mTabControl.getCurrentTab());
2053 break;
2054 }
2055 closeCurrentTab();
2056 break;
2057
kaiyiza8b6dbb2013-07-29 18:11:22 +08002058 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002059 Object[] params = { new String("persist.debug.browsermonkeytest")};
2060 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002061 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002062 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002063 if (ret != null && ret.equals("enable"))
2064 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002065 if (BrowserConfig.getInstance(getContext())
2066 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2067 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002068 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 case R.id.homepage_menu_id:
2070 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002071 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002072 break;
2073
2074 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002075 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002076 break;
2077
2078 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002079 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 break;
2081
John Reck2bc80422011-06-30 15:11:49 -07002082 case R.id.save_snapshot_menu_id:
2083 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002084 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002085 createScreenshotAsync(
2086 source.getWebView(),
2087 getDesiredThumbnailWidth(mActivity),
2088 getDesiredThumbnailHeight(mActivity),
2089 new ValueCallback<Bitmap>() {
2090 @Override
2091 public void onReceiveValue(Bitmap bitmap) {
2092 new SaveSnapshotTask(source, bitmap).execute();
2093 }
2094 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002095 break;
2096
Michael Kolb8233fac2010-10-26 16:08:53 -07002097 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002098 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002099 break;
2100
John Recke1a03a32011-09-14 17:04:16 -07002101 case R.id.snapshot_go_live:
2102 goLive();
2103 return true;
2104
Michael Kolb8233fac2010-10-26 16:08:53 -07002105 case R.id.share_page_menu_id:
2106 Tab currentTab = mTabControl.getCurrentTab();
2107 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002108 return false;
2109 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002110 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002111 break;
2112
2113 case R.id.dump_nav_menu_id:
2114 getCurrentTopWebView().debugDump();
2115 break;
2116
Michael Kolb8233fac2010-10-26 16:08:53 -07002117 case R.id.zoom_in_menu_id:
2118 getCurrentTopWebView().zoomIn();
2119 break;
2120
2121 case R.id.zoom_out_menu_id:
2122 getCurrentTopWebView().zoomOut();
2123 break;
2124
2125 case R.id.view_downloads_menu_id:
2126 viewDownloads();
2127 break;
2128
John Reck42229bc2011-08-19 13:26:43 -07002129 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002130 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002131 break;
2132
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 case R.id.window_one_menu_id:
2134 case R.id.window_two_menu_id:
2135 case R.id.window_three_menu_id:
2136 case R.id.window_four_menu_id:
2137 case R.id.window_five_menu_id:
2138 case R.id.window_six_menu_id:
2139 case R.id.window_seven_menu_id:
2140 case R.id.window_eight_menu_id:
2141 {
2142 int menuid = item.getItemId();
2143 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2144 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2145 Tab desiredTab = mTabControl.getTab(id);
2146 if (desiredTab != null &&
2147 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002148 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 }
2150 break;
2151 }
2152 }
2153 }
2154 break;
2155
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002156 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002157 Bundle bundle = new Bundle();
2158 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2159 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2160 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2161 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2162 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002163 break;
2164
Pankaj Garg49b79252014-11-07 17:33:41 -08002165 case R.id.add_to_homescreen:
2166 final WebView w = getCurrentTopWebView();
2167 final EditText input = new EditText(getContext());
2168 input.setText(w.getTitle());
2169 new AlertDialog.Builder(getContext())
2170 .setTitle("Add to homescreen")
2171 .setMessage("Title")
2172 .setView(input)
2173 .setPositiveButton("Add", new DialogInterface.OnClickListener() {
2174 public void onClick(DialogInterface dialog, int whichButton) {
2175 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2176 getContext(),
2177 w.getUrl(),
2178 input.getText().toString(),
2179 w.getViewportBitmap(),
2180 w.getFavicon()));
2181
2182 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2183 .addCategory(Intent.CATEGORY_HOME));
2184 }})
2185 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
2186 public void onClick(DialogInterface dialog, int whichButton) {
2187 // Do nothing.
2188 }
2189 })
2190 .show();
2191 break;
2192
Michael Kolb8233fac2010-10-26 16:08:53 -07002193 default:
2194 return false;
2195 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002196 return true;
2197 }
2198
John Reck68234a92012-04-19 15:27:12 -07002199 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2200 implements OnCancelListener {
2201
2202 private Tab mTab;
2203 private Dialog mProgressDialog;
2204 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002205 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002206
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002207 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002208 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002209 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002210 }
2211
2212 @Override
2213 protected void onPreExecute() {
2214 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2215 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2216 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002217 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002218 }
2219
2220 @Override
2221 protected Long doInBackground(Void... params) {
2222 if (!mTab.saveViewState(mValues)) {
2223 return null;
2224 }
2225 if (isCancelled()) {
2226 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2227 File file = mActivity.getFileStreamPath(path);
2228 if (!file.delete()) {
2229 file.deleteOnExit();
2230 }
2231 return null;
2232 }
2233 final ContentResolver cr = mActivity.getContentResolver();
2234 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2235 if (result == null) {
2236 return null;
2237 }
2238 long id = ContentUris.parseId(result);
2239 return id;
2240 }
2241
2242 @Override
2243 protected void onPostExecute(Long id) {
2244 if (isCancelled()) {
2245 return;
2246 }
2247 mProgressDialog.dismiss();
2248 if (id == null) {
2249 Toast.makeText(mActivity, R.string.snapshot_failed,
2250 Toast.LENGTH_SHORT).show();
2251 return;
2252 }
2253 Bundle b = new Bundle();
2254 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2255 mUi.showComboView(ComboViews.Snapshots, b);
2256 }
2257
2258 @Override
2259 public void onCancel(DialogInterface dialog) {
2260 cancel(true);
2261 }
2262 }
2263
Michael Kolb80f75082012-04-10 10:50:06 -07002264 @Override
2265 public void toggleUserAgent() {
2266 WebView web = getCurrentWebView();
2267 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002268 }
2269
2270 @Override
2271 public void findOnPage() {
2272 getCurrentTopWebView().showFindDialog(null, true);
2273 }
2274
2275 @Override
2276 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002277 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002278 }
2279
2280 @Override
2281 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002282 WebView w = getCurrentTopWebView();
2283 if (w == null)
2284 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002285 final Intent i = createBookmarkCurrentPageIntent(false);
2286 createScreenshotAsync(
2287 w, getDesiredThumbnailWidth(mActivity),
2288 getDesiredThumbnailHeight(mActivity),
2289 new ValueCallback<Bitmap>() {
2290 @Override
2291 public void onReceiveValue(Bitmap bitmap) {
2292 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2293 mActivity.startActivity(i);
2294 }
2295 });
Michael Kolb80f75082012-04-10 10:50:06 -07002296 }
2297
John Recke1a03a32011-09-14 17:04:16 -07002298 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002299 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002300 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002301 boolean onlySingleTabRemaining = false;
2302 if (mTabControl.getTabCount() > 1) {
2303 // destroy the old snapshot tab
2304 closeCurrentTab();
2305 } else {
2306 onlySingleTabRemaining = true;
2307 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002308 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002309 if (onlySingleTabRemaining) {
2310 closeTab(t);
2311 }
2312
Tarun Nainani8eb00912014-07-17 12:28:32 -07002313 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002314 }
2315
luxiaolb40014b2013-07-19 10:01:43 +08002316 private void showExitDialog(final Activity activity) {
2317 new AlertDialog.Builder(activity)
2318 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002319 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002320 .setMessage(R.string.exit_browser_msg)
2321 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2322 public void onClick(DialogInterface dialog, int which) {
2323 activity.moveTaskToBack(true);
2324 dialog.dismiss();
2325 }
2326 })
2327 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2328 public void onClick(DialogInterface dialog, int which) {
2329 activity.finish();
2330 mHandler.postDelayed(new Runnable() {
2331 @Override
2332 public void run() {
luxiaolb40014b2013-07-19 10:01:43 +08002333 mCrashRecoveryHandler.clearState(true);
2334 int pid = android.os.Process.myPid();
2335 android.os.Process.killProcess(pid);
2336 }
2337 }, 300);
2338 dialog.dismiss();
2339 }
2340 })
2341 .show();
2342 }
Michael Kolb315d5022011-10-13 12:47:11 -07002343 @Override
2344 public void showPageInfo() {
2345 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2346 }
2347
John Reck9c35b9c2012-05-30 10:08:50 -07002348 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002349 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002350 // Let the History and Bookmark fragments handle menus they created.
2351 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2352 return false;
2353 }
2354
Michael Kolb8233fac2010-10-26 16:08:53 -07002355 int id = item.getItemId();
2356 boolean result = true;
2357 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002358 // -- Browser context menu
2359 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002360 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002361 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002362 case R.id.copy_link_context_menu_id:
2363 final WebView webView = getCurrentTopWebView();
2364 if (null == webView) {
2365 result = false;
2366 break;
2367 }
2368 final HashMap<String, WebView> hrefMap =
2369 new HashMap<String, WebView>();
2370 hrefMap.put("webview", webView);
2371 final Message msg = mHandler.obtainMessage(
2372 FOCUS_NODE_HREF, id, 0, hrefMap);
2373 webView.requestFocusNodeHref(msg);
2374 break;
2375
2376 default:
2377 // For other context menus
2378 result = onOptionsItemSelected(item);
2379 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002380 return result;
2381 }
2382
2383 /**
2384 * support programmatically opening the context menu
2385 */
2386 public void openContextMenu(View view) {
2387 mActivity.openContextMenu(view);
2388 }
2389
2390 /**
2391 * programmatically open the options menu
2392 */
2393 public void openOptionsMenu() {
2394 mActivity.openOptionsMenu();
2395 }
2396
John Reck9c35b9c2012-05-30 10:08:50 -07002397 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002398 public boolean onMenuOpened(int featureId, Menu menu) {
2399 if (mOptionsMenuOpen) {
2400 if (mConfigChanged) {
2401 // We do not need to make any changes to the state of the
2402 // title bar, since the only thing that happened was a
2403 // change in orientation
2404 mConfigChanged = false;
2405 } else {
2406 if (!mExtendedMenuOpen) {
2407 mExtendedMenuOpen = true;
2408 mUi.onExtendedMenuOpened();
2409 } else {
2410 // Switching the menu back to icon view, so show the
2411 // title bar once again.
2412 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002413 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002414 }
2415 }
2416 } else {
2417 // The options menu is closed, so open it, and show the title
2418 mOptionsMenuOpen = true;
2419 mConfigChanged = false;
2420 mExtendedMenuOpen = false;
2421 mUi.onOptionsMenuOpened();
2422 }
2423 return true;
2424 }
2425
John Reck9c35b9c2012-05-30 10:08:50 -07002426 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002427 public void onOptionsMenuClosed(Menu menu) {
2428 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002429 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002430 }
2431
John Reck9c35b9c2012-05-30 10:08:50 -07002432 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002434 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002435 }
2436
2437 // Helper method for getting the top window.
2438 @Override
2439 public WebView getCurrentTopWebView() {
2440 return mTabControl.getCurrentTopWebView();
2441 }
2442
2443 @Override
2444 public WebView getCurrentWebView() {
2445 return mTabControl.getCurrentWebView();
2446 }
2447
2448 /*
2449 * This method is called as a result of the user selecting the options
2450 * menu to see the download window. It shows the download window on top of
2451 * the current window.
2452 */
2453 void viewDownloads() {
2454 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2455 mActivity.startActivity(intent);
2456 }
2457
John Reck30b065e2011-07-19 10:58:05 -07002458 int getActionModeHeight() {
2459 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2460 new int[] { android.R.attr.actionBarSize });
2461 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2462 actionBarSizeTypedArray.recycle();
2463 return size;
2464 }
2465
Michael Kolb8233fac2010-10-26 16:08:53 -07002466 // action mode
2467
John Reck9c35b9c2012-05-30 10:08:50 -07002468 @Override
2469 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002470 mUi.onActionModeStarted(mode);
2471 mActionMode = mode;
2472 }
2473
2474 /*
2475 * True if a custom ActionMode (i.e. find or select) is in use.
2476 */
2477 @Override
2478 public boolean isInCustomActionMode() {
2479 return mActionMode != null;
2480 }
2481
2482 /*
2483 * End the current ActionMode.
2484 */
2485 @Override
2486 public void endActionMode() {
2487 if (mActionMode != null) {
2488 mActionMode.finish();
2489 }
2490 }
2491
2492 /*
2493 * Called by find and select when they are finished. Replace title bars
2494 * as necessary.
2495 */
John Reck9c35b9c2012-05-30 10:08:50 -07002496 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 public void onActionModeFinished(ActionMode mode) {
2498 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002499 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002500 mActionMode = null;
2501 }
2502
2503 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002504 final Tab tab = getCurrentTab();
2505 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002506 }
2507
2508 // bookmark handling
2509
2510 /**
2511 * add the current page as a bookmark to the given folder id
2512 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002513 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002514 * bookmarked, and if it is, edit that bookmark. If false, and
2515 * the site is already bookmarked, do not attempt to edit the
2516 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002517 */
2518 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002519 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002520 WebView w = getCurrentTopWebView();
2521 if (w == null) {
2522 return null;
2523 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002524 Intent i = new Intent(mActivity,
2525 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002526 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2527 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2528 String touchIconUrl = w.getTouchIconUrl();
2529 if (touchIconUrl != null) {
2530 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2531 WebSettings settings = w.getSettings();
2532 if (settings != null) {
2533 i.putExtra(AddBookmarkPage.USER_AGENT,
2534 settings.getUserAgentString());
2535 }
2536 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002537 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002538 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002539 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002540 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2541 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002542 // Put the dialog at the upper right of the screen, covering the
2543 // star on the title bar.
2544 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002545 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002546 }
2547
2548 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002549 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002550 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002551 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002552 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002553 }
2554
Vivek Sekharb54614f2014-05-01 19:03:37 -07002555 @Override
2556 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2557 boolean capture) {
2558 mUploadHandler = new UploadHandler(this);
2559 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2560 }
2561
Michael Kolb8233fac2010-10-26 16:08:53 -07002562 // thumbnails
2563
2564 /**
2565 * Return the desired width for thumbnail screenshots, which are stored in
2566 * the database, and used on the bookmarks screen.
2567 * @param context Context for finding out the density of the screen.
2568 * @return desired width for thumbnail screenshot.
2569 */
2570 static int getDesiredThumbnailWidth(Context context) {
2571 return context.getResources().getDimensionPixelOffset(
2572 R.dimen.bookmarkThumbnailWidth);
2573 }
2574
2575 /**
2576 * Return the desired height for thumbnail screenshots, which are stored in
2577 * the database, and used on the bookmarks screen.
2578 * @param context Context for finding out the density of the screen.
2579 * @return desired height for thumbnail screenshot.
2580 */
2581 static int getDesiredThumbnailHeight(Context context) {
2582 return context.getResources().getDimensionPixelOffset(
2583 R.dimen.bookmarkThumbnailHeight);
2584 }
2585
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002586 static void createScreenshotAsync(WebView view, int width, int height,
2587 final ValueCallback<Bitmap> cb) {
2588 if (view == null || width == 0 || height == 0) {
2589 return;
2590 }
2591
2592 view.getContentBitmapAsync(
2593 (float) width / view.getWidth(),
2594 new Rect(),
2595 new ValueCallback<Bitmap>() {
2596 @Override
2597 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002598 if (bitmap != null)
2599 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2600 cb.onReceiveValue(bitmap);
2601 }});
2602 }
2603
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002604 private void updateScreenshot(final Tab tab) {
2605 createScreenshotAsync(
2606 tab.getWebView(),
2607 getDesiredThumbnailWidth(mActivity),
2608 getDesiredThumbnailHeight(mActivity),
2609 new ValueCallback<Bitmap>() {
2610 @Override
2611 public void onReceiveValue(Bitmap bitmap) {
2612 updateScreenshot(tab, bitmap);
2613 }
2614 });
2615 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002616
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002617 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002618 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 // FIXME: Would like to make sure there is actually something to
2620 // draw, but the API for that (WebViewCore.pictureReady()) is not
2621 // currently accessible here.
2622
John Reck34ef2672011-02-10 11:30:55 -08002623 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002624 if (view == null) {
2625 // Tab was destroyed
2626 return;
2627 }
John Reck34ef2672011-02-10 11:30:55 -08002628 final String url = tab.getUrl();
2629 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002630 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002631 if (TextUtils.isEmpty(url)) {
2632 return;
2633 }
2634
kaiyiz6e5b3e02013-08-19 20:02:01 +08002635 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002636 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002637 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002638 }
John Reck34ef2672011-02-10 11:30:55 -08002639 // Only update thumbnails for web urls (http(s)://), not for
2640 // about:, javascript:, data:, etc...
2641 // Unless it is a bookmarked site, then always update
2642 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2643 return;
2644 }
2645
kaiyiz6e5b3e02013-08-19 20:02:01 +08002646 if (url != null && mUpdateMyNavThumbnailUrl != null
2647 && Patterns.WEB_URL.matcher(url).matches()
2648 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2649 String urlHost = (new WebAddress(url)).getHost();
2650 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2651 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2652 || bookmarkHost.length() == 0) {
2653 return;
2654 }
2655 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2656 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2657 bookmarkHost.length());
2658 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2659 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2660 if (!bookmarkDomain.equals(urlDomain)) {
2661 return;
2662 }
2663 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002664 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002665 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2666 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2667 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2668 500);
2669 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002670 return;
2671 }
2672
2673 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002674 new AsyncTask<Void, Void, Void>() {
2675 @Override
2676 protected Void doInBackground(Void... unused) {
2677 Cursor cursor = null;
2678 try {
2679 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002680 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2681 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2682 : url);
2683 if (mUpdateMyNavThumbnail) {
2684 mUpdateMyNavThumbnail = false;
2685 mUpdateMyNavThumbnailUrl = null;
2686 }
John Reck34ef2672011-02-10 11:30:55 -08002687 if (cursor != null && cursor.moveToFirst()) {
2688 final ByteArrayOutputStream os =
2689 new ByteArrayOutputStream();
2690 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002691
John Reck34ef2672011-02-10 11:30:55 -08002692 ContentValues values = new ContentValues();
2693 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002694
John Reck34ef2672011-02-10 11:30:55 -08002695 do {
John Reck617fd832011-02-16 14:35:59 -08002696 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002697 cr.update(Images.CONTENT_URI, values, null, null);
2698 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002699 }
John Reck34ef2672011-02-10 11:30:55 -08002700 } catch (IllegalStateException e) {
2701 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002702 } catch (SQLiteException s) {
2703 // Added for possible error when user tries to remove the same bookmark
2704 // that is being updated with a screen shot
2705 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002706 } finally {
2707 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002708 }
John Reck34ef2672011-02-10 11:30:55 -08002709 return null;
2710 }
2711 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002712 }
2713
2714 private class Copy implements OnMenuItemClickListener {
2715 private CharSequence mText;
2716
John Reck9c35b9c2012-05-30 10:08:50 -07002717 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002718 public boolean onMenuItemClick(MenuItem item) {
2719 copy(mText);
2720 return true;
2721 }
2722
2723 public Copy(CharSequence toCopy) {
2724 mText = toCopy;
2725 }
2726 }
2727
Leon Scroggins63c02662010-11-18 15:16:27 -05002728 private static class Download implements OnMenuItemClickListener {
2729 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002731 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002732 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002733 private static final String FALLBACK_EXTENSION = "dat";
2734 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002735
John Reck9c35b9c2012-05-30 10:08:50 -07002736 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002738 if (DataUri.isDataUri(mText)) {
2739 saveDataUri();
2740 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002741 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002742 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002743 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002744 return true;
2745 }
2746
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002747 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2748 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002749 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002750 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002751 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002752 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002753 }
George Mount387d45d2011-10-07 15:57:53 -07002754
2755 /**
2756 * Treats mText as a data URI and writes its contents to a file
2757 * based on the current time.
2758 */
2759 private void saveDataUri() {
2760 FileOutputStream outputStream = null;
2761 try {
2762 DataUri uri = new DataUri(mText);
2763 File target = getTarget(uri);
2764 outputStream = new FileOutputStream(target);
2765 outputStream.write(uri.getData());
2766 final DownloadManager manager =
2767 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2768 manager.addCompletedDownload(target.getName(),
2769 mActivity.getTitle().toString(), false,
2770 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002771 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002772 } catch (IOException e) {
2773 Log.e(LOGTAG, "Could not save data URL");
2774 } finally {
2775 if (outputStream != null) {
2776 try {
2777 outputStream.close();
2778 } catch (IOException e) {
2779 // ignore close errors
2780 }
2781 }
2782 }
2783 }
2784
2785 /**
2786 * Creates a File based on the current time stamp and uses
2787 * the mime type of the DataUri to get the extension.
2788 */
2789 private File getTarget(DataUri uri) throws IOException {
2790 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002791 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002792 String nameBase = format.format(new Date());
2793 String mimeType = uri.getMimeType();
2794 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2795 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2796 if (extension == null) {
2797 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2798 extension = FALLBACK_EXTENSION;
2799 }
2800 extension = "." + extension; // createTempFile needs the '.'
2801 File targetFile = File.createTempFile(nameBase, extension, dir);
2802 return targetFile;
2803 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002804 }
2805
Cary Clark8974d282010-11-22 10:46:05 -05002806 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002807 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002808
John Reck9c35b9c2012-05-30 10:08:50 -07002809 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002810 public boolean onMenuItemClick(MenuItem item) {
2811 if (mWebView != null) {
2812 return mWebView.selectText();
2813 }
2814 return false;
2815 }
2816
2817 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002818 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002819 }
2820
2821 }
2822
Michael Kolb8233fac2010-10-26 16:08:53 -07002823 /********************** TODO: UI stuff *****************************/
2824
2825 // these methods have been copied, they still need to be cleaned up
2826
2827 /****************** tabs ***************************************************/
2828
2829 // basic tab interactions:
2830
2831 // it is assumed that tabcontrol already knows about the tab
2832 protected void addTab(Tab tab) {
2833 mUi.addTab(tab);
2834 }
2835
2836 protected void removeTab(Tab tab) {
2837 mUi.removeTab(tab);
2838 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002839 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002840 }
2841
Michael Kolbf2055602011-04-09 17:20:03 -07002842 @Override
2843 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002844 // monkey protection against delayed start
2845 if (tab != null) {
2846 mTabControl.setCurrentTab(tab);
2847 // the tab is guaranteed to have a webview after setCurrentTab
2848 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002849 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002850 //Purge active tabs
2851 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002852 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002853 }
2854
John Reck8bcafc12011-08-29 16:43:02 -07002855 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002856 Tab current = mTabControl.getCurrentTab();
2857 if (current != null
2858 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002859 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002860 }
2861 }
2862
John Reck26b18322011-06-21 13:08:58 -07002863 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002864 // Dismiss the subwindow if applicable.
2865 dismissSubWindow(appTab);
2866 // Since we might kill the WebView, remove it from the
2867 // content view first.
2868 mUi.detachTab(appTab);
2869 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002870 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002871 // TODO: analyze why the remove and add are necessary
2872 mUi.attachTab(appTab);
2873 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002874 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002875 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002876 } else {
2877 // If the tab was the current tab, we have to attach
2878 // it to the view system again.
2879 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002880 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002881 }
2882 }
2883
2884 // Remove the sub window if it exists. Also called by TabControl when the
2885 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002886 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002887 public void dismissSubWindow(Tab tab) {
2888 removeSubWindow(tab);
2889 // dismiss the subwindow. This will destroy the WebView.
2890 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002891 WebView wv = getCurrentTopWebView();
2892 if (wv != null) {
2893 wv.requestFocus();
2894 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002895 }
2896
2897 @Override
2898 public void removeSubWindow(Tab t) {
2899 if (t.getSubWebView() != null) {
2900 mUi.removeSubWindow(t.getSubViewContainer());
2901 }
2902 }
2903
2904 @Override
2905 public void attachSubWindow(Tab tab) {
2906 if (tab.getSubWebView() != null) {
2907 mUi.attachSubWindow(tab.getSubViewContainer());
2908 getCurrentTopWebView().requestFocus();
2909 }
2910 }
2911
Mathew Inwood29721c22011-06-29 17:55:24 +01002912 private Tab showPreloadedTab(final UrlData urlData) {
2913 if (!urlData.isPreloaded()) {
2914 return null;
2915 }
2916 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2917 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2918 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002919 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002920 // Could not submit query. Fallback to regular tab creation
2921 tabControl.destroy();
2922 return null;
2923 }
2924 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002925 // check tab count and make room for new tab
2926 if (!mTabControl.canCreateNewTab()) {
2927 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2928 if (leastUsed != null) {
2929 closeTab(leastUsed);
2930 }
2931 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002932 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002933 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002934 mTabControl.addPreloadedTab(t);
2935 addTab(t);
2936 setActiveTab(t);
2937 return t;
2938 }
2939
Michael Kolb7bcafde2011-05-09 13:55:59 -07002940 // open a non inconito tab with the given url data
2941 // and set as active tab
2942 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002943 Tab tab = showPreloadedTab(urlData);
2944 if (tab == null) {
2945 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002946 if ((tab != null) && !urlData.isEmpty()) {
2947 loadUrlDataIn(tab, urlData);
2948 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002949 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002950 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002951 }
2952
Michael Kolb843510f2010-12-09 10:51:49 -08002953 @Override
2954 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002955 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002956 }
2957
Michael Kolb8233fac2010-10-26 16:08:53 -07002958 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002959 public Tab openIncognitoTab() {
2960 return openTab(INCOGNITO_URI, true, true, false);
2961 }
2962
2963 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002964 public Tab openTab(String url, boolean incognito, boolean setActive,
2965 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002966 return openTab(url, incognito, setActive, useCurrent, null);
2967 }
2968
2969 @Override
2970 public Tab openTab(String url, Tab parent, boolean setActive,
2971 boolean useCurrent) {
2972 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2973 setActive, useCurrent, parent);
2974 }
2975
2976 public Tab openTab(String url, boolean incognito, boolean setActive,
2977 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002978 Tab tab = createNewTab(incognito, setActive, useCurrent);
2979 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002980 if (parent instanceof SnapshotTab) {
2981 addTab(tab);
2982 if (setActive)
2983 setActiveTab(tab);
2984 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002985 parent.addChildTab(tab);
2986 }
Michael Kolb519d2282011-05-09 17:03:19 -07002987 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002988 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002989 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002990 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002991 return tab;
2992 }
2993
2994 // this method will attempt to create a new tab
2995 // incognito: private browsing tab
2996 // setActive: ste tab as current tab
2997 // useCurrent: if no new tab can be created, return current tab
2998 private Tab createNewTab(boolean incognito, boolean setActive,
2999 boolean useCurrent) {
3000 Tab tab = null;
3001 if (mTabControl.canCreateNewTab()) {
Stewart Chaoe0e132e2014-12-01 18:28:22 -05003002 tab = mTabControl.createNewTab(incognito, !setActive);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003003 addTab(tab);
3004 if (setActive) {
3005 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07003006 } else {
3007 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003008 }
3009 } else {
3010 if (useCurrent) {
3011 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07003012 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07003013 } else {
3014 mUi.showMaxTabsWarning();
3015 }
3016 }
3017 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003018 }
3019
John Reck2bc80422011-06-30 15:11:49 -07003020 @Override
3021 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3022 SnapshotTab tab = null;
3023 if (mTabControl.canCreateNewTab()) {
3024 tab = mTabControl.createSnapshotTab(snapshotId);
3025 addTab(tab);
3026 if (setActive) {
3027 setActiveTab(tab);
3028 }
3029 } else {
3030 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003031 }
3032 return tab;
3033 }
3034
Michael Kolb8233fac2010-10-26 16:08:53 -07003035 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003036 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003037 * @return boolean True if we successfully switched to a different tab. If
3038 * the indexth tab is null, or if that tab is the same as
3039 * the current one, return false.
3040 */
3041 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003042 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003043 Tab currentTab = mTabControl.getCurrentTab();
3044 if (tab == null || tab == currentTab) {
3045 return false;
3046 }
3047 setActiveTab(tab);
3048 return true;
3049 }
3050
3051 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003052 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003053 closeCurrentTab(false);
3054 }
3055
3056 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003057 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003058 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003059 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003060 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003061 return;
3062 }
Michael Kolbc831b632011-05-11 09:30:34 -07003063 final Tab current = mTabControl.getCurrentTab();
3064 final int pos = mTabControl.getCurrentPosition();
3065 Tab newTab = current.getParent();
3066 if (newTab == null) {
3067 newTab = mTabControl.getTab(pos + 1);
3068 if (newTab == null) {
3069 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003070 }
3071 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003072 if (andQuit) {
3073 mTabControl.setCurrentTab(newTab);
3074 closeTab(current);
3075 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003076 // Close window
3077 closeTab(current);
3078 }
3079 }
3080
3081 /**
3082 * Close the tab, remove its associated title bar, and adjust mTabControl's
3083 * current tab to a valid value.
3084 */
3085 @Override
3086 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003087 if (tab == mTabControl.getCurrentTab()) {
3088 closeCurrentTab();
3089 } else {
3090 removeTab(tab);
3091 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003092 }
3093
Afzal Najamd4e33312012-04-26 01:54:01 -04003094 /**
3095 * Close all tabs except the current one
3096 */
3097 @Override
3098 public void closeOtherTabs() {
3099 int inactiveTabs = mTabControl.getTabCount() - 1;
3100 for (int i = inactiveTabs; i >= 0; i--) {
3101 Tab tab = mTabControl.getTab(i);
3102 if (tab != mTabControl.getCurrentTab()) {
3103 removeTab(tab);
3104 }
3105 }
3106 }
3107
Michael Kolb8233fac2010-10-26 16:08:53 -07003108 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003109 protected void loadUrlFromContext(String url) {
3110 Tab tab = getCurrentTab();
3111 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003112 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003113 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003114 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003115 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003116 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003117 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003118 }
3119 }
3120 }
3121
3122 /**
3123 * Load the URL into the given WebView and update the title bar
3124 * to reflect the new load. Call this instead of WebView.loadUrl
3125 * directly.
3126 * @param view The WebView used to load url.
3127 * @param url The URL to load.
3128 */
John Reck71e51422011-07-01 16:49:28 -07003129 @Override
3130 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003131 loadUrl(tab, url, null);
3132 }
3133
3134 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3135 if (tab != null) {
3136 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003137 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003138 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003139 if (tab.hasCrashed) {
3140 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3141 }
Michael Kolba53c9892011-10-05 13:31:40 -07003142 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003143 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003144 }
3145
3146 /**
3147 * Load UrlData into a Tab and update the title bar to reflect the new
3148 * load. Call this instead of UrlData.loadIn directly.
3149 * @param t The Tab used to load.
3150 * @param data The UrlData being loaded.
3151 */
3152 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003153 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003154 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003155 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003156 } else {
John Reck38b39652012-06-05 09:22:59 -07003157 if (t != null && data.mDisableUrlOverride) {
3158 t.disableUrlOverridingForLoad();
3159 }
John Reck26b18322011-06-21 13:08:58 -07003160 loadUrl(t, data.mUrl, data.mHeaders);
3161 }
3162 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003163 }
3164
John Reck30c714c2010-12-16 17:30:34 -08003165 @Override
3166 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003167 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003168 //In case of tab can go back (aka tab has navigation entry) do nothing
3169 //else just load homepage in current tab.
3170 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003171 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003172 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003173 }
3174
3175 void goBackOnePageOrQuit() {
3176 Tab current = mTabControl.getCurrentTab();
3177 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003178 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3179 showExitDialog(mActivity);
3180 } else {
3181 /*
3182 * Instead of finishing the activity, simply push this to the back
3183 * of the stack and let ActivityManager to choose the foreground
3184 * activity. As BrowserActivity is singleTask, it will be always the
3185 * root of the task. So we can use either true or false for
3186 * moveTaskToBack().
3187 */
3188 mActivity.moveTaskToBack(true);
3189 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003190 return;
3191 }
John Reckef654f12011-07-12 16:42:08 -07003192 if (current.canGoBack()) {
3193 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003194 } else {
3195 // Check to see if we are closing a window that was created by
3196 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003197 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003198 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003199 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003200 // Now we close the other tab
3201 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003202 } else if (BrowserConfig.getInstance(getContext())
3203 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3204 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003205 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003206 if ((current.getAppId() != null) || current.closeOnBack()) {
3207 closeCurrentTab(true);
3208 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003209 /*
3210 * Instead of finishing the activity, simply push this to the back
3211 * of the stack and let ActivityManager to choose the foreground
3212 * activity. As BrowserActivity is singleTask, it will be always the
3213 * root of the task. So we can use either true or false for
3214 * moveTaskToBack().
3215 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003216 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003217 }
3218 }
3219 }
3220
3221 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003222 * helper method for key handler
3223 * returns the current tab if it can't advance
3224 */
Michael Kolbc831b632011-05-11 09:30:34 -07003225 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003226 int pos = mTabControl.getCurrentPosition() + 1;
3227 if (pos >= mTabControl.getTabCount()) {
3228 pos = 0;
3229 }
3230 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003231 }
3232
3233 /**
3234 * helper method for key handler
3235 * returns the current tab if it can't advance
3236 */
Michael Kolbc831b632011-05-11 09:30:34 -07003237 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003238 int pos = mTabControl.getCurrentPosition() - 1;
3239 if ( pos < 0) {
3240 pos = mTabControl.getTabCount() - 1;
3241 }
3242 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003243 }
3244
John Reckbcef87f2012-02-03 14:58:34 -08003245 boolean isMenuOrCtrlKey(int keyCode) {
3246 return (KeyEvent.KEYCODE_MENU == keyCode)
3247 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3248 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3249 }
3250
Michael Kolb0035fad2011-03-14 13:25:28 -07003251 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003252 * handle key events in browser
3253 *
3254 * @param keyCode
3255 * @param event
3256 * @return true if handled, false to pass to super
3257 */
John Reck9c35b9c2012-05-30 10:08:50 -07003258 @Override
3259 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003260 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3261 // Hardware menu key
Enrico Ros1f5a0952014-11-18 20:15:48 -08003262 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
Pankaj Garg49b79252014-11-07 17:33:41 -08003263 true, false);
3264 return true;
3265 }
3266
Cary Clark160bbb92011-01-10 11:17:07 -05003267 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003268 // Even if MENU is already held down, we need to call to super to open
3269 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003270 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003271 mMenuIsDown = true;
3272 return false;
3273 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003274
Cary Clark8ff8c662010-12-29 15:03:05 -05003275 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003276 Tab tab = getCurrentTab();
3277 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003278
Cary Clark160bbb92011-01-10 11:17:07 -05003279 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3280 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003281
Michael Kolb8233fac2010-10-26 16:08:53 -07003282 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003283 case KeyEvent.KEYCODE_TAB:
3284 if (event.isCtrlPressed()) {
3285 if (event.isShiftPressed()) {
3286 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003287 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003288 } else {
3289 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003290 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003291 }
3292 return true;
3293 }
3294 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003295 case KeyEvent.KEYCODE_SPACE:
3296 // WebView/WebTextView handle the keys in the KeyDown. As
3297 // the Activity's shortcut keys are only handled when WebView
3298 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003299 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003300 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003301 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003302 pageDown();
3303 }
3304 return true;
3305 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003306 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003307 event.startTracking();
3308 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003309 case KeyEvent.KEYCODE_FORWARD:
3310 if (!noModifiers) break;
3311 tab.goForward();
3312 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003313 case KeyEvent.KEYCODE_DPAD_LEFT:
3314 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003315 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003316 return true;
3317 }
3318 break;
3319 case KeyEvent.KEYCODE_DPAD_RIGHT:
3320 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003321 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003322 return true;
3323 }
3324 break;
3325 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003326 if (ctrl) {
3327 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003328 return true;
3329 }
3330 break;
Michael Kolba4183062011-01-16 10:43:21 -08003331// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003332 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003333 if (ctrl ) {
3334 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003335 return true;
3336 }
3337 break;
Michael Kolba4183062011-01-16 10:43:21 -08003338// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003339// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003340// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003341// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003342// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003343// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003344// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003345// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003346// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003347// case KeyEvent.KEYCODE_M: // unused
3348// case KeyEvent.KEYCODE_N: // in Chrome: new window
3349// case KeyEvent.KEYCODE_O: // in Chrome: open file
3350// case KeyEvent.KEYCODE_P: // in Chrome: print page
3351// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003352// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003353// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3354 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003355 // we can't use the ctrl/shift flags, they check for
3356 // exclusive use of a modifier
3357 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003358 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003359 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003360 } else {
3361 openTabToHomePage();
3362 }
3363 return true;
3364 }
3365 break;
3366// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3367// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003368// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003369// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3370// case KeyEvent.KEYCODE_Y: // unused
3371// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003372 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003373 // it is a regular key and webview is not null
3374 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003375 }
3376
John Reck9c35b9c2012-05-30 10:08:50 -07003377 @Override
3378 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003379 switch(keyCode) {
3380 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003381 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003382 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003383 return true;
3384 }
3385 break;
3386 }
3387 return false;
3388 }
3389
John Reck9c35b9c2012-05-30 10:08:50 -07003390 @Override
3391 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003392 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003393 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003394 if (KeyEvent.KEYCODE_MENU == keyCode
3395 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003396 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003397 }
3398 }
Cary Clark160bbb92011-01-10 11:17:07 -05003399 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003400 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003401 case KeyEvent.KEYCODE_BACK:
3402 if (event.isTracking() && !event.isCanceled()) {
3403 onBackKey();
3404 return true;
3405 }
3406 break;
3407 }
3408 return false;
3409 }
3410
3411 public boolean isMenuDown() {
3412 return mMenuIsDown;
3413 }
3414
John Reck9c35b9c2012-05-30 10:08:50 -07003415 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003416 public void setupAutoFill(Message message) {
3417 // Open the settings activity at the AutoFill profile fragment so that
3418 // the user can create a new profile. When they return, we will dispatch
3419 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003420 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003421 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3422 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003423 }
John Reckb3417f02011-01-14 11:01:05 -08003424
John Reck9c35b9c2012-05-30 10:08:50 -07003425 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003426 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003427 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003428 return true;
3429 }
3430
John Reck1cf4b792011-07-26 10:22:22 -07003431 @Override
3432 public boolean shouldCaptureThumbnails() {
3433 return mUi.shouldCaptureThumbnails();
3434 }
3435
Michael Kolbc3af0672011-08-09 10:24:41 -07003436 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003437 public boolean supportsVoice() {
3438 PackageManager pm = mActivity.getPackageManager();
3439 List activities = pm.queryIntentActivities(new Intent(
3440 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3441 return activities.size() != 0;
3442 }
3443
3444 @Override
3445 public void startVoiceRecognizer() {
3446 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003447 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003448 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3449 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3450 mActivity.startActivityForResult(voice, VOICE_RESULT);
3451 }
3452
Pankaj Garg7b279f62014-08-12 14:47:18 -07003453 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003454 if (mLevel == level)
3455 return;
3456 mLevel = level;
3457 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003458 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3459 lp.dimAmount = level;
3460 mActivity.getWindow().setAttributes(lp);
3461 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3462 } else {
3463 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3464 }
3465 }
3466
Michael Kolb0b129122012-06-04 16:31:58 -07003467 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003468 public void setBlockEvents(boolean block) {
3469 mBlockEvents = block;
3470 }
3471
John Reck9c35b9c2012-05-30 10:08:50 -07003472 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003473 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003474 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003475 }
3476
John Reck9c35b9c2012-05-30 10:08:50 -07003477 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003478 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003479 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003480 }
3481
John Reck9c35b9c2012-05-30 10:08:50 -07003482 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003483 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003484 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003485 }
3486
John Reck9c35b9c2012-05-30 10:08:50 -07003487 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003488 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003489 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003490 }
3491
John Reck9c35b9c2012-05-30 10:08:50 -07003492 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003493 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003494 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003495 }
3496
Pankaj Garg49b79252014-11-07 17:33:41 -08003497 @Override
3498 public boolean shouldShowAppMenu() {
3499 return true;
3500 }
3501
3502 @Override
3503 public int getMenuThemeResourceId() {
3504 return R.style.OverflowMenuTheme;
3505 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003506}