blob: e643d456fe42e1d64f79a74d9b3e3798e4aa6345 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080020
luxiaolb40014b2013-07-19 10:01:43 +080021import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.ProgressDialog;
Vivek Sekharce2a4832014-03-26 13:26:53 -070025import android.content.Context;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070028import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.content.ContentValues;
John Reck68234a92012-04-19 15:27:12 -070030import android.content.DialogInterface;
31import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070032import android.content.Intent;
33import android.content.pm.PackageManager;
34import android.content.pm.ResolveInfo;
35import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070036import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050037import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.database.Cursor;
39import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020040import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080042import android.graphics.BitmapFactory;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Canvas;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080044import android.graphics.Paint;
45import android.graphics.Rect;
kaiyiza016da12013-08-26 17:50:22 +080046import android.net.ConnectivityManager;
47import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.net.Uri;
49import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080050import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080051import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.os.AsyncTask;
53import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070054import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.os.Handler;
56import android.os.Message;
57import android.os.PowerManager;
58import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.provider.ContactsContract;
61import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080062import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070063import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070064import android.text.TextUtils;
65import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080066import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070067import android.view.ActionMode;
68import android.view.ContextMenu;
69import android.view.ContextMenu.ContextMenuInfo;
70import android.view.Gravity;
71import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070072import android.view.Menu;
73import android.view.MenuInflater;
74import android.view.MenuItem;
75import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070076import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070077import android.view.View;
George Mount387d45d2011-10-07 15:57:53 -070078import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import android.webkit.WebChromeClient.CustomViewCallback;
Leon Scrogginsac993842011-02-02 12:54:07 -050081import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070082
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080083import org.codeaurora.swe.CookieManager;
84import org.codeaurora.swe.CookieSyncManager;
85import org.codeaurora.swe.HttpAuthHandler;
86import org.codeaurora.swe.SslErrorHandler;
87import org.codeaurora.swe.WebSettings;
88import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070089import org.codeaurora.swe.WebBackForwardList;
90import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080091
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070092import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070093import com.android.browser.R;
94import com.android.browser.IntentHandler.UrlData;
95import com.android.browser.UI.ComboViews;
96import com.android.browser.mynavigation.AddMyNavigationPage;
97import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070098import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070099import com.android.browser.platformsupport.BrowserContract;
100import com.android.browser.platformsupport.WebAddress;
101import com.android.browser.platformsupport.BrowserContract.Images;
102import com.android.browser.provider.BrowserProvider2.Thumbnails;
103import com.android.browser.provider.SnapshotProvider.Snapshots;
104import com.android.browser.reflect.ReflectHelper;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700107import java.io.File;
108import java.io.FileOutputStream;
109import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700110import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700111import java.text.DateFormat;
112import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700113import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700115import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700116import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800117import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200118import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700119import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700120
121/**
122 * Controller for browser
123 */
124public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700125 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700126
127 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800128 private static final String SEND_APP_ID_EXTRA =
129 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700130 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800131
Vivek Sekharb54614f2014-05-01 19:03:37 -0700132 // Remind switch to data connection if wifi is unavailable
133 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800134
Michael Kolb8233fac2010-10-26 16:08:53 -0700135 // public message ids
136 public final static int LOAD_URL = 1001;
137 public final static int STOP_LOAD = 1002;
138
139 // Message Ids
140 private static final int FOCUS_NODE_HREF = 102;
141 private static final int RELEASE_WAKELOCK = 107;
142
143 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
144
145 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800146 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700147
148 private static final int EMPTY_MENU = -1;
149
Michael Kolb8233fac2010-10-26 16:08:53 -0700150 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700151 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700152 final static int PREFERENCES_PAGE = 3;
153 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000154 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700155 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800156 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000157
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
159
160 // As the ids are dynamically created, we can't guarantee that they will
161 // be in sequence, so this static array maps ids to a window number.
162 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
163 { R.id.window_one_menu_id, R.id.window_two_menu_id,
164 R.id.window_three_menu_id, R.id.window_four_menu_id,
165 R.id.window_five_menu_id, R.id.window_six_menu_id,
166 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
167
168 // "source" parameter for Google search through search key
169 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
170 // "source" parameter for Google search through simplily type
171 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
172
George Mount387d45d2011-10-07 15:57:53 -0700173 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700174 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
175
John Reckd7dd9b22011-08-30 09:18:29 -0700176 // A bitmap that is re-used in createScreenshot as scratch space
177 private static Bitmap sThumbnailBitmap;
178
Michael Kolb8233fac2010-10-26 16:08:53 -0700179 private Activity mActivity;
180 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700181 private HomepageHandler mHomepageHandler;
182 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700183 private BrowserSettings mSettings;
184 private WebViewFactory mFactory;
185
186 private WakeLock mWakeLock;
187
188 private UrlHandler mUrlHandler;
189 private UploadHandler mUploadHandler;
190 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700191 private PageDialogsHandler mPageDialogsHandler;
192 private NetworkStateHandler mNetworkHandler;
193
Ben Murdoch8029a772010-11-16 11:58:21 +0000194 private Message mAutoFillSetupMessage;
195
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800197 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700198
Michael Kolb8233fac2010-10-26 16:08:53 -0700199 // FIXME, temp address onPrepareMenu performance problem.
200 // When we move everything out of view, we should rewrite this.
201 private int mCurrentMenuState = 0;
202 private int mMenuState = R.id.MAIN_MENU;
203 private int mOldMenuState = EMPTY_MENU;
204 private Menu mCachedMenu;
205
Michael Kolb8233fac2010-10-26 16:08:53 -0700206 private boolean mMenuIsDown;
207
208 // For select and find, we keep track of the ActionMode so that
209 // finish() can be called as desired.
210 private ActionMode mActionMode;
211
212 /**
213 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
214 * of whether the configuration has changed. The first onMenuOpened call
215 * after a configuration change is simply a reopening of the same menu
216 * (i.e. mIconView did not change).
217 */
218 private boolean mConfigChanged;
219
220 /**
221 * Keeps track of whether the options menu is open. This is important in
222 * determining whether to show or hide the title bar overlay
223 */
224 private boolean mOptionsMenuOpen;
225
226 /**
227 * Whether or not the options menu is in its bigger, popup menu form. When
228 * true, we want the title bar overlay to be gone. When false, we do not.
229 * Only meaningful if mOptionsMenuOpen is true.
230 */
231 private boolean mExtendedMenuOpen;
232
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 private boolean mActivityPaused = true;
234 private boolean mLoadStopped;
235
236 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500237 // Checks to see when the bookmarks database has changed, and updates the
238 // Tabs' notion of whether they represent bookmarked sites.
239 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700240 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700241
Michael Kolbc3af0672011-08-09 10:24:41 -0700242 private boolean mBlockEvents;
243
Michael Kolb0b129122012-06-04 16:31:58 -0700244 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800245 private boolean mUpdateMyNavThumbnail;
246 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700247
George Mount3636d0a2011-11-21 09:08:21 -0800248 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 mActivity = browser;
250 mSettings = BrowserSettings.getInstance();
251 mTabControl = new TabControl(this);
252 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700253 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800254 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700255 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700256
257 mUrlHandler = new UrlHandler(this);
258 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
260
Michael Kolb8233fac2010-10-26 16:08:53 -0700261 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500262 mBookmarksObserver = new ContentObserver(mHandler) {
263 @Override
264 public void onChange(boolean selfChange) {
265 int size = mTabControl.getTabCount();
266 for (int i = 0; i < size; i++) {
267 mTabControl.getTab(i).updateBookmarkedStatus();
268 }
269 }
270
271 };
272 browser.getContentResolver().registerContentObserver(
273 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700274
275 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700276 mHomepageHandler = new HomepageHandler(browser, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 }
278
John Reck9c35b9c2012-05-30 10:08:50 -0700279 @Override
280 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800281 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800282 // mCrashRecoverHandler has any previously saved state.
283 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700284 }
285
George Mount3636d0a2011-11-21 09:08:21 -0800286 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800287 // we dont want to ever recover incognito tabs
288 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700289
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700291 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000293
Michael Kolbc831b632011-05-11 09:30:34 -0700294 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500295 // Not able to restore so we go ahead and clear session cookies. We
296 // must do this before trying to login the user as we don't want to
297 // clear any session cookies set during login.
298 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700299 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800300 if (intent == null) {
301 // This won't happen under common scenarios. The icicle is
302 // not null, but there aren't any tabs to restore.
303 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700304 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800305 final Bundle extra = intent.getExtras();
306 // Create an initial tab.
307 // If the intent is ACTION_VIEW and data is not null, the Browser is
308 // invoked to view the content by another application. In this case,
309 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800310 UrlData urlData = null;
311 if (intent.getData() != null
312 && Intent.ACTION_VIEW.equals(intent.getAction())
313 && intent.getData().toString().startsWith("content://")) {
314 urlData = new UrlData(intent.getData().toString());
315 } else {
316 urlData = IntentHandler.getUrlDataFromIntent(intent);
317 }
George Mount3636d0a2011-11-21 09:08:21 -0800318 Tab t = null;
319 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700320 String landingPage = mActivity.getResources().getString(
321 R.string.def_landing_page);
322 if (!landingPage.isEmpty()) {
323 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800324 } else {
325 t = openTabToHomePage();
326 }
George Mount3636d0a2011-11-21 09:08:21 -0800327 } else {
328 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700329 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800330 }
331 if (t != null) {
332 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
333 }
334 WebView webView = t.getWebView();
335 if (extra != null) {
336 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
337 if (scale > 0 && scale <= 1000) {
338 webView.setInitialScale(scale);
339 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700340 }
341 }
John Reckd8c74522011-06-14 08:45:00 -0700342 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700343 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700344 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500345 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700346 List<Tab> tabs = mTabControl.getTabs();
347 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700348
John Reck1cf4b792011-07-26 10:22:22 -0700349 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700350 //handle restored pages that may require a JS interface
351 if (t.getWebView() != null) {
352 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
353 if (backForwardList != null) {
354 for (int i = 0; i < backForwardList.getSize(); i++) {
355 WebHistoryItem item = backForwardList.getItemAtIndex(i);
356 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
357 }
358 }
359 }
John Reck1cf4b792011-07-26 10:22:22 -0700360 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700361 if (t != mTabControl.getCurrentTab()) {
362 t.pause();
363 }
John Reck1cf4b792011-07-26 10:22:22 -0700364 }
365 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700366 if (tabs.size() == 0) {
367 openTabToHomePage();
368 }
John Reck1cf4b792011-07-26 10:22:22 -0700369 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700370 // TabControl.restoreState() will create a new tab even if
371 // restoring the state fails.
372 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800373 // Intent is non-null when framework thinks the browser should be
374 // launching with a new intent (icicle is null).
375 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700376 mIntentHandler.onNewIntent(intent);
377 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700378 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700380 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800381 if (jsFlags.trim().length() != 0) {
382 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700383 }
George Mount3636d0a2011-11-21 09:08:21 -0800384 if (intent != null
385 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700386 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800387 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700388 }
389
John Reck1cf4b792011-07-26 10:22:22 -0700390 private static class PruneThumbnails implements Runnable {
391 private Context mContext;
392 private List<Long> mIds;
393
394 PruneThumbnails(Context context, List<Long> preserveIds) {
395 mContext = context.getApplicationContext();
396 mIds = preserveIds;
397 }
398
399 @Override
400 public void run() {
401 ContentResolver cr = mContext.getContentResolver();
402 if (mIds == null || mIds.size() == 0) {
403 cr.delete(Thumbnails.CONTENT_URI, null, null);
404 } else {
405 int length = mIds.size();
406 StringBuilder where = new StringBuilder();
407 where.append(Thumbnails._ID);
408 where.append(" not in (");
409 for (int i = 0; i < length; i++) {
410 where.append(mIds.get(i));
411 if (i < (length - 1)) {
412 where.append(",");
413 }
414 }
415 where.append(")");
416 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
417 }
418 }
419
420 }
421
Michael Kolb1514bb72010-11-22 09:11:48 -0800422 @Override
423 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700424 return mFactory;
425 }
426
427 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800428 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800429 if (tab.hasCrashed)
430 tab.showCrashView();
431 else
432 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800433 }
434
435 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800436 public void createSubWindow(Tab tab) {
437 endActionMode();
438 WebView mainView = tab.getWebView();
439 WebView subView = mFactory.createWebView((mainView == null)
440 ? false
441 : mainView.isPrivateBrowsingEnabled());
442 mUi.createSubWindow(tab, subView);
443 }
444
445 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700446 public Context getContext() {
447 return mActivity;
448 }
449
450 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700451 public Activity getActivity() {
452 return mActivity;
453 }
454
455 void setUi(UI ui) {
456 mUi = ui;
457 }
458
Michael Kolbaf63dba2012-05-16 12:58:05 -0700459 @Override
460 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700461 return mSettings;
462 }
463
464 IntentHandler getIntentHandler() {
465 return mIntentHandler;
466 }
467
468 @Override
469 public UI getUi() {
470 return mUi;
471 }
472
473 int getMaxTabs() {
474 return mActivity.getResources().getInteger(R.integer.max_tabs);
475 }
476
477 @Override
478 public TabControl getTabControl() {
479 return mTabControl;
480 }
481
Michael Kolb1bf23132010-11-19 12:55:12 -0800482 @Override
483 public List<Tab> getTabs() {
484 return mTabControl.getTabs();
485 }
486
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 private void startHandler() {
488 mHandler = new Handler() {
489
490 @Override
491 public void handleMessage(Message msg) {
492 switch (msg.what) {
493 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700494 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 break;
496 case FOCUS_NODE_HREF:
497 {
498 String url = (String) msg.getData().get("url");
499 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500500 String src = (String) msg.getData().get("src");
501 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 if (TextUtils.isEmpty(url)) {
503 break;
504 }
505 HashMap focusNodeMap = (HashMap) msg.obj;
506 WebView view = (WebView) focusNodeMap.get("webview");
507 // Only apply the action if the top window did not change.
508 if (getCurrentTopWebView() != view) {
509 break;
510 }
511 switch (msg.arg1) {
512 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700513 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700514 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500515 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700516 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500517 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500518 case R.id.open_newtab_context_menu_id:
519 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700520 openTab(url, parent,
521 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500522 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 case R.id.copy_link_context_menu_id:
524 copy(url);
525 break;
526 case R.id.save_link_context_menu_id:
527 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500528 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800529 mActivity, url, view.getSettings().getUserAgentString(),
530 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700531 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700532 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700533 if(title == null || title == "")
534 title = url;
535
536 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
537 //SWE TODO: No thumbnail support for the url obtained via
538 //browser context menu as its not loaded in webview.
539 if (bookmarkIntent != null) {
540 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
541 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
542 mActivity.startActivity(bookmarkIntent);
543 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700544 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700545 }
546 break;
547 }
548
549 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700550 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700551 break;
552
553 case STOP_LOAD:
554 stopLoading();
555 break;
556
557 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700558 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700559 mWakeLock.release();
560 // if we reach here, Browser should be still in the
561 // background loading after WAKELOCK_TIMEOUT (5-min).
562 // To avoid burning the battery, stop loading.
563 mTabControl.stopAllLoading();
564 }
565 break;
566
567 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800568 Tab tab = (Tab) msg.obj;
569 if (tab != null) {
570 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700571 }
572 break;
kaiyiz591110b2013-08-06 17:11:06 +0800573 case OPEN_MENU:
574 if (!mOptionsMenuOpen && mActivity != null ) {
575 mActivity.openOptionsMenu();
576 }
577 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700578 }
579 }
580 };
581
582 }
583
John Reckef654f12011-07-12 16:42:08 -0700584 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200585 public Tab getCurrentTab() {
586 return mTabControl.getCurrentTab();
587 }
588
Michael Kolbba99c5d2010-11-29 14:57:41 -0800589 @Override
590 public void shareCurrentPage() {
591 shareCurrentPage(mTabControl.getCurrentTab());
592 }
593
594 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700595 if (tab == null || tab.getWebView() == null)
596 return;
597
598 final Tab mytab = tab;
599 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
600 @Override
601 public void onReceiveValue(Bitmap bitmap) {
602 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
603 mytab.getFavicon(), bitmap);
604 }
605 };
606
607 createScreenshotAsync(
608 tab.getWebView(),
609 getDesiredThumbnailWidth(mActivity),
610 getDesiredThumbnailHeight(mActivity),
611 new ValueCallback<Bitmap>() {
612 @Override
613 public void onReceiveValue(Bitmap bitmap) {
614 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
615 mytab.getFavicon(), bitmap);
616 }
617 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800618 }
619
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 /**
621 * Share a page, providing the title, url, favicon, and a screenshot. Uses
622 * an {@link Intent} to launch the Activity chooser.
623 * @param c Context used to launch a new Activity.
624 * @param title Title of the page. Stored in the Intent with
625 * {@link Intent#EXTRA_SUBJECT}
626 * @param url URL of the page. Stored in the Intent with
627 * {@link Intent#EXTRA_TEXT}
628 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
629 * with {@link Browser#EXTRA_SHARE_FAVICON}
630 * @param screenshot Bitmap of a screenshot of the page. Stored in the
631 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
632 */
633 static final void sharePage(Context c, String title, String url,
634 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700635
636 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
637 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
638 R.layout.app_row, sDialog.getApps());
639 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 }
641
642 private void copy(CharSequence text) {
643 ClipboardManager cm = (ClipboardManager) mActivity
644 .getSystemService(Context.CLIPBOARD_SERVICE);
645 cm.setText(text);
646 }
647
648 // lifecycle
649
John Reck9c35b9c2012-05-30 10:08:50 -0700650 @Override
651 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800653 // update the menu in case of a locale change
654 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800655 if (mOptionsMenuOpen) {
656 mActivity.closeOptionsMenu();
657 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
658 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700659 if (mPageDialogsHandler != null) {
660 mPageDialogsHandler.onConfigurationChanged(config);
661 }
662 mUi.onConfigurationChanged(config);
663 }
664
665 @Override
666 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700667 if (!mUi.isWebShowing()) {
668 mUi.showWeb(false);
669 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 mIntentHandler.onNewIntent(intent);
671 }
672
John Reck9c35b9c2012-05-30 10:08:50 -0700673 @Override
674 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800675 if (mUi.isCustomViewShowing()) {
676 hideCustomView();
677 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 if (mActivityPaused) {
679 Log.e(LOGTAG, "BrowserActivity is already paused.");
680 return;
681 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800683 Tab tab = mTabControl.getCurrentTab();
684 if (tab != null) {
685 tab.pause();
686 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700687 if (mWakeLock == null) {
688 PowerManager pm = (PowerManager) mActivity
689 .getSystemService(Context.POWER_SERVICE);
690 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
691 }
Michael Kolb70976932010-11-30 11:34:01 -0800692 mWakeLock.acquire();
693 mHandler.sendMessageDelayed(mHandler
694 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
695 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 }
697 mUi.onPause();
698 mNetworkHandler.onPause();
699
700 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100701 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 }
703
John Reck9c35b9c2012-05-30 10:08:50 -0700704 @Override
705 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800707 Bundle saveState = createSaveState();
708
709 // crash recovery manages all save & restore state
710 mCrashRecoveryHandler.writeState(saveState);
711 mSettings.setLastRunPaused(true);
712 }
713
714 /**
715 * Save the current state to outState. Does not write the state to
716 * disk.
717 * @return Bundle containing the current state of all tabs.
718 */
719 /* package */ Bundle createSaveState() {
720 Bundle saveState = new Bundle();
721 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800722 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700723 }
724
John Reck9c35b9c2012-05-30 10:08:50 -0700725 @Override
726 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 if (!mActivityPaused) {
728 Log.e(LOGTAG, "BrowserActivity is already resumed.");
729 return;
730 }
George Mount3636d0a2011-11-21 09:08:21 -0800731 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700732 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800733 Tab current = mTabControl.getCurrentTab();
734 if (current != null) {
735 current.resume();
736 resumeWebViewTimers(current);
737 }
John Reckf57c0292011-07-21 18:15:39 -0700738 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200739
Michael Kolb8233fac2010-10-26 16:08:53 -0700740 mUi.onResume();
741 mNetworkHandler.onResume();
742 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100743 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700744 if (mVoiceResult != null) {
745 mUi.onVoiceResult(mVoiceResult);
746 mVoiceResult = null;
747 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800748 if (current != null && current.hasCrashed) {
749 current.showCrashView();
750 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700751 }
752
John Reckf57c0292011-07-21 18:15:39 -0700753 private void releaseWakeLock() {
754 if (mWakeLock != null && mWakeLock.isHeld()) {
755 mHandler.removeMessages(RELEASE_WAKELOCK);
756 mWakeLock.release();
757 }
758 }
759
Michael Kolb70976932010-11-30 11:34:01 -0800760 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800761 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800762 * @param tab guaranteed non-null
763 */
764 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700765 boolean inLoad = tab.inPageLoad();
766 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
767 CookieSyncManager.getInstance().startSync();
768 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100769 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700770 }
771 }
772
Michael Kolb70976932010-11-30 11:34:01 -0800773 /**
774 * Pause all WebView timers using the WebView of the given tab
775 * @param tab
776 * @return true if the timers are paused or tab is null
777 */
778 private boolean pauseWebViewTimers(Tab tab) {
779 if (tab == null) {
780 return true;
781 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700782 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100783 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700784 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700785 }
Michael Kolb70976932010-11-30 11:34:01 -0800786 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700787 }
788
John Reck9c35b9c2012-05-30 10:08:50 -0700789 @Override
790 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800791 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
793 mUploadHandler = null;
794 }
795 if (mTabControl == null) return;
796 mUi.onDestroy();
797 // Remove the current tab and sub window
798 Tab t = mTabControl.getCurrentTab();
799 if (t != null) {
800 dismissSubWindow(t);
801 removeTab(t);
802 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500803 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 // Destroy all the tabs
805 mTabControl.destroy();
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 }
807
808 protected boolean isActivityPaused() {
809 return mActivityPaused;
810 }
811
John Reck9c35b9c2012-05-30 10:08:50 -0700812 @Override
813 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 mTabControl.freeMemory();
815 }
816
817 @Override
818 public boolean shouldShowErrorConsole() {
819 return mShouldShowErrorConsole;
820 }
821
822 protected void setShouldShowErrorConsole(boolean show) {
823 if (show == mShouldShowErrorConsole) {
824 // Nothing to do.
825 return;
826 }
827 mShouldShowErrorConsole = show;
828 Tab t = mTabControl.getCurrentTab();
829 if (t == null) {
830 // There is no current tab so we cannot toggle the error console
831 return;
832 }
833 mUi.setShouldShowErrorConsole(t, show);
834 }
835
836 @Override
837 public void stopLoading() {
838 mLoadStopped = true;
839 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700841 if (w != null) {
842 w.stopLoading();
843 mUi.onPageStopped(tab);
844 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 }
846
847 boolean didUserStopLoading() {
848 return mLoadStopped;
849 }
850
kaiyiza016da12013-08-26 17:50:22 +0800851 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700852 final String reminderType = getContext().getResources().getString(
853 R.string.def_wifi_browser_interaction_remind_type);
854 final String selectionConnnection = getContext().getResources().getString(
855 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700856 final String wifiSelection = getContext().getResources().getString(
857 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700858
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700859 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
860 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700861 return;
862
kaiyiza016da12013-08-26 17:50:22 +0800863 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700864 Context.CONNECTIVITY_SERVICE);
865 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700866 WifiManager wifiMgr = (WifiManager) this.getContext()
867 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700868 if (networkInfo == null
869 || (networkInfo != null && (networkInfo.getType() !=
870 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700871 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
872 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700873 List<ScanResult> list = wifiMgr.getScanResults();
874 // Have no AP's for Wifi's fall back to data
875 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700876 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700877 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
878 this.getContext().startActivity(intent);
879 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700880 // Request to select Wifi AP
881 try {
882 Intent intent = new Intent(wifiSelection);
883 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
884 this.getContext().startActivity(intent);
885 } catch (Exception e) {
886 String err_msg = this.getContext().getString(
887 R.string.acivity_not_found, wifiSelection);
888 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
889 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700890 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700891 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800892 }
893 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700894
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 // WebViewController
896
897 @Override
John Reck324d4402011-01-11 16:56:42 -0800898 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700899
900 // We've started to load a new page. If there was a pending message
901 // to save a screenshot then we will now take the new page and save
902 // an incorrect screenshot. Therefore, remove any pending thumbnail
903 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700904 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700905
906 // reset sync timer to avoid sync starts during loading a page
907 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700908 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700909 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700910 boolean networkNotifier =
911 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700912 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700913 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800914 } else {
915 if (!mNetworkHandler.isNetworkUp()) {
916 view.setNetworkAvailable(false);
917 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700918 }
919
920 // when BrowserActivity just starts, onPageStarted may be called before
921 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
922 // to start the timer. As we won't switch tabs while an activity is in
923 // pause state, we can ensure calling resume and pause in pair.
924 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800925 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700926 }
927 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 endActionMode();
929
John Reck30c714c2010-12-16 17:30:34 -0800930 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700931
John Reck324d4402011-01-11 16:56:42 -0800932 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700933 // update the bookmark database for favicon
934 maybeUpdateFavicon(tab, null, url, favicon);
935
936 Performance.tracePageStart(url);
937
938 // Performance probe
939 if (false) {
940 Performance.onPageStarted();
941 }
942
943 }
944
945 @Override
John Reck324d4402011-01-11 16:56:42 -0800946 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700947 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800948 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200949
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 // Performance probe
951 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800952 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700953 }
954
Tarun Nainani8eb00912014-07-17 12:28:32 -0700955 tab.onPageFinished();
956
Michael Kolb8233fac2010-10-26 16:08:53 -0700957 Performance.tracePageFinished();
958 }
959
960 @Override
John Reck30c714c2010-12-16 17:30:34 -0800961 public void onProgressChanged(Tab tab) {
962 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700963
964 if (newProgress == 100) {
965 CookieSyncManager.getInstance().sync();
966 // onProgressChanged() may continue to be called after the main
967 // frame has finished loading, as any remaining sub frames continue
968 // to load. We'll only get called once though with newProgress as
969 // 100 when everything is loaded. (onPageFinished is called once
970 // when the main frame completes loading regardless of the state of
971 // any sub frames so calls to onProgressChanges may continue after
972 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800973 if (tab.inPageLoad()) {
974 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200975 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
976 // pause the WebView timer and release the wake lock if it is
977 // finished while BrowserActivity is in pause state.
978 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 }
John Reckd9862372012-02-21 15:04:50 -0800980 if (!tab.isPrivateBrowsingEnabled()
981 && !TextUtils.isEmpty(tab.getUrl())
982 && !tab.isSnapshot()) {
983 // Only update the bookmark screenshot if the user did not
984 // cancel the load early and there is not already
985 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700986 if (tab.shouldUpdateThumbnail() &&
987 (tab.inForeground() && !didUserStopLoading()
988 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800989 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
990 mHandler.sendMessageDelayed(mHandler.obtainMessage(
991 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800992 1500);
John Reckd9862372012-02-21 15:04:50 -0800993 }
994 }
995 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800997 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700998 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800999 // still loading
1000 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001001 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001002 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 }
1004 }
John Reck30c714c2010-12-16 17:30:34 -08001005 mUi.onProgressChanged(tab);
1006 }
1007
1008 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001009 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001010 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001011 }
1012
1013 @Override
1014 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001015 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001016 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001017 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1019 return;
1020 }
1021 // Update the title in the history database if not in private browsing mode
1022 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001023 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 }
1025 }
1026
1027 @Override
1028 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001029 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001030 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1031 }
1032
1033 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001034 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1035 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001036 }
1037
1038 @Override
1039 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1040 if (mMenuIsDown) {
1041 // only check shortcut key when MENU is held
1042 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1043 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001045 int keyCode = event.getKeyCode();
1046 // We need to send almost every key to WebKit. However:
1047 // 1. We don't want to block the device on the renderer for
1048 // some keys like menu, home, call.
1049 // 2. There are no WebKit equivalents for some of these keys
1050 // (see app/keyboard_codes_win.h)
1051 // Note that these are not the same set as KeyEvent.isSystemKey:
1052 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1053 if (keyCode == KeyEvent.KEYCODE_MENU ||
1054 keyCode == KeyEvent.KEYCODE_HOME ||
1055 keyCode == KeyEvent.KEYCODE_BACK ||
1056 keyCode == KeyEvent.KEYCODE_CALL ||
1057 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1058 keyCode == KeyEvent.KEYCODE_POWER ||
1059 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1060 keyCode == KeyEvent.KEYCODE_CAMERA ||
1061 keyCode == KeyEvent.KEYCODE_FOCUS ||
1062 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1063 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1064 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1065 return true;
1066 }
1067
1068 // We also have to intercept some shortcuts before we send them to the ContentView.
1069 if (event.isCtrlPressed() && (
1070 keyCode == KeyEvent.KEYCODE_TAB ||
1071 keyCode == KeyEvent.KEYCODE_W ||
1072 keyCode == KeyEvent.KEYCODE_F4)) {
1073 return true;
1074 }
1075
1076 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 }
1078
1079 @Override
John Reck997b1b72012-04-19 18:08:25 -07001080 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 if (!isActivityPaused()) {
1082 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001083 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 } else {
John Reck997b1b72012-04-19 18:08:25 -07001085 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001086 }
1087 }
John Reck997b1b72012-04-19 18:08:25 -07001088 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001089 }
1090
1091 @Override
John Reck324d4402011-01-11 16:56:42 -08001092 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001093 boolean disableHistoryWrites =
1094 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1095
1096 // Don't save anything in private browsing mode or when explicitly set
1097 // not to write history via an overlay
1098 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001099 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001100
John Reck324d4402011-01-11 16:56:42 -08001101 if (TextUtils.isEmpty(url)
1102 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 return;
1104 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001105
John Reckf57c0292011-07-21 18:15:39 -07001106 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001107 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001108 }
1109
1110 @Override
1111 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1112 AsyncTask<Void, Void, String[]> task =
1113 new AsyncTask<Void, Void, String[]>() {
1114 @Override
1115 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001116 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 }
1118 @Override
1119 public void onPostExecute(String[] result) {
1120 callback.onReceiveValue(result);
1121 }
1122 };
1123 task.execute();
1124 }
1125
1126 @Override
1127 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1128 final HttpAuthHandler handler, final String host,
1129 final String realm) {
1130 String username = null;
1131 String password = null;
1132
1133 boolean reuseHttpAuthUsernamePassword
1134 = handler.useHttpAuthUsernamePassword();
1135
1136 if (reuseHttpAuthUsernamePassword && view != null) {
1137 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1138 if (credentials != null && credentials.length == 2) {
1139 username = credentials[0];
1140 password = credentials[1];
1141 }
1142 }
1143
1144 if (username != null && password != null) {
1145 handler.proceed(username, password);
1146 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001147 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1149 } else {
1150 handler.cancel();
1151 }
1152 }
1153 }
1154
1155 @Override
1156 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001157 String contentDisposition, String mimetype, String referer,
1158 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001159 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001160 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001161 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001162 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001163 // This Tab was opened for the sole purpose of downloading a
1164 // file. Remove it.
1165 if (tab == mTabControl.getCurrentTab()) {
1166 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001167 if (tab.getDerivedFromIntent())
1168 closeTab(tab);
1169 else
1170 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001171 } else {
1172 // In this case, it is not.
1173 closeTab(tab);
1174 }
1175 }
1176 }
1177
1178 @Override
1179 public Bitmap getDefaultVideoPoster() {
1180 return mUi.getDefaultVideoPoster();
1181 }
1182
1183 @Override
1184 public View getVideoLoadingProgressView() {
1185 return mUi.getVideoLoadingProgressView();
1186 }
1187
1188 @Override
1189 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1190 SslError error) {
1191 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1192 }
1193
1194 // helper method
1195
1196 /*
1197 * Update the favorites icon if the private browsing isn't enabled and the
1198 * icon is valid.
1199 */
1200 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1201 final String url, Bitmap favicon) {
1202 if (favicon == null) {
1203 return;
1204 }
1205 if (!tab.isPrivateBrowsingEnabled()) {
1206 Bookmarks.updateFavicon(mActivity
1207 .getContentResolver(), originalUrl, url, favicon);
1208 }
1209 }
1210
Leon Scroggins4cd97792010-12-03 15:31:56 -05001211 @Override
1212 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001213 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001214 mUi.bookmarkedStatusHasChanged(tab);
1215 }
1216
Michael Kolb8233fac2010-10-26 16:08:53 -07001217 // end WebViewController
1218
1219 protected void pageUp() {
1220 getCurrentTopWebView().pageUp(false);
1221 }
1222
1223 protected void pageDown() {
1224 getCurrentTopWebView().pageDown(false);
1225 }
1226
1227 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001228 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001229 public void editUrl() {
1230 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001231 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 }
1233
John Reck9c35b9c2012-05-30 10:08:50 -07001234 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001235 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001236 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001237 if (tab.inForeground()) {
1238 if (mUi.isCustomViewShowing()) {
1239 callback.onCustomViewHidden();
1240 return;
1241 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001242 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 // Save the menu state and set it to empty while the custom
1244 // view is showing.
1245 mOldMenuState = mMenuState;
1246 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001247 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001248 }
1249 }
1250
1251 @Override
1252 public void hideCustomView() {
1253 if (mUi.isCustomViewShowing()) {
1254 mUi.onHideCustomView();
1255 // Reset the old menu state.
1256 mMenuState = mOldMenuState;
1257 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001258 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 }
1260 }
1261
John Reck9c35b9c2012-05-30 10:08:50 -07001262 @Override
1263 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 Intent intent) {
1265 if (getCurrentTopWebView() == null) return;
1266 switch (requestCode) {
1267 case PREFERENCES_PAGE:
1268 if (resultCode == Activity.RESULT_OK && intent != null) {
1269 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001270 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001271 mTabControl.removeParentChildRelationShips();
1272 }
1273 }
1274 break;
1275 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001276 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001277 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001278 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001280 case AUTOFILL_SETUP:
1281 // Determine whether a profile was actually set up or not
1282 // and if so, send the message back to the WebTextView to
1283 // fill the form with the new profile.
1284 if (getSettings().getAutoFillProfile() != null) {
1285 mAutoFillSetupMessage.sendToTarget();
1286 mAutoFillSetupMessage = null;
1287 }
1288 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001289 case COMBO_VIEW:
1290 if (intent == null || resultCode != Activity.RESULT_OK) {
1291 break;
1292 }
John Reck3ba45532011-08-11 16:26:53 -07001293 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001294 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1295 Tab t = getCurrentTab();
1296 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001297 mUpdateMyNavThumbnail = true;
1298 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001299 loadUrl(t, uri.toString());
1300 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1301 String[] urls = intent.getStringArrayExtra(
1302 ComboViewActivity.EXTRA_OPEN_ALL);
1303 Tab parent = getCurrentTab();
1304 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001305 if (url != null) {
1306 parent = openTab(url, parent,
1307 !mSettings.openInBackground(), true);
1308 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001309 }
1310 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1311 long id = intent.getLongExtra(
1312 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1313 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001314 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001315 }
1316 }
1317 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001318 case VOICE_RESULT:
1319 if (resultCode == Activity.RESULT_OK && intent != null) {
1320 ArrayList<String> results = intent.getStringArrayListExtra(
1321 RecognizerIntent.EXTRA_RESULTS);
1322 if (results.size() >= 1) {
1323 mVoiceResult = results.get(0);
1324 }
1325 }
1326 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001327 case MY_NAVIGATION:
1328 if (intent == null || resultCode != Activity.RESULT_OK) {
1329 break;
1330 }
1331
1332 if (intent.getBooleanExtra("need_refresh", false) &&
1333 getCurrentTopWebView() != null) {
1334 getCurrentTopWebView().reload();
1335 }
1336 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 default:
1338 break;
1339 }
1340 getCurrentTopWebView().requestFocus();
1341 }
1342
1343 /**
1344 * Open the Go page.
1345 * @param startWithHistory If true, open starting on the history tab.
1346 * Otherwise, start with the bookmarks tab.
1347 */
1348 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001349 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001350 if (mTabControl.getCurrentWebView() == null) {
1351 return;
1352 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001353 // clear action mode
1354 if (isInCustomActionMode()) {
1355 endActionMode();
1356 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001357 Bundle extras = new Bundle();
1358 // Disable opening in a new window if we have maxed out the windows
1359 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1360 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001361 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001362 }
1363
1364 // combo view callbacks
1365
Michael Kolb8233fac2010-10-26 16:08:53 -07001366 // key handling
1367 protected void onBackKey() {
1368 if (!mUi.onBackKey()) {
1369 WebView subwindow = mTabControl.getCurrentSubWindow();
1370 if (subwindow != null) {
1371 if (subwindow.canGoBack()) {
1372 subwindow.goBack();
1373 } else {
1374 dismissSubWindow(mTabControl.getCurrentTab());
1375 }
1376 } else {
1377 goBackOnePageOrQuit();
1378 }
1379 }
1380 }
1381
Michael Kolb4bd767d2011-05-27 11:33:55 -07001382 protected boolean onMenuKey() {
1383 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001384 }
1385
Michael Kolb8233fac2010-10-26 16:08:53 -07001386 // menu handling and state
1387 // TODO: maybe put into separate handler
1388
John Reck9c35b9c2012-05-30 10:08:50 -07001389 @Override
1390 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001391 if (mMenuState == EMPTY_MENU) {
1392 return false;
1393 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 MenuInflater inflater = mActivity.getMenuInflater();
1395 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 return true;
1397 }
1398
John Reck9c35b9c2012-05-30 10:08:50 -07001399 @Override
1400 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001402 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001403 return;
1404 }
1405 if (!(v instanceof WebView)) {
1406 return;
1407 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001408 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 WebView.HitTestResult result = webview.getHitTestResult();
1410 if (result == null) {
1411 return;
1412 }
1413
1414 int type = result.getType();
1415 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1416 Log.w(LOGTAG,
1417 "We should not show context menu when nothing is touched");
1418 return;
1419 }
1420 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1421 // let TextView handles context menu
1422 return;
1423 }
1424
1425 // Note, http://b/issue?id=1106666 is requesting that
1426 // an inflated menu can be used again. This is not available
1427 // yet, so inflate each time (yuk!)
1428 MenuInflater inflater = mActivity.getMenuInflater();
1429 inflater.inflate(R.menu.browsercontext, menu);
1430
1431 // Show the correct menu group
1432 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001433 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001434 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 menu.setGroupVisible(R.id.PHONE_MENU,
1436 type == WebView.HitTestResult.PHONE_TYPE);
1437 menu.setGroupVisible(R.id.EMAIL_MENU,
1438 type == WebView.HitTestResult.EMAIL_TYPE);
1439 menu.setGroupVisible(R.id.GEO_MENU,
1440 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001441 String itemUrl = null;
1442 String url = webview.getOriginalUrl();
1443 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1444 itemUrl = Uri.decode(navigationUrl);
1445 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1446 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1447 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1448 } else {
1449 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1450 }
1451 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1452 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1453 } else {
1454 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1455
1456 menu.setGroupVisible(R.id.IMAGE_MENU,
1457 type == WebView.HitTestResult.IMAGE_TYPE
1458 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1459 menu.setGroupVisible(R.id.ANCHOR_MENU,
1460 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1461 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001462 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1463 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001464 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001465 // Setup custom handling depending on the type
1466 switch (type) {
1467 case WebView.HitTestResult.PHONE_TYPE:
1468 menu.setHeaderTitle(Uri.decode(extra));
1469 menu.findItem(R.id.dial_context_menu_id).setIntent(
1470 new Intent(Intent.ACTION_VIEW, Uri
1471 .parse(WebView.SCHEME_TEL + extra)));
1472 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1473 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1474 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1475 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1476 addIntent);
1477 menu.findItem(R.id.copy_phone_context_menu_id)
1478 .setOnMenuItemClickListener(
1479 new Copy(extra));
1480 break;
1481
1482 case WebView.HitTestResult.EMAIL_TYPE:
1483 menu.setHeaderTitle(extra);
1484 menu.findItem(R.id.email_context_menu_id).setIntent(
1485 new Intent(Intent.ACTION_VIEW, Uri
1486 .parse(WebView.SCHEME_MAILTO + extra)));
1487 menu.findItem(R.id.copy_mail_context_menu_id)
1488 .setOnMenuItemClickListener(
1489 new Copy(extra));
1490 break;
1491
1492 case WebView.HitTestResult.GEO_TYPE:
1493 menu.setHeaderTitle(extra);
1494 menu.findItem(R.id.map_context_menu_id).setIntent(
1495 new Intent(Intent.ACTION_VIEW, Uri
1496 .parse(WebView.SCHEME_GEO
1497 + URLEncoder.encode(extra))));
1498 menu.findItem(R.id.copy_geo_context_menu_id)
1499 .setOnMenuItemClickListener(
1500 new Copy(extra));
1501 break;
1502
1503 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1504 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001505 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001506 // decide whether to show the open link in new tab option
1507 boolean showNewTab = mTabControl.canCreateNewTab();
1508 MenuItem newTabItem
1509 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001510 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001511 ? R.string.contextmenu_openlink_newwindow_background
1512 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001513 newTabItem.setVisible(showNewTab);
1514 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001515 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1516 newTabItem.setOnMenuItemClickListener(
1517 new MenuItem.OnMenuItemClickListener() {
1518 @Override
1519 public boolean onMenuItemClick(MenuItem item) {
1520 final HashMap<String, WebView> hrefMap =
1521 new HashMap<String, WebView>();
1522 hrefMap.put("webview", webview);
1523 final Message msg = mHandler.obtainMessage(
1524 FOCUS_NODE_HREF,
1525 R.id.open_newtab_context_menu_id,
1526 0, hrefMap);
1527 webview.requestFocusNodeHref(msg);
1528 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001529 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001530 });
1531 } else {
1532 newTabItem.setOnMenuItemClickListener(
1533 new MenuItem.OnMenuItemClickListener() {
1534 @Override
1535 public boolean onMenuItemClick(MenuItem item) {
1536 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001537 openTab(extra, parent,
1538 !mSettings.openInBackground(),
1539 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001540 return true;
1541 }
1542 });
1543 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001544 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001545 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1546 menu.setHeaderTitle(navigationUrl);
1547 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1548
1549 if (itemUrl != null) {
1550 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1551 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1552 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1553 @Override
1554 public boolean onMenuItemClick(MenuItem item) {
1555 final Intent intent = new Intent(Controller.this
1556 .getContext(),
1557 AddMyNavigationPage.class);
1558 Bundle bundle = new Bundle();
1559 String url = Uri.decode(navigationUrl);
1560 bundle.putBoolean("isAdding", false);
1561 bundle.putString("url", url);
1562 bundle.putString("name", getNameFromUrl(url));
1563 intent.putExtra("websites", bundle);
1564 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1565 return false;
1566 }
1567 });
1568 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1569 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1570 @Override
1571 public boolean onMenuItemClick(MenuItem item) {
1572 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1573 return false;
1574 }
1575 });
1576 }
1577 } else {
1578 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1579 }
1580 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1582 break;
1583 }
1584 // otherwise fall through to handle image part
1585 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001586 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1587 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001588 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1589 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001590 shareItem.setOnMenuItemClickListener(
1591 new MenuItem.OnMenuItemClickListener() {
1592 @Override
1593 public boolean onMenuItemClick(MenuItem item) {
1594 sharePage(mActivity, null, extra, null,
1595 null);
1596 return true;
1597 }
1598 }
1599 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001601 menu.findItem(R.id.view_image_context_menu_id)
1602 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1603 @Override
1604 public boolean onMenuItemClick(MenuItem item) {
1605 openTab(extra, mTabControl.getCurrentTab(), true, true);
1606 return false;
1607 }
1608 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001609 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1610 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1611 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001612 menu.findItem(R.id.set_wallpaper_context_menu_id).
1613 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1614 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001615 break;
1616
1617 default:
1618 Log.w(LOGTAG, "We should not get here.");
1619 break;
1620 }
1621 //update the ui
1622 mUi.onContextMenuCreated(menu);
1623 }
1624
kaiyiz6e5b3e02013-08-19 20:02:01 +08001625 public void startAddMyNavigation(String url) {
1626 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1627 Bundle bundle = new Bundle();
1628 bundle.putBoolean("isAdding", true);
1629 bundle.putString("url", url);
1630 bundle.putString("name", getNameFromUrl(url));
1631 intent.putExtra("websites", bundle);
1632 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1633 }
1634
1635 private void showMyNavigationDeleteDialog(final String itemUrl) {
1636 new AlertDialog.Builder(this.getContext())
1637 .setTitle(R.string.my_navigation_delete_label)
1638 .setIcon(android.R.drawable.ic_dialog_alert)
1639 .setMessage(R.string.my_navigation_delete_msg)
1640 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1641 @Override
1642 public void onClick(DialogInterface dialog, int whichButton) {
1643 deleteMyNavigationItem(itemUrl);
1644 }
1645 })
1646 .setNegativeButton(R.string.cancel, null)
1647 .show();
1648 }
1649
1650 private void deleteMyNavigationItem(final String itemUrl) {
1651 ContentResolver cr = this.getContext().getContentResolver();
1652 Cursor cursor = null;
1653
1654 try {
1655 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1656 new String[] {
1657 MyNavigationUtil.ID
1658 }, "url = ?", new String[] {
1659 itemUrl
1660 }, null);
1661 if (null != cursor && cursor.moveToFirst()) {
1662 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1663 cursor.getLong(0));
1664
1665 ContentValues values = new ContentValues();
1666 values.put(MyNavigationUtil.TITLE, "");
1667 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1668 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1669 ByteArrayOutputStream os = new ByteArrayOutputStream();
1670 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1671 R.raw.my_navigation_add);
1672 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1673 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1674 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1675 cr.update(uri, values, null, null);
1676 } else {
1677 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1678 }
1679 } catch (IllegalStateException e) {
1680 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1681 } finally {
1682 if (null != cursor) {
1683 cursor.close();
1684 }
1685 }
1686
1687 if (getCurrentTopWebView() != null) {
1688 getCurrentTopWebView().reload();
1689 }
1690 }
1691
1692 private String getNameFromUrl(String itemUrl) {
1693 ContentResolver cr = this.getContext().getContentResolver();
1694 Cursor cursor = null;
1695 String name = null;
1696
1697 try {
1698 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1699 new String[] {
1700 MyNavigationUtil.TITLE
1701 }, "url = ?", new String[] {
1702 itemUrl
1703 }, null);
1704 if (null != cursor && cursor.moveToFirst()) {
1705 name = cursor.getString(0);
1706 } else {
1707 Log.e(LOGTAG, "this item does not exist!");
1708 }
1709 } catch (IllegalStateException e) {
1710 Log.e(LOGTAG, "getNameFromUrl", e);
1711 } finally {
1712 if (null != cursor) {
1713 cursor.close();
1714 }
1715 }
1716 return name;
1717 }
1718
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001719 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
1720 if (bitmap == null) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001721 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1722 return;
1723 }
1724
1725 final ContentResolver cr = mActivity.getContentResolver();
1726 new AsyncTask<Void, Void, Void>() {
1727 @Override
1728 protected Void doInBackground(Void... unused) {
1729 ContentResolver cr = mActivity.getContentResolver();
1730 Cursor cursor = null;
1731 try {
1732 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1733 new String[] {
1734 MyNavigationUtil.ID
1735 }, "url = ?", new String[] {
1736 itemUrl
1737 }, null);
1738 if (null != cursor && cursor.moveToFirst()) {
1739 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001740 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001741
1742 ContentValues values = new ContentValues();
1743 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1744 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1745 cursor.getLong(0));
1746 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1747 cr.update(uri, values, null, null);
1748 os.close();
1749 }
1750 } catch (IllegalStateException e) {
1751 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1752 } catch (IOException e) {
1753 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1754 } finally {
1755 if (null != cursor) {
1756 cursor.close();
1757 }
1758 }
1759 return null;
1760 }
1761 }.execute();
1762 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001763 /**
1764 * As the menu can be open when loading state changes
1765 * we must manually update the state of the stop/reload menu
1766 * item
1767 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001768 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001769 if (menu == null) {
1770 return;
1771 }
1772 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001773 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001774 menu.findItem(R.id.stop_menu_id):
1775 menu.findItem(R.id.reload_menu_id);
1776 if (src != null) {
1777 dest.setIcon(src.getIcon());
1778 dest.setTitle(src.getTitle());
1779 }
1780 }
1781
John Reck9c35b9c2012-05-30 10:08:50 -07001782 @Override
1783 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001784 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001785 // hold on to the menu reference here; it is used by the page callbacks
1786 // to update the menu based on loading state
1787 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001788 // Note: setVisible will decide whether an item is visible; while
1789 // setEnabled() will decide whether an item is enabled, which also means
1790 // whether the matching shortcut key will function.
1791 switch (mMenuState) {
1792 case EMPTY_MENU:
1793 if (mCurrentMenuState != mMenuState) {
1794 menu.setGroupVisible(R.id.MAIN_MENU, false);
1795 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1796 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1797 }
1798 break;
1799 default:
1800 if (mCurrentMenuState != mMenuState) {
1801 menu.setGroupVisible(R.id.MAIN_MENU, true);
1802 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1803 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1804 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001805 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001806 break;
1807 }
1808 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001809 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001810 }
1811
Michael Kolb4bf79712011-07-14 14:18:12 -07001812 @Override
1813 public void updateMenuState(Tab tab, Menu menu) {
1814 boolean canGoBack = false;
1815 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001816 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001817 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001818 // Following flag is used to identify schemes for which the LIVE_MENU
1819 // items defined in res/menu/browser.xml should be enabled
1820 boolean isLiveScheme = false;
1821 boolean isPageFinished = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001822 if (tab != null) {
1823 canGoBack = tab.canGoBack();
1824 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001825 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001826 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001827 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
1828 isPageFinished = tab.getPageFinishedStatus();
Michael Kolb4bf79712011-07-14 14:18:12 -07001829 }
1830 final MenuItem back = menu.findItem(R.id.back_menu_id);
1831 back.setEnabled(canGoBack);
1832
1833 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001834
1835 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1836 forward.setEnabled(canGoForward);
1837
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001838 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1839 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001840 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001841 if (source != null && dest != null) {
1842 dest.setTitle(source.getTitle());
1843 dest.setIcon(source.getIcon());
1844 }
John Recke1a03a32011-09-14 17:04:16 -07001845 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001846
1847 // decide whether to show the share link option
1848 PackageManager pm = mActivity.getPackageManager();
1849 Intent send = new Intent(Intent.ACTION_SEND);
1850 send.setType("text/plain");
1851 ResolveInfo ri = pm.resolveActivity(send,
1852 PackageManager.MATCH_DEFAULT_ONLY);
1853 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1854
1855 boolean isNavDump = mSettings.enableNavDump();
1856 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1857 nav.setVisible(isNavDump);
1858 nav.setEnabled(isNavDump);
1859
1860 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001861 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1862 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001863 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001864 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001865 menu.setGroupEnabled(R.id.OFFLINE_READING, isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001866 // history and snapshots item are the members of COMBO menu group,
1867 // so if show history item, only make snapshots item invisible.
1868 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001869
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001870 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001871 }
1872
John Reck9c35b9c2012-05-30 10:08:50 -07001873 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001874 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001875 if (null == getCurrentTopWebView()) {
1876 return false;
1877 }
1878 if (mMenuIsDown) {
1879 // The shortcut action consumes the MENU. Even if it is still down,
1880 // it won't trigger the next shortcut action. In the case of the
1881 // shortcut action triggering a new activity, like Bookmarks, we
1882 // won't get onKeyUp for MENU. So it is important to reset it here.
1883 mMenuIsDown = false;
1884 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001885 if (mUi.onOptionsItemSelected(item)) {
1886 // ui callback handled it
1887 return true;
1888 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001889 switch (item.getItemId()) {
1890 // -- Main menu
1891 case R.id.new_tab_menu_id:
1892 openTabToHomePage();
1893 break;
1894
1895 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001896 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 break;
1898
Afzal Najamd4e33312012-04-26 01:54:01 -04001899 case R.id.close_other_tabs_id:
1900 closeOtherTabs();
1901 break;
1902
Michael Kolb8233fac2010-10-26 16:08:53 -07001903 case R.id.goto_menu_id:
1904 editUrl();
1905 break;
1906
1907 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001908 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1909 break;
1910
1911 case R.id.history_menu_id:
1912 bookmarksOrHistoryPicker(ComboViews.History);
1913 break;
1914
1915 case R.id.snapshots_menu_id:
1916 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001917 break;
1918
Michael Kolb8233fac2010-10-26 16:08:53 -07001919 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001920 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001921 break;
1922
1923 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001924 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001925 stopLoading();
1926 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001927 Tab currentTab = mTabControl.getCurrentTab();
1928 if (currentTab.hasCrashed) {
1929 currentTab.replaceCrashView(getCurrentTopWebView(),
1930 currentTab.getViewContainer());
1931 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001932 getCurrentTopWebView().reload();
1933 }
1934 break;
1935
1936 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001937 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 break;
1939
1940 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001941 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001942 break;
1943
1944 case R.id.close_menu_id:
1945 // Close the subwindow if it exists.
1946 if (mTabControl.getCurrentSubWindow() != null) {
1947 dismissSubWindow(mTabControl.getCurrentTab());
1948 break;
1949 }
1950 closeCurrentTab();
1951 break;
1952
kaiyiza8b6dbb2013-07-29 18:11:22 +08001953 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001954 Object[] params = { new String("persist.debug.browsermonkeytest")};
1955 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001956 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001957 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001958 if (ret != null && ret.equals("enable"))
1959 break;
luxiaolb40014b2013-07-19 10:01:43 +08001960 showExitDialog(mActivity);
1961 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001962 case R.id.homepage_menu_id:
1963 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001964 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001965 break;
1966
1967 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001968 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001969 break;
1970
1971 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001972 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001973 break;
1974
John Reck2bc80422011-06-30 15:11:49 -07001975 case R.id.save_snapshot_menu_id:
1976 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001977 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001978 createScreenshotAsync(
1979 source.getWebView(),
1980 getDesiredThumbnailWidth(mActivity),
1981 getDesiredThumbnailHeight(mActivity),
1982 new ValueCallback<Bitmap>() {
1983 @Override
1984 public void onReceiveValue(Bitmap bitmap) {
1985 new SaveSnapshotTask(source, bitmap).execute();
1986 }
1987 });
Leon Scrogginsac993842011-02-02 12:54:07 -05001988 break;
1989
Michael Kolb8233fac2010-10-26 16:08:53 -07001990 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001991 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 break;
1993
John Recke1a03a32011-09-14 17:04:16 -07001994 case R.id.snapshot_go_live:
1995 goLive();
1996 return true;
1997
Michael Kolb8233fac2010-10-26 16:08:53 -07001998 case R.id.share_page_menu_id:
1999 Tab currentTab = mTabControl.getCurrentTab();
2000 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002001 return false;
2002 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002003 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002004 break;
2005
2006 case R.id.dump_nav_menu_id:
2007 getCurrentTopWebView().debugDump();
2008 break;
2009
Michael Kolb8233fac2010-10-26 16:08:53 -07002010 case R.id.zoom_in_menu_id:
2011 getCurrentTopWebView().zoomIn();
2012 break;
2013
2014 case R.id.zoom_out_menu_id:
2015 getCurrentTopWebView().zoomOut();
2016 break;
2017
2018 case R.id.view_downloads_menu_id:
2019 viewDownloads();
2020 break;
2021
John Reck42229bc2011-08-19 13:26:43 -07002022 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002023 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002024 break;
2025
Michael Kolb8233fac2010-10-26 16:08:53 -07002026 case R.id.window_one_menu_id:
2027 case R.id.window_two_menu_id:
2028 case R.id.window_three_menu_id:
2029 case R.id.window_four_menu_id:
2030 case R.id.window_five_menu_id:
2031 case R.id.window_six_menu_id:
2032 case R.id.window_seven_menu_id:
2033 case R.id.window_eight_menu_id:
2034 {
2035 int menuid = item.getItemId();
2036 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2037 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2038 Tab desiredTab = mTabControl.getTab(id);
2039 if (desiredTab != null &&
2040 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002041 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 }
2043 break;
2044 }
2045 }
2046 }
2047 break;
2048
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002049 case R.id.about_menu_id:
2050 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2051 builder.setTitle(R.string.about);
2052 builder.setCancelable(true);
2053 String ua = "";
2054 final WebView currentWebView = getCurrentWebView();
2055 if (currentWebView != null) {
2056 final WebSettings s = currentWebView.getSettings();
2057 if (s != null) {
2058 ua = s.getUserAgentString();
2059 }
2060 }
2061 builder.setMessage("Agent:" + ua);
2062 builder.setPositiveButton(android.R.string.ok, null);
2063 builder.create().show();
2064 break;
2065
Michael Kolb8233fac2010-10-26 16:08:53 -07002066 default:
2067 return false;
2068 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 return true;
2070 }
2071
John Reck68234a92012-04-19 15:27:12 -07002072 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2073 implements OnCancelListener {
2074
2075 private Tab mTab;
2076 private Dialog mProgressDialog;
2077 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002078 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002079
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002080 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002081 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002082 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002083 }
2084
2085 @Override
2086 protected void onPreExecute() {
2087 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2088 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2089 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002090 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002091 }
2092
2093 @Override
2094 protected Long doInBackground(Void... params) {
2095 if (!mTab.saveViewState(mValues)) {
2096 return null;
2097 }
2098 if (isCancelled()) {
2099 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2100 File file = mActivity.getFileStreamPath(path);
2101 if (!file.delete()) {
2102 file.deleteOnExit();
2103 }
2104 return null;
2105 }
2106 final ContentResolver cr = mActivity.getContentResolver();
2107 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2108 if (result == null) {
2109 return null;
2110 }
2111 long id = ContentUris.parseId(result);
2112 return id;
2113 }
2114
2115 @Override
2116 protected void onPostExecute(Long id) {
2117 if (isCancelled()) {
2118 return;
2119 }
2120 mProgressDialog.dismiss();
2121 if (id == null) {
2122 Toast.makeText(mActivity, R.string.snapshot_failed,
2123 Toast.LENGTH_SHORT).show();
2124 return;
2125 }
2126 Bundle b = new Bundle();
2127 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2128 mUi.showComboView(ComboViews.Snapshots, b);
2129 }
2130
2131 @Override
2132 public void onCancel(DialogInterface dialog) {
2133 cancel(true);
2134 }
2135 }
2136
Michael Kolb80f75082012-04-10 10:50:06 -07002137 @Override
2138 public void toggleUserAgent() {
2139 WebView web = getCurrentWebView();
2140 mSettings.toggleDesktopUseragent(web);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002141 web.reload();
Michael Kolb80f75082012-04-10 10:50:06 -07002142 }
2143
2144 @Override
2145 public void findOnPage() {
2146 getCurrentTopWebView().showFindDialog(null, true);
2147 }
2148
2149 @Override
2150 public void openPreferences() {
2151 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2152 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2153 getCurrentTopWebView().getUrl());
2154 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2155 }
2156
2157 @Override
2158 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002159 WebView w = getCurrentTopWebView();
2160 if (w == null)
2161 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002162 final Intent i = createBookmarkCurrentPageIntent(false);
2163 createScreenshotAsync(
2164 w, getDesiredThumbnailWidth(mActivity),
2165 getDesiredThumbnailHeight(mActivity),
2166 new ValueCallback<Bitmap>() {
2167 @Override
2168 public void onReceiveValue(Bitmap bitmap) {
2169 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2170 mActivity.startActivity(i);
2171 }
2172 });
Michael Kolb80f75082012-04-10 10:50:06 -07002173 }
2174
John Recke1a03a32011-09-14 17:04:16 -07002175 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002176 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002177 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002178 boolean onlySingleTabRemaining = false;
2179 if (mTabControl.getTabCount() > 1) {
2180 // destroy the old snapshot tab
2181 closeCurrentTab();
2182 } else {
2183 onlySingleTabRemaining = true;
2184 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002185 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002186 if (onlySingleTabRemaining) {
2187 closeTab(t);
2188 }
2189
Tarun Nainani8eb00912014-07-17 12:28:32 -07002190 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002191 }
2192
luxiaolb40014b2013-07-19 10:01:43 +08002193 private void showExitDialog(final Activity activity) {
2194 new AlertDialog.Builder(activity)
2195 .setTitle(R.string.exit_browser_title)
2196 .setIcon(android.R.drawable.ic_dialog_alert)
2197 .setMessage(R.string.exit_browser_msg)
2198 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2199 public void onClick(DialogInterface dialog, int which) {
2200 activity.moveTaskToBack(true);
2201 dialog.dismiss();
2202 }
2203 })
2204 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2205 public void onClick(DialogInterface dialog, int which) {
2206 activity.finish();
2207 mHandler.postDelayed(new Runnable() {
2208 @Override
2209 public void run() {
2210 // TODO Auto-generated method stub
2211 mCrashRecoveryHandler.clearState(true);
2212 int pid = android.os.Process.myPid();
2213 android.os.Process.killProcess(pid);
2214 }
2215 }, 300);
2216 dialog.dismiss();
2217 }
2218 })
2219 .show();
2220 }
Michael Kolb315d5022011-10-13 12:47:11 -07002221 @Override
2222 public void showPageInfo() {
2223 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2224 }
2225
John Reck9c35b9c2012-05-30 10:08:50 -07002226 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002227 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002228 // Let the History and Bookmark fragments handle menus they created.
2229 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2230 return false;
2231 }
2232
Michael Kolb8233fac2010-10-26 16:08:53 -07002233 int id = item.getItemId();
2234 boolean result = true;
2235 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002236 // -- Browser context menu
2237 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002238 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002239 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002240 case R.id.copy_link_context_menu_id:
2241 final WebView webView = getCurrentTopWebView();
2242 if (null == webView) {
2243 result = false;
2244 break;
2245 }
2246 final HashMap<String, WebView> hrefMap =
2247 new HashMap<String, WebView>();
2248 hrefMap.put("webview", webView);
2249 final Message msg = mHandler.obtainMessage(
2250 FOCUS_NODE_HREF, id, 0, hrefMap);
2251 webView.requestFocusNodeHref(msg);
2252 break;
2253
2254 default:
2255 // For other context menus
2256 result = onOptionsItemSelected(item);
2257 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002258 return result;
2259 }
2260
2261 /**
2262 * support programmatically opening the context menu
2263 */
2264 public void openContextMenu(View view) {
2265 mActivity.openContextMenu(view);
2266 }
2267
2268 /**
2269 * programmatically open the options menu
2270 */
2271 public void openOptionsMenu() {
2272 mActivity.openOptionsMenu();
2273 }
2274
John Reck9c35b9c2012-05-30 10:08:50 -07002275 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002276 public boolean onMenuOpened(int featureId, Menu menu) {
2277 if (mOptionsMenuOpen) {
2278 if (mConfigChanged) {
2279 // We do not need to make any changes to the state of the
2280 // title bar, since the only thing that happened was a
2281 // change in orientation
2282 mConfigChanged = false;
2283 } else {
2284 if (!mExtendedMenuOpen) {
2285 mExtendedMenuOpen = true;
2286 mUi.onExtendedMenuOpened();
2287 } else {
2288 // Switching the menu back to icon view, so show the
2289 // title bar once again.
2290 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002291 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002292 }
2293 }
2294 } else {
2295 // The options menu is closed, so open it, and show the title
2296 mOptionsMenuOpen = true;
2297 mConfigChanged = false;
2298 mExtendedMenuOpen = false;
2299 mUi.onOptionsMenuOpened();
2300 }
2301 return true;
2302 }
2303
John Reck9c35b9c2012-05-30 10:08:50 -07002304 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002305 public void onOptionsMenuClosed(Menu menu) {
2306 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002307 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 }
2309
John Reck9c35b9c2012-05-30 10:08:50 -07002310 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002311 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002312 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002313 }
2314
2315 // Helper method for getting the top window.
2316 @Override
2317 public WebView getCurrentTopWebView() {
2318 return mTabControl.getCurrentTopWebView();
2319 }
2320
2321 @Override
2322 public WebView getCurrentWebView() {
2323 return mTabControl.getCurrentWebView();
2324 }
2325
2326 /*
2327 * This method is called as a result of the user selecting the options
2328 * menu to see the download window. It shows the download window on top of
2329 * the current window.
2330 */
2331 void viewDownloads() {
2332 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2333 mActivity.startActivity(intent);
2334 }
2335
John Reck30b065e2011-07-19 10:58:05 -07002336 int getActionModeHeight() {
2337 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2338 new int[] { android.R.attr.actionBarSize });
2339 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2340 actionBarSizeTypedArray.recycle();
2341 return size;
2342 }
2343
Michael Kolb8233fac2010-10-26 16:08:53 -07002344 // action mode
2345
John Reck9c35b9c2012-05-30 10:08:50 -07002346 @Override
2347 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002348 mUi.onActionModeStarted(mode);
2349 mActionMode = mode;
2350 }
2351
2352 /*
2353 * True if a custom ActionMode (i.e. find or select) is in use.
2354 */
2355 @Override
2356 public boolean isInCustomActionMode() {
2357 return mActionMode != null;
2358 }
2359
2360 /*
2361 * End the current ActionMode.
2362 */
2363 @Override
2364 public void endActionMode() {
2365 if (mActionMode != null) {
2366 mActionMode.finish();
2367 }
2368 }
2369
2370 /*
2371 * Called by find and select when they are finished. Replace title bars
2372 * as necessary.
2373 */
John Reck9c35b9c2012-05-30 10:08:50 -07002374 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002375 public void onActionModeFinished(ActionMode mode) {
2376 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002377 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002378 mActionMode = null;
2379 }
2380
2381 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002382 final Tab tab = getCurrentTab();
2383 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002384 }
2385
2386 // bookmark handling
2387
2388 /**
2389 * add the current page as a bookmark to the given folder id
2390 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002391 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002392 * bookmarked, and if it is, edit that bookmark. If false, and
2393 * the site is already bookmarked, do not attempt to edit the
2394 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002395 */
2396 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002397 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002398 WebView w = getCurrentTopWebView();
2399 if (w == null) {
2400 return null;
2401 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002402 Intent i = new Intent(mActivity,
2403 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002404 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2405 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2406 String touchIconUrl = w.getTouchIconUrl();
2407 if (touchIconUrl != null) {
2408 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2409 WebSettings settings = w.getSettings();
2410 if (settings != null) {
2411 i.putExtra(AddBookmarkPage.USER_AGENT,
2412 settings.getUserAgentString());
2413 }
2414 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002415 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002416 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002417 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002418 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2419 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002420 // Put the dialog at the upper right of the screen, covering the
2421 // star on the title bar.
2422 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002423 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002424 }
2425
2426 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002427 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002428 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002429 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002430 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002431 }
2432
Vivek Sekharb54614f2014-05-01 19:03:37 -07002433 @Override
2434 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2435 boolean capture) {
2436 mUploadHandler = new UploadHandler(this);
2437 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2438 }
2439
Michael Kolb8233fac2010-10-26 16:08:53 -07002440 // thumbnails
2441
2442 /**
2443 * Return the desired width for thumbnail screenshots, which are stored in
2444 * the database, and used on the bookmarks screen.
2445 * @param context Context for finding out the density of the screen.
2446 * @return desired width for thumbnail screenshot.
2447 */
2448 static int getDesiredThumbnailWidth(Context context) {
2449 return context.getResources().getDimensionPixelOffset(
2450 R.dimen.bookmarkThumbnailWidth);
2451 }
2452
2453 /**
2454 * Return the desired height for thumbnail screenshots, which are stored in
2455 * the database, and used on the bookmarks screen.
2456 * @param context Context for finding out the density of the screen.
2457 * @return desired height for thumbnail screenshot.
2458 */
2459 static int getDesiredThumbnailHeight(Context context) {
2460 return context.getResources().getDimensionPixelOffset(
2461 R.dimen.bookmarkThumbnailHeight);
2462 }
2463
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002464 static void createScreenshotAsync(WebView view, int width, int height,
2465 final ValueCallback<Bitmap> cb) {
2466 if (view == null || width == 0 || height == 0) {
2467 return;
2468 }
2469
2470 view.getContentBitmapAsync(
2471 (float) width / view.getWidth(),
2472 new Rect(),
2473 new ValueCallback<Bitmap>() {
2474 @Override
2475 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002476 if (bitmap != null)
2477 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2478 cb.onReceiveValue(bitmap);
2479 }});
2480 }
2481
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002482 private void updateScreenshot(final Tab tab) {
2483 createScreenshotAsync(
2484 tab.getWebView(),
2485 getDesiredThumbnailWidth(mActivity),
2486 getDesiredThumbnailHeight(mActivity),
2487 new ValueCallback<Bitmap>() {
2488 @Override
2489 public void onReceiveValue(Bitmap bitmap) {
2490 updateScreenshot(tab, bitmap);
2491 }
2492 });
2493 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002494
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002495 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002496 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 // FIXME: Would like to make sure there is actually something to
2498 // draw, but the API for that (WebViewCore.pictureReady()) is not
2499 // currently accessible here.
2500
John Reck34ef2672011-02-10 11:30:55 -08002501 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002502 if (view == null) {
2503 // Tab was destroyed
2504 return;
2505 }
John Reck34ef2672011-02-10 11:30:55 -08002506 final String url = tab.getUrl();
2507 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002508 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002509 if (TextUtils.isEmpty(url)) {
2510 return;
2511 }
2512
kaiyiz6e5b3e02013-08-19 20:02:01 +08002513 //update My Navigation Thumbnails
2514 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2515 if (isMyNavigationUrl) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002516 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002517 }
John Reck34ef2672011-02-10 11:30:55 -08002518 // Only update thumbnails for web urls (http(s)://), not for
2519 // about:, javascript:, data:, etc...
2520 // Unless it is a bookmarked site, then always update
2521 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2522 return;
2523 }
2524
kaiyiz6e5b3e02013-08-19 20:02:01 +08002525 if (url != null && mUpdateMyNavThumbnailUrl != null
2526 && Patterns.WEB_URL.matcher(url).matches()
2527 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2528 String urlHost = (new WebAddress(url)).getHost();
2529 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2530 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2531 || bookmarkHost.length() == 0) {
2532 return;
2533 }
2534 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2535 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2536 bookmarkHost.length());
2537 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2538 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2539 if (!bookmarkDomain.equals(urlDomain)) {
2540 return;
2541 }
2542 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002543 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002544 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2545 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2546 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2547 500);
2548 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002549 return;
2550 }
2551
2552 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002553 new AsyncTask<Void, Void, Void>() {
2554 @Override
2555 protected Void doInBackground(Void... unused) {
2556 Cursor cursor = null;
2557 try {
2558 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002559 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2560 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2561 : url);
2562 if (mUpdateMyNavThumbnail) {
2563 mUpdateMyNavThumbnail = false;
2564 mUpdateMyNavThumbnailUrl = null;
2565 }
John Reck34ef2672011-02-10 11:30:55 -08002566 if (cursor != null && cursor.moveToFirst()) {
2567 final ByteArrayOutputStream os =
2568 new ByteArrayOutputStream();
2569 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002570
John Reck34ef2672011-02-10 11:30:55 -08002571 ContentValues values = new ContentValues();
2572 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002573
John Reck34ef2672011-02-10 11:30:55 -08002574 do {
John Reck617fd832011-02-16 14:35:59 -08002575 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002576 cr.update(Images.CONTENT_URI, values, null, null);
2577 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 }
John Reck34ef2672011-02-10 11:30:55 -08002579 } catch (IllegalStateException e) {
2580 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002581 } catch (SQLiteException s) {
2582 // Added for possible error when user tries to remove the same bookmark
2583 // that is being updated with a screen shot
2584 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002585 } finally {
2586 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002587 }
John Reck34ef2672011-02-10 11:30:55 -08002588 return null;
2589 }
2590 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002591 }
2592
2593 private class Copy implements OnMenuItemClickListener {
2594 private CharSequence mText;
2595
John Reck9c35b9c2012-05-30 10:08:50 -07002596 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002597 public boolean onMenuItemClick(MenuItem item) {
2598 copy(mText);
2599 return true;
2600 }
2601
2602 public Copy(CharSequence toCopy) {
2603 mText = toCopy;
2604 }
2605 }
2606
Leon Scroggins63c02662010-11-18 15:16:27 -05002607 private static class Download implements OnMenuItemClickListener {
2608 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002609 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002610 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002611 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002612 private static final String FALLBACK_EXTENSION = "dat";
2613 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002614
John Reck9c35b9c2012-05-30 10:08:50 -07002615 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002616 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002617 if (DataUri.isDataUri(mText)) {
2618 saveDataUri();
2619 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002620 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002621 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002622 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002623 return true;
2624 }
2625
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002626 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2627 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002628 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002629 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002630 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002631 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002632 }
George Mount387d45d2011-10-07 15:57:53 -07002633
2634 /**
2635 * Treats mText as a data URI and writes its contents to a file
2636 * based on the current time.
2637 */
2638 private void saveDataUri() {
2639 FileOutputStream outputStream = null;
2640 try {
2641 DataUri uri = new DataUri(mText);
2642 File target = getTarget(uri);
2643 outputStream = new FileOutputStream(target);
2644 outputStream.write(uri.getData());
2645 final DownloadManager manager =
2646 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2647 manager.addCompletedDownload(target.getName(),
2648 mActivity.getTitle().toString(), false,
2649 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002650 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002651 } catch (IOException e) {
2652 Log.e(LOGTAG, "Could not save data URL");
2653 } finally {
2654 if (outputStream != null) {
2655 try {
2656 outputStream.close();
2657 } catch (IOException e) {
2658 // ignore close errors
2659 }
2660 }
2661 }
2662 }
2663
2664 /**
2665 * Creates a File based on the current time stamp and uses
2666 * the mime type of the DataUri to get the extension.
2667 */
2668 private File getTarget(DataUri uri) throws IOException {
2669 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002670 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002671 String nameBase = format.format(new Date());
2672 String mimeType = uri.getMimeType();
2673 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2674 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2675 if (extension == null) {
2676 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2677 extension = FALLBACK_EXTENSION;
2678 }
2679 extension = "." + extension; // createTempFile needs the '.'
2680 File targetFile = File.createTempFile(nameBase, extension, dir);
2681 return targetFile;
2682 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002683 }
2684
Cary Clark8974d282010-11-22 10:46:05 -05002685 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002686 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002687
John Reck9c35b9c2012-05-30 10:08:50 -07002688 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002689 public boolean onMenuItemClick(MenuItem item) {
2690 if (mWebView != null) {
2691 return mWebView.selectText();
2692 }
2693 return false;
2694 }
2695
2696 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002697 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002698 }
2699
2700 }
2701
Michael Kolb8233fac2010-10-26 16:08:53 -07002702 /********************** TODO: UI stuff *****************************/
2703
2704 // these methods have been copied, they still need to be cleaned up
2705
2706 /****************** tabs ***************************************************/
2707
2708 // basic tab interactions:
2709
2710 // it is assumed that tabcontrol already knows about the tab
2711 protected void addTab(Tab tab) {
2712 mUi.addTab(tab);
2713 }
2714
2715 protected void removeTab(Tab tab) {
2716 mUi.removeTab(tab);
2717 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002718 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002719 }
2720
Michael Kolbf2055602011-04-09 17:20:03 -07002721 @Override
2722 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002723 // monkey protection against delayed start
2724 if (tab != null) {
2725 mTabControl.setCurrentTab(tab);
2726 // the tab is guaranteed to have a webview after setCurrentTab
2727 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002728 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002729 //Purge active tabs
2730 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002731 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002732 }
2733
John Reck8bcafc12011-08-29 16:43:02 -07002734 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002735 Tab current = mTabControl.getCurrentTab();
2736 if (current != null
2737 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002738 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002739 }
2740 }
2741
John Reck26b18322011-06-21 13:08:58 -07002742 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002743 // Dismiss the subwindow if applicable.
2744 dismissSubWindow(appTab);
2745 // Since we might kill the WebView, remove it from the
2746 // content view first.
2747 mUi.detachTab(appTab);
2748 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002749 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002750 // TODO: analyze why the remove and add are necessary
2751 mUi.attachTab(appTab);
2752 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002753 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002754 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002755 } else {
2756 // If the tab was the current tab, we have to attach
2757 // it to the view system again.
2758 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002759 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002760 }
2761 }
2762
2763 // Remove the sub window if it exists. Also called by TabControl when the
2764 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002765 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002766 public void dismissSubWindow(Tab tab) {
2767 removeSubWindow(tab);
2768 // dismiss the subwindow. This will destroy the WebView.
2769 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002770 WebView wv = getCurrentTopWebView();
2771 if (wv != null) {
2772 wv.requestFocus();
2773 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002774 }
2775
2776 @Override
2777 public void removeSubWindow(Tab t) {
2778 if (t.getSubWebView() != null) {
2779 mUi.removeSubWindow(t.getSubViewContainer());
2780 }
2781 }
2782
2783 @Override
2784 public void attachSubWindow(Tab tab) {
2785 if (tab.getSubWebView() != null) {
2786 mUi.attachSubWindow(tab.getSubViewContainer());
2787 getCurrentTopWebView().requestFocus();
2788 }
2789 }
2790
Mathew Inwood29721c22011-06-29 17:55:24 +01002791 private Tab showPreloadedTab(final UrlData urlData) {
2792 if (!urlData.isPreloaded()) {
2793 return null;
2794 }
2795 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2796 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2797 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002798 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002799 // Could not submit query. Fallback to regular tab creation
2800 tabControl.destroy();
2801 return null;
2802 }
2803 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002804 // check tab count and make room for new tab
2805 if (!mTabControl.canCreateNewTab()) {
2806 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2807 if (leastUsed != null) {
2808 closeTab(leastUsed);
2809 }
2810 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002811 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002812 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002813 mTabControl.addPreloadedTab(t);
2814 addTab(t);
2815 setActiveTab(t);
2816 return t;
2817 }
2818
Michael Kolb7bcafde2011-05-09 13:55:59 -07002819 // open a non inconito tab with the given url data
2820 // and set as active tab
2821 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002822 Tab tab = showPreloadedTab(urlData);
2823 if (tab == null) {
2824 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002825 if ((tab != null) && !urlData.isEmpty()) {
2826 loadUrlDataIn(tab, urlData);
2827 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002828 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002829 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002830 }
2831
Michael Kolb843510f2010-12-09 10:51:49 -08002832 @Override
2833 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002834 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002835 }
2836
Michael Kolb8233fac2010-10-26 16:08:53 -07002837 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002838 public Tab openIncognitoTab() {
2839 return openTab(INCOGNITO_URI, true, true, false);
2840 }
2841
2842 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002843 public Tab openTab(String url, boolean incognito, boolean setActive,
2844 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002845 return openTab(url, incognito, setActive, useCurrent, null);
2846 }
2847
2848 @Override
2849 public Tab openTab(String url, Tab parent, boolean setActive,
2850 boolean useCurrent) {
2851 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2852 setActive, useCurrent, parent);
2853 }
2854
2855 public Tab openTab(String url, boolean incognito, boolean setActive,
2856 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002857 Tab tab = createNewTab(incognito, setActive, useCurrent);
2858 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002859 if (parent instanceof SnapshotTab) {
2860 addTab(tab);
2861 if (setActive)
2862 setActiveTab(tab);
2863 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002864 parent.addChildTab(tab);
2865 }
Michael Kolb519d2282011-05-09 17:03:19 -07002866 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002867 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002868 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002869 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002870 return tab;
2871 }
2872
2873 // this method will attempt to create a new tab
2874 // incognito: private browsing tab
2875 // setActive: ste tab as current tab
2876 // useCurrent: if no new tab can be created, return current tab
2877 private Tab createNewTab(boolean incognito, boolean setActive,
2878 boolean useCurrent) {
2879 Tab tab = null;
2880 if (mTabControl.canCreateNewTab()) {
2881 tab = mTabControl.createNewTab(incognito);
2882 addTab(tab);
2883 if (setActive) {
2884 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002885 } else {
2886 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002887 }
2888 } else {
2889 if (useCurrent) {
2890 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002891 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002892 } else {
2893 mUi.showMaxTabsWarning();
2894 }
2895 }
2896 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002897 }
2898
John Reck2bc80422011-06-30 15:11:49 -07002899 @Override
2900 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2901 SnapshotTab tab = null;
2902 if (mTabControl.canCreateNewTab()) {
2903 tab = mTabControl.createSnapshotTab(snapshotId);
2904 addTab(tab);
2905 if (setActive) {
2906 setActiveTab(tab);
2907 }
2908 } else {
2909 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002910 }
2911 return tab;
2912 }
2913
Michael Kolb8233fac2010-10-26 16:08:53 -07002914 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002915 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002916 * @return boolean True if we successfully switched to a different tab. If
2917 * the indexth tab is null, or if that tab is the same as
2918 * the current one, return false.
2919 */
2920 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002921 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002922 Tab currentTab = mTabControl.getCurrentTab();
2923 if (tab == null || tab == currentTab) {
2924 return false;
2925 }
2926 setActiveTab(tab);
2927 return true;
2928 }
2929
2930 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002931 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002932 closeCurrentTab(false);
2933 }
2934
2935 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002936 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002937 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002938 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002939 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002940 return;
2941 }
Michael Kolbc831b632011-05-11 09:30:34 -07002942 final Tab current = mTabControl.getCurrentTab();
2943 final int pos = mTabControl.getCurrentPosition();
2944 Tab newTab = current.getParent();
2945 if (newTab == null) {
2946 newTab = mTabControl.getTab(pos + 1);
2947 if (newTab == null) {
2948 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002949 }
2950 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002951 if (andQuit) {
2952 mTabControl.setCurrentTab(newTab);
2953 closeTab(current);
2954 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002955 // Close window
2956 closeTab(current);
2957 }
2958 }
2959
2960 /**
2961 * Close the tab, remove its associated title bar, and adjust mTabControl's
2962 * current tab to a valid value.
2963 */
2964 @Override
2965 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002966 if (tab == mTabControl.getCurrentTab()) {
2967 closeCurrentTab();
2968 } else {
2969 removeTab(tab);
2970 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002971 }
2972
Afzal Najamd4e33312012-04-26 01:54:01 -04002973 /**
2974 * Close all tabs except the current one
2975 */
2976 @Override
2977 public void closeOtherTabs() {
2978 int inactiveTabs = mTabControl.getTabCount() - 1;
2979 for (int i = inactiveTabs; i >= 0; i--) {
2980 Tab tab = mTabControl.getTab(i);
2981 if (tab != mTabControl.getCurrentTab()) {
2982 removeTab(tab);
2983 }
2984 }
2985 }
2986
Michael Kolb8233fac2010-10-26 16:08:53 -07002987 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002988 protected void loadUrlFromContext(String url) {
2989 Tab tab = getCurrentTab();
2990 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002991 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002992 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002993 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002994 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002995 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002996 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002997 }
2998 }
2999 }
3000
3001 /**
3002 * Load the URL into the given WebView and update the title bar
3003 * to reflect the new load. Call this instead of WebView.loadUrl
3004 * directly.
3005 * @param view The WebView used to load url.
3006 * @param url The URL to load.
3007 */
John Reck71e51422011-07-01 16:49:28 -07003008 @Override
3009 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003010 loadUrl(tab, url, null);
3011 }
3012
3013 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3014 if (tab != null) {
3015 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003016 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003017 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003018 if (tab.hasCrashed) {
3019 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3020 }
Michael Kolba53c9892011-10-05 13:31:40 -07003021 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003022 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003023 }
3024
3025 /**
3026 * Load UrlData into a Tab and update the title bar to reflect the new
3027 * load. Call this instead of UrlData.loadIn directly.
3028 * @param t The Tab used to load.
3029 * @param data The UrlData being loaded.
3030 */
3031 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003032 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003033 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003034 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003035 } else {
John Reck38b39652012-06-05 09:22:59 -07003036 if (t != null && data.mDisableUrlOverride) {
3037 t.disableUrlOverridingForLoad();
3038 }
John Reck26b18322011-06-21 13:08:58 -07003039 loadUrl(t, data.mUrl, data.mHeaders);
3040 }
3041 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003042 }
3043
John Reck30c714c2010-12-16 17:30:34 -08003044 @Override
3045 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003046 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003047 if (tab.canGoBack()) {
3048 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003049 } else {
John Reckef654f12011-07-12 16:42:08 -07003050 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003051 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003052 }
3053
3054 void goBackOnePageOrQuit() {
3055 Tab current = mTabControl.getCurrentTab();
3056 if (current == null) {
3057 /*
3058 * Instead of finishing the activity, simply push this to the back
3059 * of the stack and let ActivityManager to choose the foreground
3060 * activity. As BrowserActivity is singleTask, it will be always the
3061 * root of the task. So we can use either true or false for
3062 * moveTaskToBack().
3063 */
luxiaolb40014b2013-07-19 10:01:43 +08003064 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003065 return;
3066 }
John Reckef654f12011-07-12 16:42:08 -07003067 if (current.canGoBack()) {
3068 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003069 } else {
3070 // Check to see if we are closing a window that was created by
3071 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003072 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003073 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003074 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 // Now we close the other tab
3076 closeTab(current);
3077 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003078 /*
3079 * Instead of finishing the activity, simply push this to the back
3080 * of the stack and let ActivityManager to choose the foreground
3081 * activity. As BrowserActivity is singleTask, it will be always the
3082 * root of the task. So we can use either true or false for
3083 * moveTaskToBack().
3084 */
luxiaolb40014b2013-07-19 10:01:43 +08003085 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003086 }
3087 }
3088 }
3089
3090 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003091 * helper method for key handler
3092 * returns the current tab if it can't advance
3093 */
Michael Kolbc831b632011-05-11 09:30:34 -07003094 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003095 int pos = mTabControl.getCurrentPosition() + 1;
3096 if (pos >= mTabControl.getTabCount()) {
3097 pos = 0;
3098 }
3099 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003100 }
3101
3102 /**
3103 * helper method for key handler
3104 * returns the current tab if it can't advance
3105 */
Michael Kolbc831b632011-05-11 09:30:34 -07003106 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003107 int pos = mTabControl.getCurrentPosition() - 1;
3108 if ( pos < 0) {
3109 pos = mTabControl.getTabCount() - 1;
3110 }
3111 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003112 }
3113
John Reckbcef87f2012-02-03 14:58:34 -08003114 boolean isMenuOrCtrlKey(int keyCode) {
3115 return (KeyEvent.KEYCODE_MENU == keyCode)
3116 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3117 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3118 }
3119
Michael Kolb0035fad2011-03-14 13:25:28 -07003120 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003121 * handle key events in browser
3122 *
3123 * @param keyCode
3124 * @param event
3125 * @return true if handled, false to pass to super
3126 */
John Reck9c35b9c2012-05-30 10:08:50 -07003127 @Override
3128 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003129 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003130 // Even if MENU is already held down, we need to call to super to open
3131 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003132 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003133 mMenuIsDown = true;
3134 return false;
3135 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003136
Cary Clark8ff8c662010-12-29 15:03:05 -05003137 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003138 Tab tab = getCurrentTab();
3139 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003140
Cary Clark160bbb92011-01-10 11:17:07 -05003141 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3142 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003143
Michael Kolb8233fac2010-10-26 16:08:53 -07003144 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003145 case KeyEvent.KEYCODE_TAB:
3146 if (event.isCtrlPressed()) {
3147 if (event.isShiftPressed()) {
3148 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003149 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003150 } else {
3151 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003152 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003153 }
3154 return true;
3155 }
3156 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 case KeyEvent.KEYCODE_SPACE:
3158 // WebView/WebTextView handle the keys in the KeyDown. As
3159 // the Activity's shortcut keys are only handled when WebView
3160 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003161 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003162 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003163 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003164 pageDown();
3165 }
3166 return true;
3167 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003168 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003169 event.startTracking();
3170 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003171 case KeyEvent.KEYCODE_FORWARD:
3172 if (!noModifiers) break;
3173 tab.goForward();
3174 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003175 case KeyEvent.KEYCODE_DPAD_LEFT:
3176 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003177 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003178 return true;
3179 }
3180 break;
3181 case KeyEvent.KEYCODE_DPAD_RIGHT:
3182 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003183 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003184 return true;
3185 }
3186 break;
3187 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003188 if (ctrl) {
3189 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003190 return true;
3191 }
3192 break;
Michael Kolba4183062011-01-16 10:43:21 -08003193// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003194 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003195 if (ctrl ) {
3196 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003197 return true;
3198 }
3199 break;
Michael Kolba4183062011-01-16 10:43:21 -08003200// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003201// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003202// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003203// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003204// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003205// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003206// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003207// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003208// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003209// case KeyEvent.KEYCODE_M: // unused
3210// case KeyEvent.KEYCODE_N: // in Chrome: new window
3211// case KeyEvent.KEYCODE_O: // in Chrome: open file
3212// case KeyEvent.KEYCODE_P: // in Chrome: print page
3213// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003214// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003215// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3216 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003217 // we can't use the ctrl/shift flags, they check for
3218 // exclusive use of a modifier
3219 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003220 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003221 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003222 } else {
3223 openTabToHomePage();
3224 }
3225 return true;
3226 }
3227 break;
3228// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3229// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003230// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003231// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3232// case KeyEvent.KEYCODE_Y: // unused
3233// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003234 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003235 // it is a regular key and webview is not null
3236 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003237 }
3238
John Reck9c35b9c2012-05-30 10:08:50 -07003239 @Override
3240 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003241 switch(keyCode) {
3242 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003243 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003244 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003245 return true;
3246 }
3247 break;
3248 }
3249 return false;
3250 }
3251
John Reck9c35b9c2012-05-30 10:08:50 -07003252 @Override
3253 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003254 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003255 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003256 if (KeyEvent.KEYCODE_MENU == keyCode
3257 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003258 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003259 }
3260 }
Cary Clark160bbb92011-01-10 11:17:07 -05003261 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003262 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003263 case KeyEvent.KEYCODE_BACK:
3264 if (event.isTracking() && !event.isCanceled()) {
3265 onBackKey();
3266 return true;
3267 }
3268 break;
3269 }
3270 return false;
3271 }
3272
3273 public boolean isMenuDown() {
3274 return mMenuIsDown;
3275 }
3276
John Reck9c35b9c2012-05-30 10:08:50 -07003277 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003278 public void setupAutoFill(Message message) {
3279 // Open the settings activity at the AutoFill profile fragment so that
3280 // the user can create a new profile. When they return, we will dispatch
3281 // the message so that we can autofill the form using their new profile.
3282 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3283 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3284 AutoFillSettingsFragment.class.getName());
3285 mAutoFillSetupMessage = message;
3286 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3287 }
John Reckb3417f02011-01-14 11:01:05 -08003288
John Reck9c35b9c2012-05-30 10:08:50 -07003289 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003290 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003291 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003292 return true;
3293 }
3294
John Reck1cf4b792011-07-26 10:22:22 -07003295 @Override
3296 public boolean shouldCaptureThumbnails() {
3297 return mUi.shouldCaptureThumbnails();
3298 }
3299
Michael Kolbc3af0672011-08-09 10:24:41 -07003300 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003301 public boolean supportsVoice() {
3302 PackageManager pm = mActivity.getPackageManager();
3303 List activities = pm.queryIntentActivities(new Intent(
3304 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3305 return activities.size() != 0;
3306 }
3307
3308 @Override
3309 public void startVoiceRecognizer() {
3310 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003311 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003312 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3313 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3314 mActivity.startActivityForResult(voice, VOICE_RESULT);
3315 }
3316
3317 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003318 public void setBlockEvents(boolean block) {
3319 mBlockEvents = block;
3320 }
3321
John Reck9c35b9c2012-05-30 10:08:50 -07003322 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003323 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003324 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003325 }
3326
John Reck9c35b9c2012-05-30 10:08:50 -07003327 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003328 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003329 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003330 }
3331
John Reck9c35b9c2012-05-30 10:08:50 -07003332 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003333 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003334 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003335 }
3336
John Reck9c35b9c2012-05-30 10:08:50 -07003337 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003338 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003339 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003340 }
3341
John Reck9c35b9c2012-05-30 10:08:50 -07003342 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003343 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003344 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003345 }
3346
Michael Kolb8233fac2010-10-26 16:08:53 -07003347}