Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.DownloadManager; |
| 21 | import android.app.SearchManager; |
| 22 | import android.content.ClipboardManager; |
| 23 | import android.content.ContentProvider; |
| 24 | import android.content.ContentProviderClient; |
| 25 | import android.content.ContentResolver; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 26 | import android.content.ContentUris; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 27 | import android.content.ContentValues; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.pm.PackageManager; |
| 31 | import android.content.pm.ResolveInfo; |
| 32 | import android.content.res.Configuration; |
John Reck | 30b065e | 2011-07-19 10:58:05 -0700 | [diff] [blame] | 33 | import android.content.res.TypedArray; |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 34 | import android.database.ContentObserver; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 35 | import android.database.Cursor; |
| 36 | import android.database.sqlite.SQLiteDatabase; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 37 | import android.graphics.Bitmap; |
| 38 | import android.graphics.Canvas; |
| 39 | import android.graphics.Picture; |
| 40 | import android.net.Uri; |
| 41 | import android.net.http.SslError; |
| 42 | import android.os.AsyncTask; |
| 43 | import android.os.Bundle; |
| 44 | import android.os.Handler; |
| 45 | import android.os.Message; |
| 46 | import android.os.PowerManager; |
| 47 | import android.os.PowerManager.WakeLock; |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 48 | import android.preference.PreferenceActivity; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 49 | import android.provider.Browser; |
| 50 | import android.provider.BrowserContract; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 51 | import android.provider.BrowserContract.Images; |
| 52 | import android.provider.ContactsContract; |
| 53 | import android.provider.ContactsContract.Intents.Insert; |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 54 | import android.speech.RecognizerIntent; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 55 | import android.text.TextUtils; |
| 56 | import android.util.Log; |
John Reck | a00cbbd | 2010-12-16 12:38:19 -0800 | [diff] [blame] | 57 | import android.util.Patterns; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 58 | import android.view.ActionMode; |
| 59 | import android.view.ContextMenu; |
| 60 | import android.view.ContextMenu.ContextMenuInfo; |
| 61 | import android.view.Gravity; |
| 62 | import android.view.KeyEvent; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 63 | import android.view.Menu; |
| 64 | import android.view.MenuInflater; |
| 65 | import android.view.MenuItem; |
| 66 | import android.view.MenuItem.OnMenuItemClickListener; |
| 67 | import android.view.View; |
| 68 | import android.webkit.CookieManager; |
| 69 | import android.webkit.CookieSyncManager; |
| 70 | import android.webkit.HttpAuthHandler; |
| 71 | import android.webkit.SslErrorHandler; |
| 72 | import android.webkit.ValueCallback; |
| 73 | import android.webkit.WebChromeClient; |
| 74 | import android.webkit.WebIconDatabase; |
| 75 | import android.webkit.WebSettings; |
| 76 | import android.webkit.WebView; |
Leon Scroggins | ac99384 | 2011-02-02 12:54:07 -0500 | [diff] [blame] | 77 | import android.widget.Toast; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 78 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 79 | import com.android.browser.IntentHandler.UrlData; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 80 | import com.android.browser.UI.ComboViews; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 81 | import com.android.browser.UI.DropdownChangeListener; |
| 82 | import com.android.browser.provider.BrowserProvider; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 83 | import com.android.browser.provider.BrowserProvider2.Thumbnails; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 84 | import com.android.browser.provider.SnapshotProvider.Snapshots; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 85 | import com.android.browser.search.SearchEngine; |
| 86 | import com.android.common.Search; |
| 87 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 88 | import java.io.ByteArrayOutputStream; |
| 89 | import java.io.File; |
| 90 | import java.net.URLEncoder; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 91 | import java.util.ArrayList; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 92 | import java.util.Calendar; |
| 93 | import java.util.HashMap; |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 94 | import java.util.List; |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 95 | import java.util.Map; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Controller for browser |
| 99 | */ |
| 100 | public class Controller |
| 101 | implements WebViewController, UiController { |
| 102 | |
| 103 | private static final String LOGTAG = "Controller"; |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 104 | private static final String SEND_APP_ID_EXTRA = |
| 105 | "android.speech.extras.SEND_APPLICATION_ID_EXTRA"; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 106 | private static final String INCOGNITO_URI = "browser:incognito"; |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 107 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 108 | |
| 109 | // public message ids |
| 110 | public final static int LOAD_URL = 1001; |
| 111 | public final static int STOP_LOAD = 1002; |
| 112 | |
| 113 | // Message Ids |
| 114 | private static final int FOCUS_NODE_HREF = 102; |
| 115 | private static final int RELEASE_WAKELOCK = 107; |
| 116 | |
| 117 | static final int UPDATE_BOOKMARK_THUMBNAIL = 108; |
| 118 | |
| 119 | private static final int OPEN_BOOKMARKS = 201; |
| 120 | |
| 121 | private static final int EMPTY_MENU = -1; |
| 122 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 123 | // activity requestCode |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 124 | final static int COMBO_VIEW = 1; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 125 | final static int PREFERENCES_PAGE = 3; |
| 126 | final static int FILE_SELECTED = 4; |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 127 | final static int AUTOFILL_SETUP = 5; |
| 128 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 129 | private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes |
| 130 | |
| 131 | // As the ids are dynamically created, we can't guarantee that they will |
| 132 | // be in sequence, so this static array maps ids to a window number. |
| 133 | final static private int[] WINDOW_SHORTCUT_ID_ARRAY = |
| 134 | { R.id.window_one_menu_id, R.id.window_two_menu_id, |
| 135 | R.id.window_three_menu_id, R.id.window_four_menu_id, |
| 136 | R.id.window_five_menu_id, R.id.window_six_menu_id, |
| 137 | R.id.window_seven_menu_id, R.id.window_eight_menu_id }; |
| 138 | |
| 139 | // "source" parameter for Google search through search key |
| 140 | final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key"; |
| 141 | // "source" parameter for Google search through simplily type |
| 142 | final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type"; |
| 143 | |
Guang Zhu | 9e78f51 | 2011-05-04 11:45:11 -0700 | [diff] [blame] | 144 | // "no-crash-recovery" parameter in intetnt to suppress crash recovery |
| 145 | final static String NO_CRASH_RECOVERY = "no-crash-recovery"; |
| 146 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 147 | private Activity mActivity; |
| 148 | private UI mUi; |
| 149 | private TabControl mTabControl; |
| 150 | private BrowserSettings mSettings; |
| 151 | private WebViewFactory mFactory; |
| 152 | |
| 153 | private WakeLock mWakeLock; |
| 154 | |
| 155 | private UrlHandler mUrlHandler; |
| 156 | private UploadHandler mUploadHandler; |
| 157 | private IntentHandler mIntentHandler; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 158 | private PageDialogsHandler mPageDialogsHandler; |
| 159 | private NetworkStateHandler mNetworkHandler; |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 160 | private NfcHandler mNfcHandler; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 161 | |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 162 | private Message mAutoFillSetupMessage; |
| 163 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 164 | private boolean mShouldShowErrorConsole; |
| 165 | |
| 166 | private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins; |
| 167 | |
| 168 | // FIXME, temp address onPrepareMenu performance problem. |
| 169 | // When we move everything out of view, we should rewrite this. |
| 170 | private int mCurrentMenuState = 0; |
| 171 | private int mMenuState = R.id.MAIN_MENU; |
| 172 | private int mOldMenuState = EMPTY_MENU; |
| 173 | private Menu mCachedMenu; |
| 174 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 175 | private boolean mMenuIsDown; |
| 176 | |
| 177 | // For select and find, we keep track of the ActionMode so that |
| 178 | // finish() can be called as desired. |
| 179 | private ActionMode mActionMode; |
| 180 | |
| 181 | /** |
| 182 | * Only meaningful when mOptionsMenuOpen is true. This variable keeps track |
| 183 | * of whether the configuration has changed. The first onMenuOpened call |
| 184 | * after a configuration change is simply a reopening of the same menu |
| 185 | * (i.e. mIconView did not change). |
| 186 | */ |
| 187 | private boolean mConfigChanged; |
| 188 | |
| 189 | /** |
| 190 | * Keeps track of whether the options menu is open. This is important in |
| 191 | * determining whether to show or hide the title bar overlay |
| 192 | */ |
| 193 | private boolean mOptionsMenuOpen; |
| 194 | |
| 195 | /** |
| 196 | * Whether or not the options menu is in its bigger, popup menu form. When |
| 197 | * true, we want the title bar overlay to be gone. When false, we do not. |
| 198 | * Only meaningful if mOptionsMenuOpen is true. |
| 199 | */ |
| 200 | private boolean mExtendedMenuOpen; |
| 201 | |
| 202 | private boolean mInLoad; |
| 203 | |
| 204 | private boolean mActivityPaused = true; |
| 205 | private boolean mLoadStopped; |
| 206 | |
| 207 | private Handler mHandler; |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 208 | // Checks to see when the bookmarks database has changed, and updates the |
| 209 | // Tabs' notion of whether they represent bookmarked sites. |
| 210 | private ContentObserver mBookmarksObserver; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 211 | private CrashRecoveryHandler mCrashRecoveryHandler; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 212 | |
John Reck | 30b065e | 2011-07-19 10:58:05 -0700 | [diff] [blame] | 213 | private boolean mSimulateActionBarOverlayMode; |
| 214 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 215 | private static class ClearThumbnails extends AsyncTask<File, Void, Void> { |
| 216 | @Override |
| 217 | public Void doInBackground(File... files) { |
| 218 | if (files != null) { |
| 219 | for (File f : files) { |
| 220 | if (!f.delete()) { |
| 221 | Log.e(LOGTAG, f.getPath() + " was not deleted"); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | return null; |
| 226 | } |
| 227 | } |
| 228 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 229 | public Controller(Activity browser, boolean preloadCrashState) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 230 | mActivity = browser; |
| 231 | mSettings = BrowserSettings.getInstance(); |
| 232 | mTabControl = new TabControl(this); |
| 233 | mSettings.setController(this); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 234 | mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 235 | if (preloadCrashState) { |
| 236 | mCrashRecoveryHandler.preloadCrashState(); |
| 237 | } |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 238 | mFactory = new BrowserWebViewFactory(browser); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 239 | |
| 240 | mUrlHandler = new UrlHandler(this); |
| 241 | mIntentHandler = new IntentHandler(mActivity, this); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 242 | mPageDialogsHandler = new PageDialogsHandler(mActivity, this); |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 243 | mNfcHandler = new NfcHandler(mActivity, this); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 244 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 245 | startHandler(); |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 246 | mBookmarksObserver = new ContentObserver(mHandler) { |
| 247 | @Override |
| 248 | public void onChange(boolean selfChange) { |
| 249 | int size = mTabControl.getTabCount(); |
| 250 | for (int i = 0; i < size; i++) { |
| 251 | mTabControl.getTab(i).updateBookmarkedStatus(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | }; |
| 256 | browser.getContentResolver().registerContentObserver( |
| 257 | BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 258 | |
| 259 | mNetworkHandler = new NetworkStateHandler(mActivity, this); |
| 260 | // Start watching the default geolocation permissions |
| 261 | mSystemAllowGeolocationOrigins = |
| 262 | new SystemAllowGeolocationOrigins(mActivity.getApplicationContext()); |
| 263 | mSystemAllowGeolocationOrigins.start(); |
| 264 | |
John Reck | af262e7 | 2011-07-25 13:55:44 -0700 | [diff] [blame] | 265 | openIconDatabase(); |
John Reck | 30b065e | 2011-07-19 10:58:05 -0700 | [diff] [blame] | 266 | mSimulateActionBarOverlayMode = !BrowserActivity.isTablet(mActivity); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 269 | void start(final Bundle icicle, final Intent intent) { |
Guang Zhu | 9e78f51 | 2011-05-04 11:45:11 -0700 | [diff] [blame] | 270 | boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false); |
| 271 | if (icicle != null || noCrashRecovery) { |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 272 | doStart(icicle, intent); |
| 273 | } else { |
| 274 | mCrashRecoveryHandler.startRecovery(intent); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void doStart(final Bundle icicle, final Intent intent) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 279 | // Unless the last browser usage was within 24 hours, destroy any |
| 280 | // remaining incognito tabs. |
| 281 | |
| 282 | Calendar lastActiveDate = icicle != null ? |
| 283 | (Calendar) icicle.getSerializable("lastActiveDate") : null; |
| 284 | Calendar today = Calendar.getInstance(); |
| 285 | Calendar yesterday = Calendar.getInstance(); |
| 286 | yesterday.add(Calendar.DATE, -1); |
| 287 | |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 288 | final boolean restoreIncognitoTabs = !(lastActiveDate == null |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 289 | || lastActiveDate.before(yesterday) |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 290 | || lastActiveDate.after(today)); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 291 | |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 292 | // Find out if we will restore any state and remember the tab. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 293 | final long currentTabId = |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 294 | mTabControl.canRestoreState(icicle, restoreIncognitoTabs); |
Kristian Monsen | 2cd9701 | 2010-12-07 11:11:40 +0000 | [diff] [blame] | 295 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 296 | if (currentTabId == -1) { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 297 | // Not able to restore so we go ahead and clear session cookies. We |
| 298 | // must do this before trying to login the user as we don't want to |
| 299 | // clear any session cookies set during login. |
| 300 | CookieManager.getInstance().removeSessionCookie(); |
| 301 | } |
| 302 | |
Patrick Scott | d43e75a | 2011-03-14 14:47:23 -0400 | [diff] [blame] | 303 | GoogleAccountLogin.startLoginIfNeeded(mActivity, |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 304 | new Runnable() { |
| 305 | @Override public void run() { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 306 | onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs); |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 307 | } |
| 308 | }); |
| 309 | } |
| 310 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 311 | private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId, |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 312 | boolean restoreIncognitoTabs) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 313 | if (currentTabId == -1) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 314 | BackgroundHandler.execute(new PruneThumbnails(mActivity, null)); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 315 | final Bundle extra = intent.getExtras(); |
| 316 | // Create an initial tab. |
| 317 | // If the intent is ACTION_VIEW and data is not null, the Browser is |
| 318 | // invoked to view the content by another application. In this case, |
| 319 | // the tab will be close when exit. |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 320 | UrlData urlData = IntentHandler.getUrlDataFromIntent(intent); |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 321 | Tab t = null; |
| 322 | if (urlData.isEmpty()) { |
| 323 | t = openTabToHomePage(); |
| 324 | } else { |
| 325 | t = openTab(urlData); |
| 326 | } |
| 327 | if (t != null) { |
| 328 | t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID)); |
| 329 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 330 | WebView webView = t.getWebView(); |
| 331 | if (extra != null) { |
| 332 | int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0); |
| 333 | if (scale > 0 && scale <= 1000) { |
| 334 | webView.setInitialScale(scale); |
| 335 | } |
| 336 | } |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 337 | mUi.updateTabs(mTabControl.getTabs()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 338 | } else { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 339 | mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs, |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 340 | mUi.needsRestoreAllTabs()); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 341 | List<Tab> tabs = mTabControl.getTabs(); |
| 342 | ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size()); |
| 343 | for (Tab t : tabs) { |
| 344 | restoredTabs.add(t.getId()); |
| 345 | } |
| 346 | BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs)); |
| 347 | mUi.updateTabs(tabs); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 348 | // TabControl.restoreState() will create a new tab even if |
| 349 | // restoring the state fails. |
| 350 | setActiveTab(mTabControl.getCurrentTab()); |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 351 | // Handle the intent |
| 352 | mIntentHandler.onNewIntent(intent); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 353 | } |
| 354 | // clear up the thumbnail directory, which is no longer used; |
| 355 | // ideally this should only be run once after an upgrade from |
| 356 | // a previous version of the browser |
| 357 | new ClearThumbnails().execute(mTabControl.getThumbnailDir() |
| 358 | .listFiles()); |
| 359 | // Read JavaScript flags if it exists. |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 360 | String jsFlags = getSettings().getJsEngineFlags(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 361 | if (jsFlags.trim().length() != 0) { |
| 362 | getCurrentWebView().setJsFlags(jsFlags); |
| 363 | } |
John Reck | 439c9a5 | 2010-12-14 10:04:39 -0800 | [diff] [blame] | 364 | if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) { |
| 365 | bookmarksOrHistoryPicker(false); |
| 366 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 367 | } |
| 368 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 369 | private static class PruneThumbnails implements Runnable { |
| 370 | private Context mContext; |
| 371 | private List<Long> mIds; |
| 372 | |
| 373 | PruneThumbnails(Context context, List<Long> preserveIds) { |
| 374 | mContext = context.getApplicationContext(); |
| 375 | mIds = preserveIds; |
| 376 | } |
| 377 | |
| 378 | @Override |
| 379 | public void run() { |
| 380 | ContentResolver cr = mContext.getContentResolver(); |
| 381 | if (mIds == null || mIds.size() == 0) { |
| 382 | cr.delete(Thumbnails.CONTENT_URI, null, null); |
| 383 | } else { |
| 384 | int length = mIds.size(); |
| 385 | StringBuilder where = new StringBuilder(); |
| 386 | where.append(Thumbnails._ID); |
| 387 | where.append(" not in ("); |
| 388 | for (int i = 0; i < length; i++) { |
| 389 | where.append(mIds.get(i)); |
| 390 | if (i < (length - 1)) { |
| 391 | where.append(","); |
| 392 | } |
| 393 | } |
| 394 | where.append(")"); |
| 395 | cr.delete(Thumbnails.CONTENT_URI, where.toString(), null); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | } |
| 400 | |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 401 | @Override |
| 402 | public WebViewFactory getWebViewFactory() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 403 | return mFactory; |
| 404 | } |
| 405 | |
| 406 | @Override |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 407 | public void onSetWebView(Tab tab, WebView view) { |
| 408 | mUi.onSetWebView(tab, view); |
| 409 | } |
| 410 | |
| 411 | @Override |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 412 | public void createSubWindow(Tab tab) { |
| 413 | endActionMode(); |
| 414 | WebView mainView = tab.getWebView(); |
| 415 | WebView subView = mFactory.createWebView((mainView == null) |
| 416 | ? false |
| 417 | : mainView.isPrivateBrowsingEnabled()); |
| 418 | mUi.createSubWindow(tab, subView); |
| 419 | } |
| 420 | |
| 421 | @Override |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 422 | public Context getContext() { |
| 423 | return mActivity; |
| 424 | } |
| 425 | |
| 426 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 427 | public Activity getActivity() { |
| 428 | return mActivity; |
| 429 | } |
| 430 | |
| 431 | void setUi(UI ui) { |
| 432 | mUi = ui; |
| 433 | } |
| 434 | |
| 435 | BrowserSettings getSettings() { |
| 436 | return mSettings; |
| 437 | } |
| 438 | |
| 439 | IntentHandler getIntentHandler() { |
| 440 | return mIntentHandler; |
| 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public UI getUi() { |
| 445 | return mUi; |
| 446 | } |
| 447 | |
| 448 | int getMaxTabs() { |
| 449 | return mActivity.getResources().getInteger(R.integer.max_tabs); |
| 450 | } |
| 451 | |
| 452 | @Override |
| 453 | public TabControl getTabControl() { |
| 454 | return mTabControl; |
| 455 | } |
| 456 | |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 457 | @Override |
| 458 | public List<Tab> getTabs() { |
| 459 | return mTabControl.getTabs(); |
| 460 | } |
| 461 | |
John Reck | af262e7 | 2011-07-25 13:55:44 -0700 | [diff] [blame] | 462 | // Open the icon database. |
| 463 | private void openIconDatabase() { |
| 464 | // We have to call getInstance on the UI thread |
| 465 | final WebIconDatabase instance = WebIconDatabase.getInstance(); |
| 466 | BackgroundHandler.execute(new Runnable() { |
Ben Murdoch | 9446b93 | 2010-11-25 16:20:14 +0000 | [diff] [blame] | 467 | |
John Reck | af262e7 | 2011-07-25 13:55:44 -0700 | [diff] [blame] | 468 | @Override |
| 469 | public void run() { |
| 470 | instance.open(mActivity.getDir("icons", 0).getPath()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 471 | } |
John Reck | af262e7 | 2011-07-25 13:55:44 -0700 | [diff] [blame] | 472 | }); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | private void startHandler() { |
| 476 | mHandler = new Handler() { |
| 477 | |
| 478 | @Override |
| 479 | public void handleMessage(Message msg) { |
| 480 | switch (msg.what) { |
| 481 | case OPEN_BOOKMARKS: |
| 482 | bookmarksOrHistoryPicker(false); |
| 483 | break; |
| 484 | case FOCUS_NODE_HREF: |
| 485 | { |
| 486 | String url = (String) msg.getData().get("url"); |
| 487 | String title = (String) msg.getData().get("title"); |
Cary Clark | 043c2d6 | 2010-12-15 11:19:39 -0500 | [diff] [blame] | 488 | String src = (String) msg.getData().get("src"); |
| 489 | if (url == "") url = src; // use image if no anchor |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 490 | if (TextUtils.isEmpty(url)) { |
| 491 | break; |
| 492 | } |
| 493 | HashMap focusNodeMap = (HashMap) msg.obj; |
| 494 | WebView view = (WebView) focusNodeMap.get("webview"); |
| 495 | // Only apply the action if the top window did not change. |
| 496 | if (getCurrentTopWebView() != view) { |
| 497 | break; |
| 498 | } |
| 499 | switch (msg.arg1) { |
| 500 | case R.id.open_context_menu_id: |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 501 | loadUrlFromContext(url); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 502 | break; |
Cary Clark | 043c2d6 | 2010-12-15 11:19:39 -0500 | [diff] [blame] | 503 | case R.id.view_image_context_menu_id: |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 504 | loadUrlFromContext(src); |
Cary Clark | 043c2d6 | 2010-12-15 11:19:39 -0500 | [diff] [blame] | 505 | break; |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 506 | case R.id.open_newtab_context_menu_id: |
| 507 | final Tab parent = mTabControl.getCurrentTab(); |
John Reck | 5949c66 | 2011-05-27 09:52:29 -0700 | [diff] [blame] | 508 | openTab(url, parent, |
| 509 | !mSettings.openInBackground(), true); |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 510 | break; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 511 | case R.id.copy_link_context_menu_id: |
| 512 | copy(url); |
| 513 | break; |
| 514 | case R.id.save_link_context_menu_id: |
| 515 | case R.id.download_context_menu_id: |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 516 | DownloadHandler.onDownloadStartNoStream( |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 517 | mActivity, url, null, null, null, |
| 518 | view.isPrivateBrowsingEnabled()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 519 | break; |
| 520 | } |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | case LOAD_URL: |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 525 | loadUrlFromContext((String) msg.obj); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 526 | break; |
| 527 | |
| 528 | case STOP_LOAD: |
| 529 | stopLoading(); |
| 530 | break; |
| 531 | |
| 532 | case RELEASE_WAKELOCK: |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 533 | if (mWakeLock != null && mWakeLock.isHeld()) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 534 | mWakeLock.release(); |
| 535 | // if we reach here, Browser should be still in the |
| 536 | // background loading after WAKELOCK_TIMEOUT (5-min). |
| 537 | // To avoid burning the battery, stop loading. |
| 538 | mTabControl.stopAllLoading(); |
| 539 | } |
| 540 | break; |
| 541 | |
| 542 | case UPDATE_BOOKMARK_THUMBNAIL: |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 543 | Tab tab = (Tab) msg.obj; |
| 544 | if (tab != null) { |
| 545 | updateScreenshot(tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 546 | } |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | }; |
| 551 | |
| 552 | } |
| 553 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 554 | @Override |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 555 | public Tab getCurrentTab() { |
| 556 | return mTabControl.getCurrentTab(); |
| 557 | } |
| 558 | |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 559 | @Override |
| 560 | public void shareCurrentPage() { |
| 561 | shareCurrentPage(mTabControl.getCurrentTab()); |
| 562 | } |
| 563 | |
| 564 | private void shareCurrentPage(Tab tab) { |
| 565 | if (tab != null) { |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 566 | sharePage(mActivity, tab.getTitle(), |
| 567 | tab.getUrl(), tab.getFavicon(), |
| 568 | createScreenshot(tab.getWebView(), |
| 569 | getDesiredThumbnailWidth(mActivity), |
| 570 | getDesiredThumbnailHeight(mActivity))); |
| 571 | } |
| 572 | } |
| 573 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 574 | /** |
| 575 | * Share a page, providing the title, url, favicon, and a screenshot. Uses |
| 576 | * an {@link Intent} to launch the Activity chooser. |
| 577 | * @param c Context used to launch a new Activity. |
| 578 | * @param title Title of the page. Stored in the Intent with |
| 579 | * {@link Intent#EXTRA_SUBJECT} |
| 580 | * @param url URL of the page. Stored in the Intent with |
| 581 | * {@link Intent#EXTRA_TEXT} |
| 582 | * @param favicon Bitmap of the favicon for the page. Stored in the Intent |
| 583 | * with {@link Browser#EXTRA_SHARE_FAVICON} |
| 584 | * @param screenshot Bitmap of a screenshot of the page. Stored in the |
| 585 | * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT} |
| 586 | */ |
| 587 | static final void sharePage(Context c, String title, String url, |
| 588 | Bitmap favicon, Bitmap screenshot) { |
| 589 | Intent send = new Intent(Intent.ACTION_SEND); |
| 590 | send.setType("text/plain"); |
| 591 | send.putExtra(Intent.EXTRA_TEXT, url); |
| 592 | send.putExtra(Intent.EXTRA_SUBJECT, title); |
| 593 | send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon); |
| 594 | send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot); |
| 595 | try { |
| 596 | c.startActivity(Intent.createChooser(send, c.getString( |
| 597 | R.string.choosertitle_sharevia))); |
| 598 | } catch(android.content.ActivityNotFoundException ex) { |
| 599 | // if no app handles it, do nothing |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | private void copy(CharSequence text) { |
| 604 | ClipboardManager cm = (ClipboardManager) mActivity |
| 605 | .getSystemService(Context.CLIPBOARD_SERVICE); |
| 606 | cm.setText(text); |
| 607 | } |
| 608 | |
| 609 | // lifecycle |
| 610 | |
| 611 | protected void onConfgurationChanged(Configuration config) { |
| 612 | mConfigChanged = true; |
| 613 | if (mPageDialogsHandler != null) { |
| 614 | mPageDialogsHandler.onConfigurationChanged(config); |
| 615 | } |
| 616 | mUi.onConfigurationChanged(config); |
| 617 | } |
| 618 | |
| 619 | @Override |
| 620 | public void handleNewIntent(Intent intent) { |
| 621 | mIntentHandler.onNewIntent(intent); |
| 622 | } |
| 623 | |
| 624 | protected void onPause() { |
Michael Kolb | 11fe02d | 2011-02-02 09:52:16 -0800 | [diff] [blame] | 625 | if (mUi.isCustomViewShowing()) { |
| 626 | hideCustomView(); |
| 627 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 628 | if (mActivityPaused) { |
| 629 | Log.e(LOGTAG, "BrowserActivity is already paused."); |
| 630 | return; |
| 631 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 632 | mActivityPaused = true; |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 633 | Tab tab = mTabControl.getCurrentTab(); |
| 634 | if (tab != null) { |
| 635 | tab.pause(); |
| 636 | if (!pauseWebViewTimers(tab)) { |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 637 | if (mWakeLock == null) { |
| 638 | PowerManager pm = (PowerManager) mActivity |
| 639 | .getSystemService(Context.POWER_SERVICE); |
| 640 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser"); |
| 641 | } |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 642 | mWakeLock.acquire(); |
| 643 | mHandler.sendMessageDelayed(mHandler |
| 644 | .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT); |
| 645 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 646 | } |
| 647 | mUi.onPause(); |
| 648 | mNetworkHandler.onPause(); |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 649 | mNfcHandler.onPause(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 650 | |
| 651 | WebView.disablePlatformNotifications(); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 652 | mCrashRecoveryHandler.backupState(); |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 653 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 654 | } |
| 655 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 656 | void onSaveInstanceState(Bundle outState) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 657 | // the default implementation requires each view to have an id. As the |
| 658 | // browser handles the state itself and it doesn't use id for the views, |
| 659 | // don't call the default implementation. Otherwise it will trigger the |
| 660 | // warning like this, "couldn't save which view has focus because the |
| 661 | // focused view XXX has no id". |
| 662 | |
| 663 | // Save all the tabs |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 664 | mTabControl.saveState(outState); |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 665 | if (!outState.isEmpty()) { |
| 666 | // Save time so that we know how old incognito tabs (if any) are. |
| 667 | outState.putSerializable("lastActiveDate", Calendar.getInstance()); |
| 668 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | void onResume() { |
| 672 | if (!mActivityPaused) { |
| 673 | Log.e(LOGTAG, "BrowserActivity is already resumed."); |
| 674 | return; |
| 675 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 676 | mActivityPaused = false; |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 677 | Tab current = mTabControl.getCurrentTab(); |
| 678 | if (current != null) { |
| 679 | current.resume(); |
| 680 | resumeWebViewTimers(current); |
| 681 | } |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 682 | releaseWakeLock(); |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 683 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 684 | mUi.onResume(); |
| 685 | mNetworkHandler.onResume(); |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 686 | mNfcHandler.onResume(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 687 | WebView.enablePlatformNotifications(); |
| 688 | } |
| 689 | |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 690 | private void releaseWakeLock() { |
| 691 | if (mWakeLock != null && mWakeLock.isHeld()) { |
| 692 | mHandler.removeMessages(RELEASE_WAKELOCK); |
| 693 | mWakeLock.release(); |
| 694 | } |
| 695 | } |
| 696 | |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 697 | /** |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 698 | * resume all WebView timers using the WebView instance of the given tab |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 699 | * @param tab guaranteed non-null |
| 700 | */ |
| 701 | private void resumeWebViewTimers(Tab tab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 702 | boolean inLoad = tab.inPageLoad(); |
| 703 | if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) { |
| 704 | CookieSyncManager.getInstance().startSync(); |
| 705 | WebView w = tab.getWebView(); |
Mathew Inwood | e1dbb95 | 2011-07-08 17:27:38 +0100 | [diff] [blame] | 706 | WebViewTimersControl.getInstance().onBrowserActivityResume(w); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 710 | /** |
| 711 | * Pause all WebView timers using the WebView of the given tab |
| 712 | * @param tab |
| 713 | * @return true if the timers are paused or tab is null |
| 714 | */ |
| 715 | private boolean pauseWebViewTimers(Tab tab) { |
| 716 | if (tab == null) { |
| 717 | return true; |
| 718 | } else if (!tab.inPageLoad()) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 719 | CookieSyncManager.getInstance().stopSync(); |
Mathew Inwood | e1dbb95 | 2011-07-08 17:27:38 +0100 | [diff] [blame] | 720 | WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 721 | return true; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 722 | } |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 723 | return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | void onDestroy() { |
John Reck | 38b4bf5 | 2011-02-22 14:39:34 -0800 | [diff] [blame] | 727 | if (mUploadHandler != null && !mUploadHandler.handled()) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 728 | mUploadHandler.onResult(Activity.RESULT_CANCELED, null); |
| 729 | mUploadHandler = null; |
| 730 | } |
| 731 | if (mTabControl == null) return; |
| 732 | mUi.onDestroy(); |
| 733 | // Remove the current tab and sub window |
| 734 | Tab t = mTabControl.getCurrentTab(); |
| 735 | if (t != null) { |
| 736 | dismissSubWindow(t); |
| 737 | removeTab(t); |
| 738 | } |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 739 | mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 740 | // Destroy all the tabs |
| 741 | mTabControl.destroy(); |
| 742 | WebIconDatabase.getInstance().close(); |
| 743 | // Stop watching the default geolocation permissions |
| 744 | mSystemAllowGeolocationOrigins.stop(); |
| 745 | mSystemAllowGeolocationOrigins = null; |
| 746 | } |
| 747 | |
| 748 | protected boolean isActivityPaused() { |
| 749 | return mActivityPaused; |
| 750 | } |
| 751 | |
| 752 | protected void onLowMemory() { |
| 753 | mTabControl.freeMemory(); |
| 754 | } |
| 755 | |
| 756 | @Override |
| 757 | public boolean shouldShowErrorConsole() { |
| 758 | return mShouldShowErrorConsole; |
| 759 | } |
| 760 | |
| 761 | protected void setShouldShowErrorConsole(boolean show) { |
| 762 | if (show == mShouldShowErrorConsole) { |
| 763 | // Nothing to do. |
| 764 | return; |
| 765 | } |
| 766 | mShouldShowErrorConsole = show; |
| 767 | Tab t = mTabControl.getCurrentTab(); |
| 768 | if (t == null) { |
| 769 | // There is no current tab so we cannot toggle the error console |
| 770 | return; |
| 771 | } |
| 772 | mUi.setShouldShowErrorConsole(t, show); |
| 773 | } |
| 774 | |
| 775 | @Override |
| 776 | public void stopLoading() { |
| 777 | mLoadStopped = true; |
| 778 | Tab tab = mTabControl.getCurrentTab(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 779 | WebView w = getCurrentTopWebView(); |
| 780 | w.stopLoading(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 781 | mUi.onPageStopped(tab); |
| 782 | } |
| 783 | |
| 784 | boolean didUserStopLoading() { |
| 785 | return mLoadStopped; |
| 786 | } |
| 787 | |
| 788 | // WebViewController |
| 789 | |
| 790 | @Override |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 791 | public void onPageStarted(Tab tab, WebView view, Bitmap favicon) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 792 | |
| 793 | // We've started to load a new page. If there was a pending message |
| 794 | // to save a screenshot then we will now take the new page and save |
| 795 | // an incorrect screenshot. Therefore, remove any pending thumbnail |
| 796 | // messages from the queue. |
| 797 | mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 798 | tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 799 | |
| 800 | // reset sync timer to avoid sync starts during loading a page |
| 801 | CookieSyncManager.getInstance().resetSync(); |
| 802 | |
| 803 | if (!mNetworkHandler.isNetworkUp()) { |
| 804 | view.setNetworkAvailable(false); |
| 805 | } |
| 806 | |
| 807 | // when BrowserActivity just starts, onPageStarted may be called before |
| 808 | // onResume as it is triggered from onCreate. Call resumeWebViewTimers |
| 809 | // to start the timer. As we won't switch tabs while an activity is in |
| 810 | // pause state, we can ensure calling resume and pause in pair. |
| 811 | if (mActivityPaused) { |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 812 | resumeWebViewTimers(tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 813 | } |
| 814 | mLoadStopped = false; |
| 815 | if (!mNetworkHandler.isNetworkUp()) { |
| 816 | mNetworkHandler.createAndShowNetworkDialog(); |
| 817 | } |
| 818 | endActionMode(); |
| 819 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 820 | mUi.onTabDataChanged(tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 821 | |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 822 | String url = tab.getUrl(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 823 | // update the bookmark database for favicon |
| 824 | maybeUpdateFavicon(tab, null, url, favicon); |
| 825 | |
| 826 | Performance.tracePageStart(url); |
| 827 | |
| 828 | // Performance probe |
| 829 | if (false) { |
| 830 | Performance.onPageStarted(); |
| 831 | } |
| 832 | |
| 833 | } |
| 834 | |
| 835 | @Override |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 836 | public void onPageFinished(Tab tab) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 837 | mUi.onTabDataChanged(tab); |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 838 | if (!tab.isPrivateBrowsingEnabled() |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 839 | && !TextUtils.isEmpty(tab.getUrl()) |
| 840 | && !tab.isSnapshot()) { |
John Reck | a169628 | 2011-07-08 14:10:37 -0700 | [diff] [blame] | 841 | // Only update the bookmark screenshot if the user did not |
| 842 | // cancel the load early and there is not already |
| 843 | // a pending update for the tab. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 844 | if (tab.inForeground() && !didUserStopLoading() |
| 845 | || !tab.inForeground()) { |
John Reck | a169628 | 2011-07-08 14:10:37 -0700 | [diff] [blame] | 846 | if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) { |
| 847 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 848 | UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab), |
| 849 | 500); |
| 850 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | // pause the WebView timer and release the wake lock if it is finished |
| 854 | // while BrowserActivity is in pause state. |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 855 | if (mActivityPaused && pauseWebViewTimers(tab)) { |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 856 | releaseWakeLock(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 857 | } |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 858 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 859 | // Performance probe |
| 860 | if (false) { |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 861 | Performance.onPageFinished(tab.getUrl()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | Performance.tracePageFinished(); |
| 865 | } |
| 866 | |
| 867 | @Override |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 868 | public void onProgressChanged(Tab tab) { |
| 869 | int newProgress = tab.getLoadProgress(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 870 | |
| 871 | if (newProgress == 100) { |
| 872 | CookieSyncManager.getInstance().sync(); |
| 873 | // onProgressChanged() may continue to be called after the main |
| 874 | // frame has finished loading, as any remaining sub frames continue |
| 875 | // to load. We'll only get called once though with newProgress as |
| 876 | // 100 when everything is loaded. (onPageFinished is called once |
| 877 | // when the main frame completes loading regardless of the state of |
| 878 | // any sub frames so calls to onProgressChanges may continue after |
| 879 | // onPageFinished has executed) |
| 880 | if (mInLoad) { |
| 881 | mInLoad = false; |
| 882 | updateInLoadMenuItems(mCachedMenu); |
| 883 | } |
| 884 | } else { |
| 885 | if (!mInLoad) { |
| 886 | // onPageFinished may have already been called but a subframe is |
| 887 | // still loading and updating the progress. Reset mInLoad and |
| 888 | // update the menu items. |
| 889 | mInLoad = true; |
| 890 | updateInLoadMenuItems(mCachedMenu); |
| 891 | } |
| 892 | } |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 893 | mUi.onProgressChanged(tab); |
| 894 | } |
| 895 | |
| 896 | @Override |
| 897 | public void onUpdatedLockIcon(Tab tab) { |
| 898 | mUi.onTabDataChanged(tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | @Override |
| 902 | public void onReceivedTitle(Tab tab, final String title) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 903 | mUi.onTabDataChanged(tab); |
John Reck | 49a603c | 2011-03-03 09:33:05 -0800 | [diff] [blame] | 904 | final String pageUrl = tab.getOriginalUrl(); |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 905 | if (TextUtils.isEmpty(pageUrl) || pageUrl.length() |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 906 | >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) { |
| 907 | return; |
| 908 | } |
| 909 | // Update the title in the history database if not in private browsing mode |
| 910 | if (!tab.isPrivateBrowsingEnabled()) { |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 911 | DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | |
| 915 | @Override |
| 916 | public void onFavicon(Tab tab, WebView view, Bitmap icon) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 917 | mUi.onTabDataChanged(tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 918 | maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon); |
| 919 | } |
| 920 | |
| 921 | @Override |
Michael Kolb | 18eb377 | 2010-12-10 14:29:51 -0800 | [diff] [blame] | 922 | public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) { |
| 923 | return mUrlHandler.shouldOverrideUrlLoading(tab, view, url); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | @Override |
| 927 | public boolean shouldOverrideKeyEvent(KeyEvent event) { |
| 928 | if (mMenuIsDown) { |
| 929 | // only check shortcut key when MENU is held |
| 930 | return mActivity.getWindow().isShortcutKey(event.getKeyCode(), |
| 931 | event); |
| 932 | } else { |
| 933 | return false; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | @Override |
| 938 | public void onUnhandledKeyEvent(KeyEvent event) { |
| 939 | if (!isActivityPaused()) { |
| 940 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| 941 | mActivity.onKeyDown(event.getKeyCode(), event); |
| 942 | } else { |
| 943 | mActivity.onKeyUp(event.getKeyCode(), event); |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | @Override |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 949 | public void doUpdateVisitedHistory(Tab tab, boolean isReload) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 950 | // Don't save anything in private browsing mode |
| 951 | if (tab.isPrivateBrowsingEnabled()) return; |
John Reck | 49a603c | 2011-03-03 09:33:05 -0800 | [diff] [blame] | 952 | String url = tab.getOriginalUrl(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 953 | |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 954 | if (TextUtils.isEmpty(url) |
| 955 | || url.regionMatches(true, 0, "about:", 0, 6)) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 956 | return; |
| 957 | } |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 958 | DataController.getInstance(mActivity).updateVisitedHistory(url); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 959 | if (!mActivityPaused) { |
| 960 | // Since we clear the state in onPause, don't backup the current |
| 961 | // state if we are already paused |
| 962 | mCrashRecoveryHandler.backupState(); |
| 963 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | @Override |
| 967 | public void getVisitedHistory(final ValueCallback<String[]> callback) { |
| 968 | AsyncTask<Void, Void, String[]> task = |
| 969 | new AsyncTask<Void, Void, String[]>() { |
| 970 | @Override |
| 971 | public String[] doInBackground(Void... unused) { |
| 972 | return Browser.getVisitedHistory(mActivity.getContentResolver()); |
| 973 | } |
| 974 | @Override |
| 975 | public void onPostExecute(String[] result) { |
| 976 | callback.onReceiveValue(result); |
| 977 | } |
| 978 | }; |
| 979 | task.execute(); |
| 980 | } |
| 981 | |
| 982 | @Override |
| 983 | public void onReceivedHttpAuthRequest(Tab tab, WebView view, |
| 984 | final HttpAuthHandler handler, final String host, |
| 985 | final String realm) { |
| 986 | String username = null; |
| 987 | String password = null; |
| 988 | |
| 989 | boolean reuseHttpAuthUsernamePassword |
| 990 | = handler.useHttpAuthUsernamePassword(); |
| 991 | |
| 992 | if (reuseHttpAuthUsernamePassword && view != null) { |
| 993 | String[] credentials = view.getHttpAuthUsernamePassword(host, realm); |
| 994 | if (credentials != null && credentials.length == 2) { |
| 995 | username = credentials[0]; |
| 996 | password = credentials[1]; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | if (username != null && password != null) { |
| 1001 | handler.proceed(username, password); |
| 1002 | } else { |
Ben Murdoch | fdd3711 | 2011-08-05 15:48:14 +0100 | [diff] [blame^] | 1003 | if (tab.inForeground() && !handler.suppressDialog()) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1004 | mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm); |
| 1005 | } else { |
| 1006 | handler.cancel(); |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | @Override |
| 1012 | public void onDownloadStart(Tab tab, String url, String userAgent, |
| 1013 | String contentDisposition, String mimetype, long contentLength) { |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 1014 | WebView w = tab.getWebView(); |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 1015 | DownloadHandler.onDownloadStart(mActivity, url, userAgent, |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 1016 | contentDisposition, mimetype, w.isPrivateBrowsingEnabled()); |
| 1017 | if (w.copyBackForwardList().getSize() == 0) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1018 | // This Tab was opened for the sole purpose of downloading a |
| 1019 | // file. Remove it. |
| 1020 | if (tab == mTabControl.getCurrentTab()) { |
| 1021 | // In this case, the Tab is still on top. |
| 1022 | goBackOnePageOrQuit(); |
| 1023 | } else { |
| 1024 | // In this case, it is not. |
| 1025 | closeTab(tab); |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | @Override |
| 1031 | public Bitmap getDefaultVideoPoster() { |
| 1032 | return mUi.getDefaultVideoPoster(); |
| 1033 | } |
| 1034 | |
| 1035 | @Override |
| 1036 | public View getVideoLoadingProgressView() { |
| 1037 | return mUi.getVideoLoadingProgressView(); |
| 1038 | } |
| 1039 | |
| 1040 | @Override |
| 1041 | public void showSslCertificateOnError(WebView view, SslErrorHandler handler, |
| 1042 | SslError error) { |
| 1043 | mPageDialogsHandler.showSSLCertificateOnError(view, handler, error); |
| 1044 | } |
| 1045 | |
Patrick Scott | 9206677 | 2011-03-10 08:46:27 -0500 | [diff] [blame] | 1046 | @Override |
| 1047 | public void showAutoLogin(Tab tab) { |
| 1048 | assert tab.inForeground(); |
| 1049 | // Update the title bar to show the auto-login request. |
| 1050 | mUi.showAutoLogin(tab); |
| 1051 | } |
| 1052 | |
| 1053 | @Override |
| 1054 | public void hideAutoLogin(Tab tab) { |
| 1055 | assert tab.inForeground(); |
| 1056 | mUi.hideAutoLogin(tab); |
| 1057 | } |
| 1058 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1059 | // helper method |
| 1060 | |
| 1061 | /* |
| 1062 | * Update the favorites icon if the private browsing isn't enabled and the |
| 1063 | * icon is valid. |
| 1064 | */ |
| 1065 | private void maybeUpdateFavicon(Tab tab, final String originalUrl, |
| 1066 | final String url, Bitmap favicon) { |
| 1067 | if (favicon == null) { |
| 1068 | return; |
| 1069 | } |
| 1070 | if (!tab.isPrivateBrowsingEnabled()) { |
| 1071 | Bookmarks.updateFavicon(mActivity |
| 1072 | .getContentResolver(), originalUrl, url, favicon); |
| 1073 | } |
| 1074 | } |
| 1075 | |
Leon Scroggins | 4cd9779 | 2010-12-03 15:31:56 -0500 | [diff] [blame] | 1076 | @Override |
| 1077 | public void bookmarkedStatusHasChanged(Tab tab) { |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1078 | // TODO: Switch to using onTabDataChanged after b/3262950 is fixed |
Leon Scroggins | 4cd9779 | 2010-12-03 15:31:56 -0500 | [diff] [blame] | 1079 | mUi.bookmarkedStatusHasChanged(tab); |
| 1080 | } |
| 1081 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1082 | // end WebViewController |
| 1083 | |
| 1084 | protected void pageUp() { |
| 1085 | getCurrentTopWebView().pageUp(false); |
| 1086 | } |
| 1087 | |
| 1088 | protected void pageDown() { |
| 1089 | getCurrentTopWebView().pageDown(false); |
| 1090 | } |
| 1091 | |
| 1092 | // callback from phone title bar |
| 1093 | public void editUrl() { |
| 1094 | if (mOptionsMenuOpen) mActivity.closeOptionsMenu(); |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 1095 | mUi.editUrl(false); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 1098 | public void startVoiceSearch() { |
| 1099 | Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH); |
| 1100 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, |
| 1101 | RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); |
| 1102 | intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, |
| 1103 | mActivity.getComponentName().flattenToString()); |
| 1104 | intent.putExtra(SEND_APP_ID_EXTRA, false); |
Michael Kolb | 17c4eba | 2011-01-10 13:10:07 -0800 | [diff] [blame] | 1105 | intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 1106 | mActivity.startActivity(intent); |
| 1107 | } |
| 1108 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 1109 | @Override |
| 1110 | public void activateVoiceSearchMode(String title, List<String> results) { |
| 1111 | mUi.showVoiceTitleBar(title, results); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | public void revertVoiceSearchMode(Tab tab) { |
| 1115 | mUi.revertVoiceTitleBar(tab); |
| 1116 | } |
| 1117 | |
Michael Kolb | 736990c | 2011-03-20 10:01:20 -0700 | [diff] [blame] | 1118 | public boolean supportsVoiceSearch() { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 1119 | SearchEngine searchEngine = getSettings().getSearchEngine(); |
Michael Kolb | 736990c | 2011-03-20 10:01:20 -0700 | [diff] [blame] | 1120 | return (searchEngine != null && searchEngine.supportsVoiceSearch()); |
| 1121 | } |
| 1122 | |
Derek Sollenberger | 2d4f1e2 | 2011-06-01 14:50:42 -0400 | [diff] [blame] | 1123 | public void showCustomView(Tab tab, View view, int requestedOrientation, |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1124 | WebChromeClient.CustomViewCallback callback) { |
| 1125 | if (tab.inForeground()) { |
| 1126 | if (mUi.isCustomViewShowing()) { |
| 1127 | callback.onCustomViewHidden(); |
| 1128 | return; |
| 1129 | } |
Derek Sollenberger | 2d4f1e2 | 2011-06-01 14:50:42 -0400 | [diff] [blame] | 1130 | mUi.showCustomView(view, requestedOrientation, callback); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1131 | // Save the menu state and set it to empty while the custom |
| 1132 | // view is showing. |
| 1133 | mOldMenuState = mMenuState; |
| 1134 | mMenuState = EMPTY_MENU; |
John Reck | d73c5a2 | 2010-12-22 10:22:50 -0800 | [diff] [blame] | 1135 | mActivity.invalidateOptionsMenu(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | @Override |
| 1140 | public void hideCustomView() { |
| 1141 | if (mUi.isCustomViewShowing()) { |
| 1142 | mUi.onHideCustomView(); |
| 1143 | // Reset the old menu state. |
| 1144 | mMenuState = mOldMenuState; |
| 1145 | mOldMenuState = EMPTY_MENU; |
John Reck | d73c5a2 | 2010-12-22 10:22:50 -0800 | [diff] [blame] | 1146 | mActivity.invalidateOptionsMenu(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | protected void onActivityResult(int requestCode, int resultCode, |
| 1151 | Intent intent) { |
| 1152 | if (getCurrentTopWebView() == null) return; |
| 1153 | switch (requestCode) { |
| 1154 | case PREFERENCES_PAGE: |
| 1155 | if (resultCode == Activity.RESULT_OK && intent != null) { |
| 1156 | String action = intent.getStringExtra(Intent.EXTRA_TEXT); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 1157 | if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1158 | mTabControl.removeParentChildRelationShips(); |
| 1159 | } |
| 1160 | } |
| 1161 | break; |
| 1162 | case FILE_SELECTED: |
Ben Murdoch | 51f6a2f | 2011-02-21 12:27:07 +0000 | [diff] [blame] | 1163 | // Chose a file from the file picker. |
John Reck | 9dfcdb1 | 2011-02-22 16:40:46 -0800 | [diff] [blame] | 1164 | if (null == mUploadHandler) break; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1165 | mUploadHandler.onResult(resultCode, intent); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1166 | break; |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 1167 | case AUTOFILL_SETUP: |
| 1168 | // Determine whether a profile was actually set up or not |
| 1169 | // and if so, send the message back to the WebTextView to |
| 1170 | // fill the form with the new profile. |
| 1171 | if (getSettings().getAutoFillProfile() != null) { |
| 1172 | mAutoFillSetupMessage.sendToTarget(); |
| 1173 | mAutoFillSetupMessage = null; |
| 1174 | } |
| 1175 | break; |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 1176 | case COMBO_VIEW: |
| 1177 | if (intent == null || resultCode != Activity.RESULT_OK) { |
| 1178 | break; |
| 1179 | } |
| 1180 | if (Intent.ACTION_VIEW.equals(intent.getAction())) { |
| 1181 | Tab t = getCurrentTab(); |
| 1182 | Uri uri = intent.getData(); |
| 1183 | loadUrl(t, uri.toString()); |
| 1184 | } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) { |
| 1185 | String[] urls = intent.getStringArrayExtra( |
| 1186 | ComboViewActivity.EXTRA_OPEN_ALL); |
| 1187 | Tab parent = getCurrentTab(); |
| 1188 | for (String url : urls) { |
| 1189 | parent = openTab(url, parent, |
| 1190 | !mSettings.openInBackground(), true); |
| 1191 | } |
| 1192 | } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) { |
| 1193 | long id = intent.getLongExtra( |
| 1194 | ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1); |
| 1195 | if (id >= 0) { |
| 1196 | createNewSnapshotTab(id, true); |
| 1197 | } |
| 1198 | } |
| 1199 | break; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1200 | default: |
| 1201 | break; |
| 1202 | } |
| 1203 | getCurrentTopWebView().requestFocus(); |
| 1204 | } |
| 1205 | |
| 1206 | /** |
| 1207 | * Open the Go page. |
| 1208 | * @param startWithHistory If true, open starting on the history tab. |
| 1209 | * Otherwise, start with the bookmarks tab. |
| 1210 | */ |
| 1211 | @Override |
| 1212 | public void bookmarksOrHistoryPicker(boolean startWithHistory) { |
| 1213 | if (mTabControl.getCurrentWebView() == null) { |
| 1214 | return; |
| 1215 | } |
Michael Kolb | bd3dd94 | 2011-01-12 11:09:38 -0800 | [diff] [blame] | 1216 | // clear action mode |
| 1217 | if (isInCustomActionMode()) { |
| 1218 | endActionMode(); |
| 1219 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1220 | Bundle extras = new Bundle(); |
| 1221 | // Disable opening in a new window if we have maxed out the windows |
| 1222 | extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW, |
| 1223 | !mTabControl.canCreateNewTab()); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 1224 | mUi.showComboView(startWithHistory |
| 1225 | ? ComboViews.History : ComboViews.Bookmarks, extras); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | // combo view callbacks |
| 1229 | |
| 1230 | /** |
| 1231 | * callback from ComboPage when clear history is requested |
| 1232 | */ |
| 1233 | public void onRemoveParentChildRelationships() { |
| 1234 | mTabControl.removeParentChildRelationShips(); |
| 1235 | } |
| 1236 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1237 | // key handling |
| 1238 | protected void onBackKey() { |
| 1239 | if (!mUi.onBackKey()) { |
| 1240 | WebView subwindow = mTabControl.getCurrentSubWindow(); |
| 1241 | if (subwindow != null) { |
| 1242 | if (subwindow.canGoBack()) { |
| 1243 | subwindow.goBack(); |
| 1244 | } else { |
| 1245 | dismissSubWindow(mTabControl.getCurrentTab()); |
| 1246 | } |
| 1247 | } else { |
| 1248 | goBackOnePageOrQuit(); |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 1253 | protected boolean onMenuKey() { |
| 1254 | return mUi.onMenuKey(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1257 | // menu handling and state |
| 1258 | // TODO: maybe put into separate handler |
| 1259 | |
| 1260 | protected boolean onCreateOptionsMenu(Menu menu) { |
John Reck | d73c5a2 | 2010-12-22 10:22:50 -0800 | [diff] [blame] | 1261 | if (mMenuState == EMPTY_MENU) { |
| 1262 | return false; |
| 1263 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1264 | MenuInflater inflater = mActivity.getMenuInflater(); |
| 1265 | inflater.inflate(R.menu.browser, menu); |
| 1266 | updateInLoadMenuItems(menu); |
| 1267 | // hold on to the menu reference here; it is used by the page callbacks |
| 1268 | // to update the menu based on loading state |
| 1269 | mCachedMenu = menu; |
| 1270 | return true; |
| 1271 | } |
| 1272 | |
| 1273 | protected void onCreateContextMenu(ContextMenu menu, View v, |
| 1274 | ContextMenuInfo menuInfo) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 1275 | if (v instanceof TitleBar) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1276 | return; |
| 1277 | } |
| 1278 | if (!(v instanceof WebView)) { |
| 1279 | return; |
| 1280 | } |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 1281 | final WebView webview = (WebView) v; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1282 | WebView.HitTestResult result = webview.getHitTestResult(); |
| 1283 | if (result == null) { |
| 1284 | return; |
| 1285 | } |
| 1286 | |
| 1287 | int type = result.getType(); |
| 1288 | if (type == WebView.HitTestResult.UNKNOWN_TYPE) { |
| 1289 | Log.w(LOGTAG, |
| 1290 | "We should not show context menu when nothing is touched"); |
| 1291 | return; |
| 1292 | } |
| 1293 | if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) { |
| 1294 | // let TextView handles context menu |
| 1295 | return; |
| 1296 | } |
| 1297 | |
| 1298 | // Note, http://b/issue?id=1106666 is requesting that |
| 1299 | // an inflated menu can be used again. This is not available |
| 1300 | // yet, so inflate each time (yuk!) |
| 1301 | MenuInflater inflater = mActivity.getMenuInflater(); |
| 1302 | inflater.inflate(R.menu.browsercontext, menu); |
| 1303 | |
| 1304 | // Show the correct menu group |
| 1305 | final String extra = result.getExtra(); |
| 1306 | menu.setGroupVisible(R.id.PHONE_MENU, |
| 1307 | type == WebView.HitTestResult.PHONE_TYPE); |
| 1308 | menu.setGroupVisible(R.id.EMAIL_MENU, |
| 1309 | type == WebView.HitTestResult.EMAIL_TYPE); |
| 1310 | menu.setGroupVisible(R.id.GEO_MENU, |
| 1311 | type == WebView.HitTestResult.GEO_TYPE); |
| 1312 | menu.setGroupVisible(R.id.IMAGE_MENU, |
| 1313 | type == WebView.HitTestResult.IMAGE_TYPE |
| 1314 | || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE); |
| 1315 | menu.setGroupVisible(R.id.ANCHOR_MENU, |
| 1316 | type == WebView.HitTestResult.SRC_ANCHOR_TYPE |
| 1317 | || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE); |
Cary Clark | 8974d28 | 2010-11-22 10:46:05 -0500 | [diff] [blame] | 1318 | boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE |
| 1319 | || type == WebView.HitTestResult.PHONE_TYPE |
| 1320 | || type == WebView.HitTestResult.EMAIL_TYPE |
| 1321 | || type == WebView.HitTestResult.GEO_TYPE; |
| 1322 | menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText); |
| 1323 | if (hitText) { |
| 1324 | menu.findItem(R.id.select_text_menu_id) |
| 1325 | .setOnMenuItemClickListener(new SelectText(webview)); |
| 1326 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1327 | // Setup custom handling depending on the type |
| 1328 | switch (type) { |
| 1329 | case WebView.HitTestResult.PHONE_TYPE: |
| 1330 | menu.setHeaderTitle(Uri.decode(extra)); |
| 1331 | menu.findItem(R.id.dial_context_menu_id).setIntent( |
| 1332 | new Intent(Intent.ACTION_VIEW, Uri |
| 1333 | .parse(WebView.SCHEME_TEL + extra))); |
| 1334 | Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT); |
| 1335 | addIntent.putExtra(Insert.PHONE, Uri.decode(extra)); |
| 1336 | addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); |
| 1337 | menu.findItem(R.id.add_contact_context_menu_id).setIntent( |
| 1338 | addIntent); |
| 1339 | menu.findItem(R.id.copy_phone_context_menu_id) |
| 1340 | .setOnMenuItemClickListener( |
| 1341 | new Copy(extra)); |
| 1342 | break; |
| 1343 | |
| 1344 | case WebView.HitTestResult.EMAIL_TYPE: |
| 1345 | menu.setHeaderTitle(extra); |
| 1346 | menu.findItem(R.id.email_context_menu_id).setIntent( |
| 1347 | new Intent(Intent.ACTION_VIEW, Uri |
| 1348 | .parse(WebView.SCHEME_MAILTO + extra))); |
| 1349 | menu.findItem(R.id.copy_mail_context_menu_id) |
| 1350 | .setOnMenuItemClickListener( |
| 1351 | new Copy(extra)); |
| 1352 | break; |
| 1353 | |
| 1354 | case WebView.HitTestResult.GEO_TYPE: |
| 1355 | menu.setHeaderTitle(extra); |
| 1356 | menu.findItem(R.id.map_context_menu_id).setIntent( |
| 1357 | new Intent(Intent.ACTION_VIEW, Uri |
| 1358 | .parse(WebView.SCHEME_GEO |
| 1359 | + URLEncoder.encode(extra)))); |
| 1360 | menu.findItem(R.id.copy_geo_context_menu_id) |
| 1361 | .setOnMenuItemClickListener( |
| 1362 | new Copy(extra)); |
| 1363 | break; |
| 1364 | |
| 1365 | case WebView.HitTestResult.SRC_ANCHOR_TYPE: |
| 1366 | case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE: |
Michael Kolb | 4c537ce | 2011-01-13 15:19:33 -0800 | [diff] [blame] | 1367 | menu.setHeaderTitle(extra); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1368 | // decide whether to show the open link in new tab option |
| 1369 | boolean showNewTab = mTabControl.canCreateNewTab(); |
| 1370 | MenuItem newTabItem |
| 1371 | = menu.findItem(R.id.open_newtab_context_menu_id); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 1372 | newTabItem.setTitle(getSettings().openInBackground() |
Michael Kolb | 2dd65c8 | 2011-01-14 11:07:38 -0800 | [diff] [blame] | 1373 | ? R.string.contextmenu_openlink_newwindow_background |
| 1374 | : R.string.contextmenu_openlink_newwindow); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1375 | newTabItem.setVisible(showNewTab); |
| 1376 | if (showNewTab) { |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 1377 | if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) { |
| 1378 | newTabItem.setOnMenuItemClickListener( |
| 1379 | new MenuItem.OnMenuItemClickListener() { |
| 1380 | @Override |
| 1381 | public boolean onMenuItemClick(MenuItem item) { |
| 1382 | final HashMap<String, WebView> hrefMap = |
| 1383 | new HashMap<String, WebView>(); |
| 1384 | hrefMap.put("webview", webview); |
| 1385 | final Message msg = mHandler.obtainMessage( |
| 1386 | FOCUS_NODE_HREF, |
| 1387 | R.id.open_newtab_context_menu_id, |
| 1388 | 0, hrefMap); |
| 1389 | webview.requestFocusNodeHref(msg); |
| 1390 | return true; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1391 | } |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 1392 | }); |
| 1393 | } else { |
| 1394 | newTabItem.setOnMenuItemClickListener( |
| 1395 | new MenuItem.OnMenuItemClickListener() { |
| 1396 | @Override |
| 1397 | public boolean onMenuItemClick(MenuItem item) { |
| 1398 | final Tab parent = mTabControl.getCurrentTab(); |
John Reck | 5949c66 | 2011-05-27 09:52:29 -0700 | [diff] [blame] | 1399 | openTab(extra, parent, |
| 1400 | !mSettings.openInBackground(), |
| 1401 | true); |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 1402 | return true; |
| 1403 | } |
| 1404 | }); |
| 1405 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1406 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1407 | if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) { |
| 1408 | break; |
| 1409 | } |
| 1410 | // otherwise fall through to handle image part |
| 1411 | case WebView.HitTestResult.IMAGE_TYPE: |
| 1412 | if (type == WebView.HitTestResult.IMAGE_TYPE) { |
| 1413 | menu.setHeaderTitle(extra); |
| 1414 | } |
| 1415 | menu.findItem(R.id.view_image_context_menu_id).setIntent( |
| 1416 | new Intent(Intent.ACTION_VIEW, Uri.parse(extra))); |
| 1417 | menu.findItem(R.id.download_context_menu_id). |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 1418 | setOnMenuItemClickListener( |
| 1419 | new Download(mActivity, extra, webview.isPrivateBrowsingEnabled())); |
John Reck | 3527dd1 | 2011-02-22 10:35:29 -0800 | [diff] [blame] | 1420 | menu.findItem(R.id.set_wallpaper_context_menu_id). |
| 1421 | setOnMenuItemClickListener(new WallpaperHandler(mActivity, |
| 1422 | extra)); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1423 | break; |
| 1424 | |
| 1425 | default: |
| 1426 | Log.w(LOGTAG, "We should not get here."); |
| 1427 | break; |
| 1428 | } |
| 1429 | //update the ui |
| 1430 | mUi.onContextMenuCreated(menu); |
| 1431 | } |
| 1432 | |
| 1433 | /** |
| 1434 | * As the menu can be open when loading state changes |
| 1435 | * we must manually update the state of the stop/reload menu |
| 1436 | * item |
| 1437 | */ |
| 1438 | private void updateInLoadMenuItems(Menu menu) { |
| 1439 | if (menu == null) { |
| 1440 | return; |
| 1441 | } |
| 1442 | MenuItem dest = menu.findItem(R.id.stop_reload_menu_id); |
| 1443 | MenuItem src = mInLoad ? |
| 1444 | menu.findItem(R.id.stop_menu_id): |
| 1445 | menu.findItem(R.id.reload_menu_id); |
| 1446 | if (src != null) { |
| 1447 | dest.setIcon(src.getIcon()); |
| 1448 | dest.setTitle(src.getTitle()); |
| 1449 | } |
| 1450 | } |
| 1451 | |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 1452 | boolean onPrepareOptionsMenu(Menu menu) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1453 | // Note: setVisible will decide whether an item is visible; while |
| 1454 | // setEnabled() will decide whether an item is enabled, which also means |
| 1455 | // whether the matching shortcut key will function. |
| 1456 | switch (mMenuState) { |
| 1457 | case EMPTY_MENU: |
| 1458 | if (mCurrentMenuState != mMenuState) { |
| 1459 | menu.setGroupVisible(R.id.MAIN_MENU, false); |
| 1460 | menu.setGroupEnabled(R.id.MAIN_MENU, false); |
| 1461 | menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false); |
| 1462 | } |
| 1463 | break; |
| 1464 | default: |
| 1465 | if (mCurrentMenuState != mMenuState) { |
| 1466 | menu.setGroupVisible(R.id.MAIN_MENU, true); |
| 1467 | menu.setGroupEnabled(R.id.MAIN_MENU, true); |
| 1468 | menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true); |
| 1469 | } |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1470 | updateMenuState(getCurrentTab(), menu); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1471 | break; |
| 1472 | } |
| 1473 | mCurrentMenuState = mMenuState; |
Michael Kolb | 1acef69 | 2011-03-08 14:12:06 -0800 | [diff] [blame] | 1474 | return mUi.onPrepareOptionsMenu(menu); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1477 | @Override |
| 1478 | public void updateMenuState(Tab tab, Menu menu) { |
| 1479 | boolean canGoBack = false; |
| 1480 | boolean canGoForward = false; |
| 1481 | boolean isHome = false; |
| 1482 | if (tab != null) { |
| 1483 | canGoBack = tab.canGoBack(); |
| 1484 | canGoForward = tab.canGoForward(); |
| 1485 | isHome = mSettings.getHomePage().equals(tab.getUrl()); |
| 1486 | } |
| 1487 | final MenuItem back = menu.findItem(R.id.back_menu_id); |
| 1488 | back.setEnabled(canGoBack); |
| 1489 | |
| 1490 | final MenuItem home = menu.findItem(R.id.homepage_menu_id); |
| 1491 | home.setEnabled(!isHome); |
| 1492 | |
| 1493 | final MenuItem forward = menu.findItem(R.id.forward_menu_id); |
| 1494 | forward.setEnabled(canGoForward); |
| 1495 | |
| 1496 | final MenuItem source = menu.findItem(mInLoad ? R.id.stop_menu_id : R.id.reload_menu_id); |
| 1497 | final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id); |
Michael Kolb | 7ab75ee | 2011-07-14 16:36:38 -0700 | [diff] [blame] | 1498 | if (source != null && dest != null) { |
| 1499 | dest.setTitle(source.getTitle()); |
| 1500 | dest.setIcon(source.getIcon()); |
| 1501 | } |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1502 | |
| 1503 | // decide whether to show the share link option |
| 1504 | PackageManager pm = mActivity.getPackageManager(); |
| 1505 | Intent send = new Intent(Intent.ACTION_SEND); |
| 1506 | send.setType("text/plain"); |
| 1507 | ResolveInfo ri = pm.resolveActivity(send, |
| 1508 | PackageManager.MATCH_DEFAULT_ONLY); |
| 1509 | menu.findItem(R.id.share_page_menu_id).setVisible(ri != null); |
| 1510 | |
| 1511 | boolean isNavDump = mSettings.enableNavDump(); |
| 1512 | final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id); |
| 1513 | nav.setVisible(isNavDump); |
| 1514 | nav.setEnabled(isNavDump); |
| 1515 | |
| 1516 | boolean showDebugSettings = mSettings.isDebugEnabled(); |
| 1517 | final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id); |
| 1518 | counter.setVisible(showDebugSettings); |
| 1519 | counter.setEnabled(showDebugSettings); |
| 1520 | |
| 1521 | MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id); |
| 1522 | saveSnapshot.setVisible(tab != null && !tab.isSnapshot()); |
Michael Kolb | 7bdee0b | 2011-08-01 11:55:38 -0700 | [diff] [blame] | 1523 | mUi.updateMenuState(tab, menu); |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1526 | public boolean onOptionsItemSelected(MenuItem item) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1527 | if (null == getCurrentTopWebView()) { |
| 1528 | return false; |
| 1529 | } |
| 1530 | if (mMenuIsDown) { |
| 1531 | // The shortcut action consumes the MENU. Even if it is still down, |
| 1532 | // it won't trigger the next shortcut action. In the case of the |
| 1533 | // shortcut action triggering a new activity, like Bookmarks, we |
| 1534 | // won't get onKeyUp for MENU. So it is important to reset it here. |
| 1535 | mMenuIsDown = false; |
| 1536 | } |
Michael Kolb | 3ca1275 | 2011-07-20 13:52:25 -0700 | [diff] [blame] | 1537 | if (mUi.onOptionsItemSelected(item)) { |
| 1538 | // ui callback handled it |
| 1539 | return true; |
| 1540 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1541 | switch (item.getItemId()) { |
| 1542 | // -- Main menu |
| 1543 | case R.id.new_tab_menu_id: |
| 1544 | openTabToHomePage(); |
| 1545 | break; |
| 1546 | |
| 1547 | case R.id.incognito_menu_id: |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 1548 | openIncognitoTab(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1549 | break; |
| 1550 | |
| 1551 | case R.id.goto_menu_id: |
| 1552 | editUrl(); |
| 1553 | break; |
| 1554 | |
| 1555 | case R.id.bookmarks_menu_id: |
| 1556 | bookmarksOrHistoryPicker(false); |
| 1557 | break; |
| 1558 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1559 | case R.id.add_bookmark_menu_id: |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 1560 | mActivity.startActivity(createBookmarkCurrentPageIntent(false)); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1561 | break; |
| 1562 | |
| 1563 | case R.id.stop_reload_menu_id: |
| 1564 | if (mInLoad) { |
| 1565 | stopLoading(); |
| 1566 | } else { |
| 1567 | getCurrentTopWebView().reload(); |
| 1568 | } |
| 1569 | break; |
| 1570 | |
| 1571 | case R.id.back_menu_id: |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 1572 | getCurrentTab().goBack(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1573 | break; |
| 1574 | |
| 1575 | case R.id.forward_menu_id: |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 1576 | getCurrentTab().goForward(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1577 | break; |
| 1578 | |
| 1579 | case R.id.close_menu_id: |
| 1580 | // Close the subwindow if it exists. |
| 1581 | if (mTabControl.getCurrentSubWindow() != null) { |
| 1582 | dismissSubWindow(mTabControl.getCurrentTab()); |
| 1583 | break; |
| 1584 | } |
| 1585 | closeCurrentTab(); |
| 1586 | break; |
| 1587 | |
| 1588 | case R.id.homepage_menu_id: |
| 1589 | Tab current = mTabControl.getCurrentTab(); |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 1590 | loadUrl(current, mSettings.getHomePage()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1591 | break; |
| 1592 | |
| 1593 | case R.id.preferences_menu_id: |
| 1594 | Intent intent = new Intent(mActivity, BrowserPreferencesPage.class); |
| 1595 | intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE, |
| 1596 | getCurrentTopWebView().getUrl()); |
| 1597 | mActivity.startActivityForResult(intent, PREFERENCES_PAGE); |
| 1598 | break; |
| 1599 | |
| 1600 | case R.id.find_menu_id: |
Leon Scroggins | 1c00d5e | 2011-01-04 10:45:58 -0500 | [diff] [blame] | 1601 | getCurrentTopWebView().showFindDialog(null, true); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1602 | break; |
| 1603 | |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 1604 | case R.id.save_snapshot_menu_id: |
| 1605 | final Tab source = getTabControl().getCurrentTab(); |
John Reck | f33b163 | 2011-06-04 20:00:23 -0700 | [diff] [blame] | 1606 | if (source == null) break; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 1607 | final ContentResolver cr = mActivity.getContentResolver(); |
| 1608 | final ContentValues values = source.createSnapshotValues(); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 1609 | if (values != null) { |
| 1610 | new AsyncTask<Tab, Void, Long>() { |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 1611 | |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 1612 | @Override |
| 1613 | protected Long doInBackground(Tab... params) { |
| 1614 | Uri result = cr.insert(Snapshots.CONTENT_URI, values); |
| 1615 | long id = ContentUris.parseId(result); |
| 1616 | return id; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 1617 | } |
John Reck | f33b163 | 2011-06-04 20:00:23 -0700 | [diff] [blame] | 1618 | |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 1619 | @Override |
| 1620 | protected void onPostExecute(Long id) { |
| 1621 | Bundle b = new Bundle(); |
| 1622 | b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id); |
| 1623 | mUi.showComboView(ComboViews.Snapshots, b); |
| 1624 | }; |
| 1625 | }.execute(source); |
| 1626 | } else { |
| 1627 | Toast.makeText(mActivity, R.string.snapshot_failed, |
Leon Scroggins | ac99384 | 2011-02-02 12:54:07 -0500 | [diff] [blame] | 1628 | Toast.LENGTH_SHORT).show(); |
Leon Scroggins | ac99384 | 2011-02-02 12:54:07 -0500 | [diff] [blame] | 1629 | } |
Leon Scroggins | ac99384 | 2011-02-02 12:54:07 -0500 | [diff] [blame] | 1630 | break; |
| 1631 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1632 | case R.id.page_info_menu_id: |
Huahui Wu | ae0c041 | 2011-06-28 10:17:05 -0700 | [diff] [blame] | 1633 | mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1634 | break; |
| 1635 | |
| 1636 | case R.id.classic_history_menu_id: |
| 1637 | bookmarksOrHistoryPicker(true); |
| 1638 | break; |
| 1639 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1640 | case R.id.share_page_menu_id: |
| 1641 | Tab currentTab = mTabControl.getCurrentTab(); |
| 1642 | if (null == currentTab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1643 | return false; |
| 1644 | } |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 1645 | shareCurrentPage(currentTab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1646 | break; |
| 1647 | |
| 1648 | case R.id.dump_nav_menu_id: |
| 1649 | getCurrentTopWebView().debugDump(); |
| 1650 | break; |
| 1651 | |
| 1652 | case R.id.dump_counters_menu_id: |
| 1653 | getCurrentTopWebView().dumpV8Counters(); |
| 1654 | break; |
| 1655 | |
| 1656 | case R.id.zoom_in_menu_id: |
| 1657 | getCurrentTopWebView().zoomIn(); |
| 1658 | break; |
| 1659 | |
| 1660 | case R.id.zoom_out_menu_id: |
| 1661 | getCurrentTopWebView().zoomOut(); |
| 1662 | break; |
| 1663 | |
| 1664 | case R.id.view_downloads_menu_id: |
| 1665 | viewDownloads(); |
| 1666 | break; |
| 1667 | |
| 1668 | case R.id.window_one_menu_id: |
| 1669 | case R.id.window_two_menu_id: |
| 1670 | case R.id.window_three_menu_id: |
| 1671 | case R.id.window_four_menu_id: |
| 1672 | case R.id.window_five_menu_id: |
| 1673 | case R.id.window_six_menu_id: |
| 1674 | case R.id.window_seven_menu_id: |
| 1675 | case R.id.window_eight_menu_id: |
| 1676 | { |
| 1677 | int menuid = item.getItemId(); |
| 1678 | for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) { |
| 1679 | if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) { |
| 1680 | Tab desiredTab = mTabControl.getTab(id); |
| 1681 | if (desiredTab != null && |
| 1682 | desiredTab != mTabControl.getCurrentTab()) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1683 | switchToTab(desiredTab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1684 | } |
| 1685 | break; |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | break; |
| 1690 | |
| 1691 | default: |
| 1692 | return false; |
| 1693 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1694 | return true; |
| 1695 | } |
| 1696 | |
| 1697 | public boolean onContextItemSelected(MenuItem item) { |
John Reck | dbf57df | 2010-11-09 16:34:03 -0800 | [diff] [blame] | 1698 | // Let the History and Bookmark fragments handle menus they created. |
| 1699 | if (item.getGroupId() == R.id.CONTEXT_MENU) { |
| 1700 | return false; |
| 1701 | } |
| 1702 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1703 | int id = item.getItemId(); |
| 1704 | boolean result = true; |
| 1705 | switch (id) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1706 | // -- Browser context menu |
| 1707 | case R.id.open_context_menu_id: |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1708 | case R.id.save_link_context_menu_id: |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1709 | case R.id.copy_link_context_menu_id: |
| 1710 | final WebView webView = getCurrentTopWebView(); |
| 1711 | if (null == webView) { |
| 1712 | result = false; |
| 1713 | break; |
| 1714 | } |
| 1715 | final HashMap<String, WebView> hrefMap = |
| 1716 | new HashMap<String, WebView>(); |
| 1717 | hrefMap.put("webview", webView); |
| 1718 | final Message msg = mHandler.obtainMessage( |
| 1719 | FOCUS_NODE_HREF, id, 0, hrefMap); |
| 1720 | webView.requestFocusNodeHref(msg); |
| 1721 | break; |
| 1722 | |
| 1723 | default: |
| 1724 | // For other context menus |
| 1725 | result = onOptionsItemSelected(item); |
| 1726 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1727 | return result; |
| 1728 | } |
| 1729 | |
| 1730 | /** |
| 1731 | * support programmatically opening the context menu |
| 1732 | */ |
| 1733 | public void openContextMenu(View view) { |
| 1734 | mActivity.openContextMenu(view); |
| 1735 | } |
| 1736 | |
| 1737 | /** |
| 1738 | * programmatically open the options menu |
| 1739 | */ |
| 1740 | public void openOptionsMenu() { |
| 1741 | mActivity.openOptionsMenu(); |
| 1742 | } |
| 1743 | |
| 1744 | public boolean onMenuOpened(int featureId, Menu menu) { |
| 1745 | if (mOptionsMenuOpen) { |
| 1746 | if (mConfigChanged) { |
| 1747 | // We do not need to make any changes to the state of the |
| 1748 | // title bar, since the only thing that happened was a |
| 1749 | // change in orientation |
| 1750 | mConfigChanged = false; |
| 1751 | } else { |
| 1752 | if (!mExtendedMenuOpen) { |
| 1753 | mExtendedMenuOpen = true; |
| 1754 | mUi.onExtendedMenuOpened(); |
| 1755 | } else { |
| 1756 | // Switching the menu back to icon view, so show the |
| 1757 | // title bar once again. |
| 1758 | mExtendedMenuOpen = false; |
| 1759 | mUi.onExtendedMenuClosed(mInLoad); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | } else { |
| 1763 | // The options menu is closed, so open it, and show the title |
| 1764 | mOptionsMenuOpen = true; |
| 1765 | mConfigChanged = false; |
| 1766 | mExtendedMenuOpen = false; |
| 1767 | mUi.onOptionsMenuOpened(); |
| 1768 | } |
| 1769 | return true; |
| 1770 | } |
| 1771 | |
| 1772 | public void onOptionsMenuClosed(Menu menu) { |
| 1773 | mOptionsMenuOpen = false; |
| 1774 | mUi.onOptionsMenuClosed(mInLoad); |
| 1775 | } |
| 1776 | |
| 1777 | public void onContextMenuClosed(Menu menu) { |
| 1778 | mUi.onContextMenuClosed(menu, mInLoad); |
| 1779 | } |
| 1780 | |
| 1781 | // Helper method for getting the top window. |
| 1782 | @Override |
| 1783 | public WebView getCurrentTopWebView() { |
| 1784 | return mTabControl.getCurrentTopWebView(); |
| 1785 | } |
| 1786 | |
| 1787 | @Override |
| 1788 | public WebView getCurrentWebView() { |
| 1789 | return mTabControl.getCurrentWebView(); |
| 1790 | } |
| 1791 | |
| 1792 | /* |
| 1793 | * This method is called as a result of the user selecting the options |
| 1794 | * menu to see the download window. It shows the download window on top of |
| 1795 | * the current window. |
| 1796 | */ |
| 1797 | void viewDownloads() { |
| 1798 | Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS); |
| 1799 | mActivity.startActivity(intent); |
| 1800 | } |
| 1801 | |
John Reck | 30b065e | 2011-07-19 10:58:05 -0700 | [diff] [blame] | 1802 | int getActionModeHeight() { |
| 1803 | TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes( |
| 1804 | new int[] { android.R.attr.actionBarSize }); |
| 1805 | int size = (int) actionBarSizeTypedArray.getDimension(0, 0f); |
| 1806 | actionBarSizeTypedArray.recycle(); |
| 1807 | return size; |
| 1808 | } |
| 1809 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1810 | // action mode |
| 1811 | |
| 1812 | void onActionModeStarted(ActionMode mode) { |
| 1813 | mUi.onActionModeStarted(mode); |
| 1814 | mActionMode = mode; |
Michael Kolb | 42c0c06 | 2011-08-02 12:56:06 -0700 | [diff] [blame] | 1815 | if (mSimulateActionBarOverlayMode && !mUi.isEditingUrl()) { |
John Reck | 30b065e | 2011-07-19 10:58:05 -0700 | [diff] [blame] | 1816 | WebView web = getCurrentWebView(); |
| 1817 | // Simulate overlay mode by scrolling the webview the amount it will be |
| 1818 | // pushed down. Actual overlay mode doesn't work for us as otherwise |
| 1819 | // the CAB will, well, overlay the content, which breaks things like |
| 1820 | // find on page. |
| 1821 | int scrollBy = getActionModeHeight(); |
| 1822 | web.scrollBy(0, scrollBy); |
| 1823 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | /* |
| 1827 | * True if a custom ActionMode (i.e. find or select) is in use. |
| 1828 | */ |
| 1829 | @Override |
| 1830 | public boolean isInCustomActionMode() { |
| 1831 | return mActionMode != null; |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * End the current ActionMode. |
| 1836 | */ |
| 1837 | @Override |
| 1838 | public void endActionMode() { |
| 1839 | if (mActionMode != null) { |
| 1840 | mActionMode.finish(); |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | /* |
| 1845 | * Called by find and select when they are finished. Replace title bars |
| 1846 | * as necessary. |
| 1847 | */ |
| 1848 | public void onActionModeFinished(ActionMode mode) { |
| 1849 | if (!isInCustomActionMode()) return; |
| 1850 | mUi.onActionModeFinished(mInLoad); |
| 1851 | mActionMode = null; |
John Reck | 30b065e | 2011-07-19 10:58:05 -0700 | [diff] [blame] | 1852 | if (mSimulateActionBarOverlayMode) { |
| 1853 | WebView web = getCurrentWebView(); |
| 1854 | int scrollBy = getActionModeHeight(); |
| 1855 | web.scrollBy(0, -scrollBy); |
| 1856 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | boolean isInLoad() { |
| 1860 | return mInLoad; |
| 1861 | } |
| 1862 | |
| 1863 | // bookmark handling |
| 1864 | |
| 1865 | /** |
| 1866 | * add the current page as a bookmark to the given folder id |
| 1867 | * @param folderId use -1 for the default folder |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 1868 | * @param editExisting If true, check to see whether the site is already |
Leon Scroggins | bdff8a7 | 2011-02-11 15:49:04 -0500 | [diff] [blame] | 1869 | * bookmarked, and if it is, edit that bookmark. If false, and |
| 1870 | * the site is already bookmarked, do not attempt to edit the |
| 1871 | * existing bookmark. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1872 | */ |
| 1873 | @Override |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 1874 | public Intent createBookmarkCurrentPageIntent(boolean editExisting) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1875 | Intent i = new Intent(mActivity, |
| 1876 | AddBookmarkPage.class); |
| 1877 | WebView w = getCurrentTopWebView(); |
| 1878 | i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl()); |
| 1879 | i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle()); |
| 1880 | String touchIconUrl = w.getTouchIconUrl(); |
| 1881 | if (touchIconUrl != null) { |
| 1882 | i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl); |
| 1883 | WebSettings settings = w.getSettings(); |
| 1884 | if (settings != null) { |
| 1885 | i.putExtra(AddBookmarkPage.USER_AGENT, |
| 1886 | settings.getUserAgentString()); |
| 1887 | } |
| 1888 | } |
| 1889 | i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, |
| 1890 | createScreenshot(w, getDesiredThumbnailWidth(mActivity), |
| 1891 | getDesiredThumbnailHeight(mActivity))); |
| 1892 | i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon()); |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 1893 | if (editExisting) { |
Leon Scroggins | bdff8a7 | 2011-02-11 15:49:04 -0500 | [diff] [blame] | 1894 | i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true); |
| 1895 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1896 | // Put the dialog at the upper right of the screen, covering the |
| 1897 | // star on the title bar. |
| 1898 | i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP); |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 1899 | return i; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | // file chooser |
| 1903 | public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { |
| 1904 | mUploadHandler = new UploadHandler(this); |
| 1905 | mUploadHandler.openFileChooser(uploadMsg, acceptType); |
| 1906 | } |
| 1907 | |
| 1908 | // thumbnails |
| 1909 | |
| 1910 | /** |
| 1911 | * Return the desired width for thumbnail screenshots, which are stored in |
| 1912 | * the database, and used on the bookmarks screen. |
| 1913 | * @param context Context for finding out the density of the screen. |
| 1914 | * @return desired width for thumbnail screenshot. |
| 1915 | */ |
| 1916 | static int getDesiredThumbnailWidth(Context context) { |
| 1917 | return context.getResources().getDimensionPixelOffset( |
| 1918 | R.dimen.bookmarkThumbnailWidth); |
| 1919 | } |
| 1920 | |
| 1921 | /** |
| 1922 | * Return the desired height for thumbnail screenshots, which are stored in |
| 1923 | * the database, and used on the bookmarks screen. |
| 1924 | * @param context Context for finding out the density of the screen. |
| 1925 | * @return desired height for thumbnail screenshot. |
| 1926 | */ |
| 1927 | static int getDesiredThumbnailHeight(Context context) { |
| 1928 | return context.getResources().getDimensionPixelOffset( |
| 1929 | R.dimen.bookmarkThumbnailHeight); |
| 1930 | } |
| 1931 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 1932 | static Bitmap createScreenshot(WebView view, int width, int height) { |
John Reck | 5c6ac2f | 2011-01-05 10:18:03 -0800 | [diff] [blame] | 1933 | // We render to a bitmap 2x the desired size so that we can then |
| 1934 | // re-scale it with filtering since canvas.scale doesn't filter |
| 1935 | // This helps reduce aliasing at the cost of being slightly blurry |
| 1936 | final int filter_scale = 2; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1937 | Picture thumbnail = view.capturePicture(); |
| 1938 | if (thumbnail == null) { |
| 1939 | return null; |
| 1940 | } |
John Reck | 5c6ac2f | 2011-01-05 10:18:03 -0800 | [diff] [blame] | 1941 | width *= filter_scale; |
| 1942 | height *= filter_scale; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1943 | Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); |
| 1944 | Canvas canvas = new Canvas(bm); |
| 1945 | // May need to tweak these values to determine what is the |
| 1946 | // best scale factor |
| 1947 | int thumbnailWidth = thumbnail.getWidth(); |
| 1948 | int thumbnailHeight = thumbnail.getHeight(); |
John Reck | fe49ab4 | 2010-11-16 17:09:37 -0800 | [diff] [blame] | 1949 | float scaleFactor = 1.0f; |
Michael Kolb | eb95db4 | 2011-03-03 10:38:40 -0800 | [diff] [blame] | 1950 | if (thumbnailWidth > 0 && thumbnailHeight > 0) { |
John Reck | fe49ab4 | 2010-11-16 17:09:37 -0800 | [diff] [blame] | 1951 | scaleFactor = (float) width / (float)thumbnailWidth; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1952 | } else { |
| 1953 | return null; |
| 1954 | } |
John Reck | fe49ab4 | 2010-11-16 17:09:37 -0800 | [diff] [blame] | 1955 | |
Michael Kolb | eb95db4 | 2011-03-03 10:38:40 -0800 | [diff] [blame] | 1956 | float scaleFactorY = (float) height / (float)thumbnailHeight; |
| 1957 | if (scaleFactorY > scaleFactor) { |
| 1958 | // The picture is narrower than the requested AR |
| 1959 | // Center the thumnail and crop the sides |
| 1960 | scaleFactor = scaleFactorY; |
John Reck | fe49ab4 | 2010-11-16 17:09:37 -0800 | [diff] [blame] | 1961 | float wx = (thumbnailWidth * scaleFactor) - width; |
| 1962 | canvas.translate((int) -(wx / 2), 0); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
John Reck | fe49ab4 | 2010-11-16 17:09:37 -0800 | [diff] [blame] | 1965 | canvas.scale(scaleFactor, scaleFactor); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1966 | |
| 1967 | thumbnail.draw(canvas); |
John Reck | 5c6ac2f | 2011-01-05 10:18:03 -0800 | [diff] [blame] | 1968 | Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale, |
| 1969 | height / filter_scale, true); |
Dianne Hackborn | 43cfe8a | 2011-08-02 16:59:35 -0700 | [diff] [blame] | 1970 | canvas.setBitmap(null); |
John Reck | 5c6ac2f | 2011-01-05 10:18:03 -0800 | [diff] [blame] | 1971 | bm.recycle(); |
| 1972 | return ret; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 1975 | private void updateScreenshot(Tab tab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1976 | // If this is a bookmarked site, add a screenshot to the database. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1977 | // FIXME: Would like to make sure there is actually something to |
| 1978 | // draw, but the API for that (WebViewCore.pictureReady()) is not |
| 1979 | // currently accessible here. |
| 1980 | |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 1981 | WebView view = tab.getWebView(); |
John Reck | 7a59120 | 2011-02-16 15:44:01 -0800 | [diff] [blame] | 1982 | if (view == null) { |
| 1983 | // Tab was destroyed |
| 1984 | return; |
| 1985 | } |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 1986 | final String url = tab.getUrl(); |
| 1987 | final String originalUrl = view.getOriginalUrl(); |
| 1988 | |
| 1989 | if (TextUtils.isEmpty(url)) { |
| 1990 | return; |
| 1991 | } |
| 1992 | |
| 1993 | // Only update thumbnails for web urls (http(s)://), not for |
| 1994 | // about:, javascript:, data:, etc... |
| 1995 | // Unless it is a bookmarked site, then always update |
| 1996 | if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) { |
| 1997 | return; |
| 1998 | } |
| 1999 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2000 | final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity), |
| 2001 | getDesiredThumbnailHeight(mActivity)); |
| 2002 | if (bm == null) { |
| 2003 | return; |
| 2004 | } |
| 2005 | |
| 2006 | final ContentResolver cr = mActivity.getContentResolver(); |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2007 | new AsyncTask<Void, Void, Void>() { |
| 2008 | @Override |
| 2009 | protected Void doInBackground(Void... unused) { |
| 2010 | Cursor cursor = null; |
| 2011 | try { |
| 2012 | // TODO: Clean this up |
| 2013 | cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url); |
| 2014 | if (cursor != null && cursor.moveToFirst()) { |
| 2015 | final ByteArrayOutputStream os = |
| 2016 | new ByteArrayOutputStream(); |
| 2017 | bm.compress(Bitmap.CompressFormat.PNG, 100, os); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2018 | |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2019 | ContentValues values = new ContentValues(); |
| 2020 | values.put(Images.THUMBNAIL, os.toByteArray()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2021 | |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2022 | do { |
John Reck | 617fd83 | 2011-02-16 14:35:59 -0800 | [diff] [blame] | 2023 | values.put(Images.URL, cursor.getString(0)); |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2024 | cr.update(Images.CONTENT_URI, values, null, null); |
| 2025 | } while (cursor.moveToNext()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2026 | } |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2027 | } catch (IllegalStateException e) { |
| 2028 | // Ignore |
| 2029 | } finally { |
| 2030 | if (cursor != null) cursor.close(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2031 | } |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2032 | return null; |
| 2033 | } |
| 2034 | }.execute(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | private class Copy implements OnMenuItemClickListener { |
| 2038 | private CharSequence mText; |
| 2039 | |
| 2040 | public boolean onMenuItemClick(MenuItem item) { |
| 2041 | copy(mText); |
| 2042 | return true; |
| 2043 | } |
| 2044 | |
| 2045 | public Copy(CharSequence toCopy) { |
| 2046 | mText = toCopy; |
| 2047 | } |
| 2048 | } |
| 2049 | |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 2050 | private static class Download implements OnMenuItemClickListener { |
| 2051 | private Activity mActivity; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2052 | private String mText; |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 2053 | private boolean mPrivateBrowsing; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2054 | |
| 2055 | public boolean onMenuItemClick(MenuItem item) { |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 2056 | DownloadHandler.onDownloadStartNoStream(mActivity, mText, null, |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 2057 | null, null, mPrivateBrowsing); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2058 | return true; |
| 2059 | } |
| 2060 | |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 2061 | public Download(Activity activity, String toDownload, boolean privateBrowsing) { |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 2062 | mActivity = activity; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2063 | mText = toDownload; |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 2064 | mPrivateBrowsing = privateBrowsing; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2065 | } |
| 2066 | } |
| 2067 | |
Cary Clark | 8974d28 | 2010-11-22 10:46:05 -0500 | [diff] [blame] | 2068 | private static class SelectText implements OnMenuItemClickListener { |
| 2069 | private WebView mWebView; |
| 2070 | |
| 2071 | public boolean onMenuItemClick(MenuItem item) { |
| 2072 | if (mWebView != null) { |
| 2073 | return mWebView.selectText(); |
| 2074 | } |
| 2075 | return false; |
| 2076 | } |
| 2077 | |
| 2078 | public SelectText(WebView webView) { |
| 2079 | mWebView = webView; |
| 2080 | } |
| 2081 | |
| 2082 | } |
| 2083 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2084 | /********************** TODO: UI stuff *****************************/ |
| 2085 | |
| 2086 | // these methods have been copied, they still need to be cleaned up |
| 2087 | |
| 2088 | /****************** tabs ***************************************************/ |
| 2089 | |
| 2090 | // basic tab interactions: |
| 2091 | |
| 2092 | // it is assumed that tabcontrol already knows about the tab |
| 2093 | protected void addTab(Tab tab) { |
| 2094 | mUi.addTab(tab); |
| 2095 | } |
| 2096 | |
| 2097 | protected void removeTab(Tab tab) { |
| 2098 | mUi.removeTab(tab); |
| 2099 | mTabControl.removeTab(tab); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 2100 | mCrashRecoveryHandler.backupState(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2101 | } |
| 2102 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 2103 | @Override |
| 2104 | public void setActiveTab(Tab tab) { |
Michael Kolb | cd424e9 | 2011-02-24 10:26:26 -0800 | [diff] [blame] | 2105 | // monkey protection against delayed start |
| 2106 | if (tab != null) { |
| 2107 | mTabControl.setCurrentTab(tab); |
| 2108 | // the tab is guaranteed to have a webview after setCurrentTab |
| 2109 | mUi.setActiveTab(tab); |
| 2110 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | protected void closeEmptyChildTab() { |
| 2114 | Tab current = mTabControl.getCurrentTab(); |
| 2115 | if (current != null |
| 2116 | && current.getWebView().copyBackForwardList().getSize() == 0) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2117 | Tab parent = current.getParent(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2118 | if (parent != null) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2119 | switchToTab(parent); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2120 | closeTab(current); |
| 2121 | } |
| 2122 | } |
| 2123 | } |
| 2124 | |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2125 | protected void reuseTab(Tab appTab, UrlData urlData) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2126 | // Dismiss the subwindow if applicable. |
| 2127 | dismissSubWindow(appTab); |
| 2128 | // Since we might kill the WebView, remove it from the |
| 2129 | // content view first. |
| 2130 | mUi.detachTab(appTab); |
| 2131 | // Recreate the main WebView after destroying the old one. |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2132 | mTabControl.recreateWebView(appTab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2133 | // TODO: analyze why the remove and add are necessary |
| 2134 | mUi.attachTab(appTab); |
| 2135 | if (mTabControl.getCurrentTab() != appTab) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2136 | switchToTab(appTab); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2137 | loadUrlDataIn(appTab, urlData); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2138 | } else { |
| 2139 | // If the tab was the current tab, we have to attach |
| 2140 | // it to the view system again. |
| 2141 | setActiveTab(appTab); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2142 | loadUrlDataIn(appTab, urlData); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | // Remove the sub window if it exists. Also called by TabControl when the |
| 2147 | // user clicks the 'X' to dismiss a sub window. |
| 2148 | public void dismissSubWindow(Tab tab) { |
| 2149 | removeSubWindow(tab); |
| 2150 | // dismiss the subwindow. This will destroy the WebView. |
| 2151 | tab.dismissSubWindow(); |
| 2152 | getCurrentTopWebView().requestFocus(); |
| 2153 | } |
| 2154 | |
| 2155 | @Override |
| 2156 | public void removeSubWindow(Tab t) { |
| 2157 | if (t.getSubWebView() != null) { |
| 2158 | mUi.removeSubWindow(t.getSubViewContainer()); |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | @Override |
| 2163 | public void attachSubWindow(Tab tab) { |
| 2164 | if (tab.getSubWebView() != null) { |
| 2165 | mUi.attachSubWindow(tab.getSubViewContainer()); |
| 2166 | getCurrentTopWebView().requestFocus(); |
| 2167 | } |
| 2168 | } |
| 2169 | |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 2170 | private Tab showPreloadedTab(final UrlData urlData) { |
| 2171 | if (!urlData.isPreloaded()) { |
| 2172 | return null; |
| 2173 | } |
| 2174 | final PreloadedTabControl tabControl = urlData.getPreloadedTab(); |
| 2175 | final String sbQuery = urlData.getSearchBoxQueryToSubmit(); |
| 2176 | if (sbQuery != null) { |
| 2177 | if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) { |
| 2178 | // Could not submit query. Fallback to regular tab creation |
| 2179 | tabControl.destroy(); |
| 2180 | return null; |
| 2181 | } |
| 2182 | } |
Michael Kolb | ff6a748 | 2011-07-26 16:37:15 -0700 | [diff] [blame] | 2183 | // check tab count and make room for new tab |
| 2184 | if (!mTabControl.canCreateNewTab()) { |
| 2185 | Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab()); |
| 2186 | if (leastUsed != null) { |
| 2187 | closeTab(leastUsed); |
| 2188 | } |
| 2189 | } |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 2190 | Tab t = tabControl.getTab(); |
| 2191 | mTabControl.addPreloadedTab(t); |
| 2192 | addTab(t); |
| 2193 | setActiveTab(t); |
| 2194 | return t; |
| 2195 | } |
| 2196 | |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2197 | // open a non inconito tab with the given url data |
| 2198 | // and set as active tab |
| 2199 | public Tab openTab(UrlData urlData) { |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 2200 | Tab tab = showPreloadedTab(urlData); |
| 2201 | if (tab == null) { |
| 2202 | tab = createNewTab(false, true, true); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 2203 | if ((tab != null) && !urlData.isEmpty()) { |
| 2204 | loadUrlDataIn(tab, urlData); |
| 2205 | } |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2206 | } |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 2207 | return tab; |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2208 | } |
| 2209 | |
Michael Kolb | 843510f | 2010-12-09 10:51:49 -0800 | [diff] [blame] | 2210 | @Override |
| 2211 | public Tab openTabToHomePage() { |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2212 | return openTab(mSettings.getHomePage(), false, true, false); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2213 | } |
| 2214 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2215 | @Override |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 2216 | public Tab openIncognitoTab() { |
| 2217 | return openTab(INCOGNITO_URI, true, true, false); |
| 2218 | } |
| 2219 | |
| 2220 | @Override |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2221 | public Tab openTab(String url, boolean incognito, boolean setActive, |
| 2222 | boolean useCurrent) { |
John Reck | 5949c66 | 2011-05-27 09:52:29 -0700 | [diff] [blame] | 2223 | return openTab(url, incognito, setActive, useCurrent, null); |
| 2224 | } |
| 2225 | |
| 2226 | @Override |
| 2227 | public Tab openTab(String url, Tab parent, boolean setActive, |
| 2228 | boolean useCurrent) { |
| 2229 | return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(), |
| 2230 | setActive, useCurrent, parent); |
| 2231 | } |
| 2232 | |
| 2233 | public Tab openTab(String url, boolean incognito, boolean setActive, |
| 2234 | boolean useCurrent, Tab parent) { |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2235 | Tab tab = createNewTab(incognito, setActive, useCurrent); |
| 2236 | if (tab != null) { |
John Reck | 5949c66 | 2011-05-27 09:52:29 -0700 | [diff] [blame] | 2237 | if (parent != null && parent != tab) { |
| 2238 | parent.addChildTab(tab); |
| 2239 | } |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 2240 | if (url != null) { |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2241 | loadUrl(tab, url); |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 2242 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2243 | } |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2244 | return tab; |
| 2245 | } |
| 2246 | |
| 2247 | // this method will attempt to create a new tab |
| 2248 | // incognito: private browsing tab |
| 2249 | // setActive: ste tab as current tab |
| 2250 | // useCurrent: if no new tab can be created, return current tab |
| 2251 | private Tab createNewTab(boolean incognito, boolean setActive, |
| 2252 | boolean useCurrent) { |
| 2253 | Tab tab = null; |
| 2254 | if (mTabControl.canCreateNewTab()) { |
| 2255 | tab = mTabControl.createNewTab(incognito); |
| 2256 | addTab(tab); |
| 2257 | if (setActive) { |
| 2258 | setActiveTab(tab); |
| 2259 | } |
| 2260 | } else { |
| 2261 | if (useCurrent) { |
| 2262 | tab = mTabControl.getCurrentTab(); |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2263 | reuseTab(tab, null); |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2264 | } else { |
| 2265 | mUi.showMaxTabsWarning(); |
| 2266 | } |
| 2267 | } |
| 2268 | return tab; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2269 | } |
| 2270 | |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 2271 | @Override |
| 2272 | public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) { |
| 2273 | SnapshotTab tab = null; |
| 2274 | if (mTabControl.canCreateNewTab()) { |
| 2275 | tab = mTabControl.createSnapshotTab(snapshotId); |
| 2276 | addTab(tab); |
| 2277 | if (setActive) { |
| 2278 | setActiveTab(tab); |
| 2279 | } |
| 2280 | } else { |
| 2281 | mUi.showMaxTabsWarning(); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 2282 | } |
| 2283 | return tab; |
| 2284 | } |
| 2285 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2286 | /** |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2287 | * @param tab the tab to switch to |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2288 | * @return boolean True if we successfully switched to a different tab. If |
| 2289 | * the indexth tab is null, or if that tab is the same as |
| 2290 | * the current one, return false. |
| 2291 | */ |
| 2292 | @Override |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2293 | public boolean switchToTab(Tab tab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2294 | Tab currentTab = mTabControl.getCurrentTab(); |
| 2295 | if (tab == null || tab == currentTab) { |
| 2296 | return false; |
| 2297 | } |
| 2298 | setActiveTab(tab); |
| 2299 | return true; |
| 2300 | } |
| 2301 | |
| 2302 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2303 | public void closeCurrentTab() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2304 | if (mTabControl.getTabCount() == 1) { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 2305 | mCrashRecoveryHandler.clearState(); |
Michael Kolb | c1eeb12 | 2011-08-01 09:56:26 -0700 | [diff] [blame] | 2306 | mTabControl.removeTab(getCurrentTab()); |
John Reck | 958b242 | 2010-12-03 17:56:17 -0800 | [diff] [blame] | 2307 | mActivity.finish(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2308 | return; |
| 2309 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2310 | final Tab current = mTabControl.getCurrentTab(); |
| 2311 | final int pos = mTabControl.getCurrentPosition(); |
| 2312 | Tab newTab = current.getParent(); |
| 2313 | if (newTab == null) { |
| 2314 | newTab = mTabControl.getTab(pos + 1); |
| 2315 | if (newTab == null) { |
| 2316 | newTab = mTabControl.getTab(pos - 1); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2317 | } |
| 2318 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2319 | if (switchToTab(newTab)) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2320 | // Close window |
| 2321 | closeTab(current); |
| 2322 | } |
| 2323 | } |
| 2324 | |
| 2325 | /** |
| 2326 | * Close the tab, remove its associated title bar, and adjust mTabControl's |
| 2327 | * current tab to a valid value. |
| 2328 | */ |
| 2329 | @Override |
| 2330 | public void closeTab(Tab tab) { |
Michael Kolb | 2d59c32 | 2011-01-25 13:18:55 -0800 | [diff] [blame] | 2331 | removeTab(tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2334 | // Called when loading from context menu or LOAD_URL message |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2335 | protected void loadUrlFromContext(String url) { |
| 2336 | Tab tab = getCurrentTab(); |
| 2337 | WebView view = tab != null ? tab.getWebView() : null; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2338 | // In case the user enters nothing. |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2339 | if (url != null && url.length() != 0 && tab != null && view != null) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2340 | url = UrlUtils.smartUrlFilter(url); |
| 2341 | if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) { |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2342 | loadUrl(tab, url); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | /** |
| 2348 | * Load the URL into the given WebView and update the title bar |
| 2349 | * to reflect the new load. Call this instead of WebView.loadUrl |
| 2350 | * directly. |
| 2351 | * @param view The WebView used to load url. |
| 2352 | * @param url The URL to load. |
| 2353 | */ |
John Reck | 71e5142 | 2011-07-01 16:49:28 -0700 | [diff] [blame] | 2354 | @Override |
| 2355 | public void loadUrl(Tab tab, String url) { |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2356 | loadUrl(tab, url, null); |
| 2357 | } |
| 2358 | |
| 2359 | protected void loadUrl(Tab tab, String url, Map<String, String> headers) { |
| 2360 | if (tab != null) { |
| 2361 | dismissSubWindow(tab); |
| 2362 | tab.loadUrl(url, headers); |
| 2363 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | /** |
| 2367 | * Load UrlData into a Tab and update the title bar to reflect the new |
| 2368 | * load. Call this instead of UrlData.loadIn directly. |
| 2369 | * @param t The Tab used to load. |
| 2370 | * @param data The UrlData being loaded. |
| 2371 | */ |
| 2372 | protected void loadUrlDataIn(Tab t, UrlData data) { |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2373 | if (data != null) { |
| 2374 | if (data.mVoiceIntent != null) { |
| 2375 | t.activateVoiceSearchMode(data.mVoiceIntent); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 2376 | } else if (data.isPreloaded()) { |
| 2377 | // this isn't called for preloaded tabs |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2378 | } else { |
| 2379 | loadUrl(t, data.mUrl, data.mHeaders); |
| 2380 | } |
| 2381 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2384 | @Override |
| 2385 | public void onUserCanceledSsl(Tab tab) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2386 | // TODO: Figure out the "right" behavior |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 2387 | if (tab.canGoBack()) { |
| 2388 | tab.goBack(); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2389 | } else { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 2390 | tab.loadUrl(mSettings.getHomePage(), null); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2391 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | void goBackOnePageOrQuit() { |
| 2395 | Tab current = mTabControl.getCurrentTab(); |
| 2396 | if (current == null) { |
| 2397 | /* |
| 2398 | * Instead of finishing the activity, simply push this to the back |
| 2399 | * of the stack and let ActivityManager to choose the foreground |
| 2400 | * activity. As BrowserActivity is singleTask, it will be always the |
| 2401 | * root of the task. So we can use either true or false for |
| 2402 | * moveTaskToBack(). |
| 2403 | */ |
| 2404 | mActivity.moveTaskToBack(true); |
| 2405 | return; |
| 2406 | } |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 2407 | if (current.canGoBack()) { |
| 2408 | current.goBack(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2409 | } else { |
| 2410 | // Check to see if we are closing a window that was created by |
| 2411 | // another window. If so, we switch back to that window. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2412 | Tab parent = current.getParent(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2413 | if (parent != null) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2414 | switchToTab(parent); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2415 | // Now we close the other tab |
| 2416 | closeTab(current); |
| 2417 | } else { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2418 | /* |
| 2419 | * Instead of finishing the activity, simply push this to the back |
| 2420 | * of the stack and let ActivityManager to choose the foreground |
| 2421 | * activity. As BrowserActivity is singleTask, it will be always the |
| 2422 | * root of the task. So we can use either true or false for |
| 2423 | * moveTaskToBack(). |
| 2424 | */ |
| 2425 | mActivity.moveTaskToBack(true); |
| 2426 | } |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | /** |
| 2431 | * Feed the previously stored results strings to the BrowserProvider so that |
| 2432 | * the SearchDialog will show them instead of the standard searches. |
| 2433 | * @param result String to show on the editable line of the SearchDialog. |
| 2434 | */ |
| 2435 | @Override |
| 2436 | public void showVoiceSearchResults(String result) { |
| 2437 | ContentProviderClient client = mActivity.getContentResolver() |
| 2438 | .acquireContentProviderClient(Browser.BOOKMARKS_URI); |
| 2439 | ContentProvider prov = client.getLocalContentProvider(); |
| 2440 | BrowserProvider bp = (BrowserProvider) prov; |
| 2441 | bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults()); |
| 2442 | client.release(); |
| 2443 | |
| 2444 | Bundle bundle = createGoogleSearchSourceBundle( |
| 2445 | GOOGLE_SEARCH_SOURCE_SEARCHKEY); |
| 2446 | bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true); |
| 2447 | startSearch(result, false, bundle, false); |
| 2448 | } |
| 2449 | |
| 2450 | private void startSearch(String initialQuery, boolean selectInitialQuery, |
| 2451 | Bundle appSearchData, boolean globalSearch) { |
| 2452 | if (appSearchData == null) { |
| 2453 | appSearchData = createGoogleSearchSourceBundle( |
| 2454 | GOOGLE_SEARCH_SOURCE_TYPE); |
| 2455 | } |
| 2456 | |
| 2457 | SearchEngine searchEngine = mSettings.getSearchEngine(); |
| 2458 | if (searchEngine != null && !searchEngine.supportsVoiceSearch()) { |
| 2459 | appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true); |
| 2460 | } |
| 2461 | mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData, |
| 2462 | globalSearch); |
| 2463 | } |
| 2464 | |
| 2465 | private Bundle createGoogleSearchSourceBundle(String source) { |
| 2466 | Bundle bundle = new Bundle(); |
| 2467 | bundle.putString(Search.SOURCE, source); |
| 2468 | return bundle; |
| 2469 | } |
| 2470 | |
| 2471 | /** |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 2472 | * helper method for key handler |
| 2473 | * returns the current tab if it can't advance |
| 2474 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2475 | private Tab getNextTab() { |
| 2476 | return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1, |
| 2477 | mTabControl.getCurrentPosition() + 1)); |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | /** |
| 2481 | * helper method for key handler |
| 2482 | * returns the current tab if it can't advance |
| 2483 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2484 | private Tab getPrevTab() { |
| 2485 | return mTabControl.getTab(Math.max(0, |
| 2486 | mTabControl.getCurrentPosition() - 1)); |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | /** |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2490 | * handle key events in browser |
| 2491 | * |
| 2492 | * @param keyCode |
| 2493 | * @param event |
| 2494 | * @return true if handled, false to pass to super |
| 2495 | */ |
| 2496 | boolean onKeyDown(int keyCode, KeyEvent event) { |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 2497 | boolean noModifiers = event.hasNoModifiers(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2498 | // Even if MENU is already held down, we need to call to super to open |
| 2499 | // the IME on long press. |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 2500 | if (!noModifiers |
| 2501 | && ((KeyEvent.KEYCODE_MENU == keyCode) |
| 2502 | || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode) |
| 2503 | || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2504 | mMenuIsDown = true; |
| 2505 | return false; |
| 2506 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2507 | |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2508 | WebView webView = getCurrentTopWebView(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 2509 | Tab tab = getCurrentTab(); |
| 2510 | if (webView == null || tab == null) return false; |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2511 | |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 2512 | boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON); |
| 2513 | boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON); |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2514 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2515 | switch(keyCode) { |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 2516 | case KeyEvent.KEYCODE_TAB: |
| 2517 | if (event.isCtrlPressed()) { |
| 2518 | if (event.isShiftPressed()) { |
| 2519 | // prev tab |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2520 | switchToTab(getPrevTab()); |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 2521 | } else { |
| 2522 | // next tab |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2523 | switchToTab(getNextTab()); |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 2524 | } |
| 2525 | return true; |
| 2526 | } |
| 2527 | break; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2528 | case KeyEvent.KEYCODE_SPACE: |
| 2529 | // WebView/WebTextView handle the keys in the KeyDown. As |
| 2530 | // the Activity's shortcut keys are only handled when WebView |
| 2531 | // doesn't, have to do it in onKeyDown instead of onKeyUp. |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 2532 | if (shift) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2533 | pageUp(); |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 2534 | } else if (noModifiers) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2535 | pageDown(); |
| 2536 | } |
| 2537 | return true; |
| 2538 | case KeyEvent.KEYCODE_BACK: |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 2539 | if (!noModifiers) break; |
John Reck | e6bf4ab | 2011-02-24 15:48:05 -0800 | [diff] [blame] | 2540 | event.startTracking(); |
| 2541 | return true; |
Michael Kolb | e9e1d4a | 2011-07-14 15:02:17 -0700 | [diff] [blame] | 2542 | case KeyEvent.KEYCODE_FORWARD: |
| 2543 | if (!noModifiers) break; |
| 2544 | tab.goForward(); |
| 2545 | return true; |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2546 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 2547 | if (ctrl) { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 2548 | tab.goBack(); |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2549 | return true; |
| 2550 | } |
| 2551 | break; |
| 2552 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 2553 | if (ctrl) { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 2554 | tab.goForward(); |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2555 | return true; |
| 2556 | } |
| 2557 | break; |
| 2558 | case KeyEvent.KEYCODE_A: |
| 2559 | if (ctrl) { |
| 2560 | webView.selectAll(); |
| 2561 | return true; |
| 2562 | } |
| 2563 | break; |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 2564 | // case KeyEvent.KEYCODE_B: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2565 | case KeyEvent.KEYCODE_C: |
| 2566 | if (ctrl) { |
| 2567 | webView.copySelection(); |
| 2568 | return true; |
| 2569 | } |
| 2570 | break; |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 2571 | // case KeyEvent.KEYCODE_D: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2572 | // case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 2573 | // case KeyEvent.KEYCODE_F: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2574 | // case KeyEvent.KEYCODE_G: // in Chrome: finds next match |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 2575 | // case KeyEvent.KEYCODE_H: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2576 | // case KeyEvent.KEYCODE_I: // unused |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 2577 | // case KeyEvent.KEYCODE_J: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2578 | // case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 2579 | // case KeyEvent.KEYCODE_L: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2580 | // case KeyEvent.KEYCODE_M: // unused |
| 2581 | // case KeyEvent.KEYCODE_N: // in Chrome: new window |
| 2582 | // case KeyEvent.KEYCODE_O: // in Chrome: open file |
| 2583 | // case KeyEvent.KEYCODE_P: // in Chrome: print page |
| 2584 | // case KeyEvent.KEYCODE_Q: // unused |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 2585 | // case KeyEvent.KEYCODE_R: |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2586 | // case KeyEvent.KEYCODE_S: // in Chrome: saves page |
| 2587 | case KeyEvent.KEYCODE_T: |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 2588 | // we can't use the ctrl/shift flags, they check for |
| 2589 | // exclusive use of a modifier |
| 2590 | if (event.isCtrlPressed()) { |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2591 | if (event.isShiftPressed()) { |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 2592 | openIncognitoTab(); |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2593 | } else { |
| 2594 | openTabToHomePage(); |
| 2595 | } |
| 2596 | return true; |
| 2597 | } |
| 2598 | break; |
| 2599 | // case KeyEvent.KEYCODE_U: // in Chrome: opens source of page |
| 2600 | // case KeyEvent.KEYCODE_V: // text view intercepts to paste |
Michael Kolb | 1a2eba4 | 2011-03-16 16:42:49 -0700 | [diff] [blame] | 2601 | case KeyEvent.KEYCODE_W: // in Chrome: close tab |
| 2602 | if (ctrl) { |
| 2603 | closeCurrentTab(); |
| 2604 | return true; |
| 2605 | } |
| 2606 | break; |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 2607 | // case KeyEvent.KEYCODE_X: // text view intercepts to cut |
| 2608 | // case KeyEvent.KEYCODE_Y: // unused |
| 2609 | // case KeyEvent.KEYCODE_Z: // unused |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2610 | } |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 2611 | // it is a regular key and webview is not null |
| 2612 | return mUi.dispatchKey(keyCode, event); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2613 | } |
| 2614 | |
John Reck | e6bf4ab | 2011-02-24 15:48:05 -0800 | [diff] [blame] | 2615 | boolean onKeyLongPress(int keyCode, KeyEvent event) { |
| 2616 | switch(keyCode) { |
| 2617 | case KeyEvent.KEYCODE_BACK: |
| 2618 | if (mUi.showsWeb()) { |
| 2619 | bookmarksOrHistoryPicker(true); |
| 2620 | return true; |
| 2621 | } |
| 2622 | break; |
| 2623 | } |
| 2624 | return false; |
| 2625 | } |
| 2626 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2627 | boolean onKeyUp(int keyCode, KeyEvent event) { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 2628 | if (KeyEvent.KEYCODE_MENU == keyCode) { |
| 2629 | mMenuIsDown = false; |
| 2630 | if (event.isTracking() && !event.isCanceled()) { |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 2631 | return onMenuKey(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 2632 | } |
| 2633 | } |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 2634 | if (!event.hasNoModifiers()) return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2635 | switch(keyCode) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2636 | case KeyEvent.KEYCODE_BACK: |
| 2637 | if (event.isTracking() && !event.isCanceled()) { |
| 2638 | onBackKey(); |
| 2639 | return true; |
| 2640 | } |
| 2641 | break; |
| 2642 | } |
| 2643 | return false; |
| 2644 | } |
| 2645 | |
| 2646 | public boolean isMenuDown() { |
| 2647 | return mMenuIsDown; |
| 2648 | } |
| 2649 | |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 2650 | public void setupAutoFill(Message message) { |
| 2651 | // Open the settings activity at the AutoFill profile fragment so that |
| 2652 | // the user can create a new profile. When they return, we will dispatch |
| 2653 | // the message so that we can autofill the form using their new profile. |
| 2654 | Intent intent = new Intent(mActivity, BrowserPreferencesPage.class); |
| 2655 | intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, |
| 2656 | AutoFillSettingsFragment.class.getName()); |
| 2657 | mAutoFillSetupMessage = message; |
| 2658 | mActivity.startActivityForResult(intent, AUTOFILL_SETUP); |
| 2659 | } |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 2660 | |
| 2661 | @Override |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 2662 | public void registerDropdownChangeListener(DropdownChangeListener d) { |
| 2663 | mUi.registerDropdownChangeListener(d); |
| 2664 | } |
Michael Kolb | fbc579a | 2011-07-07 15:59:33 -0700 | [diff] [blame] | 2665 | |
| 2666 | public boolean onSearchRequested() { |
| 2667 | mUi.editUrl(false); |
| 2668 | return true; |
| 2669 | } |
| 2670 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 2671 | @Override |
| 2672 | public boolean shouldCaptureThumbnails() { |
| 2673 | return mUi.shouldCaptureThumbnails(); |
| 2674 | } |
| 2675 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2676 | } |