blob: 5c6a9b8d8a0261a5b457d7ed056c86962e8f861f [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;
Mattias Nilsson561d1952011-10-04 10:18:50 +020043import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070044import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080045import android.graphics.BitmapFactory;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080046import android.graphics.Rect;
Tarun Nainanif5563b62015-01-21 18:52:59 -080047import android.media.AudioManager;
kaiyiza016da12013-08-26 17:50:22 +080048import android.net.ConnectivityManager;
49import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.net.Uri;
51import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080052import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080053import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.os.AsyncTask;
Tarun Nainanif5563b62015-01-21 18:52:59 -080055import android.os.Build;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070057import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.os.Handler;
59import android.os.Message;
60import android.os.PowerManager;
61import android.os.PowerManager.WakeLock;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.provider.ContactsContract;
63import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080064import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070065import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070066import android.text.TextUtils;
67import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080068import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070069import android.view.ActionMode;
70import android.view.ContextMenu;
71import android.view.ContextMenu.ContextMenuInfo;
72import android.view.Gravity;
73import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070074import android.view.Menu;
75import android.view.MenuInflater;
76import android.view.MenuItem;
77import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070078import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070080import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070081import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070082import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080083import android.webkit.WebChromeClient.CustomViewCallback;
Pankaj Garg49b79252014-11-07 17:33:41 -080084import android.widget.EditText;
Leon Scrogginsac993842011-02-02 12:54:07 -050085import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070086
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import org.codeaurora.swe.CookieManager;
88import org.codeaurora.swe.CookieSyncManager;
Pankaj Garg18902562014-12-05 16:18:51 -080089import org.codeaurora.swe.Engine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080090import org.codeaurora.swe.HttpAuthHandler;
91import org.codeaurora.swe.SslErrorHandler;
92import org.codeaurora.swe.WebSettings;
93import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070094import org.codeaurora.swe.WebBackForwardList;
95import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080096
Bijan Amirzada41242f22014-03-21 12:12:18 -070097import com.android.browser.IntentHandler.UrlData;
98import com.android.browser.UI.ComboViews;
Dave Tharp9a082b12015-06-19 08:46:57 -070099import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -0700100import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -0700101import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700102import com.android.browser.mynavigation.AddMyNavigationPage;
103import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700104import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700105import com.android.browser.platformsupport.BrowserContract;
106import com.android.browser.platformsupport.WebAddress;
107import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800108import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700109import com.android.browser.provider.BrowserProvider2.Thumbnails;
110import com.android.browser.provider.SnapshotProvider.Snapshots;
111import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800112import com.android.browser.appmenu.AppMenuHandler;
113import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700114
Michael Kolb8233fac2010-10-26 16:08:53 -0700115import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700116import java.io.File;
117import java.io.FileOutputStream;
118import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700119import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700120import java.text.DateFormat;
121import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700122import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700123import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700124import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800125import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200126import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700127import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700128
129/**
130 * Controller for browser
131 */
132public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800133 implements WebViewController, UiController, ActivityController,
134 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700135
136 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800137 private static final String SEND_APP_ID_EXTRA =
138 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Vivek Sekhared791da2015-02-22 12:39:05 -0800139 private static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800140 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
141 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800142
Vivek Sekharb54614f2014-05-01 19:03:37 -0700143 // Remind switch to data connection if wifi is unavailable
144 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800145
Michael Kolb8233fac2010-10-26 16:08:53 -0700146 // public message ids
147 public final static int LOAD_URL = 1001;
148 public final static int STOP_LOAD = 1002;
149
150 // Message Ids
151 private static final int FOCUS_NODE_HREF = 102;
152 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800153 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700154
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800156 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700157
158 private static final int EMPTY_MENU = -1;
159
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700161 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 final static int PREFERENCES_PAGE = 3;
163 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000164 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700165 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800166 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000167
Michael Kolb8233fac2010-10-26 16:08:53 -0700168 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
169
170 // As the ids are dynamically created, we can't guarantee that they will
171 // be in sequence, so this static array maps ids to a window number.
172 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
173 { R.id.window_one_menu_id, R.id.window_two_menu_id,
174 R.id.window_three_menu_id, R.id.window_four_menu_id,
175 R.id.window_five_menu_id, R.id.window_six_menu_id,
176 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
177
178 // "source" parameter for Google search through search key
179 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
180 // "source" parameter for Google search through simplily type
181 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
182
George Mount387d45d2011-10-07 15:57:53 -0700183 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700184 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
185
John Reckd7dd9b22011-08-30 09:18:29 -0700186 // A bitmap that is re-used in createScreenshot as scratch space
187 private static Bitmap sThumbnailBitmap;
188
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private Activity mActivity;
190 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700191 private HomepageHandler mHomepageHandler;
192 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 private BrowserSettings mSettings;
194 private WebViewFactory mFactory;
195
196 private WakeLock mWakeLock;
197
198 private UrlHandler mUrlHandler;
199 private UploadHandler mUploadHandler;
200 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700201 private PageDialogsHandler mPageDialogsHandler;
202 private NetworkStateHandler mNetworkHandler;
203
Ben Murdoch8029a772010-11-16 11:58:21 +0000204 private Message mAutoFillSetupMessage;
205
kaiyiza016da12013-08-26 17:50:22 +0800206 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700207
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 // FIXME, temp address onPrepareMenu performance problem.
209 // When we move everything out of view, we should rewrite this.
210 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700211 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 private int mOldMenuState = EMPTY_MENU;
213 private Menu mCachedMenu;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 private boolean mMenuIsDown;
216
Pankaj Garg49b79252014-11-07 17:33:41 -0800217 private boolean mWasInPageLoad = false;
218 private AppMenuHandler mAppMenuHandler;
219
Michael Kolb8233fac2010-10-26 16:08:53 -0700220 // For select and find, we keep track of the ActionMode so that
221 // finish() can be called as desired.
222 private ActionMode mActionMode;
223
224 /**
225 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
226 * of whether the configuration has changed. The first onMenuOpened call
227 * after a configuration change is simply a reopening of the same menu
228 * (i.e. mIconView did not change).
229 */
230 private boolean mConfigChanged;
231
232 /**
233 * Keeps track of whether the options menu is open. This is important in
234 * determining whether to show or hide the title bar overlay
235 */
236 private boolean mOptionsMenuOpen;
237
238 /**
239 * Whether or not the options menu is in its bigger, popup menu form. When
240 * true, we want the title bar overlay to be gone. When false, we do not.
241 * Only meaningful if mOptionsMenuOpen is true.
242 */
243 private boolean mExtendedMenuOpen;
244
Michael Kolb8233fac2010-10-26 16:08:53 -0700245 private boolean mActivityPaused = true;
246 private boolean mLoadStopped;
247
248 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500249 // Checks to see when the bookmarks database has changed, and updates the
250 // Tabs' notion of whether they represent bookmarked sites.
251 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700252 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700253
Michael Kolbc3af0672011-08-09 10:24:41 -0700254 private boolean mBlockEvents;
255
Michael Kolb0b129122012-06-04 16:31:58 -0700256 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800257 private boolean mUpdateMyNavThumbnail;
258 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800259 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800260 private WebView.HitTestResult mResult;
Site Maoabb7bd32015-03-20 15:34:02 -0700261 private PowerConnectionReceiver mLowPowerReceiver;
262 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700263
George Mount3636d0a2011-11-21 09:08:21 -0800264 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 mActivity = browser;
266 mSettings = BrowserSettings.getInstance();
267 mTabControl = new TabControl(this);
268 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700269 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800270 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700271 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700272
273 mUrlHandler = new UrlHandler(this);
274 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
276
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500278 mBookmarksObserver = new ContentObserver(mHandler) {
279 @Override
280 public void onChange(boolean selfChange) {
281 int size = mTabControl.getTabCount();
282 for (int i = 0; i < size; i++) {
283 mTabControl.getTab(i).updateBookmarkedStatus();
284 }
285 }
286
287 };
288 browser.getContentResolver().registerContentObserver(
289 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700290
291 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700292 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800293 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700294 }
295
John Reck9c35b9c2012-05-30 10:08:50 -0700296 @Override
297 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700298 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800299 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800300 // mCrashRecoverHandler has any previously saved state.
301 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700302 }
303
George Mount3636d0a2011-11-21 09:08:21 -0800304 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800305 // we dont want to ever recover incognito tabs
306 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700307
Patrick Scott7d50a932011-02-04 09:27:26 -0500308 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700309 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000311
Michael Kolbc831b632011-05-11 09:30:34 -0700312 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500313 // Not able to restore so we go ahead and clear session cookies. We
314 // must do this before trying to login the user as we don't want to
315 // clear any session cookies set during login.
316 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700317 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800318 if (intent == null) {
319 // This won't happen under common scenarios. The icicle is
320 // not null, but there aren't any tabs to restore.
321 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700322 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800323 final Bundle extra = intent.getExtras();
324 // Create an initial tab.
325 // If the intent is ACTION_VIEW and data is not null, the Browser is
326 // invoked to view the content by another application. In this case,
327 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800328 UrlData urlData = null;
329 if (intent.getData() != null
330 && Intent.ACTION_VIEW.equals(intent.getAction())
331 && intent.getData().toString().startsWith("content://")) {
332 urlData = new UrlData(intent.getData().toString());
333 } else {
334 urlData = IntentHandler.getUrlDataFromIntent(intent);
335 }
George Mount3636d0a2011-11-21 09:08:21 -0800336 Tab t = null;
337 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700338 String landingPage = mActivity.getResources().getString(
339 R.string.def_landing_page);
340 if (!landingPage.isEmpty()) {
341 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800342 } else {
343 t = openTabToHomePage();
344 }
George Mount3636d0a2011-11-21 09:08:21 -0800345 } else {
346 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700347 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800348 }
349 if (t != null) {
350 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
351 }
352 WebView webView = t.getWebView();
353 if (extra != null) {
354 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
355 if (scale > 0 && scale <= 1000) {
356 webView.setInitialScale(scale);
357 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 }
359 }
John Reckd8c74522011-06-14 08:45:00 -0700360 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700362 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500363 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700364 List<Tab> tabs = mTabControl.getTabs();
365 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700366
John Reck1cf4b792011-07-26 10:22:22 -0700367 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700368 //handle restored pages that may require a JS interface
369 if (t.getWebView() != null) {
370 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
371 if (backForwardList != null) {
372 for (int i = 0; i < backForwardList.getSize(); i++) {
373 WebHistoryItem item = backForwardList.getItemAtIndex(i);
374 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
375 }
376 }
377 }
John Reck1cf4b792011-07-26 10:22:22 -0700378 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700379 if (t != mTabControl.getCurrentTab()) {
380 t.pause();
381 }
John Reck1cf4b792011-07-26 10:22:22 -0700382 }
383 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700384 if (tabs.size() == 0) {
385 openTabToHomePage();
386 }
John Reck1cf4b792011-07-26 10:22:22 -0700387 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 // TabControl.restoreState() will create a new tab even if
389 // restoring the state fails.
390 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800391 // Intent is non-null when framework thinks the browser should be
392 // launching with a new intent (icicle is null).
393 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700394 mIntentHandler.onNewIntent(intent);
395 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700398 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800399 if (jsFlags.trim().length() != 0) {
400 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 }
George Mount3636d0a2011-11-21 09:08:21 -0800402 if (intent != null
403 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700404 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800405 }
Site Maoabb7bd32015-03-20 15:34:02 -0700406 mLowPowerReceiver = new PowerConnectionReceiver();
407 mPowerChangeReceiver = new PowerConnectionReceiver();
408
409 //always track the android framework's power save mode
410 IntentFilter filter = new IntentFilter();
411 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
412 // Power save mode only exists in Lollipop and above
413 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
414 }
415 filter.addAction(Intent.ACTION_BATTERY_OKAY);
416 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700417 }
418
John Reck1cf4b792011-07-26 10:22:22 -0700419 private static class PruneThumbnails implements Runnable {
420 private Context mContext;
421 private List<Long> mIds;
422
423 PruneThumbnails(Context context, List<Long> preserveIds) {
424 mContext = context.getApplicationContext();
425 mIds = preserveIds;
426 }
427
428 @Override
429 public void run() {
430 ContentResolver cr = mContext.getContentResolver();
431 if (mIds == null || mIds.size() == 0) {
432 cr.delete(Thumbnails.CONTENT_URI, null, null);
433 } else {
434 int length = mIds.size();
435 StringBuilder where = new StringBuilder();
436 where.append(Thumbnails._ID);
437 where.append(" not in (");
438 for (int i = 0; i < length; i++) {
439 where.append(mIds.get(i));
440 if (i < (length - 1)) {
441 where.append(",");
442 }
443 }
444 where.append(")");
445 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
446 }
447 }
448
449 }
450
Michael Kolb1514bb72010-11-22 09:11:48 -0800451 @Override
452 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 return mFactory;
454 }
455
456 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800457 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700458 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700459 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800460 }
461
462 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800463 public void createSubWindow(Tab tab) {
464 endActionMode();
465 WebView mainView = tab.getWebView();
466 WebView subView = mFactory.createWebView((mainView == null)
467 ? false
468 : mainView.isPrivateBrowsingEnabled());
469 mUi.createSubWindow(tab, subView);
470 }
471
472 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700473 public Context getContext() {
474 return mActivity;
475 }
476
477 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700478 public Activity getActivity() {
479 return mActivity;
480 }
481
482 void setUi(UI ui) {
483 mUi = ui;
484 }
485
Michael Kolbaf63dba2012-05-16 12:58:05 -0700486 @Override
487 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700488 return mSettings;
489 }
490
491 IntentHandler getIntentHandler() {
492 return mIntentHandler;
493 }
494
495 @Override
496 public UI getUi() {
497 return mUi;
498 }
499
500 int getMaxTabs() {
501 return mActivity.getResources().getInteger(R.integer.max_tabs);
502 }
503
504 @Override
505 public TabControl getTabControl() {
506 return mTabControl;
507 }
508
Michael Kolb1bf23132010-11-19 12:55:12 -0800509 @Override
510 public List<Tab> getTabs() {
511 return mTabControl.getTabs();
512 }
513
Michael Kolb8233fac2010-10-26 16:08:53 -0700514 private void startHandler() {
515 mHandler = new Handler() {
516
517 @Override
518 public void handleMessage(Message msg) {
519 switch (msg.what) {
520 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700521 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700522 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800523 case UNKNOWN_TYPE_MSG:
524 HashMap unknownTypeMap = (HashMap) msg.obj;
525 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
526 /*
527 * When the context menu is shown to the user
528 * we need to assure that its happening on the current webview
529 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
530 */
531 if (getCurrentWebView() != viewForUnknownType)
532 break;
533
534 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800535 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800536 WebView.HitTestResult result = new WebView.HitTestResult();
537
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800538 // Prevent unnecessary calls to context menu
539 // if url and image src are null
540 if (unknown_type_src == null && unknown_type_url == null)
541 break;
542
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800543 //setting the HitTestResult with new RESULT TYPE
544 if (!TextUtils.isEmpty(unknown_type_src)) {
545 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
546 result.setExtra(unknown_type_src);
547 } else {
548 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
549 result.setExtra("about:blank");
550 }
551
552 mResult = result;
553 openContextMenu(viewForUnknownType);
554 mResult = null;
555
556 break;
557
Michael Kolb8233fac2010-10-26 16:08:53 -0700558 case FOCUS_NODE_HREF:
559 {
560 String url = (String) msg.getData().get("url");
561 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500562 String src = (String) msg.getData().get("src");
563 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700564 if (TextUtils.isEmpty(url)) {
565 break;
566 }
567 HashMap focusNodeMap = (HashMap) msg.obj;
568 WebView view = (WebView) focusNodeMap.get("webview");
569 // Only apply the action if the top window did not change.
570 if (getCurrentTopWebView() != view) {
571 break;
572 }
573 switch (msg.arg1) {
574 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700575 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700576 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500577 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700578 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500579 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500580 case R.id.open_newtab_context_menu_id:
581 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700582 openTab(url, parent,
583 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500584 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700585 case R.id.copy_link_context_menu_id:
586 copy(url);
587 break;
588 case R.id.save_link_context_menu_id:
589 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500590 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800591 mActivity, url, view.getSettings().getUserAgentString(),
592 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700594 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700595 if(title == null || title == "")
596 title = url;
597
598 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
599 //SWE TODO: No thumbnail support for the url obtained via
600 //browser context menu as its not loaded in webview.
601 if (bookmarkIntent != null) {
602 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
603 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
604 mActivity.startActivity(bookmarkIntent);
605 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700606 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700607 }
608 break;
609 }
610
611 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700612 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 break;
614
615 case STOP_LOAD:
616 stopLoading();
617 break;
618
619 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700620 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 mWakeLock.release();
622 // if we reach here, Browser should be still in the
623 // background loading after WAKELOCK_TIMEOUT (5-min).
624 // To avoid burning the battery, stop loading.
625 mTabControl.stopAllLoading();
626 }
627 break;
628
kaiyiz591110b2013-08-06 17:11:06 +0800629 case OPEN_MENU:
630 if (!mOptionsMenuOpen && mActivity != null ) {
631 mActivity.openOptionsMenu();
632 }
633 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700634 }
635 }
636 };
637
638 }
639
John Reckef654f12011-07-12 16:42:08 -0700640 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200641 public Tab getCurrentTab() {
642 return mTabControl.getCurrentTab();
643 }
644
Michael Kolbba99c5d2010-11-29 14:57:41 -0800645 @Override
646 public void shareCurrentPage() {
647 shareCurrentPage(mTabControl.getCurrentTab());
648 }
649
650 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700651 if (tab == null || tab.getWebView() == null)
652 return;
653
654 final Tab mytab = tab;
655 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
656 @Override
657 public void onReceiveValue(Bitmap bitmap) {
658 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
659 mytab.getFavicon(), bitmap);
660 }
661 };
662
663 createScreenshotAsync(
664 tab.getWebView(),
665 getDesiredThumbnailWidth(mActivity),
666 getDesiredThumbnailHeight(mActivity),
667 new ValueCallback<Bitmap>() {
668 @Override
669 public void onReceiveValue(Bitmap bitmap) {
670 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
671 mytab.getFavicon(), bitmap);
672 }
673 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800674 }
675
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 /**
677 * Share a page, providing the title, url, favicon, and a screenshot. Uses
678 * an {@link Intent} to launch the Activity chooser.
679 * @param c Context used to launch a new Activity.
680 * @param title Title of the page. Stored in the Intent with
681 * {@link Intent#EXTRA_SUBJECT}
682 * @param url URL of the page. Stored in the Intent with
683 * {@link Intent#EXTRA_TEXT}
684 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
685 * with {@link Browser#EXTRA_SHARE_FAVICON}
686 * @param screenshot Bitmap of a screenshot of the page. Stored in the
687 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
688 */
689 static final void sharePage(Context c, String title, String url,
690 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700691
692 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
693 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
694 R.layout.app_row, sDialog.getApps());
695 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 }
697
698 private void copy(CharSequence text) {
699 ClipboardManager cm = (ClipboardManager) mActivity
700 .getSystemService(Context.CLIPBOARD_SERVICE);
701 cm.setText(text);
702 }
703
704 // lifecycle
705
John Reck9c35b9c2012-05-30 10:08:50 -0700706 @Override
707 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700708 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800709 // update the menu in case of a locale change
710 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800711 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800712 if (mOptionsMenuOpen) {
713 mActivity.closeOptionsMenu();
714 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
715 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700716 if (mPageDialogsHandler != null) {
717 mPageDialogsHandler.onConfigurationChanged(config);
718 }
719 mUi.onConfigurationChanged(config);
720 }
721
722 @Override
723 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700724 if (!mUi.isWebShowing()) {
725 mUi.showWeb(false);
726 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 mIntentHandler.onNewIntent(intent);
728 }
729
John Reck9c35b9c2012-05-30 10:08:50 -0700730 @Override
731 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800732 if (mUi.isCustomViewShowing()) {
733 hideCustomView();
734 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700735 if (mActivityPaused) {
736 Log.e(LOGTAG, "BrowserActivity is already paused.");
737 return;
738 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800740 Tab tab = mTabControl.getCurrentTab();
741 if (tab != null) {
742 tab.pause();
743 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700744 if (mWakeLock == null) {
745 PowerManager pm = (PowerManager) mActivity
746 .getSystemService(Context.POWER_SERVICE);
747 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
748 }
Michael Kolb70976932010-11-30 11:34:01 -0800749 mWakeLock.acquire();
750 mHandler.sendMessageDelayed(mHandler
751 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
752 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 }
754 mUi.onPause();
755 mNetworkHandler.onPause();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100756 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700757 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 }
759
John Reck9c35b9c2012-05-30 10:08:50 -0700760 @Override
761 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800763 Bundle saveState = createSaveState();
764
765 // crash recovery manages all save & restore state
766 mCrashRecoveryHandler.writeState(saveState);
767 mSettings.setLastRunPaused(true);
768 }
769
770 /**
771 * Save the current state to outState. Does not write the state to
772 * disk.
773 * @return Bundle containing the current state of all tabs.
774 */
775 /* package */ Bundle createSaveState() {
776 Bundle saveState = new Bundle();
777 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800778 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700779 }
780
John Reck9c35b9c2012-05-30 10:08:50 -0700781 @Override
782 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700783 if (!mActivityPaused) {
784 Log.e(LOGTAG, "BrowserActivity is already resumed.");
785 return;
786 }
George Mount3636d0a2011-11-21 09:08:21 -0800787 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700788 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800789 Tab current = mTabControl.getCurrentTab();
790 if (current != null) {
791 current.resume();
792 resumeWebViewTimers(current);
793 }
John Reckf57c0292011-07-21 18:15:39 -0700794 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200795
Michael Kolb8233fac2010-10-26 16:08:53 -0700796 mUi.onResume();
797 mNetworkHandler.onResume();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100798 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700799 if (mVoiceResult != null) {
800 mUi.onVoiceResult(mVoiceResult);
801 mVoiceResult = null;
802 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700803 if (current != null && current.getWebView().isShowingCrashView())
804 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700805 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 }
807
John Reckf57c0292011-07-21 18:15:39 -0700808 private void releaseWakeLock() {
809 if (mWakeLock != null && mWakeLock.isHeld()) {
810 mHandler.removeMessages(RELEASE_WAKELOCK);
811 mWakeLock.release();
812 }
813 }
814
Michael Kolb70976932010-11-30 11:34:01 -0800815 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800816 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800817 * @param tab guaranteed non-null
818 */
819 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 boolean inLoad = tab.inPageLoad();
821 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
822 CookieSyncManager.getInstance().startSync();
823 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100824 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 }
826 }
827
Michael Kolb70976932010-11-30 11:34:01 -0800828 /**
829 * Pause all WebView timers using the WebView of the given tab
830 * @param tab
831 * @return true if the timers are paused or tab is null
832 */
833 private boolean pauseWebViewTimers(Tab tab) {
834 if (tab == null) {
835 return true;
836 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100838 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 }
Michael Kolb70976932010-11-30 11:34:01 -0800841 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 }
843
John Reck9c35b9c2012-05-30 10:08:50 -0700844 @Override
845 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800846 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
848 mUploadHandler = null;
849 }
850 if (mTabControl == null) return;
851 mUi.onDestroy();
852 // Remove the current tab and sub window
853 Tab t = mTabControl.getCurrentTab();
854 if (t != null) {
855 dismissSubWindow(t);
856 removeTab(t);
857 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500858 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 // Destroy all the tabs
860 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700861 // Unregister receiver
862 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 }
864
865 protected boolean isActivityPaused() {
866 return mActivityPaused;
867 }
868
John Reck9c35b9c2012-05-30 10:08:50 -0700869 @Override
870 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 mTabControl.freeMemory();
872 }
873
874 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700875 public void stopLoading() {
876 mLoadStopped = true;
877 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700879 if (w != null) {
880 w.stopLoading();
881 mUi.onPageStopped(tab);
882 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 }
884
885 boolean didUserStopLoading() {
886 return mLoadStopped;
887 }
888
kaiyiza016da12013-08-26 17:50:22 +0800889 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700890 final String reminderType = getContext().getResources().getString(
891 R.string.def_wifi_browser_interaction_remind_type);
892 final String selectionConnnection = getContext().getResources().getString(
893 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700894 final String wifiSelection = getContext().getResources().getString(
895 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700896
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700897 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
898 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700899 return;
900
kaiyiza016da12013-08-26 17:50:22 +0800901 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700902 Context.CONNECTIVITY_SERVICE);
903 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700904 WifiManager wifiMgr = (WifiManager) this.getContext()
905 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700906 if (networkInfo == null
907 || (networkInfo != null && (networkInfo.getType() !=
908 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700909 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
910 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700911 List<ScanResult> list = wifiMgr.getScanResults();
912 // Have no AP's for Wifi's fall back to data
913 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700914 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700915 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
916 this.getContext().startActivity(intent);
917 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700918 // Request to select Wifi AP
919 try {
920 Intent intent = new Intent(wifiSelection);
921 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
922 this.getContext().startActivity(intent);
923 } catch (Exception e) {
924 String err_msg = this.getContext().getString(
925 R.string.acivity_not_found, wifiSelection);
926 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
927 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700928 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700929 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800930 }
931 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700932
Michael Kolb8233fac2010-10-26 16:08:53 -0700933 // WebViewController
934
935 @Override
John Reck324d4402011-01-11 16:56:42 -0800936 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700937
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 // reset sync timer to avoid sync starts during loading a page
939 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700940 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700941 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800942 boolean networkNotifier = BrowserConfig.getInstance(getContext())
943 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700944 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700945 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800946 } else {
947 if (!mNetworkHandler.isNetworkUp()) {
948 view.setNetworkAvailable(false);
949 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 }
951
952 // when BrowserActivity just starts, onPageStarted may be called before
953 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
954 // to start the timer. As we won't switch tabs while an activity is in
955 // pause state, we can ensure calling resume and pause in pair.
956 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800957 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 }
959 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700960 endActionMode();
961
John Reck30c714c2010-12-16 17:30:34 -0800962 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700963
John Reck324d4402011-01-11 16:56:42 -0800964 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 // update the bookmark database for favicon
966 maybeUpdateFavicon(tab, null, url, favicon);
967
968 Performance.tracePageStart(url);
969
970 // Performance probe
971 if (false) {
972 Performance.onPageStarted();
973 }
974
975 }
976
977 @Override
John Reck324d4402011-01-11 16:56:42 -0800978 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700979 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800980 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200981
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 // Performance probe
983 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800984 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 }
986
Tarun Nainani8eb00912014-07-17 12:28:32 -0700987 tab.onPageFinished();
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700988 maybeUpdateFavicon(tab, tab.getOriginalUrl(), tab.getUrl(), tab.getFavicon());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700989
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 Performance.tracePageFinished();
991 }
992
993 @Override
John Reck30c714c2010-12-16 17:30:34 -0800994 public void onProgressChanged(Tab tab) {
995 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700996
997 if (newProgress == 100) {
998 CookieSyncManager.getInstance().sync();
999 // onProgressChanged() may continue to be called after the main
1000 // frame has finished loading, as any remaining sub frames continue
1001 // to load. We'll only get called once though with newProgress as
1002 // 100 when everything is loaded. (onPageFinished is called once
1003 // when the main frame completes loading regardless of the state of
1004 // any sub frames so calls to onProgressChanges may continue after
1005 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001006 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001007 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001008 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001009 } else if (mWasInPageLoad) {
1010 mWasInPageLoad = false;
1011 updateInLoadMenuItems(mCachedMenu, tab);
1012 }
1013
1014 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001015 // pause the WebView timer and release the wake lock if it is
1016 // finished while BrowserActivity is in pause state.
1017 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 }
1019 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001020 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001021 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001022 // still loading
1023 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001025 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001026 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001027 } else {
1028 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001029 }
1030 }
John Reck30c714c2010-12-16 17:30:34 -08001031 mUi.onProgressChanged(tab);
1032 }
1033
1034 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001035 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001036 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 }
1038
1039 @Override
1040 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001041 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001042 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001043 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1045 return;
1046 }
1047 // Update the title in the history database if not in private browsing mode
1048 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001049 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 }
1051 }
1052
1053 @Override
1054 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001055 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1057 }
1058
1059 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001060 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1061 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 }
1063
1064 @Override
1065 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1066 if (mMenuIsDown) {
1067 // only check shortcut key when MENU is held
1068 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1069 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001070 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001071 int keyCode = event.getKeyCode();
1072 // We need to send almost every key to WebKit. However:
1073 // 1. We don't want to block the device on the renderer for
1074 // some keys like menu, home, call.
1075 // 2. There are no WebKit equivalents for some of these keys
1076 // (see app/keyboard_codes_win.h)
1077 // Note that these are not the same set as KeyEvent.isSystemKey:
1078 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1079 if (keyCode == KeyEvent.KEYCODE_MENU ||
1080 keyCode == KeyEvent.KEYCODE_HOME ||
1081 keyCode == KeyEvent.KEYCODE_BACK ||
1082 keyCode == KeyEvent.KEYCODE_CALL ||
1083 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1084 keyCode == KeyEvent.KEYCODE_POWER ||
1085 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1086 keyCode == KeyEvent.KEYCODE_CAMERA ||
1087 keyCode == KeyEvent.KEYCODE_FOCUS ||
1088 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1089 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1090 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1091 return true;
1092 }
1093
1094 // We also have to intercept some shortcuts before we send them to the ContentView.
1095 if (event.isCtrlPressed() && (
1096 keyCode == KeyEvent.KEYCODE_TAB ||
1097 keyCode == KeyEvent.KEYCODE_W ||
1098 keyCode == KeyEvent.KEYCODE_F4)) {
1099 return true;
1100 }
1101
1102 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 }
1104
Tarun Nainanif5563b62015-01-21 18:52:59 -08001105 private void handleMediaKeyEvent(KeyEvent event) {
1106
1107 int keyCode = event.getKeyCode();
1108 // send media key events to audio manager
1109 if (Build.VERSION.SDK_INT >= 19) {
1110
1111 switch (keyCode) {
1112 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1113 case KeyEvent.KEYCODE_MEDIA_STOP:
1114 case KeyEvent.KEYCODE_MEDIA_NEXT:
1115 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1116 case KeyEvent.KEYCODE_MEDIA_REWIND:
1117 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1118 case KeyEvent.KEYCODE_MUTE:
1119 case KeyEvent.KEYCODE_MEDIA_PLAY:
1120 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1121 case KeyEvent.META_SHIFT_RIGHT_ON:
1122 case KeyEvent.KEYCODE_MEDIA_EJECT:
1123 case KeyEvent.KEYCODE_MEDIA_RECORD:
1124 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1125
1126 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1127 .getSystemService(Context.AUDIO_SERVICE);
1128 audioManager.dispatchMediaKeyEvent(event);
1129 }
1130 }
1131 }
1132
Michael Kolb8233fac2010-10-26 16:08:53 -07001133 @Override
John Reck997b1b72012-04-19 18:08:25 -07001134 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001136 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001138 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 } else {
John Reck997b1b72012-04-19 18:08:25 -07001140 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 }
1142 }
John Reck997b1b72012-04-19 18:08:25 -07001143 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 }
1145
1146 @Override
John Reck324d4402011-01-11 16:56:42 -08001147 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001148 // Don't save anything in private browsing mode or when disabling history
1149 // for regular tabs is enabled
1150 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1151 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1152 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001153
John Reck49a603c2011-03-03 09:33:05 -08001154 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001155
John Reck324d4402011-01-11 16:56:42 -08001156 if (TextUtils.isEmpty(url)
1157 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001158 return;
1159 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001160
John Reckf57c0292011-07-21 18:15:39 -07001161 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001162 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001163 }
1164
1165 @Override
1166 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1167 AsyncTask<Void, Void, String[]> task =
1168 new AsyncTask<Void, Void, String[]>() {
1169 @Override
1170 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001171 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 }
1173 @Override
1174 public void onPostExecute(String[] result) {
1175 callback.onReceiveValue(result);
1176 }
1177 };
1178 task.execute();
1179 }
1180
1181 @Override
1182 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1183 final HttpAuthHandler handler, final String host,
1184 final String realm) {
1185 String username = null;
1186 String password = null;
1187
1188 boolean reuseHttpAuthUsernamePassword
1189 = handler.useHttpAuthUsernamePassword();
1190
1191 if (reuseHttpAuthUsernamePassword && view != null) {
1192 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1193 if (credentials != null && credentials.length == 2) {
1194 username = credentials[0];
1195 password = credentials[1];
1196 }
1197 }
1198
1199 if (username != null && password != null) {
1200 handler.proceed(username, password);
1201 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001202 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1204 } else {
1205 handler.cancel();
1206 }
1207 }
1208 }
1209
1210 @Override
1211 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001212 String contentDisposition, String mimetype, String referer,
1213 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001214 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001215 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001216 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001217 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001218 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 // This Tab was opened for the sole purpose of downloading a
1220 // file. Remove it.
1221 if (tab == mTabControl.getCurrentTab()) {
1222 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001223 if (tab.getDerivedFromIntent())
1224 closeTab(tab);
1225 else
1226 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001227 } else {
1228 // In this case, it is not.
1229 closeTab(tab);
1230 }
1231 }
1232 }
1233
1234 @Override
1235 public Bitmap getDefaultVideoPoster() {
1236 return mUi.getDefaultVideoPoster();
1237 }
1238
1239 @Override
1240 public View getVideoLoadingProgressView() {
1241 return mUi.getVideoLoadingProgressView();
1242 }
1243
1244 @Override
1245 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1246 SslError error) {
1247 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1248 }
1249
1250 // helper method
1251
1252 /*
1253 * Update the favorites icon if the private browsing isn't enabled and the
1254 * icon is valid.
1255 */
1256 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1257 final String url, Bitmap favicon) {
1258 if (favicon == null) {
1259 return;
1260 }
1261 if (!tab.isPrivateBrowsingEnabled()) {
1262 Bookmarks.updateFavicon(mActivity
1263 .getContentResolver(), originalUrl, url, favicon);
1264 }
1265 }
1266
Leon Scroggins4cd97792010-12-03 15:31:56 -05001267 @Override
1268 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001269 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001270 mUi.bookmarkedStatusHasChanged(tab);
1271 }
1272
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 // end WebViewController
1274
1275 protected void pageUp() {
1276 getCurrentTopWebView().pageUp(false);
1277 }
1278
1279 protected void pageDown() {
1280 getCurrentTopWebView().pageDown(false);
1281 }
1282
1283 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001284 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001285 public void editUrl() {
1286 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001287 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001288 }
1289
John Reck9c35b9c2012-05-30 10:08:50 -07001290 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001291 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001292 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 if (tab.inForeground()) {
1294 if (mUi.isCustomViewShowing()) {
1295 callback.onCustomViewHidden();
1296 return;
1297 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001298 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001299 // Save the menu state and set it to empty while the custom
1300 // view is showing.
1301 mOldMenuState = mMenuState;
1302 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001303 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 }
1305 }
1306
1307 @Override
1308 public void hideCustomView() {
1309 if (mUi.isCustomViewShowing()) {
1310 mUi.onHideCustomView();
1311 // Reset the old menu state.
1312 mMenuState = mOldMenuState;
1313 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001314 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001315 }
1316 }
1317
John Reck9c35b9c2012-05-30 10:08:50 -07001318 @Override
1319 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001320 Intent intent) {
1321 if (getCurrentTopWebView() == null) return;
1322 switch (requestCode) {
1323 case PREFERENCES_PAGE:
1324 if (resultCode == Activity.RESULT_OK && intent != null) {
1325 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001326 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 mTabControl.removeParentChildRelationShips();
1328 }
1329 }
1330 break;
1331 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001332 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001333 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001334 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001336 case AUTOFILL_SETUP:
1337 // Determine whether a profile was actually set up or not
1338 // and if so, send the message back to the WebTextView to
1339 // fill the form with the new profile.
1340 if (getSettings().getAutoFillProfile() != null) {
1341 mAutoFillSetupMessage.sendToTarget();
1342 mAutoFillSetupMessage = null;
1343 }
1344 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001345 case COMBO_VIEW:
1346 if (intent == null || resultCode != Activity.RESULT_OK) {
1347 break;
1348 }
John Reck3ba45532011-08-11 16:26:53 -07001349 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001350 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1351 Tab t = getCurrentTab();
1352 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001353 mUpdateMyNavThumbnail = true;
1354 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001355 loadUrl(t, uri.toString());
1356 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1357 String[] urls = intent.getStringArrayExtra(
1358 ComboViewActivity.EXTRA_OPEN_ALL);
1359 Tab parent = getCurrentTab();
1360 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001361 if (url != null) {
1362 parent = openTab(url, parent,
1363 !mSettings.openInBackground(), true);
1364 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001365 }
1366 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1367 long id = intent.getLongExtra(
1368 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1369 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001370 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001371 }
1372 }
1373 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001374 case VOICE_RESULT:
1375 if (resultCode == Activity.RESULT_OK && intent != null) {
1376 ArrayList<String> results = intent.getStringArrayListExtra(
1377 RecognizerIntent.EXTRA_RESULTS);
1378 if (results.size() >= 1) {
1379 mVoiceResult = results.get(0);
1380 }
1381 }
1382 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001383 case MY_NAVIGATION:
1384 if (intent == null || resultCode != Activity.RESULT_OK) {
1385 break;
1386 }
1387
1388 if (intent.getBooleanExtra("need_refresh", false) &&
1389 getCurrentTopWebView() != null) {
1390 getCurrentTopWebView().reload();
1391 }
1392 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001393 default:
1394 break;
1395 }
1396 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001397 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 }
1399
1400 /**
1401 * Open the Go page.
1402 * @param startWithHistory If true, open starting on the history tab.
1403 * Otherwise, start with the bookmarks tab.
1404 */
1405 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001406 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 if (mTabControl.getCurrentWebView() == null) {
1408 return;
1409 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001410 // clear action mode
1411 if (isInCustomActionMode()) {
1412 endActionMode();
1413 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 Bundle extras = new Bundle();
1415 // Disable opening in a new window if we have maxed out the windows
1416 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1417 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001418 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 }
1420
1421 // combo view callbacks
1422
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 // key handling
1424 protected void onBackKey() {
1425 if (!mUi.onBackKey()) {
1426 WebView subwindow = mTabControl.getCurrentSubWindow();
1427 if (subwindow != null) {
1428 if (subwindow.canGoBack()) {
1429 subwindow.goBack();
1430 } else {
1431 dismissSubWindow(mTabControl.getCurrentTab());
1432 }
1433 } else {
1434 goBackOnePageOrQuit();
1435 }
1436 }
1437 }
1438
Michael Kolb4bd767d2011-05-27 11:33:55 -07001439 protected boolean onMenuKey() {
1440 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001441 }
1442
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 // menu handling and state
1444 // TODO: maybe put into separate handler
1445
John Reck9c35b9c2012-05-30 10:08:50 -07001446 @Override
1447 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001448 if (mMenuState == EMPTY_MENU) {
1449 return false;
1450 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 MenuInflater inflater = mActivity.getMenuInflater();
1452 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 return true;
1454 }
1455
John Reck9c35b9c2012-05-30 10:08:50 -07001456 @Override
1457 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001459 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001460 return;
1461 }
1462 if (!(v instanceof WebView)) {
1463 return;
1464 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001465 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001466 WebView.HitTestResult result;
1467
1468 /* Determine whether the ContextMenu got triggered because
1469 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1470 * received. The mResult acts as a flag to identify, how it got trigerred
1471 */
1472 if (mResult == null){
1473 result = webview.getHitTestResult();
1474 } else {
1475 result = mResult;
1476 }
1477
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 if (result == null) {
1479 return;
1480 }
1481
1482 int type = result.getType();
1483 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001484
1485 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1486 unknownTypeMap.put("webview", webview);
1487 final Message msg = mHandler.obtainMessage(
1488 UNKNOWN_TYPE_MSG, unknownTypeMap);
1489 /* As defined in android developers guide
1490 * when UNKNOWN_TYPE is received as a result of HitTest
1491 * you need to determing the type by invoking requestFocusNodeHref
1492 */
1493 webview.requestFocusNodeHref(msg);
1494
Michael Kolb8233fac2010-10-26 16:08:53 -07001495 Log.w(LOGTAG,
1496 "We should not show context menu when nothing is touched");
1497 return;
1498 }
1499 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1500 // let TextView handles context menu
1501 return;
1502 }
1503
1504 // Note, http://b/issue?id=1106666 is requesting that
1505 // an inflated menu can be used again. This is not available
1506 // yet, so inflate each time (yuk!)
1507 MenuInflater inflater = mActivity.getMenuInflater();
1508 inflater.inflate(R.menu.browsercontext, menu);
1509
1510 // Show the correct menu group
1511 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001512 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001513 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 menu.setGroupVisible(R.id.PHONE_MENU,
1515 type == WebView.HitTestResult.PHONE_TYPE);
1516 menu.setGroupVisible(R.id.EMAIL_MENU,
1517 type == WebView.HitTestResult.EMAIL_TYPE);
1518 menu.setGroupVisible(R.id.GEO_MENU,
1519 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001520 String itemUrl = null;
1521 String url = webview.getOriginalUrl();
1522 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1523 itemUrl = Uri.decode(navigationUrl);
1524 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1525 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1526 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1527 } else {
1528 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1529 }
1530 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1531 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1532 } else {
1533 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1534
1535 menu.setGroupVisible(R.id.IMAGE_MENU,
1536 type == WebView.HitTestResult.IMAGE_TYPE
1537 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1538 menu.setGroupVisible(R.id.ANCHOR_MENU,
1539 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1540 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001541 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1542 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001543 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001544 // Setup custom handling depending on the type
1545 switch (type) {
1546 case WebView.HitTestResult.PHONE_TYPE:
1547 menu.setHeaderTitle(Uri.decode(extra));
1548 menu.findItem(R.id.dial_context_menu_id).setIntent(
1549 new Intent(Intent.ACTION_VIEW, Uri
1550 .parse(WebView.SCHEME_TEL + extra)));
1551 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1552 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1553 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1554 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1555 addIntent);
1556 menu.findItem(R.id.copy_phone_context_menu_id)
1557 .setOnMenuItemClickListener(
1558 new Copy(extra));
1559 break;
1560
1561 case WebView.HitTestResult.EMAIL_TYPE:
1562 menu.setHeaderTitle(extra);
1563 menu.findItem(R.id.email_context_menu_id).setIntent(
1564 new Intent(Intent.ACTION_VIEW, Uri
1565 .parse(WebView.SCHEME_MAILTO + extra)));
1566 menu.findItem(R.id.copy_mail_context_menu_id)
1567 .setOnMenuItemClickListener(
1568 new Copy(extra));
1569 break;
1570
1571 case WebView.HitTestResult.GEO_TYPE:
1572 menu.setHeaderTitle(extra);
1573 menu.findItem(R.id.map_context_menu_id).setIntent(
1574 new Intent(Intent.ACTION_VIEW, Uri
1575 .parse(WebView.SCHEME_GEO
1576 + URLEncoder.encode(extra))));
1577 menu.findItem(R.id.copy_geo_context_menu_id)
1578 .setOnMenuItemClickListener(
1579 new Copy(extra));
1580 break;
1581
1582 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1583 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001584 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001585 // decide whether to show the open link in new tab option
1586 boolean showNewTab = mTabControl.canCreateNewTab();
1587 MenuItem newTabItem
1588 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001589 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001590 ? R.string.contextmenu_openlink_newwindow_background
1591 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001592 newTabItem.setVisible(showNewTab);
1593 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001594 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1595 newTabItem.setOnMenuItemClickListener(
1596 new MenuItem.OnMenuItemClickListener() {
1597 @Override
1598 public boolean onMenuItemClick(MenuItem item) {
1599 final HashMap<String, WebView> hrefMap =
1600 new HashMap<String, WebView>();
1601 hrefMap.put("webview", webview);
1602 final Message msg = mHandler.obtainMessage(
1603 FOCUS_NODE_HREF,
1604 R.id.open_newtab_context_menu_id,
1605 0, hrefMap);
1606 webview.requestFocusNodeHref(msg);
1607 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001609 });
1610 } else {
1611 newTabItem.setOnMenuItemClickListener(
1612 new MenuItem.OnMenuItemClickListener() {
1613 @Override
1614 public boolean onMenuItemClick(MenuItem item) {
1615 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001616 openTab(extra, parent,
1617 !mSettings.openInBackground(),
1618 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001619 return true;
1620 }
1621 });
1622 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001624 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1625 menu.setHeaderTitle(navigationUrl);
1626 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1627
1628 if (itemUrl != null) {
1629 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1630 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1631 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1632 @Override
1633 public boolean onMenuItemClick(MenuItem item) {
1634 final Intent intent = new Intent(Controller.this
1635 .getContext(),
1636 AddMyNavigationPage.class);
1637 Bundle bundle = new Bundle();
1638 String url = Uri.decode(navigationUrl);
1639 bundle.putBoolean("isAdding", false);
1640 bundle.putString("url", url);
1641 bundle.putString("name", getNameFromUrl(url));
1642 intent.putExtra("websites", bundle);
1643 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1644 return false;
1645 }
1646 });
1647 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1648 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1649 @Override
1650 public boolean onMenuItemClick(MenuItem item) {
1651 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1652 return false;
1653 }
1654 });
1655 }
1656 } else {
1657 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1658 }
1659 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001660 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1661 break;
1662 }
1663 // otherwise fall through to handle image part
1664 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001665 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1666 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001667 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1668 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001669 shareItem.setOnMenuItemClickListener(
1670 new MenuItem.OnMenuItemClickListener() {
1671 @Override
1672 public boolean onMenuItemClick(MenuItem item) {
1673 sharePage(mActivity, null, extra, null,
1674 null);
1675 return true;
1676 }
1677 }
1678 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001679 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001680 menu.findItem(R.id.view_image_context_menu_id)
1681 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1682 @Override
1683 public boolean onMenuItemClick(MenuItem item) {
1684 openTab(extra, mTabControl.getCurrentTab(), true, true);
1685 return false;
1686 }
1687 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001688 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1689 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1690 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001691 menu.findItem(R.id.set_wallpaper_context_menu_id).
1692 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1693 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001694 break;
1695
1696 default:
1697 Log.w(LOGTAG, "We should not get here.");
1698 break;
1699 }
1700 //update the ui
1701 mUi.onContextMenuCreated(menu);
1702 }
1703
kaiyiz6e5b3e02013-08-19 20:02:01 +08001704 public void startAddMyNavigation(String url) {
1705 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1706 Bundle bundle = new Bundle();
1707 bundle.putBoolean("isAdding", true);
1708 bundle.putString("url", url);
1709 bundle.putString("name", getNameFromUrl(url));
1710 intent.putExtra("websites", bundle);
1711 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1712 }
1713
1714 private void showMyNavigationDeleteDialog(final String itemUrl) {
1715 new AlertDialog.Builder(this.getContext())
1716 .setTitle(R.string.my_navigation_delete_label)
1717 .setIcon(android.R.drawable.ic_dialog_alert)
1718 .setMessage(R.string.my_navigation_delete_msg)
1719 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1720 @Override
1721 public void onClick(DialogInterface dialog, int whichButton) {
1722 deleteMyNavigationItem(itemUrl);
1723 }
1724 })
1725 .setNegativeButton(R.string.cancel, null)
1726 .show();
1727 }
1728
1729 private void deleteMyNavigationItem(final String itemUrl) {
1730 ContentResolver cr = this.getContext().getContentResolver();
1731 Cursor cursor = null;
1732
1733 try {
1734 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1735 new String[] {
1736 MyNavigationUtil.ID
1737 }, "url = ?", new String[] {
1738 itemUrl
1739 }, null);
1740 if (null != cursor && cursor.moveToFirst()) {
1741 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1742 cursor.getLong(0));
1743
1744 ContentValues values = new ContentValues();
1745 values.put(MyNavigationUtil.TITLE, "");
1746 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1747 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1748 ByteArrayOutputStream os = new ByteArrayOutputStream();
1749 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1750 R.raw.my_navigation_add);
1751 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1752 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1753 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1754 cr.update(uri, values, null, null);
1755 } else {
1756 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1757 }
1758 } catch (IllegalStateException e) {
1759 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1760 } finally {
1761 if (null != cursor) {
1762 cursor.close();
1763 }
1764 }
1765
1766 if (getCurrentTopWebView() != null) {
1767 getCurrentTopWebView().reload();
1768 }
1769 }
1770
1771 private String getNameFromUrl(String itemUrl) {
1772 ContentResolver cr = this.getContext().getContentResolver();
1773 Cursor cursor = null;
1774 String name = null;
1775
1776 try {
1777 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1778 new String[] {
1779 MyNavigationUtil.TITLE
1780 }, "url = ?", new String[] {
1781 itemUrl
1782 }, null);
1783 if (null != cursor && cursor.moveToFirst()) {
1784 name = cursor.getString(0);
1785 } else {
1786 Log.e(LOGTAG, "this item does not exist!");
1787 }
1788 } catch (IllegalStateException e) {
1789 Log.e(LOGTAG, "getNameFromUrl", e);
1790 } finally {
1791 if (null != cursor) {
1792 cursor.close();
1793 }
1794 }
1795 return name;
1796 }
1797
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001798 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001799 final ContentResolver cr = mActivity.getContentResolver();
1800 new AsyncTask<Void, Void, Void>() {
1801 @Override
1802 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001803 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1804 if(!isMyNavigationUrl)
1805 return null;
1806
kaiyiz6e5b3e02013-08-19 20:02:01 +08001807 ContentResolver cr = mActivity.getContentResolver();
1808 Cursor cursor = null;
1809 try {
1810 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1811 new String[] {
1812 MyNavigationUtil.ID
1813 }, "url = ?", new String[] {
1814 itemUrl
1815 }, null);
1816 if (null != cursor && cursor.moveToFirst()) {
1817 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001818 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001819
1820 ContentValues values = new ContentValues();
1821 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1822 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1823 cursor.getLong(0));
1824 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1825 cr.update(uri, values, null, null);
1826 os.close();
1827 }
1828 } catch (IllegalStateException e) {
1829 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1830 } catch (IOException e) {
1831 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1832 } finally {
1833 if (null != cursor) {
1834 cursor.close();
1835 }
1836 }
1837 return null;
1838 }
1839 }.execute();
1840 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001841 /**
1842 * As the menu can be open when loading state changes
1843 * we must manually update the state of the stop/reload menu
1844 * item
1845 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001846 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001847 if (menu == null) {
1848 return;
1849 }
1850 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001851 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001852 menu.findItem(R.id.stop_menu_id):
1853 menu.findItem(R.id.reload_menu_id);
1854 if (src != null) {
1855 dest.setIcon(src.getIcon());
1856 dest.setTitle(src.getTitle());
1857 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001858 mActivity.invalidateOptionsMenu();
1859 }
1860
1861 public void invalidateOptionsMenu() {
1862 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001863 }
1864
John Reck9c35b9c2012-05-30 10:08:50 -07001865 @Override
1866 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001867 // Software menu key (toolbar key)
1868 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1869 return true;
1870 }
1871
1872 @Override
1873 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001874 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001875 // hold on to the menu reference here; it is used by the page callbacks
1876 // to update the menu based on loading state
1877 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001878 // Note: setVisible will decide whether an item is visible; while
1879 // setEnabled() will decide whether an item is enabled, which also means
1880 // whether the matching shortcut key will function.
1881 switch (mMenuState) {
1882 case EMPTY_MENU:
1883 if (mCurrentMenuState != mMenuState) {
1884 menu.setGroupVisible(R.id.MAIN_MENU, false);
1885 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1886 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1887 }
1888 break;
1889 default:
1890 if (mCurrentMenuState != mMenuState) {
1891 menu.setGroupVisible(R.id.MAIN_MENU, true);
1892 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1893 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1894 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001895 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001896 break;
1897 }
1898 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001899 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001900
1901 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001902 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1903 EditBookmarksRestriction.getInstance()
1904 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001905 }
1906
1907 private void setMenuItemVisibility(Menu menu, int id,
1908 boolean visibility) {
1909 MenuItem item = menu.findItem(id);
1910 if (item != null) {
1911 item.setVisible(visibility);
1912 }
1913 }
1914
1915 private int lookupBookmark(String title, String url) {
1916 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001917 int count = 0;
1918 Cursor cursor = null;
1919 try {
1920 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1921 BookmarksLoader.PROJECTION,
1922 "title = ? OR url = ?",
1923 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001924 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001925 },
1926 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001927
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001928 if (cursor != null)
1929 count = cursor.getCount();
1930
1931 } catch (IllegalStateException e) {
1932 Log.e(LOGTAG, "lookupBookmark ", e);
1933 } finally {
1934 if (null != cursor) {
1935 cursor.close();
1936 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001937 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001938 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001939 }
1940
1941 private void resetMenuItems(Menu menu) {
1942 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1943 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1944
1945 WebView w = getCurrentTopWebView();
1946 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1947
1948 String title = w.getTitle();
1949 String url = w.getUrl();
1950 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1951 bookmark_icon.setChecked(true);
1952 } else {
1953 bookmark_icon.setChecked(false);
1954 }
Ze G Riande2a675c22015-06-03 11:15:24 -07001955
1956 // update reader mode checkbox
1957 MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
1958 readerSwitcher.setVisible(false);
1959 readerSwitcher.setChecked(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001960 }
1961
Michael Kolb4bf79712011-07-14 14:18:12 -07001962 @Override
1963 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001964 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001965 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001966 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001967 // Following flag is used to identify schemes for which the LIVE_MENU
1968 // items defined in res/menu/browser.xml should be enabled
1969 boolean isLiveScheme = false;
1970 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001971 boolean isSavable = false;
Ze G Riande2a675c22015-06-03 11:15:24 -07001972
1973 boolean isDistillable = false;
1974 boolean isDistilled = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08001975 resetMenuItems(menu);
1976
Michael Kolb4bf79712011-07-14 14:18:12 -07001977 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001978 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001979 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001980 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001981 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08001982 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001983 isSavable = tab.getWebView().isSavable();
Ze G Riande2a675c22015-06-03 11:15:24 -07001984
1985 isDistillable = tab.isDistillable();
1986 isDistilled = tab.isDistilled();
Michael Kolb4bf79712011-07-14 14:18:12 -07001987 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001988
1989 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1990 forward.setEnabled(canGoForward);
1991
Michael Kolb4bf79712011-07-14 14:18:12 -07001992 // decide whether to show the share link option
1993 PackageManager pm = mActivity.getPackageManager();
1994 Intent send = new Intent(Intent.ACTION_SEND);
1995 send.setType("text/plain");
1996 ResolveInfo ri = pm.resolveActivity(send,
1997 PackageManager.MATCH_DEFAULT_ONLY);
1998 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1999
2000 boolean isNavDump = mSettings.enableNavDump();
2001 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2002 nav.setVisible(isNavDump);
2003 nav.setEnabled(isNavDump);
2004
2005 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002006 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2007 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002008 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002009 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2010 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002011 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002012 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2013 isLive && isLiveScheme && isPageFinished);
2014 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Ze G Riande2a675c22015-06-03 11:15:24 -07002015 isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002016 // history and snapshots item are the members of COMBO menu group,
2017 // so if show history item, only make snapshots item invisible.
2018 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002019
Ze G Riande2a675c22015-06-03 11:15:24 -07002020
2021 // update reader mode checkbox
2022 final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
2023 // The reader mode checkbox is hidden only
2024 // when the current page is neither distillable nor distilled
2025 readerSwitcher.setVisible(isDistillable || isDistilled);
2026 readerSwitcher.setChecked(isDistilled);
2027
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002028 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002029 }
2030
John Reck9c35b9c2012-05-30 10:08:50 -07002031 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002032 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002033 if (null == getCurrentTopWebView()) {
2034 return false;
2035 }
2036 if (mMenuIsDown) {
2037 // The shortcut action consumes the MENU. Even if it is still down,
2038 // it won't trigger the next shortcut action. In the case of the
2039 // shortcut action triggering a new activity, like Bookmarks, we
2040 // won't get onKeyUp for MENU. So it is important to reset it here.
2041 mMenuIsDown = false;
2042 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002043 if (mUi.onOptionsItemSelected(item)) {
2044 // ui callback handled it
2045 return true;
2046 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002047 switch (item.getItemId()) {
2048 // -- Main menu
2049 case R.id.new_tab_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002050 getCurrentTab().capture();
Michael Kolb8233fac2010-10-26 16:08:53 -07002051 openTabToHomePage();
2052 break;
2053
2054 case R.id.incognito_menu_id:
Pankaj Garg21dad562015-07-02 17:17:24 -07002055 getCurrentTab().capture();
Michael Kolb519d2282011-05-09 17:03:19 -07002056 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002057 break;
2058
2059 case R.id.goto_menu_id:
2060 editUrl();
2061 break;
2062
2063 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002064 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2065 break;
2066
2067 case R.id.history_menu_id:
2068 bookmarksOrHistoryPicker(ComboViews.History);
2069 break;
2070
2071 case R.id.snapshots_menu_id:
2072 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002073 break;
2074
Pankaj Garg49b79252014-11-07 17:33:41 -08002075 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002076 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002077 break;
2078
2079 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002080 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 stopLoading();
2082 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002083 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 getCurrentTopWebView().reload();
2085 }
2086 break;
2087
Michael Kolb8233fac2010-10-26 16:08:53 -07002088 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002089 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002090 break;
2091
2092 case R.id.close_menu_id:
2093 // Close the subwindow if it exists.
2094 if (mTabControl.getCurrentSubWindow() != null) {
2095 dismissSubWindow(mTabControl.getCurrentTab());
2096 break;
2097 }
2098 closeCurrentTab();
2099 break;
2100
kaiyiza8b6dbb2013-07-29 18:11:22 +08002101 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002102 Object[] params = { new String("persist.debug.browsermonkeytest")};
2103 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002104 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002105 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002106 if (ret != null && ret.equals("enable"))
2107 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002108 if (BrowserConfig.getInstance(getContext())
2109 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2110 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002111 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002112 case R.id.homepage_menu_id:
2113 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002114 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002115 break;
2116
2117 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002118 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002119 break;
2120
2121 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002122 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002123 break;
2124
John Reck2bc80422011-06-30 15:11:49 -07002125 case R.id.save_snapshot_menu_id:
2126 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002127 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002128 createScreenshotAsync(
2129 source.getWebView(),
2130 getDesiredThumbnailWidth(mActivity),
2131 getDesiredThumbnailHeight(mActivity),
2132 new ValueCallback<Bitmap>() {
2133 @Override
2134 public void onReceiveValue(Bitmap bitmap) {
2135 new SaveSnapshotTask(source, bitmap).execute();
2136 }
2137 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002138 break;
2139
Michael Kolb8233fac2010-10-26 16:08:53 -07002140 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002141 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002142 break;
2143
John Recke1a03a32011-09-14 17:04:16 -07002144 case R.id.snapshot_go_live:
2145 goLive();
2146 return true;
2147
Michael Kolb8233fac2010-10-26 16:08:53 -07002148 case R.id.share_page_menu_id:
2149 Tab currentTab = mTabControl.getCurrentTab();
2150 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002151 return false;
2152 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002153 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 break;
2155
2156 case R.id.dump_nav_menu_id:
2157 getCurrentTopWebView().debugDump();
2158 break;
2159
Michael Kolb8233fac2010-10-26 16:08:53 -07002160 case R.id.zoom_in_menu_id:
2161 getCurrentTopWebView().zoomIn();
2162 break;
2163
2164 case R.id.zoom_out_menu_id:
2165 getCurrentTopWebView().zoomOut();
2166 break;
2167
2168 case R.id.view_downloads_menu_id:
2169 viewDownloads();
2170 break;
2171
John Reck42229bc2011-08-19 13:26:43 -07002172 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002173 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002174 break;
2175
Ze G Riande2a675c22015-06-03 11:15:24 -07002176 case R.id.reader_mode_menu_id:
2177 toggleReaderMode();
2178 break;
2179
Michael Kolb8233fac2010-10-26 16:08:53 -07002180 case R.id.window_one_menu_id:
2181 case R.id.window_two_menu_id:
2182 case R.id.window_three_menu_id:
2183 case R.id.window_four_menu_id:
2184 case R.id.window_five_menu_id:
2185 case R.id.window_six_menu_id:
2186 case R.id.window_seven_menu_id:
2187 case R.id.window_eight_menu_id:
2188 {
2189 int menuid = item.getItemId();
2190 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2191 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2192 Tab desiredTab = mTabControl.getTab(id);
2193 if (desiredTab != null &&
2194 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002195 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002196 }
2197 break;
2198 }
2199 }
2200 }
2201 break;
2202
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002203 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002204 Bundle bundle = new Bundle();
2205 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2206 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2207 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2208 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2209 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002210 break;
2211
Pankaj Garg49b79252014-11-07 17:33:41 -08002212 case R.id.add_to_homescreen:
2213 final WebView w = getCurrentTopWebView();
2214 final EditText input = new EditText(getContext());
2215 input.setText(w.getTitle());
2216 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002217 .setTitle(getContext().getResources().getString(
2218 R.string.add_to_homescreen))
2219 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002220 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002221 .setPositiveButton(getContext().getResources().getString(
2222 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002223 public void onClick(DialogInterface dialog, int whichButton) {
2224 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2225 getContext(),
2226 w.getUrl(),
2227 input.getText().toString(),
2228 w.getViewportBitmap(),
2229 w.getFavicon()));
2230
2231 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2232 .addCategory(Intent.CATEGORY_HOME));
2233 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002234 .setNegativeButton(getContext().getResources().getString(
2235 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002236 public void onClick(DialogInterface dialog, int whichButton) {
2237 // Do nothing.
2238 }
2239 })
2240 .show();
2241 break;
2242
Michael Kolb8233fac2010-10-26 16:08:53 -07002243 default:
2244 return false;
2245 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002246 return true;
2247 }
2248
John Reck68234a92012-04-19 15:27:12 -07002249 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2250 implements OnCancelListener {
2251
2252 private Tab mTab;
2253 private Dialog mProgressDialog;
2254 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002255 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002256
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002257 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002258 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002259 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002260 }
2261
2262 @Override
2263 protected void onPreExecute() {
2264 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2265 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2266 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002267 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002268 }
2269
2270 @Override
2271 protected Long doInBackground(Void... params) {
2272 if (!mTab.saveViewState(mValues)) {
2273 return null;
2274 }
2275 if (isCancelled()) {
2276 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2277 File file = mActivity.getFileStreamPath(path);
2278 if (!file.delete()) {
2279 file.deleteOnExit();
2280 }
2281 return null;
2282 }
2283 final ContentResolver cr = mActivity.getContentResolver();
2284 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2285 if (result == null) {
2286 return null;
2287 }
2288 long id = ContentUris.parseId(result);
2289 return id;
2290 }
2291
2292 @Override
2293 protected void onPostExecute(Long id) {
2294 if (isCancelled()) {
2295 return;
2296 }
2297 mProgressDialog.dismiss();
2298 if (id == null) {
2299 Toast.makeText(mActivity, R.string.snapshot_failed,
2300 Toast.LENGTH_SHORT).show();
2301 return;
2302 }
2303 Bundle b = new Bundle();
2304 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2305 mUi.showComboView(ComboViews.Snapshots, b);
2306 }
2307
2308 @Override
2309 public void onCancel(DialogInterface dialog) {
2310 cancel(true);
2311 }
2312 }
2313
Michael Kolb80f75082012-04-10 10:50:06 -07002314 @Override
2315 public void toggleUserAgent() {
2316 WebView web = getCurrentWebView();
2317 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002318 }
2319
Ze G Riande2a675c22015-06-03 11:15:24 -07002320 // This function calls the method in the webview to enable/disable
2321 // the reader mode through the DOM distiller
2322 public void toggleReaderMode() {
2323 Tab t = mTabControl.getCurrentTab();
2324 if (t.isDistilled()) {
2325 closeTab(t);
2326 } else if (t.isDistillable()) {
2327 openTab(t.getDistilledUrl(), false, true, false, t);
2328 }
2329 }
2330
Michael Kolb80f75082012-04-10 10:50:06 -07002331 @Override
2332 public void findOnPage() {
2333 getCurrentTopWebView().showFindDialog(null, true);
2334 }
2335
2336 @Override
2337 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002338 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002339 }
2340
2341 @Override
2342 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002343 if(EditBookmarksRestriction.getInstance().isEnabled()) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002344 Toast.makeText(getContext(), R.string.mdm_managed_alert,
2345 Toast.LENGTH_SHORT).show();
Dave Tharp9a082b12015-06-19 08:46:57 -07002346 }
2347 else {
2348 WebView w = getCurrentTopWebView();
2349 if (w == null)
2350 return;
2351 final Intent i = createBookmarkCurrentPageIntent(false);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002352 mActivity.startActivity(i);
Dave Tharp9a082b12015-06-19 08:46:57 -07002353 }
Michael Kolb80f75082012-04-10 10:50:06 -07002354 }
2355
John Recke1a03a32011-09-14 17:04:16 -07002356 private void goLive() {
Axesh R. Ajmera89cf4d82015-05-19 11:26:18 -07002357 if (!getCurrentTab().isSnapshot()) return;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002358 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002359 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002360 boolean onlySingleTabRemaining = false;
2361 if (mTabControl.getTabCount() > 1) {
2362 // destroy the old snapshot tab
2363 closeCurrentTab();
2364 } else {
2365 onlySingleTabRemaining = true;
2366 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002367 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002368 if (onlySingleTabRemaining) {
2369 closeTab(t);
2370 }
2371
Tarun Nainani8eb00912014-07-17 12:28:32 -07002372 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002373 }
2374
luxiaolb40014b2013-07-19 10:01:43 +08002375 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002376 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002377 new AlertDialog.Builder(activity)
2378 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002379 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002380 .setMessage(R.string.exit_browser_msg)
2381 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2382 public void onClick(DialogInterface dialog, int which) {
2383 activity.moveTaskToBack(true);
2384 dialog.dismiss();
2385 }
2386 })
2387 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2388 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002389 mCrashRecoveryHandler.clearState(true);
2390 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002391 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002392 dialog.dismiss();
2393 }
2394 })
2395 .show();
2396 }
Michael Kolb315d5022011-10-13 12:47:11 -07002397 @Override
2398 public void showPageInfo() {
2399 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2400 }
2401
John Reck9c35b9c2012-05-30 10:08:50 -07002402 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002403 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002404 // Let the History and Bookmark fragments handle menus they created.
2405 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2406 return false;
2407 }
2408
Michael Kolb8233fac2010-10-26 16:08:53 -07002409 int id = item.getItemId();
2410 boolean result = true;
2411 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002412 // -- Browser context menu
2413 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002414 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002415 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002416 case R.id.copy_link_context_menu_id:
2417 final WebView webView = getCurrentTopWebView();
2418 if (null == webView) {
2419 result = false;
2420 break;
2421 }
2422 final HashMap<String, WebView> hrefMap =
2423 new HashMap<String, WebView>();
2424 hrefMap.put("webview", webView);
2425 final Message msg = mHandler.obtainMessage(
2426 FOCUS_NODE_HREF, id, 0, hrefMap);
2427 webView.requestFocusNodeHref(msg);
2428 break;
2429
2430 default:
2431 // For other context menus
2432 result = onOptionsItemSelected(item);
2433 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002434 return result;
2435 }
2436
2437 /**
2438 * support programmatically opening the context menu
2439 */
2440 public void openContextMenu(View view) {
2441 mActivity.openContextMenu(view);
2442 }
2443
2444 /**
2445 * programmatically open the options menu
2446 */
2447 public void openOptionsMenu() {
2448 mActivity.openOptionsMenu();
2449 }
2450
John Reck9c35b9c2012-05-30 10:08:50 -07002451 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002452 public boolean onMenuOpened(int featureId, Menu menu) {
2453 if (mOptionsMenuOpen) {
2454 if (mConfigChanged) {
2455 // We do not need to make any changes to the state of the
2456 // title bar, since the only thing that happened was a
2457 // change in orientation
2458 mConfigChanged = false;
2459 } else {
2460 if (!mExtendedMenuOpen) {
2461 mExtendedMenuOpen = true;
2462 mUi.onExtendedMenuOpened();
2463 } else {
2464 // Switching the menu back to icon view, so show the
2465 // title bar once again.
2466 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002467 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002468 }
2469 }
2470 } else {
2471 // The options menu is closed, so open it, and show the title
2472 mOptionsMenuOpen = true;
2473 mConfigChanged = false;
2474 mExtendedMenuOpen = false;
2475 mUi.onOptionsMenuOpened();
2476 }
2477 return true;
2478 }
2479
John Reck9c35b9c2012-05-30 10:08:50 -07002480 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002481 public void onOptionsMenuClosed(Menu menu) {
2482 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002483 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002484 }
2485
John Reck9c35b9c2012-05-30 10:08:50 -07002486 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002487 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002488 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002489 }
2490
2491 // Helper method for getting the top window.
2492 @Override
2493 public WebView getCurrentTopWebView() {
2494 return mTabControl.getCurrentTopWebView();
2495 }
2496
2497 @Override
2498 public WebView getCurrentWebView() {
2499 return mTabControl.getCurrentWebView();
2500 }
2501
2502 /*
2503 * This method is called as a result of the user selecting the options
2504 * menu to see the download window. It shows the download window on top of
2505 * the current window.
2506 */
2507 void viewDownloads() {
2508 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2509 mActivity.startActivity(intent);
2510 }
2511
John Reck30b065e2011-07-19 10:58:05 -07002512 int getActionModeHeight() {
2513 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2514 new int[] { android.R.attr.actionBarSize });
2515 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2516 actionBarSizeTypedArray.recycle();
2517 return size;
2518 }
2519
Michael Kolb8233fac2010-10-26 16:08:53 -07002520 // action mode
2521
John Reck9c35b9c2012-05-30 10:08:50 -07002522 @Override
2523 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002524 mUi.onActionModeStarted(mode);
2525 mActionMode = mode;
2526 }
2527
2528 /*
2529 * True if a custom ActionMode (i.e. find or select) is in use.
2530 */
2531 @Override
2532 public boolean isInCustomActionMode() {
2533 return mActionMode != null;
2534 }
2535
2536 /*
2537 * End the current ActionMode.
2538 */
2539 @Override
2540 public void endActionMode() {
2541 if (mActionMode != null) {
2542 mActionMode.finish();
2543 }
2544 }
2545
2546 /*
2547 * Called by find and select when they are finished. Replace title bars
2548 * as necessary.
2549 */
John Reck9c35b9c2012-05-30 10:08:50 -07002550 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002551 public void onActionModeFinished(ActionMode mode) {
2552 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002553 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002554 mActionMode = null;
2555 }
2556
2557 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002558 final Tab tab = getCurrentTab();
2559 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002560 }
2561
2562 // bookmark handling
2563
2564 /**
2565 * add the current page as a bookmark to the given folder id
2566 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002567 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002568 * bookmarked, and if it is, edit that bookmark. If false, and
2569 * the site is already bookmarked, do not attempt to edit the
2570 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002571 */
2572 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002573 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002574 WebView w = getCurrentTopWebView();
2575 if (w == null) {
2576 return null;
2577 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 Intent i = new Intent(mActivity,
2579 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2581 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Pankaj Garg21dad562015-07-02 17:17:24 -07002582 String touchIconUrl = getCurrentTab().getTouchIconUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07002583 if (touchIconUrl != null) {
2584 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2585 WebSettings settings = w.getSettings();
2586 if (settings != null) {
2587 i.putExtra(AddBookmarkPage.USER_AGENT,
2588 settings.getUserAgentString());
2589 }
2590 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002591 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002592 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002593 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002594 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2595 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002596 // Put the dialog at the upper right of the screen, covering the
2597 // star on the title bar.
2598 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002599 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002600 }
2601
2602 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002603 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002604 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002606 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002607 }
2608
Vivek Sekharb54614f2014-05-01 19:03:37 -07002609 @Override
2610 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2611 boolean capture) {
2612 mUploadHandler = new UploadHandler(this);
2613 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2614 }
2615
Michael Kolb8233fac2010-10-26 16:08:53 -07002616 // thumbnails
2617
2618 /**
2619 * Return the desired width for thumbnail screenshots, which are stored in
2620 * the database, and used on the bookmarks screen.
2621 * @param context Context for finding out the density of the screen.
2622 * @return desired width for thumbnail screenshot.
2623 */
2624 static int getDesiredThumbnailWidth(Context context) {
2625 return context.getResources().getDimensionPixelOffset(
2626 R.dimen.bookmarkThumbnailWidth);
2627 }
2628
2629 /**
2630 * Return the desired height for thumbnail screenshots, which are stored in
2631 * the database, and used on the bookmarks screen.
2632 * @param context Context for finding out the density of the screen.
2633 * @return desired height for thumbnail screenshot.
2634 */
2635 static int getDesiredThumbnailHeight(Context context) {
2636 return context.getResources().getDimensionPixelOffset(
2637 R.dimen.bookmarkThumbnailHeight);
2638 }
2639
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002640 static void createScreenshotAsync(WebView view, int width, int height,
2641 final ValueCallback<Bitmap> cb) {
2642 if (view == null || width == 0 || height == 0) {
2643 return;
2644 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002645 view.getContentBitmapAsync(
2646 (float) width / view.getWidth(),
2647 new Rect(),
2648 new ValueCallback<Bitmap>() {
2649 @Override
2650 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002651 if (bitmap != null)
2652 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2653 cb.onReceiveValue(bitmap);
2654 }});
2655 }
2656
Michael Kolb8233fac2010-10-26 16:08:53 -07002657 private class Copy implements OnMenuItemClickListener {
2658 private CharSequence mText;
2659
John Reck9c35b9c2012-05-30 10:08:50 -07002660 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002661 public boolean onMenuItemClick(MenuItem item) {
2662 copy(mText);
2663 return true;
2664 }
2665
2666 public Copy(CharSequence toCopy) {
2667 mText = toCopy;
2668 }
2669 }
2670
Leon Scroggins63c02662010-11-18 15:16:27 -05002671 private static class Download implements OnMenuItemClickListener {
2672 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002673 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002674 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002675 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002676 private static final String FALLBACK_EXTENSION = "dat";
2677 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002678
John Reck9c35b9c2012-05-30 10:08:50 -07002679 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002680 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002681 if (DataUri.isDataUri(mText)) {
2682 saveDataUri();
2683 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002684 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002685 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002686 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002687 return true;
2688 }
2689
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002690 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2691 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002692 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002693 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002694 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002695 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002696 }
George Mount387d45d2011-10-07 15:57:53 -07002697
2698 /**
2699 * Treats mText as a data URI and writes its contents to a file
2700 * based on the current time.
2701 */
2702 private void saveDataUri() {
2703 FileOutputStream outputStream = null;
2704 try {
2705 DataUri uri = new DataUri(mText);
2706 File target = getTarget(uri);
2707 outputStream = new FileOutputStream(target);
2708 outputStream.write(uri.getData());
2709 final DownloadManager manager =
2710 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2711 manager.addCompletedDownload(target.getName(),
2712 mActivity.getTitle().toString(), false,
2713 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002714 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002715 } catch (IOException e) {
2716 Log.e(LOGTAG, "Could not save data URL");
2717 } finally {
2718 if (outputStream != null) {
2719 try {
2720 outputStream.close();
2721 } catch (IOException e) {
2722 // ignore close errors
2723 }
2724 }
2725 }
2726 }
2727
2728 /**
2729 * Creates a File based on the current time stamp and uses
2730 * the mime type of the DataUri to get the extension.
2731 */
2732 private File getTarget(DataUri uri) throws IOException {
2733 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002734 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002735 String nameBase = format.format(new Date());
2736 String mimeType = uri.getMimeType();
2737 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2738 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2739 if (extension == null) {
2740 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2741 extension = FALLBACK_EXTENSION;
2742 }
2743 extension = "." + extension; // createTempFile needs the '.'
2744 File targetFile = File.createTempFile(nameBase, extension, dir);
2745 return targetFile;
2746 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002747 }
2748
Cary Clark8974d282010-11-22 10:46:05 -05002749 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002750 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002751
John Reck9c35b9c2012-05-30 10:08:50 -07002752 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002753 public boolean onMenuItemClick(MenuItem item) {
2754 if (mWebView != null) {
2755 return mWebView.selectText();
2756 }
2757 return false;
2758 }
2759
2760 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002761 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002762 }
2763
2764 }
2765
Michael Kolb8233fac2010-10-26 16:08:53 -07002766 /********************** TODO: UI stuff *****************************/
2767
2768 // these methods have been copied, they still need to be cleaned up
2769
2770 /****************** tabs ***************************************************/
2771
2772 // basic tab interactions:
2773
2774 // it is assumed that tabcontrol already knows about the tab
2775 protected void addTab(Tab tab) {
2776 mUi.addTab(tab);
2777 }
2778
2779 protected void removeTab(Tab tab) {
2780 mUi.removeTab(tab);
2781 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002782 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002783 }
2784
Michael Kolbf2055602011-04-09 17:20:03 -07002785 @Override
2786 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002787 // monkey protection against delayed start
2788 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002789
2790 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2791 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002792 mTabControl.setCurrentTab(tab);
2793 // the tab is guaranteed to have a webview after setCurrentTab
2794 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002795
2796
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002797 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002798 //Purge active tabs
2799 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002800 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002801 }
2802
John Reck8bcafc12011-08-29 16:43:02 -07002803 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002804 Tab current = mTabControl.getCurrentTab();
2805 if (current != null
2806 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002807 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002808 }
2809 }
2810
John Reck26b18322011-06-21 13:08:58 -07002811 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002812 //Cancel navscreen request
2813 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002814 // Dismiss the subwindow if applicable.
2815 dismissSubWindow(appTab);
2816 // Since we might kill the WebView, remove it from the
2817 // content view first.
2818 mUi.detachTab(appTab);
2819 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002820 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002821 // TODO: analyze why the remove and add are necessary
2822 mUi.attachTab(appTab);
2823 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002824 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002825 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002826 } else {
2827 // If the tab was the current tab, we have to attach
2828 // it to the view system again.
2829 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002830 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002831 }
2832 }
2833
2834 // Remove the sub window if it exists. Also called by TabControl when the
2835 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002836 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002837 public void dismissSubWindow(Tab tab) {
2838 removeSubWindow(tab);
2839 // dismiss the subwindow. This will destroy the WebView.
2840 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002841 WebView wv = getCurrentTopWebView();
2842 if (wv != null) {
2843 wv.requestFocus();
2844 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002845 }
2846
2847 @Override
2848 public void removeSubWindow(Tab t) {
2849 if (t.getSubWebView() != null) {
2850 mUi.removeSubWindow(t.getSubViewContainer());
2851 }
2852 }
2853
2854 @Override
2855 public void attachSubWindow(Tab tab) {
2856 if (tab.getSubWebView() != null) {
2857 mUi.attachSubWindow(tab.getSubViewContainer());
2858 getCurrentTopWebView().requestFocus();
2859 }
2860 }
2861
Mathew Inwood29721c22011-06-29 17:55:24 +01002862 private Tab showPreloadedTab(final UrlData urlData) {
2863 if (!urlData.isPreloaded()) {
2864 return null;
2865 }
2866 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2867 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2868 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002869 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002870 // Could not submit query. Fallback to regular tab creation
2871 tabControl.destroy();
2872 return null;
2873 }
2874 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002875 // check tab count and make room for new tab
2876 if (!mTabControl.canCreateNewTab()) {
2877 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2878 if (leastUsed != null) {
2879 closeTab(leastUsed);
2880 }
2881 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002882 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002883 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002884 mTabControl.addPreloadedTab(t);
2885 addTab(t);
2886 setActiveTab(t);
2887 return t;
2888 }
2889
Michael Kolb7bcafde2011-05-09 13:55:59 -07002890 // open a non inconito tab with the given url data
2891 // and set as active tab
2892 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002893 Tab tab = showPreloadedTab(urlData);
2894 if (tab == null) {
2895 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002896 if ((tab != null) && !urlData.isEmpty()) {
2897 loadUrlDataIn(tab, urlData);
2898 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002899 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002900 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002901 }
2902
Michael Kolb843510f2010-12-09 10:51:49 -08002903 @Override
2904 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002905 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002906 }
2907
Michael Kolb8233fac2010-10-26 16:08:53 -07002908 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002909 public Tab openIncognitoTab() {
2910 return openTab(INCOGNITO_URI, true, true, false);
2911 }
2912
2913 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002914 public Tab openTab(String url, boolean incognito, boolean setActive,
2915 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002916 return openTab(url, incognito, setActive, useCurrent, null);
2917 }
2918
2919 @Override
2920 public Tab openTab(String url, Tab parent, boolean setActive,
2921 boolean useCurrent) {
2922 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2923 setActive, useCurrent, parent);
2924 }
2925
2926 public Tab openTab(String url, boolean incognito, boolean setActive,
2927 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002928 Tab tab = createNewTab(incognito, setActive, useCurrent);
2929 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002930 if (parent instanceof SnapshotTab) {
2931 addTab(tab);
2932 if (setActive)
2933 setActiveTab(tab);
2934 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002935 parent.addChildTab(tab);
2936 }
Michael Kolb519d2282011-05-09 17:03:19 -07002937 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002938 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002939 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002940 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002941 return tab;
2942 }
2943
2944 // this method will attempt to create a new tab
2945 // incognito: private browsing tab
2946 // setActive: ste tab as current tab
2947 // useCurrent: if no new tab can be created, return current tab
2948 private Tab createNewTab(boolean incognito, boolean setActive,
2949 boolean useCurrent) {
2950 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07002951 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
2952 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002953 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002954 if (mTabControl.canCreateNewTab()) {
2955 tab = mTabControl.createNewTab(incognito, !setActive);
2956 addTab(tab);
2957 if (setActive) {
2958 setActiveTab(tab);
2959 } else {
2960 tab.pause();
2961 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002962 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07002963 if (useCurrent) {
2964 tab = mTabControl.getCurrentTab();
2965 reuseTab(tab, null);
2966 } else {
2967 mUi.showMaxTabsWarning();
2968 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002969 }
2970 }
2971 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002972 }
2973
John Reck2bc80422011-06-30 15:11:49 -07002974 @Override
2975 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2976 SnapshotTab tab = null;
2977 if (mTabControl.canCreateNewTab()) {
2978 tab = mTabControl.createSnapshotTab(snapshotId);
2979 addTab(tab);
2980 if (setActive) {
2981 setActiveTab(tab);
2982 }
2983 } else {
2984 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002985 }
2986 return tab;
2987 }
2988
Michael Kolb8233fac2010-10-26 16:08:53 -07002989 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002990 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002991 * @return boolean True if we successfully switched to a different tab. If
2992 * the indexth tab is null, or if that tab is the same as
2993 * the current one, return false.
2994 */
2995 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002996 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002997 Tab currentTab = mTabControl.getCurrentTab();
2998 if (tab == null || tab == currentTab) {
2999 return false;
3000 }
3001 setActiveTab(tab);
3002 return true;
3003 }
3004
3005 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003006 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003007 closeCurrentTab(false);
3008 }
3009
3010 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003011 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003012 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003013 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003014 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003015 return;
3016 }
Michael Kolbc831b632011-05-11 09:30:34 -07003017 final Tab current = mTabControl.getCurrentTab();
3018 final int pos = mTabControl.getCurrentPosition();
3019 Tab newTab = current.getParent();
3020 if (newTab == null) {
3021 newTab = mTabControl.getTab(pos + 1);
3022 if (newTab == null) {
3023 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003024 }
3025 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003026 if (andQuit) {
3027 mTabControl.setCurrentTab(newTab);
3028 closeTab(current);
3029 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003030 // Close window
3031 closeTab(current);
3032 }
3033 }
3034
3035 /**
3036 * Close the tab, remove its associated title bar, and adjust mTabControl's
3037 * current tab to a valid value.
3038 */
3039 @Override
3040 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003041 if (tab == mTabControl.getCurrentTab()) {
3042 closeCurrentTab();
3043 } else {
3044 removeTab(tab);
3045 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003046 }
3047
Afzal Najamd4e33312012-04-26 01:54:01 -04003048 /**
3049 * Close all tabs except the current one
3050 */
3051 @Override
3052 public void closeOtherTabs() {
3053 int inactiveTabs = mTabControl.getTabCount() - 1;
3054 for (int i = inactiveTabs; i >= 0; i--) {
3055 Tab tab = mTabControl.getTab(i);
3056 if (tab != mTabControl.getCurrentTab()) {
3057 removeTab(tab);
3058 }
3059 }
3060 }
3061
Michael Kolb8233fac2010-10-26 16:08:53 -07003062 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003063 protected void loadUrlFromContext(String url) {
3064 Tab tab = getCurrentTab();
3065 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003066 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003067 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003068 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003069 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003070 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003071 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003072 }
3073 }
3074 }
3075
3076 /**
3077 * Load the URL into the given WebView and update the title bar
3078 * to reflect the new load. Call this instead of WebView.loadUrl
3079 * directly.
3080 * @param view The WebView used to load url.
3081 * @param url The URL to load.
3082 */
John Reck71e51422011-07-01 16:49:28 -07003083 @Override
3084 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003085 loadUrl(tab, url, null);
3086 }
3087
3088 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3089 if (tab != null) {
3090 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003091 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003092 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003093 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003094 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003095 }
3096
3097 /**
3098 * Load UrlData into a Tab and update the title bar to reflect the new
3099 * load. Call this instead of UrlData.loadIn directly.
3100 * @param t The Tab used to load.
3101 * @param data The UrlData being loaded.
3102 */
3103 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003104 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003105 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003106 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003107 } else {
John Reck38b39652012-06-05 09:22:59 -07003108 if (t != null && data.mDisableUrlOverride) {
3109 t.disableUrlOverridingForLoad();
3110 }
John Reck26b18322011-06-21 13:08:58 -07003111 loadUrl(t, data.mUrl, data.mHeaders);
3112 }
3113 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003114 }
3115
John Reck30c714c2010-12-16 17:30:34 -08003116 @Override
3117 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003118 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003119 //In case of tab can go back (aka tab has navigation entry) do nothing
3120 //else just load homepage in current tab.
3121 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003122 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003123 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003124 }
3125
3126 void goBackOnePageOrQuit() {
3127 Tab current = mTabControl.getCurrentTab();
3128 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003129 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3130 showExitDialog(mActivity);
3131 } else {
3132 /*
3133 * Instead of finishing the activity, simply push this to the back
3134 * of the stack and let ActivityManager to choose the foreground
3135 * activity. As BrowserActivity is singleTask, it will be always the
3136 * root of the task. So we can use either true or false for
3137 * moveTaskToBack().
3138 */
3139 mActivity.moveTaskToBack(true);
3140 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003141 return;
3142 }
John Reckef654f12011-07-12 16:42:08 -07003143 if (current.canGoBack()) {
3144 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003145 } else {
3146 // Check to see if we are closing a window that was created by
3147 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003148 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003149 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003150 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003151 // Now we close the other tab
3152 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003153 } else if (BrowserConfig.getInstance(getContext())
3154 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3155 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003156 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 /*
3158 * Instead of finishing the activity, simply push this to the back
3159 * of the stack and let ActivityManager to choose the foreground
3160 * activity. As BrowserActivity is singleTask, it will be always the
3161 * root of the task. So we can use either true or false for
3162 * moveTaskToBack().
3163 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003164 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003165 }
3166 }
3167 }
3168
3169 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003170 * helper method for key handler
3171 * returns the current tab if it can't advance
3172 */
Michael Kolbc831b632011-05-11 09:30:34 -07003173 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003174 int pos = mTabControl.getCurrentPosition() + 1;
3175 if (pos >= mTabControl.getTabCount()) {
3176 pos = 0;
3177 }
3178 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003179 }
3180
3181 /**
3182 * helper method for key handler
3183 * returns the current tab if it can't advance
3184 */
Michael Kolbc831b632011-05-11 09:30:34 -07003185 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003186 int pos = mTabControl.getCurrentPosition() - 1;
3187 if ( pos < 0) {
3188 pos = mTabControl.getTabCount() - 1;
3189 }
3190 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003191 }
3192
John Reckbcef87f2012-02-03 14:58:34 -08003193 boolean isMenuOrCtrlKey(int keyCode) {
3194 return (KeyEvent.KEYCODE_MENU == keyCode)
3195 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3196 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3197 }
3198
Michael Kolb0035fad2011-03-14 13:25:28 -07003199 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003200 * handle key events in browser
3201 *
3202 * @param keyCode
3203 * @param event
3204 * @return true if handled, false to pass to super
3205 */
John Reck9c35b9c2012-05-30 10:08:50 -07003206 @Override
3207 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003208 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3209 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003210 if (!mUi.isComboViewShowing()) {
3211 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3212 true, false);
3213 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003214 return true;
3215 }
3216
Cary Clark160bbb92011-01-10 11:17:07 -05003217 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003218 // Even if MENU is already held down, we need to call to super to open
3219 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003220 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003221 mMenuIsDown = true;
3222 return false;
3223 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003224
Cary Clark8ff8c662010-12-29 15:03:05 -05003225 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003226 Tab tab = getCurrentTab();
3227 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003228
Cary Clark160bbb92011-01-10 11:17:07 -05003229 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3230 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003231
Michael Kolb8233fac2010-10-26 16:08:53 -07003232 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003233 case KeyEvent.KEYCODE_TAB:
3234 if (event.isCtrlPressed()) {
3235 if (event.isShiftPressed()) {
3236 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003237 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003238 } else {
3239 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003240 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003241 }
3242 return true;
3243 }
3244 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003245 case KeyEvent.KEYCODE_SPACE:
3246 // WebView/WebTextView handle the keys in the KeyDown. As
3247 // the Activity's shortcut keys are only handled when WebView
3248 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003249 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003250 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003251 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003252 pageDown();
3253 }
3254 return true;
3255 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003256 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003257 event.startTracking();
3258 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003259 case KeyEvent.KEYCODE_FORWARD:
3260 if (!noModifiers) break;
3261 tab.goForward();
3262 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003263 case KeyEvent.KEYCODE_DPAD_LEFT:
3264 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003265 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003266 return true;
3267 }
3268 break;
3269 case KeyEvent.KEYCODE_DPAD_RIGHT:
3270 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003271 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003272 return true;
3273 }
3274 break;
3275 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003276 if (ctrl) {
3277 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003278 return true;
3279 }
3280 break;
Michael Kolba4183062011-01-16 10:43:21 -08003281// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003282 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003283 if (ctrl ) {
3284 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003285 return true;
3286 }
3287 break;
Michael Kolba4183062011-01-16 10:43:21 -08003288// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003289// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003290// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003291// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003292// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003293// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003294// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003295// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003296// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003297// case KeyEvent.KEYCODE_M: // unused
3298// case KeyEvent.KEYCODE_N: // in Chrome: new window
3299// case KeyEvent.KEYCODE_O: // in Chrome: open file
3300// case KeyEvent.KEYCODE_P: // in Chrome: print page
3301// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003302// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003303// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3304 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003305 // we can't use the ctrl/shift flags, they check for
3306 // exclusive use of a modifier
3307 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003308 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003309 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003310 } else {
3311 openTabToHomePage();
3312 }
3313 return true;
3314 }
3315 break;
3316// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3317// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003318// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003319// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3320// case KeyEvent.KEYCODE_Y: // unused
3321// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003322 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003323 // it is a regular key and webview is not null
3324 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003325 }
3326
John Reck9c35b9c2012-05-30 10:08:50 -07003327 @Override
3328 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003329 switch(keyCode) {
3330 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003331 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003332 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003333 return true;
3334 }
3335 break;
3336 }
3337 return false;
3338 }
3339
John Reck9c35b9c2012-05-30 10:08:50 -07003340 @Override
3341 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003342 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003343 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003344 if (KeyEvent.KEYCODE_MENU == keyCode
3345 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003346 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003347 }
3348 }
Cary Clark160bbb92011-01-10 11:17:07 -05003349 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003350 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003351 case KeyEvent.KEYCODE_BACK:
3352 if (event.isTracking() && !event.isCanceled()) {
3353 onBackKey();
3354 return true;
3355 }
3356 break;
3357 }
3358 return false;
3359 }
3360
3361 public boolean isMenuDown() {
3362 return mMenuIsDown;
3363 }
3364
John Reck9c35b9c2012-05-30 10:08:50 -07003365 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003366 public void setupAutoFill(Message message) {
3367 // Open the settings activity at the AutoFill profile fragment so that
3368 // the user can create a new profile. When they return, we will dispatch
3369 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003370 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003371 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3372 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003373 }
John Reckb3417f02011-01-14 11:01:05 -08003374
John Reck9c35b9c2012-05-30 10:08:50 -07003375 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003376 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003377 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003378 return true;
3379 }
3380
John Reck1cf4b792011-07-26 10:22:22 -07003381 @Override
3382 public boolean shouldCaptureThumbnails() {
3383 return mUi.shouldCaptureThumbnails();
3384 }
3385
Michael Kolbc3af0672011-08-09 10:24:41 -07003386 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003387 public boolean supportsVoice() {
3388 PackageManager pm = mActivity.getPackageManager();
3389 List activities = pm.queryIntentActivities(new Intent(
3390 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3391 return activities.size() != 0;
3392 }
3393
3394 @Override
3395 public void startVoiceRecognizer() {
3396 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003397 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003398 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3399 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3400 mActivity.startActivityForResult(voice, VOICE_RESULT);
3401 }
3402
Pankaj Garg7b279f62014-08-12 14:47:18 -07003403 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003404 if (mLevel == level)
3405 return;
3406 mLevel = level;
3407 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003408 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3409 lp.dimAmount = level;
3410 mActivity.getWindow().setAttributes(lp);
3411 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3412 } else {
3413 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3414 }
3415 }
3416
Michael Kolb0b129122012-06-04 16:31:58 -07003417 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003418 public void setBlockEvents(boolean block) {
3419 mBlockEvents = block;
3420 }
3421
John Reck9c35b9c2012-05-30 10:08:50 -07003422 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003423 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003424 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003425 }
3426
John Reck9c35b9c2012-05-30 10:08:50 -07003427 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003428 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003429 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003430 }
3431
John Reck9c35b9c2012-05-30 10:08:50 -07003432 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003433 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003434 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003435 }
3436
John Reck9c35b9c2012-05-30 10:08:50 -07003437 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003438 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003439 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003440 }
3441
John Reck9c35b9c2012-05-30 10:08:50 -07003442 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003443 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003444 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003445 }
3446
Pankaj Garg49b79252014-11-07 17:33:41 -08003447 @Override
3448 public boolean shouldShowAppMenu() {
3449 return true;
3450 }
3451
3452 @Override
3453 public int getMenuThemeResourceId() {
3454 return R.style.OverflowMenuTheme;
3455 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003456}