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