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