blob: 99a40feff32ec3ae8c0d3391e3409db317b742b0 [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
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070097import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070098import com.android.browser.R;
99import com.android.browser.IntentHandler.UrlData;
100import com.android.browser.UI.ComboViews;
Dave Tharpdcad7b02015-05-28 07:38:32 -0700101import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -0700102import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700103import com.android.browser.mynavigation.AddMyNavigationPage;
104import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700105import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700106import com.android.browser.platformsupport.BrowserContract;
107import com.android.browser.platformsupport.WebAddress;
108import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800109import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700110import com.android.browser.provider.BrowserProvider2.Thumbnails;
111import com.android.browser.provider.SnapshotProvider.Snapshots;
112import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800113import com.android.browser.appmenu.AppMenuHandler;
114import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700115
Michael Kolb8233fac2010-10-26 16:08:53 -0700116import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700117import java.io.File;
118import java.io.FileOutputStream;
119import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700120import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700121import java.text.DateFormat;
122import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700123import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700124import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700125import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800126import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200127import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700128import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700129
130/**
131 * Controller for browser
132 */
133public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800134 implements WebViewController, UiController, ActivityController,
135 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700136
137 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800138 private static final String SEND_APP_ID_EXTRA =
139 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Vivek Sekhared791da2015-02-22 12:39:05 -0800140 private static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800141 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
142 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800143
Vivek Sekharb54614f2014-05-01 19:03:37 -0700144 // Remind switch to data connection if wifi is unavailable
145 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800146
Michael Kolb8233fac2010-10-26 16:08:53 -0700147 // public message ids
148 public final static int LOAD_URL = 1001;
149 public final static int STOP_LOAD = 1002;
150
151 // Message Ids
152 private static final int FOCUS_NODE_HREF = 102;
153 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800154 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700155
156 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
157
158 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800159 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700160
161 private static final int EMPTY_MENU = -1;
162
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700164 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700165 final static int PREFERENCES_PAGE = 3;
166 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000167 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700168 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800169 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000170
Michael Kolb8233fac2010-10-26 16:08:53 -0700171 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
172
173 // As the ids are dynamically created, we can't guarantee that they will
174 // be in sequence, so this static array maps ids to a window number.
175 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
176 { R.id.window_one_menu_id, R.id.window_two_menu_id,
177 R.id.window_three_menu_id, R.id.window_four_menu_id,
178 R.id.window_five_menu_id, R.id.window_six_menu_id,
179 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
180
181 // "source" parameter for Google search through search key
182 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
183 // "source" parameter for Google search through simplily type
184 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
185
George Mount387d45d2011-10-07 15:57:53 -0700186 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700187 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
188
John Reckd7dd9b22011-08-30 09:18:29 -0700189 // A bitmap that is re-used in createScreenshot as scratch space
190 private static Bitmap sThumbnailBitmap;
191
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 private Activity mActivity;
193 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700194 private HomepageHandler mHomepageHandler;
195 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 private BrowserSettings mSettings;
197 private WebViewFactory mFactory;
198
199 private WakeLock mWakeLock;
200
201 private UrlHandler mUrlHandler;
202 private UploadHandler mUploadHandler;
203 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 private PageDialogsHandler mPageDialogsHandler;
205 private NetworkStateHandler mNetworkHandler;
206
Ben Murdoch8029a772010-11-16 11:58:21 +0000207 private Message mAutoFillSetupMessage;
208
kaiyiza016da12013-08-26 17:50:22 +0800209 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700210
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 // FIXME, temp address onPrepareMenu performance problem.
212 // When we move everything out of view, we should rewrite this.
213 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700214 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 private int mOldMenuState = EMPTY_MENU;
216 private Menu mCachedMenu;
217
Michael Kolb8233fac2010-10-26 16:08:53 -0700218 private boolean mMenuIsDown;
219
Pankaj Garg49b79252014-11-07 17:33:41 -0800220 private boolean mWasInPageLoad = false;
221 private AppMenuHandler mAppMenuHandler;
222
Michael Kolb8233fac2010-10-26 16:08:53 -0700223 // For select and find, we keep track of the ActionMode so that
224 // finish() can be called as desired.
225 private ActionMode mActionMode;
226
227 /**
228 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
229 * of whether the configuration has changed. The first onMenuOpened call
230 * after a configuration change is simply a reopening of the same menu
231 * (i.e. mIconView did not change).
232 */
233 private boolean mConfigChanged;
234
235 /**
236 * Keeps track of whether the options menu is open. This is important in
237 * determining whether to show or hide the title bar overlay
238 */
239 private boolean mOptionsMenuOpen;
240
241 /**
242 * Whether or not the options menu is in its bigger, popup menu form. When
243 * true, we want the title bar overlay to be gone. When false, we do not.
244 * Only meaningful if mOptionsMenuOpen is true.
245 */
246 private boolean mExtendedMenuOpen;
247
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 private boolean mActivityPaused = true;
249 private boolean mLoadStopped;
250
251 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500252 // Checks to see when the bookmarks database has changed, and updates the
253 // Tabs' notion of whether they represent bookmarked sites.
254 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700255 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700256
Michael Kolbc3af0672011-08-09 10:24:41 -0700257 private boolean mBlockEvents;
258
Michael Kolb0b129122012-06-04 16:31:58 -0700259 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800260 private boolean mUpdateMyNavThumbnail;
261 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800262 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800263 private WebView.HitTestResult mResult;
Pankaj Gargeba076f2015-03-25 13:40:59 -0700264 private static Bitmap mBookmarkBitmap;
Site Maoabb7bd32015-03-20 15:34:02 -0700265 private PowerConnectionReceiver mLowPowerReceiver;
266 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700267
George Mount3636d0a2011-11-21 09:08:21 -0800268 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 mActivity = browser;
270 mSettings = BrowserSettings.getInstance();
271 mTabControl = new TabControl(this);
272 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700273 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800274 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700275 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700276
277 mUrlHandler = new UrlHandler(this);
278 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
280
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500282 mBookmarksObserver = new ContentObserver(mHandler) {
283 @Override
284 public void onChange(boolean selfChange) {
285 int size = mTabControl.getTabCount();
286 for (int i = 0; i < size; i++) {
287 mTabControl.getTab(i).updateBookmarkedStatus();
288 }
289 }
290
291 };
292 browser.getContentResolver().registerContentObserver(
293 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700294
295 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700296 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800297 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700298 }
299
John Reck9c35b9c2012-05-30 10:08:50 -0700300 @Override
301 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700302 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800303 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800304 // mCrashRecoverHandler has any previously saved state.
305 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700306 }
307
George Mount3636d0a2011-11-21 09:08:21 -0800308 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800309 // we dont want to ever recover incognito tabs
310 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700311
Patrick Scott7d50a932011-02-04 09:27:26 -0500312 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700313 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500314 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000315
Michael Kolbc831b632011-05-11 09:30:34 -0700316 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500317 // Not able to restore so we go ahead and clear session cookies. We
318 // must do this before trying to login the user as we don't want to
319 // clear any session cookies set during login.
320 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700321 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800322 if (intent == null) {
323 // This won't happen under common scenarios. The icicle is
324 // not null, but there aren't any tabs to restore.
325 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700326 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800327 final Bundle extra = intent.getExtras();
328 // Create an initial tab.
329 // If the intent is ACTION_VIEW and data is not null, the Browser is
330 // invoked to view the content by another application. In this case,
331 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800332 UrlData urlData = null;
333 if (intent.getData() != null
334 && Intent.ACTION_VIEW.equals(intent.getAction())
335 && intent.getData().toString().startsWith("content://")) {
336 urlData = new UrlData(intent.getData().toString());
337 } else {
338 urlData = IntentHandler.getUrlDataFromIntent(intent);
339 }
George Mount3636d0a2011-11-21 09:08:21 -0800340 Tab t = null;
341 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700342 String landingPage = mActivity.getResources().getString(
343 R.string.def_landing_page);
344 if (!landingPage.isEmpty()) {
345 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800346 } else {
347 t = openTabToHomePage();
348 }
George Mount3636d0a2011-11-21 09:08:21 -0800349 } else {
350 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700351 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800352 }
353 if (t != null) {
354 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
355 }
356 WebView webView = t.getWebView();
357 if (extra != null) {
358 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
359 if (scale > 0 && scale <= 1000) {
360 webView.setInitialScale(scale);
361 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700362 }
363 }
John Reckd8c74522011-06-14 08:45:00 -0700364 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700365 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700366 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500367 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700368 List<Tab> tabs = mTabControl.getTabs();
369 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700370
John Reck1cf4b792011-07-26 10:22:22 -0700371 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700372 //handle restored pages that may require a JS interface
373 if (t.getWebView() != null) {
374 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
375 if (backForwardList != null) {
376 for (int i = 0; i < backForwardList.getSize(); i++) {
377 WebHistoryItem item = backForwardList.getItemAtIndex(i);
378 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
379 }
380 }
381 }
John Reck1cf4b792011-07-26 10:22:22 -0700382 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700383 if (t != mTabControl.getCurrentTab()) {
384 t.pause();
385 }
John Reck1cf4b792011-07-26 10:22:22 -0700386 }
387 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700388 if (tabs.size() == 0) {
389 openTabToHomePage();
390 }
John Reck1cf4b792011-07-26 10:22:22 -0700391 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700392 // TabControl.restoreState() will create a new tab even if
393 // restoring the state fails.
394 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800395 // Intent is non-null when framework thinks the browser should be
396 // launching with a new intent (icicle is null).
397 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700398 mIntentHandler.onNewIntent(intent);
399 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700400 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700402 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800403 if (jsFlags.trim().length() != 0) {
404 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700405 }
George Mount3636d0a2011-11-21 09:08:21 -0800406 if (intent != null
407 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700408 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800409 }
Site Maoabb7bd32015-03-20 15:34:02 -0700410 mLowPowerReceiver = new PowerConnectionReceiver();
411 mPowerChangeReceiver = new PowerConnectionReceiver();
412
413 //always track the android framework's power save mode
414 IntentFilter filter = new IntentFilter();
415 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
416 // Power save mode only exists in Lollipop and above
417 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
418 }
419 filter.addAction(Intent.ACTION_BATTERY_OKAY);
420 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700421 }
422
John Reck1cf4b792011-07-26 10:22:22 -0700423 private static class PruneThumbnails implements Runnable {
424 private Context mContext;
425 private List<Long> mIds;
426
427 PruneThumbnails(Context context, List<Long> preserveIds) {
428 mContext = context.getApplicationContext();
429 mIds = preserveIds;
430 }
431
432 @Override
433 public void run() {
434 ContentResolver cr = mContext.getContentResolver();
435 if (mIds == null || mIds.size() == 0) {
436 cr.delete(Thumbnails.CONTENT_URI, null, null);
437 } else {
438 int length = mIds.size();
439 StringBuilder where = new StringBuilder();
440 where.append(Thumbnails._ID);
441 where.append(" not in (");
442 for (int i = 0; i < length; i++) {
443 where.append(mIds.get(i));
444 if (i < (length - 1)) {
445 where.append(",");
446 }
447 }
448 where.append(")");
449 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
450 }
451 }
452
453 }
454
Michael Kolb1514bb72010-11-22 09:11:48 -0800455 @Override
456 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700457 return mFactory;
458 }
459
460 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800461 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700462 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700463 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800464 }
465
466 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800467 public void createSubWindow(Tab tab) {
468 endActionMode();
469 WebView mainView = tab.getWebView();
470 WebView subView = mFactory.createWebView((mainView == null)
471 ? false
472 : mainView.isPrivateBrowsingEnabled());
473 mUi.createSubWindow(tab, subView);
474 }
475
476 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700477 public Context getContext() {
478 return mActivity;
479 }
480
481 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700482 public Activity getActivity() {
483 return mActivity;
484 }
485
486 void setUi(UI ui) {
487 mUi = ui;
488 }
489
Michael Kolbaf63dba2012-05-16 12:58:05 -0700490 @Override
491 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700492 return mSettings;
493 }
494
495 IntentHandler getIntentHandler() {
496 return mIntentHandler;
497 }
498
499 @Override
500 public UI getUi() {
501 return mUi;
502 }
503
504 int getMaxTabs() {
505 return mActivity.getResources().getInteger(R.integer.max_tabs);
506 }
507
508 @Override
509 public TabControl getTabControl() {
510 return mTabControl;
511 }
512
Michael Kolb1bf23132010-11-19 12:55:12 -0800513 @Override
514 public List<Tab> getTabs() {
515 return mTabControl.getTabs();
516 }
517
Michael Kolb8233fac2010-10-26 16:08:53 -0700518 private void startHandler() {
519 mHandler = new Handler() {
520
521 @Override
522 public void handleMessage(Message msg) {
523 switch (msg.what) {
524 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700525 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700526 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800527 case UNKNOWN_TYPE_MSG:
528 HashMap unknownTypeMap = (HashMap) msg.obj;
529 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
530 /*
531 * When the context menu is shown to the user
532 * we need to assure that its happening on the current webview
533 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
534 */
535 if (getCurrentWebView() != viewForUnknownType)
536 break;
537
538 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800539 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800540 WebView.HitTestResult result = new WebView.HitTestResult();
541
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800542 // Prevent unnecessary calls to context menu
543 // if url and image src are null
544 if (unknown_type_src == null && unknown_type_url == null)
545 break;
546
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800547 //setting the HitTestResult with new RESULT TYPE
548 if (!TextUtils.isEmpty(unknown_type_src)) {
549 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
550 result.setExtra(unknown_type_src);
551 } else {
552 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
553 result.setExtra("about:blank");
554 }
555
556 mResult = result;
557 openContextMenu(viewForUnknownType);
558 mResult = null;
559
560 break;
561
Michael Kolb8233fac2010-10-26 16:08:53 -0700562 case FOCUS_NODE_HREF:
563 {
564 String url = (String) msg.getData().get("url");
565 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500566 String src = (String) msg.getData().get("src");
567 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700568 if (TextUtils.isEmpty(url)) {
569 break;
570 }
571 HashMap focusNodeMap = (HashMap) msg.obj;
572 WebView view = (WebView) focusNodeMap.get("webview");
573 // Only apply the action if the top window did not change.
574 if (getCurrentTopWebView() != view) {
575 break;
576 }
577 switch (msg.arg1) {
578 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700579 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500581 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700582 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500583 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500584 case R.id.open_newtab_context_menu_id:
585 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700586 openTab(url, parent,
587 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500588 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700589 case R.id.copy_link_context_menu_id:
590 copy(url);
591 break;
592 case R.id.save_link_context_menu_id:
593 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500594 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800595 mActivity, url, view.getSettings().getUserAgentString(),
596 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700597 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700598 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700599 if(title == null || title == "")
600 title = url;
601
602 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
603 //SWE TODO: No thumbnail support for the url obtained via
604 //browser context menu as its not loaded in webview.
605 if (bookmarkIntent != null) {
606 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
607 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
608 mActivity.startActivity(bookmarkIntent);
609 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700610 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700611 }
612 break;
613 }
614
615 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700616 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 break;
618
619 case STOP_LOAD:
620 stopLoading();
621 break;
622
623 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700624 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 mWakeLock.release();
626 // if we reach here, Browser should be still in the
627 // background loading after WAKELOCK_TIMEOUT (5-min).
628 // To avoid burning the battery, stop loading.
629 mTabControl.stopAllLoading();
630 }
631 break;
632
633 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800634 Tab tab = (Tab) msg.obj;
635 if (tab != null) {
636 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 }
638 break;
kaiyiz591110b2013-08-06 17:11:06 +0800639 case OPEN_MENU:
640 if (!mOptionsMenuOpen && mActivity != null ) {
641 mActivity.openOptionsMenu();
642 }
643 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700644 }
645 }
646 };
647
648 }
649
John Reckef654f12011-07-12 16:42:08 -0700650 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200651 public Tab getCurrentTab() {
652 return mTabControl.getCurrentTab();
653 }
654
Michael Kolbba99c5d2010-11-29 14:57:41 -0800655 @Override
656 public void shareCurrentPage() {
657 shareCurrentPage(mTabControl.getCurrentTab());
658 }
659
660 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700661 if (tab == null || tab.getWebView() == null)
662 return;
663
664 final Tab mytab = tab;
665 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
666 @Override
667 public void onReceiveValue(Bitmap bitmap) {
668 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
669 mytab.getFavicon(), bitmap);
670 }
671 };
672
673 createScreenshotAsync(
674 tab.getWebView(),
675 getDesiredThumbnailWidth(mActivity),
676 getDesiredThumbnailHeight(mActivity),
677 new ValueCallback<Bitmap>() {
678 @Override
679 public void onReceiveValue(Bitmap bitmap) {
680 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
681 mytab.getFavicon(), bitmap);
682 }
683 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800684 }
685
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 /**
687 * Share a page, providing the title, url, favicon, and a screenshot. Uses
688 * an {@link Intent} to launch the Activity chooser.
689 * @param c Context used to launch a new Activity.
690 * @param title Title of the page. Stored in the Intent with
691 * {@link Intent#EXTRA_SUBJECT}
692 * @param url URL of the page. Stored in the Intent with
693 * {@link Intent#EXTRA_TEXT}
694 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
695 * with {@link Browser#EXTRA_SHARE_FAVICON}
696 * @param screenshot Bitmap of a screenshot of the page. Stored in the
697 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
698 */
699 static final void sharePage(Context c, String title, String url,
700 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700701
702 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
703 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
704 R.layout.app_row, sDialog.getApps());
705 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 }
707
708 private void copy(CharSequence text) {
709 ClipboardManager cm = (ClipboardManager) mActivity
710 .getSystemService(Context.CLIPBOARD_SERVICE);
711 cm.setText(text);
712 }
713
714 // lifecycle
715
John Reck9c35b9c2012-05-30 10:08:50 -0700716 @Override
717 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700718 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800719 // update the menu in case of a locale change
720 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800721 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800722 if (mOptionsMenuOpen) {
723 mActivity.closeOptionsMenu();
724 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
725 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700726 if (mPageDialogsHandler != null) {
727 mPageDialogsHandler.onConfigurationChanged(config);
728 }
729 mUi.onConfigurationChanged(config);
730 }
731
732 @Override
733 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700734 if (!mUi.isWebShowing()) {
735 mUi.showWeb(false);
736 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 mIntentHandler.onNewIntent(intent);
738 }
739
John Reck9c35b9c2012-05-30 10:08:50 -0700740 @Override
741 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800742 if (mUi.isCustomViewShowing()) {
743 hideCustomView();
744 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 if (mActivityPaused) {
746 Log.e(LOGTAG, "BrowserActivity is already paused.");
747 return;
748 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700749 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800750 Tab tab = mTabControl.getCurrentTab();
751 if (tab != null) {
752 tab.pause();
753 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700754 if (mWakeLock == null) {
755 PowerManager pm = (PowerManager) mActivity
756 .getSystemService(Context.POWER_SERVICE);
757 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
758 }
Michael Kolb70976932010-11-30 11:34:01 -0800759 mWakeLock.acquire();
760 mHandler.sendMessageDelayed(mHandler
761 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
762 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700763 }
764 mUi.onPause();
765 mNetworkHandler.onPause();
766
767 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100768 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700769 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700770 }
771
John Reck9c35b9c2012-05-30 10:08:50 -0700772 @Override
773 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700774 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800775 Bundle saveState = createSaveState();
776
777 // crash recovery manages all save & restore state
778 mCrashRecoveryHandler.writeState(saveState);
779 mSettings.setLastRunPaused(true);
780 }
781
782 /**
783 * Save the current state to outState. Does not write the state to
784 * disk.
785 * @return Bundle containing the current state of all tabs.
786 */
787 /* package */ Bundle createSaveState() {
788 Bundle saveState = new Bundle();
789 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800790 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700791 }
792
John Reck9c35b9c2012-05-30 10:08:50 -0700793 @Override
794 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 if (!mActivityPaused) {
796 Log.e(LOGTAG, "BrowserActivity is already resumed.");
797 return;
798 }
George Mount3636d0a2011-11-21 09:08:21 -0800799 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800801 Tab current = mTabControl.getCurrentTab();
802 if (current != null) {
803 current.resume();
804 resumeWebViewTimers(current);
805 }
John Reckf57c0292011-07-21 18:15:39 -0700806 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200807
Michael Kolb8233fac2010-10-26 16:08:53 -0700808 mUi.onResume();
809 mNetworkHandler.onResume();
810 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100811 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700812 if (mVoiceResult != null) {
813 mUi.onVoiceResult(mVoiceResult);
814 mVoiceResult = null;
815 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700816 if (current != null && current.getWebView().isShowingCrashView())
817 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700818 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 }
820
John Reckf57c0292011-07-21 18:15:39 -0700821 private void releaseWakeLock() {
822 if (mWakeLock != null && mWakeLock.isHeld()) {
823 mHandler.removeMessages(RELEASE_WAKELOCK);
824 mWakeLock.release();
825 }
826 }
827
Michael Kolb70976932010-11-30 11:34:01 -0800828 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800829 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800830 * @param tab guaranteed non-null
831 */
832 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700833 boolean inLoad = tab.inPageLoad();
834 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
835 CookieSyncManager.getInstance().startSync();
836 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100837 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700838 }
839 }
840
Michael Kolb70976932010-11-30 11:34:01 -0800841 /**
842 * Pause all WebView timers using the WebView of the given tab
843 * @param tab
844 * @return true if the timers are paused or tab is null
845 */
846 private boolean pauseWebViewTimers(Tab tab) {
847 if (tab == null) {
848 return true;
849 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100851 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 }
Michael Kolb70976932010-11-30 11:34:01 -0800854 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 }
856
John Reck9c35b9c2012-05-30 10:08:50 -0700857 @Override
858 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800859 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700860 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
861 mUploadHandler = null;
862 }
863 if (mTabControl == null) return;
864 mUi.onDestroy();
865 // Remove the current tab and sub window
866 Tab t = mTabControl.getCurrentTab();
867 if (t != null) {
868 dismissSubWindow(t);
869 removeTab(t);
870 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500871 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 // Destroy all the tabs
873 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700874 // Unregister receiver
875 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 }
877
878 protected boolean isActivityPaused() {
879 return mActivityPaused;
880 }
881
John Reck9c35b9c2012-05-30 10:08:50 -0700882 @Override
883 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 mTabControl.freeMemory();
885 }
886
887 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700888 public void stopLoading() {
889 mLoadStopped = true;
890 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700891 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700892 if (w != null) {
893 w.stopLoading();
894 mUi.onPageStopped(tab);
895 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 }
897
898 boolean didUserStopLoading() {
899 return mLoadStopped;
900 }
901
kaiyiza016da12013-08-26 17:50:22 +0800902 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700903 final String reminderType = getContext().getResources().getString(
904 R.string.def_wifi_browser_interaction_remind_type);
905 final String selectionConnnection = getContext().getResources().getString(
906 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700907 final String wifiSelection = getContext().getResources().getString(
908 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700909
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700910 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
911 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700912 return;
913
kaiyiza016da12013-08-26 17:50:22 +0800914 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700915 Context.CONNECTIVITY_SERVICE);
916 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700917 WifiManager wifiMgr = (WifiManager) this.getContext()
918 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700919 if (networkInfo == null
920 || (networkInfo != null && (networkInfo.getType() !=
921 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700922 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
923 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700924 List<ScanResult> list = wifiMgr.getScanResults();
925 // Have no AP's for Wifi's fall back to data
926 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700927 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700928 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
929 this.getContext().startActivity(intent);
930 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700931 // Request to select Wifi AP
932 try {
933 Intent intent = new Intent(wifiSelection);
934 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
935 this.getContext().startActivity(intent);
936 } catch (Exception e) {
937 String err_msg = this.getContext().getString(
938 R.string.acivity_not_found, wifiSelection);
939 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
940 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700941 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700942 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800943 }
944 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700945
Michael Kolb8233fac2010-10-26 16:08:53 -0700946 // WebViewController
947
948 @Override
John Reck324d4402011-01-11 16:56:42 -0800949 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700950
951 // We've started to load a new page. If there was a pending message
952 // to save a screenshot then we will now take the new page and save
953 // an incorrect screenshot. Therefore, remove any pending thumbnail
954 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700955 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700956
957 // reset sync timer to avoid sync starts during loading a page
958 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700959 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700960 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800961 boolean networkNotifier = BrowserConfig.getInstance(getContext())
962 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700963 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700964 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800965 } else {
966 if (!mNetworkHandler.isNetworkUp()) {
967 view.setNetworkAvailable(false);
968 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700969 }
970
971 // when BrowserActivity just starts, onPageStarted may be called before
972 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
973 // to start the timer. As we won't switch tabs while an activity is in
974 // pause state, we can ensure calling resume and pause in pair.
975 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800976 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 }
978 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 endActionMode();
980
John Reck30c714c2010-12-16 17:30:34 -0800981 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700982
John Reck324d4402011-01-11 16:56:42 -0800983 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700984 // update the bookmark database for favicon
985 maybeUpdateFavicon(tab, null, url, favicon);
986
987 Performance.tracePageStart(url);
988
989 // Performance probe
990 if (false) {
991 Performance.onPageStarted();
992 }
993
994 }
995
996 @Override
John Reck324d4402011-01-11 16:56:42 -0800997 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700998 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800999 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001000
Michael Kolb8233fac2010-10-26 16:08:53 -07001001 // Performance probe
1002 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001003 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 }
1005
Tarun Nainani8eb00912014-07-17 12:28:32 -07001006 tab.onPageFinished();
1007
Michael Kolb8233fac2010-10-26 16:08:53 -07001008 Performance.tracePageFinished();
1009 }
1010
1011 @Override
John Reck30c714c2010-12-16 17:30:34 -08001012 public void onProgressChanged(Tab tab) {
1013 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001014
1015 if (newProgress == 100) {
1016 CookieSyncManager.getInstance().sync();
1017 // onProgressChanged() may continue to be called after the main
1018 // frame has finished loading, as any remaining sub frames continue
1019 // to load. We'll only get called once though with newProgress as
1020 // 100 when everything is loaded. (onPageFinished is called once
1021 // when the main frame completes loading regardless of the state of
1022 // any sub frames so calls to onProgressChanges may continue after
1023 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001024 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001025 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001026 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001027 } else if (mWasInPageLoad) {
1028 mWasInPageLoad = false;
1029 updateInLoadMenuItems(mCachedMenu, tab);
1030 }
1031
1032 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001033 // pause the WebView timer and release the wake lock if it is
1034 // finished while BrowserActivity is in pause state.
1035 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001036 }
John Reckd9862372012-02-21 15:04:50 -08001037 if (!tab.isPrivateBrowsingEnabled()
1038 && !TextUtils.isEmpty(tab.getUrl())
1039 && !tab.isSnapshot()) {
1040 // Only update the bookmark screenshot if the user did not
1041 // cancel the load early and there is not already
1042 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001043 if (tab.shouldUpdateThumbnail() &&
1044 (tab.inForeground() && !didUserStopLoading()
1045 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001046 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1047 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1048 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001049 1500);
John Reckd9862372012-02-21 15:04:50 -08001050 }
1051 }
1052 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001053 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001054 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001056 // still loading
1057 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001058 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001059 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001060 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001061 } else {
1062 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 }
1064 }
John Reck30c714c2010-12-16 17:30:34 -08001065 mUi.onProgressChanged(tab);
1066 }
1067
1068 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001069 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001070 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001071 }
1072
1073 @Override
1074 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001075 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001076 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001077 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001078 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1079 return;
1080 }
1081 // Update the title in the history database if not in private browsing mode
1082 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001083 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 }
1085 }
1086
1087 @Override
1088 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001089 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001090 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1091 }
1092
1093 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001094 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1095 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 }
1097
1098 @Override
1099 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1100 if (mMenuIsDown) {
1101 // only check shortcut key when MENU is held
1102 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1103 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001104 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001105 int keyCode = event.getKeyCode();
1106 // We need to send almost every key to WebKit. However:
1107 // 1. We don't want to block the device on the renderer for
1108 // some keys like menu, home, call.
1109 // 2. There are no WebKit equivalents for some of these keys
1110 // (see app/keyboard_codes_win.h)
1111 // Note that these are not the same set as KeyEvent.isSystemKey:
1112 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1113 if (keyCode == KeyEvent.KEYCODE_MENU ||
1114 keyCode == KeyEvent.KEYCODE_HOME ||
1115 keyCode == KeyEvent.KEYCODE_BACK ||
1116 keyCode == KeyEvent.KEYCODE_CALL ||
1117 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1118 keyCode == KeyEvent.KEYCODE_POWER ||
1119 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1120 keyCode == KeyEvent.KEYCODE_CAMERA ||
1121 keyCode == KeyEvent.KEYCODE_FOCUS ||
1122 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1123 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1124 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1125 return true;
1126 }
1127
1128 // We also have to intercept some shortcuts before we send them to the ContentView.
1129 if (event.isCtrlPressed() && (
1130 keyCode == KeyEvent.KEYCODE_TAB ||
1131 keyCode == KeyEvent.KEYCODE_W ||
1132 keyCode == KeyEvent.KEYCODE_F4)) {
1133 return true;
1134 }
1135
1136 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 }
1138
Tarun Nainanif5563b62015-01-21 18:52:59 -08001139 private void handleMediaKeyEvent(KeyEvent event) {
1140
1141 int keyCode = event.getKeyCode();
1142 // send media key events to audio manager
1143 if (Build.VERSION.SDK_INT >= 19) {
1144
1145 switch (keyCode) {
1146 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1147 case KeyEvent.KEYCODE_MEDIA_STOP:
1148 case KeyEvent.KEYCODE_MEDIA_NEXT:
1149 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1150 case KeyEvent.KEYCODE_MEDIA_REWIND:
1151 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1152 case KeyEvent.KEYCODE_MUTE:
1153 case KeyEvent.KEYCODE_MEDIA_PLAY:
1154 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1155 case KeyEvent.META_SHIFT_RIGHT_ON:
1156 case KeyEvent.KEYCODE_MEDIA_EJECT:
1157 case KeyEvent.KEYCODE_MEDIA_RECORD:
1158 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1159
1160 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1161 .getSystemService(Context.AUDIO_SERVICE);
1162 audioManager.dispatchMediaKeyEvent(event);
1163 }
1164 }
1165 }
1166
Michael Kolb8233fac2010-10-26 16:08:53 -07001167 @Override
John Reck997b1b72012-04-19 18:08:25 -07001168 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001170 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001171 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001172 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001173 } else {
John Reck997b1b72012-04-19 18:08:25 -07001174 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001175 }
1176 }
John Reck997b1b72012-04-19 18:08:25 -07001177 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001178 }
1179
1180 @Override
John Reck324d4402011-01-11 16:56:42 -08001181 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001182 // Don't save anything in private browsing mode or when disabling history
1183 // for regular tabs is enabled
1184 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1185 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1186 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001187
John Reck49a603c2011-03-03 09:33:05 -08001188 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001189
John Reck324d4402011-01-11 16:56:42 -08001190 if (TextUtils.isEmpty(url)
1191 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001192 return;
1193 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001194
John Reckf57c0292011-07-21 18:15:39 -07001195 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001196 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001197 }
1198
1199 @Override
1200 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1201 AsyncTask<Void, Void, String[]> task =
1202 new AsyncTask<Void, Void, String[]>() {
1203 @Override
1204 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001205 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 }
1207 @Override
1208 public void onPostExecute(String[] result) {
1209 callback.onReceiveValue(result);
1210 }
1211 };
1212 task.execute();
1213 }
1214
1215 @Override
1216 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1217 final HttpAuthHandler handler, final String host,
1218 final String realm) {
1219 String username = null;
1220 String password = null;
1221
1222 boolean reuseHttpAuthUsernamePassword
1223 = handler.useHttpAuthUsernamePassword();
1224
1225 if (reuseHttpAuthUsernamePassword && view != null) {
1226 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1227 if (credentials != null && credentials.length == 2) {
1228 username = credentials[0];
1229 password = credentials[1];
1230 }
1231 }
1232
1233 if (username != null && password != null) {
1234 handler.proceed(username, password);
1235 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001236 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001237 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1238 } else {
1239 handler.cancel();
1240 }
1241 }
1242 }
1243
1244 @Override
1245 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001246 String contentDisposition, String mimetype, String referer,
1247 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001248 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001249 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001250 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001251 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001252 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001253 // This Tab was opened for the sole purpose of downloading a
1254 // file. Remove it.
1255 if (tab == mTabControl.getCurrentTab()) {
1256 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001257 if (tab.getDerivedFromIntent())
1258 closeTab(tab);
1259 else
1260 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001261 } else {
1262 // In this case, it is not.
1263 closeTab(tab);
1264 }
1265 }
1266 }
1267
1268 @Override
1269 public Bitmap getDefaultVideoPoster() {
1270 return mUi.getDefaultVideoPoster();
1271 }
1272
1273 @Override
1274 public View getVideoLoadingProgressView() {
1275 return mUi.getVideoLoadingProgressView();
1276 }
1277
1278 @Override
1279 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1280 SslError error) {
1281 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1282 }
1283
1284 // helper method
1285
1286 /*
1287 * Update the favorites icon if the private browsing isn't enabled and the
1288 * icon is valid.
1289 */
1290 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1291 final String url, Bitmap favicon) {
1292 if (favicon == null) {
1293 return;
1294 }
1295 if (!tab.isPrivateBrowsingEnabled()) {
1296 Bookmarks.updateFavicon(mActivity
1297 .getContentResolver(), originalUrl, url, favicon);
1298 }
1299 }
1300
Leon Scroggins4cd97792010-12-03 15:31:56 -05001301 @Override
1302 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001303 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001304 mUi.bookmarkedStatusHasChanged(tab);
1305 }
1306
Michael Kolb8233fac2010-10-26 16:08:53 -07001307 // end WebViewController
1308
1309 protected void pageUp() {
1310 getCurrentTopWebView().pageUp(false);
1311 }
1312
1313 protected void pageDown() {
1314 getCurrentTopWebView().pageDown(false);
1315 }
1316
1317 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001318 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001319 public void editUrl() {
1320 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001321 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001322 }
1323
John Reck9c35b9c2012-05-30 10:08:50 -07001324 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001325 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001326 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 if (tab.inForeground()) {
1328 if (mUi.isCustomViewShowing()) {
1329 callback.onCustomViewHidden();
1330 return;
1331 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001332 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001333 // Save the menu state and set it to empty while the custom
1334 // view is showing.
1335 mOldMenuState = mMenuState;
1336 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001337 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001338 }
1339 }
1340
1341 @Override
1342 public void hideCustomView() {
1343 if (mUi.isCustomViewShowing()) {
1344 mUi.onHideCustomView();
1345 // Reset the old menu state.
1346 mMenuState = mOldMenuState;
1347 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001348 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001349 }
1350 }
1351
John Reck9c35b9c2012-05-30 10:08:50 -07001352 @Override
1353 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001354 Intent intent) {
1355 if (getCurrentTopWebView() == null) return;
1356 switch (requestCode) {
1357 case PREFERENCES_PAGE:
1358 if (resultCode == Activity.RESULT_OK && intent != null) {
1359 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001360 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001361 mTabControl.removeParentChildRelationShips();
1362 }
1363 }
1364 break;
1365 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001366 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001367 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001370 case AUTOFILL_SETUP:
1371 // Determine whether a profile was actually set up or not
1372 // and if so, send the message back to the WebTextView to
1373 // fill the form with the new profile.
1374 if (getSettings().getAutoFillProfile() != null) {
1375 mAutoFillSetupMessage.sendToTarget();
1376 mAutoFillSetupMessage = null;
1377 }
1378 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001379 case COMBO_VIEW:
1380 if (intent == null || resultCode != Activity.RESULT_OK) {
1381 break;
1382 }
John Reck3ba45532011-08-11 16:26:53 -07001383 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001384 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1385 Tab t = getCurrentTab();
1386 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001387 mUpdateMyNavThumbnail = true;
1388 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001389 loadUrl(t, uri.toString());
1390 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1391 String[] urls = intent.getStringArrayExtra(
1392 ComboViewActivity.EXTRA_OPEN_ALL);
1393 Tab parent = getCurrentTab();
1394 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001395 if (url != null) {
1396 parent = openTab(url, parent,
1397 !mSettings.openInBackground(), true);
1398 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001399 }
1400 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1401 long id = intent.getLongExtra(
1402 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1403 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001404 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001405 }
1406 }
1407 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001408 case VOICE_RESULT:
1409 if (resultCode == Activity.RESULT_OK && intent != null) {
1410 ArrayList<String> results = intent.getStringArrayListExtra(
1411 RecognizerIntent.EXTRA_RESULTS);
1412 if (results.size() >= 1) {
1413 mVoiceResult = results.get(0);
1414 }
1415 }
1416 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001417 case MY_NAVIGATION:
1418 if (intent == null || resultCode != Activity.RESULT_OK) {
1419 break;
1420 }
1421
1422 if (intent.getBooleanExtra("need_refresh", false) &&
1423 getCurrentTopWebView() != null) {
1424 getCurrentTopWebView().reload();
1425 }
1426 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001427 default:
1428 break;
1429 }
1430 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001431 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001432 }
1433
1434 /**
1435 * Open the Go page.
1436 * @param startWithHistory If true, open starting on the history tab.
1437 * Otherwise, start with the bookmarks tab.
1438 */
1439 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001440 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001441 if (mTabControl.getCurrentWebView() == null) {
1442 return;
1443 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001444 // clear action mode
1445 if (isInCustomActionMode()) {
1446 endActionMode();
1447 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001448 Bundle extras = new Bundle();
1449 // Disable opening in a new window if we have maxed out the windows
1450 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1451 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001452 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 }
1454
1455 // combo view callbacks
1456
Michael Kolb8233fac2010-10-26 16:08:53 -07001457 // key handling
1458 protected void onBackKey() {
1459 if (!mUi.onBackKey()) {
1460 WebView subwindow = mTabControl.getCurrentSubWindow();
1461 if (subwindow != null) {
1462 if (subwindow.canGoBack()) {
1463 subwindow.goBack();
1464 } else {
1465 dismissSubWindow(mTabControl.getCurrentTab());
1466 }
1467 } else {
1468 goBackOnePageOrQuit();
1469 }
1470 }
1471 }
1472
Michael Kolb4bd767d2011-05-27 11:33:55 -07001473 protected boolean onMenuKey() {
1474 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001475 }
1476
Michael Kolb8233fac2010-10-26 16:08:53 -07001477 // menu handling and state
1478 // TODO: maybe put into separate handler
1479
John Reck9c35b9c2012-05-30 10:08:50 -07001480 @Override
1481 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001482 if (mMenuState == EMPTY_MENU) {
1483 return false;
1484 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001485 MenuInflater inflater = mActivity.getMenuInflater();
1486 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001487 return true;
1488 }
1489
John Reck9c35b9c2012-05-30 10:08:50 -07001490 @Override
1491 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001492 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001493 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001494 return;
1495 }
1496 if (!(v instanceof WebView)) {
1497 return;
1498 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001499 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001500 WebView.HitTestResult result;
1501
1502 /* Determine whether the ContextMenu got triggered because
1503 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1504 * received. The mResult acts as a flag to identify, how it got trigerred
1505 */
1506 if (mResult == null){
1507 result = webview.getHitTestResult();
1508 } else {
1509 result = mResult;
1510 }
1511
Michael Kolb8233fac2010-10-26 16:08:53 -07001512 if (result == null) {
1513 return;
1514 }
1515
1516 int type = result.getType();
1517 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001518
1519 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1520 unknownTypeMap.put("webview", webview);
1521 final Message msg = mHandler.obtainMessage(
1522 UNKNOWN_TYPE_MSG, unknownTypeMap);
1523 /* As defined in android developers guide
1524 * when UNKNOWN_TYPE is received as a result of HitTest
1525 * you need to determing the type by invoking requestFocusNodeHref
1526 */
1527 webview.requestFocusNodeHref(msg);
1528
Michael Kolb8233fac2010-10-26 16:08:53 -07001529 Log.w(LOGTAG,
1530 "We should not show context menu when nothing is touched");
1531 return;
1532 }
1533 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1534 // let TextView handles context menu
1535 return;
1536 }
1537
1538 // Note, http://b/issue?id=1106666 is requesting that
1539 // an inflated menu can be used again. This is not available
1540 // yet, so inflate each time (yuk!)
1541 MenuInflater inflater = mActivity.getMenuInflater();
1542 inflater.inflate(R.menu.browsercontext, menu);
1543
1544 // Show the correct menu group
1545 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001546 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001547 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001548 menu.setGroupVisible(R.id.PHONE_MENU,
1549 type == WebView.HitTestResult.PHONE_TYPE);
1550 menu.setGroupVisible(R.id.EMAIL_MENU,
1551 type == WebView.HitTestResult.EMAIL_TYPE);
1552 menu.setGroupVisible(R.id.GEO_MENU,
1553 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001554 String itemUrl = null;
1555 String url = webview.getOriginalUrl();
1556 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1557 itemUrl = Uri.decode(navigationUrl);
1558 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1559 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1560 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1561 } else {
1562 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1563 }
1564 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1565 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1566 } else {
1567 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1568
1569 menu.setGroupVisible(R.id.IMAGE_MENU,
1570 type == WebView.HitTestResult.IMAGE_TYPE
1571 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1572 menu.setGroupVisible(R.id.ANCHOR_MENU,
1573 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1574 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001575 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1576 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001577 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001578 // Setup custom handling depending on the type
1579 switch (type) {
1580 case WebView.HitTestResult.PHONE_TYPE:
1581 menu.setHeaderTitle(Uri.decode(extra));
1582 menu.findItem(R.id.dial_context_menu_id).setIntent(
1583 new Intent(Intent.ACTION_VIEW, Uri
1584 .parse(WebView.SCHEME_TEL + extra)));
1585 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1586 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1587 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1588 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1589 addIntent);
1590 menu.findItem(R.id.copy_phone_context_menu_id)
1591 .setOnMenuItemClickListener(
1592 new Copy(extra));
1593 break;
1594
1595 case WebView.HitTestResult.EMAIL_TYPE:
1596 menu.setHeaderTitle(extra);
1597 menu.findItem(R.id.email_context_menu_id).setIntent(
1598 new Intent(Intent.ACTION_VIEW, Uri
1599 .parse(WebView.SCHEME_MAILTO + extra)));
1600 menu.findItem(R.id.copy_mail_context_menu_id)
1601 .setOnMenuItemClickListener(
1602 new Copy(extra));
1603 break;
1604
1605 case WebView.HitTestResult.GEO_TYPE:
1606 menu.setHeaderTitle(extra);
1607 menu.findItem(R.id.map_context_menu_id).setIntent(
1608 new Intent(Intent.ACTION_VIEW, Uri
1609 .parse(WebView.SCHEME_GEO
1610 + URLEncoder.encode(extra))));
1611 menu.findItem(R.id.copy_geo_context_menu_id)
1612 .setOnMenuItemClickListener(
1613 new Copy(extra));
1614 break;
1615
1616 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1617 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001618 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001619 // decide whether to show the open link in new tab option
1620 boolean showNewTab = mTabControl.canCreateNewTab();
1621 MenuItem newTabItem
1622 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001623 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001624 ? R.string.contextmenu_openlink_newwindow_background
1625 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001626 newTabItem.setVisible(showNewTab);
1627 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001628 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1629 newTabItem.setOnMenuItemClickListener(
1630 new MenuItem.OnMenuItemClickListener() {
1631 @Override
1632 public boolean onMenuItemClick(MenuItem item) {
1633 final HashMap<String, WebView> hrefMap =
1634 new HashMap<String, WebView>();
1635 hrefMap.put("webview", webview);
1636 final Message msg = mHandler.obtainMessage(
1637 FOCUS_NODE_HREF,
1638 R.id.open_newtab_context_menu_id,
1639 0, hrefMap);
1640 webview.requestFocusNodeHref(msg);
1641 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001642 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001643 });
1644 } else {
1645 newTabItem.setOnMenuItemClickListener(
1646 new MenuItem.OnMenuItemClickListener() {
1647 @Override
1648 public boolean onMenuItemClick(MenuItem item) {
1649 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001650 openTab(extra, parent,
1651 !mSettings.openInBackground(),
1652 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001653 return true;
1654 }
1655 });
1656 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001657 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001658 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1659 menu.setHeaderTitle(navigationUrl);
1660 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1661
1662 if (itemUrl != null) {
1663 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1664 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1665 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1666 @Override
1667 public boolean onMenuItemClick(MenuItem item) {
1668 final Intent intent = new Intent(Controller.this
1669 .getContext(),
1670 AddMyNavigationPage.class);
1671 Bundle bundle = new Bundle();
1672 String url = Uri.decode(navigationUrl);
1673 bundle.putBoolean("isAdding", false);
1674 bundle.putString("url", url);
1675 bundle.putString("name", getNameFromUrl(url));
1676 intent.putExtra("websites", bundle);
1677 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1678 return false;
1679 }
1680 });
1681 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1682 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1683 @Override
1684 public boolean onMenuItemClick(MenuItem item) {
1685 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1686 return false;
1687 }
1688 });
1689 }
1690 } else {
1691 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1692 }
1693 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001694 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1695 break;
1696 }
1697 // otherwise fall through to handle image part
1698 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001699 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1700 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001701 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1702 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001703 shareItem.setOnMenuItemClickListener(
1704 new MenuItem.OnMenuItemClickListener() {
1705 @Override
1706 public boolean onMenuItemClick(MenuItem item) {
1707 sharePage(mActivity, null, extra, null,
1708 null);
1709 return true;
1710 }
1711 }
1712 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001713 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001714 menu.findItem(R.id.view_image_context_menu_id)
1715 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1716 @Override
1717 public boolean onMenuItemClick(MenuItem item) {
1718 openTab(extra, mTabControl.getCurrentTab(), true, true);
1719 return false;
1720 }
1721 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001722 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1723 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1724 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001725 menu.findItem(R.id.set_wallpaper_context_menu_id).
1726 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1727 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001728 break;
1729
1730 default:
1731 Log.w(LOGTAG, "We should not get here.");
1732 break;
1733 }
1734 //update the ui
1735 mUi.onContextMenuCreated(menu);
1736 }
1737
kaiyiz6e5b3e02013-08-19 20:02:01 +08001738 public void startAddMyNavigation(String url) {
1739 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1740 Bundle bundle = new Bundle();
1741 bundle.putBoolean("isAdding", true);
1742 bundle.putString("url", url);
1743 bundle.putString("name", getNameFromUrl(url));
1744 intent.putExtra("websites", bundle);
1745 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1746 }
1747
1748 private void showMyNavigationDeleteDialog(final String itemUrl) {
1749 new AlertDialog.Builder(this.getContext())
1750 .setTitle(R.string.my_navigation_delete_label)
1751 .setIcon(android.R.drawable.ic_dialog_alert)
1752 .setMessage(R.string.my_navigation_delete_msg)
1753 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1754 @Override
1755 public void onClick(DialogInterface dialog, int whichButton) {
1756 deleteMyNavigationItem(itemUrl);
1757 }
1758 })
1759 .setNegativeButton(R.string.cancel, null)
1760 .show();
1761 }
1762
1763 private void deleteMyNavigationItem(final String itemUrl) {
1764 ContentResolver cr = this.getContext().getContentResolver();
1765 Cursor cursor = null;
1766
1767 try {
1768 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1769 new String[] {
1770 MyNavigationUtil.ID
1771 }, "url = ?", new String[] {
1772 itemUrl
1773 }, null);
1774 if (null != cursor && cursor.moveToFirst()) {
1775 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1776 cursor.getLong(0));
1777
1778 ContentValues values = new ContentValues();
1779 values.put(MyNavigationUtil.TITLE, "");
1780 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1781 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1782 ByteArrayOutputStream os = new ByteArrayOutputStream();
1783 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1784 R.raw.my_navigation_add);
1785 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1786 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1787 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1788 cr.update(uri, values, null, null);
1789 } else {
1790 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1791 }
1792 } catch (IllegalStateException e) {
1793 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1794 } finally {
1795 if (null != cursor) {
1796 cursor.close();
1797 }
1798 }
1799
1800 if (getCurrentTopWebView() != null) {
1801 getCurrentTopWebView().reload();
1802 }
1803 }
1804
1805 private String getNameFromUrl(String itemUrl) {
1806 ContentResolver cr = this.getContext().getContentResolver();
1807 Cursor cursor = null;
1808 String name = null;
1809
1810 try {
1811 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1812 new String[] {
1813 MyNavigationUtil.TITLE
1814 }, "url = ?", new String[] {
1815 itemUrl
1816 }, null);
1817 if (null != cursor && cursor.moveToFirst()) {
1818 name = cursor.getString(0);
1819 } else {
1820 Log.e(LOGTAG, "this item does not exist!");
1821 }
1822 } catch (IllegalStateException e) {
1823 Log.e(LOGTAG, "getNameFromUrl", e);
1824 } finally {
1825 if (null != cursor) {
1826 cursor.close();
1827 }
1828 }
1829 return name;
1830 }
1831
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001832 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001833 final ContentResolver cr = mActivity.getContentResolver();
1834 new AsyncTask<Void, Void, Void>() {
1835 @Override
1836 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001837 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1838 if(!isMyNavigationUrl)
1839 return null;
1840
kaiyiz6e5b3e02013-08-19 20:02:01 +08001841 ContentResolver cr = mActivity.getContentResolver();
1842 Cursor cursor = null;
1843 try {
1844 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1845 new String[] {
1846 MyNavigationUtil.ID
1847 }, "url = ?", new String[] {
1848 itemUrl
1849 }, null);
1850 if (null != cursor && cursor.moveToFirst()) {
1851 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001852 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001853
1854 ContentValues values = new ContentValues();
1855 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1856 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1857 cursor.getLong(0));
1858 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1859 cr.update(uri, values, null, null);
1860 os.close();
1861 }
1862 } catch (IllegalStateException e) {
1863 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1864 } catch (IOException e) {
1865 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1866 } finally {
1867 if (null != cursor) {
1868 cursor.close();
1869 }
1870 }
1871 return null;
1872 }
1873 }.execute();
1874 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001875 /**
1876 * As the menu can be open when loading state changes
1877 * we must manually update the state of the stop/reload menu
1878 * item
1879 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001880 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001881 if (menu == null) {
1882 return;
1883 }
1884 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001885 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001886 menu.findItem(R.id.stop_menu_id):
1887 menu.findItem(R.id.reload_menu_id);
1888 if (src != null) {
1889 dest.setIcon(src.getIcon());
1890 dest.setTitle(src.getTitle());
1891 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001892 mActivity.invalidateOptionsMenu();
1893 }
1894
1895 public void invalidateOptionsMenu() {
1896 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 }
1898
John Reck9c35b9c2012-05-30 10:08:50 -07001899 @Override
1900 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001901 // Software menu key (toolbar key)
1902 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1903 return true;
1904 }
1905
1906 @Override
1907 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001908 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001909 // hold on to the menu reference here; it is used by the page callbacks
1910 // to update the menu based on loading state
1911 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001912 // Note: setVisible will decide whether an item is visible; while
1913 // setEnabled() will decide whether an item is enabled, which also means
1914 // whether the matching shortcut key will function.
1915 switch (mMenuState) {
1916 case EMPTY_MENU:
1917 if (mCurrentMenuState != mMenuState) {
1918 menu.setGroupVisible(R.id.MAIN_MENU, false);
1919 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1920 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1921 }
1922 break;
1923 default:
1924 if (mCurrentMenuState != mMenuState) {
1925 menu.setGroupVisible(R.id.MAIN_MENU, true);
1926 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1927 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1928 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001929 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001930 break;
1931 }
1932 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001933 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001934
1935 IncognitoRestriction.getInstance()
1936 .registerControl(menu.findItem(R.id.incognito_menu_id)
1937 .getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001938 }
1939
1940 private void setMenuItemVisibility(Menu menu, int id,
1941 boolean visibility) {
1942 MenuItem item = menu.findItem(id);
1943 if (item != null) {
1944 item.setVisible(visibility);
1945 }
1946 }
1947
1948 private int lookupBookmark(String title, String url) {
1949 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001950 int count = 0;
1951 Cursor cursor = null;
1952 try {
1953 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1954 BookmarksLoader.PROJECTION,
1955 "title = ? OR url = ?",
1956 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001957 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001958 },
1959 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001960
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001961 if (cursor != null)
1962 count = cursor.getCount();
1963
1964 } catch (IllegalStateException e) {
1965 Log.e(LOGTAG, "lookupBookmark ", e);
1966 } finally {
1967 if (null != cursor) {
1968 cursor.close();
1969 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001970 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001971 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001972 }
1973
1974 private void resetMenuItems(Menu menu) {
1975 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1976 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1977
1978 WebView w = getCurrentTopWebView();
1979 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1980
1981 String title = w.getTitle();
1982 String url = w.getUrl();
1983 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1984 bookmark_icon.setChecked(true);
1985 } else {
1986 bookmark_icon.setChecked(false);
1987 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001988 }
1989
Michael Kolb4bf79712011-07-14 14:18:12 -07001990 @Override
1991 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001992 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001993 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001994 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001995 // Following flag is used to identify schemes for which the LIVE_MENU
1996 // items defined in res/menu/browser.xml should be enabled
1997 boolean isLiveScheme = false;
1998 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07001999 boolean isSavable = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002000 resetMenuItems(menu);
2001
Michael Kolb4bf79712011-07-14 14:18:12 -07002002 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002003 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002004 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002005 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002006 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002007 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002008 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07002009 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002010
2011 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2012 forward.setEnabled(canGoForward);
2013
Michael Kolb4bf79712011-07-14 14:18:12 -07002014 // decide whether to show the share link option
2015 PackageManager pm = mActivity.getPackageManager();
2016 Intent send = new Intent(Intent.ACTION_SEND);
2017 send.setType("text/plain");
2018 ResolveInfo ri = pm.resolveActivity(send,
2019 PackageManager.MATCH_DEFAULT_ONLY);
2020 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2021
2022 boolean isNavDump = mSettings.enableNavDump();
2023 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2024 nav.setVisible(isNavDump);
2025 nav.setEnabled(isNavDump);
2026
2027 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002028 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2029 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002030 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002031 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2032 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002033 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002034 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2035 isLive && isLiveScheme && isPageFinished);
2036 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002037 isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002038 // history and snapshots item are the members of COMBO menu group,
2039 // so if show history item, only make snapshots item invisible.
2040 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002041
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002042 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002043 }
2044
John Reck9c35b9c2012-05-30 10:08:50 -07002045 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002046 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002047 if (null == getCurrentTopWebView()) {
2048 return false;
2049 }
2050 if (mMenuIsDown) {
2051 // The shortcut action consumes the MENU. Even if it is still down,
2052 // it won't trigger the next shortcut action. In the case of the
2053 // shortcut action triggering a new activity, like Bookmarks, we
2054 // won't get onKeyUp for MENU. So it is important to reset it here.
2055 mMenuIsDown = false;
2056 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002057 if (mUi.onOptionsItemSelected(item)) {
2058 // ui callback handled it
2059 return true;
2060 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002061 switch (item.getItemId()) {
2062 // -- Main menu
2063 case R.id.new_tab_menu_id:
2064 openTabToHomePage();
2065 break;
2066
2067 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002068 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 break;
2070
2071 case R.id.goto_menu_id:
2072 editUrl();
2073 break;
2074
2075 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002076 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2077 break;
2078
2079 case R.id.history_menu_id:
2080 bookmarksOrHistoryPicker(ComboViews.History);
2081 break;
2082
2083 case R.id.snapshots_menu_id:
2084 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002085 break;
2086
Pankaj Garg49b79252014-11-07 17:33:41 -08002087 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002088 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002089 break;
2090
2091 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002092 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002093 stopLoading();
2094 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002095 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002096 getCurrentTopWebView().reload();
2097 }
2098 break;
2099
Michael Kolb8233fac2010-10-26 16:08:53 -07002100 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002101 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002102 break;
2103
2104 case R.id.close_menu_id:
2105 // Close the subwindow if it exists.
2106 if (mTabControl.getCurrentSubWindow() != null) {
2107 dismissSubWindow(mTabControl.getCurrentTab());
2108 break;
2109 }
2110 closeCurrentTab();
2111 break;
2112
kaiyiza8b6dbb2013-07-29 18:11:22 +08002113 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002114 Object[] params = { new String("persist.debug.browsermonkeytest")};
2115 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002116 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002117 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002118 if (ret != null && ret.equals("enable"))
2119 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002120 if (BrowserConfig.getInstance(getContext())
2121 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2122 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002123 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002124 case R.id.homepage_menu_id:
2125 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002126 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002127 break;
2128
2129 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002130 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002131 break;
2132
2133 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002134 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002135 break;
2136
John Reck2bc80422011-06-30 15:11:49 -07002137 case R.id.save_snapshot_menu_id:
2138 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002139 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002140 createScreenshotAsync(
2141 source.getWebView(),
2142 getDesiredThumbnailWidth(mActivity),
2143 getDesiredThumbnailHeight(mActivity),
2144 new ValueCallback<Bitmap>() {
2145 @Override
2146 public void onReceiveValue(Bitmap bitmap) {
2147 new SaveSnapshotTask(source, bitmap).execute();
2148 }
2149 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002150 break;
2151
Michael Kolb8233fac2010-10-26 16:08:53 -07002152 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002153 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 break;
2155
John Recke1a03a32011-09-14 17:04:16 -07002156 case R.id.snapshot_go_live:
2157 goLive();
2158 return true;
2159
Michael Kolb8233fac2010-10-26 16:08:53 -07002160 case R.id.share_page_menu_id:
2161 Tab currentTab = mTabControl.getCurrentTab();
2162 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002163 return false;
2164 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002165 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002166 break;
2167
2168 case R.id.dump_nav_menu_id:
2169 getCurrentTopWebView().debugDump();
2170 break;
2171
Michael Kolb8233fac2010-10-26 16:08:53 -07002172 case R.id.zoom_in_menu_id:
2173 getCurrentTopWebView().zoomIn();
2174 break;
2175
2176 case R.id.zoom_out_menu_id:
2177 getCurrentTopWebView().zoomOut();
2178 break;
2179
2180 case R.id.view_downloads_menu_id:
2181 viewDownloads();
2182 break;
2183
John Reck42229bc2011-08-19 13:26:43 -07002184 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002185 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002186 break;
2187
Michael Kolb8233fac2010-10-26 16:08:53 -07002188 case R.id.window_one_menu_id:
2189 case R.id.window_two_menu_id:
2190 case R.id.window_three_menu_id:
2191 case R.id.window_four_menu_id:
2192 case R.id.window_five_menu_id:
2193 case R.id.window_six_menu_id:
2194 case R.id.window_seven_menu_id:
2195 case R.id.window_eight_menu_id:
2196 {
2197 int menuid = item.getItemId();
2198 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2199 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2200 Tab desiredTab = mTabControl.getTab(id);
2201 if (desiredTab != null &&
2202 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002203 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002204 }
2205 break;
2206 }
2207 }
2208 }
2209 break;
2210
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002211 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002212 Bundle bundle = new Bundle();
2213 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2214 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2215 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2216 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2217 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002218 break;
2219
Pankaj Garg49b79252014-11-07 17:33:41 -08002220 case R.id.add_to_homescreen:
2221 final WebView w = getCurrentTopWebView();
2222 final EditText input = new EditText(getContext());
2223 input.setText(w.getTitle());
2224 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002225 .setTitle(getContext().getResources().getString(
2226 R.string.add_to_homescreen))
2227 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002228 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002229 .setPositiveButton(getContext().getResources().getString(
2230 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002231 public void onClick(DialogInterface dialog, int whichButton) {
2232 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2233 getContext(),
2234 w.getUrl(),
2235 input.getText().toString(),
2236 w.getViewportBitmap(),
2237 w.getFavicon()));
2238
2239 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2240 .addCategory(Intent.CATEGORY_HOME));
2241 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002242 .setNegativeButton(getContext().getResources().getString(
2243 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002244 public void onClick(DialogInterface dialog, int whichButton) {
2245 // Do nothing.
2246 }
2247 })
2248 .show();
2249 break;
2250
Michael Kolb8233fac2010-10-26 16:08:53 -07002251 default:
2252 return false;
2253 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002254 return true;
2255 }
2256
John Reck68234a92012-04-19 15:27:12 -07002257 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2258 implements OnCancelListener {
2259
2260 private Tab mTab;
2261 private Dialog mProgressDialog;
2262 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002263 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002264
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002265 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002266 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002267 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002268 }
2269
2270 @Override
2271 protected void onPreExecute() {
2272 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2273 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2274 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002275 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002276 }
2277
2278 @Override
2279 protected Long doInBackground(Void... params) {
2280 if (!mTab.saveViewState(mValues)) {
2281 return null;
2282 }
2283 if (isCancelled()) {
2284 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2285 File file = mActivity.getFileStreamPath(path);
2286 if (!file.delete()) {
2287 file.deleteOnExit();
2288 }
2289 return null;
2290 }
2291 final ContentResolver cr = mActivity.getContentResolver();
2292 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2293 if (result == null) {
2294 return null;
2295 }
2296 long id = ContentUris.parseId(result);
2297 return id;
2298 }
2299
2300 @Override
2301 protected void onPostExecute(Long id) {
2302 if (isCancelled()) {
2303 return;
2304 }
2305 mProgressDialog.dismiss();
2306 if (id == null) {
2307 Toast.makeText(mActivity, R.string.snapshot_failed,
2308 Toast.LENGTH_SHORT).show();
2309 return;
2310 }
2311 Bundle b = new Bundle();
2312 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2313 mUi.showComboView(ComboViews.Snapshots, b);
2314 }
2315
2316 @Override
2317 public void onCancel(DialogInterface dialog) {
2318 cancel(true);
2319 }
2320 }
2321
Michael Kolb80f75082012-04-10 10:50:06 -07002322 @Override
2323 public void toggleUserAgent() {
2324 WebView web = getCurrentWebView();
2325 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002326 }
2327
2328 @Override
2329 public void findOnPage() {
2330 getCurrentTopWebView().showFindDialog(null, true);
2331 }
2332
2333 @Override
2334 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002335 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002336 }
2337
Pankaj Gargeba076f2015-03-25 13:40:59 -07002338 // This function is specifically used from AddBookmark Activity.
2339 // The bookmark activity clears the bitmap after retrieving it.
2340 // The function usage elsewhere will result in breaking bookmark
2341 // functionality.
2342 public static Bitmap getAndReleaseLastBookmarkBitmapFromIntent() {
2343 Bitmap bitmap = mBookmarkBitmap;
2344 mBookmarkBitmap = null;
2345 return bitmap;
2346 }
2347
Michael Kolb80f75082012-04-10 10:50:06 -07002348 @Override
2349 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002350 WebView w = getCurrentTopWebView();
2351 if (w == null)
2352 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002353 final Intent i = createBookmarkCurrentPageIntent(false);
2354 createScreenshotAsync(
2355 w, getDesiredThumbnailWidth(mActivity),
2356 getDesiredThumbnailHeight(mActivity),
2357 new ValueCallback<Bitmap>() {
2358 @Override
2359 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargeba076f2015-03-25 13:40:59 -07002360 mBookmarkBitmap = bitmap;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002361 mActivity.startActivity(i);
2362 }
2363 });
Michael Kolb80f75082012-04-10 10:50:06 -07002364 }
2365
John Recke1a03a32011-09-14 17:04:16 -07002366 private void goLive() {
Axesh R. Ajmera89cf4d82015-05-19 11:26:18 -07002367 if (!getCurrentTab().isSnapshot()) return;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002368 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002369 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002370 boolean onlySingleTabRemaining = false;
2371 if (mTabControl.getTabCount() > 1) {
2372 // destroy the old snapshot tab
2373 closeCurrentTab();
2374 } else {
2375 onlySingleTabRemaining = true;
2376 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002377 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002378 if (onlySingleTabRemaining) {
2379 closeTab(t);
2380 }
2381
Tarun Nainani8eb00912014-07-17 12:28:32 -07002382 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002383 }
2384
luxiaolb40014b2013-07-19 10:01:43 +08002385 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002386 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002387 new AlertDialog.Builder(activity)
2388 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002389 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002390 .setMessage(R.string.exit_browser_msg)
2391 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2392 public void onClick(DialogInterface dialog, int which) {
2393 activity.moveTaskToBack(true);
2394 dialog.dismiss();
2395 }
2396 })
2397 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2398 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002399 mCrashRecoveryHandler.clearState(true);
2400 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002401 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002402 dialog.dismiss();
2403 }
2404 })
2405 .show();
2406 }
Michael Kolb315d5022011-10-13 12:47:11 -07002407 @Override
2408 public void showPageInfo() {
2409 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2410 }
2411
John Reck9c35b9c2012-05-30 10:08:50 -07002412 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002413 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002414 // Let the History and Bookmark fragments handle menus they created.
2415 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2416 return false;
2417 }
2418
Michael Kolb8233fac2010-10-26 16:08:53 -07002419 int id = item.getItemId();
2420 boolean result = true;
2421 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 // -- Browser context menu
2423 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002424 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002425 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002426 case R.id.copy_link_context_menu_id:
2427 final WebView webView = getCurrentTopWebView();
2428 if (null == webView) {
2429 result = false;
2430 break;
2431 }
2432 final HashMap<String, WebView> hrefMap =
2433 new HashMap<String, WebView>();
2434 hrefMap.put("webview", webView);
2435 final Message msg = mHandler.obtainMessage(
2436 FOCUS_NODE_HREF, id, 0, hrefMap);
2437 webView.requestFocusNodeHref(msg);
2438 break;
2439
2440 default:
2441 // For other context menus
2442 result = onOptionsItemSelected(item);
2443 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002444 return result;
2445 }
2446
2447 /**
2448 * support programmatically opening the context menu
2449 */
2450 public void openContextMenu(View view) {
2451 mActivity.openContextMenu(view);
2452 }
2453
2454 /**
2455 * programmatically open the options menu
2456 */
2457 public void openOptionsMenu() {
2458 mActivity.openOptionsMenu();
2459 }
2460
John Reck9c35b9c2012-05-30 10:08:50 -07002461 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002462 public boolean onMenuOpened(int featureId, Menu menu) {
2463 if (mOptionsMenuOpen) {
2464 if (mConfigChanged) {
2465 // We do not need to make any changes to the state of the
2466 // title bar, since the only thing that happened was a
2467 // change in orientation
2468 mConfigChanged = false;
2469 } else {
2470 if (!mExtendedMenuOpen) {
2471 mExtendedMenuOpen = true;
2472 mUi.onExtendedMenuOpened();
2473 } else {
2474 // Switching the menu back to icon view, so show the
2475 // title bar once again.
2476 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002477 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002478 }
2479 }
2480 } else {
2481 // The options menu is closed, so open it, and show the title
2482 mOptionsMenuOpen = true;
2483 mConfigChanged = false;
2484 mExtendedMenuOpen = false;
2485 mUi.onOptionsMenuOpened();
2486 }
2487 return true;
2488 }
2489
John Reck9c35b9c2012-05-30 10:08:50 -07002490 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002491 public void onOptionsMenuClosed(Menu menu) {
2492 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002493 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002494 }
2495
John Reck9c35b9c2012-05-30 10:08:50 -07002496 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002498 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002499 }
2500
2501 // Helper method for getting the top window.
2502 @Override
2503 public WebView getCurrentTopWebView() {
2504 return mTabControl.getCurrentTopWebView();
2505 }
2506
2507 @Override
2508 public WebView getCurrentWebView() {
2509 return mTabControl.getCurrentWebView();
2510 }
2511
2512 /*
2513 * This method is called as a result of the user selecting the options
2514 * menu to see the download window. It shows the download window on top of
2515 * the current window.
2516 */
2517 void viewDownloads() {
2518 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2519 mActivity.startActivity(intent);
2520 }
2521
John Reck30b065e2011-07-19 10:58:05 -07002522 int getActionModeHeight() {
2523 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2524 new int[] { android.R.attr.actionBarSize });
2525 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2526 actionBarSizeTypedArray.recycle();
2527 return size;
2528 }
2529
Michael Kolb8233fac2010-10-26 16:08:53 -07002530 // action mode
2531
John Reck9c35b9c2012-05-30 10:08:50 -07002532 @Override
2533 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002534 mUi.onActionModeStarted(mode);
2535 mActionMode = mode;
2536 }
2537
2538 /*
2539 * True if a custom ActionMode (i.e. find or select) is in use.
2540 */
2541 @Override
2542 public boolean isInCustomActionMode() {
2543 return mActionMode != null;
2544 }
2545
2546 /*
2547 * End the current ActionMode.
2548 */
2549 @Override
2550 public void endActionMode() {
2551 if (mActionMode != null) {
2552 mActionMode.finish();
2553 }
2554 }
2555
2556 /*
2557 * Called by find and select when they are finished. Replace title bars
2558 * as necessary.
2559 */
John Reck9c35b9c2012-05-30 10:08:50 -07002560 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002561 public void onActionModeFinished(ActionMode mode) {
2562 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002563 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002564 mActionMode = null;
2565 }
2566
2567 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002568 final Tab tab = getCurrentTab();
2569 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002570 }
2571
2572 // bookmark handling
2573
2574 /**
2575 * add the current page as a bookmark to the given folder id
2576 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002577 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002578 * bookmarked, and if it is, edit that bookmark. If false, and
2579 * the site is already bookmarked, do not attempt to edit the
2580 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002581 */
2582 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002583 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002584 WebView w = getCurrentTopWebView();
2585 if (w == null) {
2586 return null;
2587 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 Intent i = new Intent(mActivity,
2589 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002590 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2591 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2592 String touchIconUrl = w.getTouchIconUrl();
2593 if (touchIconUrl != null) {
2594 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2595 WebSettings settings = w.getSettings();
2596 if (settings != null) {
2597 i.putExtra(AddBookmarkPage.USER_AGENT,
2598 settings.getUserAgentString());
2599 }
2600 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002601 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002603 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002604 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2605 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 // Put the dialog at the upper right of the screen, covering the
2607 // star on the title bar.
2608 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002609 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002610 }
2611
2612 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002613 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002614 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002615 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002616 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002617 }
2618
Vivek Sekharb54614f2014-05-01 19:03:37 -07002619 @Override
2620 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2621 boolean capture) {
2622 mUploadHandler = new UploadHandler(this);
2623 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2624 }
2625
Michael Kolb8233fac2010-10-26 16:08:53 -07002626 // thumbnails
2627
2628 /**
2629 * Return the desired width for thumbnail screenshots, which are stored in
2630 * the database, and used on the bookmarks screen.
2631 * @param context Context for finding out the density of the screen.
2632 * @return desired width for thumbnail screenshot.
2633 */
2634 static int getDesiredThumbnailWidth(Context context) {
2635 return context.getResources().getDimensionPixelOffset(
2636 R.dimen.bookmarkThumbnailWidth);
2637 }
2638
2639 /**
2640 * Return the desired height for thumbnail screenshots, which are stored in
2641 * the database, and used on the bookmarks screen.
2642 * @param context Context for finding out the density of the screen.
2643 * @return desired height for thumbnail screenshot.
2644 */
2645 static int getDesiredThumbnailHeight(Context context) {
2646 return context.getResources().getDimensionPixelOffset(
2647 R.dimen.bookmarkThumbnailHeight);
2648 }
2649
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002650 static void createScreenshotAsync(WebView view, int width, int height,
2651 final ValueCallback<Bitmap> cb) {
2652 if (view == null || width == 0 || height == 0) {
2653 return;
2654 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002655 view.getContentBitmapAsync(
2656 (float) width / view.getWidth(),
2657 new Rect(),
2658 new ValueCallback<Bitmap>() {
2659 @Override
2660 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002661 if (bitmap != null)
2662 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2663 cb.onReceiveValue(bitmap);
2664 }});
2665 }
2666
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002667 private void updateScreenshot(final Tab tab) {
2668 createScreenshotAsync(
2669 tab.getWebView(),
2670 getDesiredThumbnailWidth(mActivity),
2671 getDesiredThumbnailHeight(mActivity),
2672 new ValueCallback<Bitmap>() {
2673 @Override
2674 public void onReceiveValue(Bitmap bitmap) {
2675 updateScreenshot(tab, bitmap);
2676 }
2677 });
2678 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002679
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002680 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002681 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002682 // FIXME: Would like to make sure there is actually something to
2683 // draw, but the API for that (WebViewCore.pictureReady()) is not
2684 // currently accessible here.
2685
John Reck34ef2672011-02-10 11:30:55 -08002686 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002687 if (view == null) {
2688 // Tab was destroyed
2689 return;
2690 }
John Reck34ef2672011-02-10 11:30:55 -08002691 final String url = tab.getUrl();
2692 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002693 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002694 if (TextUtils.isEmpty(url)) {
2695 return;
2696 }
2697
kaiyiz6e5b3e02013-08-19 20:02:01 +08002698 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002699 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002700 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002701 }
John Reck34ef2672011-02-10 11:30:55 -08002702 // Only update thumbnails for web urls (http(s)://), not for
2703 // about:, javascript:, data:, etc...
2704 // Unless it is a bookmarked site, then always update
2705 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2706 return;
2707 }
2708
kaiyiz6e5b3e02013-08-19 20:02:01 +08002709 if (url != null && mUpdateMyNavThumbnailUrl != null
2710 && Patterns.WEB_URL.matcher(url).matches()
2711 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2712 String urlHost = (new WebAddress(url)).getHost();
2713 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2714 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2715 || bookmarkHost.length() == 0) {
2716 return;
2717 }
2718 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2719 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2720 bookmarkHost.length());
2721 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2722 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2723 if (!bookmarkDomain.equals(urlDomain)) {
2724 return;
2725 }
2726 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002727 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002728 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2729 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2730 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2731 500);
2732 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002733 return;
2734 }
2735
2736 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002737 new AsyncTask<Void, Void, Void>() {
2738 @Override
2739 protected Void doInBackground(Void... unused) {
2740 Cursor cursor = null;
2741 try {
2742 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002743 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2744 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2745 : url);
2746 if (mUpdateMyNavThumbnail) {
2747 mUpdateMyNavThumbnail = false;
2748 mUpdateMyNavThumbnailUrl = null;
2749 }
John Reck34ef2672011-02-10 11:30:55 -08002750 if (cursor != null && cursor.moveToFirst()) {
2751 final ByteArrayOutputStream os =
2752 new ByteArrayOutputStream();
2753 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002754
John Reck34ef2672011-02-10 11:30:55 -08002755 ContentValues values = new ContentValues();
2756 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002757
John Reck34ef2672011-02-10 11:30:55 -08002758 do {
John Reck617fd832011-02-16 14:35:59 -08002759 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002760 cr.update(Images.CONTENT_URI, values, null, null);
2761 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002762 }
John Reck34ef2672011-02-10 11:30:55 -08002763 } catch (IllegalStateException e) {
2764 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002765 } catch (SQLiteException s) {
2766 // Added for possible error when user tries to remove the same bookmark
2767 // that is being updated with a screen shot
2768 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002769 } finally {
2770 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002771 }
John Reck34ef2672011-02-10 11:30:55 -08002772 return null;
2773 }
2774 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002775 }
2776
2777 private class Copy implements OnMenuItemClickListener {
2778 private CharSequence mText;
2779
John Reck9c35b9c2012-05-30 10:08:50 -07002780 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002781 public boolean onMenuItemClick(MenuItem item) {
2782 copy(mText);
2783 return true;
2784 }
2785
2786 public Copy(CharSequence toCopy) {
2787 mText = toCopy;
2788 }
2789 }
2790
Leon Scroggins63c02662010-11-18 15:16:27 -05002791 private static class Download implements OnMenuItemClickListener {
2792 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002793 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002794 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002795 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002796 private static final String FALLBACK_EXTENSION = "dat";
2797 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002798
John Reck9c35b9c2012-05-30 10:08:50 -07002799 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002800 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002801 if (DataUri.isDataUri(mText)) {
2802 saveDataUri();
2803 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002804 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002805 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002806 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002807 return true;
2808 }
2809
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002810 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2811 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002812 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002813 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002814 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002815 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002816 }
George Mount387d45d2011-10-07 15:57:53 -07002817
2818 /**
2819 * Treats mText as a data URI and writes its contents to a file
2820 * based on the current time.
2821 */
2822 private void saveDataUri() {
2823 FileOutputStream outputStream = null;
2824 try {
2825 DataUri uri = new DataUri(mText);
2826 File target = getTarget(uri);
2827 outputStream = new FileOutputStream(target);
2828 outputStream.write(uri.getData());
2829 final DownloadManager manager =
2830 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2831 manager.addCompletedDownload(target.getName(),
2832 mActivity.getTitle().toString(), false,
2833 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002834 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002835 } catch (IOException e) {
2836 Log.e(LOGTAG, "Could not save data URL");
2837 } finally {
2838 if (outputStream != null) {
2839 try {
2840 outputStream.close();
2841 } catch (IOException e) {
2842 // ignore close errors
2843 }
2844 }
2845 }
2846 }
2847
2848 /**
2849 * Creates a File based on the current time stamp and uses
2850 * the mime type of the DataUri to get the extension.
2851 */
2852 private File getTarget(DataUri uri) throws IOException {
2853 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002854 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002855 String nameBase = format.format(new Date());
2856 String mimeType = uri.getMimeType();
2857 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2858 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2859 if (extension == null) {
2860 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2861 extension = FALLBACK_EXTENSION;
2862 }
2863 extension = "." + extension; // createTempFile needs the '.'
2864 File targetFile = File.createTempFile(nameBase, extension, dir);
2865 return targetFile;
2866 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002867 }
2868
Cary Clark8974d282010-11-22 10:46:05 -05002869 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002870 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002871
John Reck9c35b9c2012-05-30 10:08:50 -07002872 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002873 public boolean onMenuItemClick(MenuItem item) {
2874 if (mWebView != null) {
2875 return mWebView.selectText();
2876 }
2877 return false;
2878 }
2879
2880 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002881 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002882 }
2883
2884 }
2885
Michael Kolb8233fac2010-10-26 16:08:53 -07002886 /********************** TODO: UI stuff *****************************/
2887
2888 // these methods have been copied, they still need to be cleaned up
2889
2890 /****************** tabs ***************************************************/
2891
2892 // basic tab interactions:
2893
2894 // it is assumed that tabcontrol already knows about the tab
2895 protected void addTab(Tab tab) {
2896 mUi.addTab(tab);
2897 }
2898
2899 protected void removeTab(Tab tab) {
2900 mUi.removeTab(tab);
2901 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002902 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002903 }
2904
Michael Kolbf2055602011-04-09 17:20:03 -07002905 @Override
2906 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002907 // monkey protection against delayed start
2908 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002909
2910 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2911 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002912 mTabControl.setCurrentTab(tab);
2913 // the tab is guaranteed to have a webview after setCurrentTab
2914 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002915
2916
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002917 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002918 //Purge active tabs
2919 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002920 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002921 }
2922
John Reck8bcafc12011-08-29 16:43:02 -07002923 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002924 Tab current = mTabControl.getCurrentTab();
2925 if (current != null
2926 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002927 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002928 }
2929 }
2930
John Reck26b18322011-06-21 13:08:58 -07002931 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002932 //Cancel navscreen request
2933 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002934 // Dismiss the subwindow if applicable.
2935 dismissSubWindow(appTab);
2936 // Since we might kill the WebView, remove it from the
2937 // content view first.
2938 mUi.detachTab(appTab);
2939 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002940 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002941 // TODO: analyze why the remove and add are necessary
2942 mUi.attachTab(appTab);
2943 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002944 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002945 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002946 } else {
2947 // If the tab was the current tab, we have to attach
2948 // it to the view system again.
2949 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002950 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002951 }
2952 }
2953
2954 // Remove the sub window if it exists. Also called by TabControl when the
2955 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002956 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002957 public void dismissSubWindow(Tab tab) {
2958 removeSubWindow(tab);
2959 // dismiss the subwindow. This will destroy the WebView.
2960 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002961 WebView wv = getCurrentTopWebView();
2962 if (wv != null) {
2963 wv.requestFocus();
2964 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002965 }
2966
2967 @Override
2968 public void removeSubWindow(Tab t) {
2969 if (t.getSubWebView() != null) {
2970 mUi.removeSubWindow(t.getSubViewContainer());
2971 }
2972 }
2973
2974 @Override
2975 public void attachSubWindow(Tab tab) {
2976 if (tab.getSubWebView() != null) {
2977 mUi.attachSubWindow(tab.getSubViewContainer());
2978 getCurrentTopWebView().requestFocus();
2979 }
2980 }
2981
Mathew Inwood29721c22011-06-29 17:55:24 +01002982 private Tab showPreloadedTab(final UrlData urlData) {
2983 if (!urlData.isPreloaded()) {
2984 return null;
2985 }
2986 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2987 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2988 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002989 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002990 // Could not submit query. Fallback to regular tab creation
2991 tabControl.destroy();
2992 return null;
2993 }
2994 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002995 // check tab count and make room for new tab
2996 if (!mTabControl.canCreateNewTab()) {
2997 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2998 if (leastUsed != null) {
2999 closeTab(leastUsed);
3000 }
3001 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003002 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01003003 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01003004 mTabControl.addPreloadedTab(t);
3005 addTab(t);
3006 setActiveTab(t);
3007 return t;
3008 }
3009
Michael Kolb7bcafde2011-05-09 13:55:59 -07003010 // open a non inconito tab with the given url data
3011 // and set as active tab
3012 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01003013 Tab tab = showPreloadedTab(urlData);
3014 if (tab == null) {
3015 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07003016 if ((tab != null) && !urlData.isEmpty()) {
3017 loadUrlDataIn(tab, urlData);
3018 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003019 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003020 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07003021 }
3022
Michael Kolb843510f2010-12-09 10:51:49 -08003023 @Override
3024 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003025 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07003026 }
3027
Michael Kolb8233fac2010-10-26 16:08:53 -07003028 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07003029 public Tab openIncognitoTab() {
3030 return openTab(INCOGNITO_URI, true, true, false);
3031 }
3032
3033 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003034 public Tab openTab(String url, boolean incognito, boolean setActive,
3035 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003036 return openTab(url, incognito, setActive, useCurrent, null);
3037 }
3038
3039 @Override
3040 public Tab openTab(String url, Tab parent, boolean setActive,
3041 boolean useCurrent) {
3042 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3043 setActive, useCurrent, parent);
3044 }
3045
3046 public Tab openTab(String url, boolean incognito, boolean setActive,
3047 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003048 Tab tab = createNewTab(incognito, setActive, useCurrent);
3049 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003050 if (parent instanceof SnapshotTab) {
3051 addTab(tab);
3052 if (setActive)
3053 setActiveTab(tab);
3054 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003055 parent.addChildTab(tab);
3056 }
Michael Kolb519d2282011-05-09 17:03:19 -07003057 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003058 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003059 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003060 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003061 return tab;
3062 }
3063
3064 // this method will attempt to create a new tab
3065 // incognito: private browsing tab
3066 // setActive: ste tab as current tab
3067 // useCurrent: if no new tab can be created, return current tab
3068 private Tab createNewTab(boolean incognito, boolean setActive,
3069 boolean useCurrent) {
3070 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07003071 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
3072 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003073 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003074 if (mTabControl.canCreateNewTab()) {
3075 tab = mTabControl.createNewTab(incognito, !setActive);
3076 addTab(tab);
3077 if (setActive) {
3078 setActiveTab(tab);
3079 } else {
3080 tab.pause();
3081 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003082 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003083 if (useCurrent) {
3084 tab = mTabControl.getCurrentTab();
3085 reuseTab(tab, null);
3086 } else {
3087 mUi.showMaxTabsWarning();
3088 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003089 }
3090 }
3091 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003092 }
3093
John Reck2bc80422011-06-30 15:11:49 -07003094 @Override
3095 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3096 SnapshotTab tab = null;
3097 if (mTabControl.canCreateNewTab()) {
3098 tab = mTabControl.createSnapshotTab(snapshotId);
3099 addTab(tab);
3100 if (setActive) {
3101 setActiveTab(tab);
3102 }
3103 } else {
3104 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003105 }
3106 return tab;
3107 }
3108
Michael Kolb8233fac2010-10-26 16:08:53 -07003109 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003110 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003111 * @return boolean True if we successfully switched to a different tab. If
3112 * the indexth tab is null, or if that tab is the same as
3113 * the current one, return false.
3114 */
3115 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003116 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003117 Tab currentTab = mTabControl.getCurrentTab();
3118 if (tab == null || tab == currentTab) {
3119 return false;
3120 }
3121 setActiveTab(tab);
3122 return true;
3123 }
3124
3125 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003126 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003127 closeCurrentTab(false);
3128 }
3129
3130 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003131 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003132 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003133 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003134 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003135 return;
3136 }
Michael Kolbc831b632011-05-11 09:30:34 -07003137 final Tab current = mTabControl.getCurrentTab();
3138 final int pos = mTabControl.getCurrentPosition();
3139 Tab newTab = current.getParent();
3140 if (newTab == null) {
3141 newTab = mTabControl.getTab(pos + 1);
3142 if (newTab == null) {
3143 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003144 }
3145 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003146 if (andQuit) {
3147 mTabControl.setCurrentTab(newTab);
3148 closeTab(current);
3149 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003150 // Close window
3151 closeTab(current);
3152 }
3153 }
3154
3155 /**
3156 * Close the tab, remove its associated title bar, and adjust mTabControl's
3157 * current tab to a valid value.
3158 */
3159 @Override
3160 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003161 if (tab == mTabControl.getCurrentTab()) {
3162 closeCurrentTab();
3163 } else {
3164 removeTab(tab);
3165 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003166 }
3167
Afzal Najamd4e33312012-04-26 01:54:01 -04003168 /**
3169 * Close all tabs except the current one
3170 */
3171 @Override
3172 public void closeOtherTabs() {
3173 int inactiveTabs = mTabControl.getTabCount() - 1;
3174 for (int i = inactiveTabs; i >= 0; i--) {
3175 Tab tab = mTabControl.getTab(i);
3176 if (tab != mTabControl.getCurrentTab()) {
3177 removeTab(tab);
3178 }
3179 }
3180 }
3181
Michael Kolb8233fac2010-10-26 16:08:53 -07003182 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003183 protected void loadUrlFromContext(String url) {
3184 Tab tab = getCurrentTab();
3185 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003186 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003187 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003188 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003189 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003190 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003191 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003192 }
3193 }
3194 }
3195
3196 /**
3197 * Load the URL into the given WebView and update the title bar
3198 * to reflect the new load. Call this instead of WebView.loadUrl
3199 * directly.
3200 * @param view The WebView used to load url.
3201 * @param url The URL to load.
3202 */
John Reck71e51422011-07-01 16:49:28 -07003203 @Override
3204 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003205 loadUrl(tab, url, null);
3206 }
3207
3208 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3209 if (tab != null) {
3210 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003211 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003212 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003213 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003214 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003215 }
3216
3217 /**
3218 * Load UrlData into a Tab and update the title bar to reflect the new
3219 * load. Call this instead of UrlData.loadIn directly.
3220 * @param t The Tab used to load.
3221 * @param data The UrlData being loaded.
3222 */
3223 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003224 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003225 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003226 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003227 } else {
John Reck38b39652012-06-05 09:22:59 -07003228 if (t != null && data.mDisableUrlOverride) {
3229 t.disableUrlOverridingForLoad();
3230 }
John Reck26b18322011-06-21 13:08:58 -07003231 loadUrl(t, data.mUrl, data.mHeaders);
3232 }
3233 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003234 }
3235
John Reck30c714c2010-12-16 17:30:34 -08003236 @Override
3237 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003238 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003239 //In case of tab can go back (aka tab has navigation entry) do nothing
3240 //else just load homepage in current tab.
3241 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003242 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003243 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003244 }
3245
3246 void goBackOnePageOrQuit() {
3247 Tab current = mTabControl.getCurrentTab();
3248 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003249 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3250 showExitDialog(mActivity);
3251 } else {
3252 /*
3253 * Instead of finishing the activity, simply push this to the back
3254 * of the stack and let ActivityManager to choose the foreground
3255 * activity. As BrowserActivity is singleTask, it will be always the
3256 * root of the task. So we can use either true or false for
3257 * moveTaskToBack().
3258 */
3259 mActivity.moveTaskToBack(true);
3260 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003261 return;
3262 }
John Reckef654f12011-07-12 16:42:08 -07003263 if (current.canGoBack()) {
3264 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003265 } else {
3266 // Check to see if we are closing a window that was created by
3267 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003268 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003269 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003270 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003271 // Now we close the other tab
3272 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003273 } else if (BrowserConfig.getInstance(getContext())
3274 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3275 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003276 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003277 if ((current.getAppId() != null) || current.closeOnBack()) {
3278 closeCurrentTab(true);
3279 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003280 /*
3281 * Instead of finishing the activity, simply push this to the back
3282 * of the stack and let ActivityManager to choose the foreground
3283 * activity. As BrowserActivity is singleTask, it will be always the
3284 * root of the task. So we can use either true or false for
3285 * moveTaskToBack().
3286 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003287 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003288 }
3289 }
3290 }
3291
3292 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003293 * helper method for key handler
3294 * returns the current tab if it can't advance
3295 */
Michael Kolbc831b632011-05-11 09:30:34 -07003296 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003297 int pos = mTabControl.getCurrentPosition() + 1;
3298 if (pos >= mTabControl.getTabCount()) {
3299 pos = 0;
3300 }
3301 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003302 }
3303
3304 /**
3305 * helper method for key handler
3306 * returns the current tab if it can't advance
3307 */
Michael Kolbc831b632011-05-11 09:30:34 -07003308 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003309 int pos = mTabControl.getCurrentPosition() - 1;
3310 if ( pos < 0) {
3311 pos = mTabControl.getTabCount() - 1;
3312 }
3313 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003314 }
3315
John Reckbcef87f2012-02-03 14:58:34 -08003316 boolean isMenuOrCtrlKey(int keyCode) {
3317 return (KeyEvent.KEYCODE_MENU == keyCode)
3318 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3319 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3320 }
3321
Michael Kolb0035fad2011-03-14 13:25:28 -07003322 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003323 * handle key events in browser
3324 *
3325 * @param keyCode
3326 * @param event
3327 * @return true if handled, false to pass to super
3328 */
John Reck9c35b9c2012-05-30 10:08:50 -07003329 @Override
3330 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003331 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3332 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003333 if (!mUi.isComboViewShowing()) {
3334 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3335 true, false);
3336 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003337 return true;
3338 }
3339
Cary Clark160bbb92011-01-10 11:17:07 -05003340 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003341 // Even if MENU is already held down, we need to call to super to open
3342 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003343 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003344 mMenuIsDown = true;
3345 return false;
3346 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003347
Cary Clark8ff8c662010-12-29 15:03:05 -05003348 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003349 Tab tab = getCurrentTab();
3350 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003351
Cary Clark160bbb92011-01-10 11:17:07 -05003352 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3353 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003354
Michael Kolb8233fac2010-10-26 16:08:53 -07003355 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003356 case KeyEvent.KEYCODE_TAB:
3357 if (event.isCtrlPressed()) {
3358 if (event.isShiftPressed()) {
3359 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003360 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003361 } else {
3362 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003363 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003364 }
3365 return true;
3366 }
3367 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003368 case KeyEvent.KEYCODE_SPACE:
3369 // WebView/WebTextView handle the keys in the KeyDown. As
3370 // the Activity's shortcut keys are only handled when WebView
3371 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003372 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003373 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003374 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003375 pageDown();
3376 }
3377 return true;
3378 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003379 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003380 event.startTracking();
3381 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003382 case KeyEvent.KEYCODE_FORWARD:
3383 if (!noModifiers) break;
3384 tab.goForward();
3385 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003386 case KeyEvent.KEYCODE_DPAD_LEFT:
3387 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003388 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003389 return true;
3390 }
3391 break;
3392 case KeyEvent.KEYCODE_DPAD_RIGHT:
3393 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003394 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003395 return true;
3396 }
3397 break;
3398 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003399 if (ctrl) {
3400 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003401 return true;
3402 }
3403 break;
Michael Kolba4183062011-01-16 10:43:21 -08003404// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003405 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003406 if (ctrl ) {
3407 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003408 return true;
3409 }
3410 break;
Michael Kolba4183062011-01-16 10:43:21 -08003411// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003412// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003413// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003414// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003415// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003416// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003417// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003418// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003419// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003420// case KeyEvent.KEYCODE_M: // unused
3421// case KeyEvent.KEYCODE_N: // in Chrome: new window
3422// case KeyEvent.KEYCODE_O: // in Chrome: open file
3423// case KeyEvent.KEYCODE_P: // in Chrome: print page
3424// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003425// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003426// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3427 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003428 // we can't use the ctrl/shift flags, they check for
3429 // exclusive use of a modifier
3430 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003431 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003432 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003433 } else {
3434 openTabToHomePage();
3435 }
3436 return true;
3437 }
3438 break;
3439// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3440// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003441// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003442// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3443// case KeyEvent.KEYCODE_Y: // unused
3444// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003445 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003446 // it is a regular key and webview is not null
3447 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003448 }
3449
John Reck9c35b9c2012-05-30 10:08:50 -07003450 @Override
3451 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003452 switch(keyCode) {
3453 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003454 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003455 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003456 return true;
3457 }
3458 break;
3459 }
3460 return false;
3461 }
3462
John Reck9c35b9c2012-05-30 10:08:50 -07003463 @Override
3464 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003465 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003466 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003467 if (KeyEvent.KEYCODE_MENU == keyCode
3468 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003469 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003470 }
3471 }
Cary Clark160bbb92011-01-10 11:17:07 -05003472 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003473 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003474 case KeyEvent.KEYCODE_BACK:
3475 if (event.isTracking() && !event.isCanceled()) {
3476 onBackKey();
3477 return true;
3478 }
3479 break;
3480 }
3481 return false;
3482 }
3483
3484 public boolean isMenuDown() {
3485 return mMenuIsDown;
3486 }
3487
John Reck9c35b9c2012-05-30 10:08:50 -07003488 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003489 public void setupAutoFill(Message message) {
3490 // Open the settings activity at the AutoFill profile fragment so that
3491 // the user can create a new profile. When they return, we will dispatch
3492 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003493 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003494 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3495 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003496 }
John Reckb3417f02011-01-14 11:01:05 -08003497
John Reck9c35b9c2012-05-30 10:08:50 -07003498 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003499 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003500 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003501 return true;
3502 }
3503
John Reck1cf4b792011-07-26 10:22:22 -07003504 @Override
3505 public boolean shouldCaptureThumbnails() {
3506 return mUi.shouldCaptureThumbnails();
3507 }
3508
Michael Kolbc3af0672011-08-09 10:24:41 -07003509 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003510 public boolean supportsVoice() {
3511 PackageManager pm = mActivity.getPackageManager();
3512 List activities = pm.queryIntentActivities(new Intent(
3513 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3514 return activities.size() != 0;
3515 }
3516
3517 @Override
3518 public void startVoiceRecognizer() {
3519 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003520 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003521 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3522 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3523 mActivity.startActivityForResult(voice, VOICE_RESULT);
3524 }
3525
Pankaj Garg7b279f62014-08-12 14:47:18 -07003526 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003527 if (mLevel == level)
3528 return;
3529 mLevel = level;
3530 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003531 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3532 lp.dimAmount = level;
3533 mActivity.getWindow().setAttributes(lp);
3534 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3535 } else {
3536 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3537 }
3538 }
3539
Michael Kolb0b129122012-06-04 16:31:58 -07003540 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003541 public void setBlockEvents(boolean block) {
3542 mBlockEvents = block;
3543 }
3544
John Reck9c35b9c2012-05-30 10:08:50 -07003545 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003546 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003547 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003548 }
3549
John Reck9c35b9c2012-05-30 10:08:50 -07003550 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003551 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003552 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003553 }
3554
John Reck9c35b9c2012-05-30 10:08:50 -07003555 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003556 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003557 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003558 }
3559
John Reck9c35b9c2012-05-30 10:08:50 -07003560 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003561 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003562 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003563 }
3564
John Reck9c35b9c2012-05-30 10:08:50 -07003565 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003566 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003567 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003568 }
3569
Pankaj Garg49b79252014-11-07 17:33:41 -08003570 @Override
3571 public boolean shouldShowAppMenu() {
3572 return true;
3573 }
3574
3575 @Override
3576 public int getMenuThemeResourceId() {
3577 return R.style.OverflowMenuTheme;
3578 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003579}