blob: 292a46dbdc7c4db424c28d5e924657408a11017a [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;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700203 private int mMenuState = EMPTY_MENU;
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 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) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700282 mMenuState = R.id.MAIN_MENU;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800283 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800284 // mCrashRecoverHandler has any previously saved state.
285 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700286 }
287
George Mount3636d0a2011-11-21 09:08:21 -0800288 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800289 // we dont want to ever recover incognito tabs
290 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700291
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700293 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500294 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000295
Michael Kolbc831b632011-05-11 09:30:34 -0700296 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500297 // Not able to restore so we go ahead and clear session cookies. We
298 // must do this before trying to login the user as we don't want to
299 // clear any session cookies set during login.
300 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700301 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800302 if (intent == null) {
303 // This won't happen under common scenarios. The icicle is
304 // not null, but there aren't any tabs to restore.
305 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700306 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800307 final Bundle extra = intent.getExtras();
308 // Create an initial tab.
309 // If the intent is ACTION_VIEW and data is not null, the Browser is
310 // invoked to view the content by another application. In this case,
311 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800312 UrlData urlData = null;
313 if (intent.getData() != null
314 && Intent.ACTION_VIEW.equals(intent.getAction())
315 && intent.getData().toString().startsWith("content://")) {
316 urlData = new UrlData(intent.getData().toString());
317 } else {
318 urlData = IntentHandler.getUrlDataFromIntent(intent);
319 }
George Mount3636d0a2011-11-21 09:08:21 -0800320 Tab t = null;
321 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700322 String landingPage = mActivity.getResources().getString(
323 R.string.def_landing_page);
324 if (!landingPage.isEmpty()) {
325 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800326 } else {
327 t = openTabToHomePage();
328 }
George Mount3636d0a2011-11-21 09:08:21 -0800329 } else {
330 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700331 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800332 }
333 if (t != null) {
334 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
335 }
336 WebView webView = t.getWebView();
337 if (extra != null) {
338 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
339 if (scale > 0 && scale <= 1000) {
340 webView.setInitialScale(scale);
341 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700342 }
343 }
John Reckd8c74522011-06-14 08:45:00 -0700344 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700345 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700346 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500347 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700348 List<Tab> tabs = mTabControl.getTabs();
349 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700350
John Reck1cf4b792011-07-26 10:22:22 -0700351 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700352 //handle restored pages that may require a JS interface
353 if (t.getWebView() != null) {
354 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
355 if (backForwardList != null) {
356 for (int i = 0; i < backForwardList.getSize(); i++) {
357 WebHistoryItem item = backForwardList.getItemAtIndex(i);
358 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
359 }
360 }
361 }
John Reck1cf4b792011-07-26 10:22:22 -0700362 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700363 if (t != mTabControl.getCurrentTab()) {
364 t.pause();
365 }
John Reck1cf4b792011-07-26 10:22:22 -0700366 }
367 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700368 if (tabs.size() == 0) {
369 openTabToHomePage();
370 }
John Reck1cf4b792011-07-26 10:22:22 -0700371 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700372 // TabControl.restoreState() will create a new tab even if
373 // restoring the state fails.
374 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800375 // Intent is non-null when framework thinks the browser should be
376 // launching with a new intent (icicle is null).
377 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700378 mIntentHandler.onNewIntent(intent);
379 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700380 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700381 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700382 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800383 if (jsFlags.trim().length() != 0) {
384 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700385 }
George Mount3636d0a2011-11-21 09:08:21 -0800386 if (intent != null
387 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700388 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800389 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700390 }
391
John Reck1cf4b792011-07-26 10:22:22 -0700392 private static class PruneThumbnails implements Runnable {
393 private Context mContext;
394 private List<Long> mIds;
395
396 PruneThumbnails(Context context, List<Long> preserveIds) {
397 mContext = context.getApplicationContext();
398 mIds = preserveIds;
399 }
400
401 @Override
402 public void run() {
403 ContentResolver cr = mContext.getContentResolver();
404 if (mIds == null || mIds.size() == 0) {
405 cr.delete(Thumbnails.CONTENT_URI, null, null);
406 } else {
407 int length = mIds.size();
408 StringBuilder where = new StringBuilder();
409 where.append(Thumbnails._ID);
410 where.append(" not in (");
411 for (int i = 0; i < length; i++) {
412 where.append(mIds.get(i));
413 if (i < (length - 1)) {
414 where.append(",");
415 }
416 }
417 where.append(")");
418 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
419 }
420 }
421
422 }
423
Michael Kolb1514bb72010-11-22 09:11:48 -0800424 @Override
425 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700426 return mFactory;
427 }
428
429 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800430 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800431 if (tab.hasCrashed)
432 tab.showCrashView();
433 else
434 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800435 }
436
437 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800438 public void createSubWindow(Tab tab) {
439 endActionMode();
440 WebView mainView = tab.getWebView();
441 WebView subView = mFactory.createWebView((mainView == null)
442 ? false
443 : mainView.isPrivateBrowsingEnabled());
444 mUi.createSubWindow(tab, subView);
445 }
446
447 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700448 public Context getContext() {
449 return mActivity;
450 }
451
452 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 public Activity getActivity() {
454 return mActivity;
455 }
456
457 void setUi(UI ui) {
458 mUi = ui;
459 }
460
Michael Kolbaf63dba2012-05-16 12:58:05 -0700461 @Override
462 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700463 return mSettings;
464 }
465
466 IntentHandler getIntentHandler() {
467 return mIntentHandler;
468 }
469
470 @Override
471 public UI getUi() {
472 return mUi;
473 }
474
475 int getMaxTabs() {
476 return mActivity.getResources().getInteger(R.integer.max_tabs);
477 }
478
479 @Override
480 public TabControl getTabControl() {
481 return mTabControl;
482 }
483
Michael Kolb1bf23132010-11-19 12:55:12 -0800484 @Override
485 public List<Tab> getTabs() {
486 return mTabControl.getTabs();
487 }
488
Michael Kolb8233fac2010-10-26 16:08:53 -0700489 private void startHandler() {
490 mHandler = new Handler() {
491
492 @Override
493 public void handleMessage(Message msg) {
494 switch (msg.what) {
495 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700496 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700497 break;
498 case FOCUS_NODE_HREF:
499 {
500 String url = (String) msg.getData().get("url");
501 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500502 String src = (String) msg.getData().get("src");
503 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 if (TextUtils.isEmpty(url)) {
505 break;
506 }
507 HashMap focusNodeMap = (HashMap) msg.obj;
508 WebView view = (WebView) focusNodeMap.get("webview");
509 // Only apply the action if the top window did not change.
510 if (getCurrentTopWebView() != view) {
511 break;
512 }
513 switch (msg.arg1) {
514 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700515 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700516 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500517 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700518 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500519 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500520 case R.id.open_newtab_context_menu_id:
521 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700522 openTab(url, parent,
523 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500524 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 case R.id.copy_link_context_menu_id:
526 copy(url);
527 break;
528 case R.id.save_link_context_menu_id:
529 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500530 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800531 mActivity, url, view.getSettings().getUserAgentString(),
532 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700533 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700534 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700535 if(title == null || title == "")
536 title = url;
537
538 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
539 //SWE TODO: No thumbnail support for the url obtained via
540 //browser context menu as its not loaded in webview.
541 if (bookmarkIntent != null) {
542 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
543 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
544 mActivity.startActivity(bookmarkIntent);
545 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700546 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700547 }
548 break;
549 }
550
551 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700552 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700553 break;
554
555 case STOP_LOAD:
556 stopLoading();
557 break;
558
559 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700560 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 mWakeLock.release();
562 // if we reach here, Browser should be still in the
563 // background loading after WAKELOCK_TIMEOUT (5-min).
564 // To avoid burning the battery, stop loading.
565 mTabControl.stopAllLoading();
566 }
567 break;
568
569 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800570 Tab tab = (Tab) msg.obj;
571 if (tab != null) {
572 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700573 }
574 break;
kaiyiz591110b2013-08-06 17:11:06 +0800575 case OPEN_MENU:
576 if (!mOptionsMenuOpen && mActivity != null ) {
577 mActivity.openOptionsMenu();
578 }
579 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 }
581 }
582 };
583
584 }
585
John Reckef654f12011-07-12 16:42:08 -0700586 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200587 public Tab getCurrentTab() {
588 return mTabControl.getCurrentTab();
589 }
590
Michael Kolbba99c5d2010-11-29 14:57:41 -0800591 @Override
592 public void shareCurrentPage() {
593 shareCurrentPage(mTabControl.getCurrentTab());
594 }
595
596 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700597 if (tab == null || tab.getWebView() == null)
598 return;
599
600 final Tab mytab = tab;
601 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
602 @Override
603 public void onReceiveValue(Bitmap bitmap) {
604 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
605 mytab.getFavicon(), bitmap);
606 }
607 };
608
609 createScreenshotAsync(
610 tab.getWebView(),
611 getDesiredThumbnailWidth(mActivity),
612 getDesiredThumbnailHeight(mActivity),
613 new ValueCallback<Bitmap>() {
614 @Override
615 public void onReceiveValue(Bitmap bitmap) {
616 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
617 mytab.getFavicon(), bitmap);
618 }
619 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800620 }
621
Michael Kolb8233fac2010-10-26 16:08:53 -0700622 /**
623 * Share a page, providing the title, url, favicon, and a screenshot. Uses
624 * an {@link Intent} to launch the Activity chooser.
625 * @param c Context used to launch a new Activity.
626 * @param title Title of the page. Stored in the Intent with
627 * {@link Intent#EXTRA_SUBJECT}
628 * @param url URL of the page. Stored in the Intent with
629 * {@link Intent#EXTRA_TEXT}
630 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
631 * with {@link Browser#EXTRA_SHARE_FAVICON}
632 * @param screenshot Bitmap of a screenshot of the page. Stored in the
633 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
634 */
635 static final void sharePage(Context c, String title, String url,
636 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700637
638 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
639 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
640 R.layout.app_row, sDialog.getApps());
641 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700642 }
643
644 private void copy(CharSequence text) {
645 ClipboardManager cm = (ClipboardManager) mActivity
646 .getSystemService(Context.CLIPBOARD_SERVICE);
647 cm.setText(text);
648 }
649
650 // lifecycle
651
John Reck9c35b9c2012-05-30 10:08:50 -0700652 @Override
653 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700654 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800655 // update the menu in case of a locale change
656 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800657 if (mOptionsMenuOpen) {
658 mActivity.closeOptionsMenu();
659 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
660 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 if (mPageDialogsHandler != null) {
662 mPageDialogsHandler.onConfigurationChanged(config);
663 }
664 mUi.onConfigurationChanged(config);
665 }
666
667 @Override
668 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700669 if (!mUi.isWebShowing()) {
670 mUi.showWeb(false);
671 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700672 mIntentHandler.onNewIntent(intent);
673 }
674
John Reck9c35b9c2012-05-30 10:08:50 -0700675 @Override
676 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800677 if (mUi.isCustomViewShowing()) {
678 hideCustomView();
679 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700680 if (mActivityPaused) {
681 Log.e(LOGTAG, "BrowserActivity is already paused.");
682 return;
683 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800685 Tab tab = mTabControl.getCurrentTab();
686 if (tab != null) {
687 tab.pause();
688 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700689 if (mWakeLock == null) {
690 PowerManager pm = (PowerManager) mActivity
691 .getSystemService(Context.POWER_SERVICE);
692 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
693 }
Michael Kolb70976932010-11-30 11:34:01 -0800694 mWakeLock.acquire();
695 mHandler.sendMessageDelayed(mHandler
696 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
697 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 }
699 mUi.onPause();
700 mNetworkHandler.onPause();
701
702 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100703 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 }
705
John Reck9c35b9c2012-05-30 10:08:50 -0700706 @Override
707 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700708 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800709 Bundle saveState = createSaveState();
710
711 // crash recovery manages all save & restore state
712 mCrashRecoveryHandler.writeState(saveState);
713 mSettings.setLastRunPaused(true);
714 }
715
716 /**
717 * Save the current state to outState. Does not write the state to
718 * disk.
719 * @return Bundle containing the current state of all tabs.
720 */
721 /* package */ Bundle createSaveState() {
722 Bundle saveState = new Bundle();
723 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800724 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 }
726
John Reck9c35b9c2012-05-30 10:08:50 -0700727 @Override
728 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700729 if (!mActivityPaused) {
730 Log.e(LOGTAG, "BrowserActivity is already resumed.");
731 return;
732 }
George Mount3636d0a2011-11-21 09:08:21 -0800733 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700734 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800735 Tab current = mTabControl.getCurrentTab();
736 if (current != null) {
737 current.resume();
738 resumeWebViewTimers(current);
739 }
John Reckf57c0292011-07-21 18:15:39 -0700740 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200741
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 mUi.onResume();
743 mNetworkHandler.onResume();
744 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100745 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700746 if (mVoiceResult != null) {
747 mUi.onVoiceResult(mVoiceResult);
748 mVoiceResult = null;
749 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800750 if (current != null && current.hasCrashed) {
751 current.showCrashView();
752 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 }
754
John Reckf57c0292011-07-21 18:15:39 -0700755 private void releaseWakeLock() {
756 if (mWakeLock != null && mWakeLock.isHeld()) {
757 mHandler.removeMessages(RELEASE_WAKELOCK);
758 mWakeLock.release();
759 }
760 }
761
Michael Kolb70976932010-11-30 11:34:01 -0800762 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800763 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800764 * @param tab guaranteed non-null
765 */
766 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700767 boolean inLoad = tab.inPageLoad();
768 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
769 CookieSyncManager.getInstance().startSync();
770 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100771 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700772 }
773 }
774
Michael Kolb70976932010-11-30 11:34:01 -0800775 /**
776 * Pause all WebView timers using the WebView of the given tab
777 * @param tab
778 * @return true if the timers are paused or tab is null
779 */
780 private boolean pauseWebViewTimers(Tab tab) {
781 if (tab == null) {
782 return true;
783 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700784 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100785 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700787 }
Michael Kolb70976932010-11-30 11:34:01 -0800788 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700789 }
790
John Reck9c35b9c2012-05-30 10:08:50 -0700791 @Override
792 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800793 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
795 mUploadHandler = null;
796 }
797 if (mTabControl == null) return;
798 mUi.onDestroy();
799 // Remove the current tab and sub window
800 Tab t = mTabControl.getCurrentTab();
801 if (t != null) {
802 dismissSubWindow(t);
803 removeTab(t);
804 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500805 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 // Destroy all the tabs
807 mTabControl.destroy();
Michael Kolb8233fac2010-10-26 16:08:53 -0700808 }
809
810 protected boolean isActivityPaused() {
811 return mActivityPaused;
812 }
813
John Reck9c35b9c2012-05-30 10:08:50 -0700814 @Override
815 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700816 mTabControl.freeMemory();
817 }
818
819 @Override
820 public boolean shouldShowErrorConsole() {
821 return mShouldShowErrorConsole;
822 }
823
824 protected void setShouldShowErrorConsole(boolean show) {
825 if (show == mShouldShowErrorConsole) {
826 // Nothing to do.
827 return;
828 }
829 mShouldShowErrorConsole = show;
830 Tab t = mTabControl.getCurrentTab();
831 if (t == null) {
832 // There is no current tab so we cannot toggle the error console
833 return;
834 }
835 mUi.setShouldShowErrorConsole(t, show);
836 }
837
838 @Override
839 public void stopLoading() {
840 mLoadStopped = true;
841 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700843 if (w != null) {
844 w.stopLoading();
845 mUi.onPageStopped(tab);
846 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 }
848
849 boolean didUserStopLoading() {
850 return mLoadStopped;
851 }
852
kaiyiza016da12013-08-26 17:50:22 +0800853 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700854 final String reminderType = getContext().getResources().getString(
855 R.string.def_wifi_browser_interaction_remind_type);
856 final String selectionConnnection = getContext().getResources().getString(
857 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700858 final String wifiSelection = getContext().getResources().getString(
859 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700860
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700861 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
862 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700863 return;
864
kaiyiza016da12013-08-26 17:50:22 +0800865 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700866 Context.CONNECTIVITY_SERVICE);
867 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700868 WifiManager wifiMgr = (WifiManager) this.getContext()
869 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700870 if (networkInfo == null
871 || (networkInfo != null && (networkInfo.getType() !=
872 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700873 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
874 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700875 List<ScanResult> list = wifiMgr.getScanResults();
876 // Have no AP's for Wifi's fall back to data
877 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700878 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700879 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
880 this.getContext().startActivity(intent);
881 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700882 // Request to select Wifi AP
883 try {
884 Intent intent = new Intent(wifiSelection);
885 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
886 this.getContext().startActivity(intent);
887 } catch (Exception e) {
888 String err_msg = this.getContext().getString(
889 R.string.acivity_not_found, wifiSelection);
890 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
891 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700892 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700893 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800894 }
895 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700896
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 // WebViewController
898
899 @Override
John Reck324d4402011-01-11 16:56:42 -0800900 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700901
902 // We've started to load a new page. If there was a pending message
903 // to save a screenshot then we will now take the new page and save
904 // an incorrect screenshot. Therefore, remove any pending thumbnail
905 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700906 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700907
908 // reset sync timer to avoid sync starts during loading a page
909 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700910 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700911 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700912 boolean networkNotifier =
913 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700914 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700915 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800916 } else {
917 if (!mNetworkHandler.isNetworkUp()) {
918 view.setNetworkAvailable(false);
919 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700920 }
921
922 // when BrowserActivity just starts, onPageStarted may be called before
923 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
924 // to start the timer. As we won't switch tabs while an activity is in
925 // pause state, we can ensure calling resume and pause in pair.
926 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800927 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 }
929 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700930 endActionMode();
931
John Reck30c714c2010-12-16 17:30:34 -0800932 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700933
John Reck324d4402011-01-11 16:56:42 -0800934 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 // update the bookmark database for favicon
936 maybeUpdateFavicon(tab, null, url, favicon);
937
938 Performance.tracePageStart(url);
939
940 // Performance probe
941 if (false) {
942 Performance.onPageStarted();
943 }
944
945 }
946
947 @Override
John Reck324d4402011-01-11 16:56:42 -0800948 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700949 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800950 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200951
Michael Kolb8233fac2010-10-26 16:08:53 -0700952 // Performance probe
953 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800954 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700955 }
956
Tarun Nainani8eb00912014-07-17 12:28:32 -0700957 tab.onPageFinished();
958
Michael Kolb8233fac2010-10-26 16:08:53 -0700959 Performance.tracePageFinished();
960 }
961
962 @Override
John Reck30c714c2010-12-16 17:30:34 -0800963 public void onProgressChanged(Tab tab) {
964 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700965
966 if (newProgress == 100) {
967 CookieSyncManager.getInstance().sync();
968 // onProgressChanged() may continue to be called after the main
969 // frame has finished loading, as any remaining sub frames continue
970 // to load. We'll only get called once though with newProgress as
971 // 100 when everything is loaded. (onPageFinished is called once
972 // when the main frame completes loading regardless of the state of
973 // any sub frames so calls to onProgressChanges may continue after
974 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800975 if (tab.inPageLoad()) {
976 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200977 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
978 // pause the WebView timer and release the wake lock if it is
979 // finished while BrowserActivity is in pause state.
980 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700981 }
John Reckd9862372012-02-21 15:04:50 -0800982 if (!tab.isPrivateBrowsingEnabled()
983 && !TextUtils.isEmpty(tab.getUrl())
984 && !tab.isSnapshot()) {
985 // Only update the bookmark screenshot if the user did not
986 // cancel the load early and there is not already
987 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700988 if (tab.shouldUpdateThumbnail() &&
989 (tab.inForeground() && !didUserStopLoading()
990 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800991 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
992 mHandler.sendMessageDelayed(mHandler.obtainMessage(
993 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800994 1500);
John Reckd9862372012-02-21 15:04:50 -0800995 }
996 }
997 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700998 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800999 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001000 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001001 // still loading
1002 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001004 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001005 }
1006 }
John Reck30c714c2010-12-16 17:30:34 -08001007 mUi.onProgressChanged(tab);
1008 }
1009
1010 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001011 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001012 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001013 }
1014
1015 @Override
1016 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001017 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001018 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001019 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001020 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1021 return;
1022 }
1023 // Update the title in the history database if not in private browsing mode
1024 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001025 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 }
1027 }
1028
1029 @Override
1030 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001031 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001032 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1033 }
1034
1035 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001036 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1037 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 }
1039
1040 @Override
1041 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1042 if (mMenuIsDown) {
1043 // only check shortcut key when MENU is held
1044 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1045 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001047 int keyCode = event.getKeyCode();
1048 // We need to send almost every key to WebKit. However:
1049 // 1. We don't want to block the device on the renderer for
1050 // some keys like menu, home, call.
1051 // 2. There are no WebKit equivalents for some of these keys
1052 // (see app/keyboard_codes_win.h)
1053 // Note that these are not the same set as KeyEvent.isSystemKey:
1054 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1055 if (keyCode == KeyEvent.KEYCODE_MENU ||
1056 keyCode == KeyEvent.KEYCODE_HOME ||
1057 keyCode == KeyEvent.KEYCODE_BACK ||
1058 keyCode == KeyEvent.KEYCODE_CALL ||
1059 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1060 keyCode == KeyEvent.KEYCODE_POWER ||
1061 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1062 keyCode == KeyEvent.KEYCODE_CAMERA ||
1063 keyCode == KeyEvent.KEYCODE_FOCUS ||
1064 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1065 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1066 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1067 return true;
1068 }
1069
1070 // We also have to intercept some shortcuts before we send them to the ContentView.
1071 if (event.isCtrlPressed() && (
1072 keyCode == KeyEvent.KEYCODE_TAB ||
1073 keyCode == KeyEvent.KEYCODE_W ||
1074 keyCode == KeyEvent.KEYCODE_F4)) {
1075 return true;
1076 }
1077
1078 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 }
1080
1081 @Override
John Reck997b1b72012-04-19 18:08:25 -07001082 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001083 if (!isActivityPaused()) {
1084 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001085 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001086 } else {
John Reck997b1b72012-04-19 18:08:25 -07001087 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001088 }
1089 }
John Reck997b1b72012-04-19 18:08:25 -07001090 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 }
1092
1093 @Override
John Reck324d4402011-01-11 16:56:42 -08001094 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001095 boolean disableHistoryWrites =
1096 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1097
1098 // Don't save anything in private browsing mode or when explicitly set
1099 // not to write history via an overlay
1100 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001101 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001102
John Reck324d4402011-01-11 16:56:42 -08001103 if (TextUtils.isEmpty(url)
1104 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 return;
1106 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001107
John Reckf57c0292011-07-21 18:15:39 -07001108 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001109 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001110 }
1111
1112 @Override
1113 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1114 AsyncTask<Void, Void, String[]> task =
1115 new AsyncTask<Void, Void, String[]>() {
1116 @Override
1117 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001118 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 }
1120 @Override
1121 public void onPostExecute(String[] result) {
1122 callback.onReceiveValue(result);
1123 }
1124 };
1125 task.execute();
1126 }
1127
1128 @Override
1129 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1130 final HttpAuthHandler handler, final String host,
1131 final String realm) {
1132 String username = null;
1133 String password = null;
1134
1135 boolean reuseHttpAuthUsernamePassword
1136 = handler.useHttpAuthUsernamePassword();
1137
1138 if (reuseHttpAuthUsernamePassword && view != null) {
1139 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1140 if (credentials != null && credentials.length == 2) {
1141 username = credentials[0];
1142 password = credentials[1];
1143 }
1144 }
1145
1146 if (username != null && password != null) {
1147 handler.proceed(username, password);
1148 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001149 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1151 } else {
1152 handler.cancel();
1153 }
1154 }
1155 }
1156
1157 @Override
1158 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001159 String contentDisposition, String mimetype, String referer,
1160 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001161 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001162 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001163 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001164 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001165 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 // This Tab was opened for the sole purpose of downloading a
1167 // file. Remove it.
1168 if (tab == mTabControl.getCurrentTab()) {
1169 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001170 if (tab.getDerivedFromIntent())
1171 closeTab(tab);
1172 else
1173 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 } else {
1175 // In this case, it is not.
1176 closeTab(tab);
1177 }
1178 }
1179 }
1180
1181 @Override
1182 public Bitmap getDefaultVideoPoster() {
1183 return mUi.getDefaultVideoPoster();
1184 }
1185
1186 @Override
1187 public View getVideoLoadingProgressView() {
1188 return mUi.getVideoLoadingProgressView();
1189 }
1190
1191 @Override
1192 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1193 SslError error) {
1194 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1195 }
1196
1197 // helper method
1198
1199 /*
1200 * Update the favorites icon if the private browsing isn't enabled and the
1201 * icon is valid.
1202 */
1203 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1204 final String url, Bitmap favicon) {
1205 if (favicon == null) {
1206 return;
1207 }
1208 if (!tab.isPrivateBrowsingEnabled()) {
1209 Bookmarks.updateFavicon(mActivity
1210 .getContentResolver(), originalUrl, url, favicon);
1211 }
1212 }
1213
Leon Scroggins4cd97792010-12-03 15:31:56 -05001214 @Override
1215 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001216 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001217 mUi.bookmarkedStatusHasChanged(tab);
1218 }
1219
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 // end WebViewController
1221
1222 protected void pageUp() {
1223 getCurrentTopWebView().pageUp(false);
1224 }
1225
1226 protected void pageDown() {
1227 getCurrentTopWebView().pageDown(false);
1228 }
1229
1230 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001231 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 public void editUrl() {
1233 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001234 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 }
1236
John Reck9c35b9c2012-05-30 10:08:50 -07001237 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001238 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001239 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 if (tab.inForeground()) {
1241 if (mUi.isCustomViewShowing()) {
1242 callback.onCustomViewHidden();
1243 return;
1244 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001245 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 // Save the menu state and set it to empty while the custom
1247 // view is showing.
1248 mOldMenuState = mMenuState;
1249 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001250 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 }
1252 }
1253
1254 @Override
1255 public void hideCustomView() {
1256 if (mUi.isCustomViewShowing()) {
1257 mUi.onHideCustomView();
1258 // Reset the old menu state.
1259 mMenuState = mOldMenuState;
1260 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001261 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 }
1263 }
1264
John Reck9c35b9c2012-05-30 10:08:50 -07001265 @Override
1266 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001267 Intent intent) {
1268 if (getCurrentTopWebView() == null) return;
1269 switch (requestCode) {
1270 case PREFERENCES_PAGE:
1271 if (resultCode == Activity.RESULT_OK && intent != null) {
1272 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001273 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 mTabControl.removeParentChildRelationShips();
1275 }
1276 }
1277 break;
1278 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001279 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001280 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001283 case AUTOFILL_SETUP:
1284 // Determine whether a profile was actually set up or not
1285 // and if so, send the message back to the WebTextView to
1286 // fill the form with the new profile.
1287 if (getSettings().getAutoFillProfile() != null) {
1288 mAutoFillSetupMessage.sendToTarget();
1289 mAutoFillSetupMessage = null;
1290 }
1291 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001292 case COMBO_VIEW:
1293 if (intent == null || resultCode != Activity.RESULT_OK) {
1294 break;
1295 }
John Reck3ba45532011-08-11 16:26:53 -07001296 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001297 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1298 Tab t = getCurrentTab();
1299 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001300 mUpdateMyNavThumbnail = true;
1301 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001302 loadUrl(t, uri.toString());
1303 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1304 String[] urls = intent.getStringArrayExtra(
1305 ComboViewActivity.EXTRA_OPEN_ALL);
1306 Tab parent = getCurrentTab();
1307 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001308 if (url != null) {
1309 parent = openTab(url, parent,
1310 !mSettings.openInBackground(), true);
1311 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001312 }
1313 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1314 long id = intent.getLongExtra(
1315 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1316 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001317 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001318 }
1319 }
1320 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001321 case VOICE_RESULT:
1322 if (resultCode == Activity.RESULT_OK && intent != null) {
1323 ArrayList<String> results = intent.getStringArrayListExtra(
1324 RecognizerIntent.EXTRA_RESULTS);
1325 if (results.size() >= 1) {
1326 mVoiceResult = results.get(0);
1327 }
1328 }
1329 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001330 case MY_NAVIGATION:
1331 if (intent == null || resultCode != Activity.RESULT_OK) {
1332 break;
1333 }
1334
1335 if (intent.getBooleanExtra("need_refresh", false) &&
1336 getCurrentTopWebView() != null) {
1337 getCurrentTopWebView().reload();
1338 }
1339 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001340 default:
1341 break;
1342 }
1343 getCurrentTopWebView().requestFocus();
1344 }
1345
1346 /**
1347 * Open the Go page.
1348 * @param startWithHistory If true, open starting on the history tab.
1349 * Otherwise, start with the bookmarks tab.
1350 */
1351 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001352 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001353 if (mTabControl.getCurrentWebView() == null) {
1354 return;
1355 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001356 // clear action mode
1357 if (isInCustomActionMode()) {
1358 endActionMode();
1359 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 Bundle extras = new Bundle();
1361 // Disable opening in a new window if we have maxed out the windows
1362 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1363 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001364 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001365 }
1366
1367 // combo view callbacks
1368
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 // key handling
1370 protected void onBackKey() {
1371 if (!mUi.onBackKey()) {
1372 WebView subwindow = mTabControl.getCurrentSubWindow();
1373 if (subwindow != null) {
1374 if (subwindow.canGoBack()) {
1375 subwindow.goBack();
1376 } else {
1377 dismissSubWindow(mTabControl.getCurrentTab());
1378 }
1379 } else {
1380 goBackOnePageOrQuit();
1381 }
1382 }
1383 }
1384
Michael Kolb4bd767d2011-05-27 11:33:55 -07001385 protected boolean onMenuKey() {
1386 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001387 }
1388
Michael Kolb8233fac2010-10-26 16:08:53 -07001389 // menu handling and state
1390 // TODO: maybe put into separate handler
1391
John Reck9c35b9c2012-05-30 10:08:50 -07001392 @Override
1393 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001394 if (mMenuState == EMPTY_MENU) {
1395 return false;
1396 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001397 MenuInflater inflater = mActivity.getMenuInflater();
1398 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001399 return true;
1400 }
1401
John Reck9c35b9c2012-05-30 10:08:50 -07001402 @Override
1403 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001404 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001405 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 return;
1407 }
1408 if (!(v instanceof WebView)) {
1409 return;
1410 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001411 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001412 WebView.HitTestResult result = webview.getHitTestResult();
1413 if (result == null) {
1414 return;
1415 }
1416
1417 int type = result.getType();
1418 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1419 Log.w(LOGTAG,
1420 "We should not show context menu when nothing is touched");
1421 return;
1422 }
1423 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1424 // let TextView handles context menu
1425 return;
1426 }
1427
1428 // Note, http://b/issue?id=1106666 is requesting that
1429 // an inflated menu can be used again. This is not available
1430 // yet, so inflate each time (yuk!)
1431 MenuInflater inflater = mActivity.getMenuInflater();
1432 inflater.inflate(R.menu.browsercontext, menu);
1433
1434 // Show the correct menu group
1435 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001436 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001437 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001438 menu.setGroupVisible(R.id.PHONE_MENU,
1439 type == WebView.HitTestResult.PHONE_TYPE);
1440 menu.setGroupVisible(R.id.EMAIL_MENU,
1441 type == WebView.HitTestResult.EMAIL_TYPE);
1442 menu.setGroupVisible(R.id.GEO_MENU,
1443 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001444 String itemUrl = null;
1445 String url = webview.getOriginalUrl();
1446 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1447 itemUrl = Uri.decode(navigationUrl);
1448 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1449 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1450 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1451 } else {
1452 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1453 }
1454 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1455 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1456 } else {
1457 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1458
1459 menu.setGroupVisible(R.id.IMAGE_MENU,
1460 type == WebView.HitTestResult.IMAGE_TYPE
1461 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1462 menu.setGroupVisible(R.id.ANCHOR_MENU,
1463 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1464 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001465 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1466 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001467 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001468 // Setup custom handling depending on the type
1469 switch (type) {
1470 case WebView.HitTestResult.PHONE_TYPE:
1471 menu.setHeaderTitle(Uri.decode(extra));
1472 menu.findItem(R.id.dial_context_menu_id).setIntent(
1473 new Intent(Intent.ACTION_VIEW, Uri
1474 .parse(WebView.SCHEME_TEL + extra)));
1475 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1476 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1477 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1478 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1479 addIntent);
1480 menu.findItem(R.id.copy_phone_context_menu_id)
1481 .setOnMenuItemClickListener(
1482 new Copy(extra));
1483 break;
1484
1485 case WebView.HitTestResult.EMAIL_TYPE:
1486 menu.setHeaderTitle(extra);
1487 menu.findItem(R.id.email_context_menu_id).setIntent(
1488 new Intent(Intent.ACTION_VIEW, Uri
1489 .parse(WebView.SCHEME_MAILTO + extra)));
1490 menu.findItem(R.id.copy_mail_context_menu_id)
1491 .setOnMenuItemClickListener(
1492 new Copy(extra));
1493 break;
1494
1495 case WebView.HitTestResult.GEO_TYPE:
1496 menu.setHeaderTitle(extra);
1497 menu.findItem(R.id.map_context_menu_id).setIntent(
1498 new Intent(Intent.ACTION_VIEW, Uri
1499 .parse(WebView.SCHEME_GEO
1500 + URLEncoder.encode(extra))));
1501 menu.findItem(R.id.copy_geo_context_menu_id)
1502 .setOnMenuItemClickListener(
1503 new Copy(extra));
1504 break;
1505
1506 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1507 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001508 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001509 // decide whether to show the open link in new tab option
1510 boolean showNewTab = mTabControl.canCreateNewTab();
1511 MenuItem newTabItem
1512 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001513 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001514 ? R.string.contextmenu_openlink_newwindow_background
1515 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001516 newTabItem.setVisible(showNewTab);
1517 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001518 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1519 newTabItem.setOnMenuItemClickListener(
1520 new MenuItem.OnMenuItemClickListener() {
1521 @Override
1522 public boolean onMenuItemClick(MenuItem item) {
1523 final HashMap<String, WebView> hrefMap =
1524 new HashMap<String, WebView>();
1525 hrefMap.put("webview", webview);
1526 final Message msg = mHandler.obtainMessage(
1527 FOCUS_NODE_HREF,
1528 R.id.open_newtab_context_menu_id,
1529 0, hrefMap);
1530 webview.requestFocusNodeHref(msg);
1531 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001532 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001533 });
1534 } else {
1535 newTabItem.setOnMenuItemClickListener(
1536 new MenuItem.OnMenuItemClickListener() {
1537 @Override
1538 public boolean onMenuItemClick(MenuItem item) {
1539 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001540 openTab(extra, parent,
1541 !mSettings.openInBackground(),
1542 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001543 return true;
1544 }
1545 });
1546 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001547 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001548 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1549 menu.setHeaderTitle(navigationUrl);
1550 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1551
1552 if (itemUrl != null) {
1553 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1554 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1555 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1556 @Override
1557 public boolean onMenuItemClick(MenuItem item) {
1558 final Intent intent = new Intent(Controller.this
1559 .getContext(),
1560 AddMyNavigationPage.class);
1561 Bundle bundle = new Bundle();
1562 String url = Uri.decode(navigationUrl);
1563 bundle.putBoolean("isAdding", false);
1564 bundle.putString("url", url);
1565 bundle.putString("name", getNameFromUrl(url));
1566 intent.putExtra("websites", bundle);
1567 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1568 return false;
1569 }
1570 });
1571 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1572 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1573 @Override
1574 public boolean onMenuItemClick(MenuItem item) {
1575 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1576 return false;
1577 }
1578 });
1579 }
1580 } else {
1581 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1582 }
1583 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001584 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1585 break;
1586 }
1587 // otherwise fall through to handle image part
1588 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001589 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1590 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001591 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1592 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001593 shareItem.setOnMenuItemClickListener(
1594 new MenuItem.OnMenuItemClickListener() {
1595 @Override
1596 public boolean onMenuItemClick(MenuItem item) {
1597 sharePage(mActivity, null, extra, null,
1598 null);
1599 return true;
1600 }
1601 }
1602 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001604 menu.findItem(R.id.view_image_context_menu_id)
1605 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1606 @Override
1607 public boolean onMenuItemClick(MenuItem item) {
1608 openTab(extra, mTabControl.getCurrentTab(), true, true);
1609 return false;
1610 }
1611 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001612 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1613 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1614 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001615 menu.findItem(R.id.set_wallpaper_context_menu_id).
1616 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1617 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 break;
1619
1620 default:
1621 Log.w(LOGTAG, "We should not get here.");
1622 break;
1623 }
1624 //update the ui
1625 mUi.onContextMenuCreated(menu);
1626 }
1627
kaiyiz6e5b3e02013-08-19 20:02:01 +08001628 public void startAddMyNavigation(String url) {
1629 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1630 Bundle bundle = new Bundle();
1631 bundle.putBoolean("isAdding", true);
1632 bundle.putString("url", url);
1633 bundle.putString("name", getNameFromUrl(url));
1634 intent.putExtra("websites", bundle);
1635 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1636 }
1637
1638 private void showMyNavigationDeleteDialog(final String itemUrl) {
1639 new AlertDialog.Builder(this.getContext())
1640 .setTitle(R.string.my_navigation_delete_label)
1641 .setIcon(android.R.drawable.ic_dialog_alert)
1642 .setMessage(R.string.my_navigation_delete_msg)
1643 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1644 @Override
1645 public void onClick(DialogInterface dialog, int whichButton) {
1646 deleteMyNavigationItem(itemUrl);
1647 }
1648 })
1649 .setNegativeButton(R.string.cancel, null)
1650 .show();
1651 }
1652
1653 private void deleteMyNavigationItem(final String itemUrl) {
1654 ContentResolver cr = this.getContext().getContentResolver();
1655 Cursor cursor = null;
1656
1657 try {
1658 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1659 new String[] {
1660 MyNavigationUtil.ID
1661 }, "url = ?", new String[] {
1662 itemUrl
1663 }, null);
1664 if (null != cursor && cursor.moveToFirst()) {
1665 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1666 cursor.getLong(0));
1667
1668 ContentValues values = new ContentValues();
1669 values.put(MyNavigationUtil.TITLE, "");
1670 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1671 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1672 ByteArrayOutputStream os = new ByteArrayOutputStream();
1673 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1674 R.raw.my_navigation_add);
1675 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1676 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1677 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1678 cr.update(uri, values, null, null);
1679 } else {
1680 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1681 }
1682 } catch (IllegalStateException e) {
1683 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1684 } finally {
1685 if (null != cursor) {
1686 cursor.close();
1687 }
1688 }
1689
1690 if (getCurrentTopWebView() != null) {
1691 getCurrentTopWebView().reload();
1692 }
1693 }
1694
1695 private String getNameFromUrl(String itemUrl) {
1696 ContentResolver cr = this.getContext().getContentResolver();
1697 Cursor cursor = null;
1698 String name = null;
1699
1700 try {
1701 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1702 new String[] {
1703 MyNavigationUtil.TITLE
1704 }, "url = ?", new String[] {
1705 itemUrl
1706 }, null);
1707 if (null != cursor && cursor.moveToFirst()) {
1708 name = cursor.getString(0);
1709 } else {
1710 Log.e(LOGTAG, "this item does not exist!");
1711 }
1712 } catch (IllegalStateException e) {
1713 Log.e(LOGTAG, "getNameFromUrl", e);
1714 } finally {
1715 if (null != cursor) {
1716 cursor.close();
1717 }
1718 }
1719 return name;
1720 }
1721
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001722 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
1723 if (bitmap == null) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001724 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1725 return;
1726 }
1727
1728 final ContentResolver cr = mActivity.getContentResolver();
1729 new AsyncTask<Void, Void, Void>() {
1730 @Override
1731 protected Void doInBackground(Void... unused) {
1732 ContentResolver cr = mActivity.getContentResolver();
1733 Cursor cursor = null;
1734 try {
1735 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1736 new String[] {
1737 MyNavigationUtil.ID
1738 }, "url = ?", new String[] {
1739 itemUrl
1740 }, null);
1741 if (null != cursor && cursor.moveToFirst()) {
1742 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001743 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001744
1745 ContentValues values = new ContentValues();
1746 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1747 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1748 cursor.getLong(0));
1749 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1750 cr.update(uri, values, null, null);
1751 os.close();
1752 }
1753 } catch (IllegalStateException e) {
1754 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1755 } catch (IOException e) {
1756 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1757 } finally {
1758 if (null != cursor) {
1759 cursor.close();
1760 }
1761 }
1762 return null;
1763 }
1764 }.execute();
1765 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001766 /**
1767 * As the menu can be open when loading state changes
1768 * we must manually update the state of the stop/reload menu
1769 * item
1770 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001771 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001772 if (menu == null) {
1773 return;
1774 }
1775 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001776 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001777 menu.findItem(R.id.stop_menu_id):
1778 menu.findItem(R.id.reload_menu_id);
1779 if (src != null) {
1780 dest.setIcon(src.getIcon());
1781 dest.setTitle(src.getTitle());
1782 }
1783 }
1784
John Reck9c35b9c2012-05-30 10:08:50 -07001785 @Override
1786 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001787 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001788 // hold on to the menu reference here; it is used by the page callbacks
1789 // to update the menu based on loading state
1790 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001791 // Note: setVisible will decide whether an item is visible; while
1792 // setEnabled() will decide whether an item is enabled, which also means
1793 // whether the matching shortcut key will function.
1794 switch (mMenuState) {
1795 case EMPTY_MENU:
1796 if (mCurrentMenuState != mMenuState) {
1797 menu.setGroupVisible(R.id.MAIN_MENU, false);
1798 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1799 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1800 }
1801 break;
1802 default:
1803 if (mCurrentMenuState != mMenuState) {
1804 menu.setGroupVisible(R.id.MAIN_MENU, true);
1805 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1806 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1807 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001808 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001809 break;
1810 }
1811 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001812 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001813 }
1814
Michael Kolb4bf79712011-07-14 14:18:12 -07001815 @Override
1816 public void updateMenuState(Tab tab, Menu menu) {
1817 boolean canGoBack = false;
1818 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001819 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001820 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001821 // Following flag is used to identify schemes for which the LIVE_MENU
1822 // items defined in res/menu/browser.xml should be enabled
1823 boolean isLiveScheme = false;
1824 boolean isPageFinished = false;
Axesh R. Ajmera4328e0a2014-10-30 16:07:54 -07001825 boolean isSavable = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001826 if (tab != null) {
1827 canGoBack = tab.canGoBack();
1828 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001829 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001830 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001831 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
1832 isPageFinished = tab.getPageFinishedStatus();
Axesh R. Ajmera4328e0a2014-10-30 16:07:54 -07001833 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07001834 }
1835 final MenuItem back = menu.findItem(R.id.back_menu_id);
1836 back.setEnabled(canGoBack);
1837
1838 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001839
1840 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1841 forward.setEnabled(canGoForward);
1842
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001843 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1844 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001845 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001846 if (source != null && dest != null) {
1847 dest.setTitle(source.getTitle());
1848 dest.setIcon(source.getIcon());
1849 }
John Recke1a03a32011-09-14 17:04:16 -07001850 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001851
1852 // decide whether to show the share link option
1853 PackageManager pm = mActivity.getPackageManager();
1854 Intent send = new Intent(Intent.ACTION_SEND);
1855 send.setType("text/plain");
1856 ResolveInfo ri = pm.resolveActivity(send,
1857 PackageManager.MATCH_DEFAULT_ONLY);
1858 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1859
1860 boolean isNavDump = mSettings.enableNavDump();
1861 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1862 nav.setVisible(isNavDump);
1863 nav.setEnabled(isNavDump);
1864
1865 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001866 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1867 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001868 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001869 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Axesh R. Ajmera4328e0a2014-10-30 16:07:54 -07001870 menu.setGroupEnabled(R.id.OFFLINE_READING, isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001871 // history and snapshots item are the members of COMBO menu group,
1872 // so if show history item, only make snapshots item invisible.
1873 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001874
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001875 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001876 }
1877
John Reck9c35b9c2012-05-30 10:08:50 -07001878 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001879 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001880 if (null == getCurrentTopWebView()) {
1881 return false;
1882 }
1883 if (mMenuIsDown) {
1884 // The shortcut action consumes the MENU. Even if it is still down,
1885 // it won't trigger the next shortcut action. In the case of the
1886 // shortcut action triggering a new activity, like Bookmarks, we
1887 // won't get onKeyUp for MENU. So it is important to reset it here.
1888 mMenuIsDown = false;
1889 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001890 if (mUi.onOptionsItemSelected(item)) {
1891 // ui callback handled it
1892 return true;
1893 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001894 switch (item.getItemId()) {
1895 // -- Main menu
1896 case R.id.new_tab_menu_id:
1897 openTabToHomePage();
1898 break;
1899
1900 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001901 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001902 break;
1903
Afzal Najamd4e33312012-04-26 01:54:01 -04001904 case R.id.close_other_tabs_id:
1905 closeOtherTabs();
1906 break;
1907
Michael Kolb8233fac2010-10-26 16:08:53 -07001908 case R.id.goto_menu_id:
1909 editUrl();
1910 break;
1911
1912 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001913 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1914 break;
1915
1916 case R.id.history_menu_id:
1917 bookmarksOrHistoryPicker(ComboViews.History);
1918 break;
1919
1920 case R.id.snapshots_menu_id:
1921 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001922 break;
1923
Michael Kolb8233fac2010-10-26 16:08:53 -07001924 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001925 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001926 break;
1927
1928 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001929 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001930 stopLoading();
1931 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001932 Tab currentTab = mTabControl.getCurrentTab();
1933 if (currentTab.hasCrashed) {
1934 currentTab.replaceCrashView(getCurrentTopWebView(),
1935 currentTab.getViewContainer());
1936 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001937 getCurrentTopWebView().reload();
1938 }
1939 break;
1940
1941 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001942 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001943 break;
1944
1945 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001946 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001947 break;
1948
1949 case R.id.close_menu_id:
1950 // Close the subwindow if it exists.
1951 if (mTabControl.getCurrentSubWindow() != null) {
1952 dismissSubWindow(mTabControl.getCurrentTab());
1953 break;
1954 }
1955 closeCurrentTab();
1956 break;
1957
kaiyiza8b6dbb2013-07-29 18:11:22 +08001958 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001959 Object[] params = { new String("persist.debug.browsermonkeytest")};
1960 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001961 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001962 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001963 if (ret != null && ret.equals("enable"))
1964 break;
luxiaolb40014b2013-07-19 10:01:43 +08001965 showExitDialog(mActivity);
1966 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 case R.id.homepage_menu_id:
1968 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001969 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001970 break;
1971
1972 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001973 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001974 break;
1975
1976 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001977 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001978 break;
1979
John Reck2bc80422011-06-30 15:11:49 -07001980 case R.id.save_snapshot_menu_id:
1981 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001982 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001983 createScreenshotAsync(
1984 source.getWebView(),
1985 getDesiredThumbnailWidth(mActivity),
1986 getDesiredThumbnailHeight(mActivity),
1987 new ValueCallback<Bitmap>() {
1988 @Override
1989 public void onReceiveValue(Bitmap bitmap) {
1990 new SaveSnapshotTask(source, bitmap).execute();
1991 }
1992 });
Leon Scrogginsac993842011-02-02 12:54:07 -05001993 break;
1994
Michael Kolb8233fac2010-10-26 16:08:53 -07001995 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001996 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001997 break;
1998
John Recke1a03a32011-09-14 17:04:16 -07001999 case R.id.snapshot_go_live:
2000 goLive();
2001 return true;
2002
Michael Kolb8233fac2010-10-26 16:08:53 -07002003 case R.id.share_page_menu_id:
2004 Tab currentTab = mTabControl.getCurrentTab();
2005 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002006 return false;
2007 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002008 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002009 break;
2010
2011 case R.id.dump_nav_menu_id:
2012 getCurrentTopWebView().debugDump();
2013 break;
2014
Michael Kolb8233fac2010-10-26 16:08:53 -07002015 case R.id.zoom_in_menu_id:
2016 getCurrentTopWebView().zoomIn();
2017 break;
2018
2019 case R.id.zoom_out_menu_id:
2020 getCurrentTopWebView().zoomOut();
2021 break;
2022
2023 case R.id.view_downloads_menu_id:
2024 viewDownloads();
2025 break;
2026
John Reck42229bc2011-08-19 13:26:43 -07002027 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002028 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002029 break;
2030
Michael Kolb8233fac2010-10-26 16:08:53 -07002031 case R.id.window_one_menu_id:
2032 case R.id.window_two_menu_id:
2033 case R.id.window_three_menu_id:
2034 case R.id.window_four_menu_id:
2035 case R.id.window_five_menu_id:
2036 case R.id.window_six_menu_id:
2037 case R.id.window_seven_menu_id:
2038 case R.id.window_eight_menu_id:
2039 {
2040 int menuid = item.getItemId();
2041 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2042 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2043 Tab desiredTab = mTabControl.getTab(id);
2044 if (desiredTab != null &&
2045 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002046 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002047 }
2048 break;
2049 }
2050 }
2051 }
2052 break;
2053
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002054 case R.id.about_menu_id:
2055 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2056 builder.setTitle(R.string.about);
2057 builder.setCancelable(true);
2058 String ua = "";
2059 final WebView currentWebView = getCurrentWebView();
2060 if (currentWebView != null) {
2061 final WebSettings s = currentWebView.getSettings();
2062 if (s != null) {
2063 ua = s.getUserAgentString();
2064 }
2065 }
2066 builder.setMessage("Agent:" + ua);
2067 builder.setPositiveButton(android.R.string.ok, null);
2068 builder.create().show();
2069 break;
2070
Michael Kolb8233fac2010-10-26 16:08:53 -07002071 default:
2072 return false;
2073 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002074 return true;
2075 }
2076
John Reck68234a92012-04-19 15:27:12 -07002077 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2078 implements OnCancelListener {
2079
2080 private Tab mTab;
2081 private Dialog mProgressDialog;
2082 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002083 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002084
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002085 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002086 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002087 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002088 }
2089
2090 @Override
2091 protected void onPreExecute() {
2092 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2093 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2094 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002095 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002096 }
2097
2098 @Override
2099 protected Long doInBackground(Void... params) {
2100 if (!mTab.saveViewState(mValues)) {
2101 return null;
2102 }
2103 if (isCancelled()) {
2104 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2105 File file = mActivity.getFileStreamPath(path);
2106 if (!file.delete()) {
2107 file.deleteOnExit();
2108 }
2109 return null;
2110 }
2111 final ContentResolver cr = mActivity.getContentResolver();
2112 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2113 if (result == null) {
2114 return null;
2115 }
2116 long id = ContentUris.parseId(result);
2117 return id;
2118 }
2119
2120 @Override
2121 protected void onPostExecute(Long id) {
2122 if (isCancelled()) {
2123 return;
2124 }
2125 mProgressDialog.dismiss();
2126 if (id == null) {
2127 Toast.makeText(mActivity, R.string.snapshot_failed,
2128 Toast.LENGTH_SHORT).show();
2129 return;
2130 }
2131 Bundle b = new Bundle();
2132 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2133 mUi.showComboView(ComboViews.Snapshots, b);
2134 }
2135
2136 @Override
2137 public void onCancel(DialogInterface dialog) {
2138 cancel(true);
2139 }
2140 }
2141
Michael Kolb80f75082012-04-10 10:50:06 -07002142 @Override
2143 public void toggleUserAgent() {
2144 WebView web = getCurrentWebView();
2145 mSettings.toggleDesktopUseragent(web);
Michael Kolb80f75082012-04-10 10:50:06 -07002146 }
2147
2148 @Override
2149 public void findOnPage() {
2150 getCurrentTopWebView().showFindDialog(null, true);
2151 }
2152
2153 @Override
2154 public void openPreferences() {
2155 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2156 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2157 getCurrentTopWebView().getUrl());
2158 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2159 }
2160
2161 @Override
2162 public void bookmarkCurrentPage() {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002163 WebView w = getCurrentTopWebView();
2164 if (w == null)
2165 return;
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002166 final Intent i = createBookmarkCurrentPageIntent(false);
2167 createScreenshotAsync(
2168 w, getDesiredThumbnailWidth(mActivity),
2169 getDesiredThumbnailHeight(mActivity),
2170 new ValueCallback<Bitmap>() {
2171 @Override
2172 public void onReceiveValue(Bitmap bitmap) {
2173 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, bitmap);
2174 mActivity.startActivity(i);
2175 }
2176 });
Michael Kolb80f75082012-04-10 10:50:06 -07002177 }
2178
John Recke1a03a32011-09-14 17:04:16 -07002179 private void goLive() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002180 SnapshotTab t = (SnapshotTab) getCurrentTab();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002181 String url = t.getLiveUrl();
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002182 boolean onlySingleTabRemaining = false;
2183 if (mTabControl.getTabCount() > 1) {
2184 // destroy the old snapshot tab
2185 closeCurrentTab();
2186 } else {
2187 onlySingleTabRemaining = true;
2188 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002189 Tab liveTab = createNewTab(false, true, false);
Axesh R. Ajmera8b3ed5d2014-10-22 16:35:25 -07002190 if (onlySingleTabRemaining) {
2191 closeTab(t);
2192 }
2193
Tarun Nainani8eb00912014-07-17 12:28:32 -07002194 loadUrl(liveTab, url);
John Recke1a03a32011-09-14 17:04:16 -07002195 }
2196
luxiaolb40014b2013-07-19 10:01:43 +08002197 private void showExitDialog(final Activity activity) {
2198 new AlertDialog.Builder(activity)
2199 .setTitle(R.string.exit_browser_title)
2200 .setIcon(android.R.drawable.ic_dialog_alert)
2201 .setMessage(R.string.exit_browser_msg)
2202 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2203 public void onClick(DialogInterface dialog, int which) {
2204 activity.moveTaskToBack(true);
2205 dialog.dismiss();
2206 }
2207 })
2208 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2209 public void onClick(DialogInterface dialog, int which) {
2210 activity.finish();
2211 mHandler.postDelayed(new Runnable() {
2212 @Override
2213 public void run() {
2214 // TODO Auto-generated method stub
2215 mCrashRecoveryHandler.clearState(true);
2216 int pid = android.os.Process.myPid();
2217 android.os.Process.killProcess(pid);
2218 }
2219 }, 300);
2220 dialog.dismiss();
2221 }
2222 })
2223 .show();
2224 }
Michael Kolb315d5022011-10-13 12:47:11 -07002225 @Override
2226 public void showPageInfo() {
2227 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2228 }
2229
John Reck9c35b9c2012-05-30 10:08:50 -07002230 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002231 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002232 // Let the History and Bookmark fragments handle menus they created.
2233 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2234 return false;
2235 }
2236
Michael Kolb8233fac2010-10-26 16:08:53 -07002237 int id = item.getItemId();
2238 boolean result = true;
2239 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002240 // -- Browser context menu
2241 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002242 case R.id.save_link_context_menu_id:
Tarun Nainani700b69b2014-03-26 16:07:25 -07002243 case R.id.save_link_bookmark_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002244 case R.id.copy_link_context_menu_id:
2245 final WebView webView = getCurrentTopWebView();
2246 if (null == webView) {
2247 result = false;
2248 break;
2249 }
2250 final HashMap<String, WebView> hrefMap =
2251 new HashMap<String, WebView>();
2252 hrefMap.put("webview", webView);
2253 final Message msg = mHandler.obtainMessage(
2254 FOCUS_NODE_HREF, id, 0, hrefMap);
2255 webView.requestFocusNodeHref(msg);
2256 break;
2257
2258 default:
2259 // For other context menus
2260 result = onOptionsItemSelected(item);
2261 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002262 return result;
2263 }
2264
2265 /**
2266 * support programmatically opening the context menu
2267 */
2268 public void openContextMenu(View view) {
2269 mActivity.openContextMenu(view);
2270 }
2271
2272 /**
2273 * programmatically open the options menu
2274 */
2275 public void openOptionsMenu() {
2276 mActivity.openOptionsMenu();
2277 }
2278
John Reck9c35b9c2012-05-30 10:08:50 -07002279 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002280 public boolean onMenuOpened(int featureId, Menu menu) {
2281 if (mOptionsMenuOpen) {
2282 if (mConfigChanged) {
2283 // We do not need to make any changes to the state of the
2284 // title bar, since the only thing that happened was a
2285 // change in orientation
2286 mConfigChanged = false;
2287 } else {
2288 if (!mExtendedMenuOpen) {
2289 mExtendedMenuOpen = true;
2290 mUi.onExtendedMenuOpened();
2291 } else {
2292 // Switching the menu back to icon view, so show the
2293 // title bar once again.
2294 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002295 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002296 }
2297 }
2298 } else {
2299 // The options menu is closed, so open it, and show the title
2300 mOptionsMenuOpen = true;
2301 mConfigChanged = false;
2302 mExtendedMenuOpen = false;
2303 mUi.onOptionsMenuOpened();
2304 }
2305 return true;
2306 }
2307
John Reck9c35b9c2012-05-30 10:08:50 -07002308 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002309 public void onOptionsMenuClosed(Menu menu) {
2310 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002311 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002312 }
2313
John Reck9c35b9c2012-05-30 10:08:50 -07002314 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002315 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002316 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002317 }
2318
2319 // Helper method for getting the top window.
2320 @Override
2321 public WebView getCurrentTopWebView() {
2322 return mTabControl.getCurrentTopWebView();
2323 }
2324
2325 @Override
2326 public WebView getCurrentWebView() {
2327 return mTabControl.getCurrentWebView();
2328 }
2329
2330 /*
2331 * This method is called as a result of the user selecting the options
2332 * menu to see the download window. It shows the download window on top of
2333 * the current window.
2334 */
2335 void viewDownloads() {
2336 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2337 mActivity.startActivity(intent);
2338 }
2339
John Reck30b065e2011-07-19 10:58:05 -07002340 int getActionModeHeight() {
2341 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2342 new int[] { android.R.attr.actionBarSize });
2343 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2344 actionBarSizeTypedArray.recycle();
2345 return size;
2346 }
2347
Michael Kolb8233fac2010-10-26 16:08:53 -07002348 // action mode
2349
John Reck9c35b9c2012-05-30 10:08:50 -07002350 @Override
2351 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002352 mUi.onActionModeStarted(mode);
2353 mActionMode = mode;
2354 }
2355
2356 /*
2357 * True if a custom ActionMode (i.e. find or select) is in use.
2358 */
2359 @Override
2360 public boolean isInCustomActionMode() {
2361 return mActionMode != null;
2362 }
2363
2364 /*
2365 * End the current ActionMode.
2366 */
2367 @Override
2368 public void endActionMode() {
2369 if (mActionMode != null) {
2370 mActionMode.finish();
2371 }
2372 }
2373
2374 /*
2375 * Called by find and select when they are finished. Replace title bars
2376 * as necessary.
2377 */
John Reck9c35b9c2012-05-30 10:08:50 -07002378 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002379 public void onActionModeFinished(ActionMode mode) {
2380 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002381 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002382 mActionMode = null;
2383 }
2384
2385 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002386 final Tab tab = getCurrentTab();
2387 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002388 }
2389
2390 // bookmark handling
2391
2392 /**
2393 * add the current page as a bookmark to the given folder id
2394 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002395 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002396 * bookmarked, and if it is, edit that bookmark. If false, and
2397 * the site is already bookmarked, do not attempt to edit the
2398 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002399 */
2400 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002401 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002402 WebView w = getCurrentTopWebView();
2403 if (w == null) {
2404 return null;
2405 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002406 Intent i = new Intent(mActivity,
2407 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002408 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2409 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2410 String touchIconUrl = w.getTouchIconUrl();
2411 if (touchIconUrl != null) {
2412 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2413 WebSettings settings = w.getSettings();
2414 if (settings != null) {
2415 i.putExtra(AddBookmarkPage.USER_AGENT,
2416 settings.getUserAgentString());
2417 }
2418 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002419 //SWE: Thumbnail will need to be set asynchronously
Michael Kolb8233fac2010-10-26 16:08:53 -07002420 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002421 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002422 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2423 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002424 // Put the dialog at the upper right of the screen, covering the
2425 // star on the title bar.
2426 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002427 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002428 }
2429
2430 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002431 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002432 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002434 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002435 }
2436
Vivek Sekharb54614f2014-05-01 19:03:37 -07002437 @Override
2438 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
2439 boolean capture) {
2440 mUploadHandler = new UploadHandler(this);
2441 mUploadHandler.showFileChooser(uploadFilePaths, acceptTypes, capture);
2442 }
2443
Michael Kolb8233fac2010-10-26 16:08:53 -07002444 // thumbnails
2445
2446 /**
2447 * Return the desired width for thumbnail screenshots, which are stored in
2448 * the database, and used on the bookmarks screen.
2449 * @param context Context for finding out the density of the screen.
2450 * @return desired width for thumbnail screenshot.
2451 */
2452 static int getDesiredThumbnailWidth(Context context) {
2453 return context.getResources().getDimensionPixelOffset(
2454 R.dimen.bookmarkThumbnailWidth);
2455 }
2456
2457 /**
2458 * Return the desired height for thumbnail screenshots, which are stored in
2459 * the database, and used on the bookmarks screen.
2460 * @param context Context for finding out the density of the screen.
2461 * @return desired height for thumbnail screenshot.
2462 */
2463 static int getDesiredThumbnailHeight(Context context) {
2464 return context.getResources().getDimensionPixelOffset(
2465 R.dimen.bookmarkThumbnailHeight);
2466 }
2467
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002468 static void createScreenshotAsync(WebView view, int width, int height,
2469 final ValueCallback<Bitmap> cb) {
2470 if (view == null || width == 0 || height == 0) {
2471 return;
2472 }
2473
2474 view.getContentBitmapAsync(
2475 (float) width / view.getWidth(),
2476 new Rect(),
2477 new ValueCallback<Bitmap>() {
2478 @Override
2479 public void onReceiveValue(Bitmap bitmap) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002480 if (bitmap != null)
2481 bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
2482 cb.onReceiveValue(bitmap);
2483 }});
2484 }
2485
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002486 private void updateScreenshot(final Tab tab) {
2487 createScreenshotAsync(
2488 tab.getWebView(),
2489 getDesiredThumbnailWidth(mActivity),
2490 getDesiredThumbnailHeight(mActivity),
2491 new ValueCallback<Bitmap>() {
2492 @Override
2493 public void onReceiveValue(Bitmap bitmap) {
2494 updateScreenshot(tab, bitmap);
2495 }
2496 });
2497 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002498
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002499 private void updateScreenshot(Tab tab, final Bitmap bm) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002500 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002501 // FIXME: Would like to make sure there is actually something to
2502 // draw, but the API for that (WebViewCore.pictureReady()) is not
2503 // currently accessible here.
2504
John Reck34ef2672011-02-10 11:30:55 -08002505 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002506 if (view == null) {
2507 // Tab was destroyed
2508 return;
2509 }
John Reck34ef2672011-02-10 11:30:55 -08002510 final String url = tab.getUrl();
2511 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002512 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002513 if (TextUtils.isEmpty(url)) {
2514 return;
2515 }
2516
kaiyiz6e5b3e02013-08-19 20:02:01 +08002517 //update My Navigation Thumbnails
2518 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2519 if (isMyNavigationUrl) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002520 updateMyNavigationThumbnail(url, bm);
kaiyiz6e5b3e02013-08-19 20:02:01 +08002521 }
John Reck34ef2672011-02-10 11:30:55 -08002522 // Only update thumbnails for web urls (http(s)://), not for
2523 // about:, javascript:, data:, etc...
2524 // Unless it is a bookmarked site, then always update
2525 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2526 return;
2527 }
2528
kaiyiz6e5b3e02013-08-19 20:02:01 +08002529 if (url != null && mUpdateMyNavThumbnailUrl != null
2530 && Patterns.WEB_URL.matcher(url).matches()
2531 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2532 String urlHost = (new WebAddress(url)).getHost();
2533 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2534 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2535 || bookmarkHost.length() == 0) {
2536 return;
2537 }
2538 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2539 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2540 bookmarkHost.length());
2541 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2542 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2543 if (!bookmarkDomain.equals(urlDomain)) {
2544 return;
2545 }
2546 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002547 if (bm == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002548 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
2549 mHandler.sendMessageDelayed(mHandler.obtainMessage(
2550 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
2551 500);
2552 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002553 return;
2554 }
2555
2556 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002557 new AsyncTask<Void, Void, Void>() {
2558 @Override
2559 protected Void doInBackground(Void... unused) {
2560 Cursor cursor = null;
2561 try {
2562 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002563 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2564 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2565 : url);
2566 if (mUpdateMyNavThumbnail) {
2567 mUpdateMyNavThumbnail = false;
2568 mUpdateMyNavThumbnailUrl = null;
2569 }
John Reck34ef2672011-02-10 11:30:55 -08002570 if (cursor != null && cursor.moveToFirst()) {
2571 final ByteArrayOutputStream os =
2572 new ByteArrayOutputStream();
2573 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002574
John Reck34ef2672011-02-10 11:30:55 -08002575 ContentValues values = new ContentValues();
2576 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002577
John Reck34ef2672011-02-10 11:30:55 -08002578 do {
John Reck617fd832011-02-16 14:35:59 -08002579 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002580 cr.update(Images.CONTENT_URI, values, null, null);
2581 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002582 }
John Reck34ef2672011-02-10 11:30:55 -08002583 } catch (IllegalStateException e) {
2584 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002585 } catch (SQLiteException s) {
2586 // Added for possible error when user tries to remove the same bookmark
2587 // that is being updated with a screen shot
2588 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002589 } finally {
2590 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002591 }
John Reck34ef2672011-02-10 11:30:55 -08002592 return null;
2593 }
2594 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002595 }
2596
2597 private class Copy implements OnMenuItemClickListener {
2598 private CharSequence mText;
2599
John Reck9c35b9c2012-05-30 10:08:50 -07002600 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002601 public boolean onMenuItemClick(MenuItem item) {
2602 copy(mText);
2603 return true;
2604 }
2605
2606 public Copy(CharSequence toCopy) {
2607 mText = toCopy;
2608 }
2609 }
2610
Leon Scroggins63c02662010-11-18 15:16:27 -05002611 private static class Download implements OnMenuItemClickListener {
2612 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002613 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002614 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002615 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002616 private static final String FALLBACK_EXTENSION = "dat";
2617 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002618
John Reck9c35b9c2012-05-30 10:08:50 -07002619 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002620 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002621 if (DataUri.isDataUri(mText)) {
2622 saveDataUri();
2623 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002624 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002625 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002626 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002627 return true;
2628 }
2629
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002630 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2631 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002632 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002633 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002634 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002635 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002636 }
George Mount387d45d2011-10-07 15:57:53 -07002637
2638 /**
2639 * Treats mText as a data URI and writes its contents to a file
2640 * based on the current time.
2641 */
2642 private void saveDataUri() {
2643 FileOutputStream outputStream = null;
2644 try {
2645 DataUri uri = new DataUri(mText);
2646 File target = getTarget(uri);
2647 outputStream = new FileOutputStream(target);
2648 outputStream.write(uri.getData());
2649 final DownloadManager manager =
2650 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2651 manager.addCompletedDownload(target.getName(),
2652 mActivity.getTitle().toString(), false,
2653 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002654 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002655 } catch (IOException e) {
2656 Log.e(LOGTAG, "Could not save data URL");
2657 } finally {
2658 if (outputStream != null) {
2659 try {
2660 outputStream.close();
2661 } catch (IOException e) {
2662 // ignore close errors
2663 }
2664 }
2665 }
2666 }
2667
2668 /**
2669 * Creates a File based on the current time stamp and uses
2670 * the mime type of the DataUri to get the extension.
2671 */
2672 private File getTarget(DataUri uri) throws IOException {
2673 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002674 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002675 String nameBase = format.format(new Date());
2676 String mimeType = uri.getMimeType();
2677 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2678 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2679 if (extension == null) {
2680 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2681 extension = FALLBACK_EXTENSION;
2682 }
2683 extension = "." + extension; // createTempFile needs the '.'
2684 File targetFile = File.createTempFile(nameBase, extension, dir);
2685 return targetFile;
2686 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002687 }
2688
Cary Clark8974d282010-11-22 10:46:05 -05002689 private static class SelectText implements OnMenuItemClickListener {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002690 private WebView mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002691
John Reck9c35b9c2012-05-30 10:08:50 -07002692 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002693 public boolean onMenuItemClick(MenuItem item) {
2694 if (mWebView != null) {
2695 return mWebView.selectText();
2696 }
2697 return false;
2698 }
2699
2700 public SelectText(WebView webView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002701 mWebView = webView;
Cary Clark8974d282010-11-22 10:46:05 -05002702 }
2703
2704 }
2705
Michael Kolb8233fac2010-10-26 16:08:53 -07002706 /********************** TODO: UI stuff *****************************/
2707
2708 // these methods have been copied, they still need to be cleaned up
2709
2710 /****************** tabs ***************************************************/
2711
2712 // basic tab interactions:
2713
2714 // it is assumed that tabcontrol already knows about the tab
2715 protected void addTab(Tab tab) {
2716 mUi.addTab(tab);
2717 }
2718
2719 protected void removeTab(Tab tab) {
2720 mUi.removeTab(tab);
2721 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002722 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002723 }
2724
Michael Kolbf2055602011-04-09 17:20:03 -07002725 @Override
2726 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002727 // monkey protection against delayed start
2728 if (tab != null) {
2729 mTabControl.setCurrentTab(tab);
2730 // the tab is guaranteed to have a webview after setCurrentTab
2731 mUi.setActiveTab(tab);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002732 tab.setTimeStamp();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002733 //Purge active tabs
2734 MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
Michael Kolbcd424e92011-02-24 10:26:26 -08002735 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002736 }
2737
John Reck8bcafc12011-08-29 16:43:02 -07002738 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002739 Tab current = mTabControl.getCurrentTab();
2740 if (current != null
2741 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002742 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002743 }
2744 }
2745
John Reck26b18322011-06-21 13:08:58 -07002746 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002747 // Dismiss the subwindow if applicable.
2748 dismissSubWindow(appTab);
2749 // Since we might kill the WebView, remove it from the
2750 // content view first.
2751 mUi.detachTab(appTab);
2752 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002753 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002754 // TODO: analyze why the remove and add are necessary
2755 mUi.attachTab(appTab);
2756 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002757 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002758 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002759 } else {
2760 // If the tab was the current tab, we have to attach
2761 // it to the view system again.
2762 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002763 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002764 }
2765 }
2766
2767 // Remove the sub window if it exists. Also called by TabControl when the
2768 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002769 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002770 public void dismissSubWindow(Tab tab) {
2771 removeSubWindow(tab);
2772 // dismiss the subwindow. This will destroy the WebView.
2773 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002774 WebView wv = getCurrentTopWebView();
2775 if (wv != null) {
2776 wv.requestFocus();
2777 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002778 }
2779
2780 @Override
2781 public void removeSubWindow(Tab t) {
2782 if (t.getSubWebView() != null) {
2783 mUi.removeSubWindow(t.getSubViewContainer());
2784 }
2785 }
2786
2787 @Override
2788 public void attachSubWindow(Tab tab) {
2789 if (tab.getSubWebView() != null) {
2790 mUi.attachSubWindow(tab.getSubViewContainer());
2791 getCurrentTopWebView().requestFocus();
2792 }
2793 }
2794
Mathew Inwood29721c22011-06-29 17:55:24 +01002795 private Tab showPreloadedTab(final UrlData urlData) {
2796 if (!urlData.isPreloaded()) {
2797 return null;
2798 }
2799 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2800 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2801 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002802 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002803 // Could not submit query. Fallback to regular tab creation
2804 tabControl.destroy();
2805 return null;
2806 }
2807 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002808 // check tab count and make room for new tab
2809 if (!mTabControl.canCreateNewTab()) {
2810 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2811 if (leastUsed != null) {
2812 closeTab(leastUsed);
2813 }
2814 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002815 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002816 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002817 mTabControl.addPreloadedTab(t);
2818 addTab(t);
2819 setActiveTab(t);
2820 return t;
2821 }
2822
Michael Kolb7bcafde2011-05-09 13:55:59 -07002823 // open a non inconito tab with the given url data
2824 // and set as active tab
2825 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002826 Tab tab = showPreloadedTab(urlData);
2827 if (tab == null) {
2828 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002829 if ((tab != null) && !urlData.isEmpty()) {
2830 loadUrlDataIn(tab, urlData);
2831 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002832 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002833 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002834 }
2835
Michael Kolb843510f2010-12-09 10:51:49 -08002836 @Override
2837 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002838 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002839 }
2840
Michael Kolb8233fac2010-10-26 16:08:53 -07002841 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002842 public Tab openIncognitoTab() {
2843 return openTab(INCOGNITO_URI, true, true, false);
2844 }
2845
2846 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002847 public Tab openTab(String url, boolean incognito, boolean setActive,
2848 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002849 return openTab(url, incognito, setActive, useCurrent, null);
2850 }
2851
2852 @Override
2853 public Tab openTab(String url, Tab parent, boolean setActive,
2854 boolean useCurrent) {
2855 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2856 setActive, useCurrent, parent);
2857 }
2858
2859 public Tab openTab(String url, boolean incognito, boolean setActive,
2860 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002861 Tab tab = createNewTab(incognito, setActive, useCurrent);
2862 if (tab != null) {
Vivek Sekhar871172e2014-07-23 12:16:54 -07002863 if (parent instanceof SnapshotTab) {
2864 addTab(tab);
2865 if (setActive)
2866 setActiveTab(tab);
2867 }else if (parent != null && parent != tab) {
John Reck5949c662011-05-27 09:52:29 -07002868 parent.addChildTab(tab);
2869 }
Michael Kolb519d2282011-05-09 17:03:19 -07002870 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002871 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002872 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002873 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002874 return tab;
2875 }
2876
2877 // this method will attempt to create a new tab
2878 // incognito: private browsing tab
2879 // setActive: ste tab as current tab
2880 // useCurrent: if no new tab can be created, return current tab
2881 private Tab createNewTab(boolean incognito, boolean setActive,
2882 boolean useCurrent) {
2883 Tab tab = null;
2884 if (mTabControl.canCreateNewTab()) {
2885 tab = mTabControl.createNewTab(incognito);
2886 addTab(tab);
2887 if (setActive) {
2888 setActiveTab(tab);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002889 } else {
2890 tab.pause();
Michael Kolb7bcafde2011-05-09 13:55:59 -07002891 }
2892 } else {
2893 if (useCurrent) {
2894 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002895 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002896 } else {
2897 mUi.showMaxTabsWarning();
2898 }
2899 }
2900 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002901 }
2902
John Reck2bc80422011-06-30 15:11:49 -07002903 @Override
2904 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2905 SnapshotTab tab = null;
2906 if (mTabControl.canCreateNewTab()) {
2907 tab = mTabControl.createSnapshotTab(snapshotId);
2908 addTab(tab);
2909 if (setActive) {
2910 setActiveTab(tab);
2911 }
2912 } else {
2913 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002914 }
2915 return tab;
2916 }
2917
Michael Kolb8233fac2010-10-26 16:08:53 -07002918 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002919 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002920 * @return boolean True if we successfully switched to a different tab. If
2921 * the indexth tab is null, or if that tab is the same as
2922 * the current one, return false.
2923 */
2924 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002925 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002926 Tab currentTab = mTabControl.getCurrentTab();
2927 if (tab == null || tab == currentTab) {
2928 return false;
2929 }
2930 setActiveTab(tab);
2931 return true;
2932 }
2933
2934 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002935 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002936 closeCurrentTab(false);
2937 }
2938
2939 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002940 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002941 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002942 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002943 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002944 return;
2945 }
Michael Kolbc831b632011-05-11 09:30:34 -07002946 final Tab current = mTabControl.getCurrentTab();
2947 final int pos = mTabControl.getCurrentPosition();
2948 Tab newTab = current.getParent();
2949 if (newTab == null) {
2950 newTab = mTabControl.getTab(pos + 1);
2951 if (newTab == null) {
2952 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002953 }
2954 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002955 if (andQuit) {
2956 mTabControl.setCurrentTab(newTab);
2957 closeTab(current);
2958 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002959 // Close window
2960 closeTab(current);
2961 }
2962 }
2963
2964 /**
2965 * Close the tab, remove its associated title bar, and adjust mTabControl's
2966 * current tab to a valid value.
2967 */
2968 @Override
2969 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002970 if (tab == mTabControl.getCurrentTab()) {
2971 closeCurrentTab();
2972 } else {
2973 removeTab(tab);
2974 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002975 }
2976
Afzal Najamd4e33312012-04-26 01:54:01 -04002977 /**
2978 * Close all tabs except the current one
2979 */
2980 @Override
2981 public void closeOtherTabs() {
2982 int inactiveTabs = mTabControl.getTabCount() - 1;
2983 for (int i = inactiveTabs; i >= 0; i--) {
2984 Tab tab = mTabControl.getTab(i);
2985 if (tab != mTabControl.getCurrentTab()) {
2986 removeTab(tab);
2987 }
2988 }
2989 }
2990
Michael Kolb8233fac2010-10-26 16:08:53 -07002991 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002992 protected void loadUrlFromContext(String url) {
2993 Tab tab = getCurrentTab();
2994 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002995 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002996 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002997 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002998 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002999 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07003000 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07003001 }
3002 }
3003 }
3004
3005 /**
3006 * Load the URL into the given WebView and update the title bar
3007 * to reflect the new load. Call this instead of WebView.loadUrl
3008 * directly.
3009 * @param view The WebView used to load url.
3010 * @param url The URL to load.
3011 */
John Reck71e51422011-07-01 16:49:28 -07003012 @Override
3013 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07003014 loadUrl(tab, url, null);
3015 }
3016
3017 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
3018 if (tab != null) {
3019 dismissSubWindow(tab);
Vivek Sekhar0e10a202014-09-12 19:13:23 -07003020 mHomepageHandler.registerJsInterface(tab.getWebView(), url);
John Reck26b18322011-06-21 13:08:58 -07003021 tab.loadUrl(url, headers);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003022 if (tab.hasCrashed) {
3023 tab.replaceCrashView(tab.getWebView(), tab.getViewContainer());
3024 }
Michael Kolba53c9892011-10-05 13:31:40 -07003025 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07003026 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003027 }
3028
3029 /**
3030 * Load UrlData into a Tab and update the title bar to reflect the new
3031 * load. Call this instead of UrlData.loadIn directly.
3032 * @param t The Tab used to load.
3033 * @param data The UrlData being loaded.
3034 */
3035 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07003036 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07003037 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07003038 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07003039 } else {
John Reck38b39652012-06-05 09:22:59 -07003040 if (t != null && data.mDisableUrlOverride) {
3041 t.disableUrlOverridingForLoad();
3042 }
John Reck26b18322011-06-21 13:08:58 -07003043 loadUrl(t, data.mUrl, data.mHeaders);
3044 }
3045 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003046 }
3047
John Reck30c714c2010-12-16 17:30:34 -08003048 @Override
3049 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003050 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003051 if (tab.canGoBack()) {
3052 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003053 } else {
John Reckef654f12011-07-12 16:42:08 -07003054 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003055 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003056 }
3057
3058 void goBackOnePageOrQuit() {
3059 Tab current = mTabControl.getCurrentTab();
3060 if (current == null) {
3061 /*
3062 * Instead of finishing the activity, simply push this to the back
3063 * of the stack and let ActivityManager to choose the foreground
3064 * activity. As BrowserActivity is singleTask, it will be always the
3065 * root of the task. So we can use either true or false for
3066 * moveTaskToBack().
3067 */
luxiaolb40014b2013-07-19 10:01:43 +08003068 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003069 return;
3070 }
John Reckef654f12011-07-12 16:42:08 -07003071 if (current.canGoBack()) {
3072 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003073 } else {
3074 // Check to see if we are closing a window that was created by
3075 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003076 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003077 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003078 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003079 // Now we close the other tab
3080 closeTab(current);
3081 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003082 /*
3083 * Instead of finishing the activity, simply push this to the back
3084 * of the stack and let ActivityManager to choose the foreground
3085 * activity. As BrowserActivity is singleTask, it will be always the
3086 * root of the task. So we can use either true or false for
3087 * moveTaskToBack().
3088 */
luxiaolb40014b2013-07-19 10:01:43 +08003089 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003090 }
3091 }
3092 }
3093
3094 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003095 * helper method for key handler
3096 * returns the current tab if it can't advance
3097 */
Michael Kolbc831b632011-05-11 09:30:34 -07003098 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003099 int pos = mTabControl.getCurrentPosition() + 1;
3100 if (pos >= mTabControl.getTabCount()) {
3101 pos = 0;
3102 }
3103 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003104 }
3105
3106 /**
3107 * helper method for key handler
3108 * returns the current tab if it can't advance
3109 */
Michael Kolbc831b632011-05-11 09:30:34 -07003110 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003111 int pos = mTabControl.getCurrentPosition() - 1;
3112 if ( pos < 0) {
3113 pos = mTabControl.getTabCount() - 1;
3114 }
3115 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003116 }
3117
John Reckbcef87f2012-02-03 14:58:34 -08003118 boolean isMenuOrCtrlKey(int keyCode) {
3119 return (KeyEvent.KEYCODE_MENU == keyCode)
3120 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3121 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3122 }
3123
Michael Kolb0035fad2011-03-14 13:25:28 -07003124 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003125 * handle key events in browser
3126 *
3127 * @param keyCode
3128 * @param event
3129 * @return true if handled, false to pass to super
3130 */
John Reck9c35b9c2012-05-30 10:08:50 -07003131 @Override
3132 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003133 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003134 // Even if MENU is already held down, we need to call to super to open
3135 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003136 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003137 mMenuIsDown = true;
3138 return false;
3139 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003140
Cary Clark8ff8c662010-12-29 15:03:05 -05003141 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003142 Tab tab = getCurrentTab();
3143 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003144
Cary Clark160bbb92011-01-10 11:17:07 -05003145 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3146 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003147
Michael Kolb8233fac2010-10-26 16:08:53 -07003148 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003149 case KeyEvent.KEYCODE_TAB:
3150 if (event.isCtrlPressed()) {
3151 if (event.isShiftPressed()) {
3152 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003153 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003154 } else {
3155 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003156 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003157 }
3158 return true;
3159 }
3160 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003161 case KeyEvent.KEYCODE_SPACE:
3162 // WebView/WebTextView handle the keys in the KeyDown. As
3163 // the Activity's shortcut keys are only handled when WebView
3164 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003165 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003166 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003167 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003168 pageDown();
3169 }
3170 return true;
3171 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003172 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003173 event.startTracking();
3174 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003175 case KeyEvent.KEYCODE_FORWARD:
3176 if (!noModifiers) break;
3177 tab.goForward();
3178 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003179 case KeyEvent.KEYCODE_DPAD_LEFT:
3180 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003181 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003182 return true;
3183 }
3184 break;
3185 case KeyEvent.KEYCODE_DPAD_RIGHT:
3186 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003187 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003188 return true;
3189 }
3190 break;
3191 case KeyEvent.KEYCODE_A:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003192 if (ctrl) {
3193 webView.selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003194 return true;
3195 }
3196 break;
Michael Kolba4183062011-01-16 10:43:21 -08003197// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003198 case KeyEvent.KEYCODE_C:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003199 if (ctrl ) {
3200 webView.copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003201 return true;
3202 }
3203 break;
Michael Kolba4183062011-01-16 10:43:21 -08003204// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003205// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003206// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003207// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003208// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003209// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003210// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003211// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003212// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003213// case KeyEvent.KEYCODE_M: // unused
3214// case KeyEvent.KEYCODE_N: // in Chrome: new window
3215// case KeyEvent.KEYCODE_O: // in Chrome: open file
3216// case KeyEvent.KEYCODE_P: // in Chrome: print page
3217// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003218// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003219// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3220 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003221 // we can't use the ctrl/shift flags, they check for
3222 // exclusive use of a modifier
3223 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003224 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003225 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003226 } else {
3227 openTabToHomePage();
3228 }
3229 return true;
3230 }
3231 break;
3232// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3233// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003234// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003235// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3236// case KeyEvent.KEYCODE_Y: // unused
3237// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003238 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003239 // it is a regular key and webview is not null
3240 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003241 }
3242
John Reck9c35b9c2012-05-30 10:08:50 -07003243 @Override
3244 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003245 switch(keyCode) {
3246 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003247 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003248 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003249 return true;
3250 }
3251 break;
3252 }
3253 return false;
3254 }
3255
John Reck9c35b9c2012-05-30 10:08:50 -07003256 @Override
3257 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003258 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003259 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003260 if (KeyEvent.KEYCODE_MENU == keyCode
3261 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003262 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003263 }
3264 }
Cary Clark160bbb92011-01-10 11:17:07 -05003265 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003266 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003267 case KeyEvent.KEYCODE_BACK:
3268 if (event.isTracking() && !event.isCanceled()) {
3269 onBackKey();
3270 return true;
3271 }
3272 break;
3273 }
3274 return false;
3275 }
3276
3277 public boolean isMenuDown() {
3278 return mMenuIsDown;
3279 }
3280
John Reck9c35b9c2012-05-30 10:08:50 -07003281 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003282 public void setupAutoFill(Message message) {
3283 // Open the settings activity at the AutoFill profile fragment so that
3284 // the user can create a new profile. When they return, we will dispatch
3285 // the message so that we can autofill the form using their new profile.
3286 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3287 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3288 AutoFillSettingsFragment.class.getName());
3289 mAutoFillSetupMessage = message;
3290 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3291 }
John Reckb3417f02011-01-14 11:01:05 -08003292
John Reck9c35b9c2012-05-30 10:08:50 -07003293 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003294 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003295 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003296 return true;
3297 }
3298
John Reck1cf4b792011-07-26 10:22:22 -07003299 @Override
3300 public boolean shouldCaptureThumbnails() {
3301 return mUi.shouldCaptureThumbnails();
3302 }
3303
Michael Kolbc3af0672011-08-09 10:24:41 -07003304 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003305 public boolean supportsVoice() {
3306 PackageManager pm = mActivity.getPackageManager();
3307 List activities = pm.queryIntentActivities(new Intent(
3308 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3309 return activities.size() != 0;
3310 }
3311
3312 @Override
3313 public void startVoiceRecognizer() {
3314 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08003315 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
Michael Kolb0b129122012-06-04 16:31:58 -07003316 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3317 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3318 mActivity.startActivityForResult(voice, VOICE_RESULT);
3319 }
3320
Pankaj Garg7b279f62014-08-12 14:47:18 -07003321 public void setWindowDimming(float level) {
3322 if (level != 0) {
3323 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
3324 lp.dimAmount = level;
3325 mActivity.getWindow().setAttributes(lp);
3326 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3327 } else {
3328 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
3329 }
3330 }
3331
Michael Kolb0b129122012-06-04 16:31:58 -07003332 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003333 public void setBlockEvents(boolean block) {
3334 mBlockEvents = block;
3335 }
3336
John Reck9c35b9c2012-05-30 10:08:50 -07003337 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003338 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003339 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003340 }
3341
John Reck9c35b9c2012-05-30 10:08:50 -07003342 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003343 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003344 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003345 }
3346
John Reck9c35b9c2012-05-30 10:08:50 -07003347 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003348 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003349 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003350 }
3351
John Reck9c35b9c2012-05-30 10:08:50 -07003352 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003353 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003354 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003355 }
3356
John Reck9c35b9c2012-05-30 10:08:50 -07003357 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003358 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003359 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003360 }
3361
Michael Kolb8233fac2010-10-26 16:08:53 -07003362}