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