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