The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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.google.android.googleapps.IGoogleLoginService; |
| 20 | import com.google.android.googlelogin.GoogleLoginServiceConstants; |
| 21 | |
| 22 | import android.app.Activity; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 23 | import android.app.AlertDialog; |
| 24 | import android.app.ProgressDialog; |
| 25 | import android.app.SearchManager; |
| 26 | import android.content.ActivityNotFoundException; |
| 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.ComponentName; |
| 29 | import android.content.ContentResolver; |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 30 | import android.content.ContentUris; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 31 | import android.content.ContentValues; |
| 32 | import android.content.Context; |
| 33 | import android.content.DialogInterface; |
| 34 | import android.content.Intent; |
| 35 | import android.content.IntentFilter; |
| 36 | import android.content.ServiceConnection; |
| 37 | import android.content.DialogInterface.OnCancelListener; |
Grace Kloba | b4da0ad | 2009-05-14 14:45:40 -0700 | [diff] [blame] | 38 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | import android.content.pm.PackageManager; |
| 40 | import android.content.pm.ResolveInfo; |
| 41 | import android.content.res.AssetManager; |
| 42 | import android.content.res.Configuration; |
| 43 | import android.content.res.Resources; |
| 44 | import android.database.Cursor; |
| 45 | import android.database.sqlite.SQLiteDatabase; |
| 46 | import android.database.sqlite.SQLiteException; |
| 47 | import android.graphics.Bitmap; |
Andrei Popescu | 540035d | 2009-09-18 18:59:20 +0100 | [diff] [blame] | 48 | import android.graphics.BitmapFactory; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 49 | import android.graphics.Canvas; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 50 | import android.graphics.DrawFilter; |
| 51 | import android.graphics.Paint; |
| 52 | import android.graphics.PaintFlagsDrawFilter; |
| 53 | import android.graphics.Picture; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 54 | import android.graphics.PixelFormat; |
| 55 | import android.graphics.Rect; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 56 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 57 | import android.hardware.SensorListener; |
| 58 | import android.hardware.SensorManager; |
| 59 | import android.net.ConnectivityManager; |
Patrick Scott | eb6ab2a | 2009-09-16 10:00:17 -0400 | [diff] [blame] | 60 | import android.net.NetworkInfo; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 61 | import android.net.Uri; |
| 62 | import android.net.WebAddress; |
| 63 | import android.net.http.EventHandler; |
| 64 | import android.net.http.SslCertificate; |
| 65 | import android.net.http.SslError; |
| 66 | import android.os.AsyncTask; |
| 67 | import android.os.Bundle; |
| 68 | import android.os.Debug; |
| 69 | import android.os.Environment; |
| 70 | import android.os.Handler; |
| 71 | import android.os.IBinder; |
| 72 | import android.os.Message; |
| 73 | import android.os.PowerManager; |
| 74 | import android.os.Process; |
| 75 | import android.os.RemoteException; |
| 76 | import android.os.ServiceManager; |
| 77 | import android.os.SystemClock; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 78 | import android.provider.Browser; |
Cary Clark | 5e335a3 | 2009-09-22 14:53:11 -0400 | [diff] [blame] | 79 | import android.provider.ContactsContract; |
| 80 | import android.provider.ContactsContract.Intents.Insert; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 81 | import android.provider.Downloads; |
| 82 | import android.provider.MediaStore; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 83 | import android.text.IClipboard; |
| 84 | import android.text.TextUtils; |
| 85 | import android.text.format.DateFormat; |
| 86 | import android.text.util.Regex; |
Leon Scroggins | b94bf27 | 2009-09-25 15:22:08 -0400 | [diff] [blame] | 87 | import android.util.AttributeSet; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 88 | import android.util.Log; |
| 89 | import android.view.ContextMenu; |
| 90 | import android.view.Gravity; |
| 91 | import android.view.KeyEvent; |
| 92 | import android.view.LayoutInflater; |
| 93 | import android.view.Menu; |
| 94 | import android.view.MenuInflater; |
| 95 | import android.view.MenuItem; |
| 96 | import android.view.View; |
| 97 | import android.view.ViewGroup; |
| 98 | import android.view.Window; |
| 99 | import android.view.WindowManager; |
| 100 | import android.view.ContextMenu.ContextMenuInfo; |
| 101 | import android.view.MenuItem.OnMenuItemClickListener; |
| 102 | import android.view.animation.AlphaAnimation; |
| 103 | import android.view.animation.Animation; |
| 104 | import android.view.animation.AnimationSet; |
| 105 | import android.view.animation.DecelerateInterpolator; |
| 106 | import android.view.animation.ScaleAnimation; |
| 107 | import android.view.animation.TranslateAnimation; |
| 108 | import android.webkit.CookieManager; |
| 109 | import android.webkit.CookieSyncManager; |
| 110 | import android.webkit.DownloadListener; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 111 | import android.webkit.GeolocationPermissions; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 112 | import android.webkit.HttpAuthHandler; |
Grace Kloba | b4da0ad | 2009-05-14 14:45:40 -0700 | [diff] [blame] | 113 | import android.webkit.PluginManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 114 | import android.webkit.SslErrorHandler; |
| 115 | import android.webkit.URLUtil; |
| 116 | import android.webkit.WebChromeClient; |
Andrei Popescu | c9b5556 | 2009-07-07 10:51:15 +0100 | [diff] [blame] | 117 | import android.webkit.WebChromeClient.CustomViewCallback; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 118 | import android.webkit.WebHistoryItem; |
| 119 | import android.webkit.WebIconDatabase; |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 120 | import android.webkit.WebStorage; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 121 | import android.webkit.WebView; |
| 122 | import android.webkit.WebViewClient; |
| 123 | import android.widget.EditText; |
| 124 | import android.widget.FrameLayout; |
| 125 | import android.widget.LinearLayout; |
| 126 | import android.widget.TextView; |
| 127 | import android.widget.Toast; |
| 128 | |
| 129 | import java.io.BufferedOutputStream; |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 130 | import java.io.ByteArrayOutputStream; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 131 | import java.io.File; |
| 132 | import java.io.FileInputStream; |
| 133 | import java.io.FileOutputStream; |
| 134 | import java.io.IOException; |
| 135 | import java.io.InputStream; |
| 136 | import java.net.MalformedURLException; |
| 137 | import java.net.URI; |
Dianne Hackborn | 9918943 | 2009-06-17 18:06:18 -0700 | [diff] [blame] | 138 | import java.net.URISyntaxException; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 139 | import java.net.URL; |
| 140 | import java.net.URLEncoder; |
| 141 | import java.text.ParseException; |
| 142 | import java.util.Date; |
| 143 | import java.util.Enumeration; |
| 144 | import java.util.HashMap; |
Patrick Scott | 37911c7 | 2009-03-24 18:02:58 -0700 | [diff] [blame] | 145 | import java.util.LinkedList; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 146 | import java.util.Vector; |
| 147 | import java.util.regex.Matcher; |
| 148 | import java.util.regex.Pattern; |
| 149 | import java.util.zip.ZipEntry; |
| 150 | import java.util.zip.ZipFile; |
| 151 | |
| 152 | public class BrowserActivity extends Activity |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 153 | implements View.OnCreateContextMenuListener, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 154 | DownloadListener { |
| 155 | |
Dave Bort | 31a6d1c | 2009-04-13 15:56:49 -0700 | [diff] [blame] | 156 | /* Define some aliases to make these debugging flags easier to refer to. |
| 157 | * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG". |
| 158 | */ |
| 159 | private final static boolean DEBUG = com.android.browser.Browser.DEBUG; |
| 160 | private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED; |
| 161 | private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED; |
| 162 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 163 | private IGoogleLoginService mGls = null; |
| 164 | private ServiceConnection mGlsConnection = null; |
| 165 | |
| 166 | private SensorManager mSensorManager = null; |
| 167 | |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 168 | // These are single-character shortcuts for searching popular sources. |
| 169 | private static final int SHORTCUT_INVALID = 0; |
| 170 | private static final int SHORTCUT_GOOGLE_SEARCH = 1; |
| 171 | private static final int SHORTCUT_WIKIPEDIA_SEARCH = 2; |
| 172 | private static final int SHORTCUT_DICTIONARY_SEARCH = 3; |
| 173 | private static final int SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH = 4; |
| 174 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 175 | /* Whitelisted webpages |
| 176 | private static HashSet<String> sWhiteList; |
| 177 | |
| 178 | static { |
| 179 | sWhiteList = new HashSet<String>(); |
| 180 | sWhiteList.add("cnn.com/"); |
| 181 | sWhiteList.add("espn.go.com/"); |
| 182 | sWhiteList.add("nytimes.com/"); |
| 183 | sWhiteList.add("engadget.com/"); |
| 184 | sWhiteList.add("yahoo.com/"); |
| 185 | sWhiteList.add("msn.com/"); |
| 186 | sWhiteList.add("amazon.com/"); |
| 187 | sWhiteList.add("consumerist.com/"); |
| 188 | sWhiteList.add("google.com/m/news"); |
| 189 | } |
| 190 | */ |
| 191 | |
| 192 | private void setupHomePage() { |
| 193 | final Runnable getAccount = new Runnable() { |
| 194 | public void run() { |
| 195 | // Lower priority |
| 196 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 197 | // get the default home page |
| 198 | String homepage = mSettings.getHomePage(); |
| 199 | |
| 200 | try { |
| 201 | if (mGls == null) return; |
| 202 | |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 203 | if (!homepage.startsWith("http://www.google.")) return; |
| 204 | if (homepage.indexOf('?') == -1) return; |
| 205 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 206 | String hostedUser = mGls.getAccount(GoogleLoginServiceConstants.PREFER_HOSTED); |
| 207 | String googleUser = mGls.getAccount(GoogleLoginServiceConstants.REQUIRE_GOOGLE); |
| 208 | |
| 209 | // three cases: |
| 210 | // |
| 211 | // hostedUser == googleUser |
| 212 | // The device has only a google account |
| 213 | // |
| 214 | // hostedUser != googleUser |
| 215 | // The device has a hosted account and a google account |
| 216 | // |
| 217 | // hostedUser != null, googleUser == null |
| 218 | // The device has only a hosted account (so far) |
| 219 | |
| 220 | // developers might have no accounts at all |
| 221 | if (hostedUser == null) return; |
| 222 | |
| 223 | if (googleUser == null || !hostedUser.equals(googleUser)) { |
| 224 | String domain = hostedUser.substring(hostedUser.lastIndexOf('@')+1); |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 225 | homepage = homepage.replace("?", "/a/" + domain + "?"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 226 | } |
| 227 | } catch (RemoteException ignore) { |
| 228 | // Login service died; carry on |
| 229 | } catch (RuntimeException ignore) { |
| 230 | // Login service died; carry on |
| 231 | } finally { |
| 232 | finish(homepage); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private void finish(final String homepage) { |
| 237 | mHandler.post(new Runnable() { |
| 238 | public void run() { |
| 239 | mSettings.setHomePage(BrowserActivity.this, homepage); |
| 240 | resumeAfterCredentials(); |
| 241 | |
| 242 | // as this is running in a separate thread, |
| 243 | // BrowserActivity's onDestroy() may have been called, |
| 244 | // which also calls unbindService(). |
| 245 | if (mGlsConnection != null) { |
| 246 | // we no longer need to keep GLS open |
| 247 | unbindService(mGlsConnection); |
| 248 | mGlsConnection = null; |
| 249 | } |
| 250 | } }); |
| 251 | } }; |
| 252 | |
| 253 | final boolean[] done = { false }; |
| 254 | |
| 255 | // Open a connection to the Google Login Service. The first |
| 256 | // time the connection is established, set up the homepage depending on |
| 257 | // the account in a background thread. |
| 258 | mGlsConnection = new ServiceConnection() { |
| 259 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 260 | mGls = IGoogleLoginService.Stub.asInterface(service); |
| 261 | if (done[0] == false) { |
| 262 | done[0] = true; |
| 263 | Thread account = new Thread(getAccount); |
| 264 | account.setName("GLSAccount"); |
| 265 | account.start(); |
| 266 | } |
| 267 | } |
| 268 | public void onServiceDisconnected(ComponentName className) { |
| 269 | mGls = null; |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | bindService(GoogleLoginServiceConstants.SERVICE_INTENT, |
| 274 | mGlsConnection, Context.BIND_AUTO_CREATE); |
| 275 | } |
| 276 | |
Cary Clark | a977124 | 2009-08-11 16:42:26 -0400 | [diff] [blame] | 277 | private static class ClearThumbnails extends AsyncTask<File, Void, Void> { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 278 | @Override |
| 279 | public Void doInBackground(File... files) { |
| 280 | if (files != null) { |
| 281 | for (File f : files) { |
Cary Clark | d6be175 | 2009-08-12 12:56:42 -0400 | [diff] [blame] | 282 | if (!f.delete()) { |
| 283 | Log.e(LOGTAG, f.getPath() + " was not deleted"); |
| 284 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | return null; |
| 288 | } |
| 289 | } |
| 290 | |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 291 | /** |
| 292 | * This layout holds everything you see below the status bar, including the |
| 293 | * error console, the custom view container, and the webviews. |
| 294 | */ |
| 295 | private FrameLayout mBrowserFrameLayout; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 296 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 297 | @Override public void onCreate(Bundle icicle) { |
Dave Bort | 31a6d1c | 2009-04-13 15:56:49 -0700 | [diff] [blame] | 298 | if (LOGV_ENABLED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 299 | Log.v(LOGTAG, this + " onStart"); |
| 300 | } |
| 301 | super.onCreate(icicle); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 302 | // test the browser in OpenGL |
| 303 | // requestWindowFeature(Window.FEATURE_OPENGL); |
| 304 | |
| 305 | setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); |
| 306 | |
| 307 | mResolver = getContentResolver(); |
| 308 | |
Grace Kloba | 0923d69 | 2009-09-23 21:37:25 -0700 | [diff] [blame] | 309 | // If this was a web search request, pass it on to the default web |
| 310 | // search provider and finish this activity. |
| 311 | if (handleWebSearchIntent(getIntent())) { |
| 312 | finish(); |
| 313 | return; |
| 314 | } |
| 315 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 316 | // |
| 317 | // start MASF proxy service |
| 318 | // |
| 319 | //Intent proxyServiceIntent = new Intent(); |
| 320 | //proxyServiceIntent.setComponent |
| 321 | // (new ComponentName( |
| 322 | // "com.android.masfproxyservice", |
| 323 | // "com.android.masfproxyservice.MasfProxyService")); |
| 324 | //startService(proxyServiceIntent, null); |
| 325 | |
| 326 | mSecLockIcon = Resources.getSystem().getDrawable( |
| 327 | android.R.drawable.ic_secure); |
| 328 | mMixLockIcon = Resources.getSystem().getDrawable( |
| 329 | android.R.drawable.ic_partial_secure); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 330 | |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 331 | FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView() |
| 332 | .findViewById(com.android.internal.R.id.content); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 333 | mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this) |
| 334 | .inflate(R.layout.custom_screen, null); |
| 335 | mContentView = (FrameLayout) mBrowserFrameLayout.findViewById( |
| 336 | R.id.main_content); |
| 337 | mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout |
| 338 | .findViewById(R.id.error_console); |
| 339 | mCustomViewContainer = (FrameLayout) mBrowserFrameLayout |
| 340 | .findViewById(R.id.fullscreen_custom_content); |
| 341 | frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS); |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 342 | mTitleBar = new TitleBar(this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 343 | |
| 344 | // Create the tab control and our initial tab |
| 345 | mTabControl = new TabControl(this); |
| 346 | |
| 347 | // Open the icon database and retain all the bookmark urls for favicons |
| 348 | retainIconsOnStartup(); |
| 349 | |
| 350 | // Keep a settings instance handy. |
| 351 | mSettings = BrowserSettings.getInstance(); |
| 352 | mSettings.setTabControl(mTabControl); |
| 353 | mSettings.loadFromDb(this); |
| 354 | |
| 355 | PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 356 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser"); |
| 357 | |
Grace Kloba | a34f686 | 2009-07-31 16:28:17 -0700 | [diff] [blame] | 358 | /* enables registration for changes in network status from |
| 359 | http stack */ |
| 360 | mNetworkStateChangedFilter = new IntentFilter(); |
| 361 | mNetworkStateChangedFilter.addAction( |
| 362 | ConnectivityManager.CONNECTIVITY_ACTION); |
| 363 | mNetworkStateIntentReceiver = new BroadcastReceiver() { |
| 364 | @Override |
| 365 | public void onReceive(Context context, Intent intent) { |
| 366 | if (intent.getAction().equals( |
| 367 | ConnectivityManager.CONNECTIVITY_ACTION)) { |
Patrick Scott | eb6ab2a | 2009-09-16 10:00:17 -0400 | [diff] [blame] | 368 | NetworkInfo info = |
| 369 | (NetworkInfo) intent.getParcelableExtra( |
| 370 | ConnectivityManager.EXTRA_NETWORK_INFO); |
| 371 | onNetworkToggle( |
| 372 | (info != null) ? info.isConnected() : false); |
Grace Kloba | a34f686 | 2009-07-31 16:28:17 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | }; |
| 376 | |
Grace Kloba | 615c6c9 | 2009-08-03 10:22:44 -0700 | [diff] [blame] | 377 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 378 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 379 | filter.addDataScheme("package"); |
| 380 | mPackageInstallationReceiver = new BroadcastReceiver() { |
| 381 | @Override |
| 382 | public void onReceive(Context context, Intent intent) { |
| 383 | final String action = intent.getAction(); |
| 384 | final String packageName = intent.getData() |
| 385 | .getSchemeSpecificPart(); |
| 386 | final boolean replacing = intent.getBooleanExtra( |
| 387 | Intent.EXTRA_REPLACING, false); |
| 388 | if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) { |
| 389 | // if it is replacing, refreshPlugins() when adding |
| 390 | return; |
| 391 | } |
| 392 | PackageManager pm = BrowserActivity.this.getPackageManager(); |
| 393 | PackageInfo pkgInfo = null; |
| 394 | try { |
| 395 | pkgInfo = pm.getPackageInfo(packageName, |
| 396 | PackageManager.GET_PERMISSIONS); |
| 397 | } catch (PackageManager.NameNotFoundException e) { |
| 398 | return; |
| 399 | } |
| 400 | if (pkgInfo != null) { |
| 401 | String permissions[] = pkgInfo.requestedPermissions; |
| 402 | if (permissions == null) { |
| 403 | return; |
| 404 | } |
| 405 | boolean permissionOk = false; |
| 406 | for (String permit : permissions) { |
| 407 | if (PluginManager.PLUGIN_PERMISSION.equals(permit)) { |
| 408 | permissionOk = true; |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | if (permissionOk) { |
| 413 | PluginManager.getInstance(BrowserActivity.this) |
| 414 | .refreshPlugins( |
| 415 | Intent.ACTION_PACKAGE_ADDED |
| 416 | .equals(action)); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | }; |
| 421 | registerReceiver(mPackageInstallationReceiver, filter); |
| 422 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 423 | if (!mTabControl.restoreState(icicle)) { |
| 424 | // clear up the thumbnail directory if we can't restore the state as |
| 425 | // none of the files in the directory are referenced any more. |
| 426 | new ClearThumbnails().execute( |
| 427 | mTabControl.getThumbnailDir().listFiles()); |
Grace Kloba | aab3f09 | 2009-07-30 12:29:51 -0700 | [diff] [blame] | 428 | // there is no quit on Android. But if we can't restore the state, |
| 429 | // we can treat it as a new Browser, remove the old session cookies. |
| 430 | CookieManager.getInstance().removeSessionCookie(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 431 | final Intent intent = getIntent(); |
| 432 | final Bundle extra = intent.getExtras(); |
| 433 | // Create an initial tab. |
| 434 | // If the intent is ACTION_VIEW and data is not null, the Browser is |
| 435 | // invoked to view the content by another application. In this case, |
| 436 | // the tab will be close when exit. |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 437 | UrlData urlData = getUrlDataFromIntent(intent); |
| 438 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 439 | final TabControl.Tab t = mTabControl.createNewTab( |
| 440 | Intent.ACTION_VIEW.equals(intent.getAction()) && |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 441 | intent.getData() != null, |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 442 | intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 443 | mTabControl.setCurrentTab(t); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 444 | attachTabToContentView(t); |
| 445 | WebView webView = t.getWebView(); |
| 446 | if (extra != null) { |
| 447 | int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0); |
| 448 | if (scale > 0 && scale <= 1000) { |
| 449 | webView.setInitialScale(scale); |
| 450 | } |
| 451 | } |
| 452 | // If we are not restoring from an icicle, then there is a high |
| 453 | // likely hood this is the first run. So, check to see if the |
| 454 | // homepage needs to be configured and copy any plugins from our |
| 455 | // asset directory to the data partition. |
| 456 | if ((extra == null || !extra.getBoolean("testing")) |
| 457 | && !mSettings.isLoginInitialized()) { |
| 458 | setupHomePage(); |
| 459 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 460 | |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 461 | if (urlData.isEmpty()) { |
Leon Scroggins | 3044423 | 2009-09-04 18:36:20 -0400 | [diff] [blame] | 462 | if (mSettings.isLoginInitialized()) { |
| 463 | webView.loadUrl(mSettings.getHomePage()); |
| 464 | } else { |
| 465 | waitForCredentials(); |
| 466 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 467 | } else { |
Grace Kloba | 81678d9 | 2009-06-30 07:09:56 -0700 | [diff] [blame] | 468 | if (extra != null) { |
| 469 | urlData.setPostData(extra |
| 470 | .getByteArray(Browser.EXTRA_POST_DATA)); |
| 471 | } |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 472 | urlData.loadIn(webView); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 473 | } |
| 474 | } else { |
| 475 | // TabControl.restoreState() will create a new tab even if |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 476 | // restoring the state fails. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 477 | attachTabToContentView(mTabControl.getCurrentTab()); |
| 478 | } |
Grace Kloba | 615c6c9 | 2009-08-03 10:22:44 -0700 | [diff] [blame] | 479 | |
Feng Qian | b3c02da | 2009-06-29 15:58:08 -0700 | [diff] [blame] | 480 | // Read JavaScript flags if it exists. |
| 481 | String jsFlags = mSettings.getJsFlags(); |
| 482 | if (jsFlags.trim().length() != 0) { |
| 483 | mTabControl.getCurrentWebView().setJsFlags(jsFlags); |
| 484 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | @Override |
| 488 | protected void onNewIntent(Intent intent) { |
| 489 | TabControl.Tab current = mTabControl.getCurrentTab(); |
| 490 | // When a tab is closed on exit, the current tab index is set to -1. |
| 491 | // Reset before proceed as Browser requires the current tab to be set. |
| 492 | if (current == null) { |
| 493 | // Try to reset the tab in case the index was incorrect. |
| 494 | current = mTabControl.getTab(0); |
| 495 | if (current == null) { |
| 496 | // No tabs at all so just ignore this intent. |
| 497 | return; |
| 498 | } |
| 499 | mTabControl.setCurrentTab(current); |
| 500 | attachTabToContentView(current); |
| 501 | resetTitleAndIcon(current.getWebView()); |
| 502 | } |
| 503 | final String action = intent.getAction(); |
| 504 | final int flags = intent.getFlags(); |
| 505 | if (Intent.ACTION_MAIN.equals(action) || |
| 506 | (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { |
| 507 | // just resume the browser |
| 508 | return; |
| 509 | } |
| 510 | if (Intent.ACTION_VIEW.equals(action) |
| 511 | || Intent.ACTION_SEARCH.equals(action) |
| 512 | || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action) |
| 513 | || Intent.ACTION_WEB_SEARCH.equals(action)) { |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 514 | // If this was a search request (e.g. search query directly typed into the address bar), |
| 515 | // pass it on to the default web search provider. |
| 516 | if (handleWebSearchIntent(intent)) { |
| 517 | return; |
| 518 | } |
| 519 | |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 520 | UrlData urlData = getUrlDataFromIntent(intent); |
| 521 | if (urlData.isEmpty()) { |
| 522 | urlData = new UrlData(mSettings.getHomePage()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 523 | } |
Grace Kloba | 81678d9 | 2009-06-30 07:09:56 -0700 | [diff] [blame] | 524 | urlData.setPostData(intent |
| 525 | .getByteArrayExtra(Browser.EXTRA_POST_DATA)); |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 526 | |
Grace Kloba | cc63403 | 2009-07-28 15:58:19 -0700 | [diff] [blame] | 527 | final String appId = intent |
| 528 | .getStringExtra(Browser.EXTRA_APPLICATION_ID); |
| 529 | if (Intent.ACTION_VIEW.equals(action) |
| 530 | && !getPackageName().equals(appId) |
| 531 | && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 532 | TabControl.Tab appTab = mTabControl.getTabFromId(appId); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 533 | if (appTab != null) { |
| 534 | Log.i(LOGTAG, "Reusing tab for " + appId); |
| 535 | // Dismiss the subwindow if applicable. |
| 536 | dismissSubWindow(appTab); |
| 537 | // Since we might kill the WebView, remove it from the |
| 538 | // content view first. |
| 539 | removeTabFromContentView(appTab); |
| 540 | // Recreate the main WebView after destroying the old one. |
| 541 | // If the WebView has the same original url and is on that |
| 542 | // page, it can be reused. |
| 543 | boolean needsLoad = |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 544 | mTabControl.recreateWebView(appTab, urlData.mUrl); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 545 | |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 546 | if (current != appTab) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 547 | switchToTab(mTabControl.getTabIndex(appTab)); |
| 548 | if (needsLoad) { |
| 549 | urlData.loadIn(appTab.getWebView()); |
| 550 | } |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 551 | } else { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 552 | // If the tab was the current tab, we have to attach |
| 553 | // it to the view system again. |
| 554 | attachTabToContentView(appTab); |
| 555 | if (needsLoad) { |
| 556 | urlData.loadIn(appTab.getWebView()); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | return; |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 560 | } else { |
| 561 | // No matching application tab, try to find a regular tab |
| 562 | // with a matching url. |
| 563 | appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl); |
Leon Scroggins | 25515f8 | 2009-08-19 15:31:58 -0400 | [diff] [blame] | 564 | if (appTab != null) { |
| 565 | if (current != appTab) { |
| 566 | switchToTab(mTabControl.getTabIndex(appTab)); |
| 567 | } |
| 568 | // Otherwise, we are already viewing the correct tab. |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 569 | } else { |
| 570 | // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url |
| 571 | // will be opened in a new tab unless we have reached |
| 572 | // MAX_TABS. Then the url will be opened in the current |
| 573 | // tab. If a new tab is created, it will have "true" for |
| 574 | // exit on close. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 575 | openTabAndShow(urlData, true, appId); |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 576 | } |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 577 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 578 | } else { |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 579 | if ("about:debug".equals(urlData.mUrl)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 580 | mSettings.toggleDebugSettings(); |
| 581 | return; |
| 582 | } |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 583 | // Get rid of the subwindow if it exists |
| 584 | dismissSubWindow(current); |
| 585 | urlData.loadIn(current.getWebView()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 590 | private int parseUrlShortcut(String url) { |
| 591 | if (url == null) return SHORTCUT_INVALID; |
| 592 | |
| 593 | // FIXME: quick search, need to be customized by setting |
| 594 | if (url.length() > 2 && url.charAt(1) == ' ') { |
| 595 | switch (url.charAt(0)) { |
| 596 | case 'g': return SHORTCUT_GOOGLE_SEARCH; |
| 597 | case 'w': return SHORTCUT_WIKIPEDIA_SEARCH; |
| 598 | case 'd': return SHORTCUT_DICTIONARY_SEARCH; |
| 599 | case 'l': return SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH; |
| 600 | } |
| 601 | } |
| 602 | return SHORTCUT_INVALID; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Launches the default web search activity with the query parameters if the given intent's data |
| 607 | * are identified as plain search terms and not URLs/shortcuts. |
| 608 | * @return true if the intent was handled and web search activity was launched, false if not. |
| 609 | */ |
| 610 | private boolean handleWebSearchIntent(Intent intent) { |
| 611 | if (intent == null) return false; |
| 612 | |
| 613 | String url = null; |
| 614 | final String action = intent.getAction(); |
| 615 | if (Intent.ACTION_VIEW.equals(action)) { |
| 616 | url = intent.getData().toString(); |
| 617 | } else if (Intent.ACTION_SEARCH.equals(action) |
| 618 | || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action) |
| 619 | || Intent.ACTION_WEB_SEARCH.equals(action)) { |
| 620 | url = intent.getStringExtra(SearchManager.QUERY); |
| 621 | } |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 622 | return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA), |
| 623 | intent.getStringExtra(SearchManager.EXTRA_DATA_KEY)); |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Launches the default web search activity with the query parameters if the given url string |
| 628 | * was identified as plain search terms and not URL/shortcut. |
| 629 | * @return true if the request was handled and web search activity was launched, false if not. |
| 630 | */ |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 631 | private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) { |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 632 | if (inUrl == null) return false; |
| 633 | |
| 634 | // In general, we shouldn't modify URL from Intent. |
| 635 | // But currently, we get the user-typed URL from search box as well. |
| 636 | String url = fixUrl(inUrl).trim(); |
| 637 | |
| 638 | // URLs and site specific search shortcuts are handled by the regular flow of control, so |
| 639 | // return early. |
| 640 | if (Regex.WEB_URL_PATTERN.matcher(url).matches() |
Satish Sampath | bc5b9f3 | 2009-06-04 18:21:40 +0100 | [diff] [blame] | 641 | || ACCEPTED_URI_SCHEMA.matcher(url).matches() |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 642 | || parseUrlShortcut(url) != SHORTCUT_INVALID) { |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | Browser.updateVisitedHistory(mResolver, url, false); |
| 647 | Browser.addSearchUrl(mResolver, url); |
| 648 | |
| 649 | Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); |
| 650 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 651 | intent.putExtra(SearchManager.QUERY, url); |
Satish Sampath | 15e9f2d | 2009-06-23 22:29:49 +0100 | [diff] [blame] | 652 | if (appData != null) { |
| 653 | intent.putExtra(SearchManager.APP_DATA, appData); |
| 654 | } |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 655 | if (extraData != null) { |
| 656 | intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData); |
| 657 | } |
Grace Kloba | cc63403 | 2009-07-28 15:58:19 -0700 | [diff] [blame] | 658 | intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName()); |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 659 | startActivity(intent); |
| 660 | |
| 661 | return true; |
| 662 | } |
| 663 | |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 664 | private UrlData getUrlDataFromIntent(Intent intent) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 665 | String url = null; |
| 666 | if (intent != null) { |
| 667 | final String action = intent.getAction(); |
| 668 | if (Intent.ACTION_VIEW.equals(action)) { |
| 669 | url = smartUrlFilter(intent.getData()); |
| 670 | if (url != null && url.startsWith("content:")) { |
| 671 | /* Append mimetype so webview knows how to display */ |
| 672 | String mimeType = intent.resolveType(getContentResolver()); |
| 673 | if (mimeType != null) { |
| 674 | url += "?" + mimeType; |
| 675 | } |
| 676 | } |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 677 | if ("inline:".equals(url)) { |
| 678 | return new InlinedUrlData( |
| 679 | intent.getStringExtra(Browser.EXTRA_INLINE_CONTENT), |
| 680 | intent.getType(), |
| 681 | intent.getStringExtra(Browser.EXTRA_INLINE_ENCODING), |
| 682 | intent.getStringExtra(Browser.EXTRA_INLINE_FAILURL)); |
| 683 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 684 | } else if (Intent.ACTION_SEARCH.equals(action) |
| 685 | || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action) |
| 686 | || Intent.ACTION_WEB_SEARCH.equals(action)) { |
| 687 | url = intent.getStringExtra(SearchManager.QUERY); |
| 688 | if (url != null) { |
| 689 | mLastEnteredUrl = url; |
| 690 | // Don't add Urls, just search terms. |
| 691 | // Urls will get added when the page is loaded. |
| 692 | if (!Regex.WEB_URL_PATTERN.matcher(url).matches()) { |
| 693 | Browser.updateVisitedHistory(mResolver, url, false); |
| 694 | } |
| 695 | // In general, we shouldn't modify URL from Intent. |
| 696 | // But currently, we get the user-typed URL from search box as well. |
| 697 | url = fixUrl(url); |
| 698 | url = smartUrlFilter(url); |
| 699 | String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&"; |
| 700 | if (url.contains(searchSource)) { |
| 701 | String source = null; |
| 702 | final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA); |
| 703 | if (appData != null) { |
| 704 | source = appData.getString(SearchManager.SOURCE); |
| 705 | } |
| 706 | if (TextUtils.isEmpty(source)) { |
| 707 | source = GOOGLE_SEARCH_SOURCE_UNKNOWN; |
| 708 | } |
| 709 | url = url.replace(searchSource, "&source=android-"+source+"&"); |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | } |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 714 | return new UrlData(url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /* package */ static String fixUrl(String inUrl) { |
Cary Clark | 652ff87 | 2009-09-10 13:34:44 -0400 | [diff] [blame] | 718 | // FIXME: Converting the url to lower case |
| 719 | // duplicates functionality in smartUrlFilter(). |
| 720 | // However, changing all current callers of fixUrl to |
| 721 | // call smartUrlFilter in addition may have unwanted |
| 722 | // consequences, and is deferred for now. |
| 723 | int colon = inUrl.indexOf(':'); |
| 724 | boolean allLower = true; |
| 725 | for (int index = 0; index < colon; index++) { |
| 726 | char ch = inUrl.charAt(index); |
| 727 | if (!Character.isLetter(ch)) { |
| 728 | break; |
| 729 | } |
| 730 | allLower &= Character.isLowerCase(ch); |
| 731 | if (index == colon - 1 && !allLower) { |
| 732 | inUrl = inUrl.substring(0, colon).toLowerCase() |
| 733 | + inUrl.substring(colon); |
| 734 | } |
| 735 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 736 | if (inUrl.startsWith("http://") || inUrl.startsWith("https://")) |
| 737 | return inUrl; |
| 738 | if (inUrl.startsWith("http:") || |
| 739 | inUrl.startsWith("https:")) { |
| 740 | if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) { |
| 741 | inUrl = inUrl.replaceFirst("/", "//"); |
| 742 | } else inUrl = inUrl.replaceFirst(":", "://"); |
| 743 | } |
| 744 | return inUrl; |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Looking for the pattern like this |
| 749 | * |
| 750 | * * |
| 751 | * * * |
| 752 | * *** * ******* |
| 753 | * * * |
| 754 | * * * |
| 755 | * * |
| 756 | */ |
| 757 | private final SensorListener mSensorListener = new SensorListener() { |
| 758 | private long mLastGestureTime; |
| 759 | private float[] mPrev = new float[3]; |
| 760 | private float[] mPrevDiff = new float[3]; |
| 761 | private float[] mDiff = new float[3]; |
| 762 | private float[] mRevertDiff = new float[3]; |
| 763 | |
| 764 | public void onSensorChanged(int sensor, float[] values) { |
| 765 | boolean show = false; |
| 766 | float[] diff = new float[3]; |
| 767 | |
| 768 | for (int i = 0; i < 3; i++) { |
| 769 | diff[i] = values[i] - mPrev[i]; |
| 770 | if (Math.abs(diff[i]) > 1) { |
| 771 | show = true; |
| 772 | } |
| 773 | if ((diff[i] > 1.0 && mDiff[i] < 0.2) |
| 774 | || (diff[i] < -1.0 && mDiff[i] > -0.2)) { |
| 775 | // start track when there is a big move, or revert |
| 776 | mRevertDiff[i] = mDiff[i]; |
| 777 | mDiff[i] = 0; |
| 778 | } else if (diff[i] > -0.2 && diff[i] < 0.2) { |
| 779 | // reset when it is flat |
| 780 | mDiff[i] = mRevertDiff[i] = 0; |
| 781 | } |
| 782 | mDiff[i] += diff[i]; |
| 783 | mPrevDiff[i] = diff[i]; |
| 784 | mPrev[i] = values[i]; |
| 785 | } |
| 786 | |
| 787 | if (false) { |
| 788 | // only shows if we think the delta is big enough, in an attempt |
| 789 | // to detect "serious" moves left/right or up/down |
| 790 | Log.d("BrowserSensorHack", "sensorChanged " + sensor + " (" |
| 791 | + values[0] + ", " + values[1] + ", " + values[2] + ")" |
| 792 | + " diff(" + diff[0] + " " + diff[1] + " " + diff[2] |
| 793 | + ")"); |
| 794 | Log.d("BrowserSensorHack", " mDiff(" + mDiff[0] + " " |
| 795 | + mDiff[1] + " " + mDiff[2] + ")" + " mRevertDiff(" |
| 796 | + mRevertDiff[0] + " " + mRevertDiff[1] + " " |
| 797 | + mRevertDiff[2] + ")"); |
| 798 | } |
| 799 | |
| 800 | long now = android.os.SystemClock.uptimeMillis(); |
| 801 | if (now - mLastGestureTime > 1000) { |
| 802 | mLastGestureTime = 0; |
| 803 | |
| 804 | float y = mDiff[1]; |
| 805 | float z = mDiff[2]; |
| 806 | float ay = Math.abs(y); |
| 807 | float az = Math.abs(z); |
| 808 | float ry = mRevertDiff[1]; |
| 809 | float rz = mRevertDiff[2]; |
| 810 | float ary = Math.abs(ry); |
| 811 | float arz = Math.abs(rz); |
| 812 | boolean gestY = ay > 2.5f && ary > 1.0f && ay > ary; |
| 813 | boolean gestZ = az > 3.5f && arz > 1.0f && az > arz; |
| 814 | |
| 815 | if ((gestY || gestZ) && !(gestY && gestZ)) { |
| 816 | WebView view = mTabControl.getCurrentWebView(); |
| 817 | |
| 818 | if (view != null) { |
| 819 | if (gestZ) { |
| 820 | if (z < 0) { |
| 821 | view.zoomOut(); |
| 822 | } else { |
| 823 | view.zoomIn(); |
| 824 | } |
| 825 | } else { |
| 826 | view.flingScroll(0, Math.round(y * 100)); |
| 827 | } |
| 828 | } |
| 829 | mLastGestureTime = now; |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | public void onAccuracyChanged(int sensor, int accuracy) { |
| 835 | // TODO Auto-generated method stub |
| 836 | |
| 837 | } |
| 838 | }; |
| 839 | |
| 840 | @Override protected void onResume() { |
| 841 | super.onResume(); |
Dave Bort | 31a6d1c | 2009-04-13 15:56:49 -0700 | [diff] [blame] | 842 | if (LOGV_ENABLED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 843 | Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this); |
| 844 | } |
| 845 | |
| 846 | if (!mActivityInPause) { |
| 847 | Log.e(LOGTAG, "BrowserActivity is already resumed."); |
| 848 | return; |
| 849 | } |
| 850 | |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 851 | mTabControl.resumeCurrentTab(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 852 | mActivityInPause = false; |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 853 | resumeWebViewTimers(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 854 | |
| 855 | if (mWakeLock.isHeld()) { |
| 856 | mHandler.removeMessages(RELEASE_WAKELOCK); |
| 857 | mWakeLock.release(); |
| 858 | } |
| 859 | |
| 860 | if (mCredsDlg != null) { |
| 861 | if (!mHandler.hasMessages(CANCEL_CREDS_REQUEST)) { |
| 862 | // In case credential request never comes back |
| 863 | mHandler.sendEmptyMessageDelayed(CANCEL_CREDS_REQUEST, 6000); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | registerReceiver(mNetworkStateIntentReceiver, |
| 868 | mNetworkStateChangedFilter); |
| 869 | WebView.enablePlatformNotifications(); |
| 870 | |
| 871 | if (mSettings.doFlick()) { |
| 872 | if (mSensorManager == null) { |
| 873 | mSensorManager = (SensorManager) getSystemService( |
| 874 | Context.SENSOR_SERVICE); |
| 875 | } |
| 876 | mSensorManager.registerListener(mSensorListener, |
| 877 | SensorManager.SENSOR_ACCELEROMETER, |
| 878 | SensorManager.SENSOR_DELAY_FASTEST); |
| 879 | } else { |
| 880 | mSensorManager = null; |
| 881 | } |
| 882 | } |
| 883 | |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 884 | /** |
| 885 | * Since the actual title bar is embedded in the WebView, and removing it |
| 886 | * would change its appearance, create a temporary title bar to go at |
| 887 | * the top of the screen while the menu is open. |
| 888 | */ |
| 889 | private TitleBar mFakeTitleBar; |
| 890 | |
| 891 | /** |
Leon Scroggins | d8fd2fc | 2009-09-16 11:12:09 -0400 | [diff] [blame] | 892 | * Holder for the fake title bar. It will have a foreground shadow, as well |
| 893 | * as a white background, so the fake title bar looks like the real one. |
| 894 | */ |
| 895 | private ViewGroup mFakeTitleBarHolder; |
| 896 | |
| 897 | /** |
| 898 | * Layout parameters for the fake title bar within mFakeTitleBarHolder |
| 899 | */ |
| 900 | private FrameLayout.LayoutParams mFakeTitleBarParams |
| 901 | = new FrameLayout.LayoutParams( |
Leon Scroggins | c01e4a8 | 2009-09-16 14:41:00 -0400 | [diff] [blame] | 902 | ViewGroup.LayoutParams.FILL_PARENT, |
Leon Scroggins | d8fd2fc | 2009-09-16 11:12:09 -0400 | [diff] [blame] | 903 | ViewGroup.LayoutParams.WRAP_CONTENT); |
| 904 | /** |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 905 | * Keeps track of whether the options menu is open. This is important in |
| 906 | * determining whether to show or hide the title bar overlay. |
| 907 | */ |
| 908 | private boolean mOptionsMenuOpen; |
| 909 | |
| 910 | /** |
| 911 | * Only meaningful when mOptionsMenuOpen is true. This variable keeps track |
| 912 | * of whether the configuration has changed. The first onMenuOpened call |
| 913 | * after a configuration change is simply a reopening of the same menu |
| 914 | * (i.e. mIconView did not change). |
| 915 | */ |
| 916 | private boolean mConfigChanged; |
| 917 | |
| 918 | /** |
| 919 | * Whether or not the options menu is in its smaller, icon menu form. When |
| 920 | * true, we want the title bar overlay to be up. When false, we do not. |
| 921 | * Only meaningful if mOptionsMenuOpen is true. |
| 922 | */ |
| 923 | private boolean mIconView; |
| 924 | |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 925 | @Override |
| 926 | public boolean onMenuOpened(int featureId, Menu menu) { |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 927 | if (Window.FEATURE_OPTIONS_PANEL == featureId) { |
| 928 | if (mOptionsMenuOpen) { |
| 929 | if (mConfigChanged) { |
| 930 | // We do not need to make any changes to the state of the |
| 931 | // title bar, since the only thing that happened was a |
| 932 | // change in orientation |
| 933 | mConfigChanged = false; |
| 934 | } else { |
| 935 | if (mIconView) { |
| 936 | // Switching the menu to expanded view, so hide the |
| 937 | // title bar. |
| 938 | hideFakeTitleBar(); |
| 939 | mIconView = false; |
| 940 | } else { |
| 941 | // Switching the menu back to icon view, so show the |
| 942 | // title bar once again. |
| 943 | showFakeTitleBar(); |
| 944 | mIconView = true; |
| 945 | } |
| 946 | } |
| 947 | } else { |
| 948 | // The options menu is closed, so open it, and show the title |
| 949 | showFakeTitleBar(); |
| 950 | mOptionsMenuOpen = true; |
| 951 | mConfigChanged = false; |
| 952 | mIconView = true; |
| 953 | } |
| 954 | } |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 955 | return true; |
| 956 | } |
| 957 | |
Leon Scroggins | b94bf27 | 2009-09-25 15:22:08 -0400 | [diff] [blame] | 958 | /** |
| 959 | * Special class used exclusively for the shadow drawn underneath the fake |
| 960 | * title bar. The shadow does not need to be drawn if the WebView |
| 961 | * underneath is scrolled to the top, because it will draw directly on top |
| 962 | * of the embedded shadow. |
| 963 | */ |
| 964 | private static class Shadow extends View { |
| 965 | private WebView mWebView; |
| 966 | |
| 967 | public Shadow(Context context, AttributeSet attrs) { |
| 968 | super(context, attrs); |
| 969 | } |
| 970 | |
| 971 | public void setWebView(WebView view) { |
| 972 | mWebView = view; |
| 973 | } |
| 974 | |
| 975 | @Override |
| 976 | public void draw(Canvas canvas) { |
| 977 | // In general onDraw is the method to override, but we care about |
| 978 | // whether or not the background gets drawn, which happens in draw() |
| 979 | if (mWebView == null || mWebView.getScrollY() > getHeight()) { |
| 980 | super.draw(canvas); |
| 981 | } |
| 982 | // Need to invalidate so that if the scroll position changes, we |
| 983 | // still draw as appropriate. |
| 984 | invalidate(); |
| 985 | } |
| 986 | } |
| 987 | |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 988 | private void showFakeTitleBar() { |
Patrick Scott | f8de8ec | 2009-09-21 16:37:29 -0400 | [diff] [blame] | 989 | final View decor = getWindow().peekDecorView(); |
Leon Scroggins | 4d7e406 | 2009-09-15 15:49:45 -0400 | [diff] [blame] | 990 | if (mFakeTitleBar == null && mActiveTabsPage == null |
Patrick Scott | f8de8ec | 2009-09-21 16:37:29 -0400 | [diff] [blame] | 991 | && !mActivityInPause && decor != null |
| 992 | && decor.getWindowToken() != null) { |
Cary Clark | a046455 | 2009-09-29 13:00:45 -0400 | [diff] [blame] | 993 | Rect visRect = new Rect(); |
| 994 | if (!mBrowserFrameLayout.getGlobalVisibleRect(visRect)) { |
| 995 | if (LOGD_ENABLED) { |
| 996 | Log.d(LOGTAG, "showFakeTitleBar visRect failed"); |
| 997 | } |
| 998 | return; |
| 999 | } |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 1000 | final WebView webView = getTopWindow(); |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 1001 | mFakeTitleBar = new TitleBar(this); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 1002 | mFakeTitleBar.setTitleAndUrl(null, webView.getUrl()); |
| 1003 | mFakeTitleBar.setProgress(webView.getProgress()); |
| 1004 | mFakeTitleBar.setFavicon(webView.getFavicon()); |
| 1005 | updateLockIconToLatest(); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 1006 | |
| 1007 | WindowManager manager |
| 1008 | = (WindowManager) getSystemService(Context.WINDOW_SERVICE); |
| 1009 | |
| 1010 | // Add the title bar to the window manager so it can receive touches |
| 1011 | // while the menu is up |
| 1012 | WindowManager.LayoutParams params |
| 1013 | = new WindowManager.LayoutParams( |
| 1014 | ViewGroup.LayoutParams.FILL_PARENT, |
| 1015 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 1016 | WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL, |
| 1017 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, |
Leon Scroggins | 6854986 | 2009-09-21 16:02:01 -0400 | [diff] [blame] | 1018 | PixelFormat.TRANSLUCENT); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 1019 | params.gravity = Gravity.TOP; |
Leon Scroggins | a27ff19 | 2009-09-14 12:58:04 -0400 | [diff] [blame] | 1020 | WebView mainView = mTabControl.getCurrentWebView(); |
Leon Scroggins | 6854986 | 2009-09-21 16:02:01 -0400 | [diff] [blame] | 1021 | boolean atTop = mainView != null && mainView.getScrollY() == 0; |
| 1022 | params.windowAnimations = atTop ? 0 |
| 1023 | : com.android.internal.R.style.Animation_DropDownDown; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 1024 | // XXX : Without providing an offset, the fake title bar will be |
| 1025 | // placed underneath the status bar. Use the global visible rect |
| 1026 | // of mBrowserFrameLayout to determine the bottom of the status bar |
Cary Clark | a046455 | 2009-09-29 13:00:45 -0400 | [diff] [blame] | 1027 | params.y = visRect.top; |
Leon Scroggins | 6854986 | 2009-09-21 16:02:01 -0400 | [diff] [blame] | 1028 | // Add a holder for the title bar. It also holds a shadow to show |
| 1029 | // below the title bar. |
Leon Scroggins | d8fd2fc | 2009-09-16 11:12:09 -0400 | [diff] [blame] | 1030 | if (mFakeTitleBarHolder == null) { |
| 1031 | mFakeTitleBarHolder = (ViewGroup) LayoutInflater.from(this) |
| 1032 | .inflate(R.layout.title_bar_bg, null); |
| 1033 | } |
Leon Scroggins | b94bf27 | 2009-09-25 15:22:08 -0400 | [diff] [blame] | 1034 | Shadow shadow = (Shadow) mFakeTitleBarHolder.findViewById( |
| 1035 | R.id.shadow); |
| 1036 | shadow.setWebView(mainView); |
Leon Scroggins | 6854986 | 2009-09-21 16:02:01 -0400 | [diff] [blame] | 1037 | mFakeTitleBarHolder.addView(mFakeTitleBar, 0, mFakeTitleBarParams); |
Leon Scroggins | d8fd2fc | 2009-09-16 11:12:09 -0400 | [diff] [blame] | 1038 | manager.addView(mFakeTitleBarHolder, params); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | @Override |
| 1043 | public void onOptionsMenuClosed(Menu menu) { |
| 1044 | mOptionsMenuOpen = false; |
Leon Scroggins | a27ff19 | 2009-09-14 12:58:04 -0400 | [diff] [blame] | 1045 | if (!mInLoad) { |
| 1046 | hideFakeTitleBar(); |
| 1047 | } else if (!mIconView) { |
| 1048 | // The page is currently loading, and we are in expanded mode, so |
| 1049 | // we were not showing the menu. Show it once again. It will be |
| 1050 | // removed when the page finishes. |
| 1051 | showFakeTitleBar(); |
| 1052 | } |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 1053 | } |
| 1054 | private void hideFakeTitleBar() { |
| 1055 | if (mFakeTitleBar == null) return; |
Leon Scroggins | 2032957 | 2009-09-23 17:42:41 -0400 | [diff] [blame] | 1056 | WindowManager.LayoutParams params = (WindowManager.LayoutParams) |
| 1057 | mFakeTitleBarHolder.getLayoutParams(); |
| 1058 | WebView mainView = mTabControl.getCurrentWebView(); |
| 1059 | // Although we decided whether or not to animate based on the current |
| 1060 | // scroll position, the scroll position may have changed since the |
| 1061 | // fake title bar was displayed. Make sure it has the appropriate |
| 1062 | // animation/lack thereof before removing. |
| 1063 | params.windowAnimations = mainView != null && mainView.getScrollY() == 0 |
| 1064 | ? 0 : com.android.internal.R.style.Animation_DropDownDown; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 1065 | WindowManager manager |
| 1066 | = (WindowManager) getSystemService(Context.WINDOW_SERVICE); |
Leon Scroggins | 2032957 | 2009-09-23 17:42:41 -0400 | [diff] [blame] | 1067 | manager.updateViewLayout(mFakeTitleBarHolder, params); |
Leon Scroggins | d8fd2fc | 2009-09-16 11:12:09 -0400 | [diff] [blame] | 1068 | mFakeTitleBarHolder.removeView(mFakeTitleBar); |
| 1069 | manager.removeView(mFakeTitleBarHolder); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 1070 | mFakeTitleBar = null; |
| 1071 | } |
| 1072 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1073 | /** |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 1074 | * Special method for the fake title bar to call when displaying its context |
| 1075 | * menu, since it is in its own Window, and its parent does not show a |
| 1076 | * context menu. |
| 1077 | */ |
| 1078 | /* package */ void showTitleBarContextMenu() { |
Cary Clark | 65f4a3c | 2009-09-28 17:05:06 -0400 | [diff] [blame] | 1079 | if (null == mTitleBar.getParent()) { |
| 1080 | return; |
| 1081 | } |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 1082 | openContextMenu(mTitleBar); |
| 1083 | } |
| 1084 | |
| 1085 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1086 | * onSaveInstanceState(Bundle map) |
| 1087 | * onSaveInstanceState is called right before onStop(). The map contains |
| 1088 | * the saved state. |
| 1089 | */ |
| 1090 | @Override protected void onSaveInstanceState(Bundle outState) { |
Dave Bort | 31a6d1c | 2009-04-13 15:56:49 -0700 | [diff] [blame] | 1091 | if (LOGV_ENABLED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1092 | Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this); |
| 1093 | } |
| 1094 | // the default implementation requires each view to have an id. As the |
| 1095 | // browser handles the state itself and it doesn't use id for the views, |
| 1096 | // don't call the default implementation. Otherwise it will trigger the |
| 1097 | // warning like this, "couldn't save which view has focus because the |
| 1098 | // focused view XXX has no id". |
| 1099 | |
| 1100 | // Save all the tabs |
| 1101 | mTabControl.saveState(outState); |
| 1102 | } |
| 1103 | |
| 1104 | @Override protected void onPause() { |
| 1105 | super.onPause(); |
| 1106 | |
| 1107 | if (mActivityInPause) { |
| 1108 | Log.e(LOGTAG, "BrowserActivity is already paused."); |
| 1109 | return; |
| 1110 | } |
| 1111 | |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 1112 | mTabControl.pauseCurrentTab(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1113 | mActivityInPause = true; |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 1114 | if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1115 | mWakeLock.acquire(); |
| 1116 | mHandler.sendMessageDelayed(mHandler |
| 1117 | .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT); |
| 1118 | } |
| 1119 | |
| 1120 | // Clear the credentials toast if it is up |
| 1121 | if (mCredsDlg != null && mCredsDlg.isShowing()) { |
| 1122 | mCredsDlg.dismiss(); |
| 1123 | } |
| 1124 | mCredsDlg = null; |
| 1125 | |
Leon Scroggins | a2ab6a7 | 2009-09-11 11:49:52 -0400 | [diff] [blame] | 1126 | // FIXME: This removes the active tabs page and resets the menu to |
| 1127 | // MAIN_MENU. A better solution might be to do this work in onNewIntent |
| 1128 | // but then we would need to save it in onSaveInstanceState and restore |
| 1129 | // it in onCreate/onRestoreInstanceState |
| 1130 | if (mActiveTabsPage != null) { |
| 1131 | removeActiveTabPage(true); |
| 1132 | } |
| 1133 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1134 | cancelStopToast(); |
| 1135 | |
| 1136 | // unregister network state listener |
| 1137 | unregisterReceiver(mNetworkStateIntentReceiver); |
| 1138 | WebView.disablePlatformNotifications(); |
| 1139 | |
| 1140 | if (mSensorManager != null) { |
| 1141 | mSensorManager.unregisterListener(mSensorListener); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | @Override protected void onDestroy() { |
Dave Bort | 31a6d1c | 2009-04-13 15:56:49 -0700 | [diff] [blame] | 1146 | if (LOGV_ENABLED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1147 | Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this); |
| 1148 | } |
| 1149 | super.onDestroy(); |
Grace Kloba | 0923d69 | 2009-09-23 21:37:25 -0700 | [diff] [blame] | 1150 | |
| 1151 | if (mTabControl == null) return; |
| 1152 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1153 | // Remove the current tab and sub window |
| 1154 | TabControl.Tab t = mTabControl.getCurrentTab(); |
Patrick Scott | fb5e77f | 2009-04-08 19:17:37 -0700 | [diff] [blame] | 1155 | if (t != null) { |
| 1156 | dismissSubWindow(t); |
| 1157 | removeTabFromContentView(t); |
| 1158 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1159 | // Destroy all the tabs |
| 1160 | mTabControl.destroy(); |
| 1161 | WebIconDatabase.getInstance().close(); |
| 1162 | if (mGlsConnection != null) { |
| 1163 | unbindService(mGlsConnection); |
| 1164 | mGlsConnection = null; |
| 1165 | } |
| 1166 | |
| 1167 | // |
| 1168 | // stop MASF proxy service |
| 1169 | // |
| 1170 | //Intent proxyServiceIntent = new Intent(); |
| 1171 | //proxyServiceIntent.setComponent |
| 1172 | // (new ComponentName( |
| 1173 | // "com.android.masfproxyservice", |
| 1174 | // "com.android.masfproxyservice.MasfProxyService")); |
| 1175 | //stopService(proxyServiceIntent); |
Grace Kloba | b4da0ad | 2009-05-14 14:45:40 -0700 | [diff] [blame] | 1176 | |
| 1177 | unregisterReceiver(mPackageInstallationReceiver); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | @Override |
| 1181 | public void onConfigurationChanged(Configuration newConfig) { |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 1182 | mConfigChanged = true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1183 | super.onConfigurationChanged(newConfig); |
| 1184 | |
| 1185 | if (mPageInfoDialog != null) { |
| 1186 | mPageInfoDialog.dismiss(); |
| 1187 | showPageInfo( |
| 1188 | mPageInfoView, |
| 1189 | mPageInfoFromShowSSLCertificateOnError.booleanValue()); |
| 1190 | } |
| 1191 | if (mSSLCertificateDialog != null) { |
| 1192 | mSSLCertificateDialog.dismiss(); |
| 1193 | showSSLCertificate( |
| 1194 | mSSLCertificateView); |
| 1195 | } |
| 1196 | if (mSSLCertificateOnErrorDialog != null) { |
| 1197 | mSSLCertificateOnErrorDialog.dismiss(); |
| 1198 | showSSLCertificateOnError( |
| 1199 | mSSLCertificateOnErrorView, |
| 1200 | mSSLCertificateOnErrorHandler, |
| 1201 | mSSLCertificateOnErrorError); |
| 1202 | } |
| 1203 | if (mHttpAuthenticationDialog != null) { |
| 1204 | String title = ((TextView) mHttpAuthenticationDialog |
| 1205 | .findViewById(com.android.internal.R.id.alertTitle)).getText() |
| 1206 | .toString(); |
| 1207 | String name = ((TextView) mHttpAuthenticationDialog |
| 1208 | .findViewById(R.id.username_edit)).getText().toString(); |
| 1209 | String password = ((TextView) mHttpAuthenticationDialog |
| 1210 | .findViewById(R.id.password_edit)).getText().toString(); |
| 1211 | int focusId = mHttpAuthenticationDialog.getCurrentFocus() |
| 1212 | .getId(); |
| 1213 | mHttpAuthenticationDialog.dismiss(); |
| 1214 | showHttpAuthentication(mHttpAuthHandler, null, null, title, |
| 1215 | name, password, focusId); |
| 1216 | } |
| 1217 | if (mFindDialog != null && mFindDialog.isShowing()) { |
| 1218 | mFindDialog.onConfigurationChanged(newConfig); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | @Override public void onLowMemory() { |
| 1223 | super.onLowMemory(); |
| 1224 | mTabControl.freeMemory(); |
| 1225 | } |
| 1226 | |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 1227 | private boolean resumeWebViewTimers() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1228 | if ((!mActivityInPause && !mPageStarted) || |
| 1229 | (mActivityInPause && mPageStarted)) { |
| 1230 | CookieSyncManager.getInstance().startSync(); |
| 1231 | WebView w = mTabControl.getCurrentWebView(); |
| 1232 | if (w != null) { |
| 1233 | w.resumeTimers(); |
| 1234 | } |
| 1235 | return true; |
| 1236 | } else { |
| 1237 | return false; |
| 1238 | } |
| 1239 | } |
| 1240 | |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 1241 | private boolean pauseWebViewTimers() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1242 | if (mActivityInPause && !mPageStarted) { |
| 1243 | CookieSyncManager.getInstance().stopSync(); |
| 1244 | WebView w = mTabControl.getCurrentWebView(); |
| 1245 | if (w != null) { |
| 1246 | w.pauseTimers(); |
| 1247 | } |
| 1248 | return true; |
| 1249 | } else { |
| 1250 | return false; |
| 1251 | } |
| 1252 | } |
| 1253 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1254 | // FIXME: Do we want to call this when loading google for the first time? |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1255 | /* |
| 1256 | * This function is called when we are launching for the first time. We |
| 1257 | * are waiting for the login credentials before loading Google home |
| 1258 | * pages. This way the user will be logged in straight away. |
| 1259 | */ |
| 1260 | private void waitForCredentials() { |
| 1261 | // Show a toast |
| 1262 | mCredsDlg = new ProgressDialog(this); |
| 1263 | mCredsDlg.setIndeterminate(true); |
| 1264 | mCredsDlg.setMessage(getText(R.string.retrieving_creds_dlg_msg)); |
| 1265 | // If the user cancels the operation, then cancel the Google |
| 1266 | // Credentials request. |
| 1267 | mCredsDlg.setCancelMessage(mHandler.obtainMessage(CANCEL_CREDS_REQUEST)); |
| 1268 | mCredsDlg.show(); |
| 1269 | |
| 1270 | // We set a timeout for the retrieval of credentials in onResume() |
| 1271 | // as that is when we have freed up some CPU time to get |
| 1272 | // the login credentials. |
| 1273 | } |
| 1274 | |
| 1275 | /* |
| 1276 | * If we have received the credentials or we have timed out and we are |
| 1277 | * showing the credentials dialog, then it is time to move on. |
| 1278 | */ |
| 1279 | private void resumeAfterCredentials() { |
| 1280 | if (mCredsDlg == null) { |
| 1281 | return; |
| 1282 | } |
| 1283 | |
| 1284 | // Clear the toast |
| 1285 | if (mCredsDlg.isShowing()) { |
| 1286 | mCredsDlg.dismiss(); |
| 1287 | } |
| 1288 | mCredsDlg = null; |
| 1289 | |
| 1290 | // Clear any pending timeout |
| 1291 | mHandler.removeMessages(CANCEL_CREDS_REQUEST); |
| 1292 | |
| 1293 | // Load the page |
| 1294 | WebView w = mTabControl.getCurrentWebView(); |
| 1295 | if (w != null) { |
| 1296 | w.loadUrl(mSettings.getHomePage()); |
| 1297 | } |
| 1298 | |
| 1299 | // Update the settings, need to do this last as it can take a moment |
| 1300 | // to persist the settings. In the mean time we could be loading |
| 1301 | // content. |
| 1302 | mSettings.setLoginInitialized(this); |
| 1303 | } |
| 1304 | |
| 1305 | // Open the icon database and retain all the icons for visited sites. |
| 1306 | private void retainIconsOnStartup() { |
| 1307 | final WebIconDatabase db = WebIconDatabase.getInstance(); |
| 1308 | db.open(getDir("icons", 0).getPath()); |
| 1309 | try { |
| 1310 | Cursor c = Browser.getAllBookmarks(mResolver); |
| 1311 | if (!c.moveToFirst()) { |
| 1312 | c.deactivate(); |
| 1313 | return; |
| 1314 | } |
| 1315 | int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL); |
| 1316 | do { |
| 1317 | String url = c.getString(urlIndex); |
| 1318 | db.retainIconForPageUrl(url); |
| 1319 | } while (c.moveToNext()); |
| 1320 | c.deactivate(); |
| 1321 | } catch (IllegalStateException e) { |
| 1322 | Log.e(LOGTAG, "retainIconsOnStartup", e); |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | // Helper method for getting the top window. |
| 1327 | WebView getTopWindow() { |
| 1328 | return mTabControl.getCurrentTopWebView(); |
| 1329 | } |
| 1330 | |
| 1331 | @Override |
| 1332 | public boolean onCreateOptionsMenu(Menu menu) { |
| 1333 | super.onCreateOptionsMenu(menu); |
| 1334 | |
| 1335 | MenuInflater inflater = getMenuInflater(); |
| 1336 | inflater.inflate(R.menu.browser, menu); |
| 1337 | mMenu = menu; |
| 1338 | updateInLoadMenuItems(); |
| 1339 | return true; |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * As the menu can be open when loading state changes |
| 1344 | * we must manually update the state of the stop/reload menu |
| 1345 | * item |
| 1346 | */ |
| 1347 | private void updateInLoadMenuItems() { |
| 1348 | if (mMenu == null) { |
| 1349 | return; |
| 1350 | } |
| 1351 | MenuItem src = mInLoad ? |
| 1352 | mMenu.findItem(R.id.stop_menu_id): |
| 1353 | mMenu.findItem(R.id.reload_menu_id); |
| 1354 | MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id); |
| 1355 | dest.setIcon(src.getIcon()); |
| 1356 | dest.setTitle(src.getTitle()); |
| 1357 | } |
| 1358 | |
| 1359 | @Override |
| 1360 | public boolean onContextItemSelected(MenuItem item) { |
| 1361 | // chording is not an issue with context menus, but we use the same |
| 1362 | // options selector, so set mCanChord to true so we can access them. |
| 1363 | mCanChord = true; |
| 1364 | int id = item.getItemId(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1365 | switch (id) { |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 1366 | // For the context menu from the title bar |
| 1367 | case R.id.title_bar_share_page_url: |
| 1368 | case R.id.title_bar_copy_page_url: |
| 1369 | WebView mainView = mTabControl.getCurrentWebView(); |
| 1370 | if (null == mainView) { |
| 1371 | return false; |
| 1372 | } |
| 1373 | if (id == R.id.title_bar_share_page_url) { |
| 1374 | Browser.sendString(this, mainView.getUrl()); |
| 1375 | } else { |
| 1376 | copy(mainView.getUrl()); |
| 1377 | } |
| 1378 | break; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1379 | // -- Browser context menu |
| 1380 | case R.id.open_context_menu_id: |
| 1381 | case R.id.open_newtab_context_menu_id: |
| 1382 | case R.id.bookmark_context_menu_id: |
| 1383 | case R.id.save_link_context_menu_id: |
| 1384 | case R.id.share_link_context_menu_id: |
| 1385 | case R.id.copy_link_context_menu_id: |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 1386 | final WebView webView = getTopWindow(); |
| 1387 | if (null == webView) { |
| 1388 | return false; |
| 1389 | } |
| 1390 | final HashMap hrefMap = new HashMap(); |
| 1391 | hrefMap.put("webview", webView); |
| 1392 | final Message msg = mHandler.obtainMessage( |
| 1393 | FOCUS_NODE_HREF, id, 0, hrefMap); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1394 | webView.requestFocusNodeHref(msg); |
| 1395 | break; |
| 1396 | |
| 1397 | default: |
| 1398 | // For other context menus |
| 1399 | return onOptionsItemSelected(item); |
| 1400 | } |
| 1401 | mCanChord = false; |
| 1402 | return true; |
| 1403 | } |
| 1404 | |
| 1405 | private Bundle createGoogleSearchSourceBundle(String source) { |
| 1406 | Bundle bundle = new Bundle(); |
| 1407 | bundle.putString(SearchManager.SOURCE, source); |
| 1408 | return bundle; |
| 1409 | } |
| 1410 | |
| 1411 | /** |
The Android Open Source Project | 4e5f587 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 1412 | * Overriding this to insert a local information bundle |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1413 | */ |
| 1414 | @Override |
| 1415 | public boolean onSearchRequested() { |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 1416 | if (mOptionsMenuOpen) closeOptionsMenu(); |
Leon Scroggins | 5bbe980 | 2009-07-31 13:10:55 -0400 | [diff] [blame] | 1417 | String url = (getTopWindow() == null) ? null : getTopWindow().getUrl(); |
Grace Kloba | 83f4734 | 2009-07-20 10:44:31 -0700 | [diff] [blame] | 1418 | startSearch(mSettings.getHomePage().equals(url) ? null : url, true, |
The Android Open Source Project | 4e5f587 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 1419 | createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_SEARCHKEY), false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1420 | return true; |
| 1421 | } |
| 1422 | |
| 1423 | @Override |
| 1424 | public void startSearch(String initialQuery, boolean selectInitialQuery, |
| 1425 | Bundle appSearchData, boolean globalSearch) { |
| 1426 | if (appSearchData == null) { |
| 1427 | appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE); |
| 1428 | } |
| 1429 | super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); |
| 1430 | } |
| 1431 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1432 | /** |
| 1433 | * Switch tabs. Called by the TitleBarSet when sliding the title bar |
| 1434 | * results in changing tabs. |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 1435 | * @param index Index of the tab to change to, as defined by |
| 1436 | * mTabControl.getTabIndex(Tab t). |
| 1437 | * @return boolean True if we successfully switched to a different tab. If |
| 1438 | * the indexth tab is null, or if that tab is the same as |
| 1439 | * the current one, return false. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1440 | */ |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 1441 | /* package */ boolean switchToTab(int index) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1442 | TabControl.Tab tab = mTabControl.getTab(index); |
| 1443 | TabControl.Tab currentTab = mTabControl.getCurrentTab(); |
| 1444 | if (tab == null || tab == currentTab) { |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 1445 | return false; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1446 | } |
| 1447 | if (currentTab != null) { |
| 1448 | // currentTab may be null if it was just removed. In that case, |
| 1449 | // we do not need to remove it |
| 1450 | removeTabFromContentView(currentTab); |
| 1451 | } |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1452 | mTabControl.setCurrentTab(tab); |
| 1453 | attachTabToContentView(tab); |
Grace Kloba | eb6eef4 | 2009-09-15 17:56:32 -0700 | [diff] [blame] | 1454 | resetTitleIconAndProgress(); |
| 1455 | updateLockIconToLatest(); |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 1456 | return true; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1457 | } |
| 1458 | |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 1459 | /* package */ TabControl.Tab openTabToHomePage() { |
| 1460 | return openTabAndShow(mSettings.getHomePage(), false, null); |
| 1461 | } |
| 1462 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1463 | /* package */ void closeCurrentWindow() { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1464 | final TabControl.Tab current = mTabControl.getCurrentTab(); |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 1465 | if (mTabControl.getTabCount() == 1) { |
Leon Scroggins | 3044423 | 2009-09-04 18:36:20 -0400 | [diff] [blame] | 1466 | // This is the last tab. Open a new one, with the home |
| 1467 | // page and close the current one. |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 1468 | TabControl.Tab newTab = openTabToHomePage(); |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 1469 | closeTab(current); |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 1470 | return; |
| 1471 | } |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1472 | final TabControl.Tab parent = current.getParentTab(); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1473 | int indexToShow = -1; |
| 1474 | if (parent != null) { |
| 1475 | indexToShow = mTabControl.getTabIndex(parent); |
| 1476 | } else { |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 1477 | final int currentIndex = mTabControl.getCurrentIndex(); |
| 1478 | // Try to move to the tab to the right |
| 1479 | indexToShow = currentIndex + 1; |
| 1480 | if (indexToShow > mTabControl.getTabCount() - 1) { |
| 1481 | // Try to move to the tab to the left |
| 1482 | indexToShow = currentIndex - 1; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1483 | } |
| 1484 | } |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 1485 | if (switchToTab(indexToShow)) { |
| 1486 | // Close window |
| 1487 | closeTab(current); |
| 1488 | } |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1489 | } |
| 1490 | |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 1491 | private ActiveTabsPage mActiveTabsPage; |
| 1492 | |
| 1493 | /** |
| 1494 | * Remove the active tabs page. |
| 1495 | * @param needToAttach If true, the active tabs page did not attach a tab |
| 1496 | * to the content view, so we need to do that here. |
| 1497 | */ |
| 1498 | /* package */ void removeActiveTabPage(boolean needToAttach) { |
| 1499 | mContentView.removeView(mActiveTabsPage); |
| 1500 | mActiveTabsPage = null; |
| 1501 | mMenuState = R.id.MAIN_MENU; |
| 1502 | if (needToAttach) { |
| 1503 | attachTabToContentView(mTabControl.getCurrentTab()); |
| 1504 | } |
| 1505 | getTopWindow().requestFocus(); |
| 1506 | } |
| 1507 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1508 | @Override |
| 1509 | public boolean onOptionsItemSelected(MenuItem item) { |
| 1510 | if (!mCanChord) { |
| 1511 | // The user has already fired a shortcut with this hold down of the |
| 1512 | // menu key. |
| 1513 | return false; |
| 1514 | } |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1515 | if (null == getTopWindow()) { |
Leon Scroggins | 0d7ae0e | 2009-06-05 11:04:45 -0400 | [diff] [blame] | 1516 | return false; |
| 1517 | } |
Grace Kloba | 6ee9c49 | 2009-07-13 10:04:34 -0700 | [diff] [blame] | 1518 | if (mMenuIsDown) { |
| 1519 | // The shortcut action consumes the MENU. Even if it is still down, |
| 1520 | // it won't trigger the next shortcut action. In the case of the |
| 1521 | // shortcut action triggering a new activity, like Bookmarks, we |
| 1522 | // won't get onKeyUp for MENU. So it is important to reset it here. |
| 1523 | mMenuIsDown = false; |
| 1524 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1525 | switch (item.getItemId()) { |
| 1526 | // -- Main menu |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 1527 | case R.id.new_tab_menu_id: |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 1528 | openTabToHomePage(); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 1529 | break; |
| 1530 | |
Leon Scroggins | 64b80f3 | 2009-08-07 12:03:34 -0400 | [diff] [blame] | 1531 | case R.id.goto_menu_id: |
Leon Scroggins | b3a5bed | 2009-09-28 11:21:56 -0400 | [diff] [blame] | 1532 | onSearchRequested(); |
| 1533 | break; |
| 1534 | |
| 1535 | case R.id.bookmarks_menu_id: |
Leon Scroggins | 3044423 | 2009-09-04 18:36:20 -0400 | [diff] [blame] | 1536 | bookmarksOrHistoryPicker(false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1537 | break; |
| 1538 | |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 1539 | case R.id.active_tabs_menu_id: |
| 1540 | mActiveTabsPage = new ActiveTabsPage(this, mTabControl); |
| 1541 | removeTabFromContentView(mTabControl.getCurrentTab()); |
Leon Scroggins | 43de616 | 2009-09-14 19:59:58 -0400 | [diff] [blame] | 1542 | hideFakeTitleBar(); |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 1543 | mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS); |
| 1544 | mActiveTabsPage.requestFocus(); |
| 1545 | mMenuState = EMPTY_MENU; |
| 1546 | break; |
| 1547 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1548 | case R.id.add_bookmark_menu_id: |
| 1549 | Intent i = new Intent(BrowserActivity.this, |
| 1550 | AddBookmarkPage.class); |
| 1551 | WebView w = getTopWindow(); |
| 1552 | i.putExtra("url", w.getUrl()); |
| 1553 | i.putExtra("title", w.getTitle()); |
Grace Kloba | 83cdb2c | 2009-09-16 00:48:57 -0700 | [diff] [blame] | 1554 | i.putExtra("touch_icon_url", w.getTouchIconUrl()); |
Ben Murdoch | dcc2b6f | 2009-09-21 14:29:20 +0100 | [diff] [blame] | 1555 | i.putExtra("thumbnail", createScreenshot(w)); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1556 | startActivity(i); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1557 | break; |
| 1558 | |
| 1559 | case R.id.stop_reload_menu_id: |
| 1560 | if (mInLoad) { |
| 1561 | stopLoading(); |
| 1562 | } else { |
| 1563 | getTopWindow().reload(); |
| 1564 | } |
| 1565 | break; |
| 1566 | |
| 1567 | case R.id.back_menu_id: |
| 1568 | getTopWindow().goBack(); |
| 1569 | break; |
| 1570 | |
| 1571 | case R.id.forward_menu_id: |
| 1572 | getTopWindow().goForward(); |
| 1573 | break; |
| 1574 | |
| 1575 | case R.id.close_menu_id: |
| 1576 | // Close the subwindow if it exists. |
| 1577 | if (mTabControl.getCurrentSubWindow() != null) { |
| 1578 | dismissSubWindow(mTabControl.getCurrentTab()); |
| 1579 | break; |
| 1580 | } |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1581 | closeCurrentWindow(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1582 | break; |
| 1583 | |
| 1584 | case R.id.homepage_menu_id: |
| 1585 | TabControl.Tab current = mTabControl.getCurrentTab(); |
| 1586 | if (current != null) { |
| 1587 | dismissSubWindow(current); |
| 1588 | current.getWebView().loadUrl(mSettings.getHomePage()); |
| 1589 | } |
| 1590 | break; |
| 1591 | |
| 1592 | case R.id.preferences_menu_id: |
| 1593 | Intent intent = new Intent(this, |
| 1594 | BrowserPreferencesPage.class); |
| 1595 | startActivityForResult(intent, PREFERENCES_PAGE); |
| 1596 | break; |
| 1597 | |
| 1598 | case R.id.find_menu_id: |
| 1599 | if (null == mFindDialog) { |
| 1600 | mFindDialog = new FindDialog(this); |
| 1601 | } |
| 1602 | mFindDialog.setWebView(getTopWindow()); |
| 1603 | mFindDialog.show(); |
| 1604 | mMenuState = EMPTY_MENU; |
| 1605 | break; |
| 1606 | |
| 1607 | case R.id.select_text_id: |
| 1608 | getTopWindow().emulateShiftHeld(); |
| 1609 | break; |
| 1610 | case R.id.page_info_menu_id: |
| 1611 | showPageInfo(mTabControl.getCurrentTab(), false); |
| 1612 | break; |
| 1613 | |
| 1614 | case R.id.classic_history_menu_id: |
Leon Scroggins | 3044423 | 2009-09-04 18:36:20 -0400 | [diff] [blame] | 1615 | bookmarksOrHistoryPicker(true); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1616 | break; |
| 1617 | |
| 1618 | case R.id.share_page_menu_id: |
Andrei Popescu | 10fdba8 | 2009-09-24 13:25:47 +0100 | [diff] [blame] | 1619 | Browser.sendString(this, getTopWindow().getUrl(), |
| 1620 | getText(R.string.choosertitle_sharevia).toString()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1621 | break; |
| 1622 | |
| 1623 | case R.id.dump_nav_menu_id: |
| 1624 | getTopWindow().debugDump(); |
| 1625 | break; |
| 1626 | |
| 1627 | case R.id.zoom_in_menu_id: |
| 1628 | getTopWindow().zoomIn(); |
| 1629 | break; |
| 1630 | |
| 1631 | case R.id.zoom_out_menu_id: |
| 1632 | getTopWindow().zoomOut(); |
| 1633 | break; |
| 1634 | |
| 1635 | case R.id.view_downloads_menu_id: |
| 1636 | viewDownloads(null); |
| 1637 | break; |
| 1638 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1639 | case R.id.window_one_menu_id: |
| 1640 | case R.id.window_two_menu_id: |
| 1641 | case R.id.window_three_menu_id: |
| 1642 | case R.id.window_four_menu_id: |
| 1643 | case R.id.window_five_menu_id: |
| 1644 | case R.id.window_six_menu_id: |
| 1645 | case R.id.window_seven_menu_id: |
| 1646 | case R.id.window_eight_menu_id: |
| 1647 | { |
| 1648 | int menuid = item.getItemId(); |
| 1649 | for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) { |
| 1650 | if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) { |
| 1651 | TabControl.Tab desiredTab = mTabControl.getTab(id); |
| 1652 | if (desiredTab != null && |
| 1653 | desiredTab != mTabControl.getCurrentTab()) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1654 | switchToTab(id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1655 | } |
| 1656 | break; |
| 1657 | } |
| 1658 | } |
| 1659 | } |
| 1660 | break; |
| 1661 | |
| 1662 | default: |
| 1663 | if (!super.onOptionsItemSelected(item)) { |
| 1664 | return false; |
| 1665 | } |
| 1666 | // Otherwise fall through. |
| 1667 | } |
| 1668 | mCanChord = false; |
| 1669 | return true; |
| 1670 | } |
| 1671 | |
| 1672 | public void closeFind() { |
| 1673 | mMenuState = R.id.MAIN_MENU; |
| 1674 | } |
| 1675 | |
| 1676 | @Override public boolean onPrepareOptionsMenu(Menu menu) |
| 1677 | { |
| 1678 | // This happens when the user begins to hold down the menu key, so |
| 1679 | // allow them to chord to get a shortcut. |
| 1680 | mCanChord = true; |
| 1681 | // Note: setVisible will decide whether an item is visible; while |
| 1682 | // setEnabled() will decide whether an item is enabled, which also means |
| 1683 | // whether the matching shortcut key will function. |
| 1684 | super.onPrepareOptionsMenu(menu); |
| 1685 | switch (mMenuState) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1686 | case EMPTY_MENU: |
| 1687 | if (mCurrentMenuState != mMenuState) { |
| 1688 | menu.setGroupVisible(R.id.MAIN_MENU, false); |
| 1689 | menu.setGroupEnabled(R.id.MAIN_MENU, false); |
| 1690 | menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1691 | } |
| 1692 | break; |
| 1693 | default: |
| 1694 | if (mCurrentMenuState != mMenuState) { |
| 1695 | menu.setGroupVisible(R.id.MAIN_MENU, true); |
| 1696 | menu.setGroupEnabled(R.id.MAIN_MENU, true); |
| 1697 | menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1698 | } |
| 1699 | final WebView w = getTopWindow(); |
| 1700 | boolean canGoBack = false; |
| 1701 | boolean canGoForward = false; |
| 1702 | boolean isHome = false; |
| 1703 | if (w != null) { |
| 1704 | canGoBack = w.canGoBack(); |
| 1705 | canGoForward = w.canGoForward(); |
| 1706 | isHome = mSettings.getHomePage().equals(w.getUrl()); |
| 1707 | } |
| 1708 | final MenuItem back = menu.findItem(R.id.back_menu_id); |
| 1709 | back.setEnabled(canGoBack); |
| 1710 | |
| 1711 | final MenuItem home = menu.findItem(R.id.homepage_menu_id); |
| 1712 | home.setEnabled(!isHome); |
| 1713 | |
| 1714 | menu.findItem(R.id.forward_menu_id) |
| 1715 | .setEnabled(canGoForward); |
| 1716 | |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 1717 | menu.findItem(R.id.new_tab_menu_id).setEnabled( |
| 1718 | mTabControl.getTabCount() < TabControl.MAX_TABS); |
| 1719 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1720 | // decide whether to show the share link option |
| 1721 | PackageManager pm = getPackageManager(); |
| 1722 | Intent send = new Intent(Intent.ACTION_SEND); |
| 1723 | send.setType("text/plain"); |
| 1724 | ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY); |
| 1725 | menu.findItem(R.id.share_page_menu_id).setVisible(ri != null); |
| 1726 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1727 | boolean isNavDump = mSettings.isNavDump(); |
| 1728 | final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id); |
| 1729 | nav.setVisible(isNavDump); |
| 1730 | nav.setEnabled(isNavDump); |
| 1731 | break; |
| 1732 | } |
| 1733 | mCurrentMenuState = mMenuState; |
| 1734 | return true; |
| 1735 | } |
| 1736 | |
| 1737 | @Override |
| 1738 | public void onCreateContextMenu(ContextMenu menu, View v, |
| 1739 | ContextMenuInfo menuInfo) { |
| 1740 | WebView webview = (WebView) v; |
| 1741 | WebView.HitTestResult result = webview.getHitTestResult(); |
| 1742 | if (result == null) { |
| 1743 | return; |
| 1744 | } |
| 1745 | |
| 1746 | int type = result.getType(); |
| 1747 | if (type == WebView.HitTestResult.UNKNOWN_TYPE) { |
| 1748 | Log.w(LOGTAG, |
| 1749 | "We should not show context menu when nothing is touched"); |
| 1750 | return; |
| 1751 | } |
| 1752 | if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) { |
| 1753 | // let TextView handles context menu |
| 1754 | return; |
| 1755 | } |
| 1756 | |
| 1757 | // Note, http://b/issue?id=1106666 is requesting that |
| 1758 | // an inflated menu can be used again. This is not available |
| 1759 | // yet, so inflate each time (yuk!) |
| 1760 | MenuInflater inflater = getMenuInflater(); |
| 1761 | inflater.inflate(R.menu.browsercontext, menu); |
| 1762 | |
| 1763 | // Show the correct menu group |
| 1764 | String extra = result.getExtra(); |
| 1765 | menu.setGroupVisible(R.id.PHONE_MENU, |
| 1766 | type == WebView.HitTestResult.PHONE_TYPE); |
| 1767 | menu.setGroupVisible(R.id.EMAIL_MENU, |
| 1768 | type == WebView.HitTestResult.EMAIL_TYPE); |
| 1769 | menu.setGroupVisible(R.id.GEO_MENU, |
| 1770 | type == WebView.HitTestResult.GEO_TYPE); |
| 1771 | menu.setGroupVisible(R.id.IMAGE_MENU, |
| 1772 | type == WebView.HitTestResult.IMAGE_TYPE |
| 1773 | || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE); |
| 1774 | menu.setGroupVisible(R.id.ANCHOR_MENU, |
| 1775 | type == WebView.HitTestResult.SRC_ANCHOR_TYPE |
| 1776 | || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE); |
| 1777 | |
| 1778 | // Setup custom handling depending on the type |
| 1779 | switch (type) { |
| 1780 | case WebView.HitTestResult.PHONE_TYPE: |
| 1781 | menu.setHeaderTitle(Uri.decode(extra)); |
| 1782 | menu.findItem(R.id.dial_context_menu_id).setIntent( |
| 1783 | new Intent(Intent.ACTION_VIEW, Uri |
| 1784 | .parse(WebView.SCHEME_TEL + extra))); |
| 1785 | Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT); |
| 1786 | addIntent.putExtra(Insert.PHONE, Uri.decode(extra)); |
Cary Clark | 5e335a3 | 2009-09-22 14:53:11 -0400 | [diff] [blame] | 1787 | addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1788 | menu.findItem(R.id.add_contact_context_menu_id).setIntent( |
| 1789 | addIntent); |
| 1790 | menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener( |
| 1791 | new Copy(extra)); |
| 1792 | break; |
| 1793 | |
| 1794 | case WebView.HitTestResult.EMAIL_TYPE: |
| 1795 | menu.setHeaderTitle(extra); |
| 1796 | menu.findItem(R.id.email_context_menu_id).setIntent( |
| 1797 | new Intent(Intent.ACTION_VIEW, Uri |
| 1798 | .parse(WebView.SCHEME_MAILTO + extra))); |
| 1799 | menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener( |
| 1800 | new Copy(extra)); |
| 1801 | break; |
| 1802 | |
| 1803 | case WebView.HitTestResult.GEO_TYPE: |
| 1804 | menu.setHeaderTitle(extra); |
| 1805 | menu.findItem(R.id.map_context_menu_id).setIntent( |
| 1806 | new Intent(Intent.ACTION_VIEW, Uri |
| 1807 | .parse(WebView.SCHEME_GEO |
| 1808 | + URLEncoder.encode(extra)))); |
| 1809 | menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener( |
| 1810 | new Copy(extra)); |
| 1811 | break; |
| 1812 | |
| 1813 | case WebView.HitTestResult.SRC_ANCHOR_TYPE: |
| 1814 | case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE: |
| 1815 | TextView titleView = (TextView) LayoutInflater.from(this) |
| 1816 | .inflate(android.R.layout.browser_link_context_header, |
| 1817 | null); |
| 1818 | titleView.setText(extra); |
| 1819 | menu.setHeaderView(titleView); |
| 1820 | // decide whether to show the open link in new tab option |
| 1821 | menu.findItem(R.id.open_newtab_context_menu_id).setVisible( |
| 1822 | mTabControl.getTabCount() < TabControl.MAX_TABS); |
| 1823 | PackageManager pm = getPackageManager(); |
| 1824 | Intent send = new Intent(Intent.ACTION_SEND); |
| 1825 | send.setType("text/plain"); |
| 1826 | ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY); |
| 1827 | menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null); |
| 1828 | if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) { |
| 1829 | break; |
| 1830 | } |
| 1831 | // otherwise fall through to handle image part |
| 1832 | case WebView.HitTestResult.IMAGE_TYPE: |
| 1833 | if (type == WebView.HitTestResult.IMAGE_TYPE) { |
| 1834 | menu.setHeaderTitle(extra); |
| 1835 | } |
| 1836 | menu.findItem(R.id.view_image_context_menu_id).setIntent( |
| 1837 | new Intent(Intent.ACTION_VIEW, Uri.parse(extra))); |
| 1838 | menu.findItem(R.id.download_context_menu_id). |
| 1839 | setOnMenuItemClickListener(new Download(extra)); |
| 1840 | break; |
| 1841 | |
| 1842 | default: |
| 1843 | Log.w(LOGTAG, "We should not get here."); |
| 1844 | break; |
| 1845 | } |
| 1846 | } |
| 1847 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1848 | // Attach the given tab to the content view. |
Grace Kloba | c928c30 | 2009-09-17 11:51:21 -0700 | [diff] [blame] | 1849 | // this should only be called for the current tab. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1850 | private void attachTabToContentView(TabControl.Tab t) { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 1851 | // Attach the container that contains the main WebView and any other UI |
| 1852 | // associated with the tab. |
Patrick Scott | d011953 | 2009-09-17 08:00:31 -0400 | [diff] [blame] | 1853 | t.attachTabToContentView(mContentView); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 1854 | |
| 1855 | if (mShouldShowErrorConsole) { |
| 1856 | ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(true); |
| 1857 | if (errorConsole.numberOfErrors() == 0) { |
| 1858 | errorConsole.showConsole(ErrorConsoleView.SHOW_NONE); |
| 1859 | } else { |
| 1860 | errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED); |
| 1861 | } |
| 1862 | |
| 1863 | mErrorConsoleContainer.addView(errorConsole, |
| 1864 | new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, |
| 1865 | ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 1866 | } |
| 1867 | |
Grace Kloba | c928c30 | 2009-09-17 11:51:21 -0700 | [diff] [blame] | 1868 | setLockIconType(t.getLockIconType()); |
| 1869 | setPrevLockType(t.getPrevLockIconType()); |
| 1870 | |
| 1871 | // this is to match the code in removeTabFromContentView() |
| 1872 | if (!mPageStarted && t.getTopWindow().getProgress() < 100) { |
| 1873 | mPageStarted = true; |
Grace Kloba | eb6eef4 | 2009-09-15 17:56:32 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
Leon Scroggins | 39ab28e | 2009-09-02 21:20:30 -0400 | [diff] [blame] | 1876 | WebView view = t.getWebView(); |
Leon Scroggins | 55a5bc2 | 2009-09-04 17:00:08 -0400 | [diff] [blame] | 1877 | view.setEmbeddedTitleBar(mTitleBar); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1878 | // Request focus on the top window. |
| 1879 | t.getTopWindow().requestFocus(); |
| 1880 | } |
| 1881 | |
| 1882 | // Attach a sub window to the main WebView of the given tab. |
| 1883 | private void attachSubWindow(TabControl.Tab t) { |
Patrick Scott | d011953 | 2009-09-17 08:00:31 -0400 | [diff] [blame] | 1884 | t.attachSubWindow(mContentView); |
| 1885 | getTopWindow().requestFocus(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | // Remove the given tab from the content view. |
| 1889 | private void removeTabFromContentView(TabControl.Tab t) { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 1890 | // Remove the container that contains the main WebView. |
Patrick Scott | d011953 | 2009-09-17 08:00:31 -0400 | [diff] [blame] | 1891 | t.removeTabFromContentView(mContentView); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 1892 | |
| 1893 | if (mTabControl.getCurrentErrorConsole(false) != null) { |
| 1894 | mErrorConsoleContainer.removeView(mTabControl.getCurrentErrorConsole(false)); |
| 1895 | } |
| 1896 | |
Leon Scroggins | 39ab28e | 2009-09-02 21:20:30 -0400 | [diff] [blame] | 1897 | WebView view = t.getWebView(); |
Leon Scroggins | bb85b90 | 2009-09-14 19:27:20 -0400 | [diff] [blame] | 1898 | if (view != null) { |
| 1899 | view.setEmbeddedTitleBar(null); |
| 1900 | } |
Leon Scroggins | 39ab28e | 2009-09-02 21:20:30 -0400 | [diff] [blame] | 1901 | |
Grace Kloba | c928c30 | 2009-09-17 11:51:21 -0700 | [diff] [blame] | 1902 | // unlike attachTabToContentView(), removeTabFromContentView() can be |
| 1903 | // called for the non-current tab. Need to add the check. |
Grace Kloba | eb6eef4 | 2009-09-15 17:56:32 -0700 | [diff] [blame] | 1904 | if (t == mTabControl.getCurrentTab()) { |
| 1905 | t.setLockIconType(getLockIconType()); |
| 1906 | t.setPrevLockIconType(getPrevLockType()); |
Grace Kloba | c928c30 | 2009-09-17 11:51:21 -0700 | [diff] [blame] | 1907 | |
| 1908 | // this is not a perfect solution. But currently there is one |
| 1909 | // WebViewClient for all the WebView. if user switches from an |
| 1910 | // in-load window to an already loaded window, mPageStarted will not |
| 1911 | // be set to false. If user leaves the Browser, pauseWebViewTimers() |
| 1912 | // won't do anything and leaves the timer running even Browser is in |
| 1913 | // the background. |
| 1914 | if (mPageStarted) { |
| 1915 | mPageStarted = false; |
| 1916 | } |
Grace Kloba | eb6eef4 | 2009-09-15 17:56:32 -0700 | [diff] [blame] | 1917 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | // Remove the sub window if it exists. Also called by TabControl when the |
| 1921 | // user clicks the 'X' to dismiss a sub window. |
| 1922 | /* package */ void dismissSubWindow(TabControl.Tab t) { |
Patrick Scott | d011953 | 2009-09-17 08:00:31 -0400 | [diff] [blame] | 1923 | t.removeSubWindow(mContentView); |
| 1924 | // Tell the TabControl to dismiss the subwindow. This will destroy |
| 1925 | // the WebView. |
| 1926 | mTabControl.dismissSubWindow(t); |
| 1927 | getTopWindow().requestFocus(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1928 | } |
| 1929 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1930 | // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)} |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 1931 | // that accepts url as string. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1932 | private TabControl.Tab openTabAndShow(String url, boolean closeOnExit, |
| 1933 | String appId) { |
| 1934 | return openTabAndShow(new UrlData(url), closeOnExit, appId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | // This method does a ton of stuff. It will attempt to create a new tab |
| 1938 | // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1939 | // url isn't null, it will load the given url. |
| 1940 | /* package */ TabControl.Tab openTabAndShow(UrlData urlData, |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1941 | boolean closeOnExit, String appId) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1942 | final boolean newTab = mTabControl.getTabCount() != TabControl.MAX_TABS; |
| 1943 | final TabControl.Tab currentTab = mTabControl.getCurrentTab(); |
| 1944 | if (newTab) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1945 | final TabControl.Tab tab = mTabControl.createNewTab( |
| 1946 | closeOnExit, appId, urlData.mUrl); |
| 1947 | WebView webview = tab.getWebView(); |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 1948 | // If the last tab was removed from the active tabs page, currentTab |
| 1949 | // will be null. |
| 1950 | if (currentTab != null) { |
| 1951 | removeTabFromContentView(currentTab); |
| 1952 | } |
Patrick Scott | 8bbd69f | 2009-08-14 13:35:53 -0400 | [diff] [blame] | 1953 | // We must set the new tab as the current tab to reflect the old |
| 1954 | // animation behavior. |
| 1955 | mTabControl.setCurrentTab(tab); |
Grace Kloba | eb6eef4 | 2009-09-15 17:56:32 -0700 | [diff] [blame] | 1956 | attachTabToContentView(tab); |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 1957 | if (!urlData.isEmpty()) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1958 | urlData.loadIn(webview); |
| 1959 | } |
| 1960 | return tab; |
| 1961 | } else { |
| 1962 | // Get rid of the subwindow if it exists |
| 1963 | dismissSubWindow(currentTab); |
| 1964 | if (!urlData.isEmpty()) { |
| 1965 | // Load the given url. |
| 1966 | urlData.loadIn(currentTab.getWebView()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1967 | } |
| 1968 | } |
Grace Kloba | c918184 | 2009-04-14 08:53:22 -0700 | [diff] [blame] | 1969 | return currentTab; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1970 | } |
| 1971 | |
Grace Kloba | c918184 | 2009-04-14 08:53:22 -0700 | [diff] [blame] | 1972 | private TabControl.Tab openTab(String url) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1973 | if (mSettings.openInBackground()) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1974 | TabControl.Tab t = mTabControl.createNewTab(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1975 | if (t != null) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1976 | WebView view = t.getWebView(); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1977 | view.loadUrl(url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1978 | } |
Grace Kloba | c918184 | 2009-04-14 08:53:22 -0700 | [diff] [blame] | 1979 | return t; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1980 | } else { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1981 | return openTabAndShow(url, false, null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | private class Copy implements OnMenuItemClickListener { |
| 1986 | private CharSequence mText; |
| 1987 | |
| 1988 | public boolean onMenuItemClick(MenuItem item) { |
| 1989 | copy(mText); |
| 1990 | return true; |
| 1991 | } |
| 1992 | |
| 1993 | public Copy(CharSequence toCopy) { |
| 1994 | mText = toCopy; |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | private class Download implements OnMenuItemClickListener { |
| 1999 | private String mText; |
| 2000 | |
| 2001 | public boolean onMenuItemClick(MenuItem item) { |
| 2002 | onDownloadStartNoStream(mText, null, null, null, -1); |
| 2003 | return true; |
| 2004 | } |
| 2005 | |
| 2006 | public Download(String toDownload) { |
| 2007 | mText = toDownload; |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | private void copy(CharSequence text) { |
| 2012 | try { |
| 2013 | IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard")); |
| 2014 | if (clip != null) { |
| 2015 | clip.setClipboardText(text); |
| 2016 | } |
| 2017 | } catch (android.os.RemoteException e) { |
| 2018 | Log.e(LOGTAG, "Copy failed", e); |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2023 | * Resets the browser title-view to whatever it must be |
| 2024 | * (for example, if we had a loading error) |
| 2025 | * When we have a new page, we call resetTitle, when we |
| 2026 | * have to reset the titlebar to whatever it used to be |
| 2027 | * (for example, if the user chose to stop loading), we |
| 2028 | * call resetTitleAndRevertLockIcon. |
| 2029 | */ |
| 2030 | /* package */ void resetTitleAndRevertLockIcon() { |
| 2031 | revertLockIcon(); |
| 2032 | resetTitleIconAndProgress(); |
| 2033 | } |
| 2034 | |
| 2035 | /** |
| 2036 | * Reset the title, favicon, and progress. |
| 2037 | */ |
| 2038 | private void resetTitleIconAndProgress() { |
| 2039 | WebView current = mTabControl.getCurrentWebView(); |
| 2040 | if (current == null) { |
| 2041 | return; |
| 2042 | } |
| 2043 | resetTitleAndIcon(current); |
| 2044 | int progress = current.getProgress(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2045 | mWebChromeClient.onProgressChanged(current, progress); |
| 2046 | } |
| 2047 | |
| 2048 | // Reset the title and the icon based on the given item. |
| 2049 | private void resetTitleAndIcon(WebView view) { |
| 2050 | WebHistoryItem item = view.copyBackForwardList().getCurrentItem(); |
| 2051 | if (item != null) { |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 2052 | setUrlTitle(item.getUrl(), item.getTitle()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2053 | setFavicon(item.getFavicon()); |
| 2054 | } else { |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 2055 | setUrlTitle(null, null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2056 | setFavicon(null); |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | /** |
| 2061 | * Sets a title composed of the URL and the title string. |
| 2062 | * @param url The URL of the site being loaded. |
| 2063 | * @param title The title of the site being loaded. |
| 2064 | */ |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 2065 | private void setUrlTitle(String url, String title) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2066 | mUrl = url; |
| 2067 | mTitle = title; |
| 2068 | |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 2069 | mTitleBar.setTitleAndUrl(title, url); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 2070 | if (mFakeTitleBar != null) { |
| 2071 | mFakeTitleBar.setTitleAndUrl(title, url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2076 | * @param url The URL to build a title version of the URL from. |
| 2077 | * @return The title version of the URL or null if fails. |
| 2078 | * The title version of the URL can be either the URL hostname, |
| 2079 | * or the hostname with an "https://" prefix (for secure URLs), |
| 2080 | * or an empty string if, for example, the URL in question is a |
| 2081 | * file:// URL with no hostname. |
| 2082 | */ |
Leon Scroggins | 32e14a6 | 2009-06-11 10:26:34 -0400 | [diff] [blame] | 2083 | /* package */ static String buildTitleUrl(String url) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2084 | String titleUrl = null; |
| 2085 | |
| 2086 | if (url != null) { |
| 2087 | try { |
| 2088 | // parse the url string |
| 2089 | URL urlObj = new URL(url); |
| 2090 | if (urlObj != null) { |
| 2091 | titleUrl = ""; |
| 2092 | |
| 2093 | String protocol = urlObj.getProtocol(); |
| 2094 | String host = urlObj.getHost(); |
| 2095 | |
| 2096 | if (host != null && 0 < host.length()) { |
| 2097 | titleUrl = host; |
| 2098 | if (protocol != null) { |
| 2099 | // if a secure site, add an "https://" prefix! |
| 2100 | if (protocol.equalsIgnoreCase("https")) { |
| 2101 | titleUrl = protocol + "://" + host; |
| 2102 | } |
| 2103 | } |
| 2104 | } |
| 2105 | } |
| 2106 | } catch (MalformedURLException e) {} |
| 2107 | } |
| 2108 | |
| 2109 | return titleUrl; |
| 2110 | } |
| 2111 | |
| 2112 | // Set the favicon in the title bar. |
| 2113 | private void setFavicon(Bitmap icon) { |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 2114 | mTitleBar.setFavicon(icon); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 2115 | if (mFakeTitleBar != null) { |
| 2116 | mFakeTitleBar.setFavicon(icon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2117 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | /** |
| 2121 | * Saves the current lock-icon state before resetting |
| 2122 | * the lock icon. If we have an error, we may need to |
| 2123 | * roll back to the previous state. |
| 2124 | */ |
| 2125 | private void saveLockIcon() { |
| 2126 | mPrevLockType = mLockIconType; |
| 2127 | } |
| 2128 | |
| 2129 | /** |
| 2130 | * Reverts the lock-icon state to the last saved state, |
| 2131 | * for example, if we had an error, and need to cancel |
| 2132 | * the load. |
| 2133 | */ |
| 2134 | private void revertLockIcon() { |
| 2135 | mLockIconType = mPrevLockType; |
| 2136 | |
Dave Bort | 31a6d1c | 2009-04-13 15:56:49 -0700 | [diff] [blame] | 2137 | if (LOGV_ENABLED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2138 | Log.v(LOGTAG, "BrowserActivity.revertLockIcon:" + |
| 2139 | " revert lock icon to " + mLockIconType); |
| 2140 | } |
| 2141 | |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 2142 | updateLockIconToLatest(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2143 | } |
| 2144 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 2145 | /** |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 2146 | * Close the tab, remove its associated title bar, and adjust mTabControl's |
| 2147 | * current tab to a valid value. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 2148 | */ |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 2149 | /* package */ void closeTab(TabControl.Tab t) { |
| 2150 | int currentIndex = mTabControl.getCurrentIndex(); |
| 2151 | int removeIndex = mTabControl.getTabIndex(t); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 2152 | mTabControl.removeTab(t); |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 2153 | if (currentIndex >= removeIndex && currentIndex != 0) { |
| 2154 | currentIndex--; |
| 2155 | } |
| 2156 | mTabControl.setCurrentTab(mTabControl.getTab(currentIndex)); |
Andrei Popescu | a5bf1de | 2009-09-23 16:39:23 +0100 | [diff] [blame] | 2157 | resetTitleIconAndProgress(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | private void goBackOnePageOrQuit() { |
| 2161 | TabControl.Tab current = mTabControl.getCurrentTab(); |
| 2162 | if (current == null) { |
| 2163 | /* |
| 2164 | * Instead of finishing the activity, simply push this to the back |
| 2165 | * of the stack and let ActivityManager to choose the foreground |
| 2166 | * activity. As BrowserActivity is singleTask, it will be always the |
| 2167 | * root of the task. So we can use either true or false for |
| 2168 | * moveTaskToBack(). |
| 2169 | */ |
| 2170 | moveTaskToBack(true); |
Grace Kloba | 00d85e7 | 2009-09-23 18:50:05 -0700 | [diff] [blame] | 2171 | return; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2172 | } |
| 2173 | WebView w = current.getWebView(); |
| 2174 | if (w.canGoBack()) { |
| 2175 | w.goBack(); |
| 2176 | } else { |
| 2177 | // Check to see if we are closing a window that was created by |
| 2178 | // another window. If so, we switch back to that window. |
| 2179 | TabControl.Tab parent = current.getParentTab(); |
| 2180 | if (parent != null) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 2181 | switchToTab(mTabControl.getTabIndex(parent)); |
| 2182 | // Now we close the other tab |
| 2183 | closeTab(current); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2184 | } else { |
| 2185 | if (current.closeOnExit()) { |
Grace Kloba | bb0af5c | 2009-09-01 00:56:09 -0700 | [diff] [blame] | 2186 | // force mPageStarted to be false as we are going to either |
| 2187 | // finish the activity or remove the tab. This will ensure |
| 2188 | // pauseWebView() taking action. |
| 2189 | mPageStarted = false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2190 | if (mTabControl.getTabCount() == 1) { |
| 2191 | finish(); |
| 2192 | return; |
| 2193 | } |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 2194 | // call pauseWebViewTimers() now, we won't be able to call |
| 2195 | // it in onPause() as the WebView won't be valid. |
Grace Kloba | ec1b5ad | 2009-08-18 08:42:32 -0700 | [diff] [blame] | 2196 | // Temporarily change mActivityInPause to be true as |
| 2197 | // pauseWebViewTimers() will do nothing if mActivityInPause |
| 2198 | // is false. |
Grace Kloba | 918e1d7 | 2009-08-13 14:55:06 -0700 | [diff] [blame] | 2199 | boolean savedState = mActivityInPause; |
| 2200 | if (savedState) { |
Grace Kloba | ec1b5ad | 2009-08-18 08:42:32 -0700 | [diff] [blame] | 2201 | Log.e(LOGTAG, "BrowserActivity is already paused " |
| 2202 | + "while handing goBackOnePageOrQuit."); |
Grace Kloba | 918e1d7 | 2009-08-13 14:55:06 -0700 | [diff] [blame] | 2203 | } |
| 2204 | mActivityInPause = true; |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 2205 | pauseWebViewTimers(); |
Grace Kloba | 918e1d7 | 2009-08-13 14:55:06 -0700 | [diff] [blame] | 2206 | mActivityInPause = savedState; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2207 | removeTabFromContentView(current); |
| 2208 | mTabControl.removeTab(current); |
| 2209 | } |
| 2210 | /* |
| 2211 | * Instead of finishing the activity, simply push this to the back |
| 2212 | * of the stack and let ActivityManager to choose the foreground |
| 2213 | * activity. As BrowserActivity is singleTask, it will be always the |
| 2214 | * root of the task. So we can use either true or false for |
| 2215 | * moveTaskToBack(). |
| 2216 | */ |
| 2217 | moveTaskToBack(true); |
| 2218 | } |
| 2219 | } |
| 2220 | } |
| 2221 | |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 2222 | @Override |
| 2223 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 2224 | // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is |
| 2225 | // still down, we don't want to trigger the search. Pretend to consume |
| 2226 | // the key and do nothing. |
| 2227 | if (mMenuIsDown) return true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2228 | |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 2229 | switch(keyCode) { |
| 2230 | case KeyEvent.KEYCODE_MENU: |
| 2231 | mMenuIsDown = true; |
| 2232 | break; |
| 2233 | case KeyEvent.KEYCODE_SPACE: |
Grace Kloba | da0fe55 | 2009-09-22 18:17:24 -0700 | [diff] [blame] | 2234 | // WebView/WebTextView handle the keys in the KeyDown. As |
| 2235 | // the Activity's shortcut keys are only handled when WebView |
| 2236 | // doesn't, have to do it in onKeyDown instead of onKeyUp. |
| 2237 | if (event.isShiftPressed()) { |
| 2238 | getTopWindow().pageUp(false); |
| 2239 | } else { |
| 2240 | getTopWindow().pageDown(false); |
| 2241 | } |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 2242 | return true; |
| 2243 | case KeyEvent.KEYCODE_BACK: |
| 2244 | if (event.getRepeatCount() == 0) { |
| 2245 | event.startTracking(); |
| 2246 | return true; |
| 2247 | } else if (mCustomView == null && mActiveTabsPage == null |
| 2248 | && event.isLongPress()) { |
| 2249 | bookmarksOrHistoryPicker(true); |
| 2250 | return true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2251 | } |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 2252 | break; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2253 | } |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 2254 | return super.onKeyDown(keyCode, event); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2255 | } |
| 2256 | |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 2257 | @Override |
| 2258 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 2259 | switch(keyCode) { |
| 2260 | case KeyEvent.KEYCODE_MENU: |
| 2261 | mMenuIsDown = false; |
| 2262 | break; |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 2263 | case KeyEvent.KEYCODE_BACK: |
| 2264 | if (event.isTracking() && !event.isCanceled()) { |
| 2265 | if (mCustomView != null) { |
| 2266 | // if a custom view is showing, hide it |
| 2267 | mWebChromeClient.onHideCustomView(); |
| 2268 | } else if (mActiveTabsPage != null) { |
| 2269 | // if tab page is showing, hide it |
| 2270 | removeActiveTabPage(true); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2271 | } else { |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 2272 | WebView subwindow = mTabControl.getCurrentSubWindow(); |
| 2273 | if (subwindow != null) { |
| 2274 | if (subwindow.canGoBack()) { |
| 2275 | subwindow.goBack(); |
| 2276 | } else { |
| 2277 | dismissSubWindow(mTabControl.getCurrentTab()); |
| 2278 | } |
| 2279 | } else { |
| 2280 | goBackOnePageOrQuit(); |
| 2281 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2282 | } |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 2283 | return true; |
| 2284 | } |
| 2285 | break; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2286 | } |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 2287 | return super.onKeyUp(keyCode, event); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2288 | } |
| 2289 | |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 2290 | /* package */ void stopLoading() { |
Ben Murdoch | b7cc8b4 | 2009-09-28 10:59:09 +0100 | [diff] [blame] | 2291 | mDidStopLoad = true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2292 | resetTitleAndRevertLockIcon(); |
| 2293 | WebView w = getTopWindow(); |
| 2294 | w.stopLoading(); |
| 2295 | mWebViewClient.onPageFinished(w, w.getUrl()); |
| 2296 | |
| 2297 | cancelStopToast(); |
| 2298 | mStopToast = Toast |
| 2299 | .makeText(this, R.string.stopping, Toast.LENGTH_SHORT); |
| 2300 | mStopToast.show(); |
| 2301 | } |
| 2302 | |
| 2303 | private void cancelStopToast() { |
| 2304 | if (mStopToast != null) { |
| 2305 | mStopToast.cancel(); |
| 2306 | mStopToast = null; |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | // called by a non-UI thread to post the message |
| 2311 | public void postMessage(int what, int arg1, int arg2, Object obj) { |
| 2312 | mHandler.sendMessage(mHandler.obtainMessage(what, arg1, arg2, obj)); |
| 2313 | } |
| 2314 | |
| 2315 | // public message ids |
| 2316 | public final static int LOAD_URL = 1001; |
| 2317 | public final static int STOP_LOAD = 1002; |
| 2318 | |
| 2319 | // Message Ids |
| 2320 | private static final int FOCUS_NODE_HREF = 102; |
| 2321 | private static final int CANCEL_CREDS_REQUEST = 103; |
Grace Kloba | 92c18a5 | 2009-07-31 23:48:32 -0700 | [diff] [blame] | 2322 | private static final int RELEASE_WAKELOCK = 107; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2323 | |
Ben Murdoch | 2694e23 | 2009-09-29 09:41:11 +0100 | [diff] [blame] | 2324 | private static final int UPDATE_BOOKMARK_THUMBNAIL = 108; |
| 2325 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2326 | // Private handler for handling javascript and saving passwords |
| 2327 | private Handler mHandler = new Handler() { |
| 2328 | |
| 2329 | public void handleMessage(Message msg) { |
| 2330 | switch (msg.what) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2331 | case FOCUS_NODE_HREF: |
Ben Murdoch | 2694e23 | 2009-09-29 09:41:11 +0100 | [diff] [blame] | 2332 | { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2333 | String url = (String) msg.getData().get("url"); |
| 2334 | if (url == null || url.length() == 0) { |
| 2335 | break; |
| 2336 | } |
| 2337 | HashMap focusNodeMap = (HashMap) msg.obj; |
| 2338 | WebView view = (WebView) focusNodeMap.get("webview"); |
| 2339 | // Only apply the action if the top window did not change. |
| 2340 | if (getTopWindow() != view) { |
| 2341 | break; |
| 2342 | } |
| 2343 | switch (msg.arg1) { |
| 2344 | case R.id.open_context_menu_id: |
| 2345 | case R.id.view_image_context_menu_id: |
| 2346 | loadURL(getTopWindow(), url); |
| 2347 | break; |
| 2348 | case R.id.open_newtab_context_menu_id: |
Grace Kloba | c918184 | 2009-04-14 08:53:22 -0700 | [diff] [blame] | 2349 | final TabControl.Tab parent = mTabControl |
| 2350 | .getCurrentTab(); |
| 2351 | final TabControl.Tab newTab = openTab(url); |
| 2352 | if (newTab != parent) { |
| 2353 | parent.addChildTab(newTab); |
| 2354 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2355 | break; |
| 2356 | case R.id.bookmark_context_menu_id: |
| 2357 | Intent intent = new Intent(BrowserActivity.this, |
| 2358 | AddBookmarkPage.class); |
| 2359 | intent.putExtra("url", url); |
| 2360 | startActivity(intent); |
| 2361 | break; |
| 2362 | case R.id.share_link_context_menu_id: |
Andrei Popescu | 10fdba8 | 2009-09-24 13:25:47 +0100 | [diff] [blame] | 2363 | Browser.sendString(BrowserActivity.this, url, |
| 2364 | getText(R.string.choosertitle_sharevia).toString()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2365 | break; |
| 2366 | case R.id.copy_link_context_menu_id: |
| 2367 | copy(url); |
| 2368 | break; |
| 2369 | case R.id.save_link_context_menu_id: |
| 2370 | case R.id.download_context_menu_id: |
| 2371 | onDownloadStartNoStream(url, null, null, null, -1); |
| 2372 | break; |
| 2373 | } |
| 2374 | break; |
Ben Murdoch | 2694e23 | 2009-09-29 09:41:11 +0100 | [diff] [blame] | 2375 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2376 | |
| 2377 | case LOAD_URL: |
| 2378 | loadURL(getTopWindow(), (String) msg.obj); |
| 2379 | break; |
| 2380 | |
| 2381 | case STOP_LOAD: |
| 2382 | stopLoading(); |
| 2383 | break; |
| 2384 | |
| 2385 | case CANCEL_CREDS_REQUEST: |
| 2386 | resumeAfterCredentials(); |
| 2387 | break; |
| 2388 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2389 | case RELEASE_WAKELOCK: |
| 2390 | if (mWakeLock.isHeld()) { |
| 2391 | mWakeLock.release(); |
| 2392 | } |
| 2393 | break; |
Ben Murdoch | 2694e23 | 2009-09-29 09:41:11 +0100 | [diff] [blame] | 2394 | |
| 2395 | case UPDATE_BOOKMARK_THUMBNAIL: |
| 2396 | WebView view = (WebView) msg.obj; |
| 2397 | if (view != null) { |
| 2398 | updateScreenshot(view); |
| 2399 | } |
| 2400 | break; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | }; |
| 2404 | |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 2405 | private void updateScreenshot(WebView view) { |
| 2406 | // If this is a bookmarked site, add a screenshot to the database. |
| 2407 | // FIXME: When should we update? Every time? |
| 2408 | // FIXME: Would like to make sure there is actually something to |
| 2409 | // draw, but the API for that (WebViewCore.pictureReady()) is not |
| 2410 | // currently accessible here. |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 2411 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 2412 | ContentResolver cr = getContentResolver(); |
| 2413 | final Cursor c = BrowserBookmarksAdapter.queryBookmarksForUrl( |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 2414 | cr, view.getOriginalUrl(), view.getUrl(), true); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 2415 | if (c != null) { |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 2416 | boolean succeed = c.moveToFirst(); |
| 2417 | ContentValues values = null; |
| 2418 | while (succeed) { |
| 2419 | if (values == null) { |
| 2420 | final ByteArrayOutputStream os |
| 2421 | = new ByteArrayOutputStream(); |
Ben Murdoch | dcc2b6f | 2009-09-21 14:29:20 +0100 | [diff] [blame] | 2422 | Bitmap bm = createScreenshot(view); |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 2423 | bm.compress(Bitmap.CompressFormat.PNG, 100, os); |
| 2424 | values = new ContentValues(); |
| 2425 | values.put(Browser.BookmarkColumns.THUMBNAIL, |
| 2426 | os.toByteArray()); |
| 2427 | } |
| 2428 | cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI, |
| 2429 | c.getInt(0)), values, null, null); |
| 2430 | succeed = c.moveToNext(); |
| 2431 | } |
| 2432 | c.close(); |
| 2433 | } |
| 2434 | } |
| 2435 | |
Leon Scroggins | 06ec5f2 | 2009-09-17 12:46:04 -0400 | [diff] [blame] | 2436 | /** |
Leon Scroggins | f855161 | 2009-09-24 16:06:02 -0400 | [diff] [blame] | 2437 | * Values for the size of the thumbnail created when taking a screenshot. |
| 2438 | * Lazily initialized. Instead of using these directly, use |
| 2439 | * getDesiredThumbnailWidth() or getDesiredThumbnailHeight(). |
Leon Scroggins | 06ec5f2 | 2009-09-17 12:46:04 -0400 | [diff] [blame] | 2440 | */ |
Leon Scroggins | f855161 | 2009-09-24 16:06:02 -0400 | [diff] [blame] | 2441 | private static int THUMBNAIL_WIDTH = 0; |
| 2442 | private static int THUMBNAIL_HEIGHT = 0; |
| 2443 | |
| 2444 | /** |
| 2445 | * Return the desired width for thumbnail screenshots, which are stored in |
| 2446 | * the database, and used on the bookmarks screen. |
| 2447 | * @param context Context for finding out the density of the screen. |
| 2448 | * @return int desired width for thumbnail screenshot. |
| 2449 | */ |
| 2450 | /* package */ static int getDesiredThumbnailWidth(Context context) { |
| 2451 | if (THUMBNAIL_WIDTH == 0) { |
| 2452 | float density = context.getResources().getDisplayMetrics().density; |
| 2453 | THUMBNAIL_WIDTH = (int) (90 * density); |
| 2454 | THUMBNAIL_HEIGHT = (int) (80 * density); |
| 2455 | } |
| 2456 | return THUMBNAIL_WIDTH; |
| 2457 | } |
| 2458 | |
| 2459 | /** |
| 2460 | * Return the desired height for thumbnail screenshots, which are stored in |
| 2461 | * the database, and used on the bookmarks screen. |
| 2462 | * @param context Context for finding out the density of the screen. |
| 2463 | * @return int desired height for thumbnail screenshot. |
| 2464 | */ |
| 2465 | /* package */ static int getDesiredThumbnailHeight(Context context) { |
| 2466 | // To ensure that they are both initialized. |
| 2467 | getDesiredThumbnailWidth(context); |
| 2468 | return THUMBNAIL_HEIGHT; |
| 2469 | } |
Leon Scroggins | 06ec5f2 | 2009-09-17 12:46:04 -0400 | [diff] [blame] | 2470 | |
Ben Murdoch | dcc2b6f | 2009-09-21 14:29:20 +0100 | [diff] [blame] | 2471 | private Bitmap createScreenshot(WebView view) { |
| 2472 | Picture thumbnail = view.capturePicture(); |
Leon Scroggins | f855161 | 2009-09-24 16:06:02 -0400 | [diff] [blame] | 2473 | Bitmap bm = Bitmap.createBitmap(getDesiredThumbnailWidth(this), |
| 2474 | getDesiredThumbnailHeight(this), Bitmap.Config.ARGB_4444); |
Ben Murdoch | dcc2b6f | 2009-09-21 14:29:20 +0100 | [diff] [blame] | 2475 | Canvas canvas = new Canvas(bm); |
| 2476 | // May need to tweak these values to determine what is the |
| 2477 | // best scale factor |
Ben Murdoch | 2694e23 | 2009-09-29 09:41:11 +0100 | [diff] [blame] | 2478 | int thumbnailWidth = thumbnail.getWidth(); |
| 2479 | if (thumbnailWidth > 0) { |
| 2480 | float scaleFactor = (float) getDesiredThumbnailWidth(this) / |
| 2481 | (float)thumbnailWidth; |
Leon Scroggins | 06ec5f2 | 2009-09-17 12:46:04 -0400 | [diff] [blame] | 2482 | canvas.scale(scaleFactor, scaleFactor); |
| 2483 | } |
Ben Murdoch | dcc2b6f | 2009-09-21 14:29:20 +0100 | [diff] [blame] | 2484 | thumbnail.draw(canvas); |
| 2485 | return bm; |
| 2486 | } |
| 2487 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2488 | // ------------------------------------------------------------------------- |
| 2489 | // WebViewClient implementation. |
| 2490 | //------------------------------------------------------------------------- |
| 2491 | |
| 2492 | // Use in overrideUrlLoading |
| 2493 | /* package */ final static String SCHEME_WTAI = "wtai://wp/"; |
| 2494 | /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;"; |
| 2495 | /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;"; |
| 2496 | /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;"; |
| 2497 | |
| 2498 | /* package */ WebViewClient getWebViewClient() { |
| 2499 | return mWebViewClient; |
| 2500 | } |
| 2501 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 2502 | private void updateIcon(WebView view, Bitmap icon) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2503 | if (icon != null) { |
| 2504 | BrowserBookmarksAdapter.updateBookmarkFavicon(mResolver, |
Patrick Scott | 15525d4 | 2009-09-21 13:39:37 -0400 | [diff] [blame] | 2505 | view.getOriginalUrl(), view.getUrl(), icon); |
| 2506 | } |
| 2507 | setFavicon(icon); |
| 2508 | } |
| 2509 | |
| 2510 | private void updateIcon(String url, Bitmap icon) { |
| 2511 | if (icon != null) { |
| 2512 | BrowserBookmarksAdapter.updateBookmarkFavicon(mResolver, |
| 2513 | null, url, icon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2514 | } |
| 2515 | setFavicon(icon); |
| 2516 | } |
| 2517 | |
| 2518 | private final WebViewClient mWebViewClient = new WebViewClient() { |
| 2519 | @Override |
| 2520 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 2521 | resetLockIcon(url); |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 2522 | setUrlTitle(url, null); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 2523 | |
Ben Murdoch | 2694e23 | 2009-09-29 09:41:11 +0100 | [diff] [blame] | 2524 | // We've started to load a new page. If there was a pending message |
| 2525 | // to save a screenshot then we will now take the new page and |
| 2526 | // save an incorrect screenshot. Therefore, remove any pending |
| 2527 | // thumbnail messages from the queue. |
| 2528 | mHandler.removeMessages(UPDATE_BOOKMARK_THUMBNAIL); |
| 2529 | |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 2530 | // If we start a touch icon load and then load a new page, we don't |
| 2531 | // want to cancel the current touch icon loader. But, we do want to |
| 2532 | // create a new one when the touch icon url is known. |
| 2533 | if (mTouchIconLoader != null) { |
| 2534 | mTouchIconLoader.mActivity = null; |
| 2535 | mTouchIconLoader = null; |
| 2536 | } |
| 2537 | |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 2538 | ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(false); |
| 2539 | if (errorConsole != null) { |
| 2540 | errorConsole.clearErrorMessages(); |
| 2541 | if (mShouldShowErrorConsole) { |
| 2542 | errorConsole.showConsole(ErrorConsoleView.SHOW_NONE); |
| 2543 | } |
| 2544 | } |
| 2545 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2546 | // Call updateIcon instead of setFavicon so the bookmark |
| 2547 | // database can be updated. |
Patrick Scott | 15525d4 | 2009-09-21 13:39:37 -0400 | [diff] [blame] | 2548 | updateIcon(url, favicon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2549 | |
Grace Kloba | 4d7880f | 2009-08-12 09:35:42 -0700 | [diff] [blame] | 2550 | if (mSettings.isTracing()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2551 | String host; |
| 2552 | try { |
| 2553 | WebAddress uri = new WebAddress(url); |
| 2554 | host = uri.mHost; |
| 2555 | } catch (android.net.ParseException ex) { |
Grace Kloba | 4d7880f | 2009-08-12 09:35:42 -0700 | [diff] [blame] | 2556 | host = "browser"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2557 | } |
| 2558 | host = host.replace('.', '_'); |
Grace Kloba | 4d7880f | 2009-08-12 09:35:42 -0700 | [diff] [blame] | 2559 | host += ".trace"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2560 | mInTrace = true; |
Grace Kloba | 4d7880f | 2009-08-12 09:35:42 -0700 | [diff] [blame] | 2561 | Debug.startMethodTracing(host, 20 * 1024 * 1024); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | // Performance probe |
| 2565 | if (false) { |
| 2566 | mStart = SystemClock.uptimeMillis(); |
| 2567 | mProcessStart = Process.getElapsedCpuTime(); |
| 2568 | long[] sysCpu = new long[7]; |
| 2569 | if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null, |
| 2570 | sysCpu, null)) { |
| 2571 | mUserStart = sysCpu[0] + sysCpu[1]; |
| 2572 | mSystemStart = sysCpu[2]; |
| 2573 | mIdleStart = sysCpu[3]; |
| 2574 | mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6]; |
| 2575 | } |
| 2576 | mUiStart = SystemClock.currentThreadTimeMillis(); |
| 2577 | } |
| 2578 | |
| 2579 | if (!mPageStarted) { |
| 2580 | mPageStarted = true; |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 2581 | // if onResume() has been called, resumeWebViewTimers() does |
| 2582 | // nothing. |
| 2583 | resumeWebViewTimers(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | // reset sync timer to avoid sync starts during loading a page |
| 2587 | CookieSyncManager.getInstance().resetSync(); |
| 2588 | |
| 2589 | mInLoad = true; |
Ben Murdoch | b7cc8b4 | 2009-09-28 10:59:09 +0100 | [diff] [blame] | 2590 | mDidStopLoad = false; |
Leon Scroggins | 184f5e3 | 2009-09-21 10:38:24 -0400 | [diff] [blame] | 2591 | showFakeTitleBar(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2592 | updateInLoadMenuItems(); |
| 2593 | if (!mIsNetworkUp) { |
Patrick Scott | eb6ab2a | 2009-09-16 10:00:17 -0400 | [diff] [blame] | 2594 | createAndShowNetworkDialog(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2595 | if (view != null) { |
| 2596 | view.setNetworkAvailable(false); |
| 2597 | } |
| 2598 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | @Override |
| 2602 | public void onPageFinished(WebView view, String url) { |
| 2603 | // Reset the title and icon in case we stopped a provisional |
| 2604 | // load. |
| 2605 | resetTitleAndIcon(view); |
Ben Murdoch | b7cc8b4 | 2009-09-28 10:59:09 +0100 | [diff] [blame] | 2606 | |
| 2607 | if (!mDidStopLoad) { |
| 2608 | // Only update the bookmark screenshot if the user did not |
| 2609 | // cancel the load early. |
Ben Murdoch | 2694e23 | 2009-09-29 09:41:11 +0100 | [diff] [blame] | 2610 | Message updateScreenshot = Message.obtain(mHandler, UPDATE_BOOKMARK_THUMBNAIL, view); |
| 2611 | mHandler.sendMessageDelayed(updateScreenshot, 500); |
Ben Murdoch | b7cc8b4 | 2009-09-28 10:59:09 +0100 | [diff] [blame] | 2612 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2613 | |
| 2614 | // Update the lock icon image only once we are done loading |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 2615 | updateLockIconToLatest(); |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 2616 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2617 | // Performance probe |
The Android Open Source Project | cb9a0bb | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 2618 | if (false) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2619 | long[] sysCpu = new long[7]; |
| 2620 | if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null, |
| 2621 | sysCpu, null)) { |
| 2622 | String uiInfo = "UI thread used " |
| 2623 | + (SystemClock.currentThreadTimeMillis() - mUiStart) |
| 2624 | + " ms"; |
Dave Bort | 31a6d1c | 2009-04-13 15:56:49 -0700 | [diff] [blame] | 2625 | if (LOGD_ENABLED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2626 | Log.d(LOGTAG, uiInfo); |
| 2627 | } |
| 2628 | //The string that gets written to the log |
| 2629 | String performanceString = "It took total " |
| 2630 | + (SystemClock.uptimeMillis() - mStart) |
| 2631 | + " ms clock time to load the page." |
| 2632 | + "\nbrowser process used " |
| 2633 | + (Process.getElapsedCpuTime() - mProcessStart) |
| 2634 | + " ms, user processes used " |
| 2635 | + (sysCpu[0] + sysCpu[1] - mUserStart) * 10 |
| 2636 | + " ms, kernel used " |
| 2637 | + (sysCpu[2] - mSystemStart) * 10 |
| 2638 | + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10 |
| 2639 | + " ms and irq took " |
| 2640 | + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart) |
| 2641 | * 10 + " ms, " + uiInfo; |
Dave Bort | 31a6d1c | 2009-04-13 15:56:49 -0700 | [diff] [blame] | 2642 | if (LOGD_ENABLED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2643 | Log.d(LOGTAG, performanceString + "\nWebpage: " + url); |
| 2644 | } |
| 2645 | if (url != null) { |
| 2646 | // strip the url to maintain consistency |
| 2647 | String newUrl = new String(url); |
| 2648 | if (newUrl.startsWith("http://www.")) { |
| 2649 | newUrl = newUrl.substring(11); |
| 2650 | } else if (newUrl.startsWith("http://")) { |
| 2651 | newUrl = newUrl.substring(7); |
| 2652 | } else if (newUrl.startsWith("https://www.")) { |
| 2653 | newUrl = newUrl.substring(12); |
| 2654 | } else if (newUrl.startsWith("https://")) { |
| 2655 | newUrl = newUrl.substring(8); |
| 2656 | } |
Dave Bort | 31a6d1c | 2009-04-13 15:56:49 -0700 | [diff] [blame] | 2657 | if (LOGD_ENABLED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2658 | Log.d(LOGTAG, newUrl + " loaded"); |
| 2659 | } |
| 2660 | /* |
| 2661 | if (sWhiteList.contains(newUrl)) { |
| 2662 | // The string that gets pushed to the statistcs |
| 2663 | // service |
| 2664 | performanceString = performanceString |
| 2665 | + "\nWebpage: " |
| 2666 | + newUrl |
| 2667 | + "\nCarrier: " |
| 2668 | + android.os.SystemProperties |
| 2669 | .get("gsm.sim.operator.alpha"); |
| 2670 | if (mWebView != null |
| 2671 | && mWebView.getContext() != null |
| 2672 | && mWebView.getContext().getSystemService( |
| 2673 | Context.CONNECTIVITY_SERVICE) != null) { |
| 2674 | ConnectivityManager cManager = |
| 2675 | (ConnectivityManager) mWebView |
| 2676 | .getContext().getSystemService( |
| 2677 | Context.CONNECTIVITY_SERVICE); |
| 2678 | NetworkInfo nInfo = cManager |
| 2679 | .getActiveNetworkInfo(); |
| 2680 | if (nInfo != null) { |
| 2681 | performanceString = performanceString |
| 2682 | + "\nNetwork Type: " |
| 2683 | + nInfo.getType().toString(); |
| 2684 | } |
| 2685 | } |
| 2686 | Checkin.logEvent(mResolver, |
| 2687 | Checkin.Events.Tag.WEBPAGE_LOAD, |
| 2688 | performanceString); |
| 2689 | Log.w(LOGTAG, "pushed to the statistics service"); |
| 2690 | } |
| 2691 | */ |
| 2692 | } |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | if (mInTrace) { |
| 2697 | mInTrace = false; |
| 2698 | Debug.stopMethodTracing(); |
| 2699 | } |
| 2700 | |
| 2701 | if (mPageStarted) { |
| 2702 | mPageStarted = false; |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 2703 | // pauseWebViewTimers() will do nothing and return false if |
| 2704 | // onPause() is not called yet. |
| 2705 | if (pauseWebViewTimers()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2706 | if (mWakeLock.isHeld()) { |
| 2707 | mHandler.removeMessages(RELEASE_WAKELOCK); |
| 2708 | mWakeLock.release(); |
| 2709 | } |
| 2710 | } |
| 2711 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | // return true if want to hijack the url to let another app to handle it |
| 2715 | @Override |
| 2716 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 2717 | if (url.startsWith(SCHEME_WTAI)) { |
| 2718 | // wtai://wp/mc;number |
| 2719 | // number=string(phone-number) |
| 2720 | if (url.startsWith(SCHEME_WTAI_MC)) { |
| 2721 | Intent intent = new Intent(Intent.ACTION_VIEW, |
| 2722 | Uri.parse(WebView.SCHEME_TEL + |
| 2723 | url.substring(SCHEME_WTAI_MC.length()))); |
| 2724 | startActivity(intent); |
| 2725 | return true; |
| 2726 | } |
| 2727 | // wtai://wp/sd;dtmf |
| 2728 | // dtmf=string(dialstring) |
| 2729 | if (url.startsWith(SCHEME_WTAI_SD)) { |
| 2730 | // TODO |
| 2731 | // only send when there is active voice connection |
| 2732 | return false; |
| 2733 | } |
| 2734 | // wtai://wp/ap;number;name |
| 2735 | // number=string(phone-number) |
| 2736 | // name=string |
| 2737 | if (url.startsWith(SCHEME_WTAI_AP)) { |
| 2738 | // TODO |
| 2739 | return false; |
| 2740 | } |
| 2741 | } |
| 2742 | |
Dianne Hackborn | 9918943 | 2009-06-17 18:06:18 -0700 | [diff] [blame] | 2743 | // The "about:" schemes are internal to the browser; don't |
| 2744 | // want these to be dispatched to other apps. |
| 2745 | if (url.startsWith("about:")) { |
| 2746 | return false; |
| 2747 | } |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 2748 | |
Dianne Hackborn | 9918943 | 2009-06-17 18:06:18 -0700 | [diff] [blame] | 2749 | Intent intent; |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 2750 | |
Dianne Hackborn | 9918943 | 2009-06-17 18:06:18 -0700 | [diff] [blame] | 2751 | // perform generic parsing of the URI to turn it into an Intent. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2752 | try { |
Dianne Hackborn | 9918943 | 2009-06-17 18:06:18 -0700 | [diff] [blame] | 2753 | intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
| 2754 | } catch (URISyntaxException ex) { |
| 2755 | Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2756 | return false; |
| 2757 | } |
| 2758 | |
Grace Kloba | 5b078b5 | 2009-06-24 20:23:41 -0700 | [diff] [blame] | 2759 | // check whether the intent can be resolved. If not, we will see |
| 2760 | // whether we can download it from the Market. |
| 2761 | if (getPackageManager().resolveActivity(intent, 0) == null) { |
| 2762 | String packagename = intent.getPackage(); |
| 2763 | if (packagename != null) { |
| 2764 | intent = new Intent(Intent.ACTION_VIEW, Uri |
| 2765 | .parse("market://search?q=pname:" + packagename)); |
| 2766 | intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 2767 | startActivity(intent); |
| 2768 | return true; |
| 2769 | } else { |
| 2770 | return false; |
| 2771 | } |
| 2772 | } |
| 2773 | |
Dianne Hackborn | 9918943 | 2009-06-17 18:06:18 -0700 | [diff] [blame] | 2774 | // sanitize the Intent, ensuring web pages can not bypass browser |
| 2775 | // security (only access to BROWSABLE activities). |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2776 | intent.addCategory(Intent.CATEGORY_BROWSABLE); |
Dianne Hackborn | 9918943 | 2009-06-17 18:06:18 -0700 | [diff] [blame] | 2777 | intent.setComponent(null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2778 | try { |
| 2779 | if (startActivityIfNeeded(intent, -1)) { |
| 2780 | return true; |
| 2781 | } |
| 2782 | } catch (ActivityNotFoundException ex) { |
| 2783 | // ignore the error. If no application can handle the URL, |
| 2784 | // eg about:blank, assume the browser can handle it. |
| 2785 | } |
| 2786 | |
| 2787 | if (mMenuIsDown) { |
| 2788 | openTab(url); |
| 2789 | closeOptionsMenu(); |
| 2790 | return true; |
| 2791 | } |
| 2792 | |
| 2793 | return false; |
| 2794 | } |
| 2795 | |
| 2796 | /** |
| 2797 | * Updates the lock icon. This method is called when we discover another |
| 2798 | * resource to be loaded for this page (for example, javascript). While |
| 2799 | * we update the icon type, we do not update the lock icon itself until |
| 2800 | * we are done loading, it is slightly more secure this way. |
| 2801 | */ |
| 2802 | @Override |
| 2803 | public void onLoadResource(WebView view, String url) { |
| 2804 | if (url != null && url.length() > 0) { |
| 2805 | // It is only if the page claims to be secure |
| 2806 | // that we may have to update the lock: |
| 2807 | if (mLockIconType == LOCK_ICON_SECURE) { |
| 2808 | // If NOT a 'safe' url, change the lock to mixed content! |
| 2809 | if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url) || URLUtil.isAboutUrl(url))) { |
| 2810 | mLockIconType = LOCK_ICON_MIXED; |
Dave Bort | 31a6d1c | 2009-04-13 15:56:49 -0700 | [diff] [blame] | 2811 | if (LOGV_ENABLED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2812 | Log.v(LOGTAG, "BrowserActivity.updateLockIcon:" + |
| 2813 | " updated lock icon to " + mLockIconType + " due to " + url); |
| 2814 | } |
| 2815 | } |
| 2816 | } |
| 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | /** |
| 2821 | * Show the dialog, asking the user if they would like to continue after |
| 2822 | * an excessive number of HTTP redirects. |
| 2823 | */ |
| 2824 | @Override |
| 2825 | public void onTooManyRedirects(WebView view, final Message cancelMsg, |
| 2826 | final Message continueMsg) { |
| 2827 | new AlertDialog.Builder(BrowserActivity.this) |
| 2828 | .setTitle(R.string.browserFrameRedirect) |
| 2829 | .setMessage(R.string.browserFrame307Post) |
| 2830 | .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 2831 | public void onClick(DialogInterface dialog, int which) { |
| 2832 | continueMsg.sendToTarget(); |
| 2833 | }}) |
| 2834 | .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { |
| 2835 | public void onClick(DialogInterface dialog, int which) { |
| 2836 | cancelMsg.sendToTarget(); |
| 2837 | }}) |
| 2838 | .setOnCancelListener(new OnCancelListener() { |
| 2839 | public void onCancel(DialogInterface dialog) { |
| 2840 | cancelMsg.sendToTarget(); |
| 2841 | }}) |
| 2842 | .show(); |
| 2843 | } |
| 2844 | |
Patrick Scott | 37911c7 | 2009-03-24 18:02:58 -0700 | [diff] [blame] | 2845 | // Container class for the next error dialog that needs to be |
| 2846 | // displayed. |
| 2847 | class ErrorDialog { |
| 2848 | public final int mTitle; |
| 2849 | public final String mDescription; |
| 2850 | public final int mError; |
| 2851 | ErrorDialog(int title, String desc, int error) { |
| 2852 | mTitle = title; |
| 2853 | mDescription = desc; |
| 2854 | mError = error; |
| 2855 | } |
| 2856 | }; |
| 2857 | |
| 2858 | private void processNextError() { |
| 2859 | if (mQueuedErrors == null) { |
| 2860 | return; |
| 2861 | } |
| 2862 | // The first one is currently displayed so just remove it. |
| 2863 | mQueuedErrors.removeFirst(); |
| 2864 | if (mQueuedErrors.size() == 0) { |
| 2865 | mQueuedErrors = null; |
| 2866 | return; |
| 2867 | } |
| 2868 | showError(mQueuedErrors.getFirst()); |
| 2869 | } |
| 2870 | |
| 2871 | private DialogInterface.OnDismissListener mDialogListener = |
| 2872 | new DialogInterface.OnDismissListener() { |
| 2873 | public void onDismiss(DialogInterface d) { |
| 2874 | processNextError(); |
| 2875 | } |
| 2876 | }; |
| 2877 | private LinkedList<ErrorDialog> mQueuedErrors; |
| 2878 | |
| 2879 | private void queueError(int err, String desc) { |
| 2880 | if (mQueuedErrors == null) { |
| 2881 | mQueuedErrors = new LinkedList<ErrorDialog>(); |
| 2882 | } |
| 2883 | for (ErrorDialog d : mQueuedErrors) { |
| 2884 | if (d.mError == err) { |
| 2885 | // Already saw a similar error, ignore the new one. |
| 2886 | return; |
| 2887 | } |
| 2888 | } |
| 2889 | ErrorDialog errDialog = new ErrorDialog( |
Patrick Scott | 5d61a6c | 2009-08-25 13:52:46 -0400 | [diff] [blame] | 2890 | err == WebViewClient.ERROR_FILE_NOT_FOUND ? |
Patrick Scott | 37911c7 | 2009-03-24 18:02:58 -0700 | [diff] [blame] | 2891 | R.string.browserFrameFileErrorLabel : |
| 2892 | R.string.browserFrameNetworkErrorLabel, |
| 2893 | desc, err); |
| 2894 | mQueuedErrors.addLast(errDialog); |
| 2895 | |
| 2896 | // Show the dialog now if the queue was empty. |
| 2897 | if (mQueuedErrors.size() == 1) { |
| 2898 | showError(errDialog); |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | private void showError(ErrorDialog errDialog) { |
| 2903 | AlertDialog d = new AlertDialog.Builder(BrowserActivity.this) |
| 2904 | .setTitle(errDialog.mTitle) |
| 2905 | .setMessage(errDialog.mDescription) |
| 2906 | .setPositiveButton(R.string.ok, null) |
| 2907 | .create(); |
| 2908 | d.setOnDismissListener(mDialogListener); |
| 2909 | d.show(); |
| 2910 | } |
| 2911 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2912 | /** |
| 2913 | * Show a dialog informing the user of the network error reported by |
| 2914 | * WebCore. |
| 2915 | */ |
| 2916 | @Override |
| 2917 | public void onReceivedError(WebView view, int errorCode, |
| 2918 | String description, String failingUrl) { |
Patrick Scott | 5d61a6c | 2009-08-25 13:52:46 -0400 | [diff] [blame] | 2919 | if (errorCode != WebViewClient.ERROR_HOST_LOOKUP && |
| 2920 | errorCode != WebViewClient.ERROR_CONNECT && |
| 2921 | errorCode != WebViewClient.ERROR_BAD_URL && |
| 2922 | errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME && |
| 2923 | errorCode != WebViewClient.ERROR_FILE) { |
Patrick Scott | 37911c7 | 2009-03-24 18:02:58 -0700 | [diff] [blame] | 2924 | queueError(errorCode, description); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2925 | } |
Patrick Scott | 37911c7 | 2009-03-24 18:02:58 -0700 | [diff] [blame] | 2926 | Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl |
| 2927 | + " " + description); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2928 | |
| 2929 | // We need to reset the title after an error. |
| 2930 | resetTitleAndRevertLockIcon(); |
| 2931 | } |
| 2932 | |
| 2933 | /** |
| 2934 | * Check with the user if it is ok to resend POST data as the page they |
| 2935 | * are trying to navigate to is the result of a POST. |
| 2936 | */ |
| 2937 | @Override |
| 2938 | public void onFormResubmission(WebView view, final Message dontResend, |
| 2939 | final Message resend) { |
| 2940 | new AlertDialog.Builder(BrowserActivity.this) |
| 2941 | .setTitle(R.string.browserFrameFormResubmitLabel) |
| 2942 | .setMessage(R.string.browserFrameFormResubmitMessage) |
| 2943 | .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 2944 | public void onClick(DialogInterface dialog, int which) { |
| 2945 | resend.sendToTarget(); |
| 2946 | }}) |
| 2947 | .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { |
| 2948 | public void onClick(DialogInterface dialog, int which) { |
| 2949 | dontResend.sendToTarget(); |
| 2950 | }}) |
| 2951 | .setOnCancelListener(new OnCancelListener() { |
| 2952 | public void onCancel(DialogInterface dialog) { |
| 2953 | dontResend.sendToTarget(); |
| 2954 | }}) |
| 2955 | .show(); |
| 2956 | } |
| 2957 | |
| 2958 | /** |
| 2959 | * Insert the url into the visited history database. |
| 2960 | * @param url The url to be inserted. |
| 2961 | * @param isReload True if this url is being reloaded. |
| 2962 | * FIXME: Not sure what to do when reloading the page. |
| 2963 | */ |
| 2964 | @Override |
| 2965 | public void doUpdateVisitedHistory(WebView view, String url, |
| 2966 | boolean isReload) { |
| 2967 | if (url.regionMatches(true, 0, "about:", 0, 6)) { |
| 2968 | return; |
| 2969 | } |
Grace Kloba | 6b52a55 | 2009-09-03 16:29:56 -0700 | [diff] [blame] | 2970 | // remove "client" before updating it to the history so that it wont |
| 2971 | // show up in the auto-complete list. |
| 2972 | int index = url.indexOf("client=ms-"); |
| 2973 | if (index > 0 && url.contains(".google.")) { |
| 2974 | int end = url.indexOf('&', index); |
| 2975 | if (end > 0) { |
| 2976 | url = url.substring(0, index-1).concat(url.substring(end)); |
| 2977 | } else { |
| 2978 | url = url.substring(0, index-1); |
| 2979 | } |
| 2980 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2981 | Browser.updateVisitedHistory(mResolver, url, true); |
| 2982 | WebIconDatabase.getInstance().retainIconForPageUrl(url); |
| 2983 | } |
| 2984 | |
| 2985 | /** |
| 2986 | * Displays SSL error(s) dialog to the user. |
| 2987 | */ |
| 2988 | @Override |
| 2989 | public void onReceivedSslError( |
| 2990 | final WebView view, final SslErrorHandler handler, final SslError error) { |
| 2991 | |
| 2992 | if (mSettings.showSecurityWarnings()) { |
| 2993 | final LayoutInflater factory = |
| 2994 | LayoutInflater.from(BrowserActivity.this); |
| 2995 | final View warningsView = |
| 2996 | factory.inflate(R.layout.ssl_warnings, null); |
| 2997 | final LinearLayout placeholder = |
| 2998 | (LinearLayout)warningsView.findViewById(R.id.placeholder); |
| 2999 | |
| 3000 | if (error.hasError(SslError.SSL_UNTRUSTED)) { |
| 3001 | LinearLayout ll = (LinearLayout)factory |
| 3002 | .inflate(R.layout.ssl_warning, null); |
| 3003 | ((TextView)ll.findViewById(R.id.warning)) |
| 3004 | .setText(R.string.ssl_untrusted); |
| 3005 | placeholder.addView(ll); |
| 3006 | } |
| 3007 | |
| 3008 | if (error.hasError(SslError.SSL_IDMISMATCH)) { |
| 3009 | LinearLayout ll = (LinearLayout)factory |
| 3010 | .inflate(R.layout.ssl_warning, null); |
| 3011 | ((TextView)ll.findViewById(R.id.warning)) |
| 3012 | .setText(R.string.ssl_mismatch); |
| 3013 | placeholder.addView(ll); |
| 3014 | } |
| 3015 | |
| 3016 | if (error.hasError(SslError.SSL_EXPIRED)) { |
| 3017 | LinearLayout ll = (LinearLayout)factory |
| 3018 | .inflate(R.layout.ssl_warning, null); |
| 3019 | ((TextView)ll.findViewById(R.id.warning)) |
| 3020 | .setText(R.string.ssl_expired); |
| 3021 | placeholder.addView(ll); |
| 3022 | } |
| 3023 | |
| 3024 | if (error.hasError(SslError.SSL_NOTYETVALID)) { |
| 3025 | LinearLayout ll = (LinearLayout)factory |
| 3026 | .inflate(R.layout.ssl_warning, null); |
| 3027 | ((TextView)ll.findViewById(R.id.warning)) |
| 3028 | .setText(R.string.ssl_not_yet_valid); |
| 3029 | placeholder.addView(ll); |
| 3030 | } |
| 3031 | |
| 3032 | new AlertDialog.Builder(BrowserActivity.this) |
| 3033 | .setTitle(R.string.security_warning) |
| 3034 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 3035 | .setView(warningsView) |
| 3036 | .setPositiveButton(R.string.ssl_continue, |
| 3037 | new DialogInterface.OnClickListener() { |
| 3038 | public void onClick(DialogInterface dialog, int whichButton) { |
| 3039 | handler.proceed(); |
| 3040 | } |
| 3041 | }) |
| 3042 | .setNeutralButton(R.string.view_certificate, |
| 3043 | new DialogInterface.OnClickListener() { |
| 3044 | public void onClick(DialogInterface dialog, int whichButton) { |
| 3045 | showSSLCertificateOnError(view, handler, error); |
| 3046 | } |
| 3047 | }) |
| 3048 | .setNegativeButton(R.string.cancel, |
| 3049 | new DialogInterface.OnClickListener() { |
| 3050 | public void onClick(DialogInterface dialog, int whichButton) { |
| 3051 | handler.cancel(); |
| 3052 | BrowserActivity.this.resetTitleAndRevertLockIcon(); |
| 3053 | } |
| 3054 | }) |
| 3055 | .setOnCancelListener( |
| 3056 | new DialogInterface.OnCancelListener() { |
| 3057 | public void onCancel(DialogInterface dialog) { |
| 3058 | handler.cancel(); |
| 3059 | BrowserActivity.this.resetTitleAndRevertLockIcon(); |
| 3060 | } |
| 3061 | }) |
| 3062 | .show(); |
| 3063 | } else { |
| 3064 | handler.proceed(); |
| 3065 | } |
| 3066 | } |
| 3067 | |
| 3068 | /** |
| 3069 | * Handles an HTTP authentication request. |
| 3070 | * |
| 3071 | * @param handler The authentication handler |
| 3072 | * @param host The host |
| 3073 | * @param realm The realm |
| 3074 | */ |
| 3075 | @Override |
| 3076 | public void onReceivedHttpAuthRequest(WebView view, |
| 3077 | final HttpAuthHandler handler, final String host, final String realm) { |
| 3078 | String username = null; |
| 3079 | String password = null; |
| 3080 | |
| 3081 | boolean reuseHttpAuthUsernamePassword = |
| 3082 | handler.useHttpAuthUsernamePassword(); |
| 3083 | |
| 3084 | if (reuseHttpAuthUsernamePassword && |
| 3085 | (mTabControl.getCurrentWebView() != null)) { |
| 3086 | String[] credentials = |
| 3087 | mTabControl.getCurrentWebView() |
| 3088 | .getHttpAuthUsernamePassword(host, realm); |
| 3089 | if (credentials != null && credentials.length == 2) { |
| 3090 | username = credentials[0]; |
| 3091 | password = credentials[1]; |
| 3092 | } |
| 3093 | } |
| 3094 | |
| 3095 | if (username != null && password != null) { |
| 3096 | handler.proceed(username, password); |
| 3097 | } else { |
| 3098 | showHttpAuthentication(handler, host, realm, null, null, null, 0); |
| 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | @Override |
| 3103 | public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) { |
| 3104 | if (mMenuIsDown) { |
| 3105 | // only check shortcut key when MENU is held |
| 3106 | return getWindow().isShortcutKey(event.getKeyCode(), event); |
| 3107 | } else { |
| 3108 | return false; |
| 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | @Override |
| 3113 | public void onUnhandledKeyEvent(WebView view, KeyEvent event) { |
| 3114 | if (view != mTabControl.getCurrentTopWebView()) { |
| 3115 | return; |
| 3116 | } |
| 3117 | if (event.isDown()) { |
| 3118 | BrowserActivity.this.onKeyDown(event.getKeyCode(), event); |
| 3119 | } else { |
| 3120 | BrowserActivity.this.onKeyUp(event.getKeyCode(), event); |
| 3121 | } |
| 3122 | } |
| 3123 | }; |
| 3124 | |
| 3125 | //-------------------------------------------------------------------------- |
| 3126 | // WebChromeClient implementation |
| 3127 | //-------------------------------------------------------------------------- |
| 3128 | |
| 3129 | /* package */ WebChromeClient getWebChromeClient() { |
| 3130 | return mWebChromeClient; |
| 3131 | } |
| 3132 | |
| 3133 | private final WebChromeClient mWebChromeClient = new WebChromeClient() { |
| 3134 | // Helper method to create a new tab or sub window. |
| 3135 | private void createWindow(final boolean dialog, final Message msg) { |
| 3136 | if (dialog) { |
| 3137 | mTabControl.createSubWindow(); |
| 3138 | final TabControl.Tab t = mTabControl.getCurrentTab(); |
| 3139 | attachSubWindow(t); |
| 3140 | WebView.WebViewTransport transport = |
| 3141 | (WebView.WebViewTransport) msg.obj; |
| 3142 | transport.setWebView(t.getSubWebView()); |
| 3143 | msg.sendToTarget(); |
| 3144 | } else { |
| 3145 | final TabControl.Tab parent = mTabControl.getCurrentTab(); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 3146 | final TabControl.Tab newTab |
| 3147 | = openTabAndShow(EMPTY_URL_DATA, false, null); |
Grace Kloba | c918184 | 2009-04-14 08:53:22 -0700 | [diff] [blame] | 3148 | if (newTab != parent) { |
| 3149 | parent.addChildTab(newTab); |
| 3150 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3151 | WebView.WebViewTransport transport = |
| 3152 | (WebView.WebViewTransport) msg.obj; |
| 3153 | transport.setWebView(mTabControl.getCurrentWebView()); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 3154 | msg.sendToTarget(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | @Override |
| 3159 | public boolean onCreateWindow(WebView view, final boolean dialog, |
| 3160 | final boolean userGesture, final Message resultMsg) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3161 | // Short-circuit if we can't create any more tabs or sub windows. |
| 3162 | if (dialog && mTabControl.getCurrentSubWindow() != null) { |
| 3163 | new AlertDialog.Builder(BrowserActivity.this) |
| 3164 | .setTitle(R.string.too_many_subwindows_dialog_title) |
| 3165 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 3166 | .setMessage(R.string.too_many_subwindows_dialog_message) |
| 3167 | .setPositiveButton(R.string.ok, null) |
| 3168 | .show(); |
| 3169 | return false; |
| 3170 | } else if (mTabControl.getTabCount() >= TabControl.MAX_TABS) { |
| 3171 | new AlertDialog.Builder(BrowserActivity.this) |
| 3172 | .setTitle(R.string.too_many_windows_dialog_title) |
| 3173 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 3174 | .setMessage(R.string.too_many_windows_dialog_message) |
| 3175 | .setPositiveButton(R.string.ok, null) |
| 3176 | .show(); |
| 3177 | return false; |
| 3178 | } |
| 3179 | |
| 3180 | // Short-circuit if this was a user gesture. |
| 3181 | if (userGesture) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3182 | createWindow(dialog, resultMsg); |
| 3183 | return true; |
| 3184 | } |
| 3185 | |
| 3186 | // Allow the popup and create the appropriate window. |
| 3187 | final AlertDialog.OnClickListener allowListener = |
| 3188 | new AlertDialog.OnClickListener() { |
| 3189 | public void onClick(DialogInterface d, |
| 3190 | int which) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3191 | createWindow(dialog, resultMsg); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3192 | } |
| 3193 | }; |
| 3194 | |
| 3195 | // Block the popup by returning a null WebView. |
| 3196 | final AlertDialog.OnClickListener blockListener = |
| 3197 | new AlertDialog.OnClickListener() { |
| 3198 | public void onClick(DialogInterface d, int which) { |
| 3199 | resultMsg.sendToTarget(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3200 | } |
| 3201 | }; |
| 3202 | |
| 3203 | // Build a confirmation dialog to display to the user. |
| 3204 | final AlertDialog d = |
| 3205 | new AlertDialog.Builder(BrowserActivity.this) |
| 3206 | .setTitle(R.string.attention) |
| 3207 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 3208 | .setMessage(R.string.popup_window_attempt) |
| 3209 | .setPositiveButton(R.string.allow, allowListener) |
| 3210 | .setNegativeButton(R.string.block, blockListener) |
| 3211 | .setCancelable(false) |
| 3212 | .create(); |
| 3213 | |
| 3214 | // Show the confirmation dialog. |
| 3215 | d.show(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3216 | return true; |
| 3217 | } |
| 3218 | |
| 3219 | @Override |
| 3220 | public void onCloseWindow(WebView window) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 3221 | final TabControl.Tab current = mTabControl.getCurrentTab(); |
| 3222 | final TabControl.Tab parent = current.getParentTab(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3223 | if (parent != null) { |
| 3224 | // JavaScript can only close popup window. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 3225 | switchToTab(mTabControl.getTabIndex(parent)); |
| 3226 | // Now we need to close the window |
| 3227 | closeTab(current); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3228 | } |
| 3229 | } |
| 3230 | |
| 3231 | @Override |
| 3232 | public void onProgressChanged(WebView view, int newProgress) { |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 3233 | mTitleBar.setProgress(newProgress); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 3234 | if (mFakeTitleBar != null) { |
| 3235 | mFakeTitleBar.setProgress(newProgress); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | if (newProgress == 100) { |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 3239 | // onProgressChanged() may continue to be called after the main |
| 3240 | // frame has finished loading, as any remaining sub frames |
| 3241 | // continue to load. We'll only get called once though with |
| 3242 | // newProgress as 100 when everything is loaded. |
| 3243 | // (onPageFinished is called once when the main frame completes |
| 3244 | // loading regardless of the state of any sub frames so calls |
| 3245 | // to onProgressChanges may continue after onPageFinished has |
| 3246 | // executed) |
| 3247 | |
| 3248 | // sync cookies and cache promptly here. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3249 | CookieSyncManager.getInstance().sync(); |
The Android Open Source Project | cb9a0bb | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 3250 | if (mInLoad) { |
| 3251 | mInLoad = false; |
| 3252 | updateInLoadMenuItems(); |
Leon Scroggins | a27ff19 | 2009-09-14 12:58:04 -0400 | [diff] [blame] | 3253 | // If the options menu is open, leave the title bar |
| 3254 | if (!mOptionsMenuOpen || !mIconView) { |
| 3255 | hideFakeTitleBar(); |
| 3256 | } |
The Android Open Source Project | cb9a0bb | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 3257 | } |
Leon Scroggins | a27ff19 | 2009-09-14 12:58:04 -0400 | [diff] [blame] | 3258 | } else if (!mInLoad) { |
The Android Open Source Project | cb9a0bb | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 3259 | // onPageFinished may have already been called but a subframe |
| 3260 | // is still loading and updating the progress. Reset mInLoad |
| 3261 | // and update the menu items. |
Leon Scroggins | a27ff19 | 2009-09-14 12:58:04 -0400 | [diff] [blame] | 3262 | mInLoad = true; |
| 3263 | updateInLoadMenuItems(); |
Leon Scroggins | 184f5e3 | 2009-09-21 10:38:24 -0400 | [diff] [blame] | 3264 | if (!mOptionsMenuOpen || mIconView) { |
Leon Scroggins | a27ff19 | 2009-09-14 12:58:04 -0400 | [diff] [blame] | 3265 | // This page has begun to load, so show the title bar |
| 3266 | showFakeTitleBar(); |
The Android Open Source Project | cb9a0bb | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 3267 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | @Override |
| 3272 | public void onReceivedTitle(WebView view, String title) { |
Patrick Scott | 598c9cc | 2009-06-04 11:10:38 -0400 | [diff] [blame] | 3273 | String url = view.getUrl(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3274 | |
| 3275 | // here, if url is null, we want to reset the title |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 3276 | setUrlTitle(url, title); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3277 | |
| 3278 | if (url == null || |
| 3279 | url.length() >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) { |
| 3280 | return; |
| 3281 | } |
Leon Scroggins | fce182b | 2009-05-08 13:54:52 -0400 | [diff] [blame] | 3282 | // See if we can find the current url in our history database and |
| 3283 | // add the new title to it. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3284 | if (url.startsWith("http://www.")) { |
| 3285 | url = url.substring(11); |
| 3286 | } else if (url.startsWith("http://")) { |
| 3287 | url = url.substring(4); |
| 3288 | } |
| 3289 | try { |
| 3290 | url = "%" + url; |
| 3291 | String [] selArgs = new String[] { url }; |
| 3292 | |
| 3293 | String where = Browser.BookmarkColumns.URL + " LIKE ? AND " |
| 3294 | + Browser.BookmarkColumns.BOOKMARK + " = 0"; |
| 3295 | Cursor c = mResolver.query(Browser.BOOKMARKS_URI, |
| 3296 | Browser.HISTORY_PROJECTION, where, selArgs, null); |
| 3297 | if (c.moveToFirst()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3298 | // Current implementation of database only has one entry per |
| 3299 | // url. |
Leon Scroggins | fce182b | 2009-05-08 13:54:52 -0400 | [diff] [blame] | 3300 | ContentValues map = new ContentValues(); |
| 3301 | map.put(Browser.BookmarkColumns.TITLE, title); |
| 3302 | mResolver.update(Browser.BOOKMARKS_URI, map, |
| 3303 | "_id = " + c.getInt(0), null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3304 | } |
| 3305 | c.close(); |
| 3306 | } catch (IllegalStateException e) { |
| 3307 | Log.e(LOGTAG, "BrowserActivity onReceived title", e); |
| 3308 | } catch (SQLiteException ex) { |
| 3309 | Log.e(LOGTAG, "onReceivedTitle() caught SQLiteException: ", ex); |
| 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | @Override |
| 3314 | public void onReceivedIcon(WebView view, Bitmap icon) { |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 3315 | updateIcon(view, icon); |
| 3316 | } |
| 3317 | |
| 3318 | @Override |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 3319 | public void onReceivedTouchIconUrl(WebView view, String url, |
| 3320 | boolean precomposed) { |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 3321 | final ContentResolver cr = getContentResolver(); |
| 3322 | final Cursor c = |
| 3323 | BrowserBookmarksAdapter.queryBookmarksForUrl(cr, |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 3324 | view.getOriginalUrl(), view.getUrl(), true); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 3325 | if (c != null) { |
| 3326 | if (c.getCount() > 0) { |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 3327 | // Let precomposed icons take precedence over non-composed |
| 3328 | // icons. |
| 3329 | if (precomposed && mTouchIconLoader != null) { |
| 3330 | mTouchIconLoader.cancel(false); |
| 3331 | mTouchIconLoader = null; |
| 3332 | } |
| 3333 | // Have only one async task at a time. |
| 3334 | if (mTouchIconLoader == null) { |
| 3335 | mTouchIconLoader = new DownloadTouchIcon( |
| 3336 | BrowserActivity.this, cr, c, view); |
| 3337 | mTouchIconLoader.execute(url); |
| 3338 | } |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 3339 | } else { |
| 3340 | c.close(); |
| 3341 | } |
| 3342 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3343 | } |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 3344 | |
Andrei Popescu | adc008d | 2009-06-26 14:11:30 +0100 | [diff] [blame] | 3345 | @Override |
Andrei Popescu | c9b5556 | 2009-07-07 10:51:15 +0100 | [diff] [blame] | 3346 | public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) { |
Andrei Popescu | adc008d | 2009-06-26 14:11:30 +0100 | [diff] [blame] | 3347 | if (mCustomView != null) |
| 3348 | return; |
| 3349 | |
| 3350 | // Add the custom view to its container. |
| 3351 | mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER); |
| 3352 | mCustomView = view; |
Andrei Popescu | c9b5556 | 2009-07-07 10:51:15 +0100 | [diff] [blame] | 3353 | mCustomViewCallback = callback; |
Andrei Popescu | adc008d | 2009-06-26 14:11:30 +0100 | [diff] [blame] | 3354 | // Save the menu state and set it to empty while the custom |
| 3355 | // view is showing. |
| 3356 | mOldMenuState = mMenuState; |
| 3357 | mMenuState = EMPTY_MENU; |
Andrei Popescu | c9b5556 | 2009-07-07 10:51:15 +0100 | [diff] [blame] | 3358 | // Hide the content view. |
| 3359 | mContentView.setVisibility(View.GONE); |
Andrei Popescu | adc008d | 2009-06-26 14:11:30 +0100 | [diff] [blame] | 3360 | // Finally show the custom view container. |
Andrei Popescu | c9b5556 | 2009-07-07 10:51:15 +0100 | [diff] [blame] | 3361 | mCustomViewContainer.setVisibility(View.VISIBLE); |
| 3362 | mCustomViewContainer.bringToFront(); |
Andrei Popescu | adc008d | 2009-06-26 14:11:30 +0100 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | @Override |
| 3366 | public void onHideCustomView() { |
| 3367 | if (mCustomView == null) |
| 3368 | return; |
| 3369 | |
Andrei Popescu | c9b5556 | 2009-07-07 10:51:15 +0100 | [diff] [blame] | 3370 | // Hide the custom view. |
| 3371 | mCustomView.setVisibility(View.GONE); |
Andrei Popescu | adc008d | 2009-06-26 14:11:30 +0100 | [diff] [blame] | 3372 | // Remove the custom view from its container. |
| 3373 | mCustomViewContainer.removeView(mCustomView); |
| 3374 | mCustomView = null; |
| 3375 | // Reset the old menu state. |
| 3376 | mMenuState = mOldMenuState; |
| 3377 | mOldMenuState = EMPTY_MENU; |
| 3378 | mCustomViewContainer.setVisibility(View.GONE); |
Andrei Popescu | c9b5556 | 2009-07-07 10:51:15 +0100 | [diff] [blame] | 3379 | mCustomViewCallback.onCustomViewHidden(); |
| 3380 | // Show the content view. |
| 3381 | mContentView.setVisibility(View.VISIBLE); |
Andrei Popescu | adc008d | 2009-06-26 14:11:30 +0100 | [diff] [blame] | 3382 | } |
| 3383 | |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 3384 | /** |
Andrei Popescu | 79e82b7 | 2009-07-27 12:01:59 +0100 | [diff] [blame] | 3385 | * The origin has exceeded its database quota. |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 3386 | * @param url the URL that exceeded the quota |
| 3387 | * @param databaseIdentifier the identifier of the database on |
| 3388 | * which the transaction that caused the quota overflow was run |
| 3389 | * @param currentQuota the current quota for the origin. |
Ben Murdoch | 25a1523 | 2009-08-25 19:38:07 +0100 | [diff] [blame] | 3390 | * @param estimatedSize the estimated size of the database. |
Andrei Popescu | 79e82b7 | 2009-07-27 12:01:59 +0100 | [diff] [blame] | 3391 | * @param totalUsedQuota is the sum of all origins' quota. |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 3392 | * @param quotaUpdater The callback to run when a decision to allow or |
| 3393 | * deny quota has been made. Don't forget to call this! |
| 3394 | */ |
| 3395 | @Override |
| 3396 | public void onExceededDatabaseQuota(String url, |
Ben Murdoch | 25a1523 | 2009-08-25 19:38:07 +0100 | [diff] [blame] | 3397 | String databaseIdentifier, long currentQuota, long estimatedSize, |
| 3398 | long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { |
Andrei Popescu | 79e82b7 | 2009-07-27 12:01:59 +0100 | [diff] [blame] | 3399 | mSettings.getWebStorageSizeManager().onExceededDatabaseQuota( |
Ben Murdoch | 25a1523 | 2009-08-25 19:38:07 +0100 | [diff] [blame] | 3400 | url, databaseIdentifier, currentQuota, estimatedSize, |
| 3401 | totalUsedQuota, quotaUpdater); |
Andrei Popescu | 79e82b7 | 2009-07-27 12:01:59 +0100 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | /** |
| 3405 | * The Application Cache has exceeded its max size. |
| 3406 | * @param spaceNeeded is the amount of disk space that would be needed |
| 3407 | * in order for the last appcache operation to succeed. |
| 3408 | * @param totalUsedQuota is the sum of all origins' quota. |
| 3409 | * @param quotaUpdater A callback to inform the WebCore thread that a new |
| 3410 | * app cache size is available. This callback must always be executed at |
| 3411 | * some point to ensure that the sleeping WebCore thread is woken up. |
| 3412 | */ |
| 3413 | @Override |
| 3414 | public void onReachedMaxAppCacheSize(long spaceNeeded, |
| 3415 | long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { |
| 3416 | mSettings.getWebStorageSizeManager().onReachedMaxAppCacheSize( |
| 3417 | spaceNeeded, totalUsedQuota, quotaUpdater); |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 3418 | } |
Ben Murdoch | 7db2634 | 2009-06-03 18:21:19 +0100 | [diff] [blame] | 3419 | |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 3420 | /** |
| 3421 | * Instructs the browser to show a prompt to ask the user to set the |
| 3422 | * Geolocation permission state for the specified origin. |
| 3423 | * @param origin The origin for which Geolocation permissions are |
| 3424 | * requested. |
| 3425 | * @param callback The callback to call once the user has set the |
| 3426 | * Geolocation permission state. |
| 3427 | */ |
| 3428 | @Override |
| 3429 | public void onGeolocationPermissionsShowPrompt(String origin, |
| 3430 | GeolocationPermissions.Callback callback) { |
| 3431 | mTabControl.getCurrentTab().getGeolocationPermissionsPrompt().show( |
| 3432 | origin, callback); |
| 3433 | } |
| 3434 | |
| 3435 | /** |
| 3436 | * Instructs the browser to hide the Geolocation permissions prompt. |
| 3437 | */ |
| 3438 | @Override |
| 3439 | public void onGeolocationPermissionsHidePrompt() { |
| 3440 | mTabControl.getCurrentTab().getGeolocationPermissionsPrompt().hide(); |
| 3441 | } |
| 3442 | |
Ben Murdoch | 7db2634 | 2009-06-03 18:21:19 +0100 | [diff] [blame] | 3443 | /* Adds a JavaScript error message to the system log. |
| 3444 | * @param message The error message to report. |
| 3445 | * @param lineNumber The line number of the error. |
| 3446 | * @param sourceID The name of the source file that caused the error. |
| 3447 | */ |
| 3448 | @Override |
| 3449 | public void addMessageToConsole(String message, int lineNumber, String sourceID) { |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 3450 | ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(true); |
| 3451 | errorConsole.addErrorMessage(message, sourceID, lineNumber); |
| 3452 | if (mShouldShowErrorConsole && |
| 3453 | errorConsole.getShowState() != ErrorConsoleView.SHOW_MAXIMIZED) { |
| 3454 | errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED); |
| 3455 | } |
| 3456 | Log.w(LOGTAG, "Console: " + message + " " + sourceID + ":" + lineNumber); |
Ben Murdoch | 7db2634 | 2009-06-03 18:21:19 +0100 | [diff] [blame] | 3457 | } |
Andrei Popescu | 540035d | 2009-09-18 18:59:20 +0100 | [diff] [blame] | 3458 | |
| 3459 | /** |
| 3460 | * Ask the browser for an icon to represent a <video> element. |
| 3461 | * This icon will be used if the Web page did not specify a poster attribute. |
| 3462 | * |
| 3463 | * @return Bitmap The icon or null if no such icon is available. |
| 3464 | * @hide pending API Council approval |
| 3465 | */ |
| 3466 | @Override |
| 3467 | public Bitmap getDefaultVideoPoster() { |
| 3468 | if (mDefaultVideoPoster == null) { |
| 3469 | mDefaultVideoPoster = BitmapFactory.decodeResource( |
| 3470 | getResources(), R.drawable.default_video_poster); |
| 3471 | } |
| 3472 | return mDefaultVideoPoster; |
| 3473 | } |
| 3474 | |
| 3475 | /** |
| 3476 | * Ask the host application for a custom progress view to show while |
| 3477 | * a <video> is loading. |
| 3478 | * |
| 3479 | * @return View The progress view. |
| 3480 | * @hide pending API Council approval |
| 3481 | */ |
| 3482 | @Override |
| 3483 | public View getVideoLoadingProgressView() { |
| 3484 | if (mVideoProgressView == null) { |
| 3485 | LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this); |
| 3486 | mVideoProgressView = inflater.inflate(R.layout.video_loading_progress, null); |
| 3487 | } |
| 3488 | return mVideoProgressView; |
| 3489 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3490 | }; |
| 3491 | |
| 3492 | /** |
| 3493 | * Notify the host application a download should be done, or that |
| 3494 | * the data should be streamed if a streaming viewer is available. |
| 3495 | * @param url The full url to the content that should be downloaded |
| 3496 | * @param contentDisposition Content-disposition http header, if |
| 3497 | * present. |
| 3498 | * @param mimetype The mimetype of the content reported by the server |
| 3499 | * @param contentLength The file size reported by the server |
| 3500 | */ |
| 3501 | public void onDownloadStart(String url, String userAgent, |
| 3502 | String contentDisposition, String mimetype, long contentLength) { |
| 3503 | // if we're dealing wih A/V content that's not explicitly marked |
| 3504 | // for download, check if it's streamable. |
| 3505 | if (contentDisposition == null |
Patrick Scott | e1fb966 | 2009-08-31 14:31:52 -0400 | [diff] [blame] | 3506 | || !contentDisposition.regionMatches( |
| 3507 | true, 0, "attachment", 0, 10)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3508 | // query the package manager to see if there's a registered handler |
| 3509 | // that matches. |
| 3510 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 3511 | intent.setDataAndType(Uri.parse(url), mimetype); |
Patrick Scott | e1fb966 | 2009-08-31 14:31:52 -0400 | [diff] [blame] | 3512 | ResolveInfo info = getPackageManager().resolveActivity(intent, |
| 3513 | PackageManager.MATCH_DEFAULT_ONLY); |
| 3514 | if (info != null) { |
| 3515 | ComponentName myName = getComponentName(); |
| 3516 | // If we resolved to ourselves, we don't want to attempt to |
| 3517 | // load the url only to try and download it again. |
| 3518 | if (!myName.getPackageName().equals( |
| 3519 | info.activityInfo.packageName) |
| 3520 | || !myName.getClassName().equals( |
| 3521 | info.activityInfo.name)) { |
| 3522 | // someone (other than us) knows how to handle this mime |
| 3523 | // type with this scheme, don't download. |
| 3524 | try { |
| 3525 | startActivity(intent); |
| 3526 | return; |
| 3527 | } catch (ActivityNotFoundException ex) { |
| 3528 | if (LOGD_ENABLED) { |
| 3529 | Log.d(LOGTAG, "activity not found for " + mimetype |
| 3530 | + " over " + Uri.parse(url).getScheme(), |
| 3531 | ex); |
| 3532 | } |
| 3533 | // Best behavior is to fall back to a download in this |
| 3534 | // case |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3535 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | } |
| 3539 | onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength); |
| 3540 | } |
| 3541 | |
| 3542 | /** |
| 3543 | * Notify the host application a download should be done, even if there |
| 3544 | * is a streaming viewer available for thise type. |
| 3545 | * @param url The full url to the content that should be downloaded |
| 3546 | * @param contentDisposition Content-disposition http header, if |
| 3547 | * present. |
| 3548 | * @param mimetype The mimetype of the content reported by the server |
| 3549 | * @param contentLength The file size reported by the server |
| 3550 | */ |
| 3551 | /*package */ void onDownloadStartNoStream(String url, String userAgent, |
| 3552 | String contentDisposition, String mimetype, long contentLength) { |
| 3553 | |
| 3554 | String filename = URLUtil.guessFileName(url, |
| 3555 | contentDisposition, mimetype); |
| 3556 | |
| 3557 | // Check to see if we have an SDCard |
| 3558 | String status = Environment.getExternalStorageState(); |
| 3559 | if (!status.equals(Environment.MEDIA_MOUNTED)) { |
| 3560 | int title; |
| 3561 | String msg; |
| 3562 | |
| 3563 | // Check to see if the SDCard is busy, same as the music app |
| 3564 | if (status.equals(Environment.MEDIA_SHARED)) { |
| 3565 | msg = getString(R.string.download_sdcard_busy_dlg_msg); |
| 3566 | title = R.string.download_sdcard_busy_dlg_title; |
| 3567 | } else { |
| 3568 | msg = getString(R.string.download_no_sdcard_dlg_msg, filename); |
| 3569 | title = R.string.download_no_sdcard_dlg_title; |
| 3570 | } |
| 3571 | |
| 3572 | new AlertDialog.Builder(this) |
| 3573 | .setTitle(title) |
| 3574 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 3575 | .setMessage(msg) |
| 3576 | .setPositiveButton(R.string.ok, null) |
| 3577 | .show(); |
| 3578 | return; |
| 3579 | } |
| 3580 | |
| 3581 | // java.net.URI is a lot stricter than KURL so we have to undo |
| 3582 | // KURL's percent-encoding and redo the encoding using java.net.URI. |
| 3583 | URI uri = null; |
| 3584 | try { |
| 3585 | // Undo the percent-encoding that KURL may have done. |
| 3586 | String newUrl = new String(URLUtil.decode(url.getBytes())); |
| 3587 | // Parse the url into pieces |
| 3588 | WebAddress w = new WebAddress(newUrl); |
| 3589 | String frag = null; |
| 3590 | String query = null; |
| 3591 | String path = w.mPath; |
| 3592 | // Break the path into path, query, and fragment |
| 3593 | if (path.length() > 0) { |
| 3594 | // Strip the fragment |
| 3595 | int idx = path.lastIndexOf('#'); |
| 3596 | if (idx != -1) { |
| 3597 | frag = path.substring(idx + 1); |
| 3598 | path = path.substring(0, idx); |
| 3599 | } |
| 3600 | idx = path.lastIndexOf('?'); |
| 3601 | if (idx != -1) { |
| 3602 | query = path.substring(idx + 1); |
| 3603 | path = path.substring(0, idx); |
| 3604 | } |
| 3605 | } |
| 3606 | uri = new URI(w.mScheme, w.mAuthInfo, w.mHost, w.mPort, path, |
| 3607 | query, frag); |
| 3608 | } catch (Exception e) { |
| 3609 | Log.e(LOGTAG, "Could not parse url for download: " + url, e); |
| 3610 | return; |
| 3611 | } |
| 3612 | |
| 3613 | // XXX: Have to use the old url since the cookies were stored using the |
| 3614 | // old percent-encoded url. |
| 3615 | String cookies = CookieManager.getInstance().getCookie(url); |
| 3616 | |
| 3617 | ContentValues values = new ContentValues(); |
Jean-Baptiste Queru | 3dc09b2 | 2009-03-31 16:49:44 -0700 | [diff] [blame] | 3618 | values.put(Downloads.COLUMN_URI, uri.toString()); |
| 3619 | values.put(Downloads.COLUMN_COOKIE_DATA, cookies); |
| 3620 | values.put(Downloads.COLUMN_USER_AGENT, userAgent); |
| 3621 | values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3622 | getPackageName()); |
Jean-Baptiste Queru | 3dc09b2 | 2009-03-31 16:49:44 -0700 | [diff] [blame] | 3623 | values.put(Downloads.COLUMN_NOTIFICATION_CLASS, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3624 | BrowserDownloadPage.class.getCanonicalName()); |
Jean-Baptiste Queru | 3dc09b2 | 2009-03-31 16:49:44 -0700 | [diff] [blame] | 3625 | values.put(Downloads.COLUMN_VISIBILITY, Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); |
| 3626 | values.put(Downloads.COLUMN_MIME_TYPE, mimetype); |
| 3627 | values.put(Downloads.COLUMN_FILE_NAME_HINT, filename); |
| 3628 | values.put(Downloads.COLUMN_DESCRIPTION, uri.getHost()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3629 | if (contentLength > 0) { |
Jean-Baptiste Queru | 3dc09b2 | 2009-03-31 16:49:44 -0700 | [diff] [blame] | 3630 | values.put(Downloads.COLUMN_TOTAL_BYTES, contentLength); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3631 | } |
| 3632 | if (mimetype == null) { |
| 3633 | // We must have long pressed on a link or image to download it. We |
| 3634 | // are not sure of the mimetype in this case, so do a head request |
| 3635 | new FetchUrlMimeType(this).execute(values); |
| 3636 | } else { |
| 3637 | final Uri contentUri = |
| 3638 | getContentResolver().insert(Downloads.CONTENT_URI, values); |
| 3639 | viewDownloads(contentUri); |
| 3640 | } |
| 3641 | |
| 3642 | } |
| 3643 | |
| 3644 | /** |
| 3645 | * Resets the lock icon. This method is called when we start a new load and |
| 3646 | * know the url to be loaded. |
| 3647 | */ |
| 3648 | private void resetLockIcon(String url) { |
| 3649 | // Save the lock-icon state (we revert to it if the load gets cancelled) |
| 3650 | saveLockIcon(); |
| 3651 | |
| 3652 | mLockIconType = LOCK_ICON_UNSECURE; |
| 3653 | if (URLUtil.isHttpsUrl(url)) { |
| 3654 | mLockIconType = LOCK_ICON_SECURE; |
Dave Bort | 31a6d1c | 2009-04-13 15:56:49 -0700 | [diff] [blame] | 3655 | if (LOGV_ENABLED) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3656 | Log.v(LOGTAG, "BrowserActivity.resetLockIcon:" + |
| 3657 | " reset lock icon to " + mLockIconType); |
| 3658 | } |
| 3659 | } |
| 3660 | |
| 3661 | updateLockIconImage(LOCK_ICON_UNSECURE); |
| 3662 | } |
| 3663 | |
Grace Kloba | eb6eef4 | 2009-09-15 17:56:32 -0700 | [diff] [blame] | 3664 | /* package */ void setLockIconType(int type) { |
| 3665 | mLockIconType = type; |
| 3666 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3667 | |
Grace Kloba | eb6eef4 | 2009-09-15 17:56:32 -0700 | [diff] [blame] | 3668 | /* package */ int getLockIconType() { |
| 3669 | return mLockIconType; |
| 3670 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3671 | |
Grace Kloba | eb6eef4 | 2009-09-15 17:56:32 -0700 | [diff] [blame] | 3672 | /* package */ void setPrevLockType(int type) { |
| 3673 | mPrevLockType = type; |
| 3674 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3675 | |
Grace Kloba | eb6eef4 | 2009-09-15 17:56:32 -0700 | [diff] [blame] | 3676 | /* package */ int getPrevLockType() { |
| 3677 | return mPrevLockType; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | /** |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 3681 | * Update the lock icon to correspond to our latest state. |
| 3682 | */ |
| 3683 | /* package */ void updateLockIconToLatest() { |
| 3684 | updateLockIconImage(mLockIconType); |
| 3685 | } |
| 3686 | |
| 3687 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3688 | * Updates the lock-icon image in the title-bar. |
| 3689 | */ |
| 3690 | private void updateLockIconImage(int lockIconType) { |
| 3691 | Drawable d = null; |
| 3692 | if (lockIconType == LOCK_ICON_SECURE) { |
| 3693 | d = mSecLockIcon; |
| 3694 | } else if (lockIconType == LOCK_ICON_MIXED) { |
| 3695 | d = mMixLockIcon; |
| 3696 | } |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 3697 | mTitleBar.setLock(d); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 3698 | if (mFakeTitleBar != null) { |
| 3699 | mFakeTitleBar.setLock(d); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3700 | } |
| 3701 | } |
| 3702 | |
| 3703 | /** |
| 3704 | * Displays a page-info dialog. |
| 3705 | * @param tab The tab to show info about |
| 3706 | * @param fromShowSSLCertificateOnError The flag that indicates whether |
| 3707 | * this dialog was opened from the SSL-certificate-on-error dialog or |
| 3708 | * not. This is important, since we need to know whether to return to |
| 3709 | * the parent dialog or simply dismiss. |
| 3710 | */ |
| 3711 | private void showPageInfo(final TabControl.Tab tab, |
| 3712 | final boolean fromShowSSLCertificateOnError) { |
| 3713 | final LayoutInflater factory = LayoutInflater |
| 3714 | .from(this); |
| 3715 | |
| 3716 | final View pageInfoView = factory.inflate(R.layout.page_info, null); |
| 3717 | |
| 3718 | final WebView view = tab.getWebView(); |
| 3719 | |
| 3720 | String url = null; |
| 3721 | String title = null; |
| 3722 | |
| 3723 | if (view == null) { |
| 3724 | url = tab.getUrl(); |
| 3725 | title = tab.getTitle(); |
| 3726 | } else if (view == mTabControl.getCurrentWebView()) { |
| 3727 | // Use the cached title and url if this is the current WebView |
| 3728 | url = mUrl; |
| 3729 | title = mTitle; |
| 3730 | } else { |
| 3731 | url = view.getUrl(); |
| 3732 | title = view.getTitle(); |
| 3733 | } |
| 3734 | |
| 3735 | if (url == null) { |
| 3736 | url = ""; |
| 3737 | } |
| 3738 | if (title == null) { |
| 3739 | title = ""; |
| 3740 | } |
| 3741 | |
| 3742 | ((TextView) pageInfoView.findViewById(R.id.address)).setText(url); |
| 3743 | ((TextView) pageInfoView.findViewById(R.id.title)).setText(title); |
| 3744 | |
| 3745 | mPageInfoView = tab; |
| 3746 | mPageInfoFromShowSSLCertificateOnError = new Boolean(fromShowSSLCertificateOnError); |
| 3747 | |
| 3748 | AlertDialog.Builder alertDialogBuilder = |
| 3749 | new AlertDialog.Builder(this) |
| 3750 | .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info) |
| 3751 | .setView(pageInfoView) |
| 3752 | .setPositiveButton( |
| 3753 | R.string.ok, |
| 3754 | new DialogInterface.OnClickListener() { |
| 3755 | public void onClick(DialogInterface dialog, |
| 3756 | int whichButton) { |
| 3757 | mPageInfoDialog = null; |
| 3758 | mPageInfoView = null; |
| 3759 | mPageInfoFromShowSSLCertificateOnError = null; |
| 3760 | |
| 3761 | // if we came here from the SSL error dialog |
| 3762 | if (fromShowSSLCertificateOnError) { |
| 3763 | // go back to the SSL error dialog |
| 3764 | showSSLCertificateOnError( |
| 3765 | mSSLCertificateOnErrorView, |
| 3766 | mSSLCertificateOnErrorHandler, |
| 3767 | mSSLCertificateOnErrorError); |
| 3768 | } |
| 3769 | } |
| 3770 | }) |
| 3771 | .setOnCancelListener( |
| 3772 | new DialogInterface.OnCancelListener() { |
| 3773 | public void onCancel(DialogInterface dialog) { |
| 3774 | mPageInfoDialog = null; |
| 3775 | mPageInfoView = null; |
| 3776 | mPageInfoFromShowSSLCertificateOnError = null; |
| 3777 | |
| 3778 | // if we came here from the SSL error dialog |
| 3779 | if (fromShowSSLCertificateOnError) { |
| 3780 | // go back to the SSL error dialog |
| 3781 | showSSLCertificateOnError( |
| 3782 | mSSLCertificateOnErrorView, |
| 3783 | mSSLCertificateOnErrorHandler, |
| 3784 | mSSLCertificateOnErrorError); |
| 3785 | } |
| 3786 | } |
| 3787 | }); |
| 3788 | |
| 3789 | // if we have a main top-level page SSL certificate set or a certificate |
| 3790 | // error |
| 3791 | if (fromShowSSLCertificateOnError || |
| 3792 | (view != null && view.getCertificate() != null)) { |
| 3793 | // add a 'View Certificate' button |
| 3794 | alertDialogBuilder.setNeutralButton( |
| 3795 | R.string.view_certificate, |
| 3796 | new DialogInterface.OnClickListener() { |
| 3797 | public void onClick(DialogInterface dialog, |
| 3798 | int whichButton) { |
| 3799 | mPageInfoDialog = null; |
| 3800 | mPageInfoView = null; |
| 3801 | mPageInfoFromShowSSLCertificateOnError = null; |
| 3802 | |
| 3803 | // if we came here from the SSL error dialog |
| 3804 | if (fromShowSSLCertificateOnError) { |
| 3805 | // go back to the SSL error dialog |
| 3806 | showSSLCertificateOnError( |
| 3807 | mSSLCertificateOnErrorView, |
| 3808 | mSSLCertificateOnErrorHandler, |
| 3809 | mSSLCertificateOnErrorError); |
| 3810 | } else { |
| 3811 | // otherwise, display the top-most certificate from |
| 3812 | // the chain |
| 3813 | if (view.getCertificate() != null) { |
| 3814 | showSSLCertificate(tab); |
| 3815 | } |
| 3816 | } |
| 3817 | } |
| 3818 | }); |
| 3819 | } |
| 3820 | |
| 3821 | mPageInfoDialog = alertDialogBuilder.show(); |
| 3822 | } |
| 3823 | |
| 3824 | /** |
| 3825 | * Displays the main top-level page SSL certificate dialog |
| 3826 | * (accessible from the Page-Info dialog). |
| 3827 | * @param tab The tab to show certificate for. |
| 3828 | */ |
| 3829 | private void showSSLCertificate(final TabControl.Tab tab) { |
| 3830 | final View certificateView = |
| 3831 | inflateCertificateView(tab.getWebView().getCertificate()); |
| 3832 | if (certificateView == null) { |
| 3833 | return; |
| 3834 | } |
| 3835 | |
| 3836 | LayoutInflater factory = LayoutInflater.from(this); |
| 3837 | |
| 3838 | final LinearLayout placeholder = |
| 3839 | (LinearLayout)certificateView.findViewById(R.id.placeholder); |
| 3840 | |
| 3841 | LinearLayout ll = (LinearLayout) factory.inflate( |
| 3842 | R.layout.ssl_success, placeholder); |
| 3843 | ((TextView)ll.findViewById(R.id.success)) |
| 3844 | .setText(R.string.ssl_certificate_is_valid); |
| 3845 | |
| 3846 | mSSLCertificateView = tab; |
| 3847 | mSSLCertificateDialog = |
| 3848 | new AlertDialog.Builder(this) |
| 3849 | .setTitle(R.string.ssl_certificate).setIcon( |
| 3850 | R.drawable.ic_dialog_browser_certificate_secure) |
| 3851 | .setView(certificateView) |
| 3852 | .setPositiveButton(R.string.ok, |
| 3853 | new DialogInterface.OnClickListener() { |
| 3854 | public void onClick(DialogInterface dialog, |
| 3855 | int whichButton) { |
| 3856 | mSSLCertificateDialog = null; |
| 3857 | mSSLCertificateView = null; |
| 3858 | |
| 3859 | showPageInfo(tab, false); |
| 3860 | } |
| 3861 | }) |
| 3862 | .setOnCancelListener( |
| 3863 | new DialogInterface.OnCancelListener() { |
| 3864 | public void onCancel(DialogInterface dialog) { |
| 3865 | mSSLCertificateDialog = null; |
| 3866 | mSSLCertificateView = null; |
| 3867 | |
| 3868 | showPageInfo(tab, false); |
| 3869 | } |
| 3870 | }) |
| 3871 | .show(); |
| 3872 | } |
| 3873 | |
| 3874 | /** |
| 3875 | * Displays the SSL error certificate dialog. |
| 3876 | * @param view The target web-view. |
| 3877 | * @param handler The SSL error handler responsible for cancelling the |
| 3878 | * connection that resulted in an SSL error or proceeding per user request. |
| 3879 | * @param error The SSL error object. |
| 3880 | */ |
| 3881 | private void showSSLCertificateOnError( |
| 3882 | final WebView view, final SslErrorHandler handler, final SslError error) { |
| 3883 | |
| 3884 | final View certificateView = |
| 3885 | inflateCertificateView(error.getCertificate()); |
| 3886 | if (certificateView == null) { |
| 3887 | return; |
| 3888 | } |
| 3889 | |
| 3890 | LayoutInflater factory = LayoutInflater.from(this); |
| 3891 | |
| 3892 | final LinearLayout placeholder = |
| 3893 | (LinearLayout)certificateView.findViewById(R.id.placeholder); |
| 3894 | |
| 3895 | if (error.hasError(SslError.SSL_UNTRUSTED)) { |
| 3896 | LinearLayout ll = (LinearLayout)factory |
| 3897 | .inflate(R.layout.ssl_warning, placeholder); |
| 3898 | ((TextView)ll.findViewById(R.id.warning)) |
| 3899 | .setText(R.string.ssl_untrusted); |
| 3900 | } |
| 3901 | |
| 3902 | if (error.hasError(SslError.SSL_IDMISMATCH)) { |
| 3903 | LinearLayout ll = (LinearLayout)factory |
| 3904 | .inflate(R.layout.ssl_warning, placeholder); |
| 3905 | ((TextView)ll.findViewById(R.id.warning)) |
| 3906 | .setText(R.string.ssl_mismatch); |
| 3907 | } |
| 3908 | |
| 3909 | if (error.hasError(SslError.SSL_EXPIRED)) { |
| 3910 | LinearLayout ll = (LinearLayout)factory |
| 3911 | .inflate(R.layout.ssl_warning, placeholder); |
| 3912 | ((TextView)ll.findViewById(R.id.warning)) |
| 3913 | .setText(R.string.ssl_expired); |
| 3914 | } |
| 3915 | |
| 3916 | if (error.hasError(SslError.SSL_NOTYETVALID)) { |
| 3917 | LinearLayout ll = (LinearLayout)factory |
| 3918 | .inflate(R.layout.ssl_warning, placeholder); |
| 3919 | ((TextView)ll.findViewById(R.id.warning)) |
| 3920 | .setText(R.string.ssl_not_yet_valid); |
| 3921 | } |
| 3922 | |
| 3923 | mSSLCertificateOnErrorHandler = handler; |
| 3924 | mSSLCertificateOnErrorView = view; |
| 3925 | mSSLCertificateOnErrorError = error; |
| 3926 | mSSLCertificateOnErrorDialog = |
| 3927 | new AlertDialog.Builder(this) |
| 3928 | .setTitle(R.string.ssl_certificate).setIcon( |
| 3929 | R.drawable.ic_dialog_browser_certificate_partially_secure) |
| 3930 | .setView(certificateView) |
| 3931 | .setPositiveButton(R.string.ok, |
| 3932 | new DialogInterface.OnClickListener() { |
| 3933 | public void onClick(DialogInterface dialog, |
| 3934 | int whichButton) { |
| 3935 | mSSLCertificateOnErrorDialog = null; |
| 3936 | mSSLCertificateOnErrorView = null; |
| 3937 | mSSLCertificateOnErrorHandler = null; |
| 3938 | mSSLCertificateOnErrorError = null; |
| 3939 | |
| 3940 | mWebViewClient.onReceivedSslError( |
| 3941 | view, handler, error); |
| 3942 | } |
| 3943 | }) |
| 3944 | .setNeutralButton(R.string.page_info_view, |
| 3945 | new DialogInterface.OnClickListener() { |
| 3946 | public void onClick(DialogInterface dialog, |
| 3947 | int whichButton) { |
| 3948 | mSSLCertificateOnErrorDialog = null; |
| 3949 | |
| 3950 | // do not clear the dialog state: we will |
| 3951 | // need to show the dialog again once the |
| 3952 | // user is done exploring the page-info details |
| 3953 | |
| 3954 | showPageInfo(mTabControl.getTabFromView(view), |
| 3955 | true); |
| 3956 | } |
| 3957 | }) |
| 3958 | .setOnCancelListener( |
| 3959 | new DialogInterface.OnCancelListener() { |
| 3960 | public void onCancel(DialogInterface dialog) { |
| 3961 | mSSLCertificateOnErrorDialog = null; |
| 3962 | mSSLCertificateOnErrorView = null; |
| 3963 | mSSLCertificateOnErrorHandler = null; |
| 3964 | mSSLCertificateOnErrorError = null; |
| 3965 | |
| 3966 | mWebViewClient.onReceivedSslError( |
| 3967 | view, handler, error); |
| 3968 | } |
| 3969 | }) |
| 3970 | .show(); |
| 3971 | } |
| 3972 | |
| 3973 | /** |
| 3974 | * Inflates the SSL certificate view (helper method). |
| 3975 | * @param certificate The SSL certificate. |
| 3976 | * @return The resultant certificate view with issued-to, issued-by, |
| 3977 | * issued-on, expires-on, and possibly other fields set. |
| 3978 | * If the input certificate is null, returns null. |
| 3979 | */ |
| 3980 | private View inflateCertificateView(SslCertificate certificate) { |
| 3981 | if (certificate == null) { |
| 3982 | return null; |
| 3983 | } |
| 3984 | |
| 3985 | LayoutInflater factory = LayoutInflater.from(this); |
| 3986 | |
| 3987 | View certificateView = factory.inflate( |
| 3988 | R.layout.ssl_certificate, null); |
| 3989 | |
| 3990 | // issued to: |
| 3991 | SslCertificate.DName issuedTo = certificate.getIssuedTo(); |
| 3992 | if (issuedTo != null) { |
| 3993 | ((TextView) certificateView.findViewById(R.id.to_common)) |
| 3994 | .setText(issuedTo.getCName()); |
| 3995 | ((TextView) certificateView.findViewById(R.id.to_org)) |
| 3996 | .setText(issuedTo.getOName()); |
| 3997 | ((TextView) certificateView.findViewById(R.id.to_org_unit)) |
| 3998 | .setText(issuedTo.getUName()); |
| 3999 | } |
| 4000 | |
| 4001 | // issued by: |
| 4002 | SslCertificate.DName issuedBy = certificate.getIssuedBy(); |
| 4003 | if (issuedBy != null) { |
| 4004 | ((TextView) certificateView.findViewById(R.id.by_common)) |
| 4005 | .setText(issuedBy.getCName()); |
| 4006 | ((TextView) certificateView.findViewById(R.id.by_org)) |
| 4007 | .setText(issuedBy.getOName()); |
| 4008 | ((TextView) certificateView.findViewById(R.id.by_org_unit)) |
| 4009 | .setText(issuedBy.getUName()); |
| 4010 | } |
| 4011 | |
| 4012 | // issued on: |
| 4013 | String issuedOn = reformatCertificateDate( |
| 4014 | certificate.getValidNotBefore()); |
| 4015 | ((TextView) certificateView.findViewById(R.id.issued_on)) |
| 4016 | .setText(issuedOn); |
| 4017 | |
| 4018 | // expires on: |
| 4019 | String expiresOn = reformatCertificateDate( |
| 4020 | certificate.getValidNotAfter()); |
| 4021 | ((TextView) certificateView.findViewById(R.id.expires_on)) |
| 4022 | .setText(expiresOn); |
| 4023 | |
| 4024 | return certificateView; |
| 4025 | } |
| 4026 | |
| 4027 | /** |
| 4028 | * Re-formats the certificate date (Date.toString()) string to |
| 4029 | * a properly localized date string. |
| 4030 | * @return Properly localized version of the certificate date string and |
| 4031 | * the original certificate date string if fails to localize. |
| 4032 | * If the original string is null, returns an empty string "". |
| 4033 | */ |
| 4034 | private String reformatCertificateDate(String certificateDate) { |
| 4035 | String reformattedDate = null; |
| 4036 | |
| 4037 | if (certificateDate != null) { |
| 4038 | Date date = null; |
| 4039 | try { |
| 4040 | date = java.text.DateFormat.getInstance().parse(certificateDate); |
| 4041 | } catch (ParseException e) { |
| 4042 | date = null; |
| 4043 | } |
| 4044 | |
| 4045 | if (date != null) { |
| 4046 | reformattedDate = |
| 4047 | DateFormat.getDateFormat(this).format(date); |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | return reformattedDate != null ? reformattedDate : |
| 4052 | (certificateDate != null ? certificateDate : ""); |
| 4053 | } |
| 4054 | |
| 4055 | /** |
| 4056 | * Displays an http-authentication dialog. |
| 4057 | */ |
| 4058 | private void showHttpAuthentication(final HttpAuthHandler handler, |
| 4059 | final String host, final String realm, final String title, |
| 4060 | final String name, final String password, int focusId) { |
| 4061 | LayoutInflater factory = LayoutInflater.from(this); |
| 4062 | final View v = factory |
| 4063 | .inflate(R.layout.http_authentication, null); |
| 4064 | if (name != null) { |
| 4065 | ((EditText) v.findViewById(R.id.username_edit)).setText(name); |
| 4066 | } |
| 4067 | if (password != null) { |
| 4068 | ((EditText) v.findViewById(R.id.password_edit)).setText(password); |
| 4069 | } |
| 4070 | |
| 4071 | String titleText = title; |
| 4072 | if (titleText == null) { |
| 4073 | titleText = getText(R.string.sign_in_to).toString().replace( |
| 4074 | "%s1", host).replace("%s2", realm); |
| 4075 | } |
| 4076 | |
| 4077 | mHttpAuthHandler = handler; |
| 4078 | AlertDialog dialog = new AlertDialog.Builder(this) |
| 4079 | .setTitle(titleText) |
| 4080 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 4081 | .setView(v) |
| 4082 | .setPositiveButton(R.string.action, |
| 4083 | new DialogInterface.OnClickListener() { |
| 4084 | public void onClick(DialogInterface dialog, |
| 4085 | int whichButton) { |
| 4086 | String nm = ((EditText) v |
| 4087 | .findViewById(R.id.username_edit)) |
| 4088 | .getText().toString(); |
| 4089 | String pw = ((EditText) v |
| 4090 | .findViewById(R.id.password_edit)) |
| 4091 | .getText().toString(); |
| 4092 | BrowserActivity.this.setHttpAuthUsernamePassword |
| 4093 | (host, realm, nm, pw); |
| 4094 | handler.proceed(nm, pw); |
| 4095 | mHttpAuthenticationDialog = null; |
| 4096 | mHttpAuthHandler = null; |
| 4097 | }}) |
| 4098 | .setNegativeButton(R.string.cancel, |
| 4099 | new DialogInterface.OnClickListener() { |
| 4100 | public void onClick(DialogInterface dialog, |
| 4101 | int whichButton) { |
| 4102 | handler.cancel(); |
| 4103 | BrowserActivity.this.resetTitleAndRevertLockIcon(); |
| 4104 | mHttpAuthenticationDialog = null; |
| 4105 | mHttpAuthHandler = null; |
| 4106 | }}) |
| 4107 | .setOnCancelListener(new DialogInterface.OnCancelListener() { |
| 4108 | public void onCancel(DialogInterface dialog) { |
| 4109 | handler.cancel(); |
| 4110 | BrowserActivity.this.resetTitleAndRevertLockIcon(); |
| 4111 | mHttpAuthenticationDialog = null; |
| 4112 | mHttpAuthHandler = null; |
| 4113 | }}) |
| 4114 | .create(); |
| 4115 | // Make the IME appear when the dialog is displayed if applicable. |
| 4116 | dialog.getWindow().setSoftInputMode( |
| 4117 | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); |
| 4118 | dialog.show(); |
| 4119 | if (focusId != 0) { |
| 4120 | dialog.findViewById(focusId).requestFocus(); |
| 4121 | } else { |
| 4122 | v.findViewById(R.id.username_edit).requestFocus(); |
| 4123 | } |
| 4124 | mHttpAuthenticationDialog = dialog; |
| 4125 | } |
| 4126 | |
| 4127 | public int getProgress() { |
| 4128 | WebView w = mTabControl.getCurrentWebView(); |
| 4129 | if (w != null) { |
| 4130 | return w.getProgress(); |
| 4131 | } else { |
| 4132 | return 100; |
| 4133 | } |
| 4134 | } |
| 4135 | |
| 4136 | /** |
| 4137 | * Set HTTP authentication password. |
| 4138 | * |
| 4139 | * @param host The host for the password |
| 4140 | * @param realm The realm for the password |
| 4141 | * @param username The username for the password. If it is null, it means |
| 4142 | * password can't be saved. |
| 4143 | * @param password The password |
| 4144 | */ |
| 4145 | public void setHttpAuthUsernamePassword(String host, String realm, |
| 4146 | String username, |
| 4147 | String password) { |
| 4148 | WebView w = mTabControl.getCurrentWebView(); |
| 4149 | if (w != null) { |
| 4150 | w.setHttpAuthUsernamePassword(host, realm, username, password); |
| 4151 | } |
| 4152 | } |
| 4153 | |
| 4154 | /** |
| 4155 | * connectivity manager says net has come or gone... inform the user |
| 4156 | * @param up true if net has come up, false if net has gone down |
| 4157 | */ |
| 4158 | public void onNetworkToggle(boolean up) { |
| 4159 | if (up == mIsNetworkUp) { |
| 4160 | return; |
| 4161 | } else if (up) { |
| 4162 | mIsNetworkUp = true; |
| 4163 | if (mAlertDialog != null) { |
| 4164 | mAlertDialog.cancel(); |
| 4165 | mAlertDialog = null; |
| 4166 | } |
| 4167 | } else { |
| 4168 | mIsNetworkUp = false; |
Patrick Scott | eb6ab2a | 2009-09-16 10:00:17 -0400 | [diff] [blame] | 4169 | if (mInLoad) { |
| 4170 | createAndShowNetworkDialog(); |
| 4171 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4172 | } |
| 4173 | WebView w = mTabControl.getCurrentWebView(); |
| 4174 | if (w != null) { |
| 4175 | w.setNetworkAvailable(up); |
| 4176 | } |
| 4177 | } |
| 4178 | |
Patrick Scott | eb6ab2a | 2009-09-16 10:00:17 -0400 | [diff] [blame] | 4179 | // This method shows the network dialog alerting the user that the net is |
| 4180 | // down. It will only show the dialog if mAlertDialog is null. |
| 4181 | private void createAndShowNetworkDialog() { |
| 4182 | if (mAlertDialog == null) { |
| 4183 | mAlertDialog = new AlertDialog.Builder(this) |
| 4184 | .setTitle(R.string.loadSuspendedTitle) |
| 4185 | .setMessage(R.string.loadSuspended) |
| 4186 | .setPositiveButton(R.string.ok, null) |
| 4187 | .show(); |
| 4188 | } |
| 4189 | } |
| 4190 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4191 | @Override |
| 4192 | protected void onActivityResult(int requestCode, int resultCode, |
| 4193 | Intent intent) { |
| 4194 | switch (requestCode) { |
| 4195 | case COMBO_PAGE: |
| 4196 | if (resultCode == RESULT_OK && intent != null) { |
| 4197 | String data = intent.getAction(); |
| 4198 | Bundle extras = intent.getExtras(); |
| 4199 | if (extras != null && extras.getBoolean("new_window", false)) { |
Leon Scroggins | 25d3547 | 2009-09-15 11:37:27 -0400 | [diff] [blame] | 4200 | openTab(data); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4201 | } else { |
| 4202 | final TabControl.Tab currentTab = |
| 4203 | mTabControl.getCurrentTab(); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 4204 | dismissSubWindow(currentTab); |
| 4205 | if (data != null && data.length() != 0) { |
| 4206 | getTopWindow().loadUrl(data); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4207 | } |
| 4208 | } |
| 4209 | } |
| 4210 | break; |
| 4211 | default: |
| 4212 | break; |
| 4213 | } |
Leon Scroggins | 3044423 | 2009-09-04 18:36:20 -0400 | [diff] [blame] | 4214 | getTopWindow().requestFocus(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | /* |
| 4218 | * This method is called as a result of the user selecting the options |
| 4219 | * menu to see the download window, or when a download changes state. It |
| 4220 | * shows the download window ontop of the current window. |
| 4221 | */ |
| 4222 | /* package */ void viewDownloads(Uri downloadRecord) { |
| 4223 | Intent intent = new Intent(this, |
| 4224 | BrowserDownloadPage.class); |
| 4225 | intent.setData(downloadRecord); |
| 4226 | startActivityForResult(intent, this.DOWNLOAD_PAGE); |
| 4227 | |
| 4228 | } |
| 4229 | |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 4230 | /** |
| 4231 | * Open the Go page. |
| 4232 | * @param startWithHistory If true, open starting on the history tab. |
| 4233 | * Otherwise, start with the bookmarks tab. |
Leon Scroggins | 160a7e7 | 2009-08-14 18:28:01 -0400 | [diff] [blame] | 4234 | */ |
Leon Scroggins | 3044423 | 2009-09-04 18:36:20 -0400 | [diff] [blame] | 4235 | /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4236 | WebView current = mTabControl.getCurrentWebView(); |
| 4237 | if (current == null) { |
| 4238 | return; |
| 4239 | } |
| 4240 | Intent intent = new Intent(this, |
| 4241 | CombinedBookmarkHistoryActivity.class); |
| 4242 | String title = current.getTitle(); |
| 4243 | String url = current.getUrl(); |
Ben Murdoch | dcc2b6f | 2009-09-21 14:29:20 +0100 | [diff] [blame] | 4244 | Bitmap thumbnail = createScreenshot(current); |
| 4245 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4246 | // Just in case the user opens bookmarks before a page finishes loading |
| 4247 | // so the current history item, and therefore the page, is null. |
| 4248 | if (null == url) { |
| 4249 | url = mLastEnteredUrl; |
| 4250 | // This can happen. |
| 4251 | if (null == url) { |
| 4252 | url = mSettings.getHomePage(); |
| 4253 | } |
| 4254 | } |
| 4255 | // In case the web page has not yet received its associated title. |
| 4256 | if (title == null) { |
| 4257 | title = url; |
| 4258 | } |
| 4259 | intent.putExtra("title", title); |
| 4260 | intent.putExtra("url", url); |
Ben Murdoch | dcc2b6f | 2009-09-21 14:29:20 +0100 | [diff] [blame] | 4261 | intent.putExtra("thumbnail", thumbnail); |
Leon Scroggins | 3044423 | 2009-09-04 18:36:20 -0400 | [diff] [blame] | 4262 | // Disable opening in a new window if we have maxed out the windows |
| 4263 | intent.putExtra("disable_new_window", mTabControl.getTabCount() |
| 4264 | >= TabControl.MAX_TABS); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 4265 | intent.putExtra("touch_icon_url", current.getTouchIconUrl()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4266 | if (startWithHistory) { |
| 4267 | intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB, |
| 4268 | CombinedBookmarkHistoryActivity.HISTORY_TAB); |
| 4269 | } |
| 4270 | startActivityForResult(intent, COMBO_PAGE); |
| 4271 | } |
| 4272 | |
| 4273 | // Called when loading from context menu or LOAD_URL message |
| 4274 | private void loadURL(WebView view, String url) { |
| 4275 | // In case the user enters nothing. |
| 4276 | if (url != null && url.length() != 0 && view != null) { |
| 4277 | url = smartUrlFilter(url); |
| 4278 | if (!mWebViewClient.shouldOverrideUrlLoading(view, url)) { |
| 4279 | view.loadUrl(url); |
| 4280 | } |
| 4281 | } |
| 4282 | } |
| 4283 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4284 | private String smartUrlFilter(Uri inUri) { |
| 4285 | if (inUri != null) { |
| 4286 | return smartUrlFilter(inUri.toString()); |
| 4287 | } |
| 4288 | return null; |
| 4289 | } |
| 4290 | |
| 4291 | |
| 4292 | // get window count |
| 4293 | |
| 4294 | int getWindowCount(){ |
| 4295 | if(mTabControl != null){ |
| 4296 | return mTabControl.getTabCount(); |
| 4297 | } |
| 4298 | return 0; |
| 4299 | } |
| 4300 | |
Feng Qian | b34f87a | 2009-03-24 21:27:26 -0700 | [diff] [blame] | 4301 | protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile( |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4302 | "(?i)" + // switch on case insensitive matching |
| 4303 | "(" + // begin group for schema |
| 4304 | "(?:http|https|file):\\/\\/" + |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 4305 | "|(?:inline|data|about|content|javascript):" + |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4306 | ")" + |
| 4307 | "(.*)" ); |
| 4308 | |
| 4309 | /** |
| 4310 | * Attempts to determine whether user input is a URL or search |
| 4311 | * terms. Anything with a space is passed to search. |
| 4312 | * |
| 4313 | * Converts to lowercase any mistakenly uppercased schema (i.e., |
| 4314 | * "Http://" converts to "http://" |
| 4315 | * |
| 4316 | * @return Original or modified URL |
| 4317 | * |
| 4318 | */ |
| 4319 | String smartUrlFilter(String url) { |
| 4320 | |
| 4321 | String inUrl = url.trim(); |
| 4322 | boolean hasSpace = inUrl.indexOf(' ') != -1; |
| 4323 | |
| 4324 | Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl); |
| 4325 | if (matcher.matches()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4326 | // force scheme to lowercase |
| 4327 | String scheme = matcher.group(1); |
| 4328 | String lcScheme = scheme.toLowerCase(); |
| 4329 | if (!lcScheme.equals(scheme)) { |
Mitsuru Oshima | 123ecfb | 2009-05-18 19:11:14 -0700 | [diff] [blame] | 4330 | inUrl = lcScheme + matcher.group(2); |
| 4331 | } |
| 4332 | if (hasSpace) { |
| 4333 | inUrl = inUrl.replace(" ", "%20"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4334 | } |
| 4335 | return inUrl; |
| 4336 | } |
| 4337 | if (hasSpace) { |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 4338 | // FIXME: Is this the correct place to add to searches? |
| 4339 | // what if someone else calls this function? |
| 4340 | int shortcut = parseUrlShortcut(inUrl); |
| 4341 | if (shortcut != SHORTCUT_INVALID) { |
| 4342 | Browser.addSearchUrl(mResolver, inUrl); |
| 4343 | String query = inUrl.substring(2); |
| 4344 | switch (shortcut) { |
| 4345 | case SHORTCUT_GOOGLE_SEARCH: |
Grace Kloba | 47fdfdb | 2009-06-30 11:15:34 -0700 | [diff] [blame] | 4346 | return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER); |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 4347 | case SHORTCUT_WIKIPEDIA_SEARCH: |
| 4348 | return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER); |
| 4349 | case SHORTCUT_DICTIONARY_SEARCH: |
| 4350 | return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER); |
| 4351 | case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH: |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4352 | // FIXME: we need location in this case |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 4353 | return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4354 | } |
| 4355 | } |
| 4356 | } else { |
| 4357 | if (Regex.WEB_URL_PATTERN.matcher(inUrl).matches()) { |
| 4358 | return URLUtil.guessUrl(inUrl); |
| 4359 | } |
| 4360 | } |
| 4361 | |
| 4362 | Browser.addSearchUrl(mResolver, inUrl); |
Grace Kloba | 47fdfdb | 2009-06-30 11:15:34 -0700 | [diff] [blame] | 4363 | return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4364 | } |
| 4365 | |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 4366 | /* package */ void setShouldShowErrorConsole(boolean flag) { |
| 4367 | if (flag == mShouldShowErrorConsole) { |
| 4368 | // Nothing to do. |
| 4369 | return; |
| 4370 | } |
| 4371 | |
| 4372 | mShouldShowErrorConsole = flag; |
| 4373 | |
| 4374 | ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(true); |
| 4375 | |
| 4376 | if (flag) { |
| 4377 | // Setting the show state of the console will cause it's the layout to be inflated. |
| 4378 | if (errorConsole.numberOfErrors() > 0) { |
| 4379 | errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED); |
| 4380 | } else { |
| 4381 | errorConsole.showConsole(ErrorConsoleView.SHOW_NONE); |
| 4382 | } |
| 4383 | |
| 4384 | // Now we can add it to the main view. |
| 4385 | mErrorConsoleContainer.addView(errorConsole, |
| 4386 | new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, |
| 4387 | ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 4388 | } else { |
| 4389 | mErrorConsoleContainer.removeView(errorConsole); |
| 4390 | } |
| 4391 | |
| 4392 | } |
| 4393 | |
Grace Kloba | eb6eef4 | 2009-09-15 17:56:32 -0700 | [diff] [blame] | 4394 | final static int LOCK_ICON_UNSECURE = 0; |
| 4395 | final static int LOCK_ICON_SECURE = 1; |
| 4396 | final static int LOCK_ICON_MIXED = 2; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4397 | |
| 4398 | private int mLockIconType = LOCK_ICON_UNSECURE; |
| 4399 | private int mPrevLockType = LOCK_ICON_UNSECURE; |
| 4400 | |
| 4401 | private BrowserSettings mSettings; |
| 4402 | private TabControl mTabControl; |
| 4403 | private ContentResolver mResolver; |
| 4404 | private FrameLayout mContentView; |
Andrei Popescu | adc008d | 2009-06-26 14:11:30 +0100 | [diff] [blame] | 4405 | private View mCustomView; |
| 4406 | private FrameLayout mCustomViewContainer; |
Andrei Popescu | c9b5556 | 2009-07-07 10:51:15 +0100 | [diff] [blame] | 4407 | private WebChromeClient.CustomViewCallback mCustomViewCallback; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4408 | |
| 4409 | // FIXME, temp address onPrepareMenu performance problem. When we move everything out of |
| 4410 | // view, we should rewrite this. |
| 4411 | private int mCurrentMenuState = 0; |
| 4412 | private int mMenuState = R.id.MAIN_MENU; |
Andrei Popescu | adc008d | 2009-06-26 14:11:30 +0100 | [diff] [blame] | 4413 | private int mOldMenuState = EMPTY_MENU; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4414 | private static final int EMPTY_MENU = -1; |
| 4415 | private Menu mMenu; |
| 4416 | |
| 4417 | private FindDialog mFindDialog; |
| 4418 | // Used to prevent chording to result in firing two shortcuts immediately |
| 4419 | // one after another. Fixes bug 1211714. |
| 4420 | boolean mCanChord; |
| 4421 | |
| 4422 | private boolean mInLoad; |
| 4423 | private boolean mIsNetworkUp; |
Ben Murdoch | b7cc8b4 | 2009-09-28 10:59:09 +0100 | [diff] [blame] | 4424 | private boolean mDidStopLoad; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4425 | |
| 4426 | private boolean mPageStarted; |
| 4427 | private boolean mActivityInPause = true; |
| 4428 | |
| 4429 | private boolean mMenuIsDown; |
| 4430 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4431 | private static boolean mInTrace; |
| 4432 | |
| 4433 | // Performance probe |
| 4434 | private static final int[] SYSTEM_CPU_FORMAT = new int[] { |
| 4435 | Process.PROC_SPACE_TERM | Process.PROC_COMBINE, |
| 4436 | Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time |
| 4437 | Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time |
| 4438 | Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time |
| 4439 | Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time |
| 4440 | Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time |
| 4441 | Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time |
| 4442 | Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time |
| 4443 | }; |
| 4444 | |
| 4445 | private long mStart; |
| 4446 | private long mProcessStart; |
| 4447 | private long mUserStart; |
| 4448 | private long mSystemStart; |
| 4449 | private long mIdleStart; |
| 4450 | private long mIrqStart; |
| 4451 | |
| 4452 | private long mUiStart; |
| 4453 | |
| 4454 | private Drawable mMixLockIcon; |
| 4455 | private Drawable mSecLockIcon; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4456 | |
| 4457 | /* hold a ref so we can auto-cancel if necessary */ |
| 4458 | private AlertDialog mAlertDialog; |
| 4459 | |
| 4460 | // Wait for credentials before loading google.com |
| 4461 | private ProgressDialog mCredsDlg; |
| 4462 | |
| 4463 | // The up-to-date URL and title (these can be different from those stored |
| 4464 | // in WebView, since it takes some time for the information in WebView to |
| 4465 | // get updated) |
| 4466 | private String mUrl; |
| 4467 | private String mTitle; |
| 4468 | |
| 4469 | // As PageInfo has different style for landscape / portrait, we have |
| 4470 | // to re-open it when configuration changed |
| 4471 | private AlertDialog mPageInfoDialog; |
| 4472 | private TabControl.Tab mPageInfoView; |
| 4473 | // If the Page-Info dialog is launched from the SSL-certificate-on-error |
| 4474 | // dialog, we should not just dismiss it, but should get back to the |
| 4475 | // SSL-certificate-on-error dialog. This flag is used to store this state |
| 4476 | private Boolean mPageInfoFromShowSSLCertificateOnError; |
| 4477 | |
| 4478 | // as SSLCertificateOnError has different style for landscape / portrait, |
| 4479 | // we have to re-open it when configuration changed |
| 4480 | private AlertDialog mSSLCertificateOnErrorDialog; |
| 4481 | private WebView mSSLCertificateOnErrorView; |
| 4482 | private SslErrorHandler mSSLCertificateOnErrorHandler; |
| 4483 | private SslError mSSLCertificateOnErrorError; |
| 4484 | |
| 4485 | // as SSLCertificate has different style for landscape / portrait, we |
| 4486 | // have to re-open it when configuration changed |
| 4487 | private AlertDialog mSSLCertificateDialog; |
| 4488 | private TabControl.Tab mSSLCertificateView; |
| 4489 | |
| 4490 | // as HttpAuthentication has different style for landscape / portrait, we |
| 4491 | // have to re-open it when configuration changed |
| 4492 | private AlertDialog mHttpAuthenticationDialog; |
| 4493 | private HttpAuthHandler mHttpAuthHandler; |
| 4494 | |
| 4495 | /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = |
| 4496 | new FrameLayout.LayoutParams( |
| 4497 | ViewGroup.LayoutParams.FILL_PARENT, |
| 4498 | ViewGroup.LayoutParams.FILL_PARENT); |
Andrei Popescu | adc008d | 2009-06-26 14:11:30 +0100 | [diff] [blame] | 4499 | /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = |
| 4500 | new FrameLayout.LayoutParams( |
| 4501 | ViewGroup.LayoutParams.FILL_PARENT, |
| 4502 | ViewGroup.LayoutParams.FILL_PARENT, |
| 4503 | Gravity.CENTER); |
Grace Kloba | 47fdfdb | 2009-06-30 11:15:34 -0700 | [diff] [blame] | 4504 | // Google search |
| 4505 | final static String QuickSearch_G = "http://www.google.com/m?q=%s"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4506 | // Wikipedia search |
| 4507 | final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go"; |
| 4508 | // Dictionary search |
| 4509 | final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s"; |
| 4510 | // Google Mobile Local search |
| 4511 | final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view"; |
| 4512 | |
| 4513 | final static String QUERY_PLACE_HOLDER = "%s"; |
| 4514 | |
| 4515 | // "source" parameter for Google search through search key |
| 4516 | final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key"; |
| 4517 | // "source" parameter for Google search through goto menu |
| 4518 | final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto"; |
| 4519 | // "source" parameter for Google search through simplily type |
| 4520 | final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type"; |
| 4521 | // "source" parameter for Google search suggested by the browser |
| 4522 | final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest"; |
| 4523 | // "source" parameter for Google search from unknown source |
| 4524 | final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown"; |
| 4525 | |
| 4526 | private final static String LOGTAG = "browser"; |
| 4527 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4528 | private String mLastEnteredUrl; |
| 4529 | |
| 4530 | private PowerManager.WakeLock mWakeLock; |
| 4531 | private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes |
| 4532 | |
| 4533 | private Toast mStopToast; |
| 4534 | |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 4535 | private TitleBar mTitleBar; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 4536 | |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 4537 | private LinearLayout mErrorConsoleContainer = null; |
| 4538 | private boolean mShouldShowErrorConsole = false; |
| 4539 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4540 | // As the ids are dynamically created, we can't guarantee that they will |
| 4541 | // be in sequence, so this static array maps ids to a window number. |
| 4542 | final static private int[] WINDOW_SHORTCUT_ID_ARRAY = |
| 4543 | { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id, |
| 4544 | R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id, |
| 4545 | R.id.window_seven_menu_id, R.id.window_eight_menu_id }; |
| 4546 | |
| 4547 | // monitor platform changes |
| 4548 | private IntentFilter mNetworkStateChangedFilter; |
| 4549 | private BroadcastReceiver mNetworkStateIntentReceiver; |
| 4550 | |
Grace Kloba | b4da0ad | 2009-05-14 14:45:40 -0700 | [diff] [blame] | 4551 | private BroadcastReceiver mPackageInstallationReceiver; |
| 4552 | |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 4553 | // AsyncTask for downloading touch icons |
| 4554 | /* package */ DownloadTouchIcon mTouchIconLoader; |
| 4555 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4556 | // activity requestCode |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 4557 | final static int COMBO_PAGE = 1; |
| 4558 | final static int DOWNLOAD_PAGE = 2; |
| 4559 | final static int PREFERENCES_PAGE = 3; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4560 | |
Andrei Popescu | 540035d | 2009-09-18 18:59:20 +0100 | [diff] [blame] | 4561 | // the default <video> poster |
| 4562 | private Bitmap mDefaultVideoPoster; |
| 4563 | // the video progress view |
| 4564 | private View mVideoProgressView; |
| 4565 | |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 4566 | /** |
| 4567 | * A UrlData class to abstract how the content will be set to WebView. |
| 4568 | * This base class uses loadUrl to show the content. |
| 4569 | */ |
| 4570 | private static class UrlData { |
| 4571 | String mUrl; |
Grace Kloba | 60e095c | 2009-06-16 11:50:55 -0700 | [diff] [blame] | 4572 | byte[] mPostData; |
| 4573 | |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 4574 | UrlData(String url) { |
| 4575 | this.mUrl = url; |
| 4576 | } |
Grace Kloba | 60e095c | 2009-06-16 11:50:55 -0700 | [diff] [blame] | 4577 | |
| 4578 | void setPostData(byte[] postData) { |
| 4579 | mPostData = postData; |
| 4580 | } |
| 4581 | |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 4582 | boolean isEmpty() { |
| 4583 | return mUrl == null || mUrl.length() == 0; |
| 4584 | } |
| 4585 | |
Mitsuru Oshima | 7944b7d | 2009-06-16 16:34:51 -0700 | [diff] [blame] | 4586 | public void loadIn(WebView webView) { |
Grace Kloba | 60e095c | 2009-06-16 11:50:55 -0700 | [diff] [blame] | 4587 | if (mPostData != null) { |
| 4588 | webView.postUrl(mUrl, mPostData); |
| 4589 | } else { |
| 4590 | webView.loadUrl(mUrl); |
| 4591 | } |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 4592 | } |
| 4593 | }; |
| 4594 | |
| 4595 | /** |
| 4596 | * A subclass of UrlData class that can display inlined content using |
| 4597 | * {@link WebView#loadDataWithBaseURL(String, String, String, String, String)}. |
| 4598 | */ |
| 4599 | private static class InlinedUrlData extends UrlData { |
| 4600 | InlinedUrlData(String inlined, String mimeType, String encoding, String failUrl) { |
| 4601 | super(failUrl); |
| 4602 | mInlined = inlined; |
| 4603 | mMimeType = mimeType; |
| 4604 | mEncoding = encoding; |
| 4605 | } |
| 4606 | String mMimeType; |
| 4607 | String mInlined; |
| 4608 | String mEncoding; |
Mitsuru Oshima | 7944b7d | 2009-06-16 16:34:51 -0700 | [diff] [blame] | 4609 | @Override |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 4610 | boolean isEmpty() { |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 4611 | return mInlined == null || mInlined.length() == 0 || super.isEmpty(); |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 4612 | } |
| 4613 | |
Mitsuru Oshima | 7944b7d | 2009-06-16 16:34:51 -0700 | [diff] [blame] | 4614 | @Override |
| 4615 | public void loadIn(WebView webView) { |
Mitsuru Oshima | 25ad8ab | 2009-06-10 16:26:07 -0700 | [diff] [blame] | 4616 | webView.loadDataWithBaseURL(null, mInlined, mMimeType, mEncoding, mUrl); |
| 4617 | } |
| 4618 | } |
| 4619 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 4620 | /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 4621 | } |