blob: 5d8ac3565894b9673ce77a5d3bea4f468b3c6553 [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();
qqzhoua95a2e22013-04-18 17:28:31 +08001161 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001162 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001163 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 // This Tab was opened for the sole purpose of downloading a
1165 // file. Remove it.
1166 if (tab == mTabControl.getCurrentTab()) {
1167 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001168 if (tab.getDerivedFromIntent())
1169 closeTab(tab);
1170 else
1171 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 } else {
1173 // In this case, it is not.
1174 closeTab(tab);
1175 }
1176 }
1177 }
1178
1179 @Override
1180 public Bitmap getDefaultVideoPoster() {
1181 return mUi.getDefaultVideoPoster();
1182 }
1183
1184 @Override
1185 public View getVideoLoadingProgressView() {
1186 return mUi.getVideoLoadingProgressView();
1187 }
1188
1189 @Override
1190 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1191 SslError error) {
1192 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1193 }
1194
1195 // helper method
1196
1197 /*
1198 * Update the favorites icon if the private browsing isn't enabled and the
1199 * icon is valid.
1200 */
1201 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1202 final String url, Bitmap favicon) {
1203 if (favicon == null) {
1204 return;
1205 }
1206 if (!tab.isPrivateBrowsingEnabled()) {
1207 Bookmarks.updateFavicon(mActivity
1208 .getContentResolver(), originalUrl, url, favicon);
1209 }
1210 }
1211
Leon Scroggins4cd97792010-12-03 15:31:56 -05001212 @Override
1213 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001214 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001215 mUi.bookmarkedStatusHasChanged(tab);
1216 }
1217
Michael Kolb8233fac2010-10-26 16:08:53 -07001218 // end WebViewController
1219
1220 protected void pageUp() {
1221 getCurrentTopWebView().pageUp(false);
1222 }
1223
1224 protected void pageDown() {
1225 getCurrentTopWebView().pageDown(false);
1226 }
1227
1228 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001229 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001230 public void editUrl() {
1231 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001232 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001233 }
1234
John Reck9c35b9c2012-05-30 10:08:50 -07001235 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001236 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001237 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001238 if (tab.inForeground()) {
1239 if (mUi.isCustomViewShowing()) {
1240 callback.onCustomViewHidden();
1241 return;
1242 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001243 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001244 // Save the menu state and set it to empty while the custom
1245 // view is showing.
1246 mOldMenuState = mMenuState;
1247 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001248 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001249 }
1250 }
1251
1252 @Override
1253 public void hideCustomView() {
1254 if (mUi.isCustomViewShowing()) {
1255 mUi.onHideCustomView();
1256 // Reset the old menu state.
1257 mMenuState = mOldMenuState;
1258 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001259 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001260 }
1261 }
1262
John Reck9c35b9c2012-05-30 10:08:50 -07001263 @Override
1264 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001265 Intent intent) {
1266 if (getCurrentTopWebView() == null) return;
1267 switch (requestCode) {
1268 case PREFERENCES_PAGE:
1269 if (resultCode == Activity.RESULT_OK && intent != null) {
1270 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001271 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 mTabControl.removeParentChildRelationShips();
1273 }
1274 }
1275 break;
1276 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001277 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001278 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001280 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001281 case AUTOFILL_SETUP:
1282 // Determine whether a profile was actually set up or not
1283 // and if so, send the message back to the WebTextView to
1284 // fill the form with the new profile.
1285 if (getSettings().getAutoFillProfile() != null) {
1286 mAutoFillSetupMessage.sendToTarget();
1287 mAutoFillSetupMessage = null;
1288 }
1289 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001290 case COMBO_VIEW:
1291 if (intent == null || resultCode != Activity.RESULT_OK) {
1292 break;
1293 }
John Reck3ba45532011-08-11 16:26:53 -07001294 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001295 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1296 Tab t = getCurrentTab();
1297 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001298 mUpdateMyNavThumbnail = true;
1299 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001300 loadUrl(t, uri.toString());
1301 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1302 String[] urls = intent.getStringArrayExtra(
1303 ComboViewActivity.EXTRA_OPEN_ALL);
1304 Tab parent = getCurrentTab();
1305 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001306 if (url != null) {
1307 parent = openTab(url, parent,
1308 !mSettings.openInBackground(), true);
1309 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001310 }
1311 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1312 long id = intent.getLongExtra(
1313 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1314 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001315 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001316 }
1317 }
1318 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001319 case VOICE_RESULT:
1320 if (resultCode == Activity.RESULT_OK && intent != null) {
1321 ArrayList<String> results = intent.getStringArrayListExtra(
1322 RecognizerIntent.EXTRA_RESULTS);
1323 if (results.size() >= 1) {
1324 mVoiceResult = results.get(0);
1325 }
1326 }
1327 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001328 case MY_NAVIGATION:
1329 if (intent == null || resultCode != Activity.RESULT_OK) {
1330 break;
1331 }
1332
1333 if (intent.getBooleanExtra("need_refresh", false) &&
1334 getCurrentTopWebView() != null) {
1335 getCurrentTopWebView().reload();
1336 }
1337 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001338 default:
1339 break;
1340 }
1341 getCurrentTopWebView().requestFocus();
1342 }
1343
1344 /**
1345 * Open the Go page.
1346 * @param startWithHistory If true, open starting on the history tab.
1347 * Otherwise, start with the bookmarks tab.
1348 */
1349 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001350 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001351 if (mTabControl.getCurrentWebView() == null) {
1352 return;
1353 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001354 // clear action mode
1355 if (isInCustomActionMode()) {
1356 endActionMode();
1357 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 Bundle extras = new Bundle();
1359 // Disable opening in a new window if we have maxed out the windows
1360 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1361 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001362 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001363 }
1364
1365 // combo view callbacks
1366
Michael Kolb8233fac2010-10-26 16:08:53 -07001367 // key handling
1368 protected void onBackKey() {
1369 if (!mUi.onBackKey()) {
1370 WebView subwindow = mTabControl.getCurrentSubWindow();
1371 if (subwindow != null) {
1372 if (subwindow.canGoBack()) {
1373 subwindow.goBack();
1374 } else {
1375 dismissSubWindow(mTabControl.getCurrentTab());
1376 }
1377 } else {
1378 goBackOnePageOrQuit();
1379 }
1380 }
1381 }
1382
Michael Kolb4bd767d2011-05-27 11:33:55 -07001383 protected boolean onMenuKey() {
1384 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001385 }
1386
Michael Kolb8233fac2010-10-26 16:08:53 -07001387 // menu handling and state
1388 // TODO: maybe put into separate handler
1389
John Reck9c35b9c2012-05-30 10:08:50 -07001390 @Override
1391 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001392 if (mMenuState == EMPTY_MENU) {
1393 return false;
1394 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001395 MenuInflater inflater = mActivity.getMenuInflater();
1396 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001397 return true;
1398 }
1399
John Reck9c35b9c2012-05-30 10:08:50 -07001400 @Override
1401 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001402 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001403 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001404 return;
1405 }
1406 if (!(v instanceof WebView)) {
1407 return;
1408 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001409 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 WebView.HitTestResult result = webview.getHitTestResult();
1411 if (result == null) {
1412 return;
1413 }
1414
1415 int type = result.getType();
1416 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1417 Log.w(LOGTAG,
1418 "We should not show context menu when nothing is touched");
1419 return;
1420 }
1421 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1422 // let TextView handles context menu
1423 return;
1424 }
1425
1426 // Note, http://b/issue?id=1106666 is requesting that
1427 // an inflated menu can be used again. This is not available
1428 // yet, so inflate each time (yuk!)
1429 MenuInflater inflater = mActivity.getMenuInflater();
1430 inflater.inflate(R.menu.browsercontext, menu);
1431
1432 // Show the correct menu group
1433 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001434 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001435 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001436 menu.setGroupVisible(R.id.PHONE_MENU,
1437 type == WebView.HitTestResult.PHONE_TYPE);
1438 menu.setGroupVisible(R.id.EMAIL_MENU,
1439 type == WebView.HitTestResult.EMAIL_TYPE);
1440 menu.setGroupVisible(R.id.GEO_MENU,
1441 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001442 String itemUrl = null;
1443 String url = webview.getOriginalUrl();
1444 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1445 itemUrl = Uri.decode(navigationUrl);
1446 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1447 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1448 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1449 } else {
1450 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1451 }
1452 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1453 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1454 } else {
1455 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1456
1457 menu.setGroupVisible(R.id.IMAGE_MENU,
1458 type == WebView.HitTestResult.IMAGE_TYPE
1459 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1460 menu.setGroupVisible(R.id.ANCHOR_MENU,
1461 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1462 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001463 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1464 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001465 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001466 // Setup custom handling depending on the type
1467 switch (type) {
1468 case WebView.HitTestResult.PHONE_TYPE:
1469 menu.setHeaderTitle(Uri.decode(extra));
1470 menu.findItem(R.id.dial_context_menu_id).setIntent(
1471 new Intent(Intent.ACTION_VIEW, Uri
1472 .parse(WebView.SCHEME_TEL + extra)));
1473 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1474 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1475 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1476 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1477 addIntent);
1478 menu.findItem(R.id.copy_phone_context_menu_id)
1479 .setOnMenuItemClickListener(
1480 new Copy(extra));
1481 break;
1482
1483 case WebView.HitTestResult.EMAIL_TYPE:
1484 menu.setHeaderTitle(extra);
1485 menu.findItem(R.id.email_context_menu_id).setIntent(
1486 new Intent(Intent.ACTION_VIEW, Uri
1487 .parse(WebView.SCHEME_MAILTO + extra)));
1488 menu.findItem(R.id.copy_mail_context_menu_id)
1489 .setOnMenuItemClickListener(
1490 new Copy(extra));
1491 break;
1492
1493 case WebView.HitTestResult.GEO_TYPE:
1494 menu.setHeaderTitle(extra);
1495 menu.findItem(R.id.map_context_menu_id).setIntent(
1496 new Intent(Intent.ACTION_VIEW, Uri
1497 .parse(WebView.SCHEME_GEO
1498 + URLEncoder.encode(extra))));
1499 menu.findItem(R.id.copy_geo_context_menu_id)
1500 .setOnMenuItemClickListener(
1501 new Copy(extra));
1502 break;
1503
1504 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1505 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001506 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001507 // decide whether to show the open link in new tab option
1508 boolean showNewTab = mTabControl.canCreateNewTab();
1509 MenuItem newTabItem
1510 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001511 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001512 ? R.string.contextmenu_openlink_newwindow_background
1513 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 newTabItem.setVisible(showNewTab);
1515 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001516 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1517 newTabItem.setOnMenuItemClickListener(
1518 new MenuItem.OnMenuItemClickListener() {
1519 @Override
1520 public boolean onMenuItemClick(MenuItem item) {
1521 final HashMap<String, WebView> hrefMap =
1522 new HashMap<String, WebView>();
1523 hrefMap.put("webview", webview);
1524 final Message msg = mHandler.obtainMessage(
1525 FOCUS_NODE_HREF,
1526 R.id.open_newtab_context_menu_id,
1527 0, hrefMap);
1528 webview.requestFocusNodeHref(msg);
1529 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001530 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001531 });
1532 } else {
1533 newTabItem.setOnMenuItemClickListener(
1534 new MenuItem.OnMenuItemClickListener() {
1535 @Override
1536 public boolean onMenuItemClick(MenuItem item) {
1537 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001538 openTab(extra, parent,
1539 !mSettings.openInBackground(),
1540 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001541 return true;
1542 }
1543 });
1544 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001545 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001546 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1547 menu.setHeaderTitle(navigationUrl);
1548 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1549
1550 if (itemUrl != null) {
1551 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1552 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1553 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1554 @Override
1555 public boolean onMenuItemClick(MenuItem item) {
1556 final Intent intent = new Intent(Controller.this
1557 .getContext(),
1558 AddMyNavigationPage.class);
1559 Bundle bundle = new Bundle();
1560 String url = Uri.decode(navigationUrl);
1561 bundle.putBoolean("isAdding", false);
1562 bundle.putString("url", url);
1563 bundle.putString("name", getNameFromUrl(url));
1564 intent.putExtra("websites", bundle);
1565 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1566 return false;
1567 }
1568 });
1569 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1570 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1571 @Override
1572 public boolean onMenuItemClick(MenuItem item) {
1573 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1574 return false;
1575 }
1576 });
1577 }
1578 } else {
1579 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1580 }
1581 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001582 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1583 break;
1584 }
1585 // otherwise fall through to handle image part
1586 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001587 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1588 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1590 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001591 shareItem.setOnMenuItemClickListener(
1592 new MenuItem.OnMenuItemClickListener() {
1593 @Override
1594 public boolean onMenuItemClick(MenuItem item) {
1595 sharePage(mActivity, null, extra, null,
1596 null);
1597 return true;
1598 }
1599 }
1600 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001602 menu.findItem(R.id.view_image_context_menu_id)
1603 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1604 @Override
1605 public boolean onMenuItemClick(MenuItem item) {
1606 openTab(extra, mTabControl.getCurrentTab(), true, true);
1607 return false;
1608 }
1609 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001610 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1611 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1612 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001613 menu.findItem(R.id.set_wallpaper_context_menu_id).
1614 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1615 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 break;
1617
1618 default:
1619 Log.w(LOGTAG, "We should not get here.");
1620 break;
1621 }
1622 //update the ui
1623 mUi.onContextMenuCreated(menu);
1624 }
1625
kaiyiz6e5b3e02013-08-19 20:02:01 +08001626 public void startAddMyNavigation(String url) {
1627 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1628 Bundle bundle = new Bundle();
1629 bundle.putBoolean("isAdding", true);
1630 bundle.putString("url", url);
1631 bundle.putString("name", getNameFromUrl(url));
1632 intent.putExtra("websites", bundle);
1633 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1634 }
1635
1636 private void showMyNavigationDeleteDialog(final String itemUrl) {
1637 new AlertDialog.Builder(this.getContext())
1638 .setTitle(R.string.my_navigation_delete_label)
1639 .setIcon(android.R.drawable.ic_dialog_alert)
1640 .setMessage(R.string.my_navigation_delete_msg)
1641 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1642 @Override
1643 public void onClick(DialogInterface dialog, int whichButton) {
1644 deleteMyNavigationItem(itemUrl);
1645 }
1646 })
1647 .setNegativeButton(R.string.cancel, null)
1648 .show();
1649 }
1650
1651 private void deleteMyNavigationItem(final String itemUrl) {
1652 ContentResolver cr = this.getContext().getContentResolver();
1653 Cursor cursor = null;
1654
1655 try {
1656 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1657 new String[] {
1658 MyNavigationUtil.ID
1659 }, "url = ?", new String[] {
1660 itemUrl
1661 }, null);
1662 if (null != cursor && cursor.moveToFirst()) {
1663 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1664 cursor.getLong(0));
1665
1666 ContentValues values = new ContentValues();
1667 values.put(MyNavigationUtil.TITLE, "");
1668 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1669 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1670 ByteArrayOutputStream os = new ByteArrayOutputStream();
1671 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1672 R.raw.my_navigation_add);
1673 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1674 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1675 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1676 cr.update(uri, values, null, null);
1677 } else {
1678 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1679 }
1680 } catch (IllegalStateException e) {
1681 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1682 } finally {
1683 if (null != cursor) {
1684 cursor.close();
1685 }
1686 }
1687
1688 if (getCurrentTopWebView() != null) {
1689 getCurrentTopWebView().reload();
1690 }
1691 }
1692
1693 private String getNameFromUrl(String itemUrl) {
1694 ContentResolver cr = this.getContext().getContentResolver();
1695 Cursor cursor = null;
1696 String name = null;
1697
1698 try {
1699 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1700 new String[] {
1701 MyNavigationUtil.TITLE
1702 }, "url = ?", new String[] {
1703 itemUrl
1704 }, null);
1705 if (null != cursor && cursor.moveToFirst()) {
1706 name = cursor.getString(0);
1707 } else {
1708 Log.e(LOGTAG, "this item does not exist!");
1709 }
1710 } catch (IllegalStateException e) {
1711 Log.e(LOGTAG, "getNameFromUrl", e);
1712 } finally {
1713 if (null != cursor) {
1714 cursor.close();
1715 }
1716 }
1717 return name;
1718 }
1719
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001720 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
1721 if (bitmap == null) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001722 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1723 return;
1724 }
1725
1726 final ContentResolver cr = mActivity.getContentResolver();
1727 new AsyncTask<Void, Void, Void>() {
1728 @Override
1729 protected Void doInBackground(Void... unused) {
1730 ContentResolver cr = mActivity.getContentResolver();
1731 Cursor cursor = null;
1732 try {
1733 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1734 new String[] {
1735 MyNavigationUtil.ID
1736 }, "url = ?", new String[] {
1737 itemUrl
1738 }, null);
1739 if (null != cursor && cursor.moveToFirst()) {
1740 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001741 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001742
1743 ContentValues values = new ContentValues();
1744 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1745 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1746 cursor.getLong(0));
1747 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1748 cr.update(uri, values, null, null);
1749 os.close();
1750 }
1751 } catch (IllegalStateException e) {
1752 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1753 } catch (IOException e) {
1754 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1755 } finally {
1756 if (null != cursor) {
1757 cursor.close();
1758 }
1759 }
1760 return null;
1761 }
1762 }.execute();
1763 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001764 /**
1765 * As the menu can be open when loading state changes
1766 * we must manually update the state of the stop/reload menu
1767 * item
1768 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001769 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001770 if (menu == null) {
1771 return;
1772 }
1773 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001774 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001775 menu.findItem(R.id.stop_menu_id):
1776 menu.findItem(R.id.reload_menu_id);
1777 if (src != null) {
1778 dest.setIcon(src.getIcon());
1779 dest.setTitle(src.getTitle());
1780 }
1781 }
1782
John Reck9c35b9c2012-05-30 10:08:50 -07001783 @Override
1784 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001785 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001786 // hold on to the menu reference here; it is used by the page callbacks
1787 // to update the menu based on loading state
1788 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001789 // Note: setVisible will decide whether an item is visible; while
1790 // setEnabled() will decide whether an item is enabled, which also means
1791 // whether the matching shortcut key will function.
1792 switch (mMenuState) {
1793 case EMPTY_MENU:
1794 if (mCurrentMenuState != mMenuState) {
1795 menu.setGroupVisible(R.id.MAIN_MENU, false);
1796 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1797 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1798 }
1799 break;
1800 default:
1801 if (mCurrentMenuState != mMenuState) {
1802 menu.setGroupVisible(R.id.MAIN_MENU, true);
1803 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1804 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1805 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001806 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001807 break;
1808 }
1809 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001810 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001811 }
1812
Michael Kolb4bf79712011-07-14 14:18:12 -07001813 @Override
1814 public void updateMenuState(Tab tab, Menu menu) {
1815 boolean canGoBack = false;
1816 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001817 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001818 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001819 // Following flag is used to identify schemes for which the LIVE_MENU
1820 // items defined in res/menu/browser.xml should be enabled
1821 boolean isLiveScheme = false;
1822 boolean isPageFinished = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001823 if (tab != null) {
1824 canGoBack = tab.canGoBack();
1825 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001826 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001827 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001828 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
1829 isPageFinished = tab.getPageFinishedStatus();
Michael Kolb4bf79712011-07-14 14:18:12 -07001830 }
1831 final MenuItem back = menu.findItem(R.id.back_menu_id);
1832 back.setEnabled(canGoBack);
1833
1834 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001835
1836 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1837 forward.setEnabled(canGoForward);
1838
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001839 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1840 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001841 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001842 if (source != null && dest != null) {
1843 dest.setTitle(source.getTitle());
1844 dest.setIcon(source.getIcon());
1845 }
John Recke1a03a32011-09-14 17:04:16 -07001846 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001847
1848 // decide whether to show the share link option
1849 PackageManager pm = mActivity.getPackageManager();
1850 Intent send = new Intent(Intent.ACTION_SEND);
1851 send.setType("text/plain");
1852 ResolveInfo ri = pm.resolveActivity(send,
1853 PackageManager.MATCH_DEFAULT_ONLY);
1854 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1855
1856 boolean isNavDump = mSettings.enableNavDump();
1857 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1858 nav.setVisible(isNavDump);
1859 nav.setEnabled(isNavDump);
1860
1861 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001862 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1863 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001864 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001865 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001866 menu.setGroupEnabled(R.id.OFFLINE_READING, isLive && isLiveScheme && isPageFinished);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001867 // history and snapshots item are the members of COMBO menu group,
1868 // so if show history item, only make snapshots item invisible.
1869 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001870
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001871 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001872 }
1873
John Reck9c35b9c2012-05-30 10:08:50 -07001874 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001875 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001876 if (null == getCurrentTopWebView()) {
1877 return false;
1878 }
1879 if (mMenuIsDown) {
1880 // The shortcut action consumes the MENU. Even if it is still down,
1881 // it won't trigger the next shortcut action. In the case of the
1882 // shortcut action triggering a new activity, like Bookmarks, we
1883 // won't get onKeyUp for MENU. So it is important to reset it here.
1884 mMenuIsDown = false;
1885 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001886 if (mUi.onOptionsItemSelected(item)) {
1887 // ui callback handled it
1888 return true;
1889 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001890 switch (item.getItemId()) {
1891 // -- Main menu
1892 case R.id.new_tab_menu_id:
1893 openTabToHomePage();
1894 break;
1895
1896 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001897 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001898 break;
1899
Afzal Najamd4e33312012-04-26 01:54:01 -04001900 case R.id.close_other_tabs_id:
1901 closeOtherTabs();
1902 break;
1903
Michael Kolb8233fac2010-10-26 16:08:53 -07001904 case R.id.goto_menu_id:
1905 editUrl();
1906 break;
1907
1908 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001909 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1910 break;
1911
1912 case R.id.history_menu_id:
1913 bookmarksOrHistoryPicker(ComboViews.History);
1914 break;
1915
1916 case R.id.snapshots_menu_id:
1917 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001918 break;
1919
Michael Kolb8233fac2010-10-26 16:08:53 -07001920 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001921 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001922 break;
1923
1924 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001925 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001926 stopLoading();
1927 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001928 Tab currentTab = mTabControl.getCurrentTab();
1929 if (currentTab.hasCrashed) {
1930 currentTab.replaceCrashView(getCurrentTopWebView(),
1931 currentTab.getViewContainer());
1932 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 getCurrentTopWebView().reload();
1934 }
1935 break;
1936
1937 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001938 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001939 break;
1940
1941 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001942 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001943 break;
1944
1945 case R.id.close_menu_id:
1946 // Close the subwindow if it exists.
1947 if (mTabControl.getCurrentSubWindow() != null) {
1948 dismissSubWindow(mTabControl.getCurrentTab());
1949 break;
1950 }
1951 closeCurrentTab();
1952 break;
1953
kaiyiza8b6dbb2013-07-29 18:11:22 +08001954 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001955 Object[] params = { new String("persist.debug.browsermonkeytest")};
1956 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001957 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001958 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001959 if (ret != null && ret.equals("enable"))
1960 break;
luxiaolb40014b2013-07-19 10:01:43 +08001961 showExitDialog(mActivity);
1962 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 case R.id.homepage_menu_id:
1964 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001965 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001966 break;
1967
1968 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001969 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001970 break;
1971
1972 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001973 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001974 break;
1975
John Reck2bc80422011-06-30 15:11:49 -07001976 case R.id.save_snapshot_menu_id:
1977 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001978 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001979 createScreenshotAsync(
1980 source.getWebView(),
1981 getDesiredThumbnailWidth(mActivity),
1982 getDesiredThumbnailHeight(mActivity),
1983 new ValueCallback<Bitmap>() {
1984 @Override
1985 public void onReceiveValue(Bitmap bitmap) {
1986 new SaveSnapshotTask(source, bitmap).execute();
1987 }
1988 });
Leon Scrogginsac993842011-02-02 12:54:07 -05001989 break;
1990
Michael Kolb8233fac2010-10-26 16:08:53 -07001991 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001992 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001993 break;
1994
John Recke1a03a32011-09-14 17:04:16 -07001995 case R.id.snapshot_go_live:
1996 goLive();
1997 return true;
1998
Michael Kolb8233fac2010-10-26 16:08:53 -07001999 case R.id.share_page_menu_id:
2000 Tab currentTab = mTabControl.getCurrentTab();
2001 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002002 return false;
2003 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002004 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002005 break;
2006
2007 case R.id.dump_nav_menu_id:
2008 getCurrentTopWebView().debugDump();
2009 break;
2010
Michael Kolb8233fac2010-10-26 16:08:53 -07002011 case R.id.zoom_in_menu_id:
2012 getCurrentTopWebView().zoomIn();
2013 break;
2014
2015 case R.id.zoom_out_menu_id:
2016 getCurrentTopWebView().zoomOut();
2017 break;
2018
2019 case R.id.view_downloads_menu_id:
2020 viewDownloads();
2021 break;
2022
John Reck42229bc2011-08-19 13:26:43 -07002023 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002024 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002025 break;
2026
Michael Kolb8233fac2010-10-26 16:08:53 -07002027 case R.id.window_one_menu_id:
2028 case R.id.window_two_menu_id:
2029 case R.id.window_three_menu_id:
2030 case R.id.window_four_menu_id:
2031 case R.id.window_five_menu_id:
2032 case R.id.window_six_menu_id:
2033 case R.id.window_seven_menu_id:
2034 case R.id.window_eight_menu_id:
2035 {
2036 int menuid = item.getItemId();
2037 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2038 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2039 Tab desiredTab = mTabControl.getTab(id);
2040 if (desiredTab != null &&
2041 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002042 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002043 }
2044 break;
2045 }
2046 }
2047 }
2048 break;
2049
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002050 case R.id.about_menu_id:
2051 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2052 builder.setTitle(R.string.about);
2053 builder.setCancelable(true);
2054 String ua = "";
2055 final WebView currentWebView = getCurrentWebView();
2056 if (currentWebView != null) {
2057 final WebSettings s = currentWebView.getSettings();
2058 if (s != null) {
2059 ua = s.getUserAgentString();
2060 }
2061 }
2062 builder.setMessage("Agent:" + ua);
2063 builder.setPositiveButton(android.R.string.ok, null);
2064 builder.create().show();
2065 break;
2066
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 default:
2068 return false;
2069 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002070 return true;
2071 }
2072
John Reck68234a92012-04-19 15:27:12 -07002073 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2074 implements OnCancelListener {
2075
2076 private Tab mTab;
2077 private Dialog mProgressDialog;
2078 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002079 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002080
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002081 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002082 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002083 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002084 }
2085
2086 @Override
2087 protected void onPreExecute() {
2088 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2089 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2090 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002091 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002092 }
2093
2094 @Override
2095 protected Long doInBackground(Void... params) {
2096 if (!mTab.saveViewState(mValues)) {
2097 return null;
2098 }
2099 if (isCancelled()) {
2100 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2101 File file = mActivity.getFileStreamPath(path);
2102 if (!file.delete()) {
2103 file.deleteOnExit();
2104 }
2105 return null;
2106 }
2107 final ContentResolver cr = mActivity.getContentResolver();
2108 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2109 if (result == null) {
2110 return null;
2111 }
2112 long id = ContentUris.parseId(result);
2113 return id;
2114 }
2115
2116 @Override
2117 protected void onPostExecute(Long id) {
2118 if (isCancelled()) {
2119 return;
2120 }
2121 mProgressDialog.dismiss();
2122 if (id == null) {
2123 Toast.makeText(mActivity, R.string.snapshot_failed,
2124 Toast.LENGTH_SHORT).show();
2125 return;
2126 }
2127 Bundle b = new Bundle();
2128 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2129 mUi.showComboView(ComboViews.Snapshots, b);
2130 }
2131
2132 @Override
2133 public void onCancel(DialogInterface dialog) {
2134 cancel(true);
2135 }
2136 }
2137
Michael Kolb80f75082012-04-10 10:50:06 -07002138 @Override
2139 public void toggleUserAgent() {
2140 WebView web = getCurrentWebView();
2141 mSettings.toggleDesktopUseragent(web);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002142 web.reload();
Michael Kolb80f75082012-04-10 10:50:06 -07002143 }
2144
2145 @Override
2146 public void findOnPage() {
2147 getCurrentTopWebView().showFindDialog(null, true);
2148 }
2149
2150 @Override
2151 public void openPreferences() {
2152 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2153 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2154 getCurrentTopWebView().getUrl());
2155 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2156 }
2157
2158 @Override
2159 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002160 WebView w = getCurrentTopWebView();
2161 if (w == null)
2162 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002163 final Intent i = createBookmarkCurrentPageIntent(false);
2164 createScreenshotAsync(
2165 w, getDesiredThumbnailWidth(mActivity),
2166 getDesiredThumbnailHeight(mActivity),
2167 new ValueCallback<Bitmap>() {
2168 @Override
2169 public void onReceiveValue(Bitmap bitmap) {
2170 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2171 mActivity.startActivity(i);
2172 }
2173 });
Michael Kolb80f75082012-04-10 10:50:06 -07002174 }
2175
John Recke1a03a32011-09-14 17:04:16 -07002176 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002177 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002178 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002179 boolean onlySingleTabRemaining = false;
2180 if (mTabControl.getTabCount() > 1) {
2181 // destroy the old snapshot tab
2182 closeCurrentTab();
2183 } else {
2184 onlySingleTabRemaining = true;
2185 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002186 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002187 if (onlySingleTabRemaining) {
2188 closeTab(t);
2189 }
2190
Tarun Nainani8eb00912014-07-17 12:28:32 -07002191 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002192 }
2193
luxiaolb40014b2013-07-19 10:01:43 +08002194 private void showExitDialog(final Activity activity) {
2195 new AlertDialog.Builder(activity)
2196 .setTitle(R.string.exit_browser_title)
2197 .setIcon(android.R.drawable.ic_dialog_alert)
2198 .setMessage(R.string.exit_browser_msg)
2199 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2200 public void onClick(DialogInterface dialog, int which) {
2201 activity.moveTaskToBack(true);
2202 dialog.dismiss();
2203 }
2204 })
2205 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2206 public void onClick(DialogInterface dialog, int which) {
2207 activity.finish();
2208 mHandler.postDelayed(new Runnable() {
2209 @Override
2210 public void run() {
2211 // TODO Auto-generated method stub
2212 mCrashRecoveryHandler.clearState(true);
2213 int pid = android.os.Process.myPid();
2214 android.os.Process.killProcess(pid);
2215 }
2216 }, 300);
2217 dialog.dismiss();
2218 }
2219 })
2220 .show();
2221 }
Michael Kolb315d5022011-10-13 12:47:11 -07002222 @Override
2223 public void showPageInfo() {
2224 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2225 }
2226
John Reck9c35b9c2012-05-30 10:08:50 -07002227 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002228 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002229 // Let the History and Bookmark fragments handle menus they created.
2230 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2231 return false;
2232 }
2233
Michael Kolb8233fac2010-10-26 16:08:53 -07002234 int id = item.getItemId();
2235 boolean result = true;
2236 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002237 // -- Browser context menu
2238 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002239 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002240 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002241 case R.id.copy_link_context_menu_id:
2242 final WebView webView = getCurrentTopWebView();
2243 if (null == webView) {
2244 result = false;
2245 break;
2246 }
2247 final HashMap<String, WebView> hrefMap =
2248 new HashMap<String, WebView>();
2249 hrefMap.put("webview", webView);
2250 final Message msg = mHandler.obtainMessage(
2251 FOCUS_NODE_HREF, id, 0, hrefMap);
2252 webView.requestFocusNodeHref(msg);
2253 break;
2254
2255 default:
2256 // For other context menus
2257 result = onOptionsItemSelected(item);
2258 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002259 return result;
2260 }
2261
2262 /**
2263 * support programmatically opening the context menu
2264 */
2265 public void openContextMenu(View view) {
2266 mActivity.openContextMenu(view);
2267 }
2268
2269 /**
2270 * programmatically open the options menu
2271 */
2272 public void openOptionsMenu() {
2273 mActivity.openOptionsMenu();
2274 }
2275
John Reck9c35b9c2012-05-30 10:08:50 -07002276 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002277 public boolean onMenuOpened(int featureId, Menu menu) {
2278 if (mOptionsMenuOpen) {
2279 if (mConfigChanged) {
2280 // We do not need to make any changes to the state of the
2281 // title bar, since the only thing that happened was a
2282 // change in orientation
2283 mConfigChanged = false;
2284 } else {
2285 if (!mExtendedMenuOpen) {
2286 mExtendedMenuOpen = true;
2287 mUi.onExtendedMenuOpened();
2288 } else {
2289 // Switching the menu back to icon view, so show the
2290 // title bar once again.
2291 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002292 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002293 }
2294 }
2295 } else {
2296 // The options menu is closed, so open it, and show the title
2297 mOptionsMenuOpen = true;
2298 mConfigChanged = false;
2299 mExtendedMenuOpen = false;
2300 mUi.onOptionsMenuOpened();
2301 }
2302 return true;
2303 }
2304
John Reck9c35b9c2012-05-30 10:08:50 -07002305 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002306 public void onOptionsMenuClosed(Menu menu) {
2307 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002308 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002309 }
2310
John Reck9c35b9c2012-05-30 10:08:50 -07002311 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002312 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002313 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002314 }
2315
2316 // Helper method for getting the top window.
2317 @Override
2318 public WebView getCurrentTopWebView() {
2319 return mTabControl.getCurrentTopWebView();
2320 }
2321
2322 @Override
2323 public WebView getCurrentWebView() {
2324 return mTabControl.getCurrentWebView();
2325 }
2326
2327 /*
2328 * This method is called as a result of the user selecting the options
2329 * menu to see the download window. It shows the download window on top of
2330 * the current window.
2331 */
2332 void viewDownloads() {
2333 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2334 mActivity.startActivity(intent);
2335 }
2336
John Reck30b065e2011-07-19 10:58:05 -07002337 int getActionModeHeight() {
2338 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2339 new int[] { android.R.attr.actionBarSize });
2340 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2341 actionBarSizeTypedArray.recycle();
2342 return size;
2343 }
2344
Michael Kolb8233fac2010-10-26 16:08:53 -07002345 // action mode
2346
John Reck9c35b9c2012-05-30 10:08:50 -07002347 @Override
2348 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002349 mUi.onActionModeStarted(mode);
2350 mActionMode = mode;
2351 }
2352
2353 /*
2354 * True if a custom ActionMode (i.e. find or select) is in use.
2355 */
2356 @Override
2357 public boolean isInCustomActionMode() {
2358 return mActionMode != null;
2359 }
2360
2361 /*
2362 * End the current ActionMode.
2363 */
2364 @Override
2365 public void endActionMode() {
2366 if (mActionMode != null) {
2367 mActionMode.finish();
2368 }
2369 }
2370
2371 /*
2372 * Called by find and select when they are finished. Replace title bars
2373 * as necessary.
2374 */
John Reck9c35b9c2012-05-30 10:08:50 -07002375 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002376 public void onActionModeFinished(ActionMode mode) {
2377 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002378 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002379 mActionMode = null;
2380 }
2381
2382 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002383 final Tab tab = getCurrentTab();
2384 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002385 }
2386
2387 // bookmark handling
2388
2389 /**
2390 * add the current page as a bookmark to the given folder id
2391 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002392 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002393 * bookmarked, and if it is, edit that bookmark. If false, and
2394 * the site is already bookmarked, do not attempt to edit the
2395 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002396 */
2397 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002398 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002399 WebView w = getCurrentTopWebView();
2400 if (w == null) {
2401 return null;
2402 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002403 Intent i = new Intent(mActivity,
2404 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002405 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2406 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2407 String touchIconUrl = w.getTouchIconUrl();
2408 if (touchIconUrl != null) {
2409 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2410 WebSettings settings = w.getSettings();
2411 if (settings != null) {
2412 i.putExtra(AddBookmarkPage.USER_AGENT,
2413 settings.getUserAgentString());
2414 }
2415 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002416 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002417 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002418 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002419 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2420 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002421 // Put the dialog at the upper right of the screen, covering the
2422 // star on the title bar.
2423 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002424 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002425 }
2426
2427 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002428 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002429 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002430 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002431 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002432 }
2433
Vivek Sekharb54614f2014-05-01 19:03:37 -07002434 @Override
2435 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2436 boolean capture) {
2437 mUploadHandler = new UploadHandler(this);
2438 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2439 }
2440
Michael Kolb8233fac2010-10-26 16:08:53 -07002441 // thumbnails
2442
2443 /**
2444 * Return the desired width for thumbnail screenshots, which are stored in
2445 * the database, and used on the bookmarks screen.
2446 * @param context Context for finding out the density of the screen.
2447 * @return desired width for thumbnail screenshot.
2448 */
2449 static int getDesiredThumbnailWidth(Context context) {
2450 return context.getResources().getDimensionPixelOffset(
2451 R.dimen.bookmarkThumbnailWidth);
2452 }
2453
2454 /**
2455 * Return the desired height for thumbnail screenshots, which are stored in
2456 * the database, and used on the bookmarks screen.
2457 * @param context Context for finding out the density of the screen.
2458 * @return desired height for thumbnail screenshot.
2459 */
2460 static int getDesiredThumbnailHeight(Context context) {
2461 return context.getResources().getDimensionPixelOffset(
2462 R.dimen.bookmarkThumbnailHeight);
2463 }
2464
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002465 static void createScreenshotAsync(WebView view, int width, int height,
2466 final ValueCallback<Bitmap> cb) {
2467 if (view == null || width == 0 || height == 0) {
2468 return;
2469 }
2470
2471 view.getContentBitmapAsync(
2472 (float) width / view.getWidth(),
2473 new Rect(),
2474 new ValueCallback<Bitmap>() {
2475 @Override
2476 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002477 if (bitmap != null)
2478 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2479 cb.onReceiveValue(bitmap);
2480 }});
2481 }
2482
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002483 private void updateScreenshot(final Tab tab) {
2484 createScreenshotAsync(
2485 tab.getWebView(),
2486 getDesiredThumbnailWidth(mActivity),
2487 getDesiredThumbnailHeight(mActivity),
2488 new ValueCallback<Bitmap>() {
2489 @Override
2490 public void onReceiveValue(Bitmap bitmap) {
2491 updateScreenshot(tab, bitmap);
2492 }
2493 });
2494 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002495
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002496 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002498 // FIXME: Would like to make sure there is actually something to
2499 // draw, but the API for that (WebViewCore.pictureReady()) is not
2500 // currently accessible here.
2501
John Reck34ef2672011-02-10 11:30:55 -08002502 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002503 if (view == null) {
2504 // Tab was destroyed
2505 return;
2506 }
John Reck34ef2672011-02-10 11:30:55 -08002507 final String url = tab.getUrl();
2508 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002509 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002510 if (TextUtils.isEmpty(url)) {
2511 return;
2512 }
2513
kaiyiz6e5b3e02013-08-19 20:02:01 +08002514 //update My Navigation Thumbnails
2515 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2516 if (isMyNavigationUrl) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002517 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002518 }
John Reck34ef2672011-02-10 11:30:55 -08002519 // Only update thumbnails for web urls (http(s)://), not for
2520 // about:, javascript:, data:, etc...
2521 // Unless it is a bookmarked site, then always update
2522 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2523 return;
2524 }
2525
kaiyiz6e5b3e02013-08-19 20:02:01 +08002526 if (url != null && mUpdateMyNavThumbnailUrl != null
2527 && Patterns.WEB_URL.matcher(url).matches()
2528 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2529 String urlHost = (new WebAddress(url)).getHost();
2530 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2531 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2532 || bookmarkHost.length() == 0) {
2533 return;
2534 }
2535 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2536 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2537 bookmarkHost.length());
2538 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2539 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2540 if (!bookmarkDomain.equals(urlDomain)) {
2541 return;
2542 }
2543 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002544 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002545 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2546 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2547 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2548 500);
2549 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002550 return;
2551 }
2552
2553 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002554 new AsyncTask<Void, Void, Void>() {
2555 @Override
2556 protected Void doInBackground(Void... unused) {
2557 Cursor cursor = null;
2558 try {
2559 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002560 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2561 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2562 : url);
2563 if (mUpdateMyNavThumbnail) {
2564 mUpdateMyNavThumbnail = false;
2565 mUpdateMyNavThumbnailUrl = null;
2566 }
John Reck34ef2672011-02-10 11:30:55 -08002567 if (cursor != null && cursor.moveToFirst()) {
2568 final ByteArrayOutputStream os =
2569 new ByteArrayOutputStream();
2570 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002571
John Reck34ef2672011-02-10 11:30:55 -08002572 ContentValues values = new ContentValues();
2573 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002574
John Reck34ef2672011-02-10 11:30:55 -08002575 do {
John Reck617fd832011-02-16 14:35:59 -08002576 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002577 cr.update(Images.CONTENT_URI, values, null, null);
2578 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002579 }
John Reck34ef2672011-02-10 11:30:55 -08002580 } catch (IllegalStateException e) {
2581 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002582 } catch (SQLiteException s) {
2583 // Added for possible error when user tries to remove the same bookmark
2584 // that is being updated with a screen shot
2585 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002586 } finally {
2587 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 }
John Reck34ef2672011-02-10 11:30:55 -08002589 return null;
2590 }
2591 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002592 }
2593
2594 private class Copy implements OnMenuItemClickListener {
2595 private CharSequence mText;
2596
John Reck9c35b9c2012-05-30 10:08:50 -07002597 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002598 public boolean onMenuItemClick(MenuItem item) {
2599 copy(mText);
2600 return true;
2601 }
2602
2603 public Copy(CharSequence toCopy) {
2604 mText = toCopy;
2605 }
2606 }
2607
Leon Scroggins63c02662010-11-18 15:16:27 -05002608 private static class Download implements OnMenuItemClickListener {
2609 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002610 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002611 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002612 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002613 private static final String FALLBACK_EXTENSION = "dat";
2614 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002615
John Reck9c35b9c2012-05-30 10:08:50 -07002616 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002617 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002618 if (DataUri.isDataUri(mText)) {
2619 saveDataUri();
2620 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002621 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002622 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002623 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002624 return true;
2625 }
2626
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002627 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2628 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002629 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002630 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002631 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002632 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002633 }
George Mount387d45d2011-10-07 15:57:53 -07002634
2635 /**
2636 * Treats mText as a data URI and writes its contents to a file
2637 * based on the current time.
2638 */
2639 private void saveDataUri() {
2640 FileOutputStream outputStream = null;
2641 try {
2642 DataUri uri = new DataUri(mText);
2643 File target = getTarget(uri);
2644 outputStream = new FileOutputStream(target);
2645 outputStream.write(uri.getData());
2646 final DownloadManager manager =
2647 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2648 manager.addCompletedDownload(target.getName(),
2649 mActivity.getTitle().toString(), false,
2650 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002651 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002652 } catch (IOException e) {
2653 Log.e(LOGTAG, "Could not save data URL");
2654 } finally {
2655 if (outputStream != null) {
2656 try {
2657 outputStream.close();
2658 } catch (IOException e) {
2659 // ignore close errors
2660 }
2661 }
2662 }
2663 }
2664
2665 /**
2666 * Creates a File based on the current time stamp and uses
2667 * the mime type of the DataUri to get the extension.
2668 */
2669 private File getTarget(DataUri uri) throws IOException {
2670 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002671 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002672 String nameBase = format.format(new Date());
2673 String mimeType = uri.getMimeType();
2674 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2675 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2676 if (extension == null) {
2677 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2678 extension = FALLBACK_EXTENSION;
2679 }
2680 extension = "." + extension; // createTempFile needs the '.'
2681 File targetFile = File.createTempFile(nameBase, extension, dir);
2682 return targetFile;
2683 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002684 }
2685
Cary Clark8974d282010-11-22 10:46:05 -05002686 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002687 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002688
John Reck9c35b9c2012-05-30 10:08:50 -07002689 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002690 public boolean onMenuItemClick(MenuItem item) {
2691 if (mWebView != null) {
2692 return mWebView.selectText();
2693 }
2694 return false;
2695 }
2696
2697 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002698 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002699 }
2700
2701 }
2702
Michael Kolb8233fac2010-10-26 16:08:53 -07002703 /********************** TODO: UI stuff *****************************/
2704
2705 // these methods have been copied, they still need to be cleaned up
2706
2707 /****************** tabs ***************************************************/
2708
2709 // basic tab interactions:
2710
2711 // it is assumed that tabcontrol already knows about the tab
2712 protected void addTab(Tab tab) {
2713 mUi.addTab(tab);
2714 }
2715
2716 protected void removeTab(Tab tab) {
2717 mUi.removeTab(tab);
2718 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002719 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002720 }
2721
Michael Kolbf2055602011-04-09 17:20:03 -07002722 @Override
2723 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002724 // monkey protection against delayed start
2725 if (tab != null) {
2726 mTabControl.setCurrentTab(tab);
2727 // the tab is guaranteed to have a webview after setCurrentTab
2728 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002729 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002730 //Purge active tabs
2731 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002732 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002733 }
2734
John Reck8bcafc12011-08-29 16:43:02 -07002735 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002736 Tab current = mTabControl.getCurrentTab();
2737 if (current != null
2738 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002739 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002740 }
2741 }
2742
John Reck26b18322011-06-21 13:08:58 -07002743 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002744 // Dismiss the subwindow if applicable.
2745 dismissSubWindow(appTab);
2746 // Since we might kill the WebView, remove it from the
2747 // content view first.
2748 mUi.detachTab(appTab);
2749 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002750 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002751 // TODO: analyze why the remove and add are necessary
2752 mUi.attachTab(appTab);
2753 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002754 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002755 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002756 } else {
2757 // If the tab was the current tab, we have to attach
2758 // it to the view system again.
2759 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002760 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002761 }
2762 }
2763
2764 // Remove the sub window if it exists. Also called by TabControl when the
2765 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002766 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002767 public void dismissSubWindow(Tab tab) {
2768 removeSubWindow(tab);
2769 // dismiss the subwindow. This will destroy the WebView.
2770 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002771 WebView wv = getCurrentTopWebView();
2772 if (wv != null) {
2773 wv.requestFocus();
2774 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002775 }
2776
2777 @Override
2778 public void removeSubWindow(Tab t) {
2779 if (t.getSubWebView() != null) {
2780 mUi.removeSubWindow(t.getSubViewContainer());
2781 }
2782 }
2783
2784 @Override
2785 public void attachSubWindow(Tab tab) {
2786 if (tab.getSubWebView() != null) {
2787 mUi.attachSubWindow(tab.getSubViewContainer());
2788 getCurrentTopWebView().requestFocus();
2789 }
2790 }
2791
Mathew Inwood29721c22011-06-29 17:55:24 +01002792 private Tab showPreloadedTab(final UrlData urlData) {
2793 if (!urlData.isPreloaded()) {
2794 return null;
2795 }
2796 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2797 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2798 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002799 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002800 // Could not submit query. Fallback to regular tab creation
2801 tabControl.destroy();
2802 return null;
2803 }
2804 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002805 // check tab count and make room for new tab
2806 if (!mTabControl.canCreateNewTab()) {
2807 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2808 if (leastUsed != null) {
2809 closeTab(leastUsed);
2810 }
2811 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002812 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002813 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002814 mTabControl.addPreloadedTab(t);
2815 addTab(t);
2816 setActiveTab(t);
2817 return t;
2818 }
2819
Michael Kolb7bcafde2011-05-09 13:55:59 -07002820 // open a non inconito tab with the given url data
2821 // and set as active tab
2822 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002823 Tab tab = showPreloadedTab(urlData);
2824 if (tab == null) {
2825 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002826 if ((tab != null) && !urlData.isEmpty()) {
2827 loadUrlDataIn(tab, urlData);
2828 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002829 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002830 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002831 }
2832
Michael Kolb843510f2010-12-09 10:51:49 -08002833 @Override
2834 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002835 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002836 }
2837
Michael Kolb8233fac2010-10-26 16:08:53 -07002838 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002839 public Tab openIncognitoTab() {
2840 return openTab(INCOGNITO_URI, true, true, false);
2841 }
2842
2843 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002844 public Tab openTab(String url, boolean incognito, boolean setActive,
2845 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002846 return openTab(url, incognito, setActive, useCurrent, null);
2847 }
2848
2849 @Override
2850 public Tab openTab(String url, Tab parent, boolean setActive,
2851 boolean useCurrent) {
2852 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2853 setActive, useCurrent, parent);
2854 }
2855
2856 public Tab openTab(String url, boolean incognito, boolean setActive,
2857 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002858 Tab tab = createNewTab(incognito, setActive, useCurrent);
2859 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002860 if (parent instanceof SnapshotTab) {
2861 addTab(tab);
2862 if (setActive)
2863 setActiveTab(tab);
2864 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002865 parent.addChildTab(tab);
2866 }
Michael Kolb519d2282011-05-09 17:03:19 -07002867 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002868 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002869 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002870 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002871 return tab;
2872 }
2873
2874 // this method will attempt to create a new tab
2875 // incognito: private browsing tab
2876 // setActive: ste tab as current tab
2877 // useCurrent: if no new tab can be created, return current tab
2878 private Tab createNewTab(boolean incognito, boolean setActive,
2879 boolean useCurrent) {
2880 Tab tab = null;
2881 if (mTabControl.canCreateNewTab()) {
2882 tab = mTabControl.createNewTab(incognito);
2883 addTab(tab);
2884 if (setActive) {
2885 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002886 } else {
2887 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002888 }
2889 } else {
2890 if (useCurrent) {
2891 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002892 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002893 } else {
2894 mUi.showMaxTabsWarning();
2895 }
2896 }
2897 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002898 }
2899
John Reck2bc80422011-06-30 15:11:49 -07002900 @Override
2901 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2902 SnapshotTab tab = null;
2903 if (mTabControl.canCreateNewTab()) {
2904 tab = mTabControl.createSnapshotTab(snapshotId);
2905 addTab(tab);
2906 if (setActive) {
2907 setActiveTab(tab);
2908 }
2909 } else {
2910 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002911 }
2912 return tab;
2913 }
2914
Michael Kolb8233fac2010-10-26 16:08:53 -07002915 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002916 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002917 * @return boolean True if we successfully switched to a different tab. If
2918 * the indexth tab is null, or if that tab is the same as
2919 * the current one, return false.
2920 */
2921 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002922 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002923 Tab currentTab = mTabControl.getCurrentTab();
2924 if (tab == null || tab == currentTab) {
2925 return false;
2926 }
2927 setActiveTab(tab);
2928 return true;
2929 }
2930
2931 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002932 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002933 closeCurrentTab(false);
2934 }
2935
2936 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002937 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002938 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002939 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002940 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002941 return;
2942 }
Michael Kolbc831b632011-05-11 09:30:34 -07002943 final Tab current = mTabControl.getCurrentTab();
2944 final int pos = mTabControl.getCurrentPosition();
2945 Tab newTab = current.getParent();
2946 if (newTab == null) {
2947 newTab = mTabControl.getTab(pos + 1);
2948 if (newTab == null) {
2949 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002950 }
2951 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002952 if (andQuit) {
2953 mTabControl.setCurrentTab(newTab);
2954 closeTab(current);
2955 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002956 // Close window
2957 closeTab(current);
2958 }
2959 }
2960
2961 /**
2962 * Close the tab, remove its associated title bar, and adjust mTabControl's
2963 * current tab to a valid value.
2964 */
2965 @Override
2966 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002967 if (tab == mTabControl.getCurrentTab()) {
2968 closeCurrentTab();
2969 } else {
2970 removeTab(tab);
2971 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002972 }
2973
Afzal Najamd4e33312012-04-26 01:54:01 -04002974 /**
2975 * Close all tabs except the current one
2976 */
2977 @Override
2978 public void closeOtherTabs() {
2979 int inactiveTabs = mTabControl.getTabCount() - 1;
2980 for (int i = inactiveTabs; i >= 0; i--) {
2981 Tab tab = mTabControl.getTab(i);
2982 if (tab != mTabControl.getCurrentTab()) {
2983 removeTab(tab);
2984 }
2985 }
2986 }
2987
Michael Kolb8233fac2010-10-26 16:08:53 -07002988 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002989 protected void loadUrlFromContext(String url) {
2990 Tab tab = getCurrentTab();
2991 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002992 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002993 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002994 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002995 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002996 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002997 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002998 }
2999 }
3000 }
3001
3002 /**
3003 * Load the URL into the given WebView and update the title bar
3004 * to reflect the new load. Call this instead of WebView.loadUrl
3005 * directly.
3006 * @param view The WebView used to load url.
3007 * @param url The URL to load.
3008 */
John Reck71e51422011-07-01 16:49:28 -07003009 @Override
3010 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003011 loadUrl(tab, url, null);
3012 }
3013
3014 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3015 if (tab != null) {
3016 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003017 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003018 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003019 if (tab.hasCrashed) {
3020 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3021 }
Michael Kolba53c9892011-10-05 13:31:40 -07003022 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003023 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003024 }
3025
3026 /**
3027 * Load UrlData into a Tab and update the title bar to reflect the new
3028 * load. Call this instead of UrlData.loadIn directly.
3029 * @param t The Tab used to load.
3030 * @param data The UrlData being loaded.
3031 */
3032 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003033 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003034 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003035 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003036 } else {
John Reck38b39652012-06-05 09:22:59 -07003037 if (t != null && data.mDisableUrlOverride) {
3038 t.disableUrlOverridingForLoad();
3039 }
John Reck26b18322011-06-21 13:08:58 -07003040 loadUrl(t, data.mUrl, data.mHeaders);
3041 }
3042 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003043 }
3044
John Reck30c714c2010-12-16 17:30:34 -08003045 @Override
3046 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003047 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003048 if (tab.canGoBack()) {
3049 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003050 } else {
John Reckef654f12011-07-12 16:42:08 -07003051 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003052 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003053 }
3054
3055 void goBackOnePageOrQuit() {
3056 Tab current = mTabControl.getCurrentTab();
3057 if (current == null) {
3058 /*
3059 * Instead of finishing the activity, simply push this to the back
3060 * of the stack and let ActivityManager to choose the foreground
3061 * activity. As BrowserActivity is singleTask, it will be always the
3062 * root of the task. So we can use either true or false for
3063 * moveTaskToBack().
3064 */
luxiaolb40014b2013-07-19 10:01:43 +08003065 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003066 return;
3067 }
John Reckef654f12011-07-12 16:42:08 -07003068 if (current.canGoBack()) {
3069 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003070 } else {
3071 // Check to see if we are closing a window that was created by
3072 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003073 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003074 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003075 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003076 // Now we close the other tab
3077 closeTab(current);
3078 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003079 /*
3080 * Instead of finishing the activity, simply push this to the back
3081 * of the stack and let ActivityManager to choose the foreground
3082 * activity. As BrowserActivity is singleTask, it will be always the
3083 * root of the task. So we can use either true or false for
3084 * moveTaskToBack().
3085 */
luxiaolb40014b2013-07-19 10:01:43 +08003086 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003087 }
3088 }
3089 }
3090
3091 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003092 * helper method for key handler
3093 * returns the current tab if it can't advance
3094 */
Michael Kolbc831b632011-05-11 09:30:34 -07003095 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003096 int pos = mTabControl.getCurrentPosition() + 1;
3097 if (pos >= mTabControl.getTabCount()) {
3098 pos = 0;
3099 }
3100 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003101 }
3102
3103 /**
3104 * helper method for key handler
3105 * returns the current tab if it can't advance
3106 */
Michael Kolbc831b632011-05-11 09:30:34 -07003107 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003108 int pos = mTabControl.getCurrentPosition() - 1;
3109 if ( pos < 0) {
3110 pos = mTabControl.getTabCount() - 1;
3111 }
3112 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003113 }
3114
John Reckbcef87f2012-02-03 14:58:34 -08003115 boolean isMenuOrCtrlKey(int keyCode) {
3116 return (KeyEvent.KEYCODE_MENU == keyCode)
3117 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3118 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3119 }
3120
Michael Kolb0035fad2011-03-14 13:25:28 -07003121 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003122 * handle key events in browser
3123 *
3124 * @param keyCode
3125 * @param event
3126 * @return true if handled, false to pass to super
3127 */
John Reck9c35b9c2012-05-30 10:08:50 -07003128 @Override
3129 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003130 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003131 // Even if MENU is already held down, we need to call to super to open
3132 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003133 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003134 mMenuIsDown = true;
3135 return false;
3136 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003137
Cary Clark8ff8c662010-12-29 15:03:05 -05003138 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003139 Tab tab = getCurrentTab();
3140 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003141
Cary Clark160bbb92011-01-10 11:17:07 -05003142 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3143 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003144
Michael Kolb8233fac2010-10-26 16:08:53 -07003145 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003146 case KeyEvent.KEYCODE_TAB:
3147 if (event.isCtrlPressed()) {
3148 if (event.isShiftPressed()) {
3149 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003150 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003151 } else {
3152 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003153 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003154 }
3155 return true;
3156 }
3157 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003158 case KeyEvent.KEYCODE_SPACE:
3159 // WebView/WebTextView handle the keys in the KeyDown. As
3160 // the Activity's shortcut keys are only handled when WebView
3161 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003162 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003163 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003164 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003165 pageDown();
3166 }
3167 return true;
3168 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003169 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003170 event.startTracking();
3171 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003172 case KeyEvent.KEYCODE_FORWARD:
3173 if (!noModifiers) break;
3174 tab.goForward();
3175 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003176 case KeyEvent.KEYCODE_DPAD_LEFT:
3177 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003178 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003179 return true;
3180 }
3181 break;
3182 case KeyEvent.KEYCODE_DPAD_RIGHT:
3183 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003184 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003185 return true;
3186 }
3187 break;
3188 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003189 if (ctrl) {
3190 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003191 return true;
3192 }
3193 break;
Michael Kolba4183062011-01-16 10:43:21 -08003194// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003195 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003196 if (ctrl ) {
3197 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003198 return true;
3199 }
3200 break;
Michael Kolba4183062011-01-16 10:43:21 -08003201// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003202// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003203// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003204// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003205// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003206// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003207// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003208// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003209// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003210// case KeyEvent.KEYCODE_M: // unused
3211// case KeyEvent.KEYCODE_N: // in Chrome: new window
3212// case KeyEvent.KEYCODE_O: // in Chrome: open file
3213// case KeyEvent.KEYCODE_P: // in Chrome: print page
3214// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003215// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003216// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3217 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003218 // we can't use the ctrl/shift flags, they check for
3219 // exclusive use of a modifier
3220 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003221 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003222 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003223 } else {
3224 openTabToHomePage();
3225 }
3226 return true;
3227 }
3228 break;
3229// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3230// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003231// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003232// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3233// case KeyEvent.KEYCODE_Y: // unused
3234// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003235 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003236 // it is a regular key and webview is not null
3237 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003238 }
3239
John Reck9c35b9c2012-05-30 10:08:50 -07003240 @Override
3241 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003242 switch(keyCode) {
3243 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003244 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003245 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003246 return true;
3247 }
3248 break;
3249 }
3250 return false;
3251 }
3252
John Reck9c35b9c2012-05-30 10:08:50 -07003253 @Override
3254 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003255 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003256 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003257 if (KeyEvent.KEYCODE_MENU == keyCode
3258 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003259 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003260 }
3261 }
Cary Clark160bbb92011-01-10 11:17:07 -05003262 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003263 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003264 case KeyEvent.KEYCODE_BACK:
3265 if (event.isTracking() && !event.isCanceled()) {
3266 onBackKey();
3267 return true;
3268 }
3269 break;
3270 }
3271 return false;
3272 }
3273
3274 public boolean isMenuDown() {
3275 return mMenuIsDown;
3276 }
3277
John Reck9c35b9c2012-05-30 10:08:50 -07003278 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003279 public void setupAutoFill(Message message) {
3280 // Open the settings activity at the AutoFill profile fragment so that
3281 // the user can create a new profile. When they return, we will dispatch
3282 // the message so that we can autofill the form using their new profile.
3283 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3284 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3285 AutoFillSettingsFragment.class.getName());
3286 mAutoFillSetupMessage = message;
3287 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3288 }
John Reckb3417f02011-01-14 11:01:05 -08003289
John Reck9c35b9c2012-05-30 10:08:50 -07003290 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003291 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003292 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003293 return true;
3294 }
3295
John Reck1cf4b792011-07-26 10:22:22 -07003296 @Override
3297 public boolean shouldCaptureThumbnails() {
3298 return mUi.shouldCaptureThumbnails();
3299 }
3300
Michael Kolbc3af0672011-08-09 10:24:41 -07003301 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003302 public boolean supportsVoice() {
3303 PackageManager pm = mActivity.getPackageManager();
3304 List activities = pm.queryIntentActivities(new Intent(
3305 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3306 return activities.size() != 0;
3307 }
3308
3309 @Override
3310 public void startVoiceRecognizer() {
3311 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003312 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003313 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3314 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3315 mActivity.startActivityForResult(voice, VOICE_RESULT);
3316 }
3317
Pankaj Garg7b279f62014-08-12 14:47:18 -07003318 public void setWindowDimming(float level) {
3319 if (level != 0) {
3320 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3321 lp.dimAmount = level;
3322 mActivity.getWindow().setAttributes(lp);
3323 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3324 } else {
3325 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3326 }
3327 }
3328
Michael Kolb0b129122012-06-04 16:31:58 -07003329 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003330 public void setBlockEvents(boolean block) {
3331 mBlockEvents = block;
3332 }
3333
John Reck9c35b9c2012-05-30 10:08:50 -07003334 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003335 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003336 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003337 }
3338
John Reck9c35b9c2012-05-30 10:08:50 -07003339 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003340 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003341 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003342 }
3343
John Reck9c35b9c2012-05-30 10:08:50 -07003344 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003345 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003346 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003347 }
3348
John Reck9c35b9c2012-05-30 10:08:50 -07003349 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003350 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003351 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003352 }
3353
John Reck9c35b9c2012-05-30 10:08:50 -07003354 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003355 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003356 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003357 }
3358
Michael Kolb8233fac2010-10-26 16:08:53 -07003359}