blob: 5f3402f0b09a112b39e08cb2a2ec4546069e179d [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) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700604 if (tab == null || tab.getWebView() == null)
605 return;
606
607 final Tab mytab = tab;
608 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
609 @Override
610 public void onReceiveValue(Bitmap bitmap) {
611 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
612 mytab.getFavicon(), bitmap);
613 }
614 };
615
616 createScreenshotAsync(
617 tab.getWebView(),
618 getDesiredThumbnailWidth(mActivity),
619 getDesiredThumbnailHeight(mActivity),
620 new ValueCallback<Bitmap>() {
621 @Override
622 public void onReceiveValue(Bitmap bitmap) {
623 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
624 mytab.getFavicon(), bitmap);
625 }
626 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800627 }
628
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 /**
630 * Share a page, providing the title, url, favicon, and a screenshot. Uses
631 * an {@link Intent} to launch the Activity chooser.
632 * @param c Context used to launch a new Activity.
633 * @param title Title of the page. Stored in the Intent with
634 * {@link Intent#EXTRA_SUBJECT}
635 * @param url URL of the page. Stored in the Intent with
636 * {@link Intent#EXTRA_TEXT}
637 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
638 * with {@link Browser#EXTRA_SHARE_FAVICON}
639 * @param screenshot Bitmap of a screenshot of the page. Stored in the
640 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
641 */
642 static final void sharePage(Context c, String title, String url,
643 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700644
645 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
646 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
647 R.layout.app_row, sDialog.getApps());
648 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700649 }
650
651 private void copy(CharSequence text) {
652 ClipboardManager cm = (ClipboardManager) mActivity
653 .getSystemService(Context.CLIPBOARD_SERVICE);
654 cm.setText(text);
655 }
656
657 // lifecycle
658
John Reck9c35b9c2012-05-30 10:08:50 -0700659 @Override
660 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800662 // update the menu in case of a locale change
663 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800664 if (mOptionsMenuOpen) {
665 mActivity.closeOptionsMenu();
666 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
667 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700668 if (mPageDialogsHandler != null) {
669 mPageDialogsHandler.onConfigurationChanged(config);
670 }
671 mUi.onConfigurationChanged(config);
672 }
673
674 @Override
675 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700676 if (!mUi.isWebShowing()) {
677 mUi.showWeb(false);
678 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 mIntentHandler.onNewIntent(intent);
680 }
681
John Reck9c35b9c2012-05-30 10:08:50 -0700682 @Override
683 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800684 if (mUi.isCustomViewShowing()) {
685 hideCustomView();
686 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 if (mActivityPaused) {
688 Log.e(LOGTAG, "BrowserActivity is already paused.");
689 return;
690 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700691 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800692 Tab tab = mTabControl.getCurrentTab();
693 if (tab != null) {
694 tab.pause();
695 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700696 if (mWakeLock == null) {
697 PowerManager pm = (PowerManager) mActivity
698 .getSystemService(Context.POWER_SERVICE);
699 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
700 }
Michael Kolb70976932010-11-30 11:34:01 -0800701 mWakeLock.acquire();
702 mHandler.sendMessageDelayed(mHandler
703 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
704 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 }
706 mUi.onPause();
707 mNetworkHandler.onPause();
708
709 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100710 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700711 }
712
John Reck9c35b9c2012-05-30 10:08:50 -0700713 @Override
714 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800716 Bundle saveState = createSaveState();
717
718 // crash recovery manages all save & restore state
719 mCrashRecoveryHandler.writeState(saveState);
720 mSettings.setLastRunPaused(true);
721 }
722
723 /**
724 * Save the current state to outState. Does not write the state to
725 * disk.
726 * @return Bundle containing the current state of all tabs.
727 */
728 /* package */ Bundle createSaveState() {
729 Bundle saveState = new Bundle();
730 mTabControl.saveState(saveState);
731 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700732 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800733 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700734 }
George Mount3636d0a2011-11-21 09:08:21 -0800735 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700736 }
737
John Reck9c35b9c2012-05-30 10:08:50 -0700738 @Override
739 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700740 if (!mActivityPaused) {
741 Log.e(LOGTAG, "BrowserActivity is already resumed.");
742 return;
743 }
George Mount3636d0a2011-11-21 09:08:21 -0800744 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800746 Tab current = mTabControl.getCurrentTab();
747 if (current != null) {
748 current.resume();
749 resumeWebViewTimers(current);
750 }
John Reckf57c0292011-07-21 18:15:39 -0700751 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200752
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 mUi.onResume();
754 mNetworkHandler.onResume();
755 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100756 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700757 if (mVoiceResult != null) {
758 mUi.onVoiceResult(mVoiceResult);
759 mVoiceResult = null;
760 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800761 if (current != null && current.hasCrashed) {
762 current.showCrashView();
763 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 }
765
John Reckf57c0292011-07-21 18:15:39 -0700766 private void releaseWakeLock() {
767 if (mWakeLock != null && mWakeLock.isHeld()) {
768 mHandler.removeMessages(RELEASE_WAKELOCK);
769 mWakeLock.release();
770 }
771 }
772
Michael Kolb70976932010-11-30 11:34:01 -0800773 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800774 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800775 * @param tab guaranteed non-null
776 */
777 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700778 boolean inLoad = tab.inPageLoad();
779 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
780 CookieSyncManager.getInstance().startSync();
781 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100782 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700783 }
784 }
785
Michael Kolb70976932010-11-30 11:34:01 -0800786 /**
787 * Pause all WebView timers using the WebView of the given tab
788 * @param tab
789 * @return true if the timers are paused or tab is null
790 */
791 private boolean pauseWebViewTimers(Tab tab) {
792 if (tab == null) {
793 return true;
794 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100796 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700797 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 }
Michael Kolb70976932010-11-30 11:34:01 -0800799 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 }
801
John Reck9c35b9c2012-05-30 10:08:50 -0700802 @Override
803 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800804 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
806 mUploadHandler = null;
807 }
808 if (mTabControl == null) return;
809 mUi.onDestroy();
810 // Remove the current tab and sub window
811 Tab t = mTabControl.getCurrentTab();
812 if (t != null) {
813 dismissSubWindow(t);
814 removeTab(t);
815 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500816 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700817 // Destroy all the tabs
818 mTabControl.destroy();
819 WebIconDatabase.getInstance().close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 }
821
822 protected boolean isActivityPaused() {
823 return mActivityPaused;
824 }
825
John Reck9c35b9c2012-05-30 10:08:50 -0700826 @Override
827 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700828 mTabControl.freeMemory();
829 }
830
831 @Override
832 public boolean shouldShowErrorConsole() {
833 return mShouldShowErrorConsole;
834 }
835
836 protected void setShouldShowErrorConsole(boolean show) {
837 if (show == mShouldShowErrorConsole) {
838 // Nothing to do.
839 return;
840 }
841 mShouldShowErrorConsole = show;
842 Tab t = mTabControl.getCurrentTab();
843 if (t == null) {
844 // There is no current tab so we cannot toggle the error console
845 return;
846 }
847 mUi.setShouldShowErrorConsole(t, show);
848 }
849
850 @Override
851 public void stopLoading() {
852 mLoadStopped = true;
853 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700855 if (w != null) {
856 w.stopLoading();
857 mUi.onPageStopped(tab);
858 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 }
860
861 boolean didUserStopLoading() {
862 return mLoadStopped;
863 }
864
kaiyiza016da12013-08-26 17:50:22 +0800865 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700866 final String reminderType = getContext().getResources().getString(
867 R.string.def_wifi_browser_interaction_remind_type);
868 final String selectionConnnection = getContext().getResources().getString(
869 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700870 final String wifiSelection = getContext().getResources().getString(
871 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700872
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700873 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
874 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700875 return;
876
kaiyiza016da12013-08-26 17:50:22 +0800877 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700878 Context.CONNECTIVITY_SERVICE);
879 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700880 WifiManager wifiMgr = (WifiManager) this.getContext()
881 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700882 if (networkInfo == null
883 || (networkInfo != null && (networkInfo.getType() !=
884 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700885 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
886 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700887 List<ScanResult> list = wifiMgr.getScanResults();
888 // Have no AP's for Wifi's fall back to data
889 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700890 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700891 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
892 this.getContext().startActivity(intent);
893 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700894 // Request to select Wifi AP
895 try {
896 Intent intent = new Intent(wifiSelection);
897 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
898 this.getContext().startActivity(intent);
899 } catch (Exception e) {
900 String err_msg = this.getContext().getString(
901 R.string.acivity_not_found, wifiSelection);
902 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
903 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700904 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700905 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800906 }
907 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700908
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 // WebViewController
910
911 @Override
John Reck324d4402011-01-11 16:56:42 -0800912 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700913
914 // We've started to load a new page. If there was a pending message
915 // to save a screenshot then we will now take the new page and save
916 // an incorrect screenshot. Therefore, remove any pending thumbnail
917 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700918 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700919
920 // reset sync timer to avoid sync starts during loading a page
921 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700922 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700923 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700924 boolean networkNotifier =
925 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700926 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700927 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800928 } else {
929 if (!mNetworkHandler.isNetworkUp()) {
930 view.setNetworkAvailable(false);
931 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700932 }
933
934 // when BrowserActivity just starts, onPageStarted may be called before
935 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
936 // to start the timer. As we won't switch tabs while an activity is in
937 // pause state, we can ensure calling resume and pause in pair.
938 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800939 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700940 }
941 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 endActionMode();
943
John Reck30c714c2010-12-16 17:30:34 -0800944 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700945
John Reck324d4402011-01-11 16:56:42 -0800946 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700947 // update the bookmark database for favicon
948 maybeUpdateFavicon(tab, null, url, favicon);
949
950 Performance.tracePageStart(url);
951
952 // Performance probe
953 if (false) {
954 Performance.onPageStarted();
955 }
956
957 }
958
959 @Override
John Reck324d4402011-01-11 16:56:42 -0800960 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700961 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800962 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200963
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 // Performance probe
965 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800966 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 }
968
Tarun Nainani8eb00912014-07-17 12:28:32 -0700969 tab.onPageFinished();
970
Michael Kolb8233fac2010-10-26 16:08:53 -0700971 Performance.tracePageFinished();
972 }
973
974 @Override
John Reck30c714c2010-12-16 17:30:34 -0800975 public void onProgressChanged(Tab tab) {
976 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700977
978 if (newProgress == 100) {
979 CookieSyncManager.getInstance().sync();
980 // onProgressChanged() may continue to be called after the main
981 // frame has finished loading, as any remaining sub frames continue
982 // to load. We'll only get called once though with newProgress as
983 // 100 when everything is loaded. (onPageFinished is called once
984 // when the main frame completes loading regardless of the state of
985 // any sub frames so calls to onProgressChanges may continue after
986 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800987 if (tab.inPageLoad()) {
988 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200989 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
990 // pause the WebView timer and release the wake lock if it is
991 // finished while BrowserActivity is in pause state.
992 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700993 }
John Reckd9862372012-02-21 15:04:50 -0800994 if (!tab.isPrivateBrowsingEnabled()
995 && !TextUtils.isEmpty(tab.getUrl())
996 && !tab.isSnapshot()) {
997 // Only update the bookmark screenshot if the user did not
998 // cancel the load early and there is not already
999 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001000 if (tab.shouldUpdateThumbnail() &&
1001 (tab.inForeground() && !didUserStopLoading()
1002 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001003 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1004 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1005 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001006 1500);
John Reckd9862372012-02-21 15:04:50 -08001007 }
1008 }
1009 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001010 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001011 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001012 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001013 // still loading
1014 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001015 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001016 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001017 }
1018 }
John Reck30c714c2010-12-16 17:30:34 -08001019 mUi.onProgressChanged(tab);
1020 }
1021
1022 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001023 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001024 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001025 }
1026
1027 @Override
1028 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001029 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001030 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001031 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001032 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1033 return;
1034 }
1035 // Update the title in the history database if not in private browsing mode
1036 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001037 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 }
1039 }
1040
1041 @Override
1042 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001043 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1045 }
1046
1047 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001048 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1049 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 }
1051
1052 @Override
1053 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1054 if (mMenuIsDown) {
1055 // only check shortcut key when MENU is held
1056 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1057 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001058 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001059 int keyCode = event.getKeyCode();
1060 // We need to send almost every key to WebKit. However:
1061 // 1. We don't want to block the device on the renderer for
1062 // some keys like menu, home, call.
1063 // 2. There are no WebKit equivalents for some of these keys
1064 // (see app/keyboard_codes_win.h)
1065 // Note that these are not the same set as KeyEvent.isSystemKey:
1066 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1067 if (keyCode == KeyEvent.KEYCODE_MENU ||
1068 keyCode == KeyEvent.KEYCODE_HOME ||
1069 keyCode == KeyEvent.KEYCODE_BACK ||
1070 keyCode == KeyEvent.KEYCODE_CALL ||
1071 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1072 keyCode == KeyEvent.KEYCODE_POWER ||
1073 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1074 keyCode == KeyEvent.KEYCODE_CAMERA ||
1075 keyCode == KeyEvent.KEYCODE_FOCUS ||
1076 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1077 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1078 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1079 return true;
1080 }
1081
1082 // We also have to intercept some shortcuts before we send them to the ContentView.
1083 if (event.isCtrlPressed() && (
1084 keyCode == KeyEvent.KEYCODE_TAB ||
1085 keyCode == KeyEvent.KEYCODE_W ||
1086 keyCode == KeyEvent.KEYCODE_F4)) {
1087 return true;
1088 }
1089
1090 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 }
1092
1093 @Override
John Reck997b1b72012-04-19 18:08:25 -07001094 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001095 if (!isActivityPaused()) {
1096 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001097 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001098 } else {
John Reck997b1b72012-04-19 18:08:25 -07001099 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001100 }
1101 }
John Reck997b1b72012-04-19 18:08:25 -07001102 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 }
1104
1105 @Override
John Reck324d4402011-01-11 16:56:42 -08001106 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001107 boolean disableHistoryWrites =
1108 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1109
1110 // Don't save anything in private browsing mode or when explicitly set
1111 // not to write history via an overlay
1112 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001113 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001114
John Reck324d4402011-01-11 16:56:42 -08001115 if (TextUtils.isEmpty(url)
1116 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 return;
1118 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001119
John Reckf57c0292011-07-21 18:15:39 -07001120 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001121 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 }
1123
1124 @Override
1125 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1126 AsyncTask<Void, Void, String[]> task =
1127 new AsyncTask<Void, Void, String[]>() {
1128 @Override
1129 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001130 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 }
1132 @Override
1133 public void onPostExecute(String[] result) {
1134 callback.onReceiveValue(result);
1135 }
1136 };
1137 task.execute();
1138 }
1139
1140 @Override
1141 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1142 final HttpAuthHandler handler, final String host,
1143 final String realm) {
1144 String username = null;
1145 String password = null;
1146
1147 boolean reuseHttpAuthUsernamePassword
1148 = handler.useHttpAuthUsernamePassword();
1149
1150 if (reuseHttpAuthUsernamePassword && view != null) {
1151 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1152 if (credentials != null && credentials.length == 2) {
1153 username = credentials[0];
1154 password = credentials[1];
1155 }
1156 }
1157
1158 if (username != null && password != null) {
1159 handler.proceed(username, password);
1160 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001161 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001162 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1163 } else {
1164 handler.cancel();
1165 }
1166 }
1167 }
1168
1169 @Override
1170 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001171 String contentDisposition, String mimetype, String referer,
1172 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001173 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001174 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001175 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001176 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001177 // This Tab was opened for the sole purpose of downloading a
1178 // file. Remove it.
1179 if (tab == mTabControl.getCurrentTab()) {
1180 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001181 if (tab.getDerivedFromIntent())
1182 closeTab(tab);
1183 else
1184 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 } else {
1186 // In this case, it is not.
1187 closeTab(tab);
1188 }
1189 }
1190 }
1191
1192 @Override
1193 public Bitmap getDefaultVideoPoster() {
1194 return mUi.getDefaultVideoPoster();
1195 }
1196
1197 @Override
1198 public View getVideoLoadingProgressView() {
1199 return mUi.getVideoLoadingProgressView();
1200 }
1201
1202 @Override
1203 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1204 SslError error) {
1205 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1206 }
1207
1208 // helper method
1209
1210 /*
1211 * Update the favorites icon if the private browsing isn't enabled and the
1212 * icon is valid.
1213 */
1214 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1215 final String url, Bitmap favicon) {
1216 if (favicon == null) {
1217 return;
1218 }
1219 if (!tab.isPrivateBrowsingEnabled()) {
1220 Bookmarks.updateFavicon(mActivity
1221 .getContentResolver(), originalUrl, url, favicon);
1222 }
1223 }
1224
Leon Scroggins4cd97792010-12-03 15:31:56 -05001225 @Override
1226 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001227 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001228 mUi.bookmarkedStatusHasChanged(tab);
1229 }
1230
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 // end WebViewController
1232
1233 protected void pageUp() {
1234 getCurrentTopWebView().pageUp(false);
1235 }
1236
1237 protected void pageDown() {
1238 getCurrentTopWebView().pageDown(false);
1239 }
1240
1241 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001242 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 public void editUrl() {
1244 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001245 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 }
1247
John Reck9c35b9c2012-05-30 10:08:50 -07001248 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001249 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001250 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 if (tab.inForeground()) {
1252 if (mUi.isCustomViewShowing()) {
1253 callback.onCustomViewHidden();
1254 return;
1255 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001256 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001257 // Save the menu state and set it to empty while the custom
1258 // view is showing.
1259 mOldMenuState = mMenuState;
1260 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001261 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 }
1263 }
1264
1265 @Override
1266 public void hideCustomView() {
1267 if (mUi.isCustomViewShowing()) {
1268 mUi.onHideCustomView();
1269 // Reset the old menu state.
1270 mMenuState = mOldMenuState;
1271 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001272 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 }
1274 }
1275
John Reck9c35b9c2012-05-30 10:08:50 -07001276 @Override
1277 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001278 Intent intent) {
1279 if (getCurrentTopWebView() == null) return;
1280 switch (requestCode) {
1281 case PREFERENCES_PAGE:
1282 if (resultCode == Activity.RESULT_OK && intent != null) {
1283 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001284 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001285 mTabControl.removeParentChildRelationShips();
1286 }
1287 }
1288 break;
1289 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001290 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001291 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001294 case AUTOFILL_SETUP:
1295 // Determine whether a profile was actually set up or not
1296 // and if so, send the message back to the WebTextView to
1297 // fill the form with the new profile.
1298 if (getSettings().getAutoFillProfile() != null) {
1299 mAutoFillSetupMessage.sendToTarget();
1300 mAutoFillSetupMessage = null;
1301 }
1302 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001303 case COMBO_VIEW:
1304 if (intent == null || resultCode != Activity.RESULT_OK) {
1305 break;
1306 }
John Reck3ba45532011-08-11 16:26:53 -07001307 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001308 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1309 Tab t = getCurrentTab();
1310 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001311 mUpdateMyNavThumbnail = true;
1312 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001313 loadUrl(t, uri.toString());
1314 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1315 String[] urls = intent.getStringArrayExtra(
1316 ComboViewActivity.EXTRA_OPEN_ALL);
1317 Tab parent = getCurrentTab();
1318 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001319 if (url != null) {
1320 parent = openTab(url, parent,
1321 !mSettings.openInBackground(), true);
1322 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001323 }
1324 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1325 long id = intent.getLongExtra(
1326 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1327 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001328 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001329 }
1330 }
1331 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001332 case VOICE_RESULT:
1333 if (resultCode == Activity.RESULT_OK && intent != null) {
1334 ArrayList<String> results = intent.getStringArrayListExtra(
1335 RecognizerIntent.EXTRA_RESULTS);
1336 if (results.size() >= 1) {
1337 mVoiceResult = results.get(0);
1338 }
1339 }
1340 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001341 case MY_NAVIGATION:
1342 if (intent == null || resultCode != Activity.RESULT_OK) {
1343 break;
1344 }
1345
1346 if (intent.getBooleanExtra("need_refresh", false) &&
1347 getCurrentTopWebView() != null) {
1348 getCurrentTopWebView().reload();
1349 }
1350 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001351 default:
1352 break;
1353 }
1354 getCurrentTopWebView().requestFocus();
1355 }
1356
1357 /**
1358 * Open the Go page.
1359 * @param startWithHistory If true, open starting on the history tab.
1360 * Otherwise, start with the bookmarks tab.
1361 */
1362 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001363 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 if (mTabControl.getCurrentWebView() == null) {
1365 return;
1366 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001367 // clear action mode
1368 if (isInCustomActionMode()) {
1369 endActionMode();
1370 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001371 Bundle extras = new Bundle();
1372 // Disable opening in a new window if we have maxed out the windows
1373 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1374 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001375 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001376 }
1377
1378 // combo view callbacks
1379
Michael Kolb8233fac2010-10-26 16:08:53 -07001380 // key handling
1381 protected void onBackKey() {
1382 if (!mUi.onBackKey()) {
1383 WebView subwindow = mTabControl.getCurrentSubWindow();
1384 if (subwindow != null) {
1385 if (subwindow.canGoBack()) {
1386 subwindow.goBack();
1387 } else {
1388 dismissSubWindow(mTabControl.getCurrentTab());
1389 }
1390 } else {
1391 goBackOnePageOrQuit();
1392 }
1393 }
1394 }
1395
Michael Kolb4bd767d2011-05-27 11:33:55 -07001396 protected boolean onMenuKey() {
1397 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001398 }
1399
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 // menu handling and state
1401 // TODO: maybe put into separate handler
1402
John Reck9c35b9c2012-05-30 10:08:50 -07001403 @Override
1404 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001405 if (mMenuState == EMPTY_MENU) {
1406 return false;
1407 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001408 MenuInflater inflater = mActivity.getMenuInflater();
1409 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 return true;
1411 }
1412
John Reck9c35b9c2012-05-30 10:08:50 -07001413 @Override
1414 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001415 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001416 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 return;
1418 }
1419 if (!(v instanceof WebView)) {
1420 return;
1421 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001422 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 WebView.HitTestResult result = webview.getHitTestResult();
1424 if (result == null) {
1425 return;
1426 }
1427
1428 int type = result.getType();
1429 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1430 Log.w(LOGTAG,
1431 "We should not show context menu when nothing is touched");
1432 return;
1433 }
1434 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1435 // let TextView handles context menu
1436 return;
1437 }
1438
1439 // Note, http://b/issue?id=1106666 is requesting that
1440 // an inflated menu can be used again. This is not available
1441 // yet, so inflate each time (yuk!)
1442 MenuInflater inflater = mActivity.getMenuInflater();
1443 inflater.inflate(R.menu.browsercontext, menu);
1444
1445 // Show the correct menu group
1446 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001447 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001448 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 menu.setGroupVisible(R.id.PHONE_MENU,
1450 type == WebView.HitTestResult.PHONE_TYPE);
1451 menu.setGroupVisible(R.id.EMAIL_MENU,
1452 type == WebView.HitTestResult.EMAIL_TYPE);
1453 menu.setGroupVisible(R.id.GEO_MENU,
1454 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001455 String itemUrl = null;
1456 String url = webview.getOriginalUrl();
1457 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1458 itemUrl = Uri.decode(navigationUrl);
1459 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1460 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1461 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1462 } else {
1463 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1464 }
1465 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1466 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1467 } else {
1468 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1469
1470 menu.setGroupVisible(R.id.IMAGE_MENU,
1471 type == WebView.HitTestResult.IMAGE_TYPE
1472 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1473 menu.setGroupVisible(R.id.ANCHOR_MENU,
1474 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1475 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001476 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1477 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001478 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001479 // Setup custom handling depending on the type
1480 switch (type) {
1481 case WebView.HitTestResult.PHONE_TYPE:
1482 menu.setHeaderTitle(Uri.decode(extra));
1483 menu.findItem(R.id.dial_context_menu_id).setIntent(
1484 new Intent(Intent.ACTION_VIEW, Uri
1485 .parse(WebView.SCHEME_TEL + extra)));
1486 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1487 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1488 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1489 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1490 addIntent);
1491 menu.findItem(R.id.copy_phone_context_menu_id)
1492 .setOnMenuItemClickListener(
1493 new Copy(extra));
1494 break;
1495
1496 case WebView.HitTestResult.EMAIL_TYPE:
1497 menu.setHeaderTitle(extra);
1498 menu.findItem(R.id.email_context_menu_id).setIntent(
1499 new Intent(Intent.ACTION_VIEW, Uri
1500 .parse(WebView.SCHEME_MAILTO + extra)));
1501 menu.findItem(R.id.copy_mail_context_menu_id)
1502 .setOnMenuItemClickListener(
1503 new Copy(extra));
1504 break;
1505
1506 case WebView.HitTestResult.GEO_TYPE:
1507 menu.setHeaderTitle(extra);
1508 menu.findItem(R.id.map_context_menu_id).setIntent(
1509 new Intent(Intent.ACTION_VIEW, Uri
1510 .parse(WebView.SCHEME_GEO
1511 + URLEncoder.encode(extra))));
1512 menu.findItem(R.id.copy_geo_context_menu_id)
1513 .setOnMenuItemClickListener(
1514 new Copy(extra));
1515 break;
1516
1517 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1518 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001519 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001520 // decide whether to show the open link in new tab option
1521 boolean showNewTab = mTabControl.canCreateNewTab();
1522 MenuItem newTabItem
1523 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001524 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001525 ? R.string.contextmenu_openlink_newwindow_background
1526 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001527 newTabItem.setVisible(showNewTab);
1528 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001529 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1530 newTabItem.setOnMenuItemClickListener(
1531 new MenuItem.OnMenuItemClickListener() {
1532 @Override
1533 public boolean onMenuItemClick(MenuItem item) {
1534 final HashMap<String, WebView> hrefMap =
1535 new HashMap<String, WebView>();
1536 hrefMap.put("webview", webview);
1537 final Message msg = mHandler.obtainMessage(
1538 FOCUS_NODE_HREF,
1539 R.id.open_newtab_context_menu_id,
1540 0, hrefMap);
1541 webview.requestFocusNodeHref(msg);
1542 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001543 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001544 });
1545 } else {
1546 newTabItem.setOnMenuItemClickListener(
1547 new MenuItem.OnMenuItemClickListener() {
1548 @Override
1549 public boolean onMenuItemClick(MenuItem item) {
1550 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001551 openTab(extra, parent,
1552 !mSettings.openInBackground(),
1553 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001554 return true;
1555 }
1556 });
1557 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001558 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001559 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1560 menu.setHeaderTitle(navigationUrl);
1561 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1562
1563 if (itemUrl != null) {
1564 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1565 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1566 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1567 @Override
1568 public boolean onMenuItemClick(MenuItem item) {
1569 final Intent intent = new Intent(Controller.this
1570 .getContext(),
1571 AddMyNavigationPage.class);
1572 Bundle bundle = new Bundle();
1573 String url = Uri.decode(navigationUrl);
1574 bundle.putBoolean("isAdding", false);
1575 bundle.putString("url", url);
1576 bundle.putString("name", getNameFromUrl(url));
1577 intent.putExtra("websites", bundle);
1578 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1579 return false;
1580 }
1581 });
1582 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1583 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1584 @Override
1585 public boolean onMenuItemClick(MenuItem item) {
1586 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1587 return false;
1588 }
1589 });
1590 }
1591 } else {
1592 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1593 }
1594 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001595 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1596 break;
1597 }
1598 // otherwise fall through to handle image part
1599 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001600 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1601 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1603 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001604 shareItem.setOnMenuItemClickListener(
1605 new MenuItem.OnMenuItemClickListener() {
1606 @Override
1607 public boolean onMenuItemClick(MenuItem item) {
1608 sharePage(mActivity, null, extra, null,
1609 null);
1610 return true;
1611 }
1612 }
1613 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001614 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001615 menu.findItem(R.id.view_image_context_menu_id)
1616 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1617 @Override
1618 public boolean onMenuItemClick(MenuItem item) {
1619 openTab(extra, mTabControl.getCurrentTab(), true, true);
1620 return false;
1621 }
1622 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001623 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1624 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1625 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001626 menu.findItem(R.id.set_wallpaper_context_menu_id).
1627 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1628 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 break;
1630
1631 default:
1632 Log.w(LOGTAG, "We should not get here.");
1633 break;
1634 }
1635 //update the ui
1636 mUi.onContextMenuCreated(menu);
1637 }
1638
kaiyiz6e5b3e02013-08-19 20:02:01 +08001639 public void startAddMyNavigation(String url) {
1640 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1641 Bundle bundle = new Bundle();
1642 bundle.putBoolean("isAdding", true);
1643 bundle.putString("url", url);
1644 bundle.putString("name", getNameFromUrl(url));
1645 intent.putExtra("websites", bundle);
1646 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1647 }
1648
1649 private void showMyNavigationDeleteDialog(final String itemUrl) {
1650 new AlertDialog.Builder(this.getContext())
1651 .setTitle(R.string.my_navigation_delete_label)
1652 .setIcon(android.R.drawable.ic_dialog_alert)
1653 .setMessage(R.string.my_navigation_delete_msg)
1654 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1655 @Override
1656 public void onClick(DialogInterface dialog, int whichButton) {
1657 deleteMyNavigationItem(itemUrl);
1658 }
1659 })
1660 .setNegativeButton(R.string.cancel, null)
1661 .show();
1662 }
1663
1664 private void deleteMyNavigationItem(final String itemUrl) {
1665 ContentResolver cr = this.getContext().getContentResolver();
1666 Cursor cursor = null;
1667
1668 try {
1669 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1670 new String[] {
1671 MyNavigationUtil.ID
1672 }, "url = ?", new String[] {
1673 itemUrl
1674 }, null);
1675 if (null != cursor && cursor.moveToFirst()) {
1676 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1677 cursor.getLong(0));
1678
1679 ContentValues values = new ContentValues();
1680 values.put(MyNavigationUtil.TITLE, "");
1681 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1682 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1683 ByteArrayOutputStream os = new ByteArrayOutputStream();
1684 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1685 R.raw.my_navigation_add);
1686 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1687 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1688 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1689 cr.update(uri, values, null, null);
1690 } else {
1691 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1692 }
1693 } catch (IllegalStateException e) {
1694 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1695 } finally {
1696 if (null != cursor) {
1697 cursor.close();
1698 }
1699 }
1700
1701 if (getCurrentTopWebView() != null) {
1702 getCurrentTopWebView().reload();
1703 }
1704 }
1705
1706 private String getNameFromUrl(String itemUrl) {
1707 ContentResolver cr = this.getContext().getContentResolver();
1708 Cursor cursor = null;
1709 String name = null;
1710
1711 try {
1712 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1713 new String[] {
1714 MyNavigationUtil.TITLE
1715 }, "url = ?", new String[] {
1716 itemUrl
1717 }, null);
1718 if (null != cursor && cursor.moveToFirst()) {
1719 name = cursor.getString(0);
1720 } else {
1721 Log.e(LOGTAG, "this item does not exist!");
1722 }
1723 } catch (IllegalStateException e) {
1724 Log.e(LOGTAG, "getNameFromUrl", e);
1725 } finally {
1726 if (null != cursor) {
1727 cursor.close();
1728 }
1729 }
1730 return name;
1731 }
1732
1733 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1734 int width = mActivity.getResources().getDimensionPixelOffset(
1735 R.dimen.myNavigationThumbnailWidth);
1736 int height = mActivity.getResources().getDimensionPixelOffset(
1737 R.dimen.myNavigationThumbnailHeight);
1738
1739 final Bitmap bm = createScreenshot(webView, width, height);
1740
1741 if (bm == null) {
1742 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1743 return;
1744 }
1745
1746 final ContentResolver cr = mActivity.getContentResolver();
1747 new AsyncTask<Void, Void, Void>() {
1748 @Override
1749 protected Void doInBackground(Void... unused) {
1750 ContentResolver cr = mActivity.getContentResolver();
1751 Cursor cursor = null;
1752 try {
1753 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1754 new String[] {
1755 MyNavigationUtil.ID
1756 }, "url = ?", new String[] {
1757 itemUrl
1758 }, null);
1759 if (null != cursor && cursor.moveToFirst()) {
1760 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1761 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1762
1763 ContentValues values = new ContentValues();
1764 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1765 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1766 cursor.getLong(0));
1767 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1768 cr.update(uri, values, null, null);
1769 os.close();
1770 }
1771 } catch (IllegalStateException e) {
1772 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1773 } catch (IOException e) {
1774 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1775 } finally {
1776 if (null != cursor) {
1777 cursor.close();
1778 }
1779 }
1780 return null;
1781 }
1782 }.execute();
1783 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001784 /**
1785 * As the menu can be open when loading state changes
1786 * we must manually update the state of the stop/reload menu
1787 * item
1788 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001789 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001790 if (menu == null) {
1791 return;
1792 }
1793 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001794 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001795 menu.findItem(R.id.stop_menu_id):
1796 menu.findItem(R.id.reload_menu_id);
1797 if (src != null) {
1798 dest.setIcon(src.getIcon());
1799 dest.setTitle(src.getTitle());
1800 }
1801 }
1802
John Reck9c35b9c2012-05-30 10:08:50 -07001803 @Override
1804 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001805 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001806 // hold on to the menu reference here; it is used by the page callbacks
1807 // to update the menu based on loading state
1808 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001809 // Note: setVisible will decide whether an item is visible; while
1810 // setEnabled() will decide whether an item is enabled, which also means
1811 // whether the matching shortcut key will function.
1812 switch (mMenuState) {
1813 case EMPTY_MENU:
1814 if (mCurrentMenuState != mMenuState) {
1815 menu.setGroupVisible(R.id.MAIN_MENU, false);
1816 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1817 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1818 }
1819 break;
1820 default:
1821 if (mCurrentMenuState != mMenuState) {
1822 menu.setGroupVisible(R.id.MAIN_MENU, true);
1823 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1824 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1825 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001826 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001827 break;
1828 }
1829 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001830 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001831 }
1832
Michael Kolb4bf79712011-07-14 14:18:12 -07001833 @Override
1834 public void updateMenuState(Tab tab, Menu menu) {
1835 boolean canGoBack = false;
1836 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001837 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001838 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001839 // Following flag is used to identify schemes for which the LIVE_MENU
1840 // items defined in res/menu/browser.xml should be enabled
1841 boolean isLiveScheme = false;
1842 boolean isPageFinished = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001843 if (tab != null) {
1844 canGoBack = tab.canGoBack();
1845 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001846 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001847 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001848 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
1849 isPageFinished = tab.getPageFinishedStatus();
Michael Kolb4bf79712011-07-14 14:18:12 -07001850 }
1851 final MenuItem back = menu.findItem(R.id.back_menu_id);
1852 back.setEnabled(canGoBack);
1853
1854 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001855
1856 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1857 forward.setEnabled(canGoForward);
1858
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001859 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1860 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001861 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001862 if (source != null && dest != null) {
1863 dest.setTitle(source.getTitle());
1864 dest.setIcon(source.getIcon());
1865 }
John Recke1a03a32011-09-14 17:04:16 -07001866 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001867
1868 // decide whether to show the share link option
1869 PackageManager pm = mActivity.getPackageManager();
1870 Intent send = new Intent(Intent.ACTION_SEND);
1871 send.setType("text/plain");
1872 ResolveInfo ri = pm.resolveActivity(send,
1873 PackageManager.MATCH_DEFAULT_ONLY);
1874 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1875
1876 boolean isNavDump = mSettings.enableNavDump();
1877 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1878 nav.setVisible(isNavDump);
1879 nav.setEnabled(isNavDump);
1880
1881 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001882 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1883 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001884 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001885 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001886 menu.setGroupEnabled(R.id.OFFLINE_READING, isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001887 // history and snapshots item are the members of COMBO menu group,
1888 // so if show history item, only make snapshots item invisible.
1889 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001890
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001891 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001892 }
1893
John Reck9c35b9c2012-05-30 10:08:50 -07001894 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001895 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001896 if (null == getCurrentTopWebView()) {
1897 return false;
1898 }
1899 if (mMenuIsDown) {
1900 // The shortcut action consumes the MENU. Even if it is still down,
1901 // it won't trigger the next shortcut action. In the case of the
1902 // shortcut action triggering a new activity, like Bookmarks, we
1903 // won't get onKeyUp for MENU. So it is important to reset it here.
1904 mMenuIsDown = false;
1905 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001906 if (mUi.onOptionsItemSelected(item)) {
1907 // ui callback handled it
1908 return true;
1909 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001910 switch (item.getItemId()) {
1911 // -- Main menu
1912 case R.id.new_tab_menu_id:
1913 openTabToHomePage();
1914 break;
1915
1916 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001917 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001918 break;
1919
Afzal Najamd4e33312012-04-26 01:54:01 -04001920 case R.id.close_other_tabs_id:
1921 closeOtherTabs();
1922 break;
1923
Michael Kolb8233fac2010-10-26 16:08:53 -07001924 case R.id.goto_menu_id:
1925 editUrl();
1926 break;
1927
1928 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001929 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1930 break;
1931
1932 case R.id.history_menu_id:
1933 bookmarksOrHistoryPicker(ComboViews.History);
1934 break;
1935
1936 case R.id.snapshots_menu_id:
1937 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 break;
1939
Michael Kolb8233fac2010-10-26 16:08:53 -07001940 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001941 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001942 break;
1943
1944 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001945 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001946 stopLoading();
1947 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001948 Tab currentTab = mTabControl.getCurrentTab();
1949 if (currentTab.hasCrashed) {
1950 currentTab.replaceCrashView(getCurrentTopWebView(),
1951 currentTab.getViewContainer());
1952 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001953 getCurrentTopWebView().reload();
1954 }
1955 break;
1956
1957 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001958 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001959 break;
1960
1961 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001962 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 break;
1964
1965 case R.id.close_menu_id:
1966 // Close the subwindow if it exists.
1967 if (mTabControl.getCurrentSubWindow() != null) {
1968 dismissSubWindow(mTabControl.getCurrentTab());
1969 break;
1970 }
1971 closeCurrentTab();
1972 break;
1973
kaiyiza8b6dbb2013-07-29 18:11:22 +08001974 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001975 Object[] params = { new String("persist.debug.browsermonkeytest")};
1976 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001977 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001978 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001979 if (ret != null && ret.equals("enable"))
1980 break;
luxiaolb40014b2013-07-19 10:01:43 +08001981 showExitDialog(mActivity);
1982 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 case R.id.homepage_menu_id:
1984 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001985 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001986 break;
1987
1988 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001989 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001990 break;
1991
1992 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001993 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 break;
1995
John Reck2bc80422011-06-30 15:11:49 -07001996 case R.id.save_snapshot_menu_id:
1997 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001998 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001999 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05002000 break;
2001
Michael Kolb8233fac2010-10-26 16:08:53 -07002002 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002003 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002004 break;
2005
John Recke1a03a32011-09-14 17:04:16 -07002006 case R.id.snapshot_go_live:
2007 goLive();
2008 return true;
2009
Michael Kolb8233fac2010-10-26 16:08:53 -07002010 case R.id.share_page_menu_id:
2011 Tab currentTab = mTabControl.getCurrentTab();
2012 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002013 return false;
2014 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002015 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002016 break;
2017
2018 case R.id.dump_nav_menu_id:
2019 getCurrentTopWebView().debugDump();
2020 break;
2021
Michael Kolb8233fac2010-10-26 16:08:53 -07002022 case R.id.zoom_in_menu_id:
2023 getCurrentTopWebView().zoomIn();
2024 break;
2025
2026 case R.id.zoom_out_menu_id:
2027 getCurrentTopWebView().zoomOut();
2028 break;
2029
2030 case R.id.view_downloads_menu_id:
2031 viewDownloads();
2032 break;
2033
John Reck42229bc2011-08-19 13:26:43 -07002034 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002035 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002036 break;
2037
Michael Kolb8233fac2010-10-26 16:08:53 -07002038 case R.id.window_one_menu_id:
2039 case R.id.window_two_menu_id:
2040 case R.id.window_three_menu_id:
2041 case R.id.window_four_menu_id:
2042 case R.id.window_five_menu_id:
2043 case R.id.window_six_menu_id:
2044 case R.id.window_seven_menu_id:
2045 case R.id.window_eight_menu_id:
2046 {
2047 int menuid = item.getItemId();
2048 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2049 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2050 Tab desiredTab = mTabControl.getTab(id);
2051 if (desiredTab != null &&
2052 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002053 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002054 }
2055 break;
2056 }
2057 }
2058 }
2059 break;
2060
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002061 case R.id.about_menu_id:
2062 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2063 builder.setTitle(R.string.about);
2064 builder.setCancelable(true);
2065 String ua = "";
2066 final WebView currentWebView = getCurrentWebView();
2067 if (currentWebView != null) {
2068 final WebSettings s = currentWebView.getSettings();
2069 if (s != null) {
2070 ua = s.getUserAgentString();
2071 }
2072 }
2073 builder.setMessage("Agent:" + ua);
2074 builder.setPositiveButton(android.R.string.ok, null);
2075 builder.create().show();
2076 break;
2077
Michael Kolb8233fac2010-10-26 16:08:53 -07002078 default:
2079 return false;
2080 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 return true;
2082 }
2083
John Reck68234a92012-04-19 15:27:12 -07002084 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2085 implements OnCancelListener {
2086
2087 private Tab mTab;
2088 private Dialog mProgressDialog;
2089 private ContentValues mValues;
2090
2091 private SaveSnapshotTask(Tab tab) {
2092 mTab = tab;
2093 }
2094
2095 @Override
2096 protected void onPreExecute() {
2097 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2098 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2099 true, true, this);
2100 mValues = mTab.createSnapshotValues();
2101 }
2102
2103 @Override
2104 protected Long doInBackground(Void... params) {
2105 if (!mTab.saveViewState(mValues)) {
2106 return null;
2107 }
2108 if (isCancelled()) {
2109 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2110 File file = mActivity.getFileStreamPath(path);
2111 if (!file.delete()) {
2112 file.deleteOnExit();
2113 }
2114 return null;
2115 }
2116 final ContentResolver cr = mActivity.getContentResolver();
2117 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2118 if (result == null) {
2119 return null;
2120 }
2121 long id = ContentUris.parseId(result);
2122 return id;
2123 }
2124
2125 @Override
2126 protected void onPostExecute(Long id) {
2127 if (isCancelled()) {
2128 return;
2129 }
2130 mProgressDialog.dismiss();
2131 if (id == null) {
2132 Toast.makeText(mActivity, R.string.snapshot_failed,
2133 Toast.LENGTH_SHORT).show();
2134 return;
2135 }
2136 Bundle b = new Bundle();
2137 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2138 mUi.showComboView(ComboViews.Snapshots, b);
2139 }
2140
2141 @Override
2142 public void onCancel(DialogInterface dialog) {
2143 cancel(true);
2144 }
2145 }
2146
Michael Kolb80f75082012-04-10 10:50:06 -07002147 @Override
2148 public void toggleUserAgent() {
2149 WebView web = getCurrentWebView();
2150 mSettings.toggleDesktopUseragent(web);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002151 web.reload();
Michael Kolb80f75082012-04-10 10:50:06 -07002152 }
2153
2154 @Override
2155 public void findOnPage() {
2156 getCurrentTopWebView().showFindDialog(null, true);
2157 }
2158
2159 @Override
2160 public void openPreferences() {
2161 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2162 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2163 getCurrentTopWebView().getUrl());
2164 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2165 }
2166
2167 @Override
2168 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002169 WebView w = getCurrentTopWebView();
2170 if (w == null)
2171 return;
2172
2173 final Intent i = createBookmarkCurrentPageIntent(false);
2174 createScreenshotAsync(
2175 w, getDesiredThumbnailWidth(mActivity),
2176 getDesiredThumbnailHeight(mActivity),
2177 new ValueCallback<Bitmap>() {
2178 @Override
2179 public void onReceiveValue(Bitmap bitmap) {
2180 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2181 mActivity.startActivity(i);
2182 }
2183 });
Michael Kolb80f75082012-04-10 10:50:06 -07002184 }
2185
John Recke1a03a32011-09-14 17:04:16 -07002186 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002187 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002188 String url = t.getLiveUrl();
2189 // destroy the old snapshot tab
2190 closeCurrentTab();
2191 Tab liveTab = createNewTab(false, true, false);
2192 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002193 }
2194
luxiaolb40014b2013-07-19 10:01:43 +08002195 private void showExitDialog(final Activity activity) {
2196 new AlertDialog.Builder(activity)
2197 .setTitle(R.string.exit_browser_title)
2198 .setIcon(android.R.drawable.ic_dialog_alert)
2199 .setMessage(R.string.exit_browser_msg)
2200 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2201 public void onClick(DialogInterface dialog, int which) {
2202 activity.moveTaskToBack(true);
2203 dialog.dismiss();
2204 }
2205 })
2206 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2207 public void onClick(DialogInterface dialog, int which) {
2208 activity.finish();
2209 mHandler.postDelayed(new Runnable() {
2210 @Override
2211 public void run() {
2212 // TODO Auto-generated method stub
2213 mCrashRecoveryHandler.clearState(true);
2214 int pid = android.os.Process.myPid();
2215 android.os.Process.killProcess(pid);
2216 }
2217 }, 300);
2218 dialog.dismiss();
2219 }
2220 })
2221 .show();
2222 }
Michael Kolb315d5022011-10-13 12:47:11 -07002223 @Override
2224 public void showPageInfo() {
2225 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2226 }
2227
John Reck9c35b9c2012-05-30 10:08:50 -07002228 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002229 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002230 // Let the History and Bookmark fragments handle menus they created.
2231 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2232 return false;
2233 }
2234
Michael Kolb8233fac2010-10-26 16:08:53 -07002235 int id = item.getItemId();
2236 boolean result = true;
2237 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002238 // -- Browser context menu
2239 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002240 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002241 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002242 case R.id.copy_link_context_menu_id:
2243 final WebView webView = getCurrentTopWebView();
2244 if (null == webView) {
2245 result = false;
2246 break;
2247 }
2248 final HashMap<String, WebView> hrefMap =
2249 new HashMap<String, WebView>();
2250 hrefMap.put("webview", webView);
2251 final Message msg = mHandler.obtainMessage(
2252 FOCUS_NODE_HREF, id, 0, hrefMap);
2253 webView.requestFocusNodeHref(msg);
2254 break;
2255
2256 default:
2257 // For other context menus
2258 result = onOptionsItemSelected(item);
2259 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002260 return result;
2261 }
2262
2263 /**
2264 * support programmatically opening the context menu
2265 */
2266 public void openContextMenu(View view) {
2267 mActivity.openContextMenu(view);
2268 }
2269
2270 /**
2271 * programmatically open the options menu
2272 */
2273 public void openOptionsMenu() {
2274 mActivity.openOptionsMenu();
2275 }
2276
John Reck9c35b9c2012-05-30 10:08:50 -07002277 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002278 public boolean onMenuOpened(int featureId, Menu menu) {
2279 if (mOptionsMenuOpen) {
2280 if (mConfigChanged) {
2281 // We do not need to make any changes to the state of the
2282 // title bar, since the only thing that happened was a
2283 // change in orientation
2284 mConfigChanged = false;
2285 } else {
2286 if (!mExtendedMenuOpen) {
2287 mExtendedMenuOpen = true;
2288 mUi.onExtendedMenuOpened();
2289 } else {
2290 // Switching the menu back to icon view, so show the
2291 // title bar once again.
2292 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002293 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002294 }
2295 }
2296 } else {
2297 // The options menu is closed, so open it, and show the title
2298 mOptionsMenuOpen = true;
2299 mConfigChanged = false;
2300 mExtendedMenuOpen = false;
2301 mUi.onOptionsMenuOpened();
2302 }
2303 return true;
2304 }
2305
John Reck9c35b9c2012-05-30 10:08:50 -07002306 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002307 public void onOptionsMenuClosed(Menu menu) {
2308 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002309 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002310 }
2311
John Reck9c35b9c2012-05-30 10:08:50 -07002312 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002313 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002314 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002315 }
2316
2317 // Helper method for getting the top window.
2318 @Override
2319 public WebView getCurrentTopWebView() {
2320 return mTabControl.getCurrentTopWebView();
2321 }
2322
2323 @Override
2324 public WebView getCurrentWebView() {
2325 return mTabControl.getCurrentWebView();
2326 }
2327
2328 /*
2329 * This method is called as a result of the user selecting the options
2330 * menu to see the download window. It shows the download window on top of
2331 * the current window.
2332 */
2333 void viewDownloads() {
2334 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2335 mActivity.startActivity(intent);
2336 }
2337
John Reck30b065e2011-07-19 10:58:05 -07002338 int getActionModeHeight() {
2339 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2340 new int[] { android.R.attr.actionBarSize });
2341 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2342 actionBarSizeTypedArray.recycle();
2343 return size;
2344 }
2345
Michael Kolb8233fac2010-10-26 16:08:53 -07002346 // action mode
2347
John Reck9c35b9c2012-05-30 10:08:50 -07002348 @Override
2349 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002350 mUi.onActionModeStarted(mode);
2351 mActionMode = mode;
2352 }
2353
2354 /*
2355 * True if a custom ActionMode (i.e. find or select) is in use.
2356 */
2357 @Override
2358 public boolean isInCustomActionMode() {
2359 return mActionMode != null;
2360 }
2361
2362 /*
2363 * End the current ActionMode.
2364 */
2365 @Override
2366 public void endActionMode() {
2367 if (mActionMode != null) {
2368 mActionMode.finish();
2369 }
2370 }
2371
2372 /*
2373 * Called by find and select when they are finished. Replace title bars
2374 * as necessary.
2375 */
John Reck9c35b9c2012-05-30 10:08:50 -07002376 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002377 public void onActionModeFinished(ActionMode mode) {
2378 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002379 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002380 mActionMode = null;
2381 }
2382
2383 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002384 final Tab tab = getCurrentTab();
2385 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002386 }
2387
2388 // bookmark handling
2389
2390 /**
2391 * add the current page as a bookmark to the given folder id
2392 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002393 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002394 * bookmarked, and if it is, edit that bookmark. If false, and
2395 * the site is already bookmarked, do not attempt to edit the
2396 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002397 */
2398 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002399 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002400 WebView w = getCurrentTopWebView();
2401 if (w == null) {
2402 return null;
2403 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002404 Intent i = new Intent(mActivity,
2405 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002406 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2407 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2408 String touchIconUrl = w.getTouchIconUrl();
2409 if (touchIconUrl != null) {
2410 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2411 WebSettings settings = w.getSettings();
2412 if (settings != null) {
2413 i.putExtra(AddBookmarkPage.USER_AGENT,
2414 settings.getUserAgentString());
2415 }
2416 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002417 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002418 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002419 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002420 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2421 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 // Put the dialog at the upper right of the screen, covering the
2423 // star on the title bar.
2424 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002425 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002426 }
2427
2428 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002429 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002430 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002431 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002432 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 }
2434
Vivek Sekharb54614f2014-05-01 19:03:37 -07002435 @Override
2436 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2437 boolean capture) {
2438 mUploadHandler = new UploadHandler(this);
2439 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2440 }
2441
Michael Kolb8233fac2010-10-26 16:08:53 -07002442 // thumbnails
2443
2444 /**
2445 * Return the desired width for thumbnail screenshots, which are stored in
2446 * the database, and used on the bookmarks screen.
2447 * @param context Context for finding out the density of the screen.
2448 * @return desired width for thumbnail screenshot.
2449 */
2450 static int getDesiredThumbnailWidth(Context context) {
2451 return context.getResources().getDimensionPixelOffset(
2452 R.dimen.bookmarkThumbnailWidth);
2453 }
2454
2455 /**
2456 * Return the desired height for thumbnail screenshots, which are stored in
2457 * the database, and used on the bookmarks screen.
2458 * @param context Context for finding out the density of the screen.
2459 * @return desired height for thumbnail screenshot.
2460 */
2461 static int getDesiredThumbnailHeight(Context context) {
2462 return context.getResources().getDimensionPixelOffset(
2463 R.dimen.bookmarkThumbnailHeight);
2464 }
2465
John Reck8cc92352011-07-06 17:41:52 -07002466 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002467 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002468 return null;
2469 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002470
2471 Bitmap viewportBitmap = view.getViewportBitmap();
2472 if (viewportBitmap == null) {
2473 return null;
2474 }
2475
2476 float aspectRatio = (float) width/height;
2477 int viewportWidth = viewportBitmap.getWidth();
2478 int viewportHeight = viewportBitmap.getHeight();
2479
2480 //modify the size to attain the same aspect ratio of desired thumbnail size
2481 if (viewportHeight > viewportWidth) {
Tarun Nainani0be3b7c2014-07-03 01:17:12 -07002482 viewportHeight= (int)Math.round(viewportWidth * (1/aspectRatio));
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002483 } else {
2484 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2485 }
2486
2487 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2488 Rect dstRect = new Rect(0, 0, width, height);
2489
2490 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2491 || sThumbnailBitmap.getHeight() != height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002492 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002493 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002494
John Reckd7dd9b22011-08-30 09:18:29 -07002495 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002496 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
Vivek Sekhar0ab52512014-07-02 14:51:32 -07002497 if (viewportBitmap != null) {
2498 viewportBitmap.recycle();
2499 viewportBitmap = null;
2500 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002501 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002502 }
2503
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002504 static void createScreenshotAsync(WebView view, int width, int height,
2505 final ValueCallback<Bitmap> cb) {
2506 if (view == null || width == 0 || height == 0) {
2507 return;
2508 }
2509
2510 view.getContentBitmapAsync(
2511 (float) width / view.getWidth(),
2512 new Rect(),
2513 new ValueCallback<Bitmap>() {
2514 @Override
2515 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002516 if (bitmap != null)
2517 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2518 cb.onReceiveValue(bitmap);
2519 }});
2520 }
2521
2522
John Reck34ef2672011-02-10 11:30:55 -08002523 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002524 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 // FIXME: Would like to make sure there is actually something to
2526 // draw, but the API for that (WebViewCore.pictureReady()) is not
2527 // currently accessible here.
2528
John Reck34ef2672011-02-10 11:30:55 -08002529 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002530 if (view == null) {
2531 // Tab was destroyed
2532 return;
2533 }
John Reck34ef2672011-02-10 11:30:55 -08002534 final String url = tab.getUrl();
2535 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002536 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002537 if (TextUtils.isEmpty(url)) {
2538 return;
2539 }
2540
kaiyiz6e5b3e02013-08-19 20:02:01 +08002541 //update My Navigation Thumbnails
2542 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2543 if (isMyNavigationUrl) {
2544 updateMyNavigationThumbnail(url, view);
2545 }
John Reck34ef2672011-02-10 11:30:55 -08002546 // Only update thumbnails for web urls (http(s)://), not for
2547 // about:, javascript:, data:, etc...
2548 // Unless it is a bookmarked site, then always update
2549 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2550 return;
2551 }
2552
kaiyiz6e5b3e02013-08-19 20:02:01 +08002553 if (url != null && mUpdateMyNavThumbnailUrl != null
2554 && Patterns.WEB_URL.matcher(url).matches()
2555 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2556 String urlHost = (new WebAddress(url)).getHost();
2557 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2558 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2559 || bookmarkHost.length() == 0) {
2560 return;
2561 }
2562 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2563 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2564 bookmarkHost.length());
2565 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2566 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2567 if (!bookmarkDomain.equals(urlDomain)) {
2568 return;
2569 }
2570 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002571 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2572 getDesiredThumbnailHeight(mActivity));
2573 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002574 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2575 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2576 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2577 500);
2578 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002579 return;
2580 }
2581
2582 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002583 new AsyncTask<Void, Void, Void>() {
2584 @Override
2585 protected Void doInBackground(Void... unused) {
2586 Cursor cursor = null;
2587 try {
2588 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002589 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2590 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2591 : url);
2592 if (mUpdateMyNavThumbnail) {
2593 mUpdateMyNavThumbnail = false;
2594 mUpdateMyNavThumbnailUrl = null;
2595 }
John Reck34ef2672011-02-10 11:30:55 -08002596 if (cursor != null && cursor.moveToFirst()) {
2597 final ByteArrayOutputStream os =
2598 new ByteArrayOutputStream();
2599 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002600
John Reck34ef2672011-02-10 11:30:55 -08002601 ContentValues values = new ContentValues();
2602 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002603
John Reck34ef2672011-02-10 11:30:55 -08002604 do {
John Reck617fd832011-02-16 14:35:59 -08002605 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002606 cr.update(Images.CONTENT_URI, values, null, null);
2607 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 }
John Reck34ef2672011-02-10 11:30:55 -08002609 } catch (IllegalStateException e) {
2610 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002611 } catch (SQLiteException s) {
2612 // Added for possible error when user tries to remove the same bookmark
2613 // that is being updated with a screen shot
2614 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002615 } finally {
2616 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002617 }
John Reck34ef2672011-02-10 11:30:55 -08002618 return null;
2619 }
2620 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002621 }
2622
2623 private class Copy implements OnMenuItemClickListener {
2624 private CharSequence mText;
2625
John Reck9c35b9c2012-05-30 10:08:50 -07002626 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002627 public boolean onMenuItemClick(MenuItem item) {
2628 copy(mText);
2629 return true;
2630 }
2631
2632 public Copy(CharSequence toCopy) {
2633 mText = toCopy;
2634 }
2635 }
2636
Leon Scroggins63c02662010-11-18 15:16:27 -05002637 private static class Download implements OnMenuItemClickListener {
2638 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002639 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002640 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002641 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002642 private static final String FALLBACK_EXTENSION = "dat";
2643 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002644
John Reck9c35b9c2012-05-30 10:08:50 -07002645 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002646 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002647 if (DataUri.isDataUri(mText)) {
2648 saveDataUri();
2649 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002650 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002651 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002652 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002653 return true;
2654 }
2655
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002656 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2657 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002658 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002659 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002660 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002661 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002662 }
George Mount387d45d2011-10-07 15:57:53 -07002663
2664 /**
2665 * Treats mText as a data URI and writes its contents to a file
2666 * based on the current time.
2667 */
2668 private void saveDataUri() {
2669 FileOutputStream outputStream = null;
2670 try {
2671 DataUri uri = new DataUri(mText);
2672 File target = getTarget(uri);
2673 outputStream = new FileOutputStream(target);
2674 outputStream.write(uri.getData());
2675 final DownloadManager manager =
2676 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2677 manager.addCompletedDownload(target.getName(),
2678 mActivity.getTitle().toString(), false,
2679 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002680 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002681 } catch (IOException e) {
2682 Log.e(LOGTAG, "Could not save data URL");
2683 } finally {
2684 if (outputStream != null) {
2685 try {
2686 outputStream.close();
2687 } catch (IOException e) {
2688 // ignore close errors
2689 }
2690 }
2691 }
2692 }
2693
2694 /**
2695 * Creates a File based on the current time stamp and uses
2696 * the mime type of the DataUri to get the extension.
2697 */
2698 private File getTarget(DataUri uri) throws IOException {
2699 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002700 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002701 String nameBase = format.format(new Date());
2702 String mimeType = uri.getMimeType();
2703 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2704 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2705 if (extension == null) {
2706 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2707 extension = FALLBACK_EXTENSION;
2708 }
2709 extension = "." + extension; // createTempFile needs the '.'
2710 File targetFile = File.createTempFile(nameBase, extension, dir);
2711 return targetFile;
2712 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002713 }
2714
Cary Clark8974d282010-11-22 10:46:05 -05002715 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002716 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002717
John Reck9c35b9c2012-05-30 10:08:50 -07002718 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002719 public boolean onMenuItemClick(MenuItem item) {
2720 if (mWebView != null) {
2721 return mWebView.selectText();
2722 }
2723 return false;
2724 }
2725
2726 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002727 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002728 }
2729
2730 }
2731
Michael Kolb8233fac2010-10-26 16:08:53 -07002732 /********************** TODO: UI stuff *****************************/
2733
2734 // these methods have been copied, they still need to be cleaned up
2735
2736 /****************** tabs ***************************************************/
2737
2738 // basic tab interactions:
2739
2740 // it is assumed that tabcontrol already knows about the tab
2741 protected void addTab(Tab tab) {
2742 mUi.addTab(tab);
2743 }
2744
2745 protected void removeTab(Tab tab) {
2746 mUi.removeTab(tab);
2747 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002748 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002749 }
2750
Michael Kolbf2055602011-04-09 17:20:03 -07002751 @Override
2752 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002753 // monkey protection against delayed start
2754 if (tab != null) {
2755 mTabControl.setCurrentTab(tab);
2756 // the tab is guaranteed to have a webview after setCurrentTab
2757 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002758 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002759 //Purge active tabs
2760 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002761 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002762 }
2763
John Reck8bcafc12011-08-29 16:43:02 -07002764 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002765 Tab current = mTabControl.getCurrentTab();
2766 if (current != null
2767 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002768 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002769 }
2770 }
2771
John Reck26b18322011-06-21 13:08:58 -07002772 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002773 // Dismiss the subwindow if applicable.
2774 dismissSubWindow(appTab);
2775 // Since we might kill the WebView, remove it from the
2776 // content view first.
2777 mUi.detachTab(appTab);
2778 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002779 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002780 // TODO: analyze why the remove and add are necessary
2781 mUi.attachTab(appTab);
2782 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002783 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002784 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002785 } else {
2786 // If the tab was the current tab, we have to attach
2787 // it to the view system again.
2788 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002789 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002790 }
2791 }
2792
2793 // Remove the sub window if it exists. Also called by TabControl when the
2794 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002795 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002796 public void dismissSubWindow(Tab tab) {
2797 removeSubWindow(tab);
2798 // dismiss the subwindow. This will destroy the WebView.
2799 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002800 WebView wv = getCurrentTopWebView();
2801 if (wv != null) {
2802 wv.requestFocus();
2803 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002804 }
2805
2806 @Override
2807 public void removeSubWindow(Tab t) {
2808 if (t.getSubWebView() != null) {
2809 mUi.removeSubWindow(t.getSubViewContainer());
2810 }
2811 }
2812
2813 @Override
2814 public void attachSubWindow(Tab tab) {
2815 if (tab.getSubWebView() != null) {
2816 mUi.attachSubWindow(tab.getSubViewContainer());
2817 getCurrentTopWebView().requestFocus();
2818 }
2819 }
2820
Mathew Inwood29721c22011-06-29 17:55:24 +01002821 private Tab showPreloadedTab(final UrlData urlData) {
2822 if (!urlData.isPreloaded()) {
2823 return null;
2824 }
2825 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2826 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2827 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002828 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002829 // Could not submit query. Fallback to regular tab creation
2830 tabControl.destroy();
2831 return null;
2832 }
2833 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002834 // check tab count and make room for new tab
2835 if (!mTabControl.canCreateNewTab()) {
2836 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2837 if (leastUsed != null) {
2838 closeTab(leastUsed);
2839 }
2840 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002841 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002842 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002843 mTabControl.addPreloadedTab(t);
2844 addTab(t);
2845 setActiveTab(t);
2846 return t;
2847 }
2848
Michael Kolb7bcafde2011-05-09 13:55:59 -07002849 // open a non inconito tab with the given url data
2850 // and set as active tab
2851 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002852 Tab tab = showPreloadedTab(urlData);
2853 if (tab == null) {
2854 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002855 if ((tab != null) && !urlData.isEmpty()) {
2856 loadUrlDataIn(tab, urlData);
2857 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002858 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002859 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002860 }
2861
Michael Kolb843510f2010-12-09 10:51:49 -08002862 @Override
2863 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002864 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002865 }
2866
Michael Kolb8233fac2010-10-26 16:08:53 -07002867 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002868 public Tab openIncognitoTab() {
2869 return openTab(INCOGNITO_URI, true, true, false);
2870 }
2871
2872 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002873 public Tab openTab(String url, boolean incognito, boolean setActive,
2874 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002875 return openTab(url, incognito, setActive, useCurrent, null);
2876 }
2877
2878 @Override
2879 public Tab openTab(String url, Tab parent, boolean setActive,
2880 boolean useCurrent) {
2881 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2882 setActive, useCurrent, parent);
2883 }
2884
2885 public Tab openTab(String url, boolean incognito, boolean setActive,
2886 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002887 Tab tab = createNewTab(incognito, setActive, useCurrent);
2888 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002889 if (parent instanceof SnapshotTab) {
2890 addTab(tab);
2891 if (setActive)
2892 setActiveTab(tab);
2893 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002894 parent.addChildTab(tab);
2895 }
Michael Kolb519d2282011-05-09 17:03:19 -07002896 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002897 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002898 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002899 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002900 return tab;
2901 }
2902
2903 // this method will attempt to create a new tab
2904 // incognito: private browsing tab
2905 // setActive: ste tab as current tab
2906 // useCurrent: if no new tab can be created, return current tab
2907 private Tab createNewTab(boolean incognito, boolean setActive,
2908 boolean useCurrent) {
2909 Tab tab = null;
2910 if (mTabControl.canCreateNewTab()) {
2911 tab = mTabControl.createNewTab(incognito);
2912 addTab(tab);
2913 if (setActive) {
2914 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002915 } else {
2916 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002917 }
2918 } else {
2919 if (useCurrent) {
2920 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002921 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002922 } else {
2923 mUi.showMaxTabsWarning();
2924 }
2925 }
2926 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002927 }
2928
John Reck2bc80422011-06-30 15:11:49 -07002929 @Override
2930 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2931 SnapshotTab tab = null;
2932 if (mTabControl.canCreateNewTab()) {
2933 tab = mTabControl.createSnapshotTab(snapshotId);
2934 addTab(tab);
2935 if (setActive) {
2936 setActiveTab(tab);
2937 }
2938 } else {
2939 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002940 }
2941 return tab;
2942 }
2943
Michael Kolb8233fac2010-10-26 16:08:53 -07002944 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002945 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002946 * @return boolean True if we successfully switched to a different tab. If
2947 * the indexth tab is null, or if that tab is the same as
2948 * the current one, return false.
2949 */
2950 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002951 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002952 Tab currentTab = mTabControl.getCurrentTab();
2953 if (tab == null || tab == currentTab) {
2954 return false;
2955 }
2956 setActiveTab(tab);
2957 return true;
2958 }
2959
2960 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002961 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002962 closeCurrentTab(false);
2963 }
2964
2965 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002966 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002967 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002968 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002969 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002970 return;
2971 }
Michael Kolbc831b632011-05-11 09:30:34 -07002972 final Tab current = mTabControl.getCurrentTab();
2973 final int pos = mTabControl.getCurrentPosition();
2974 Tab newTab = current.getParent();
2975 if (newTab == null) {
2976 newTab = mTabControl.getTab(pos + 1);
2977 if (newTab == null) {
2978 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002979 }
2980 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002981 if (andQuit) {
2982 mTabControl.setCurrentTab(newTab);
2983 closeTab(current);
2984 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002985 // Close window
2986 closeTab(current);
2987 }
2988 }
2989
2990 /**
2991 * Close the tab, remove its associated title bar, and adjust mTabControl's
2992 * current tab to a valid value.
2993 */
2994 @Override
2995 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002996 if (tab == mTabControl.getCurrentTab()) {
2997 closeCurrentTab();
2998 } else {
2999 removeTab(tab);
3000 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003001 }
3002
Afzal Najamd4e33312012-04-26 01:54:01 -04003003 /**
3004 * Close all tabs except the current one
3005 */
3006 @Override
3007 public void closeOtherTabs() {
3008 int inactiveTabs = mTabControl.getTabCount() - 1;
3009 for (int i = inactiveTabs; i >= 0; i--) {
3010 Tab tab = mTabControl.getTab(i);
3011 if (tab != mTabControl.getCurrentTab()) {
3012 removeTab(tab);
3013 }
3014 }
3015 }
3016
Michael Kolb8233fac2010-10-26 16:08:53 -07003017 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003018 protected void loadUrlFromContext(String url) {
3019 Tab tab = getCurrentTab();
3020 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003021 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003022 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003023 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003024 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003025 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003026 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003027 }
3028 }
3029 }
3030
3031 /**
3032 * Load the URL into the given WebView and update the title bar
3033 * to reflect the new load. Call this instead of WebView.loadUrl
3034 * directly.
3035 * @param view The WebView used to load url.
3036 * @param url The URL to load.
3037 */
John Reck71e51422011-07-01 16:49:28 -07003038 @Override
3039 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003040 loadUrl(tab, url, null);
3041 }
3042
3043 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3044 if (tab != null) {
3045 dismissSubWindow(tab);
3046 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003047 if (tab.hasCrashed) {
3048 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3049 }
Michael Kolba53c9892011-10-05 13:31:40 -07003050 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003051 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003052 }
3053
3054 /**
3055 * Load UrlData into a Tab and update the title bar to reflect the new
3056 * load. Call this instead of UrlData.loadIn directly.
3057 * @param t The Tab used to load.
3058 * @param data The UrlData being loaded.
3059 */
3060 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003061 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003062 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003063 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003064 } else {
John Reck38b39652012-06-05 09:22:59 -07003065 if (t != null && data.mDisableUrlOverride) {
3066 t.disableUrlOverridingForLoad();
3067 }
John Reck26b18322011-06-21 13:08:58 -07003068 loadUrl(t, data.mUrl, data.mHeaders);
3069 }
3070 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003071 }
3072
John Reck30c714c2010-12-16 17:30:34 -08003073 @Override
3074 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003075 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003076 if (tab.canGoBack()) {
3077 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003078 } else {
John Reckef654f12011-07-12 16:42:08 -07003079 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003080 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003081 }
3082
3083 void goBackOnePageOrQuit() {
3084 Tab current = mTabControl.getCurrentTab();
3085 if (current == null) {
3086 /*
3087 * Instead of finishing the activity, simply push this to the back
3088 * of the stack and let ActivityManager to choose the foreground
3089 * activity. As BrowserActivity is singleTask, it will be always the
3090 * root of the task. So we can use either true or false for
3091 * moveTaskToBack().
3092 */
luxiaolb40014b2013-07-19 10:01:43 +08003093 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003094 return;
3095 }
John Reckef654f12011-07-12 16:42:08 -07003096 if (current.canGoBack()) {
3097 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003098 } else {
3099 // Check to see if we are closing a window that was created by
3100 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003101 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003102 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003103 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003104 // Now we close the other tab
3105 closeTab(current);
3106 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003107 /*
3108 * Instead of finishing the activity, simply push this to the back
3109 * of the stack and let ActivityManager to choose the foreground
3110 * activity. As BrowserActivity is singleTask, it will be always the
3111 * root of the task. So we can use either true or false for
3112 * moveTaskToBack().
3113 */
luxiaolb40014b2013-07-19 10:01:43 +08003114 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003115 }
3116 }
3117 }
3118
3119 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003120 * helper method for key handler
3121 * returns the current tab if it can't advance
3122 */
Michael Kolbc831b632011-05-11 09:30:34 -07003123 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003124 int pos = mTabControl.getCurrentPosition() + 1;
3125 if (pos >= mTabControl.getTabCount()) {
3126 pos = 0;
3127 }
3128 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003129 }
3130
3131 /**
3132 * helper method for key handler
3133 * returns the current tab if it can't advance
3134 */
Michael Kolbc831b632011-05-11 09:30:34 -07003135 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003136 int pos = mTabControl.getCurrentPosition() - 1;
3137 if ( pos < 0) {
3138 pos = mTabControl.getTabCount() - 1;
3139 }
3140 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003141 }
3142
John Reckbcef87f2012-02-03 14:58:34 -08003143 boolean isMenuOrCtrlKey(int keyCode) {
3144 return (KeyEvent.KEYCODE_MENU == keyCode)
3145 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3146 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3147 }
3148
Michael Kolb0035fad2011-03-14 13:25:28 -07003149 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003150 * handle key events in browser
3151 *
3152 * @param keyCode
3153 * @param event
3154 * @return true if handled, false to pass to super
3155 */
John Reck9c35b9c2012-05-30 10:08:50 -07003156 @Override
3157 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003158 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003159 // Even if MENU is already held down, we need to call to super to open
3160 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003161 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003162 mMenuIsDown = true;
3163 return false;
3164 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003165
Cary Clark8ff8c662010-12-29 15:03:05 -05003166 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003167 Tab tab = getCurrentTab();
3168 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003169
Cary Clark160bbb92011-01-10 11:17:07 -05003170 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3171 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003172
Michael Kolb8233fac2010-10-26 16:08:53 -07003173 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003174 case KeyEvent.KEYCODE_TAB:
3175 if (event.isCtrlPressed()) {
3176 if (event.isShiftPressed()) {
3177 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003178 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003179 } else {
3180 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003181 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003182 }
3183 return true;
3184 }
3185 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003186 case KeyEvent.KEYCODE_SPACE:
3187 // WebView/WebTextView handle the keys in the KeyDown. As
3188 // the Activity's shortcut keys are only handled when WebView
3189 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003190 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003191 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003192 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003193 pageDown();
3194 }
3195 return true;
3196 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003197 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003198 event.startTracking();
3199 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003200 case KeyEvent.KEYCODE_FORWARD:
3201 if (!noModifiers) break;
3202 tab.goForward();
3203 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003204 case KeyEvent.KEYCODE_DPAD_LEFT:
3205 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003206 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003207 return true;
3208 }
3209 break;
3210 case KeyEvent.KEYCODE_DPAD_RIGHT:
3211 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003212 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003213 return true;
3214 }
3215 break;
3216 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003217 if (ctrl) {
3218 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003219 return true;
3220 }
3221 break;
Michael Kolba4183062011-01-16 10:43:21 -08003222// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003223 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003224 if (ctrl ) {
3225 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003226 return true;
3227 }
3228 break;
Michael Kolba4183062011-01-16 10:43:21 -08003229// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003230// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003231// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003232// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003233// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003234// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003235// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003236// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003237// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003238// case KeyEvent.KEYCODE_M: // unused
3239// case KeyEvent.KEYCODE_N: // in Chrome: new window
3240// case KeyEvent.KEYCODE_O: // in Chrome: open file
3241// case KeyEvent.KEYCODE_P: // in Chrome: print page
3242// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003243// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003244// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3245 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003246 // we can't use the ctrl/shift flags, they check for
3247 // exclusive use of a modifier
3248 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003249 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003250 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003251 } else {
3252 openTabToHomePage();
3253 }
3254 return true;
3255 }
3256 break;
3257// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3258// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003259// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003260// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3261// case KeyEvent.KEYCODE_Y: // unused
3262// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003263 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003264 // it is a regular key and webview is not null
3265 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003266 }
3267
John Reck9c35b9c2012-05-30 10:08:50 -07003268 @Override
3269 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003270 switch(keyCode) {
3271 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003272 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003273 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003274 return true;
3275 }
3276 break;
3277 }
3278 return false;
3279 }
3280
John Reck9c35b9c2012-05-30 10:08:50 -07003281 @Override
3282 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003283 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003284 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003285 if (KeyEvent.KEYCODE_MENU == keyCode
3286 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003287 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003288 }
3289 }
Cary Clark160bbb92011-01-10 11:17:07 -05003290 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003291 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003292 case KeyEvent.KEYCODE_BACK:
3293 if (event.isTracking() && !event.isCanceled()) {
3294 onBackKey();
3295 return true;
3296 }
3297 break;
3298 }
3299 return false;
3300 }
3301
3302 public boolean isMenuDown() {
3303 return mMenuIsDown;
3304 }
3305
John Reck9c35b9c2012-05-30 10:08:50 -07003306 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003307 public void setupAutoFill(Message message) {
3308 // Open the settings activity at the AutoFill profile fragment so that
3309 // the user can create a new profile. When they return, we will dispatch
3310 // the message so that we can autofill the form using their new profile.
3311 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3312 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3313 AutoFillSettingsFragment.class.getName());
3314 mAutoFillSetupMessage = message;
3315 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3316 }
John Reckb3417f02011-01-14 11:01:05 -08003317
John Reck9c35b9c2012-05-30 10:08:50 -07003318 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003319 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003320 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003321 return true;
3322 }
3323
John Reck1cf4b792011-07-26 10:22:22 -07003324 @Override
3325 public boolean shouldCaptureThumbnails() {
3326 return mUi.shouldCaptureThumbnails();
3327 }
3328
Michael Kolbc3af0672011-08-09 10:24:41 -07003329 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003330 public boolean supportsVoice() {
3331 PackageManager pm = mActivity.getPackageManager();
3332 List activities = pm.queryIntentActivities(new Intent(
3333 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3334 return activities.size() != 0;
3335 }
3336
3337 @Override
3338 public void startVoiceRecognizer() {
3339 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003340 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003341 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3342 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3343 mActivity.startActivityForResult(voice, VOICE_RESULT);
3344 }
3345
3346 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003347 public void setBlockEvents(boolean block) {
3348 mBlockEvents = block;
3349 }
3350
John Reck9c35b9c2012-05-30 10:08:50 -07003351 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003352 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003353 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003354 }
3355
John Reck9c35b9c2012-05-30 10:08:50 -07003356 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003357 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003358 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003359 }
3360
John Reck9c35b9c2012-05-30 10:08:50 -07003361 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003362 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003363 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003364 }
3365
John Reck9c35b9c2012-05-30 10:08:50 -07003366 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003367 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003368 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003369 }
3370
John Reck9c35b9c2012-05-30 10:08:50 -07003371 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003372 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003373 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003374 }
3375
Michael Kolb8233fac2010-10-26 16:08:53 -07003376}