blob: e4def81e066735f3fe2d24bc5fb18a73cdf1d5b2 [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) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800283 // we dont want to ever recover incognito tabs
284 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700285
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700287 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500288 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000289
Michael Kolbc831b632011-05-11 09:30:34 -0700290 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500291 // Not able to restore so we go ahead and clear session cookies. We
292 // must do this before trying to login the user as we don't want to
293 // clear any session cookies set during login.
294 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700295 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800296 if (intent == null) {
297 // This won't happen under common scenarios. The icicle is
298 // not null, but there aren't any tabs to restore.
299 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700300 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800301 final Bundle extra = intent.getExtras();
302 // Create an initial tab.
303 // If the intent is ACTION_VIEW and data is not null, the Browser is
304 // invoked to view the content by another application. In this case,
305 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800306 UrlData urlData = null;
307 if (intent.getData() != null
308 && Intent.ACTION_VIEW.equals(intent.getAction())
309 && intent.getData().toString().startsWith("content://")) {
310 urlData = new UrlData(intent.getData().toString());
311 } else {
312 urlData = IntentHandler.getUrlDataFromIntent(intent);
313 }
George Mount3636d0a2011-11-21 09:08:21 -0800314 Tab t = null;
315 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700316 String landingPage = mActivity.getResources().getString(
317 R.string.def_landing_page);
318 if (!landingPage.isEmpty()) {
319 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800320 } else {
321 t = openTabToHomePage();
322 }
George Mount3636d0a2011-11-21 09:08:21 -0800323 } else {
324 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700325 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800326 }
327 if (t != null) {
328 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
329 }
330 WebView webView = t.getWebView();
331 if (extra != null) {
332 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
333 if (scale > 0 && scale <= 1000) {
334 webView.setInitialScale(scale);
335 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700336 }
337 }
John Reckd8c74522011-06-14 08:45:00 -0700338 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700339 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700340 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500341 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700342 List<Tab> tabs = mTabControl.getTabs();
343 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
344 for (Tab t : tabs) {
345 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700346 if (t != mTabControl.getCurrentTab()) {
347 t.pause();
348 }
John Reck1cf4b792011-07-26 10:22:22 -0700349 }
350 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700351 if (tabs.size() == 0) {
352 openTabToHomePage();
353 }
John Reck1cf4b792011-07-26 10:22:22 -0700354 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 // TabControl.restoreState() will create a new tab even if
356 // restoring the state fails.
357 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800358 // Intent is non-null when framework thinks the browser should be
359 // launching with a new intent (icicle is null).
360 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700361 mIntentHandler.onNewIntent(intent);
362 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700364 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700365 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800366 if (jsFlags.trim().length() != 0) {
367 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700368 }
George Mount3636d0a2011-11-21 09:08:21 -0800369 if (intent != null
370 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700371 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800372 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700373 }
374
John Reck1cf4b792011-07-26 10:22:22 -0700375 private static class PruneThumbnails implements Runnable {
376 private Context mContext;
377 private List<Long> mIds;
378
379 PruneThumbnails(Context context, List<Long> preserveIds) {
380 mContext = context.getApplicationContext();
381 mIds = preserveIds;
382 }
383
384 @Override
385 public void run() {
386 ContentResolver cr = mContext.getContentResolver();
387 if (mIds == null || mIds.size() == 0) {
388 cr.delete(Thumbnails.CONTENT_URI, null, null);
389 } else {
390 int length = mIds.size();
391 StringBuilder where = new StringBuilder();
392 where.append(Thumbnails._ID);
393 where.append(" not in (");
394 for (int i = 0; i < length; i++) {
395 where.append(mIds.get(i));
396 if (i < (length - 1)) {
397 where.append(",");
398 }
399 }
400 where.append(")");
401 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
402 }
403 }
404
405 }
406
Michael Kolb1514bb72010-11-22 09:11:48 -0800407 @Override
408 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700409 return mFactory;
410 }
411
412 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800413 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800414 if (tab.hasCrashed)
415 tab.showCrashView();
416 else
417 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800418 }
419
420 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800421 public void createSubWindow(Tab tab) {
422 endActionMode();
423 WebView mainView = tab.getWebView();
424 WebView subView = mFactory.createWebView((mainView == null)
425 ? false
426 : mainView.isPrivateBrowsingEnabled());
427 mUi.createSubWindow(tab, subView);
428 }
429
430 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700431 public Context getContext() {
432 return mActivity;
433 }
434
435 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700436 public Activity getActivity() {
437 return mActivity;
438 }
439
440 void setUi(UI ui) {
441 mUi = ui;
442 }
443
Michael Kolbaf63dba2012-05-16 12:58:05 -0700444 @Override
445 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700446 return mSettings;
447 }
448
449 IntentHandler getIntentHandler() {
450 return mIntentHandler;
451 }
452
453 @Override
454 public UI getUi() {
455 return mUi;
456 }
457
458 int getMaxTabs() {
459 return mActivity.getResources().getInteger(R.integer.max_tabs);
460 }
461
462 @Override
463 public TabControl getTabControl() {
464 return mTabControl;
465 }
466
Michael Kolb1bf23132010-11-19 12:55:12 -0800467 @Override
468 public List<Tab> getTabs() {
469 return mTabControl.getTabs();
470 }
471
Michael Kolb8233fac2010-10-26 16:08:53 -0700472 private void startHandler() {
473 mHandler = new Handler() {
474
475 @Override
476 public void handleMessage(Message msg) {
477 switch (msg.what) {
478 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700479 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700480 break;
481 case FOCUS_NODE_HREF:
482 {
483 String url = (String) msg.getData().get("url");
484 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500485 String src = (String) msg.getData().get("src");
486 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 if (TextUtils.isEmpty(url)) {
488 break;
489 }
490 HashMap focusNodeMap = (HashMap) msg.obj;
491 WebView view = (WebView) focusNodeMap.get("webview");
492 // Only apply the action if the top window did not change.
493 if (getCurrentTopWebView() != view) {
494 break;
495 }
496 switch (msg.arg1) {
497 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700498 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700499 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500500 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700501 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500502 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500503 case R.id.open_newtab_context_menu_id:
504 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700505 openTab(url, parent,
506 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500507 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 case R.id.copy_link_context_menu_id:
509 copy(url);
510 break;
511 case R.id.save_link_context_menu_id:
512 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500513 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800514 mActivity, url, view.getSettings().getUserAgentString(),
515 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700516 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700517 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700518 if(title == null || title == "")
519 title = url;
520
521 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
522 //SWE TODO: No thumbnail support for the url obtained via
523 //browser context menu as its not loaded in webview.
524 if (bookmarkIntent != null) {
525 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
526 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
527 mActivity.startActivity(bookmarkIntent);
528 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700529 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 }
531 break;
532 }
533
534 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700535 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 break;
537
538 case STOP_LOAD:
539 stopLoading();
540 break;
541
542 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700543 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700544 mWakeLock.release();
545 // if we reach here, Browser should be still in the
546 // background loading after WAKELOCK_TIMEOUT (5-min).
547 // To avoid burning the battery, stop loading.
548 mTabControl.stopAllLoading();
549 }
550 break;
551
552 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800553 Tab tab = (Tab) msg.obj;
554 if (tab != null) {
555 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 }
557 break;
kaiyiz591110b2013-08-06 17:11:06 +0800558 case OPEN_MENU:
559 if (!mOptionsMenuOpen && mActivity != null ) {
560 mActivity.openOptionsMenu();
561 }
562 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700563 }
564 }
565 };
566
567 }
568
John Reckef654f12011-07-12 16:42:08 -0700569 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200570 public Tab getCurrentTab() {
571 return mTabControl.getCurrentTab();
572 }
573
Michael Kolbba99c5d2010-11-29 14:57:41 -0800574 @Override
575 public void shareCurrentPage() {
576 shareCurrentPage(mTabControl.getCurrentTab());
577 }
578
579 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700580 if (tab == null || tab.getWebView() == null)
581 return;
582
583 final Tab mytab = tab;
584 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
585 @Override
586 public void onReceiveValue(Bitmap bitmap) {
587 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
588 mytab.getFavicon(), bitmap);
589 }
590 };
591
592 createScreenshotAsync(
593 tab.getWebView(),
594 getDesiredThumbnailWidth(mActivity),
595 getDesiredThumbnailHeight(mActivity),
596 new ValueCallback<Bitmap>() {
597 @Override
598 public void onReceiveValue(Bitmap bitmap) {
599 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
600 mytab.getFavicon(), bitmap);
601 }
602 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800603 }
604
Michael Kolb8233fac2010-10-26 16:08:53 -0700605 /**
606 * Share a page, providing the title, url, favicon, and a screenshot. Uses
607 * an {@link Intent} to launch the Activity chooser.
608 * @param c Context used to launch a new Activity.
609 * @param title Title of the page. Stored in the Intent with
610 * {@link Intent#EXTRA_SUBJECT}
611 * @param url URL of the page. Stored in the Intent with
612 * {@link Intent#EXTRA_TEXT}
613 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
614 * with {@link Browser#EXTRA_SHARE_FAVICON}
615 * @param screenshot Bitmap of a screenshot of the page. Stored in the
616 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
617 */
618 static final void sharePage(Context c, String title, String url,
619 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700620
621 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
622 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
623 R.layout.app_row, sDialog.getApps());
624 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 }
626
627 private void copy(CharSequence text) {
628 ClipboardManager cm = (ClipboardManager) mActivity
629 .getSystemService(Context.CLIPBOARD_SERVICE);
630 cm.setText(text);
631 }
632
633 // lifecycle
634
John Reck9c35b9c2012-05-30 10:08:50 -0700635 @Override
636 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800638 // update the menu in case of a locale change
639 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800640 if (mOptionsMenuOpen) {
641 mActivity.closeOptionsMenu();
642 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
643 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700644 if (mPageDialogsHandler != null) {
645 mPageDialogsHandler.onConfigurationChanged(config);
646 }
647 mUi.onConfigurationChanged(config);
648 }
649
650 @Override
651 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700652 if (!mUi.isWebShowing()) {
653 mUi.showWeb(false);
654 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 mIntentHandler.onNewIntent(intent);
656 }
657
John Reck9c35b9c2012-05-30 10:08:50 -0700658 @Override
659 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800660 if (mUi.isCustomViewShowing()) {
661 hideCustomView();
662 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 if (mActivityPaused) {
664 Log.e(LOGTAG, "BrowserActivity is already paused.");
665 return;
666 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800668 Tab tab = mTabControl.getCurrentTab();
669 if (tab != null) {
670 tab.pause();
671 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700672 if (mWakeLock == null) {
673 PowerManager pm = (PowerManager) mActivity
674 .getSystemService(Context.POWER_SERVICE);
675 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
676 }
Michael Kolb70976932010-11-30 11:34:01 -0800677 mWakeLock.acquire();
678 mHandler.sendMessageDelayed(mHandler
679 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
680 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700681 }
682 mUi.onPause();
683 mNetworkHandler.onPause();
684
685 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100686 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 }
688
John Reck9c35b9c2012-05-30 10:08:50 -0700689 @Override
690 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700691 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800692 Bundle saveState = createSaveState();
693
694 // crash recovery manages all save & restore state
695 mCrashRecoveryHandler.writeState(saveState);
696 mSettings.setLastRunPaused(true);
697 }
698
699 /**
700 * Save the current state to outState. Does not write the state to
701 * disk.
702 * @return Bundle containing the current state of all tabs.
703 */
704 /* package */ Bundle createSaveState() {
705 Bundle saveState = new Bundle();
706 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800707 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700708 }
709
John Reck9c35b9c2012-05-30 10:08:50 -0700710 @Override
711 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 if (!mActivityPaused) {
713 Log.e(LOGTAG, "BrowserActivity is already resumed.");
714 return;
715 }
George Mount3636d0a2011-11-21 09:08:21 -0800716 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700717 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800718 Tab current = mTabControl.getCurrentTab();
719 if (current != null) {
720 current.resume();
721 resumeWebViewTimers(current);
722 }
John Reckf57c0292011-07-21 18:15:39 -0700723 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200724
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 mUi.onResume();
726 mNetworkHandler.onResume();
727 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100728 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700729 if (mVoiceResult != null) {
730 mUi.onVoiceResult(mVoiceResult);
731 mVoiceResult = null;
732 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800733 if (current != null && current.hasCrashed) {
734 current.showCrashView();
735 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700736 }
737
John Reckf57c0292011-07-21 18:15:39 -0700738 private void releaseWakeLock() {
739 if (mWakeLock != null && mWakeLock.isHeld()) {
740 mHandler.removeMessages(RELEASE_WAKELOCK);
741 mWakeLock.release();
742 }
743 }
744
Michael Kolb70976932010-11-30 11:34:01 -0800745 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800746 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800747 * @param tab guaranteed non-null
748 */
749 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700750 boolean inLoad = tab.inPageLoad();
751 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
752 CookieSyncManager.getInstance().startSync();
753 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100754 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700755 }
756 }
757
Michael Kolb70976932010-11-30 11:34:01 -0800758 /**
759 * Pause all WebView timers using the WebView of the given tab
760 * @param tab
761 * @return true if the timers are paused or tab is null
762 */
763 private boolean pauseWebViewTimers(Tab tab) {
764 if (tab == null) {
765 return true;
766 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700767 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100768 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700769 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700770 }
Michael Kolb70976932010-11-30 11:34:01 -0800771 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700772 }
773
John Reck9c35b9c2012-05-30 10:08:50 -0700774 @Override
775 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800776 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700777 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
778 mUploadHandler = null;
779 }
780 if (mTabControl == null) return;
781 mUi.onDestroy();
782 // Remove the current tab and sub window
783 Tab t = mTabControl.getCurrentTab();
784 if (t != null) {
785 dismissSubWindow(t);
786 removeTab(t);
787 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500788 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700789 // Destroy all the tabs
790 mTabControl.destroy();
Michael Kolb8233fac2010-10-26 16:08:53 -0700791 }
792
793 protected boolean isActivityPaused() {
794 return mActivityPaused;
795 }
796
John Reck9c35b9c2012-05-30 10:08:50 -0700797 @Override
798 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 mTabControl.freeMemory();
800 }
801
802 @Override
803 public boolean shouldShowErrorConsole() {
804 return mShouldShowErrorConsole;
805 }
806
807 protected void setShouldShowErrorConsole(boolean show) {
808 if (show == mShouldShowErrorConsole) {
809 // Nothing to do.
810 return;
811 }
812 mShouldShowErrorConsole = show;
813 Tab t = mTabControl.getCurrentTab();
814 if (t == null) {
815 // There is no current tab so we cannot toggle the error console
816 return;
817 }
818 mUi.setShouldShowErrorConsole(t, show);
819 }
820
821 @Override
822 public void stopLoading() {
823 mLoadStopped = true;
824 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700826 if (w != null) {
827 w.stopLoading();
828 mUi.onPageStopped(tab);
829 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700830 }
831
832 boolean didUserStopLoading() {
833 return mLoadStopped;
834 }
835
kaiyiza016da12013-08-26 17:50:22 +0800836 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700837 final String reminderType = getContext().getResources().getString(
838 R.string.def_wifi_browser_interaction_remind_type);
839 final String selectionConnnection = getContext().getResources().getString(
840 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700841 final String wifiSelection = getContext().getResources().getString(
842 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700843
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700844 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
845 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700846 return;
847
kaiyiza016da12013-08-26 17:50:22 +0800848 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700849 Context.CONNECTIVITY_SERVICE);
850 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700851 WifiManager wifiMgr = (WifiManager) this.getContext()
852 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700853 if (networkInfo == null
854 || (networkInfo != null && (networkInfo.getType() !=
855 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700856 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
857 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700858 List<ScanResult> list = wifiMgr.getScanResults();
859 // Have no AP's for Wifi's fall back to data
860 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700861 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700862 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
863 this.getContext().startActivity(intent);
864 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700865 // Request to select Wifi AP
866 try {
867 Intent intent = new Intent(wifiSelection);
868 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
869 this.getContext().startActivity(intent);
870 } catch (Exception e) {
871 String err_msg = this.getContext().getString(
872 R.string.acivity_not_found, wifiSelection);
873 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
874 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700875 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700876 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800877 }
878 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700879
Michael Kolb8233fac2010-10-26 16:08:53 -0700880 // WebViewController
881
882 @Override
John Reck324d4402011-01-11 16:56:42 -0800883 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700884
885 // We've started to load a new page. If there was a pending message
886 // to save a screenshot then we will now take the new page and save
887 // an incorrect screenshot. Therefore, remove any pending thumbnail
888 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700889 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700890
891 // reset sync timer to avoid sync starts during loading a page
892 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700893 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700894 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700895 boolean networkNotifier =
896 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700897 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700898 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800899 } else {
900 if (!mNetworkHandler.isNetworkUp()) {
901 view.setNetworkAvailable(false);
902 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 }
904
905 // when BrowserActivity just starts, onPageStarted may be called before
906 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
907 // to start the timer. As we won't switch tabs while an activity is in
908 // pause state, we can ensure calling resume and pause in pair.
909 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800910 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700911 }
912 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700913 endActionMode();
914
John Reck30c714c2010-12-16 17:30:34 -0800915 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700916
John Reck324d4402011-01-11 16:56:42 -0800917 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700918 // update the bookmark database for favicon
919 maybeUpdateFavicon(tab, null, url, favicon);
920
921 Performance.tracePageStart(url);
922
923 // Performance probe
924 if (false) {
925 Performance.onPageStarted();
926 }
927
928 }
929
930 @Override
John Reck324d4402011-01-11 16:56:42 -0800931 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700932 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800933 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200934
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 // Performance probe
936 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800937 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 }
939
Tarun Nainani8eb00912014-07-17 12:28:32 -0700940 tab.onPageFinished();
941
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 Performance.tracePageFinished();
943 }
944
945 @Override
John Reck30c714c2010-12-16 17:30:34 -0800946 public void onProgressChanged(Tab tab) {
947 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700948
949 if (newProgress == 100) {
950 CookieSyncManager.getInstance().sync();
951 // onProgressChanged() may continue to be called after the main
952 // frame has finished loading, as any remaining sub frames continue
953 // to load. We'll only get called once though with newProgress as
954 // 100 when everything is loaded. (onPageFinished is called once
955 // when the main frame completes loading regardless of the state of
956 // any sub frames so calls to onProgressChanges may continue after
957 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800958 if (tab.inPageLoad()) {
959 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200960 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
961 // pause the WebView timer and release the wake lock if it is
962 // finished while BrowserActivity is in pause state.
963 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 }
John Reckd9862372012-02-21 15:04:50 -0800965 if (!tab.isPrivateBrowsingEnabled()
966 && !TextUtils.isEmpty(tab.getUrl())
967 && !tab.isSnapshot()) {
968 // Only update the bookmark screenshot if the user did not
969 // cancel the load early and there is not already
970 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700971 if (tab.shouldUpdateThumbnail() &&
972 (tab.inForeground() && !didUserStopLoading()
973 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800974 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
975 mHandler.sendMessageDelayed(mHandler.obtainMessage(
976 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800977 1500);
John Reckd9862372012-02-21 15:04:50 -0800978 }
979 }
980 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700981 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800982 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800984 // still loading
985 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800987 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700988 }
989 }
John Reck30c714c2010-12-16 17:30:34 -0800990 mUi.onProgressChanged(tab);
991 }
992
993 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100994 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800995 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 }
997
998 @Override
999 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001000 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001001 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001002 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1004 return;
1005 }
1006 // Update the title in the history database if not in private browsing mode
1007 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001008 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001009 }
1010 }
1011
1012 @Override
1013 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001014 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001015 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1016 }
1017
1018 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001019 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1020 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001021 }
1022
1023 @Override
1024 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1025 if (mMenuIsDown) {
1026 // only check shortcut key when MENU is held
1027 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1028 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001029 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001030 int keyCode = event.getKeyCode();
1031 // We need to send almost every key to WebKit. However:
1032 // 1. We don't want to block the device on the renderer for
1033 // some keys like menu, home, call.
1034 // 2. There are no WebKit equivalents for some of these keys
1035 // (see app/keyboard_codes_win.h)
1036 // Note that these are not the same set as KeyEvent.isSystemKey:
1037 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1038 if (keyCode == KeyEvent.KEYCODE_MENU ||
1039 keyCode == KeyEvent.KEYCODE_HOME ||
1040 keyCode == KeyEvent.KEYCODE_BACK ||
1041 keyCode == KeyEvent.KEYCODE_CALL ||
1042 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1043 keyCode == KeyEvent.KEYCODE_POWER ||
1044 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1045 keyCode == KeyEvent.KEYCODE_CAMERA ||
1046 keyCode == KeyEvent.KEYCODE_FOCUS ||
1047 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1048 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1049 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1050 return true;
1051 }
1052
1053 // We also have to intercept some shortcuts before we send them to the ContentView.
1054 if (event.isCtrlPressed() && (
1055 keyCode == KeyEvent.KEYCODE_TAB ||
1056 keyCode == KeyEvent.KEYCODE_W ||
1057 keyCode == KeyEvent.KEYCODE_F4)) {
1058 return true;
1059 }
1060
1061 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 }
1063
1064 @Override
John Reck997b1b72012-04-19 18:08:25 -07001065 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001066 if (!isActivityPaused()) {
1067 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001068 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001069 } else {
John Reck997b1b72012-04-19 18:08:25 -07001070 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001071 }
1072 }
John Reck997b1b72012-04-19 18:08:25 -07001073 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001074 }
1075
1076 @Override
John Reck324d4402011-01-11 16:56:42 -08001077 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001078 boolean disableHistoryWrites =
1079 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1080
1081 // Don't save anything in private browsing mode or when explicitly set
1082 // not to write history via an overlay
1083 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001084 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001085
John Reck324d4402011-01-11 16:56:42 -08001086 if (TextUtils.isEmpty(url)
1087 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001088 return;
1089 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001090
John Reckf57c0292011-07-21 18:15:39 -07001091 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001092 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001093 }
1094
1095 @Override
1096 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1097 AsyncTask<Void, Void, String[]> task =
1098 new AsyncTask<Void, Void, String[]>() {
1099 @Override
1100 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001101 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001102 }
1103 @Override
1104 public void onPostExecute(String[] result) {
1105 callback.onReceiveValue(result);
1106 }
1107 };
1108 task.execute();
1109 }
1110
1111 @Override
1112 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1113 final HttpAuthHandler handler, final String host,
1114 final String realm) {
1115 String username = null;
1116 String password = null;
1117
1118 boolean reuseHttpAuthUsernamePassword
1119 = handler.useHttpAuthUsernamePassword();
1120
1121 if (reuseHttpAuthUsernamePassword && view != null) {
1122 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1123 if (credentials != null && credentials.length == 2) {
1124 username = credentials[0];
1125 password = credentials[1];
1126 }
1127 }
1128
1129 if (username != null && password != null) {
1130 handler.proceed(username, password);
1131 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001132 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001133 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1134 } else {
1135 handler.cancel();
1136 }
1137 }
1138 }
1139
1140 @Override
1141 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001142 String contentDisposition, String mimetype, String referer,
1143 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001144 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001145 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001146 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001147 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 // This Tab was opened for the sole purpose of downloading a
1149 // file. Remove it.
1150 if (tab == mTabControl.getCurrentTab()) {
1151 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001152 if (tab.getDerivedFromIntent())
1153 closeTab(tab);
1154 else
1155 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 } else {
1157 // In this case, it is not.
1158 closeTab(tab);
1159 }
1160 }
1161 }
1162
1163 @Override
1164 public Bitmap getDefaultVideoPoster() {
1165 return mUi.getDefaultVideoPoster();
1166 }
1167
1168 @Override
1169 public View getVideoLoadingProgressView() {
1170 return mUi.getVideoLoadingProgressView();
1171 }
1172
1173 @Override
1174 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1175 SslError error) {
1176 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1177 }
1178
1179 // helper method
1180
1181 /*
1182 * Update the favorites icon if the private browsing isn't enabled and the
1183 * icon is valid.
1184 */
1185 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1186 final String url, Bitmap favicon) {
1187 if (favicon == null) {
1188 return;
1189 }
1190 if (!tab.isPrivateBrowsingEnabled()) {
1191 Bookmarks.updateFavicon(mActivity
1192 .getContentResolver(), originalUrl, url, favicon);
1193 }
1194 }
1195
Leon Scroggins4cd97792010-12-03 15:31:56 -05001196 @Override
1197 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001198 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001199 mUi.bookmarkedStatusHasChanged(tab);
1200 }
1201
Michael Kolb8233fac2010-10-26 16:08:53 -07001202 // end WebViewController
1203
1204 protected void pageUp() {
1205 getCurrentTopWebView().pageUp(false);
1206 }
1207
1208 protected void pageDown() {
1209 getCurrentTopWebView().pageDown(false);
1210 }
1211
1212 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001213 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001214 public void editUrl() {
1215 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001216 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001217 }
1218
John Reck9c35b9c2012-05-30 10:08:50 -07001219 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001220 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001221 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001222 if (tab.inForeground()) {
1223 if (mUi.isCustomViewShowing()) {
1224 callback.onCustomViewHidden();
1225 return;
1226 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001227 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001228 // Save the menu state and set it to empty while the custom
1229 // view is showing.
1230 mOldMenuState = mMenuState;
1231 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001232 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001233 }
1234 }
1235
1236 @Override
1237 public void hideCustomView() {
1238 if (mUi.isCustomViewShowing()) {
1239 mUi.onHideCustomView();
1240 // Reset the old menu state.
1241 mMenuState = mOldMenuState;
1242 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001243 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001244 }
1245 }
1246
John Reck9c35b9c2012-05-30 10:08:50 -07001247 @Override
1248 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001249 Intent intent) {
1250 if (getCurrentTopWebView() == null) return;
1251 switch (requestCode) {
1252 case PREFERENCES_PAGE:
1253 if (resultCode == Activity.RESULT_OK && intent != null) {
1254 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001255 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001256 mTabControl.removeParentChildRelationShips();
1257 }
1258 }
1259 break;
1260 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001261 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001262 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001263 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001265 case AUTOFILL_SETUP:
1266 // Determine whether a profile was actually set up or not
1267 // and if so, send the message back to the WebTextView to
1268 // fill the form with the new profile.
1269 if (getSettings().getAutoFillProfile() != null) {
1270 mAutoFillSetupMessage.sendToTarget();
1271 mAutoFillSetupMessage = null;
1272 }
1273 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001274 case COMBO_VIEW:
1275 if (intent == null || resultCode != Activity.RESULT_OK) {
1276 break;
1277 }
John Reck3ba45532011-08-11 16:26:53 -07001278 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001279 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1280 Tab t = getCurrentTab();
1281 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001282 mUpdateMyNavThumbnail = true;
1283 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001284 loadUrl(t, uri.toString());
1285 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1286 String[] urls = intent.getStringArrayExtra(
1287 ComboViewActivity.EXTRA_OPEN_ALL);
1288 Tab parent = getCurrentTab();
1289 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001290 if (url != null) {
1291 parent = openTab(url, parent,
1292 !mSettings.openInBackground(), true);
1293 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001294 }
1295 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1296 long id = intent.getLongExtra(
1297 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1298 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001299 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001300 }
1301 }
1302 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001303 case VOICE_RESULT:
1304 if (resultCode == Activity.RESULT_OK && intent != null) {
1305 ArrayList<String> results = intent.getStringArrayListExtra(
1306 RecognizerIntent.EXTRA_RESULTS);
1307 if (results.size() >= 1) {
1308 mVoiceResult = results.get(0);
1309 }
1310 }
1311 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001312 case MY_NAVIGATION:
1313 if (intent == null || resultCode != Activity.RESULT_OK) {
1314 break;
1315 }
1316
1317 if (intent.getBooleanExtra("need_refresh", false) &&
1318 getCurrentTopWebView() != null) {
1319 getCurrentTopWebView().reload();
1320 }
1321 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001322 default:
1323 break;
1324 }
1325 getCurrentTopWebView().requestFocus();
1326 }
1327
1328 /**
1329 * Open the Go page.
1330 * @param startWithHistory If true, open starting on the history tab.
1331 * Otherwise, start with the bookmarks tab.
1332 */
1333 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001334 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 if (mTabControl.getCurrentWebView() == null) {
1336 return;
1337 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001338 // clear action mode
1339 if (isInCustomActionMode()) {
1340 endActionMode();
1341 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001342 Bundle extras = new Bundle();
1343 // Disable opening in a new window if we have maxed out the windows
1344 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1345 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001346 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001347 }
1348
1349 // combo view callbacks
1350
Michael Kolb8233fac2010-10-26 16:08:53 -07001351 // key handling
1352 protected void onBackKey() {
1353 if (!mUi.onBackKey()) {
1354 WebView subwindow = mTabControl.getCurrentSubWindow();
1355 if (subwindow != null) {
1356 if (subwindow.canGoBack()) {
1357 subwindow.goBack();
1358 } else {
1359 dismissSubWindow(mTabControl.getCurrentTab());
1360 }
1361 } else {
1362 goBackOnePageOrQuit();
1363 }
1364 }
1365 }
1366
Michael Kolb4bd767d2011-05-27 11:33:55 -07001367 protected boolean onMenuKey() {
1368 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001369 }
1370
Michael Kolb8233fac2010-10-26 16:08:53 -07001371 // menu handling and state
1372 // TODO: maybe put into separate handler
1373
John Reck9c35b9c2012-05-30 10:08:50 -07001374 @Override
1375 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001376 if (mMenuState == EMPTY_MENU) {
1377 return false;
1378 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001379 MenuInflater inflater = mActivity.getMenuInflater();
1380 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001381 return true;
1382 }
1383
John Reck9c35b9c2012-05-30 10:08:50 -07001384 @Override
1385 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001386 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001387 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001388 return;
1389 }
1390 if (!(v instanceof WebView)) {
1391 return;
1392 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001393 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 WebView.HitTestResult result = webview.getHitTestResult();
1395 if (result == null) {
1396 return;
1397 }
1398
1399 int type = result.getType();
1400 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1401 Log.w(LOGTAG,
1402 "We should not show context menu when nothing is touched");
1403 return;
1404 }
1405 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1406 // let TextView handles context menu
1407 return;
1408 }
1409
1410 // Note, http://b/issue?id=1106666 is requesting that
1411 // an inflated menu can be used again. This is not available
1412 // yet, so inflate each time (yuk!)
1413 MenuInflater inflater = mActivity.getMenuInflater();
1414 inflater.inflate(R.menu.browsercontext, menu);
1415
1416 // Show the correct menu group
1417 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001418 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001419 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001420 menu.setGroupVisible(R.id.PHONE_MENU,
1421 type == WebView.HitTestResult.PHONE_TYPE);
1422 menu.setGroupVisible(R.id.EMAIL_MENU,
1423 type == WebView.HitTestResult.EMAIL_TYPE);
1424 menu.setGroupVisible(R.id.GEO_MENU,
1425 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001426 String itemUrl = null;
1427 String url = webview.getOriginalUrl();
1428 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1429 itemUrl = Uri.decode(navigationUrl);
1430 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1431 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1432 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1433 } else {
1434 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1435 }
1436 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1437 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1438 } else {
1439 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1440
1441 menu.setGroupVisible(R.id.IMAGE_MENU,
1442 type == WebView.HitTestResult.IMAGE_TYPE
1443 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1444 menu.setGroupVisible(R.id.ANCHOR_MENU,
1445 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1446 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001447 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1448 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001449 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001450 // Setup custom handling depending on the type
1451 switch (type) {
1452 case WebView.HitTestResult.PHONE_TYPE:
1453 menu.setHeaderTitle(Uri.decode(extra));
1454 menu.findItem(R.id.dial_context_menu_id).setIntent(
1455 new Intent(Intent.ACTION_VIEW, Uri
1456 .parse(WebView.SCHEME_TEL + extra)));
1457 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1458 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1459 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1460 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1461 addIntent);
1462 menu.findItem(R.id.copy_phone_context_menu_id)
1463 .setOnMenuItemClickListener(
1464 new Copy(extra));
1465 break;
1466
1467 case WebView.HitTestResult.EMAIL_TYPE:
1468 menu.setHeaderTitle(extra);
1469 menu.findItem(R.id.email_context_menu_id).setIntent(
1470 new Intent(Intent.ACTION_VIEW, Uri
1471 .parse(WebView.SCHEME_MAILTO + extra)));
1472 menu.findItem(R.id.copy_mail_context_menu_id)
1473 .setOnMenuItemClickListener(
1474 new Copy(extra));
1475 break;
1476
1477 case WebView.HitTestResult.GEO_TYPE:
1478 menu.setHeaderTitle(extra);
1479 menu.findItem(R.id.map_context_menu_id).setIntent(
1480 new Intent(Intent.ACTION_VIEW, Uri
1481 .parse(WebView.SCHEME_GEO
1482 + URLEncoder.encode(extra))));
1483 menu.findItem(R.id.copy_geo_context_menu_id)
1484 .setOnMenuItemClickListener(
1485 new Copy(extra));
1486 break;
1487
1488 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1489 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001490 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001491 // decide whether to show the open link in new tab option
1492 boolean showNewTab = mTabControl.canCreateNewTab();
1493 MenuItem newTabItem
1494 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001495 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001496 ? R.string.contextmenu_openlink_newwindow_background
1497 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001498 newTabItem.setVisible(showNewTab);
1499 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001500 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1501 newTabItem.setOnMenuItemClickListener(
1502 new MenuItem.OnMenuItemClickListener() {
1503 @Override
1504 public boolean onMenuItemClick(MenuItem item) {
1505 final HashMap<String, WebView> hrefMap =
1506 new HashMap<String, WebView>();
1507 hrefMap.put("webview", webview);
1508 final Message msg = mHandler.obtainMessage(
1509 FOCUS_NODE_HREF,
1510 R.id.open_newtab_context_menu_id,
1511 0, hrefMap);
1512 webview.requestFocusNodeHref(msg);
1513 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001515 });
1516 } else {
1517 newTabItem.setOnMenuItemClickListener(
1518 new MenuItem.OnMenuItemClickListener() {
1519 @Override
1520 public boolean onMenuItemClick(MenuItem item) {
1521 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001522 openTab(extra, parent,
1523 !mSettings.openInBackground(),
1524 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001525 return true;
1526 }
1527 });
1528 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001529 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001530 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1531 menu.setHeaderTitle(navigationUrl);
1532 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1533
1534 if (itemUrl != null) {
1535 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1536 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1537 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1538 @Override
1539 public boolean onMenuItemClick(MenuItem item) {
1540 final Intent intent = new Intent(Controller.this
1541 .getContext(),
1542 AddMyNavigationPage.class);
1543 Bundle bundle = new Bundle();
1544 String url = Uri.decode(navigationUrl);
1545 bundle.putBoolean("isAdding", false);
1546 bundle.putString("url", url);
1547 bundle.putString("name", getNameFromUrl(url));
1548 intent.putExtra("websites", bundle);
1549 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1550 return false;
1551 }
1552 });
1553 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1554 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1555 @Override
1556 public boolean onMenuItemClick(MenuItem item) {
1557 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1558 return false;
1559 }
1560 });
1561 }
1562 } else {
1563 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1564 }
1565 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001566 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1567 break;
1568 }
1569 // otherwise fall through to handle image part
1570 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001571 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1572 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001573 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1574 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001575 shareItem.setOnMenuItemClickListener(
1576 new MenuItem.OnMenuItemClickListener() {
1577 @Override
1578 public boolean onMenuItemClick(MenuItem item) {
1579 sharePage(mActivity, null, extra, null,
1580 null);
1581 return true;
1582 }
1583 }
1584 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001585 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001586 menu.findItem(R.id.view_image_context_menu_id)
1587 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1588 @Override
1589 public boolean onMenuItemClick(MenuItem item) {
1590 openTab(extra, mTabControl.getCurrentTab(), true, true);
1591 return false;
1592 }
1593 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001594 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1595 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1596 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001597 menu.findItem(R.id.set_wallpaper_context_menu_id).
1598 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1599 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 break;
1601
1602 default:
1603 Log.w(LOGTAG, "We should not get here.");
1604 break;
1605 }
1606 //update the ui
1607 mUi.onContextMenuCreated(menu);
1608 }
1609
kaiyiz6e5b3e02013-08-19 20:02:01 +08001610 public void startAddMyNavigation(String url) {
1611 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1612 Bundle bundle = new Bundle();
1613 bundle.putBoolean("isAdding", true);
1614 bundle.putString("url", url);
1615 bundle.putString("name", getNameFromUrl(url));
1616 intent.putExtra("websites", bundle);
1617 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1618 }
1619
1620 private void showMyNavigationDeleteDialog(final String itemUrl) {
1621 new AlertDialog.Builder(this.getContext())
1622 .setTitle(R.string.my_navigation_delete_label)
1623 .setIcon(android.R.drawable.ic_dialog_alert)
1624 .setMessage(R.string.my_navigation_delete_msg)
1625 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1626 @Override
1627 public void onClick(DialogInterface dialog, int whichButton) {
1628 deleteMyNavigationItem(itemUrl);
1629 }
1630 })
1631 .setNegativeButton(R.string.cancel, null)
1632 .show();
1633 }
1634
1635 private void deleteMyNavigationItem(final String itemUrl) {
1636 ContentResolver cr = this.getContext().getContentResolver();
1637 Cursor cursor = null;
1638
1639 try {
1640 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1641 new String[] {
1642 MyNavigationUtil.ID
1643 }, "url = ?", new String[] {
1644 itemUrl
1645 }, null);
1646 if (null != cursor && cursor.moveToFirst()) {
1647 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1648 cursor.getLong(0));
1649
1650 ContentValues values = new ContentValues();
1651 values.put(MyNavigationUtil.TITLE, "");
1652 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1653 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1654 ByteArrayOutputStream os = new ByteArrayOutputStream();
1655 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1656 R.raw.my_navigation_add);
1657 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1658 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1659 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1660 cr.update(uri, values, null, null);
1661 } else {
1662 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1663 }
1664 } catch (IllegalStateException e) {
1665 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1666 } finally {
1667 if (null != cursor) {
1668 cursor.close();
1669 }
1670 }
1671
1672 if (getCurrentTopWebView() != null) {
1673 getCurrentTopWebView().reload();
1674 }
1675 }
1676
1677 private String getNameFromUrl(String itemUrl) {
1678 ContentResolver cr = this.getContext().getContentResolver();
1679 Cursor cursor = null;
1680 String name = null;
1681
1682 try {
1683 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1684 new String[] {
1685 MyNavigationUtil.TITLE
1686 }, "url = ?", new String[] {
1687 itemUrl
1688 }, null);
1689 if (null != cursor && cursor.moveToFirst()) {
1690 name = cursor.getString(0);
1691 } else {
1692 Log.e(LOGTAG, "this item does not exist!");
1693 }
1694 } catch (IllegalStateException e) {
1695 Log.e(LOGTAG, "getNameFromUrl", e);
1696 } finally {
1697 if (null != cursor) {
1698 cursor.close();
1699 }
1700 }
1701 return name;
1702 }
1703
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001704 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
1705 if (bitmap == null) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001706 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1707 return;
1708 }
1709
1710 final ContentResolver cr = mActivity.getContentResolver();
1711 new AsyncTask<Void, Void, Void>() {
1712 @Override
1713 protected Void doInBackground(Void... unused) {
1714 ContentResolver cr = mActivity.getContentResolver();
1715 Cursor cursor = null;
1716 try {
1717 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1718 new String[] {
1719 MyNavigationUtil.ID
1720 }, "url = ?", new String[] {
1721 itemUrl
1722 }, null);
1723 if (null != cursor && cursor.moveToFirst()) {
1724 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001725 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001726
1727 ContentValues values = new ContentValues();
1728 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1729 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1730 cursor.getLong(0));
1731 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1732 cr.update(uri, values, null, null);
1733 os.close();
1734 }
1735 } catch (IllegalStateException e) {
1736 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1737 } catch (IOException e) {
1738 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1739 } finally {
1740 if (null != cursor) {
1741 cursor.close();
1742 }
1743 }
1744 return null;
1745 }
1746 }.execute();
1747 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001748 /**
1749 * As the menu can be open when loading state changes
1750 * we must manually update the state of the stop/reload menu
1751 * item
1752 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001753 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001754 if (menu == null) {
1755 return;
1756 }
1757 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001758 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001759 menu.findItem(R.id.stop_menu_id):
1760 menu.findItem(R.id.reload_menu_id);
1761 if (src != null) {
1762 dest.setIcon(src.getIcon());
1763 dest.setTitle(src.getTitle());
1764 }
1765 }
1766
John Reck9c35b9c2012-05-30 10:08:50 -07001767 @Override
1768 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001769 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001770 // hold on to the menu reference here; it is used by the page callbacks
1771 // to update the menu based on loading state
1772 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001773 // Note: setVisible will decide whether an item is visible; while
1774 // setEnabled() will decide whether an item is enabled, which also means
1775 // whether the matching shortcut key will function.
1776 switch (mMenuState) {
1777 case EMPTY_MENU:
1778 if (mCurrentMenuState != mMenuState) {
1779 menu.setGroupVisible(R.id.MAIN_MENU, false);
1780 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1781 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1782 }
1783 break;
1784 default:
1785 if (mCurrentMenuState != mMenuState) {
1786 menu.setGroupVisible(R.id.MAIN_MENU, true);
1787 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1788 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1789 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001790 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001791 break;
1792 }
1793 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001794 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001795 }
1796
Michael Kolb4bf79712011-07-14 14:18:12 -07001797 @Override
1798 public void updateMenuState(Tab tab, Menu menu) {
1799 boolean canGoBack = false;
1800 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001801 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001802 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001803 // Following flag is used to identify schemes for which the LIVE_MENU
1804 // items defined in res/menu/browser.xml should be enabled
1805 boolean isLiveScheme = false;
1806 boolean isPageFinished = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001807 if (tab != null) {
1808 canGoBack = tab.canGoBack();
1809 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001810 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001811 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001812 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
1813 isPageFinished = tab.getPageFinishedStatus();
Michael Kolb4bf79712011-07-14 14:18:12 -07001814 }
1815 final MenuItem back = menu.findItem(R.id.back_menu_id);
1816 back.setEnabled(canGoBack);
1817
1818 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001819
1820 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1821 forward.setEnabled(canGoForward);
1822
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001823 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1824 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001825 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001826 if (source != null && dest != null) {
1827 dest.setTitle(source.getTitle());
1828 dest.setIcon(source.getIcon());
1829 }
John Recke1a03a32011-09-14 17:04:16 -07001830 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001831
1832 // decide whether to show the share link option
1833 PackageManager pm = mActivity.getPackageManager();
1834 Intent send = new Intent(Intent.ACTION_SEND);
1835 send.setType("text/plain");
1836 ResolveInfo ri = pm.resolveActivity(send,
1837 PackageManager.MATCH_DEFAULT_ONLY);
1838 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1839
1840 boolean isNavDump = mSettings.enableNavDump();
1841 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1842 nav.setVisible(isNavDump);
1843 nav.setEnabled(isNavDump);
1844
1845 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001846 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1847 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001848 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001849 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001850 menu.setGroupEnabled(R.id.OFFLINE_READING, isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001851 // history and snapshots item are the members of COMBO menu group,
1852 // so if show history item, only make snapshots item invisible.
1853 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001854
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001855 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001856 }
1857
John Reck9c35b9c2012-05-30 10:08:50 -07001858 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001859 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001860 if (null == getCurrentTopWebView()) {
1861 return false;
1862 }
1863 if (mMenuIsDown) {
1864 // The shortcut action consumes the MENU. Even if it is still down,
1865 // it won't trigger the next shortcut action. In the case of the
1866 // shortcut action triggering a new activity, like Bookmarks, we
1867 // won't get onKeyUp for MENU. So it is important to reset it here.
1868 mMenuIsDown = false;
1869 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001870 if (mUi.onOptionsItemSelected(item)) {
1871 // ui callback handled it
1872 return true;
1873 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001874 switch (item.getItemId()) {
1875 // -- Main menu
1876 case R.id.new_tab_menu_id:
1877 openTabToHomePage();
1878 break;
1879
1880 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001881 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001882 break;
1883
Afzal Najamd4e33312012-04-26 01:54:01 -04001884 case R.id.close_other_tabs_id:
1885 closeOtherTabs();
1886 break;
1887
Michael Kolb8233fac2010-10-26 16:08:53 -07001888 case R.id.goto_menu_id:
1889 editUrl();
1890 break;
1891
1892 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001893 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1894 break;
1895
1896 case R.id.history_menu_id:
1897 bookmarksOrHistoryPicker(ComboViews.History);
1898 break;
1899
1900 case R.id.snapshots_menu_id:
1901 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001902 break;
1903
Michael Kolb8233fac2010-10-26 16:08:53 -07001904 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001905 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001906 break;
1907
1908 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001909 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001910 stopLoading();
1911 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001912 Tab currentTab = mTabControl.getCurrentTab();
1913 if (currentTab.hasCrashed) {
1914 currentTab.replaceCrashView(getCurrentTopWebView(),
1915 currentTab.getViewContainer());
1916 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001917 getCurrentTopWebView().reload();
1918 }
1919 break;
1920
1921 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001922 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001923 break;
1924
1925 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001926 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001927 break;
1928
1929 case R.id.close_menu_id:
1930 // Close the subwindow if it exists.
1931 if (mTabControl.getCurrentSubWindow() != null) {
1932 dismissSubWindow(mTabControl.getCurrentTab());
1933 break;
1934 }
1935 closeCurrentTab();
1936 break;
1937
kaiyiza8b6dbb2013-07-29 18:11:22 +08001938 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001939 Object[] params = { new String("persist.debug.browsermonkeytest")};
1940 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001941 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001942 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001943 if (ret != null && ret.equals("enable"))
1944 break;
luxiaolb40014b2013-07-19 10:01:43 +08001945 showExitDialog(mActivity);
1946 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001947 case R.id.homepage_menu_id:
1948 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001949 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001950 break;
1951
1952 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001953 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001954 break;
1955
1956 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001957 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001958 break;
1959
John Reck2bc80422011-06-30 15:11:49 -07001960 case R.id.save_snapshot_menu_id:
1961 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001962 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001963 createScreenshotAsync(
1964 source.getWebView(),
1965 getDesiredThumbnailWidth(mActivity),
1966 getDesiredThumbnailHeight(mActivity),
1967 new ValueCallback<Bitmap>() {
1968 @Override
1969 public void onReceiveValue(Bitmap bitmap) {
1970 new SaveSnapshotTask(source, bitmap).execute();
1971 }
1972 });
Leon Scrogginsac993842011-02-02 12:54:07 -05001973 break;
1974
Michael Kolb8233fac2010-10-26 16:08:53 -07001975 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001976 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001977 break;
1978
John Recke1a03a32011-09-14 17:04:16 -07001979 case R.id.snapshot_go_live:
1980 goLive();
1981 return true;
1982
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 case R.id.share_page_menu_id:
1984 Tab currentTab = mTabControl.getCurrentTab();
1985 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001986 return false;
1987 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001988 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001989 break;
1990
1991 case R.id.dump_nav_menu_id:
1992 getCurrentTopWebView().debugDump();
1993 break;
1994
Michael Kolb8233fac2010-10-26 16:08:53 -07001995 case R.id.zoom_in_menu_id:
1996 getCurrentTopWebView().zoomIn();
1997 break;
1998
1999 case R.id.zoom_out_menu_id:
2000 getCurrentTopWebView().zoomOut();
2001 break;
2002
2003 case R.id.view_downloads_menu_id:
2004 viewDownloads();
2005 break;
2006
John Reck42229bc2011-08-19 13:26:43 -07002007 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002008 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002009 break;
2010
Michael Kolb8233fac2010-10-26 16:08:53 -07002011 case R.id.window_one_menu_id:
2012 case R.id.window_two_menu_id:
2013 case R.id.window_three_menu_id:
2014 case R.id.window_four_menu_id:
2015 case R.id.window_five_menu_id:
2016 case R.id.window_six_menu_id:
2017 case R.id.window_seven_menu_id:
2018 case R.id.window_eight_menu_id:
2019 {
2020 int menuid = item.getItemId();
2021 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2022 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2023 Tab desiredTab = mTabControl.getTab(id);
2024 if (desiredTab != null &&
2025 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002026 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002027 }
2028 break;
2029 }
2030 }
2031 }
2032 break;
2033
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002034 case R.id.about_menu_id:
2035 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2036 builder.setTitle(R.string.about);
2037 builder.setCancelable(true);
2038 String ua = "";
2039 final WebView currentWebView = getCurrentWebView();
2040 if (currentWebView != null) {
2041 final WebSettings s = currentWebView.getSettings();
2042 if (s != null) {
2043 ua = s.getUserAgentString();
2044 }
2045 }
2046 builder.setMessage("Agent:" + ua);
2047 builder.setPositiveButton(android.R.string.ok, null);
2048 builder.create().show();
2049 break;
2050
Michael Kolb8233fac2010-10-26 16:08:53 -07002051 default:
2052 return false;
2053 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002054 return true;
2055 }
2056
John Reck68234a92012-04-19 15:27:12 -07002057 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2058 implements OnCancelListener {
2059
2060 private Tab mTab;
2061 private Dialog mProgressDialog;
2062 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002063 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002064
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002065 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002066 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002067 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002068 }
2069
2070 @Override
2071 protected void onPreExecute() {
2072 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2073 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2074 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002075 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002076 }
2077
2078 @Override
2079 protected Long doInBackground(Void... params) {
2080 if (!mTab.saveViewState(mValues)) {
2081 return null;
2082 }
2083 if (isCancelled()) {
2084 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2085 File file = mActivity.getFileStreamPath(path);
2086 if (!file.delete()) {
2087 file.deleteOnExit();
2088 }
2089 return null;
2090 }
2091 final ContentResolver cr = mActivity.getContentResolver();
2092 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2093 if (result == null) {
2094 return null;
2095 }
2096 long id = ContentUris.parseId(result);
2097 return id;
2098 }
2099
2100 @Override
2101 protected void onPostExecute(Long id) {
2102 if (isCancelled()) {
2103 return;
2104 }
2105 mProgressDialog.dismiss();
2106 if (id == null) {
2107 Toast.makeText(mActivity, R.string.snapshot_failed,
2108 Toast.LENGTH_SHORT).show();
2109 return;
2110 }
2111 Bundle b = new Bundle();
2112 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2113 mUi.showComboView(ComboViews.Snapshots, b);
2114 }
2115
2116 @Override
2117 public void onCancel(DialogInterface dialog) {
2118 cancel(true);
2119 }
2120 }
2121
Michael Kolb80f75082012-04-10 10:50:06 -07002122 @Override
2123 public void toggleUserAgent() {
2124 WebView web = getCurrentWebView();
2125 mSettings.toggleDesktopUseragent(web);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002126 web.reload();
Michael Kolb80f75082012-04-10 10:50:06 -07002127 }
2128
2129 @Override
2130 public void findOnPage() {
2131 getCurrentTopWebView().showFindDialog(null, true);
2132 }
2133
2134 @Override
2135 public void openPreferences() {
2136 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2137 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2138 getCurrentTopWebView().getUrl());
2139 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2140 }
2141
2142 @Override
2143 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002144 WebView w = getCurrentTopWebView();
2145 if (w == null)
2146 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002147 final Intent i = createBookmarkCurrentPageIntent(false);
2148 createScreenshotAsync(
2149 w, getDesiredThumbnailWidth(mActivity),
2150 getDesiredThumbnailHeight(mActivity),
2151 new ValueCallback<Bitmap>() {
2152 @Override
2153 public void onReceiveValue(Bitmap bitmap) {
2154 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2155 mActivity.startActivity(i);
2156 }
2157 });
Michael Kolb80f75082012-04-10 10:50:06 -07002158 }
2159
John Recke1a03a32011-09-14 17:04:16 -07002160 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002161 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002162 String url = t.getLiveUrl();
2163 // destroy the old snapshot tab
2164 closeCurrentTab();
2165 Tab liveTab = createNewTab(false, true, false);
2166 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002167 }
2168
luxiaolb40014b2013-07-19 10:01:43 +08002169 private void showExitDialog(final Activity activity) {
2170 new AlertDialog.Builder(activity)
2171 .setTitle(R.string.exit_browser_title)
2172 .setIcon(android.R.drawable.ic_dialog_alert)
2173 .setMessage(R.string.exit_browser_msg)
2174 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2175 public void onClick(DialogInterface dialog, int which) {
2176 activity.moveTaskToBack(true);
2177 dialog.dismiss();
2178 }
2179 })
2180 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2181 public void onClick(DialogInterface dialog, int which) {
2182 activity.finish();
2183 mHandler.postDelayed(new Runnable() {
2184 @Override
2185 public void run() {
2186 // TODO Auto-generated method stub
2187 mCrashRecoveryHandler.clearState(true);
2188 int pid = android.os.Process.myPid();
2189 android.os.Process.killProcess(pid);
2190 }
2191 }, 300);
2192 dialog.dismiss();
2193 }
2194 })
2195 .show();
2196 }
Michael Kolb315d5022011-10-13 12:47:11 -07002197 @Override
2198 public void showPageInfo() {
2199 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2200 }
2201
John Reck9c35b9c2012-05-30 10:08:50 -07002202 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002203 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002204 // Let the History and Bookmark fragments handle menus they created.
2205 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2206 return false;
2207 }
2208
Michael Kolb8233fac2010-10-26 16:08:53 -07002209 int id = item.getItemId();
2210 boolean result = true;
2211 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002212 // -- Browser context menu
2213 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002214 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002215 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002216 case R.id.copy_link_context_menu_id:
2217 final WebView webView = getCurrentTopWebView();
2218 if (null == webView) {
2219 result = false;
2220 break;
2221 }
2222 final HashMap<String, WebView> hrefMap =
2223 new HashMap<String, WebView>();
2224 hrefMap.put("webview", webView);
2225 final Message msg = mHandler.obtainMessage(
2226 FOCUS_NODE_HREF, id, 0, hrefMap);
2227 webView.requestFocusNodeHref(msg);
2228 break;
2229
2230 default:
2231 // For other context menus
2232 result = onOptionsItemSelected(item);
2233 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002234 return result;
2235 }
2236
2237 /**
2238 * support programmatically opening the context menu
2239 */
2240 public void openContextMenu(View view) {
2241 mActivity.openContextMenu(view);
2242 }
2243
2244 /**
2245 * programmatically open the options menu
2246 */
2247 public void openOptionsMenu() {
2248 mActivity.openOptionsMenu();
2249 }
2250
John Reck9c35b9c2012-05-30 10:08:50 -07002251 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002252 public boolean onMenuOpened(int featureId, Menu menu) {
2253 if (mOptionsMenuOpen) {
2254 if (mConfigChanged) {
2255 // We do not need to make any changes to the state of the
2256 // title bar, since the only thing that happened was a
2257 // change in orientation
2258 mConfigChanged = false;
2259 } else {
2260 if (!mExtendedMenuOpen) {
2261 mExtendedMenuOpen = true;
2262 mUi.onExtendedMenuOpened();
2263 } else {
2264 // Switching the menu back to icon view, so show the
2265 // title bar once again.
2266 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002267 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002268 }
2269 }
2270 } else {
2271 // The options menu is closed, so open it, and show the title
2272 mOptionsMenuOpen = true;
2273 mConfigChanged = false;
2274 mExtendedMenuOpen = false;
2275 mUi.onOptionsMenuOpened();
2276 }
2277 return true;
2278 }
2279
John Reck9c35b9c2012-05-30 10:08:50 -07002280 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002281 public void onOptionsMenuClosed(Menu menu) {
2282 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002283 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002284 }
2285
John Reck9c35b9c2012-05-30 10:08:50 -07002286 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002287 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002288 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002289 }
2290
2291 // Helper method for getting the top window.
2292 @Override
2293 public WebView getCurrentTopWebView() {
2294 return mTabControl.getCurrentTopWebView();
2295 }
2296
2297 @Override
2298 public WebView getCurrentWebView() {
2299 return mTabControl.getCurrentWebView();
2300 }
2301
2302 /*
2303 * This method is called as a result of the user selecting the options
2304 * menu to see the download window. It shows the download window on top of
2305 * the current window.
2306 */
2307 void viewDownloads() {
2308 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2309 mActivity.startActivity(intent);
2310 }
2311
John Reck30b065e2011-07-19 10:58:05 -07002312 int getActionModeHeight() {
2313 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2314 new int[] { android.R.attr.actionBarSize });
2315 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2316 actionBarSizeTypedArray.recycle();
2317 return size;
2318 }
2319
Michael Kolb8233fac2010-10-26 16:08:53 -07002320 // action mode
2321
John Reck9c35b9c2012-05-30 10:08:50 -07002322 @Override
2323 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002324 mUi.onActionModeStarted(mode);
2325 mActionMode = mode;
2326 }
2327
2328 /*
2329 * True if a custom ActionMode (i.e. find or select) is in use.
2330 */
2331 @Override
2332 public boolean isInCustomActionMode() {
2333 return mActionMode != null;
2334 }
2335
2336 /*
2337 * End the current ActionMode.
2338 */
2339 @Override
2340 public void endActionMode() {
2341 if (mActionMode != null) {
2342 mActionMode.finish();
2343 }
2344 }
2345
2346 /*
2347 * Called by find and select when they are finished. Replace title bars
2348 * as necessary.
2349 */
John Reck9c35b9c2012-05-30 10:08:50 -07002350 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002351 public void onActionModeFinished(ActionMode mode) {
2352 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002353 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002354 mActionMode = null;
2355 }
2356
2357 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002358 final Tab tab = getCurrentTab();
2359 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002360 }
2361
2362 // bookmark handling
2363
2364 /**
2365 * add the current page as a bookmark to the given folder id
2366 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002367 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002368 * bookmarked, and if it is, edit that bookmark. If false, and
2369 * the site is already bookmarked, do not attempt to edit the
2370 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002371 */
2372 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002373 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002374 WebView w = getCurrentTopWebView();
2375 if (w == null) {
2376 return null;
2377 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002378 Intent i = new Intent(mActivity,
2379 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002380 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2381 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2382 String touchIconUrl = w.getTouchIconUrl();
2383 if (touchIconUrl != null) {
2384 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2385 WebSettings settings = w.getSettings();
2386 if (settings != null) {
2387 i.putExtra(AddBookmarkPage.USER_AGENT,
2388 settings.getUserAgentString());
2389 }
2390 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002391 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002392 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002393 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002394 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2395 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002396 // Put the dialog at the upper right of the screen, covering the
2397 // star on the title bar.
2398 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002399 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002400 }
2401
2402 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002403 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002404 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002405 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002406 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002407 }
2408
Vivek Sekharb54614f2014-05-01 19:03:37 -07002409 @Override
2410 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2411 boolean capture) {
2412 mUploadHandler = new UploadHandler(this);
2413 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2414 }
2415
Michael Kolb8233fac2010-10-26 16:08:53 -07002416 // thumbnails
2417
2418 /**
2419 * Return the desired width for thumbnail screenshots, which are stored in
2420 * the database, and used on the bookmarks screen.
2421 * @param context Context for finding out the density of the screen.
2422 * @return desired width for thumbnail screenshot.
2423 */
2424 static int getDesiredThumbnailWidth(Context context) {
2425 return context.getResources().getDimensionPixelOffset(
2426 R.dimen.bookmarkThumbnailWidth);
2427 }
2428
2429 /**
2430 * Return the desired height for thumbnail screenshots, which are stored in
2431 * the database, and used on the bookmarks screen.
2432 * @param context Context for finding out the density of the screen.
2433 * @return desired height for thumbnail screenshot.
2434 */
2435 static int getDesiredThumbnailHeight(Context context) {
2436 return context.getResources().getDimensionPixelOffset(
2437 R.dimen.bookmarkThumbnailHeight);
2438 }
2439
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002440 static void createScreenshotAsync(WebView view, int width, int height,
2441 final ValueCallback<Bitmap> cb) {
2442 if (view == null || width == 0 || height == 0) {
2443 return;
2444 }
2445
2446 view.getContentBitmapAsync(
2447 (float) width / view.getWidth(),
2448 new Rect(),
2449 new ValueCallback<Bitmap>() {
2450 @Override
2451 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002452 if (bitmap != null)
2453 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2454 cb.onReceiveValue(bitmap);
2455 }});
2456 }
2457
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002458 private void updateScreenshot(final Tab tab) {
2459 createScreenshotAsync(
2460 tab.getWebView(),
2461 getDesiredThumbnailWidth(mActivity),
2462 getDesiredThumbnailHeight(mActivity),
2463 new ValueCallback<Bitmap>() {
2464 @Override
2465 public void onReceiveValue(Bitmap bitmap) {
2466 updateScreenshot(tab, bitmap);
2467 }
2468 });
2469 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002470
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002471 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002472 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002473 // FIXME: Would like to make sure there is actually something to
2474 // draw, but the API for that (WebViewCore.pictureReady()) is not
2475 // currently accessible here.
2476
John Reck34ef2672011-02-10 11:30:55 -08002477 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002478 if (view == null) {
2479 // Tab was destroyed
2480 return;
2481 }
John Reck34ef2672011-02-10 11:30:55 -08002482 final String url = tab.getUrl();
2483 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002484 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002485 if (TextUtils.isEmpty(url)) {
2486 return;
2487 }
2488
kaiyiz6e5b3e02013-08-19 20:02:01 +08002489 //update My Navigation Thumbnails
2490 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2491 if (isMyNavigationUrl) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002492 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002493 }
John Reck34ef2672011-02-10 11:30:55 -08002494 // Only update thumbnails for web urls (http(s)://), not for
2495 // about:, javascript:, data:, etc...
2496 // Unless it is a bookmarked site, then always update
2497 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2498 return;
2499 }
2500
kaiyiz6e5b3e02013-08-19 20:02:01 +08002501 if (url != null && mUpdateMyNavThumbnailUrl != null
2502 && Patterns.WEB_URL.matcher(url).matches()
2503 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2504 String urlHost = (new WebAddress(url)).getHost();
2505 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2506 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2507 || bookmarkHost.length() == 0) {
2508 return;
2509 }
2510 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2511 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2512 bookmarkHost.length());
2513 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2514 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2515 if (!bookmarkDomain.equals(urlDomain)) {
2516 return;
2517 }
2518 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002519 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002520 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2521 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2522 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2523 500);
2524 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 return;
2526 }
2527
2528 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002529 new AsyncTask<Void, Void, Void>() {
2530 @Override
2531 protected Void doInBackground(Void... unused) {
2532 Cursor cursor = null;
2533 try {
2534 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002535 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2536 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2537 : url);
2538 if (mUpdateMyNavThumbnail) {
2539 mUpdateMyNavThumbnail = false;
2540 mUpdateMyNavThumbnailUrl = null;
2541 }
John Reck34ef2672011-02-10 11:30:55 -08002542 if (cursor != null && cursor.moveToFirst()) {
2543 final ByteArrayOutputStream os =
2544 new ByteArrayOutputStream();
2545 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002546
John Reck34ef2672011-02-10 11:30:55 -08002547 ContentValues values = new ContentValues();
2548 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002549
John Reck34ef2672011-02-10 11:30:55 -08002550 do {
John Reck617fd832011-02-16 14:35:59 -08002551 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002552 cr.update(Images.CONTENT_URI, values, null, null);
2553 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002554 }
John Reck34ef2672011-02-10 11:30:55 -08002555 } catch (IllegalStateException e) {
2556 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002557 } catch (SQLiteException s) {
2558 // Added for possible error when user tries to remove the same bookmark
2559 // that is being updated with a screen shot
2560 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002561 } finally {
2562 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002563 }
John Reck34ef2672011-02-10 11:30:55 -08002564 return null;
2565 }
2566 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002567 }
2568
2569 private class Copy implements OnMenuItemClickListener {
2570 private CharSequence mText;
2571
John Reck9c35b9c2012-05-30 10:08:50 -07002572 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002573 public boolean onMenuItemClick(MenuItem item) {
2574 copy(mText);
2575 return true;
2576 }
2577
2578 public Copy(CharSequence toCopy) {
2579 mText = toCopy;
2580 }
2581 }
2582
Leon Scroggins63c02662010-11-18 15:16:27 -05002583 private static class Download implements OnMenuItemClickListener {
2584 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002585 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002586 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002587 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002588 private static final String FALLBACK_EXTENSION = "dat";
2589 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002590
John Reck9c35b9c2012-05-30 10:08:50 -07002591 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002592 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002593 if (DataUri.isDataUri(mText)) {
2594 saveDataUri();
2595 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002596 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002597 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002598 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 return true;
2600 }
2601
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002602 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2603 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002604 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002606 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002607 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 }
George Mount387d45d2011-10-07 15:57:53 -07002609
2610 /**
2611 * Treats mText as a data URI and writes its contents to a file
2612 * based on the current time.
2613 */
2614 private void saveDataUri() {
2615 FileOutputStream outputStream = null;
2616 try {
2617 DataUri uri = new DataUri(mText);
2618 File target = getTarget(uri);
2619 outputStream = new FileOutputStream(target);
2620 outputStream.write(uri.getData());
2621 final DownloadManager manager =
2622 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2623 manager.addCompletedDownload(target.getName(),
2624 mActivity.getTitle().toString(), false,
2625 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002626 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002627 } catch (IOException e) {
2628 Log.e(LOGTAG, "Could not save data URL");
2629 } finally {
2630 if (outputStream != null) {
2631 try {
2632 outputStream.close();
2633 } catch (IOException e) {
2634 // ignore close errors
2635 }
2636 }
2637 }
2638 }
2639
2640 /**
2641 * Creates a File based on the current time stamp and uses
2642 * the mime type of the DataUri to get the extension.
2643 */
2644 private File getTarget(DataUri uri) throws IOException {
2645 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002646 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002647 String nameBase = format.format(new Date());
2648 String mimeType = uri.getMimeType();
2649 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2650 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2651 if (extension == null) {
2652 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2653 extension = FALLBACK_EXTENSION;
2654 }
2655 extension = "." + extension; // createTempFile needs the '.'
2656 File targetFile = File.createTempFile(nameBase, extension, dir);
2657 return targetFile;
2658 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002659 }
2660
Cary Clark8974d282010-11-22 10:46:05 -05002661 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002662 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002663
John Reck9c35b9c2012-05-30 10:08:50 -07002664 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002665 public boolean onMenuItemClick(MenuItem item) {
2666 if (mWebView != null) {
2667 return mWebView.selectText();
2668 }
2669 return false;
2670 }
2671
2672 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002673 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002674 }
2675
2676 }
2677
Michael Kolb8233fac2010-10-26 16:08:53 -07002678 /********************** TODO: UI stuff *****************************/
2679
2680 // these methods have been copied, they still need to be cleaned up
2681
2682 /****************** tabs ***************************************************/
2683
2684 // basic tab interactions:
2685
2686 // it is assumed that tabcontrol already knows about the tab
2687 protected void addTab(Tab tab) {
2688 mUi.addTab(tab);
2689 }
2690
2691 protected void removeTab(Tab tab) {
2692 mUi.removeTab(tab);
2693 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002694 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002695 }
2696
Michael Kolbf2055602011-04-09 17:20:03 -07002697 @Override
2698 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002699 // monkey protection against delayed start
2700 if (tab != null) {
2701 mTabControl.setCurrentTab(tab);
2702 // the tab is guaranteed to have a webview after setCurrentTab
2703 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002704 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002705 //Purge active tabs
2706 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002707 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002708 }
2709
John Reck8bcafc12011-08-29 16:43:02 -07002710 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002711 Tab current = mTabControl.getCurrentTab();
2712 if (current != null
2713 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002714 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002715 }
2716 }
2717
John Reck26b18322011-06-21 13:08:58 -07002718 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002719 // Dismiss the subwindow if applicable.
2720 dismissSubWindow(appTab);
2721 // Since we might kill the WebView, remove it from the
2722 // content view first.
2723 mUi.detachTab(appTab);
2724 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002725 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002726 // TODO: analyze why the remove and add are necessary
2727 mUi.attachTab(appTab);
2728 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002729 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002730 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002731 } else {
2732 // If the tab was the current tab, we have to attach
2733 // it to the view system again.
2734 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002735 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002736 }
2737 }
2738
2739 // Remove the sub window if it exists. Also called by TabControl when the
2740 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002741 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002742 public void dismissSubWindow(Tab tab) {
2743 removeSubWindow(tab);
2744 // dismiss the subwindow. This will destroy the WebView.
2745 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002746 WebView wv = getCurrentTopWebView();
2747 if (wv != null) {
2748 wv.requestFocus();
2749 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002750 }
2751
2752 @Override
2753 public void removeSubWindow(Tab t) {
2754 if (t.getSubWebView() != null) {
2755 mUi.removeSubWindow(t.getSubViewContainer());
2756 }
2757 }
2758
2759 @Override
2760 public void attachSubWindow(Tab tab) {
2761 if (tab.getSubWebView() != null) {
2762 mUi.attachSubWindow(tab.getSubViewContainer());
2763 getCurrentTopWebView().requestFocus();
2764 }
2765 }
2766
Mathew Inwood29721c22011-06-29 17:55:24 +01002767 private Tab showPreloadedTab(final UrlData urlData) {
2768 if (!urlData.isPreloaded()) {
2769 return null;
2770 }
2771 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2772 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2773 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002774 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002775 // Could not submit query. Fallback to regular tab creation
2776 tabControl.destroy();
2777 return null;
2778 }
2779 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002780 // check tab count and make room for new tab
2781 if (!mTabControl.canCreateNewTab()) {
2782 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2783 if (leastUsed != null) {
2784 closeTab(leastUsed);
2785 }
2786 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002787 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002788 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002789 mTabControl.addPreloadedTab(t);
2790 addTab(t);
2791 setActiveTab(t);
2792 return t;
2793 }
2794
Michael Kolb7bcafde2011-05-09 13:55:59 -07002795 // open a non inconito tab with the given url data
2796 // and set as active tab
2797 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002798 Tab tab = showPreloadedTab(urlData);
2799 if (tab == null) {
2800 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002801 if ((tab != null) && !urlData.isEmpty()) {
2802 loadUrlDataIn(tab, urlData);
2803 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002804 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002805 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002806 }
2807
Michael Kolb843510f2010-12-09 10:51:49 -08002808 @Override
2809 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002810 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002811 }
2812
Michael Kolb8233fac2010-10-26 16:08:53 -07002813 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002814 public Tab openIncognitoTab() {
2815 return openTab(INCOGNITO_URI, true, true, false);
2816 }
2817
2818 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002819 public Tab openTab(String url, boolean incognito, boolean setActive,
2820 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002821 return openTab(url, incognito, setActive, useCurrent, null);
2822 }
2823
2824 @Override
2825 public Tab openTab(String url, Tab parent, boolean setActive,
2826 boolean useCurrent) {
2827 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2828 setActive, useCurrent, parent);
2829 }
2830
2831 public Tab openTab(String url, boolean incognito, boolean setActive,
2832 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002833 Tab tab = createNewTab(incognito, setActive, useCurrent);
2834 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002835 if (parent instanceof SnapshotTab) {
2836 addTab(tab);
2837 if (setActive)
2838 setActiveTab(tab);
2839 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002840 parent.addChildTab(tab);
2841 }
Michael Kolb519d2282011-05-09 17:03:19 -07002842 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002843 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002844 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002845 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002846 return tab;
2847 }
2848
2849 // this method will attempt to create a new tab
2850 // incognito: private browsing tab
2851 // setActive: ste tab as current tab
2852 // useCurrent: if no new tab can be created, return current tab
2853 private Tab createNewTab(boolean incognito, boolean setActive,
2854 boolean useCurrent) {
2855 Tab tab = null;
2856 if (mTabControl.canCreateNewTab()) {
2857 tab = mTabControl.createNewTab(incognito);
2858 addTab(tab);
2859 if (setActive) {
2860 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002861 } else {
2862 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002863 }
2864 } else {
2865 if (useCurrent) {
2866 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002867 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002868 } else {
2869 mUi.showMaxTabsWarning();
2870 }
2871 }
2872 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002873 }
2874
John Reck2bc80422011-06-30 15:11:49 -07002875 @Override
2876 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2877 SnapshotTab tab = null;
2878 if (mTabControl.canCreateNewTab()) {
2879 tab = mTabControl.createSnapshotTab(snapshotId);
2880 addTab(tab);
2881 if (setActive) {
2882 setActiveTab(tab);
2883 }
2884 } else {
2885 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002886 }
2887 return tab;
2888 }
2889
Michael Kolb8233fac2010-10-26 16:08:53 -07002890 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002891 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002892 * @return boolean True if we successfully switched to a different tab. If
2893 * the indexth tab is null, or if that tab is the same as
2894 * the current one, return false.
2895 */
2896 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002897 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002898 Tab currentTab = mTabControl.getCurrentTab();
2899 if (tab == null || tab == currentTab) {
2900 return false;
2901 }
2902 setActiveTab(tab);
2903 return true;
2904 }
2905
2906 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002907 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002908 closeCurrentTab(false);
2909 }
2910
2911 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002912 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002913 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002914 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002915 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002916 return;
2917 }
Michael Kolbc831b632011-05-11 09:30:34 -07002918 final Tab current = mTabControl.getCurrentTab();
2919 final int pos = mTabControl.getCurrentPosition();
2920 Tab newTab = current.getParent();
2921 if (newTab == null) {
2922 newTab = mTabControl.getTab(pos + 1);
2923 if (newTab == null) {
2924 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002925 }
2926 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002927 if (andQuit) {
2928 mTabControl.setCurrentTab(newTab);
2929 closeTab(current);
2930 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002931 // Close window
2932 closeTab(current);
2933 }
2934 }
2935
2936 /**
2937 * Close the tab, remove its associated title bar, and adjust mTabControl's
2938 * current tab to a valid value.
2939 */
2940 @Override
2941 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002942 if (tab == mTabControl.getCurrentTab()) {
2943 closeCurrentTab();
2944 } else {
2945 removeTab(tab);
2946 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002947 }
2948
Afzal Najamd4e33312012-04-26 01:54:01 -04002949 /**
2950 * Close all tabs except the current one
2951 */
2952 @Override
2953 public void closeOtherTabs() {
2954 int inactiveTabs = mTabControl.getTabCount() - 1;
2955 for (int i = inactiveTabs; i >= 0; i--) {
2956 Tab tab = mTabControl.getTab(i);
2957 if (tab != mTabControl.getCurrentTab()) {
2958 removeTab(tab);
2959 }
2960 }
2961 }
2962
Michael Kolb8233fac2010-10-26 16:08:53 -07002963 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002964 protected void loadUrlFromContext(String url) {
2965 Tab tab = getCurrentTab();
2966 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002967 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002968 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002969 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002970 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002971 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002972 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002973 }
2974 }
2975 }
2976
2977 /**
2978 * Load the URL into the given WebView and update the title bar
2979 * to reflect the new load. Call this instead of WebView.loadUrl
2980 * directly.
2981 * @param view The WebView used to load url.
2982 * @param url The URL to load.
2983 */
John Reck71e51422011-07-01 16:49:28 -07002984 @Override
2985 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002986 loadUrl(tab, url, null);
2987 }
2988
2989 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2990 if (tab != null) {
2991 dismissSubWindow(tab);
2992 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002993 if (tab.hasCrashed) {
2994 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
2995 }
Michael Kolba53c9892011-10-05 13:31:40 -07002996 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002997 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 }
2999
3000 /**
3001 * Load UrlData into a Tab and update the title bar to reflect the new
3002 * load. Call this instead of UrlData.loadIn directly.
3003 * @param t The Tab used to load.
3004 * @param data The UrlData being loaded.
3005 */
3006 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003007 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003008 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003009 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003010 } else {
John Reck38b39652012-06-05 09:22:59 -07003011 if (t != null && data.mDisableUrlOverride) {
3012 t.disableUrlOverridingForLoad();
3013 }
John Reck26b18322011-06-21 13:08:58 -07003014 loadUrl(t, data.mUrl, data.mHeaders);
3015 }
3016 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003017 }
3018
John Reck30c714c2010-12-16 17:30:34 -08003019 @Override
3020 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003021 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003022 if (tab.canGoBack()) {
3023 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003024 } else {
John Reckef654f12011-07-12 16:42:08 -07003025 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003026 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003027 }
3028
3029 void goBackOnePageOrQuit() {
3030 Tab current = mTabControl.getCurrentTab();
3031 if (current == null) {
3032 /*
3033 * Instead of finishing the activity, simply push this to the back
3034 * of the stack and let ActivityManager to choose the foreground
3035 * activity. As BrowserActivity is singleTask, it will be always the
3036 * root of the task. So we can use either true or false for
3037 * moveTaskToBack().
3038 */
luxiaolb40014b2013-07-19 10:01:43 +08003039 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003040 return;
3041 }
John Reckef654f12011-07-12 16:42:08 -07003042 if (current.canGoBack()) {
3043 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003044 } else {
3045 // Check to see if we are closing a window that was created by
3046 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003047 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003048 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003049 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003050 // Now we close the other tab
3051 closeTab(current);
3052 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003053 /*
3054 * Instead of finishing the activity, simply push this to the back
3055 * of the stack and let ActivityManager to choose the foreground
3056 * activity. As BrowserActivity is singleTask, it will be always the
3057 * root of the task. So we can use either true or false for
3058 * moveTaskToBack().
3059 */
luxiaolb40014b2013-07-19 10:01:43 +08003060 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003061 }
3062 }
3063 }
3064
3065 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003066 * helper method for key handler
3067 * returns the current tab if it can't advance
3068 */
Michael Kolbc831b632011-05-11 09:30:34 -07003069 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003070 int pos = mTabControl.getCurrentPosition() + 1;
3071 if (pos >= mTabControl.getTabCount()) {
3072 pos = 0;
3073 }
3074 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003075 }
3076
3077 /**
3078 * helper method for key handler
3079 * returns the current tab if it can't advance
3080 */
Michael Kolbc831b632011-05-11 09:30:34 -07003081 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003082 int pos = mTabControl.getCurrentPosition() - 1;
3083 if ( pos < 0) {
3084 pos = mTabControl.getTabCount() - 1;
3085 }
3086 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003087 }
3088
John Reckbcef87f2012-02-03 14:58:34 -08003089 boolean isMenuOrCtrlKey(int keyCode) {
3090 return (KeyEvent.KEYCODE_MENU == keyCode)
3091 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3092 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3093 }
3094
Michael Kolb0035fad2011-03-14 13:25:28 -07003095 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003096 * handle key events in browser
3097 *
3098 * @param keyCode
3099 * @param event
3100 * @return true if handled, false to pass to super
3101 */
John Reck9c35b9c2012-05-30 10:08:50 -07003102 @Override
3103 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003104 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003105 // Even if MENU is already held down, we need to call to super to open
3106 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003107 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003108 mMenuIsDown = true;
3109 return false;
3110 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003111
Cary Clark8ff8c662010-12-29 15:03:05 -05003112 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003113 Tab tab = getCurrentTab();
3114 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003115
Cary Clark160bbb92011-01-10 11:17:07 -05003116 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3117 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003118
Michael Kolb8233fac2010-10-26 16:08:53 -07003119 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003120 case KeyEvent.KEYCODE_TAB:
3121 if (event.isCtrlPressed()) {
3122 if (event.isShiftPressed()) {
3123 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003124 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003125 } else {
3126 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003127 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003128 }
3129 return true;
3130 }
3131 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003132 case KeyEvent.KEYCODE_SPACE:
3133 // WebView/WebTextView handle the keys in the KeyDown. As
3134 // the Activity's shortcut keys are only handled when WebView
3135 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003136 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003137 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003138 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003139 pageDown();
3140 }
3141 return true;
3142 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003143 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003144 event.startTracking();
3145 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003146 case KeyEvent.KEYCODE_FORWARD:
3147 if (!noModifiers) break;
3148 tab.goForward();
3149 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003150 case KeyEvent.KEYCODE_DPAD_LEFT:
3151 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003152 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003153 return true;
3154 }
3155 break;
3156 case KeyEvent.KEYCODE_DPAD_RIGHT:
3157 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003158 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003159 return true;
3160 }
3161 break;
3162 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003163 if (ctrl) {
3164 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003165 return true;
3166 }
3167 break;
Michael Kolba4183062011-01-16 10:43:21 -08003168// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003169 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003170 if (ctrl ) {
3171 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003172 return true;
3173 }
3174 break;
Michael Kolba4183062011-01-16 10:43:21 -08003175// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003176// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003177// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003178// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003179// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003180// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003181// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003182// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003183// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003184// case KeyEvent.KEYCODE_M: // unused
3185// case KeyEvent.KEYCODE_N: // in Chrome: new window
3186// case KeyEvent.KEYCODE_O: // in Chrome: open file
3187// case KeyEvent.KEYCODE_P: // in Chrome: print page
3188// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003189// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003190// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3191 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003192 // we can't use the ctrl/shift flags, they check for
3193 // exclusive use of a modifier
3194 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003195 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003196 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003197 } else {
3198 openTabToHomePage();
3199 }
3200 return true;
3201 }
3202 break;
3203// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3204// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003205// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003206// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3207// case KeyEvent.KEYCODE_Y: // unused
3208// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003209 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003210 // it is a regular key and webview is not null
3211 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003212 }
3213
John Reck9c35b9c2012-05-30 10:08:50 -07003214 @Override
3215 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003216 switch(keyCode) {
3217 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003218 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003219 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003220 return true;
3221 }
3222 break;
3223 }
3224 return false;
3225 }
3226
John Reck9c35b9c2012-05-30 10:08:50 -07003227 @Override
3228 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003229 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003230 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003231 if (KeyEvent.KEYCODE_MENU == keyCode
3232 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003233 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003234 }
3235 }
Cary Clark160bbb92011-01-10 11:17:07 -05003236 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003237 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003238 case KeyEvent.KEYCODE_BACK:
3239 if (event.isTracking() && !event.isCanceled()) {
3240 onBackKey();
3241 return true;
3242 }
3243 break;
3244 }
3245 return false;
3246 }
3247
3248 public boolean isMenuDown() {
3249 return mMenuIsDown;
3250 }
3251
John Reck9c35b9c2012-05-30 10:08:50 -07003252 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003253 public void setupAutoFill(Message message) {
3254 // Open the settings activity at the AutoFill profile fragment so that
3255 // the user can create a new profile. When they return, we will dispatch
3256 // the message so that we can autofill the form using their new profile.
3257 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3258 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3259 AutoFillSettingsFragment.class.getName());
3260 mAutoFillSetupMessage = message;
3261 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3262 }
John Reckb3417f02011-01-14 11:01:05 -08003263
John Reck9c35b9c2012-05-30 10:08:50 -07003264 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003265 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003266 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003267 return true;
3268 }
3269
John Reck1cf4b792011-07-26 10:22:22 -07003270 @Override
3271 public boolean shouldCaptureThumbnails() {
3272 return mUi.shouldCaptureThumbnails();
3273 }
3274
Michael Kolbc3af0672011-08-09 10:24:41 -07003275 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003276 public boolean supportsVoice() {
3277 PackageManager pm = mActivity.getPackageManager();
3278 List activities = pm.queryIntentActivities(new Intent(
3279 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3280 return activities.size() != 0;
3281 }
3282
3283 @Override
3284 public void startVoiceRecognizer() {
3285 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003286 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003287 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3288 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3289 mActivity.startActivityForResult(voice, VOICE_RESULT);
3290 }
3291
3292 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003293 public void setBlockEvents(boolean block) {
3294 mBlockEvents = block;
3295 }
3296
John Reck9c35b9c2012-05-30 10:08:50 -07003297 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003298 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003299 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003300 }
3301
John Reck9c35b9c2012-05-30 10:08:50 -07003302 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003303 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003304 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003305 }
3306
John Reck9c35b9c2012-05-30 10:08:50 -07003307 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003308 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003309 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003310 }
3311
John Reck9c35b9c2012-05-30 10:08:50 -07003312 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003313 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003314 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003315 }
3316
John Reck9c35b9c2012-05-30 10:08:50 -07003317 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003318 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003319 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003320 }
3321
Michael Kolb8233fac2010-10-26 16:08:53 -07003322}