Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.app.DownloadManager; |
| 22 | import android.content.ActivityNotFoundException; |
| 23 | import android.content.ComponentName; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.net.Uri; |
| 29 | import android.net.WebAddress; |
| 30 | import android.os.Environment; |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 31 | import android.support.v4.app.ActivityCompat; |
| 32 | import android.support.v4.content.ContextCompat; |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 33 | import android.text.TextUtils; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 34 | import android.util.Log; |
| 35 | import android.webkit.CookieManager; |
| 36 | import android.webkit.URLUtil; |
| 37 | import android.widget.Toast; |
| 38 | |
| 39 | /** |
| 40 | * Handle download requests |
| 41 | */ |
| 42 | public class DownloadHandler { |
| 43 | |
| 44 | private static final boolean LOGD_ENABLED = |
| 45 | com.android.browser.Browser.LOGD_ENABLED; |
| 46 | |
| 47 | private static final String LOGTAG = "DLHandler"; |
| 48 | |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 49 | //Singleton to hold the information about any pending downloads |
| 50 | private static PendingDownloadBundle pendingDownloadBundle; |
| 51 | |
| 52 | private static class PendingDownloadBundle { |
| 53 | String url; |
| 54 | String userAgent; |
| 55 | String contentDisposition; |
| 56 | String mimetype; |
| 57 | String referer; |
| 58 | boolean privateBrowsing; |
| 59 | |
| 60 | public static PendingDownloadBundle create(String url, String userAgent, |
| 61 | String contentDisposition, String mimetype, String referer, |
| 62 | boolean privateBrowsing) { |
| 63 | PendingDownloadBundle pdb = new PendingDownloadBundle(); |
| 64 | pdb.url = url; |
| 65 | pdb.userAgent = userAgent; |
| 66 | pdb.contentDisposition = contentDisposition; |
| 67 | pdb.mimetype = mimetype; |
| 68 | pdb.referer = referer; |
| 69 | pdb.privateBrowsing = privateBrowsing; |
| 70 | return pdb; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Check if there is any pending download and start the download automatically in case |
| 76 | * there is one. |
| 77 | * @param activity Activity requesting the download. |
| 78 | */ |
| 79 | public static void checkPendingDownloads(Activity activity) { |
| 80 | if (pendingDownloadBundle != null) { |
| 81 | onDownloadStartNoStream(activity, pendingDownloadBundle.url, |
| 82 | pendingDownloadBundle.userAgent, pendingDownloadBundle.contentDisposition, |
| 83 | pendingDownloadBundle.mimetype, pendingDownloadBundle.referer, |
| 84 | pendingDownloadBundle.privateBrowsing); |
| 85 | pendingDownloadBundle = null; |
| 86 | } |
| 87 | } |
| 88 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Notify the host application a download should be done, or that |
| 91 | * the data should be streamed if a streaming viewer is available. |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 92 | * @param activity Activity requesting the download. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 93 | * @param url The full url to the content that should be downloaded |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 94 | * @param userAgent User agent of the downloading application. |
| 95 | * @param contentDisposition Content-disposition http header, if present. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 96 | * @param mimetype The mimetype of the content reported by the server |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 97 | * @param referer The referer associated with the downloaded url |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 98 | * @param privateBrowsing If the request is coming from a private browsing tab. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 99 | */ |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 100 | public static void onDownloadStart(Activity activity, String url, |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 101 | String userAgent, String contentDisposition, String mimetype, |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 102 | String referer, boolean privateBrowsing) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 103 | // if we're dealing wih A/V content that's not explicitly marked |
| 104 | // for download, check if it's streamable. |
| 105 | if (contentDisposition == null |
| 106 | || !contentDisposition.regionMatches( |
| 107 | true, 0, "attachment", 0, 10)) { |
| 108 | // query the package manager to see if there's a registered handler |
| 109 | // that matches. |
| 110 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 111 | intent.setDataAndType(Uri.parse(url), mimetype); |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 112 | ResolveInfo info = activity.getPackageManager().resolveActivity(intent, |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 113 | PackageManager.MATCH_DEFAULT_ONLY); |
| 114 | if (info != null) { |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 115 | ComponentName myName = activity.getComponentName(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 116 | // If we resolved to ourselves, we don't want to attempt to |
| 117 | // load the url only to try and download it again. |
| 118 | if (!myName.getPackageName().equals( |
| 119 | info.activityInfo.packageName) |
| 120 | || !myName.getClassName().equals( |
| 121 | info.activityInfo.name)) { |
| 122 | // someone (other than us) knows how to handle this mime |
| 123 | // type with this scheme, don't download. |
| 124 | try { |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 125 | activity.startActivity(intent); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 126 | return; |
| 127 | } catch (ActivityNotFoundException ex) { |
| 128 | if (LOGD_ENABLED) { |
| 129 | Log.d(LOGTAG, "activity not found for " + mimetype |
| 130 | + " over " + Uri.parse(url).getScheme(), |
| 131 | ex); |
| 132 | } |
| 133 | // Best behavior is to fall back to a download in this |
| 134 | // case |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 139 | onDownloadStartNoStream(activity, url, userAgent, contentDisposition, |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 140 | mimetype, referer, privateBrowsing); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // This is to work around the fact that java.net.URI throws Exceptions |
| 144 | // instead of just encoding URL's properly |
| 145 | // Helper method for onDownloadStartNoStream |
| 146 | private static String encodePath(String path) { |
| 147 | char[] chars = path.toCharArray(); |
| 148 | |
| 149 | boolean needed = false; |
| 150 | for (char c : chars) { |
Selim Gurun | a770f8d | 2012-06-13 14:51:23 -0700 | [diff] [blame] | 151 | if (c == '[' || c == ']' || c == '|') { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 152 | needed = true; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | if (needed == false) { |
| 157 | return path; |
| 158 | } |
| 159 | |
| 160 | StringBuilder sb = new StringBuilder(""); |
| 161 | for (char c : chars) { |
Selim Gurun | a770f8d | 2012-06-13 14:51:23 -0700 | [diff] [blame] | 162 | if (c == '[' || c == ']' || c == '|') { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 163 | sb.append('%'); |
| 164 | sb.append(Integer.toHexString(c)); |
| 165 | } else { |
| 166 | sb.append(c); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return sb.toString(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Notify the host application a download should be done, even if there |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 175 | * is a streaming viewer available for this type. |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 176 | * @param activity Activity requesting the download. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 177 | * @param url The full url to the content that should be downloaded |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 178 | * @param userAgent User agent of the downloading application. |
| 179 | * @param contentDisposition Content-disposition http header, if present. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 180 | * @param mimetype The mimetype of the content reported by the server |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 181 | * @param referer The referer associated with the downloaded url |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 182 | * @param privateBrowsing If the request is coming from a private browsing tab. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 183 | */ |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 184 | /*package */ static void onDownloadStartNoStream(Activity activity, |
| 185 | String url, String userAgent, String contentDisposition, |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 186 | String mimetype, String referer, boolean privateBrowsing) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 187 | |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 188 | // Check permissions first when download will be start. |
| 189 | int permissionCheck = ContextCompat.checkSelfPermission(activity, |
| 190 | android.Manifest.permission.WRITE_EXTERNAL_STORAGE); |
| 191 | if (permissionCheck == PackageManager.PERMISSION_GRANTED) { |
| 192 | onDownloadNoStreamImpl(activity, url, userAgent, contentDisposition, |
| 193 | mimetype, referer, privateBrowsing); |
| 194 | } else { |
| 195 | pendingDownloadBundle = PendingDownloadBundle.create(url, userAgent, |
| 196 | contentDisposition, mimetype, referer, privateBrowsing); |
| 197 | // Permission not granted, request it from the user |
| 198 | ActivityCompat.requestPermissions(activity, |
| 199 | new String[] {android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, |
| 200 | BrowserActivity.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private static void onDownloadNoStreamImpl(Activity activity, |
| 205 | String url, String userAgent, String contentDisposition, |
| 206 | String mimetype, String referer, boolean privateBrowsing) { |
| 207 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 208 | String filename = URLUtil.guessFileName(url, |
| 209 | contentDisposition, mimetype); |
| 210 | |
| 211 | // Check to see if we have an SDCard |
| 212 | String status = Environment.getExternalStorageState(); |
| 213 | if (!status.equals(Environment.MEDIA_MOUNTED)) { |
| 214 | int title; |
| 215 | String msg; |
| 216 | |
| 217 | // Check to see if the SDCard is busy, same as the music app |
| 218 | if (status.equals(Environment.MEDIA_SHARED)) { |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 219 | msg = activity.getString(R.string.download_sdcard_busy_dlg_msg); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 220 | title = R.string.download_sdcard_busy_dlg_title; |
| 221 | } else { |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 222 | msg = activity.getString(R.string.download_no_sdcard_dlg_msg, filename); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 223 | title = R.string.download_no_sdcard_dlg_title; |
| 224 | } |
| 225 | |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 226 | new AlertDialog.Builder(activity) |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 227 | .setTitle(title) |
Björn Lundén | 2aa8ba2 | 2012-05-31 23:05:56 +0200 | [diff] [blame] | 228 | .setIconAttribute(android.R.attr.alertDialogIcon) |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 229 | .setMessage(msg) |
| 230 | .setPositiveButton(R.string.ok, null) |
| 231 | .show(); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | // java.net.URI is a lot stricter than KURL so we have to encode some |
| 236 | // extra characters. Fix for b 2538060 and b 1634719 |
| 237 | WebAddress webAddress; |
| 238 | try { |
| 239 | webAddress = new WebAddress(url); |
| 240 | webAddress.setPath(encodePath(webAddress.getPath())); |
| 241 | } catch (Exception e) { |
| 242 | // This only happens for very bad urls, we want to chatch the |
| 243 | // exception here |
| 244 | Log.e(LOGTAG, "Exception trying to parse url:" + url); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | String addressString = webAddress.toString(); |
| 249 | Uri uri = Uri.parse(addressString); |
Leon Scroggins | 11e309c | 2011-02-01 13:37:14 -0500 | [diff] [blame] | 250 | final DownloadManager.Request request; |
| 251 | try { |
| 252 | request = new DownloadManager.Request(uri); |
| 253 | } catch (IllegalArgumentException e) { |
| 254 | Toast.makeText(activity, R.string.cannot_download, Toast.LENGTH_SHORT).show(); |
| 255 | return; |
| 256 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 257 | request.setMimeType(mimetype); |
Vasu Nori | c2df834 | 2010-12-18 20:15:46 -0800 | [diff] [blame] | 258 | // set downloaded file destination to /sdcard/Download. |
| 259 | // or, should it be set to one of several Environment.DIRECTORY* dirs depending on mimetype? |
Filip Matusiak | c499ca9 | 2012-08-23 15:05:05 +0200 | [diff] [blame] | 260 | try { |
| 261 | request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); |
| 262 | } catch (IllegalStateException ex) { |
| 263 | // This only happens when directory Downloads can't be created or it isn't a directory |
| 264 | // this is most commonly due to temporary problems with sdcard so show appropriate string |
| 265 | Log.w(LOGTAG, "Exception trying to create Download dir:", ex); |
| 266 | Toast.makeText(activity, R.string.download_sdcard_busy_dlg_title, |
| 267 | Toast.LENGTH_SHORT).show(); |
| 268 | return; |
| 269 | } |
| 270 | // let this downloaded file be scanned by MediaScanner - so that it can |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 271 | // show up in Gallery app, for example. |
| 272 | request.allowScanningByMediaScanner(); |
| 273 | request.setDescription(webAddress.getHost()); |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 274 | // XXX: Have to use the old url since the cookies were stored using the |
| 275 | // old percent-encoded url. |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 276 | String cookies = CookieManager.getInstance().getCookie(url, privateBrowsing); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 277 | request.addRequestHeader("cookie", cookies); |
Patrik Stenkilsson | d0fc589 | 2012-02-14 09:33:24 +0100 | [diff] [blame] | 278 | request.addRequestHeader("User-Agent", userAgent); |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 279 | request.addRequestHeader("Referer", referer); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 280 | request.setNotificationVisibility( |
| 281 | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); |
| 282 | if (mimetype == null) { |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 283 | if (TextUtils.isEmpty(addressString)) { |
| 284 | return; |
| 285 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 286 | // We must have long pressed on a link or image to download it. We |
| 287 | // are not sure of the mimetype in this case, so do a head request |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 288 | new FetchUrlMimeType(activity, request, addressString, cookies, |
| 289 | userAgent).start(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 290 | } else { |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 291 | final DownloadManager manager |
| 292 | = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE); |
| 293 | new Thread("Browser download") { |
| 294 | public void run() { |
| 295 | manager.enqueue(request); |
| 296 | } |
| 297 | }.start(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 298 | } |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 299 | Toast.makeText(activity, R.string.download_pending, Toast.LENGTH_SHORT) |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 300 | .show(); |
| 301 | } |
| 302 | |
| 303 | } |