blob: ea1a60cea647416fffaf8a6463bbb52c6c8b73fd [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);
John Reckd7dd9b22011-08-30 09:18:29 -0700711 if (sThumbnailBitmap != null) {
712 sThumbnailBitmap.recycle();
713 sThumbnailBitmap = null;
714 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 }
716
John Reck9c35b9c2012-05-30 10:08:50 -0700717 @Override
718 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700719 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800720 Bundle saveState = createSaveState();
721
722 // crash recovery manages all save & restore state
723 mCrashRecoveryHandler.writeState(saveState);
724 mSettings.setLastRunPaused(true);
725 }
726
727 /**
728 * Save the current state to outState. Does not write the state to
729 * disk.
730 * @return Bundle containing the current state of all tabs.
731 */
732 /* package */ Bundle createSaveState() {
733 Bundle saveState = new Bundle();
734 mTabControl.saveState(saveState);
735 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700736 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800737 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700738 }
George Mount3636d0a2011-11-21 09:08:21 -0800739 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700740 }
741
John Reck9c35b9c2012-05-30 10:08:50 -0700742 @Override
743 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 if (!mActivityPaused) {
745 Log.e(LOGTAG, "BrowserActivity is already resumed.");
746 return;
747 }
George Mount3636d0a2011-11-21 09:08:21 -0800748 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700749 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800750 Tab current = mTabControl.getCurrentTab();
751 if (current != null) {
752 current.resume();
753 resumeWebViewTimers(current);
754 }
John Reckf57c0292011-07-21 18:15:39 -0700755 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200756
Michael Kolb8233fac2010-10-26 16:08:53 -0700757 mUi.onResume();
758 mNetworkHandler.onResume();
759 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100760 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700761 if (mVoiceResult != null) {
762 mUi.onVoiceResult(mVoiceResult);
763 mVoiceResult = null;
764 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800765 if (current != null && current.hasCrashed) {
766 current.showCrashView();
767 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700768 }
769
John Reckf57c0292011-07-21 18:15:39 -0700770 private void releaseWakeLock() {
771 if (mWakeLock != null && mWakeLock.isHeld()) {
772 mHandler.removeMessages(RELEASE_WAKELOCK);
773 mWakeLock.release();
774 }
775 }
776
Michael Kolb70976932010-11-30 11:34:01 -0800777 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800778 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800779 * @param tab guaranteed non-null
780 */
781 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700782 boolean inLoad = tab.inPageLoad();
783 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
784 CookieSyncManager.getInstance().startSync();
785 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100786 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700787 }
788 }
789
Michael Kolb70976932010-11-30 11:34:01 -0800790 /**
791 * Pause all WebView timers using the WebView of the given tab
792 * @param tab
793 * @return true if the timers are paused or tab is null
794 */
795 private boolean pauseWebViewTimers(Tab tab) {
796 if (tab == null) {
797 return true;
798 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100800 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700801 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700802 }
Michael Kolb70976932010-11-30 11:34:01 -0800803 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 }
805
John Reck9c35b9c2012-05-30 10:08:50 -0700806 @Override
807 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800808 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700809 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
810 mUploadHandler = null;
811 }
812 if (mTabControl == null) return;
813 mUi.onDestroy();
814 // Remove the current tab and sub window
815 Tab t = mTabControl.getCurrentTab();
816 if (t != null) {
817 dismissSubWindow(t);
818 removeTab(t);
819 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500820 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700821 // Destroy all the tabs
822 mTabControl.destroy();
823 WebIconDatabase.getInstance().close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700824 }
825
826 protected boolean isActivityPaused() {
827 return mActivityPaused;
828 }
829
John Reck9c35b9c2012-05-30 10:08:50 -0700830 @Override
831 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 mTabControl.freeMemory();
833 }
834
835 @Override
836 public boolean shouldShowErrorConsole() {
837 return mShouldShowErrorConsole;
838 }
839
840 protected void setShouldShowErrorConsole(boolean show) {
841 if (show == mShouldShowErrorConsole) {
842 // Nothing to do.
843 return;
844 }
845 mShouldShowErrorConsole = show;
846 Tab t = mTabControl.getCurrentTab();
847 if (t == null) {
848 // There is no current tab so we cannot toggle the error console
849 return;
850 }
851 mUi.setShouldShowErrorConsole(t, show);
852 }
853
854 @Override
855 public void stopLoading() {
856 mLoadStopped = true;
857 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700859 if (w != null) {
860 w.stopLoading();
861 mUi.onPageStopped(tab);
862 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 }
864
865 boolean didUserStopLoading() {
866 return mLoadStopped;
867 }
868
kaiyiza016da12013-08-26 17:50:22 +0800869 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700870 final String reminderType = getContext().getResources().getString(
871 R.string.def_wifi_browser_interaction_remind_type);
872 final String selectionConnnection = getContext().getResources().getString(
873 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700874 final String wifiSelection = getContext().getResources().getString(
875 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700876
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700877 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
878 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700879 return;
880
kaiyiza016da12013-08-26 17:50:22 +0800881 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700882 Context.CONNECTIVITY_SERVICE);
883 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700884 WifiManager wifiMgr = (WifiManager) this.getContext()
885 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700886 if (networkInfo == null
887 || (networkInfo != null && (networkInfo.getType() !=
888 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700889 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
890 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700891 List<ScanResult> list = wifiMgr.getScanResults();
892 // Have no AP's for Wifi's fall back to data
893 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700894 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700895 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
896 this.getContext().startActivity(intent);
897 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700898 // Request to select Wifi AP
899 try {
900 Intent intent = new Intent(wifiSelection);
901 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
902 this.getContext().startActivity(intent);
903 } catch (Exception e) {
904 String err_msg = this.getContext().getString(
905 R.string.acivity_not_found, wifiSelection);
906 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
907 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700908 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700909 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800910 }
911 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700912
Michael Kolb8233fac2010-10-26 16:08:53 -0700913 // WebViewController
914
915 @Override
John Reck324d4402011-01-11 16:56:42 -0800916 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700917
918 // We've started to load a new page. If there was a pending message
919 // to save a screenshot then we will now take the new page and save
920 // an incorrect screenshot. Therefore, remove any pending thumbnail
921 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700922 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700923
924 // reset sync timer to avoid sync starts during loading a page
925 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700926 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700927 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700928 boolean networkNotifier =
929 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700930 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700931 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800932 } else {
933 if (!mNetworkHandler.isNetworkUp()) {
934 view.setNetworkAvailable(false);
935 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 }
937
938 // when BrowserActivity just starts, onPageStarted may be called before
939 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
940 // to start the timer. As we won't switch tabs while an activity is in
941 // pause state, we can ensure calling resume and pause in pair.
942 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800943 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700944 }
945 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700946 endActionMode();
947
John Reck30c714c2010-12-16 17:30:34 -0800948 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700949
John Reck324d4402011-01-11 16:56:42 -0800950 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 // update the bookmark database for favicon
952 maybeUpdateFavicon(tab, null, url, favicon);
953
954 Performance.tracePageStart(url);
955
956 // Performance probe
957 if (false) {
958 Performance.onPageStarted();
959 }
960
961 }
962
963 @Override
John Reck324d4402011-01-11 16:56:42 -0800964 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700965 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800966 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200967
Michael Kolb8233fac2010-10-26 16:08:53 -0700968 // Performance probe
969 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800970 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700971 }
972
973 Performance.tracePageFinished();
974 }
975
976 @Override
John Reck30c714c2010-12-16 17:30:34 -0800977 public void onProgressChanged(Tab tab) {
978 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700979
980 if (newProgress == 100) {
981 CookieSyncManager.getInstance().sync();
982 // onProgressChanged() may continue to be called after the main
983 // frame has finished loading, as any remaining sub frames continue
984 // to load. We'll only get called once though with newProgress as
985 // 100 when everything is loaded. (onPageFinished is called once
986 // when the main frame completes loading regardless of the state of
987 // any sub frames so calls to onProgressChanges may continue after
988 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800989 if (tab.inPageLoad()) {
990 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200991 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
992 // pause the WebView timer and release the wake lock if it is
993 // finished while BrowserActivity is in pause state.
994 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700995 }
John Reckd9862372012-02-21 15:04:50 -0800996 if (!tab.isPrivateBrowsingEnabled()
997 && !TextUtils.isEmpty(tab.getUrl())
998 && !tab.isSnapshot()) {
999 // Only update the bookmark screenshot if the user did not
1000 // cancel the load early and there is not already
1001 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001002 if (tab.shouldUpdateThumbnail() &&
1003 (tab.inForeground() && !didUserStopLoading()
1004 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001005 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1006 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1007 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001008 1500);
John Reckd9862372012-02-21 15:04:50 -08001009 }
1010 }
1011 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001012 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001013 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001014 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001015 // still loading
1016 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001017 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001018 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001019 }
1020 }
John Reck30c714c2010-12-16 17:30:34 -08001021 mUi.onProgressChanged(tab);
1022 }
1023
1024 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001025 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001026 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001027 }
1028
1029 @Override
1030 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001031 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001032 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001033 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001034 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1035 return;
1036 }
1037 // Update the title in the history database if not in private browsing mode
1038 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001039 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 }
1041 }
1042
1043 @Override
1044 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001045 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1047 }
1048
1049 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001050 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1051 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 }
1053
1054 @Override
1055 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1056 if (mMenuIsDown) {
1057 // only check shortcut key when MENU is held
1058 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1059 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001061 int keyCode = event.getKeyCode();
1062 // We need to send almost every key to WebKit. However:
1063 // 1. We don't want to block the device on the renderer for
1064 // some keys like menu, home, call.
1065 // 2. There are no WebKit equivalents for some of these keys
1066 // (see app/keyboard_codes_win.h)
1067 // Note that these are not the same set as KeyEvent.isSystemKey:
1068 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1069 if (keyCode == KeyEvent.KEYCODE_MENU ||
1070 keyCode == KeyEvent.KEYCODE_HOME ||
1071 keyCode == KeyEvent.KEYCODE_BACK ||
1072 keyCode == KeyEvent.KEYCODE_CALL ||
1073 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1074 keyCode == KeyEvent.KEYCODE_POWER ||
1075 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1076 keyCode == KeyEvent.KEYCODE_CAMERA ||
1077 keyCode == KeyEvent.KEYCODE_FOCUS ||
1078 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1079 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1080 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1081 return true;
1082 }
1083
1084 // We also have to intercept some shortcuts before we send them to the ContentView.
1085 if (event.isCtrlPressed() && (
1086 keyCode == KeyEvent.KEYCODE_TAB ||
1087 keyCode == KeyEvent.KEYCODE_W ||
1088 keyCode == KeyEvent.KEYCODE_F4)) {
1089 return true;
1090 }
1091
1092 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001093 }
1094
1095 @Override
John Reck997b1b72012-04-19 18:08:25 -07001096 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 if (!isActivityPaused()) {
1098 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001099 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001100 } else {
John Reck997b1b72012-04-19 18:08:25 -07001101 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001102 }
1103 }
John Reck997b1b72012-04-19 18:08:25 -07001104 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 }
1106
1107 @Override
John Reck324d4402011-01-11 16:56:42 -08001108 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001109 boolean disableHistoryWrites =
1110 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1111
1112 // Don't save anything in private browsing mode or when explicitly set
1113 // not to write history via an overlay
1114 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001115 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001116
John Reck324d4402011-01-11 16:56:42 -08001117 if (TextUtils.isEmpty(url)
1118 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 return;
1120 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001121
John Reckf57c0292011-07-21 18:15:39 -07001122 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001123 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001124 }
1125
1126 @Override
1127 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1128 AsyncTask<Void, Void, String[]> task =
1129 new AsyncTask<Void, Void, String[]>() {
1130 @Override
1131 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001132 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001133 }
1134 @Override
1135 public void onPostExecute(String[] result) {
1136 callback.onReceiveValue(result);
1137 }
1138 };
1139 task.execute();
1140 }
1141
1142 @Override
1143 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1144 final HttpAuthHandler handler, final String host,
1145 final String realm) {
1146 String username = null;
1147 String password = null;
1148
1149 boolean reuseHttpAuthUsernamePassword
1150 = handler.useHttpAuthUsernamePassword();
1151
1152 if (reuseHttpAuthUsernamePassword && view != null) {
1153 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1154 if (credentials != null && credentials.length == 2) {
1155 username = credentials[0];
1156 password = credentials[1];
1157 }
1158 }
1159
1160 if (username != null && password != null) {
1161 handler.proceed(username, password);
1162 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001163 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1165 } else {
1166 handler.cancel();
1167 }
1168 }
1169 }
1170
1171 @Override
1172 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001173 String contentDisposition, String mimetype, String referer,
1174 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001175 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001176 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001177 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001178 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001179 // This Tab was opened for the sole purpose of downloading a
1180 // file. Remove it.
1181 if (tab == mTabControl.getCurrentTab()) {
1182 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001183 if (tab.getDerivedFromIntent())
1184 closeTab(tab);
1185 else
1186 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001187 } else {
1188 // In this case, it is not.
1189 closeTab(tab);
1190 }
1191 }
1192 }
1193
1194 @Override
1195 public Bitmap getDefaultVideoPoster() {
1196 return mUi.getDefaultVideoPoster();
1197 }
1198
1199 @Override
1200 public View getVideoLoadingProgressView() {
1201 return mUi.getVideoLoadingProgressView();
1202 }
1203
1204 @Override
1205 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1206 SslError error) {
1207 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1208 }
1209
1210 // helper method
1211
1212 /*
1213 * Update the favorites icon if the private browsing isn't enabled and the
1214 * icon is valid.
1215 */
1216 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1217 final String url, Bitmap favicon) {
1218 if (favicon == null) {
1219 return;
1220 }
1221 if (!tab.isPrivateBrowsingEnabled()) {
1222 Bookmarks.updateFavicon(mActivity
1223 .getContentResolver(), originalUrl, url, favicon);
1224 }
1225 }
1226
Leon Scroggins4cd97792010-12-03 15:31:56 -05001227 @Override
1228 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001229 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001230 mUi.bookmarkedStatusHasChanged(tab);
1231 }
1232
Michael Kolb8233fac2010-10-26 16:08:53 -07001233 // end WebViewController
1234
1235 protected void pageUp() {
1236 getCurrentTopWebView().pageUp(false);
1237 }
1238
1239 protected void pageDown() {
1240 getCurrentTopWebView().pageDown(false);
1241 }
1242
1243 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001244 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001245 public void editUrl() {
1246 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001247 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001248 }
1249
John Reck9c35b9c2012-05-30 10:08:50 -07001250 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001251 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001252 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001253 if (tab.inForeground()) {
1254 if (mUi.isCustomViewShowing()) {
1255 callback.onCustomViewHidden();
1256 return;
1257 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001258 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 // Save the menu state and set it to empty while the custom
1260 // view is showing.
1261 mOldMenuState = mMenuState;
1262 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001263 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 }
1265 }
1266
1267 @Override
1268 public void hideCustomView() {
1269 if (mUi.isCustomViewShowing()) {
1270 mUi.onHideCustomView();
1271 // Reset the old menu state.
1272 mMenuState = mOldMenuState;
1273 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001274 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001275 }
1276 }
1277
John Reck9c35b9c2012-05-30 10:08:50 -07001278 @Override
1279 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001280 Intent intent) {
1281 if (getCurrentTopWebView() == null) return;
1282 switch (requestCode) {
1283 case PREFERENCES_PAGE:
1284 if (resultCode == Activity.RESULT_OK && intent != null) {
1285 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001286 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001287 mTabControl.removeParentChildRelationShips();
1288 }
1289 }
1290 break;
1291 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001292 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001293 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001294 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001296 case AUTOFILL_SETUP:
1297 // Determine whether a profile was actually set up or not
1298 // and if so, send the message back to the WebTextView to
1299 // fill the form with the new profile.
1300 if (getSettings().getAutoFillProfile() != null) {
1301 mAutoFillSetupMessage.sendToTarget();
1302 mAutoFillSetupMessage = null;
1303 }
1304 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001305 case COMBO_VIEW:
1306 if (intent == null || resultCode != Activity.RESULT_OK) {
1307 break;
1308 }
John Reck3ba45532011-08-11 16:26:53 -07001309 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001310 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1311 Tab t = getCurrentTab();
1312 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001313 mUpdateMyNavThumbnail = true;
1314 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001315 loadUrl(t, uri.toString());
1316 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1317 String[] urls = intent.getStringArrayExtra(
1318 ComboViewActivity.EXTRA_OPEN_ALL);
1319 Tab parent = getCurrentTab();
1320 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001321 if (url != null) {
1322 parent = openTab(url, parent,
1323 !mSettings.openInBackground(), true);
1324 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001325 }
1326 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1327 long id = intent.getLongExtra(
1328 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1329 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001330 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001331 }
1332 }
1333 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001334 case VOICE_RESULT:
1335 if (resultCode == Activity.RESULT_OK && intent != null) {
1336 ArrayList<String> results = intent.getStringArrayListExtra(
1337 RecognizerIntent.EXTRA_RESULTS);
1338 if (results.size() >= 1) {
1339 mVoiceResult = results.get(0);
1340 }
1341 }
1342 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001343 case MY_NAVIGATION:
1344 if (intent == null || resultCode != Activity.RESULT_OK) {
1345 break;
1346 }
1347
1348 if (intent.getBooleanExtra("need_refresh", false) &&
1349 getCurrentTopWebView() != null) {
1350 getCurrentTopWebView().reload();
1351 }
1352 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001353 default:
1354 break;
1355 }
1356 getCurrentTopWebView().requestFocus();
1357 }
1358
1359 /**
1360 * Open the Go page.
1361 * @param startWithHistory If true, open starting on the history tab.
1362 * Otherwise, start with the bookmarks tab.
1363 */
1364 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001365 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001366 if (mTabControl.getCurrentWebView() == null) {
1367 return;
1368 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001369 // clear action mode
1370 if (isInCustomActionMode()) {
1371 endActionMode();
1372 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001373 Bundle extras = new Bundle();
1374 // Disable opening in a new window if we have maxed out the windows
1375 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1376 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001377 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001378 }
1379
1380 // combo view callbacks
1381
Michael Kolb8233fac2010-10-26 16:08:53 -07001382 // key handling
1383 protected void onBackKey() {
1384 if (!mUi.onBackKey()) {
1385 WebView subwindow = mTabControl.getCurrentSubWindow();
1386 if (subwindow != null) {
1387 if (subwindow.canGoBack()) {
1388 subwindow.goBack();
1389 } else {
1390 dismissSubWindow(mTabControl.getCurrentTab());
1391 }
1392 } else {
1393 goBackOnePageOrQuit();
1394 }
1395 }
1396 }
1397
Michael Kolb4bd767d2011-05-27 11:33:55 -07001398 protected boolean onMenuKey() {
1399 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001400 }
1401
Michael Kolb8233fac2010-10-26 16:08:53 -07001402 // menu handling and state
1403 // TODO: maybe put into separate handler
1404
John Reck9c35b9c2012-05-30 10:08:50 -07001405 @Override
1406 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001407 if (mMenuState == EMPTY_MENU) {
1408 return false;
1409 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 MenuInflater inflater = mActivity.getMenuInflater();
1411 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001412 return true;
1413 }
1414
John Reck9c35b9c2012-05-30 10:08:50 -07001415 @Override
1416 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001418 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 return;
1420 }
1421 if (!(v instanceof WebView)) {
1422 return;
1423 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001424 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001425 WebView.HitTestResult result = webview.getHitTestResult();
1426 if (result == null) {
1427 return;
1428 }
1429
1430 int type = result.getType();
1431 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1432 Log.w(LOGTAG,
1433 "We should not show context menu when nothing is touched");
1434 return;
1435 }
1436 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1437 // let TextView handles context menu
1438 return;
1439 }
1440
1441 // Note, http://b/issue?id=1106666 is requesting that
1442 // an inflated menu can be used again. This is not available
1443 // yet, so inflate each time (yuk!)
1444 MenuInflater inflater = mActivity.getMenuInflater();
1445 inflater.inflate(R.menu.browsercontext, menu);
1446
1447 // Show the correct menu group
1448 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001449 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001450 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 menu.setGroupVisible(R.id.PHONE_MENU,
1452 type == WebView.HitTestResult.PHONE_TYPE);
1453 menu.setGroupVisible(R.id.EMAIL_MENU,
1454 type == WebView.HitTestResult.EMAIL_TYPE);
1455 menu.setGroupVisible(R.id.GEO_MENU,
1456 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001457 String itemUrl = null;
1458 String url = webview.getOriginalUrl();
1459 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1460 itemUrl = Uri.decode(navigationUrl);
1461 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1462 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1463 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1464 } else {
1465 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1466 }
1467 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1468 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1469 } else {
1470 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1471
1472 menu.setGroupVisible(R.id.IMAGE_MENU,
1473 type == WebView.HitTestResult.IMAGE_TYPE
1474 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1475 menu.setGroupVisible(R.id.ANCHOR_MENU,
1476 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1477 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001478 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1479 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001480 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001481 // Setup custom handling depending on the type
1482 switch (type) {
1483 case WebView.HitTestResult.PHONE_TYPE:
1484 menu.setHeaderTitle(Uri.decode(extra));
1485 menu.findItem(R.id.dial_context_menu_id).setIntent(
1486 new Intent(Intent.ACTION_VIEW, Uri
1487 .parse(WebView.SCHEME_TEL + extra)));
1488 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1489 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1490 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1491 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1492 addIntent);
1493 menu.findItem(R.id.copy_phone_context_menu_id)
1494 .setOnMenuItemClickListener(
1495 new Copy(extra));
1496 break;
1497
1498 case WebView.HitTestResult.EMAIL_TYPE:
1499 menu.setHeaderTitle(extra);
1500 menu.findItem(R.id.email_context_menu_id).setIntent(
1501 new Intent(Intent.ACTION_VIEW, Uri
1502 .parse(WebView.SCHEME_MAILTO + extra)));
1503 menu.findItem(R.id.copy_mail_context_menu_id)
1504 .setOnMenuItemClickListener(
1505 new Copy(extra));
1506 break;
1507
1508 case WebView.HitTestResult.GEO_TYPE:
1509 menu.setHeaderTitle(extra);
1510 menu.findItem(R.id.map_context_menu_id).setIntent(
1511 new Intent(Intent.ACTION_VIEW, Uri
1512 .parse(WebView.SCHEME_GEO
1513 + URLEncoder.encode(extra))));
1514 menu.findItem(R.id.copy_geo_context_menu_id)
1515 .setOnMenuItemClickListener(
1516 new Copy(extra));
1517 break;
1518
1519 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1520 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001521 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001522 // decide whether to show the open link in new tab option
1523 boolean showNewTab = mTabControl.canCreateNewTab();
1524 MenuItem newTabItem
1525 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001526 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001527 ? R.string.contextmenu_openlink_newwindow_background
1528 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001529 newTabItem.setVisible(showNewTab);
1530 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001531 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1532 newTabItem.setOnMenuItemClickListener(
1533 new MenuItem.OnMenuItemClickListener() {
1534 @Override
1535 public boolean onMenuItemClick(MenuItem item) {
1536 final HashMap<String, WebView> hrefMap =
1537 new HashMap<String, WebView>();
1538 hrefMap.put("webview", webview);
1539 final Message msg = mHandler.obtainMessage(
1540 FOCUS_NODE_HREF,
1541 R.id.open_newtab_context_menu_id,
1542 0, hrefMap);
1543 webview.requestFocusNodeHref(msg);
1544 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001545 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001546 });
1547 } else {
1548 newTabItem.setOnMenuItemClickListener(
1549 new MenuItem.OnMenuItemClickListener() {
1550 @Override
1551 public boolean onMenuItemClick(MenuItem item) {
1552 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001553 openTab(extra, parent,
1554 !mSettings.openInBackground(),
1555 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001556 return true;
1557 }
1558 });
1559 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001560 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001561 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1562 menu.setHeaderTitle(navigationUrl);
1563 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1564
1565 if (itemUrl != null) {
1566 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1567 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1568 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1569 @Override
1570 public boolean onMenuItemClick(MenuItem item) {
1571 final Intent intent = new Intent(Controller.this
1572 .getContext(),
1573 AddMyNavigationPage.class);
1574 Bundle bundle = new Bundle();
1575 String url = Uri.decode(navigationUrl);
1576 bundle.putBoolean("isAdding", false);
1577 bundle.putString("url", url);
1578 bundle.putString("name", getNameFromUrl(url));
1579 intent.putExtra("websites", bundle);
1580 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1581 return false;
1582 }
1583 });
1584 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1585 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1586 @Override
1587 public boolean onMenuItemClick(MenuItem item) {
1588 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1589 return false;
1590 }
1591 });
1592 }
1593 } else {
1594 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1595 }
1596 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001597 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1598 break;
1599 }
1600 // otherwise fall through to handle image part
1601 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001602 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1603 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001604 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1605 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001606 shareItem.setOnMenuItemClickListener(
1607 new MenuItem.OnMenuItemClickListener() {
1608 @Override
1609 public boolean onMenuItemClick(MenuItem item) {
1610 sharePage(mActivity, null, extra, null,
1611 null);
1612 return true;
1613 }
1614 }
1615 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001617 menu.findItem(R.id.view_image_context_menu_id)
1618 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1619 @Override
1620 public boolean onMenuItemClick(MenuItem item) {
1621 openTab(extra, mTabControl.getCurrentTab(), true, true);
1622 return false;
1623 }
1624 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001625 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1626 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1627 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001628 menu.findItem(R.id.set_wallpaper_context_menu_id).
1629 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1630 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001631 break;
1632
1633 default:
1634 Log.w(LOGTAG, "We should not get here.");
1635 break;
1636 }
1637 //update the ui
1638 mUi.onContextMenuCreated(menu);
1639 }
1640
kaiyiz6e5b3e02013-08-19 20:02:01 +08001641 public void startAddMyNavigation(String url) {
1642 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1643 Bundle bundle = new Bundle();
1644 bundle.putBoolean("isAdding", true);
1645 bundle.putString("url", url);
1646 bundle.putString("name", getNameFromUrl(url));
1647 intent.putExtra("websites", bundle);
1648 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1649 }
1650
1651 private void showMyNavigationDeleteDialog(final String itemUrl) {
1652 new AlertDialog.Builder(this.getContext())
1653 .setTitle(R.string.my_navigation_delete_label)
1654 .setIcon(android.R.drawable.ic_dialog_alert)
1655 .setMessage(R.string.my_navigation_delete_msg)
1656 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1657 @Override
1658 public void onClick(DialogInterface dialog, int whichButton) {
1659 deleteMyNavigationItem(itemUrl);
1660 }
1661 })
1662 .setNegativeButton(R.string.cancel, null)
1663 .show();
1664 }
1665
1666 private void deleteMyNavigationItem(final String itemUrl) {
1667 ContentResolver cr = this.getContext().getContentResolver();
1668 Cursor cursor = null;
1669
1670 try {
1671 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1672 new String[] {
1673 MyNavigationUtil.ID
1674 }, "url = ?", new String[] {
1675 itemUrl
1676 }, null);
1677 if (null != cursor && cursor.moveToFirst()) {
1678 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1679 cursor.getLong(0));
1680
1681 ContentValues values = new ContentValues();
1682 values.put(MyNavigationUtil.TITLE, "");
1683 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1684 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1685 ByteArrayOutputStream os = new ByteArrayOutputStream();
1686 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1687 R.raw.my_navigation_add);
1688 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1689 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1690 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1691 cr.update(uri, values, null, null);
1692 } else {
1693 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1694 }
1695 } catch (IllegalStateException e) {
1696 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1697 } finally {
1698 if (null != cursor) {
1699 cursor.close();
1700 }
1701 }
1702
1703 if (getCurrentTopWebView() != null) {
1704 getCurrentTopWebView().reload();
1705 }
1706 }
1707
1708 private String getNameFromUrl(String itemUrl) {
1709 ContentResolver cr = this.getContext().getContentResolver();
1710 Cursor cursor = null;
1711 String name = null;
1712
1713 try {
1714 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1715 new String[] {
1716 MyNavigationUtil.TITLE
1717 }, "url = ?", new String[] {
1718 itemUrl
1719 }, null);
1720 if (null != cursor && cursor.moveToFirst()) {
1721 name = cursor.getString(0);
1722 } else {
1723 Log.e(LOGTAG, "this item does not exist!");
1724 }
1725 } catch (IllegalStateException e) {
1726 Log.e(LOGTAG, "getNameFromUrl", e);
1727 } finally {
1728 if (null != cursor) {
1729 cursor.close();
1730 }
1731 }
1732 return name;
1733 }
1734
1735 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1736 int width = mActivity.getResources().getDimensionPixelOffset(
1737 R.dimen.myNavigationThumbnailWidth);
1738 int height = mActivity.getResources().getDimensionPixelOffset(
1739 R.dimen.myNavigationThumbnailHeight);
1740
1741 final Bitmap bm = createScreenshot(webView, width, height);
1742
1743 if (bm == null) {
1744 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1745 return;
1746 }
1747
1748 final ContentResolver cr = mActivity.getContentResolver();
1749 new AsyncTask<Void, Void, Void>() {
1750 @Override
1751 protected Void doInBackground(Void... unused) {
1752 ContentResolver cr = mActivity.getContentResolver();
1753 Cursor cursor = null;
1754 try {
1755 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1756 new String[] {
1757 MyNavigationUtil.ID
1758 }, "url = ?", new String[] {
1759 itemUrl
1760 }, null);
1761 if (null != cursor && cursor.moveToFirst()) {
1762 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1763 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1764
1765 ContentValues values = new ContentValues();
1766 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1767 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1768 cursor.getLong(0));
1769 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1770 cr.update(uri, values, null, null);
1771 os.close();
1772 }
1773 } catch (IllegalStateException e) {
1774 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1775 } catch (IOException e) {
1776 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1777 } finally {
1778 if (null != cursor) {
1779 cursor.close();
1780 }
1781 }
1782 return null;
1783 }
1784 }.execute();
1785 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001786 /**
1787 * As the menu can be open when loading state changes
1788 * we must manually update the state of the stop/reload menu
1789 * item
1790 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001791 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001792 if (menu == null) {
1793 return;
1794 }
1795 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001796 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001797 menu.findItem(R.id.stop_menu_id):
1798 menu.findItem(R.id.reload_menu_id);
1799 if (src != null) {
1800 dest.setIcon(src.getIcon());
1801 dest.setTitle(src.getTitle());
1802 }
1803 }
1804
John Reck9c35b9c2012-05-30 10:08:50 -07001805 @Override
1806 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001807 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001808 // hold on to the menu reference here; it is used by the page callbacks
1809 // to update the menu based on loading state
1810 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001811 // Note: setVisible will decide whether an item is visible; while
1812 // setEnabled() will decide whether an item is enabled, which also means
1813 // whether the matching shortcut key will function.
1814 switch (mMenuState) {
1815 case EMPTY_MENU:
1816 if (mCurrentMenuState != mMenuState) {
1817 menu.setGroupVisible(R.id.MAIN_MENU, false);
1818 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1819 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1820 }
1821 break;
1822 default:
1823 if (mCurrentMenuState != mMenuState) {
1824 menu.setGroupVisible(R.id.MAIN_MENU, true);
1825 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1826 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1827 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001828 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001829 break;
1830 }
1831 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001832 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001833 }
1834
Michael Kolb4bf79712011-07-14 14:18:12 -07001835 @Override
1836 public void updateMenuState(Tab tab, Menu menu) {
1837 boolean canGoBack = false;
1838 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001839 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001840 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001841 if (tab != null) {
1842 canGoBack = tab.canGoBack();
1843 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001844 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001845 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001846 }
1847 final MenuItem back = menu.findItem(R.id.back_menu_id);
1848 back.setEnabled(canGoBack);
1849
1850 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001851
1852 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1853 forward.setEnabled(canGoForward);
1854
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001855 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1856 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001857 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001858 if (source != null && dest != null) {
1859 dest.setTitle(source.getTitle());
1860 dest.setIcon(source.getIcon());
1861 }
John Recke1a03a32011-09-14 17:04:16 -07001862 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001863
1864 // decide whether to show the share link option
1865 PackageManager pm = mActivity.getPackageManager();
1866 Intent send = new Intent(Intent.ACTION_SEND);
1867 send.setType("text/plain");
1868 ResolveInfo ri = pm.resolveActivity(send,
1869 PackageManager.MATCH_DEFAULT_ONLY);
1870 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1871
1872 boolean isNavDump = mSettings.enableNavDump();
1873 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1874 nav.setVisible(isNavDump);
1875 nav.setEnabled(isNavDump);
1876
1877 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001878 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1879 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001880 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1881 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001882 // history and snapshots item are the members of COMBO menu group,
1883 // so if show history item, only make snapshots item invisible.
1884 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001885
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001886 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001887 }
1888
John Reck9c35b9c2012-05-30 10:08:50 -07001889 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001890 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001891 if (null == getCurrentTopWebView()) {
1892 return false;
1893 }
1894 if (mMenuIsDown) {
1895 // The shortcut action consumes the MENU. Even if it is still down,
1896 // it won't trigger the next shortcut action. In the case of the
1897 // shortcut action triggering a new activity, like Bookmarks, we
1898 // won't get onKeyUp for MENU. So it is important to reset it here.
1899 mMenuIsDown = false;
1900 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001901 if (mUi.onOptionsItemSelected(item)) {
1902 // ui callback handled it
1903 return true;
1904 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001905 switch (item.getItemId()) {
1906 // -- Main menu
1907 case R.id.new_tab_menu_id:
1908 openTabToHomePage();
1909 break;
1910
1911 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001912 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001913 break;
1914
Afzal Najamd4e33312012-04-26 01:54:01 -04001915 case R.id.close_other_tabs_id:
1916 closeOtherTabs();
1917 break;
1918
Michael Kolb8233fac2010-10-26 16:08:53 -07001919 case R.id.goto_menu_id:
1920 editUrl();
1921 break;
1922
1923 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001924 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1925 break;
1926
1927 case R.id.history_menu_id:
1928 bookmarksOrHistoryPicker(ComboViews.History);
1929 break;
1930
1931 case R.id.snapshots_menu_id:
1932 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 break;
1934
Michael Kolb8233fac2010-10-26 16:08:53 -07001935 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001936 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001937 break;
1938
1939 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001940 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001941 stopLoading();
1942 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001943 Tab currentTab = mTabControl.getCurrentTab();
1944 if (currentTab.hasCrashed) {
1945 currentTab.replaceCrashView(getCurrentTopWebView(),
1946 currentTab.getViewContainer());
1947 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001948 getCurrentTopWebView().reload();
1949 }
1950 break;
1951
1952 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001953 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001954 break;
1955
1956 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001957 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001958 break;
1959
1960 case R.id.close_menu_id:
1961 // Close the subwindow if it exists.
1962 if (mTabControl.getCurrentSubWindow() != null) {
1963 dismissSubWindow(mTabControl.getCurrentTab());
1964 break;
1965 }
1966 closeCurrentTab();
1967 break;
1968
kaiyiza8b6dbb2013-07-29 18:11:22 +08001969 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001970 Object[] params = { new String("persist.debug.browsermonkeytest")};
1971 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001972 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001973 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001974 if (ret != null && ret.equals("enable"))
1975 break;
luxiaolb40014b2013-07-19 10:01:43 +08001976 showExitDialog(mActivity);
1977 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001978 case R.id.homepage_menu_id:
1979 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001980 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001981 break;
1982
1983 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001984 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001985 break;
1986
1987 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001988 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001989 break;
1990
John Reck2bc80422011-06-30 15:11:49 -07001991 case R.id.save_snapshot_menu_id:
1992 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001993 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001994 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001995 break;
1996
Michael Kolb8233fac2010-10-26 16:08:53 -07001997 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001998 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001999 break;
2000
John Recke1a03a32011-09-14 17:04:16 -07002001 case R.id.snapshot_go_live:
2002 goLive();
2003 return true;
2004
Michael Kolb8233fac2010-10-26 16:08:53 -07002005 case R.id.share_page_menu_id:
2006 Tab currentTab = mTabControl.getCurrentTab();
2007 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002008 return false;
2009 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002010 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002011 break;
2012
2013 case R.id.dump_nav_menu_id:
2014 getCurrentTopWebView().debugDump();
2015 break;
2016
Michael Kolb8233fac2010-10-26 16:08:53 -07002017 case R.id.zoom_in_menu_id:
2018 getCurrentTopWebView().zoomIn();
2019 break;
2020
2021 case R.id.zoom_out_menu_id:
2022 getCurrentTopWebView().zoomOut();
2023 break;
2024
2025 case R.id.view_downloads_menu_id:
2026 viewDownloads();
2027 break;
2028
John Reck42229bc2011-08-19 13:26:43 -07002029 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002030 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002031 break;
2032
Michael Kolb8233fac2010-10-26 16:08:53 -07002033 case R.id.window_one_menu_id:
2034 case R.id.window_two_menu_id:
2035 case R.id.window_three_menu_id:
2036 case R.id.window_four_menu_id:
2037 case R.id.window_five_menu_id:
2038 case R.id.window_six_menu_id:
2039 case R.id.window_seven_menu_id:
2040 case R.id.window_eight_menu_id:
2041 {
2042 int menuid = item.getItemId();
2043 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2044 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2045 Tab desiredTab = mTabControl.getTab(id);
2046 if (desiredTab != null &&
2047 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002048 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002049 }
2050 break;
2051 }
2052 }
2053 }
2054 break;
2055
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002056 case R.id.about_menu_id:
2057 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2058 builder.setTitle(R.string.about);
2059 builder.setCancelable(true);
2060 String ua = "";
2061 final WebView currentWebView = getCurrentWebView();
2062 if (currentWebView != null) {
2063 final WebSettings s = currentWebView.getSettings();
2064 if (s != null) {
2065 ua = s.getUserAgentString();
2066 }
2067 }
2068 builder.setMessage("Agent:" + ua);
2069 builder.setPositiveButton(android.R.string.ok, null);
2070 builder.create().show();
2071 break;
2072
Michael Kolb8233fac2010-10-26 16:08:53 -07002073 default:
2074 return false;
2075 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002076 return true;
2077 }
2078
John Reck68234a92012-04-19 15:27:12 -07002079 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2080 implements OnCancelListener {
2081
2082 private Tab mTab;
2083 private Dialog mProgressDialog;
2084 private ContentValues mValues;
2085
2086 private SaveSnapshotTask(Tab tab) {
2087 mTab = tab;
2088 }
2089
2090 @Override
2091 protected void onPreExecute() {
2092 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2093 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2094 true, true, this);
2095 mValues = mTab.createSnapshotValues();
2096 }
2097
2098 @Override
2099 protected Long doInBackground(Void... params) {
2100 if (!mTab.saveViewState(mValues)) {
2101 return null;
2102 }
2103 if (isCancelled()) {
2104 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2105 File file = mActivity.getFileStreamPath(path);
2106 if (!file.delete()) {
2107 file.deleteOnExit();
2108 }
2109 return null;
2110 }
2111 final ContentResolver cr = mActivity.getContentResolver();
2112 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2113 if (result == null) {
2114 return null;
2115 }
2116 long id = ContentUris.parseId(result);
2117 return id;
2118 }
2119
2120 @Override
2121 protected void onPostExecute(Long id) {
2122 if (isCancelled()) {
2123 return;
2124 }
2125 mProgressDialog.dismiss();
2126 if (id == null) {
2127 Toast.makeText(mActivity, R.string.snapshot_failed,
2128 Toast.LENGTH_SHORT).show();
2129 return;
2130 }
2131 Bundle b = new Bundle();
2132 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2133 mUi.showComboView(ComboViews.Snapshots, b);
2134 }
2135
2136 @Override
2137 public void onCancel(DialogInterface dialog) {
2138 cancel(true);
2139 }
2140 }
2141
Michael Kolb80f75082012-04-10 10:50:06 -07002142 @Override
2143 public void toggleUserAgent() {
2144 WebView web = getCurrentWebView();
2145 mSettings.toggleDesktopUseragent(web);
2146 web.loadUrl(web.getOriginalUrl());
2147 }
2148
2149 @Override
2150 public void findOnPage() {
2151 getCurrentTopWebView().showFindDialog(null, true);
2152 }
2153
2154 @Override
2155 public void openPreferences() {
2156 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2157 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2158 getCurrentTopWebView().getUrl());
2159 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2160 }
2161
2162 @Override
2163 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002164 WebView w = getCurrentTopWebView();
2165 if (w == null)
2166 return;
2167
2168 final Intent i = createBookmarkCurrentPageIntent(false);
2169 createScreenshotAsync(
2170 w, getDesiredThumbnailWidth(mActivity),
2171 getDesiredThumbnailHeight(mActivity),
2172 new ValueCallback<Bitmap>() {
2173 @Override
2174 public void onReceiveValue(Bitmap bitmap) {
2175 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2176 mActivity.startActivity(i);
2177 }
2178 });
Michael Kolb80f75082012-04-10 10:50:06 -07002179 }
2180
John Recke1a03a32011-09-14 17:04:16 -07002181 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002182 SnapshotTab t = (SnapshotTab) getCurrentTab();
2183 t.loadUrl(t.getLiveUrl(), null);
John Recke1a03a32011-09-14 17:04:16 -07002184 }
2185
luxiaolb40014b2013-07-19 10:01:43 +08002186 private void showExitDialog(final Activity activity) {
2187 new AlertDialog.Builder(activity)
2188 .setTitle(R.string.exit_browser_title)
2189 .setIcon(android.R.drawable.ic_dialog_alert)
2190 .setMessage(R.string.exit_browser_msg)
2191 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2192 public void onClick(DialogInterface dialog, int which) {
2193 activity.moveTaskToBack(true);
2194 dialog.dismiss();
2195 }
2196 })
2197 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2198 public void onClick(DialogInterface dialog, int which) {
2199 activity.finish();
2200 mHandler.postDelayed(new Runnable() {
2201 @Override
2202 public void run() {
2203 // TODO Auto-generated method stub
2204 mCrashRecoveryHandler.clearState(true);
2205 int pid = android.os.Process.myPid();
2206 android.os.Process.killProcess(pid);
2207 }
2208 }, 300);
2209 dialog.dismiss();
2210 }
2211 })
2212 .show();
2213 }
Michael Kolb315d5022011-10-13 12:47:11 -07002214 @Override
2215 public void showPageInfo() {
2216 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2217 }
2218
John Reck9c35b9c2012-05-30 10:08:50 -07002219 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002220 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002221 // Let the History and Bookmark fragments handle menus they created.
2222 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2223 return false;
2224 }
2225
Michael Kolb8233fac2010-10-26 16:08:53 -07002226 int id = item.getItemId();
2227 boolean result = true;
2228 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002229 // -- Browser context menu
2230 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002231 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002232 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002233 case R.id.copy_link_context_menu_id:
2234 final WebView webView = getCurrentTopWebView();
2235 if (null == webView) {
2236 result = false;
2237 break;
2238 }
2239 final HashMap<String, WebView> hrefMap =
2240 new HashMap<String, WebView>();
2241 hrefMap.put("webview", webView);
2242 final Message msg = mHandler.obtainMessage(
2243 FOCUS_NODE_HREF, id, 0, hrefMap);
2244 webView.requestFocusNodeHref(msg);
2245 break;
2246
2247 default:
2248 // For other context menus
2249 result = onOptionsItemSelected(item);
2250 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002251 return result;
2252 }
2253
2254 /**
2255 * support programmatically opening the context menu
2256 */
2257 public void openContextMenu(View view) {
2258 mActivity.openContextMenu(view);
2259 }
2260
2261 /**
2262 * programmatically open the options menu
2263 */
2264 public void openOptionsMenu() {
2265 mActivity.openOptionsMenu();
2266 }
2267
John Reck9c35b9c2012-05-30 10:08:50 -07002268 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002269 public boolean onMenuOpened(int featureId, Menu menu) {
2270 if (mOptionsMenuOpen) {
2271 if (mConfigChanged) {
2272 // We do not need to make any changes to the state of the
2273 // title bar, since the only thing that happened was a
2274 // change in orientation
2275 mConfigChanged = false;
2276 } else {
2277 if (!mExtendedMenuOpen) {
2278 mExtendedMenuOpen = true;
2279 mUi.onExtendedMenuOpened();
2280 } else {
2281 // Switching the menu back to icon view, so show the
2282 // title bar once again.
2283 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002284 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002285 }
2286 }
2287 } else {
2288 // The options menu is closed, so open it, and show the title
2289 mOptionsMenuOpen = true;
2290 mConfigChanged = false;
2291 mExtendedMenuOpen = false;
2292 mUi.onOptionsMenuOpened();
2293 }
2294 return true;
2295 }
2296
John Reck9c35b9c2012-05-30 10:08:50 -07002297 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002298 public void onOptionsMenuClosed(Menu menu) {
2299 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002300 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002301 }
2302
John Reck9c35b9c2012-05-30 10:08:50 -07002303 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002304 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002305 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002306 }
2307
2308 // Helper method for getting the top window.
2309 @Override
2310 public WebView getCurrentTopWebView() {
2311 return mTabControl.getCurrentTopWebView();
2312 }
2313
2314 @Override
2315 public WebView getCurrentWebView() {
2316 return mTabControl.getCurrentWebView();
2317 }
2318
2319 /*
2320 * This method is called as a result of the user selecting the options
2321 * menu to see the download window. It shows the download window on top of
2322 * the current window.
2323 */
2324 void viewDownloads() {
2325 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2326 mActivity.startActivity(intent);
2327 }
2328
John Reck30b065e2011-07-19 10:58:05 -07002329 int getActionModeHeight() {
2330 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2331 new int[] { android.R.attr.actionBarSize });
2332 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2333 actionBarSizeTypedArray.recycle();
2334 return size;
2335 }
2336
Michael Kolb8233fac2010-10-26 16:08:53 -07002337 // action mode
2338
John Reck9c35b9c2012-05-30 10:08:50 -07002339 @Override
2340 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002341 mUi.onActionModeStarted(mode);
2342 mActionMode = mode;
2343 }
2344
2345 /*
2346 * True if a custom ActionMode (i.e. find or select) is in use.
2347 */
2348 @Override
2349 public boolean isInCustomActionMode() {
2350 return mActionMode != null;
2351 }
2352
2353 /*
2354 * End the current ActionMode.
2355 */
2356 @Override
2357 public void endActionMode() {
2358 if (mActionMode != null) {
2359 mActionMode.finish();
2360 }
2361 }
2362
2363 /*
2364 * Called by find and select when they are finished. Replace title bars
2365 * as necessary.
2366 */
John Reck9c35b9c2012-05-30 10:08:50 -07002367 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002368 public void onActionModeFinished(ActionMode mode) {
2369 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002370 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002371 mActionMode = null;
2372 }
2373
2374 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002375 final Tab tab = getCurrentTab();
2376 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002377 }
2378
2379 // bookmark handling
2380
2381 /**
2382 * add the current page as a bookmark to the given folder id
2383 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002384 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002385 * bookmarked, and if it is, edit that bookmark. If false, and
2386 * the site is already bookmarked, do not attempt to edit the
2387 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002388 */
2389 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002390 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002391 WebView w = getCurrentTopWebView();
2392 if (w == null) {
2393 return null;
2394 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002395 Intent i = new Intent(mActivity,
2396 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002397 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2398 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2399 String touchIconUrl = w.getTouchIconUrl();
2400 if (touchIconUrl != null) {
2401 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2402 WebSettings settings = w.getSettings();
2403 if (settings != null) {
2404 i.putExtra(AddBookmarkPage.USER_AGENT,
2405 settings.getUserAgentString());
2406 }
2407 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002408 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002409 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002410 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002411 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2412 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002413 // Put the dialog at the upper right of the screen, covering the
2414 // star on the title bar.
2415 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002416 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002417 }
2418
2419 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002420 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002421 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002423 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002424 }
2425
Vivek Sekharb54614f2014-05-01 19:03:37 -07002426 @Override
2427 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2428 boolean capture) {
2429 mUploadHandler = new UploadHandler(this);
2430 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2431 }
2432
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 // thumbnails
2434
2435 /**
2436 * Return the desired width for thumbnail screenshots, which are stored in
2437 * the database, and used on the bookmarks screen.
2438 * @param context Context for finding out the density of the screen.
2439 * @return desired width for thumbnail screenshot.
2440 */
2441 static int getDesiredThumbnailWidth(Context context) {
2442 return context.getResources().getDimensionPixelOffset(
2443 R.dimen.bookmarkThumbnailWidth);
2444 }
2445
2446 /**
2447 * Return the desired height for thumbnail screenshots, which are stored in
2448 * the database, and used on the bookmarks screen.
2449 * @param context Context for finding out the density of the screen.
2450 * @return desired height for thumbnail screenshot.
2451 */
2452 static int getDesiredThumbnailHeight(Context context) {
2453 return context.getResources().getDimensionPixelOffset(
2454 R.dimen.bookmarkThumbnailHeight);
2455 }
2456
John Reck8cc92352011-07-06 17:41:52 -07002457 static Bitmap createScreenshot(WebView view, int width, int height) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002458 if (view == null || width == 0 || height == 0) {
John Reckd7dd9b22011-08-30 09:18:29 -07002459 return null;
2460 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002461
2462 Bitmap viewportBitmap = view.getViewportBitmap();
2463 if (viewportBitmap == null) {
2464 return null;
2465 }
2466
2467 float aspectRatio = (float) width/height;
2468 int viewportWidth = viewportBitmap.getWidth();
2469 int viewportHeight = viewportBitmap.getHeight();
2470
2471 //modify the size to attain the same aspect ratio of desired thumbnail size
2472 if (viewportHeight > viewportWidth) {
Tarun Nainani0be3b7c2014-07-03 01:17:12 -07002473 viewportHeight= (int)Math.round(viewportWidth * (1/aspectRatio));
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002474 } else {
2475 viewportWidth = (int)Math.round(viewportHeight * aspectRatio);
2476 }
2477
2478 Rect srcRect = new Rect(0, 0, viewportWidth, viewportHeight);
2479 Rect dstRect = new Rect(0, 0, width, height);
2480
2481 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != width
2482 || sThumbnailBitmap.getHeight() != height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002483 if (sThumbnailBitmap != null) {
2484 sThumbnailBitmap.recycle();
2485 sThumbnailBitmap = null;
2486 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002487
2488 sThumbnailBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002489 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002490
John Reckd7dd9b22011-08-30 09:18:29 -07002491 Canvas canvas = new Canvas(sThumbnailBitmap);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002492 canvas.drawBitmap(viewportBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
Vivek Sekhar0ab52512014-07-02 14:51:32 -07002493 if (viewportBitmap != null) {
2494 viewportBitmap.recycle();
2495 viewportBitmap = null;
2496 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002497 return sThumbnailBitmap;
Michael Kolb8233fac2010-10-26 16:08:53 -07002498 }
2499
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002500 static void createScreenshotAsync(WebView view, int width, int height,
2501 final ValueCallback<Bitmap> cb) {
2502 if (view == null || width == 0 || height == 0) {
2503 return;
2504 }
2505
2506 view.getContentBitmapAsync(
2507 (float) width / view.getWidth(),
2508 new Rect(),
2509 new ValueCallback<Bitmap>() {
2510 @Override
2511 public void onReceiveValue(Bitmap bitmap) {
2512 Log.e("sudheer", "screensot bitmap: w: " + bitmap.getWidth()
2513 + " h: " + bitmap.getHeight());
2514 if (bitmap != null)
2515 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2516 cb.onReceiveValue(bitmap);
2517 }});
2518 }
2519
2520
John Reck34ef2672011-02-10 11:30:55 -08002521 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002522 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002523 // FIXME: Would like to make sure there is actually something to
2524 // draw, but the API for that (WebViewCore.pictureReady()) is not
2525 // currently accessible here.
2526
John Reck34ef2672011-02-10 11:30:55 -08002527 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002528 if (view == null) {
2529 // Tab was destroyed
2530 return;
2531 }
John Reck34ef2672011-02-10 11:30:55 -08002532 final String url = tab.getUrl();
2533 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002534 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002535 if (TextUtils.isEmpty(url)) {
2536 return;
2537 }
2538
kaiyiz6e5b3e02013-08-19 20:02:01 +08002539 //update My Navigation Thumbnails
2540 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2541 if (isMyNavigationUrl) {
2542 updateMyNavigationThumbnail(url, view);
2543 }
John Reck34ef2672011-02-10 11:30:55 -08002544 // Only update thumbnails for web urls (http(s)://), not for
2545 // about:, javascript:, data:, etc...
2546 // Unless it is a bookmarked site, then always update
2547 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2548 return;
2549 }
2550
kaiyiz6e5b3e02013-08-19 20:02:01 +08002551 if (url != null && mUpdateMyNavThumbnailUrl != null
2552 && Patterns.WEB_URL.matcher(url).matches()
2553 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2554 String urlHost = (new WebAddress(url)).getHost();
2555 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2556 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2557 || bookmarkHost.length() == 0) {
2558 return;
2559 }
2560 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2561 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2562 bookmarkHost.length());
2563 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2564 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2565 if (!bookmarkDomain.equals(urlDomain)) {
2566 return;
2567 }
2568 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002569 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2570 getDesiredThumbnailHeight(mActivity));
2571 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002572 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2573 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2574 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2575 500);
2576 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002577 return;
2578 }
2579
2580 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002581 new AsyncTask<Void, Void, Void>() {
2582 @Override
2583 protected Void doInBackground(Void... unused) {
2584 Cursor cursor = null;
2585 try {
2586 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002587 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2588 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2589 : url);
2590 if (mUpdateMyNavThumbnail) {
2591 mUpdateMyNavThumbnail = false;
2592 mUpdateMyNavThumbnailUrl = null;
2593 }
John Reck34ef2672011-02-10 11:30:55 -08002594 if (cursor != null && cursor.moveToFirst()) {
2595 final ByteArrayOutputStream os =
2596 new ByteArrayOutputStream();
2597 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002598
John Reck34ef2672011-02-10 11:30:55 -08002599 ContentValues values = new ContentValues();
2600 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002601
John Reck34ef2672011-02-10 11:30:55 -08002602 do {
John Reck617fd832011-02-16 14:35:59 -08002603 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002604 cr.update(Images.CONTENT_URI, values, null, null);
2605 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 }
John Reck34ef2672011-02-10 11:30:55 -08002607 } catch (IllegalStateException e) {
2608 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002609 } catch (SQLiteException s) {
2610 // Added for possible error when user tries to remove the same bookmark
2611 // that is being updated with a screen shot
2612 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002613 } finally {
2614 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002615 }
John Reck34ef2672011-02-10 11:30:55 -08002616 return null;
2617 }
2618 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 }
2620
2621 private class Copy implements OnMenuItemClickListener {
2622 private CharSequence mText;
2623
John Reck9c35b9c2012-05-30 10:08:50 -07002624 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002625 public boolean onMenuItemClick(MenuItem item) {
2626 copy(mText);
2627 return true;
2628 }
2629
2630 public Copy(CharSequence toCopy) {
2631 mText = toCopy;
2632 }
2633 }
2634
Leon Scroggins63c02662010-11-18 15:16:27 -05002635 private static class Download implements OnMenuItemClickListener {
2636 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002637 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002638 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002639 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002640 private static final String FALLBACK_EXTENSION = "dat";
2641 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002642
John Reck9c35b9c2012-05-30 10:08:50 -07002643 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002644 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002645 if (DataUri.isDataUri(mText)) {
2646 saveDataUri();
2647 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002648 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002649 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002650 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002651 return true;
2652 }
2653
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002654 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2655 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002656 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002657 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002658 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002659 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002660 }
George Mount387d45d2011-10-07 15:57:53 -07002661
2662 /**
2663 * Treats mText as a data URI and writes its contents to a file
2664 * based on the current time.
2665 */
2666 private void saveDataUri() {
2667 FileOutputStream outputStream = null;
2668 try {
2669 DataUri uri = new DataUri(mText);
2670 File target = getTarget(uri);
2671 outputStream = new FileOutputStream(target);
2672 outputStream.write(uri.getData());
2673 final DownloadManager manager =
2674 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2675 manager.addCompletedDownload(target.getName(),
2676 mActivity.getTitle().toString(), false,
2677 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002678 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002679 } catch (IOException e) {
2680 Log.e(LOGTAG, "Could not save data URL");
2681 } finally {
2682 if (outputStream != null) {
2683 try {
2684 outputStream.close();
2685 } catch (IOException e) {
2686 // ignore close errors
2687 }
2688 }
2689 }
2690 }
2691
2692 /**
2693 * Creates a File based on the current time stamp and uses
2694 * the mime type of the DataUri to get the extension.
2695 */
2696 private File getTarget(DataUri uri) throws IOException {
2697 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002698 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002699 String nameBase = format.format(new Date());
2700 String mimeType = uri.getMimeType();
2701 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2702 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2703 if (extension == null) {
2704 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2705 extension = FALLBACK_EXTENSION;
2706 }
2707 extension = "." + extension; // createTempFile needs the '.'
2708 File targetFile = File.createTempFile(nameBase, extension, dir);
2709 return targetFile;
2710 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002711 }
2712
Cary Clark8974d282010-11-22 10:46:05 -05002713 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002714 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002715
John Reck9c35b9c2012-05-30 10:08:50 -07002716 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002717 public boolean onMenuItemClick(MenuItem item) {
2718 if (mWebView != null) {
2719 return mWebView.selectText();
2720 }
2721 return false;
2722 }
2723
2724 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002725 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002726 }
2727
2728 }
2729
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 /********************** TODO: UI stuff *****************************/
2731
2732 // these methods have been copied, they still need to be cleaned up
2733
2734 /****************** tabs ***************************************************/
2735
2736 // basic tab interactions:
2737
2738 // it is assumed that tabcontrol already knows about the tab
2739 protected void addTab(Tab tab) {
2740 mUi.addTab(tab);
2741 }
2742
2743 protected void removeTab(Tab tab) {
2744 mUi.removeTab(tab);
2745 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002746 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002747 }
2748
Michael Kolbf2055602011-04-09 17:20:03 -07002749 @Override
2750 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002751 // monkey protection against delayed start
2752 if (tab != null) {
2753 mTabControl.setCurrentTab(tab);
2754 // the tab is guaranteed to have a webview after setCurrentTab
2755 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002756 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002757 //Purge active tabs
2758 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002759 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002760 }
2761
John Reck8bcafc12011-08-29 16:43:02 -07002762 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002763 Tab current = mTabControl.getCurrentTab();
2764 if (current != null
2765 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002766 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002767 }
2768 }
2769
John Reck26b18322011-06-21 13:08:58 -07002770 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002771 // Dismiss the subwindow if applicable.
2772 dismissSubWindow(appTab);
2773 // Since we might kill the WebView, remove it from the
2774 // content view first.
2775 mUi.detachTab(appTab);
2776 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002777 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002778 // TODO: analyze why the remove and add are necessary
2779 mUi.attachTab(appTab);
2780 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002781 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002782 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002783 } else {
2784 // If the tab was the current tab, we have to attach
2785 // it to the view system again.
2786 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002787 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002788 }
2789 }
2790
2791 // Remove the sub window if it exists. Also called by TabControl when the
2792 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002793 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002794 public void dismissSubWindow(Tab tab) {
2795 removeSubWindow(tab);
2796 // dismiss the subwindow. This will destroy the WebView.
2797 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002798 WebView wv = getCurrentTopWebView();
2799 if (wv != null) {
2800 wv.requestFocus();
2801 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002802 }
2803
2804 @Override
2805 public void removeSubWindow(Tab t) {
2806 if (t.getSubWebView() != null) {
2807 mUi.removeSubWindow(t.getSubViewContainer());
2808 }
2809 }
2810
2811 @Override
2812 public void attachSubWindow(Tab tab) {
2813 if (tab.getSubWebView() != null) {
2814 mUi.attachSubWindow(tab.getSubViewContainer());
2815 getCurrentTopWebView().requestFocus();
2816 }
2817 }
2818
Mathew Inwood29721c22011-06-29 17:55:24 +01002819 private Tab showPreloadedTab(final UrlData urlData) {
2820 if (!urlData.isPreloaded()) {
2821 return null;
2822 }
2823 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2824 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2825 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002826 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002827 // Could not submit query. Fallback to regular tab creation
2828 tabControl.destroy();
2829 return null;
2830 }
2831 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002832 // check tab count and make room for new tab
2833 if (!mTabControl.canCreateNewTab()) {
2834 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2835 if (leastUsed != null) {
2836 closeTab(leastUsed);
2837 }
2838 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002839 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002840 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002841 mTabControl.addPreloadedTab(t);
2842 addTab(t);
2843 setActiveTab(t);
2844 return t;
2845 }
2846
Michael Kolb7bcafde2011-05-09 13:55:59 -07002847 // open a non inconito tab with the given url data
2848 // and set as active tab
2849 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002850 Tab tab = showPreloadedTab(urlData);
2851 if (tab == null) {
2852 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002853 if ((tab != null) && !urlData.isEmpty()) {
2854 loadUrlDataIn(tab, urlData);
2855 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002856 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002857 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002858 }
2859
Michael Kolb843510f2010-12-09 10:51:49 -08002860 @Override
2861 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002862 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002863 }
2864
Michael Kolb8233fac2010-10-26 16:08:53 -07002865 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002866 public Tab openIncognitoTab() {
2867 return openTab(INCOGNITO_URI, true, true, false);
2868 }
2869
2870 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002871 public Tab openTab(String url, boolean incognito, boolean setActive,
2872 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002873 return openTab(url, incognito, setActive, useCurrent, null);
2874 }
2875
2876 @Override
2877 public Tab openTab(String url, Tab parent, boolean setActive,
2878 boolean useCurrent) {
2879 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2880 setActive, useCurrent, parent);
2881 }
2882
2883 public Tab openTab(String url, boolean incognito, boolean setActive,
2884 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002885 Tab tab = createNewTab(incognito, setActive, useCurrent);
2886 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002887 if (parent instanceof SnapshotTab) {
2888 addTab(tab);
2889 if (setActive)
2890 setActiveTab(tab);
2891 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002892 parent.addChildTab(tab);
2893 }
Michael Kolb519d2282011-05-09 17:03:19 -07002894 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002895 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002896 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002897 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002898 return tab;
2899 }
2900
2901 // this method will attempt to create a new tab
2902 // incognito: private browsing tab
2903 // setActive: ste tab as current tab
2904 // useCurrent: if no new tab can be created, return current tab
2905 private Tab createNewTab(boolean incognito, boolean setActive,
2906 boolean useCurrent) {
2907 Tab tab = null;
2908 if (mTabControl.canCreateNewTab()) {
2909 tab = mTabControl.createNewTab(incognito);
2910 addTab(tab);
2911 if (setActive) {
2912 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002913 } else {
2914 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002915 }
2916 } else {
2917 if (useCurrent) {
2918 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002919 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002920 } else {
2921 mUi.showMaxTabsWarning();
2922 }
2923 }
2924 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002925 }
2926
John Reck2bc80422011-06-30 15:11:49 -07002927 @Override
2928 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2929 SnapshotTab tab = null;
2930 if (mTabControl.canCreateNewTab()) {
2931 tab = mTabControl.createSnapshotTab(snapshotId);
2932 addTab(tab);
2933 if (setActive) {
2934 setActiveTab(tab);
2935 }
2936 } else {
2937 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002938 }
2939 return tab;
2940 }
2941
Michael Kolb8233fac2010-10-26 16:08:53 -07002942 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002943 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002944 * @return boolean True if we successfully switched to a different tab. If
2945 * the indexth tab is null, or if that tab is the same as
2946 * the current one, return false.
2947 */
2948 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002949 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002950 Tab currentTab = mTabControl.getCurrentTab();
2951 if (tab == null || tab == currentTab) {
2952 return false;
2953 }
2954 setActiveTab(tab);
2955 return true;
2956 }
2957
2958 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002959 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002960 closeCurrentTab(false);
2961 }
2962
2963 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002964 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002965 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002966 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002967 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002968 return;
2969 }
Michael Kolbc831b632011-05-11 09:30:34 -07002970 final Tab current = mTabControl.getCurrentTab();
2971 final int pos = mTabControl.getCurrentPosition();
2972 Tab newTab = current.getParent();
2973 if (newTab == null) {
2974 newTab = mTabControl.getTab(pos + 1);
2975 if (newTab == null) {
2976 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002977 }
2978 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002979 if (andQuit) {
2980 mTabControl.setCurrentTab(newTab);
2981 closeTab(current);
2982 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002983 // Close window
2984 closeTab(current);
2985 }
2986 }
2987
2988 /**
2989 * Close the tab, remove its associated title bar, and adjust mTabControl's
2990 * current tab to a valid value.
2991 */
2992 @Override
2993 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002994 if (tab == mTabControl.getCurrentTab()) {
2995 closeCurrentTab();
2996 } else {
2997 removeTab(tab);
2998 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002999 }
3000
Afzal Najamd4e33312012-04-26 01:54:01 -04003001 /**
3002 * Close all tabs except the current one
3003 */
3004 @Override
3005 public void closeOtherTabs() {
3006 int inactiveTabs = mTabControl.getTabCount() - 1;
3007 for (int i = inactiveTabs; i >= 0; i--) {
3008 Tab tab = mTabControl.getTab(i);
3009 if (tab != mTabControl.getCurrentTab()) {
3010 removeTab(tab);
3011 }
3012 }
3013 }
3014
Michael Kolb8233fac2010-10-26 16:08:53 -07003015 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07003016 protected void loadUrlFromContext(String url) {
3017 Tab tab = getCurrentTab();
3018 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07003019 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07003020 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003021 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003022 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003023 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003024 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003025 }
3026 }
3027 }
3028
3029 /**
3030 * Load the URL into the given WebView and update the title bar
3031 * to reflect the new load. Call this instead of WebView.loadUrl
3032 * directly.
3033 * @param view The WebView used to load url.
3034 * @param url The URL to load.
3035 */
John Reck71e51422011-07-01 16:49:28 -07003036 @Override
3037 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003038 loadUrl(tab, url, null);
3039 }
3040
3041 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3042 if (tab != null) {
3043 dismissSubWindow(tab);
3044 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003045 if (tab.hasCrashed) {
3046 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3047 }
Michael Kolba53c9892011-10-05 13:31:40 -07003048 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003049 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003050 }
3051
3052 /**
3053 * Load UrlData into a Tab and update the title bar to reflect the new
3054 * load. Call this instead of UrlData.loadIn directly.
3055 * @param t The Tab used to load.
3056 * @param data The UrlData being loaded.
3057 */
3058 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003059 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003060 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003061 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003062 } else {
John Reck38b39652012-06-05 09:22:59 -07003063 if (t != null && data.mDisableUrlOverride) {
3064 t.disableUrlOverridingForLoad();
3065 }
John Reck26b18322011-06-21 13:08:58 -07003066 loadUrl(t, data.mUrl, data.mHeaders);
3067 }
3068 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003069 }
3070
John Reck30c714c2010-12-16 17:30:34 -08003071 @Override
3072 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003073 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003074 if (tab.canGoBack()) {
3075 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003076 } else {
John Reckef654f12011-07-12 16:42:08 -07003077 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003078 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003079 }
3080
3081 void goBackOnePageOrQuit() {
3082 Tab current = mTabControl.getCurrentTab();
3083 if (current == null) {
3084 /*
3085 * Instead of finishing the activity, simply push this to the back
3086 * of the stack and let ActivityManager to choose the foreground
3087 * activity. As BrowserActivity is singleTask, it will be always the
3088 * root of the task. So we can use either true or false for
3089 * moveTaskToBack().
3090 */
luxiaolb40014b2013-07-19 10:01:43 +08003091 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003092 return;
3093 }
John Reckef654f12011-07-12 16:42:08 -07003094 if (current.canGoBack()) {
3095 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003096 } else {
3097 // Check to see if we are closing a window that was created by
3098 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003099 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003100 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003101 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003102 // Now we close the other tab
3103 closeTab(current);
3104 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003105 /*
3106 * Instead of finishing the activity, simply push this to the back
3107 * of the stack and let ActivityManager to choose the foreground
3108 * activity. As BrowserActivity is singleTask, it will be always the
3109 * root of the task. So we can use either true or false for
3110 * moveTaskToBack().
3111 */
luxiaolb40014b2013-07-19 10:01:43 +08003112 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003113 }
3114 }
3115 }
3116
3117 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003118 * helper method for key handler
3119 * returns the current tab if it can't advance
3120 */
Michael Kolbc831b632011-05-11 09:30:34 -07003121 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003122 int pos = mTabControl.getCurrentPosition() + 1;
3123 if (pos >= mTabControl.getTabCount()) {
3124 pos = 0;
3125 }
3126 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003127 }
3128
3129 /**
3130 * helper method for key handler
3131 * returns the current tab if it can't advance
3132 */
Michael Kolbc831b632011-05-11 09:30:34 -07003133 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003134 int pos = mTabControl.getCurrentPosition() - 1;
3135 if ( pos < 0) {
3136 pos = mTabControl.getTabCount() - 1;
3137 }
3138 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003139 }
3140
John Reckbcef87f2012-02-03 14:58:34 -08003141 boolean isMenuOrCtrlKey(int keyCode) {
3142 return (KeyEvent.KEYCODE_MENU == keyCode)
3143 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3144 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3145 }
3146
Michael Kolb0035fad2011-03-14 13:25:28 -07003147 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003148 * handle key events in browser
3149 *
3150 * @param keyCode
3151 * @param event
3152 * @return true if handled, false to pass to super
3153 */
John Reck9c35b9c2012-05-30 10:08:50 -07003154 @Override
3155 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003156 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003157 // Even if MENU is already held down, we need to call to super to open
3158 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003159 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003160 mMenuIsDown = true;
3161 return false;
3162 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003163
Cary Clark8ff8c662010-12-29 15:03:05 -05003164 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003165 Tab tab = getCurrentTab();
3166 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003167
Cary Clark160bbb92011-01-10 11:17:07 -05003168 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3169 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003170
Michael Kolb8233fac2010-10-26 16:08:53 -07003171 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003172 case KeyEvent.KEYCODE_TAB:
3173 if (event.isCtrlPressed()) {
3174 if (event.isShiftPressed()) {
3175 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003176 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003177 } else {
3178 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003179 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003180 }
3181 return true;
3182 }
3183 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003184 case KeyEvent.KEYCODE_SPACE:
3185 // WebView/WebTextView handle the keys in the KeyDown. As
3186 // the Activity's shortcut keys are only handled when WebView
3187 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003188 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003189 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003190 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003191 pageDown();
3192 }
3193 return true;
3194 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003195 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003196 event.startTracking();
3197 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003198 case KeyEvent.KEYCODE_FORWARD:
3199 if (!noModifiers) break;
3200 tab.goForward();
3201 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003202 case KeyEvent.KEYCODE_DPAD_LEFT:
3203 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003204 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003205 return true;
3206 }
3207 break;
3208 case KeyEvent.KEYCODE_DPAD_RIGHT:
3209 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003210 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003211 return true;
3212 }
3213 break;
3214 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003215 if (ctrl) {
3216 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003217 return true;
3218 }
3219 break;
Michael Kolba4183062011-01-16 10:43:21 -08003220// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003221 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003222 if (ctrl ) {
3223 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003224 return true;
3225 }
3226 break;
Michael Kolba4183062011-01-16 10:43:21 -08003227// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003228// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003229// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003230// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003231// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003232// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003233// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003234// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003235// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003236// case KeyEvent.KEYCODE_M: // unused
3237// case KeyEvent.KEYCODE_N: // in Chrome: new window
3238// case KeyEvent.KEYCODE_O: // in Chrome: open file
3239// case KeyEvent.KEYCODE_P: // in Chrome: print page
3240// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003241// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003242// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3243 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003244 // we can't use the ctrl/shift flags, they check for
3245 // exclusive use of a modifier
3246 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003247 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003248 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003249 } else {
3250 openTabToHomePage();
3251 }
3252 return true;
3253 }
3254 break;
3255// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3256// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003257// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003258// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3259// case KeyEvent.KEYCODE_Y: // unused
3260// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003261 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003262 // it is a regular key and webview is not null
3263 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003264 }
3265
John Reck9c35b9c2012-05-30 10:08:50 -07003266 @Override
3267 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003268 switch(keyCode) {
3269 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003270 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003271 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003272 return true;
3273 }
3274 break;
3275 }
3276 return false;
3277 }
3278
John Reck9c35b9c2012-05-30 10:08:50 -07003279 @Override
3280 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003281 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003282 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003283 if (KeyEvent.KEYCODE_MENU == keyCode
3284 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003285 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003286 }
3287 }
Cary Clark160bbb92011-01-10 11:17:07 -05003288 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003289 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003290 case KeyEvent.KEYCODE_BACK:
3291 if (event.isTracking() && !event.isCanceled()) {
3292 onBackKey();
3293 return true;
3294 }
3295 break;
3296 }
3297 return false;
3298 }
3299
3300 public boolean isMenuDown() {
3301 return mMenuIsDown;
3302 }
3303
John Reck9c35b9c2012-05-30 10:08:50 -07003304 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003305 public void setupAutoFill(Message message) {
3306 // Open the settings activity at the AutoFill profile fragment so that
3307 // the user can create a new profile. When they return, we will dispatch
3308 // the message so that we can autofill the form using their new profile.
3309 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3310 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3311 AutoFillSettingsFragment.class.getName());
3312 mAutoFillSetupMessage = message;
3313 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3314 }
John Reckb3417f02011-01-14 11:01:05 -08003315
John Reck9c35b9c2012-05-30 10:08:50 -07003316 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003317 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003318 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003319 return true;
3320 }
3321
John Reck1cf4b792011-07-26 10:22:22 -07003322 @Override
3323 public boolean shouldCaptureThumbnails() {
3324 return mUi.shouldCaptureThumbnails();
3325 }
3326
Michael Kolbc3af0672011-08-09 10:24:41 -07003327 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003328 public boolean supportsVoice() {
3329 PackageManager pm = mActivity.getPackageManager();
3330 List activities = pm.queryIntentActivities(new Intent(
3331 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3332 return activities.size() != 0;
3333 }
3334
3335 @Override
3336 public void startVoiceRecognizer() {
3337 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003338 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003339 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3340 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3341 mActivity.startActivityForResult(voice, VOICE_RESULT);
3342 }
3343
3344 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003345 public void setBlockEvents(boolean block) {
3346 mBlockEvents = block;
3347 }
3348
John Reck9c35b9c2012-05-30 10:08:50 -07003349 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003350 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003351 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003352 }
3353
John Reck9c35b9c2012-05-30 10:08:50 -07003354 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003355 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003356 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003357 }
3358
John Reck9c35b9c2012-05-30 10:08:50 -07003359 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003360 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003361 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003362 }
3363
John Reck9c35b9c2012-05-30 10:08:50 -07003364 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003365 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003366 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003367 }
3368
John Reck9c35b9c2012-05-30 10:08:50 -07003369 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003370 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003371 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003372 }
3373
Michael Kolb8233fac2010-10-26 16:08:53 -07003374}