blob: 4d72320eca71ced33c9ec1b5ea2eeb6b1fe34f17 [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;
Pankaj Garg7b279f62014-08-12 14:47:18 -070078import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070079import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070080import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080081import android.webkit.WebChromeClient.CustomViewCallback;
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;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070090import org.codeaurora.swe.WebBackForwardList;
91import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080092
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070093import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070094import com.android.browser.R;
95import com.android.browser.IntentHandler.UrlData;
96import com.android.browser.UI.ComboViews;
97import com.android.browser.mynavigation.AddMyNavigationPage;
98import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070099import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700100import com.android.browser.platformsupport.BrowserContract;
101import com.android.browser.platformsupport.WebAddress;
102import com.android.browser.platformsupport.BrowserContract.Images;
103import com.android.browser.provider.BrowserProvider2.Thumbnails;
104import com.android.browser.provider.SnapshotProvider.Snapshots;
105import com.android.browser.reflect.ReflectHelper;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700106
Michael Kolb8233fac2010-10-26 16:08:53 -0700107import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700108import java.io.File;
109import java.io.FileOutputStream;
110import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700111import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700112import java.text.DateFormat;
113import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700114import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700116import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700117import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800118import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200119import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700120import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700121
122/**
123 * Controller for browser
124 */
125public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700126 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700127
128 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800129 private static final String SEND_APP_ID_EXTRA =
130 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700131 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800132
Vivek Sekharb54614f2014-05-01 19:03:37 -0700133 // Remind switch to data connection if wifi is unavailable
134 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800135
Michael Kolb8233fac2010-10-26 16:08:53 -0700136 // public message ids
137 public final static int LOAD_URL = 1001;
138 public final static int STOP_LOAD = 1002;
139
140 // Message Ids
141 private static final int FOCUS_NODE_HREF = 102;
142 private static final int RELEASE_WAKELOCK = 107;
143
144 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
145
146 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800147 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700148
149 private static final int EMPTY_MENU = -1;
150
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700152 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700153 final static int PREFERENCES_PAGE = 3;
154 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000155 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700156 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800157 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000158
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
160
161 // As the ids are dynamically created, we can't guarantee that they will
162 // be in sequence, so this static array maps ids to a window number.
163 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
164 { R.id.window_one_menu_id, R.id.window_two_menu_id,
165 R.id.window_three_menu_id, R.id.window_four_menu_id,
166 R.id.window_five_menu_id, R.id.window_six_menu_id,
167 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
168
169 // "source" parameter for Google search through search key
170 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
171 // "source" parameter for Google search through simplily type
172 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
173
George Mount387d45d2011-10-07 15:57:53 -0700174 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700175 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
176
John Reckd7dd9b22011-08-30 09:18:29 -0700177 // A bitmap that is re-used in createScreenshot as scratch space
178 private static Bitmap sThumbnailBitmap;
179
Michael Kolb8233fac2010-10-26 16:08:53 -0700180 private Activity mActivity;
181 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700182 private HomepageHandler mHomepageHandler;
183 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700184 private BrowserSettings mSettings;
185 private WebViewFactory mFactory;
186
187 private WakeLock mWakeLock;
188
189 private UrlHandler mUrlHandler;
190 private UploadHandler mUploadHandler;
191 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 private PageDialogsHandler mPageDialogsHandler;
193 private NetworkStateHandler mNetworkHandler;
194
Ben Murdoch8029a772010-11-16 11:58:21 +0000195 private Message mAutoFillSetupMessage;
196
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800198 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700199
Michael Kolb8233fac2010-10-26 16:08:53 -0700200 // FIXME, temp address onPrepareMenu performance problem.
201 // When we move everything out of view, we should rewrite this.
202 private int mCurrentMenuState = 0;
203 private int mMenuState = R.id.MAIN_MENU;
204 private int mOldMenuState = EMPTY_MENU;
205 private Menu mCachedMenu;
206
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 private boolean mMenuIsDown;
208
209 // For select and find, we keep track of the ActionMode so that
210 // finish() can be called as desired.
211 private ActionMode mActionMode;
212
213 /**
214 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
215 * of whether the configuration has changed. The first onMenuOpened call
216 * after a configuration change is simply a reopening of the same menu
217 * (i.e. mIconView did not change).
218 */
219 private boolean mConfigChanged;
220
221 /**
222 * Keeps track of whether the options menu is open. This is important in
223 * determining whether to show or hide the title bar overlay
224 */
225 private boolean mOptionsMenuOpen;
226
227 /**
228 * Whether or not the options menu is in its bigger, popup menu form. When
229 * true, we want the title bar overlay to be gone. When false, we do not.
230 * Only meaningful if mOptionsMenuOpen is true.
231 */
232 private boolean mExtendedMenuOpen;
233
Michael Kolb8233fac2010-10-26 16:08:53 -0700234 private boolean mActivityPaused = true;
235 private boolean mLoadStopped;
236
237 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500238 // Checks to see when the bookmarks database has changed, and updates the
239 // Tabs' notion of whether they represent bookmarked sites.
240 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700241 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700242
Michael Kolbc3af0672011-08-09 10:24:41 -0700243 private boolean mBlockEvents;
244
Michael Kolb0b129122012-06-04 16:31:58 -0700245 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800246 private boolean mUpdateMyNavThumbnail;
247 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700248
George Mount3636d0a2011-11-21 09:08:21 -0800249 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700250 mActivity = browser;
251 mSettings = BrowserSettings.getInstance();
252 mTabControl = new TabControl(this);
253 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700254 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800255 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700256 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700257
258 mUrlHandler = new UrlHandler(this);
259 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
261
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500263 mBookmarksObserver = new ContentObserver(mHandler) {
264 @Override
265 public void onChange(boolean selfChange) {
266 int size = mTabControl.getTabCount();
267 for (int i = 0; i < size; i++) {
268 mTabControl.getTab(i).updateBookmarkedStatus();
269 }
270 }
271
272 };
273 browser.getContentResolver().registerContentObserver(
274 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700275
276 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700277 mHomepageHandler = new HomepageHandler(browser, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 }
279
John Reck9c35b9c2012-05-30 10:08:50 -0700280 @Override
281 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800282 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800283 // mCrashRecoverHandler has any previously saved state.
284 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700285 }
286
George Mount3636d0a2011-11-21 09:08:21 -0800287 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800288 // we dont want to ever recover incognito tabs
289 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700290
Patrick Scott7d50a932011-02-04 09:27:26 -0500291 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700292 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500293 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000294
Michael Kolbc831b632011-05-11 09:30:34 -0700295 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500296 // Not able to restore so we go ahead and clear session cookies. We
297 // must do this before trying to login the user as we don't want to
298 // clear any session cookies set during login.
299 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700300 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800301 if (intent == null) {
302 // This won't happen under common scenarios. The icicle is
303 // not null, but there aren't any tabs to restore.
304 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700305 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800306 final Bundle extra = intent.getExtras();
307 // Create an initial tab.
308 // If the intent is ACTION_VIEW and data is not null, the Browser is
309 // invoked to view the content by another application. In this case,
310 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800311 UrlData urlData = null;
312 if (intent.getData() != null
313 && Intent.ACTION_VIEW.equals(intent.getAction())
314 && intent.getData().toString().startsWith("content://")) {
315 urlData = new UrlData(intent.getData().toString());
316 } else {
317 urlData = IntentHandler.getUrlDataFromIntent(intent);
318 }
George Mount3636d0a2011-11-21 09:08:21 -0800319 Tab t = null;
320 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700321 String landingPage = mActivity.getResources().getString(
322 R.string.def_landing_page);
323 if (!landingPage.isEmpty()) {
324 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800325 } else {
326 t = openTabToHomePage();
327 }
George Mount3636d0a2011-11-21 09:08:21 -0800328 } else {
329 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700330 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800331 }
332 if (t != null) {
333 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
334 }
335 WebView webView = t.getWebView();
336 if (extra != null) {
337 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
338 if (scale > 0 && scale <= 1000) {
339 webView.setInitialScale(scale);
340 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700341 }
342 }
John Reckd8c74522011-06-14 08:45:00 -0700343 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700345 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500346 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700347 List<Tab> tabs = mTabControl.getTabs();
348 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700349
John Reck1cf4b792011-07-26 10:22:22 -0700350 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700351 //handle restored pages that may require a JS interface
352 if (t.getWebView() != null) {
353 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
354 if (backForwardList != null) {
355 for (int i = 0; i < backForwardList.getSize(); i++) {
356 WebHistoryItem item = backForwardList.getItemAtIndex(i);
357 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
358 }
359 }
360 }
John Reck1cf4b792011-07-26 10:22:22 -0700361 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700362 if (t != mTabControl.getCurrentTab()) {
363 t.pause();
364 }
John Reck1cf4b792011-07-26 10:22:22 -0700365 }
366 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700367 if (tabs.size() == 0) {
368 openTabToHomePage();
369 }
John Reck1cf4b792011-07-26 10:22:22 -0700370 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700371 // TabControl.restoreState() will create a new tab even if
372 // restoring the state fails.
373 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800374 // Intent is non-null when framework thinks the browser should be
375 // launching with a new intent (icicle is null).
376 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700377 mIntentHandler.onNewIntent(intent);
378 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700380 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700381 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800382 if (jsFlags.trim().length() != 0) {
383 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700384 }
George Mount3636d0a2011-11-21 09:08:21 -0800385 if (intent != null
386 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700387 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800388 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700389 }
390
John Reck1cf4b792011-07-26 10:22:22 -0700391 private static class PruneThumbnails implements Runnable {
392 private Context mContext;
393 private List<Long> mIds;
394
395 PruneThumbnails(Context context, List<Long> preserveIds) {
396 mContext = context.getApplicationContext();
397 mIds = preserveIds;
398 }
399
400 @Override
401 public void run() {
402 ContentResolver cr = mContext.getContentResolver();
403 if (mIds == null || mIds.size() == 0) {
404 cr.delete(Thumbnails.CONTENT_URI, null, null);
405 } else {
406 int length = mIds.size();
407 StringBuilder where = new StringBuilder();
408 where.append(Thumbnails._ID);
409 where.append(" not in (");
410 for (int i = 0; i < length; i++) {
411 where.append(mIds.get(i));
412 if (i < (length - 1)) {
413 where.append(",");
414 }
415 }
416 where.append(")");
417 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
418 }
419 }
420
421 }
422
Michael Kolb1514bb72010-11-22 09:11:48 -0800423 @Override
424 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700425 return mFactory;
426 }
427
428 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800429 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800430 if (tab.hasCrashed)
431 tab.showCrashView();
432 else
433 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800434 }
435
436 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800437 public void createSubWindow(Tab tab) {
438 endActionMode();
439 WebView mainView = tab.getWebView();
440 WebView subView = mFactory.createWebView((mainView == null)
441 ? false
442 : mainView.isPrivateBrowsingEnabled());
443 mUi.createSubWindow(tab, subView);
444 }
445
446 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700447 public Context getContext() {
448 return mActivity;
449 }
450
451 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700452 public Activity getActivity() {
453 return mActivity;
454 }
455
456 void setUi(UI ui) {
457 mUi = ui;
458 }
459
Michael Kolbaf63dba2012-05-16 12:58:05 -0700460 @Override
461 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700462 return mSettings;
463 }
464
465 IntentHandler getIntentHandler() {
466 return mIntentHandler;
467 }
468
469 @Override
470 public UI getUi() {
471 return mUi;
472 }
473
474 int getMaxTabs() {
475 return mActivity.getResources().getInteger(R.integer.max_tabs);
476 }
477
478 @Override
479 public TabControl getTabControl() {
480 return mTabControl;
481 }
482
Michael Kolb1bf23132010-11-19 12:55:12 -0800483 @Override
484 public List<Tab> getTabs() {
485 return mTabControl.getTabs();
486 }
487
Michael Kolb8233fac2010-10-26 16:08:53 -0700488 private void startHandler() {
489 mHandler = new Handler() {
490
491 @Override
492 public void handleMessage(Message msg) {
493 switch (msg.what) {
494 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700495 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 break;
497 case FOCUS_NODE_HREF:
498 {
499 String url = (String) msg.getData().get("url");
500 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500501 String src = (String) msg.getData().get("src");
502 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 if (TextUtils.isEmpty(url)) {
504 break;
505 }
506 HashMap focusNodeMap = (HashMap) msg.obj;
507 WebView view = (WebView) focusNodeMap.get("webview");
508 // Only apply the action if the top window did not change.
509 if (getCurrentTopWebView() != view) {
510 break;
511 }
512 switch (msg.arg1) {
513 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700514 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700515 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500516 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700517 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500518 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500519 case R.id.open_newtab_context_menu_id:
520 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700521 openTab(url, parent,
522 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500523 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700524 case R.id.copy_link_context_menu_id:
525 copy(url);
526 break;
527 case R.id.save_link_context_menu_id:
528 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500529 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800530 mActivity, url, view.getSettings().getUserAgentString(),
531 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700532 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700533 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700534 if(title == null || title == "")
535 title = url;
536
537 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
538 //SWE TODO: No thumbnail support for the url obtained via
539 //browser context menu as its not loaded in webview.
540 if (bookmarkIntent != null) {
541 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
542 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
543 mActivity.startActivity(bookmarkIntent);
544 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700545 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 }
547 break;
548 }
549
550 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700551 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700552 break;
553
554 case STOP_LOAD:
555 stopLoading();
556 break;
557
558 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700559 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 mWakeLock.release();
561 // if we reach here, Browser should be still in the
562 // background loading after WAKELOCK_TIMEOUT (5-min).
563 // To avoid burning the battery, stop loading.
564 mTabControl.stopAllLoading();
565 }
566 break;
567
568 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800569 Tab tab = (Tab) msg.obj;
570 if (tab != null) {
571 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700572 }
573 break;
kaiyiz591110b2013-08-06 17:11:06 +0800574 case OPEN_MENU:
575 if (!mOptionsMenuOpen && mActivity != null ) {
576 mActivity.openOptionsMenu();
577 }
578 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 }
580 }
581 };
582
583 }
584
John Reckef654f12011-07-12 16:42:08 -0700585 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200586 public Tab getCurrentTab() {
587 return mTabControl.getCurrentTab();
588 }
589
Michael Kolbba99c5d2010-11-29 14:57:41 -0800590 @Override
591 public void shareCurrentPage() {
592 shareCurrentPage(mTabControl.getCurrentTab());
593 }
594
595 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700596 if (tab == null || tab.getWebView() == null)
597 return;
598
599 final Tab mytab = tab;
600 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
601 @Override
602 public void onReceiveValue(Bitmap bitmap) {
603 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
604 mytab.getFavicon(), bitmap);
605 }
606 };
607
608 createScreenshotAsync(
609 tab.getWebView(),
610 getDesiredThumbnailWidth(mActivity),
611 getDesiredThumbnailHeight(mActivity),
612 new ValueCallback<Bitmap>() {
613 @Override
614 public void onReceiveValue(Bitmap bitmap) {
615 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
616 mytab.getFavicon(), bitmap);
617 }
618 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800619 }
620
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 /**
622 * Share a page, providing the title, url, favicon, and a screenshot. Uses
623 * an {@link Intent} to launch the Activity chooser.
624 * @param c Context used to launch a new Activity.
625 * @param title Title of the page. Stored in the Intent with
626 * {@link Intent#EXTRA_SUBJECT}
627 * @param url URL of the page. Stored in the Intent with
628 * {@link Intent#EXTRA_TEXT}
629 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
630 * with {@link Browser#EXTRA_SHARE_FAVICON}
631 * @param screenshot Bitmap of a screenshot of the page. Stored in the
632 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
633 */
634 static final void sharePage(Context c, String title, String url,
635 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700636
637 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
638 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
639 R.layout.app_row, sDialog.getApps());
640 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700641 }
642
643 private void copy(CharSequence text) {
644 ClipboardManager cm = (ClipboardManager) mActivity
645 .getSystemService(Context.CLIPBOARD_SERVICE);
646 cm.setText(text);
647 }
648
649 // lifecycle
650
John Reck9c35b9c2012-05-30 10:08:50 -0700651 @Override
652 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800654 // update the menu in case of a locale change
655 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800656 if (mOptionsMenuOpen) {
657 mActivity.closeOptionsMenu();
658 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
659 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 if (mPageDialogsHandler != null) {
661 mPageDialogsHandler.onConfigurationChanged(config);
662 }
663 mUi.onConfigurationChanged(config);
664 }
665
666 @Override
667 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700668 if (!mUi.isWebShowing()) {
669 mUi.showWeb(false);
670 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700671 mIntentHandler.onNewIntent(intent);
672 }
673
John Reck9c35b9c2012-05-30 10:08:50 -0700674 @Override
675 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800676 if (mUi.isCustomViewShowing()) {
677 hideCustomView();
678 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 if (mActivityPaused) {
680 Log.e(LOGTAG, "BrowserActivity is already paused.");
681 return;
682 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800684 Tab tab = mTabControl.getCurrentTab();
685 if (tab != null) {
686 tab.pause();
687 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700688 if (mWakeLock == null) {
689 PowerManager pm = (PowerManager) mActivity
690 .getSystemService(Context.POWER_SERVICE);
691 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
692 }
Michael Kolb70976932010-11-30 11:34:01 -0800693 mWakeLock.acquire();
694 mHandler.sendMessageDelayed(mHandler
695 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
696 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 }
698 mUi.onPause();
699 mNetworkHandler.onPause();
700
701 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100702 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 }
704
John Reck9c35b9c2012-05-30 10:08:50 -0700705 @Override
706 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800708 Bundle saveState = createSaveState();
709
710 // crash recovery manages all save & restore state
711 mCrashRecoveryHandler.writeState(saveState);
712 mSettings.setLastRunPaused(true);
713 }
714
715 /**
716 * Save the current state to outState. Does not write the state to
717 * disk.
718 * @return Bundle containing the current state of all tabs.
719 */
720 /* package */ Bundle createSaveState() {
721 Bundle saveState = new Bundle();
722 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800723 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700724 }
725
John Reck9c35b9c2012-05-30 10:08:50 -0700726 @Override
727 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700728 if (!mActivityPaused) {
729 Log.e(LOGTAG, "BrowserActivity is already resumed.");
730 return;
731 }
George Mount3636d0a2011-11-21 09:08:21 -0800732 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800734 Tab current = mTabControl.getCurrentTab();
735 if (current != null) {
736 current.resume();
737 resumeWebViewTimers(current);
738 }
John Reckf57c0292011-07-21 18:15:39 -0700739 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200740
Michael Kolb8233fac2010-10-26 16:08:53 -0700741 mUi.onResume();
742 mNetworkHandler.onResume();
743 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100744 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700745 if (mVoiceResult != null) {
746 mUi.onVoiceResult(mVoiceResult);
747 mVoiceResult = null;
748 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800749 if (current != null && current.hasCrashed) {
750 current.showCrashView();
751 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700752 }
753
John Reckf57c0292011-07-21 18:15:39 -0700754 private void releaseWakeLock() {
755 if (mWakeLock != null && mWakeLock.isHeld()) {
756 mHandler.removeMessages(RELEASE_WAKELOCK);
757 mWakeLock.release();
758 }
759 }
760
Michael Kolb70976932010-11-30 11:34:01 -0800761 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800762 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800763 * @param tab guaranteed non-null
764 */
765 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700766 boolean inLoad = tab.inPageLoad();
767 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
768 CookieSyncManager.getInstance().startSync();
769 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100770 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700771 }
772 }
773
Michael Kolb70976932010-11-30 11:34:01 -0800774 /**
775 * Pause all WebView timers using the WebView of the given tab
776 * @param tab
777 * @return true if the timers are paused or tab is null
778 */
779 private boolean pauseWebViewTimers(Tab tab) {
780 if (tab == null) {
781 return true;
782 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700783 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100784 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700785 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 }
Michael Kolb70976932010-11-30 11:34:01 -0800787 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700788 }
789
John Reck9c35b9c2012-05-30 10:08:50 -0700790 @Override
791 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800792 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700793 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
794 mUploadHandler = null;
795 }
796 if (mTabControl == null) return;
797 mUi.onDestroy();
798 // Remove the current tab and sub window
799 Tab t = mTabControl.getCurrentTab();
800 if (t != null) {
801 dismissSubWindow(t);
802 removeTab(t);
803 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500804 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 // Destroy all the tabs
806 mTabControl.destroy();
Michael Kolb8233fac2010-10-26 16:08:53 -0700807 }
808
809 protected boolean isActivityPaused() {
810 return mActivityPaused;
811 }
812
John Reck9c35b9c2012-05-30 10:08:50 -0700813 @Override
814 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700815 mTabControl.freeMemory();
816 }
817
818 @Override
819 public boolean shouldShowErrorConsole() {
820 return mShouldShowErrorConsole;
821 }
822
823 protected void setShouldShowErrorConsole(boolean show) {
824 if (show == mShouldShowErrorConsole) {
825 // Nothing to do.
826 return;
827 }
828 mShouldShowErrorConsole = show;
829 Tab t = mTabControl.getCurrentTab();
830 if (t == null) {
831 // There is no current tab so we cannot toggle the error console
832 return;
833 }
834 mUi.setShouldShowErrorConsole(t, show);
835 }
836
837 @Override
838 public void stopLoading() {
839 mLoadStopped = true;
840 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700842 if (w != null) {
843 w.stopLoading();
844 mUi.onPageStopped(tab);
845 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700846 }
847
848 boolean didUserStopLoading() {
849 return mLoadStopped;
850 }
851
kaiyiza016da12013-08-26 17:50:22 +0800852 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700853 final String reminderType = getContext().getResources().getString(
854 R.string.def_wifi_browser_interaction_remind_type);
855 final String selectionConnnection = getContext().getResources().getString(
856 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700857 final String wifiSelection = getContext().getResources().getString(
858 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700859
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700860 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
861 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700862 return;
863
kaiyiza016da12013-08-26 17:50:22 +0800864 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700865 Context.CONNECTIVITY_SERVICE);
866 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700867 WifiManager wifiMgr = (WifiManager) this.getContext()
868 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700869 if (networkInfo == null
870 || (networkInfo != null && (networkInfo.getType() !=
871 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700872 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
873 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700874 List<ScanResult> list = wifiMgr.getScanResults();
875 // Have no AP's for Wifi's fall back to data
876 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700877 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700878 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
879 this.getContext().startActivity(intent);
880 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700881 // Request to select Wifi AP
882 try {
883 Intent intent = new Intent(wifiSelection);
884 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
885 this.getContext().startActivity(intent);
886 } catch (Exception e) {
887 String err_msg = this.getContext().getString(
888 R.string.acivity_not_found, wifiSelection);
889 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
890 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700891 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700892 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800893 }
894 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700895
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 // WebViewController
897
898 @Override
John Reck324d4402011-01-11 16:56:42 -0800899 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700900
901 // We've started to load a new page. If there was a pending message
902 // to save a screenshot then we will now take the new page and save
903 // an incorrect screenshot. Therefore, remove any pending thumbnail
904 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700905 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700906
907 // reset sync timer to avoid sync starts during loading a page
908 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700909 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700910 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700911 boolean networkNotifier =
912 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700913 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700914 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800915 } else {
916 if (!mNetworkHandler.isNetworkUp()) {
917 view.setNetworkAvailable(false);
918 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700919 }
920
921 // when BrowserActivity just starts, onPageStarted may be called before
922 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
923 // to start the timer. As we won't switch tabs while an activity is in
924 // pause state, we can ensure calling resume and pause in pair.
925 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800926 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700927 }
928 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 endActionMode();
930
John Reck30c714c2010-12-16 17:30:34 -0800931 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700932
John Reck324d4402011-01-11 16:56:42 -0800933 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700934 // update the bookmark database for favicon
935 maybeUpdateFavicon(tab, null, url, favicon);
936
937 Performance.tracePageStart(url);
938
939 // Performance probe
940 if (false) {
941 Performance.onPageStarted();
942 }
943
944 }
945
946 @Override
John Reck324d4402011-01-11 16:56:42 -0800947 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700948 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800949 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200950
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 // Performance probe
952 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800953 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700954 }
955
Tarun Nainani8eb00912014-07-17 12:28:32 -0700956 tab.onPageFinished();
957
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 Performance.tracePageFinished();
959 }
960
961 @Override
John Reck30c714c2010-12-16 17:30:34 -0800962 public void onProgressChanged(Tab tab) {
963 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700964
965 if (newProgress == 100) {
966 CookieSyncManager.getInstance().sync();
967 // onProgressChanged() may continue to be called after the main
968 // frame has finished loading, as any remaining sub frames continue
969 // to load. We'll only get called once though with newProgress as
970 // 100 when everything is loaded. (onPageFinished is called once
971 // when the main frame completes loading regardless of the state of
972 // any sub frames so calls to onProgressChanges may continue after
973 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800974 if (tab.inPageLoad()) {
975 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200976 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
977 // pause the WebView timer and release the wake lock if it is
978 // finished while BrowserActivity is in pause state.
979 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 }
John Reckd9862372012-02-21 15:04:50 -0800981 if (!tab.isPrivateBrowsingEnabled()
982 && !TextUtils.isEmpty(tab.getUrl())
983 && !tab.isSnapshot()) {
984 // Only update the bookmark screenshot if the user did not
985 // cancel the load early and there is not already
986 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700987 if (tab.shouldUpdateThumbnail() &&
988 (tab.inForeground() && !didUserStopLoading()
989 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800990 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
991 mHandler.sendMessageDelayed(mHandler.obtainMessage(
992 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800993 1500);
John Reckd9862372012-02-21 15:04:50 -0800994 }
995 }
996 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800998 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700999 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001000 // still loading
1001 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001002 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001003 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 }
1005 }
John Reck30c714c2010-12-16 17:30:34 -08001006 mUi.onProgressChanged(tab);
1007 }
1008
1009 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001010 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001011 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001012 }
1013
1014 @Override
1015 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001016 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001017 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001018 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001019 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1020 return;
1021 }
1022 // Update the title in the history database if not in private browsing mode
1023 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001024 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001025 }
1026 }
1027
1028 @Override
1029 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001030 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1032 }
1033
1034 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001035 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1036 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 }
1038
1039 @Override
1040 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1041 if (mMenuIsDown) {
1042 // only check shortcut key when MENU is held
1043 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1044 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001045 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001046 int keyCode = event.getKeyCode();
1047 // We need to send almost every key to WebKit. However:
1048 // 1. We don't want to block the device on the renderer for
1049 // some keys like menu, home, call.
1050 // 2. There are no WebKit equivalents for some of these keys
1051 // (see app/keyboard_codes_win.h)
1052 // Note that these are not the same set as KeyEvent.isSystemKey:
1053 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1054 if (keyCode == KeyEvent.KEYCODE_MENU ||
1055 keyCode == KeyEvent.KEYCODE_HOME ||
1056 keyCode == KeyEvent.KEYCODE_BACK ||
1057 keyCode == KeyEvent.KEYCODE_CALL ||
1058 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1059 keyCode == KeyEvent.KEYCODE_POWER ||
1060 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1061 keyCode == KeyEvent.KEYCODE_CAMERA ||
1062 keyCode == KeyEvent.KEYCODE_FOCUS ||
1063 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1064 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1065 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1066 return true;
1067 }
1068
1069 // We also have to intercept some shortcuts before we send them to the ContentView.
1070 if (event.isCtrlPressed() && (
1071 keyCode == KeyEvent.KEYCODE_TAB ||
1072 keyCode == KeyEvent.KEYCODE_W ||
1073 keyCode == KeyEvent.KEYCODE_F4)) {
1074 return true;
1075 }
1076
1077 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001078 }
1079
1080 @Override
John Reck997b1b72012-04-19 18:08:25 -07001081 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 if (!isActivityPaused()) {
1083 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001084 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 } else {
John Reck997b1b72012-04-19 18:08:25 -07001086 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001087 }
1088 }
John Reck997b1b72012-04-19 18:08:25 -07001089 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001090 }
1091
1092 @Override
John Reck324d4402011-01-11 16:56:42 -08001093 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001094 boolean disableHistoryWrites =
1095 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1096
1097 // Don't save anything in private browsing mode or when explicitly set
1098 // not to write history via an overlay
1099 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001100 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001101
John Reck324d4402011-01-11 16:56:42 -08001102 if (TextUtils.isEmpty(url)
1103 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001104 return;
1105 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001106
John Reckf57c0292011-07-21 18:15:39 -07001107 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001108 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 }
1110
1111 @Override
1112 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1113 AsyncTask<Void, Void, String[]> task =
1114 new AsyncTask<Void, Void, String[]>() {
1115 @Override
1116 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001117 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001118 }
1119 @Override
1120 public void onPostExecute(String[] result) {
1121 callback.onReceiveValue(result);
1122 }
1123 };
1124 task.execute();
1125 }
1126
1127 @Override
1128 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1129 final HttpAuthHandler handler, final String host,
1130 final String realm) {
1131 String username = null;
1132 String password = null;
1133
1134 boolean reuseHttpAuthUsernamePassword
1135 = handler.useHttpAuthUsernamePassword();
1136
1137 if (reuseHttpAuthUsernamePassword && view != null) {
1138 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1139 if (credentials != null && credentials.length == 2) {
1140 username = credentials[0];
1141 password = credentials[1];
1142 }
1143 }
1144
1145 if (username != null && password != null) {
1146 handler.proceed(username, password);
1147 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001148 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001149 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1150 } else {
1151 handler.cancel();
1152 }
1153 }
1154 }
1155
1156 @Override
1157 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001158 String contentDisposition, String mimetype, String referer,
1159 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001160 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001161 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001162 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001163 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001164 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 // This Tab was opened for the sole purpose of downloading a
1166 // file. Remove it.
1167 if (tab == mTabControl.getCurrentTab()) {
1168 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001169 if (tab.getDerivedFromIntent())
1170 closeTab(tab);
1171 else
1172 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001173 } else {
1174 // In this case, it is not.
1175 closeTab(tab);
1176 }
1177 }
1178 }
1179
1180 @Override
1181 public Bitmap getDefaultVideoPoster() {
1182 return mUi.getDefaultVideoPoster();
1183 }
1184
1185 @Override
1186 public View getVideoLoadingProgressView() {
1187 return mUi.getVideoLoadingProgressView();
1188 }
1189
1190 @Override
1191 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1192 SslError error) {
1193 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1194 }
1195
1196 // helper method
1197
1198 /*
1199 * Update the favorites icon if the private browsing isn't enabled and the
1200 * icon is valid.
1201 */
1202 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1203 final String url, Bitmap favicon) {
1204 if (favicon == null) {
1205 return;
1206 }
1207 if (!tab.isPrivateBrowsingEnabled()) {
1208 Bookmarks.updateFavicon(mActivity
1209 .getContentResolver(), originalUrl, url, favicon);
1210 }
1211 }
1212
Leon Scroggins4cd97792010-12-03 15:31:56 -05001213 @Override
1214 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001215 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001216 mUi.bookmarkedStatusHasChanged(tab);
1217 }
1218
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 // end WebViewController
1220
1221 protected void pageUp() {
1222 getCurrentTopWebView().pageUp(false);
1223 }
1224
1225 protected void pageDown() {
1226 getCurrentTopWebView().pageDown(false);
1227 }
1228
1229 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001230 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 public void editUrl() {
1232 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001233 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001234 }
1235
John Reck9c35b9c2012-05-30 10:08:50 -07001236 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001237 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001238 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 if (tab.inForeground()) {
1240 if (mUi.isCustomViewShowing()) {
1241 callback.onCustomViewHidden();
1242 return;
1243 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001244 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001245 // Save the menu state and set it to empty while the custom
1246 // view is showing.
1247 mOldMenuState = mMenuState;
1248 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001249 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001250 }
1251 }
1252
1253 @Override
1254 public void hideCustomView() {
1255 if (mUi.isCustomViewShowing()) {
1256 mUi.onHideCustomView();
1257 // Reset the old menu state.
1258 mMenuState = mOldMenuState;
1259 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001260 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001261 }
1262 }
1263
John Reck9c35b9c2012-05-30 10:08:50 -07001264 @Override
1265 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001266 Intent intent) {
1267 if (getCurrentTopWebView() == null) return;
1268 switch (requestCode) {
1269 case PREFERENCES_PAGE:
1270 if (resultCode == Activity.RESULT_OK && intent != null) {
1271 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001272 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 mTabControl.removeParentChildRelationShips();
1274 }
1275 }
1276 break;
1277 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001278 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001279 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001280 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001282 case AUTOFILL_SETUP:
1283 // Determine whether a profile was actually set up or not
1284 // and if so, send the message back to the WebTextView to
1285 // fill the form with the new profile.
1286 if (getSettings().getAutoFillProfile() != null) {
1287 mAutoFillSetupMessage.sendToTarget();
1288 mAutoFillSetupMessage = null;
1289 }
1290 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001291 case COMBO_VIEW:
1292 if (intent == null || resultCode != Activity.RESULT_OK) {
1293 break;
1294 }
John Reck3ba45532011-08-11 16:26:53 -07001295 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001296 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1297 Tab t = getCurrentTab();
1298 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001299 mUpdateMyNavThumbnail = true;
1300 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001301 loadUrl(t, uri.toString());
1302 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1303 String[] urls = intent.getStringArrayExtra(
1304 ComboViewActivity.EXTRA_OPEN_ALL);
1305 Tab parent = getCurrentTab();
1306 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001307 if (url != null) {
1308 parent = openTab(url, parent,
1309 !mSettings.openInBackground(), true);
1310 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001311 }
1312 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1313 long id = intent.getLongExtra(
1314 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1315 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001316 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001317 }
1318 }
1319 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001320 case VOICE_RESULT:
1321 if (resultCode == Activity.RESULT_OK && intent != null) {
1322 ArrayList<String> results = intent.getStringArrayListExtra(
1323 RecognizerIntent.EXTRA_RESULTS);
1324 if (results.size() >= 1) {
1325 mVoiceResult = results.get(0);
1326 }
1327 }
1328 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001329 case MY_NAVIGATION:
1330 if (intent == null || resultCode != Activity.RESULT_OK) {
1331 break;
1332 }
1333
1334 if (intent.getBooleanExtra("need_refresh", false) &&
1335 getCurrentTopWebView() != null) {
1336 getCurrentTopWebView().reload();
1337 }
1338 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001339 default:
1340 break;
1341 }
1342 getCurrentTopWebView().requestFocus();
1343 }
1344
1345 /**
1346 * Open the Go page.
1347 * @param startWithHistory If true, open starting on the history tab.
1348 * Otherwise, start with the bookmarks tab.
1349 */
1350 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001351 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001352 if (mTabControl.getCurrentWebView() == null) {
1353 return;
1354 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001355 // clear action mode
1356 if (isInCustomActionMode()) {
1357 endActionMode();
1358 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001359 Bundle extras = new Bundle();
1360 // Disable opening in a new window if we have maxed out the windows
1361 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1362 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001363 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 }
1365
1366 // combo view callbacks
1367
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 // key handling
1369 protected void onBackKey() {
1370 if (!mUi.onBackKey()) {
1371 WebView subwindow = mTabControl.getCurrentSubWindow();
1372 if (subwindow != null) {
1373 if (subwindow.canGoBack()) {
1374 subwindow.goBack();
1375 } else {
1376 dismissSubWindow(mTabControl.getCurrentTab());
1377 }
1378 } else {
1379 goBackOnePageOrQuit();
1380 }
1381 }
1382 }
1383
Michael Kolb4bd767d2011-05-27 11:33:55 -07001384 protected boolean onMenuKey() {
1385 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001386 }
1387
Michael Kolb8233fac2010-10-26 16:08:53 -07001388 // menu handling and state
1389 // TODO: maybe put into separate handler
1390
John Reck9c35b9c2012-05-30 10:08:50 -07001391 @Override
1392 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001393 if (mMenuState == EMPTY_MENU) {
1394 return false;
1395 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 MenuInflater inflater = mActivity.getMenuInflater();
1397 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 return true;
1399 }
1400
John Reck9c35b9c2012-05-30 10:08:50 -07001401 @Override
1402 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001403 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001404 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001405 return;
1406 }
1407 if (!(v instanceof WebView)) {
1408 return;
1409 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001410 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001411 WebView.HitTestResult result = webview.getHitTestResult();
1412 if (result == null) {
1413 return;
1414 }
1415
1416 int type = result.getType();
1417 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1418 Log.w(LOGTAG,
1419 "We should not show context menu when nothing is touched");
1420 return;
1421 }
1422 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1423 // let TextView handles context menu
1424 return;
1425 }
1426
1427 // Note, http://b/issue?id=1106666 is requesting that
1428 // an inflated menu can be used again. This is not available
1429 // yet, so inflate each time (yuk!)
1430 MenuInflater inflater = mActivity.getMenuInflater();
1431 inflater.inflate(R.menu.browsercontext, menu);
1432
1433 // Show the correct menu group
1434 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001435 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001436 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001437 menu.setGroupVisible(R.id.PHONE_MENU,
1438 type == WebView.HitTestResult.PHONE_TYPE);
1439 menu.setGroupVisible(R.id.EMAIL_MENU,
1440 type == WebView.HitTestResult.EMAIL_TYPE);
1441 menu.setGroupVisible(R.id.GEO_MENU,
1442 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001443 String itemUrl = null;
1444 String url = webview.getOriginalUrl();
1445 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1446 itemUrl = Uri.decode(navigationUrl);
1447 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1448 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1449 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1450 } else {
1451 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1452 }
1453 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1454 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1455 } else {
1456 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1457
1458 menu.setGroupVisible(R.id.IMAGE_MENU,
1459 type == WebView.HitTestResult.IMAGE_TYPE
1460 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1461 menu.setGroupVisible(R.id.ANCHOR_MENU,
1462 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1463 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001464 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1465 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001466 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001467 // Setup custom handling depending on the type
1468 switch (type) {
1469 case WebView.HitTestResult.PHONE_TYPE:
1470 menu.setHeaderTitle(Uri.decode(extra));
1471 menu.findItem(R.id.dial_context_menu_id).setIntent(
1472 new Intent(Intent.ACTION_VIEW, Uri
1473 .parse(WebView.SCHEME_TEL + extra)));
1474 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1475 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1476 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1477 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1478 addIntent);
1479 menu.findItem(R.id.copy_phone_context_menu_id)
1480 .setOnMenuItemClickListener(
1481 new Copy(extra));
1482 break;
1483
1484 case WebView.HitTestResult.EMAIL_TYPE:
1485 menu.setHeaderTitle(extra);
1486 menu.findItem(R.id.email_context_menu_id).setIntent(
1487 new Intent(Intent.ACTION_VIEW, Uri
1488 .parse(WebView.SCHEME_MAILTO + extra)));
1489 menu.findItem(R.id.copy_mail_context_menu_id)
1490 .setOnMenuItemClickListener(
1491 new Copy(extra));
1492 break;
1493
1494 case WebView.HitTestResult.GEO_TYPE:
1495 menu.setHeaderTitle(extra);
1496 menu.findItem(R.id.map_context_menu_id).setIntent(
1497 new Intent(Intent.ACTION_VIEW, Uri
1498 .parse(WebView.SCHEME_GEO
1499 + URLEncoder.encode(extra))));
1500 menu.findItem(R.id.copy_geo_context_menu_id)
1501 .setOnMenuItemClickListener(
1502 new Copy(extra));
1503 break;
1504
1505 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1506 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001507 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001508 // decide whether to show the open link in new tab option
1509 boolean showNewTab = mTabControl.canCreateNewTab();
1510 MenuItem newTabItem
1511 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001512 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001513 ? R.string.contextmenu_openlink_newwindow_background
1514 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001515 newTabItem.setVisible(showNewTab);
1516 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001517 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1518 newTabItem.setOnMenuItemClickListener(
1519 new MenuItem.OnMenuItemClickListener() {
1520 @Override
1521 public boolean onMenuItemClick(MenuItem item) {
1522 final HashMap<String, WebView> hrefMap =
1523 new HashMap<String, WebView>();
1524 hrefMap.put("webview", webview);
1525 final Message msg = mHandler.obtainMessage(
1526 FOCUS_NODE_HREF,
1527 R.id.open_newtab_context_menu_id,
1528 0, hrefMap);
1529 webview.requestFocusNodeHref(msg);
1530 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001531 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001532 });
1533 } else {
1534 newTabItem.setOnMenuItemClickListener(
1535 new MenuItem.OnMenuItemClickListener() {
1536 @Override
1537 public boolean onMenuItemClick(MenuItem item) {
1538 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001539 openTab(extra, parent,
1540 !mSettings.openInBackground(),
1541 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001542 return true;
1543 }
1544 });
1545 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001546 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001547 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1548 menu.setHeaderTitle(navigationUrl);
1549 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1550
1551 if (itemUrl != null) {
1552 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1553 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1554 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1555 @Override
1556 public boolean onMenuItemClick(MenuItem item) {
1557 final Intent intent = new Intent(Controller.this
1558 .getContext(),
1559 AddMyNavigationPage.class);
1560 Bundle bundle = new Bundle();
1561 String url = Uri.decode(navigationUrl);
1562 bundle.putBoolean("isAdding", false);
1563 bundle.putString("url", url);
1564 bundle.putString("name", getNameFromUrl(url));
1565 intent.putExtra("websites", bundle);
1566 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1567 return false;
1568 }
1569 });
1570 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1571 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1572 @Override
1573 public boolean onMenuItemClick(MenuItem item) {
1574 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1575 return false;
1576 }
1577 });
1578 }
1579 } else {
1580 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1581 }
1582 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001583 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1584 break;
1585 }
1586 // otherwise fall through to handle image part
1587 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001588 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1589 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001590 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1591 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001592 shareItem.setOnMenuItemClickListener(
1593 new MenuItem.OnMenuItemClickListener() {
1594 @Override
1595 public boolean onMenuItemClick(MenuItem item) {
1596 sharePage(mActivity, null, extra, null,
1597 null);
1598 return true;
1599 }
1600 }
1601 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001603 menu.findItem(R.id.view_image_context_menu_id)
1604 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1605 @Override
1606 public boolean onMenuItemClick(MenuItem item) {
1607 openTab(extra, mTabControl.getCurrentTab(), true, true);
1608 return false;
1609 }
1610 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001611 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1612 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1613 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001614 menu.findItem(R.id.set_wallpaper_context_menu_id).
1615 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1616 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 break;
1618
1619 default:
1620 Log.w(LOGTAG, "We should not get here.");
1621 break;
1622 }
1623 //update the ui
1624 mUi.onContextMenuCreated(menu);
1625 }
1626
kaiyiz6e5b3e02013-08-19 20:02:01 +08001627 public void startAddMyNavigation(String url) {
1628 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1629 Bundle bundle = new Bundle();
1630 bundle.putBoolean("isAdding", true);
1631 bundle.putString("url", url);
1632 bundle.putString("name", getNameFromUrl(url));
1633 intent.putExtra("websites", bundle);
1634 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1635 }
1636
1637 private void showMyNavigationDeleteDialog(final String itemUrl) {
1638 new AlertDialog.Builder(this.getContext())
1639 .setTitle(R.string.my_navigation_delete_label)
1640 .setIcon(android.R.drawable.ic_dialog_alert)
1641 .setMessage(R.string.my_navigation_delete_msg)
1642 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1643 @Override
1644 public void onClick(DialogInterface dialog, int whichButton) {
1645 deleteMyNavigationItem(itemUrl);
1646 }
1647 })
1648 .setNegativeButton(R.string.cancel, null)
1649 .show();
1650 }
1651
1652 private void deleteMyNavigationItem(final String itemUrl) {
1653 ContentResolver cr = this.getContext().getContentResolver();
1654 Cursor cursor = null;
1655
1656 try {
1657 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1658 new String[] {
1659 MyNavigationUtil.ID
1660 }, "url = ?", new String[] {
1661 itemUrl
1662 }, null);
1663 if (null != cursor && cursor.moveToFirst()) {
1664 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1665 cursor.getLong(0));
1666
1667 ContentValues values = new ContentValues();
1668 values.put(MyNavigationUtil.TITLE, "");
1669 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1670 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1671 ByteArrayOutputStream os = new ByteArrayOutputStream();
1672 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1673 R.raw.my_navigation_add);
1674 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1675 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1676 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1677 cr.update(uri, values, null, null);
1678 } else {
1679 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1680 }
1681 } catch (IllegalStateException e) {
1682 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1683 } finally {
1684 if (null != cursor) {
1685 cursor.close();
1686 }
1687 }
1688
1689 if (getCurrentTopWebView() != null) {
1690 getCurrentTopWebView().reload();
1691 }
1692 }
1693
1694 private String getNameFromUrl(String itemUrl) {
1695 ContentResolver cr = this.getContext().getContentResolver();
1696 Cursor cursor = null;
1697 String name = null;
1698
1699 try {
1700 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1701 new String[] {
1702 MyNavigationUtil.TITLE
1703 }, "url = ?", new String[] {
1704 itemUrl
1705 }, null);
1706 if (null != cursor && cursor.moveToFirst()) {
1707 name = cursor.getString(0);
1708 } else {
1709 Log.e(LOGTAG, "this item does not exist!");
1710 }
1711 } catch (IllegalStateException e) {
1712 Log.e(LOGTAG, "getNameFromUrl", e);
1713 } finally {
1714 if (null != cursor) {
1715 cursor.close();
1716 }
1717 }
1718 return name;
1719 }
1720
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001721 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
1722 if (bitmap == null) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001723 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1724 return;
1725 }
1726
1727 final ContentResolver cr = mActivity.getContentResolver();
1728 new AsyncTask<Void, Void, Void>() {
1729 @Override
1730 protected Void doInBackground(Void... unused) {
1731 ContentResolver cr = mActivity.getContentResolver();
1732 Cursor cursor = null;
1733 try {
1734 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1735 new String[] {
1736 MyNavigationUtil.ID
1737 }, "url = ?", new String[] {
1738 itemUrl
1739 }, null);
1740 if (null != cursor && cursor.moveToFirst()) {
1741 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001742 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001743
1744 ContentValues values = new ContentValues();
1745 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1746 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1747 cursor.getLong(0));
1748 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1749 cr.update(uri, values, null, null);
1750 os.close();
1751 }
1752 } catch (IllegalStateException e) {
1753 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1754 } catch (IOException e) {
1755 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1756 } finally {
1757 if (null != cursor) {
1758 cursor.close();
1759 }
1760 }
1761 return null;
1762 }
1763 }.execute();
1764 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001765 /**
1766 * As the menu can be open when loading state changes
1767 * we must manually update the state of the stop/reload menu
1768 * item
1769 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001770 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001771 if (menu == null) {
1772 return;
1773 }
1774 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001775 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001776 menu.findItem(R.id.stop_menu_id):
1777 menu.findItem(R.id.reload_menu_id);
1778 if (src != null) {
1779 dest.setIcon(src.getIcon());
1780 dest.setTitle(src.getTitle());
1781 }
1782 }
1783
John Reck9c35b9c2012-05-30 10:08:50 -07001784 @Override
1785 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001786 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001787 // hold on to the menu reference here; it is used by the page callbacks
1788 // to update the menu based on loading state
1789 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001790 // Note: setVisible will decide whether an item is visible; while
1791 // setEnabled() will decide whether an item is enabled, which also means
1792 // whether the matching shortcut key will function.
1793 switch (mMenuState) {
1794 case EMPTY_MENU:
1795 if (mCurrentMenuState != mMenuState) {
1796 menu.setGroupVisible(R.id.MAIN_MENU, false);
1797 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1798 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1799 }
1800 break;
1801 default:
1802 if (mCurrentMenuState != mMenuState) {
1803 menu.setGroupVisible(R.id.MAIN_MENU, true);
1804 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1805 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1806 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001807 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001808 break;
1809 }
1810 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001811 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001812 }
1813
Michael Kolb4bf79712011-07-14 14:18:12 -07001814 @Override
1815 public void updateMenuState(Tab tab, Menu menu) {
1816 boolean canGoBack = false;
1817 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001818 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001819 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001820 // Following flag is used to identify schemes for which the LIVE_MENU
1821 // items defined in res/menu/browser.xml should be enabled
1822 boolean isLiveScheme = false;
1823 boolean isPageFinished = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001824 if (tab != null) {
1825 canGoBack = tab.canGoBack();
1826 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001827 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001828 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001829 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
1830 isPageFinished = tab.getPageFinishedStatus();
Michael Kolb4bf79712011-07-14 14:18:12 -07001831 }
1832 final MenuItem back = menu.findItem(R.id.back_menu_id);
1833 back.setEnabled(canGoBack);
1834
1835 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001836
1837 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1838 forward.setEnabled(canGoForward);
1839
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001840 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1841 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001842 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001843 if (source != null && dest != null) {
1844 dest.setTitle(source.getTitle());
1845 dest.setIcon(source.getIcon());
1846 }
John Recke1a03a32011-09-14 17:04:16 -07001847 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001848
1849 // decide whether to show the share link option
1850 PackageManager pm = mActivity.getPackageManager();
1851 Intent send = new Intent(Intent.ACTION_SEND);
1852 send.setType("text/plain");
1853 ResolveInfo ri = pm.resolveActivity(send,
1854 PackageManager.MATCH_DEFAULT_ONLY);
1855 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1856
1857 boolean isNavDump = mSettings.enableNavDump();
1858 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1859 nav.setVisible(isNavDump);
1860 nav.setEnabled(isNavDump);
1861
1862 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001863 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1864 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001865 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001866 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001867 menu.setGroupEnabled(R.id.OFFLINE_READING, isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001868 // history and snapshots item are the members of COMBO menu group,
1869 // so if show history item, only make snapshots item invisible.
1870 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001871
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001872 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001873 }
1874
John Reck9c35b9c2012-05-30 10:08:50 -07001875 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001876 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001877 if (null == getCurrentTopWebView()) {
1878 return false;
1879 }
1880 if (mMenuIsDown) {
1881 // The shortcut action consumes the MENU. Even if it is still down,
1882 // it won't trigger the next shortcut action. In the case of the
1883 // shortcut action triggering a new activity, like Bookmarks, we
1884 // won't get onKeyUp for MENU. So it is important to reset it here.
1885 mMenuIsDown = false;
1886 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001887 if (mUi.onOptionsItemSelected(item)) {
1888 // ui callback handled it
1889 return true;
1890 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001891 switch (item.getItemId()) {
1892 // -- Main menu
1893 case R.id.new_tab_menu_id:
1894 openTabToHomePage();
1895 break;
1896
1897 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001898 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001899 break;
1900
Afzal Najamd4e33312012-04-26 01:54:01 -04001901 case R.id.close_other_tabs_id:
1902 closeOtherTabs();
1903 break;
1904
Michael Kolb8233fac2010-10-26 16:08:53 -07001905 case R.id.goto_menu_id:
1906 editUrl();
1907 break;
1908
1909 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001910 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1911 break;
1912
1913 case R.id.history_menu_id:
1914 bookmarksOrHistoryPicker(ComboViews.History);
1915 break;
1916
1917 case R.id.snapshots_menu_id:
1918 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001919 break;
1920
Michael Kolb8233fac2010-10-26 16:08:53 -07001921 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001922 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001923 break;
1924
1925 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001926 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001927 stopLoading();
1928 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001929 Tab currentTab = mTabControl.getCurrentTab();
1930 if (currentTab.hasCrashed) {
1931 currentTab.replaceCrashView(getCurrentTopWebView(),
1932 currentTab.getViewContainer());
1933 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001934 getCurrentTopWebView().reload();
1935 }
1936 break;
1937
1938 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001939 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001940 break;
1941
1942 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001943 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001944 break;
1945
1946 case R.id.close_menu_id:
1947 // Close the subwindow if it exists.
1948 if (mTabControl.getCurrentSubWindow() != null) {
1949 dismissSubWindow(mTabControl.getCurrentTab());
1950 break;
1951 }
1952 closeCurrentTab();
1953 break;
1954
kaiyiza8b6dbb2013-07-29 18:11:22 +08001955 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001956 Object[] params = { new String("persist.debug.browsermonkeytest")};
1957 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001958 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001959 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001960 if (ret != null && ret.equals("enable"))
1961 break;
luxiaolb40014b2013-07-19 10:01:43 +08001962 showExitDialog(mActivity);
1963 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001964 case R.id.homepage_menu_id:
1965 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001966 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 break;
1968
1969 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001970 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001971 break;
1972
1973 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001974 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001975 break;
1976
John Reck2bc80422011-06-30 15:11:49 -07001977 case R.id.save_snapshot_menu_id:
1978 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001979 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001980 createScreenshotAsync(
1981 source.getWebView(),
1982 getDesiredThumbnailWidth(mActivity),
1983 getDesiredThumbnailHeight(mActivity),
1984 new ValueCallback<Bitmap>() {
1985 @Override
1986 public void onReceiveValue(Bitmap bitmap) {
1987 new SaveSnapshotTask(source, bitmap).execute();
1988 }
1989 });
Leon Scrogginsac993842011-02-02 12:54:07 -05001990 break;
1991
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001993 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 break;
1995
John Recke1a03a32011-09-14 17:04:16 -07001996 case R.id.snapshot_go_live:
1997 goLive();
1998 return true;
1999
Michael Kolb8233fac2010-10-26 16:08:53 -07002000 case R.id.share_page_menu_id:
2001 Tab currentTab = mTabControl.getCurrentTab();
2002 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002003 return false;
2004 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002005 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002006 break;
2007
2008 case R.id.dump_nav_menu_id:
2009 getCurrentTopWebView().debugDump();
2010 break;
2011
Michael Kolb8233fac2010-10-26 16:08:53 -07002012 case R.id.zoom_in_menu_id:
2013 getCurrentTopWebView().zoomIn();
2014 break;
2015
2016 case R.id.zoom_out_menu_id:
2017 getCurrentTopWebView().zoomOut();
2018 break;
2019
2020 case R.id.view_downloads_menu_id:
2021 viewDownloads();
2022 break;
2023
John Reck42229bc2011-08-19 13:26:43 -07002024 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002025 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002026 break;
2027
Michael Kolb8233fac2010-10-26 16:08:53 -07002028 case R.id.window_one_menu_id:
2029 case R.id.window_two_menu_id:
2030 case R.id.window_three_menu_id:
2031 case R.id.window_four_menu_id:
2032 case R.id.window_five_menu_id:
2033 case R.id.window_six_menu_id:
2034 case R.id.window_seven_menu_id:
2035 case R.id.window_eight_menu_id:
2036 {
2037 int menuid = item.getItemId();
2038 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2039 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2040 Tab desiredTab = mTabControl.getTab(id);
2041 if (desiredTab != null &&
2042 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002043 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002044 }
2045 break;
2046 }
2047 }
2048 }
2049 break;
2050
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002051 case R.id.about_menu_id:
2052 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2053 builder.setTitle(R.string.about);
2054 builder.setCancelable(true);
2055 String ua = "";
2056 final WebView currentWebView = getCurrentWebView();
2057 if (currentWebView != null) {
2058 final WebSettings s = currentWebView.getSettings();
2059 if (s != null) {
2060 ua = s.getUserAgentString();
2061 }
2062 }
2063 builder.setMessage("Agent:" + ua);
2064 builder.setPositiveButton(android.R.string.ok, null);
2065 builder.create().show();
2066 break;
2067
Michael Kolb8233fac2010-10-26 16:08:53 -07002068 default:
2069 return false;
2070 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002071 return true;
2072 }
2073
John Reck68234a92012-04-19 15:27:12 -07002074 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2075 implements OnCancelListener {
2076
2077 private Tab mTab;
2078 private Dialog mProgressDialog;
2079 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002080 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002081
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002082 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002083 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002084 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002085 }
2086
2087 @Override
2088 protected void onPreExecute() {
2089 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2090 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2091 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002092 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002093 }
2094
2095 @Override
2096 protected Long doInBackground(Void... params) {
2097 if (!mTab.saveViewState(mValues)) {
2098 return null;
2099 }
2100 if (isCancelled()) {
2101 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2102 File file = mActivity.getFileStreamPath(path);
2103 if (!file.delete()) {
2104 file.deleteOnExit();
2105 }
2106 return null;
2107 }
2108 final ContentResolver cr = mActivity.getContentResolver();
2109 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2110 if (result == null) {
2111 return null;
2112 }
2113 long id = ContentUris.parseId(result);
2114 return id;
2115 }
2116
2117 @Override
2118 protected void onPostExecute(Long id) {
2119 if (isCancelled()) {
2120 return;
2121 }
2122 mProgressDialog.dismiss();
2123 if (id == null) {
2124 Toast.makeText(mActivity, R.string.snapshot_failed,
2125 Toast.LENGTH_SHORT).show();
2126 return;
2127 }
2128 Bundle b = new Bundle();
2129 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2130 mUi.showComboView(ComboViews.Snapshots, b);
2131 }
2132
2133 @Override
2134 public void onCancel(DialogInterface dialog) {
2135 cancel(true);
2136 }
2137 }
2138
Michael Kolb80f75082012-04-10 10:50:06 -07002139 @Override
2140 public void toggleUserAgent() {
2141 WebView web = getCurrentWebView();
2142 mSettings.toggleDesktopUseragent(web);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002143 web.reload();
Michael Kolb80f75082012-04-10 10:50:06 -07002144 }
2145
2146 @Override
2147 public void findOnPage() {
2148 getCurrentTopWebView().showFindDialog(null, true);
2149 }
2150
2151 @Override
2152 public void openPreferences() {
2153 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2154 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2155 getCurrentTopWebView().getUrl());
2156 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2157 }
2158
2159 @Override
2160 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002161 WebView w = getCurrentTopWebView();
2162 if (w == null)
2163 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002164 final Intent i = createBookmarkCurrentPageIntent(false);
2165 createScreenshotAsync(
2166 w, getDesiredThumbnailWidth(mActivity),
2167 getDesiredThumbnailHeight(mActivity),
2168 new ValueCallback<Bitmap>() {
2169 @Override
2170 public void onReceiveValue(Bitmap bitmap) {
2171 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2172 mActivity.startActivity(i);
2173 }
2174 });
Michael Kolb80f75082012-04-10 10:50:06 -07002175 }
2176
John Recke1a03a32011-09-14 17:04:16 -07002177 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002178 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002179 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002180 boolean onlySingleTabRemaining = false;
2181 if (mTabControl.getTabCount() > 1) {
2182 // destroy the old snapshot tab
2183 closeCurrentTab();
2184 } else {
2185 onlySingleTabRemaining = true;
2186 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002187 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002188 if (onlySingleTabRemaining) {
2189 closeTab(t);
2190 }
2191
Tarun Nainani8eb00912014-07-17 12:28:32 -07002192 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002193 }
2194
luxiaolb40014b2013-07-19 10:01:43 +08002195 private void showExitDialog(final Activity activity) {
2196 new AlertDialog.Builder(activity)
2197 .setTitle(R.string.exit_browser_title)
2198 .setIcon(android.R.drawable.ic_dialog_alert)
2199 .setMessage(R.string.exit_browser_msg)
2200 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2201 public void onClick(DialogInterface dialog, int which) {
2202 activity.moveTaskToBack(true);
2203 dialog.dismiss();
2204 }
2205 })
2206 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2207 public void onClick(DialogInterface dialog, int which) {
2208 activity.finish();
2209 mHandler.postDelayed(new Runnable() {
2210 @Override
2211 public void run() {
2212 // TODO Auto-generated method stub
2213 mCrashRecoveryHandler.clearState(true);
2214 int pid = android.os.Process.myPid();
2215 android.os.Process.killProcess(pid);
2216 }
2217 }, 300);
2218 dialog.dismiss();
2219 }
2220 })
2221 .show();
2222 }
Michael Kolb315d5022011-10-13 12:47:11 -07002223 @Override
2224 public void showPageInfo() {
2225 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2226 }
2227
John Reck9c35b9c2012-05-30 10:08:50 -07002228 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002229 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002230 // Let the History and Bookmark fragments handle menus they created.
2231 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2232 return false;
2233 }
2234
Michael Kolb8233fac2010-10-26 16:08:53 -07002235 int id = item.getItemId();
2236 boolean result = true;
2237 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002238 // -- Browser context menu
2239 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002240 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002241 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002242 case R.id.copy_link_context_menu_id:
2243 final WebView webView = getCurrentTopWebView();
2244 if (null == webView) {
2245 result = false;
2246 break;
2247 }
2248 final HashMap<String, WebView> hrefMap =
2249 new HashMap<String, WebView>();
2250 hrefMap.put("webview", webView);
2251 final Message msg = mHandler.obtainMessage(
2252 FOCUS_NODE_HREF, id, 0, hrefMap);
2253 webView.requestFocusNodeHref(msg);
2254 break;
2255
2256 default:
2257 // For other context menus
2258 result = onOptionsItemSelected(item);
2259 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002260 return result;
2261 }
2262
2263 /**
2264 * support programmatically opening the context menu
2265 */
2266 public void openContextMenu(View view) {
2267 mActivity.openContextMenu(view);
2268 }
2269
2270 /**
2271 * programmatically open the options menu
2272 */
2273 public void openOptionsMenu() {
2274 mActivity.openOptionsMenu();
2275 }
2276
John Reck9c35b9c2012-05-30 10:08:50 -07002277 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002278 public boolean onMenuOpened(int featureId, Menu menu) {
2279 if (mOptionsMenuOpen) {
2280 if (mConfigChanged) {
2281 // We do not need to make any changes to the state of the
2282 // title bar, since the only thing that happened was a
2283 // change in orientation
2284 mConfigChanged = false;
2285 } else {
2286 if (!mExtendedMenuOpen) {
2287 mExtendedMenuOpen = true;
2288 mUi.onExtendedMenuOpened();
2289 } else {
2290 // Switching the menu back to icon view, so show the
2291 // title bar once again.
2292 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002293 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002294 }
2295 }
2296 } else {
2297 // The options menu is closed, so open it, and show the title
2298 mOptionsMenuOpen = true;
2299 mConfigChanged = false;
2300 mExtendedMenuOpen = false;
2301 mUi.onOptionsMenuOpened();
2302 }
2303 return true;
2304 }
2305
John Reck9c35b9c2012-05-30 10:08:50 -07002306 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002307 public void onOptionsMenuClosed(Menu menu) {
2308 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002309 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002310 }
2311
John Reck9c35b9c2012-05-30 10:08:50 -07002312 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002313 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002314 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002315 }
2316
2317 // Helper method for getting the top window.
2318 @Override
2319 public WebView getCurrentTopWebView() {
2320 return mTabControl.getCurrentTopWebView();
2321 }
2322
2323 @Override
2324 public WebView getCurrentWebView() {
2325 return mTabControl.getCurrentWebView();
2326 }
2327
2328 /*
2329 * This method is called as a result of the user selecting the options
2330 * menu to see the download window. It shows the download window on top of
2331 * the current window.
2332 */
2333 void viewDownloads() {
2334 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2335 mActivity.startActivity(intent);
2336 }
2337
John Reck30b065e2011-07-19 10:58:05 -07002338 int getActionModeHeight() {
2339 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2340 new int[] { android.R.attr.actionBarSize });
2341 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2342 actionBarSizeTypedArray.recycle();
2343 return size;
2344 }
2345
Michael Kolb8233fac2010-10-26 16:08:53 -07002346 // action mode
2347
John Reck9c35b9c2012-05-30 10:08:50 -07002348 @Override
2349 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002350 mUi.onActionModeStarted(mode);
2351 mActionMode = mode;
2352 }
2353
2354 /*
2355 * True if a custom ActionMode (i.e. find or select) is in use.
2356 */
2357 @Override
2358 public boolean isInCustomActionMode() {
2359 return mActionMode != null;
2360 }
2361
2362 /*
2363 * End the current ActionMode.
2364 */
2365 @Override
2366 public void endActionMode() {
2367 if (mActionMode != null) {
2368 mActionMode.finish();
2369 }
2370 }
2371
2372 /*
2373 * Called by find and select when they are finished. Replace title bars
2374 * as necessary.
2375 */
John Reck9c35b9c2012-05-30 10:08:50 -07002376 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002377 public void onActionModeFinished(ActionMode mode) {
2378 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002379 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002380 mActionMode = null;
2381 }
2382
2383 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002384 final Tab tab = getCurrentTab();
2385 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002386 }
2387
2388 // bookmark handling
2389
2390 /**
2391 * add the current page as a bookmark to the given folder id
2392 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002393 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002394 * bookmarked, and if it is, edit that bookmark. If false, and
2395 * the site is already bookmarked, do not attempt to edit the
2396 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002397 */
2398 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002399 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002400 WebView w = getCurrentTopWebView();
2401 if (w == null) {
2402 return null;
2403 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002404 Intent i = new Intent(mActivity,
2405 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002406 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2407 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2408 String touchIconUrl = w.getTouchIconUrl();
2409 if (touchIconUrl != null) {
2410 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2411 WebSettings settings = w.getSettings();
2412 if (settings != null) {
2413 i.putExtra(AddBookmarkPage.USER_AGENT,
2414 settings.getUserAgentString());
2415 }
2416 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002417 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002418 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002419 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002420 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2421 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 // Put the dialog at the upper right of the screen, covering the
2423 // star on the title bar.
2424 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002425 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002426 }
2427
2428 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002429 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002430 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002431 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002432 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 }
2434
Vivek Sekharb54614f2014-05-01 19:03:37 -07002435 @Override
2436 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2437 boolean capture) {
2438 mUploadHandler = new UploadHandler(this);
2439 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2440 }
2441
Michael Kolb8233fac2010-10-26 16:08:53 -07002442 // thumbnails
2443
2444 /**
2445 * Return the desired width for thumbnail screenshots, which are stored in
2446 * the database, and used on the bookmarks screen.
2447 * @param context Context for finding out the density of the screen.
2448 * @return desired width for thumbnail screenshot.
2449 */
2450 static int getDesiredThumbnailWidth(Context context) {
2451 return context.getResources().getDimensionPixelOffset(
2452 R.dimen.bookmarkThumbnailWidth);
2453 }
2454
2455 /**
2456 * Return the desired height for thumbnail screenshots, which are stored in
2457 * the database, and used on the bookmarks screen.
2458 * @param context Context for finding out the density of the screen.
2459 * @return desired height for thumbnail screenshot.
2460 */
2461 static int getDesiredThumbnailHeight(Context context) {
2462 return context.getResources().getDimensionPixelOffset(
2463 R.dimen.bookmarkThumbnailHeight);
2464 }
2465
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002466 static void createScreenshotAsync(WebView view, int width, int height,
2467 final ValueCallback<Bitmap> cb) {
2468 if (view == null || width == 0 || height == 0) {
2469 return;
2470 }
2471
2472 view.getContentBitmapAsync(
2473 (float) width / view.getWidth(),
2474 new Rect(),
2475 new ValueCallback<Bitmap>() {
2476 @Override
2477 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002478 if (bitmap != null)
2479 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2480 cb.onReceiveValue(bitmap);
2481 }});
2482 }
2483
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002484 private void updateScreenshot(final Tab tab) {
2485 createScreenshotAsync(
2486 tab.getWebView(),
2487 getDesiredThumbnailWidth(mActivity),
2488 getDesiredThumbnailHeight(mActivity),
2489 new ValueCallback<Bitmap>() {
2490 @Override
2491 public void onReceiveValue(Bitmap bitmap) {
2492 updateScreenshot(tab, bitmap);
2493 }
2494 });
2495 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002496
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002497 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002498 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002499 // FIXME: Would like to make sure there is actually something to
2500 // draw, but the API for that (WebViewCore.pictureReady()) is not
2501 // currently accessible here.
2502
John Reck34ef2672011-02-10 11:30:55 -08002503 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002504 if (view == null) {
2505 // Tab was destroyed
2506 return;
2507 }
John Reck34ef2672011-02-10 11:30:55 -08002508 final String url = tab.getUrl();
2509 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002510 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002511 if (TextUtils.isEmpty(url)) {
2512 return;
2513 }
2514
kaiyiz6e5b3e02013-08-19 20:02:01 +08002515 //update My Navigation Thumbnails
2516 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2517 if (isMyNavigationUrl) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002518 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002519 }
John Reck34ef2672011-02-10 11:30:55 -08002520 // Only update thumbnails for web urls (http(s)://), not for
2521 // about:, javascript:, data:, etc...
2522 // Unless it is a bookmarked site, then always update
2523 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2524 return;
2525 }
2526
kaiyiz6e5b3e02013-08-19 20:02:01 +08002527 if (url != null && mUpdateMyNavThumbnailUrl != null
2528 && Patterns.WEB_URL.matcher(url).matches()
2529 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2530 String urlHost = (new WebAddress(url)).getHost();
2531 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2532 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2533 || bookmarkHost.length() == 0) {
2534 return;
2535 }
2536 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2537 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2538 bookmarkHost.length());
2539 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2540 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2541 if (!bookmarkDomain.equals(urlDomain)) {
2542 return;
2543 }
2544 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002545 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002546 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2547 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2548 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2549 500);
2550 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002551 return;
2552 }
2553
2554 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002555 new AsyncTask<Void, Void, Void>() {
2556 @Override
2557 protected Void doInBackground(Void... unused) {
2558 Cursor cursor = null;
2559 try {
2560 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002561 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2562 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2563 : url);
2564 if (mUpdateMyNavThumbnail) {
2565 mUpdateMyNavThumbnail = false;
2566 mUpdateMyNavThumbnailUrl = null;
2567 }
John Reck34ef2672011-02-10 11:30:55 -08002568 if (cursor != null && cursor.moveToFirst()) {
2569 final ByteArrayOutputStream os =
2570 new ByteArrayOutputStream();
2571 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002572
John Reck34ef2672011-02-10 11:30:55 -08002573 ContentValues values = new ContentValues();
2574 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002575
John Reck34ef2672011-02-10 11:30:55 -08002576 do {
John Reck617fd832011-02-16 14:35:59 -08002577 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002578 cr.update(Images.CONTENT_URI, values, null, null);
2579 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 }
John Reck34ef2672011-02-10 11:30:55 -08002581 } catch (IllegalStateException e) {
2582 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002583 } catch (SQLiteException s) {
2584 // Added for possible error when user tries to remove the same bookmark
2585 // that is being updated with a screen shot
2586 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002587 } finally {
2588 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002589 }
John Reck34ef2672011-02-10 11:30:55 -08002590 return null;
2591 }
2592 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002593 }
2594
2595 private class Copy implements OnMenuItemClickListener {
2596 private CharSequence mText;
2597
John Reck9c35b9c2012-05-30 10:08:50 -07002598 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 public boolean onMenuItemClick(MenuItem item) {
2600 copy(mText);
2601 return true;
2602 }
2603
2604 public Copy(CharSequence toCopy) {
2605 mText = toCopy;
2606 }
2607 }
2608
Leon Scroggins63c02662010-11-18 15:16:27 -05002609 private static class Download implements OnMenuItemClickListener {
2610 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002611 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002612 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002613 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002614 private static final String FALLBACK_EXTENSION = "dat";
2615 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002616
John Reck9c35b9c2012-05-30 10:08:50 -07002617 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002618 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002619 if (DataUri.isDataUri(mText)) {
2620 saveDataUri();
2621 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002622 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002623 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002624 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002625 return true;
2626 }
2627
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002628 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2629 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002630 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002631 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002632 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002633 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002634 }
George Mount387d45d2011-10-07 15:57:53 -07002635
2636 /**
2637 * Treats mText as a data URI and writes its contents to a file
2638 * based on the current time.
2639 */
2640 private void saveDataUri() {
2641 FileOutputStream outputStream = null;
2642 try {
2643 DataUri uri = new DataUri(mText);
2644 File target = getTarget(uri);
2645 outputStream = new FileOutputStream(target);
2646 outputStream.write(uri.getData());
2647 final DownloadManager manager =
2648 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2649 manager.addCompletedDownload(target.getName(),
2650 mActivity.getTitle().toString(), false,
2651 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002652 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002653 } catch (IOException e) {
2654 Log.e(LOGTAG, "Could not save data URL");
2655 } finally {
2656 if (outputStream != null) {
2657 try {
2658 outputStream.close();
2659 } catch (IOException e) {
2660 // ignore close errors
2661 }
2662 }
2663 }
2664 }
2665
2666 /**
2667 * Creates a File based on the current time stamp and uses
2668 * the mime type of the DataUri to get the extension.
2669 */
2670 private File getTarget(DataUri uri) throws IOException {
2671 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002672 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002673 String nameBase = format.format(new Date());
2674 String mimeType = uri.getMimeType();
2675 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2676 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2677 if (extension == null) {
2678 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2679 extension = FALLBACK_EXTENSION;
2680 }
2681 extension = "." + extension; // createTempFile needs the '.'
2682 File targetFile = File.createTempFile(nameBase, extension, dir);
2683 return targetFile;
2684 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002685 }
2686
Cary Clark8974d282010-11-22 10:46:05 -05002687 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002688 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002689
John Reck9c35b9c2012-05-30 10:08:50 -07002690 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002691 public boolean onMenuItemClick(MenuItem item) {
2692 if (mWebView != null) {
2693 return mWebView.selectText();
2694 }
2695 return false;
2696 }
2697
2698 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002699 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002700 }
2701
2702 }
2703
Michael Kolb8233fac2010-10-26 16:08:53 -07002704 /********************** TODO: UI stuff *****************************/
2705
2706 // these methods have been copied, they still need to be cleaned up
2707
2708 /****************** tabs ***************************************************/
2709
2710 // basic tab interactions:
2711
2712 // it is assumed that tabcontrol already knows about the tab
2713 protected void addTab(Tab tab) {
2714 mUi.addTab(tab);
2715 }
2716
2717 protected void removeTab(Tab tab) {
2718 mUi.removeTab(tab);
2719 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002720 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002721 }
2722
Michael Kolbf2055602011-04-09 17:20:03 -07002723 @Override
2724 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002725 // monkey protection against delayed start
2726 if (tab != null) {
2727 mTabControl.setCurrentTab(tab);
2728 // the tab is guaranteed to have a webview after setCurrentTab
2729 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002730 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002731 //Purge active tabs
2732 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002733 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002734 }
2735
John Reck8bcafc12011-08-29 16:43:02 -07002736 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 Tab current = mTabControl.getCurrentTab();
2738 if (current != null
2739 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002740 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002741 }
2742 }
2743
John Reck26b18322011-06-21 13:08:58 -07002744 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002745 // Dismiss the subwindow if applicable.
2746 dismissSubWindow(appTab);
2747 // Since we might kill the WebView, remove it from the
2748 // content view first.
2749 mUi.detachTab(appTab);
2750 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002751 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002752 // TODO: analyze why the remove and add are necessary
2753 mUi.attachTab(appTab);
2754 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002755 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002756 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002757 } else {
2758 // If the tab was the current tab, we have to attach
2759 // it to the view system again.
2760 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002761 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002762 }
2763 }
2764
2765 // Remove the sub window if it exists. Also called by TabControl when the
2766 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002767 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002768 public void dismissSubWindow(Tab tab) {
2769 removeSubWindow(tab);
2770 // dismiss the subwindow. This will destroy the WebView.
2771 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002772 WebView wv = getCurrentTopWebView();
2773 if (wv != null) {
2774 wv.requestFocus();
2775 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002776 }
2777
2778 @Override
2779 public void removeSubWindow(Tab t) {
2780 if (t.getSubWebView() != null) {
2781 mUi.removeSubWindow(t.getSubViewContainer());
2782 }
2783 }
2784
2785 @Override
2786 public void attachSubWindow(Tab tab) {
2787 if (tab.getSubWebView() != null) {
2788 mUi.attachSubWindow(tab.getSubViewContainer());
2789 getCurrentTopWebView().requestFocus();
2790 }
2791 }
2792
Mathew Inwood29721c22011-06-29 17:55:24 +01002793 private Tab showPreloadedTab(final UrlData urlData) {
2794 if (!urlData.isPreloaded()) {
2795 return null;
2796 }
2797 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2798 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2799 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002800 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002801 // Could not submit query. Fallback to regular tab creation
2802 tabControl.destroy();
2803 return null;
2804 }
2805 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002806 // check tab count and make room for new tab
2807 if (!mTabControl.canCreateNewTab()) {
2808 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2809 if (leastUsed != null) {
2810 closeTab(leastUsed);
2811 }
2812 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002813 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002814 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002815 mTabControl.addPreloadedTab(t);
2816 addTab(t);
2817 setActiveTab(t);
2818 return t;
2819 }
2820
Michael Kolb7bcafde2011-05-09 13:55:59 -07002821 // open a non inconito tab with the given url data
2822 // and set as active tab
2823 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002824 Tab tab = showPreloadedTab(urlData);
2825 if (tab == null) {
2826 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002827 if ((tab != null) && !urlData.isEmpty()) {
2828 loadUrlDataIn(tab, urlData);
2829 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002830 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002831 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002832 }
2833
Michael Kolb843510f2010-12-09 10:51:49 -08002834 @Override
2835 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002836 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002837 }
2838
Michael Kolb8233fac2010-10-26 16:08:53 -07002839 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002840 public Tab openIncognitoTab() {
2841 return openTab(INCOGNITO_URI, true, true, false);
2842 }
2843
2844 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002845 public Tab openTab(String url, boolean incognito, boolean setActive,
2846 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002847 return openTab(url, incognito, setActive, useCurrent, null);
2848 }
2849
2850 @Override
2851 public Tab openTab(String url, Tab parent, boolean setActive,
2852 boolean useCurrent) {
2853 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2854 setActive, useCurrent, parent);
2855 }
2856
2857 public Tab openTab(String url, boolean incognito, boolean setActive,
2858 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002859 Tab tab = createNewTab(incognito, setActive, useCurrent);
2860 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002861 if (parent instanceof SnapshotTab) {
2862 addTab(tab);
2863 if (setActive)
2864 setActiveTab(tab);
2865 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002866 parent.addChildTab(tab);
2867 }
Michael Kolb519d2282011-05-09 17:03:19 -07002868 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002869 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002870 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002871 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002872 return tab;
2873 }
2874
2875 // this method will attempt to create a new tab
2876 // incognito: private browsing tab
2877 // setActive: ste tab as current tab
2878 // useCurrent: if no new tab can be created, return current tab
2879 private Tab createNewTab(boolean incognito, boolean setActive,
2880 boolean useCurrent) {
2881 Tab tab = null;
2882 if (mTabControl.canCreateNewTab()) {
2883 tab = mTabControl.createNewTab(incognito);
2884 addTab(tab);
2885 if (setActive) {
2886 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002887 } else {
2888 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002889 }
2890 } else {
2891 if (useCurrent) {
2892 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002893 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002894 } else {
2895 mUi.showMaxTabsWarning();
2896 }
2897 }
2898 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002899 }
2900
John Reck2bc80422011-06-30 15:11:49 -07002901 @Override
2902 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2903 SnapshotTab tab = null;
2904 if (mTabControl.canCreateNewTab()) {
2905 tab = mTabControl.createSnapshotTab(snapshotId);
2906 addTab(tab);
2907 if (setActive) {
2908 setActiveTab(tab);
2909 }
2910 } else {
2911 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002912 }
2913 return tab;
2914 }
2915
Michael Kolb8233fac2010-10-26 16:08:53 -07002916 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002917 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002918 * @return boolean True if we successfully switched to a different tab. If
2919 * the indexth tab is null, or if that tab is the same as
2920 * the current one, return false.
2921 */
2922 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002923 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002924 Tab currentTab = mTabControl.getCurrentTab();
2925 if (tab == null || tab == currentTab) {
2926 return false;
2927 }
2928 setActiveTab(tab);
2929 return true;
2930 }
2931
2932 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002933 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002934 closeCurrentTab(false);
2935 }
2936
2937 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002938 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002939 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002940 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002941 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002942 return;
2943 }
Michael Kolbc831b632011-05-11 09:30:34 -07002944 final Tab current = mTabControl.getCurrentTab();
2945 final int pos = mTabControl.getCurrentPosition();
2946 Tab newTab = current.getParent();
2947 if (newTab == null) {
2948 newTab = mTabControl.getTab(pos + 1);
2949 if (newTab == null) {
2950 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002951 }
2952 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002953 if (andQuit) {
2954 mTabControl.setCurrentTab(newTab);
2955 closeTab(current);
2956 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002957 // Close window
2958 closeTab(current);
2959 }
2960 }
2961
2962 /**
2963 * Close the tab, remove its associated title bar, and adjust mTabControl's
2964 * current tab to a valid value.
2965 */
2966 @Override
2967 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002968 if (tab == mTabControl.getCurrentTab()) {
2969 closeCurrentTab();
2970 } else {
2971 removeTab(tab);
2972 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002973 }
2974
Afzal Najamd4e33312012-04-26 01:54:01 -04002975 /**
2976 * Close all tabs except the current one
2977 */
2978 @Override
2979 public void closeOtherTabs() {
2980 int inactiveTabs = mTabControl.getTabCount() - 1;
2981 for (int i = inactiveTabs; i >= 0; i--) {
2982 Tab tab = mTabControl.getTab(i);
2983 if (tab != mTabControl.getCurrentTab()) {
2984 removeTab(tab);
2985 }
2986 }
2987 }
2988
Michael Kolb8233fac2010-10-26 16:08:53 -07002989 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002990 protected void loadUrlFromContext(String url) {
2991 Tab tab = getCurrentTab();
2992 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002993 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002994 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002995 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002996 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002997 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002998 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002999 }
3000 }
3001 }
3002
3003 /**
3004 * Load the URL into the given WebView and update the title bar
3005 * to reflect the new load. Call this instead of WebView.loadUrl
3006 * directly.
3007 * @param view The WebView used to load url.
3008 * @param url The URL to load.
3009 */
John Reck71e51422011-07-01 16:49:28 -07003010 @Override
3011 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003012 loadUrl(tab, url, null);
3013 }
3014
3015 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3016 if (tab != null) {
3017 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003018 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003019 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003020 if (tab.hasCrashed) {
3021 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3022 }
Michael Kolba53c9892011-10-05 13:31:40 -07003023 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003024 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003025 }
3026
3027 /**
3028 * Load UrlData into a Tab and update the title bar to reflect the new
3029 * load. Call this instead of UrlData.loadIn directly.
3030 * @param t The Tab used to load.
3031 * @param data The UrlData being loaded.
3032 */
3033 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003034 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003035 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003036 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003037 } else {
John Reck38b39652012-06-05 09:22:59 -07003038 if (t != null && data.mDisableUrlOverride) {
3039 t.disableUrlOverridingForLoad();
3040 }
John Reck26b18322011-06-21 13:08:58 -07003041 loadUrl(t, data.mUrl, data.mHeaders);
3042 }
3043 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003044 }
3045
John Reck30c714c2010-12-16 17:30:34 -08003046 @Override
3047 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003048 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003049 if (tab.canGoBack()) {
3050 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003051 } else {
John Reckef654f12011-07-12 16:42:08 -07003052 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003053 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003054 }
3055
3056 void goBackOnePageOrQuit() {
3057 Tab current = mTabControl.getCurrentTab();
3058 if (current == null) {
3059 /*
3060 * Instead of finishing the activity, simply push this to the back
3061 * of the stack and let ActivityManager to choose the foreground
3062 * activity. As BrowserActivity is singleTask, it will be always the
3063 * root of the task. So we can use either true or false for
3064 * moveTaskToBack().
3065 */
luxiaolb40014b2013-07-19 10:01:43 +08003066 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003067 return;
3068 }
John Reckef654f12011-07-12 16:42:08 -07003069 if (current.canGoBack()) {
3070 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003071 } else {
3072 // Check to see if we are closing a window that was created by
3073 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003074 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003075 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003076 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003077 // Now we close the other tab
3078 closeTab(current);
3079 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003080 /*
3081 * Instead of finishing the activity, simply push this to the back
3082 * of the stack and let ActivityManager to choose the foreground
3083 * activity. As BrowserActivity is singleTask, it will be always the
3084 * root of the task. So we can use either true or false for
3085 * moveTaskToBack().
3086 */
luxiaolb40014b2013-07-19 10:01:43 +08003087 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003088 }
3089 }
3090 }
3091
3092 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003093 * helper method for key handler
3094 * returns the current tab if it can't advance
3095 */
Michael Kolbc831b632011-05-11 09:30:34 -07003096 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003097 int pos = mTabControl.getCurrentPosition() + 1;
3098 if (pos >= mTabControl.getTabCount()) {
3099 pos = 0;
3100 }
3101 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003102 }
3103
3104 /**
3105 * helper method for key handler
3106 * returns the current tab if it can't advance
3107 */
Michael Kolbc831b632011-05-11 09:30:34 -07003108 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003109 int pos = mTabControl.getCurrentPosition() - 1;
3110 if ( pos < 0) {
3111 pos = mTabControl.getTabCount() - 1;
3112 }
3113 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003114 }
3115
John Reckbcef87f2012-02-03 14:58:34 -08003116 boolean isMenuOrCtrlKey(int keyCode) {
3117 return (KeyEvent.KEYCODE_MENU == keyCode)
3118 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3119 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3120 }
3121
Michael Kolb0035fad2011-03-14 13:25:28 -07003122 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003123 * handle key events in browser
3124 *
3125 * @param keyCode
3126 * @param event
3127 * @return true if handled, false to pass to super
3128 */
John Reck9c35b9c2012-05-30 10:08:50 -07003129 @Override
3130 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003131 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003132 // Even if MENU is already held down, we need to call to super to open
3133 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003134 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003135 mMenuIsDown = true;
3136 return false;
3137 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003138
Cary Clark8ff8c662010-12-29 15:03:05 -05003139 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003140 Tab tab = getCurrentTab();
3141 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003142
Cary Clark160bbb92011-01-10 11:17:07 -05003143 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3144 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003145
Michael Kolb8233fac2010-10-26 16:08:53 -07003146 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003147 case KeyEvent.KEYCODE_TAB:
3148 if (event.isCtrlPressed()) {
3149 if (event.isShiftPressed()) {
3150 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003151 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003152 } else {
3153 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003154 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003155 }
3156 return true;
3157 }
3158 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003159 case KeyEvent.KEYCODE_SPACE:
3160 // WebView/WebTextView handle the keys in the KeyDown. As
3161 // the Activity's shortcut keys are only handled when WebView
3162 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003163 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003164 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003165 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003166 pageDown();
3167 }
3168 return true;
3169 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003170 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003171 event.startTracking();
3172 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003173 case KeyEvent.KEYCODE_FORWARD:
3174 if (!noModifiers) break;
3175 tab.goForward();
3176 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003177 case KeyEvent.KEYCODE_DPAD_LEFT:
3178 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003179 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003180 return true;
3181 }
3182 break;
3183 case KeyEvent.KEYCODE_DPAD_RIGHT:
3184 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003185 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003186 return true;
3187 }
3188 break;
3189 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003190 if (ctrl) {
3191 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003192 return true;
3193 }
3194 break;
Michael Kolba4183062011-01-16 10:43:21 -08003195// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003196 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003197 if (ctrl ) {
3198 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003199 return true;
3200 }
3201 break;
Michael Kolba4183062011-01-16 10:43:21 -08003202// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003203// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003204// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003205// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003206// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003207// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003208// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003209// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003210// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003211// case KeyEvent.KEYCODE_M: // unused
3212// case KeyEvent.KEYCODE_N: // in Chrome: new window
3213// case KeyEvent.KEYCODE_O: // in Chrome: open file
3214// case KeyEvent.KEYCODE_P: // in Chrome: print page
3215// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003216// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003217// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3218 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003219 // we can't use the ctrl/shift flags, they check for
3220 // exclusive use of a modifier
3221 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003222 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003223 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003224 } else {
3225 openTabToHomePage();
3226 }
3227 return true;
3228 }
3229 break;
3230// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3231// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003232// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003233// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3234// case KeyEvent.KEYCODE_Y: // unused
3235// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003236 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003237 // it is a regular key and webview is not null
3238 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003239 }
3240
John Reck9c35b9c2012-05-30 10:08:50 -07003241 @Override
3242 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003243 switch(keyCode) {
3244 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003245 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003246 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003247 return true;
3248 }
3249 break;
3250 }
3251 return false;
3252 }
3253
John Reck9c35b9c2012-05-30 10:08:50 -07003254 @Override
3255 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003256 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003257 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003258 if (KeyEvent.KEYCODE_MENU == keyCode
3259 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003260 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003261 }
3262 }
Cary Clark160bbb92011-01-10 11:17:07 -05003263 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003264 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003265 case KeyEvent.KEYCODE_BACK:
3266 if (event.isTracking() && !event.isCanceled()) {
3267 onBackKey();
3268 return true;
3269 }
3270 break;
3271 }
3272 return false;
3273 }
3274
3275 public boolean isMenuDown() {
3276 return mMenuIsDown;
3277 }
3278
John Reck9c35b9c2012-05-30 10:08:50 -07003279 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003280 public void setupAutoFill(Message message) {
3281 // Open the settings activity at the AutoFill profile fragment so that
3282 // the user can create a new profile. When they return, we will dispatch
3283 // the message so that we can autofill the form using their new profile.
3284 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3285 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3286 AutoFillSettingsFragment.class.getName());
3287 mAutoFillSetupMessage = message;
3288 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3289 }
John Reckb3417f02011-01-14 11:01:05 -08003290
John Reck9c35b9c2012-05-30 10:08:50 -07003291 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003292 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003293 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003294 return true;
3295 }
3296
John Reck1cf4b792011-07-26 10:22:22 -07003297 @Override
3298 public boolean shouldCaptureThumbnails() {
3299 return mUi.shouldCaptureThumbnails();
3300 }
3301
Michael Kolbc3af0672011-08-09 10:24:41 -07003302 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003303 public boolean supportsVoice() {
3304 PackageManager pm = mActivity.getPackageManager();
3305 List activities = pm.queryIntentActivities(new Intent(
3306 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3307 return activities.size() != 0;
3308 }
3309
3310 @Override
3311 public void startVoiceRecognizer() {
3312 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003313 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003314 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3315 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3316 mActivity.startActivityForResult(voice, VOICE_RESULT);
3317 }
3318
Pankaj Garg7b279f62014-08-12 14:47:18 -07003319 public void setWindowDimming(float level) {
3320 if (level != 0) {
3321 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3322 lp.dimAmount = level;
3323 mActivity.getWindow().setAttributes(lp);
3324 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3325 } else {
3326 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3327 }
3328 }
3329
Michael Kolb0b129122012-06-04 16:31:58 -07003330 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003331 public void setBlockEvents(boolean block) {
3332 mBlockEvents = block;
3333 }
3334
John Reck9c35b9c2012-05-30 10:08:50 -07003335 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003336 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003337 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003338 }
3339
John Reck9c35b9c2012-05-30 10:08:50 -07003340 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003341 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003342 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003343 }
3344
John Reck9c35b9c2012-05-30 10:08:50 -07003345 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003346 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003347 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003348 }
3349
John Reck9c35b9c2012-05-30 10:08:50 -07003350 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003351 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003352 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003353 }
3354
John Reck9c35b9c2012-05-30 10:08:50 -07003355 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003356 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003357 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003358 }
3359
Michael Kolb8233fac2010-10-26 16:08:53 -07003360}