blob: d0d329bf0759f45b12547f84925e9ca5e99f9040 [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;
89
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070090import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070091import com.android.browser.R;
92import com.android.browser.IntentHandler.UrlData;
93import com.android.browser.UI.ComboViews;
94import com.android.browser.mynavigation.AddMyNavigationPage;
95import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070096import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070097import com.android.browser.platformsupport.BrowserContract;
98import com.android.browser.platformsupport.WebAddress;
99import com.android.browser.platformsupport.BrowserContract.Images;
100import com.android.browser.provider.BrowserProvider2.Thumbnails;
101import com.android.browser.provider.SnapshotProvider.Snapshots;
102import com.android.browser.reflect.ReflectHelper;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700103
Michael Kolb8233fac2010-10-26 16:08:53 -0700104import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700105import java.io.File;
106import java.io.FileOutputStream;
107import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700108import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700109import java.text.DateFormat;
110import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700111import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700112import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700113import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800115import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200116import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700117import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700118
119/**
120 * Controller for browser
121 */
122public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700123 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700124
125 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800126 private static final String SEND_APP_ID_EXTRA =
127 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700128 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800129
Vivek Sekharb54614f2014-05-01 19:03:37 -0700130 // Remind switch to data connection if wifi is unavailable
131 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800132
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 // public message ids
134 public final static int LOAD_URL = 1001;
135 public final static int STOP_LOAD = 1002;
136
137 // Message Ids
138 private static final int FOCUS_NODE_HREF = 102;
139 private static final int RELEASE_WAKELOCK = 107;
140
141 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
142
143 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800144 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700145
146 private static final int EMPTY_MENU = -1;
147
Michael Kolb8233fac2010-10-26 16:08:53 -0700148 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700149 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150 final static int PREFERENCES_PAGE = 3;
151 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000152 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700153 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800154 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
157
158 // As the ids are dynamically created, we can't guarantee that they will
159 // be in sequence, so this static array maps ids to a window number.
160 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
161 { R.id.window_one_menu_id, R.id.window_two_menu_id,
162 R.id.window_three_menu_id, R.id.window_four_menu_id,
163 R.id.window_five_menu_id, R.id.window_six_menu_id,
164 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
165
166 // "source" parameter for Google search through search key
167 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
168 // "source" parameter for Google search through simplily type
169 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
170
George Mount387d45d2011-10-07 15:57:53 -0700171 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700172 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
173
John Reckd7dd9b22011-08-30 09:18:29 -0700174 // A bitmap that is re-used in createScreenshot as scratch space
175 private static Bitmap sThumbnailBitmap;
176
Michael Kolb8233fac2010-10-26 16:08:53 -0700177 private Activity mActivity;
178 private UI mUi;
179 private TabControl mTabControl;
180 private BrowserSettings mSettings;
181 private WebViewFactory mFactory;
182
183 private WakeLock mWakeLock;
184
185 private UrlHandler mUrlHandler;
186 private UploadHandler mUploadHandler;
187 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700188 private PageDialogsHandler mPageDialogsHandler;
189 private NetworkStateHandler mNetworkHandler;
190
Ben Murdoch8029a772010-11-16 11:58:21 +0000191 private Message mAutoFillSetupMessage;
192
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800194 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700195
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 // FIXME, temp address onPrepareMenu performance problem.
197 // When we move everything out of view, we should rewrite this.
198 private int mCurrentMenuState = 0;
199 private int mMenuState = R.id.MAIN_MENU;
200 private int mOldMenuState = EMPTY_MENU;
201 private Menu mCachedMenu;
202
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 private boolean mMenuIsDown;
204
205 // For select and find, we keep track of the ActionMode so that
206 // finish() can be called as desired.
207 private ActionMode mActionMode;
208
209 /**
210 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
211 * of whether the configuration has changed. The first onMenuOpened call
212 * after a configuration change is simply a reopening of the same menu
213 * (i.e. mIconView did not change).
214 */
215 private boolean mConfigChanged;
216
217 /**
218 * Keeps track of whether the options menu is open. This is important in
219 * determining whether to show or hide the title bar overlay
220 */
221 private boolean mOptionsMenuOpen;
222
223 /**
224 * Whether or not the options menu is in its bigger, popup menu form. When
225 * true, we want the title bar overlay to be gone. When false, we do not.
226 * Only meaningful if mOptionsMenuOpen is true.
227 */
228 private boolean mExtendedMenuOpen;
229
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 private boolean mActivityPaused = true;
231 private boolean mLoadStopped;
232
233 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500234 // Checks to see when the bookmarks database has changed, and updates the
235 // Tabs' notion of whether they represent bookmarked sites.
236 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700237 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700238
Michael Kolbc3af0672011-08-09 10:24:41 -0700239 private boolean mBlockEvents;
240
Michael Kolb0b129122012-06-04 16:31:58 -0700241 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800242 private boolean mUpdateMyNavThumbnail;
243 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700244
George Mount3636d0a2011-11-21 09:08:21 -0800245 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700246 mActivity = browser;
247 mSettings = BrowserSettings.getInstance();
248 mTabControl = new TabControl(this);
249 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700250 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800251 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700252 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700253
254 mUrlHandler = new UrlHandler(this);
255 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700256 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
257
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500259 mBookmarksObserver = new ContentObserver(mHandler) {
260 @Override
261 public void onChange(boolean selfChange) {
262 int size = mTabControl.getTabCount();
263 for (int i = 0; i < size; i++) {
264 mTabControl.getTab(i).updateBookmarkedStatus();
265 }
266 }
267
268 };
269 browser.getContentResolver().registerContentObserver(
270 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700271
272 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 }
274
John Reck9c35b9c2012-05-30 10:08:50 -0700275 @Override
276 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800277 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800278 // mCrashRecoverHandler has any previously saved state.
279 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700280 }
281
George Mount3636d0a2011-11-21 09:08:21 -0800282 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 // Unless the last browser usage was within 24 hours, destroy any
284 // remaining incognito tabs.
285
286 Calendar lastActiveDate = icicle != null ?
287 (Calendar) icicle.getSerializable("lastActiveDate") : null;
288 Calendar today = Calendar.getInstance();
289 Calendar yesterday = Calendar.getInstance();
290 yesterday.add(Calendar.DATE, -1);
291
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800292 // we dont want to ever recover incognito tabs
293 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700294
Patrick Scott7d50a932011-02-04 09:27:26 -0500295 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700296 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500297 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000298
Michael Kolbc831b632011-05-11 09:30:34 -0700299 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500300 // Not able to restore so we go ahead and clear session cookies. We
301 // must do this before trying to login the user as we don't want to
302 // clear any session cookies set during login.
303 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700304 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800305 if (intent == null) {
306 // This won't happen under common scenarios. The icicle is
307 // not null, but there aren't any tabs to restore.
308 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700309 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800310 final Bundle extra = intent.getExtras();
311 // Create an initial tab.
312 // If the intent is ACTION_VIEW and data is not null, the Browser is
313 // invoked to view the content by another application. In this case,
314 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800315 UrlData urlData = null;
316 if (intent.getData() != null
317 && Intent.ACTION_VIEW.equals(intent.getAction())
318 && intent.getData().toString().startsWith("content://")) {
319 urlData = new UrlData(intent.getData().toString());
320 } else {
321 urlData = IntentHandler.getUrlDataFromIntent(intent);
322 }
George Mount3636d0a2011-11-21 09:08:21 -0800323 Tab t = null;
324 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700325 String landingPage = mActivity.getResources().getString(
326 R.string.def_landing_page);
327 if (!landingPage.isEmpty()) {
328 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800329 } else {
330 t = openTabToHomePage();
331 }
George Mount3636d0a2011-11-21 09:08:21 -0800332 } else {
333 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700334 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800335 }
336 if (t != null) {
337 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
338 }
339 WebView webView = t.getWebView();
340 if (extra != null) {
341 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
342 if (scale > 0 && scale <= 1000) {
343 webView.setInitialScale(scale);
344 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700345 }
346 }
John Reckd8c74522011-06-14 08:45:00 -0700347 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700348 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700349 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500350 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700351 List<Tab> tabs = mTabControl.getTabs();
352 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
353 for (Tab t : tabs) {
354 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700355 if (t != mTabControl.getCurrentTab()) {
356 t.pause();
357 }
John Reck1cf4b792011-07-26 10:22:22 -0700358 }
359 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700360 if (tabs.size() == 0) {
361 openTabToHomePage();
362 }
John Reck1cf4b792011-07-26 10:22:22 -0700363 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700364 // TabControl.restoreState() will create a new tab even if
365 // restoring the state fails.
366 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800367 // Intent is non-null when framework thinks the browser should be
368 // launching with a new intent (icicle is null).
369 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700370 mIntentHandler.onNewIntent(intent);
371 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700372 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700373 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700374 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800375 if (jsFlags.trim().length() != 0) {
376 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700377 }
George Mount3636d0a2011-11-21 09:08:21 -0800378 if (intent != null
379 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700380 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800381 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700382 }
383
John Reck1cf4b792011-07-26 10:22:22 -0700384 private static class PruneThumbnails implements Runnable {
385 private Context mContext;
386 private List<Long> mIds;
387
388 PruneThumbnails(Context context, List<Long> preserveIds) {
389 mContext = context.getApplicationContext();
390 mIds = preserveIds;
391 }
392
393 @Override
394 public void run() {
395 ContentResolver cr = mContext.getContentResolver();
396 if (mIds == null || mIds.size() == 0) {
397 cr.delete(Thumbnails.CONTENT_URI, null, null);
398 } else {
399 int length = mIds.size();
400 StringBuilder where = new StringBuilder();
401 where.append(Thumbnails._ID);
402 where.append(" not in (");
403 for (int i = 0; i < length; i++) {
404 where.append(mIds.get(i));
405 if (i < (length - 1)) {
406 where.append(",");
407 }
408 }
409 where.append(")");
410 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
411 }
412 }
413
414 }
415
Michael Kolb1514bb72010-11-22 09:11:48 -0800416 @Override
417 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700418 return mFactory;
419 }
420
421 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800422 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800423 if (tab.hasCrashed)
424 tab.showCrashView();
425 else
426 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800427 }
428
429 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800430 public void createSubWindow(Tab tab) {
431 endActionMode();
432 WebView mainView = tab.getWebView();
433 WebView subView = mFactory.createWebView((mainView == null)
434 ? false
435 : mainView.isPrivateBrowsingEnabled());
436 mUi.createSubWindow(tab, subView);
437 }
438
439 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700440 public Context getContext() {
441 return mActivity;
442 }
443
444 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700445 public Activity getActivity() {
446 return mActivity;
447 }
448
449 void setUi(UI ui) {
450 mUi = ui;
451 }
452
Michael Kolbaf63dba2012-05-16 12:58:05 -0700453 @Override
454 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700455 return mSettings;
456 }
457
458 IntentHandler getIntentHandler() {
459 return mIntentHandler;
460 }
461
462 @Override
463 public UI getUi() {
464 return mUi;
465 }
466
467 int getMaxTabs() {
468 return mActivity.getResources().getInteger(R.integer.max_tabs);
469 }
470
471 @Override
472 public TabControl getTabControl() {
473 return mTabControl;
474 }
475
Michael Kolb1bf23132010-11-19 12:55:12 -0800476 @Override
477 public List<Tab> getTabs() {
478 return mTabControl.getTabs();
479 }
480
Michael Kolb8233fac2010-10-26 16:08:53 -0700481 private void startHandler() {
482 mHandler = new Handler() {
483
484 @Override
485 public void handleMessage(Message msg) {
486 switch (msg.what) {
487 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700488 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700489 break;
490 case FOCUS_NODE_HREF:
491 {
492 String url = (String) msg.getData().get("url");
493 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500494 String src = (String) msg.getData().get("src");
495 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 if (TextUtils.isEmpty(url)) {
497 break;
498 }
499 HashMap focusNodeMap = (HashMap) msg.obj;
500 WebView view = (WebView) focusNodeMap.get("webview");
501 // Only apply the action if the top window did not change.
502 if (getCurrentTopWebView() != view) {
503 break;
504 }
505 switch (msg.arg1) {
506 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700507 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500509 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700510 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500511 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500512 case R.id.open_newtab_context_menu_id:
513 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700514 openTab(url, parent,
515 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500516 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700517 case R.id.copy_link_context_menu_id:
518 copy(url);
519 break;
520 case R.id.save_link_context_menu_id:
521 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500522 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800523 mActivity, url, view.getSettings().getUserAgentString(),
524 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700526 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700527 if(title == null || title == "")
528 title = url;
529
530 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
531 //SWE TODO: No thumbnail support for the url obtained via
532 //browser context menu as its not loaded in webview.
533 if (bookmarkIntent != null) {
534 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
535 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
536 mActivity.startActivity(bookmarkIntent);
537 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700538 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 }
540 break;
541 }
542
543 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700544 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700545 break;
546
547 case STOP_LOAD:
548 stopLoading();
549 break;
550
551 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700552 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700553 mWakeLock.release();
554 // if we reach here, Browser should be still in the
555 // background loading after WAKELOCK_TIMEOUT (5-min).
556 // To avoid burning the battery, stop loading.
557 mTabControl.stopAllLoading();
558 }
559 break;
560
561 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800562 Tab tab = (Tab) msg.obj;
563 if (tab != null) {
564 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700565 }
566 break;
kaiyiz591110b2013-08-06 17:11:06 +0800567 case OPEN_MENU:
568 if (!mOptionsMenuOpen && mActivity != null ) {
569 mActivity.openOptionsMenu();
570 }
571 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700572 }
573 }
574 };
575
576 }
577
John Reckef654f12011-07-12 16:42:08 -0700578 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200579 public Tab getCurrentTab() {
580 return mTabControl.getCurrentTab();
581 }
582
Michael Kolbba99c5d2010-11-29 14:57:41 -0800583 @Override
584 public void shareCurrentPage() {
585 shareCurrentPage(mTabControl.getCurrentTab());
586 }
587
588 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700589 if (tab == null || tab.getWebView() == null)
590 return;
591
592 final Tab mytab = tab;
593 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
594 @Override
595 public void onReceiveValue(Bitmap bitmap) {
596 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
597 mytab.getFavicon(), bitmap);
598 }
599 };
600
601 createScreenshotAsync(
602 tab.getWebView(),
603 getDesiredThumbnailWidth(mActivity),
604 getDesiredThumbnailHeight(mActivity),
605 new ValueCallback<Bitmap>() {
606 @Override
607 public void onReceiveValue(Bitmap bitmap) {
608 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
609 mytab.getFavicon(), bitmap);
610 }
611 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800612 }
613
Michael Kolb8233fac2010-10-26 16:08:53 -0700614 /**
615 * Share a page, providing the title, url, favicon, and a screenshot. Uses
616 * an {@link Intent} to launch the Activity chooser.
617 * @param c Context used to launch a new Activity.
618 * @param title Title of the page. Stored in the Intent with
619 * {@link Intent#EXTRA_SUBJECT}
620 * @param url URL of the page. Stored in the Intent with
621 * {@link Intent#EXTRA_TEXT}
622 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
623 * with {@link Browser#EXTRA_SHARE_FAVICON}
624 * @param screenshot Bitmap of a screenshot of the page. Stored in the
625 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
626 */
627 static final void sharePage(Context c, String title, String url,
628 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700629
630 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
631 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
632 R.layout.app_row, sDialog.getApps());
633 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700634 }
635
636 private void copy(CharSequence text) {
637 ClipboardManager cm = (ClipboardManager) mActivity
638 .getSystemService(Context.CLIPBOARD_SERVICE);
639 cm.setText(text);
640 }
641
642 // lifecycle
643
John Reck9c35b9c2012-05-30 10:08:50 -0700644 @Override
645 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700646 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800647 // update the menu in case of a locale change
648 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800649 if (mOptionsMenuOpen) {
650 mActivity.closeOptionsMenu();
651 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
652 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 if (mPageDialogsHandler != null) {
654 mPageDialogsHandler.onConfigurationChanged(config);
655 }
656 mUi.onConfigurationChanged(config);
657 }
658
659 @Override
660 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700661 if (!mUi.isWebShowing()) {
662 mUi.showWeb(false);
663 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700664 mIntentHandler.onNewIntent(intent);
665 }
666
John Reck9c35b9c2012-05-30 10:08:50 -0700667 @Override
668 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800669 if (mUi.isCustomViewShowing()) {
670 hideCustomView();
671 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700672 if (mActivityPaused) {
673 Log.e(LOGTAG, "BrowserActivity is already paused.");
674 return;
675 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800677 Tab tab = mTabControl.getCurrentTab();
678 if (tab != null) {
679 tab.pause();
680 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700681 if (mWakeLock == null) {
682 PowerManager pm = (PowerManager) mActivity
683 .getSystemService(Context.POWER_SERVICE);
684 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
685 }
Michael Kolb70976932010-11-30 11:34:01 -0800686 mWakeLock.acquire();
687 mHandler.sendMessageDelayed(mHandler
688 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
689 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700690 }
691 mUi.onPause();
692 mNetworkHandler.onPause();
693
694 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100695 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 }
697
John Reck9c35b9c2012-05-30 10:08:50 -0700698 @Override
699 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700700 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800701 Bundle saveState = createSaveState();
702
703 // crash recovery manages all save & restore state
704 mCrashRecoveryHandler.writeState(saveState);
705 mSettings.setLastRunPaused(true);
706 }
707
708 /**
709 * Save the current state to outState. Does not write the state to
710 * disk.
711 * @return Bundle containing the current state of all tabs.
712 */
713 /* package */ Bundle createSaveState() {
714 Bundle saveState = new Bundle();
715 mTabControl.saveState(saveState);
716 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700717 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800718 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700719 }
George Mount3636d0a2011-11-21 09:08:21 -0800720 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700721 }
722
John Reck9c35b9c2012-05-30 10:08:50 -0700723 @Override
724 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 if (!mActivityPaused) {
726 Log.e(LOGTAG, "BrowserActivity is already resumed.");
727 return;
728 }
George Mount3636d0a2011-11-21 09:08:21 -0800729 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700730 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800731 Tab current = mTabControl.getCurrentTab();
732 if (current != null) {
733 current.resume();
734 resumeWebViewTimers(current);
735 }
John Reckf57c0292011-07-21 18:15:39 -0700736 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200737
Michael Kolb8233fac2010-10-26 16:08:53 -0700738 mUi.onResume();
739 mNetworkHandler.onResume();
740 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100741 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700742 if (mVoiceResult != null) {
743 mUi.onVoiceResult(mVoiceResult);
744 mVoiceResult = null;
745 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800746 if (current != null && current.hasCrashed) {
747 current.showCrashView();
748 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700749 }
750
John Reckf57c0292011-07-21 18:15:39 -0700751 private void releaseWakeLock() {
752 if (mWakeLock != null && mWakeLock.isHeld()) {
753 mHandler.removeMessages(RELEASE_WAKELOCK);
754 mWakeLock.release();
755 }
756 }
757
Michael Kolb70976932010-11-30 11:34:01 -0800758 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800759 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800760 * @param tab guaranteed non-null
761 */
762 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700763 boolean inLoad = tab.inPageLoad();
764 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
765 CookieSyncManager.getInstance().startSync();
766 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100767 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700768 }
769 }
770
Michael Kolb70976932010-11-30 11:34:01 -0800771 /**
772 * Pause all WebView timers using the WebView of the given tab
773 * @param tab
774 * @return true if the timers are paused or tab is null
775 */
776 private boolean pauseWebViewTimers(Tab tab) {
777 if (tab == null) {
778 return true;
779 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700780 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100781 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700782 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700783 }
Michael Kolb70976932010-11-30 11:34:01 -0800784 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700785 }
786
John Reck9c35b9c2012-05-30 10:08:50 -0700787 @Override
788 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800789 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700790 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
791 mUploadHandler = null;
792 }
793 if (mTabControl == null) return;
794 mUi.onDestroy();
795 // Remove the current tab and sub window
796 Tab t = mTabControl.getCurrentTab();
797 if (t != null) {
798 dismissSubWindow(t);
799 removeTab(t);
800 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500801 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700802 // Destroy all the tabs
803 mTabControl.destroy();
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 }
805
806 protected boolean isActivityPaused() {
807 return mActivityPaused;
808 }
809
John Reck9c35b9c2012-05-30 10:08:50 -0700810 @Override
811 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700812 mTabControl.freeMemory();
813 }
814
815 @Override
816 public boolean shouldShowErrorConsole() {
817 return mShouldShowErrorConsole;
818 }
819
820 protected void setShouldShowErrorConsole(boolean show) {
821 if (show == mShouldShowErrorConsole) {
822 // Nothing to do.
823 return;
824 }
825 mShouldShowErrorConsole = show;
826 Tab t = mTabControl.getCurrentTab();
827 if (t == null) {
828 // There is no current tab so we cannot toggle the error console
829 return;
830 }
831 mUi.setShouldShowErrorConsole(t, show);
832 }
833
834 @Override
835 public void stopLoading() {
836 mLoadStopped = true;
837 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700838 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700839 if (w != null) {
840 w.stopLoading();
841 mUi.onPageStopped(tab);
842 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700843 }
844
845 boolean didUserStopLoading() {
846 return mLoadStopped;
847 }
848
kaiyiza016da12013-08-26 17:50:22 +0800849 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700850 final String reminderType = getContext().getResources().getString(
851 R.string.def_wifi_browser_interaction_remind_type);
852 final String selectionConnnection = getContext().getResources().getString(
853 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700854 final String wifiSelection = getContext().getResources().getString(
855 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700856
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700857 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
858 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700859 return;
860
kaiyiza016da12013-08-26 17:50:22 +0800861 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700862 Context.CONNECTIVITY_SERVICE);
863 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700864 WifiManager wifiMgr = (WifiManager) this.getContext()
865 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700866 if (networkInfo == null
867 || (networkInfo != null && (networkInfo.getType() !=
868 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700869 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
870 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700871 List<ScanResult> list = wifiMgr.getScanResults();
872 // Have no AP's for Wifi's fall back to data
873 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700874 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700875 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
876 this.getContext().startActivity(intent);
877 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700878 // Request to select Wifi AP
879 try {
880 Intent intent = new Intent(wifiSelection);
881 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
882 this.getContext().startActivity(intent);
883 } catch (Exception e) {
884 String err_msg = this.getContext().getString(
885 R.string.acivity_not_found, wifiSelection);
886 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
887 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700888 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700889 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800890 }
891 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700892
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 // WebViewController
894
895 @Override
John Reck324d4402011-01-11 16:56:42 -0800896 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700897
898 // We've started to load a new page. If there was a pending message
899 // to save a screenshot then we will now take the new page and save
900 // an incorrect screenshot. Therefore, remove any pending thumbnail
901 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700902 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700903
904 // reset sync timer to avoid sync starts during loading a page
905 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700906 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700907 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700908 boolean networkNotifier =
909 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700910 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700911 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800912 } else {
913 if (!mNetworkHandler.isNetworkUp()) {
914 view.setNetworkAvailable(false);
915 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 }
917
918 // when BrowserActivity just starts, onPageStarted may be called before
919 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
920 // to start the timer. As we won't switch tabs while an activity is in
921 // pause state, we can ensure calling resume and pause in pair.
922 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800923 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 }
925 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700926 endActionMode();
927
John Reck30c714c2010-12-16 17:30:34 -0800928 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700929
John Reck324d4402011-01-11 16:56:42 -0800930 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700931 // update the bookmark database for favicon
932 maybeUpdateFavicon(tab, null, url, favicon);
933
934 Performance.tracePageStart(url);
935
936 // Performance probe
937 if (false) {
938 Performance.onPageStarted();
939 }
940
941 }
942
943 @Override
John Reck324d4402011-01-11 16:56:42 -0800944 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700945 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800946 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200947
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 // Performance probe
949 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800950 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 }
952
Tarun Nainani8eb00912014-07-17 12:28:32 -0700953 tab.onPageFinished();
954
Michael Kolb8233fac2010-10-26 16:08:53 -0700955 Performance.tracePageFinished();
956 }
957
958 @Override
John Reck30c714c2010-12-16 17:30:34 -0800959 public void onProgressChanged(Tab tab) {
960 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700961
962 if (newProgress == 100) {
963 CookieSyncManager.getInstance().sync();
964 // onProgressChanged() may continue to be called after the main
965 // frame has finished loading, as any remaining sub frames continue
966 // to load. We'll only get called once though with newProgress as
967 // 100 when everything is loaded. (onPageFinished is called once
968 // when the main frame completes loading regardless of the state of
969 // any sub frames so calls to onProgressChanges may continue after
970 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800971 if (tab.inPageLoad()) {
972 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200973 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
974 // pause the WebView timer and release the wake lock if it is
975 // finished while BrowserActivity is in pause state.
976 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 }
John Reckd9862372012-02-21 15:04:50 -0800978 if (!tab.isPrivateBrowsingEnabled()
979 && !TextUtils.isEmpty(tab.getUrl())
980 && !tab.isSnapshot()) {
981 // Only update the bookmark screenshot if the user did not
982 // cancel the load early and there is not already
983 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700984 if (tab.shouldUpdateThumbnail() &&
985 (tab.inForeground() && !didUserStopLoading()
986 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800987 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
988 mHandler.sendMessageDelayed(mHandler.obtainMessage(
989 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800990 1500);
John Reckd9862372012-02-21 15:04:50 -0800991 }
992 }
993 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700994 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800995 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800997 // still loading
998 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700999 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001000 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001001 }
1002 }
John Reck30c714c2010-12-16 17:30:34 -08001003 mUi.onProgressChanged(tab);
1004 }
1005
1006 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001007 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001008 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001009 }
1010
1011 @Override
1012 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001013 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001014 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001015 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001016 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1017 return;
1018 }
1019 // Update the title in the history database if not in private browsing mode
1020 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001021 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001022 }
1023 }
1024
1025 @Override
1026 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001027 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001028 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1029 }
1030
1031 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001032 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1033 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001034 }
1035
1036 @Override
1037 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1038 if (mMenuIsDown) {
1039 // only check shortcut key when MENU is held
1040 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1041 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001043 int keyCode = event.getKeyCode();
1044 // We need to send almost every key to WebKit. However:
1045 // 1. We don't want to block the device on the renderer for
1046 // some keys like menu, home, call.
1047 // 2. There are no WebKit equivalents for some of these keys
1048 // (see app/keyboard_codes_win.h)
1049 // Note that these are not the same set as KeyEvent.isSystemKey:
1050 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1051 if (keyCode == KeyEvent.KEYCODE_MENU ||
1052 keyCode == KeyEvent.KEYCODE_HOME ||
1053 keyCode == KeyEvent.KEYCODE_BACK ||
1054 keyCode == KeyEvent.KEYCODE_CALL ||
1055 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1056 keyCode == KeyEvent.KEYCODE_POWER ||
1057 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1058 keyCode == KeyEvent.KEYCODE_CAMERA ||
1059 keyCode == KeyEvent.KEYCODE_FOCUS ||
1060 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1061 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1062 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1063 return true;
1064 }
1065
1066 // We also have to intercept some shortcuts before we send them to the ContentView.
1067 if (event.isCtrlPressed() && (
1068 keyCode == KeyEvent.KEYCODE_TAB ||
1069 keyCode == KeyEvent.KEYCODE_W ||
1070 keyCode == KeyEvent.KEYCODE_F4)) {
1071 return true;
1072 }
1073
1074 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001075 }
1076
1077 @Override
John Reck997b1b72012-04-19 18:08:25 -07001078 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 if (!isActivityPaused()) {
1080 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001081 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 } else {
John Reck997b1b72012-04-19 18:08:25 -07001083 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 }
1085 }
John Reck997b1b72012-04-19 18:08:25 -07001086 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001087 }
1088
1089 @Override
John Reck324d4402011-01-11 16:56:42 -08001090 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001091 boolean disableHistoryWrites =
1092 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1093
1094 // Don't save anything in private browsing mode or when explicitly set
1095 // not to write history via an overlay
1096 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001097 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001098
John Reck324d4402011-01-11 16:56:42 -08001099 if (TextUtils.isEmpty(url)
1100 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001101 return;
1102 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001103
John Reckf57c0292011-07-21 18:15:39 -07001104 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001105 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 }
1107
1108 @Override
1109 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1110 AsyncTask<Void, Void, String[]> task =
1111 new AsyncTask<Void, Void, String[]>() {
1112 @Override
1113 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001114 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 }
1116 @Override
1117 public void onPostExecute(String[] result) {
1118 callback.onReceiveValue(result);
1119 }
1120 };
1121 task.execute();
1122 }
1123
1124 @Override
1125 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1126 final HttpAuthHandler handler, final String host,
1127 final String realm) {
1128 String username = null;
1129 String password = null;
1130
1131 boolean reuseHttpAuthUsernamePassword
1132 = handler.useHttpAuthUsernamePassword();
1133
1134 if (reuseHttpAuthUsernamePassword && view != null) {
1135 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1136 if (credentials != null && credentials.length == 2) {
1137 username = credentials[0];
1138 password = credentials[1];
1139 }
1140 }
1141
1142 if (username != null && password != null) {
1143 handler.proceed(username, password);
1144 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001145 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1147 } else {
1148 handler.cancel();
1149 }
1150 }
1151 }
1152
1153 @Override
1154 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001155 String contentDisposition, String mimetype, String referer,
1156 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001157 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001158 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001159 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001160 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 // This Tab was opened for the sole purpose of downloading a
1162 // file. Remove it.
1163 if (tab == mTabControl.getCurrentTab()) {
1164 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001165 if (tab.getDerivedFromIntent())
1166 closeTab(tab);
1167 else
1168 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 } else {
1170 // In this case, it is not.
1171 closeTab(tab);
1172 }
1173 }
1174 }
1175
1176 @Override
1177 public Bitmap getDefaultVideoPoster() {
1178 return mUi.getDefaultVideoPoster();
1179 }
1180
1181 @Override
1182 public View getVideoLoadingProgressView() {
1183 return mUi.getVideoLoadingProgressView();
1184 }
1185
1186 @Override
1187 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1188 SslError error) {
1189 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1190 }
1191
1192 // helper method
1193
1194 /*
1195 * Update the favorites icon if the private browsing isn't enabled and the
1196 * icon is valid.
1197 */
1198 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1199 final String url, Bitmap favicon) {
1200 if (favicon == null) {
1201 return;
1202 }
1203 if (!tab.isPrivateBrowsingEnabled()) {
1204 Bookmarks.updateFavicon(mActivity
1205 .getContentResolver(), originalUrl, url, favicon);
1206 }
1207 }
1208
Leon Scroggins4cd97792010-12-03 15:31:56 -05001209 @Override
1210 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001211 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001212 mUi.bookmarkedStatusHasChanged(tab);
1213 }
1214
Michael Kolb8233fac2010-10-26 16:08:53 -07001215 // end WebViewController
1216
1217 protected void pageUp() {
1218 getCurrentTopWebView().pageUp(false);
1219 }
1220
1221 protected void pageDown() {
1222 getCurrentTopWebView().pageDown(false);
1223 }
1224
1225 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001226 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001227 public void editUrl() {
1228 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001229 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001230 }
1231
John Reck9c35b9c2012-05-30 10:08:50 -07001232 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001233 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001234 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 if (tab.inForeground()) {
1236 if (mUi.isCustomViewShowing()) {
1237 callback.onCustomViewHidden();
1238 return;
1239 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001240 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001241 // Save the menu state and set it to empty while the custom
1242 // view is showing.
1243 mOldMenuState = mMenuState;
1244 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001245 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 }
1247 }
1248
1249 @Override
1250 public void hideCustomView() {
1251 if (mUi.isCustomViewShowing()) {
1252 mUi.onHideCustomView();
1253 // Reset the old menu state.
1254 mMenuState = mOldMenuState;
1255 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001256 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001257 }
1258 }
1259
John Reck9c35b9c2012-05-30 10:08:50 -07001260 @Override
1261 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 Intent intent) {
1263 if (getCurrentTopWebView() == null) return;
1264 switch (requestCode) {
1265 case PREFERENCES_PAGE:
1266 if (resultCode == Activity.RESULT_OK && intent != null) {
1267 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001268 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001269 mTabControl.removeParentChildRelationShips();
1270 }
1271 }
1272 break;
1273 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001274 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001275 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001277 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001278 case AUTOFILL_SETUP:
1279 // Determine whether a profile was actually set up or not
1280 // and if so, send the message back to the WebTextView to
1281 // fill the form with the new profile.
1282 if (getSettings().getAutoFillProfile() != null) {
1283 mAutoFillSetupMessage.sendToTarget();
1284 mAutoFillSetupMessage = null;
1285 }
1286 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001287 case COMBO_VIEW:
1288 if (intent == null || resultCode != Activity.RESULT_OK) {
1289 break;
1290 }
John Reck3ba45532011-08-11 16:26:53 -07001291 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001292 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1293 Tab t = getCurrentTab();
1294 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001295 mUpdateMyNavThumbnail = true;
1296 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001297 loadUrl(t, uri.toString());
1298 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1299 String[] urls = intent.getStringArrayExtra(
1300 ComboViewActivity.EXTRA_OPEN_ALL);
1301 Tab parent = getCurrentTab();
1302 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001303 if (url != null) {
1304 parent = openTab(url, parent,
1305 !mSettings.openInBackground(), true);
1306 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001307 }
1308 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1309 long id = intent.getLongExtra(
1310 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1311 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001312 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001313 }
1314 }
1315 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001316 case VOICE_RESULT:
1317 if (resultCode == Activity.RESULT_OK && intent != null) {
1318 ArrayList<String> results = intent.getStringArrayListExtra(
1319 RecognizerIntent.EXTRA_RESULTS);
1320 if (results.size() >= 1) {
1321 mVoiceResult = results.get(0);
1322 }
1323 }
1324 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001325 case MY_NAVIGATION:
1326 if (intent == null || resultCode != Activity.RESULT_OK) {
1327 break;
1328 }
1329
1330 if (intent.getBooleanExtra("need_refresh", false) &&
1331 getCurrentTopWebView() != null) {
1332 getCurrentTopWebView().reload();
1333 }
1334 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 default:
1336 break;
1337 }
1338 getCurrentTopWebView().requestFocus();
1339 }
1340
1341 /**
1342 * Open the Go page.
1343 * @param startWithHistory If true, open starting on the history tab.
1344 * Otherwise, start with the bookmarks tab.
1345 */
1346 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001347 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001348 if (mTabControl.getCurrentWebView() == null) {
1349 return;
1350 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001351 // clear action mode
1352 if (isInCustomActionMode()) {
1353 endActionMode();
1354 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001355 Bundle extras = new Bundle();
1356 // Disable opening in a new window if we have maxed out the windows
1357 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1358 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001359 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 }
1361
1362 // combo view callbacks
1363
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 // key handling
1365 protected void onBackKey() {
1366 if (!mUi.onBackKey()) {
1367 WebView subwindow = mTabControl.getCurrentSubWindow();
1368 if (subwindow != null) {
1369 if (subwindow.canGoBack()) {
1370 subwindow.goBack();
1371 } else {
1372 dismissSubWindow(mTabControl.getCurrentTab());
1373 }
1374 } else {
1375 goBackOnePageOrQuit();
1376 }
1377 }
1378 }
1379
Michael Kolb4bd767d2011-05-27 11:33:55 -07001380 protected boolean onMenuKey() {
1381 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001382 }
1383
Michael Kolb8233fac2010-10-26 16:08:53 -07001384 // menu handling and state
1385 // TODO: maybe put into separate handler
1386
John Reck9c35b9c2012-05-30 10:08:50 -07001387 @Override
1388 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001389 if (mMenuState == EMPTY_MENU) {
1390 return false;
1391 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001392 MenuInflater inflater = mActivity.getMenuInflater();
1393 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 return true;
1395 }
1396
John Reck9c35b9c2012-05-30 10:08:50 -07001397 @Override
1398 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001399 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001400 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 return;
1402 }
1403 if (!(v instanceof WebView)) {
1404 return;
1405 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001406 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 WebView.HitTestResult result = webview.getHitTestResult();
1408 if (result == null) {
1409 return;
1410 }
1411
1412 int type = result.getType();
1413 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1414 Log.w(LOGTAG,
1415 "We should not show context menu when nothing is touched");
1416 return;
1417 }
1418 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1419 // let TextView handles context menu
1420 return;
1421 }
1422
1423 // Note, http://b/issue?id=1106666 is requesting that
1424 // an inflated menu can be used again. This is not available
1425 // yet, so inflate each time (yuk!)
1426 MenuInflater inflater = mActivity.getMenuInflater();
1427 inflater.inflate(R.menu.browsercontext, menu);
1428
1429 // Show the correct menu group
1430 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001431 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001432 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001433 menu.setGroupVisible(R.id.PHONE_MENU,
1434 type == WebView.HitTestResult.PHONE_TYPE);
1435 menu.setGroupVisible(R.id.EMAIL_MENU,
1436 type == WebView.HitTestResult.EMAIL_TYPE);
1437 menu.setGroupVisible(R.id.GEO_MENU,
1438 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001439 String itemUrl = null;
1440 String url = webview.getOriginalUrl();
1441 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1442 itemUrl = Uri.decode(navigationUrl);
1443 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1444 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1445 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1446 } else {
1447 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1448 }
1449 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1450 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1451 } else {
1452 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1453
1454 menu.setGroupVisible(R.id.IMAGE_MENU,
1455 type == WebView.HitTestResult.IMAGE_TYPE
1456 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1457 menu.setGroupVisible(R.id.ANCHOR_MENU,
1458 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1459 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001460 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1461 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001462 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 // Setup custom handling depending on the type
1464 switch (type) {
1465 case WebView.HitTestResult.PHONE_TYPE:
1466 menu.setHeaderTitle(Uri.decode(extra));
1467 menu.findItem(R.id.dial_context_menu_id).setIntent(
1468 new Intent(Intent.ACTION_VIEW, Uri
1469 .parse(WebView.SCHEME_TEL + extra)));
1470 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1471 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1472 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1473 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1474 addIntent);
1475 menu.findItem(R.id.copy_phone_context_menu_id)
1476 .setOnMenuItemClickListener(
1477 new Copy(extra));
1478 break;
1479
1480 case WebView.HitTestResult.EMAIL_TYPE:
1481 menu.setHeaderTitle(extra);
1482 menu.findItem(R.id.email_context_menu_id).setIntent(
1483 new Intent(Intent.ACTION_VIEW, Uri
1484 .parse(WebView.SCHEME_MAILTO + extra)));
1485 menu.findItem(R.id.copy_mail_context_menu_id)
1486 .setOnMenuItemClickListener(
1487 new Copy(extra));
1488 break;
1489
1490 case WebView.HitTestResult.GEO_TYPE:
1491 menu.setHeaderTitle(extra);
1492 menu.findItem(R.id.map_context_menu_id).setIntent(
1493 new Intent(Intent.ACTION_VIEW, Uri
1494 .parse(WebView.SCHEME_GEO
1495 + URLEncoder.encode(extra))));
1496 menu.findItem(R.id.copy_geo_context_menu_id)
1497 .setOnMenuItemClickListener(
1498 new Copy(extra));
1499 break;
1500
1501 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1502 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001503 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001504 // decide whether to show the open link in new tab option
1505 boolean showNewTab = mTabControl.canCreateNewTab();
1506 MenuItem newTabItem
1507 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001508 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001509 ? R.string.contextmenu_openlink_newwindow_background
1510 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001511 newTabItem.setVisible(showNewTab);
1512 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001513 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1514 newTabItem.setOnMenuItemClickListener(
1515 new MenuItem.OnMenuItemClickListener() {
1516 @Override
1517 public boolean onMenuItemClick(MenuItem item) {
1518 final HashMap<String, WebView> hrefMap =
1519 new HashMap<String, WebView>();
1520 hrefMap.put("webview", webview);
1521 final Message msg = mHandler.obtainMessage(
1522 FOCUS_NODE_HREF,
1523 R.id.open_newtab_context_menu_id,
1524 0, hrefMap);
1525 webview.requestFocusNodeHref(msg);
1526 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001527 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001528 });
1529 } else {
1530 newTabItem.setOnMenuItemClickListener(
1531 new MenuItem.OnMenuItemClickListener() {
1532 @Override
1533 public boolean onMenuItemClick(MenuItem item) {
1534 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001535 openTab(extra, parent,
1536 !mSettings.openInBackground(),
1537 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001538 return true;
1539 }
1540 });
1541 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001542 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001543 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1544 menu.setHeaderTitle(navigationUrl);
1545 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1546
1547 if (itemUrl != null) {
1548 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1549 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1550 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1551 @Override
1552 public boolean onMenuItemClick(MenuItem item) {
1553 final Intent intent = new Intent(Controller.this
1554 .getContext(),
1555 AddMyNavigationPage.class);
1556 Bundle bundle = new Bundle();
1557 String url = Uri.decode(navigationUrl);
1558 bundle.putBoolean("isAdding", false);
1559 bundle.putString("url", url);
1560 bundle.putString("name", getNameFromUrl(url));
1561 intent.putExtra("websites", bundle);
1562 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1563 return false;
1564 }
1565 });
1566 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1567 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1568 @Override
1569 public boolean onMenuItemClick(MenuItem item) {
1570 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1571 return false;
1572 }
1573 });
1574 }
1575 } else {
1576 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1577 }
1578 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001579 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1580 break;
1581 }
1582 // otherwise fall through to handle image part
1583 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001584 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1585 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001586 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1587 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001588 shareItem.setOnMenuItemClickListener(
1589 new MenuItem.OnMenuItemClickListener() {
1590 @Override
1591 public boolean onMenuItemClick(MenuItem item) {
1592 sharePage(mActivity, null, extra, null,
1593 null);
1594 return true;
1595 }
1596 }
1597 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001598 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001599 menu.findItem(R.id.view_image_context_menu_id)
1600 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1601 @Override
1602 public boolean onMenuItemClick(MenuItem item) {
1603 openTab(extra, mTabControl.getCurrentTab(), true, true);
1604 return false;
1605 }
1606 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001607 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1608 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1609 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001610 menu.findItem(R.id.set_wallpaper_context_menu_id).
1611 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1612 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001613 break;
1614
1615 default:
1616 Log.w(LOGTAG, "We should not get here.");
1617 break;
1618 }
1619 //update the ui
1620 mUi.onContextMenuCreated(menu);
1621 }
1622
kaiyiz6e5b3e02013-08-19 20:02:01 +08001623 public void startAddMyNavigation(String url) {
1624 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1625 Bundle bundle = new Bundle();
1626 bundle.putBoolean("isAdding", true);
1627 bundle.putString("url", url);
1628 bundle.putString("name", getNameFromUrl(url));
1629 intent.putExtra("websites", bundle);
1630 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1631 }
1632
1633 private void showMyNavigationDeleteDialog(final String itemUrl) {
1634 new AlertDialog.Builder(this.getContext())
1635 .setTitle(R.string.my_navigation_delete_label)
1636 .setIcon(android.R.drawable.ic_dialog_alert)
1637 .setMessage(R.string.my_navigation_delete_msg)
1638 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1639 @Override
1640 public void onClick(DialogInterface dialog, int whichButton) {
1641 deleteMyNavigationItem(itemUrl);
1642 }
1643 })
1644 .setNegativeButton(R.string.cancel, null)
1645 .show();
1646 }
1647
1648 private void deleteMyNavigationItem(final String itemUrl) {
1649 ContentResolver cr = this.getContext().getContentResolver();
1650 Cursor cursor = null;
1651
1652 try {
1653 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1654 new String[] {
1655 MyNavigationUtil.ID
1656 }, "url = ?", new String[] {
1657 itemUrl
1658 }, null);
1659 if (null != cursor && cursor.moveToFirst()) {
1660 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1661 cursor.getLong(0));
1662
1663 ContentValues values = new ContentValues();
1664 values.put(MyNavigationUtil.TITLE, "");
1665 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1666 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1667 ByteArrayOutputStream os = new ByteArrayOutputStream();
1668 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1669 R.raw.my_navigation_add);
1670 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1671 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1672 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1673 cr.update(uri, values, null, null);
1674 } else {
1675 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1676 }
1677 } catch (IllegalStateException e) {
1678 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1679 } finally {
1680 if (null != cursor) {
1681 cursor.close();
1682 }
1683 }
1684
1685 if (getCurrentTopWebView() != null) {
1686 getCurrentTopWebView().reload();
1687 }
1688 }
1689
1690 private String getNameFromUrl(String itemUrl) {
1691 ContentResolver cr = this.getContext().getContentResolver();
1692 Cursor cursor = null;
1693 String name = null;
1694
1695 try {
1696 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1697 new String[] {
1698 MyNavigationUtil.TITLE
1699 }, "url = ?", new String[] {
1700 itemUrl
1701 }, null);
1702 if (null != cursor && cursor.moveToFirst()) {
1703 name = cursor.getString(0);
1704 } else {
1705 Log.e(LOGTAG, "this item does not exist!");
1706 }
1707 } catch (IllegalStateException e) {
1708 Log.e(LOGTAG, "getNameFromUrl", e);
1709 } finally {
1710 if (null != cursor) {
1711 cursor.close();
1712 }
1713 }
1714 return name;
1715 }
1716
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001717 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
1718 if (bitmap == null) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001719 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1720 return;
1721 }
1722
1723 final ContentResolver cr = mActivity.getContentResolver();
1724 new AsyncTask<Void, Void, Void>() {
1725 @Override
1726 protected Void doInBackground(Void... unused) {
1727 ContentResolver cr = mActivity.getContentResolver();
1728 Cursor cursor = null;
1729 try {
1730 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1731 new String[] {
1732 MyNavigationUtil.ID
1733 }, "url = ?", new String[] {
1734 itemUrl
1735 }, null);
1736 if (null != cursor && cursor.moveToFirst()) {
1737 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001738 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001739
1740 ContentValues values = new ContentValues();
1741 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1742 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1743 cursor.getLong(0));
1744 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1745 cr.update(uri, values, null, null);
1746 os.close();
1747 }
1748 } catch (IllegalStateException e) {
1749 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1750 } catch (IOException e) {
1751 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1752 } finally {
1753 if (null != cursor) {
1754 cursor.close();
1755 }
1756 }
1757 return null;
1758 }
1759 }.execute();
1760 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001761 /**
1762 * As the menu can be open when loading state changes
1763 * we must manually update the state of the stop/reload menu
1764 * item
1765 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001766 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001767 if (menu == null) {
1768 return;
1769 }
1770 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001771 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001772 menu.findItem(R.id.stop_menu_id):
1773 menu.findItem(R.id.reload_menu_id);
1774 if (src != null) {
1775 dest.setIcon(src.getIcon());
1776 dest.setTitle(src.getTitle());
1777 }
1778 }
1779
John Reck9c35b9c2012-05-30 10:08:50 -07001780 @Override
1781 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001782 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001783 // hold on to the menu reference here; it is used by the page callbacks
1784 // to update the menu based on loading state
1785 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001786 // Note: setVisible will decide whether an item is visible; while
1787 // setEnabled() will decide whether an item is enabled, which also means
1788 // whether the matching shortcut key will function.
1789 switch (mMenuState) {
1790 case EMPTY_MENU:
1791 if (mCurrentMenuState != mMenuState) {
1792 menu.setGroupVisible(R.id.MAIN_MENU, false);
1793 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1794 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1795 }
1796 break;
1797 default:
1798 if (mCurrentMenuState != mMenuState) {
1799 menu.setGroupVisible(R.id.MAIN_MENU, true);
1800 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1801 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1802 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001803 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001804 break;
1805 }
1806 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001807 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001808 }
1809
Michael Kolb4bf79712011-07-14 14:18:12 -07001810 @Override
1811 public void updateMenuState(Tab tab, Menu menu) {
1812 boolean canGoBack = false;
1813 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001814 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001815 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001816 // Following flag is used to identify schemes for which the LIVE_MENU
1817 // items defined in res/menu/browser.xml should be enabled
1818 boolean isLiveScheme = false;
1819 boolean isPageFinished = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001820 if (tab != null) {
1821 canGoBack = tab.canGoBack();
1822 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001823 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001824 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001825 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
1826 isPageFinished = tab.getPageFinishedStatus();
Michael Kolb4bf79712011-07-14 14:18:12 -07001827 }
1828 final MenuItem back = menu.findItem(R.id.back_menu_id);
1829 back.setEnabled(canGoBack);
1830
1831 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001832
1833 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1834 forward.setEnabled(canGoForward);
1835
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001836 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1837 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001838 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001839 if (source != null && dest != null) {
1840 dest.setTitle(source.getTitle());
1841 dest.setIcon(source.getIcon());
1842 }
John Recke1a03a32011-09-14 17:04:16 -07001843 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001844
1845 // decide whether to show the share link option
1846 PackageManager pm = mActivity.getPackageManager();
1847 Intent send = new Intent(Intent.ACTION_SEND);
1848 send.setType("text/plain");
1849 ResolveInfo ri = pm.resolveActivity(send,
1850 PackageManager.MATCH_DEFAULT_ONLY);
1851 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1852
1853 boolean isNavDump = mSettings.enableNavDump();
1854 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1855 nav.setVisible(isNavDump);
1856 nav.setEnabled(isNavDump);
1857
1858 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001859 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1860 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001861 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001862 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001863 menu.setGroupEnabled(R.id.OFFLINE_READING, isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001864 // history and snapshots item are the members of COMBO menu group,
1865 // so if show history item, only make snapshots item invisible.
1866 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001867
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001868 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001869 }
1870
John Reck9c35b9c2012-05-30 10:08:50 -07001871 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001872 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001873 if (null == getCurrentTopWebView()) {
1874 return false;
1875 }
1876 if (mMenuIsDown) {
1877 // The shortcut action consumes the MENU. Even if it is still down,
1878 // it won't trigger the next shortcut action. In the case of the
1879 // shortcut action triggering a new activity, like Bookmarks, we
1880 // won't get onKeyUp for MENU. So it is important to reset it here.
1881 mMenuIsDown = false;
1882 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001883 if (mUi.onOptionsItemSelected(item)) {
1884 // ui callback handled it
1885 return true;
1886 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 switch (item.getItemId()) {
1888 // -- Main menu
1889 case R.id.new_tab_menu_id:
1890 openTabToHomePage();
1891 break;
1892
1893 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001894 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001895 break;
1896
Afzal Najamd4e33312012-04-26 01:54:01 -04001897 case R.id.close_other_tabs_id:
1898 closeOtherTabs();
1899 break;
1900
Michael Kolb8233fac2010-10-26 16:08:53 -07001901 case R.id.goto_menu_id:
1902 editUrl();
1903 break;
1904
1905 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001906 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1907 break;
1908
1909 case R.id.history_menu_id:
1910 bookmarksOrHistoryPicker(ComboViews.History);
1911 break;
1912
1913 case R.id.snapshots_menu_id:
1914 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001915 break;
1916
Michael Kolb8233fac2010-10-26 16:08:53 -07001917 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001918 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001919 break;
1920
1921 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001922 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001923 stopLoading();
1924 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001925 Tab currentTab = mTabControl.getCurrentTab();
1926 if (currentTab.hasCrashed) {
1927 currentTab.replaceCrashView(getCurrentTopWebView(),
1928 currentTab.getViewContainer());
1929 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001930 getCurrentTopWebView().reload();
1931 }
1932 break;
1933
1934 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001935 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001936 break;
1937
1938 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001939 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001940 break;
1941
1942 case R.id.close_menu_id:
1943 // Close the subwindow if it exists.
1944 if (mTabControl.getCurrentSubWindow() != null) {
1945 dismissSubWindow(mTabControl.getCurrentTab());
1946 break;
1947 }
1948 closeCurrentTab();
1949 break;
1950
kaiyiza8b6dbb2013-07-29 18:11:22 +08001951 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001952 Object[] params = { new String("persist.debug.browsermonkeytest")};
1953 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001954 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001955 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001956 if (ret != null && ret.equals("enable"))
1957 break;
luxiaolb40014b2013-07-19 10:01:43 +08001958 showExitDialog(mActivity);
1959 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001960 case R.id.homepage_menu_id:
1961 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001962 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 break;
1964
1965 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001966 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 break;
1968
1969 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001970 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001971 break;
1972
John Reck2bc80422011-06-30 15:11:49 -07001973 case R.id.save_snapshot_menu_id:
1974 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001975 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001976 createScreenshotAsync(
1977 source.getWebView(),
1978 getDesiredThumbnailWidth(mActivity),
1979 getDesiredThumbnailHeight(mActivity),
1980 new ValueCallback<Bitmap>() {
1981 @Override
1982 public void onReceiveValue(Bitmap bitmap) {
1983 new SaveSnapshotTask(source, bitmap).execute();
1984 }
1985 });
Leon Scrogginsac993842011-02-02 12:54:07 -05001986 break;
1987
Michael Kolb8233fac2010-10-26 16:08:53 -07001988 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001989 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001990 break;
1991
John Recke1a03a32011-09-14 17:04:16 -07001992 case R.id.snapshot_go_live:
1993 goLive();
1994 return true;
1995
Michael Kolb8233fac2010-10-26 16:08:53 -07001996 case R.id.share_page_menu_id:
1997 Tab currentTab = mTabControl.getCurrentTab();
1998 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001999 return false;
2000 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002001 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002002 break;
2003
2004 case R.id.dump_nav_menu_id:
2005 getCurrentTopWebView().debugDump();
2006 break;
2007
Michael Kolb8233fac2010-10-26 16:08:53 -07002008 case R.id.zoom_in_menu_id:
2009 getCurrentTopWebView().zoomIn();
2010 break;
2011
2012 case R.id.zoom_out_menu_id:
2013 getCurrentTopWebView().zoomOut();
2014 break;
2015
2016 case R.id.view_downloads_menu_id:
2017 viewDownloads();
2018 break;
2019
John Reck42229bc2011-08-19 13:26:43 -07002020 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002021 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002022 break;
2023
Michael Kolb8233fac2010-10-26 16:08:53 -07002024 case R.id.window_one_menu_id:
2025 case R.id.window_two_menu_id:
2026 case R.id.window_three_menu_id:
2027 case R.id.window_four_menu_id:
2028 case R.id.window_five_menu_id:
2029 case R.id.window_six_menu_id:
2030 case R.id.window_seven_menu_id:
2031 case R.id.window_eight_menu_id:
2032 {
2033 int menuid = item.getItemId();
2034 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2035 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2036 Tab desiredTab = mTabControl.getTab(id);
2037 if (desiredTab != null &&
2038 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002039 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002040 }
2041 break;
2042 }
2043 }
2044 }
2045 break;
2046
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002047 case R.id.about_menu_id:
2048 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2049 builder.setTitle(R.string.about);
2050 builder.setCancelable(true);
2051 String ua = "";
2052 final WebView currentWebView = getCurrentWebView();
2053 if (currentWebView != null) {
2054 final WebSettings s = currentWebView.getSettings();
2055 if (s != null) {
2056 ua = s.getUserAgentString();
2057 }
2058 }
2059 builder.setMessage("Agent:" + ua);
2060 builder.setPositiveButton(android.R.string.ok, null);
2061 builder.create().show();
2062 break;
2063
Michael Kolb8233fac2010-10-26 16:08:53 -07002064 default:
2065 return false;
2066 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 return true;
2068 }
2069
John Reck68234a92012-04-19 15:27:12 -07002070 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2071 implements OnCancelListener {
2072
2073 private Tab mTab;
2074 private Dialog mProgressDialog;
2075 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002076 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002077
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002078 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002079 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002080 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002081 }
2082
2083 @Override
2084 protected void onPreExecute() {
2085 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2086 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2087 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002088 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002089 }
2090
2091 @Override
2092 protected Long doInBackground(Void... params) {
2093 if (!mTab.saveViewState(mValues)) {
2094 return null;
2095 }
2096 if (isCancelled()) {
2097 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2098 File file = mActivity.getFileStreamPath(path);
2099 if (!file.delete()) {
2100 file.deleteOnExit();
2101 }
2102 return null;
2103 }
2104 final ContentResolver cr = mActivity.getContentResolver();
2105 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2106 if (result == null) {
2107 return null;
2108 }
2109 long id = ContentUris.parseId(result);
2110 return id;
2111 }
2112
2113 @Override
2114 protected void onPostExecute(Long id) {
2115 if (isCancelled()) {
2116 return;
2117 }
2118 mProgressDialog.dismiss();
2119 if (id == null) {
2120 Toast.makeText(mActivity, R.string.snapshot_failed,
2121 Toast.LENGTH_SHORT).show();
2122 return;
2123 }
2124 Bundle b = new Bundle();
2125 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2126 mUi.showComboView(ComboViews.Snapshots, b);
2127 }
2128
2129 @Override
2130 public void onCancel(DialogInterface dialog) {
2131 cancel(true);
2132 }
2133 }
2134
Michael Kolb80f75082012-04-10 10:50:06 -07002135 @Override
2136 public void toggleUserAgent() {
2137 WebView web = getCurrentWebView();
2138 mSettings.toggleDesktopUseragent(web);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002139 web.reload();
Michael Kolb80f75082012-04-10 10:50:06 -07002140 }
2141
2142 @Override
2143 public void findOnPage() {
2144 getCurrentTopWebView().showFindDialog(null, true);
2145 }
2146
2147 @Override
2148 public void openPreferences() {
2149 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2150 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2151 getCurrentTopWebView().getUrl());
2152 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2153 }
2154
2155 @Override
2156 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002157 WebView w = getCurrentTopWebView();
2158 if (w == null)
2159 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002160 final Intent i = createBookmarkCurrentPageIntent(false);
2161 createScreenshotAsync(
2162 w, getDesiredThumbnailWidth(mActivity),
2163 getDesiredThumbnailHeight(mActivity),
2164 new ValueCallback<Bitmap>() {
2165 @Override
2166 public void onReceiveValue(Bitmap bitmap) {
2167 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2168 mActivity.startActivity(i);
2169 }
2170 });
Michael Kolb80f75082012-04-10 10:50:06 -07002171 }
2172
John Recke1a03a32011-09-14 17:04:16 -07002173 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002174 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002175 String url = t.getLiveUrl();
2176 // destroy the old snapshot tab
2177 closeCurrentTab();
2178 Tab liveTab = createNewTab(false, true, false);
2179 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002180 }
2181
luxiaolb40014b2013-07-19 10:01:43 +08002182 private void showExitDialog(final Activity activity) {
2183 new AlertDialog.Builder(activity)
2184 .setTitle(R.string.exit_browser_title)
2185 .setIcon(android.R.drawable.ic_dialog_alert)
2186 .setMessage(R.string.exit_browser_msg)
2187 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2188 public void onClick(DialogInterface dialog, int which) {
2189 activity.moveTaskToBack(true);
2190 dialog.dismiss();
2191 }
2192 })
2193 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2194 public void onClick(DialogInterface dialog, int which) {
2195 activity.finish();
2196 mHandler.postDelayed(new Runnable() {
2197 @Override
2198 public void run() {
2199 // TODO Auto-generated method stub
2200 mCrashRecoveryHandler.clearState(true);
2201 int pid = android.os.Process.myPid();
2202 android.os.Process.killProcess(pid);
2203 }
2204 }, 300);
2205 dialog.dismiss();
2206 }
2207 })
2208 .show();
2209 }
Michael Kolb315d5022011-10-13 12:47:11 -07002210 @Override
2211 public void showPageInfo() {
2212 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2213 }
2214
John Reck9c35b9c2012-05-30 10:08:50 -07002215 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002216 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002217 // Let the History and Bookmark fragments handle menus they created.
2218 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2219 return false;
2220 }
2221
Michael Kolb8233fac2010-10-26 16:08:53 -07002222 int id = item.getItemId();
2223 boolean result = true;
2224 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002225 // -- Browser context menu
2226 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002227 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002228 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002229 case R.id.copy_link_context_menu_id:
2230 final WebView webView = getCurrentTopWebView();
2231 if (null == webView) {
2232 result = false;
2233 break;
2234 }
2235 final HashMap<String, WebView> hrefMap =
2236 new HashMap<String, WebView>();
2237 hrefMap.put("webview", webView);
2238 final Message msg = mHandler.obtainMessage(
2239 FOCUS_NODE_HREF, id, 0, hrefMap);
2240 webView.requestFocusNodeHref(msg);
2241 break;
2242
2243 default:
2244 // For other context menus
2245 result = onOptionsItemSelected(item);
2246 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002247 return result;
2248 }
2249
2250 /**
2251 * support programmatically opening the context menu
2252 */
2253 public void openContextMenu(View view) {
2254 mActivity.openContextMenu(view);
2255 }
2256
2257 /**
2258 * programmatically open the options menu
2259 */
2260 public void openOptionsMenu() {
2261 mActivity.openOptionsMenu();
2262 }
2263
John Reck9c35b9c2012-05-30 10:08:50 -07002264 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002265 public boolean onMenuOpened(int featureId, Menu menu) {
2266 if (mOptionsMenuOpen) {
2267 if (mConfigChanged) {
2268 // We do not need to make any changes to the state of the
2269 // title bar, since the only thing that happened was a
2270 // change in orientation
2271 mConfigChanged = false;
2272 } else {
2273 if (!mExtendedMenuOpen) {
2274 mExtendedMenuOpen = true;
2275 mUi.onExtendedMenuOpened();
2276 } else {
2277 // Switching the menu back to icon view, so show the
2278 // title bar once again.
2279 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002280 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002281 }
2282 }
2283 } else {
2284 // The options menu is closed, so open it, and show the title
2285 mOptionsMenuOpen = true;
2286 mConfigChanged = false;
2287 mExtendedMenuOpen = false;
2288 mUi.onOptionsMenuOpened();
2289 }
2290 return true;
2291 }
2292
John Reck9c35b9c2012-05-30 10:08:50 -07002293 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002294 public void onOptionsMenuClosed(Menu menu) {
2295 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002296 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002297 }
2298
John Reck9c35b9c2012-05-30 10:08:50 -07002299 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002300 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002301 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 }
2303
2304 // Helper method for getting the top window.
2305 @Override
2306 public WebView getCurrentTopWebView() {
2307 return mTabControl.getCurrentTopWebView();
2308 }
2309
2310 @Override
2311 public WebView getCurrentWebView() {
2312 return mTabControl.getCurrentWebView();
2313 }
2314
2315 /*
2316 * This method is called as a result of the user selecting the options
2317 * menu to see the download window. It shows the download window on top of
2318 * the current window.
2319 */
2320 void viewDownloads() {
2321 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2322 mActivity.startActivity(intent);
2323 }
2324
John Reck30b065e2011-07-19 10:58:05 -07002325 int getActionModeHeight() {
2326 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2327 new int[] { android.R.attr.actionBarSize });
2328 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2329 actionBarSizeTypedArray.recycle();
2330 return size;
2331 }
2332
Michael Kolb8233fac2010-10-26 16:08:53 -07002333 // action mode
2334
John Reck9c35b9c2012-05-30 10:08:50 -07002335 @Override
2336 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002337 mUi.onActionModeStarted(mode);
2338 mActionMode = mode;
2339 }
2340
2341 /*
2342 * True if a custom ActionMode (i.e. find or select) is in use.
2343 */
2344 @Override
2345 public boolean isInCustomActionMode() {
2346 return mActionMode != null;
2347 }
2348
2349 /*
2350 * End the current ActionMode.
2351 */
2352 @Override
2353 public void endActionMode() {
2354 if (mActionMode != null) {
2355 mActionMode.finish();
2356 }
2357 }
2358
2359 /*
2360 * Called by find and select when they are finished. Replace title bars
2361 * as necessary.
2362 */
John Reck9c35b9c2012-05-30 10:08:50 -07002363 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002364 public void onActionModeFinished(ActionMode mode) {
2365 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002366 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002367 mActionMode = null;
2368 }
2369
2370 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002371 final Tab tab = getCurrentTab();
2372 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002373 }
2374
2375 // bookmark handling
2376
2377 /**
2378 * add the current page as a bookmark to the given folder id
2379 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002380 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002381 * bookmarked, and if it is, edit that bookmark. If false, and
2382 * the site is already bookmarked, do not attempt to edit the
2383 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002384 */
2385 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002386 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002387 WebView w = getCurrentTopWebView();
2388 if (w == null) {
2389 return null;
2390 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002391 Intent i = new Intent(mActivity,
2392 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002393 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2394 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2395 String touchIconUrl = w.getTouchIconUrl();
2396 if (touchIconUrl != null) {
2397 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2398 WebSettings settings = w.getSettings();
2399 if (settings != null) {
2400 i.putExtra(AddBookmarkPage.USER_AGENT,
2401 settings.getUserAgentString());
2402 }
2403 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002404 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002405 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002406 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002407 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2408 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002409 // Put the dialog at the upper right of the screen, covering the
2410 // star on the title bar.
2411 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002412 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002413 }
2414
2415 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002416 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002417 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002418 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002419 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002420 }
2421
Vivek Sekharb54614f2014-05-01 19:03:37 -07002422 @Override
2423 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2424 boolean capture) {
2425 mUploadHandler = new UploadHandler(this);
2426 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2427 }
2428
Michael Kolb8233fac2010-10-26 16:08:53 -07002429 // thumbnails
2430
2431 /**
2432 * Return the desired width for thumbnail screenshots, which are stored in
2433 * the database, and used on the bookmarks screen.
2434 * @param context Context for finding out the density of the screen.
2435 * @return desired width for thumbnail screenshot.
2436 */
2437 static int getDesiredThumbnailWidth(Context context) {
2438 return context.getResources().getDimensionPixelOffset(
2439 R.dimen.bookmarkThumbnailWidth);
2440 }
2441
2442 /**
2443 * Return the desired height 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 height for thumbnail screenshot.
2447 */
2448 static int getDesiredThumbnailHeight(Context context) {
2449 return context.getResources().getDimensionPixelOffset(
2450 R.dimen.bookmarkThumbnailHeight);
2451 }
2452
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002453 static void createScreenshotAsync(WebView view, int width, int height,
2454 final ValueCallback<Bitmap> cb) {
2455 if (view == null || width == 0 || height == 0) {
2456 return;
2457 }
2458
2459 view.getContentBitmapAsync(
2460 (float) width / view.getWidth(),
2461 new Rect(),
2462 new ValueCallback<Bitmap>() {
2463 @Override
2464 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002465 if (bitmap != null)
2466 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2467 cb.onReceiveValue(bitmap);
2468 }});
2469 }
2470
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002471 private void updateScreenshot(final Tab tab) {
2472 createScreenshotAsync(
2473 tab.getWebView(),
2474 getDesiredThumbnailWidth(mActivity),
2475 getDesiredThumbnailHeight(mActivity),
2476 new ValueCallback<Bitmap>() {
2477 @Override
2478 public void onReceiveValue(Bitmap bitmap) {
2479 updateScreenshot(tab, bitmap);
2480 }
2481 });
2482 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002483
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002484 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002485 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002486 // FIXME: Would like to make sure there is actually something to
2487 // draw, but the API for that (WebViewCore.pictureReady()) is not
2488 // currently accessible here.
2489
John Reck34ef2672011-02-10 11:30:55 -08002490 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002491 if (view == null) {
2492 // Tab was destroyed
2493 return;
2494 }
John Reck34ef2672011-02-10 11:30:55 -08002495 final String url = tab.getUrl();
2496 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002497 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002498 if (TextUtils.isEmpty(url)) {
2499 return;
2500 }
2501
kaiyiz6e5b3e02013-08-19 20:02:01 +08002502 //update My Navigation Thumbnails
2503 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2504 if (isMyNavigationUrl) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002505 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002506 }
John Reck34ef2672011-02-10 11:30:55 -08002507 // Only update thumbnails for web urls (http(s)://), not for
2508 // about:, javascript:, data:, etc...
2509 // Unless it is a bookmarked site, then always update
2510 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2511 return;
2512 }
2513
kaiyiz6e5b3e02013-08-19 20:02:01 +08002514 if (url != null && mUpdateMyNavThumbnailUrl != null
2515 && Patterns.WEB_URL.matcher(url).matches()
2516 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2517 String urlHost = (new WebAddress(url)).getHost();
2518 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2519 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2520 || bookmarkHost.length() == 0) {
2521 return;
2522 }
2523 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2524 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2525 bookmarkHost.length());
2526 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2527 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2528 if (!bookmarkDomain.equals(urlDomain)) {
2529 return;
2530 }
2531 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002533 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2534 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2535 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2536 500);
2537 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002538 return;
2539 }
2540
2541 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002542 new AsyncTask<Void, Void, Void>() {
2543 @Override
2544 protected Void doInBackground(Void... unused) {
2545 Cursor cursor = null;
2546 try {
2547 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002548 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2549 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2550 : url);
2551 if (mUpdateMyNavThumbnail) {
2552 mUpdateMyNavThumbnail = false;
2553 mUpdateMyNavThumbnailUrl = null;
2554 }
John Reck34ef2672011-02-10 11:30:55 -08002555 if (cursor != null && cursor.moveToFirst()) {
2556 final ByteArrayOutputStream os =
2557 new ByteArrayOutputStream();
2558 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002559
John Reck34ef2672011-02-10 11:30:55 -08002560 ContentValues values = new ContentValues();
2561 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002562
John Reck34ef2672011-02-10 11:30:55 -08002563 do {
John Reck617fd832011-02-16 14:35:59 -08002564 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002565 cr.update(Images.CONTENT_URI, values, null, null);
2566 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002567 }
John Reck34ef2672011-02-10 11:30:55 -08002568 } catch (IllegalStateException e) {
2569 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002570 } catch (SQLiteException s) {
2571 // Added for possible error when user tries to remove the same bookmark
2572 // that is being updated with a screen shot
2573 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002574 } finally {
2575 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002576 }
John Reck34ef2672011-02-10 11:30:55 -08002577 return null;
2578 }
2579 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 }
2581
2582 private class Copy implements OnMenuItemClickListener {
2583 private CharSequence mText;
2584
John Reck9c35b9c2012-05-30 10:08:50 -07002585 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002586 public boolean onMenuItemClick(MenuItem item) {
2587 copy(mText);
2588 return true;
2589 }
2590
2591 public Copy(CharSequence toCopy) {
2592 mText = toCopy;
2593 }
2594 }
2595
Leon Scroggins63c02662010-11-18 15:16:27 -05002596 private static class Download implements OnMenuItemClickListener {
2597 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002598 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002599 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002600 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002601 private static final String FALLBACK_EXTENSION = "dat";
2602 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002603
John Reck9c35b9c2012-05-30 10:08:50 -07002604 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002606 if (DataUri.isDataUri(mText)) {
2607 saveDataUri();
2608 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002609 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002610 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002611 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 return true;
2613 }
2614
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002615 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2616 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002617 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002618 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002619 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002620 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002621 }
George Mount387d45d2011-10-07 15:57:53 -07002622
2623 /**
2624 * Treats mText as a data URI and writes its contents to a file
2625 * based on the current time.
2626 */
2627 private void saveDataUri() {
2628 FileOutputStream outputStream = null;
2629 try {
2630 DataUri uri = new DataUri(mText);
2631 File target = getTarget(uri);
2632 outputStream = new FileOutputStream(target);
2633 outputStream.write(uri.getData());
2634 final DownloadManager manager =
2635 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2636 manager.addCompletedDownload(target.getName(),
2637 mActivity.getTitle().toString(), false,
2638 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002639 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002640 } catch (IOException e) {
2641 Log.e(LOGTAG, "Could not save data URL");
2642 } finally {
2643 if (outputStream != null) {
2644 try {
2645 outputStream.close();
2646 } catch (IOException e) {
2647 // ignore close errors
2648 }
2649 }
2650 }
2651 }
2652
2653 /**
2654 * Creates a File based on the current time stamp and uses
2655 * the mime type of the DataUri to get the extension.
2656 */
2657 private File getTarget(DataUri uri) throws IOException {
2658 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002659 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002660 String nameBase = format.format(new Date());
2661 String mimeType = uri.getMimeType();
2662 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2663 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2664 if (extension == null) {
2665 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2666 extension = FALLBACK_EXTENSION;
2667 }
2668 extension = "." + extension; // createTempFile needs the '.'
2669 File targetFile = File.createTempFile(nameBase, extension, dir);
2670 return targetFile;
2671 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002672 }
2673
Cary Clark8974d282010-11-22 10:46:05 -05002674 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002675 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002676
John Reck9c35b9c2012-05-30 10:08:50 -07002677 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002678 public boolean onMenuItemClick(MenuItem item) {
2679 if (mWebView != null) {
2680 return mWebView.selectText();
2681 }
2682 return false;
2683 }
2684
2685 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002686 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002687 }
2688
2689 }
2690
Michael Kolb8233fac2010-10-26 16:08:53 -07002691 /********************** TODO: UI stuff *****************************/
2692
2693 // these methods have been copied, they still need to be cleaned up
2694
2695 /****************** tabs ***************************************************/
2696
2697 // basic tab interactions:
2698
2699 // it is assumed that tabcontrol already knows about the tab
2700 protected void addTab(Tab tab) {
2701 mUi.addTab(tab);
2702 }
2703
2704 protected void removeTab(Tab tab) {
2705 mUi.removeTab(tab);
2706 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002707 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002708 }
2709
Michael Kolbf2055602011-04-09 17:20:03 -07002710 @Override
2711 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002712 // monkey protection against delayed start
2713 if (tab != null) {
2714 mTabControl.setCurrentTab(tab);
2715 // the tab is guaranteed to have a webview after setCurrentTab
2716 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002717 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002718 //Purge active tabs
2719 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002720 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002721 }
2722
John Reck8bcafc12011-08-29 16:43:02 -07002723 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002724 Tab current = mTabControl.getCurrentTab();
2725 if (current != null
2726 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002727 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002728 }
2729 }
2730
John Reck26b18322011-06-21 13:08:58 -07002731 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002732 // Dismiss the subwindow if applicable.
2733 dismissSubWindow(appTab);
2734 // Since we might kill the WebView, remove it from the
2735 // content view first.
2736 mUi.detachTab(appTab);
2737 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002738 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002739 // TODO: analyze why the remove and add are necessary
2740 mUi.attachTab(appTab);
2741 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002742 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002743 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002744 } else {
2745 // If the tab was the current tab, we have to attach
2746 // it to the view system again.
2747 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002748 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002749 }
2750 }
2751
2752 // Remove the sub window if it exists. Also called by TabControl when the
2753 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002754 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002755 public void dismissSubWindow(Tab tab) {
2756 removeSubWindow(tab);
2757 // dismiss the subwindow. This will destroy the WebView.
2758 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002759 WebView wv = getCurrentTopWebView();
2760 if (wv != null) {
2761 wv.requestFocus();
2762 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002763 }
2764
2765 @Override
2766 public void removeSubWindow(Tab t) {
2767 if (t.getSubWebView() != null) {
2768 mUi.removeSubWindow(t.getSubViewContainer());
2769 }
2770 }
2771
2772 @Override
2773 public void attachSubWindow(Tab tab) {
2774 if (tab.getSubWebView() != null) {
2775 mUi.attachSubWindow(tab.getSubViewContainer());
2776 getCurrentTopWebView().requestFocus();
2777 }
2778 }
2779
Mathew Inwood29721c22011-06-29 17:55:24 +01002780 private Tab showPreloadedTab(final UrlData urlData) {
2781 if (!urlData.isPreloaded()) {
2782 return null;
2783 }
2784 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2785 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2786 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002787 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002788 // Could not submit query. Fallback to regular tab creation
2789 tabControl.destroy();
2790 return null;
2791 }
2792 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002793 // check tab count and make room for new tab
2794 if (!mTabControl.canCreateNewTab()) {
2795 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2796 if (leastUsed != null) {
2797 closeTab(leastUsed);
2798 }
2799 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002800 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002801 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002802 mTabControl.addPreloadedTab(t);
2803 addTab(t);
2804 setActiveTab(t);
2805 return t;
2806 }
2807
Michael Kolb7bcafde2011-05-09 13:55:59 -07002808 // open a non inconito tab with the given url data
2809 // and set as active tab
2810 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002811 Tab tab = showPreloadedTab(urlData);
2812 if (tab == null) {
2813 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002814 if ((tab != null) && !urlData.isEmpty()) {
2815 loadUrlDataIn(tab, urlData);
2816 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002817 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002818 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002819 }
2820
Michael Kolb843510f2010-12-09 10:51:49 -08002821 @Override
2822 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002823 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002824 }
2825
Michael Kolb8233fac2010-10-26 16:08:53 -07002826 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002827 public Tab openIncognitoTab() {
2828 return openTab(INCOGNITO_URI, true, true, false);
2829 }
2830
2831 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002832 public Tab openTab(String url, boolean incognito, boolean setActive,
2833 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002834 return openTab(url, incognito, setActive, useCurrent, null);
2835 }
2836
2837 @Override
2838 public Tab openTab(String url, Tab parent, boolean setActive,
2839 boolean useCurrent) {
2840 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2841 setActive, useCurrent, parent);
2842 }
2843
2844 public Tab openTab(String url, boolean incognito, boolean setActive,
2845 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002846 Tab tab = createNewTab(incognito, setActive, useCurrent);
2847 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002848 if (parent instanceof SnapshotTab) {
2849 addTab(tab);
2850 if (setActive)
2851 setActiveTab(tab);
2852 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002853 parent.addChildTab(tab);
2854 }
Michael Kolb519d2282011-05-09 17:03:19 -07002855 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002856 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002857 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002858 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002859 return tab;
2860 }
2861
2862 // this method will attempt to create a new tab
2863 // incognito: private browsing tab
2864 // setActive: ste tab as current tab
2865 // useCurrent: if no new tab can be created, return current tab
2866 private Tab createNewTab(boolean incognito, boolean setActive,
2867 boolean useCurrent) {
2868 Tab tab = null;
2869 if (mTabControl.canCreateNewTab()) {
2870 tab = mTabControl.createNewTab(incognito);
2871 addTab(tab);
2872 if (setActive) {
2873 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002874 } else {
2875 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002876 }
2877 } else {
2878 if (useCurrent) {
2879 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002880 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002881 } else {
2882 mUi.showMaxTabsWarning();
2883 }
2884 }
2885 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002886 }
2887
John Reck2bc80422011-06-30 15:11:49 -07002888 @Override
2889 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2890 SnapshotTab tab = null;
2891 if (mTabControl.canCreateNewTab()) {
2892 tab = mTabControl.createSnapshotTab(snapshotId);
2893 addTab(tab);
2894 if (setActive) {
2895 setActiveTab(tab);
2896 }
2897 } else {
2898 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002899 }
2900 return tab;
2901 }
2902
Michael Kolb8233fac2010-10-26 16:08:53 -07002903 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002904 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002905 * @return boolean True if we successfully switched to a different tab. If
2906 * the indexth tab is null, or if that tab is the same as
2907 * the current one, return false.
2908 */
2909 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002910 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002911 Tab currentTab = mTabControl.getCurrentTab();
2912 if (tab == null || tab == currentTab) {
2913 return false;
2914 }
2915 setActiveTab(tab);
2916 return true;
2917 }
2918
2919 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002920 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002921 closeCurrentTab(false);
2922 }
2923
2924 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002925 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002926 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002927 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002928 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002929 return;
2930 }
Michael Kolbc831b632011-05-11 09:30:34 -07002931 final Tab current = mTabControl.getCurrentTab();
2932 final int pos = mTabControl.getCurrentPosition();
2933 Tab newTab = current.getParent();
2934 if (newTab == null) {
2935 newTab = mTabControl.getTab(pos + 1);
2936 if (newTab == null) {
2937 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002938 }
2939 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002940 if (andQuit) {
2941 mTabControl.setCurrentTab(newTab);
2942 closeTab(current);
2943 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002944 // Close window
2945 closeTab(current);
2946 }
2947 }
2948
2949 /**
2950 * Close the tab, remove its associated title bar, and adjust mTabControl's
2951 * current tab to a valid value.
2952 */
2953 @Override
2954 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002955 if (tab == mTabControl.getCurrentTab()) {
2956 closeCurrentTab();
2957 } else {
2958 removeTab(tab);
2959 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002960 }
2961
Afzal Najamd4e33312012-04-26 01:54:01 -04002962 /**
2963 * Close all tabs except the current one
2964 */
2965 @Override
2966 public void closeOtherTabs() {
2967 int inactiveTabs = mTabControl.getTabCount() - 1;
2968 for (int i = inactiveTabs; i >= 0; i--) {
2969 Tab tab = mTabControl.getTab(i);
2970 if (tab != mTabControl.getCurrentTab()) {
2971 removeTab(tab);
2972 }
2973 }
2974 }
2975
Michael Kolb8233fac2010-10-26 16:08:53 -07002976 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002977 protected void loadUrlFromContext(String url) {
2978 Tab tab = getCurrentTab();
2979 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002980 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002981 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002982 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002983 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002984 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002985 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002986 }
2987 }
2988 }
2989
2990 /**
2991 * Load the URL into the given WebView and update the title bar
2992 * to reflect the new load. Call this instead of WebView.loadUrl
2993 * directly.
2994 * @param view The WebView used to load url.
2995 * @param url The URL to load.
2996 */
John Reck71e51422011-07-01 16:49:28 -07002997 @Override
2998 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002999 loadUrl(tab, url, null);
3000 }
3001
3002 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3003 if (tab != null) {
3004 dismissSubWindow(tab);
3005 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003006 if (tab.hasCrashed) {
3007 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3008 }
Michael Kolba53c9892011-10-05 13:31:40 -07003009 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003010 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003011 }
3012
3013 /**
3014 * Load UrlData into a Tab and update the title bar to reflect the new
3015 * load. Call this instead of UrlData.loadIn directly.
3016 * @param t The Tab used to load.
3017 * @param data The UrlData being loaded.
3018 */
3019 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003020 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003021 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003022 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003023 } else {
John Reck38b39652012-06-05 09:22:59 -07003024 if (t != null && data.mDisableUrlOverride) {
3025 t.disableUrlOverridingForLoad();
3026 }
John Reck26b18322011-06-21 13:08:58 -07003027 loadUrl(t, data.mUrl, data.mHeaders);
3028 }
3029 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003030 }
3031
John Reck30c714c2010-12-16 17:30:34 -08003032 @Override
3033 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003034 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003035 if (tab.canGoBack()) {
3036 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003037 } else {
John Reckef654f12011-07-12 16:42:08 -07003038 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003039 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003040 }
3041
3042 void goBackOnePageOrQuit() {
3043 Tab current = mTabControl.getCurrentTab();
3044 if (current == null) {
3045 /*
3046 * Instead of finishing the activity, simply push this to the back
3047 * of the stack and let ActivityManager to choose the foreground
3048 * activity. As BrowserActivity is singleTask, it will be always the
3049 * root of the task. So we can use either true or false for
3050 * moveTaskToBack().
3051 */
luxiaolb40014b2013-07-19 10:01:43 +08003052 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003053 return;
3054 }
John Reckef654f12011-07-12 16:42:08 -07003055 if (current.canGoBack()) {
3056 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003057 } else {
3058 // Check to see if we are closing a window that was created by
3059 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003060 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003061 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003062 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003063 // Now we close the other tab
3064 closeTab(current);
3065 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003066 /*
3067 * Instead of finishing the activity, simply push this to the back
3068 * of the stack and let ActivityManager to choose the foreground
3069 * activity. As BrowserActivity is singleTask, it will be always the
3070 * root of the task. So we can use either true or false for
3071 * moveTaskToBack().
3072 */
luxiaolb40014b2013-07-19 10:01:43 +08003073 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003074 }
3075 }
3076 }
3077
3078 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003079 * helper method for key handler
3080 * returns the current tab if it can't advance
3081 */
Michael Kolbc831b632011-05-11 09:30:34 -07003082 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003083 int pos = mTabControl.getCurrentPosition() + 1;
3084 if (pos >= mTabControl.getTabCount()) {
3085 pos = 0;
3086 }
3087 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003088 }
3089
3090 /**
3091 * 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 getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003095 int pos = mTabControl.getCurrentPosition() - 1;
3096 if ( pos < 0) {
3097 pos = mTabControl.getTabCount() - 1;
3098 }
3099 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003100 }
3101
John Reckbcef87f2012-02-03 14:58:34 -08003102 boolean isMenuOrCtrlKey(int keyCode) {
3103 return (KeyEvent.KEYCODE_MENU == keyCode)
3104 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3105 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3106 }
3107
Michael Kolb0035fad2011-03-14 13:25:28 -07003108 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003109 * handle key events in browser
3110 *
3111 * @param keyCode
3112 * @param event
3113 * @return true if handled, false to pass to super
3114 */
John Reck9c35b9c2012-05-30 10:08:50 -07003115 @Override
3116 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003117 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003118 // Even if MENU is already held down, we need to call to super to open
3119 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003120 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003121 mMenuIsDown = true;
3122 return false;
3123 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003124
Cary Clark8ff8c662010-12-29 15:03:05 -05003125 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003126 Tab tab = getCurrentTab();
3127 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003128
Cary Clark160bbb92011-01-10 11:17:07 -05003129 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3130 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003131
Michael Kolb8233fac2010-10-26 16:08:53 -07003132 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003133 case KeyEvent.KEYCODE_TAB:
3134 if (event.isCtrlPressed()) {
3135 if (event.isShiftPressed()) {
3136 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003137 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003138 } else {
3139 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003140 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003141 }
3142 return true;
3143 }
3144 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003145 case KeyEvent.KEYCODE_SPACE:
3146 // WebView/WebTextView handle the keys in the KeyDown. As
3147 // the Activity's shortcut keys are only handled when WebView
3148 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003149 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003150 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003151 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003152 pageDown();
3153 }
3154 return true;
3155 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003156 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003157 event.startTracking();
3158 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003159 case KeyEvent.KEYCODE_FORWARD:
3160 if (!noModifiers) break;
3161 tab.goForward();
3162 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003163 case KeyEvent.KEYCODE_DPAD_LEFT:
3164 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003165 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003166 return true;
3167 }
3168 break;
3169 case KeyEvent.KEYCODE_DPAD_RIGHT:
3170 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003171 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003172 return true;
3173 }
3174 break;
3175 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003176 if (ctrl) {
3177 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003178 return true;
3179 }
3180 break;
Michael Kolba4183062011-01-16 10:43:21 -08003181// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003182 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003183 if (ctrl ) {
3184 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003185 return true;
3186 }
3187 break;
Michael Kolba4183062011-01-16 10:43:21 -08003188// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003189// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003190// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003191// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003192// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003193// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003194// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003195// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003196// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003197// case KeyEvent.KEYCODE_M: // unused
3198// case KeyEvent.KEYCODE_N: // in Chrome: new window
3199// case KeyEvent.KEYCODE_O: // in Chrome: open file
3200// case KeyEvent.KEYCODE_P: // in Chrome: print page
3201// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003202// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003203// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3204 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003205 // we can't use the ctrl/shift flags, they check for
3206 // exclusive use of a modifier
3207 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003208 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003209 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003210 } else {
3211 openTabToHomePage();
3212 }
3213 return true;
3214 }
3215 break;
3216// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3217// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003218// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003219// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3220// case KeyEvent.KEYCODE_Y: // unused
3221// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003222 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003223 // it is a regular key and webview is not null
3224 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003225 }
3226
John Reck9c35b9c2012-05-30 10:08:50 -07003227 @Override
3228 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003229 switch(keyCode) {
3230 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003231 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003232 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003233 return true;
3234 }
3235 break;
3236 }
3237 return false;
3238 }
3239
John Reck9c35b9c2012-05-30 10:08:50 -07003240 @Override
3241 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003242 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003243 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003244 if (KeyEvent.KEYCODE_MENU == keyCode
3245 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003246 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003247 }
3248 }
Cary Clark160bbb92011-01-10 11:17:07 -05003249 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003250 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003251 case KeyEvent.KEYCODE_BACK:
3252 if (event.isTracking() && !event.isCanceled()) {
3253 onBackKey();
3254 return true;
3255 }
3256 break;
3257 }
3258 return false;
3259 }
3260
3261 public boolean isMenuDown() {
3262 return mMenuIsDown;
3263 }
3264
John Reck9c35b9c2012-05-30 10:08:50 -07003265 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003266 public void setupAutoFill(Message message) {
3267 // Open the settings activity at the AutoFill profile fragment so that
3268 // the user can create a new profile. When they return, we will dispatch
3269 // the message so that we can autofill the form using their new profile.
3270 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3271 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3272 AutoFillSettingsFragment.class.getName());
3273 mAutoFillSetupMessage = message;
3274 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3275 }
John Reckb3417f02011-01-14 11:01:05 -08003276
John Reck9c35b9c2012-05-30 10:08:50 -07003277 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003278 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003279 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003280 return true;
3281 }
3282
John Reck1cf4b792011-07-26 10:22:22 -07003283 @Override
3284 public boolean shouldCaptureThumbnails() {
3285 return mUi.shouldCaptureThumbnails();
3286 }
3287
Michael Kolbc3af0672011-08-09 10:24:41 -07003288 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003289 public boolean supportsVoice() {
3290 PackageManager pm = mActivity.getPackageManager();
3291 List activities = pm.queryIntentActivities(new Intent(
3292 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3293 return activities.size() != 0;
3294 }
3295
3296 @Override
3297 public void startVoiceRecognizer() {
3298 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003299 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003300 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3301 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3302 mActivity.startActivityForResult(voice, VOICE_RESULT);
3303 }
3304
3305 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003306 public void setBlockEvents(boolean block) {
3307 mBlockEvents = block;
3308 }
3309
John Reck9c35b9c2012-05-30 10:08:50 -07003310 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003311 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003312 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003313 }
3314
John Reck9c35b9c2012-05-30 10:08:50 -07003315 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003316 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003317 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003318 }
3319
John Reck9c35b9c2012-05-30 10:08:50 -07003320 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003321 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003322 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003323 }
3324
John Reck9c35b9c2012-05-30 10:08:50 -07003325 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003326 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003327 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003328 }
3329
John Reck9c35b9c2012-05-30 10:08:50 -07003330 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003331 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003332 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003333 }
3334
Michael Kolb8233fac2010-10-26 16:08:53 -07003335}