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