blob: 2f8db527ab4b682b5cd395118c1611b6840a596d [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 Tharp9a082b12015-06-19 08:46:57 -0700101import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharpdcad7b02015-05-28 07:38:32 -0700102import com.android.browser.mdm.IncognitoRestriction;
Dave Tharp851e8d52015-04-26 14:58:18 -0700103import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700104import com.android.browser.mynavigation.AddMyNavigationPage;
105import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700106import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700107import com.android.browser.platformsupport.BrowserContract;
108import com.android.browser.platformsupport.WebAddress;
109import com.android.browser.platformsupport.BrowserContract.Images;
Pankaj Garg18902562014-12-05 16:18:51 -0800110import com.android.browser.preferences.AboutPreferencesFragment;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700111import com.android.browser.provider.BrowserProvider2.Thumbnails;
112import com.android.browser.provider.SnapshotProvider.Snapshots;
113import com.android.browser.reflect.ReflectHelper;
Pankaj Garg49b79252014-11-07 17:33:41 -0800114import com.android.browser.appmenu.AppMenuHandler;
115import com.android.browser.appmenu.AppMenuPropertiesDelegate;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700116
Michael Kolb8233fac2010-10-26 16:08:53 -0700117import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700118import java.io.File;
119import java.io.FileOutputStream;
120import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700121import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700122import java.text.DateFormat;
123import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700124import java.util.ArrayList;
George Mount387d45d2011-10-07 15:57:53 -0700125import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700126import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800127import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200128import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700129import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
131/**
132 * Controller for browser
133 */
134public class Controller
Pankaj Garg49b79252014-11-07 17:33:41 -0800135 implements WebViewController, UiController, ActivityController,
136 AppMenuPropertiesDelegate {
Michael Kolb8233fac2010-10-26 16:08:53 -0700137
138 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800139 private static final String SEND_APP_ID_EXTRA =
140 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Vivek Sekhared791da2015-02-22 12:39:05 -0800141 private static final String INCOGNITO_URI = "chrome://incognito";
Tarun Nainani87a86682015-02-05 11:47:35 -0800142 public static final String EXTRA_REQUEST_CODE = "_fake_request_code_";
143 public static final String EXTRA_RESULT_CODE = "_fake_result_code_";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800144
Vivek Sekharb54614f2014-05-01 19:03:37 -0700145 // Remind switch to data connection if wifi is unavailable
146 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800147
Michael Kolb8233fac2010-10-26 16:08:53 -0700148 // public message ids
149 public final static int LOAD_URL = 1001;
150 public final static int STOP_LOAD = 1002;
151
152 // Message Ids
153 private static final int FOCUS_NODE_HREF = 102;
154 private static final int RELEASE_WAKELOCK = 107;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800155 private static final int UNKNOWN_TYPE_MSG = 109;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156
157 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
158
159 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800160 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700161
162 private static final int EMPTY_MENU = -1;
163
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700165 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 final static int PREFERENCES_PAGE = 3;
167 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000168 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700169 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800170 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000171
Michael Kolb8233fac2010-10-26 16:08:53 -0700172 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
173
174 // As the ids are dynamically created, we can't guarantee that they will
175 // be in sequence, so this static array maps ids to a window number.
176 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
177 { R.id.window_one_menu_id, R.id.window_two_menu_id,
178 R.id.window_three_menu_id, R.id.window_four_menu_id,
179 R.id.window_five_menu_id, R.id.window_six_menu_id,
180 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
181
182 // "source" parameter for Google search through search key
183 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
184 // "source" parameter for Google search through simplily type
185 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
186
George Mount387d45d2011-10-07 15:57:53 -0700187 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700188 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
189
John Reckd7dd9b22011-08-30 09:18:29 -0700190 // A bitmap that is re-used in createScreenshot as scratch space
191 private static Bitmap sThumbnailBitmap;
192
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 private Activity mActivity;
194 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700195 private HomepageHandler mHomepageHandler;
196 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 private BrowserSettings mSettings;
198 private WebViewFactory mFactory;
199
200 private WakeLock mWakeLock;
201
202 private UrlHandler mUrlHandler;
203 private UploadHandler mUploadHandler;
204 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 private PageDialogsHandler mPageDialogsHandler;
206 private NetworkStateHandler mNetworkHandler;
207
Ben Murdoch8029a772010-11-16 11:58:21 +0000208 private Message mAutoFillSetupMessage;
209
kaiyiza016da12013-08-26 17:50:22 +0800210 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700211
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 // FIXME, temp address onPrepareMenu performance problem.
213 // When we move everything out of view, we should rewrite this.
214 private int mCurrentMenuState = 0;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700215 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700216 private int mOldMenuState = EMPTY_MENU;
217 private Menu mCachedMenu;
218
Michael Kolb8233fac2010-10-26 16:08:53 -0700219 private boolean mMenuIsDown;
220
Pankaj Garg49b79252014-11-07 17:33:41 -0800221 private boolean mWasInPageLoad = false;
222 private AppMenuHandler mAppMenuHandler;
223
Michael Kolb8233fac2010-10-26 16:08:53 -0700224 // For select and find, we keep track of the ActionMode so that
225 // finish() can be called as desired.
226 private ActionMode mActionMode;
227
228 /**
229 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
230 * of whether the configuration has changed. The first onMenuOpened call
231 * after a configuration change is simply a reopening of the same menu
232 * (i.e. mIconView did not change).
233 */
234 private boolean mConfigChanged;
235
236 /**
237 * Keeps track of whether the options menu is open. This is important in
238 * determining whether to show or hide the title bar overlay
239 */
240 private boolean mOptionsMenuOpen;
241
242 /**
243 * Whether or not the options menu is in its bigger, popup menu form. When
244 * true, we want the title bar overlay to be gone. When false, we do not.
245 * Only meaningful if mOptionsMenuOpen is true.
246 */
247 private boolean mExtendedMenuOpen;
248
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 private boolean mActivityPaused = true;
250 private boolean mLoadStopped;
251
252 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500253 // Checks to see when the bookmarks database has changed, and updates the
254 // Tabs' notion of whether they represent bookmarked sites.
255 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700256 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700257
Michael Kolbc3af0672011-08-09 10:24:41 -0700258 private boolean mBlockEvents;
259
Michael Kolb0b129122012-06-04 16:31:58 -0700260 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800261 private boolean mUpdateMyNavThumbnail;
262 private String mUpdateMyNavThumbnailUrl;
Vivek Sekharb991edb2014-12-17 18:18:07 -0800263 private float mLevel = 0.0f;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800264 private WebView.HitTestResult mResult;
Pankaj Gargeba076f2015-03-25 13:40:59 -0700265 private static Bitmap mBookmarkBitmap;
Site Maoabb7bd32015-03-20 15:34:02 -0700266 private PowerConnectionReceiver mLowPowerReceiver;
267 private PowerConnectionReceiver mPowerChangeReceiver;
Michael Kolb0b129122012-06-04 16:31:58 -0700268
George Mount3636d0a2011-11-21 09:08:21 -0800269 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 mActivity = browser;
271 mSettings = BrowserSettings.getInstance();
272 mTabControl = new TabControl(this);
273 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700274 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800275 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700276 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700277
278 mUrlHandler = new UrlHandler(this);
279 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
281
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500283 mBookmarksObserver = new ContentObserver(mHandler) {
284 @Override
285 public void onChange(boolean selfChange) {
286 int size = mTabControl.getTabCount();
287 for (int i = 0; i < size; i++) {
288 mTabControl.getTab(i).updateBookmarkedStatus();
289 }
290 }
291
292 };
293 browser.getContentResolver().registerContentObserver(
294 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700295
296 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700297 mHomepageHandler = new HomepageHandler(browser, this);
Pankaj Garg49b79252014-11-07 17:33:41 -0800298 mAppMenuHandler = new AppMenuHandler(browser, this, R.menu.browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700299 }
300
John Reck9c35b9c2012-05-30 10:08:50 -0700301 @Override
302 public void start(final Intent intent) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700303 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800304 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800305 // mCrashRecoverHandler has any previously saved state.
306 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700307 }
308
George Mount3636d0a2011-11-21 09:08:21 -0800309 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800310 // we dont want to ever recover incognito tabs
311 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700312
Patrick Scott7d50a932011-02-04 09:27:26 -0500313 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700314 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500315 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000316
Michael Kolbc831b632011-05-11 09:30:34 -0700317 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500318 // Not able to restore so we go ahead and clear session cookies. We
319 // must do this before trying to login the user as we don't want to
320 // clear any session cookies set during login.
321 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700322 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800323 if (intent == null) {
324 // This won't happen under common scenarios. The icicle is
325 // not null, but there aren't any tabs to restore.
326 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700327 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800328 final Bundle extra = intent.getExtras();
329 // Create an initial tab.
330 // If the intent is ACTION_VIEW and data is not null, the Browser is
331 // invoked to view the content by another application. In this case,
332 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800333 UrlData urlData = null;
334 if (intent.getData() != null
335 && Intent.ACTION_VIEW.equals(intent.getAction())
336 && intent.getData().toString().startsWith("content://")) {
337 urlData = new UrlData(intent.getData().toString());
338 } else {
339 urlData = IntentHandler.getUrlDataFromIntent(intent);
340 }
George Mount3636d0a2011-11-21 09:08:21 -0800341 Tab t = null;
342 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700343 String landingPage = mActivity.getResources().getString(
344 R.string.def_landing_page);
345 if (!landingPage.isEmpty()) {
346 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800347 } else {
348 t = openTabToHomePage();
349 }
George Mount3636d0a2011-11-21 09:08:21 -0800350 } else {
351 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700352 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800353 }
354 if (t != null) {
355 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
356 }
357 WebView webView = t.getWebView();
358 if (extra != null) {
359 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
360 if (scale > 0 && scale <= 1000) {
361 webView.setInitialScale(scale);
362 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 }
364 }
John Reckd8c74522011-06-14 08:45:00 -0700365 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700367 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500368 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700369 List<Tab> tabs = mTabControl.getTabs();
370 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700371
John Reck1cf4b792011-07-26 10:22:22 -0700372 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700373 //handle restored pages that may require a JS interface
374 if (t.getWebView() != null) {
375 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
376 if (backForwardList != null) {
377 for (int i = 0; i < backForwardList.getSize(); i++) {
378 WebHistoryItem item = backForwardList.getItemAtIndex(i);
379 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
380 }
381 }
382 }
John Reck1cf4b792011-07-26 10:22:22 -0700383 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700384 if (t != mTabControl.getCurrentTab()) {
385 t.pause();
386 }
John Reck1cf4b792011-07-26 10:22:22 -0700387 }
388 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700389 if (tabs.size() == 0) {
390 openTabToHomePage();
391 }
John Reck1cf4b792011-07-26 10:22:22 -0700392 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700393 // TabControl.restoreState() will create a new tab even if
394 // restoring the state fails.
395 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800396 // Intent is non-null when framework thinks the browser should be
397 // launching with a new intent (icicle is null).
398 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700399 mIntentHandler.onNewIntent(intent);
400 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700403 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800404 if (jsFlags.trim().length() != 0) {
405 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700406 }
George Mount3636d0a2011-11-21 09:08:21 -0800407 if (intent != null
408 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700409 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800410 }
Site Maoabb7bd32015-03-20 15:34:02 -0700411 mLowPowerReceiver = new PowerConnectionReceiver();
412 mPowerChangeReceiver = new PowerConnectionReceiver();
413
414 //always track the android framework's power save mode
415 IntentFilter filter = new IntentFilter();
416 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
417 // Power save mode only exists in Lollipop and above
418 filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
419 }
420 filter.addAction(Intent.ACTION_BATTERY_OKAY);
421 mActivity.registerReceiver(mPowerChangeReceiver, filter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700422 }
423
John Reck1cf4b792011-07-26 10:22:22 -0700424 private static class PruneThumbnails implements Runnable {
425 private Context mContext;
426 private List<Long> mIds;
427
428 PruneThumbnails(Context context, List<Long> preserveIds) {
429 mContext = context.getApplicationContext();
430 mIds = preserveIds;
431 }
432
433 @Override
434 public void run() {
435 ContentResolver cr = mContext.getContentResolver();
436 if (mIds == null || mIds.size() == 0) {
437 cr.delete(Thumbnails.CONTENT_URI, null, null);
438 } else {
439 int length = mIds.size();
440 StringBuilder where = new StringBuilder();
441 where.append(Thumbnails._ID);
442 where.append(" not in (");
443 for (int i = 0; i < length; i++) {
444 where.append(mIds.get(i));
445 if (i < (length - 1)) {
446 where.append(",");
447 }
448 }
449 where.append(")");
450 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
451 }
452 }
453
454 }
455
Michael Kolb1514bb72010-11-22 09:11:48 -0800456 @Override
457 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700458 return mFactory;
459 }
460
461 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800462 public void onSetWebView(Tab tab, WebView view) {
Vivek Sekharc70ae632015-04-08 17:54:05 -0700463 mUi.onSetWebView(tab, view);
Dave Tharp851e8d52015-04-26 14:58:18 -0700464 URLFilterRestriction.getInstance();
Michael Kolba713ec82010-11-29 17:27:06 -0800465 }
466
467 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800468 public void createSubWindow(Tab tab) {
469 endActionMode();
470 WebView mainView = tab.getWebView();
471 WebView subView = mFactory.createWebView((mainView == null)
472 ? false
473 : mainView.isPrivateBrowsingEnabled());
474 mUi.createSubWindow(tab, subView);
475 }
476
477 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700478 public Context getContext() {
479 return mActivity;
480 }
481
482 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700483 public Activity getActivity() {
484 return mActivity;
485 }
486
487 void setUi(UI ui) {
488 mUi = ui;
489 }
490
Michael Kolbaf63dba2012-05-16 12:58:05 -0700491 @Override
492 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 return mSettings;
494 }
495
496 IntentHandler getIntentHandler() {
497 return mIntentHandler;
498 }
499
500 @Override
501 public UI getUi() {
502 return mUi;
503 }
504
505 int getMaxTabs() {
506 return mActivity.getResources().getInteger(R.integer.max_tabs);
507 }
508
509 @Override
510 public TabControl getTabControl() {
511 return mTabControl;
512 }
513
Michael Kolb1bf23132010-11-19 12:55:12 -0800514 @Override
515 public List<Tab> getTabs() {
516 return mTabControl.getTabs();
517 }
518
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 private void startHandler() {
520 mHandler = new Handler() {
521
522 @Override
523 public void handleMessage(Message msg) {
524 switch (msg.what) {
525 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700526 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 break;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800528 case UNKNOWN_TYPE_MSG:
529 HashMap unknownTypeMap = (HashMap) msg.obj;
530 WebView viewForUnknownType = (WebView) unknownTypeMap.get("webview");
531 /*
532 * When the context menu is shown to the user
533 * we need to assure that its happening on the current webview
534 * and its the current webview only which had sent the UNKNOWN_TYPE_MSG
535 */
536 if (getCurrentWebView() != viewForUnknownType)
537 break;
538
539 String unknown_type_src = (String)msg.getData().get("src");
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800540 String unknown_type_url = (String)msg.getData().get("url");
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800541 WebView.HitTestResult result = new WebView.HitTestResult();
542
Axesh R. Ajmera7f5b8bb2015-01-22 17:17:47 -0800543 // Prevent unnecessary calls to context menu
544 // if url and image src are null
545 if (unknown_type_src == null && unknown_type_url == null)
546 break;
547
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -0800548 //setting the HitTestResult with new RESULT TYPE
549 if (!TextUtils.isEmpty(unknown_type_src)) {
550 result.setType(WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
551 result.setExtra(unknown_type_src);
552 } else {
553 result.setType(WebView.HitTestResult.SRC_ANCHOR_TYPE);
554 result.setExtra("about:blank");
555 }
556
557 mResult = result;
558 openContextMenu(viewForUnknownType);
559 mResult = null;
560
561 break;
562
Michael Kolb8233fac2010-10-26 16:08:53 -0700563 case FOCUS_NODE_HREF:
564 {
565 String url = (String) msg.getData().get("url");
566 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500567 String src = (String) msg.getData().get("src");
568 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700569 if (TextUtils.isEmpty(url)) {
570 break;
571 }
572 HashMap focusNodeMap = (HashMap) msg.obj;
573 WebView view = (WebView) focusNodeMap.get("webview");
574 // Only apply the action if the top window did not change.
575 if (getCurrentTopWebView() != view) {
576 break;
577 }
578 switch (msg.arg1) {
579 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700580 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500582 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700583 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500584 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500585 case R.id.open_newtab_context_menu_id:
586 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700587 openTab(url, parent,
588 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500589 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 case R.id.copy_link_context_menu_id:
591 copy(url);
592 break;
593 case R.id.save_link_context_menu_id:
594 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500595 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800596 mActivity, url, view.getSettings().getUserAgentString(),
597 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700599 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700600 if(title == null || title == "")
601 title = url;
602
603 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
604 //SWE TODO: No thumbnail support for the url obtained via
605 //browser context menu as its not loaded in webview.
606 if (bookmarkIntent != null) {
607 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
608 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
609 mActivity.startActivity(bookmarkIntent);
610 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700611 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 }
613 break;
614 }
615
616 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700617 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 break;
619
620 case STOP_LOAD:
621 stopLoading();
622 break;
623
624 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700625 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 mWakeLock.release();
627 // if we reach here, Browser should be still in the
628 // background loading after WAKELOCK_TIMEOUT (5-min).
629 // To avoid burning the battery, stop loading.
630 mTabControl.stopAllLoading();
631 }
632 break;
633
634 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800635 Tab tab = (Tab) msg.obj;
636 if (tab != null) {
637 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700638 }
639 break;
kaiyiz591110b2013-08-06 17:11:06 +0800640 case OPEN_MENU:
641 if (!mOptionsMenuOpen && mActivity != null ) {
642 mActivity.openOptionsMenu();
643 }
644 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700645 }
646 }
647 };
648
649 }
650
John Reckef654f12011-07-12 16:42:08 -0700651 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200652 public Tab getCurrentTab() {
653 return mTabControl.getCurrentTab();
654 }
655
Michael Kolbba99c5d2010-11-29 14:57:41 -0800656 @Override
657 public void shareCurrentPage() {
658 shareCurrentPage(mTabControl.getCurrentTab());
659 }
660
661 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700662 if (tab == null || tab.getWebView() == null)
663 return;
664
665 final Tab mytab = tab;
666 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
667 @Override
668 public void onReceiveValue(Bitmap bitmap) {
669 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
670 mytab.getFavicon(), bitmap);
671 }
672 };
673
674 createScreenshotAsync(
675 tab.getWebView(),
676 getDesiredThumbnailWidth(mActivity),
677 getDesiredThumbnailHeight(mActivity),
678 new ValueCallback<Bitmap>() {
679 @Override
680 public void onReceiveValue(Bitmap bitmap) {
681 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
682 mytab.getFavicon(), bitmap);
683 }
684 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800685 }
686
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 /**
688 * Share a page, providing the title, url, favicon, and a screenshot. Uses
689 * an {@link Intent} to launch the Activity chooser.
690 * @param c Context used to launch a new Activity.
691 * @param title Title of the page. Stored in the Intent with
692 * {@link Intent#EXTRA_SUBJECT}
693 * @param url URL of the page. Stored in the Intent with
694 * {@link Intent#EXTRA_TEXT}
695 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
696 * with {@link Browser#EXTRA_SHARE_FAVICON}
697 * @param screenshot Bitmap of a screenshot of the page. Stored in the
698 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
699 */
700 static final void sharePage(Context c, String title, String url,
701 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700702
703 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
704 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
705 R.layout.app_row, sDialog.getApps());
706 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 }
708
709 private void copy(CharSequence text) {
710 ClipboardManager cm = (ClipboardManager) mActivity
711 .getSystemService(Context.CLIPBOARD_SERVICE);
712 cm.setText(text);
713 }
714
715 // lifecycle
716
John Reck9c35b9c2012-05-30 10:08:50 -0700717 @Override
718 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700719 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800720 // update the menu in case of a locale change
721 mActivity.invalidateOptionsMenu();
Pankaj Garg49b79252014-11-07 17:33:41 -0800722 mAppMenuHandler.hideAppMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800723 if (mOptionsMenuOpen) {
724 mActivity.closeOptionsMenu();
725 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
726 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 if (mPageDialogsHandler != null) {
728 mPageDialogsHandler.onConfigurationChanged(config);
729 }
730 mUi.onConfigurationChanged(config);
731 }
732
733 @Override
734 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700735 if (!mUi.isWebShowing()) {
736 mUi.showWeb(false);
737 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700738 mIntentHandler.onNewIntent(intent);
739 }
740
John Reck9c35b9c2012-05-30 10:08:50 -0700741 @Override
742 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800743 if (mUi.isCustomViewShowing()) {
744 hideCustomView();
745 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700746 if (mActivityPaused) {
747 Log.e(LOGTAG, "BrowserActivity is already paused.");
748 return;
749 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700750 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800751 Tab tab = mTabControl.getCurrentTab();
752 if (tab != null) {
753 tab.pause();
754 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700755 if (mWakeLock == null) {
756 PowerManager pm = (PowerManager) mActivity
757 .getSystemService(Context.POWER_SERVICE);
758 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
759 }
Michael Kolb70976932010-11-30 11:34:01 -0800760 mWakeLock.acquire();
761 mHandler.sendMessageDelayed(mHandler
762 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
763 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 }
765 mUi.onPause();
766 mNetworkHandler.onPause();
767
768 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100769 NfcHandler.unregister(mActivity);
Site Maoabb7bd32015-03-20 15:34:02 -0700770 mActivity.unregisterReceiver(mLowPowerReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700771 }
772
John Reck9c35b9c2012-05-30 10:08:50 -0700773 @Override
774 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700775 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800776 Bundle saveState = createSaveState();
777
778 // crash recovery manages all save & restore state
779 mCrashRecoveryHandler.writeState(saveState);
780 mSettings.setLastRunPaused(true);
781 }
782
783 /**
784 * Save the current state to outState. Does not write the state to
785 * disk.
786 * @return Bundle containing the current state of all tabs.
787 */
788 /* package */ Bundle createSaveState() {
789 Bundle saveState = new Bundle();
790 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800791 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 }
793
John Reck9c35b9c2012-05-30 10:08:50 -0700794 @Override
795 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700796 if (!mActivityPaused) {
797 Log.e(LOGTAG, "BrowserActivity is already resumed.");
798 return;
799 }
George Mount3636d0a2011-11-21 09:08:21 -0800800 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700801 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800802 Tab current = mTabControl.getCurrentTab();
803 if (current != null) {
804 current.resume();
805 resumeWebViewTimers(current);
806 }
John Reckf57c0292011-07-21 18:15:39 -0700807 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200808
Michael Kolb8233fac2010-10-26 16:08:53 -0700809 mUi.onResume();
810 mNetworkHandler.onResume();
811 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100812 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700813 if (mVoiceResult != null) {
814 mUi.onVoiceResult(mVoiceResult);
815 mVoiceResult = null;
816 }
Vivek Sekharc70ae632015-04-08 17:54:05 -0700817 if (current != null && current.getWebView().isShowingCrashView())
818 current.getWebView().reload();
Site Maoabb7bd32015-03-20 15:34:02 -0700819 mActivity.registerReceiver(mLowPowerReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 }
821
John Reckf57c0292011-07-21 18:15:39 -0700822 private void releaseWakeLock() {
823 if (mWakeLock != null && mWakeLock.isHeld()) {
824 mHandler.removeMessages(RELEASE_WAKELOCK);
825 mWakeLock.release();
826 }
827 }
828
Michael Kolb70976932010-11-30 11:34:01 -0800829 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800830 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800831 * @param tab guaranteed non-null
832 */
833 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700834 boolean inLoad = tab.inPageLoad();
835 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
836 CookieSyncManager.getInstance().startSync();
837 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100838 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 }
840 }
841
Michael Kolb70976932010-11-30 11:34:01 -0800842 /**
843 * Pause all WebView timers using the WebView of the given tab
844 * @param tab
845 * @return true if the timers are paused or tab is null
846 */
847 private boolean pauseWebViewTimers(Tab tab) {
848 if (tab == null) {
849 return true;
850 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700851 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100852 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 }
Michael Kolb70976932010-11-30 11:34:01 -0800855 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 }
857
John Reck9c35b9c2012-05-30 10:08:50 -0700858 @Override
859 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800860 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
862 mUploadHandler = null;
863 }
864 if (mTabControl == null) return;
865 mUi.onDestroy();
866 // Remove the current tab and sub window
867 Tab t = mTabControl.getCurrentTab();
868 if (t != null) {
869 dismissSubWindow(t);
870 removeTab(t);
871 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500872 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700873 // Destroy all the tabs
874 mTabControl.destroy();
Site Maoabb7bd32015-03-20 15:34:02 -0700875 // Unregister receiver
876 mActivity.unregisterReceiver(mPowerChangeReceiver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700877 }
878
879 protected boolean isActivityPaused() {
880 return mActivityPaused;
881 }
882
John Reck9c35b9c2012-05-30 10:08:50 -0700883 @Override
884 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 mTabControl.freeMemory();
886 }
887
888 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 public void stopLoading() {
890 mLoadStopped = true;
891 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700893 if (w != null) {
894 w.stopLoading();
895 mUi.onPageStopped(tab);
896 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 }
898
899 boolean didUserStopLoading() {
900 return mLoadStopped;
901 }
902
kaiyiza016da12013-08-26 17:50:22 +0800903 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700904 final String reminderType = getContext().getResources().getString(
905 R.string.def_wifi_browser_interaction_remind_type);
906 final String selectionConnnection = getContext().getResources().getString(
907 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700908 final String wifiSelection = getContext().getResources().getString(
909 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700910
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700911 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
912 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700913 return;
914
kaiyiza016da12013-08-26 17:50:22 +0800915 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700916 Context.CONNECTIVITY_SERVICE);
917 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700918 WifiManager wifiMgr = (WifiManager) this.getContext()
919 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700920 if (networkInfo == null
921 || (networkInfo != null && (networkInfo.getType() !=
922 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700923 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
924 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700925 List<ScanResult> list = wifiMgr.getScanResults();
926 // Have no AP's for Wifi's fall back to data
927 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700928 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700929 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
930 this.getContext().startActivity(intent);
931 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700932 // Request to select Wifi AP
933 try {
934 Intent intent = new Intent(wifiSelection);
935 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
936 this.getContext().startActivity(intent);
937 } catch (Exception e) {
938 String err_msg = this.getContext().getString(
939 R.string.acivity_not_found, wifiSelection);
940 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
941 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700942 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700943 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800944 }
945 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700946
Michael Kolb8233fac2010-10-26 16:08:53 -0700947 // WebViewController
948
949 @Override
John Reck324d4402011-01-11 16:56:42 -0800950 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700951
952 // We've started to load a new page. If there was a pending message
953 // to save a screenshot then we will now take the new page and save
954 // an incorrect screenshot. Therefore, remove any pending thumbnail
955 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700956 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700957
958 // reset sync timer to avoid sync starts during loading a page
959 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700960 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700961 .getSystemService(Context.WIFI_SERVICE);
Panos Thomas4bdb5252014-11-13 16:20:11 -0800962 boolean networkNotifier = BrowserConfig.getInstance(getContext())
963 .hasFeature(BrowserConfig.Feature.NETWORK_NOTIFIER);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700964 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700965 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800966 } else {
967 if (!mNetworkHandler.isNetworkUp()) {
968 view.setNetworkAvailable(false);
969 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 }
971
972 // when BrowserActivity just starts, onPageStarted may be called before
973 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
974 // to start the timer. As we won't switch tabs while an activity is in
975 // pause state, we can ensure calling resume and pause in pair.
976 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800977 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700978 }
979 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 endActionMode();
981
John Reck30c714c2010-12-16 17:30:34 -0800982 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700983
John Reck324d4402011-01-11 16:56:42 -0800984 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 // update the bookmark database for favicon
986 maybeUpdateFavicon(tab, null, url, favicon);
987
988 Performance.tracePageStart(url);
989
990 // Performance probe
991 if (false) {
992 Performance.onPageStarted();
993 }
994
995 }
996
997 @Override
John Reck324d4402011-01-11 16:56:42 -0800998 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700999 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -08001000 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +02001001
Michael Kolb8233fac2010-10-26 16:08:53 -07001002 // Performance probe
1003 if (false) {
John Reck324d4402011-01-11 16:56:42 -08001004 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001005 }
1006
Tarun Nainani8eb00912014-07-17 12:28:32 -07001007 tab.onPageFinished();
1008
Michael Kolb8233fac2010-10-26 16:08:53 -07001009 Performance.tracePageFinished();
1010 }
1011
1012 @Override
John Reck30c714c2010-12-16 17:30:34 -08001013 public void onProgressChanged(Tab tab) {
1014 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -07001015
1016 if (newProgress == 100) {
1017 CookieSyncManager.getInstance().sync();
1018 // onProgressChanged() may continue to be called after the main
1019 // frame has finished loading, as any remaining sub frames continue
1020 // to load. We'll only get called once though with newProgress as
1021 // 100 when everything is loaded. (onPageFinished is called once
1022 // when the main frame completes loading regardless of the state of
1023 // any sub frames so calls to onProgressChanges may continue after
1024 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001025 if (tab.inPageLoad()) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001026 mWasInPageLoad = true;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001027 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001028 } else if (mWasInPageLoad) {
1029 mWasInPageLoad = false;
1030 updateInLoadMenuItems(mCachedMenu, tab);
1031 }
1032
1033 if (mActivityPaused && pauseWebViewTimers(tab)) {
Mattias Falk9cc2d032012-05-25 09:40:31 +02001034 // pause the WebView timer and release the wake lock if it is
1035 // finished while BrowserActivity is in pause state.
1036 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 }
John Reckd9862372012-02-21 15:04:50 -08001038 if (!tab.isPrivateBrowsingEnabled()
1039 && !TextUtils.isEmpty(tab.getUrl())
1040 && !tab.isSnapshot()) {
1041 // Only update the bookmark screenshot if the user did not
1042 // cancel the load early and there is not already
1043 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001044 if (tab.shouldUpdateThumbnail() &&
1045 (tab.inForeground() && !didUserStopLoading()
1046 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001047 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1048 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1049 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001050 1500);
John Reckd9862372012-02-21 15:04:50 -08001051 }
1052 }
1053 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001054 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001055 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001057 // still loading
1058 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001059 // update the menu items.
Pankaj Garg49b79252014-11-07 17:33:41 -08001060 mWasInPageLoad = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001061 updateInLoadMenuItems(mCachedMenu, tab);
Pankaj Garg49b79252014-11-07 17:33:41 -08001062 } else {
1063 mWasInPageLoad = true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 }
1065 }
John Reck30c714c2010-12-16 17:30:34 -08001066 mUi.onProgressChanged(tab);
1067 }
1068
1069 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001070 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001071 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001072 }
1073
1074 @Override
1075 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001076 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001077 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001078 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1080 return;
1081 }
1082 // Update the title in the history database if not in private browsing mode
1083 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001084 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 }
1086 }
1087
1088 @Override
1089 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001090 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1092 }
1093
1094 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001095 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1096 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 }
1098
1099 @Override
1100 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1101 if (mMenuIsDown) {
1102 // only check shortcut key when MENU is held
1103 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1104 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001106 int keyCode = event.getKeyCode();
1107 // We need to send almost every key to WebKit. However:
1108 // 1. We don't want to block the device on the renderer for
1109 // some keys like menu, home, call.
1110 // 2. There are no WebKit equivalents for some of these keys
1111 // (see app/keyboard_codes_win.h)
1112 // Note that these are not the same set as KeyEvent.isSystemKey:
1113 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1114 if (keyCode == KeyEvent.KEYCODE_MENU ||
1115 keyCode == KeyEvent.KEYCODE_HOME ||
1116 keyCode == KeyEvent.KEYCODE_BACK ||
1117 keyCode == KeyEvent.KEYCODE_CALL ||
1118 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1119 keyCode == KeyEvent.KEYCODE_POWER ||
1120 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1121 keyCode == KeyEvent.KEYCODE_CAMERA ||
1122 keyCode == KeyEvent.KEYCODE_FOCUS ||
1123 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1124 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1125 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1126 return true;
1127 }
1128
1129 // We also have to intercept some shortcuts before we send them to the ContentView.
1130 if (event.isCtrlPressed() && (
1131 keyCode == KeyEvent.KEYCODE_TAB ||
1132 keyCode == KeyEvent.KEYCODE_W ||
1133 keyCode == KeyEvent.KEYCODE_F4)) {
1134 return true;
1135 }
1136
1137 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 }
1139
Tarun Nainanif5563b62015-01-21 18:52:59 -08001140 private void handleMediaKeyEvent(KeyEvent event) {
1141
1142 int keyCode = event.getKeyCode();
1143 // send media key events to audio manager
1144 if (Build.VERSION.SDK_INT >= 19) {
1145
1146 switch (keyCode) {
1147 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
1148 case KeyEvent.KEYCODE_MEDIA_STOP:
1149 case KeyEvent.KEYCODE_MEDIA_NEXT:
1150 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
1151 case KeyEvent.KEYCODE_MEDIA_REWIND:
1152 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
1153 case KeyEvent.KEYCODE_MUTE:
1154 case KeyEvent.KEYCODE_MEDIA_PLAY:
1155 case KeyEvent.KEYCODE_MEDIA_PAUSE:
1156 case KeyEvent.META_SHIFT_RIGHT_ON:
1157 case KeyEvent.KEYCODE_MEDIA_EJECT:
1158 case KeyEvent.KEYCODE_MEDIA_RECORD:
1159 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
1160
1161 AudioManager audioManager = (AudioManager) mActivity.getApplicationContext()
1162 .getSystemService(Context.AUDIO_SERVICE);
1163 audioManager.dispatchMediaKeyEvent(event);
1164 }
1165 }
1166 }
1167
Michael Kolb8233fac2010-10-26 16:08:53 -07001168 @Override
John Reck997b1b72012-04-19 18:08:25 -07001169 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 if (!isActivityPaused()) {
Tarun Nainanif5563b62015-01-21 18:52:59 -08001171 handleMediaKeyEvent(event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001173 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 } else {
John Reck997b1b72012-04-19 18:08:25 -07001175 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001176 }
1177 }
John Reck997b1b72012-04-19 18:08:25 -07001178 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001179 }
1180
1181 @Override
John Reck324d4402011-01-11 16:56:42 -08001182 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08001183 // Don't save anything in private browsing mode or when disabling history
1184 // for regular tabs is enabled
1185 if (tab.isPrivateBrowsingEnabled() || BrowserConfig.getInstance(getContext())
1186 .hasFeature(BrowserConfig.Feature.DISABLE_HISTORY))
1187 return;
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001188
John Reck49a603c2011-03-03 09:33:05 -08001189 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001190
John Reck324d4402011-01-11 16:56:42 -08001191 if (TextUtils.isEmpty(url)
1192 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001193 return;
1194 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001195
John Reckf57c0292011-07-21 18:15:39 -07001196 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001197 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001198 }
1199
1200 @Override
1201 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1202 AsyncTask<Void, Void, String[]> task =
1203 new AsyncTask<Void, Void, String[]>() {
1204 @Override
1205 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001206 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001207 }
1208 @Override
1209 public void onPostExecute(String[] result) {
1210 callback.onReceiveValue(result);
1211 }
1212 };
1213 task.execute();
1214 }
1215
1216 @Override
1217 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1218 final HttpAuthHandler handler, final String host,
1219 final String realm) {
1220 String username = null;
1221 String password = null;
1222
1223 boolean reuseHttpAuthUsernamePassword
1224 = handler.useHttpAuthUsernamePassword();
1225
1226 if (reuseHttpAuthUsernamePassword && view != null) {
1227 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1228 if (credentials != null && credentials.length == 2) {
1229 username = credentials[0];
1230 password = credentials[1];
1231 }
1232 }
1233
1234 if (username != null && password != null) {
1235 handler.proceed(username, password);
1236 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001237 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001238 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1239 } else {
1240 handler.cancel();
1241 }
1242 }
1243 }
1244
1245 @Override
1246 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001247 String contentDisposition, String mimetype, String referer,
1248 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001249 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001250 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001251 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001252 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001253 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001254 // This Tab was opened for the sole purpose of downloading a
1255 // file. Remove it.
1256 if (tab == mTabControl.getCurrentTab()) {
1257 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001258 if (tab.getDerivedFromIntent())
1259 closeTab(tab);
1260 else
1261 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 } else {
1263 // In this case, it is not.
1264 closeTab(tab);
1265 }
1266 }
1267 }
1268
1269 @Override
1270 public Bitmap getDefaultVideoPoster() {
1271 return mUi.getDefaultVideoPoster();
1272 }
1273
1274 @Override
1275 public View getVideoLoadingProgressView() {
1276 return mUi.getVideoLoadingProgressView();
1277 }
1278
1279 @Override
1280 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1281 SslError error) {
1282 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1283 }
1284
1285 // helper method
1286
1287 /*
1288 * Update the favorites icon if the private browsing isn't enabled and the
1289 * icon is valid.
1290 */
1291 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1292 final String url, Bitmap favicon) {
1293 if (favicon == null) {
1294 return;
1295 }
1296 if (!tab.isPrivateBrowsingEnabled()) {
1297 Bookmarks.updateFavicon(mActivity
1298 .getContentResolver(), originalUrl, url, favicon);
1299 }
1300 }
1301
Leon Scroggins4cd97792010-12-03 15:31:56 -05001302 @Override
1303 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001304 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001305 mUi.bookmarkedStatusHasChanged(tab);
1306 }
1307
Michael Kolb8233fac2010-10-26 16:08:53 -07001308 // end WebViewController
1309
1310 protected void pageUp() {
1311 getCurrentTopWebView().pageUp(false);
1312 }
1313
1314 protected void pageDown() {
1315 getCurrentTopWebView().pageDown(false);
1316 }
1317
1318 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001319 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001320 public void editUrl() {
1321 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001322 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001323 }
1324
John Reck9c35b9c2012-05-30 10:08:50 -07001325 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001326 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001327 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001328 if (tab.inForeground()) {
1329 if (mUi.isCustomViewShowing()) {
1330 callback.onCustomViewHidden();
1331 return;
1332 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001333 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001334 // Save the menu state and set it to empty while the custom
1335 // view is showing.
1336 mOldMenuState = mMenuState;
1337 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001338 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001339 }
1340 }
1341
1342 @Override
1343 public void hideCustomView() {
1344 if (mUi.isCustomViewShowing()) {
1345 mUi.onHideCustomView();
1346 // Reset the old menu state.
1347 mMenuState = mOldMenuState;
1348 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001349 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001350 }
1351 }
1352
John Reck9c35b9c2012-05-30 10:08:50 -07001353 @Override
1354 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001355 Intent intent) {
1356 if (getCurrentTopWebView() == null) return;
1357 switch (requestCode) {
1358 case PREFERENCES_PAGE:
1359 if (resultCode == Activity.RESULT_OK && intent != null) {
1360 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001361 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001362 mTabControl.removeParentChildRelationShips();
1363 }
1364 }
1365 break;
1366 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001367 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001368 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001370 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001371 case AUTOFILL_SETUP:
1372 // Determine whether a profile was actually set up or not
1373 // and if so, send the message back to the WebTextView to
1374 // fill the form with the new profile.
1375 if (getSettings().getAutoFillProfile() != null) {
1376 mAutoFillSetupMessage.sendToTarget();
1377 mAutoFillSetupMessage = null;
1378 }
1379 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001380 case COMBO_VIEW:
1381 if (intent == null || resultCode != Activity.RESULT_OK) {
1382 break;
1383 }
John Reck3ba45532011-08-11 16:26:53 -07001384 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001385 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1386 Tab t = getCurrentTab();
1387 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001388 mUpdateMyNavThumbnail = true;
1389 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001390 loadUrl(t, uri.toString());
1391 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1392 String[] urls = intent.getStringArrayExtra(
1393 ComboViewActivity.EXTRA_OPEN_ALL);
1394 Tab parent = getCurrentTab();
1395 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001396 if (url != null) {
1397 parent = openTab(url, parent,
1398 !mSettings.openInBackground(), true);
1399 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001400 }
1401 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1402 long id = intent.getLongExtra(
1403 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1404 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001405 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001406 }
1407 }
1408 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001409 case VOICE_RESULT:
1410 if (resultCode == Activity.RESULT_OK && intent != null) {
1411 ArrayList<String> results = intent.getStringArrayListExtra(
1412 RecognizerIntent.EXTRA_RESULTS);
1413 if (results.size() >= 1) {
1414 mVoiceResult = results.get(0);
1415 }
1416 }
1417 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001418 case MY_NAVIGATION:
1419 if (intent == null || resultCode != Activity.RESULT_OK) {
1420 break;
1421 }
1422
1423 if (intent.getBooleanExtra("need_refresh", false) &&
1424 getCurrentTopWebView() != null) {
1425 getCurrentTopWebView().reload();
1426 }
1427 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001428 default:
1429 break;
1430 }
1431 getCurrentTopWebView().requestFocus();
Vivek Sekhared791da2015-02-22 12:39:05 -08001432 getCurrentTopWebView().onActivityResult(requestCode, resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001433 }
1434
1435 /**
1436 * Open the Go page.
1437 * @param startWithHistory If true, open starting on the history tab.
1438 * Otherwise, start with the bookmarks tab.
1439 */
1440 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001441 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001442 if (mTabControl.getCurrentWebView() == null) {
1443 return;
1444 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001445 // clear action mode
1446 if (isInCustomActionMode()) {
1447 endActionMode();
1448 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 Bundle extras = new Bundle();
1450 // Disable opening in a new window if we have maxed out the windows
1451 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1452 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001453 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001454 }
1455
1456 // combo view callbacks
1457
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 // key handling
1459 protected void onBackKey() {
1460 if (!mUi.onBackKey()) {
1461 WebView subwindow = mTabControl.getCurrentSubWindow();
1462 if (subwindow != null) {
1463 if (subwindow.canGoBack()) {
1464 subwindow.goBack();
1465 } else {
1466 dismissSubWindow(mTabControl.getCurrentTab());
1467 }
1468 } else {
1469 goBackOnePageOrQuit();
1470 }
1471 }
1472 }
1473
Michael Kolb4bd767d2011-05-27 11:33:55 -07001474 protected boolean onMenuKey() {
1475 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001476 }
1477
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 // menu handling and state
1479 // TODO: maybe put into separate handler
1480
John Reck9c35b9c2012-05-30 10:08:50 -07001481 @Override
1482 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001483 if (mMenuState == EMPTY_MENU) {
1484 return false;
1485 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001486 MenuInflater inflater = mActivity.getMenuInflater();
1487 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001488 return true;
1489 }
1490
John Reck9c35b9c2012-05-30 10:08:50 -07001491 @Override
1492 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001493 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001494 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001495 return;
1496 }
1497 if (!(v instanceof WebView)) {
1498 return;
1499 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001500 final WebView webview = (WebView) v;
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001501 WebView.HitTestResult result;
1502
1503 /* Determine whether the ContextMenu got triggered because
1504 * of user action of long click or because of the UNKNOWN_TYPE_MSG
1505 * received. The mResult acts as a flag to identify, how it got trigerred
1506 */
1507 if (mResult == null){
1508 result = webview.getHitTestResult();
1509 } else {
1510 result = mResult;
1511 }
1512
Michael Kolb8233fac2010-10-26 16:08:53 -07001513 if (result == null) {
1514 return;
1515 }
1516
1517 int type = result.getType();
1518 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
Axesh R. Ajmera7d5eb332015-01-06 11:00:34 -08001519
1520 HashMap<String, Object> unknownTypeMap = new HashMap<String, Object>();
1521 unknownTypeMap.put("webview", webview);
1522 final Message msg = mHandler.obtainMessage(
1523 UNKNOWN_TYPE_MSG, unknownTypeMap);
1524 /* As defined in android developers guide
1525 * when UNKNOWN_TYPE is received as a result of HitTest
1526 * you need to determing the type by invoking requestFocusNodeHref
1527 */
1528 webview.requestFocusNodeHref(msg);
1529
Michael Kolb8233fac2010-10-26 16:08:53 -07001530 Log.w(LOGTAG,
1531 "We should not show context menu when nothing is touched");
1532 return;
1533 }
1534 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1535 // let TextView handles context menu
1536 return;
1537 }
1538
1539 // Note, http://b/issue?id=1106666 is requesting that
1540 // an inflated menu can be used again. This is not available
1541 // yet, so inflate each time (yuk!)
1542 MenuInflater inflater = mActivity.getMenuInflater();
1543 inflater.inflate(R.menu.browsercontext, menu);
1544
1545 // Show the correct menu group
1546 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001547 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001548 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001549 menu.setGroupVisible(R.id.PHONE_MENU,
1550 type == WebView.HitTestResult.PHONE_TYPE);
1551 menu.setGroupVisible(R.id.EMAIL_MENU,
1552 type == WebView.HitTestResult.EMAIL_TYPE);
1553 menu.setGroupVisible(R.id.GEO_MENU,
1554 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001555 String itemUrl = null;
1556 String url = webview.getOriginalUrl();
1557 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1558 itemUrl = Uri.decode(navigationUrl);
1559 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1560 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1561 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1562 } else {
1563 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1564 }
1565 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1566 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1567 } else {
1568 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1569
1570 menu.setGroupVisible(R.id.IMAGE_MENU,
1571 type == WebView.HitTestResult.IMAGE_TYPE
1572 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1573 menu.setGroupVisible(R.id.ANCHOR_MENU,
1574 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1575 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001576 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1577 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001578 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001579 // Setup custom handling depending on the type
1580 switch (type) {
1581 case WebView.HitTestResult.PHONE_TYPE:
1582 menu.setHeaderTitle(Uri.decode(extra));
1583 menu.findItem(R.id.dial_context_menu_id).setIntent(
1584 new Intent(Intent.ACTION_VIEW, Uri
1585 .parse(WebView.SCHEME_TEL + extra)));
1586 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1587 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1588 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1589 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1590 addIntent);
1591 menu.findItem(R.id.copy_phone_context_menu_id)
1592 .setOnMenuItemClickListener(
1593 new Copy(extra));
1594 break;
1595
1596 case WebView.HitTestResult.EMAIL_TYPE:
1597 menu.setHeaderTitle(extra);
1598 menu.findItem(R.id.email_context_menu_id).setIntent(
1599 new Intent(Intent.ACTION_VIEW, Uri
1600 .parse(WebView.SCHEME_MAILTO + extra)));
1601 menu.findItem(R.id.copy_mail_context_menu_id)
1602 .setOnMenuItemClickListener(
1603 new Copy(extra));
1604 break;
1605
1606 case WebView.HitTestResult.GEO_TYPE:
1607 menu.setHeaderTitle(extra);
1608 menu.findItem(R.id.map_context_menu_id).setIntent(
1609 new Intent(Intent.ACTION_VIEW, Uri
1610 .parse(WebView.SCHEME_GEO
1611 + URLEncoder.encode(extra))));
1612 menu.findItem(R.id.copy_geo_context_menu_id)
1613 .setOnMenuItemClickListener(
1614 new Copy(extra));
1615 break;
1616
1617 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1618 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001619 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001620 // decide whether to show the open link in new tab option
1621 boolean showNewTab = mTabControl.canCreateNewTab();
1622 MenuItem newTabItem
1623 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001624 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001625 ? R.string.contextmenu_openlink_newwindow_background
1626 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001627 newTabItem.setVisible(showNewTab);
1628 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001629 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1630 newTabItem.setOnMenuItemClickListener(
1631 new MenuItem.OnMenuItemClickListener() {
1632 @Override
1633 public boolean onMenuItemClick(MenuItem item) {
1634 final HashMap<String, WebView> hrefMap =
1635 new HashMap<String, WebView>();
1636 hrefMap.put("webview", webview);
1637 final Message msg = mHandler.obtainMessage(
1638 FOCUS_NODE_HREF,
1639 R.id.open_newtab_context_menu_id,
1640 0, hrefMap);
1641 webview.requestFocusNodeHref(msg);
1642 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001643 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001644 });
1645 } else {
1646 newTabItem.setOnMenuItemClickListener(
1647 new MenuItem.OnMenuItemClickListener() {
1648 @Override
1649 public boolean onMenuItemClick(MenuItem item) {
1650 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001651 openTab(extra, parent,
1652 !mSettings.openInBackground(),
1653 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001654 return true;
1655 }
1656 });
1657 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001658 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001659 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1660 menu.setHeaderTitle(navigationUrl);
1661 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1662
1663 if (itemUrl != null) {
1664 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1665 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1666 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1667 @Override
1668 public boolean onMenuItemClick(MenuItem item) {
1669 final Intent intent = new Intent(Controller.this
1670 .getContext(),
1671 AddMyNavigationPage.class);
1672 Bundle bundle = new Bundle();
1673 String url = Uri.decode(navigationUrl);
1674 bundle.putBoolean("isAdding", false);
1675 bundle.putString("url", url);
1676 bundle.putString("name", getNameFromUrl(url));
1677 intent.putExtra("websites", bundle);
1678 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1679 return false;
1680 }
1681 });
1682 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1683 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1684 @Override
1685 public boolean onMenuItemClick(MenuItem item) {
1686 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1687 return false;
1688 }
1689 });
1690 }
1691 } else {
1692 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1693 }
1694 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001695 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1696 break;
1697 }
1698 // otherwise fall through to handle image part
1699 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001700 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1701 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001702 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1703 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001704 shareItem.setOnMenuItemClickListener(
1705 new MenuItem.OnMenuItemClickListener() {
1706 @Override
1707 public boolean onMenuItemClick(MenuItem item) {
1708 sharePage(mActivity, null, extra, null,
1709 null);
1710 return true;
1711 }
1712 }
1713 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001714 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001715 menu.findItem(R.id.view_image_context_menu_id)
1716 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1717 @Override
1718 public boolean onMenuItemClick(MenuItem item) {
1719 openTab(extra, mTabControl.getCurrentTab(), true, true);
1720 return false;
1721 }
1722 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001723 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1724 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1725 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001726 menu.findItem(R.id.set_wallpaper_context_menu_id).
1727 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1728 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001729 break;
1730
1731 default:
1732 Log.w(LOGTAG, "We should not get here.");
1733 break;
1734 }
1735 //update the ui
1736 mUi.onContextMenuCreated(menu);
1737 }
1738
kaiyiz6e5b3e02013-08-19 20:02:01 +08001739 public void startAddMyNavigation(String url) {
1740 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1741 Bundle bundle = new Bundle();
1742 bundle.putBoolean("isAdding", true);
1743 bundle.putString("url", url);
1744 bundle.putString("name", getNameFromUrl(url));
1745 intent.putExtra("websites", bundle);
1746 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1747 }
1748
1749 private void showMyNavigationDeleteDialog(final String itemUrl) {
1750 new AlertDialog.Builder(this.getContext())
1751 .setTitle(R.string.my_navigation_delete_label)
1752 .setIcon(android.R.drawable.ic_dialog_alert)
1753 .setMessage(R.string.my_navigation_delete_msg)
1754 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1755 @Override
1756 public void onClick(DialogInterface dialog, int whichButton) {
1757 deleteMyNavigationItem(itemUrl);
1758 }
1759 })
1760 .setNegativeButton(R.string.cancel, null)
1761 .show();
1762 }
1763
1764 private void deleteMyNavigationItem(final String itemUrl) {
1765 ContentResolver cr = this.getContext().getContentResolver();
1766 Cursor cursor = null;
1767
1768 try {
1769 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1770 new String[] {
1771 MyNavigationUtil.ID
1772 }, "url = ?", new String[] {
1773 itemUrl
1774 }, null);
1775 if (null != cursor && cursor.moveToFirst()) {
1776 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1777 cursor.getLong(0));
1778
1779 ContentValues values = new ContentValues();
1780 values.put(MyNavigationUtil.TITLE, "");
1781 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1782 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1783 ByteArrayOutputStream os = new ByteArrayOutputStream();
1784 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1785 R.raw.my_navigation_add);
1786 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1787 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1788 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1789 cr.update(uri, values, null, null);
1790 } else {
1791 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1792 }
1793 } catch (IllegalStateException e) {
1794 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1795 } finally {
1796 if (null != cursor) {
1797 cursor.close();
1798 }
1799 }
1800
1801 if (getCurrentTopWebView() != null) {
1802 getCurrentTopWebView().reload();
1803 }
1804 }
1805
1806 private String getNameFromUrl(String itemUrl) {
1807 ContentResolver cr = this.getContext().getContentResolver();
1808 Cursor cursor = null;
1809 String name = null;
1810
1811 try {
1812 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1813 new String[] {
1814 MyNavigationUtil.TITLE
1815 }, "url = ?", new String[] {
1816 itemUrl
1817 }, null);
1818 if (null != cursor && cursor.moveToFirst()) {
1819 name = cursor.getString(0);
1820 } else {
1821 Log.e(LOGTAG, "this item does not exist!");
1822 }
1823 } catch (IllegalStateException e) {
1824 Log.e(LOGTAG, "getNameFromUrl", e);
1825 } finally {
1826 if (null != cursor) {
1827 cursor.close();
1828 }
1829 }
1830 return name;
1831 }
1832
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001833 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001834 final ContentResolver cr = mActivity.getContentResolver();
1835 new AsyncTask<Void, Void, Void>() {
1836 @Override
1837 protected Void doInBackground(Void... unused) {
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08001838 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, itemUrl);
1839 if(!isMyNavigationUrl)
1840 return null;
1841
kaiyiz6e5b3e02013-08-19 20:02:01 +08001842 ContentResolver cr = mActivity.getContentResolver();
1843 Cursor cursor = null;
1844 try {
1845 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1846 new String[] {
1847 MyNavigationUtil.ID
1848 }, "url = ?", new String[] {
1849 itemUrl
1850 }, null);
1851 if (null != cursor && cursor.moveToFirst()) {
1852 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001853 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001854
1855 ContentValues values = new ContentValues();
1856 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1857 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1858 cursor.getLong(0));
1859 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1860 cr.update(uri, values, null, null);
1861 os.close();
1862 }
1863 } catch (IllegalStateException e) {
1864 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1865 } catch (IOException e) {
1866 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1867 } finally {
1868 if (null != cursor) {
1869 cursor.close();
1870 }
1871 }
1872 return null;
1873 }
1874 }.execute();
1875 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001876 /**
1877 * As the menu can be open when loading state changes
1878 * we must manually update the state of the stop/reload menu
1879 * item
1880 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001881 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001882 if (menu == null) {
1883 return;
1884 }
1885 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001886 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 menu.findItem(R.id.stop_menu_id):
1888 menu.findItem(R.id.reload_menu_id);
1889 if (src != null) {
1890 dest.setIcon(src.getIcon());
1891 dest.setTitle(src.getTitle());
1892 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001893 mActivity.invalidateOptionsMenu();
1894 }
1895
1896 public void invalidateOptionsMenu() {
1897 mAppMenuHandler.invalidateAppMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001898 }
1899
John Reck9c35b9c2012-05-30 10:08:50 -07001900 @Override
1901 public boolean onPrepareOptionsMenu(Menu menu) {
Pankaj Garg49b79252014-11-07 17:33:41 -08001902 // Software menu key (toolbar key)
1903 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.more_browser_settings), false, false);
1904 return true;
1905 }
1906
1907 @Override
1908 public void prepareMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001909 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001910 // hold on to the menu reference here; it is used by the page callbacks
1911 // to update the menu based on loading state
1912 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001913 // Note: setVisible will decide whether an item is visible; while
1914 // setEnabled() will decide whether an item is enabled, which also means
1915 // whether the matching shortcut key will function.
1916 switch (mMenuState) {
1917 case EMPTY_MENU:
1918 if (mCurrentMenuState != mMenuState) {
1919 menu.setGroupVisible(R.id.MAIN_MENU, false);
1920 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1921 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1922 }
1923 break;
1924 default:
1925 if (mCurrentMenuState != mMenuState) {
1926 menu.setGroupVisible(R.id.MAIN_MENU, true);
1927 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1928 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1929 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001930 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001931 break;
1932 }
1933 mCurrentMenuState = mMenuState;
Pankaj Garg49b79252014-11-07 17:33:41 -08001934 mUi.onPrepareOptionsMenu(menu);
Dave Tharpdcad7b02015-05-28 07:38:32 -07001935
1936 IncognitoRestriction.getInstance()
Dave Tharp9a082b12015-06-19 08:46:57 -07001937 .registerControl(menu.findItem(R.id.incognito_menu_id).getIcon());
1938 EditBookmarksRestriction.getInstance()
1939 .registerControl(menu.findItem(R.id.bookmark_this_page_id).getIcon());
Pankaj Garg49b79252014-11-07 17:33:41 -08001940 }
1941
1942 private void setMenuItemVisibility(Menu menu, int id,
1943 boolean visibility) {
1944 MenuItem item = menu.findItem(id);
1945 if (item != null) {
1946 item.setVisible(visibility);
1947 }
1948 }
1949
1950 private int lookupBookmark(String title, String url) {
1951 final ContentResolver cr = getActivity().getContentResolver();
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001952 int count = 0;
1953 Cursor cursor = null;
1954 try {
1955 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1956 BookmarksLoader.PROJECTION,
1957 "title = ? OR url = ?",
1958 new String[] {
Pankaj Garg49b79252014-11-07 17:33:41 -08001959 title, url
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001960 },
1961 null);
Pankaj Garg49b79252014-11-07 17:33:41 -08001962
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001963 if (cursor != null)
1964 count = cursor.getCount();
1965
1966 } catch (IllegalStateException e) {
1967 Log.e(LOGTAG, "lookupBookmark ", e);
1968 } finally {
1969 if (null != cursor) {
1970 cursor.close();
1971 }
Pankaj Garg49b79252014-11-07 17:33:41 -08001972 }
Tarun Nainanif7c4cfc2015-01-07 12:43:46 -08001973 return count;
Pankaj Garg49b79252014-11-07 17:33:41 -08001974 }
1975
1976 private void resetMenuItems(Menu menu) {
1977 setMenuItemVisibility(menu, R.id.history_menu_id, true);
1978 setMenuItemVisibility(menu, R.id.find_menu_id, true);
1979
1980 WebView w = getCurrentTopWebView();
1981 MenuItem bookmark_icon = menu.findItem(R.id.bookmark_this_page_id);
1982
1983 String title = w.getTitle();
1984 String url = w.getUrl();
1985 if (title != null && url != null && lookupBookmark(title, url) > 0) {
1986 bookmark_icon.setChecked(true);
1987 } else {
1988 bookmark_icon.setChecked(false);
1989 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001990 }
1991
Michael Kolb4bf79712011-07-14 14:18:12 -07001992 @Override
1993 public void updateMenuState(Tab tab, Menu menu) {
Michael Kolb4bf79712011-07-14 14:18:12 -07001994 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001995 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001996 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001997 // Following flag is used to identify schemes for which the LIVE_MENU
1998 // items defined in res/menu/browser.xml should be enabled
1999 boolean isLiveScheme = false;
2000 boolean isPageFinished = false;
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002001 boolean isSavable = false;
Pankaj Garg49b79252014-11-07 17:33:41 -08002002 resetMenuItems(menu);
2003
Michael Kolb4bf79712011-07-14 14:18:12 -07002004 if (tab != null) {
Michael Kolb4bf79712011-07-14 14:18:12 -07002005 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07002006 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07002007 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002008 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
Pankaj Garg49b79252014-11-07 17:33:41 -08002009 isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002010 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07002011 }
Michael Kolb4bf79712011-07-14 14:18:12 -07002012
2013 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
2014 forward.setEnabled(canGoForward);
2015
Michael Kolb4bf79712011-07-14 14:18:12 -07002016 // decide whether to show the share link option
2017 PackageManager pm = mActivity.getPackageManager();
2018 Intent send = new Intent(Intent.ACTION_SEND);
2019 send.setType("text/plain");
2020 ResolveInfo ri = pm.resolveActivity(send,
2021 PackageManager.MATCH_DEFAULT_ONLY);
2022 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
2023
2024 boolean isNavDump = mSettings.enableNavDump();
2025 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
2026 nav.setVisible(isNavDump);
2027 nav.setEnabled(isNavDump);
2028
2029 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07002030 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
2031 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002032 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
Pankaj Garg49b79252014-11-07 17:33:41 -08002033 menu.setGroupVisible(R.id.NAV_MENU, isLive && isLiveScheme);
2034 setMenuItemVisibility(menu, R.id.find_menu_id, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07002035 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Pankaj Garg49b79252014-11-07 17:33:41 -08002036 setMenuItemVisibility(menu, R.id.add_to_homescreen,
2037 isLive && isLiveScheme && isPageFinished);
2038 setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
Axesh R. Ajmera75b811c2015-04-10 16:26:52 -07002039 isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002040 // history and snapshots item are the members of COMBO menu group,
2041 // so if show history item, only make snapshots item invisible.
2042 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07002043
Michael Kolb7bdee0b2011-08-01 11:55:38 -07002044 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07002045 }
2046
John Reck9c35b9c2012-05-30 10:08:50 -07002047 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002048 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002049 if (null == getCurrentTopWebView()) {
2050 return false;
2051 }
2052 if (mMenuIsDown) {
2053 // The shortcut action consumes the MENU. Even if it is still down,
2054 // it won't trigger the next shortcut action. In the case of the
2055 // shortcut action triggering a new activity, like Bookmarks, we
2056 // won't get onKeyUp for MENU. So it is important to reset it here.
2057 mMenuIsDown = false;
2058 }
Michael Kolb3ca12752011-07-20 13:52:25 -07002059 if (mUi.onOptionsItemSelected(item)) {
2060 // ui callback handled it
2061 return true;
2062 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002063 switch (item.getItemId()) {
2064 // -- Main menu
2065 case R.id.new_tab_menu_id:
2066 openTabToHomePage();
2067 break;
2068
2069 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07002070 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002071 break;
2072
2073 case R.id.goto_menu_id:
2074 editUrl();
2075 break;
2076
2077 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002078 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
2079 break;
2080
2081 case R.id.history_menu_id:
2082 bookmarksOrHistoryPicker(ComboViews.History);
2083 break;
2084
2085 case R.id.snapshots_menu_id:
2086 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07002087 break;
2088
Pankaj Garg49b79252014-11-07 17:33:41 -08002089 case R.id.bookmark_this_page_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002090 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 break;
2092
2093 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002094 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002095 stopLoading();
2096 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002097 Tab currentTab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002098 getCurrentTopWebView().reload();
2099 }
2100 break;
2101
Michael Kolb8233fac2010-10-26 16:08:53 -07002102 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07002103 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07002104 break;
2105
2106 case R.id.close_menu_id:
2107 // Close the subwindow if it exists.
2108 if (mTabControl.getCurrentSubWindow() != null) {
2109 dismissSubWindow(mTabControl.getCurrentTab());
2110 break;
2111 }
2112 closeCurrentTab();
2113 break;
2114
kaiyiza8b6dbb2013-07-29 18:11:22 +08002115 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002116 Object[] params = { new String("persist.debug.browsermonkeytest")};
2117 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07002118 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002119 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08002120 if (ret != null && ret.equals("enable"))
2121 break;
Panos Thomas4bdb5252014-11-13 16:20:11 -08002122 if (BrowserConfig.getInstance(getContext())
2123 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG))
2124 showExitDialog(mActivity);
luxiaolb40014b2013-07-19 10:01:43 +08002125 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 case R.id.homepage_menu_id:
2127 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002128 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07002129 break;
2130
2131 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002132 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 break;
2134
2135 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07002136 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07002137 break;
2138
John Reck2bc80422011-06-30 15:11:49 -07002139 case R.id.save_snapshot_menu_id:
2140 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07002141 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002142 createScreenshotAsync(
2143 source.getWebView(),
2144 getDesiredThumbnailWidth(mActivity),
2145 getDesiredThumbnailHeight(mActivity),
2146 new ValueCallback<Bitmap>() {
2147 @Override
2148 public void onReceiveValue(Bitmap bitmap) {
2149 new SaveSnapshotTask(source, bitmap).execute();
2150 }
2151 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002152 break;
2153
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002155 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002156 break;
2157
John Recke1a03a32011-09-14 17:04:16 -07002158 case R.id.snapshot_go_live:
2159 goLive();
2160 return true;
2161
Michael Kolb8233fac2010-10-26 16:08:53 -07002162 case R.id.share_page_menu_id:
2163 Tab currentTab = mTabControl.getCurrentTab();
2164 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002165 return false;
2166 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002167 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002168 break;
2169
2170 case R.id.dump_nav_menu_id:
2171 getCurrentTopWebView().debugDump();
2172 break;
2173
Michael Kolb8233fac2010-10-26 16:08:53 -07002174 case R.id.zoom_in_menu_id:
2175 getCurrentTopWebView().zoomIn();
2176 break;
2177
2178 case R.id.zoom_out_menu_id:
2179 getCurrentTopWebView().zoomOut();
2180 break;
2181
2182 case R.id.view_downloads_menu_id:
2183 viewDownloads();
2184 break;
2185
John Reck42229bc2011-08-19 13:26:43 -07002186 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002187 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002188 break;
2189
Michael Kolb8233fac2010-10-26 16:08:53 -07002190 case R.id.window_one_menu_id:
2191 case R.id.window_two_menu_id:
2192 case R.id.window_three_menu_id:
2193 case R.id.window_four_menu_id:
2194 case R.id.window_five_menu_id:
2195 case R.id.window_six_menu_id:
2196 case R.id.window_seven_menu_id:
2197 case R.id.window_eight_menu_id:
2198 {
2199 int menuid = item.getItemId();
2200 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2201 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2202 Tab desiredTab = mTabControl.getTab(id);
2203 if (desiredTab != null &&
2204 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002205 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002206 }
2207 break;
2208 }
2209 }
2210 }
2211 break;
2212
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002213 case R.id.about_menu_id:
Pankaj Garg18902562014-12-05 16:18:51 -08002214 Bundle bundle = new Bundle();
2215 bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
2216 bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
2217 bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
2218 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
2219 AboutPreferencesFragment.class.getName(), bundle, 0);
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002220 break;
2221
Pankaj Garg49b79252014-11-07 17:33:41 -08002222 case R.id.add_to_homescreen:
2223 final WebView w = getCurrentTopWebView();
2224 final EditText input = new EditText(getContext());
2225 input.setText(w.getTitle());
2226 new AlertDialog.Builder(getContext())
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002227 .setTitle(getContext().getResources().getString(
2228 R.string.add_to_homescreen))
2229 .setMessage(R.string.my_navigation_name)
Pankaj Garg49b79252014-11-07 17:33:41 -08002230 .setView(input)
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002231 .setPositiveButton(getContext().getResources().getString(
2232 R.string.add_bookmark_short), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002233 public void onClick(DialogInterface dialog, int whichButton) {
2234 mActivity.sendBroadcast(BookmarkUtils.createAddToHomeIntent(
2235 getContext(),
2236 w.getUrl(),
2237 input.getText().toString(),
2238 w.getViewportBitmap(),
2239 w.getFavicon()));
2240
2241 mActivity.startActivity(new Intent(Intent.ACTION_MAIN)
2242 .addCategory(Intent.CATEGORY_HOME));
2243 }})
Axesh R. Ajmera9026e7c2015-05-17 16:41:06 -07002244 .setNegativeButton(getContext().getResources().getString(
2245 R.string.import_bookmarks_wizard_cancel), new DialogInterface.OnClickListener() {
Pankaj Garg49b79252014-11-07 17:33:41 -08002246 public void onClick(DialogInterface dialog, int whichButton) {
2247 // Do nothing.
2248 }
2249 })
2250 .show();
2251 break;
2252
Michael Kolb8233fac2010-10-26 16:08:53 -07002253 default:
2254 return false;
2255 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002256 return true;
2257 }
2258
John Reck68234a92012-04-19 15:27:12 -07002259 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2260 implements OnCancelListener {
2261
2262 private Tab mTab;
2263 private Dialog mProgressDialog;
2264 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002265 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002266
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002267 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002268 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002269 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002270 }
2271
2272 @Override
2273 protected void onPreExecute() {
2274 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2275 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2276 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002277 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002278 }
2279
2280 @Override
2281 protected Long doInBackground(Void... params) {
2282 if (!mTab.saveViewState(mValues)) {
2283 return null;
2284 }
2285 if (isCancelled()) {
2286 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2287 File file = mActivity.getFileStreamPath(path);
2288 if (!file.delete()) {
2289 file.deleteOnExit();
2290 }
2291 return null;
2292 }
2293 final ContentResolver cr = mActivity.getContentResolver();
2294 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2295 if (result == null) {
2296 return null;
2297 }
2298 long id = ContentUris.parseId(result);
2299 return id;
2300 }
2301
2302 @Override
2303 protected void onPostExecute(Long id) {
2304 if (isCancelled()) {
2305 return;
2306 }
2307 mProgressDialog.dismiss();
2308 if (id == null) {
2309 Toast.makeText(mActivity, R.string.snapshot_failed,
2310 Toast.LENGTH_SHORT).show();
2311 return;
2312 }
2313 Bundle b = new Bundle();
2314 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2315 mUi.showComboView(ComboViews.Snapshots, b);
2316 }
2317
2318 @Override
2319 public void onCancel(DialogInterface dialog) {
2320 cancel(true);
2321 }
2322 }
2323
Michael Kolb80f75082012-04-10 10:50:06 -07002324 @Override
2325 public void toggleUserAgent() {
2326 WebView web = getCurrentWebView();
2327 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002328 }
2329
2330 @Override
2331 public void findOnPage() {
2332 getCurrentTopWebView().showFindDialog(null, true);
2333 }
2334
2335 @Override
2336 public void openPreferences() {
Enrico Rosd6efa972014-12-02 19:49:59 -08002337 BrowserPreferencesPage.startPreferencesForResult(mActivity, getCurrentTopWebView().getUrl(), PREFERENCES_PAGE);
Michael Kolb80f75082012-04-10 10:50:06 -07002338 }
2339
Pankaj Gargeba076f2015-03-25 13:40:59 -07002340 // This function is specifically used from AddBookmark Activity.
2341 // The bookmark activity clears the bitmap after retrieving it.
2342 // The function usage elsewhere will result in breaking bookmark
2343 // functionality.
2344 public static Bitmap getAndReleaseLastBookmarkBitmapFromIntent() {
2345 Bitmap bitmap = mBookmarkBitmap;
2346 mBookmarkBitmap = null;
2347 return bitmap;
2348 }
2349
Michael Kolb80f75082012-04-10 10:50:06 -07002350 @Override
2351 public void bookmarkCurrentPage() {
Dave Tharp9a082b12015-06-19 08:46:57 -07002352 if(EditBookmarksRestriction.getInstance().isEnabled()) {
2353 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
2354 }
2355 else {
2356 WebView w = getCurrentTopWebView();
2357 if (w == null)
2358 return;
2359 final Intent i = createBookmarkCurrentPageIntent(false);
2360 createScreenshotAsync(
2361 w, getDesiredThumbnailWidth(mActivity),
2362 getDesiredThumbnailHeight(mActivity),
2363 new ValueCallback<Bitmap>() {
2364 @Override
2365 public void onReceiveValue(Bitmap bitmap) {
2366 mBookmarkBitmap = bitmap;
2367 mActivity.startActivity(i);
2368 }
2369 });
2370 }
Michael Kolb80f75082012-04-10 10:50:06 -07002371 }
2372
John Recke1a03a32011-09-14 17:04:16 -07002373 private void goLive() {
Axesh R. Ajmera89cf4d82015-05-19 11:26:18 -07002374 if (!getCurrentTab().isSnapshot()) return;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002375 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002376 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002377 boolean onlySingleTabRemaining = false;
2378 if (mTabControl.getTabCount() > 1) {
2379 // destroy the old snapshot tab
2380 closeCurrentTab();
2381 } else {
2382 onlySingleTabRemaining = true;
2383 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002384 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002385 if (onlySingleTabRemaining) {
2386 closeTab(t);
2387 }
2388
Tarun Nainani8eb00912014-07-17 12:28:32 -07002389 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002390 }
2391
luxiaolb40014b2013-07-19 10:01:43 +08002392 private void showExitDialog(final Activity activity) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002393 BrowserActivity.killOnExitDialog = false;
luxiaolb40014b2013-07-19 10:01:43 +08002394 new AlertDialog.Builder(activity)
2395 .setTitle(R.string.exit_browser_title)
Enrico Ros1f5a0952014-11-18 20:15:48 -08002396 /* disabled, was worrying people: .setIcon(android.R.drawable.ic_dialog_alert) */
luxiaolb40014b2013-07-19 10:01:43 +08002397 .setMessage(R.string.exit_browser_msg)
2398 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2399 public void onClick(DialogInterface dialog, int which) {
2400 activity.moveTaskToBack(true);
2401 dialog.dismiss();
2402 }
2403 })
2404 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2405 public void onClick(DialogInterface dialog, int which) {
Axesh R. Ajmera32558762015-04-23 13:21:56 -07002406 mCrashRecoveryHandler.clearState(true);
2407 BrowserActivity.killOnExitDialog = true;
luxiaolb40014b2013-07-19 10:01:43 +08002408 activity.finish();
luxiaolb40014b2013-07-19 10:01:43 +08002409 dialog.dismiss();
2410 }
2411 })
2412 .show();
2413 }
Michael Kolb315d5022011-10-13 12:47:11 -07002414 @Override
2415 public void showPageInfo() {
2416 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2417 }
2418
John Reck9c35b9c2012-05-30 10:08:50 -07002419 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002420 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002421 // Let the History and Bookmark fragments handle menus they created.
2422 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2423 return false;
2424 }
2425
Michael Kolb8233fac2010-10-26 16:08:53 -07002426 int id = item.getItemId();
2427 boolean result = true;
2428 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002429 // -- Browser context menu
2430 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002431 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002432 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 case R.id.copy_link_context_menu_id:
2434 final WebView webView = getCurrentTopWebView();
2435 if (null == webView) {
2436 result = false;
2437 break;
2438 }
2439 final HashMap<String, WebView> hrefMap =
2440 new HashMap<String, WebView>();
2441 hrefMap.put("webview", webView);
2442 final Message msg = mHandler.obtainMessage(
2443 FOCUS_NODE_HREF, id, 0, hrefMap);
2444 webView.requestFocusNodeHref(msg);
2445 break;
2446
2447 default:
2448 // For other context menus
2449 result = onOptionsItemSelected(item);
2450 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002451 return result;
2452 }
2453
2454 /**
2455 * support programmatically opening the context menu
2456 */
2457 public void openContextMenu(View view) {
2458 mActivity.openContextMenu(view);
2459 }
2460
2461 /**
2462 * programmatically open the options menu
2463 */
2464 public void openOptionsMenu() {
2465 mActivity.openOptionsMenu();
2466 }
2467
John Reck9c35b9c2012-05-30 10:08:50 -07002468 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002469 public boolean onMenuOpened(int featureId, Menu menu) {
2470 if (mOptionsMenuOpen) {
2471 if (mConfigChanged) {
2472 // We do not need to make any changes to the state of the
2473 // title bar, since the only thing that happened was a
2474 // change in orientation
2475 mConfigChanged = false;
2476 } else {
2477 if (!mExtendedMenuOpen) {
2478 mExtendedMenuOpen = true;
2479 mUi.onExtendedMenuOpened();
2480 } else {
2481 // Switching the menu back to icon view, so show the
2482 // title bar once again.
2483 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002484 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002485 }
2486 }
2487 } else {
2488 // The options menu is closed, so open it, and show the title
2489 mOptionsMenuOpen = true;
2490 mConfigChanged = false;
2491 mExtendedMenuOpen = false;
2492 mUi.onOptionsMenuOpened();
2493 }
2494 return true;
2495 }
2496
John Reck9c35b9c2012-05-30 10:08:50 -07002497 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002498 public void onOptionsMenuClosed(Menu menu) {
2499 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002500 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002501 }
2502
John Reck9c35b9c2012-05-30 10:08:50 -07002503 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002504 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002505 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002506 }
2507
2508 // Helper method for getting the top window.
2509 @Override
2510 public WebView getCurrentTopWebView() {
2511 return mTabControl.getCurrentTopWebView();
2512 }
2513
2514 @Override
2515 public WebView getCurrentWebView() {
2516 return mTabControl.getCurrentWebView();
2517 }
2518
2519 /*
2520 * This method is called as a result of the user selecting the options
2521 * menu to see the download window. It shows the download window on top of
2522 * the current window.
2523 */
2524 void viewDownloads() {
2525 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2526 mActivity.startActivity(intent);
2527 }
2528
John Reck30b065e2011-07-19 10:58:05 -07002529 int getActionModeHeight() {
2530 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2531 new int[] { android.R.attr.actionBarSize });
2532 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2533 actionBarSizeTypedArray.recycle();
2534 return size;
2535 }
2536
Michael Kolb8233fac2010-10-26 16:08:53 -07002537 // action mode
2538
John Reck9c35b9c2012-05-30 10:08:50 -07002539 @Override
2540 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 mUi.onActionModeStarted(mode);
2542 mActionMode = mode;
2543 }
2544
2545 /*
2546 * True if a custom ActionMode (i.e. find or select) is in use.
2547 */
2548 @Override
2549 public boolean isInCustomActionMode() {
2550 return mActionMode != null;
2551 }
2552
2553 /*
2554 * End the current ActionMode.
2555 */
2556 @Override
2557 public void endActionMode() {
2558 if (mActionMode != null) {
2559 mActionMode.finish();
2560 }
2561 }
2562
2563 /*
2564 * Called by find and select when they are finished. Replace title bars
2565 * as necessary.
2566 */
John Reck9c35b9c2012-05-30 10:08:50 -07002567 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002568 public void onActionModeFinished(ActionMode mode) {
2569 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002570 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002571 mActionMode = null;
2572 }
2573
2574 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002575 final Tab tab = getCurrentTab();
2576 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002577 }
2578
2579 // bookmark handling
2580
2581 /**
2582 * add the current page as a bookmark to the given folder id
2583 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002584 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002585 * bookmarked, and if it is, edit that bookmark. If false, and
2586 * the site is already bookmarked, do not attempt to edit the
2587 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 */
2589 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002590 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002591 WebView w = getCurrentTopWebView();
2592 if (w == null) {
2593 return null;
2594 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002595 Intent i = new Intent(mActivity,
2596 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002597 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2598 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2599 String touchIconUrl = w.getTouchIconUrl();
2600 if (touchIconUrl != null) {
2601 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2602 WebSettings settings = w.getSettings();
2603 if (settings != null) {
2604 i.putExtra(AddBookmarkPage.USER_AGENT,
2605 settings.getUserAgentString());
2606 }
2607 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002608 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002609 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002610 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002611 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2612 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002613 // Put the dialog at the upper right of the screen, covering the
2614 // star on the title bar.
2615 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002616 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002617 }
2618
2619 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002620 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002621 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002622 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002623 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002624 }
2625
Vivek Sekharb54614f2014-05-01 19:03:37 -07002626 @Override
2627 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2628 boolean capture) {
2629 mUploadHandler = new UploadHandler(this);
2630 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2631 }
2632
Michael Kolb8233fac2010-10-26 16:08:53 -07002633 // thumbnails
2634
2635 /**
2636 * Return the desired width for thumbnail screenshots, which are stored in
2637 * the database, and used on the bookmarks screen.
2638 * @param context Context for finding out the density of the screen.
2639 * @return desired width for thumbnail screenshot.
2640 */
2641 static int getDesiredThumbnailWidth(Context context) {
2642 return context.getResources().getDimensionPixelOffset(
2643 R.dimen.bookmarkThumbnailWidth);
2644 }
2645
2646 /**
2647 * Return the desired height for thumbnail screenshots, which are stored in
2648 * the database, and used on the bookmarks screen.
2649 * @param context Context for finding out the density of the screen.
2650 * @return desired height for thumbnail screenshot.
2651 */
2652 static int getDesiredThumbnailHeight(Context context) {
2653 return context.getResources().getDimensionPixelOffset(
2654 R.dimen.bookmarkThumbnailHeight);
2655 }
2656
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002657 static void createScreenshotAsync(WebView view, int width, int height,
2658 final ValueCallback<Bitmap> cb) {
2659 if (view == null || width == 0 || height == 0) {
2660 return;
2661 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002662 view.getContentBitmapAsync(
2663 (float) width / view.getWidth(),
2664 new Rect(),
2665 new ValueCallback<Bitmap>() {
2666 @Override
2667 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002668 if (bitmap != null)
2669 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2670 cb.onReceiveValue(bitmap);
2671 }});
2672 }
2673
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002674 private void updateScreenshot(final Tab tab) {
2675 createScreenshotAsync(
2676 tab.getWebView(),
2677 getDesiredThumbnailWidth(mActivity),
2678 getDesiredThumbnailHeight(mActivity),
2679 new ValueCallback<Bitmap>() {
2680 @Override
2681 public void onReceiveValue(Bitmap bitmap) {
2682 updateScreenshot(tab, bitmap);
2683 }
2684 });
2685 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002686
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002687 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002688 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002689 // FIXME: Would like to make sure there is actually something to
2690 // draw, but the API for that (WebViewCore.pictureReady()) is not
2691 // currently accessible here.
2692
John Reck34ef2672011-02-10 11:30:55 -08002693 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002694 if (view == null) {
2695 // Tab was destroyed
2696 return;
2697 }
John Reck34ef2672011-02-10 11:30:55 -08002698 final String url = tab.getUrl();
2699 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002700 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002701 if (TextUtils.isEmpty(url)) {
2702 return;
2703 }
2704
kaiyiz6e5b3e02013-08-19 20:02:01 +08002705 //update My Navigation Thumbnails
Tarun Nainanicc8ac9e2015-01-08 14:45:21 -08002706 if (bm != null) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002707 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002708 }
John Reck34ef2672011-02-10 11:30:55 -08002709 // Only update thumbnails for web urls (http(s)://), not for
2710 // about:, javascript:, data:, etc...
2711 // Unless it is a bookmarked site, then always update
2712 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2713 return;
2714 }
2715
kaiyiz6e5b3e02013-08-19 20:02:01 +08002716 if (url != null && mUpdateMyNavThumbnailUrl != null
2717 && Patterns.WEB_URL.matcher(url).matches()
2718 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2719 String urlHost = (new WebAddress(url)).getHost();
2720 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2721 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2722 || bookmarkHost.length() == 0) {
2723 return;
2724 }
2725 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2726 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2727 bookmarkHost.length());
2728 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2729 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2730 if (!bookmarkDomain.equals(urlDomain)) {
2731 return;
2732 }
2733 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002734 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002735 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2736 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2737 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2738 500);
2739 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002740 return;
2741 }
2742
2743 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002744 new AsyncTask<Void, Void, Void>() {
2745 @Override
2746 protected Void doInBackground(Void... unused) {
2747 Cursor cursor = null;
2748 try {
2749 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002750 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2751 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2752 : url);
2753 if (mUpdateMyNavThumbnail) {
2754 mUpdateMyNavThumbnail = false;
2755 mUpdateMyNavThumbnailUrl = null;
2756 }
John Reck34ef2672011-02-10 11:30:55 -08002757 if (cursor != null && cursor.moveToFirst()) {
2758 final ByteArrayOutputStream os =
2759 new ByteArrayOutputStream();
2760 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002761
John Reck34ef2672011-02-10 11:30:55 -08002762 ContentValues values = new ContentValues();
2763 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002764
John Reck34ef2672011-02-10 11:30:55 -08002765 do {
John Reck617fd832011-02-16 14:35:59 -08002766 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002767 cr.update(Images.CONTENT_URI, values, null, null);
2768 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002769 }
John Reck34ef2672011-02-10 11:30:55 -08002770 } catch (IllegalStateException e) {
2771 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002772 } catch (SQLiteException s) {
2773 // Added for possible error when user tries to remove the same bookmark
2774 // that is being updated with a screen shot
2775 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002776 } finally {
2777 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002778 }
John Reck34ef2672011-02-10 11:30:55 -08002779 return null;
2780 }
2781 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002782 }
2783
2784 private class Copy implements OnMenuItemClickListener {
2785 private CharSequence mText;
2786
John Reck9c35b9c2012-05-30 10:08:50 -07002787 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002788 public boolean onMenuItemClick(MenuItem item) {
2789 copy(mText);
2790 return true;
2791 }
2792
2793 public Copy(CharSequence toCopy) {
2794 mText = toCopy;
2795 }
2796 }
2797
Leon Scroggins63c02662010-11-18 15:16:27 -05002798 private static class Download implements OnMenuItemClickListener {
2799 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002800 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002801 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002802 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002803 private static final String FALLBACK_EXTENSION = "dat";
2804 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002805
John Reck9c35b9c2012-05-30 10:08:50 -07002806 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002807 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002808 if (DataUri.isDataUri(mText)) {
2809 saveDataUri();
2810 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002811 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002812 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002813 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002814 return true;
2815 }
2816
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002817 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2818 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002819 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002820 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002821 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002822 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002823 }
George Mount387d45d2011-10-07 15:57:53 -07002824
2825 /**
2826 * Treats mText as a data URI and writes its contents to a file
2827 * based on the current time.
2828 */
2829 private void saveDataUri() {
2830 FileOutputStream outputStream = null;
2831 try {
2832 DataUri uri = new DataUri(mText);
2833 File target = getTarget(uri);
2834 outputStream = new FileOutputStream(target);
2835 outputStream.write(uri.getData());
2836 final DownloadManager manager =
2837 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2838 manager.addCompletedDownload(target.getName(),
2839 mActivity.getTitle().toString(), false,
2840 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002841 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002842 } catch (IOException e) {
2843 Log.e(LOGTAG, "Could not save data URL");
2844 } finally {
2845 if (outputStream != null) {
2846 try {
2847 outputStream.close();
2848 } catch (IOException e) {
2849 // ignore close errors
2850 }
2851 }
2852 }
2853 }
2854
2855 /**
2856 * Creates a File based on the current time stamp and uses
2857 * the mime type of the DataUri to get the extension.
2858 */
2859 private File getTarget(DataUri uri) throws IOException {
2860 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002861 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002862 String nameBase = format.format(new Date());
2863 String mimeType = uri.getMimeType();
2864 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2865 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2866 if (extension == null) {
2867 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2868 extension = FALLBACK_EXTENSION;
2869 }
2870 extension = "." + extension; // createTempFile needs the '.'
2871 File targetFile = File.createTempFile(nameBase, extension, dir);
2872 return targetFile;
2873 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002874 }
2875
Cary Clark8974d282010-11-22 10:46:05 -05002876 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002877 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002878
John Reck9c35b9c2012-05-30 10:08:50 -07002879 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002880 public boolean onMenuItemClick(MenuItem item) {
2881 if (mWebView != null) {
2882 return mWebView.selectText();
2883 }
2884 return false;
2885 }
2886
2887 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002888 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002889 }
2890
2891 }
2892
Michael Kolb8233fac2010-10-26 16:08:53 -07002893 /********************** TODO: UI stuff *****************************/
2894
2895 // these methods have been copied, they still need to be cleaned up
2896
2897 /****************** tabs ***************************************************/
2898
2899 // basic tab interactions:
2900
2901 // it is assumed that tabcontrol already knows about the tab
2902 protected void addTab(Tab tab) {
2903 mUi.addTab(tab);
2904 }
2905
2906 protected void removeTab(Tab tab) {
2907 mUi.removeTab(tab);
2908 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002909 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002910 }
2911
Michael Kolbf2055602011-04-09 17:20:03 -07002912 @Override
2913 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002914 // monkey protection against delayed start
2915 if (tab != null) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002916
2917 //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
2918 mUi.cancelNavScreenRequest();
Michael Kolbcd424e92011-02-24 10:26:26 -08002919 mTabControl.setCurrentTab(tab);
2920 // the tab is guaranteed to have a webview after setCurrentTab
2921 mUi.setActiveTab(tab);
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002922
2923
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002924 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002925 //Purge active tabs
2926 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002927 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002928 }
2929
John Reck8bcafc12011-08-29 16:43:02 -07002930 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002931 Tab current = mTabControl.getCurrentTab();
2932 if (current != null
2933 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002934 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002935 }
2936 }
2937
John Reck26b18322011-06-21 13:08:58 -07002938 protected void reuseTab(Tab appTab, UrlData urlData) {
Sagar Dhawanf7b5d0b2015-02-06 15:47:20 -08002939 //Cancel navscreen request
2940 mUi.cancelNavScreenRequest();
Michael Kolb8233fac2010-10-26 16:08:53 -07002941 // Dismiss the subwindow if applicable.
2942 dismissSubWindow(appTab);
2943 // Since we might kill the WebView, remove it from the
2944 // content view first.
2945 mUi.detachTab(appTab);
2946 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002947 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002948 // TODO: analyze why the remove and add are necessary
2949 mUi.attachTab(appTab);
2950 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002951 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002952 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002953 } else {
2954 // If the tab was the current tab, we have to attach
2955 // it to the view system again.
2956 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002957 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002958 }
2959 }
2960
2961 // Remove the sub window if it exists. Also called by TabControl when the
2962 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002963 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002964 public void dismissSubWindow(Tab tab) {
2965 removeSubWindow(tab);
2966 // dismiss the subwindow. This will destroy the WebView.
2967 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002968 WebView wv = getCurrentTopWebView();
2969 if (wv != null) {
2970 wv.requestFocus();
2971 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002972 }
2973
2974 @Override
2975 public void removeSubWindow(Tab t) {
2976 if (t.getSubWebView() != null) {
2977 mUi.removeSubWindow(t.getSubViewContainer());
2978 }
2979 }
2980
2981 @Override
2982 public void attachSubWindow(Tab tab) {
2983 if (tab.getSubWebView() != null) {
2984 mUi.attachSubWindow(tab.getSubViewContainer());
2985 getCurrentTopWebView().requestFocus();
2986 }
2987 }
2988
Mathew Inwood29721c22011-06-29 17:55:24 +01002989 private Tab showPreloadedTab(final UrlData urlData) {
2990 if (!urlData.isPreloaded()) {
2991 return null;
2992 }
2993 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2994 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2995 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002996 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002997 // Could not submit query. Fallback to regular tab creation
2998 tabControl.destroy();
2999 return null;
3000 }
3001 }
Michael Kolbff6a7482011-07-26 16:37:15 -07003002 // check tab count and make room for new tab
3003 if (!mTabControl.canCreateNewTab()) {
3004 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
3005 if (leastUsed != null) {
3006 closeTab(leastUsed);
3007 }
3008 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003009 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01003010 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01003011 mTabControl.addPreloadedTab(t);
3012 addTab(t);
3013 setActiveTab(t);
3014 return t;
3015 }
3016
Michael Kolb7bcafde2011-05-09 13:55:59 -07003017 // open a non inconito tab with the given url data
3018 // and set as active tab
3019 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01003020 Tab tab = showPreloadedTab(urlData);
3021 if (tab == null) {
3022 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07003023 if ((tab != null) && !urlData.isEmpty()) {
3024 loadUrlDataIn(tab, urlData);
3025 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003026 }
Mathew Inwood29721c22011-06-29 17:55:24 +01003027 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07003028 }
3029
Michael Kolb843510f2010-12-09 10:51:49 -08003030 @Override
3031 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003032 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07003033 }
3034
Michael Kolb8233fac2010-10-26 16:08:53 -07003035 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07003036 public Tab openIncognitoTab() {
3037 return openTab(INCOGNITO_URI, true, true, false);
3038 }
3039
3040 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07003041 public Tab openTab(String url, boolean incognito, boolean setActive,
3042 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07003043 return openTab(url, incognito, setActive, useCurrent, null);
3044 }
3045
3046 @Override
3047 public Tab openTab(String url, Tab parent, boolean setActive,
3048 boolean useCurrent) {
3049 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
3050 setActive, useCurrent, parent);
3051 }
3052
3053 public Tab openTab(String url, boolean incognito, boolean setActive,
3054 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07003055 Tab tab = createNewTab(incognito, setActive, useCurrent);
3056 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07003057 if (parent instanceof SnapshotTab) {
3058 addTab(tab);
3059 if (setActive)
3060 setActiveTab(tab);
3061 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07003062 parent.addChildTab(tab);
3063 }
Michael Kolb519d2282011-05-09 17:03:19 -07003064 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07003065 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07003066 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003067 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003068 return tab;
3069 }
3070
3071 // this method will attempt to create a new tab
3072 // incognito: private browsing tab
3073 // setActive: ste tab as current tab
3074 // useCurrent: if no new tab can be created, return current tab
3075 private Tab createNewTab(boolean incognito, boolean setActive,
3076 boolean useCurrent) {
3077 Tab tab = null;
Dave Tharpdcad7b02015-05-28 07:38:32 -07003078 if (IncognitoRestriction.getInstance().isEnabled() && incognito) {
3079 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
Michael Kolb7bcafde2011-05-09 13:55:59 -07003080 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003081 if (mTabControl.canCreateNewTab()) {
3082 tab = mTabControl.createNewTab(incognito, !setActive);
3083 addTab(tab);
3084 if (setActive) {
3085 setActiveTab(tab);
3086 } else {
3087 tab.pause();
3088 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003089 } else {
Dave Tharpdcad7b02015-05-28 07:38:32 -07003090 if (useCurrent) {
3091 tab = mTabControl.getCurrentTab();
3092 reuseTab(tab, null);
3093 } else {
3094 mUi.showMaxTabsWarning();
3095 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07003096 }
3097 }
3098 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07003099 }
3100
John Reck2bc80422011-06-30 15:11:49 -07003101 @Override
3102 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
3103 SnapshotTab tab = null;
3104 if (mTabControl.canCreateNewTab()) {
3105 tab = mTabControl.createSnapshotTab(snapshotId);
3106 addTab(tab);
3107 if (setActive) {
3108 setActiveTab(tab);
3109 }
3110 } else {
3111 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07003112 }
3113 return tab;
3114 }
3115
Michael Kolb8233fac2010-10-26 16:08:53 -07003116 /**
Michael Kolbc831b632011-05-11 09:30:34 -07003117 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07003118 * @return boolean True if we successfully switched to a different tab. If
3119 * the indexth tab is null, or if that tab is the same as
3120 * the current one, return false.
3121 */
3122 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07003123 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003124 Tab currentTab = mTabControl.getCurrentTab();
3125 if (tab == null || tab == currentTab) {
3126 return false;
3127 }
3128 setActiveTab(tab);
3129 return true;
3130 }
3131
3132 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07003133 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003134 closeCurrentTab(false);
3135 }
3136
3137 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003138 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07003139 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07003140 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08003141 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07003142 return;
3143 }
Michael Kolbc831b632011-05-11 09:30:34 -07003144 final Tab current = mTabControl.getCurrentTab();
3145 final int pos = mTabControl.getCurrentPosition();
3146 Tab newTab = current.getParent();
3147 if (newTab == null) {
3148 newTab = mTabControl.getTab(pos + 1);
3149 if (newTab == null) {
3150 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07003151 }
3152 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07003153 if (andQuit) {
3154 mTabControl.setCurrentTab(newTab);
3155 closeTab(current);
3156 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 // Close window
3158 closeTab(current);
3159 }
3160 }
3161
3162 /**
3163 * Close the tab, remove its associated title bar, and adjust mTabControl's
3164 * current tab to a valid value.
3165 */
3166 @Override
3167 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07003168 if (tab == mTabControl.getCurrentTab()) {
3169 closeCurrentTab();
3170 } else {
3171 removeTab(tab);
3172 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003173 }
3174
Afzal Najamd4e33312012-04-26 01:54:01 -04003175 /**
3176 * Close all tabs except the current one
3177 */
3178 @Override
3179 public void closeOtherTabs() {
3180 int inactiveTabs = mTabControl.getTabCount() - 1;
3181 for (int i = inactiveTabs; i >= 0; i--) {
3182 Tab tab = mTabControl.getTab(i);
3183 if (tab != mTabControl.getCurrentTab()) {
3184 removeTab(tab);
3185 }
3186 }
3187 }
3188
Michael Kolb8233fac2010-10-26 16:08:53 -07003189 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003190 protected void loadUrlFromContext(String url) {
3191 Tab tab = getCurrentTab();
3192 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003193 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003194 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003195 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003196 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003197 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003198 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003199 }
3200 }
3201 }
3202
3203 /**
3204 * Load the URL into the given WebView and update the title bar
3205 * to reflect the new load. Call this instead of WebView.loadUrl
3206 * directly.
3207 * @param view The WebView used to load url.
3208 * @param url The URL to load.
3209 */
John Reck71e51422011-07-01 16:49:28 -07003210 @Override
3211 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003212 loadUrl(tab, url, null);
3213 }
3214
3215 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3216 if (tab != null) {
3217 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003218 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003219 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07003220 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003221 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003222 }
3223
3224 /**
3225 * Load UrlData into a Tab and update the title bar to reflect the new
3226 * load. Call this instead of UrlData.loadIn directly.
3227 * @param t The Tab used to load.
3228 * @param data The UrlData being loaded.
3229 */
3230 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003231 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003232 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003233 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003234 } else {
John Reck38b39652012-06-05 09:22:59 -07003235 if (t != null && data.mDisableUrlOverride) {
3236 t.disableUrlOverridingForLoad();
3237 }
John Reck26b18322011-06-21 13:08:58 -07003238 loadUrl(t, data.mUrl, data.mHeaders);
3239 }
3240 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003241 }
3242
John Reck30c714c2010-12-16 17:30:34 -08003243 @Override
3244 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003245 // TODO: Figure out the "right" behavior
Tarun Nainani1f481922014-12-18 13:54:54 -08003246 //In case of tab can go back (aka tab has navigation entry) do nothing
3247 //else just load homepage in current tab.
3248 if (!tab.canGoBack()) {
John Reckef654f12011-07-12 16:42:08 -07003249 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003250 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003251 }
3252
3253 void goBackOnePageOrQuit() {
3254 Tab current = mTabControl.getCurrentTab();
3255 if (current == null) {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003256 if (BrowserConfig.getInstance(getContext()).hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3257 showExitDialog(mActivity);
3258 } else {
3259 /*
3260 * Instead of finishing the activity, simply push this to the back
3261 * of the stack and let ActivityManager to choose the foreground
3262 * activity. As BrowserActivity is singleTask, it will be always the
3263 * root of the task. So we can use either true or false for
3264 * moveTaskToBack().
3265 */
3266 mActivity.moveTaskToBack(true);
3267 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003268 return;
3269 }
John Reckef654f12011-07-12 16:42:08 -07003270 if (current.canGoBack()) {
3271 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003272 } else {
3273 // Check to see if we are closing a window that was created by
3274 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003275 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003276 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003277 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003278 // Now we close the other tab
3279 closeTab(current);
Panos Thomas4bdb5252014-11-13 16:20:11 -08003280 } else if (BrowserConfig.getInstance(getContext())
3281 .hasFeature(BrowserConfig.Feature.EXIT_DIALOG)) {
3282 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003283 } else {
Panos Thomas4bdb5252014-11-13 16:20:11 -08003284 if ((current.getAppId() != null) || current.closeOnBack()) {
3285 closeCurrentTab(true);
3286 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003287 /*
3288 * Instead of finishing the activity, simply push this to the back
3289 * of the stack and let ActivityManager to choose the foreground
3290 * activity. As BrowserActivity is singleTask, it will be always the
3291 * root of the task. So we can use either true or false for
3292 * moveTaskToBack().
3293 */
Panos Thomas4bdb5252014-11-13 16:20:11 -08003294 mActivity.moveTaskToBack(true);
Michael Kolb8233fac2010-10-26 16:08:53 -07003295 }
3296 }
3297 }
3298
3299 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003300 * helper method for key handler
3301 * returns the current tab if it can't advance
3302 */
Michael Kolbc831b632011-05-11 09:30:34 -07003303 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003304 int pos = mTabControl.getCurrentPosition() + 1;
3305 if (pos >= mTabControl.getTabCount()) {
3306 pos = 0;
3307 }
3308 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003309 }
3310
3311 /**
3312 * helper method for key handler
3313 * returns the current tab if it can't advance
3314 */
Michael Kolbc831b632011-05-11 09:30:34 -07003315 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003316 int pos = mTabControl.getCurrentPosition() - 1;
3317 if ( pos < 0) {
3318 pos = mTabControl.getTabCount() - 1;
3319 }
3320 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003321 }
3322
John Reckbcef87f2012-02-03 14:58:34 -08003323 boolean isMenuOrCtrlKey(int keyCode) {
3324 return (KeyEvent.KEYCODE_MENU == keyCode)
3325 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3326 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3327 }
3328
Michael Kolb0035fad2011-03-14 13:25:28 -07003329 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003330 * handle key events in browser
3331 *
3332 * @param keyCode
3333 * @param event
3334 * @return true if handled, false to pass to super
3335 */
John Reck9c35b9c2012-05-30 10:08:50 -07003336 @Override
3337 public boolean onKeyDown(int keyCode, KeyEvent event) {
Pankaj Garg49b79252014-11-07 17:33:41 -08003338 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
3339 // Hardware menu key
Tarun Nainani87a86682015-02-05 11:47:35 -08003340 if (!mUi.isComboViewShowing()) {
3341 mAppMenuHandler.showAppMenu(mActivity.findViewById(R.id.taburlbar),
3342 true, false);
3343 }
Pankaj Garg49b79252014-11-07 17:33:41 -08003344 return true;
3345 }
3346
Cary Clark160bbb92011-01-10 11:17:07 -05003347 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003348 // Even if MENU is already held down, we need to call to super to open
3349 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003350 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003351 mMenuIsDown = true;
3352 return false;
3353 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003354
Cary Clark8ff8c662010-12-29 15:03:05 -05003355 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003356 Tab tab = getCurrentTab();
3357 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003358
Cary Clark160bbb92011-01-10 11:17:07 -05003359 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3360 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003361
Michael Kolb8233fac2010-10-26 16:08:53 -07003362 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003363 case KeyEvent.KEYCODE_TAB:
3364 if (event.isCtrlPressed()) {
3365 if (event.isShiftPressed()) {
3366 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003367 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003368 } else {
3369 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003370 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003371 }
3372 return true;
3373 }
3374 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003375 case KeyEvent.KEYCODE_SPACE:
3376 // WebView/WebTextView handle the keys in the KeyDown. As
3377 // the Activity's shortcut keys are only handled when WebView
3378 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003379 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003380 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003381 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003382 pageDown();
3383 }
3384 return true;
3385 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003386 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003387 event.startTracking();
3388 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003389 case KeyEvent.KEYCODE_FORWARD:
3390 if (!noModifiers) break;
3391 tab.goForward();
3392 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003393 case KeyEvent.KEYCODE_DPAD_LEFT:
3394 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003395 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003396 return true;
3397 }
3398 break;
3399 case KeyEvent.KEYCODE_DPAD_RIGHT:
3400 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003401 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003402 return true;
3403 }
3404 break;
3405 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003406 if (ctrl) {
3407 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003408 return true;
3409 }
3410 break;
Michael Kolba4183062011-01-16 10:43:21 -08003411// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003412 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003413 if (ctrl ) {
3414 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003415 return true;
3416 }
3417 break;
Michael Kolba4183062011-01-16 10:43:21 -08003418// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003419// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003420// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003421// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003422// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003423// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003424// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003425// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003426// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003427// case KeyEvent.KEYCODE_M: // unused
3428// case KeyEvent.KEYCODE_N: // in Chrome: new window
3429// case KeyEvent.KEYCODE_O: // in Chrome: open file
3430// case KeyEvent.KEYCODE_P: // in Chrome: print page
3431// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003432// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003433// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3434 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003435 // we can't use the ctrl/shift flags, they check for
3436 // exclusive use of a modifier
3437 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003438 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003439 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003440 } else {
3441 openTabToHomePage();
3442 }
3443 return true;
3444 }
3445 break;
3446// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3447// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003448// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003449// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3450// case KeyEvent.KEYCODE_Y: // unused
3451// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003452 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003453 // it is a regular key and webview is not null
3454 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003455 }
3456
John Reck9c35b9c2012-05-30 10:08:50 -07003457 @Override
3458 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003459 switch(keyCode) {
3460 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003461 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003462 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003463 return true;
3464 }
3465 break;
3466 }
3467 return false;
3468 }
3469
John Reck9c35b9c2012-05-30 10:08:50 -07003470 @Override
3471 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003472 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003473 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003474 if (KeyEvent.KEYCODE_MENU == keyCode
3475 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003476 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003477 }
3478 }
Cary Clark160bbb92011-01-10 11:17:07 -05003479 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003480 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003481 case KeyEvent.KEYCODE_BACK:
3482 if (event.isTracking() && !event.isCanceled()) {
3483 onBackKey();
3484 return true;
3485 }
3486 break;
3487 }
3488 return false;
3489 }
3490
3491 public boolean isMenuDown() {
3492 return mMenuIsDown;
3493 }
3494
John Reck9c35b9c2012-05-30 10:08:50 -07003495 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003496 public void setupAutoFill(Message message) {
3497 // Open the settings activity at the AutoFill profile fragment so that
3498 // the user can create a new profile. When they return, we will dispatch
3499 // the message so that we can autofill the form using their new profile.
Ben Murdoch8029a772010-11-16 11:58:21 +00003500 mAutoFillSetupMessage = message;
Enrico Rosd6efa972014-12-02 19:49:59 -08003501 BrowserPreferencesPage.startPreferenceFragmentForResult(mActivity,
3502 AutoFillSettingsFragment.class.getName(), AUTOFILL_SETUP);
Ben Murdoch8029a772010-11-16 11:58:21 +00003503 }
John Reckb3417f02011-01-14 11:01:05 -08003504
John Reck9c35b9c2012-05-30 10:08:50 -07003505 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003506 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003507 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003508 return true;
3509 }
3510
John Reck1cf4b792011-07-26 10:22:22 -07003511 @Override
3512 public boolean shouldCaptureThumbnails() {
3513 return mUi.shouldCaptureThumbnails();
3514 }
3515
Michael Kolbc3af0672011-08-09 10:24:41 -07003516 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003517 public boolean supportsVoice() {
3518 PackageManager pm = mActivity.getPackageManager();
3519 List activities = pm.queryIntentActivities(new Intent(
3520 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3521 return activities.size() != 0;
3522 }
3523
3524 @Override
3525 public void startVoiceRecognizer() {
3526 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003527 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003528 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3529 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3530 mActivity.startActivityForResult(voice, VOICE_RESULT);
3531 }
3532
Pankaj Garg7b279f62014-08-12 14:47:18 -07003533 public void setWindowDimming(float level) {
Vivek Sekharb991edb2014-12-17 18:18:07 -08003534 if (mLevel == level)
3535 return;
3536 mLevel = level;
3537 if (level != 0.0f) {
Pankaj Garg7b279f62014-08-12 14:47:18 -07003538 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3539 lp.dimAmount = level;
3540 mActivity.getWindow().setAttributes(lp);
3541 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3542 } else {
3543 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3544 }
3545 }
3546
Michael Kolb0b129122012-06-04 16:31:58 -07003547 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003548 public void setBlockEvents(boolean block) {
3549 mBlockEvents = block;
3550 }
3551
John Reck9c35b9c2012-05-30 10:08:50 -07003552 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003553 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003554 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003555 }
3556
John Reck9c35b9c2012-05-30 10:08:50 -07003557 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003558 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003559 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003560 }
3561
John Reck9c35b9c2012-05-30 10:08:50 -07003562 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003563 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003564 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003565 }
3566
John Reck9c35b9c2012-05-30 10:08:50 -07003567 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003568 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003569 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003570 }
3571
John Reck9c35b9c2012-05-30 10:08:50 -07003572 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003573 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003574 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003575 }
3576
Pankaj Garg49b79252014-11-07 17:33:41 -08003577 @Override
3578 public boolean shouldShowAppMenu() {
3579 return true;
3580 }
3581
3582 @Override
3583 public int getMenuThemeResourceId() {
3584 return R.style.OverflowMenuTheme;
3585 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003586}