blob: 8c88c7d21394ea58f239d8fc00f3375cf50e1306 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
Dave Tharp851e8d52015-04-26 14:58:18 -07004 * Copyright (c) 2015 The Linux Foundation, All rights reserved.
Site Maoabb7bd32015-03-20 15:34:02 -07005 *
Michael Kolb8233fac2010-10-26 16:08:53 -07006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Bijan Amirzada41242f22014-03-21 12:12:18 -070019package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070020
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080022
luxiaolb40014b2013-07-19 10:01:43 +080023import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070025import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070026import android.app.ProgressDialog;
Vivek Sekharce2a4832014-03-26 13:26:53 -070027import android.content.Context;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070030import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070031import android.content.ContentValues;
John Reck68234a92012-04-19 15:27:12 -070032import android.content.DialogInterface;
33import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.content.Intent;
Site Maoabb7bd32015-03-20 15:34:02 -070035import android.content.IntentFilter;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
38import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070039import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050040import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.database.Cursor;
42import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080044import android.graphics.BitmapFactory;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080045import android.graphics.Rect;
Tarun Nainanif5563b62015-01-21 18:52:59 -080046import android.media.AudioManager;
kaiyiza016da12013-08-26 17:50:22 +080047import android.net.ConnectivityManager;
48import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070049import android.net.Uri;
50import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080051import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080052import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.os.AsyncTask;
Tarun Nainanif5563b62015-01-21 18:52:59 -080054import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070056import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.os.Handler;
58import android.os.Message;
59import android.os.PowerManager;
60import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070061import android.provider.ContactsContract;
62import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080063import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070064import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070065import android.text.TextUtils;
66import android.util.Log;
67import android.view.ActionMode;
68import android.view.ContextMenu;
69import android.view.ContextMenu.ContextMenuInfo;
70import android.view.Gravity;
71import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070072import android.view.Menu;
73import android.view.MenuInflater;
74import android.view.MenuItem;
75import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070076import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070077import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070078import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070079import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070080import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080081import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080082import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050083import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070084
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080085import org.codeaurora.swe.CookieManager;
86import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080087import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080088import org.codeaurora.swe.HttpAuthHandler;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080089import org.codeaurora.swe.WebSettings;
90import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070091import org.codeaurora.swe.WebBackForwardList;
92import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080093
Bijan Amirzada41242f22014-03-21 12:12:18 -070094import com.android.browser.IntentHandler.UrlData;
95import com.android.browser.UI.ComboViews;
Dave Tharp13f3f7c2015-07-16 14:58:51 -070096import com.android.browser.mdm.DownloadDirRestriction;
Dave Tharp9a082b12015-06-19 08:46:57 -070097import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -070098import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -070099import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700100import com.android.browser.mynavigation.AddMyNavigationPage;
101import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700102import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700103import com.android.browser.platformsupport.BrowserContract;
Pankaj Garg18902562014-12-05 16:18:51 -0800104import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700105import com.android.browser.provider.BrowserProvider2.Thumbnails;
106import com.android.browser.provider.SnapshotProvider.Snapshots;
107import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800108import com.android.browser.appmenu.AppMenuHandler;
109import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700110
Michael Kolb8233fac2010-10-26 16:08:53 -0700111import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700112import java.io.File;
113import java.io.FileOutputStream;
114import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700116import java.text.DateFormat;
117import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700118import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700119import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700120import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800121import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200122import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700123import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700124
125/**
126 * Controller for browser
127 */
128public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800129 implements WebViewController, UiController, ActivityController,
130 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700131
132 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800133 private static final String SEND_APP_ID_EXTRA =
134 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -0700135 public static final String INCOGNITO_URI = "browser://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800136 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
137 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800138
Vivek Sekharb54614f2014-05-01 19:03:37 -0700139 // Remind switch to data connection if wifi is unavailable
140 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800141
Michael Kolb8233fac2010-10-26 16:08:53 -0700142 // public message ids
143 public final static int LOAD_URL = 1001;
144 public final static int STOP_LOAD = 1002;
145
146 // Message Ids
147 private static final int FOCUS_NODE_HREF = 102;
148 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800149 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800152 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700153
154 private static final int EMPTY_MENU = -1;
155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700157 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 final static int PREFERENCES_PAGE = 3;
159 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700161 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800162 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000163
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
165
166 // As the ids are dynamically created, we can't guarantee that they will
167 // be in sequence, so this static array maps ids to a window number.
168 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
169 { R.id.window_one_menu_id, R.id.window_two_menu_id,
170 R.id.window_three_menu_id, R.id.window_four_menu_id,
171 R.id.window_five_menu_id, R.id.window_six_menu_id,
172 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
173
174 // "source" parameter for Google search through search key
175 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
176 // "source" parameter for Google search through simplily type
177 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
178
George Mount387d45d2011-10-07 15:57:53 -0700179 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700180 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
181
John Reckd7dd9b22011-08-30 09:18:29 -0700182 // A bitmap that is re-used in createScreenshot as scratch space
183 private static Bitmap sThumbnailBitmap;
184
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 private Activity mActivity;
186 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700187 private HomepageHandler mHomepageHandler;
188 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private BrowserSettings mSettings;
190 private WebViewFactory mFactory;
191
192 private WakeLock mWakeLock;
193
194 private UrlHandler mUrlHandler;
195 private UploadHandler mUploadHandler;
196 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 private NetworkStateHandler mNetworkHandler;
198
Ben Murdoch8029a772010-11-16 11:58:21 +0000199 private Message mAutoFillSetupMessage;
200
kaiyiza016da12013-08-26 17:50:22 +0800201 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700202
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 // FIXME, temp address onPrepareMenu performance problem.
204 // When we move everything out of view, we should rewrite this.
205 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700206 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 private int mOldMenuState = EMPTY_MENU;
208 private Menu mCachedMenu;
209
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 private boolean mMenuIsDown;
211
Pankaj Garg49b79252014-11-07 17:33:41 -0800212 private boolean mWasInPageLoad = false;
213 private AppMenuHandler mAppMenuHandler;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 // For select and find, we keep track of the ActionMode so that
216 // finish() can be called as desired.
217 private ActionMode mActionMode;
218
219 /**
220 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
221 * of whether the configuration has changed. The first onMenuOpened call
222 * after a configuration change is simply a reopening of the same menu
223 * (i.e. mIconView did not change).
224 */
225 private boolean mConfigChanged;
226
227 /**
228 * Keeps track of whether the options menu is open. This is important in
229 * determining whether to show or hide the title bar overlay
230 */
231 private boolean mOptionsMenuOpen;
232
233 /**
234 * Whether or not the options menu is in its bigger, popup menu form. When
235 * true, we want the title bar overlay to be gone. When false, we do not.
236 * Only meaningful if mOptionsMenuOpen is true.
237 */
238 private boolean mExtendedMenuOpen;
239
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 private boolean mActivityPaused = true;
241 private boolean mLoadStopped;
242
243 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500244 // Checks to see when the bookmarks database has changed, and updates the
245 // Tabs' notion of whether they represent bookmarked sites.
246 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700247 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700248
Michael Kolbc3af0672011-08-09 10:24:41 -0700249 private boolean mBlockEvents;
250
Michael Kolb0b129122012-06-04 16:31:58 -0700251 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800252 private boolean mUpdateMyNavThumbnail;
253 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800254 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800255 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700256 private PowerConnectionReceiver mLowPowerReceiver;
257 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700258
Pankaj Garg634bf432015-07-13 09:54:21 -0700259 private boolean mCurrentPageBookmarked;
260
George Mount3636d0a2011-11-21 09:08:21 -0800261 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 mActivity = browser;
263 mSettings = BrowserSettings.getInstance();
264 mTabControl = new TabControl(this);
265 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700266 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800267 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700268 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700269
270 mUrlHandler = new UrlHandler(this);
271 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700272
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500274 mBookmarksObserver = new ContentObserver(mHandler) {
275 @Override
276 public void onChange(boolean selfChange) {
277 int size = mTabControl.getTabCount();
278 for (int i = 0; i < size; i++) {
279 mTabControl.getTab(i).updateBookmarkedStatus();
280 }
281 }
282
283 };
284 browser.getContentResolver().registerContentObserver(
285 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700286
287 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700288 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800289 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700290 }
291
John Reck9c35b9c2012-05-30 10:08:50 -0700292 @Override
293 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700294 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800295 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800296 // mCrashRecoverHandler has any previously saved state.
297 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700298 }
299
George Mount3636d0a2011-11-21 09:08:21 -0800300 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800301 // we dont want to ever recover incognito tabs
302 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700303
Patrick Scott7d50a932011-02-04 09:27:26 -0500304 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700305 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500306 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000307
Michael Kolbc831b632011-05-11 09:30:34 -0700308 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500309 // Not able to restore so we go ahead and clear session cookies. We
310 // must do this before trying to login the user as we don't want to
311 // clear any session cookies set during login.
312 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700313 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800314 if (intent == null) {
315 // This won't happen under common scenarios. The icicle is
316 // not null, but there aren't any tabs to restore.
317 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700318 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800319 final Bundle extra = intent.getExtras();
320 // Create an initial tab.
321 // If the intent is ACTION_VIEW and data is not null, the Browser is
322 // invoked to view the content by another application. In this case,
323 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800324 UrlData urlData = null;
325 if (intent.getData() != null
326 && Intent.ACTION_VIEW.equals(intent.getAction())
327 && intent.getData().toString().startsWith("content://")) {
328 urlData = new UrlData(intent.getData().toString());
329 } else {
330 urlData = IntentHandler.getUrlDataFromIntent(intent);
331 }
George Mount3636d0a2011-11-21 09:08:21 -0800332 Tab t = null;
333 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700334 String landingPage = mActivity.getResources().getString(
335 R.string.def_landing_page);
336 if (!landingPage.isEmpty()) {
337 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800338 } else {
339 t = openTabToHomePage();
340 }
George Mount3636d0a2011-11-21 09:08:21 -0800341 } else {
342 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700343 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800344 }
345 if (t != null) {
346 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
347 }
348 WebView webView = t.getWebView();
349 if (extra != null) {
350 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
351 if (scale > 0 && scale <= 1000) {
352 webView.setInitialScale(scale);
353 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700354 }
355 }
John Reckd8c74522011-06-14 08:45:00 -0700356 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700358 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500359 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700360 List<Tab> tabs = mTabControl.getTabs();
361 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700362
John Reck1cf4b792011-07-26 10:22:22 -0700363 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700364 //handle restored pages that may require a JS interface
365 if (t.getWebView() != null) {
366 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
367 if (backForwardList != null) {
368 for (int i = 0; i < backForwardList.getSize(); i++) {
369 WebHistoryItem item = backForwardList.getItemAtIndex(i);
370 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
371 }
372 }
373 }
John Reck1cf4b792011-07-26 10:22:22 -0700374 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700375 if (t != mTabControl.getCurrentTab()) {
376 t.pause();
377 }
John Reck1cf4b792011-07-26 10:22:22 -0700378 }
379 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700380 if (tabs.size() == 0) {
381 openTabToHomePage();
382 }
John Reck1cf4b792011-07-26 10:22:22 -0700383 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700384 // TabControl.restoreState() will create a new tab even if
385 // restoring the state fails.
386 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800387 // Intent is non-null when framework thinks the browser should be
388 // launching with a new intent (icicle is null).
389 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700390 mIntentHandler.onNewIntent(intent);
391 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700392 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700394 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800395 if (jsFlags.trim().length() != 0) {
396 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 }
George Mount3636d0a2011-11-21 09:08:21 -0800398 if (intent != null
399 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700400 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800401 }
Site Maoabb7bd32015-03-20 15:34:02 -0700402 mLowPowerReceiver = new PowerConnectionReceiver();
403 mPowerChangeReceiver = new PowerConnectionReceiver();
404
405 //always track the android framework's power save mode
406 IntentFilter filter = new IntentFilter();
407 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
408 // Power save mode only exists in Lollipop and above
409 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
410 }
411 filter.addAction(Intent.ACTION_BATTERY_OKAY);
412 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700413 }
414
John Reck1cf4b792011-07-26 10:22:22 -0700415 private static class PruneThumbnails implements Runnable {
416 private Context mContext;
417 private List<Long> mIds;
418
419 PruneThumbnails(Context context, List<Long> preserveIds) {
420 mContext = context.getApplicationContext();
421 mIds = preserveIds;
422 }
423
424 @Override
425 public void run() {
426 ContentResolver cr = mContext.getContentResolver();
427 if (mIds == null || mIds.size() == 0) {
428 cr.delete(Thumbnails.CONTENT_URI, null, null);
429 } else {
430 int length = mIds.size();
431 StringBuilder where = new StringBuilder();
432 where.append(Thumbnails._ID);
433 where.append(" not in (");
434 for (int i = 0; i < length; i++) {
435 where.append(mIds.get(i));
436 if (i < (length - 1)) {
437 where.append(",");
438 }
439 }
440 where.append(")");
441 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
442 }
443 }
444
445 }
446
Michael Kolb1514bb72010-11-22 09:11:48 -0800447 @Override
448 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700449 return mFactory;
450 }
451
452 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800453 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700454 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700455 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800456 }
457
458 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800459 public void createSubWindow(Tab tab) {
460 endActionMode();
461 WebView mainView = tab.getWebView();
462 WebView subView = mFactory.createWebView((mainView == null)
463 ? false
464 : mainView.isPrivateBrowsingEnabled());
465 mUi.createSubWindow(tab, subView);
466 }
467
468 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700469 public Context getContext() {
470 return mActivity;
471 }
472
473 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700474 public Activity getActivity() {
475 return mActivity;
476 }
477
478 void setUi(UI ui) {
479 mUi = ui;
480 }
481
Michael Kolbaf63dba2012-05-16 12:58:05 -0700482 @Override
483 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 return mSettings;
485 }
486
487 IntentHandler getIntentHandler() {
488 return mIntentHandler;
489 }
490
491 @Override
492 public UI getUi() {
493 return mUi;
494 }
495
496 int getMaxTabs() {
497 return mActivity.getResources().getInteger(R.integer.max_tabs);
498 }
499
500 @Override
501 public TabControl getTabControl() {
502 return mTabControl;
503 }
504
Michael Kolb1bf23132010-11-19 12:55:12 -0800505 @Override
506 public List<Tab> getTabs() {
507 return mTabControl.getTabs();
508 }
509
Michael Kolb8233fac2010-10-26 16:08:53 -0700510 private void startHandler() {
511 mHandler = new Handler() {
512
513 @Override
514 public void handleMessage(Message msg) {
515 switch (msg.what) {
516 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700517 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700518 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800519 case UNKNOWN_TYPE_MSG:
520 HashMap unknownTypeMap = (HashMap) msg.obj;
521 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
522 /*
523 * When the context menu is shown to the user
524 * we need to assure that its happening on the current webview
525 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
526 */
527 if (getCurrentWebView() != viewForUnknownType)
528 break;
529
530 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800531 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800532 WebView.HitTestResult result = new WebView.HitTestResult();
533
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800534 // Prevent unnecessary calls to context menu
535 // if url and image src are null
536 if (unknown_type_src == null && unknown_type_url == null)
537 break;
538
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800539 //setting the HitTestResult with new RESULT TYPE
540 if (!TextUtils.isEmpty(unknown_type_src)) {
541 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
542 result.setExtra(unknown_type_src);
543 } else {
544 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
545 result.setExtra("about:blank");
546 }
547
548 mResult = result;
549 openContextMenu(viewForUnknownType);
550 mResult = null;
551
552 break;
553
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 case FOCUS_NODE_HREF:
555 {
556 String url = (String) msg.getData().get("url");
557 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500558 String src = (String) msg.getData().get("src");
559 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 if (TextUtils.isEmpty(url)) {
561 break;
562 }
563 HashMap focusNodeMap = (HashMap) msg.obj;
564 WebView view = (WebView) focusNodeMap.get("webview");
565 // Only apply the action if the top window did not change.
566 if (getCurrentTopWebView() != view) {
567 break;
568 }
569 switch (msg.arg1) {
570 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700571 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700572 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500573 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700574 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500575 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500576 case R.id.open_newtab_context_menu_id:
577 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700578 openTab(url, parent,
579 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500580 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 case R.id.copy_link_context_menu_id:
582 copy(url);
583 break;
584 case R.id.save_link_context_menu_id:
585 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500586 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800587 mActivity, url, view.getSettings().getUserAgentString(),
588 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700589 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700590 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700591 if(title == null || title == "")
592 title = url;
593
594 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
595 //SWE TODO: No thumbnail support for the url obtained via
596 //browser context menu as its not loaded in webview.
597 if (bookmarkIntent != null) {
598 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
599 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
600 mActivity.startActivity(bookmarkIntent);
601 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700602 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 }
604 break;
605 }
606
607 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700608 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700609 break;
610
611 case STOP_LOAD:
612 stopLoading();
613 break;
614
615 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700616 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 mWakeLock.release();
618 // if we reach here, Browser should be still in the
619 // background loading after WAKELOCK_TIMEOUT (5-min).
620 // To avoid burning the battery, stop loading.
621 mTabControl.stopAllLoading();
622 }
623 break;
624
kaiyiz591110b2013-08-06 17:11:06 +0800625 case OPEN_MENU:
626 if (!mOptionsMenuOpen && mActivity != null ) {
627 mActivity.openOptionsMenu();
628 }
629 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700630 }
631 }
632 };
633
634 }
635
John Reckef654f12011-07-12 16:42:08 -0700636 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200637 public Tab getCurrentTab() {
638 return mTabControl.getCurrentTab();
639 }
640
Michael Kolbba99c5d2010-11-29 14:57:41 -0800641 @Override
642 public void shareCurrentPage() {
643 shareCurrentPage(mTabControl.getCurrentTab());
644 }
645
646 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700647 if (tab == null || tab.getWebView() == null)
648 return;
649
650 final Tab mytab = tab;
651 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
652 @Override
653 public void onReceiveValue(Bitmap bitmap) {
654 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
655 mytab.getFavicon(), bitmap);
656 }
657 };
658
659 createScreenshotAsync(
660 tab.getWebView(),
661 getDesiredThumbnailWidth(mActivity),
662 getDesiredThumbnailHeight(mActivity),
663 new ValueCallback<Bitmap>() {
664 @Override
665 public void onReceiveValue(Bitmap bitmap) {
666 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
667 mytab.getFavicon(), bitmap);
668 }
669 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800670 }
671
Michael Kolb8233fac2010-10-26 16:08:53 -0700672 /**
673 * Share a page, providing the title, url, favicon, and a screenshot. Uses
674 * an {@link Intent} to launch the Activity chooser.
675 * @param c Context used to launch a new Activity.
676 * @param title Title of the page. Stored in the Intent with
677 * {@link Intent#EXTRA_SUBJECT}
678 * @param url URL of the page. Stored in the Intent with
679 * {@link Intent#EXTRA_TEXT}
680 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
681 * with {@link Browser#EXTRA_SHARE_FAVICON}
682 * @param screenshot Bitmap of a screenshot of the page. Stored in the
683 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
684 */
685 static final void sharePage(Context c, String title, String url,
686 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700687
688 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
689 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
690 R.layout.app_row, sDialog.getApps());
691 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 }
693
694 private void copy(CharSequence text) {
695 ClipboardManager cm = (ClipboardManager) mActivity
696 .getSystemService(Context.CLIPBOARD_SERVICE);
697 cm.setText(text);
698 }
699
700 // lifecycle
701
John Reck9c35b9c2012-05-30 10:08:50 -0700702 @Override
703 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800705 // update the menu in case of a locale change
706 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800707 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800708 if (mOptionsMenuOpen) {
709 mActivity.closeOptionsMenu();
710 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
711 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 mUi.onConfigurationChanged(config);
713 }
714
715 @Override
716 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700717 if (!mUi.isWebShowing()) {
718 mUi.showWeb(false);
719 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700720 mIntentHandler.onNewIntent(intent);
721 }
722
John Reck9c35b9c2012-05-30 10:08:50 -0700723 @Override
724 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800725 if (mUi.isCustomViewShowing()) {
726 hideCustomView();
727 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700728 if (mActivityPaused) {
729 Log.e(LOGTAG, "BrowserActivity is already paused.");
730 return;
731 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700732 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800733 Tab tab = mTabControl.getCurrentTab();
734 if (tab != null) {
735 tab.pause();
736 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700737 if (mWakeLock == null) {
738 PowerManager pm = (PowerManager) mActivity
739 .getSystemService(Context.POWER_SERVICE);
740 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
741 }
Michael Kolb70976932010-11-30 11:34:01 -0800742 mWakeLock.acquire();
743 mHandler.sendMessageDelayed(mHandler
744 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
745 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700746 }
747 mUi.onPause();
748 mNetworkHandler.onPause();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100749 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700750 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700751 }
752
John Reck9c35b9c2012-05-30 10:08:50 -0700753 @Override
754 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700755 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800756 Bundle saveState = createSaveState();
757
758 // crash recovery manages all save & restore state
759 mCrashRecoveryHandler.writeState(saveState);
760 mSettings.setLastRunPaused(true);
761 }
762
763 /**
764 * Save the current state to outState. Does not write the state to
765 * disk.
766 * @return Bundle containing the current state of all tabs.
767 */
768 /* package */ Bundle createSaveState() {
769 Bundle saveState = new Bundle();
770 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800771 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700772 }
773
John Reck9c35b9c2012-05-30 10:08:50 -0700774 @Override
775 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700776 if (!mActivityPaused) {
777 Log.e(LOGTAG, "BrowserActivity is already resumed.");
778 return;
779 }
Vivek Sekharb53a5952015-07-17 14:14:38 -0700780 UpdateNotificationService.updateCheck(mActivity);
George Mount3636d0a2011-11-21 09:08:21 -0800781 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700782 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800783 Tab current = mTabControl.getCurrentTab();
784 if (current != null) {
785 current.resume();
786 resumeWebViewTimers(current);
787 }
John Reckf57c0292011-07-21 18:15:39 -0700788 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200789
Michael Kolb8233fac2010-10-26 16:08:53 -0700790 mUi.onResume();
791 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100792 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700793 if (mVoiceResult != null) {
794 mUi.onVoiceResult(mVoiceResult);
795 mVoiceResult = null;
796 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700797 if (current != null && current.getWebView().isShowingCrashView())
798 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700799 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 }
801
John Reckf57c0292011-07-21 18:15:39 -0700802 private void releaseWakeLock() {
803 if (mWakeLock != null && mWakeLock.isHeld()) {
804 mHandler.removeMessages(RELEASE_WAKELOCK);
805 mWakeLock.release();
806 }
807 }
808
Michael Kolb70976932010-11-30 11:34:01 -0800809 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800810 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800811 * @param tab guaranteed non-null
812 */
813 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 boolean inLoad = tab.inPageLoad();
815 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
816 CookieSyncManager.getInstance().startSync();
817 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100818 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 }
820 }
821
Michael Kolb70976932010-11-30 11:34:01 -0800822 /**
823 * Pause all WebView timers using the WebView of the given tab
824 * @param tab
825 * @return true if the timers are paused or tab is null
826 */
827 private boolean pauseWebViewTimers(Tab tab) {
828 if (tab == null) {
829 return true;
830 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100832 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700833 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700834 }
Michael Kolb70976932010-11-30 11:34:01 -0800835 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700836 }
837
John Reck9c35b9c2012-05-30 10:08:50 -0700838 @Override
839 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800840 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
842 mUploadHandler = null;
843 }
844 if (mTabControl == null) return;
845 mUi.onDestroy();
846 // Remove the current tab and sub window
847 Tab t = mTabControl.getCurrentTab();
848 if (t != null) {
849 dismissSubWindow(t);
850 removeTab(t);
851 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500852 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 // Destroy all the tabs
854 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700855 // Unregister receiver
856 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700857 }
858
859 protected boolean isActivityPaused() {
860 return mActivityPaused;
861 }
862
John Reck9c35b9c2012-05-30 10:08:50 -0700863 @Override
864 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 mTabControl.freeMemory();
866 }
867
868 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 public void stopLoading() {
870 mLoadStopped = true;
871 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700873 if (w != null) {
874 w.stopLoading();
875 mUi.onPageStopped(tab);
876 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700877 }
878
879 boolean didUserStopLoading() {
880 return mLoadStopped;
881 }
882
kaiyiza016da12013-08-26 17:50:22 +0800883 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700884 final String reminderType = getContext().getResources().getString(
885 R.string.def_wifi_browser_interaction_remind_type);
886 final String selectionConnnection = getContext().getResources().getString(
887 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700888 final String wifiSelection = getContext().getResources().getString(
889 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700890
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700891 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
892 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700893 return;
894
kaiyiza016da12013-08-26 17:50:22 +0800895 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700896 Context.CONNECTIVITY_SERVICE);
897 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700898 WifiManager wifiMgr = (WifiManager) this.getContext()
899 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700900 if (networkInfo == null
901 || (networkInfo != null && (networkInfo.getType() !=
902 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700903 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
904 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700905 List<ScanResult> list = wifiMgr.getScanResults();
906 // Have no AP's for Wifi's fall back to data
907 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700908 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700909 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
910 this.getContext().startActivity(intent);
911 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700912 // Request to select Wifi AP
913 try {
914 Intent intent = new Intent(wifiSelection);
915 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
916 this.getContext().startActivity(intent);
917 } catch (Exception e) {
918 String err_msg = this.getContext().getString(
919 R.string.acivity_not_found, wifiSelection);
920 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
921 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700922 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700923 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800924 }
925 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700926
Michael Kolb8233fac2010-10-26 16:08:53 -0700927 // WebViewController
928
929 @Override
John Reck324d4402011-01-11 16:56:42 -0800930 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700931
Michael Kolb8233fac2010-10-26 16:08:53 -0700932 // reset sync timer to avoid sync starts during loading a page
933 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700934 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700935 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800936 boolean networkNotifier = BrowserConfig.getInstance(getContext())
937 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700938 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700939 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800940 } else {
941 if (!mNetworkHandler.isNetworkUp()) {
942 view.setNetworkAvailable(false);
943 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700944 }
945
946 // when BrowserActivity just starts, onPageStarted may be called before
947 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
948 // to start the timer. As we won't switch tabs while an activity is in
949 // pause state, we can ensure calling resume and pause in pair.
950 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800951 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700952 }
953 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700954 endActionMode();
955
John Reck30c714c2010-12-16 17:30:34 -0800956 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700957
John Reck324d4402011-01-11 16:56:42 -0800958 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700959 // update the bookmark database for favicon
960 maybeUpdateFavicon(tab, null, url, favicon);
961
962 Performance.tracePageStart(url);
963
964 // Performance probe
965 if (false) {
966 Performance.onPageStarted();
967 }
968
969 }
970
971 @Override
John Reck324d4402011-01-11 16:56:42 -0800972 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700973 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800974 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200975
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 // Performance probe
977 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800978 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 }
980
Tarun Nainani8eb00912014-07-17 12:28:32 -0700981 tab.onPageFinished();
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700982 maybeUpdateFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700983
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 }
1013 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001014 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001015 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001016 // still loading
1017 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001019 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001020 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001021 } else {
1022 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001023 }
1024 }
Vivek Sekhar30d0a2e2015-07-28 19:38:01 -07001025 if (isInLoad())
1026 mUi.onProgressChanged(tab);
John Reck30c714c2010-12-16 17:30:34 -08001027 }
1028
1029 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001030 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001031 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001032 }
1033
1034 @Override
1035 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001036 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001037 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001038 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1040 return;
1041 }
1042 // Update the title in the history database if not in private browsing mode
1043 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001044 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001045 }
1046 }
1047
1048 @Override
1049 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001050 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001051 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1052 }
1053
1054 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001055 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07001056 // if tab is snapshot tab we want to prevent navigation from occuring
1057 // since snapshot tab opens a new tab with the url
1058 return goLive(url) || mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001059 }
1060
1061 @Override
1062 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1063 if (mMenuIsDown) {
1064 // only check shortcut key when MENU is held
1065 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1066 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001067 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001068 int keyCode = event.getKeyCode();
1069 // We need to send almost every key to WebKit. However:
1070 // 1. We don't want to block the device on the renderer for
1071 // some keys like menu, home, call.
1072 // 2. There are no WebKit equivalents for some of these keys
1073 // (see app/keyboard_codes_win.h)
1074 // Note that these are not the same set as KeyEvent.isSystemKey:
1075 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1076 if (keyCode == KeyEvent.KEYCODE_MENU ||
1077 keyCode == KeyEvent.KEYCODE_HOME ||
1078 keyCode == KeyEvent.KEYCODE_BACK ||
1079 keyCode == KeyEvent.KEYCODE_CALL ||
1080 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1081 keyCode == KeyEvent.KEYCODE_POWER ||
1082 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1083 keyCode == KeyEvent.KEYCODE_CAMERA ||
1084 keyCode == KeyEvent.KEYCODE_FOCUS ||
1085 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1086 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1087 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1088 return true;
1089 }
1090
1091 // We also have to intercept some shortcuts before we send them to the ContentView.
1092 if (event.isCtrlPressed() && (
1093 keyCode == KeyEvent.KEYCODE_TAB ||
1094 keyCode == KeyEvent.KEYCODE_W ||
1095 keyCode == KeyEvent.KEYCODE_F4)) {
1096 return true;
1097 }
1098
1099 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001100 }
1101
Tarun Nainanif5563b62015-01-21 18:52:59 -08001102 private void handleMediaKeyEvent(KeyEvent event) {
1103
1104 int keyCode = event.getKeyCode();
1105 // send media key events to audio manager
1106 if (Build.VERSION.SDK_INT >= 19) {
1107
1108 switch (keyCode) {
1109 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1110 case KeyEvent.KEYCODE_MEDIA_STOP:
1111 case KeyEvent.KEYCODE_MEDIA_NEXT:
1112 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1113 case KeyEvent.KEYCODE_MEDIA_REWIND:
1114 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1115 case KeyEvent.KEYCODE_MUTE:
1116 case KeyEvent.KEYCODE_MEDIA_PLAY:
1117 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1118 case KeyEvent.META_SHIFT_RIGHT_ON:
1119 case KeyEvent.KEYCODE_MEDIA_EJECT:
1120 case KeyEvent.KEYCODE_MEDIA_RECORD:
1121 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1122
1123 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1124 .getSystemService(Context.AUDIO_SERVICE);
1125 audioManager.dispatchMediaKeyEvent(event);
1126 }
1127 }
1128 }
1129
Michael Kolb8233fac2010-10-26 16:08:53 -07001130 @Override
John Reck997b1b72012-04-19 18:08:25 -07001131 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001132 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001133 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001134 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001135 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 } else {
John Reck997b1b72012-04-19 18:08:25 -07001137 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 }
1139 }
John Reck997b1b72012-04-19 18:08:25 -07001140 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 }
1142
1143 @Override
John Reck324d4402011-01-11 16:56:42 -08001144 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001145 // Don't save anything in private browsing mode or when disabling history
1146 // for regular tabs is enabled
1147 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1148 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1149 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001150
John Reck49a603c2011-03-03 09:33:05 -08001151 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001152
John Reck324d4402011-01-11 16:56:42 -08001153 if (TextUtils.isEmpty(url)
1154 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 return;
1156 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001157
John Reckf57c0292011-07-21 18:15:39 -07001158 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001159 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 }
1161
1162 @Override
1163 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1164 AsyncTask<Void, Void, String[]> task =
1165 new AsyncTask<Void, Void, String[]>() {
1166 @Override
1167 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001168 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 }
1170 @Override
1171 public void onPostExecute(String[] result) {
1172 callback.onReceiveValue(result);
1173 }
1174 };
1175 task.execute();
1176 }
1177
1178 @Override
1179 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1180 final HttpAuthHandler handler, final String host,
1181 final String realm) {
1182 String username = null;
1183 String password = null;
1184
1185 boolean reuseHttpAuthUsernamePassword
1186 = handler.useHttpAuthUsernamePassword();
1187
1188 if (reuseHttpAuthUsernamePassword && view != null) {
1189 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1190 if (credentials != null && credentials.length == 2) {
1191 username = credentials[0];
1192 password = credentials[1];
1193 }
1194 }
1195
1196 if (username != null && password != null) {
1197 handler.proceed(username, password);
1198 } else {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -07001199 if (!tab.inForeground()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001200 handler.cancel();
1201 }
1202 }
1203 }
1204
1205 @Override
1206 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001207 String contentDisposition, String mimetype, String referer,
1208 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001209 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001210 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001211 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001212 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001213 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001214 // This Tab was opened for the sole purpose of downloading a
1215 // file. Remove it.
1216 if (tab == mTabControl.getCurrentTab()) {
1217 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001218 if (tab.getDerivedFromIntent())
1219 closeTab(tab);
1220 else
1221 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001222 } else {
1223 // In this case, it is not.
1224 closeTab(tab);
1225 }
1226 }
1227 }
1228
1229 @Override
1230 public Bitmap getDefaultVideoPoster() {
1231 return mUi.getDefaultVideoPoster();
1232 }
1233
1234 @Override
1235 public View getVideoLoadingProgressView() {
1236 return mUi.getVideoLoadingProgressView();
1237 }
1238
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 // helper method
1240
1241 /*
1242 * Update the favorites icon if the private browsing isn't enabled and the
1243 * icon is valid.
1244 */
1245 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1246 final String url, Bitmap favicon) {
1247 if (favicon == null) {
1248 return;
1249 }
1250 if (!tab.isPrivateBrowsingEnabled()) {
1251 Bookmarks.updateFavicon(mActivity
1252 .getContentResolver(), originalUrl, url, favicon);
1253 }
1254 }
1255
Leon Scroggins4cd97792010-12-03 15:31:56 -05001256 @Override
1257 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001258 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001259 mUi.bookmarkedStatusHasChanged(tab);
1260 }
1261
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 // end WebViewController
1263
1264 protected void pageUp() {
1265 getCurrentTopWebView().pageUp(false);
1266 }
1267
1268 protected void pageDown() {
1269 getCurrentTopWebView().pageDown(false);
1270 }
1271
1272 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001273 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 public void editUrl() {
1275 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001276 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001277 }
1278
John Reck9c35b9c2012-05-30 10:08:50 -07001279 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001280 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001281 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 if (tab.inForeground()) {
1283 if (mUi.isCustomViewShowing()) {
1284 callback.onCustomViewHidden();
1285 return;
1286 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001287 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001288 // Save the menu state and set it to empty while the custom
1289 // view is showing.
1290 mOldMenuState = mMenuState;
1291 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001292 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 }
1294 }
1295
1296 @Override
1297 public void hideCustomView() {
1298 if (mUi.isCustomViewShowing()) {
1299 mUi.onHideCustomView();
1300 // Reset the old menu state.
1301 mMenuState = mOldMenuState;
1302 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001303 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 }
1305 }
1306
John Reck9c35b9c2012-05-30 10:08:50 -07001307 @Override
1308 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001309 Intent intent) {
1310 if (getCurrentTopWebView() == null) return;
1311 switch (requestCode) {
1312 case PREFERENCES_PAGE:
1313 if (resultCode == Activity.RESULT_OK && intent != null) {
1314 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001315 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001316 mTabControl.removeParentChildRelationShips();
1317 }
1318 }
1319 break;
1320 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001321 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001322 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001323 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001324 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001325 case AUTOFILL_SETUP:
1326 // Determine whether a profile was actually set up or not
1327 // and if so, send the message back to the WebTextView to
1328 // fill the form with the new profile.
1329 if (getSettings().getAutoFillProfile() != null) {
1330 mAutoFillSetupMessage.sendToTarget();
1331 mAutoFillSetupMessage = null;
1332 }
1333 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001334 case COMBO_VIEW:
1335 if (intent == null || resultCode != Activity.RESULT_OK) {
1336 break;
1337 }
John Reck3ba45532011-08-11 16:26:53 -07001338 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001339 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1340 Tab t = getCurrentTab();
1341 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001342 mUpdateMyNavThumbnail = true;
1343 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001344 loadUrl(t, uri.toString());
1345 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1346 String[] urls = intent.getStringArrayExtra(
1347 ComboViewActivity.EXTRA_OPEN_ALL);
1348 Tab parent = getCurrentTab();
1349 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001350 if (url != null) {
1351 parent = openTab(url, parent,
1352 !mSettings.openInBackground(), true);
1353 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001354 }
1355 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1356 long id = intent.getLongExtra(
1357 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1358 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001359 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001360 }
1361 }
1362 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001363 case VOICE_RESULT:
1364 if (resultCode == Activity.RESULT_OK && intent != null) {
1365 ArrayList<String> results = intent.getStringArrayListExtra(
1366 RecognizerIntent.EXTRA_RESULTS);
1367 if (results.size() >= 1) {
1368 mVoiceResult = results.get(0);
1369 }
1370 }
1371 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001372 case MY_NAVIGATION:
1373 if (intent == null || resultCode != Activity.RESULT_OK) {
1374 break;
1375 }
1376
1377 if (intent.getBooleanExtra("need_refresh", false) &&
1378 getCurrentTopWebView() != null) {
1379 getCurrentTopWebView().reload();
1380 }
1381 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001382 default:
1383 break;
1384 }
1385 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001386 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001387 }
1388
1389 /**
1390 * Open the Go page.
1391 * @param startWithHistory If true, open starting on the history tab.
1392 * Otherwise, start with the bookmarks tab.
1393 */
1394 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001395 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 if (mTabControl.getCurrentWebView() == null) {
1397 return;
1398 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001399 // clear action mode
1400 if (isInCustomActionMode()) {
1401 endActionMode();
1402 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001403 Bundle extras = new Bundle();
1404 // Disable opening in a new window if we have maxed out the windows
1405 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1406 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001407 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001408 }
1409
1410 // combo view callbacks
1411
Michael Kolb8233fac2010-10-26 16:08:53 -07001412 // key handling
1413 protected void onBackKey() {
1414 if (!mUi.onBackKey()) {
1415 WebView subwindow = mTabControl.getCurrentSubWindow();
1416 if (subwindow != null) {
1417 if (subwindow.canGoBack()) {
1418 subwindow.goBack();
1419 } else {
1420 dismissSubWindow(mTabControl.getCurrentTab());
1421 }
1422 } else {
1423 goBackOnePageOrQuit();
1424 }
1425 }
1426 }
1427
Michael Kolb4bd767d2011-05-27 11:33:55 -07001428 protected boolean onMenuKey() {
1429 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001430 }
1431
Michael Kolb8233fac2010-10-26 16:08:53 -07001432 // menu handling and state
1433 // TODO: maybe put into separate handler
1434
John Reck9c35b9c2012-05-30 10:08:50 -07001435 @Override
1436 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001437 if (mMenuState == EMPTY_MENU) {
1438 return false;
1439 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001440 MenuInflater inflater = mActivity.getMenuInflater();
1441 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001442 return true;
1443 }
1444
John Reck9c35b9c2012-05-30 10:08:50 -07001445 @Override
1446 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001447 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001448 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 return;
1450 }
1451 if (!(v instanceof WebView)) {
1452 return;
1453 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001454 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001455 WebView.HitTestResult result;
1456
1457 /* Determine whether the ContextMenu got triggered because
1458 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1459 * received. The mResult acts as a flag to identify, how it got trigerred
1460 */
1461 if (mResult == null){
1462 result = webview.getHitTestResult();
1463 } else {
1464 result = mResult;
1465 }
1466
Michael Kolb8233fac2010-10-26 16:08:53 -07001467 if (result == null) {
1468 return;
1469 }
1470
1471 int type = result.getType();
1472 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001473
1474 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1475 unknownTypeMap.put("webview", webview);
1476 final Message msg = mHandler.obtainMessage(
1477 UNKNOWN_TYPE_MSG, unknownTypeMap);
1478 /* As defined in android developers guide
1479 * when UNKNOWN_TYPE is received as a result of HitTest
1480 * you need to determing the type by invoking requestFocusNodeHref
1481 */
1482 webview.requestFocusNodeHref(msg);
1483
Michael Kolb8233fac2010-10-26 16:08:53 -07001484 Log.w(LOGTAG,
1485 "We should not show context menu when nothing is touched");
1486 return;
1487 }
1488 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1489 // let TextView handles context menu
1490 return;
1491 }
1492
1493 // Note, http://b/issue?id=1106666 is requesting that
1494 // an inflated menu can be used again. This is not available
1495 // yet, so inflate each time (yuk!)
1496 MenuInflater inflater = mActivity.getMenuInflater();
1497 inflater.inflate(R.menu.browsercontext, menu);
1498
1499 // Show the correct menu group
1500 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001501 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001502 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001503 menu.setGroupVisible(R.id.PHONE_MENU,
1504 type == WebView.HitTestResult.PHONE_TYPE);
1505 menu.setGroupVisible(R.id.EMAIL_MENU,
1506 type == WebView.HitTestResult.EMAIL_TYPE);
1507 menu.setGroupVisible(R.id.GEO_MENU,
1508 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001509 String itemUrl = null;
1510 String url = webview.getOriginalUrl();
1511 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1512 itemUrl = Uri.decode(navigationUrl);
1513 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1514 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1515 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1516 } else {
1517 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1518 }
1519 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1520 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1521 } else {
1522 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1523
1524 menu.setGroupVisible(R.id.IMAGE_MENU,
1525 type == WebView.HitTestResult.IMAGE_TYPE
1526 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1527 menu.setGroupVisible(R.id.ANCHOR_MENU,
1528 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1529 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Dave Tharp13f3f7c2015-07-16 14:58:51 -07001530
1531 if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
1532 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1533 UrlUtils.isDownloadableScheme(extra));
1534 }
1535 else {
1536 menu.findItem(R.id.save_link_context_menu_id).setEnabled(false);
1537 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001538 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001539 // Setup custom handling depending on the type
1540 switch (type) {
1541 case WebView.HitTestResult.PHONE_TYPE:
1542 menu.setHeaderTitle(Uri.decode(extra));
1543 menu.findItem(R.id.dial_context_menu_id).setIntent(
1544 new Intent(Intent.ACTION_VIEW, Uri
1545 .parse(WebView.SCHEME_TEL + extra)));
1546 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1547 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1548 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1549 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1550 addIntent);
1551 menu.findItem(R.id.copy_phone_context_menu_id)
1552 .setOnMenuItemClickListener(
1553 new Copy(extra));
1554 break;
1555
1556 case WebView.HitTestResult.EMAIL_TYPE:
1557 menu.setHeaderTitle(extra);
1558 menu.findItem(R.id.email_context_menu_id).setIntent(
1559 new Intent(Intent.ACTION_VIEW, Uri
1560 .parse(WebView.SCHEME_MAILTO + extra)));
1561 menu.findItem(R.id.copy_mail_context_menu_id)
1562 .setOnMenuItemClickListener(
1563 new Copy(extra));
1564 break;
1565
1566 case WebView.HitTestResult.GEO_TYPE:
1567 menu.setHeaderTitle(extra);
1568 menu.findItem(R.id.map_context_menu_id).setIntent(
1569 new Intent(Intent.ACTION_VIEW, Uri
1570 .parse(WebView.SCHEME_GEO
1571 + URLEncoder.encode(extra))));
1572 menu.findItem(R.id.copy_geo_context_menu_id)
1573 .setOnMenuItemClickListener(
1574 new Copy(extra));
1575 break;
1576
1577 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1578 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001579 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001580 // decide whether to show the open link in new tab option
1581 boolean showNewTab = mTabControl.canCreateNewTab();
1582 MenuItem newTabItem
1583 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001584 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001585 ? R.string.contextmenu_openlink_newwindow_background
1586 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 newTabItem.setVisible(showNewTab);
1588 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001589 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1590 newTabItem.setOnMenuItemClickListener(
1591 new MenuItem.OnMenuItemClickListener() {
1592 @Override
1593 public boolean onMenuItemClick(MenuItem item) {
1594 final HashMap<String, WebView> hrefMap =
1595 new HashMap<String, WebView>();
1596 hrefMap.put("webview", webview);
1597 final Message msg = mHandler.obtainMessage(
1598 FOCUS_NODE_HREF,
1599 R.id.open_newtab_context_menu_id,
1600 0, hrefMap);
1601 webview.requestFocusNodeHref(msg);
1602 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001604 });
1605 } else {
1606 newTabItem.setOnMenuItemClickListener(
1607 new MenuItem.OnMenuItemClickListener() {
1608 @Override
1609 public boolean onMenuItemClick(MenuItem item) {
1610 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001611 openTab(extra, parent,
1612 !mSettings.openInBackground(),
1613 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001614 return true;
1615 }
1616 });
1617 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001619 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1620 menu.setHeaderTitle(navigationUrl);
1621 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1622
1623 if (itemUrl != null) {
1624 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1625 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1626 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1627 @Override
1628 public boolean onMenuItemClick(MenuItem item) {
1629 final Intent intent = new Intent(Controller.this
1630 .getContext(),
1631 AddMyNavigationPage.class);
1632 Bundle bundle = new Bundle();
1633 String url = Uri.decode(navigationUrl);
1634 bundle.putBoolean("isAdding", false);
1635 bundle.putString("url", url);
1636 bundle.putString("name", getNameFromUrl(url));
1637 intent.putExtra("websites", bundle);
1638 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1639 return false;
1640 }
1641 });
1642 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1643 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1644 @Override
1645 public boolean onMenuItemClick(MenuItem item) {
1646 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1647 return false;
1648 }
1649 });
1650 }
1651 } else {
1652 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1653 }
1654 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001655 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1656 break;
1657 }
1658 // otherwise fall through to handle image part
1659 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001660 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1661 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001662 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1663 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001664 shareItem.setOnMenuItemClickListener(
1665 new MenuItem.OnMenuItemClickListener() {
1666 @Override
1667 public boolean onMenuItemClick(MenuItem item) {
1668 sharePage(mActivity, null, extra, null,
1669 null);
1670 return true;
1671 }
1672 }
1673 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001674 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001675 menu.findItem(R.id.view_image_context_menu_id)
1676 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1677 @Override
1678 public boolean onMenuItemClick(MenuItem item) {
1679 openTab(extra, mTabControl.getCurrentTab(), true, true);
1680 return false;
1681 }
1682 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001683 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1684 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1685 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001686 menu.findItem(R.id.set_wallpaper_context_menu_id).
1687 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1688 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001689 break;
1690
1691 default:
1692 Log.w(LOGTAG, "We should not get here.");
1693 break;
1694 }
1695 //update the ui
1696 mUi.onContextMenuCreated(menu);
1697 }
1698
kaiyiz6e5b3e02013-08-19 20:02:01 +08001699 public void startAddMyNavigation(String url) {
1700 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1701 Bundle bundle = new Bundle();
1702 bundle.putBoolean("isAdding", true);
1703 bundle.putString("url", url);
1704 bundle.putString("name", getNameFromUrl(url));
1705 intent.putExtra("websites", bundle);
1706 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1707 }
1708
1709 private void showMyNavigationDeleteDialog(final String itemUrl) {
1710 new AlertDialog.Builder(this.getContext())
1711 .setTitle(R.string.my_navigation_delete_label)
1712 .setIcon(android.R.drawable.ic_dialog_alert)
1713 .setMessage(R.string.my_navigation_delete_msg)
1714 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1715 @Override
1716 public void onClick(DialogInterface dialog, int whichButton) {
1717 deleteMyNavigationItem(itemUrl);
1718 }
1719 })
1720 .setNegativeButton(R.string.cancel, null)
1721 .show();
1722 }
1723
1724 private void deleteMyNavigationItem(final String itemUrl) {
1725 ContentResolver cr = this.getContext().getContentResolver();
1726 Cursor cursor = null;
1727
1728 try {
1729 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1730 new String[] {
1731 MyNavigationUtil.ID
1732 }, "url = ?", new String[] {
1733 itemUrl
1734 }, null);
1735 if (null != cursor && cursor.moveToFirst()) {
1736 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1737 cursor.getLong(0));
1738
1739 ContentValues values = new ContentValues();
1740 values.put(MyNavigationUtil.TITLE, "");
1741 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1742 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1743 ByteArrayOutputStream os = new ByteArrayOutputStream();
1744 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1745 R.raw.my_navigation_add);
1746 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1747 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1748 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1749 cr.update(uri, values, null, null);
1750 } else {
1751 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1752 }
1753 } catch (IllegalStateException e) {
1754 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1755 } finally {
1756 if (null != cursor) {
1757 cursor.close();
1758 }
1759 }
1760
1761 if (getCurrentTopWebView() != null) {
1762 getCurrentTopWebView().reload();
1763 }
1764 }
1765
1766 private String getNameFromUrl(String itemUrl) {
1767 ContentResolver cr = this.getContext().getContentResolver();
1768 Cursor cursor = null;
1769 String name = null;
1770
1771 try {
1772 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1773 new String[] {
1774 MyNavigationUtil.TITLE
1775 }, "url = ?", new String[] {
1776 itemUrl
1777 }, null);
1778 if (null != cursor && cursor.moveToFirst()) {
1779 name = cursor.getString(0);
1780 } else {
1781 Log.e(LOGTAG, "this item does not exist!");
1782 }
1783 } catch (IllegalStateException e) {
1784 Log.e(LOGTAG, "getNameFromUrl", e);
1785 } finally {
1786 if (null != cursor) {
1787 cursor.close();
1788 }
1789 }
1790 return name;
1791 }
1792
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001793 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001794 final ContentResolver cr = mActivity.getContentResolver();
1795 new AsyncTask<Void, Void, Void>() {
1796 @Override
1797 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001798 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1799 if(!isMyNavigationUrl)
1800 return null;
1801
kaiyiz6e5b3e02013-08-19 20:02:01 +08001802 ContentResolver cr = mActivity.getContentResolver();
1803 Cursor cursor = null;
1804 try {
1805 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1806 new String[] {
1807 MyNavigationUtil.ID
1808 }, "url = ?", new String[] {
1809 itemUrl
1810 }, null);
1811 if (null != cursor && cursor.moveToFirst()) {
1812 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001813 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001814
1815 ContentValues values = new ContentValues();
1816 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1817 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1818 cursor.getLong(0));
1819 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1820 cr.update(uri, values, null, null);
1821 os.close();
1822 }
1823 } catch (IllegalStateException e) {
1824 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1825 } catch (IOException e) {
1826 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1827 } finally {
1828 if (null != cursor) {
1829 cursor.close();
1830 }
1831 }
1832 return null;
1833 }
1834 }.execute();
1835 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001836 /**
1837 * As the menu can be open when loading state changes
1838 * we must manually update the state of the stop/reload menu
1839 * item
1840 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001841 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001842 if (menu == null) {
1843 return;
1844 }
1845 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001846 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001847 menu.findItem(R.id.stop_menu_id):
1848 menu.findItem(R.id.reload_menu_id);
1849 if (src != null) {
1850 dest.setIcon(src.getIcon());
1851 dest.setTitle(src.getTitle());
1852 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001853 mActivity.invalidateOptionsMenu();
1854 }
1855
1856 public void invalidateOptionsMenu() {
1857 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001858 }
1859
John Reck9c35b9c2012-05-30 10:08:50 -07001860 @Override
1861 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001862 // Software menu key (toolbar key)
1863 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1864 return true;
1865 }
1866
1867 @Override
1868 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001869 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001870 // hold on to the menu reference here; it is used by the page callbacks
1871 // to update the menu based on loading state
1872 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001873 // Note: setVisible will decide whether an item is visible; while
1874 // setEnabled() will decide whether an item is enabled, which also means
1875 // whether the matching shortcut key will function.
1876 switch (mMenuState) {
1877 case EMPTY_MENU:
1878 if (mCurrentMenuState != mMenuState) {
1879 menu.setGroupVisible(R.id.MAIN_MENU, false);
1880 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1881 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1882 }
1883 break;
1884 default:
1885 if (mCurrentMenuState != mMenuState) {
1886 menu.setGroupVisible(R.id.MAIN_MENU, true);
1887 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1888 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1889 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001890 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001891 break;
1892 }
1893 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001894 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001895
1896 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001897 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1898 EditBookmarksRestriction.getInstance()
1899 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001900 }
1901
1902 private void setMenuItemVisibility(Menu menu, int id,
1903 boolean visibility) {
1904 MenuItem item = menu.findItem(id);
1905 if (item != null) {
1906 item.setVisible(visibility);
1907 }
1908 }
1909
1910 private int lookupBookmark(String title, String url) {
1911 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001912 int count = 0;
1913 Cursor cursor = null;
1914 try {
1915 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1916 BookmarksLoader.PROJECTION,
1917 "title = ? OR url = ?",
1918 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001919 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001920 },
1921 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001922
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001923 if (cursor != null)
1924 count = cursor.getCount();
1925
1926 } catch (IllegalStateException e) {
1927 Log.e(LOGTAG, "lookupBookmark ", e);
1928 } finally {
1929 if (null != cursor) {
1930 cursor.close();
1931 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001932 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001933 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001934 }
1935
1936 private void resetMenuItems(Menu menu) {
1937 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1938 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1939
1940 WebView w = getCurrentTopWebView();
1941 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1942
1943 String title = w.getTitle();
1944 String url = w.getUrl();
Pankaj Garg634bf432015-07-13 09:54:21 -07001945 mCurrentPageBookmarked = (lookupBookmark(title, url) > 0);
1946 if (title != null && url != null && mCurrentPageBookmarked) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001947 bookmark_icon.setChecked(true);
1948 } else {
1949 bookmark_icon.setChecked(false);
1950 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001951
1952 // update reader mode checkbox
1953 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1954 readerSwitcher.setVisible(false);
1955 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001956 }
1957
Michael Kolb4bf79712011-07-14 14:18:12 -07001958 @Override
1959 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001960 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001961 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001962 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001963 // Following flag is used to identify schemes for which the LIVE_MENU
1964 // items defined in res/menu/browser.xml should be enabled
1965 boolean isLiveScheme = false;
1966 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001967 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07001968
1969 boolean isDistillable = false;
1970 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001971 resetMenuItems(menu);
1972
Michael Kolb4bf79712011-07-14 14:18:12 -07001973 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001974 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001975 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001976 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001977 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001978 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001979 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07001980
1981 isDistillable = tab.isDistillable();
1982 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07001983 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001984
1985 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1986 forward.setEnabled(canGoForward);
1987
Michael Kolb4bf79712011-07-14 14:18:12 -07001988 // decide whether to show the share link option
1989 PackageManager pm = mActivity.getPackageManager();
1990 Intent send = new Intent(Intent.ACTION_SEND);
1991 send.setType("text/plain");
1992 ResolveInfo ri = pm.resolveActivity(send,
1993 PackageManager.MATCH_DEFAULT_ONLY);
1994 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1995
1996 boolean isNavDump = mSettings.enableNavDump();
1997 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1998 nav.setVisible(isNavDump);
1999 nav.setEnabled(isNavDump);
2000
2001 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002002 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2003 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002004 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002005 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2006 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002007 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002008 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2009 isLive && isLiveScheme && isPageFinished);
2010 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002011 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002012 // history and snapshots item are the members of COMBO menu group,
2013 // so if show history item, only make snapshots item invisible.
2014 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002015
Ze G Riande2a675c22015-06-03 11:15:24 -07002016
2017 // update reader mode checkbox
2018 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2019 // The reader mode checkbox is hidden only
2020 // when the current page is neither distillable nor distilled
2021 readerSwitcher.setVisible(isDistillable || isDistilled);
2022 readerSwitcher.setChecked(isDistilled);
2023
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002024 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002025 }
2026
John Reck9c35b9c2012-05-30 10:08:50 -07002027 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002028 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002029 if (null == getCurrentTopWebView()) {
2030 return false;
2031 }
2032 if (mMenuIsDown) {
2033 // The shortcut action consumes the MENU. Even if it is still down,
2034 // it won't trigger the next shortcut action. In the case of the
2035 // shortcut action triggering a new activity, like Bookmarks, we
2036 // won't get onKeyUp for MENU. So it is important to reset it here.
2037 mMenuIsDown = false;
2038 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002039 if (mUi.onOptionsItemSelected(item)) {
2040 // ui callback handled it
2041 return true;
2042 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002043 switch (item.getItemId()) {
2044 // -- Main menu
2045 case R.id.new_tab_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002046 getCurrentTab().capture();
Michael Kolb8233fac2010-10-26 16:08:53 -07002047 openTabToHomePage();
2048 break;
2049
2050 case R.id.incognito_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002051 getCurrentTab().capture();
Michael Kolb519d2282011-05-09 17:03:19 -07002052 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002053 break;
2054
2055 case R.id.goto_menu_id:
2056 editUrl();
2057 break;
2058
2059 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002060 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2061 break;
2062
2063 case R.id.history_menu_id:
2064 bookmarksOrHistoryPicker(ComboViews.History);
2065 break;
2066
2067 case R.id.snapshots_menu_id:
2068 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 break;
2070
Pankaj Garg49b79252014-11-07 17:33:41 -08002071 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002072 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002073 break;
2074
2075 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002076 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002077 stopLoading();
2078 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002079 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 getCurrentTopWebView().reload();
2081 }
2082 break;
2083
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002085 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002086 break;
2087
2088 case R.id.close_menu_id:
2089 // Close the subwindow if it exists.
2090 if (mTabControl.getCurrentSubWindow() != null) {
2091 dismissSubWindow(mTabControl.getCurrentTab());
2092 break;
2093 }
2094 closeCurrentTab();
2095 break;
2096
kaiyiza8b6dbb2013-07-29 18:11:22 +08002097 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002098 Object[] params = { new String("persist.debug.browsermonkeytest")};
2099 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002100 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002101 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002102 if (ret != null && ret.equals("enable"))
2103 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002104 if (BrowserConfig.getInstance(getContext())
2105 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2106 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002107 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002108 case R.id.homepage_menu_id:
2109 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002110 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002111 break;
2112
2113 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002114 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002115 break;
2116
2117 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002118 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002119 break;
2120
John Reck2bc80422011-06-30 15:11:49 -07002121 case R.id.save_snapshot_menu_id:
2122 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002123 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002124 createScreenshotAsync(
2125 source.getWebView(),
2126 getDesiredThumbnailWidth(mActivity),
2127 getDesiredThumbnailHeight(mActivity),
2128 new ValueCallback<Bitmap>() {
2129 @Override
2130 public void onReceiveValue(Bitmap bitmap) {
2131 new SaveSnapshotTask(source, bitmap).execute();
2132 }
2133 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002134 break;
2135
Michael Kolb8233fac2010-10-26 16:08:53 -07002136 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002137 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002138 break;
2139
John Recke1a03a32011-09-14 17:04:16 -07002140 case R.id.snapshot_go_live:
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002141 // passing null to distinguish between
2142 // "go live" button and navigating a web page
2143 // on a snapshot tab
2144 return goLive(null);
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 case R.id.share_page_menu_id:
2146 Tab currentTab = mTabControl.getCurrentTab();
2147 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002148 return false;
2149 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002150 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002151 break;
2152
2153 case R.id.dump_nav_menu_id:
2154 getCurrentTopWebView().debugDump();
2155 break;
2156
Michael Kolb8233fac2010-10-26 16:08:53 -07002157 case R.id.zoom_in_menu_id:
2158 getCurrentTopWebView().zoomIn();
2159 break;
2160
2161 case R.id.zoom_out_menu_id:
2162 getCurrentTopWebView().zoomOut();
2163 break;
2164
2165 case R.id.view_downloads_menu_id:
2166 viewDownloads();
2167 break;
2168
John Reck42229bc2011-08-19 13:26:43 -07002169 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002170 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002171 break;
2172
Ze G Riande2a675c22015-06-03 11:15:24 -07002173 case R.id.reader_mode_menu_id:
2174 toggleReaderMode();
2175 break;
2176
Michael Kolb8233fac2010-10-26 16:08:53 -07002177 case R.id.window_one_menu_id:
2178 case R.id.window_two_menu_id:
2179 case R.id.window_three_menu_id:
2180 case R.id.window_four_menu_id:
2181 case R.id.window_five_menu_id:
2182 case R.id.window_six_menu_id:
2183 case R.id.window_seven_menu_id:
2184 case R.id.window_eight_menu_id:
2185 {
2186 int menuid = item.getItemId();
2187 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2188 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2189 Tab desiredTab = mTabControl.getTab(id);
2190 if (desiredTab != null &&
2191 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002192 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002193 }
2194 break;
2195 }
2196 }
2197 }
2198 break;
2199
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002200 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002201 Bundle bundle = new Bundle();
2202 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2203 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2204 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2205 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2206 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002207 break;
2208
Pankaj Garg49b79252014-11-07 17:33:41 -08002209 case R.id.add_to_homescreen:
2210 final WebView w = getCurrentTopWebView();
2211 final EditText input = new EditText(getContext());
2212 input.setText(w.getTitle());
2213 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002214 .setTitle(getContext().getResources().getString(
2215 R.string.add_to_homescreen))
2216 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002217 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002218 .setPositiveButton(getContext().getResources().getString(
2219 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002220 public void onClick(DialogInterface dialog, int whichButton) {
2221 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2222 getContext(),
2223 w.getUrl(),
2224 input.getText().toString(),
2225 w.getViewportBitmap(),
2226 w.getFavicon()));
2227
2228 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2229 .addCategory(Intent.CATEGORY_HOME));
2230 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002231 .setNegativeButton(getContext().getResources().getString(
2232 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002233 public void onClick(DialogInterface dialog, int whichButton) {
2234 // Do nothing.
2235 }
2236 })
2237 .show();
2238 break;
2239
Michael Kolb8233fac2010-10-26 16:08:53 -07002240 default:
2241 return false;
2242 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002243 return true;
2244 }
2245
John Reck68234a92012-04-19 15:27:12 -07002246 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2247 implements OnCancelListener {
2248
2249 private Tab mTab;
2250 private Dialog mProgressDialog;
2251 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002252 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002253
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002254 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002255 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002256 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002257 }
2258
2259 @Override
2260 protected void onPreExecute() {
2261 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2262 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2263 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002264 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002265 }
2266
2267 @Override
2268 protected Long doInBackground(Void... params) {
2269 if (!mTab.saveViewState(mValues)) {
2270 return null;
2271 }
2272 if (isCancelled()) {
2273 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2274 File file = mActivity.getFileStreamPath(path);
2275 if (!file.delete()) {
2276 file.deleteOnExit();
2277 }
2278 return null;
2279 }
2280 final ContentResolver cr = mActivity.getContentResolver();
2281 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2282 if (result == null) {
2283 return null;
2284 }
2285 long id = ContentUris.parseId(result);
2286 return id;
2287 }
2288
2289 @Override
2290 protected void onPostExecute(Long id) {
2291 if (isCancelled()) {
2292 return;
2293 }
2294 mProgressDialog.dismiss();
2295 if (id == null) {
2296 Toast.makeText(mActivity, R.string.snapshot_failed,
2297 Toast.LENGTH_SHORT).show();
2298 return;
2299 }
2300 Bundle b = new Bundle();
2301 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2302 mUi.showComboView(ComboViews.Snapshots, b);
2303 }
2304
2305 @Override
2306 public void onCancel(DialogInterface dialog) {
2307 cancel(true);
2308 }
2309 }
2310
Michael Kolb80f75082012-04-10 10:50:06 -07002311 @Override
2312 public void toggleUserAgent() {
2313 WebView web = getCurrentWebView();
2314 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002315 }
2316
Ze G Riande2a675c22015-06-03 11:15:24 -07002317 // This function calls the method in the webview to enable/disable
2318 // the reader mode through the DOM distiller
2319 public void toggleReaderMode() {
2320 Tab t = mTabControl.getCurrentTab();
2321 if (t.isDistilled()) {
2322 closeTab(t);
2323 } else if (t.isDistillable()) {
2324 openTab(t.getDistilledUrl(), false, true, false, t);
2325 }
2326 }
2327
Michael Kolb80f75082012-04-10 10:50:06 -07002328 @Override
2329 public void findOnPage() {
2330 getCurrentTopWebView().showFindDialog(null, true);
2331 }
2332
2333 @Override
2334 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002335 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002336 }
2337
2338 @Override
2339 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002340 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002341 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2342 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002343 }
2344 else {
2345 WebView w = getCurrentTopWebView();
2346 if (w == null)
2347 return;
Pankaj Garg634bf432015-07-13 09:54:21 -07002348 final Intent i = createBookmarkCurrentPageIntent(mCurrentPageBookmarked);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002349 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002350 }
Michael Kolb80f75082012-04-10 10:50:06 -07002351 }
2352
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002353 public boolean goLive(String url) {
2354 if (!getCurrentTab().isSnapshot())
2355 return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002356 SnapshotTab t = (SnapshotTab) getCurrentTab();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002357
2358 if (url == null) { // "go live" button was clicked
2359 url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002360 closeTab(t);
2361 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002362 Tab liveTab = createNewTab(false, true, false);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002363 loadUrl(liveTab, url);
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -07002364 return true;
John Recke1a03a32011-09-14 17:04:16 -07002365 }
2366
luxiaolb40014b2013-07-19 10:01:43 +08002367 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002368 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002369 new AlertDialog.Builder(activity)
2370 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002371 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002372 .setMessage(R.string.exit_browser_msg)
2373 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2374 public void onClick(DialogInterface dialog, int which) {
2375 activity.moveTaskToBack(true);
2376 dialog.dismiss();
2377 }
2378 })
2379 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2380 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002381 mCrashRecoveryHandler.clearState(true);
2382 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002383 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002384 dialog.dismiss();
2385 }
2386 })
2387 .show();
2388 }
Vivek Sekharb53a5952015-07-17 14:14:38 -07002389
Michael Kolb315d5022011-10-13 12:47:11 -07002390 @Override
2391 public void showPageInfo() {
Michael Kolb315d5022011-10-13 12:47:11 -07002392 }
2393
John Reck9c35b9c2012-05-30 10:08:50 -07002394 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002395 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002396 // Let the History and Bookmark fragments handle menus they created.
2397 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2398 return false;
2399 }
2400
Michael Kolb8233fac2010-10-26 16:08:53 -07002401 int id = item.getItemId();
2402 boolean result = true;
2403 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002404 // -- Browser context menu
2405 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002406 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002407 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002408 case R.id.copy_link_context_menu_id:
2409 final WebView webView = getCurrentTopWebView();
2410 if (null == webView) {
2411 result = false;
2412 break;
2413 }
2414 final HashMap<String, WebView> hrefMap =
2415 new HashMap<String, WebView>();
2416 hrefMap.put("webview", webView);
2417 final Message msg = mHandler.obtainMessage(
2418 FOCUS_NODE_HREF, id, 0, hrefMap);
2419 webView.requestFocusNodeHref(msg);
2420 break;
2421
2422 default:
2423 // For other context menus
2424 result = onOptionsItemSelected(item);
2425 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002426 return result;
2427 }
2428
2429 /**
2430 * support programmatically opening the context menu
2431 */
2432 public void openContextMenu(View view) {
2433 mActivity.openContextMenu(view);
2434 }
2435
2436 /**
2437 * programmatically open the options menu
2438 */
2439 public void openOptionsMenu() {
2440 mActivity.openOptionsMenu();
2441 }
2442
John Reck9c35b9c2012-05-30 10:08:50 -07002443 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002444 public boolean onMenuOpened(int featureId, Menu menu) {
2445 if (mOptionsMenuOpen) {
2446 if (mConfigChanged) {
2447 // We do not need to make any changes to the state of the
2448 // title bar, since the only thing that happened was a
2449 // change in orientation
2450 mConfigChanged = false;
2451 } else {
2452 if (!mExtendedMenuOpen) {
2453 mExtendedMenuOpen = true;
2454 mUi.onExtendedMenuOpened();
2455 } else {
2456 // Switching the menu back to icon view, so show the
2457 // title bar once again.
2458 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002459 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002460 }
2461 }
2462 } else {
2463 // The options menu is closed, so open it, and show the title
2464 mOptionsMenuOpen = true;
2465 mConfigChanged = false;
2466 mExtendedMenuOpen = false;
2467 mUi.onOptionsMenuOpened();
2468 }
2469 return true;
2470 }
2471
John Reck9c35b9c2012-05-30 10:08:50 -07002472 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002473 public void onOptionsMenuClosed(Menu menu) {
2474 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002475 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002476 }
2477
John Reck9c35b9c2012-05-30 10:08:50 -07002478 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002479 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002480 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002481 }
2482
2483 // Helper method for getting the top window.
2484 @Override
2485 public WebView getCurrentTopWebView() {
2486 return mTabControl.getCurrentTopWebView();
2487 }
2488
2489 @Override
2490 public WebView getCurrentWebView() {
2491 return mTabControl.getCurrentWebView();
2492 }
2493
2494 /*
2495 * This method is called as a result of the user selecting the options
2496 * menu to see the download window. It shows the download window on top of
2497 * the current window.
2498 */
2499 void viewDownloads() {
2500 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2501 mActivity.startActivity(intent);
2502 }
2503
John Reck30b065e2011-07-19 10:58:05 -07002504 int getActionModeHeight() {
2505 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2506 new int[] { android.R.attr.actionBarSize });
2507 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2508 actionBarSizeTypedArray.recycle();
2509 return size;
2510 }
2511
Michael Kolb8233fac2010-10-26 16:08:53 -07002512 // action mode
2513
John Reck9c35b9c2012-05-30 10:08:50 -07002514 @Override
2515 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002516 mUi.onActionModeStarted(mode);
2517 mActionMode = mode;
2518 }
2519
2520 /*
2521 * True if a custom ActionMode (i.e. find or select) is in use.
2522 */
2523 @Override
2524 public boolean isInCustomActionMode() {
2525 return mActionMode != null;
2526 }
2527
2528 /*
2529 * End the current ActionMode.
2530 */
2531 @Override
2532 public void endActionMode() {
2533 if (mActionMode != null) {
2534 mActionMode.finish();
2535 }
2536 }
2537
2538 /*
2539 * Called by find and select when they are finished. Replace title bars
2540 * as necessary.
2541 */
John Reck9c35b9c2012-05-30 10:08:50 -07002542 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002543 public void onActionModeFinished(ActionMode mode) {
2544 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002545 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002546 mActionMode = null;
2547 }
2548
2549 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002550 final Tab tab = getCurrentTab();
2551 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002552 }
2553
2554 // bookmark handling
2555
2556 /**
2557 * add the current page as a bookmark to the given folder id
2558 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002559 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002560 * bookmarked, and if it is, edit that bookmark. If false, and
2561 * the site is already bookmarked, do not attempt to edit the
2562 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002563 */
2564 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002565 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002566 WebView w = getCurrentTopWebView();
2567 if (w == null) {
2568 return null;
2569 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002570 Intent i = new Intent(mActivity,
2571 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002572 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2573 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002574 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002575 if (touchIconUrl != null) {
2576 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2577 WebSettings settings = w.getSettings();
2578 if (settings != null) {
2579 i.putExtra(AddBookmarkPage.USER_AGENT,
2580 settings.getUserAgentString());
2581 }
2582 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002583 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002584 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002585 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002586 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2587 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 // Put the dialog at the upper right of the screen, covering the
2589 // star on the title bar.
2590 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002591 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002592 }
2593
2594 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002595 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002596 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002597 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002598 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 }
2600
Vivek Sekharb54614f2014-05-01 19:03:37 -07002601 @Override
2602 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2603 boolean capture) {
2604 mUploadHandler = new UploadHandler(this);
2605 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2606 }
2607
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 // thumbnails
2609
2610 /**
2611 * Return the desired width for thumbnail screenshots, which are stored in
2612 * the database, and used on the bookmarks screen.
2613 * @param context Context for finding out the density of the screen.
2614 * @return desired width for thumbnail screenshot.
2615 */
2616 static int getDesiredThumbnailWidth(Context context) {
2617 return context.getResources().getDimensionPixelOffset(
2618 R.dimen.bookmarkThumbnailWidth);
2619 }
2620
2621 /**
2622 * Return the desired height for thumbnail screenshots, which are stored in
2623 * the database, and used on the bookmarks screen.
2624 * @param context Context for finding out the density of the screen.
2625 * @return desired height for thumbnail screenshot.
2626 */
2627 static int getDesiredThumbnailHeight(Context context) {
2628 return context.getResources().getDimensionPixelOffset(
2629 R.dimen.bookmarkThumbnailHeight);
2630 }
2631
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002632 static void createScreenshotAsync(WebView view, int width, int height,
2633 final ValueCallback<Bitmap> cb) {
2634 if (view == null || width == 0 || height == 0) {
2635 return;
2636 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002637 view.getContentBitmapAsync(
2638 (float) width / view.getWidth(),
2639 new Rect(),
2640 new ValueCallback<Bitmap>() {
2641 @Override
2642 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002643 if (bitmap != null)
2644 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2645 cb.onReceiveValue(bitmap);
2646 }});
2647 }
2648
Michael Kolb8233fac2010-10-26 16:08:53 -07002649 private class Copy implements OnMenuItemClickListener {
2650 private CharSequence mText;
2651
John Reck9c35b9c2012-05-30 10:08:50 -07002652 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002653 public boolean onMenuItemClick(MenuItem item) {
2654 copy(mText);
2655 return true;
2656 }
2657
2658 public Copy(CharSequence toCopy) {
2659 mText = toCopy;
2660 }
2661 }
2662
Leon Scroggins63c02662010-11-18 15:16:27 -05002663 private static class Download implements OnMenuItemClickListener {
2664 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002665 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002666 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002667 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002668 private static final String FALLBACK_EXTENSION = "dat";
2669 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002670
John Reck9c35b9c2012-05-30 10:08:50 -07002671 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002672 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002673 if (DataUri.isDataUri(mText)) {
2674 saveDataUri();
2675 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002676 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002677 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002678 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002679 return true;
2680 }
2681
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002682 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2683 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002684 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002685 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002686 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002687 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002688 }
George Mount387d45d2011-10-07 15:57:53 -07002689
2690 /**
2691 * Treats mText as a data URI and writes its contents to a file
2692 * based on the current time.
2693 */
2694 private void saveDataUri() {
2695 FileOutputStream outputStream = null;
2696 try {
2697 DataUri uri = new DataUri(mText);
2698 File target = getTarget(uri);
2699 outputStream = new FileOutputStream(target);
2700 outputStream.write(uri.getData());
2701 final DownloadManager manager =
2702 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2703 manager.addCompletedDownload(target.getName(),
2704 mActivity.getTitle().toString(), false,
2705 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002706 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002707 } catch (IOException e) {
2708 Log.e(LOGTAG, "Could not save data URL");
2709 } finally {
2710 if (outputStream != null) {
2711 try {
2712 outputStream.close();
2713 } catch (IOException e) {
2714 // ignore close errors
2715 }
2716 }
2717 }
2718 }
2719
2720 /**
2721 * Creates a File based on the current time stamp and uses
2722 * the mime type of the DataUri to get the extension.
2723 */
2724 private File getTarget(DataUri uri) throws IOException {
2725 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002726 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002727 String nameBase = format.format(new Date());
2728 String mimeType = uri.getMimeType();
2729 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2730 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2731 if (extension == null) {
2732 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2733 extension = FALLBACK_EXTENSION;
2734 }
2735 extension = "." + extension; // createTempFile needs the '.'
2736 File targetFile = File.createTempFile(nameBase, extension, dir);
2737 return targetFile;
2738 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002739 }
2740
Cary Clark8974d282010-11-22 10:46:05 -05002741 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002742 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002743
John Reck9c35b9c2012-05-30 10:08:50 -07002744 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002745 public boolean onMenuItemClick(MenuItem item) {
2746 if (mWebView != null) {
2747 return mWebView.selectText();
2748 }
2749 return false;
2750 }
2751
2752 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002753 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002754 }
2755
2756 }
2757
Michael Kolb8233fac2010-10-26 16:08:53 -07002758 /********************** TODO: UI stuff *****************************/
2759
2760 // these methods have been copied, they still need to be cleaned up
2761
2762 /****************** tabs ***************************************************/
2763
2764 // basic tab interactions:
2765
2766 // it is assumed that tabcontrol already knows about the tab
2767 protected void addTab(Tab tab) {
2768 mUi.addTab(tab);
2769 }
2770
2771 protected void removeTab(Tab tab) {
2772 mUi.removeTab(tab);
2773 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002774 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002775 }
2776
Michael Kolbf2055602011-04-09 17:20:03 -07002777 @Override
2778 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002779 // monkey protection against delayed start
2780 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002781
2782 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2783 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002784 mTabControl.setCurrentTab(tab);
2785 // the tab is guaranteed to have a webview after setCurrentTab
2786 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002787
2788
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002789 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002790 //Purge active tabs
2791 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002792 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002793 }
2794
John Reck8bcafc12011-08-29 16:43:02 -07002795 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002796 Tab current = mTabControl.getCurrentTab();
2797 if (current != null
2798 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002799 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002800 }
2801 }
2802
John Reck26b18322011-06-21 13:08:58 -07002803 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002804 //Cancel navscreen request
2805 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002806 // Dismiss the subwindow if applicable.
2807 dismissSubWindow(appTab);
2808 // Since we might kill the WebView, remove it from the
2809 // content view first.
2810 mUi.detachTab(appTab);
2811 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002812 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002813 // TODO: analyze why the remove and add are necessary
2814 mUi.attachTab(appTab);
2815 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002816 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002817 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002818 } else {
2819 // If the tab was the current tab, we have to attach
2820 // it to the view system again.
2821 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002822 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002823 }
2824 }
2825
2826 // Remove the sub window if it exists. Also called by TabControl when the
2827 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002828 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002829 public void dismissSubWindow(Tab tab) {
2830 removeSubWindow(tab);
2831 // dismiss the subwindow. This will destroy the WebView.
2832 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002833 WebView wv = getCurrentTopWebView();
2834 if (wv != null) {
2835 wv.requestFocus();
2836 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002837 }
2838
2839 @Override
2840 public void removeSubWindow(Tab t) {
2841 if (t.getSubWebView() != null) {
2842 mUi.removeSubWindow(t.getSubViewContainer());
2843 }
2844 }
2845
2846 @Override
2847 public void attachSubWindow(Tab tab) {
2848 if (tab.getSubWebView() != null) {
2849 mUi.attachSubWindow(tab.getSubViewContainer());
2850 getCurrentTopWebView().requestFocus();
2851 }
2852 }
2853
Mathew Inwood29721c22011-06-29 17:55:24 +01002854 private Tab showPreloadedTab(final UrlData urlData) {
2855 if (!urlData.isPreloaded()) {
2856 return null;
2857 }
2858 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2859 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2860 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002861 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002862 // Could not submit query. Fallback to regular tab creation
2863 tabControl.destroy();
2864 return null;
2865 }
2866 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002867 // check tab count and make room for new tab
2868 if (!mTabControl.canCreateNewTab()) {
2869 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2870 if (leastUsed != null) {
2871 closeTab(leastUsed);
2872 }
2873 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002874 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002875 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002876 mTabControl.addPreloadedTab(t);
2877 addTab(t);
2878 setActiveTab(t);
2879 return t;
2880 }
2881
Michael Kolb7bcafde2011-05-09 13:55:59 -07002882 // open a non inconito tab with the given url data
2883 // and set as active tab
2884 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002885 Tab tab = showPreloadedTab(urlData);
2886 if (tab == null) {
2887 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002888 if ((tab != null) && !urlData.isEmpty()) {
2889 loadUrlDataIn(tab, urlData);
2890 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002891 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002892 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002893 }
2894
Michael Kolb843510f2010-12-09 10:51:49 -08002895 @Override
2896 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002897 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002898 }
2899
Michael Kolb8233fac2010-10-26 16:08:53 -07002900 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002901 public Tab openIncognitoTab() {
2902 return openTab(INCOGNITO_URI, true, true, false);
2903 }
2904
2905 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002906 public Tab openTab(String url, boolean incognito, boolean setActive,
2907 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002908 return openTab(url, incognito, setActive, useCurrent, null);
2909 }
2910
2911 @Override
2912 public Tab openTab(String url, Tab parent, boolean setActive,
2913 boolean useCurrent) {
2914 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2915 setActive, useCurrent, parent);
2916 }
2917
2918 public Tab openTab(String url, boolean incognito, boolean setActive,
2919 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002920 Tab tab = createNewTab(incognito, setActive, useCurrent);
2921 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002922 if (parent instanceof SnapshotTab) {
2923 addTab(tab);
2924 if (setActive)
2925 setActiveTab(tab);
2926 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002927 parent.addChildTab(tab);
2928 }
Michael Kolb519d2282011-05-09 17:03:19 -07002929 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002930 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002931 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002932 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002933 return tab;
2934 }
2935
2936 // this method will attempt to create a new tab
2937 // incognito: private browsing tab
2938 // setActive: ste tab as current tab
2939 // useCurrent: if no new tab can be created, return current tab
2940 private Tab createNewTab(boolean incognito, boolean setActive,
2941 boolean useCurrent) {
2942 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002943 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2944 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002945 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002946 if (mTabControl.canCreateNewTab()) {
2947 tab = mTabControl.createNewTab(incognito, !setActive);
2948 addTab(tab);
2949 if (setActive) {
2950 setActiveTab(tab);
2951 } else {
2952 tab.pause();
2953 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002954 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002955 if (useCurrent) {
2956 tab = mTabControl.getCurrentTab();
2957 reuseTab(tab, null);
2958 } else {
2959 mUi.showMaxTabsWarning();
2960 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002961 }
2962 }
2963 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002964 }
2965
John Reck2bc80422011-06-30 15:11:49 -07002966 @Override
2967 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2968 SnapshotTab tab = null;
2969 if (mTabControl.canCreateNewTab()) {
Axesh R. Ajmera4b000312015-07-23 10:00:16 -07002970 tab = mTabControl.createSnapshotTab(snapshotId, null);
John Reck2bc80422011-06-30 15:11:49 -07002971 addTab(tab);
2972 if (setActive) {
2973 setActiveTab(tab);
2974 }
2975 } else {
2976 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002977 }
2978 return tab;
2979 }
2980
Michael Kolb8233fac2010-10-26 16:08:53 -07002981 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002982 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002983 * @return boolean True if we successfully switched to a different tab. If
2984 * the indexth tab is null, or if that tab is the same as
2985 * the current one, return false.
2986 */
2987 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002988 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002989 Tab currentTab = mTabControl.getCurrentTab();
2990 if (tab == null || tab == currentTab) {
2991 return false;
2992 }
2993 setActiveTab(tab);
2994 return true;
2995 }
2996
2997 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002999 closeCurrentTab(false);
3000 }
3001
3002 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003003 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003004 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003005 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003006 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003007 return;
3008 }
Michael Kolbc831b632011-05-11 09:30:34 -07003009 final Tab current = mTabControl.getCurrentTab();
3010 final int pos = mTabControl.getCurrentPosition();
3011 Tab newTab = current.getParent();
3012 if (newTab == null) {
3013 newTab = mTabControl.getTab(pos + 1);
3014 if (newTab == null) {
3015 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003016 }
3017 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003018 if (andQuit) {
3019 mTabControl.setCurrentTab(newTab);
3020 closeTab(current);
3021 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003022 // Close window
3023 closeTab(current);
3024 }
3025 }
3026
3027 /**
3028 * Close the tab, remove its associated title bar, and adjust mTabControl's
3029 * current tab to a valid value.
3030 */
3031 @Override
3032 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003033 if (tab == mTabControl.getCurrentTab()) {
3034 closeCurrentTab();
3035 } else {
3036 removeTab(tab);
3037 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003038 }
3039
Afzal Najamd4e33312012-04-26 01:54:01 -04003040 /**
3041 * Close all tabs except the current one
3042 */
3043 @Override
3044 public void closeOtherTabs() {
3045 int inactiveTabs = mTabControl.getTabCount() - 1;
3046 for (int i = inactiveTabs; i >= 0; i--) {
3047 Tab tab = mTabControl.getTab(i);
3048 if (tab != mTabControl.getCurrentTab()) {
3049 removeTab(tab);
3050 }
3051 }
3052 }
3053
Michael Kolb8233fac2010-10-26 16:08:53 -07003054 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003055 protected void loadUrlFromContext(String url) {
3056 Tab tab = getCurrentTab();
3057 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003058 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003059 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003060 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003061 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003062 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003063 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003064 }
3065 }
3066 }
3067
3068 /**
3069 * Load the URL into the given WebView and update the title bar
3070 * to reflect the new load. Call this instead of WebView.loadUrl
3071 * directly.
3072 * @param view The WebView used to load url.
3073 * @param url The URL to load.
3074 */
John Reck71e51422011-07-01 16:49:28 -07003075 @Override
3076 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003077 loadUrl(tab, url, null);
3078 }
3079
3080 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3081 if (tab != null) {
3082 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003083 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003084 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003085 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003086 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003087 }
3088
3089 /**
3090 * Load UrlData into a Tab and update the title bar to reflect the new
3091 * load. Call this instead of UrlData.loadIn directly.
3092 * @param t The Tab used to load.
3093 * @param data The UrlData being loaded.
3094 */
3095 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003096 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003097 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003098 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003099 } else {
John Reck38b39652012-06-05 09:22:59 -07003100 if (t != null && data.mDisableUrlOverride) {
3101 t.disableUrlOverridingForLoad();
3102 }
John Reck26b18322011-06-21 13:08:58 -07003103 loadUrl(t, data.mUrl, data.mHeaders);
3104 }
3105 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003106 }
3107
John Reck30c714c2010-12-16 17:30:34 -08003108 @Override
3109 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003110 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003111 //In case of tab can go back (aka tab has navigation entry) do nothing
3112 //else just load homepage in current tab.
3113 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003114 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003115 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003116 }
3117
3118 void goBackOnePageOrQuit() {
3119 Tab current = mTabControl.getCurrentTab();
3120 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003121 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3122 showExitDialog(mActivity);
3123 } else {
3124 /*
3125 * Instead of finishing the activity, simply push this to the back
3126 * of the stack and let ActivityManager to choose the foreground
3127 * activity. As BrowserActivity is singleTask, it will be always the
3128 * root of the task. So we can use either true or false for
3129 * moveTaskToBack().
3130 */
3131 mActivity.moveTaskToBack(true);
3132 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003133 return;
3134 }
John Reckef654f12011-07-12 16:42:08 -07003135 if (current.canGoBack()) {
3136 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003137 } else {
3138 // Check to see if we are closing a window that was created by
3139 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003140 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003141 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003142 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003143 // Now we close the other tab
3144 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003145 } else if (BrowserConfig.getInstance(getContext())
3146 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3147 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003148 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003149 /*
3150 * Instead of finishing the activity, simply push this to the back
3151 * of the stack and let ActivityManager to choose the foreground
3152 * activity. As BrowserActivity is singleTask, it will be always the
3153 * root of the task. So we can use either true or false for
3154 * moveTaskToBack().
3155 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003156 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 }
3158 }
3159 }
3160
3161 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003162 * helper method for key handler
3163 * returns the current tab if it can't advance
3164 */
Michael Kolbc831b632011-05-11 09:30:34 -07003165 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003166 int pos = mTabControl.getCurrentPosition() + 1;
3167 if (pos >= mTabControl.getTabCount()) {
3168 pos = 0;
3169 }
3170 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003171 }
3172
3173 /**
3174 * helper method for key handler
3175 * returns the current tab if it can't advance
3176 */
Michael Kolbc831b632011-05-11 09:30:34 -07003177 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003178 int pos = mTabControl.getCurrentPosition() - 1;
3179 if ( pos < 0) {
3180 pos = mTabControl.getTabCount() - 1;
3181 }
3182 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003183 }
3184
John Reckbcef87f2012-02-03 14:58:34 -08003185 boolean isMenuOrCtrlKey(int keyCode) {
3186 return (KeyEvent.KEYCODE_MENU == keyCode)
3187 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3188 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3189 }
3190
Michael Kolb0035fad2011-03-14 13:25:28 -07003191 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003192 * handle key events in browser
3193 *
3194 * @param keyCode
3195 * @param event
3196 * @return true if handled, false to pass to super
3197 */
John Reck9c35b9c2012-05-30 10:08:50 -07003198 @Override
3199 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003200 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3201 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003202 if (!mUi.isComboViewShowing()) {
3203 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3204 true, false);
3205 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003206 return true;
3207 }
3208
Cary Clark160bbb92011-01-10 11:17:07 -05003209 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003210 // Even if MENU is already held down, we need to call to super to open
3211 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003212 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003213 mMenuIsDown = true;
3214 return false;
3215 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003216
Cary Clark8ff8c662010-12-29 15:03:05 -05003217 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003218 Tab tab = getCurrentTab();
3219 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003220
Cary Clark160bbb92011-01-10 11:17:07 -05003221 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3222 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003223
Michael Kolb8233fac2010-10-26 16:08:53 -07003224 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003225 case KeyEvent.KEYCODE_TAB:
3226 if (event.isCtrlPressed()) {
3227 if (event.isShiftPressed()) {
3228 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003229 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003230 } else {
3231 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003232 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003233 }
3234 return true;
3235 }
3236 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003237 case KeyEvent.KEYCODE_SPACE:
3238 // WebView/WebTextView handle the keys in the KeyDown. As
3239 // the Activity's shortcut keys are only handled when WebView
3240 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003241 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003242 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003243 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003244 pageDown();
3245 }
3246 return true;
3247 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003248 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003249 event.startTracking();
3250 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003251 case KeyEvent.KEYCODE_FORWARD:
3252 if (!noModifiers) break;
3253 tab.goForward();
3254 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003255 case KeyEvent.KEYCODE_DPAD_LEFT:
3256 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003257 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003258 return true;
3259 }
3260 break;
3261 case KeyEvent.KEYCODE_DPAD_RIGHT:
3262 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003263 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003264 return true;
3265 }
3266 break;
3267 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003268 if (ctrl) {
3269 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003270 return true;
3271 }
3272 break;
Michael Kolba4183062011-01-16 10:43:21 -08003273// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003274 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003275 if (ctrl ) {
3276 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003277 return true;
3278 }
3279 break;
Michael Kolba4183062011-01-16 10:43:21 -08003280// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003281// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003282// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003283// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003284// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003285// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003286// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003287// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003288// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003289// case KeyEvent.KEYCODE_M: // unused
3290// case KeyEvent.KEYCODE_N: // in Chrome: new window
3291// case KeyEvent.KEYCODE_O: // in Chrome: open file
3292// case KeyEvent.KEYCODE_P: // in Chrome: print page
3293// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003294// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003295// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3296 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003297 // we can't use the ctrl/shift flags, they check for
3298 // exclusive use of a modifier
3299 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003300 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003301 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003302 } else {
3303 openTabToHomePage();
3304 }
3305 return true;
3306 }
3307 break;
3308// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3309// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003310// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003311// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3312// case KeyEvent.KEYCODE_Y: // unused
3313// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003314 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003315 // it is a regular key and webview is not null
3316 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003317 }
3318
John Reck9c35b9c2012-05-30 10:08:50 -07003319 @Override
3320 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003321 switch(keyCode) {
3322 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003323 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003324 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003325 return true;
3326 }
3327 break;
3328 }
3329 return false;
3330 }
3331
John Reck9c35b9c2012-05-30 10:08:50 -07003332 @Override
3333 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003334 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003335 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003336 if (KeyEvent.KEYCODE_MENU == keyCode
3337 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003338 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003339 }
3340 }
Cary Clark160bbb92011-01-10 11:17:07 -05003341 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003342 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003343 case KeyEvent.KEYCODE_BACK:
3344 if (event.isTracking() && !event.isCanceled()) {
3345 onBackKey();
3346 return true;
3347 }
3348 break;
3349 }
3350 return false;
3351 }
3352
3353 public boolean isMenuDown() {
3354 return mMenuIsDown;
3355 }
3356
John Reck9c35b9c2012-05-30 10:08:50 -07003357 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003358 public void setupAutoFill(Message message) {
3359 // Open the settings activity at the AutoFill profile fragment so that
3360 // the user can create a new profile. When they return, we will dispatch
3361 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003362 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003363 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3364 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003365 }
John Reckb3417f02011-01-14 11:01:05 -08003366
John Reck9c35b9c2012-05-30 10:08:50 -07003367 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003368 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003369 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003370 return true;
3371 }
3372
John Reck1cf4b792011-07-26 10:22:22 -07003373 @Override
3374 public boolean shouldCaptureThumbnails() {
3375 return mUi.shouldCaptureThumbnails();
3376 }
3377
Michael Kolbc3af0672011-08-09 10:24:41 -07003378 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003379 public boolean supportsVoice() {
3380 PackageManager pm = mActivity.getPackageManager();
3381 List activities = pm.queryIntentActivities(new Intent(
3382 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3383 return activities.size() != 0;
3384 }
3385
3386 @Override
3387 public void startVoiceRecognizer() {
3388 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003389 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003390 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3391 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3392 mActivity.startActivityForResult(voice, VOICE_RESULT);
3393 }
3394
Pankaj Garg7b279f62014-08-12 14:47:18 -07003395 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003396 if (mLevel == level)
3397 return;
3398 mLevel = level;
3399 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003400 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3401 lp.dimAmount = level;
3402 mActivity.getWindow().setAttributes(lp);
3403 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3404 } else {
3405 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3406 }
3407 }
3408
Michael Kolb0b129122012-06-04 16:31:58 -07003409 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003410 public void setBlockEvents(boolean block) {
3411 mBlockEvents = block;
3412 }
3413
John Reck9c35b9c2012-05-30 10:08:50 -07003414 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003415 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003416 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003417 }
3418
John Reck9c35b9c2012-05-30 10:08:50 -07003419 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003420 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003421 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003422 }
3423
John Reck9c35b9c2012-05-30 10:08:50 -07003424 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003425 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003426 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003427 }
3428
John Reck9c35b9c2012-05-30 10:08:50 -07003429 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003430 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003431 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003432 }
3433
John Reck9c35b9c2012-05-30 10:08:50 -07003434 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003435 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003436 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003437 }
3438
Pankaj Garg49b79252014-11-07 17:33:41 -08003439 @Override
3440 public boolean shouldShowAppMenu() {
3441 return true;
3442 }
3443
3444 @Override
3445 public int getMenuThemeResourceId() {
3446 return R.style.OverflowMenuTheme;
3447 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003448}