blob: 9b22b081b7cfae764a440aa66e9ae802eaf0874b [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080020
luxiaolb40014b2013-07-19 10:01:43 +080021import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.ProgressDialog;
Vivek Sekharce2a4832014-03-26 13:26:53 -070025import android.content.Context;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070028import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.content.ContentValues;
John Reck68234a92012-04-19 15:27:12 -070030import android.content.DialogInterface;
31import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070032import android.content.Intent;
33import android.content.pm.PackageManager;
34import android.content.pm.ResolveInfo;
35import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070036import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050037import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.database.Cursor;
39import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020040import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080042import android.graphics.BitmapFactory;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Canvas;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080044import android.graphics.Paint;
45import android.graphics.Rect;
kaiyiza016da12013-08-26 17:50:22 +080046import android.net.ConnectivityManager;
47import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.net.Uri;
49import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080050import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080051import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.os.AsyncTask;
53import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070054import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.os.Handler;
56import android.os.Message;
57import android.os.PowerManager;
58import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.provider.ContactsContract;
61import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080062import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070063import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070064import android.text.TextUtils;
65import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080066import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070067import android.view.ActionMode;
68import android.view.ContextMenu;
69import android.view.ContextMenu.ContextMenuInfo;
70import android.view.Gravity;
71import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070072import android.view.Menu;
73import android.view.MenuInflater;
74import android.view.MenuItem;
75import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070076import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070077import android.view.View;
George Mount387d45d2011-10-07 15:57:53 -070078import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070079import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import android.webkit.WebChromeClient.CustomViewCallback;
Michael Kolb8233fac2010-10-26 16:08:53 -070081import android.webkit.WebIconDatabase;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import org.codeaurora.swe.CookieManager;
85import org.codeaurora.swe.CookieSyncManager;
86import org.codeaurora.swe.HttpAuthHandler;
87import org.codeaurora.swe.SslErrorHandler;
88import org.codeaurora.swe.WebSettings;
89import org.codeaurora.swe.WebView;
90
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070091import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070092import com.android.browser.R;
93import com.android.browser.IntentHandler.UrlData;
94import com.android.browser.UI.ComboViews;
95import com.android.browser.mynavigation.AddMyNavigationPage;
96import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070097import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070098import com.android.browser.platformsupport.BrowserContract;
99import com.android.browser.platformsupport.WebAddress;
100import com.android.browser.platformsupport.BrowserContract.Images;
101import com.android.browser.provider.BrowserProvider2.Thumbnails;
102import com.android.browser.provider.SnapshotProvider.Snapshots;
103import com.android.browser.reflect.ReflectHelper;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700104
Michael Kolb8233fac2010-10-26 16:08:53 -0700105import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700106import java.io.File;
107import java.io.FileOutputStream;
108import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700109import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700110import java.text.DateFormat;
111import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700112import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700113import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700114import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800116import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200117import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700118import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700119
120/**
121 * Controller for browser
122 */
123public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700124 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700125
126 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800127 private static final String SEND_APP_ID_EXTRA =
128 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700129 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800130
Vivek Sekharb54614f2014-05-01 19:03:37 -0700131 // Remind switch to data connection if wifi is unavailable
132 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800133
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 // public message ids
135 public final static int LOAD_URL = 1001;
136 public final static int STOP_LOAD = 1002;
137
138 // Message Ids
139 private static final int FOCUS_NODE_HREF = 102;
140 private static final int RELEASE_WAKELOCK = 107;
141
142 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
143
144 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800145 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700146
147 private static final int EMPTY_MENU = -1;
148
Michael Kolb8233fac2010-10-26 16:08:53 -0700149 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700150 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 final static int PREFERENCES_PAGE = 3;
152 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000153 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700154 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800155 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000156
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
158
159 // As the ids are dynamically created, we can't guarantee that they will
160 // be in sequence, so this static array maps ids to a window number.
161 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
162 { R.id.window_one_menu_id, R.id.window_two_menu_id,
163 R.id.window_three_menu_id, R.id.window_four_menu_id,
164 R.id.window_five_menu_id, R.id.window_six_menu_id,
165 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
166
167 // "source" parameter for Google search through search key
168 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
169 // "source" parameter for Google search through simplily type
170 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
171
George Mount387d45d2011-10-07 15:57:53 -0700172 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700173 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
174
John Reckd7dd9b22011-08-30 09:18:29 -0700175 // A bitmap that is re-used in createScreenshot as scratch space
176 private static Bitmap sThumbnailBitmap;
177
Michael Kolb8233fac2010-10-26 16:08:53 -0700178 private Activity mActivity;
179 private UI mUi;
180 private TabControl mTabControl;
181 private BrowserSettings mSettings;
182 private WebViewFactory mFactory;
183
184 private WakeLock mWakeLock;
185
186 private UrlHandler mUrlHandler;
187 private UploadHandler mUploadHandler;
188 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 private PageDialogsHandler mPageDialogsHandler;
190 private NetworkStateHandler mNetworkHandler;
191
Ben Murdoch8029a772010-11-16 11:58:21 +0000192 private Message mAutoFillSetupMessage;
193
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800195 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700196
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 // FIXME, temp address onPrepareMenu performance problem.
198 // When we move everything out of view, we should rewrite this.
199 private int mCurrentMenuState = 0;
200 private int mMenuState = R.id.MAIN_MENU;
201 private int mOldMenuState = EMPTY_MENU;
202 private Menu mCachedMenu;
203
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 private boolean mMenuIsDown;
205
206 // For select and find, we keep track of the ActionMode so that
207 // finish() can be called as desired.
208 private ActionMode mActionMode;
209
210 /**
211 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
212 * of whether the configuration has changed. The first onMenuOpened call
213 * after a configuration change is simply a reopening of the same menu
214 * (i.e. mIconView did not change).
215 */
216 private boolean mConfigChanged;
217
218 /**
219 * Keeps track of whether the options menu is open. This is important in
220 * determining whether to show or hide the title bar overlay
221 */
222 private boolean mOptionsMenuOpen;
223
224 /**
225 * Whether or not the options menu is in its bigger, popup menu form. When
226 * true, we want the title bar overlay to be gone. When false, we do not.
227 * Only meaningful if mOptionsMenuOpen is true.
228 */
229 private boolean mExtendedMenuOpen;
230
Michael Kolb8233fac2010-10-26 16:08:53 -0700231 private boolean mActivityPaused = true;
232 private boolean mLoadStopped;
233
234 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500235 // Checks to see when the bookmarks database has changed, and updates the
236 // Tabs' notion of whether they represent bookmarked sites.
237 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700238 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700239
Michael Kolbc3af0672011-08-09 10:24:41 -0700240 private boolean mBlockEvents;
241
Michael Kolb0b129122012-06-04 16:31:58 -0700242 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800243 private boolean mUpdateMyNavThumbnail;
244 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700245
George Mount3636d0a2011-11-21 09:08:21 -0800246 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700247 mActivity = browser;
248 mSettings = BrowserSettings.getInstance();
249 mTabControl = new TabControl(this);
250 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700251 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800252 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700253 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700254
255 mUrlHandler = new UrlHandler(this);
256 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700257 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
258
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500260 mBookmarksObserver = new ContentObserver(mHandler) {
261 @Override
262 public void onChange(boolean selfChange) {
263 int size = mTabControl.getTabCount();
264 for (int i = 0; i < size; i++) {
265 mTabControl.getTab(i).updateBookmarkedStatus();
266 }
267 }
268
269 };
270 browser.getContentResolver().registerContentObserver(
271 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700272
273 mNetworkHandler = new NetworkStateHandler(mActivity, this);
John Reckaf262e72011-07-25 13:55:44 -0700274 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 }
276
John Reck9c35b9c2012-05-30 10:08:50 -0700277 @Override
278 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800279 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800280 // mCrashRecoverHandler has any previously saved state.
281 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700282 }
283
George Mount3636d0a2011-11-21 09:08:21 -0800284 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700285 // Unless the last browser usage was within 24 hours, destroy any
286 // remaining incognito tabs.
287
288 Calendar lastActiveDate = icicle != null ?
289 (Calendar) icicle.getSerializable("lastActiveDate") : null;
290 Calendar today = Calendar.getInstance();
291 Calendar yesterday = Calendar.getInstance();
292 yesterday.add(Calendar.DATE, -1);
293
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800294 // we dont want to ever recover incognito tabs
295 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700296
Patrick Scott7d50a932011-02-04 09:27:26 -0500297 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700298 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500299 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000300
Michael Kolbc831b632011-05-11 09:30:34 -0700301 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500302 // Not able to restore so we go ahead and clear session cookies. We
303 // must do this before trying to login the user as we don't want to
304 // clear any session cookies set during login.
305 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700306 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800307 if (intent == null) {
308 // This won't happen under common scenarios. The icicle is
309 // not null, but there aren't any tabs to restore.
310 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700311 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800312 final Bundle extra = intent.getExtras();
313 // Create an initial tab.
314 // If the intent is ACTION_VIEW and data is not null, the Browser is
315 // invoked to view the content by another application. In this case,
316 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800317 UrlData urlData = null;
318 if (intent.getData() != null
319 && Intent.ACTION_VIEW.equals(intent.getAction())
320 && intent.getData().toString().startsWith("content://")) {
321 urlData = new UrlData(intent.getData().toString());
322 } else {
323 urlData = IntentHandler.getUrlDataFromIntent(intent);
324 }
George Mount3636d0a2011-11-21 09:08:21 -0800325 Tab t = null;
326 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700327 String landingPage = mActivity.getResources().getString(
328 R.string.def_landing_page);
329 if (!landingPage.isEmpty()) {
330 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800331 } else {
332 t = openTabToHomePage();
333 }
George Mount3636d0a2011-11-21 09:08:21 -0800334 } else {
335 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700336 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800337 }
338 if (t != null) {
339 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
340 }
341 WebView webView = t.getWebView();
342 if (extra != null) {
343 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
344 if (scale > 0 && scale <= 1000) {
345 webView.setInitialScale(scale);
346 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700347 }
348 }
John Reckd8c74522011-06-14 08:45:00 -0700349 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700350 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700351 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500352 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700353 List<Tab> tabs = mTabControl.getTabs();
354 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
355 for (Tab t : tabs) {
356 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700357 if (t != mTabControl.getCurrentTab()) {
358 t.pause();
359 }
John Reck1cf4b792011-07-26 10:22:22 -0700360 }
361 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700362 if (tabs.size() == 0) {
363 openTabToHomePage();
364 }
John Reck1cf4b792011-07-26 10:22:22 -0700365 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 // TabControl.restoreState() will create a new tab even if
367 // restoring the state fails.
368 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800369 // Intent is non-null when framework thinks the browser should be
370 // launching with a new intent (icicle is null).
371 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700372 mIntentHandler.onNewIntent(intent);
373 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700374 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700375 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700376 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800377 if (jsFlags.trim().length() != 0) {
378 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 }
George Mount3636d0a2011-11-21 09:08:21 -0800380 if (intent != null
381 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700382 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800383 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700384 }
385
John Reck1cf4b792011-07-26 10:22:22 -0700386 private static class PruneThumbnails implements Runnable {
387 private Context mContext;
388 private List<Long> mIds;
389
390 PruneThumbnails(Context context, List<Long> preserveIds) {
391 mContext = context.getApplicationContext();
392 mIds = preserveIds;
393 }
394
395 @Override
396 public void run() {
397 ContentResolver cr = mContext.getContentResolver();
398 if (mIds == null || mIds.size() == 0) {
399 cr.delete(Thumbnails.CONTENT_URI, null, null);
400 } else {
401 int length = mIds.size();
402 StringBuilder where = new StringBuilder();
403 where.append(Thumbnails._ID);
404 where.append(" not in (");
405 for (int i = 0; i < length; i++) {
406 where.append(mIds.get(i));
407 if (i < (length - 1)) {
408 where.append(",");
409 }
410 }
411 where.append(")");
412 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
413 }
414 }
415
416 }
417
Michael Kolb1514bb72010-11-22 09:11:48 -0800418 @Override
419 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700420 return mFactory;
421 }
422
423 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800424 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800425 if (tab.hasCrashed)
426 tab.showCrashView();
427 else
428 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800429 }
430
431 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800432 public void createSubWindow(Tab tab) {
433 endActionMode();
434 WebView mainView = tab.getWebView();
435 WebView subView = mFactory.createWebView((mainView == null)
436 ? false
437 : mainView.isPrivateBrowsingEnabled());
438 mUi.createSubWindow(tab, subView);
439 }
440
441 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700442 public Context getContext() {
443 return mActivity;
444 }
445
446 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700447 public Activity getActivity() {
448 return mActivity;
449 }
450
451 void setUi(UI ui) {
452 mUi = ui;
453 }
454
Michael Kolbaf63dba2012-05-16 12:58:05 -0700455 @Override
456 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700457 return mSettings;
458 }
459
460 IntentHandler getIntentHandler() {
461 return mIntentHandler;
462 }
463
464 @Override
465 public UI getUi() {
466 return mUi;
467 }
468
469 int getMaxTabs() {
470 return mActivity.getResources().getInteger(R.integer.max_tabs);
471 }
472
473 @Override
474 public TabControl getTabControl() {
475 return mTabControl;
476 }
477
Michael Kolb1bf23132010-11-19 12:55:12 -0800478 @Override
479 public List<Tab> getTabs() {
480 return mTabControl.getTabs();
481 }
482
John Reckaf262e72011-07-25 13:55:44 -0700483 // Open the icon database.
484 private void openIconDatabase() {
485 // We have to call getInstance on the UI thread
486 final WebIconDatabase instance = WebIconDatabase.getInstance();
487 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000488
John Reckaf262e72011-07-25 13:55:44 -0700489 @Override
490 public void run() {
491 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700492 }
John Reckaf262e72011-07-25 13:55:44 -0700493 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700494 }
495
496 private void startHandler() {
497 mHandler = new Handler() {
498
499 @Override
500 public void handleMessage(Message msg) {
501 switch (msg.what) {
502 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700503 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 break;
505 case FOCUS_NODE_HREF:
506 {
507 String url = (String) msg.getData().get("url");
508 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500509 String src = (String) msg.getData().get("src");
510 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 if (TextUtils.isEmpty(url)) {
512 break;
513 }
514 HashMap focusNodeMap = (HashMap) msg.obj;
515 WebView view = (WebView) focusNodeMap.get("webview");
516 // Only apply the action if the top window did not change.
517 if (getCurrentTopWebView() != view) {
518 break;
519 }
520 switch (msg.arg1) {
521 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700522 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500524 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700525 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500526 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500527 case R.id.open_newtab_context_menu_id:
528 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700529 openTab(url, parent,
530 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500531 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700532 case R.id.copy_link_context_menu_id:
533 copy(url);
534 break;
535 case R.id.save_link_context_menu_id:
536 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500537 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800538 mActivity, url, view.getSettings().getUserAgentString(),
539 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700541 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700542 if(title == null || title == "")
543 title = url;
544
545 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
546 //SWE TODO: No thumbnail support for the url obtained via
547 //browser context menu as its not loaded in webview.
548 if (bookmarkIntent != null) {
549 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
550 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
551 mActivity.startActivity(bookmarkIntent);
552 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700553 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 }
555 break;
556 }
557
558 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700559 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 break;
561
562 case STOP_LOAD:
563 stopLoading();
564 break;
565
566 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700567 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700568 mWakeLock.release();
569 // if we reach here, Browser should be still in the
570 // background loading after WAKELOCK_TIMEOUT (5-min).
571 // To avoid burning the battery, stop loading.
572 mTabControl.stopAllLoading();
573 }
574 break;
575
576 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800577 Tab tab = (Tab) msg.obj;
578 if (tab != null) {
579 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 }
581 break;
kaiyiz591110b2013-08-06 17:11:06 +0800582 case OPEN_MENU:
583 if (!mOptionsMenuOpen && mActivity != null ) {
584 mActivity.openOptionsMenu();
585 }
586 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700587 }
588 }
589 };
590
591 }
592
John Reckef654f12011-07-12 16:42:08 -0700593 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200594 public Tab getCurrentTab() {
595 return mTabControl.getCurrentTab();
596 }
597
Michael Kolbba99c5d2010-11-29 14:57:41 -0800598 @Override
599 public void shareCurrentPage() {
600 shareCurrentPage(mTabControl.getCurrentTab());
601 }
602
603 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700604 if (tab == null || tab.getWebView() == null)
605 return;
606
607 final Tab mytab = tab;
608 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
609 @Override
610 public void onReceiveValue(Bitmap bitmap) {
611 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
612 mytab.getFavicon(), bitmap);
613 }
614 };
615
616 createScreenshotAsync(
617 tab.getWebView(),
618 getDesiredThumbnailWidth(mActivity),
619 getDesiredThumbnailHeight(mActivity),
620 new ValueCallback<Bitmap>() {
621 @Override
622 public void onReceiveValue(Bitmap bitmap) {
623 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
624 mytab.getFavicon(), bitmap);
625 }
626 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800627 }
628
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 /**
630 * Share a page, providing the title, url, favicon, and a screenshot. Uses
631 * an {@link Intent} to launch the Activity chooser.
632 * @param c Context used to launch a new Activity.
633 * @param title Title of the page. Stored in the Intent with
634 * {@link Intent#EXTRA_SUBJECT}
635 * @param url URL of the page. Stored in the Intent with
636 * {@link Intent#EXTRA_TEXT}
637 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
638 * with {@link Browser#EXTRA_SHARE_FAVICON}
639 * @param screenshot Bitmap of a screenshot of the page. Stored in the
640 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
641 */
642 static final void sharePage(Context c, String title, String url,
643 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700644
645 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
646 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
647 R.layout.app_row, sDialog.getApps());
648 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700649 }
650
651 private void copy(CharSequence text) {
652 ClipboardManager cm = (ClipboardManager) mActivity
653 .getSystemService(Context.CLIPBOARD_SERVICE);
654 cm.setText(text);
655 }
656
657 // lifecycle
658
John Reck9c35b9c2012-05-30 10:08:50 -0700659 @Override
660 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800662 // update the menu in case of a locale change
663 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800664 if (mOptionsMenuOpen) {
665 mActivity.closeOptionsMenu();
666 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
667 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700668 if (mPageDialogsHandler != null) {
669 mPageDialogsHandler.onConfigurationChanged(config);
670 }
671 mUi.onConfigurationChanged(config);
672 }
673
674 @Override
675 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700676 if (!mUi.isWebShowing()) {
677 mUi.showWeb(false);
678 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 mIntentHandler.onNewIntent(intent);
680 }
681
John Reck9c35b9c2012-05-30 10:08:50 -0700682 @Override
683 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800684 if (mUi.isCustomViewShowing()) {
685 hideCustomView();
686 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 if (mActivityPaused) {
688 Log.e(LOGTAG, "BrowserActivity is already paused.");
689 return;
690 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700691 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800692 Tab tab = mTabControl.getCurrentTab();
693 if (tab != null) {
694 tab.pause();
695 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700696 if (mWakeLock == null) {
697 PowerManager pm = (PowerManager) mActivity
698 .getSystemService(Context.POWER_SERVICE);
699 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
700 }
Michael Kolb70976932010-11-30 11:34:01 -0800701 mWakeLock.acquire();
702 mHandler.sendMessageDelayed(mHandler
703 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
704 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 }
706 mUi.onPause();
707 mNetworkHandler.onPause();
708
709 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100710 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700711 }
712
John Reck9c35b9c2012-05-30 10:08:50 -0700713 @Override
714 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800716 Bundle saveState = createSaveState();
717
718 // crash recovery manages all save & restore state
719 mCrashRecoveryHandler.writeState(saveState);
720 mSettings.setLastRunPaused(true);
721 }
722
723 /**
724 * Save the current state to outState. Does not write the state to
725 * disk.
726 * @return Bundle containing the current state of all tabs.
727 */
728 /* package */ Bundle createSaveState() {
729 Bundle saveState = new Bundle();
730 mTabControl.saveState(saveState);
731 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700732 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800733 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700734 }
George Mount3636d0a2011-11-21 09:08:21 -0800735 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700736 }
737
John Reck9c35b9c2012-05-30 10:08:50 -0700738 @Override
739 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700740 if (!mActivityPaused) {
741 Log.e(LOGTAG, "BrowserActivity is already resumed.");
742 return;
743 }
George Mount3636d0a2011-11-21 09:08:21 -0800744 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800746 Tab current = mTabControl.getCurrentTab();
747 if (current != null) {
748 current.resume();
749 resumeWebViewTimers(current);
750 }
John Reckf57c0292011-07-21 18:15:39 -0700751 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200752
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 mUi.onResume();
754 mNetworkHandler.onResume();
755 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100756 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700757 if (mVoiceResult != null) {
758 mUi.onVoiceResult(mVoiceResult);
759 mVoiceResult = null;
760 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800761 if (current != null && current.hasCrashed) {
762 current.showCrashView();
763 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 }
765
John Reckf57c0292011-07-21 18:15:39 -0700766 private void releaseWakeLock() {
767 if (mWakeLock != null && mWakeLock.isHeld()) {
768 mHandler.removeMessages(RELEASE_WAKELOCK);
769 mWakeLock.release();
770 }
771 }
772
Michael Kolb70976932010-11-30 11:34:01 -0800773 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800774 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800775 * @param tab guaranteed non-null
776 */
777 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700778 boolean inLoad = tab.inPageLoad();
779 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
780 CookieSyncManager.getInstance().startSync();
781 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100782 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700783 }
784 }
785
Michael Kolb70976932010-11-30 11:34:01 -0800786 /**
787 * Pause all WebView timers using the WebView of the given tab
788 * @param tab
789 * @return true if the timers are paused or tab is null
790 */
791 private boolean pauseWebViewTimers(Tab tab) {
792 if (tab == null) {
793 return true;
794 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100796 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700797 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 }
Michael Kolb70976932010-11-30 11:34:01 -0800799 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 }
801
John Reck9c35b9c2012-05-30 10:08:50 -0700802 @Override
803 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800804 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
806 mUploadHandler = null;
807 }
808 if (mTabControl == null) return;
809 mUi.onDestroy();
810 // Remove the current tab and sub window
811 Tab t = mTabControl.getCurrentTab();
812 if (t != null) {
813 dismissSubWindow(t);
814 removeTab(t);
815 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500816 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700817 // Destroy all the tabs
818 mTabControl.destroy();
819 WebIconDatabase.getInstance().close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 }
821
822 protected boolean isActivityPaused() {
823 return mActivityPaused;
824 }
825
John Reck9c35b9c2012-05-30 10:08:50 -0700826 @Override
827 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700828 mTabControl.freeMemory();
829 }
830
831 @Override
832 public boolean shouldShowErrorConsole() {
833 return mShouldShowErrorConsole;
834 }
835
836 protected void setShouldShowErrorConsole(boolean show) {
837 if (show == mShouldShowErrorConsole) {
838 // Nothing to do.
839 return;
840 }
841 mShouldShowErrorConsole = show;
842 Tab t = mTabControl.getCurrentTab();
843 if (t == null) {
844 // There is no current tab so we cannot toggle the error console
845 return;
846 }
847 mUi.setShouldShowErrorConsole(t, show);
848 }
849
850 @Override
851 public void stopLoading() {
852 mLoadStopped = true;
853 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700855 if (w != null) {
856 w.stopLoading();
857 mUi.onPageStopped(tab);
858 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 }
860
861 boolean didUserStopLoading() {
862 return mLoadStopped;
863 }
864
kaiyiza016da12013-08-26 17:50:22 +0800865 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700866 final String reminderType = getContext().getResources().getString(
867 R.string.def_wifi_browser_interaction_remind_type);
868 final String selectionConnnection = getContext().getResources().getString(
869 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700870 final String wifiSelection = getContext().getResources().getString(
871 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700872
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700873 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
874 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700875 return;
876
kaiyiza016da12013-08-26 17:50:22 +0800877 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700878 Context.CONNECTIVITY_SERVICE);
879 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700880 WifiManager wifiMgr = (WifiManager) this.getContext()
881 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700882 if (networkInfo == null
883 || (networkInfo != null && (networkInfo.getType() !=
884 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700885 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
886 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700887 List<ScanResult> list = wifiMgr.getScanResults();
888 // Have no AP's for Wifi's fall back to data
889 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700890 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700891 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
892 this.getContext().startActivity(intent);
893 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700894 // Request to select Wifi AP
895 try {
896 Intent intent = new Intent(wifiSelection);
897 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
898 this.getContext().startActivity(intent);
899 } catch (Exception e) {
900 String err_msg = this.getContext().getString(
901 R.string.acivity_not_found, wifiSelection);
902 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
903 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700904 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700905 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800906 }
907 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700908
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 // WebViewController
910
911 @Override
John Reck324d4402011-01-11 16:56:42 -0800912 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700913
914 // We've started to load a new page. If there was a pending message
915 // to save a screenshot then we will now take the new page and save
916 // an incorrect screenshot. Therefore, remove any pending thumbnail
917 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700918 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700919
920 // reset sync timer to avoid sync starts during loading a page
921 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700922 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700923 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700924 boolean networkNotifier =
925 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700926 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700927 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800928 } else {
929 if (!mNetworkHandler.isNetworkUp()) {
930 view.setNetworkAvailable(false);
931 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700932 }
933
934 // when BrowserActivity just starts, onPageStarted may be called before
935 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
936 // to start the timer. As we won't switch tabs while an activity is in
937 // pause state, we can ensure calling resume and pause in pair.
938 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800939 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700940 }
941 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 endActionMode();
943
John Reck30c714c2010-12-16 17:30:34 -0800944 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700945
John Reck324d4402011-01-11 16:56:42 -0800946 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700947 // update the bookmark database for favicon
948 maybeUpdateFavicon(tab, null, url, favicon);
949
950 Performance.tracePageStart(url);
951
952 // Performance probe
953 if (false) {
954 Performance.onPageStarted();
955 }
956
957 }
958
959 @Override
John Reck324d4402011-01-11 16:56:42 -0800960 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700961 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800962 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200963
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 // Performance probe
965 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800966 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 }
968
Tarun Nainani8eb00912014-07-17 12:28:32 -0700969 tab.onPageFinished();
970
Michael Kolb8233fac2010-10-26 16:08:53 -0700971 Performance.tracePageFinished();
972 }
973
974 @Override
John Reck30c714c2010-12-16 17:30:34 -0800975 public void onProgressChanged(Tab tab) {
976 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700977
978 if (newProgress == 100) {
979 CookieSyncManager.getInstance().sync();
980 // onProgressChanged() may continue to be called after the main
981 // frame has finished loading, as any remaining sub frames continue
982 // to load. We'll only get called once though with newProgress as
983 // 100 when everything is loaded. (onPageFinished is called once
984 // when the main frame completes loading regardless of the state of
985 // any sub frames so calls to onProgressChanges may continue after
986 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800987 if (tab.inPageLoad()) {
988 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200989 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
990 // pause the WebView timer and release the wake lock if it is
991 // finished while BrowserActivity is in pause state.
992 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700993 }
John Reckd9862372012-02-21 15:04:50 -0800994 if (!tab.isPrivateBrowsingEnabled()
995 && !TextUtils.isEmpty(tab.getUrl())
996 && !tab.isSnapshot()) {
997 // Only update the bookmark screenshot if the user did not
998 // cancel the load early and there is not already
999 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001000 if (tab.shouldUpdateThumbnail() &&
1001 (tab.inForeground() && !didUserStopLoading()
1002 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001003 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1004 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1005 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001006 1500);
John Reckd9862372012-02-21 15:04:50 -08001007 }
1008 }
1009 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001010 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001011 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001012 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001013 // still loading
1014 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001015 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001016 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001017 }
1018 }
John Reck30c714c2010-12-16 17:30:34 -08001019 mUi.onProgressChanged(tab);
1020 }
1021
1022 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001023 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001024 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001025 }
1026
1027 @Override
1028 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001029 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001030 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001031 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001032 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1033 return;
1034 }
1035 // Update the title in the history database if not in private browsing mode
1036 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001037 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 }
1039 }
1040
1041 @Override
1042 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001043 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1045 }
1046
1047 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001048 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1049 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 }
1051
1052 @Override
1053 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1054 if (mMenuIsDown) {
1055 // only check shortcut key when MENU is held
1056 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1057 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001058 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001059 int keyCode = event.getKeyCode();
1060 // We need to send almost every key to WebKit. However:
1061 // 1. We don't want to block the device on the renderer for
1062 // some keys like menu, home, call.
1063 // 2. There are no WebKit equivalents for some of these keys
1064 // (see app/keyboard_codes_win.h)
1065 // Note that these are not the same set as KeyEvent.isSystemKey:
1066 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1067 if (keyCode == KeyEvent.KEYCODE_MENU ||
1068 keyCode == KeyEvent.KEYCODE_HOME ||
1069 keyCode == KeyEvent.KEYCODE_BACK ||
1070 keyCode == KeyEvent.KEYCODE_CALL ||
1071 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1072 keyCode == KeyEvent.KEYCODE_POWER ||
1073 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1074 keyCode == KeyEvent.KEYCODE_CAMERA ||
1075 keyCode == KeyEvent.KEYCODE_FOCUS ||
1076 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1077 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1078 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1079 return true;
1080 }
1081
1082 // We also have to intercept some shortcuts before we send them to the ContentView.
1083 if (event.isCtrlPressed() && (
1084 keyCode == KeyEvent.KEYCODE_TAB ||
1085 keyCode == KeyEvent.KEYCODE_W ||
1086 keyCode == KeyEvent.KEYCODE_F4)) {
1087 return true;
1088 }
1089
1090 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 }
1092
1093 @Override
John Reck997b1b72012-04-19 18:08:25 -07001094 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001095 if (!isActivityPaused()) {
1096 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001097 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001098 } else {
John Reck997b1b72012-04-19 18:08:25 -07001099 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001100 }
1101 }
John Reck997b1b72012-04-19 18:08:25 -07001102 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 }
1104
1105 @Override
John Reck324d4402011-01-11 16:56:42 -08001106 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001107 boolean disableHistoryWrites =
1108 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1109
1110 // Don't save anything in private browsing mode or when explicitly set
1111 // not to write history via an overlay
1112 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001113 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001114
John Reck324d4402011-01-11 16:56:42 -08001115 if (TextUtils.isEmpty(url)
1116 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 return;
1118 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001119
John Reckf57c0292011-07-21 18:15:39 -07001120 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001121 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 }
1123
1124 @Override
1125 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1126 AsyncTask<Void, Void, String[]> task =
1127 new AsyncTask<Void, Void, String[]>() {
1128 @Override
1129 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001130 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 }
1132 @Override
1133 public void onPostExecute(String[] result) {
1134 callback.onReceiveValue(result);
1135 }
1136 };
1137 task.execute();
1138 }
1139
1140 @Override
1141 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1142 final HttpAuthHandler handler, final String host,
1143 final String realm) {
1144 String username = null;
1145 String password = null;
1146
1147 boolean reuseHttpAuthUsernamePassword
1148 = handler.useHttpAuthUsernamePassword();
1149
1150 if (reuseHttpAuthUsernamePassword && view != null) {
1151 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1152 if (credentials != null && credentials.length == 2) {
1153 username = credentials[0];
1154 password = credentials[1];
1155 }
1156 }
1157
1158 if (username != null && password != null) {
1159 handler.proceed(username, password);
1160 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001161 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001162 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1163 } else {
1164 handler.cancel();
1165 }
1166 }
1167 }
1168
1169 @Override
1170 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001171 String contentDisposition, String mimetype, String referer,
1172 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001173 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001174 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001175 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001176 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001177 // This Tab was opened for the sole purpose of downloading a
1178 // file. Remove it.
1179 if (tab == mTabControl.getCurrentTab()) {
1180 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001181 if (tab.getDerivedFromIntent())
1182 closeTab(tab);
1183 else
1184 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 } else {
1186 // In this case, it is not.
1187 closeTab(tab);
1188 }
1189 }
1190 }
1191
1192 @Override
1193 public Bitmap getDefaultVideoPoster() {
1194 return mUi.getDefaultVideoPoster();
1195 }
1196
1197 @Override
1198 public View getVideoLoadingProgressView() {
1199 return mUi.getVideoLoadingProgressView();
1200 }
1201
1202 @Override
1203 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1204 SslError error) {
1205 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1206 }
1207
1208 // helper method
1209
1210 /*
1211 * Update the favorites icon if the private browsing isn't enabled and the
1212 * icon is valid.
1213 */
1214 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1215 final String url, Bitmap favicon) {
1216 if (favicon == null) {
1217 return;
1218 }
1219 if (!tab.isPrivateBrowsingEnabled()) {
1220 Bookmarks.updateFavicon(mActivity
1221 .getContentResolver(), originalUrl, url, favicon);
1222 }
1223 }
1224
Leon Scroggins4cd97792010-12-03 15:31:56 -05001225 @Override
1226 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001227 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001228 mUi.bookmarkedStatusHasChanged(tab);
1229 }
1230
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 // end WebViewController
1232
1233 protected void pageUp() {
1234 getCurrentTopWebView().pageUp(false);
1235 }
1236
1237 protected void pageDown() {
1238 getCurrentTopWebView().pageDown(false);
1239 }
1240
1241 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001242 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 public void editUrl() {
1244 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001245 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 }
1247
John Reck9c35b9c2012-05-30 10:08:50 -07001248 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001249 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001250 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 if (tab.inForeground()) {
1252 if (mUi.isCustomViewShowing()) {
1253 callback.onCustomViewHidden();
1254 return;
1255 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001256 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001257 // Save the menu state and set it to empty while the custom
1258 // view is showing.
1259 mOldMenuState = mMenuState;
1260 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001261 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 }
1263 }
1264
1265 @Override
1266 public void hideCustomView() {
1267 if (mUi.isCustomViewShowing()) {
1268 mUi.onHideCustomView();
1269 // Reset the old menu state.
1270 mMenuState = mOldMenuState;
1271 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001272 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 }
1274 }
1275
John Reck9c35b9c2012-05-30 10:08:50 -07001276 @Override
1277 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001278 Intent intent) {
1279 if (getCurrentTopWebView() == null) return;
1280 switch (requestCode) {
1281 case PREFERENCES_PAGE:
1282 if (resultCode == Activity.RESULT_OK && intent != null) {
1283 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001284 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001285 mTabControl.removeParentChildRelationShips();
1286 }
1287 }
1288 break;
1289 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001290 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001291 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001294 case AUTOFILL_SETUP:
1295 // Determine whether a profile was actually set up or not
1296 // and if so, send the message back to the WebTextView to
1297 // fill the form with the new profile.
1298 if (getSettings().getAutoFillProfile() != null) {
1299 mAutoFillSetupMessage.sendToTarget();
1300 mAutoFillSetupMessage = null;
1301 }
1302 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001303 case COMBO_VIEW:
1304 if (intent == null || resultCode != Activity.RESULT_OK) {
1305 break;
1306 }
John Reck3ba45532011-08-11 16:26:53 -07001307 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001308 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1309 Tab t = getCurrentTab();
1310 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001311 mUpdateMyNavThumbnail = true;
1312 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001313 loadUrl(t, uri.toString());
1314 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1315 String[] urls = intent.getStringArrayExtra(
1316 ComboViewActivity.EXTRA_OPEN_ALL);
1317 Tab parent = getCurrentTab();
1318 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001319 if (url != null) {
1320 parent = openTab(url, parent,
1321 !mSettings.openInBackground(), true);
1322 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001323 }
1324 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1325 long id = intent.getLongExtra(
1326 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1327 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001328 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001329 }
1330 }
1331 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001332 case VOICE_RESULT:
1333 if (resultCode == Activity.RESULT_OK && intent != null) {
1334 ArrayList<String> results = intent.getStringArrayListExtra(
1335 RecognizerIntent.EXTRA_RESULTS);
1336 if (results.size() >= 1) {
1337 mVoiceResult = results.get(0);
1338 }
1339 }
1340 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001341 case MY_NAVIGATION:
1342 if (intent == null || resultCode != Activity.RESULT_OK) {
1343 break;
1344 }
1345
1346 if (intent.getBooleanExtra("need_refresh", false) &&
1347 getCurrentTopWebView() != null) {
1348 getCurrentTopWebView().reload();
1349 }
1350 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001351 default:
1352 break;
1353 }
1354 getCurrentTopWebView().requestFocus();
1355 }
1356
1357 /**
1358 * Open the Go page.
1359 * @param startWithHistory If true, open starting on the history tab.
1360 * Otherwise, start with the bookmarks tab.
1361 */
1362 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001363 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 if (mTabControl.getCurrentWebView() == null) {
1365 return;
1366 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001367 // clear action mode
1368 if (isInCustomActionMode()) {
1369 endActionMode();
1370 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001371 Bundle extras = new Bundle();
1372 // Disable opening in a new window if we have maxed out the windows
1373 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1374 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001375 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001376 }
1377
1378 // combo view callbacks
1379
Michael Kolb8233fac2010-10-26 16:08:53 -07001380 // key handling
1381 protected void onBackKey() {
1382 if (!mUi.onBackKey()) {
1383 WebView subwindow = mTabControl.getCurrentSubWindow();
1384 if (subwindow != null) {
1385 if (subwindow.canGoBack()) {
1386 subwindow.goBack();
1387 } else {
1388 dismissSubWindow(mTabControl.getCurrentTab());
1389 }
1390 } else {
1391 goBackOnePageOrQuit();
1392 }
1393 }
1394 }
1395
Michael Kolb4bd767d2011-05-27 11:33:55 -07001396 protected boolean onMenuKey() {
1397 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001398 }
1399
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 // menu handling and state
1401 // TODO: maybe put into separate handler
1402
John Reck9c35b9c2012-05-30 10:08:50 -07001403 @Override
1404 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001405 if (mMenuState == EMPTY_MENU) {
1406 return false;
1407 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001408 MenuInflater inflater = mActivity.getMenuInflater();
1409 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 return true;
1411 }
1412
John Reck9c35b9c2012-05-30 10:08:50 -07001413 @Override
1414 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001415 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001416 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 return;
1418 }
1419 if (!(v instanceof WebView)) {
1420 return;
1421 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001422 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 WebView.HitTestResult result = webview.getHitTestResult();
1424 if (result == null) {
1425 return;
1426 }
1427
1428 int type = result.getType();
1429 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1430 Log.w(LOGTAG,
1431 "We should not show context menu when nothing is touched");
1432 return;
1433 }
1434 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1435 // let TextView handles context menu
1436 return;
1437 }
1438
1439 // Note, http://b/issue?id=1106666 is requesting that
1440 // an inflated menu can be used again. This is not available
1441 // yet, so inflate each time (yuk!)
1442 MenuInflater inflater = mActivity.getMenuInflater();
1443 inflater.inflate(R.menu.browsercontext, menu);
1444
1445 // Show the correct menu group
1446 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001447 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001448 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 menu.setGroupVisible(R.id.PHONE_MENU,
1450 type == WebView.HitTestResult.PHONE_TYPE);
1451 menu.setGroupVisible(R.id.EMAIL_MENU,
1452 type == WebView.HitTestResult.EMAIL_TYPE);
1453 menu.setGroupVisible(R.id.GEO_MENU,
1454 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001455 String itemUrl = null;
1456 String url = webview.getOriginalUrl();
1457 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1458 itemUrl = Uri.decode(navigationUrl);
1459 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1460 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1461 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1462 } else {
1463 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1464 }
1465 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1466 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1467 } else {
1468 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1469
1470 menu.setGroupVisible(R.id.IMAGE_MENU,
1471 type == WebView.HitTestResult.IMAGE_TYPE
1472 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1473 menu.setGroupVisible(R.id.ANCHOR_MENU,
1474 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1475 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001476 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1477 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001478 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001479 // Setup custom handling depending on the type
1480 switch (type) {
1481 case WebView.HitTestResult.PHONE_TYPE:
1482 menu.setHeaderTitle(Uri.decode(extra));
1483 menu.findItem(R.id.dial_context_menu_id).setIntent(
1484 new Intent(Intent.ACTION_VIEW, Uri
1485 .parse(WebView.SCHEME_TEL + extra)));
1486 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1487 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1488 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1489 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1490 addIntent);
1491 menu.findItem(R.id.copy_phone_context_menu_id)
1492 .setOnMenuItemClickListener(
1493 new Copy(extra));
1494 break;
1495
1496 case WebView.HitTestResult.EMAIL_TYPE:
1497 menu.setHeaderTitle(extra);
1498 menu.findItem(R.id.email_context_menu_id).setIntent(
1499 new Intent(Intent.ACTION_VIEW, Uri
1500 .parse(WebView.SCHEME_MAILTO + extra)));
1501 menu.findItem(R.id.copy_mail_context_menu_id)
1502 .setOnMenuItemClickListener(
1503 new Copy(extra));
1504 break;
1505
1506 case WebView.HitTestResult.GEO_TYPE:
1507 menu.setHeaderTitle(extra);
1508 menu.findItem(R.id.map_context_menu_id).setIntent(
1509 new Intent(Intent.ACTION_VIEW, Uri
1510 .parse(WebView.SCHEME_GEO
1511 + URLEncoder.encode(extra))));
1512 menu.findItem(R.id.copy_geo_context_menu_id)
1513 .setOnMenuItemClickListener(
1514 new Copy(extra));
1515 break;
1516
1517 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1518 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001519 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001520 // decide whether to show the open link in new tab option
1521 boolean showNewTab = mTabControl.canCreateNewTab();
1522 MenuItem newTabItem
1523 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001524 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001525 ? R.string.contextmenu_openlink_newwindow_background
1526 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001527 newTabItem.setVisible(showNewTab);
1528 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001529 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1530 newTabItem.setOnMenuItemClickListener(
1531 new MenuItem.OnMenuItemClickListener() {
1532 @Override
1533 public boolean onMenuItemClick(MenuItem item) {
1534 final HashMap<String, WebView> hrefMap =
1535 new HashMap<String, WebView>();
1536 hrefMap.put("webview", webview);
1537 final Message msg = mHandler.obtainMessage(
1538 FOCUS_NODE_HREF,
1539 R.id.open_newtab_context_menu_id,
1540 0, hrefMap);
1541 webview.requestFocusNodeHref(msg);
1542 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001543 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001544 });
1545 } else {
1546 newTabItem.setOnMenuItemClickListener(
1547 new MenuItem.OnMenuItemClickListener() {
1548 @Override
1549 public boolean onMenuItemClick(MenuItem item) {
1550 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001551 openTab(extra, parent,
1552 !mSettings.openInBackground(),
1553 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001554 return true;
1555 }
1556 });
1557 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001558 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001559 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1560 menu.setHeaderTitle(navigationUrl);
1561 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1562
1563 if (itemUrl != null) {
1564 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1565 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1566 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1567 @Override
1568 public boolean onMenuItemClick(MenuItem item) {
1569 final Intent intent = new Intent(Controller.this
1570 .getContext(),
1571 AddMyNavigationPage.class);
1572 Bundle bundle = new Bundle();
1573 String url = Uri.decode(navigationUrl);
1574 bundle.putBoolean("isAdding", false);
1575 bundle.putString("url", url);
1576 bundle.putString("name", getNameFromUrl(url));
1577 intent.putExtra("websites", bundle);
1578 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1579 return false;
1580 }
1581 });
1582 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1583 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1584 @Override
1585 public boolean onMenuItemClick(MenuItem item) {
1586 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1587 return false;
1588 }
1589 });
1590 }
1591 } else {
1592 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1593 }
1594 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001595 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1596 break;
1597 }
1598 // otherwise fall through to handle image part
1599 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001600 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1601 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1603 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001604 shareItem.setOnMenuItemClickListener(
1605 new MenuItem.OnMenuItemClickListener() {
1606 @Override
1607 public boolean onMenuItemClick(MenuItem item) {
1608 sharePage(mActivity, null, extra, null,
1609 null);
1610 return true;
1611 }
1612 }
1613 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001614 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001615 menu.findItem(R.id.view_image_context_menu_id)
1616 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1617 @Override
1618 public boolean onMenuItemClick(MenuItem item) {
1619 openTab(extra, mTabControl.getCurrentTab(), true, true);
1620 return false;
1621 }
1622 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001623 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1624 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1625 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001626 menu.findItem(R.id.set_wallpaper_context_menu_id).
1627 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1628 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 break;
1630
1631 default:
1632 Log.w(LOGTAG, "We should not get here.");
1633 break;
1634 }
1635 //update the ui
1636 mUi.onContextMenuCreated(menu);
1637 }
1638
kaiyiz6e5b3e02013-08-19 20:02:01 +08001639 public void startAddMyNavigation(String url) {
1640 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1641 Bundle bundle = new Bundle();
1642 bundle.putBoolean("isAdding", true);
1643 bundle.putString("url", url);
1644 bundle.putString("name", getNameFromUrl(url));
1645 intent.putExtra("websites", bundle);
1646 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1647 }
1648
1649 private void showMyNavigationDeleteDialog(final String itemUrl) {
1650 new AlertDialog.Builder(this.getContext())
1651 .setTitle(R.string.my_navigation_delete_label)
1652 .setIcon(android.R.drawable.ic_dialog_alert)
1653 .setMessage(R.string.my_navigation_delete_msg)
1654 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1655 @Override
1656 public void onClick(DialogInterface dialog, int whichButton) {
1657 deleteMyNavigationItem(itemUrl);
1658 }
1659 })
1660 .setNegativeButton(R.string.cancel, null)
1661 .show();
1662 }
1663
1664 private void deleteMyNavigationItem(final String itemUrl) {
1665 ContentResolver cr = this.getContext().getContentResolver();
1666 Cursor cursor = null;
1667
1668 try {
1669 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1670 new String[] {
1671 MyNavigationUtil.ID
1672 }, "url = ?", new String[] {
1673 itemUrl
1674 }, null);
1675 if (null != cursor && cursor.moveToFirst()) {
1676 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1677 cursor.getLong(0));
1678
1679 ContentValues values = new ContentValues();
1680 values.put(MyNavigationUtil.TITLE, "");
1681 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1682 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1683 ByteArrayOutputStream os = new ByteArrayOutputStream();
1684 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1685 R.raw.my_navigation_add);
1686 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1687 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1688 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1689 cr.update(uri, values, null, null);
1690 } else {
1691 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1692 }
1693 } catch (IllegalStateException e) {
1694 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1695 } finally {
1696 if (null != cursor) {
1697 cursor.close();
1698 }
1699 }
1700
1701 if (getCurrentTopWebView() != null) {
1702 getCurrentTopWebView().reload();
1703 }
1704 }
1705
1706 private String getNameFromUrl(String itemUrl) {
1707 ContentResolver cr = this.getContext().getContentResolver();
1708 Cursor cursor = null;
1709 String name = null;
1710
1711 try {
1712 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1713 new String[] {
1714 MyNavigationUtil.TITLE
1715 }, "url = ?", new String[] {
1716 itemUrl
1717 }, null);
1718 if (null != cursor && cursor.moveToFirst()) {
1719 name = cursor.getString(0);
1720 } else {
1721 Log.e(LOGTAG, "this item does not exist!");
1722 }
1723 } catch (IllegalStateException e) {
1724 Log.e(LOGTAG, "getNameFromUrl", e);
1725 } finally {
1726 if (null != cursor) {
1727 cursor.close();
1728 }
1729 }
1730 return name;
1731 }
1732
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001733 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
1734 if (bitmap == null) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001735 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1736 return;
1737 }
1738
1739 final ContentResolver cr = mActivity.getContentResolver();
1740 new AsyncTask<Void, Void, Void>() {
1741 @Override
1742 protected Void doInBackground(Void... unused) {
1743 ContentResolver cr = mActivity.getContentResolver();
1744 Cursor cursor = null;
1745 try {
1746 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1747 new String[] {
1748 MyNavigationUtil.ID
1749 }, "url = ?", new String[] {
1750 itemUrl
1751 }, null);
1752 if (null != cursor && cursor.moveToFirst()) {
1753 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001754 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001755
1756 ContentValues values = new ContentValues();
1757 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1758 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1759 cursor.getLong(0));
1760 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1761 cr.update(uri, values, null, null);
1762 os.close();
1763 }
1764 } catch (IllegalStateException e) {
1765 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1766 } catch (IOException e) {
1767 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1768 } finally {
1769 if (null != cursor) {
1770 cursor.close();
1771 }
1772 }
1773 return null;
1774 }
1775 }.execute();
1776 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001777 /**
1778 * As the menu can be open when loading state changes
1779 * we must manually update the state of the stop/reload menu
1780 * item
1781 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001782 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001783 if (menu == null) {
1784 return;
1785 }
1786 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001787 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001788 menu.findItem(R.id.stop_menu_id):
1789 menu.findItem(R.id.reload_menu_id);
1790 if (src != null) {
1791 dest.setIcon(src.getIcon());
1792 dest.setTitle(src.getTitle());
1793 }
1794 }
1795
John Reck9c35b9c2012-05-30 10:08:50 -07001796 @Override
1797 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001798 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001799 // hold on to the menu reference here; it is used by the page callbacks
1800 // to update the menu based on loading state
1801 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001802 // Note: setVisible will decide whether an item is visible; while
1803 // setEnabled() will decide whether an item is enabled, which also means
1804 // whether the matching shortcut key will function.
1805 switch (mMenuState) {
1806 case EMPTY_MENU:
1807 if (mCurrentMenuState != mMenuState) {
1808 menu.setGroupVisible(R.id.MAIN_MENU, false);
1809 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1810 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1811 }
1812 break;
1813 default:
1814 if (mCurrentMenuState != mMenuState) {
1815 menu.setGroupVisible(R.id.MAIN_MENU, true);
1816 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1817 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1818 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001819 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001820 break;
1821 }
1822 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001823 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001824 }
1825
Michael Kolb4bf79712011-07-14 14:18:12 -07001826 @Override
1827 public void updateMenuState(Tab tab, Menu menu) {
1828 boolean canGoBack = false;
1829 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001830 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001831 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001832 // Following flag is used to identify schemes for which the LIVE_MENU
1833 // items defined in res/menu/browser.xml should be enabled
1834 boolean isLiveScheme = false;
1835 boolean isPageFinished = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001836 if (tab != null) {
1837 canGoBack = tab.canGoBack();
1838 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001839 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001840 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001841 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
1842 isPageFinished = tab.getPageFinishedStatus();
Michael Kolb4bf79712011-07-14 14:18:12 -07001843 }
1844 final MenuItem back = menu.findItem(R.id.back_menu_id);
1845 back.setEnabled(canGoBack);
1846
1847 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001848
1849 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1850 forward.setEnabled(canGoForward);
1851
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001852 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1853 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001854 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001855 if (source != null && dest != null) {
1856 dest.setTitle(source.getTitle());
1857 dest.setIcon(source.getIcon());
1858 }
John Recke1a03a32011-09-14 17:04:16 -07001859 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001860
1861 // decide whether to show the share link option
1862 PackageManager pm = mActivity.getPackageManager();
1863 Intent send = new Intent(Intent.ACTION_SEND);
1864 send.setType("text/plain");
1865 ResolveInfo ri = pm.resolveActivity(send,
1866 PackageManager.MATCH_DEFAULT_ONLY);
1867 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1868
1869 boolean isNavDump = mSettings.enableNavDump();
1870 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1871 nav.setVisible(isNavDump);
1872 nav.setEnabled(isNavDump);
1873
1874 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001875 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1876 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001877 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001878 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001879 menu.setGroupEnabled(R.id.OFFLINE_READING, isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001880 // history and snapshots item are the members of COMBO menu group,
1881 // so if show history item, only make snapshots item invisible.
1882 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001883
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001884 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001885 }
1886
John Reck9c35b9c2012-05-30 10:08:50 -07001887 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001888 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001889 if (null == getCurrentTopWebView()) {
1890 return false;
1891 }
1892 if (mMenuIsDown) {
1893 // The shortcut action consumes the MENU. Even if it is still down,
1894 // it won't trigger the next shortcut action. In the case of the
1895 // shortcut action triggering a new activity, like Bookmarks, we
1896 // won't get onKeyUp for MENU. So it is important to reset it here.
1897 mMenuIsDown = false;
1898 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001899 if (mUi.onOptionsItemSelected(item)) {
1900 // ui callback handled it
1901 return true;
1902 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001903 switch (item.getItemId()) {
1904 // -- Main menu
1905 case R.id.new_tab_menu_id:
1906 openTabToHomePage();
1907 break;
1908
1909 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001910 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001911 break;
1912
Afzal Najamd4e33312012-04-26 01:54:01 -04001913 case R.id.close_other_tabs_id:
1914 closeOtherTabs();
1915 break;
1916
Michael Kolb8233fac2010-10-26 16:08:53 -07001917 case R.id.goto_menu_id:
1918 editUrl();
1919 break;
1920
1921 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001922 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1923 break;
1924
1925 case R.id.history_menu_id:
1926 bookmarksOrHistoryPicker(ComboViews.History);
1927 break;
1928
1929 case R.id.snapshots_menu_id:
1930 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001931 break;
1932
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001934 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001935 break;
1936
1937 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001938 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001939 stopLoading();
1940 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001941 Tab currentTab = mTabControl.getCurrentTab();
1942 if (currentTab.hasCrashed) {
1943 currentTab.replaceCrashView(getCurrentTopWebView(),
1944 currentTab.getViewContainer());
1945 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001946 getCurrentTopWebView().reload();
1947 }
1948 break;
1949
1950 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001951 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001952 break;
1953
1954 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001955 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001956 break;
1957
1958 case R.id.close_menu_id:
1959 // Close the subwindow if it exists.
1960 if (mTabControl.getCurrentSubWindow() != null) {
1961 dismissSubWindow(mTabControl.getCurrentTab());
1962 break;
1963 }
1964 closeCurrentTab();
1965 break;
1966
kaiyiza8b6dbb2013-07-29 18:11:22 +08001967 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001968 Object[] params = { new String("persist.debug.browsermonkeytest")};
1969 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001970 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001971 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001972 if (ret != null && ret.equals("enable"))
1973 break;
luxiaolb40014b2013-07-19 10:01:43 +08001974 showExitDialog(mActivity);
1975 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001976 case R.id.homepage_menu_id:
1977 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001978 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001979 break;
1980
1981 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001982 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 break;
1984
1985 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001986 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001987 break;
1988
John Reck2bc80422011-06-30 15:11:49 -07001989 case R.id.save_snapshot_menu_id:
1990 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001991 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001992 createScreenshotAsync(
1993 source.getWebView(),
1994 getDesiredThumbnailWidth(mActivity),
1995 getDesiredThumbnailHeight(mActivity),
1996 new ValueCallback<Bitmap>() {
1997 @Override
1998 public void onReceiveValue(Bitmap bitmap) {
1999 new SaveSnapshotTask(source, bitmap).execute();
2000 }
2001 });
Leon Scrogginsac993842011-02-02 12:54:07 -05002002 break;
2003
Michael Kolb8233fac2010-10-26 16:08:53 -07002004 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07002005 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07002006 break;
2007
John Recke1a03a32011-09-14 17:04:16 -07002008 case R.id.snapshot_go_live:
2009 goLive();
2010 return true;
2011
Michael Kolb8233fac2010-10-26 16:08:53 -07002012 case R.id.share_page_menu_id:
2013 Tab currentTab = mTabControl.getCurrentTab();
2014 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002015 return false;
2016 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002017 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002018 break;
2019
2020 case R.id.dump_nav_menu_id:
2021 getCurrentTopWebView().debugDump();
2022 break;
2023
Michael Kolb8233fac2010-10-26 16:08:53 -07002024 case R.id.zoom_in_menu_id:
2025 getCurrentTopWebView().zoomIn();
2026 break;
2027
2028 case R.id.zoom_out_menu_id:
2029 getCurrentTopWebView().zoomOut();
2030 break;
2031
2032 case R.id.view_downloads_menu_id:
2033 viewDownloads();
2034 break;
2035
John Reck42229bc2011-08-19 13:26:43 -07002036 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002037 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002038 break;
2039
Michael Kolb8233fac2010-10-26 16:08:53 -07002040 case R.id.window_one_menu_id:
2041 case R.id.window_two_menu_id:
2042 case R.id.window_three_menu_id:
2043 case R.id.window_four_menu_id:
2044 case R.id.window_five_menu_id:
2045 case R.id.window_six_menu_id:
2046 case R.id.window_seven_menu_id:
2047 case R.id.window_eight_menu_id:
2048 {
2049 int menuid = item.getItemId();
2050 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2051 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2052 Tab desiredTab = mTabControl.getTab(id);
2053 if (desiredTab != null &&
2054 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002055 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002056 }
2057 break;
2058 }
2059 }
2060 }
2061 break;
2062
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002063 case R.id.about_menu_id:
2064 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2065 builder.setTitle(R.string.about);
2066 builder.setCancelable(true);
2067 String ua = "";
2068 final WebView currentWebView = getCurrentWebView();
2069 if (currentWebView != null) {
2070 final WebSettings s = currentWebView.getSettings();
2071 if (s != null) {
2072 ua = s.getUserAgentString();
2073 }
2074 }
2075 builder.setMessage("Agent:" + ua);
2076 builder.setPositiveButton(android.R.string.ok, null);
2077 builder.create().show();
2078 break;
2079
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 default:
2081 return false;
2082 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002083 return true;
2084 }
2085
John Reck68234a92012-04-19 15:27:12 -07002086 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2087 implements OnCancelListener {
2088
2089 private Tab mTab;
2090 private Dialog mProgressDialog;
2091 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002092 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002093
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002094 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002095 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002096 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002097 }
2098
2099 @Override
2100 protected void onPreExecute() {
2101 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2102 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2103 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002104 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002105 }
2106
2107 @Override
2108 protected Long doInBackground(Void... params) {
2109 if (!mTab.saveViewState(mValues)) {
2110 return null;
2111 }
2112 if (isCancelled()) {
2113 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2114 File file = mActivity.getFileStreamPath(path);
2115 if (!file.delete()) {
2116 file.deleteOnExit();
2117 }
2118 return null;
2119 }
2120 final ContentResolver cr = mActivity.getContentResolver();
2121 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2122 if (result == null) {
2123 return null;
2124 }
2125 long id = ContentUris.parseId(result);
2126 return id;
2127 }
2128
2129 @Override
2130 protected void onPostExecute(Long id) {
2131 if (isCancelled()) {
2132 return;
2133 }
2134 mProgressDialog.dismiss();
2135 if (id == null) {
2136 Toast.makeText(mActivity, R.string.snapshot_failed,
2137 Toast.LENGTH_SHORT).show();
2138 return;
2139 }
2140 Bundle b = new Bundle();
2141 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2142 mUi.showComboView(ComboViews.Snapshots, b);
2143 }
2144
2145 @Override
2146 public void onCancel(DialogInterface dialog) {
2147 cancel(true);
2148 }
2149 }
2150
Michael Kolb80f75082012-04-10 10:50:06 -07002151 @Override
2152 public void toggleUserAgent() {
2153 WebView web = getCurrentWebView();
2154 mSettings.toggleDesktopUseragent(web);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002155 web.reload();
Michael Kolb80f75082012-04-10 10:50:06 -07002156 }
2157
2158 @Override
2159 public void findOnPage() {
2160 getCurrentTopWebView().showFindDialog(null, true);
2161 }
2162
2163 @Override
2164 public void openPreferences() {
2165 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2166 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2167 getCurrentTopWebView().getUrl());
2168 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2169 }
2170
2171 @Override
2172 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002173 WebView w = getCurrentTopWebView();
2174 if (w == null)
2175 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002176 final Intent i = createBookmarkCurrentPageIntent(false);
2177 createScreenshotAsync(
2178 w, getDesiredThumbnailWidth(mActivity),
2179 getDesiredThumbnailHeight(mActivity),
2180 new ValueCallback<Bitmap>() {
2181 @Override
2182 public void onReceiveValue(Bitmap bitmap) {
2183 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2184 mActivity.startActivity(i);
2185 }
2186 });
Michael Kolb80f75082012-04-10 10:50:06 -07002187 }
2188
John Recke1a03a32011-09-14 17:04:16 -07002189 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002190 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002191 String url = t.getLiveUrl();
2192 // destroy the old snapshot tab
2193 closeCurrentTab();
2194 Tab liveTab = createNewTab(false, true, false);
2195 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002196 }
2197
luxiaolb40014b2013-07-19 10:01:43 +08002198 private void showExitDialog(final Activity activity) {
2199 new AlertDialog.Builder(activity)
2200 .setTitle(R.string.exit_browser_title)
2201 .setIcon(android.R.drawable.ic_dialog_alert)
2202 .setMessage(R.string.exit_browser_msg)
2203 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2204 public void onClick(DialogInterface dialog, int which) {
2205 activity.moveTaskToBack(true);
2206 dialog.dismiss();
2207 }
2208 })
2209 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2210 public void onClick(DialogInterface dialog, int which) {
2211 activity.finish();
2212 mHandler.postDelayed(new Runnable() {
2213 @Override
2214 public void run() {
2215 // TODO Auto-generated method stub
2216 mCrashRecoveryHandler.clearState(true);
2217 int pid = android.os.Process.myPid();
2218 android.os.Process.killProcess(pid);
2219 }
2220 }, 300);
2221 dialog.dismiss();
2222 }
2223 })
2224 .show();
2225 }
Michael Kolb315d5022011-10-13 12:47:11 -07002226 @Override
2227 public void showPageInfo() {
2228 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2229 }
2230
John Reck9c35b9c2012-05-30 10:08:50 -07002231 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002232 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002233 // Let the History and Bookmark fragments handle menus they created.
2234 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2235 return false;
2236 }
2237
Michael Kolb8233fac2010-10-26 16:08:53 -07002238 int id = item.getItemId();
2239 boolean result = true;
2240 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002241 // -- Browser context menu
2242 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002243 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002244 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002245 case R.id.copy_link_context_menu_id:
2246 final WebView webView = getCurrentTopWebView();
2247 if (null == webView) {
2248 result = false;
2249 break;
2250 }
2251 final HashMap<String, WebView> hrefMap =
2252 new HashMap<String, WebView>();
2253 hrefMap.put("webview", webView);
2254 final Message msg = mHandler.obtainMessage(
2255 FOCUS_NODE_HREF, id, 0, hrefMap);
2256 webView.requestFocusNodeHref(msg);
2257 break;
2258
2259 default:
2260 // For other context menus
2261 result = onOptionsItemSelected(item);
2262 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002263 return result;
2264 }
2265
2266 /**
2267 * support programmatically opening the context menu
2268 */
2269 public void openContextMenu(View view) {
2270 mActivity.openContextMenu(view);
2271 }
2272
2273 /**
2274 * programmatically open the options menu
2275 */
2276 public void openOptionsMenu() {
2277 mActivity.openOptionsMenu();
2278 }
2279
John Reck9c35b9c2012-05-30 10:08:50 -07002280 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002281 public boolean onMenuOpened(int featureId, Menu menu) {
2282 if (mOptionsMenuOpen) {
2283 if (mConfigChanged) {
2284 // We do not need to make any changes to the state of the
2285 // title bar, since the only thing that happened was a
2286 // change in orientation
2287 mConfigChanged = false;
2288 } else {
2289 if (!mExtendedMenuOpen) {
2290 mExtendedMenuOpen = true;
2291 mUi.onExtendedMenuOpened();
2292 } else {
2293 // Switching the menu back to icon view, so show the
2294 // title bar once again.
2295 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002296 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002297 }
2298 }
2299 } else {
2300 // The options menu is closed, so open it, and show the title
2301 mOptionsMenuOpen = true;
2302 mConfigChanged = false;
2303 mExtendedMenuOpen = false;
2304 mUi.onOptionsMenuOpened();
2305 }
2306 return true;
2307 }
2308
John Reck9c35b9c2012-05-30 10:08:50 -07002309 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002310 public void onOptionsMenuClosed(Menu menu) {
2311 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002312 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002313 }
2314
John Reck9c35b9c2012-05-30 10:08:50 -07002315 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002316 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002317 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002318 }
2319
2320 // Helper method for getting the top window.
2321 @Override
2322 public WebView getCurrentTopWebView() {
2323 return mTabControl.getCurrentTopWebView();
2324 }
2325
2326 @Override
2327 public WebView getCurrentWebView() {
2328 return mTabControl.getCurrentWebView();
2329 }
2330
2331 /*
2332 * This method is called as a result of the user selecting the options
2333 * menu to see the download window. It shows the download window on top of
2334 * the current window.
2335 */
2336 void viewDownloads() {
2337 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2338 mActivity.startActivity(intent);
2339 }
2340
John Reck30b065e2011-07-19 10:58:05 -07002341 int getActionModeHeight() {
2342 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2343 new int[] { android.R.attr.actionBarSize });
2344 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2345 actionBarSizeTypedArray.recycle();
2346 return size;
2347 }
2348
Michael Kolb8233fac2010-10-26 16:08:53 -07002349 // action mode
2350
John Reck9c35b9c2012-05-30 10:08:50 -07002351 @Override
2352 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002353 mUi.onActionModeStarted(mode);
2354 mActionMode = mode;
2355 }
2356
2357 /*
2358 * True if a custom ActionMode (i.e. find or select) is in use.
2359 */
2360 @Override
2361 public boolean isInCustomActionMode() {
2362 return mActionMode != null;
2363 }
2364
2365 /*
2366 * End the current ActionMode.
2367 */
2368 @Override
2369 public void endActionMode() {
2370 if (mActionMode != null) {
2371 mActionMode.finish();
2372 }
2373 }
2374
2375 /*
2376 * Called by find and select when they are finished. Replace title bars
2377 * as necessary.
2378 */
John Reck9c35b9c2012-05-30 10:08:50 -07002379 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002380 public void onActionModeFinished(ActionMode mode) {
2381 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002382 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002383 mActionMode = null;
2384 }
2385
2386 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002387 final Tab tab = getCurrentTab();
2388 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002389 }
2390
2391 // bookmark handling
2392
2393 /**
2394 * add the current page as a bookmark to the given folder id
2395 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002396 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002397 * bookmarked, and if it is, edit that bookmark. If false, and
2398 * the site is already bookmarked, do not attempt to edit the
2399 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002400 */
2401 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002402 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002403 WebView w = getCurrentTopWebView();
2404 if (w == null) {
2405 return null;
2406 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002407 Intent i = new Intent(mActivity,
2408 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002409 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2410 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2411 String touchIconUrl = w.getTouchIconUrl();
2412 if (touchIconUrl != null) {
2413 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2414 WebSettings settings = w.getSettings();
2415 if (settings != null) {
2416 i.putExtra(AddBookmarkPage.USER_AGENT,
2417 settings.getUserAgentString());
2418 }
2419 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002420 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002421 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002422 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002423 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2424 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002425 // Put the dialog at the upper right of the screen, covering the
2426 // star on the title bar.
2427 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002428 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002429 }
2430
2431 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002432 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002433 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002434 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002435 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002436 }
2437
Vivek Sekharb54614f2014-05-01 19:03:37 -07002438 @Override
2439 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2440 boolean capture) {
2441 mUploadHandler = new UploadHandler(this);
2442 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2443 }
2444
Michael Kolb8233fac2010-10-26 16:08:53 -07002445 // thumbnails
2446
2447 /**
2448 * Return the desired width for thumbnail screenshots, which are stored in
2449 * the database, and used on the bookmarks screen.
2450 * @param context Context for finding out the density of the screen.
2451 * @return desired width for thumbnail screenshot.
2452 */
2453 static int getDesiredThumbnailWidth(Context context) {
2454 return context.getResources().getDimensionPixelOffset(
2455 R.dimen.bookmarkThumbnailWidth);
2456 }
2457
2458 /**
2459 * Return the desired height for thumbnail screenshots, which are stored in
2460 * the database, and used on the bookmarks screen.
2461 * @param context Context for finding out the density of the screen.
2462 * @return desired height for thumbnail screenshot.
2463 */
2464 static int getDesiredThumbnailHeight(Context context) {
2465 return context.getResources().getDimensionPixelOffset(
2466 R.dimen.bookmarkThumbnailHeight);
2467 }
2468
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002469 static void createScreenshotAsync(WebView view, int width, int height,
2470 final ValueCallback<Bitmap> cb) {
2471 if (view == null || width == 0 || height == 0) {
2472 return;
2473 }
2474
2475 view.getContentBitmapAsync(
2476 (float) width / view.getWidth(),
2477 new Rect(),
2478 new ValueCallback<Bitmap>() {
2479 @Override
2480 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002481 if (bitmap != null)
2482 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2483 cb.onReceiveValue(bitmap);
2484 }});
2485 }
2486
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002487 private void updateScreenshot(final Tab tab) {
2488 createScreenshotAsync(
2489 tab.getWebView(),
2490 getDesiredThumbnailWidth(mActivity),
2491 getDesiredThumbnailHeight(mActivity),
2492 new ValueCallback<Bitmap>() {
2493 @Override
2494 public void onReceiveValue(Bitmap bitmap) {
2495 updateScreenshot(tab, bitmap);
2496 }
2497 });
2498 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002499
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002500 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002501 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002502 // FIXME: Would like to make sure there is actually something to
2503 // draw, but the API for that (WebViewCore.pictureReady()) is not
2504 // currently accessible here.
2505
John Reck34ef2672011-02-10 11:30:55 -08002506 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002507 if (view == null) {
2508 // Tab was destroyed
2509 return;
2510 }
John Reck34ef2672011-02-10 11:30:55 -08002511 final String url = tab.getUrl();
2512 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002513 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002514 if (TextUtils.isEmpty(url)) {
2515 return;
2516 }
2517
kaiyiz6e5b3e02013-08-19 20:02:01 +08002518 //update My Navigation Thumbnails
2519 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2520 if (isMyNavigationUrl) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002521 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002522 }
John Reck34ef2672011-02-10 11:30:55 -08002523 // Only update thumbnails for web urls (http(s)://), not for
2524 // about:, javascript:, data:, etc...
2525 // Unless it is a bookmarked site, then always update
2526 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2527 return;
2528 }
2529
kaiyiz6e5b3e02013-08-19 20:02:01 +08002530 if (url != null && mUpdateMyNavThumbnailUrl != null
2531 && Patterns.WEB_URL.matcher(url).matches()
2532 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2533 String urlHost = (new WebAddress(url)).getHost();
2534 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2535 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2536 || bookmarkHost.length() == 0) {
2537 return;
2538 }
2539 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2540 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2541 bookmarkHost.length());
2542 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2543 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2544 if (!bookmarkDomain.equals(urlDomain)) {
2545 return;
2546 }
2547 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002548 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002549 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2550 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2551 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2552 500);
2553 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002554 return;
2555 }
2556
2557 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002558 new AsyncTask<Void, Void, Void>() {
2559 @Override
2560 protected Void doInBackground(Void... unused) {
2561 Cursor cursor = null;
2562 try {
2563 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002564 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2565 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2566 : url);
2567 if (mUpdateMyNavThumbnail) {
2568 mUpdateMyNavThumbnail = false;
2569 mUpdateMyNavThumbnailUrl = null;
2570 }
John Reck34ef2672011-02-10 11:30:55 -08002571 if (cursor != null && cursor.moveToFirst()) {
2572 final ByteArrayOutputStream os =
2573 new ByteArrayOutputStream();
2574 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002575
John Reck34ef2672011-02-10 11:30:55 -08002576 ContentValues values = new ContentValues();
2577 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002578
John Reck34ef2672011-02-10 11:30:55 -08002579 do {
John Reck617fd832011-02-16 14:35:59 -08002580 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002581 cr.update(Images.CONTENT_URI, values, null, null);
2582 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002583 }
John Reck34ef2672011-02-10 11:30:55 -08002584 } catch (IllegalStateException e) {
2585 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002586 } catch (SQLiteException s) {
2587 // Added for possible error when user tries to remove the same bookmark
2588 // that is being updated with a screen shot
2589 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002590 } finally {
2591 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002592 }
John Reck34ef2672011-02-10 11:30:55 -08002593 return null;
2594 }
2595 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002596 }
2597
2598 private class Copy implements OnMenuItemClickListener {
2599 private CharSequence mText;
2600
John Reck9c35b9c2012-05-30 10:08:50 -07002601 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 public boolean onMenuItemClick(MenuItem item) {
2603 copy(mText);
2604 return true;
2605 }
2606
2607 public Copy(CharSequence toCopy) {
2608 mText = toCopy;
2609 }
2610 }
2611
Leon Scroggins63c02662010-11-18 15:16:27 -05002612 private static class Download implements OnMenuItemClickListener {
2613 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002614 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002615 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002616 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002617 private static final String FALLBACK_EXTENSION = "dat";
2618 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002619
John Reck9c35b9c2012-05-30 10:08:50 -07002620 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002621 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002622 if (DataUri.isDataUri(mText)) {
2623 saveDataUri();
2624 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002625 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002626 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002627 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002628 return true;
2629 }
2630
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002631 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2632 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002633 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002634 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002635 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002636 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002637 }
George Mount387d45d2011-10-07 15:57:53 -07002638
2639 /**
2640 * Treats mText as a data URI and writes its contents to a file
2641 * based on the current time.
2642 */
2643 private void saveDataUri() {
2644 FileOutputStream outputStream = null;
2645 try {
2646 DataUri uri = new DataUri(mText);
2647 File target = getTarget(uri);
2648 outputStream = new FileOutputStream(target);
2649 outputStream.write(uri.getData());
2650 final DownloadManager manager =
2651 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2652 manager.addCompletedDownload(target.getName(),
2653 mActivity.getTitle().toString(), false,
2654 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002655 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002656 } catch (IOException e) {
2657 Log.e(LOGTAG, "Could not save data URL");
2658 } finally {
2659 if (outputStream != null) {
2660 try {
2661 outputStream.close();
2662 } catch (IOException e) {
2663 // ignore close errors
2664 }
2665 }
2666 }
2667 }
2668
2669 /**
2670 * Creates a File based on the current time stamp and uses
2671 * the mime type of the DataUri to get the extension.
2672 */
2673 private File getTarget(DataUri uri) throws IOException {
2674 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002675 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002676 String nameBase = format.format(new Date());
2677 String mimeType = uri.getMimeType();
2678 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2679 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2680 if (extension == null) {
2681 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2682 extension = FALLBACK_EXTENSION;
2683 }
2684 extension = "." + extension; // createTempFile needs the '.'
2685 File targetFile = File.createTempFile(nameBase, extension, dir);
2686 return targetFile;
2687 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002688 }
2689
Cary Clark8974d282010-11-22 10:46:05 -05002690 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002691 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002692
John Reck9c35b9c2012-05-30 10:08:50 -07002693 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002694 public boolean onMenuItemClick(MenuItem item) {
2695 if (mWebView != null) {
2696 return mWebView.selectText();
2697 }
2698 return false;
2699 }
2700
2701 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002702 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002703 }
2704
2705 }
2706
Michael Kolb8233fac2010-10-26 16:08:53 -07002707 /********************** TODO: UI stuff *****************************/
2708
2709 // these methods have been copied, they still need to be cleaned up
2710
2711 /****************** tabs ***************************************************/
2712
2713 // basic tab interactions:
2714
2715 // it is assumed that tabcontrol already knows about the tab
2716 protected void addTab(Tab tab) {
2717 mUi.addTab(tab);
2718 }
2719
2720 protected void removeTab(Tab tab) {
2721 mUi.removeTab(tab);
2722 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002723 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002724 }
2725
Michael Kolbf2055602011-04-09 17:20:03 -07002726 @Override
2727 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002728 // monkey protection against delayed start
2729 if (tab != null) {
2730 mTabControl.setCurrentTab(tab);
2731 // the tab is guaranteed to have a webview after setCurrentTab
2732 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002733 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002734 //Purge active tabs
2735 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002736 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 }
2738
John Reck8bcafc12011-08-29 16:43:02 -07002739 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002740 Tab current = mTabControl.getCurrentTab();
2741 if (current != null
2742 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002743 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002744 }
2745 }
2746
John Reck26b18322011-06-21 13:08:58 -07002747 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002748 // Dismiss the subwindow if applicable.
2749 dismissSubWindow(appTab);
2750 // Since we might kill the WebView, remove it from the
2751 // content view first.
2752 mUi.detachTab(appTab);
2753 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002754 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002755 // TODO: analyze why the remove and add are necessary
2756 mUi.attachTab(appTab);
2757 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002758 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002759 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002760 } else {
2761 // If the tab was the current tab, we have to attach
2762 // it to the view system again.
2763 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002764 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002765 }
2766 }
2767
2768 // Remove the sub window if it exists. Also called by TabControl when the
2769 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002770 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002771 public void dismissSubWindow(Tab tab) {
2772 removeSubWindow(tab);
2773 // dismiss the subwindow. This will destroy the WebView.
2774 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002775 WebView wv = getCurrentTopWebView();
2776 if (wv != null) {
2777 wv.requestFocus();
2778 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002779 }
2780
2781 @Override
2782 public void removeSubWindow(Tab t) {
2783 if (t.getSubWebView() != null) {
2784 mUi.removeSubWindow(t.getSubViewContainer());
2785 }
2786 }
2787
2788 @Override
2789 public void attachSubWindow(Tab tab) {
2790 if (tab.getSubWebView() != null) {
2791 mUi.attachSubWindow(tab.getSubViewContainer());
2792 getCurrentTopWebView().requestFocus();
2793 }
2794 }
2795
Mathew Inwood29721c22011-06-29 17:55:24 +01002796 private Tab showPreloadedTab(final UrlData urlData) {
2797 if (!urlData.isPreloaded()) {
2798 return null;
2799 }
2800 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2801 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2802 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002803 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002804 // Could not submit query. Fallback to regular tab creation
2805 tabControl.destroy();
2806 return null;
2807 }
2808 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002809 // check tab count and make room for new tab
2810 if (!mTabControl.canCreateNewTab()) {
2811 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2812 if (leastUsed != null) {
2813 closeTab(leastUsed);
2814 }
2815 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002816 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002817 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002818 mTabControl.addPreloadedTab(t);
2819 addTab(t);
2820 setActiveTab(t);
2821 return t;
2822 }
2823
Michael Kolb7bcafde2011-05-09 13:55:59 -07002824 // open a non inconito tab with the given url data
2825 // and set as active tab
2826 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002827 Tab tab = showPreloadedTab(urlData);
2828 if (tab == null) {
2829 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002830 if ((tab != null) && !urlData.isEmpty()) {
2831 loadUrlDataIn(tab, urlData);
2832 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002833 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002834 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002835 }
2836
Michael Kolb843510f2010-12-09 10:51:49 -08002837 @Override
2838 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002839 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002840 }
2841
Michael Kolb8233fac2010-10-26 16:08:53 -07002842 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002843 public Tab openIncognitoTab() {
2844 return openTab(INCOGNITO_URI, true, true, false);
2845 }
2846
2847 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002848 public Tab openTab(String url, boolean incognito, boolean setActive,
2849 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002850 return openTab(url, incognito, setActive, useCurrent, null);
2851 }
2852
2853 @Override
2854 public Tab openTab(String url, Tab parent, boolean setActive,
2855 boolean useCurrent) {
2856 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2857 setActive, useCurrent, parent);
2858 }
2859
2860 public Tab openTab(String url, boolean incognito, boolean setActive,
2861 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002862 Tab tab = createNewTab(incognito, setActive, useCurrent);
2863 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002864 if (parent instanceof SnapshotTab) {
2865 addTab(tab);
2866 if (setActive)
2867 setActiveTab(tab);
2868 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002869 parent.addChildTab(tab);
2870 }
Michael Kolb519d2282011-05-09 17:03:19 -07002871 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002872 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002873 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002874 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002875 return tab;
2876 }
2877
2878 // this method will attempt to create a new tab
2879 // incognito: private browsing tab
2880 // setActive: ste tab as current tab
2881 // useCurrent: if no new tab can be created, return current tab
2882 private Tab createNewTab(boolean incognito, boolean setActive,
2883 boolean useCurrent) {
2884 Tab tab = null;
2885 if (mTabControl.canCreateNewTab()) {
2886 tab = mTabControl.createNewTab(incognito);
2887 addTab(tab);
2888 if (setActive) {
2889 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002890 } else {
2891 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002892 }
2893 } else {
2894 if (useCurrent) {
2895 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002896 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002897 } else {
2898 mUi.showMaxTabsWarning();
2899 }
2900 }
2901 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002902 }
2903
John Reck2bc80422011-06-30 15:11:49 -07002904 @Override
2905 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2906 SnapshotTab tab = null;
2907 if (mTabControl.canCreateNewTab()) {
2908 tab = mTabControl.createSnapshotTab(snapshotId);
2909 addTab(tab);
2910 if (setActive) {
2911 setActiveTab(tab);
2912 }
2913 } else {
2914 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002915 }
2916 return tab;
2917 }
2918
Michael Kolb8233fac2010-10-26 16:08:53 -07002919 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002920 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002921 * @return boolean True if we successfully switched to a different tab. If
2922 * the indexth tab is null, or if that tab is the same as
2923 * the current one, return false.
2924 */
2925 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002926 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002927 Tab currentTab = mTabControl.getCurrentTab();
2928 if (tab == null || tab == currentTab) {
2929 return false;
2930 }
2931 setActiveTab(tab);
2932 return true;
2933 }
2934
2935 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002936 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002937 closeCurrentTab(false);
2938 }
2939
2940 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002941 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002942 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002943 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002944 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002945 return;
2946 }
Michael Kolbc831b632011-05-11 09:30:34 -07002947 final Tab current = mTabControl.getCurrentTab();
2948 final int pos = mTabControl.getCurrentPosition();
2949 Tab newTab = current.getParent();
2950 if (newTab == null) {
2951 newTab = mTabControl.getTab(pos + 1);
2952 if (newTab == null) {
2953 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002954 }
2955 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002956 if (andQuit) {
2957 mTabControl.setCurrentTab(newTab);
2958 closeTab(current);
2959 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002960 // Close window
2961 closeTab(current);
2962 }
2963 }
2964
2965 /**
2966 * Close the tab, remove its associated title bar, and adjust mTabControl's
2967 * current tab to a valid value.
2968 */
2969 @Override
2970 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002971 if (tab == mTabControl.getCurrentTab()) {
2972 closeCurrentTab();
2973 } else {
2974 removeTab(tab);
2975 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002976 }
2977
Afzal Najamd4e33312012-04-26 01:54:01 -04002978 /**
2979 * Close all tabs except the current one
2980 */
2981 @Override
2982 public void closeOtherTabs() {
2983 int inactiveTabs = mTabControl.getTabCount() - 1;
2984 for (int i = inactiveTabs; i >= 0; i--) {
2985 Tab tab = mTabControl.getTab(i);
2986 if (tab != mTabControl.getCurrentTab()) {
2987 removeTab(tab);
2988 }
2989 }
2990 }
2991
Michael Kolb8233fac2010-10-26 16:08:53 -07002992 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002993 protected void loadUrlFromContext(String url) {
2994 Tab tab = getCurrentTab();
2995 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002996 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002997 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002999 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003000 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003001 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003002 }
3003 }
3004 }
3005
3006 /**
3007 * Load the URL into the given WebView and update the title bar
3008 * to reflect the new load. Call this instead of WebView.loadUrl
3009 * directly.
3010 * @param view The WebView used to load url.
3011 * @param url The URL to load.
3012 */
John Reck71e51422011-07-01 16:49:28 -07003013 @Override
3014 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003015 loadUrl(tab, url, null);
3016 }
3017
3018 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3019 if (tab != null) {
3020 dismissSubWindow(tab);
3021 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003022 if (tab.hasCrashed) {
3023 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3024 }
Michael Kolba53c9892011-10-05 13:31:40 -07003025 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003026 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003027 }
3028
3029 /**
3030 * Load UrlData into a Tab and update the title bar to reflect the new
3031 * load. Call this instead of UrlData.loadIn directly.
3032 * @param t The Tab used to load.
3033 * @param data The UrlData being loaded.
3034 */
3035 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003036 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003037 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003038 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003039 } else {
John Reck38b39652012-06-05 09:22:59 -07003040 if (t != null && data.mDisableUrlOverride) {
3041 t.disableUrlOverridingForLoad();
3042 }
John Reck26b18322011-06-21 13:08:58 -07003043 loadUrl(t, data.mUrl, data.mHeaders);
3044 }
3045 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003046 }
3047
John Reck30c714c2010-12-16 17:30:34 -08003048 @Override
3049 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003050 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003051 if (tab.canGoBack()) {
3052 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003053 } else {
John Reckef654f12011-07-12 16:42:08 -07003054 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003055 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003056 }
3057
3058 void goBackOnePageOrQuit() {
3059 Tab current = mTabControl.getCurrentTab();
3060 if (current == null) {
3061 /*
3062 * Instead of finishing the activity, simply push this to the back
3063 * of the stack and let ActivityManager to choose the foreground
3064 * activity. As BrowserActivity is singleTask, it will be always the
3065 * root of the task. So we can use either true or false for
3066 * moveTaskToBack().
3067 */
luxiaolb40014b2013-07-19 10:01:43 +08003068 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003069 return;
3070 }
John Reckef654f12011-07-12 16:42:08 -07003071 if (current.canGoBack()) {
3072 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003073 } else {
3074 // Check to see if we are closing a window that was created by
3075 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003076 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003077 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003078 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003079 // Now we close the other tab
3080 closeTab(current);
3081 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003082 /*
3083 * Instead of finishing the activity, simply push this to the back
3084 * of the stack and let ActivityManager to choose the foreground
3085 * activity. As BrowserActivity is singleTask, it will be always the
3086 * root of the task. So we can use either true or false for
3087 * moveTaskToBack().
3088 */
luxiaolb40014b2013-07-19 10:01:43 +08003089 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003090 }
3091 }
3092 }
3093
3094 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003095 * helper method for key handler
3096 * returns the current tab if it can't advance
3097 */
Michael Kolbc831b632011-05-11 09:30:34 -07003098 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003099 int pos = mTabControl.getCurrentPosition() + 1;
3100 if (pos >= mTabControl.getTabCount()) {
3101 pos = 0;
3102 }
3103 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003104 }
3105
3106 /**
3107 * helper method for key handler
3108 * returns the current tab if it can't advance
3109 */
Michael Kolbc831b632011-05-11 09:30:34 -07003110 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003111 int pos = mTabControl.getCurrentPosition() - 1;
3112 if ( pos < 0) {
3113 pos = mTabControl.getTabCount() - 1;
3114 }
3115 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003116 }
3117
John Reckbcef87f2012-02-03 14:58:34 -08003118 boolean isMenuOrCtrlKey(int keyCode) {
3119 return (KeyEvent.KEYCODE_MENU == keyCode)
3120 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3121 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3122 }
3123
Michael Kolb0035fad2011-03-14 13:25:28 -07003124 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003125 * handle key events in browser
3126 *
3127 * @param keyCode
3128 * @param event
3129 * @return true if handled, false to pass to super
3130 */
John Reck9c35b9c2012-05-30 10:08:50 -07003131 @Override
3132 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003133 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003134 // Even if MENU is already held down, we need to call to super to open
3135 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003136 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003137 mMenuIsDown = true;
3138 return false;
3139 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003140
Cary Clark8ff8c662010-12-29 15:03:05 -05003141 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003142 Tab tab = getCurrentTab();
3143 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003144
Cary Clark160bbb92011-01-10 11:17:07 -05003145 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3146 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003147
Michael Kolb8233fac2010-10-26 16:08:53 -07003148 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003149 case KeyEvent.KEYCODE_TAB:
3150 if (event.isCtrlPressed()) {
3151 if (event.isShiftPressed()) {
3152 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003153 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003154 } else {
3155 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003156 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003157 }
3158 return true;
3159 }
3160 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003161 case KeyEvent.KEYCODE_SPACE:
3162 // WebView/WebTextView handle the keys in the KeyDown. As
3163 // the Activity's shortcut keys are only handled when WebView
3164 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003165 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003166 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003167 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003168 pageDown();
3169 }
3170 return true;
3171 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003172 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003173 event.startTracking();
3174 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003175 case KeyEvent.KEYCODE_FORWARD:
3176 if (!noModifiers) break;
3177 tab.goForward();
3178 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003179 case KeyEvent.KEYCODE_DPAD_LEFT:
3180 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003181 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003182 return true;
3183 }
3184 break;
3185 case KeyEvent.KEYCODE_DPAD_RIGHT:
3186 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003187 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003188 return true;
3189 }
3190 break;
3191 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003192 if (ctrl) {
3193 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003194 return true;
3195 }
3196 break;
Michael Kolba4183062011-01-16 10:43:21 -08003197// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003198 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003199 if (ctrl ) {
3200 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003201 return true;
3202 }
3203 break;
Michael Kolba4183062011-01-16 10:43:21 -08003204// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003205// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003206// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003207// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003208// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003209// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003210// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003211// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003212// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003213// case KeyEvent.KEYCODE_M: // unused
3214// case KeyEvent.KEYCODE_N: // in Chrome: new window
3215// case KeyEvent.KEYCODE_O: // in Chrome: open file
3216// case KeyEvent.KEYCODE_P: // in Chrome: print page
3217// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003218// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003219// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3220 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003221 // we can't use the ctrl/shift flags, they check for
3222 // exclusive use of a modifier
3223 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003224 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003225 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003226 } else {
3227 openTabToHomePage();
3228 }
3229 return true;
3230 }
3231 break;
3232// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3233// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003234// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003235// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3236// case KeyEvent.KEYCODE_Y: // unused
3237// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003238 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003239 // it is a regular key and webview is not null
3240 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003241 }
3242
John Reck9c35b9c2012-05-30 10:08:50 -07003243 @Override
3244 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003245 switch(keyCode) {
3246 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003247 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003248 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003249 return true;
3250 }
3251 break;
3252 }
3253 return false;
3254 }
3255
John Reck9c35b9c2012-05-30 10:08:50 -07003256 @Override
3257 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003258 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003259 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003260 if (KeyEvent.KEYCODE_MENU == keyCode
3261 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003262 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003263 }
3264 }
Cary Clark160bbb92011-01-10 11:17:07 -05003265 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003266 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003267 case KeyEvent.KEYCODE_BACK:
3268 if (event.isTracking() && !event.isCanceled()) {
3269 onBackKey();
3270 return true;
3271 }
3272 break;
3273 }
3274 return false;
3275 }
3276
3277 public boolean isMenuDown() {
3278 return mMenuIsDown;
3279 }
3280
John Reck9c35b9c2012-05-30 10:08:50 -07003281 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003282 public void setupAutoFill(Message message) {
3283 // Open the settings activity at the AutoFill profile fragment so that
3284 // the user can create a new profile. When they return, we will dispatch
3285 // the message so that we can autofill the form using their new profile.
3286 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3287 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3288 AutoFillSettingsFragment.class.getName());
3289 mAutoFillSetupMessage = message;
3290 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3291 }
John Reckb3417f02011-01-14 11:01:05 -08003292
John Reck9c35b9c2012-05-30 10:08:50 -07003293 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003294 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003295 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003296 return true;
3297 }
3298
John Reck1cf4b792011-07-26 10:22:22 -07003299 @Override
3300 public boolean shouldCaptureThumbnails() {
3301 return mUi.shouldCaptureThumbnails();
3302 }
3303
Michael Kolbc3af0672011-08-09 10:24:41 -07003304 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003305 public boolean supportsVoice() {
3306 PackageManager pm = mActivity.getPackageManager();
3307 List activities = pm.queryIntentActivities(new Intent(
3308 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3309 return activities.size() != 0;
3310 }
3311
3312 @Override
3313 public void startVoiceRecognizer() {
3314 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003315 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003316 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3317 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3318 mActivity.startActivityForResult(voice, VOICE_RESULT);
3319 }
3320
3321 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003322 public void setBlockEvents(boolean block) {
3323 mBlockEvents = block;
3324 }
3325
John Reck9c35b9c2012-05-30 10:08:50 -07003326 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003327 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003328 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003329 }
3330
John Reck9c35b9c2012-05-30 10:08:50 -07003331 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003332 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003333 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003334 }
3335
John Reck9c35b9c2012-05-30 10:08:50 -07003336 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003337 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003338 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003339 }
3340
John Reck9c35b9c2012-05-30 10:08:50 -07003341 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003342 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003343 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003344 }
3345
John Reck9c35b9c2012-05-30 10:08:50 -07003346 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003347 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003348 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003349 }
3350
Michael Kolb8233fac2010-10-26 16:08:53 -07003351}