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