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