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