blob: 73940bee268a63aa4a2f1339264de6ea4c186b5c [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolb8233fac2010-10-26 16:08:53 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080020
luxiaolb40014b2013-07-19 10:01:43 +080021import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.ProgressDialog;
Vivek Sekharce2a4832014-03-26 13:26:53 -070025import android.content.Context;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070028import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.content.ContentValues;
John Reck68234a92012-04-19 15:27:12 -070030import android.content.DialogInterface;
31import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070032import android.content.Intent;
33import android.content.pm.PackageManager;
34import android.content.pm.ResolveInfo;
35import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070036import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050037import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.database.Cursor;
39import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020040import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080042import android.graphics.BitmapFactory;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Canvas;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080044import android.graphics.Paint;
45import android.graphics.Rect;
kaiyiza016da12013-08-26 17:50:22 +080046import android.net.ConnectivityManager;
47import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.net.Uri;
49import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080050import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080051import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.os.AsyncTask;
53import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070054import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.os.Handler;
56import android.os.Message;
57import android.os.PowerManager;
58import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.provider.ContactsContract;
61import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080062import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070063import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070064import android.text.TextUtils;
65import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080066import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070067import android.view.ActionMode;
68import android.view.ContextMenu;
69import android.view.ContextMenu.ContextMenuInfo;
70import android.view.Gravity;
71import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070072import android.view.Menu;
73import android.view.MenuInflater;
74import android.view.MenuItem;
75import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070076import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070077import android.view.View;
Pankaj Garg7b279f62014-08-12 14:47:18 -070078import android.view.WindowManager;
George Mount387d45d2011-10-07 15:57:53 -070079import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070080import android.webkit.ValueCallback;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080081import android.webkit.WebChromeClient.CustomViewCallback;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import org.codeaurora.swe.CookieManager;
85import org.codeaurora.swe.CookieSyncManager;
86import org.codeaurora.swe.HttpAuthHandler;
87import org.codeaurora.swe.SslErrorHandler;
88import org.codeaurora.swe.WebSettings;
89import org.codeaurora.swe.WebView;
Vivek Sekhar0e10a202014-09-12 19:13:23 -070090import org.codeaurora.swe.WebBackForwardList;
91import org.codeaurora.swe.WebHistoryItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080092
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070093import com.android.browser.AppAdapter;
Bijan Amirzada41242f22014-03-21 12:12:18 -070094import com.android.browser.R;
95import com.android.browser.IntentHandler.UrlData;
96import com.android.browser.UI.ComboViews;
97import com.android.browser.mynavigation.AddMyNavigationPage;
98import com.android.browser.mynavigation.MyNavigationUtil;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070099import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -0700100import com.android.browser.platformsupport.BrowserContract;
101import com.android.browser.platformsupport.WebAddress;
102import com.android.browser.platformsupport.BrowserContract.Images;
103import com.android.browser.provider.BrowserProvider2.Thumbnails;
104import com.android.browser.provider.SnapshotProvider.Snapshots;
105import com.android.browser.reflect.ReflectHelper;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700106
Michael Kolb8233fac2010-10-26 16:08:53 -0700107import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700108import java.io.File;
109import java.io.FileOutputStream;
110import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700111import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700112import java.text.DateFormat;
113import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700114import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700116import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700117import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800118import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200119import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700120import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700121
122/**
123 * Controller for browser
124 */
125public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700126 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700127
128 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800129 private static final String SEND_APP_ID_EXTRA =
130 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700131 private static final String INCOGNITO_URI = "browser:incognito";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800132
Vivek Sekharb54614f2014-05-01 19:03:37 -0700133 // Remind switch to data connection if wifi is unavailable
134 private static final int NETWORK_SWITCH_TYPE_OK = 1;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800135
Michael Kolb8233fac2010-10-26 16:08:53 -0700136 // public message ids
137 public final static int LOAD_URL = 1001;
138 public final static int STOP_LOAD = 1002;
139
140 // Message Ids
141 private static final int FOCUS_NODE_HREF = 102;
142 private static final int RELEASE_WAKELOCK = 107;
143
144 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
145
146 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800147 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700148
149 private static final int EMPTY_MENU = -1;
150
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700152 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700153 final static int PREFERENCES_PAGE = 3;
154 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000155 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700156 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800157 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000158
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
160
161 // As the ids are dynamically created, we can't guarantee that they will
162 // be in sequence, so this static array maps ids to a window number.
163 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
164 { R.id.window_one_menu_id, R.id.window_two_menu_id,
165 R.id.window_three_menu_id, R.id.window_four_menu_id,
166 R.id.window_five_menu_id, R.id.window_six_menu_id,
167 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
168
169 // "source" parameter for Google search through search key
170 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
171 // "source" parameter for Google search through simplily type
172 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
173
George Mount387d45d2011-10-07 15:57:53 -0700174 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700175 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
176
John Reckd7dd9b22011-08-30 09:18:29 -0700177 // A bitmap that is re-used in createScreenshot as scratch space
178 private static Bitmap sThumbnailBitmap;
179
Michael Kolb8233fac2010-10-26 16:08:53 -0700180 private Activity mActivity;
181 private UI mUi;
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700182 private HomepageHandler mHomepageHandler;
183 protected TabControl mTabControl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700184 private BrowserSettings mSettings;
185 private WebViewFactory mFactory;
186
187 private WakeLock mWakeLock;
188
189 private UrlHandler mUrlHandler;
190 private UploadHandler mUploadHandler;
191 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 private PageDialogsHandler mPageDialogsHandler;
193 private NetworkStateHandler mNetworkHandler;
194
Ben Murdoch8029a772010-11-16 11:58:21 +0000195 private Message mAutoFillSetupMessage;
196
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800198 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700199
Michael Kolb8233fac2010-10-26 16:08:53 -0700200 // FIXME, temp address onPrepareMenu performance problem.
201 // When we move everything out of view, we should rewrite this.
202 private int mCurrentMenuState = 0;
203 private int mMenuState = R.id.MAIN_MENU;
204 private int mOldMenuState = EMPTY_MENU;
205 private Menu mCachedMenu;
206
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 private boolean mMenuIsDown;
208
209 // For select and find, we keep track of the ActionMode so that
210 // finish() can be called as desired.
211 private ActionMode mActionMode;
212
213 /**
214 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
215 * of whether the configuration has changed. The first onMenuOpened call
216 * after a configuration change is simply a reopening of the same menu
217 * (i.e. mIconView did not change).
218 */
219 private boolean mConfigChanged;
220
221 /**
222 * Keeps track of whether the options menu is open. This is important in
223 * determining whether to show or hide the title bar overlay
224 */
225 private boolean mOptionsMenuOpen;
226
227 /**
228 * Whether or not the options menu is in its bigger, popup menu form. When
229 * true, we want the title bar overlay to be gone. When false, we do not.
230 * Only meaningful if mOptionsMenuOpen is true.
231 */
232 private boolean mExtendedMenuOpen;
233
Michael Kolb8233fac2010-10-26 16:08:53 -0700234 private boolean mActivityPaused = true;
235 private boolean mLoadStopped;
236
237 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500238 // Checks to see when the bookmarks database has changed, and updates the
239 // Tabs' notion of whether they represent bookmarked sites.
240 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700241 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700242
Michael Kolbc3af0672011-08-09 10:24:41 -0700243 private boolean mBlockEvents;
244
Michael Kolb0b129122012-06-04 16:31:58 -0700245 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800246 private boolean mUpdateMyNavThumbnail;
247 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700248
George Mount3636d0a2011-11-21 09:08:21 -0800249 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700250 mActivity = browser;
251 mSettings = BrowserSettings.getInstance();
252 mTabControl = new TabControl(this);
253 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700254 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800255 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700256 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700257
258 mUrlHandler = new UrlHandler(this);
259 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
261
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500263 mBookmarksObserver = new ContentObserver(mHandler) {
264 @Override
265 public void onChange(boolean selfChange) {
266 int size = mTabControl.getTabCount();
267 for (int i = 0; i < size; i++) {
268 mTabControl.getTab(i).updateBookmarkedStatus();
269 }
270 }
271
272 };
273 browser.getContentResolver().registerContentObserver(
274 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700275
276 mNetworkHandler = new NetworkStateHandler(mActivity, this);
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700277 mHomepageHandler = new HomepageHandler(browser, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 }
279
John Reck9c35b9c2012-05-30 10:08:50 -0700280 @Override
281 public void start(final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800282 WebView.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800283 // mCrashRecoverHandler has any previously saved state.
284 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700285 }
286
George Mount3636d0a2011-11-21 09:08:21 -0800287 void doStart(final Bundle icicle, final Intent intent) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800288 // we dont want to ever recover incognito tabs
289 final boolean restoreIncognitoTabs = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700290
Patrick Scott7d50a932011-02-04 09:27:26 -0500291 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700292 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500293 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000294
Michael Kolbc831b632011-05-11 09:30:34 -0700295 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500296 // Not able to restore so we go ahead and clear session cookies. We
297 // must do this before trying to login the user as we don't want to
298 // clear any session cookies set during login.
299 CookieManager.getInstance().removeSessionCookie();
John Reck1cf4b792011-07-26 10:22:22 -0700300 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800301 if (intent == null) {
302 // This won't happen under common scenarios. The icicle is
303 // not null, but there aren't any tabs to restore.
304 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700305 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800306 final Bundle extra = intent.getExtras();
307 // Create an initial tab.
308 // If the intent is ACTION_VIEW and data is not null, the Browser is
309 // invoked to view the content by another application. In this case,
310 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800311 UrlData urlData = null;
312 if (intent.getData() != null
313 && Intent.ACTION_VIEW.equals(intent.getAction())
314 && intent.getData().toString().startsWith("content://")) {
315 urlData = new UrlData(intent.getData().toString());
316 } else {
317 urlData = IntentHandler.getUrlDataFromIntent(intent);
318 }
George Mount3636d0a2011-11-21 09:08:21 -0800319 Tab t = null;
320 if (urlData.isEmpty()) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700321 String landingPage = mActivity.getResources().getString(
322 R.string.def_landing_page);
323 if (!landingPage.isEmpty()) {
324 t = openTab(landingPage, false, true, true);
kaiyiz6e5b3e02013-08-19 20:02:01 +0800325 } else {
326 t = openTabToHomePage();
327 }
George Mount3636d0a2011-11-21 09:08:21 -0800328 } else {
329 t = openTab(urlData);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700330 t.setDerivedFromIntent(true);
George Mount3636d0a2011-11-21 09:08:21 -0800331 }
332 if (t != null) {
333 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
334 }
335 WebView webView = t.getWebView();
336 if (extra != null) {
337 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
338 if (scale > 0 && scale <= 1000) {
339 webView.setInitialScale(scale);
340 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700341 }
342 }
John Reckd8c74522011-06-14 08:45:00 -0700343 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700345 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500346 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700347 List<Tab> tabs = mTabControl.getTabs();
348 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700349
John Reck1cf4b792011-07-26 10:22:22 -0700350 for (Tab t : tabs) {
Vivek Sekhar0e10a202014-09-12 19:13:23 -0700351 //handle restored pages that may require a JS interface
352 if (t.getWebView() != null) {
353 WebBackForwardList backForwardList = t.getWebView().copyBackForwardList();
354 if (backForwardList != null) {
355 for (int i = 0; i < backForwardList.getSize(); i++) {
356 WebHistoryItem item = backForwardList.getItemAtIndex(i);
357 mHomepageHandler.registerJsInterface( t.getWebView(), item.getUrl());
358 }
359 }
360 }
John Reck1cf4b792011-07-26 10:22:22 -0700361 restoredTabs.add(t.getId());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700362 if (t != mTabControl.getCurrentTab()) {
363 t.pause();
364 }
John Reck1cf4b792011-07-26 10:22:22 -0700365 }
366 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700367 if (tabs.size() == 0) {
368 openTabToHomePage();
369 }
John Reck1cf4b792011-07-26 10:22:22 -0700370 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700371 // TabControl.restoreState() will create a new tab even if
372 // restoring the state fails.
373 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800374 // Intent is non-null when framework thinks the browser should be
375 // launching with a new intent (icicle is null).
376 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700377 mIntentHandler.onNewIntent(intent);
378 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700380 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700381 String jsFlags = getSettings().getJsEngineFlags();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800382 if (jsFlags.trim().length() != 0) {
383 getCurrentWebView().setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700384 }
George Mount3636d0a2011-11-21 09:08:21 -0800385 if (intent != null
386 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700387 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800388 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700389 }
390
John Reck1cf4b792011-07-26 10:22:22 -0700391 private static class PruneThumbnails implements Runnable {
392 private Context mContext;
393 private List<Long> mIds;
394
395 PruneThumbnails(Context context, List<Long> preserveIds) {
396 mContext = context.getApplicationContext();
397 mIds = preserveIds;
398 }
399
400 @Override
401 public void run() {
402 ContentResolver cr = mContext.getContentResolver();
403 if (mIds == null || mIds.size() == 0) {
404 cr.delete(Thumbnails.CONTENT_URI, null, null);
405 } else {
406 int length = mIds.size();
407 StringBuilder where = new StringBuilder();
408 where.append(Thumbnails._ID);
409 where.append(" not in (");
410 for (int i = 0; i < length; i++) {
411 where.append(mIds.get(i));
412 if (i < (length - 1)) {
413 where.append(",");
414 }
415 }
416 where.append(")");
417 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
418 }
419 }
420
421 }
422
Michael Kolb1514bb72010-11-22 09:11:48 -0800423 @Override
424 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700425 return mFactory;
426 }
427
428 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800429 public void onSetWebView(Tab tab, WebView view) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800430 if (tab.hasCrashed)
431 tab.showCrashView();
432 else
433 mUi.onSetWebView(tab, view);
Michael Kolba713ec82010-11-29 17:27:06 -0800434 }
435
436 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800437 public void createSubWindow(Tab tab) {
438 endActionMode();
439 WebView mainView = tab.getWebView();
440 WebView subView = mFactory.createWebView((mainView == null)
441 ? false
442 : mainView.isPrivateBrowsingEnabled());
443 mUi.createSubWindow(tab, subView);
444 }
445
446 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700447 public Context getContext() {
448 return mActivity;
449 }
450
451 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700452 public Activity getActivity() {
453 return mActivity;
454 }
455
456 void setUi(UI ui) {
457 mUi = ui;
458 }
459
Michael Kolbaf63dba2012-05-16 12:58:05 -0700460 @Override
461 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700462 return mSettings;
463 }
464
465 IntentHandler getIntentHandler() {
466 return mIntentHandler;
467 }
468
469 @Override
470 public UI getUi() {
471 return mUi;
472 }
473
474 int getMaxTabs() {
475 return mActivity.getResources().getInteger(R.integer.max_tabs);
476 }
477
478 @Override
479 public TabControl getTabControl() {
480 return mTabControl;
481 }
482
Michael Kolb1bf23132010-11-19 12:55:12 -0800483 @Override
484 public List<Tab> getTabs() {
485 return mTabControl.getTabs();
486 }
487
Michael Kolb8233fac2010-10-26 16:08:53 -0700488 private void startHandler() {
489 mHandler = new Handler() {
490
491 @Override
492 public void handleMessage(Message msg) {
493 switch (msg.what) {
494 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700495 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 break;
497 case FOCUS_NODE_HREF:
498 {
499 String url = (String) msg.getData().get("url");
500 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500501 String src = (String) msg.getData().get("src");
502 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 if (TextUtils.isEmpty(url)) {
504 break;
505 }
506 HashMap focusNodeMap = (HashMap) msg.obj;
507 WebView view = (WebView) focusNodeMap.get("webview");
508 // Only apply the action if the top window did not change.
509 if (getCurrentTopWebView() != view) {
510 break;
511 }
512 switch (msg.arg1) {
513 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700514 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700515 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500516 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700517 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500518 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500519 case R.id.open_newtab_context_menu_id:
520 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700521 openTab(url, parent,
522 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500523 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700524 case R.id.copy_link_context_menu_id:
525 copy(url);
526 break;
527 case R.id.save_link_context_menu_id:
528 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500529 DownloadHandler.onDownloadStartNoStream(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800530 mActivity, url, view.getSettings().getUserAgentString(),
531 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700532 break;
Tarun Nainani700b69b2014-03-26 16:07:25 -0700533 case R.id.save_link_bookmark_context_menu_id:
Tarun Nainani9f27c612014-04-02 14:34:03 -0700534 if(title == null || title == "")
535 title = url;
536
537 Intent bookmarkIntent = new Intent(mActivity, AddBookmarkPage.class);
538 //SWE TODO: No thumbnail support for the url obtained via
539 //browser context menu as its not loaded in webview.
540 if (bookmarkIntent != null) {
541 bookmarkIntent.putExtra(BrowserContract.Bookmarks.URL, url);
542 bookmarkIntent.putExtra(BrowserContract.Bookmarks.TITLE, title);
543 mActivity.startActivity(bookmarkIntent);
544 }
Tarun Nainani700b69b2014-03-26 16:07:25 -0700545 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 }
547 break;
548 }
549
550 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700551 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700552 break;
553
554 case STOP_LOAD:
555 stopLoading();
556 break;
557
558 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700559 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 mWakeLock.release();
561 // if we reach here, Browser should be still in the
562 // background loading after WAKELOCK_TIMEOUT (5-min).
563 // To avoid burning the battery, stop loading.
564 mTabControl.stopAllLoading();
565 }
566 break;
567
568 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800569 Tab tab = (Tab) msg.obj;
570 if (tab != null) {
571 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700572 }
573 break;
kaiyiz591110b2013-08-06 17:11:06 +0800574 case OPEN_MENU:
575 if (!mOptionsMenuOpen && mActivity != null ) {
576 mActivity.openOptionsMenu();
577 }
578 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 }
580 }
581 };
582
583 }
584
John Reckef654f12011-07-12 16:42:08 -0700585 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200586 public Tab getCurrentTab() {
587 return mTabControl.getCurrentTab();
588 }
589
Michael Kolbba99c5d2010-11-29 14:57:41 -0800590 @Override
591 public void shareCurrentPage() {
592 shareCurrentPage(mTabControl.getCurrentTab());
593 }
594
595 private void shareCurrentPage(Tab tab) {
Tarun Nainaniea28dde2014-08-27 17:25:09 -0700596 if (tab == null || tab.getWebView() == null)
597 return;
598
599 final Tab mytab = tab;
600 final ValueCallback<Bitmap> onScreenshot = new ValueCallback<Bitmap>() {
601 @Override
602 public void onReceiveValue(Bitmap bitmap) {
603 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
604 mytab.getFavicon(), bitmap);
605 }
606 };
607
608 createScreenshotAsync(
609 tab.getWebView(),
610 getDesiredThumbnailWidth(mActivity),
611 getDesiredThumbnailHeight(mActivity),
612 new ValueCallback<Bitmap>() {
613 @Override
614 public void onReceiveValue(Bitmap bitmap) {
615 sharePage(mActivity, mytab.getTitle(), mytab.getUrl(),
616 mytab.getFavicon(), bitmap);
617 }
618 });
Michael Kolbba99c5d2010-11-29 14:57:41 -0800619 }
620
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 /**
622 * Share a page, providing the title, url, favicon, and a screenshot. Uses
623 * an {@link Intent} to launch the Activity chooser.
624 * @param c Context used to launch a new Activity.
625 * @param title Title of the page. Stored in the Intent with
626 * {@link Intent#EXTRA_SUBJECT}
627 * @param url URL of the page. Stored in the Intent with
628 * {@link Intent#EXTRA_TEXT}
629 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
630 * with {@link Browser#EXTRA_SHARE_FAVICON}
631 * @param screenshot Bitmap of a screenshot of the page. Stored in the
632 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
633 */
634 static final void sharePage(Context c, String title, String url,
635 Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -0700636
637 ShareDialog sDialog = new ShareDialog((Activity)c, title, url, favicon, screenshot);
638 final AppAdapter adapter = new AppAdapter(c, c.getPackageManager(),
639 R.layout.app_row, sDialog.getApps());
640 sDialog.loadView(adapter);
Michael Kolb8233fac2010-10-26 16:08:53 -0700641 }
642
643 private void copy(CharSequence text) {
644 ClipboardManager cm = (ClipboardManager) mActivity
645 .getSystemService(Context.CLIPBOARD_SERVICE);
646 cm.setText(text);
647 }
648
649 // lifecycle
650
John Reck9c35b9c2012-05-30 10:08:50 -0700651 @Override
652 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800654 // update the menu in case of a locale change
655 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800656 if (mOptionsMenuOpen) {
657 mActivity.closeOptionsMenu();
658 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
659 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 if (mPageDialogsHandler != null) {
661 mPageDialogsHandler.onConfigurationChanged(config);
662 }
663 mUi.onConfigurationChanged(config);
664 }
665
666 @Override
667 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700668 if (!mUi.isWebShowing()) {
669 mUi.showWeb(false);
670 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700671 mIntentHandler.onNewIntent(intent);
672 }
673
John Reck9c35b9c2012-05-30 10:08:50 -0700674 @Override
675 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800676 if (mUi.isCustomViewShowing()) {
677 hideCustomView();
678 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 if (mActivityPaused) {
680 Log.e(LOGTAG, "BrowserActivity is already paused.");
681 return;
682 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800684 Tab tab = mTabControl.getCurrentTab();
685 if (tab != null) {
686 tab.pause();
687 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700688 if (mWakeLock == null) {
689 PowerManager pm = (PowerManager) mActivity
690 .getSystemService(Context.POWER_SERVICE);
691 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
692 }
Michael Kolb70976932010-11-30 11:34:01 -0800693 mWakeLock.acquire();
694 mHandler.sendMessageDelayed(mHandler
695 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
696 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 }
698 mUi.onPause();
699 mNetworkHandler.onPause();
700
701 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100702 NfcHandler.unregister(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 }
704
John Reck9c35b9c2012-05-30 10:08:50 -0700705 @Override
706 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800708 Bundle saveState = createSaveState();
709
710 // crash recovery manages all save & restore state
711 mCrashRecoveryHandler.writeState(saveState);
712 mSettings.setLastRunPaused(true);
713 }
714
715 /**
716 * Save the current state to outState. Does not write the state to
717 * disk.
718 * @return Bundle containing the current state of all tabs.
719 */
720 /* package */ Bundle createSaveState() {
721 Bundle saveState = new Bundle();
722 mTabControl.saveState(saveState);
George Mount3636d0a2011-11-21 09:08:21 -0800723 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700724 }
725
John Reck9c35b9c2012-05-30 10:08:50 -0700726 @Override
727 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700728 if (!mActivityPaused) {
729 Log.e(LOGTAG, "BrowserActivity is already resumed.");
730 return;
731 }
George Mount3636d0a2011-11-21 09:08:21 -0800732 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800734 Tab current = mTabControl.getCurrentTab();
735 if (current != null) {
736 current.resume();
737 resumeWebViewTimers(current);
738 }
John Reckf57c0292011-07-21 18:15:39 -0700739 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200740
Michael Kolb8233fac2010-10-26 16:08:53 -0700741 mUi.onResume();
742 mNetworkHandler.onResume();
743 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100744 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700745 if (mVoiceResult != null) {
746 mUi.onVoiceResult(mVoiceResult);
747 mVoiceResult = null;
748 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800749 if (current != null && current.hasCrashed) {
750 current.showCrashView();
751 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700752 }
753
John Reckf57c0292011-07-21 18:15:39 -0700754 private void releaseWakeLock() {
755 if (mWakeLock != null && mWakeLock.isHeld()) {
756 mHandler.removeMessages(RELEASE_WAKELOCK);
757 mWakeLock.release();
758 }
759 }
760
Michael Kolb70976932010-11-30 11:34:01 -0800761 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800762 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800763 * @param tab guaranteed non-null
764 */
765 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700766 boolean inLoad = tab.inPageLoad();
767 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
768 CookieSyncManager.getInstance().startSync();
769 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100770 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700771 }
772 }
773
Michael Kolb70976932010-11-30 11:34:01 -0800774 /**
775 * Pause all WebView timers using the WebView of the given tab
776 * @param tab
777 * @return true if the timers are paused or tab is null
778 */
779 private boolean pauseWebViewTimers(Tab tab) {
780 if (tab == null) {
781 return true;
782 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700783 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100784 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700785 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 }
Michael Kolb70976932010-11-30 11:34:01 -0800787 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700788 }
789
John Reck9c35b9c2012-05-30 10:08:50 -0700790 @Override
791 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800792 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700793 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
794 mUploadHandler = null;
795 }
796 if (mTabControl == null) return;
797 mUi.onDestroy();
798 // Remove the current tab and sub window
799 Tab t = mTabControl.getCurrentTab();
800 if (t != null) {
801 dismissSubWindow(t);
802 removeTab(t);
803 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500804 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 // Destroy all the tabs
806 mTabControl.destroy();
Michael Kolb8233fac2010-10-26 16:08:53 -0700807 }
808
809 protected boolean isActivityPaused() {
810 return mActivityPaused;
811 }
812
John Reck9c35b9c2012-05-30 10:08:50 -0700813 @Override
814 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700815 mTabControl.freeMemory();
816 }
817
818 @Override
819 public boolean shouldShowErrorConsole() {
820 return mShouldShowErrorConsole;
821 }
822
823 protected void setShouldShowErrorConsole(boolean show) {
824 if (show == mShouldShowErrorConsole) {
825 // Nothing to do.
826 return;
827 }
828 mShouldShowErrorConsole = show;
829 Tab t = mTabControl.getCurrentTab();
830 if (t == null) {
831 // There is no current tab so we cannot toggle the error console
832 return;
833 }
834 mUi.setShouldShowErrorConsole(t, show);
835 }
836
837 @Override
838 public void stopLoading() {
839 mLoadStopped = true;
840 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700842 if (w != null) {
843 w.stopLoading();
844 mUi.onPageStopped(tab);
845 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700846 }
847
848 boolean didUserStopLoading() {
849 return mLoadStopped;
850 }
851
kaiyiza016da12013-08-26 17:50:22 +0800852 private void handleNetworkNotify(WebView view) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700853 final String reminderType = getContext().getResources().getString(
854 R.string.def_wifi_browser_interaction_remind_type);
855 final String selectionConnnection = getContext().getResources().getString(
856 R.string.def_action_wifi_selection_data_connections);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700857 final String wifiSelection = getContext().getResources().getString(
858 R.string.def_intent_pick_network);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700859
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700860 if (reminderType.isEmpty() || selectionConnnection.isEmpty() ||
861 wifiSelection.isEmpty())
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700862 return;
863
kaiyiza016da12013-08-26 17:50:22 +0800864 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
Vivek Sekharb54614f2014-05-01 19:03:37 -0700865 Context.CONNECTIVITY_SERVICE);
866 NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700867 WifiManager wifiMgr = (WifiManager) this.getContext()
868 .getSystemService(Context.WIFI_SERVICE);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700869 if (networkInfo == null
870 || (networkInfo != null && (networkInfo.getType() !=
871 ConnectivityManager.TYPE_WIFI))) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700872 int isReminder = Settings.System.getInt(mActivity.getContentResolver(),
873 reminderType, NETWORK_SWITCH_TYPE_OK);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700874 List<ScanResult> list = wifiMgr.getScanResults();
875 // Have no AP's for Wifi's fall back to data
876 if (list != null && list.size() == 0 && isReminder == NETWORK_SWITCH_TYPE_OK) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700877 Intent intent = new Intent(selectionConnnection);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700878 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
879 this.getContext().startActivity(intent);
880 } else {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700881 // Request to select Wifi AP
882 try {
883 Intent intent = new Intent(wifiSelection);
884 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
885 this.getContext().startActivity(intent);
886 } catch (Exception e) {
887 String err_msg = this.getContext().getString(
888 R.string.acivity_not_found, wifiSelection);
889 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
890 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700891 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700892 mNetworkShouldNotify = false;
kaiyiza016da12013-08-26 17:50:22 +0800893 }
894 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700895
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 // WebViewController
897
898 @Override
John Reck324d4402011-01-11 16:56:42 -0800899 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700900
901 // We've started to load a new page. If there was a pending message
902 // to save a screenshot then we will now take the new page and save
903 // an incorrect screenshot. Therefore, remove any pending thumbnail
904 // messages from the queue.
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700905 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700906
907 // reset sync timer to avoid sync starts during loading a page
908 CookieSyncManager.getInstance().resetSync();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700909 WifiManager wifiMgr = (WifiManager) this.getContext()
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700910 .getSystemService(Context.WIFI_SERVICE);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700911 boolean networkNotifier =
912 mActivity.getApplicationContext().getResources().getBoolean(R.bool.network_notifier);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700913 if (networkNotifier && mNetworkShouldNotify && wifiMgr.isWifiEnabled()){
Vivek Sekharb54614f2014-05-01 19:03:37 -0700914 handleNetworkNotify(view);
kaiyiza016da12013-08-26 17:50:22 +0800915 } else {
916 if (!mNetworkHandler.isNetworkUp()) {
917 view.setNetworkAvailable(false);
918 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700919 }
920
921 // when BrowserActivity just starts, onPageStarted may be called before
922 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
923 // to start the timer. As we won't switch tabs while an activity is in
924 // pause state, we can ensure calling resume and pause in pair.
925 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800926 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700927 }
928 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 endActionMode();
930
John Reck30c714c2010-12-16 17:30:34 -0800931 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700932
John Reck324d4402011-01-11 16:56:42 -0800933 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700934 // update the bookmark database for favicon
935 maybeUpdateFavicon(tab, null, url, favicon);
936
937 Performance.tracePageStart(url);
938
939 // Performance probe
940 if (false) {
941 Performance.onPageStarted();
942 }
943
944 }
945
946 @Override
John Reck324d4402011-01-11 16:56:42 -0800947 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700948 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800949 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200950
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 // Performance probe
952 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800953 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700954 }
955
Tarun Nainani8eb00912014-07-17 12:28:32 -0700956 tab.onPageFinished();
957
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 Performance.tracePageFinished();
959 }
960
961 @Override
John Reck30c714c2010-12-16 17:30:34 -0800962 public void onProgressChanged(Tab tab) {
963 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700964
965 if (newProgress == 100) {
966 CookieSyncManager.getInstance().sync();
967 // onProgressChanged() may continue to be called after the main
968 // frame has finished loading, as any remaining sub frames continue
969 // to load. We'll only get called once though with newProgress as
970 // 100 when everything is loaded. (onPageFinished is called once
971 // when the main frame completes loading regardless of the state of
972 // any sub frames so calls to onProgressChanges may continue after
973 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800974 if (tab.inPageLoad()) {
975 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200976 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
977 // pause the WebView timer and release the wake lock if it is
978 // finished while BrowserActivity is in pause state.
979 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 }
John Reckd9862372012-02-21 15:04:50 -0800981 if (!tab.isPrivateBrowsingEnabled()
982 && !TextUtils.isEmpty(tab.getUrl())
983 && !tab.isSnapshot()) {
984 // Only update the bookmark screenshot if the user did not
985 // cancel the load early and there is not already
986 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700987 if (tab.shouldUpdateThumbnail() &&
988 (tab.inForeground() && !didUserStopLoading()
989 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800990 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
991 mHandler.sendMessageDelayed(mHandler.obtainMessage(
992 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800993 1500);
John Reckd9862372012-02-21 15:04:50 -0800994 }
995 }
996 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800998 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700999 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001000 // still loading
1001 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001002 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001003 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 }
1005 }
John Reck30c714c2010-12-16 17:30:34 -08001006 mUi.onProgressChanged(tab);
1007 }
1008
1009 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001010 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001011 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001012 }
1013
1014 @Override
1015 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001016 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001017 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001018 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001019 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1020 return;
1021 }
1022 // Update the title in the history database if not in private browsing mode
1023 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001024 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001025 }
1026 }
1027
1028 @Override
1029 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001030 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1032 }
1033
1034 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001035 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1036 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 }
1038
1039 @Override
1040 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1041 if (mMenuIsDown) {
1042 // only check shortcut key when MENU is held
1043 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1044 event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001045 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001046 int keyCode = event.getKeyCode();
1047 // We need to send almost every key to WebKit. However:
1048 // 1. We don't want to block the device on the renderer for
1049 // some keys like menu, home, call.
1050 // 2. There are no WebKit equivalents for some of these keys
1051 // (see app/keyboard_codes_win.h)
1052 // Note that these are not the same set as KeyEvent.isSystemKey:
1053 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
1054 if (keyCode == KeyEvent.KEYCODE_MENU ||
1055 keyCode == KeyEvent.KEYCODE_HOME ||
1056 keyCode == KeyEvent.KEYCODE_BACK ||
1057 keyCode == KeyEvent.KEYCODE_CALL ||
1058 keyCode == KeyEvent.KEYCODE_ENDCALL ||
1059 keyCode == KeyEvent.KEYCODE_POWER ||
1060 keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
1061 keyCode == KeyEvent.KEYCODE_CAMERA ||
1062 keyCode == KeyEvent.KEYCODE_FOCUS ||
1063 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
1064 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
1065 keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
1066 return true;
1067 }
1068
1069 // We also have to intercept some shortcuts before we send them to the ContentView.
1070 if (event.isCtrlPressed() && (
1071 keyCode == KeyEvent.KEYCODE_TAB ||
1072 keyCode == KeyEvent.KEYCODE_W ||
1073 keyCode == KeyEvent.KEYCODE_F4)) {
1074 return true;
1075 }
1076
1077 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001078 }
1079
1080 @Override
John Reck997b1b72012-04-19 18:08:25 -07001081 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 if (!isActivityPaused()) {
1083 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001084 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 } else {
John Reck997b1b72012-04-19 18:08:25 -07001086 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001087 }
1088 }
John Reck997b1b72012-04-19 18:08:25 -07001089 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001090 }
1091
1092 @Override
John Reck324d4402011-01-11 16:56:42 -08001093 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -07001094 boolean disableHistoryWrites =
1095 mActivity.getResources().getBoolean(R.bool.def_disable_history);
1096
1097 // Don't save anything in private browsing mode or when explicitly set
1098 // not to write history via an overlay
1099 if (tab.isPrivateBrowsingEnabled() || disableHistoryWrites) return;
John Reck49a603c2011-03-03 09:33:05 -08001100 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001101
John Reck324d4402011-01-11 16:56:42 -08001102 if (TextUtils.isEmpty(url)
1103 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001104 return;
1105 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001106
John Reckf57c0292011-07-21 18:15:39 -07001107 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001108 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 }
1110
1111 @Override
1112 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1113 AsyncTask<Void, Void, String[]> task =
1114 new AsyncTask<Void, Void, String[]>() {
1115 @Override
1116 public String[] doInBackground(Void... unused) {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -07001117 return (String[]) Browser.getVisitedHistory(mActivity.getContentResolver());
Michael Kolb8233fac2010-10-26 16:08:53 -07001118 }
1119 @Override
1120 public void onPostExecute(String[] result) {
1121 callback.onReceiveValue(result);
1122 }
1123 };
1124 task.execute();
1125 }
1126
1127 @Override
1128 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1129 final HttpAuthHandler handler, final String host,
1130 final String realm) {
1131 String username = null;
1132 String password = null;
1133
1134 boolean reuseHttpAuthUsernamePassword
1135 = handler.useHttpAuthUsernamePassword();
1136
1137 if (reuseHttpAuthUsernamePassword && view != null) {
1138 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1139 if (credentials != null && credentials.length == 2) {
1140 username = credentials[0];
1141 password = credentials[1];
1142 }
1143 }
1144
1145 if (username != null && password != null) {
1146 handler.proceed(username, password);
1147 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001148 if (tab.inForeground() /*&& !handler.suppressDialog()*/) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001149 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1150 } else {
1151 handler.cancel();
1152 }
1153 }
1154 }
1155
1156 @Override
1157 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001158 String contentDisposition, String mimetype, String referer,
1159 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001160 WebView w = tab.getWebView();
Axesh R. Ajmera87c5aba2014-11-06 14:13:36 -08001161 if ( w == null) return;
qqzhoua95a2e22013-04-18 17:28:31 +08001162 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001163 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001164 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 // This Tab was opened for the sole purpose of downloading a
1166 // file. Remove it.
1167 if (tab == mTabControl.getCurrentTab()) {
1168 // In this case, the Tab is still on top.
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001169 if (tab.getDerivedFromIntent())
1170 closeTab(tab);
1171 else
1172 goBackOnePageOrQuit();
Michael Kolb8233fac2010-10-26 16:08:53 -07001173 } else {
1174 // In this case, it is not.
1175 closeTab(tab);
1176 }
1177 }
1178 }
1179
1180 @Override
1181 public Bitmap getDefaultVideoPoster() {
1182 return mUi.getDefaultVideoPoster();
1183 }
1184
1185 @Override
1186 public View getVideoLoadingProgressView() {
1187 return mUi.getVideoLoadingProgressView();
1188 }
1189
1190 @Override
1191 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1192 SslError error) {
1193 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1194 }
1195
1196 // helper method
1197
1198 /*
1199 * Update the favorites icon if the private browsing isn't enabled and the
1200 * icon is valid.
1201 */
1202 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1203 final String url, Bitmap favicon) {
1204 if (favicon == null) {
1205 return;
1206 }
1207 if (!tab.isPrivateBrowsingEnabled()) {
1208 Bookmarks.updateFavicon(mActivity
1209 .getContentResolver(), originalUrl, url, favicon);
1210 }
1211 }
1212
Leon Scroggins4cd97792010-12-03 15:31:56 -05001213 @Override
1214 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001215 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001216 mUi.bookmarkedStatusHasChanged(tab);
1217 }
1218
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 // end WebViewController
1220
1221 protected void pageUp() {
1222 getCurrentTopWebView().pageUp(false);
1223 }
1224
1225 protected void pageDown() {
1226 getCurrentTopWebView().pageDown(false);
1227 }
1228
1229 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001230 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 public void editUrl() {
1232 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001233 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001234 }
1235
John Reck9c35b9c2012-05-30 10:08:50 -07001236 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001237 public void showCustomView(Tab tab, View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001238 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 if (tab.inForeground()) {
1240 if (mUi.isCustomViewShowing()) {
1241 callback.onCustomViewHidden();
1242 return;
1243 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001244 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001245 // Save the menu state and set it to empty while the custom
1246 // view is showing.
1247 mOldMenuState = mMenuState;
1248 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001249 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001250 }
1251 }
1252
1253 @Override
1254 public void hideCustomView() {
1255 if (mUi.isCustomViewShowing()) {
1256 mUi.onHideCustomView();
1257 // Reset the old menu state.
1258 mMenuState = mOldMenuState;
1259 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001260 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001261 }
1262 }
1263
John Reck9c35b9c2012-05-30 10:08:50 -07001264 @Override
1265 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001266 Intent intent) {
1267 if (getCurrentTopWebView() == null) return;
1268 switch (requestCode) {
1269 case PREFERENCES_PAGE:
1270 if (resultCode == Activity.RESULT_OK && intent != null) {
1271 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001272 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 mTabControl.removeParentChildRelationShips();
1274 }
1275 }
1276 break;
1277 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001278 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001279 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001280 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001282 case AUTOFILL_SETUP:
1283 // Determine whether a profile was actually set up or not
1284 // and if so, send the message back to the WebTextView to
1285 // fill the form with the new profile.
1286 if (getSettings().getAutoFillProfile() != null) {
1287 mAutoFillSetupMessage.sendToTarget();
1288 mAutoFillSetupMessage = null;
1289 }
1290 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001291 case COMBO_VIEW:
1292 if (intent == null || resultCode != Activity.RESULT_OK) {
1293 break;
1294 }
John Reck3ba45532011-08-11 16:26:53 -07001295 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001296 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1297 Tab t = getCurrentTab();
1298 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001299 mUpdateMyNavThumbnail = true;
1300 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001301 loadUrl(t, uri.toString());
1302 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1303 String[] urls = intent.getStringArrayExtra(
1304 ComboViewActivity.EXTRA_OPEN_ALL);
1305 Tab parent = getCurrentTab();
1306 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001307 if (url != null) {
1308 parent = openTab(url, parent,
1309 !mSettings.openInBackground(), true);
1310 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001311 }
1312 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1313 long id = intent.getLongExtra(
1314 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1315 if (id >= 0) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001316 createNewSnapshotTab(id, true);
John Reckd3e4d5b2011-07-13 15:48:43 -07001317 }
1318 }
1319 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001320 case VOICE_RESULT:
1321 if (resultCode == Activity.RESULT_OK && intent != null) {
1322 ArrayList<String> results = intent.getStringArrayListExtra(
1323 RecognizerIntent.EXTRA_RESULTS);
1324 if (results.size() >= 1) {
1325 mVoiceResult = results.get(0);
1326 }
1327 }
1328 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001329 case MY_NAVIGATION:
1330 if (intent == null || resultCode != Activity.RESULT_OK) {
1331 break;
1332 }
1333
1334 if (intent.getBooleanExtra("need_refresh", false) &&
1335 getCurrentTopWebView() != null) {
1336 getCurrentTopWebView().reload();
1337 }
1338 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001339 default:
1340 break;
1341 }
1342 getCurrentTopWebView().requestFocus();
1343 }
1344
1345 /**
1346 * Open the Go page.
1347 * @param startWithHistory If true, open starting on the history tab.
1348 * Otherwise, start with the bookmarks tab.
1349 */
1350 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001351 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001352 if (mTabControl.getCurrentWebView() == null) {
1353 return;
1354 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001355 // clear action mode
1356 if (isInCustomActionMode()) {
1357 endActionMode();
1358 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001359 Bundle extras = new Bundle();
1360 // Disable opening in a new window if we have maxed out the windows
1361 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1362 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001363 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 }
1365
1366 // combo view callbacks
1367
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 // key handling
1369 protected void onBackKey() {
1370 if (!mUi.onBackKey()) {
1371 WebView subwindow = mTabControl.getCurrentSubWindow();
1372 if (subwindow != null) {
1373 if (subwindow.canGoBack()) {
1374 subwindow.goBack();
1375 } else {
1376 dismissSubWindow(mTabControl.getCurrentTab());
1377 }
1378 } else {
1379 goBackOnePageOrQuit();
1380 }
1381 }
1382 }
1383
Michael Kolb4bd767d2011-05-27 11:33:55 -07001384 protected boolean onMenuKey() {
1385 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001386 }
1387
Michael Kolb8233fac2010-10-26 16:08:53 -07001388 // menu handling and state
1389 // TODO: maybe put into separate handler
1390
John Reck9c35b9c2012-05-30 10:08:50 -07001391 @Override
1392 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001393 if (mMenuState == EMPTY_MENU) {
1394 return false;
1395 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 MenuInflater inflater = mActivity.getMenuInflater();
1397 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 return true;
1399 }
1400
John Reck9c35b9c2012-05-30 10:08:50 -07001401 @Override
1402 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001403 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001404 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001405 return;
1406 }
1407 if (!(v instanceof WebView)) {
1408 return;
1409 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001410 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001411 WebView.HitTestResult result = webview.getHitTestResult();
1412 if (result == null) {
1413 return;
1414 }
1415
1416 int type = result.getType();
1417 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1418 Log.w(LOGTAG,
1419 "We should not show context menu when nothing is touched");
1420 return;
1421 }
1422 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1423 // let TextView handles context menu
1424 return;
1425 }
1426
1427 // Note, http://b/issue?id=1106666 is requesting that
1428 // an inflated menu can be used again. This is not available
1429 // yet, so inflate each time (yuk!)
1430 MenuInflater inflater = mActivity.getMenuInflater();
1431 inflater.inflate(R.menu.browsercontext, menu);
1432
1433 // Show the correct menu group
1434 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001435 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001436 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001437 menu.setGroupVisible(R.id.PHONE_MENU,
1438 type == WebView.HitTestResult.PHONE_TYPE);
1439 menu.setGroupVisible(R.id.EMAIL_MENU,
1440 type == WebView.HitTestResult.EMAIL_TYPE);
1441 menu.setGroupVisible(R.id.GEO_MENU,
1442 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001443 String itemUrl = null;
1444 String url = webview.getOriginalUrl();
1445 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1446 itemUrl = Uri.decode(navigationUrl);
1447 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1448 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1449 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1450 } else {
1451 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1452 }
1453 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1454 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1455 } else {
1456 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1457
1458 menu.setGroupVisible(R.id.IMAGE_MENU,
1459 type == WebView.HitTestResult.IMAGE_TYPE
1460 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1461 menu.setGroupVisible(R.id.ANCHOR_MENU,
1462 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1463 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Vivek Sekharcc4bc5e2014-05-13 12:46:37 -07001464 menu.findItem(R.id.save_link_context_menu_id).setEnabled(
1465 UrlUtils.isDownloadableScheme(extra));
kaiyiz6e5b3e02013-08-19 20:02:01 +08001466 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001467 // Setup custom handling depending on the type
1468 switch (type) {
1469 case WebView.HitTestResult.PHONE_TYPE:
1470 menu.setHeaderTitle(Uri.decode(extra));
1471 menu.findItem(R.id.dial_context_menu_id).setIntent(
1472 new Intent(Intent.ACTION_VIEW, Uri
1473 .parse(WebView.SCHEME_TEL + extra)));
1474 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1475 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1476 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1477 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1478 addIntent);
1479 menu.findItem(R.id.copy_phone_context_menu_id)
1480 .setOnMenuItemClickListener(
1481 new Copy(extra));
1482 break;
1483
1484 case WebView.HitTestResult.EMAIL_TYPE:
1485 menu.setHeaderTitle(extra);
1486 menu.findItem(R.id.email_context_menu_id).setIntent(
1487 new Intent(Intent.ACTION_VIEW, Uri
1488 .parse(WebView.SCHEME_MAILTO + extra)));
1489 menu.findItem(R.id.copy_mail_context_menu_id)
1490 .setOnMenuItemClickListener(
1491 new Copy(extra));
1492 break;
1493
1494 case WebView.HitTestResult.GEO_TYPE:
1495 menu.setHeaderTitle(extra);
1496 menu.findItem(R.id.map_context_menu_id).setIntent(
1497 new Intent(Intent.ACTION_VIEW, Uri
1498 .parse(WebView.SCHEME_GEO
1499 + URLEncoder.encode(extra))));
1500 menu.findItem(R.id.copy_geo_context_menu_id)
1501 .setOnMenuItemClickListener(
1502 new Copy(extra));
1503 break;
1504
1505 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1506 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001507 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001508 // decide whether to show the open link in new tab option
1509 boolean showNewTab = mTabControl.canCreateNewTab();
1510 MenuItem newTabItem
1511 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001512 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001513 ? R.string.contextmenu_openlink_newwindow_background
1514 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001515 newTabItem.setVisible(showNewTab);
1516 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001517 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1518 newTabItem.setOnMenuItemClickListener(
1519 new MenuItem.OnMenuItemClickListener() {
1520 @Override
1521 public boolean onMenuItemClick(MenuItem item) {
1522 final HashMap<String, WebView> hrefMap =
1523 new HashMap<String, WebView>();
1524 hrefMap.put("webview", webview);
1525 final Message msg = mHandler.obtainMessage(
1526 FOCUS_NODE_HREF,
1527 R.id.open_newtab_context_menu_id,
1528 0, hrefMap);
1529 webview.requestFocusNodeHref(msg);
1530 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001531 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001532 });
1533 } else {
1534 newTabItem.setOnMenuItemClickListener(
1535 new MenuItem.OnMenuItemClickListener() {
1536 @Override
1537 public boolean onMenuItemClick(MenuItem item) {
1538 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001539 openTab(extra, parent,
1540 !mSettings.openInBackground(),
1541 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001542 return true;
1543 }
1544 });
1545 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001546 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001547 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1548 menu.setHeaderTitle(navigationUrl);
1549 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1550
1551 if (itemUrl != null) {
1552 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1553 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1554 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1555 @Override
1556 public boolean onMenuItemClick(MenuItem item) {
1557 final Intent intent = new Intent(Controller.this
1558 .getContext(),
1559 AddMyNavigationPage.class);
1560 Bundle bundle = new Bundle();
1561 String url = Uri.decode(navigationUrl);
1562 bundle.putBoolean("isAdding", false);
1563 bundle.putString("url", url);
1564 bundle.putString("name", getNameFromUrl(url));
1565 intent.putExtra("websites", bundle);
1566 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1567 return false;
1568 }
1569 });
1570 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1571 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1572 @Override
1573 public boolean onMenuItemClick(MenuItem item) {
1574 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1575 return false;
1576 }
1577 });
1578 }
1579 } else {
1580 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1581 }
1582 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001583 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1584 break;
1585 }
1586 // otherwise fall through to handle image part
1587 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001588 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1589 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001590 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1591 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001592 shareItem.setOnMenuItemClickListener(
1593 new MenuItem.OnMenuItemClickListener() {
1594 @Override
1595 public boolean onMenuItemClick(MenuItem item) {
1596 sharePage(mActivity, null, extra, null,
1597 null);
1598 return true;
1599 }
1600 }
1601 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001603 menu.findItem(R.id.view_image_context_menu_id)
1604 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1605 @Override
1606 public boolean onMenuItemClick(MenuItem item) {
1607 openTab(extra, mTabControl.getCurrentTab(), true, true);
1608 return false;
1609 }
1610 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001611 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1612 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1613 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001614 menu.findItem(R.id.set_wallpaper_context_menu_id).
1615 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1616 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 break;
1618
1619 default:
1620 Log.w(LOGTAG, "We should not get here.");
1621 break;
1622 }
1623 //update the ui
1624 mUi.onContextMenuCreated(menu);
1625 }
1626
kaiyiz6e5b3e02013-08-19 20:02:01 +08001627 public void startAddMyNavigation(String url) {
1628 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1629 Bundle bundle = new Bundle();
1630 bundle.putBoolean("isAdding", true);
1631 bundle.putString("url", url);
1632 bundle.putString("name", getNameFromUrl(url));
1633 intent.putExtra("websites", bundle);
1634 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1635 }
1636
1637 private void showMyNavigationDeleteDialog(final String itemUrl) {
1638 new AlertDialog.Builder(this.getContext())
1639 .setTitle(R.string.my_navigation_delete_label)
1640 .setIcon(android.R.drawable.ic_dialog_alert)
1641 .setMessage(R.string.my_navigation_delete_msg)
1642 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1643 @Override
1644 public void onClick(DialogInterface dialog, int whichButton) {
1645 deleteMyNavigationItem(itemUrl);
1646 }
1647 })
1648 .setNegativeButton(R.string.cancel, null)
1649 .show();
1650 }
1651
1652 private void deleteMyNavigationItem(final String itemUrl) {
1653 ContentResolver cr = this.getContext().getContentResolver();
1654 Cursor cursor = null;
1655
1656 try {
1657 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1658 new String[] {
1659 MyNavigationUtil.ID
1660 }, "url = ?", new String[] {
1661 itemUrl
1662 }, null);
1663 if (null != cursor && cursor.moveToFirst()) {
1664 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1665 cursor.getLong(0));
1666
1667 ContentValues values = new ContentValues();
1668 values.put(MyNavigationUtil.TITLE, "");
1669 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1670 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1671 ByteArrayOutputStream os = new ByteArrayOutputStream();
1672 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1673 R.raw.my_navigation_add);
1674 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1675 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1676 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1677 cr.update(uri, values, null, null);
1678 } else {
1679 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1680 }
1681 } catch (IllegalStateException e) {
1682 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1683 } finally {
1684 if (null != cursor) {
1685 cursor.close();
1686 }
1687 }
1688
1689 if (getCurrentTopWebView() != null) {
1690 getCurrentTopWebView().reload();
1691 }
1692 }
1693
1694 private String getNameFromUrl(String itemUrl) {
1695 ContentResolver cr = this.getContext().getContentResolver();
1696 Cursor cursor = null;
1697 String name = null;
1698
1699 try {
1700 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1701 new String[] {
1702 MyNavigationUtil.TITLE
1703 }, "url = ?", new String[] {
1704 itemUrl
1705 }, null);
1706 if (null != cursor && cursor.moveToFirst()) {
1707 name = cursor.getString(0);
1708 } else {
1709 Log.e(LOGTAG, "this item does not exist!");
1710 }
1711 } catch (IllegalStateException e) {
1712 Log.e(LOGTAG, "getNameFromUrl", e);
1713 } finally {
1714 if (null != cursor) {
1715 cursor.close();
1716 }
1717 }
1718 return name;
1719 }
1720
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001721 private void updateMyNavigationThumbnail(final String itemUrl, final Bitmap bitmap) {
1722 if (bitmap == null) {
kaiyiz6e5b3e02013-08-19 20:02:01 +08001723 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1724 return;
1725 }
1726
1727 final ContentResolver cr = mActivity.getContentResolver();
1728 new AsyncTask<Void, Void, Void>() {
1729 @Override
1730 protected Void doInBackground(Void... unused) {
1731 ContentResolver cr = mActivity.getContentResolver();
1732 Cursor cursor = null;
1733 try {
1734 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1735 new String[] {
1736 MyNavigationUtil.ID
1737 }, "url = ?", new String[] {
1738 itemUrl
1739 }, null);
1740 if (null != cursor && cursor.moveToFirst()) {
1741 final ByteArrayOutputStream os = new ByteArrayOutputStream();
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001742 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001743
1744 ContentValues values = new ContentValues();
1745 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1746 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1747 cursor.getLong(0));
1748 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1749 cr.update(uri, values, null, null);
1750 os.close();
1751 }
1752 } catch (IllegalStateException e) {
1753 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1754 } catch (IOException e) {
1755 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1756 } finally {
1757 if (null != cursor) {
1758 cursor.close();
1759 }
1760 }
1761 return null;
1762 }
1763 }.execute();
1764 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001765 /**
1766 * As the menu can be open when loading state changes
1767 * we must manually update the state of the stop/reload menu
1768 * item
1769 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001770 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001771 if (menu == null) {
1772 return;
1773 }
1774 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001775 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001776 menu.findItem(R.id.stop_menu_id):
1777 menu.findItem(R.id.reload_menu_id);
1778 if (src != null) {
1779 dest.setIcon(src.getIcon());
1780 dest.setTitle(src.getTitle());
1781 }
1782 }
1783
John Reck9c35b9c2012-05-30 10:08:50 -07001784 @Override
1785 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001786 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001787 // hold on to the menu reference here; it is used by the page callbacks
1788 // to update the menu based on loading state
1789 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001790 // Note: setVisible will decide whether an item is visible; while
1791 // setEnabled() will decide whether an item is enabled, which also means
1792 // whether the matching shortcut key will function.
1793 switch (mMenuState) {
1794 case EMPTY_MENU:
1795 if (mCurrentMenuState != mMenuState) {
1796 menu.setGroupVisible(R.id.MAIN_MENU, false);
1797 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1798 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1799 }
1800 break;
1801 default:
1802 if (mCurrentMenuState != mMenuState) {
1803 menu.setGroupVisible(R.id.MAIN_MENU, true);
1804 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1805 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1806 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001807 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001808 break;
1809 }
1810 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001811 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001812 }
1813
Michael Kolb4bf79712011-07-14 14:18:12 -07001814 @Override
1815 public void updateMenuState(Tab tab, Menu menu) {
1816 boolean canGoBack = false;
1817 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001818 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001819 boolean isLive = false;
Tarun Nainani8eb00912014-07-17 12:28:32 -07001820 // Following flag is used to identify schemes for which the LIVE_MENU
1821 // items defined in res/menu/browser.xml should be enabled
1822 boolean isLiveScheme = false;
1823 boolean isPageFinished = false;
Axesh R. Ajmera4328e0a2014-10-30 16:07:54 -07001824 boolean isSavable = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001825 if (tab != null) {
1826 canGoBack = tab.canGoBack();
1827 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001828 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001829 isLive = !tab.isSnapshot();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001830 isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
1831 isPageFinished = tab.getPageFinishedStatus();
Axesh R. Ajmera4328e0a2014-10-30 16:07:54 -07001832 isSavable = tab.getWebView().isSavable();
Michael Kolb4bf79712011-07-14 14:18:12 -07001833 }
1834 final MenuItem back = menu.findItem(R.id.back_menu_id);
1835 back.setEnabled(canGoBack);
1836
1837 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001838
1839 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1840 forward.setEnabled(canGoForward);
1841
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001842 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1843 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001844 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001845 if (source != null && dest != null) {
1846 dest.setTitle(source.getTitle());
1847 dest.setIcon(source.getIcon());
1848 }
John Recke1a03a32011-09-14 17:04:16 -07001849 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001850
1851 // decide whether to show the share link option
1852 PackageManager pm = mActivity.getPackageManager();
1853 Intent send = new Intent(Intent.ACTION_SEND);
1854 send.setType("text/plain");
1855 ResolveInfo ri = pm.resolveActivity(send,
1856 PackageManager.MATCH_DEFAULT_ONLY);
1857 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1858
1859 boolean isNavDump = mSettings.enableNavDump();
1860 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1861 nav.setVisible(isNavDump);
1862 nav.setEnabled(isNavDump);
1863
1864 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001865 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1866 uaSwitcher.setChecked(isDesktopUa);
Tarun Nainani8eb00912014-07-17 12:28:32 -07001867 menu.setGroupVisible(R.id.LIVE_MENU, isLive && isLiveScheme);
John Recke1a03a32011-09-14 17:04:16 -07001868 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Axesh R. Ajmera4328e0a2014-10-30 16:07:54 -07001869 menu.setGroupEnabled(R.id.OFFLINE_READING, isLive && isLiveScheme && isPageFinished && isSavable);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001870 // history and snapshots item are the members of COMBO menu group,
1871 // so if show history item, only make snapshots item invisible.
1872 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001873
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001874 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001875 }
1876
John Reck9c35b9c2012-05-30 10:08:50 -07001877 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001878 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001879 if (null == getCurrentTopWebView()) {
1880 return false;
1881 }
1882 if (mMenuIsDown) {
1883 // The shortcut action consumes the MENU. Even if it is still down,
1884 // it won't trigger the next shortcut action. In the case of the
1885 // shortcut action triggering a new activity, like Bookmarks, we
1886 // won't get onKeyUp for MENU. So it is important to reset it here.
1887 mMenuIsDown = false;
1888 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001889 if (mUi.onOptionsItemSelected(item)) {
1890 // ui callback handled it
1891 return true;
1892 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001893 switch (item.getItemId()) {
1894 // -- Main menu
1895 case R.id.new_tab_menu_id:
1896 openTabToHomePage();
1897 break;
1898
1899 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001900 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001901 break;
1902
Afzal Najamd4e33312012-04-26 01:54:01 -04001903 case R.id.close_other_tabs_id:
1904 closeOtherTabs();
1905 break;
1906
Michael Kolb8233fac2010-10-26 16:08:53 -07001907 case R.id.goto_menu_id:
1908 editUrl();
1909 break;
1910
1911 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001912 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1913 break;
1914
1915 case R.id.history_menu_id:
1916 bookmarksOrHistoryPicker(ComboViews.History);
1917 break;
1918
1919 case R.id.snapshots_menu_id:
1920 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001921 break;
1922
Michael Kolb8233fac2010-10-26 16:08:53 -07001923 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001924 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001925 break;
1926
1927 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001928 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001929 stopLoading();
1930 } else {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001931 Tab currentTab = mTabControl.getCurrentTab();
1932 if (currentTab.hasCrashed) {
1933 currentTab.replaceCrashView(getCurrentTopWebView(),
1934 currentTab.getViewContainer());
1935 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001936 getCurrentTopWebView().reload();
1937 }
1938 break;
1939
1940 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001941 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001942 break;
1943
1944 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001945 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001946 break;
1947
1948 case R.id.close_menu_id:
1949 // Close the subwindow if it exists.
1950 if (mTabControl.getCurrentSubWindow() != null) {
1951 dismissSubWindow(mTabControl.getCurrentTab());
1952 break;
1953 }
1954 closeCurrentTab();
1955 break;
1956
kaiyiza8b6dbb2013-07-29 18:11:22 +08001957 case R.id.exit_menu_id:
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001958 Object[] params = { new String("persist.debug.browsermonkeytest")};
1959 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -07001960 String ret = (String)ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001961 "android.os.SystemProperties","get", type, params);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001962 if (ret != null && ret.equals("enable"))
1963 break;
luxiaolb40014b2013-07-19 10:01:43 +08001964 showExitDialog(mActivity);
1965 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001966 case R.id.homepage_menu_id:
1967 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001968 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001969 break;
1970
1971 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001972 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001973 break;
1974
1975 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001976 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001977 break;
1978
John Reck2bc80422011-06-30 15:11:49 -07001979 case R.id.save_snapshot_menu_id:
1980 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001981 if (source == null) break;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001982 createScreenshotAsync(
1983 source.getWebView(),
1984 getDesiredThumbnailWidth(mActivity),
1985 getDesiredThumbnailHeight(mActivity),
1986 new ValueCallback<Bitmap>() {
1987 @Override
1988 public void onReceiveValue(Bitmap bitmap) {
1989 new SaveSnapshotTask(source, bitmap).execute();
1990 }
1991 });
Leon Scrogginsac993842011-02-02 12:54:07 -05001992 break;
1993
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001995 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001996 break;
1997
John Recke1a03a32011-09-14 17:04:16 -07001998 case R.id.snapshot_go_live:
1999 goLive();
2000 return true;
2001
Michael Kolb8233fac2010-10-26 16:08:53 -07002002 case R.id.share_page_menu_id:
2003 Tab currentTab = mTabControl.getCurrentTab();
2004 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002005 return false;
2006 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002007 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002008 break;
2009
2010 case R.id.dump_nav_menu_id:
2011 getCurrentTopWebView().debugDump();
2012 break;
2013
Michael Kolb8233fac2010-10-26 16:08:53 -07002014 case R.id.zoom_in_menu_id:
2015 getCurrentTopWebView().zoomIn();
2016 break;
2017
2018 case R.id.zoom_out_menu_id:
2019 getCurrentTopWebView().zoomOut();
2020 break;
2021
2022 case R.id.view_downloads_menu_id:
2023 viewDownloads();
2024 break;
2025
John Reck42229bc2011-08-19 13:26:43 -07002026 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002027 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002028 break;
2029
Michael Kolb8233fac2010-10-26 16:08:53 -07002030 case R.id.window_one_menu_id:
2031 case R.id.window_two_menu_id:
2032 case R.id.window_three_menu_id:
2033 case R.id.window_four_menu_id:
2034 case R.id.window_five_menu_id:
2035 case R.id.window_six_menu_id:
2036 case R.id.window_seven_menu_id:
2037 case R.id.window_eight_menu_id:
2038 {
2039 int menuid = item.getItemId();
2040 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2041 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2042 Tab desiredTab = mTabControl.getTab(id);
2043 if (desiredTab != null &&
2044 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002045 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002046 }
2047 break;
2048 }
2049 }
2050 }
2051 break;
2052
Axesh R. Ajmera652d08e2014-06-16 16:36:33 -07002053 case R.id.about_menu_id:
2054 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
2055 builder.setTitle(R.string.about);
2056 builder.setCancelable(true);
2057 String ua = "";
2058 final WebView currentWebView = getCurrentWebView();
2059 if (currentWebView != null) {
2060 final WebSettings s = currentWebView.getSettings();
2061 if (s != null) {
2062 ua = s.getUserAgentString();
2063 }
2064 }
2065 builder.setMessage("Agent:" + ua);
2066 builder.setPositiveButton(android.R.string.ok, null);
2067 builder.create().show();
2068 break;
2069
Michael Kolb8233fac2010-10-26 16:08:53 -07002070 default:
2071 return false;
2072 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002073 return true;
2074 }
2075
John Reck68234a92012-04-19 15:27:12 -07002076 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2077 implements OnCancelListener {
2078
2079 private Tab mTab;
2080 private Dialog mProgressDialog;
2081 private ContentValues mValues;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002082 private Bitmap mBitmap;
John Reck68234a92012-04-19 15:27:12 -07002083
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002084 private SaveSnapshotTask(Tab tab, Bitmap bm) {
John Reck68234a92012-04-19 15:27:12 -07002085 mTab = tab;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002086 mBitmap = bm;
John Reck68234a92012-04-19 15:27:12 -07002087 }
2088
2089 @Override
2090 protected void onPreExecute() {
2091 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2092 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2093 true, true, this);
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002094 mValues = mTab.createSnapshotValues(mBitmap);
John Reck68234a92012-04-19 15:27:12 -07002095 }
2096
2097 @Override
2098 protected Long doInBackground(Void... params) {
2099 if (!mTab.saveViewState(mValues)) {
2100 return null;
2101 }
2102 if (isCancelled()) {
2103 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2104 File file = mActivity.getFileStreamPath(path);
2105 if (!file.delete()) {
2106 file.deleteOnExit();
2107 }
2108 return null;
2109 }
2110 final ContentResolver cr = mActivity.getContentResolver();
2111 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2112 if (result == null) {
2113 return null;
2114 }
2115 long id = ContentUris.parseId(result);
2116 return id;
2117 }
2118
2119 @Override
2120 protected void onPostExecute(Long id) {
2121 if (isCancelled()) {
2122 return;
2123 }
2124 mProgressDialog.dismiss();
2125 if (id == null) {
2126 Toast.makeText(mActivity, R.string.snapshot_failed,
2127 Toast.LENGTH_SHORT).show();
2128 return;
2129 }
2130 Bundle b = new Bundle();
2131 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2132 mUi.showComboView(ComboViews.Snapshots, b);
2133 }
2134
2135 @Override
2136 public void onCancel(DialogInterface dialog) {
2137 cancel(true);
2138 }
2139 }
2140
Michael Kolb80f75082012-04-10 10:50:06 -07002141 @Override
2142 public void toggleUserAgent() {
2143 WebView web = getCurrentWebView();
2144 mSettings.toggleDesktopUseragent(web);
Tarun Nainani8eb00912014-07-17 12:28:32 -07002145 web.reload();
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}