blob: 6b56439cea882e41502dba69468310a5cee67eee [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;
Michael Kolb8233fac2010-10-26 16:08:53 -070081import android.webkit.WebIconDatabase;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import org.codeaurora.swe.CookieManager;
85import org.codeaurora.swe.CookieSyncManager;
86import org.codeaurora.swe.HttpAuthHandler;
87import org.codeaurora.swe.SslErrorHandler;
88import org.codeaurora.swe.WebSettings;
89import org.codeaurora.swe.WebView;
90
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070091import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070092import com.android.browser.R;
93import com.android.browser.IntentHandler.UrlData;
94import com.android.browser.UI.ComboViews;
95import com.android.browser.mynavigation.AddMyNavigationPage;
96import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070097import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070098import com.android.browser.platformsupport.BrowserContract;
99import com.android.browser.platformsupport.WebAddress;
100import com.android.browser.platformsupport.BrowserContract.Images;
101import com.android.browser.provider.BrowserProvider2.Thumbnails;
102import com.android.browser.provider.SnapshotProvider.Snapshots;
103import com.android.browser.reflect.ReflectHelper;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700104
Michael Kolb8233fac2010-10-26 16:08:53 -0700105import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700106import java.io.File;
107import java.io.FileOutputStream;
108import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700109import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700110import java.text.DateFormat;
111import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700112import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700113import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700114import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800116import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200117import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700118import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700119
120/**
121 * Controller for browser
122 */
123public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700124 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700125
126 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800127 private static final String SEND_APP_ID_EXTRA =
128 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700129 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800130
Vivek Sekharb54614f2014-05-01 19:03:37 -0700131 // Remind switch to data connection if wifi is unavailable
132 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800133
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 // public message ids
135 public final static int LOAD_URL = 1001;
136 public final static int STOP_LOAD = 1002;
137
138 // Message Ids
139 private static final int FOCUS_NODE_HREF = 102;
140 private static final int RELEASE_WAKELOCK = 107;
141
142 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
143
144 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800145 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700146
147 private static final int EMPTY_MENU = -1;
148
Michael Kolb8233fac2010-10-26 16:08:53 -0700149 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700150 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 final static int PREFERENCES_PAGE = 3;
152 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000153 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700154 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800155 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000156
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
158
159 // As the ids are dynamically created, we can't guarantee that they will
160 // be in sequence, so this static array maps ids to a window number.
161 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
162 { R.id.window_one_menu_id, R.id.window_two_menu_id,
163 R.id.window_three_menu_id, R.id.window_four_menu_id,
164 R.id.window_five_menu_id, R.id.window_six_menu_id,
165 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
166
167 // "source" parameter for Google search through search key
168 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
169 // "source" parameter for Google search through simplily type
170 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
171
George Mount387d45d2011-10-07 15:57:53 -0700172 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700173 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
174
John Reckd7dd9b22011-08-30 09:18:29 -0700175 // A bitmap that is re-used in createScreenshot as scratch space
176 private static Bitmap sThumbnailBitmap;
177
Michael Kolb8233fac2010-10-26 16:08:53 -0700178 private Activity mActivity;
179 private UI mUi;
180 private TabControl mTabControl;
181 private BrowserSettings mSettings;
182 private WebViewFactory mFactory;
183
184 private WakeLock mWakeLock;
185
186 private UrlHandler mUrlHandler;
187 private UploadHandler mUploadHandler;
188 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private PageDialogsHandler mPageDialogsHandler;
190 private NetworkStateHandler mNetworkHandler;
191
Ben Murdoch8029a772010-11-16 11:58:21 +0000192 private Message mAutoFillSetupMessage;
193
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800195 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700196
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 // FIXME, temp address onPrepareMenu performance problem.
198 // When we move everything out of view, we should rewrite this.
199 private int mCurrentMenuState = 0;
200 private int mMenuState = R.id.MAIN_MENU;
201 private int mOldMenuState = EMPTY_MENU;
202 private Menu mCachedMenu;
203
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 private boolean mMenuIsDown;
205
206 // For select and find, we keep track of the ActionMode so that
207 // finish() can be called as desired.
208 private ActionMode mActionMode;
209
210 /**
211 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
212 * of whether the configuration has changed. The first onMenuOpened call
213 * after a configuration change is simply a reopening of the same menu
214 * (i.e. mIconView did not change).
215 */
216 private boolean mConfigChanged;
217
218 /**
219 * Keeps track of whether the options menu is open. This is important in
220 * determining whether to show or hide the title bar overlay
221 */
222 private boolean mOptionsMenuOpen;
223
224 /**
225 * Whether or not the options menu is in its bigger, popup menu form. When
226 * true, we want the title bar overlay to be gone. When false, we do not.
227 * Only meaningful if mOptionsMenuOpen is true.
228 */
229 private boolean mExtendedMenuOpen;
230
Michael Kolb8233fac2010-10-26 16:08:53 -0700231 private boolean mActivityPaused = true;
232 private boolean mLoadStopped;
233
234 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500235 // Checks to see when the bookmarks database has changed, and updates the
236 // Tabs' notion of whether they represent bookmarked sites.
237 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700238 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700239
Michael Kolbc3af0672011-08-09 10:24:41 -0700240 private boolean mBlockEvents;
241
Michael Kolb0b129122012-06-04 16:31:58 -0700242 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800243 private boolean mUpdateMyNavThumbnail;
244 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700245
George Mount3636d0a2011-11-21 09:08:21 -0800246 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700247 mActivity = browser;
248 mSettings = BrowserSettings.getInstance();
249 mTabControl = new TabControl(this);
250 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700251 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800252 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700253 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700254
255 mUrlHandler = new UrlHandler(this);
256 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700257 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
258
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500260 mBookmarksObserver = new ContentObserver(mHandler) {
261 @Override
262 public void onChange(boolean selfChange) {
263 int size = mTabControl.getTabCount();
264 for (int i = 0; i < size; i++) {
265 mTabControl.getTab(i).updateBookmarkedStatus();
266 }
267 }
268
269 };
270 browser.getContentResolver().registerContentObserver(
271 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700272
273 mNetworkHandler = new NetworkStateHandler(mActivity, this);
John Reckaf262e72011-07-25 13:55:44 -0700274 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 }
276
John Reck9c35b9c2012-05-30 10:08:50 -0700277 @Override
278 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800279 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800280 // mCrashRecoverHandler has any previously saved state.
281 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700282 }
283
George Mount3636d0a2011-11-21 09:08:21 -0800284 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700285 // Unless the last browser usage was within 24 hours, destroy any
286 // remaining incognito tabs.
287
288 Calendar lastActiveDate = icicle != null ?
289 (Calendar) icicle.getSerializable("lastActiveDate") : null;
290 Calendar today = Calendar.getInstance();
291 Calendar yesterday = Calendar.getInstance();
292 yesterday.add(Calendar.DATE, -1);
293
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800294 // we dont want to ever recover incognito tabs
295 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700296
Patrick Scott7d50a932011-02-04 09:27:26 -0500297 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700298 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500299 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000300
Michael Kolbc831b632011-05-11 09:30:34 -0700301 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500302 // Not able to restore so we go ahead and clear session cookies. We
303 // must do this before trying to login the user as we don't want to
304 // clear any session cookies set during login.
305 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700306 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800307 if (intent == null) {
308 // This won't happen under common scenarios. The icicle is
309 // not null, but there aren't any tabs to restore.
310 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700311 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800312 final Bundle extra = intent.getExtras();
313 // Create an initial tab.
314 // If the intent is ACTION_VIEW and data is not null, the Browser is
315 // invoked to view the content by another application. In this case,
316 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800317 UrlData urlData = null;
318 if (intent.getData() != null
319 && Intent.ACTION_VIEW.equals(intent.getAction())
320 && intent.getData().toString().startsWith("content://")) {
321 urlData = new UrlData(intent.getData().toString());
322 } else {
323 urlData = IntentHandler.getUrlDataFromIntent(intent);
324 }
George Mount3636d0a2011-11-21 09:08:21 -0800325 Tab t = null;
326 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700327 String landingPage = mActivity.getResources().getString(
328 R.string.def_landing_page);
329 if (!landingPage.isEmpty()) {
330 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800331 } else {
332 t = openTabToHomePage();
333 }
George Mount3636d0a2011-11-21 09:08:21 -0800334 } else {
335 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700336 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800337 }
338 if (t != null) {
339 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
340 }
341 WebView webView = t.getWebView();
342 if (extra != null) {
343 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
344 if (scale > 0 && scale <= 1000) {
345 webView.setInitialScale(scale);
346 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700347 }
348 }
John Reckd8c74522011-06-14 08:45:00 -0700349 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700350 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700351 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500352 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700353 List<Tab> tabs = mTabControl.getTabs();
354 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
355 for (Tab t : tabs) {
356 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700357 if (t != mTabControl.getCurrentTab()) {
358 t.pause();
359 }
John Reck1cf4b792011-07-26 10:22:22 -0700360 }
361 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700362 if (tabs.size() == 0) {
363 openTabToHomePage();
364 }
John Reck1cf4b792011-07-26 10:22:22 -0700365 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 // TabControl.restoreState() will create a new tab even if
367 // restoring the state fails.
368 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800369 // Intent is non-null when framework thinks the browser should be
370 // launching with a new intent (icicle is null).
371 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700372 mIntentHandler.onNewIntent(intent);
373 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700374 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700375 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700376 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800377 if (jsFlags.trim().length() != 0) {
378 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 }
George Mount3636d0a2011-11-21 09:08:21 -0800380 if (intent != null
381 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700382 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800383 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700384 }
385
John Reck1cf4b792011-07-26 10:22:22 -0700386 private static class PruneThumbnails implements Runnable {
387 private Context mContext;
388 private List<Long> mIds;
389
390 PruneThumbnails(Context context, List<Long> preserveIds) {
391 mContext = context.getApplicationContext();
392 mIds = preserveIds;
393 }
394
395 @Override
396 public void run() {
397 ContentResolver cr = mContext.getContentResolver();
398 if (mIds == null || mIds.size() == 0) {
399 cr.delete(Thumbnails.CONTENT_URI, null, null);
400 } else {
401 int length = mIds.size();
402 StringBuilder where = new StringBuilder();
403 where.append(Thumbnails._ID);
404 where.append(" not in (");
405 for (int i = 0; i < length; i++) {
406 where.append(mIds.get(i));
407 if (i < (length - 1)) {
408 where.append(",");
409 }
410 }
411 where.append(")");
412 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
413 }
414 }
415
416 }
417
Michael Kolb1514bb72010-11-22 09:11:48 -0800418 @Override
419 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700420 return mFactory;
421 }
422
423 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800424 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800425 if (tab.hasCrashed)
426 tab.showCrashView();
427 else
428 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800429 }
430
431 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800432 public void createSubWindow(Tab tab) {
433 endActionMode();
434 WebView mainView = tab.getWebView();
435 WebView subView = mFactory.createWebView((mainView == null)
436 ? false
437 : mainView.isPrivateBrowsingEnabled());
438 mUi.createSubWindow(tab, subView);
439 }
440
441 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700442 public Context getContext() {
443 return mActivity;
444 }
445
446 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700447 public Activity getActivity() {
448 return mActivity;
449 }
450
451 void setUi(UI ui) {
452 mUi = ui;
453 }
454
Michael Kolbaf63dba2012-05-16 12:58:05 -0700455 @Override
456 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700457 return mSettings;
458 }
459
460 IntentHandler getIntentHandler() {
461 return mIntentHandler;
462 }
463
464 @Override
465 public UI getUi() {
466 return mUi;
467 }
468
469 int getMaxTabs() {
470 return mActivity.getResources().getInteger(R.integer.max_tabs);
471 }
472
473 @Override
474 public TabControl getTabControl() {
475 return mTabControl;
476 }
477
Michael Kolb1bf23132010-11-19 12:55:12 -0800478 @Override
479 public List<Tab> getTabs() {
480 return mTabControl.getTabs();
481 }
482
John Reckaf262e72011-07-25 13:55:44 -0700483 // Open the icon database.
484 private void openIconDatabase() {
485 // We have to call getInstance on the UI thread
486 final WebIconDatabase instance = WebIconDatabase.getInstance();
487 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000488
John Reckaf262e72011-07-25 13:55:44 -0700489 @Override
490 public void run() {
491 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700492 }
John Reckaf262e72011-07-25 13:55:44 -0700493 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700494 }
495
496 private void startHandler() {
497 mHandler = new Handler() {
498
499 @Override
500 public void handleMessage(Message msg) {
501 switch (msg.what) {
502 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700503 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 break;
505 case FOCUS_NODE_HREF:
506 {
507 String url = (String) msg.getData().get("url");
508 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500509 String src = (String) msg.getData().get("src");
510 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 if (TextUtils.isEmpty(url)) {
512 break;
513 }
514 HashMap focusNodeMap = (HashMap) msg.obj;
515 WebView view = (WebView) focusNodeMap.get("webview");
516 // Only apply the action if the top window did not change.
517 if (getCurrentTopWebView() != view) {
518 break;
519 }
520 switch (msg.arg1) {
521 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700522 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500524 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700525 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500526 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500527 case R.id.open_newtab_context_menu_id:
528 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700529 openTab(url, parent,
530 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500531 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700532 case R.id.copy_link_context_menu_id:
533 copy(url);
534 break;
535 case R.id.save_link_context_menu_id:
536 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500537 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800538 mActivity, url, view.getSettings().getUserAgentString(),
539 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700541 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700542 if(title == null || title == "")
543 title = url;
544
545 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
546 //SWE TODO: No thumbnail support for the url obtained via
547 //browser context menu as its not loaded in webview.
548 if (bookmarkIntent != null) {
549 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
550 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
551 mActivity.startActivity(bookmarkIntent);
552 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700553 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 }
555 break;
556 }
557
558 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700559 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 break;
561
562 case STOP_LOAD:
563 stopLoading();
564 break;
565
566 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700567 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700568 mWakeLock.release();
569 // if we reach here, Browser should be still in the
570 // background loading after WAKELOCK_TIMEOUT (5-min).
571 // To avoid burning the battery, stop loading.
572 mTabControl.stopAllLoading();
573 }
574 break;
575
576 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800577 Tab tab = (Tab) msg.obj;
578 if (tab != null) {
579 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 }
581 break;
kaiyiz591110b2013-08-06 17:11:06 +0800582 case OPEN_MENU:
583 if (!mOptionsMenuOpen && mActivity != null ) {
584 mActivity.openOptionsMenu();
585 }
586 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700587 }
588 }
589 };
590
591 }
592
John Reckef654f12011-07-12 16:42:08 -0700593 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200594 public Tab getCurrentTab() {
595 return mTabControl.getCurrentTab();
596 }
597
Michael Kolbba99c5d2010-11-29 14:57:41 -0800598 @Override
599 public void shareCurrentPage() {
600 shareCurrentPage(mTabControl.getCurrentTab());
601 }
602
603 private void shareCurrentPage(Tab tab) {
604 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800605 sharePage(mActivity, tab.getTitle(),
606 tab.getUrl(), tab.getFavicon(),
607 createScreenshot(tab.getWebView(),
608 getDesiredThumbnailWidth(mActivity),
609 getDesiredThumbnailHeight(mActivity)));
610 }
611 }
612
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 /**
614 * Share a page, providing the title, url, favicon, and a screenshot. Uses
615 * an {@link Intent} to launch the Activity chooser.
616 * @param c Context used to launch a new Activity.
617 * @param title Title of the page. Stored in the Intent with
618 * {@link Intent#EXTRA_SUBJECT}
619 * @param url URL of the page. Stored in the Intent with
620 * {@link Intent#EXTRA_TEXT}
621 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
622 * with {@link Browser#EXTRA_SHARE_FAVICON}
623 * @param screenshot Bitmap of a screenshot of the page. Stored in the
624 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
625 */
626 static final void sharePage(Context c, String title, String url,
627 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700628
629 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
630 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
631 R.layout.app_row, sDialog.getApps());
632 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 }
634
635 private void copy(CharSequence text) {
636 ClipboardManager cm = (ClipboardManager) mActivity
637 .getSystemService(Context.CLIPBOARD_SERVICE);
638 cm.setText(text);
639 }
640
641 // lifecycle
642
John Reck9c35b9c2012-05-30 10:08:50 -0700643 @Override
644 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700645 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800646 // update the menu in case of a locale change
647 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800648 if (mOptionsMenuOpen) {
649 mActivity.closeOptionsMenu();
650 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
651 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 if (mPageDialogsHandler != null) {
653 mPageDialogsHandler.onConfigurationChanged(config);
654 }
655 mUi.onConfigurationChanged(config);
656 }
657
658 @Override
659 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700660 if (!mUi.isWebShowing()) {
661 mUi.showWeb(false);
662 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 mIntentHandler.onNewIntent(intent);
664 }
665
John Reck9c35b9c2012-05-30 10:08:50 -0700666 @Override
667 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800668 if (mUi.isCustomViewShowing()) {
669 hideCustomView();
670 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700671 if (mActivityPaused) {
672 Log.e(LOGTAG, "BrowserActivity is already paused.");
673 return;
674 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800676 Tab tab = mTabControl.getCurrentTab();
677 if (tab != null) {
678 tab.pause();
679 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700680 if (mWakeLock == null) {
681 PowerManager pm = (PowerManager) mActivity
682 .getSystemService(Context.POWER_SERVICE);
683 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
684 }
Michael Kolb70976932010-11-30 11:34:01 -0800685 mWakeLock.acquire();
686 mHandler.sendMessageDelayed(mHandler
687 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
688 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700689 }
690 mUi.onPause();
691 mNetworkHandler.onPause();
692
693 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100694 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700695 if (sThumbnailBitmap != null) {
696 sThumbnailBitmap.recycle();
697 sThumbnailBitmap = null;
698 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700699 }
700
John Reck9c35b9c2012-05-30 10:08:50 -0700701 @Override
702 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800704 Bundle saveState = createSaveState();
705
706 // crash recovery manages all save & restore state
707 mCrashRecoveryHandler.writeState(saveState);
708 mSettings.setLastRunPaused(true);
709 }
710
711 /**
712 * Save the current state to outState. Does not write the state to
713 * disk.
714 * @return Bundle containing the current state of all tabs.
715 */
716 /* package */ Bundle createSaveState() {
717 Bundle saveState = new Bundle();
718 mTabControl.saveState(saveState);
719 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700720 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800721 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700722 }
George Mount3636d0a2011-11-21 09:08:21 -0800723 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700724 }
725
John Reck9c35b9c2012-05-30 10:08:50 -0700726 @Override
727 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700728 if (!mActivityPaused) {
729 Log.e(LOGTAG, "BrowserActivity is already resumed.");
730 return;
731 }
George Mount3636d0a2011-11-21 09:08:21 -0800732 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800734 Tab current = mTabControl.getCurrentTab();
735 if (current != null) {
736 current.resume();
737 resumeWebViewTimers(current);
738 }
John Reckf57c0292011-07-21 18:15:39 -0700739 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200740
Michael Kolb8233fac2010-10-26 16:08:53 -0700741 mUi.onResume();
742 mNetworkHandler.onResume();
743 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100744 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700745 if (mVoiceResult != null) {
746 mUi.onVoiceResult(mVoiceResult);
747 mVoiceResult = null;
748 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800749 if (current != null && current.hasCrashed) {
750 current.showCrashView();
751 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700752 }
753
John Reckf57c0292011-07-21 18:15:39 -0700754 private void releaseWakeLock() {
755 if (mWakeLock != null && mWakeLock.isHeld()) {
756 mHandler.removeMessages(RELEASE_WAKELOCK);
757 mWakeLock.release();
758 }
759 }
760
Michael Kolb70976932010-11-30 11:34:01 -0800761 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800762 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800763 * @param tab guaranteed non-null
764 */
765 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700766 boolean inLoad = tab.inPageLoad();
767 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
768 CookieSyncManager.getInstance().startSync();
769 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100770 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700771 }
772 }
773
Michael Kolb70976932010-11-30 11:34:01 -0800774 /**
775 * Pause all WebView timers using the WebView of the given tab
776 * @param tab
777 * @return true if the timers are paused or tab is null
778 */
779 private boolean pauseWebViewTimers(Tab tab) {
780 if (tab == null) {
781 return true;
782 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700783 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100784 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700785 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 }
Michael Kolb70976932010-11-30 11:34:01 -0800787 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700788 }
789
John Reck9c35b9c2012-05-30 10:08:50 -0700790 @Override
791 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800792 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700793 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
794 mUploadHandler = null;
795 }
796 if (mTabControl == null) return;
797 mUi.onDestroy();
798 // Remove the current tab and sub window
799 Tab t = mTabControl.getCurrentTab();
800 if (t != null) {
801 dismissSubWindow(t);
802 removeTab(t);
803 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500804 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 // Destroy all the tabs
806 mTabControl.destroy();
807 WebIconDatabase.getInstance().close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700808 }
809
810 protected boolean isActivityPaused() {
811 return mActivityPaused;
812 }
813
John Reck9c35b9c2012-05-30 10:08:50 -0700814 @Override
815 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700816 mTabControl.freeMemory();
817 }
818
819 @Override
820 public boolean shouldShowErrorConsole() {
821 return mShouldShowErrorConsole;
822 }
823
824 protected void setShouldShowErrorConsole(boolean show) {
825 if (show == mShouldShowErrorConsole) {
826 // Nothing to do.
827 return;
828 }
829 mShouldShowErrorConsole = show;
830 Tab t = mTabControl.getCurrentTab();
831 if (t == null) {
832 // There is no current tab so we cannot toggle the error console
833 return;
834 }
835 mUi.setShouldShowErrorConsole(t, show);
836 }
837
838 @Override
839 public void stopLoading() {
840 mLoadStopped = true;
841 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700843 if (w != null) {
844 w.stopLoading();
845 mUi.onPageStopped(tab);
846 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 }
848
849 boolean didUserStopLoading() {
850 return mLoadStopped;
851 }
852
kaiyiza016da12013-08-26 17:50:22 +0800853 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700854 final String reminderType = getContext().getResources().getString(
855 R.string.def_wifi_browser_interaction_remind_type);
856 final String selectionConnnection = getContext().getResources().getString(
857 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700858 final String wifiSelection = getContext().getResources().getString(
859 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700860
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700861 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
862 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700863 return;
864
kaiyiza016da12013-08-26 17:50:22 +0800865 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700866 Context.CONNECTIVITY_SERVICE);
867 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700868 WifiManager wifiMgr = (WifiManager) this.getContext()
869 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700870 if (networkInfo == null
871 || (networkInfo != null && (networkInfo.getType() !=
872 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700873 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
874 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700875 List<ScanResult> list = wifiMgr.getScanResults();
876 // Have no AP's for Wifi's fall back to data
877 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700878 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700879 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
880 this.getContext().startActivity(intent);
881 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700882 // Request to select Wifi AP
883 try {
884 Intent intent = new Intent(wifiSelection);
885 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
886 this.getContext().startActivity(intent);
887 } catch (Exception e) {
888 String err_msg = this.getContext().getString(
889 R.string.acivity_not_found, wifiSelection);
890 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
891 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700892 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700893 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800894 }
895 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700896
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 // WebViewController
898
899 @Override
John Reck324d4402011-01-11 16:56:42 -0800900 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700901
902 // We've started to load a new page. If there was a pending message
903 // to save a screenshot then we will now take the new page and save
904 // an incorrect screenshot. Therefore, remove any pending thumbnail
905 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700906 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700907
908 // reset sync timer to avoid sync starts during loading a page
909 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700910 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700911 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700912 boolean networkNotifier =
913 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700914 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700915 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800916 } else {
917 if (!mNetworkHandler.isNetworkUp()) {
918 view.setNetworkAvailable(false);
919 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700920 }
921
922 // when BrowserActivity just starts, onPageStarted may be called before
923 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
924 // to start the timer. As we won't switch tabs while an activity is in
925 // pause state, we can ensure calling resume and pause in pair.
926 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800927 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 }
929 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700930 endActionMode();
931
John Reck30c714c2010-12-16 17:30:34 -0800932 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700933
John Reck324d4402011-01-11 16:56:42 -0800934 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 // update the bookmark database for favicon
936 maybeUpdateFavicon(tab, null, url, favicon);
937
938 Performance.tracePageStart(url);
939
940 // Performance probe
941 if (false) {
942 Performance.onPageStarted();
943 }
944
945 }
946
947 @Override
John Reck324d4402011-01-11 16:56:42 -0800948 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700949 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800950 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200951
Michael Kolb8233fac2010-10-26 16:08:53 -0700952 // Performance probe
953 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800954 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700955 }
956
957 Performance.tracePageFinished();
958 }
959
960 @Override
John Reck30c714c2010-12-16 17:30:34 -0800961 public void onProgressChanged(Tab tab) {
962 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700963
964 if (newProgress == 100) {
965 CookieSyncManager.getInstance().sync();
966 // onProgressChanged() may continue to be called after the main
967 // frame has finished loading, as any remaining sub frames continue
968 // to load. We'll only get called once though with newProgress as
969 // 100 when everything is loaded. (onPageFinished is called once
970 // when the main frame completes loading regardless of the state of
971 // any sub frames so calls to onProgressChanges may continue after
972 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800973 if (tab.inPageLoad()) {
974 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200975 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
976 // pause the WebView timer and release the wake lock if it is
977 // finished while BrowserActivity is in pause state.
978 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 }
John Reckd9862372012-02-21 15:04:50 -0800980 if (!tab.isPrivateBrowsingEnabled()
981 && !TextUtils.isEmpty(tab.getUrl())
982 && !tab.isSnapshot()) {
983 // Only update the bookmark screenshot if the user did not
984 // cancel the load early and there is not already
985 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700986 if (tab.shouldUpdateThumbnail() &&
987 (tab.inForeground() && !didUserStopLoading()
988 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800989 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
990 mHandler.sendMessageDelayed(mHandler.obtainMessage(
991 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800992 1500);
John Reckd9862372012-02-21 15:04:50 -0800993 }
994 }
995 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800997 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700998 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800999 // still loading
1000 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001001 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001002 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 }
1004 }
John Reck30c714c2010-12-16 17:30:34 -08001005 mUi.onProgressChanged(tab);
1006 }
1007
1008 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001009 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001010 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001011 }
1012
1013 @Override
1014 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001015 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001016 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001017 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1019 return;
1020 }
1021 // Update the title in the history database if not in private browsing mode
1022 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001023 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 }
1025 }
1026
1027 @Override
1028 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001029 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001030 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1031 }
1032
1033 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001034 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1035 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001036 }
1037
1038 @Override
1039 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1040 if (mMenuIsDown) {
1041 // only check shortcut key when MENU is held
1042 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1043 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001045 int keyCode = event.getKeyCode();
1046 // We need to send almost every key to WebKit. However:
1047 // 1. We don't want to block the device on the renderer for
1048 // some keys like menu, home, call.
1049 // 2. There are no WebKit equivalents for some of these keys
1050 // (see app/keyboard_codes_win.h)
1051 // Note that these are not the same set as KeyEvent.isSystemKey:
1052 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1053 if (keyCode == KeyEvent.KEYCODE_MENU ||
1054 keyCode == KeyEvent.KEYCODE_HOME ||
1055 keyCode == KeyEvent.KEYCODE_BACK ||
1056 keyCode == KeyEvent.KEYCODE_CALL ||
1057 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1058 keyCode == KeyEvent.KEYCODE_POWER ||
1059 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1060 keyCode == KeyEvent.KEYCODE_CAMERA ||
1061 keyCode == KeyEvent.KEYCODE_FOCUS ||
1062 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1063 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1064 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1065 return true;
1066 }
1067
1068 // We also have to intercept some shortcuts before we send them to the ContentView.
1069 if (event.isCtrlPressed() && (
1070 keyCode == KeyEvent.KEYCODE_TAB ||
1071 keyCode == KeyEvent.KEYCODE_W ||
1072 keyCode == KeyEvent.KEYCODE_F4)) {
1073 return true;
1074 }
1075
1076 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 }
1078
1079 @Override
John Reck997b1b72012-04-19 18:08:25 -07001080 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 if (!isActivityPaused()) {
1082 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001083 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 } else {
John Reck997b1b72012-04-19 18:08:25 -07001085 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001086 }
1087 }
John Reck997b1b72012-04-19 18:08:25 -07001088 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001089 }
1090
1091 @Override
John Reck324d4402011-01-11 16:56:42 -08001092 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001093 boolean disableHistoryWrites =
1094 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1095
1096 // Don't save anything in private browsing mode or when explicitly set
1097 // not to write history via an overlay
1098 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001099 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001100
John Reck324d4402011-01-11 16:56:42 -08001101 if (TextUtils.isEmpty(url)
1102 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 return;
1104 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001105
John Reckf57c0292011-07-21 18:15:39 -07001106 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001107 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001108 }
1109
1110 @Override
1111 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1112 AsyncTask<Void, Void, String[]> task =
1113 new AsyncTask<Void, Void, String[]>() {
1114 @Override
1115 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001116 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 }
1118 @Override
1119 public void onPostExecute(String[] result) {
1120 callback.onReceiveValue(result);
1121 }
1122 };
1123 task.execute();
1124 }
1125
1126 @Override
1127 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1128 final HttpAuthHandler handler, final String host,
1129 final String realm) {
1130 String username = null;
1131 String password = null;
1132
1133 boolean reuseHttpAuthUsernamePassword
1134 = handler.useHttpAuthUsernamePassword();
1135
1136 if (reuseHttpAuthUsernamePassword && view != null) {
1137 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1138 if (credentials != null && credentials.length == 2) {
1139 username = credentials[0];
1140 password = credentials[1];
1141 }
1142 }
1143
1144 if (username != null && password != null) {
1145 handler.proceed(username, password);
1146 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001147 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1149 } else {
1150 handler.cancel();
1151 }
1152 }
1153 }
1154
1155 @Override
1156 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001157 String contentDisposition, String mimetype, String referer,
1158 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001159 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001160 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001161 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001162 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001163 // This Tab was opened for the sole purpose of downloading a
1164 // file. Remove it.
1165 if (tab == mTabControl.getCurrentTab()) {
1166 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001167 if (tab.getDerivedFromIntent())
1168 closeTab(tab);
1169 else
1170 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001171 } else {
1172 // In this case, it is not.
1173 closeTab(tab);
1174 }
1175 }
1176 }
1177
1178 @Override
1179 public Bitmap getDefaultVideoPoster() {
1180 return mUi.getDefaultVideoPoster();
1181 }
1182
1183 @Override
1184 public View getVideoLoadingProgressView() {
1185 return mUi.getVideoLoadingProgressView();
1186 }
1187
1188 @Override
1189 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1190 SslError error) {
1191 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1192 }
1193
1194 // helper method
1195
1196 /*
1197 * Update the favorites icon if the private browsing isn't enabled and the
1198 * icon is valid.
1199 */
1200 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1201 final String url, Bitmap favicon) {
1202 if (favicon == null) {
1203 return;
1204 }
1205 if (!tab.isPrivateBrowsingEnabled()) {
1206 Bookmarks.updateFavicon(mActivity
1207 .getContentResolver(), originalUrl, url, favicon);
1208 }
1209 }
1210
Leon Scroggins4cd97792010-12-03 15:31:56 -05001211 @Override
1212 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001213 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001214 mUi.bookmarkedStatusHasChanged(tab);
1215 }
1216
Michael Kolb8233fac2010-10-26 16:08:53 -07001217 // end WebViewController
1218
1219 protected void pageUp() {
1220 getCurrentTopWebView().pageUp(false);
1221 }
1222
1223 protected void pageDown() {
1224 getCurrentTopWebView().pageDown(false);
1225 }
1226
1227 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001228 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001229 public void editUrl() {
1230 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001231 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 }
1233
John Reck9c35b9c2012-05-30 10:08:50 -07001234 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001235 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001236 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001237 if (tab.inForeground()) {
1238 if (mUi.isCustomViewShowing()) {
1239 callback.onCustomViewHidden();
1240 return;
1241 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001242 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 // Save the menu state and set it to empty while the custom
1244 // view is showing.
1245 mOldMenuState = mMenuState;
1246 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001247 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001248 }
1249 }
1250
1251 @Override
1252 public void hideCustomView() {
1253 if (mUi.isCustomViewShowing()) {
1254 mUi.onHideCustomView();
1255 // Reset the old menu state.
1256 mMenuState = mOldMenuState;
1257 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001258 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 }
1260 }
1261
John Reck9c35b9c2012-05-30 10:08:50 -07001262 @Override
1263 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 Intent intent) {
1265 if (getCurrentTopWebView() == null) return;
1266 switch (requestCode) {
1267 case PREFERENCES_PAGE:
1268 if (resultCode == Activity.RESULT_OK && intent != null) {
1269 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001270 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001271 mTabControl.removeParentChildRelationShips();
1272 }
1273 }
1274 break;
1275 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001276 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001277 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001278 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001280 case AUTOFILL_SETUP:
1281 // Determine whether a profile was actually set up or not
1282 // and if so, send the message back to the WebTextView to
1283 // fill the form with the new profile.
1284 if (getSettings().getAutoFillProfile() != null) {
1285 mAutoFillSetupMessage.sendToTarget();
1286 mAutoFillSetupMessage = null;
1287 }
1288 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001289 case COMBO_VIEW:
1290 if (intent == null || resultCode != Activity.RESULT_OK) {
1291 break;
1292 }
John Reck3ba45532011-08-11 16:26:53 -07001293 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001294 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1295 Tab t = getCurrentTab();
1296 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001297 mUpdateMyNavThumbnail = true;
1298 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001299 loadUrl(t, uri.toString());
1300 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1301 String[] urls = intent.getStringArrayExtra(
1302 ComboViewActivity.EXTRA_OPEN_ALL);
1303 Tab parent = getCurrentTab();
1304 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001305 if (url != null) {
1306 parent = openTab(url, parent,
1307 !mSettings.openInBackground(), true);
1308 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001309 }
1310 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1311 long id = intent.getLongExtra(
1312 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1313 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001314 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001315 }
1316 }
1317 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001318 case VOICE_RESULT:
1319 if (resultCode == Activity.RESULT_OK && intent != null) {
1320 ArrayList<String> results = intent.getStringArrayListExtra(
1321 RecognizerIntent.EXTRA_RESULTS);
1322 if (results.size() >= 1) {
1323 mVoiceResult = results.get(0);
1324 }
1325 }
1326 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001327 case MY_NAVIGATION:
1328 if (intent == null || resultCode != Activity.RESULT_OK) {
1329 break;
1330 }
1331
1332 if (intent.getBooleanExtra("need_refresh", false) &&
1333 getCurrentTopWebView() != null) {
1334 getCurrentTopWebView().reload();
1335 }
1336 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 default:
1338 break;
1339 }
1340 getCurrentTopWebView().requestFocus();
1341 }
1342
1343 /**
1344 * Open the Go page.
1345 * @param startWithHistory If true, open starting on the history tab.
1346 * Otherwise, start with the bookmarks tab.
1347 */
1348 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001349 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001350 if (mTabControl.getCurrentWebView() == null) {
1351 return;
1352 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001353 // clear action mode
1354 if (isInCustomActionMode()) {
1355 endActionMode();
1356 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001357 Bundle extras = new Bundle();
1358 // Disable opening in a new window if we have maxed out the windows
1359 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1360 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001361 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001362 }
1363
1364 // combo view callbacks
1365
Michael Kolb8233fac2010-10-26 16:08:53 -07001366 // key handling
1367 protected void onBackKey() {
1368 if (!mUi.onBackKey()) {
1369 WebView subwindow = mTabControl.getCurrentSubWindow();
1370 if (subwindow != null) {
1371 if (subwindow.canGoBack()) {
1372 subwindow.goBack();
1373 } else {
1374 dismissSubWindow(mTabControl.getCurrentTab());
1375 }
1376 } else {
1377 goBackOnePageOrQuit();
1378 }
1379 }
1380 }
1381
Michael Kolb4bd767d2011-05-27 11:33:55 -07001382 protected boolean onMenuKey() {
1383 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001384 }
1385
Michael Kolb8233fac2010-10-26 16:08:53 -07001386 // menu handling and state
1387 // TODO: maybe put into separate handler
1388
John Reck9c35b9c2012-05-30 10:08:50 -07001389 @Override
1390 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001391 if (mMenuState == EMPTY_MENU) {
1392 return false;
1393 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 MenuInflater inflater = mActivity.getMenuInflater();
1395 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 return true;
1397 }
1398
John Reck9c35b9c2012-05-30 10:08:50 -07001399 @Override
1400 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001402 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001403 return;
1404 }
1405 if (!(v instanceof WebView)) {
1406 return;
1407 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001408 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 WebView.HitTestResult result = webview.getHitTestResult();
1410 if (result == null) {
1411 return;
1412 }
1413
1414 int type = result.getType();
1415 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1416 Log.w(LOGTAG,
1417 "We should not show context menu when nothing is touched");
1418 return;
1419 }
1420 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1421 // let TextView handles context menu
1422 return;
1423 }
1424
1425 // Note, http://b/issue?id=1106666 is requesting that
1426 // an inflated menu can be used again. This is not available
1427 // yet, so inflate each time (yuk!)
1428 MenuInflater inflater = mActivity.getMenuInflater();
1429 inflater.inflate(R.menu.browsercontext, menu);
1430
1431 // Show the correct menu group
1432 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001433 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001434 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 menu.setGroupVisible(R.id.PHONE_MENU,
1436 type == WebView.HitTestResult.PHONE_TYPE);
1437 menu.setGroupVisible(R.id.EMAIL_MENU,
1438 type == WebView.HitTestResult.EMAIL_TYPE);
1439 menu.setGroupVisible(R.id.GEO_MENU,
1440 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001441 String itemUrl = null;
1442 String url = webview.getOriginalUrl();
1443 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1444 itemUrl = Uri.decode(navigationUrl);
1445 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1446 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1447 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1448 } else {
1449 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1450 }
1451 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1452 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1453 } else {
1454 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1455
1456 menu.setGroupVisible(R.id.IMAGE_MENU,
1457 type == WebView.HitTestResult.IMAGE_TYPE
1458 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1459 menu.setGroupVisible(R.id.ANCHOR_MENU,
1460 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1461 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001462 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1463 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001464 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001465 // Setup custom handling depending on the type
1466 switch (type) {
1467 case WebView.HitTestResult.PHONE_TYPE:
1468 menu.setHeaderTitle(Uri.decode(extra));
1469 menu.findItem(R.id.dial_context_menu_id).setIntent(
1470 new Intent(Intent.ACTION_VIEW, Uri
1471 .parse(WebView.SCHEME_TEL + extra)));
1472 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1473 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1474 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1475 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1476 addIntent);
1477 menu.findItem(R.id.copy_phone_context_menu_id)
1478 .setOnMenuItemClickListener(
1479 new Copy(extra));
1480 break;
1481
1482 case WebView.HitTestResult.EMAIL_TYPE:
1483 menu.setHeaderTitle(extra);
1484 menu.findItem(R.id.email_context_menu_id).setIntent(
1485 new Intent(Intent.ACTION_VIEW, Uri
1486 .parse(WebView.SCHEME_MAILTO + extra)));
1487 menu.findItem(R.id.copy_mail_context_menu_id)
1488 .setOnMenuItemClickListener(
1489 new Copy(extra));
1490 break;
1491
1492 case WebView.HitTestResult.GEO_TYPE:
1493 menu.setHeaderTitle(extra);
1494 menu.findItem(R.id.map_context_menu_id).setIntent(
1495 new Intent(Intent.ACTION_VIEW, Uri
1496 .parse(WebView.SCHEME_GEO
1497 + URLEncoder.encode(extra))));
1498 menu.findItem(R.id.copy_geo_context_menu_id)
1499 .setOnMenuItemClickListener(
1500 new Copy(extra));
1501 break;
1502
1503 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1504 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001505 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001506 // decide whether to show the open link in new tab option
1507 boolean showNewTab = mTabControl.canCreateNewTab();
1508 MenuItem newTabItem
1509 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001510 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001511 ? R.string.contextmenu_openlink_newwindow_background
1512 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001513 newTabItem.setVisible(showNewTab);
1514 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001515 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1516 newTabItem.setOnMenuItemClickListener(
1517 new MenuItem.OnMenuItemClickListener() {
1518 @Override
1519 public boolean onMenuItemClick(MenuItem item) {
1520 final HashMap<String, WebView> hrefMap =
1521 new HashMap<String, WebView>();
1522 hrefMap.put("webview", webview);
1523 final Message msg = mHandler.obtainMessage(
1524 FOCUS_NODE_HREF,
1525 R.id.open_newtab_context_menu_id,
1526 0, hrefMap);
1527 webview.requestFocusNodeHref(msg);
1528 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001529 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001530 });
1531 } else {
1532 newTabItem.setOnMenuItemClickListener(
1533 new MenuItem.OnMenuItemClickListener() {
1534 @Override
1535 public boolean onMenuItemClick(MenuItem item) {
1536 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001537 openTab(extra, parent,
1538 !mSettings.openInBackground(),
1539 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001540 return true;
1541 }
1542 });
1543 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001544 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001545 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1546 menu.setHeaderTitle(navigationUrl);
1547 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1548
1549 if (itemUrl != null) {
1550 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1551 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1552 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1553 @Override
1554 public boolean onMenuItemClick(MenuItem item) {
1555 final Intent intent = new Intent(Controller.this
1556 .getContext(),
1557 AddMyNavigationPage.class);
1558 Bundle bundle = new Bundle();
1559 String url = Uri.decode(navigationUrl);
1560 bundle.putBoolean("isAdding", false);
1561 bundle.putString("url", url);
1562 bundle.putString("name", getNameFromUrl(url));
1563 intent.putExtra("websites", bundle);
1564 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1565 return false;
1566 }
1567 });
1568 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1569 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1570 @Override
1571 public boolean onMenuItemClick(MenuItem item) {
1572 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1573 return false;
1574 }
1575 });
1576 }
1577 } else {
1578 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1579 }
1580 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1582 break;
1583 }
1584 // otherwise fall through to handle image part
1585 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001586 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1587 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001588 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1589 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001590 shareItem.setOnMenuItemClickListener(
1591 new MenuItem.OnMenuItemClickListener() {
1592 @Override
1593 public boolean onMenuItemClick(MenuItem item) {
1594 sharePage(mActivity, null, extra, null,
1595 null);
1596 return true;
1597 }
1598 }
1599 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001601 menu.findItem(R.id.view_image_context_menu_id)
1602 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1603 @Override
1604 public boolean onMenuItemClick(MenuItem item) {
1605 openTab(extra, mTabControl.getCurrentTab(), true, true);
1606 return false;
1607 }
1608 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001609 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1610 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1611 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001612 menu.findItem(R.id.set_wallpaper_context_menu_id).
1613 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1614 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001615 break;
1616
1617 default:
1618 Log.w(LOGTAG, "We should not get here.");
1619 break;
1620 }
1621 //update the ui
1622 mUi.onContextMenuCreated(menu);
1623 }
1624
kaiyiz6e5b3e02013-08-19 20:02:01 +08001625 public void startAddMyNavigation(String url) {
1626 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1627 Bundle bundle = new Bundle();
1628 bundle.putBoolean("isAdding", true);
1629 bundle.putString("url", url);
1630 bundle.putString("name", getNameFromUrl(url));
1631 intent.putExtra("websites", bundle);
1632 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1633 }
1634
1635 private void showMyNavigationDeleteDialog(final String itemUrl) {
1636 new AlertDialog.Builder(this.getContext())
1637 .setTitle(R.string.my_navigation_delete_label)
1638 .setIcon(android.R.drawable.ic_dialog_alert)
1639 .setMessage(R.string.my_navigation_delete_msg)
1640 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1641 @Override
1642 public void onClick(DialogInterface dialog, int whichButton) {
1643 deleteMyNavigationItem(itemUrl);
1644 }
1645 })
1646 .setNegativeButton(R.string.cancel, null)
1647 .show();
1648 }
1649
1650 private void deleteMyNavigationItem(final String itemUrl) {
1651 ContentResolver cr = this.getContext().getContentResolver();
1652 Cursor cursor = null;
1653
1654 try {
1655 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1656 new String[] {
1657 MyNavigationUtil.ID
1658 }, "url = ?", new String[] {
1659 itemUrl
1660 }, null);
1661 if (null != cursor && cursor.moveToFirst()) {
1662 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1663 cursor.getLong(0));
1664
1665 ContentValues values = new ContentValues();
1666 values.put(MyNavigationUtil.TITLE, "");
1667 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1668 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1669 ByteArrayOutputStream os = new ByteArrayOutputStream();
1670 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1671 R.raw.my_navigation_add);
1672 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1673 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1674 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1675 cr.update(uri, values, null, null);
1676 } else {
1677 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1678 }
1679 } catch (IllegalStateException e) {
1680 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1681 } finally {
1682 if (null != cursor) {
1683 cursor.close();
1684 }
1685 }
1686
1687 if (getCurrentTopWebView() != null) {
1688 getCurrentTopWebView().reload();
1689 }
1690 }
1691
1692 private String getNameFromUrl(String itemUrl) {
1693 ContentResolver cr = this.getContext().getContentResolver();
1694 Cursor cursor = null;
1695 String name = null;
1696
1697 try {
1698 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1699 new String[] {
1700 MyNavigationUtil.TITLE
1701 }, "url = ?", new String[] {
1702 itemUrl
1703 }, null);
1704 if (null != cursor && cursor.moveToFirst()) {
1705 name = cursor.getString(0);
1706 } else {
1707 Log.e(LOGTAG, "this item does not exist!");
1708 }
1709 } catch (IllegalStateException e) {
1710 Log.e(LOGTAG, "getNameFromUrl", e);
1711 } finally {
1712 if (null != cursor) {
1713 cursor.close();
1714 }
1715 }
1716 return name;
1717 }
1718
1719 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1720 int width = mActivity.getResources().getDimensionPixelOffset(
1721 R.dimen.myNavigationThumbnailWidth);
1722 int height = mActivity.getResources().getDimensionPixelOffset(
1723 R.dimen.myNavigationThumbnailHeight);
1724
1725 final Bitmap bm = createScreenshot(webView, width, height);
1726
1727 if (bm == null) {
1728 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1729 return;
1730 }
1731
1732 final ContentResolver cr = mActivity.getContentResolver();
1733 new AsyncTask<Void, Void, Void>() {
1734 @Override
1735 protected Void doInBackground(Void... unused) {
1736 ContentResolver cr = mActivity.getContentResolver();
1737 Cursor cursor = null;
1738 try {
1739 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1740 new String[] {
1741 MyNavigationUtil.ID
1742 }, "url = ?", new String[] {
1743 itemUrl
1744 }, null);
1745 if (null != cursor && cursor.moveToFirst()) {
1746 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1747 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1748
1749 ContentValues values = new ContentValues();
1750 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1751 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1752 cursor.getLong(0));
1753 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1754 cr.update(uri, values, null, null);
1755 os.close();
1756 }
1757 } catch (IllegalStateException e) {
1758 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1759 } catch (IOException e) {
1760 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1761 } finally {
1762 if (null != cursor) {
1763 cursor.close();
1764 }
1765 }
1766 return null;
1767 }
1768 }.execute();
1769 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001770 /**
1771 * As the menu can be open when loading state changes
1772 * we must manually update the state of the stop/reload menu
1773 * item
1774 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001775 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001776 if (menu == null) {
1777 return;
1778 }
1779 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001780 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001781 menu.findItem(R.id.stop_menu_id):
1782 menu.findItem(R.id.reload_menu_id);
1783 if (src != null) {
1784 dest.setIcon(src.getIcon());
1785 dest.setTitle(src.getTitle());
1786 }
1787 }
1788
John Reck9c35b9c2012-05-30 10:08:50 -07001789 @Override
1790 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001791 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001792 // hold on to the menu reference here; it is used by the page callbacks
1793 // to update the menu based on loading state
1794 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001795 // Note: setVisible will decide whether an item is visible; while
1796 // setEnabled() will decide whether an item is enabled, which also means
1797 // whether the matching shortcut key will function.
1798 switch (mMenuState) {
1799 case EMPTY_MENU:
1800 if (mCurrentMenuState != mMenuState) {
1801 menu.setGroupVisible(R.id.MAIN_MENU, false);
1802 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1803 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1804 }
1805 break;
1806 default:
1807 if (mCurrentMenuState != mMenuState) {
1808 menu.setGroupVisible(R.id.MAIN_MENU, true);
1809 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1810 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1811 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001812 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001813 break;
1814 }
1815 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001816 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001817 }
1818
Michael Kolb4bf79712011-07-14 14:18:12 -07001819 @Override
1820 public void updateMenuState(Tab tab, Menu menu) {
1821 boolean canGoBack = false;
1822 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001823 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001824 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001825 if (tab != null) {
1826 canGoBack = tab.canGoBack();
1827 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001828 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001829 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001830 }
1831 final MenuItem back = menu.findItem(R.id.back_menu_id);
1832 back.setEnabled(canGoBack);
1833
1834 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001835
1836 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1837 forward.setEnabled(canGoForward);
1838
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001839 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1840 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001841 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001842 if (source != null && dest != null) {
1843 dest.setTitle(source.getTitle());
1844 dest.setIcon(source.getIcon());
1845 }
John Recke1a03a32011-09-14 17:04:16 -07001846 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001847
1848 // decide whether to show the share link option
1849 PackageManager pm = mActivity.getPackageManager();
1850 Intent send = new Intent(Intent.ACTION_SEND);
1851 send.setType("text/plain");
1852 ResolveInfo ri = pm.resolveActivity(send,
1853 PackageManager.MATCH_DEFAULT_ONLY);
1854 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1855
1856 boolean isNavDump = mSettings.enableNavDump();
1857 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1858 nav.setVisible(isNavDump);
1859 nav.setEnabled(isNavDump);
1860
1861 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001862 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1863 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001864 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1865 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001866 // history and snapshots item are the members of COMBO menu group,
1867 // so if show history item, only make snapshots item invisible.
1868 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001869
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001870 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001871 }
1872
John Reck9c35b9c2012-05-30 10:08:50 -07001873 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001874 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001875 if (null == getCurrentTopWebView()) {
1876 return false;
1877 }
1878 if (mMenuIsDown) {
1879 // The shortcut action consumes the MENU. Even if it is still down,
1880 // it won't trigger the next shortcut action. In the case of the
1881 // shortcut action triggering a new activity, like Bookmarks, we
1882 // won't get onKeyUp for MENU. So it is important to reset it here.
1883 mMenuIsDown = false;
1884 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001885 if (mUi.onOptionsItemSelected(item)) {
1886 // ui callback handled it
1887 return true;
1888 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001889 switch (item.getItemId()) {
1890 // -- Main menu
1891 case R.id.new_tab_menu_id:
1892 openTabToHomePage();
1893 break;
1894
1895 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001896 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 break;
1898
Afzal Najamd4e33312012-04-26 01:54:01 -04001899 case R.id.close_other_tabs_id:
1900 closeOtherTabs();
1901 break;
1902
Michael Kolb8233fac2010-10-26 16:08:53 -07001903 case R.id.goto_menu_id:
1904 editUrl();
1905 break;
1906
1907 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001908 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1909 break;
1910
1911 case R.id.history_menu_id:
1912 bookmarksOrHistoryPicker(ComboViews.History);
1913 break;
1914
1915 case R.id.snapshots_menu_id:
1916 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001917 break;
1918
Michael Kolb8233fac2010-10-26 16:08:53 -07001919 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001920 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001921 break;
1922
1923 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001924 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001925 stopLoading();
1926 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001927 Tab currentTab = mTabControl.getCurrentTab();
1928 if (currentTab.hasCrashed) {
1929 currentTab.replaceCrashView(getCurrentTopWebView(),
1930 currentTab.getViewContainer());
1931 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001932 getCurrentTopWebView().reload();
1933 }
1934 break;
1935
1936 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001937 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 break;
1939
1940 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001941 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001942 break;
1943
1944 case R.id.close_menu_id:
1945 // Close the subwindow if it exists.
1946 if (mTabControl.getCurrentSubWindow() != null) {
1947 dismissSubWindow(mTabControl.getCurrentTab());
1948 break;
1949 }
1950 closeCurrentTab();
1951 break;
1952
kaiyiza8b6dbb2013-07-29 18:11:22 +08001953 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001954 Object[] params = { new String("persist.debug.browsermonkeytest")};
1955 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001956 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001957 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001958 if (ret != null && ret.equals("enable"))
1959 break;
luxiaolb40014b2013-07-19 10:01:43 +08001960 showExitDialog(mActivity);
1961 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001962 case R.id.homepage_menu_id:
1963 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001964 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001965 break;
1966
1967 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001968 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001969 break;
1970
1971 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001972 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001973 break;
1974
John Reck2bc80422011-06-30 15:11:49 -07001975 case R.id.save_snapshot_menu_id:
1976 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001977 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001978 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001979 break;
1980
Michael Kolb8233fac2010-10-26 16:08:53 -07001981 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001982 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 break;
1984
John Recke1a03a32011-09-14 17:04:16 -07001985 case R.id.snapshot_go_live:
1986 goLive();
1987 return true;
1988
Michael Kolb8233fac2010-10-26 16:08:53 -07001989 case R.id.share_page_menu_id:
1990 Tab currentTab = mTabControl.getCurrentTab();
1991 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 return false;
1993 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001994 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001995 break;
1996
1997 case R.id.dump_nav_menu_id:
1998 getCurrentTopWebView().debugDump();
1999 break;
2000
Michael Kolb8233fac2010-10-26 16:08:53 -07002001 case R.id.zoom_in_menu_id:
2002 getCurrentTopWebView().zoomIn();
2003 break;
2004
2005 case R.id.zoom_out_menu_id:
2006 getCurrentTopWebView().zoomOut();
2007 break;
2008
2009 case R.id.view_downloads_menu_id:
2010 viewDownloads();
2011 break;
2012
John Reck42229bc2011-08-19 13:26:43 -07002013 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002014 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002015 break;
2016
Michael Kolb8233fac2010-10-26 16:08:53 -07002017 case R.id.window_one_menu_id:
2018 case R.id.window_two_menu_id:
2019 case R.id.window_three_menu_id:
2020 case R.id.window_four_menu_id:
2021 case R.id.window_five_menu_id:
2022 case R.id.window_six_menu_id:
2023 case R.id.window_seven_menu_id:
2024 case R.id.window_eight_menu_id:
2025 {
2026 int menuid = item.getItemId();
2027 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2028 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2029 Tab desiredTab = mTabControl.getTab(id);
2030 if (desiredTab != null &&
2031 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002032 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002033 }
2034 break;
2035 }
2036 }
2037 }
2038 break;
2039
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002040 case R.id.about_menu_id:
2041 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2042 builder.setTitle(R.string.about);
2043 builder.setCancelable(true);
2044 String ua = "";
2045 final WebView currentWebView = getCurrentWebView();
2046 if (currentWebView != null) {
2047 final WebSettings s = currentWebView.getSettings();
2048 if (s != null) {
2049 ua = s.getUserAgentString();
2050 }
2051 }
2052 builder.setMessage("Agent:" + ua);
2053 builder.setPositiveButton(android.R.string.ok, null);
2054 builder.create().show();
2055 break;
2056
Michael Kolb8233fac2010-10-26 16:08:53 -07002057 default:
2058 return false;
2059 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002060 return true;
2061 }
2062
John Reck68234a92012-04-19 15:27:12 -07002063 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2064 implements OnCancelListener {
2065
2066 private Tab mTab;
2067 private Dialog mProgressDialog;
2068 private ContentValues mValues;
2069
2070 private SaveSnapshotTask(Tab tab) {
2071 mTab = tab;
2072 }
2073
2074 @Override
2075 protected void onPreExecute() {
2076 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2077 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2078 true, true, this);
2079 mValues = mTab.createSnapshotValues();
2080 }
2081
2082 @Override
2083 protected Long doInBackground(Void... params) {
2084 if (!mTab.saveViewState(mValues)) {
2085 return null;
2086 }
2087 if (isCancelled()) {
2088 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2089 File file = mActivity.getFileStreamPath(path);
2090 if (!file.delete()) {
2091 file.deleteOnExit();
2092 }
2093 return null;
2094 }
2095 final ContentResolver cr = mActivity.getContentResolver();
2096 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2097 if (result == null) {
2098 return null;
2099 }
2100 long id = ContentUris.parseId(result);
2101 return id;
2102 }
2103
2104 @Override
2105 protected void onPostExecute(Long id) {
2106 if (isCancelled()) {
2107 return;
2108 }
2109 mProgressDialog.dismiss();
2110 if (id == null) {
2111 Toast.makeText(mActivity, R.string.snapshot_failed,
2112 Toast.LENGTH_SHORT).show();
2113 return;
2114 }
2115 Bundle b = new Bundle();
2116 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2117 mUi.showComboView(ComboViews.Snapshots, b);
2118 }
2119
2120 @Override
2121 public void onCancel(DialogInterface dialog) {
2122 cancel(true);
2123 }
2124 }
2125
Michael Kolb80f75082012-04-10 10:50:06 -07002126 @Override
2127 public void toggleUserAgent() {
2128 WebView web = getCurrentWebView();
2129 mSettings.toggleDesktopUseragent(web);
2130 web.loadUrl(web.getOriginalUrl());
2131 }
2132
2133 @Override
2134 public void findOnPage() {
2135 getCurrentTopWebView().showFindDialog(null, true);
2136 }
2137
2138 @Override
2139 public void openPreferences() {
2140 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2141 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2142 getCurrentTopWebView().getUrl());
2143 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2144 }
2145
2146 @Override
2147 public void bookmarkCurrentPage() {
2148 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2149 if (bookmarkIntent != null) {
2150 mActivity.startActivity(bookmarkIntent);
2151 }
2152 }
2153
John Recke1a03a32011-09-14 17:04:16 -07002154 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002155 SnapshotTab t = (SnapshotTab) getCurrentTab();
2156 t.loadUrl(t.getLiveUrl(), null);
John Recke1a03a32011-09-14 17:04:16 -07002157 }
2158
luxiaolb40014b2013-07-19 10:01:43 +08002159 private void showExitDialog(final Activity activity) {
2160 new AlertDialog.Builder(activity)
2161 .setTitle(R.string.exit_browser_title)
2162 .setIcon(android.R.drawable.ic_dialog_alert)
2163 .setMessage(R.string.exit_browser_msg)
2164 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2165 public void onClick(DialogInterface dialog, int which) {
2166 activity.moveTaskToBack(true);
2167 dialog.dismiss();
2168 }
2169 })
2170 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2171 public void onClick(DialogInterface dialog, int which) {
2172 activity.finish();
2173 mHandler.postDelayed(new Runnable() {
2174 @Override
2175 public void run() {
2176 // TODO Auto-generated method stub
2177 mCrashRecoveryHandler.clearState(true);
2178 int pid = android.os.Process.myPid();
2179 android.os.Process.killProcess(pid);
2180 }
2181 }, 300);
2182 dialog.dismiss();
2183 }
2184 })
2185 .show();
2186 }
Michael Kolb315d5022011-10-13 12:47:11 -07002187 @Override
2188 public void showPageInfo() {
2189 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2190 }
2191
John Reck9c35b9c2012-05-30 10:08:50 -07002192 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002193 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002194 // Let the History and Bookmark fragments handle menus they created.
2195 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2196 return false;
2197 }
2198
Michael Kolb8233fac2010-10-26 16:08:53 -07002199 int id = item.getItemId();
2200 boolean result = true;
2201 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002202 // -- Browser context menu
2203 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002204 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002205 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002206 case R.id.copy_link_context_menu_id:
2207 final WebView webView = getCurrentTopWebView();
2208 if (null == webView) {
2209 result = false;
2210 break;
2211 }
2212 final HashMap<String, WebView> hrefMap =
2213 new HashMap<String, WebView>();
2214 hrefMap.put("webview", webView);
2215 final Message msg = mHandler.obtainMessage(
2216 FOCUS_NODE_HREF, id, 0, hrefMap);
2217 webView.requestFocusNodeHref(msg);
2218 break;
2219
2220 default:
2221 // For other context menus
2222 result = onOptionsItemSelected(item);
2223 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002224 return result;
2225 }
2226
2227 /**
2228 * support programmatically opening the context menu
2229 */
2230 public void openContextMenu(View view) {
2231 mActivity.openContextMenu(view);
2232 }
2233
2234 /**
2235 * programmatically open the options menu
2236 */
2237 public void openOptionsMenu() {
2238 mActivity.openOptionsMenu();
2239 }
2240
John Reck9c35b9c2012-05-30 10:08:50 -07002241 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002242 public boolean onMenuOpened(int featureId, Menu menu) {
2243 if (mOptionsMenuOpen) {
2244 if (mConfigChanged) {
2245 // We do not need to make any changes to the state of the
2246 // title bar, since the only thing that happened was a
2247 // change in orientation
2248 mConfigChanged = false;
2249 } else {
2250 if (!mExtendedMenuOpen) {
2251 mExtendedMenuOpen = true;
2252 mUi.onExtendedMenuOpened();
2253 } else {
2254 // Switching the menu back to icon view, so show the
2255 // title bar once again.
2256 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002257 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002258 }
2259 }
2260 } else {
2261 // The options menu is closed, so open it, and show the title
2262 mOptionsMenuOpen = true;
2263 mConfigChanged = false;
2264 mExtendedMenuOpen = false;
2265 mUi.onOptionsMenuOpened();
2266 }
2267 return true;
2268 }
2269
John Reck9c35b9c2012-05-30 10:08:50 -07002270 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002271 public void onOptionsMenuClosed(Menu menu) {
2272 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002273 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002274 }
2275
John Reck9c35b9c2012-05-30 10:08:50 -07002276 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002277 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002278 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002279 }
2280
2281 // Helper method for getting the top window.
2282 @Override
2283 public WebView getCurrentTopWebView() {
2284 return mTabControl.getCurrentTopWebView();
2285 }
2286
2287 @Override
2288 public WebView getCurrentWebView() {
2289 return mTabControl.getCurrentWebView();
2290 }
2291
2292 /*
2293 * This method is called as a result of the user selecting the options
2294 * menu to see the download window. It shows the download window on top of
2295 * the current window.
2296 */
2297 void viewDownloads() {
2298 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2299 mActivity.startActivity(intent);
2300 }
2301
John Reck30b065e2011-07-19 10:58:05 -07002302 int getActionModeHeight() {
2303 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2304 new int[] { android.R.attr.actionBarSize });
2305 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2306 actionBarSizeTypedArray.recycle();
2307 return size;
2308 }
2309
Michael Kolb8233fac2010-10-26 16:08:53 -07002310 // action mode
2311
John Reck9c35b9c2012-05-30 10:08:50 -07002312 @Override
2313 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002314 mUi.onActionModeStarted(mode);
2315 mActionMode = mode;
2316 }
2317
2318 /*
2319 * True if a custom ActionMode (i.e. find or select) is in use.
2320 */
2321 @Override
2322 public boolean isInCustomActionMode() {
2323 return mActionMode != null;
2324 }
2325
2326 /*
2327 * End the current ActionMode.
2328 */
2329 @Override
2330 public void endActionMode() {
2331 if (mActionMode != null) {
2332 mActionMode.finish();
2333 }
2334 }
2335
2336 /*
2337 * Called by find and select when they are finished. Replace title bars
2338 * as necessary.
2339 */
John Reck9c35b9c2012-05-30 10:08:50 -07002340 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002341 public void onActionModeFinished(ActionMode mode) {
2342 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002343 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002344 mActionMode = null;
2345 }
2346
2347 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002348 final Tab tab = getCurrentTab();
2349 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002350 }
2351
2352 // bookmark handling
2353
2354 /**
2355 * add the current page as a bookmark to the given folder id
2356 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002357 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002358 * bookmarked, and if it is, edit that bookmark. If false, and
2359 * the site is already bookmarked, do not attempt to edit the
2360 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002361 */
2362 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002363 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002364 WebView w = getCurrentTopWebView();
2365 if (w == null) {
2366 return null;
2367 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002368 Intent i = new Intent(mActivity,
2369 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002370 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2371 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2372 String touchIconUrl = w.getTouchIconUrl();
2373 if (touchIconUrl != null) {
2374 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2375 WebSettings settings = w.getSettings();
2376 if (settings != null) {
2377 i.putExtra(AddBookmarkPage.USER_AGENT,
2378 settings.getUserAgentString());
2379 }
2380 }
2381 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2382 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2383 getDesiredThumbnailHeight(mActivity)));
2384 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002385 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002386 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2387 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002388 // Put the dialog at the upper right of the screen, covering the
2389 // star on the title bar.
2390 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002391 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002392 }
2393
2394 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002395 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002396 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002397 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002398 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002399 }
2400
Vivek Sekharb54614f2014-05-01 19:03:37 -07002401 @Override
2402 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2403 boolean capture) {
2404 mUploadHandler = new UploadHandler(this);
2405 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2406 }
2407
Michael Kolb8233fac2010-10-26 16:08:53 -07002408 // thumbnails
2409
2410 /**
2411 * Return the desired width for thumbnail screenshots, which are stored in
2412 * the database, and used on the bookmarks screen.
2413 * @param context Context for finding out the density of the screen.
2414 * @return desired width for thumbnail screenshot.
2415 */
2416 static int getDesiredThumbnailWidth(Context context) {
2417 return context.getResources().getDimensionPixelOffset(
2418 R.dimen.bookmarkThumbnailWidth);
2419 }
2420
2421 /**
2422 * Return the desired height for thumbnail screenshots, which are stored in
2423 * the database, and used on the bookmarks screen.
2424 * @param context Context for finding out the density of the screen.
2425 * @return desired height for thumbnail screenshot.
2426 */
2427 static int getDesiredThumbnailHeight(Context context) {
2428 return context.getResources().getDimensionPixelOffset(
2429 R.dimen.bookmarkThumbnailHeight);
2430 }
2431
John Reck8cc92352011-07-06 17:41:52 -07002432 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002433 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002434 return null;
2435 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002436
2437 Bitmap viewportBitmap = view.getViewportBitmap();
2438 if (viewportBitmap == null) {
2439 return null;
2440 }
2441
2442 float aspectRatio = (float) width/height;
2443 int viewportWidth = viewportBitmap.getWidth();
2444 int viewportHeight = viewportBitmap.getHeight();
2445
2446 //modify the size to attain the same aspect ratio of desired thumbnail size
2447 if (viewportHeight > viewportWidth) {
Tarun Nainani0be3b7c2014-07-03 01:17:12 -07002448 viewportHeight= (int)Math.round(viewportWidth * (1/aspectRatio));
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002449 } else {
2450 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2451 }
2452
2453 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2454 Rect dstRect = new Rect(0, 0, width, height);
2455
2456 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2457 || sThumbnailBitmap.getHeight() != height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002458 if (sThumbnailBitmap != null) {
2459 sThumbnailBitmap.recycle();
2460 sThumbnailBitmap = null;
2461 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002462
2463 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002464 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002465
John Reckd7dd9b22011-08-30 09:18:29 -07002466 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002467 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
Vivek Sekhar0ab52512014-07-02 14:51:32 -07002468 if (viewportBitmap != null) {
2469 viewportBitmap.recycle();
2470 viewportBitmap = null;
2471 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002472 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002473 }
2474
John Reck34ef2672011-02-10 11:30:55 -08002475 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002476 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002477 // FIXME: Would like to make sure there is actually something to
2478 // draw, but the API for that (WebViewCore.pictureReady()) is not
2479 // currently accessible here.
2480
John Reck34ef2672011-02-10 11:30:55 -08002481 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002482 if (view == null) {
2483 // Tab was destroyed
2484 return;
2485 }
John Reck34ef2672011-02-10 11:30:55 -08002486 final String url = tab.getUrl();
2487 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002488 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002489 if (TextUtils.isEmpty(url)) {
2490 return;
2491 }
2492
kaiyiz6e5b3e02013-08-19 20:02:01 +08002493 //update My Navigation Thumbnails
2494 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2495 if (isMyNavigationUrl) {
2496 updateMyNavigationThumbnail(url, view);
2497 }
John Reck34ef2672011-02-10 11:30:55 -08002498 // Only update thumbnails for web urls (http(s)://), not for
2499 // about:, javascript:, data:, etc...
2500 // Unless it is a bookmarked site, then always update
2501 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2502 return;
2503 }
2504
kaiyiz6e5b3e02013-08-19 20:02:01 +08002505 if (url != null && mUpdateMyNavThumbnailUrl != null
2506 && Patterns.WEB_URL.matcher(url).matches()
2507 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2508 String urlHost = (new WebAddress(url)).getHost();
2509 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2510 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2511 || bookmarkHost.length() == 0) {
2512 return;
2513 }
2514 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2515 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2516 bookmarkHost.length());
2517 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2518 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2519 if (!bookmarkDomain.equals(urlDomain)) {
2520 return;
2521 }
2522 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002523 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2524 getDesiredThumbnailHeight(mActivity));
2525 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002526 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2527 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2528 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2529 500);
2530 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002531 return;
2532 }
2533
2534 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002535 new AsyncTask<Void, Void, Void>() {
2536 @Override
2537 protected Void doInBackground(Void... unused) {
2538 Cursor cursor = null;
2539 try {
2540 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002541 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2542 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2543 : url);
2544 if (mUpdateMyNavThumbnail) {
2545 mUpdateMyNavThumbnail = false;
2546 mUpdateMyNavThumbnailUrl = null;
2547 }
John Reck34ef2672011-02-10 11:30:55 -08002548 if (cursor != null && cursor.moveToFirst()) {
2549 final ByteArrayOutputStream os =
2550 new ByteArrayOutputStream();
2551 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002552
John Reck34ef2672011-02-10 11:30:55 -08002553 ContentValues values = new ContentValues();
2554 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002555
John Reck34ef2672011-02-10 11:30:55 -08002556 do {
John Reck617fd832011-02-16 14:35:59 -08002557 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002558 cr.update(Images.CONTENT_URI, values, null, null);
2559 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002560 }
John Reck34ef2672011-02-10 11:30:55 -08002561 } catch (IllegalStateException e) {
2562 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002563 } catch (SQLiteException s) {
2564 // Added for possible error when user tries to remove the same bookmark
2565 // that is being updated with a screen shot
2566 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002567 } finally {
2568 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002569 }
John Reck34ef2672011-02-10 11:30:55 -08002570 return null;
2571 }
2572 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002573 }
2574
2575 private class Copy implements OnMenuItemClickListener {
2576 private CharSequence mText;
2577
John Reck9c35b9c2012-05-30 10:08:50 -07002578 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002579 public boolean onMenuItemClick(MenuItem item) {
2580 copy(mText);
2581 return true;
2582 }
2583
2584 public Copy(CharSequence toCopy) {
2585 mText = toCopy;
2586 }
2587 }
2588
Leon Scroggins63c02662010-11-18 15:16:27 -05002589 private static class Download implements OnMenuItemClickListener {
2590 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002591 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002592 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002593 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002594 private static final String FALLBACK_EXTENSION = "dat";
2595 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002596
John Reck9c35b9c2012-05-30 10:08:50 -07002597 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002598 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002599 if (DataUri.isDataUri(mText)) {
2600 saveDataUri();
2601 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002602 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002603 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002604 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 return true;
2606 }
2607
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002608 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2609 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002610 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002611 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002612 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002613 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002614 }
George Mount387d45d2011-10-07 15:57:53 -07002615
2616 /**
2617 * Treats mText as a data URI and writes its contents to a file
2618 * based on the current time.
2619 */
2620 private void saveDataUri() {
2621 FileOutputStream outputStream = null;
2622 try {
2623 DataUri uri = new DataUri(mText);
2624 File target = getTarget(uri);
2625 outputStream = new FileOutputStream(target);
2626 outputStream.write(uri.getData());
2627 final DownloadManager manager =
2628 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2629 manager.addCompletedDownload(target.getName(),
2630 mActivity.getTitle().toString(), false,
2631 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002632 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002633 } catch (IOException e) {
2634 Log.e(LOGTAG, "Could not save data URL");
2635 } finally {
2636 if (outputStream != null) {
2637 try {
2638 outputStream.close();
2639 } catch (IOException e) {
2640 // ignore close errors
2641 }
2642 }
2643 }
2644 }
2645
2646 /**
2647 * Creates a File based on the current time stamp and uses
2648 * the mime type of the DataUri to get the extension.
2649 */
2650 private File getTarget(DataUri uri) throws IOException {
2651 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002652 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002653 String nameBase = format.format(new Date());
2654 String mimeType = uri.getMimeType();
2655 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2656 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2657 if (extension == null) {
2658 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2659 extension = FALLBACK_EXTENSION;
2660 }
2661 extension = "." + extension; // createTempFile needs the '.'
2662 File targetFile = File.createTempFile(nameBase, extension, dir);
2663 return targetFile;
2664 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002665 }
2666
Cary Clark8974d282010-11-22 10:46:05 -05002667 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002668 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002669
John Reck9c35b9c2012-05-30 10:08:50 -07002670 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002671 public boolean onMenuItemClick(MenuItem item) {
2672 if (mWebView != null) {
2673 return mWebView.selectText();
2674 }
2675 return false;
2676 }
2677
2678 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002679 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002680 }
2681
2682 }
2683
Michael Kolb8233fac2010-10-26 16:08:53 -07002684 /********************** TODO: UI stuff *****************************/
2685
2686 // these methods have been copied, they still need to be cleaned up
2687
2688 /****************** tabs ***************************************************/
2689
2690 // basic tab interactions:
2691
2692 // it is assumed that tabcontrol already knows about the tab
2693 protected void addTab(Tab tab) {
2694 mUi.addTab(tab);
2695 }
2696
2697 protected void removeTab(Tab tab) {
2698 mUi.removeTab(tab);
2699 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002700 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002701 }
2702
Michael Kolbf2055602011-04-09 17:20:03 -07002703 @Override
2704 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002705 // monkey protection against delayed start
2706 if (tab != null) {
2707 mTabControl.setCurrentTab(tab);
2708 // the tab is guaranteed to have a webview after setCurrentTab
2709 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002710 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002711 //Purge active tabs
2712 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002713 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002714 }
2715
John Reck8bcafc12011-08-29 16:43:02 -07002716 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002717 Tab current = mTabControl.getCurrentTab();
2718 if (current != null
2719 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002720 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002721 }
2722 }
2723
John Reck26b18322011-06-21 13:08:58 -07002724 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002725 // Dismiss the subwindow if applicable.
2726 dismissSubWindow(appTab);
2727 // Since we might kill the WebView, remove it from the
2728 // content view first.
2729 mUi.detachTab(appTab);
2730 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002731 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002732 // TODO: analyze why the remove and add are necessary
2733 mUi.attachTab(appTab);
2734 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002735 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002736 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 } else {
2738 // If the tab was the current tab, we have to attach
2739 // it to the view system again.
2740 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002741 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002742 }
2743 }
2744
2745 // Remove the sub window if it exists. Also called by TabControl when the
2746 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002747 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002748 public void dismissSubWindow(Tab tab) {
2749 removeSubWindow(tab);
2750 // dismiss the subwindow. This will destroy the WebView.
2751 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002752 WebView wv = getCurrentTopWebView();
2753 if (wv != null) {
2754 wv.requestFocus();
2755 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002756 }
2757
2758 @Override
2759 public void removeSubWindow(Tab t) {
2760 if (t.getSubWebView() != null) {
2761 mUi.removeSubWindow(t.getSubViewContainer());
2762 }
2763 }
2764
2765 @Override
2766 public void attachSubWindow(Tab tab) {
2767 if (tab.getSubWebView() != null) {
2768 mUi.attachSubWindow(tab.getSubViewContainer());
2769 getCurrentTopWebView().requestFocus();
2770 }
2771 }
2772
Mathew Inwood29721c22011-06-29 17:55:24 +01002773 private Tab showPreloadedTab(final UrlData urlData) {
2774 if (!urlData.isPreloaded()) {
2775 return null;
2776 }
2777 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2778 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2779 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002780 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002781 // Could not submit query. Fallback to regular tab creation
2782 tabControl.destroy();
2783 return null;
2784 }
2785 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002786 // check tab count and make room for new tab
2787 if (!mTabControl.canCreateNewTab()) {
2788 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2789 if (leastUsed != null) {
2790 closeTab(leastUsed);
2791 }
2792 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002793 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002794 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002795 mTabControl.addPreloadedTab(t);
2796 addTab(t);
2797 setActiveTab(t);
2798 return t;
2799 }
2800
Michael Kolb7bcafde2011-05-09 13:55:59 -07002801 // open a non inconito tab with the given url data
2802 // and set as active tab
2803 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002804 Tab tab = showPreloadedTab(urlData);
2805 if (tab == null) {
2806 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002807 if ((tab != null) && !urlData.isEmpty()) {
2808 loadUrlDataIn(tab, urlData);
2809 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002810 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002811 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002812 }
2813
Michael Kolb843510f2010-12-09 10:51:49 -08002814 @Override
2815 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002816 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002817 }
2818
Michael Kolb8233fac2010-10-26 16:08:53 -07002819 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002820 public Tab openIncognitoTab() {
2821 return openTab(INCOGNITO_URI, true, true, false);
2822 }
2823
2824 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002825 public Tab openTab(String url, boolean incognito, boolean setActive,
2826 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002827 return openTab(url, incognito, setActive, useCurrent, null);
2828 }
2829
2830 @Override
2831 public Tab openTab(String url, Tab parent, boolean setActive,
2832 boolean useCurrent) {
2833 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2834 setActive, useCurrent, parent);
2835 }
2836
2837 public Tab openTab(String url, boolean incognito, boolean setActive,
2838 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002839 Tab tab = createNewTab(incognito, setActive, useCurrent);
2840 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002841 if (parent != null && parent != tab) {
2842 parent.addChildTab(tab);
2843 }
Michael Kolb519d2282011-05-09 17:03:19 -07002844 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002845 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002846 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002847 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002848 return tab;
2849 }
2850
2851 // this method will attempt to create a new tab
2852 // incognito: private browsing tab
2853 // setActive: ste tab as current tab
2854 // useCurrent: if no new tab can be created, return current tab
2855 private Tab createNewTab(boolean incognito, boolean setActive,
2856 boolean useCurrent) {
2857 Tab tab = null;
2858 if (mTabControl.canCreateNewTab()) {
2859 tab = mTabControl.createNewTab(incognito);
2860 addTab(tab);
2861 if (setActive) {
2862 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002863 } else {
2864 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002865 }
2866 } else {
2867 if (useCurrent) {
2868 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002869 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002870 } else {
2871 mUi.showMaxTabsWarning();
2872 }
2873 }
2874 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002875 }
2876
John Reck2bc80422011-06-30 15:11:49 -07002877 @Override
2878 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2879 SnapshotTab tab = null;
2880 if (mTabControl.canCreateNewTab()) {
2881 tab = mTabControl.createSnapshotTab(snapshotId);
2882 addTab(tab);
2883 if (setActive) {
2884 setActiveTab(tab);
2885 }
2886 } else {
2887 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002888 }
2889 return tab;
2890 }
2891
Michael Kolb8233fac2010-10-26 16:08:53 -07002892 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002893 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002894 * @return boolean True if we successfully switched to a different tab. If
2895 * the indexth tab is null, or if that tab is the same as
2896 * the current one, return false.
2897 */
2898 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002899 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002900 Tab currentTab = mTabControl.getCurrentTab();
2901 if (tab == null || tab == currentTab) {
2902 return false;
2903 }
2904 setActiveTab(tab);
2905 return true;
2906 }
2907
2908 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002909 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002910 closeCurrentTab(false);
2911 }
2912
2913 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002914 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002915 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002916 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002917 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002918 return;
2919 }
Michael Kolbc831b632011-05-11 09:30:34 -07002920 final Tab current = mTabControl.getCurrentTab();
2921 final int pos = mTabControl.getCurrentPosition();
2922 Tab newTab = current.getParent();
2923 if (newTab == null) {
2924 newTab = mTabControl.getTab(pos + 1);
2925 if (newTab == null) {
2926 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002927 }
2928 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002929 if (andQuit) {
2930 mTabControl.setCurrentTab(newTab);
2931 closeTab(current);
2932 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002933 // Close window
2934 closeTab(current);
2935 }
2936 }
2937
2938 /**
2939 * Close the tab, remove its associated title bar, and adjust mTabControl's
2940 * current tab to a valid value.
2941 */
2942 @Override
2943 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002944 if (tab == mTabControl.getCurrentTab()) {
2945 closeCurrentTab();
2946 } else {
2947 removeTab(tab);
2948 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002949 }
2950
Afzal Najamd4e33312012-04-26 01:54:01 -04002951 /**
2952 * Close all tabs except the current one
2953 */
2954 @Override
2955 public void closeOtherTabs() {
2956 int inactiveTabs = mTabControl.getTabCount() - 1;
2957 for (int i = inactiveTabs; i >= 0; i--) {
2958 Tab tab = mTabControl.getTab(i);
2959 if (tab != mTabControl.getCurrentTab()) {
2960 removeTab(tab);
2961 }
2962 }
2963 }
2964
Michael Kolb8233fac2010-10-26 16:08:53 -07002965 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002966 protected void loadUrlFromContext(String url) {
2967 Tab tab = getCurrentTab();
2968 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002969 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002970 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002971 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002972 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002973 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002974 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002975 }
2976 }
2977 }
2978
2979 /**
2980 * Load the URL into the given WebView and update the title bar
2981 * to reflect the new load. Call this instead of WebView.loadUrl
2982 * directly.
2983 * @param view The WebView used to load url.
2984 * @param url The URL to load.
2985 */
John Reck71e51422011-07-01 16:49:28 -07002986 @Override
2987 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002988 loadUrl(tab, url, null);
2989 }
2990
2991 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2992 if (tab != null) {
2993 dismissSubWindow(tab);
2994 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002995 if (tab.hasCrashed) {
2996 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
2997 }
Michael Kolba53c9892011-10-05 13:31:40 -07002998 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002999 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003000 }
3001
3002 /**
3003 * Load UrlData into a Tab and update the title bar to reflect the new
3004 * load. Call this instead of UrlData.loadIn directly.
3005 * @param t The Tab used to load.
3006 * @param data The UrlData being loaded.
3007 */
3008 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003009 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003010 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003011 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003012 } else {
John Reck38b39652012-06-05 09:22:59 -07003013 if (t != null && data.mDisableUrlOverride) {
3014 t.disableUrlOverridingForLoad();
3015 }
John Reck26b18322011-06-21 13:08:58 -07003016 loadUrl(t, data.mUrl, data.mHeaders);
3017 }
3018 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003019 }
3020
John Reck30c714c2010-12-16 17:30:34 -08003021 @Override
3022 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003023 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003024 if (tab.canGoBack()) {
3025 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003026 } else {
John Reckef654f12011-07-12 16:42:08 -07003027 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003028 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003029 }
3030
3031 void goBackOnePageOrQuit() {
3032 Tab current = mTabControl.getCurrentTab();
3033 if (current == null) {
3034 /*
3035 * Instead of finishing the activity, simply push this to the back
3036 * of the stack and let ActivityManager to choose the foreground
3037 * activity. As BrowserActivity is singleTask, it will be always the
3038 * root of the task. So we can use either true or false for
3039 * moveTaskToBack().
3040 */
luxiaolb40014b2013-07-19 10:01:43 +08003041 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003042 return;
3043 }
John Reckef654f12011-07-12 16:42:08 -07003044 if (current.canGoBack()) {
3045 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003046 } else {
3047 // Check to see if we are closing a window that was created by
3048 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003049 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003050 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003051 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003052 // Now we close the other tab
3053 closeTab(current);
3054 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003055 /*
3056 * Instead of finishing the activity, simply push this to the back
3057 * of the stack and let ActivityManager to choose the foreground
3058 * activity. As BrowserActivity is singleTask, it will be always the
3059 * root of the task. So we can use either true or false for
3060 * moveTaskToBack().
3061 */
luxiaolb40014b2013-07-19 10:01:43 +08003062 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003063 }
3064 }
3065 }
3066
3067 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003068 * helper method for key handler
3069 * returns the current tab if it can't advance
3070 */
Michael Kolbc831b632011-05-11 09:30:34 -07003071 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003072 int pos = mTabControl.getCurrentPosition() + 1;
3073 if (pos >= mTabControl.getTabCount()) {
3074 pos = 0;
3075 }
3076 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003077 }
3078
3079 /**
3080 * helper method for key handler
3081 * returns the current tab if it can't advance
3082 */
Michael Kolbc831b632011-05-11 09:30:34 -07003083 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003084 int pos = mTabControl.getCurrentPosition() - 1;
3085 if ( pos < 0) {
3086 pos = mTabControl.getTabCount() - 1;
3087 }
3088 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003089 }
3090
John Reckbcef87f2012-02-03 14:58:34 -08003091 boolean isMenuOrCtrlKey(int keyCode) {
3092 return (KeyEvent.KEYCODE_MENU == keyCode)
3093 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3094 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3095 }
3096
Michael Kolb0035fad2011-03-14 13:25:28 -07003097 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003098 * handle key events in browser
3099 *
3100 * @param keyCode
3101 * @param event
3102 * @return true if handled, false to pass to super
3103 */
John Reck9c35b9c2012-05-30 10:08:50 -07003104 @Override
3105 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003106 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003107 // Even if MENU is already held down, we need to call to super to open
3108 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003109 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003110 mMenuIsDown = true;
3111 return false;
3112 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003113
Cary Clark8ff8c662010-12-29 15:03:05 -05003114 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003115 Tab tab = getCurrentTab();
3116 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003117
Cary Clark160bbb92011-01-10 11:17:07 -05003118 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3119 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003120
Michael Kolb8233fac2010-10-26 16:08:53 -07003121 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003122 case KeyEvent.KEYCODE_TAB:
3123 if (event.isCtrlPressed()) {
3124 if (event.isShiftPressed()) {
3125 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003126 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003127 } else {
3128 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003129 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003130 }
3131 return true;
3132 }
3133 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003134 case KeyEvent.KEYCODE_SPACE:
3135 // WebView/WebTextView handle the keys in the KeyDown. As
3136 // the Activity's shortcut keys are only handled when WebView
3137 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003138 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003139 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003140 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003141 pageDown();
3142 }
3143 return true;
3144 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003145 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003146 event.startTracking();
3147 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003148 case KeyEvent.KEYCODE_FORWARD:
3149 if (!noModifiers) break;
3150 tab.goForward();
3151 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003152 case KeyEvent.KEYCODE_DPAD_LEFT:
3153 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003154 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003155 return true;
3156 }
3157 break;
3158 case KeyEvent.KEYCODE_DPAD_RIGHT:
3159 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003160 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003161 return true;
3162 }
3163 break;
3164 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003165 if (ctrl) {
3166 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003167 return true;
3168 }
3169 break;
Michael Kolba4183062011-01-16 10:43:21 -08003170// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003171 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003172 if (ctrl ) {
3173 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003174 return true;
3175 }
3176 break;
Michael Kolba4183062011-01-16 10:43:21 -08003177// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003178// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003179// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003180// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003181// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003182// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003183// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003184// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003185// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003186// case KeyEvent.KEYCODE_M: // unused
3187// case KeyEvent.KEYCODE_N: // in Chrome: new window
3188// case KeyEvent.KEYCODE_O: // in Chrome: open file
3189// case KeyEvent.KEYCODE_P: // in Chrome: print page
3190// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003191// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003192// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3193 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003194 // we can't use the ctrl/shift flags, they check for
3195 // exclusive use of a modifier
3196 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003197 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003198 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003199 } else {
3200 openTabToHomePage();
3201 }
3202 return true;
3203 }
3204 break;
3205// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3206// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003207// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003208// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3209// case KeyEvent.KEYCODE_Y: // unused
3210// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003211 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003212 // it is a regular key and webview is not null
3213 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003214 }
3215
John Reck9c35b9c2012-05-30 10:08:50 -07003216 @Override
3217 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003218 switch(keyCode) {
3219 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003220 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003221 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003222 return true;
3223 }
3224 break;
3225 }
3226 return false;
3227 }
3228
John Reck9c35b9c2012-05-30 10:08:50 -07003229 @Override
3230 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003231 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003232 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003233 if (KeyEvent.KEYCODE_MENU == keyCode
3234 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003235 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003236 }
3237 }
Cary Clark160bbb92011-01-10 11:17:07 -05003238 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003239 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003240 case KeyEvent.KEYCODE_BACK:
3241 if (event.isTracking() && !event.isCanceled()) {
3242 onBackKey();
3243 return true;
3244 }
3245 break;
3246 }
3247 return false;
3248 }
3249
3250 public boolean isMenuDown() {
3251 return mMenuIsDown;
3252 }
3253
John Reck9c35b9c2012-05-30 10:08:50 -07003254 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003255 public void setupAutoFill(Message message) {
3256 // Open the settings activity at the AutoFill profile fragment so that
3257 // the user can create a new profile. When they return, we will dispatch
3258 // the message so that we can autofill the form using their new profile.
3259 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3260 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3261 AutoFillSettingsFragment.class.getName());
3262 mAutoFillSetupMessage = message;
3263 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3264 }
John Reckb3417f02011-01-14 11:01:05 -08003265
John Reck9c35b9c2012-05-30 10:08:50 -07003266 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003267 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003268 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003269 return true;
3270 }
3271
John Reck1cf4b792011-07-26 10:22:22 -07003272 @Override
3273 public boolean shouldCaptureThumbnails() {
3274 return mUi.shouldCaptureThumbnails();
3275 }
3276
Michael Kolbc3af0672011-08-09 10:24:41 -07003277 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003278 public boolean supportsVoice() {
3279 PackageManager pm = mActivity.getPackageManager();
3280 List activities = pm.queryIntentActivities(new Intent(
3281 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3282 return activities.size() != 0;
3283 }
3284
3285 @Override
3286 public void startVoiceRecognizer() {
3287 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003288 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003289 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3290 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3291 mActivity.startActivityForResult(voice, VOICE_RESULT);
3292 }
3293
3294 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003295 public void setBlockEvents(boolean block) {
3296 mBlockEvents = block;
3297 }
3298
John Reck9c35b9c2012-05-30 10:08:50 -07003299 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003300 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003301 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003302 }
3303
John Reck9c35b9c2012-05-30 10:08:50 -07003304 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003305 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003306 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003307 }
3308
John Reck9c35b9c2012-05-30 10:08:50 -07003309 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003310 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003311 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003312 }
3313
John Reck9c35b9c2012-05-30 10:08:50 -07003314 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003315 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003316 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003317 }
3318
John Reck9c35b9c2012-05-30 10:08:50 -07003319 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003320 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003321 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003322 }
3323
Michael Kolb8233fac2010-10-26 16:08:53 -07003324}