blob: b833bd7ccb2bd790754b48adef5ab430adf2f5a4 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
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
17package com.android.browser;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.DownloadManager;
22import android.content.ActivityNotFoundException;
23import android.content.ComponentName;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
29import android.net.Uri;
30import android.net.WebAddress;
31import android.os.Environment;
32import android.util.Log;
33import android.webkit.CookieManager;
34import android.webkit.URLUtil;
35import android.widget.Toast;
36
37/**
38 * Handle download requests
39 */
40public class DownloadHandler {
41
42 private static final boolean LOGD_ENABLED =
43 com.android.browser.Browser.LOGD_ENABLED;
44
45 private static final String LOGTAG = "DLHandler";
46
47 Activity mActivity;
48
49 public DownloadHandler(Activity activity) {
50 mActivity = activity;
51 }
52
53 /**
54 * Notify the host application a download should be done, or that
55 * the data should be streamed if a streaming viewer is available.
56 * @param url The full url to the content that should be downloaded
57 * @param contentDisposition Content-disposition http header, if
58 * present.
59 * @param mimetype The mimetype of the content reported by the server
60 * @param contentLength The file size reported by the server
61 */
62 public void onDownloadStart(String url, String userAgent,
63 String contentDisposition, String mimetype, long contentLength) {
64 // if we're dealing wih A/V content that's not explicitly marked
65 // for download, check if it's streamable.
66 if (contentDisposition == null
67 || !contentDisposition.regionMatches(
68 true, 0, "attachment", 0, 10)) {
69 // query the package manager to see if there's a registered handler
70 // that matches.
71 Intent intent = new Intent(Intent.ACTION_VIEW);
72 intent.setDataAndType(Uri.parse(url), mimetype);
73 ResolveInfo info = mActivity.getPackageManager().resolveActivity(intent,
74 PackageManager.MATCH_DEFAULT_ONLY);
75 if (info != null) {
76 ComponentName myName = mActivity.getComponentName();
77 // If we resolved to ourselves, we don't want to attempt to
78 // load the url only to try and download it again.
79 if (!myName.getPackageName().equals(
80 info.activityInfo.packageName)
81 || !myName.getClassName().equals(
82 info.activityInfo.name)) {
83 // someone (other than us) knows how to handle this mime
84 // type with this scheme, don't download.
85 try {
86 mActivity.startActivity(intent);
87 return;
88 } catch (ActivityNotFoundException ex) {
89 if (LOGD_ENABLED) {
90 Log.d(LOGTAG, "activity not found for " + mimetype
91 + " over " + Uri.parse(url).getScheme(),
92 ex);
93 }
94 // Best behavior is to fall back to a download in this
95 // case
96 }
97 }
98 }
99 }
100 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
101 }
102
103 // This is to work around the fact that java.net.URI throws Exceptions
104 // instead of just encoding URL's properly
105 // Helper method for onDownloadStartNoStream
106 private static String encodePath(String path) {
107 char[] chars = path.toCharArray();
108
109 boolean needed = false;
110 for (char c : chars) {
111 if (c == '[' || c == ']') {
112 needed = true;
113 break;
114 }
115 }
116 if (needed == false) {
117 return path;
118 }
119
120 StringBuilder sb = new StringBuilder("");
121 for (char c : chars) {
122 if (c == '[' || c == ']') {
123 sb.append('%');
124 sb.append(Integer.toHexString(c));
125 } else {
126 sb.append(c);
127 }
128 }
129
130 return sb.toString();
131 }
132
133 /**
134 * Notify the host application a download should be done, even if there
135 * is a streaming viewer available for thise type.
136 * @param url The full url to the content that should be downloaded
137 * @param contentDisposition Content-disposition http header, if
138 * present.
139 * @param mimetype The mimetype of the content reported by the server
140 * @param contentLength The file size reported by the server
141 */
142 /*package */ void onDownloadStartNoStream(String url, String userAgent,
143 String contentDisposition, String mimetype, long contentLength) {
144
145 String filename = URLUtil.guessFileName(url,
146 contentDisposition, mimetype);
147
148 // Check to see if we have an SDCard
149 String status = Environment.getExternalStorageState();
150 if (!status.equals(Environment.MEDIA_MOUNTED)) {
151 int title;
152 String msg;
153
154 // Check to see if the SDCard is busy, same as the music app
155 if (status.equals(Environment.MEDIA_SHARED)) {
156 msg = mActivity.getString(R.string.download_sdcard_busy_dlg_msg);
157 title = R.string.download_sdcard_busy_dlg_title;
158 } else {
159 msg = mActivity.getString(R.string.download_no_sdcard_dlg_msg, filename);
160 title = R.string.download_no_sdcard_dlg_title;
161 }
162
163 new AlertDialog.Builder(mActivity)
164 .setTitle(title)
165 .setIcon(android.R.drawable.ic_dialog_alert)
166 .setMessage(msg)
167 .setPositiveButton(R.string.ok, null)
168 .show();
169 return;
170 }
171
172 // java.net.URI is a lot stricter than KURL so we have to encode some
173 // extra characters. Fix for b 2538060 and b 1634719
174 WebAddress webAddress;
175 try {
176 webAddress = new WebAddress(url);
177 webAddress.setPath(encodePath(webAddress.getPath()));
178 } catch (Exception e) {
179 // This only happens for very bad urls, we want to chatch the
180 // exception here
181 Log.e(LOGTAG, "Exception trying to parse url:" + url);
182 return;
183 }
184
185 String addressString = webAddress.toString();
186 Uri uri = Uri.parse(addressString);
187 DownloadManager.Request request = new DownloadManager.Request(uri);
188 request.setMimeType(mimetype);
189 request.setDestinationInExternalFilesDir(mActivity, null, filename);
190 // let this downloaded file be scanned by MediaScanner - so that it can
191 // show up in Gallery app, for example.
192 request.allowScanningByMediaScanner();
193 request.setDescription(webAddress.getHost());
194 String cookies = CookieManager.getInstance().getCookie(url);
195 request.addRequestHeader("cookie", cookies);
196 request.setNotificationVisibility(
197 DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
198 if (mimetype == null) {
199 ContentValues values = new ContentValues();
200 values.put(FetchUrlMimeType.URI, addressString);
201 // XXX: Have to use the old url since the cookies were stored using the
202 // old percent-encoded url.
203 values.put(FetchUrlMimeType.COOKIE_DATA, cookies);
204 values.put(FetchUrlMimeType.USER_AGENT, userAgent);
205
206 // We must have long pressed on a link or image to download it. We
207 // are not sure of the mimetype in this case, so do a head request
208 new FetchUrlMimeType(mActivity, request).execute(values);
209 } else {
210 DownloadManager manager
211 = (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
212 manager.enqueue(request);
213 }
214 Toast.makeText(mActivity, R.string.download_pending, Toast.LENGTH_SHORT)
215 .show();
216 }
217
218}