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