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