2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.browser;
19 import android.content.ContentValues;
20 import android.net.Uri;
21 import android.net.http.AndroidHttpClient;
23 import org.apache.http.HttpResponse;
24 import org.apache.http.Header;
25 import org.apache.http.client.methods.HttpHead;
27 import java.io.IOException;
29 import android.os.AsyncTask;
30 import android.provider.Downloads;
31 import android.webkit.MimeTypeMap;
32 import android.webkit.URLUtil;
35 * This class is used to pull down the http headers of a given URL so that
36 * we can analyse the mimetype and make any correction needed before we give
37 * the URL to the download manager. The ContentValues class holds the
38 * content that would be provided to the download manager, so that on
39 * completion of checking the mimetype, we can issue the download to
40 * the download manager.
41 * This operation is needed when the user long-clicks on a link or image and
42 * we don't know the mimetype. If the user just clicks on the link, we will
43 * do the same steps of correcting the mimetype down in
44 * android.os.webkit.LoadListener rather than handling it here.
47 class FetchUrlMimeType extends AsyncTask<ContentValues, String, String> {
49 BrowserActivity mActivity;
50 ContentValues mValues;
52 public FetchUrlMimeType(BrowserActivity activity) {
57 public String doInBackground(ContentValues... values) {
60 // Check to make sure we have a URI to download
61 String uri = mValues.getAsString(Downloads.URI);
62 if (uri == null || uri.length() == 0) {
66 // User agent is likely to be null, though the AndroidHttpClient
67 // seems ok with that.
68 AndroidHttpClient client = AndroidHttpClient.newInstance(
69 mValues.getAsString(Downloads.USER_AGENT));
70 HttpHead request = new HttpHead(uri);
72 String cookie = mValues.getAsString(Downloads.COOKIE_DATA);
73 if (cookie != null && cookie.length() > 0) {
74 request.addHeader("Cookie", cookie);
77 String referer = mValues.getAsString(Downloads.REFERER);
78 if (referer != null && referer.length() > 0) {
79 request.addHeader("Referer", referer);
82 HttpResponse response;
83 Boolean succeeded = true;
84 String mimeType = null;
86 response = client.execute(request);
87 // We could get a redirect here, but if we do lets let
88 // the download manager take care of it, and thus trust that
89 // the server sends the right mimetype
90 if (response.getStatusLine().getStatusCode() == 200) {
91 Header header = response.getFirstHeader("Content-Type");
93 mimeType = header.getValue();
94 final int semicolonIndex = mimeType.indexOf(';');
95 if (semicolonIndex != -1) {
96 mimeType = mimeType.substring(0, semicolonIndex);
100 } catch (IllegalArgumentException ex) {
102 } catch (IOException ex) {
112 public void onPostExecute(String mimeType) {
113 if (mimeType != null) {
114 String url = mValues.getAsString(Downloads.URI);
115 if (mimeType.equalsIgnoreCase("text/plain") ||
116 mimeType.equalsIgnoreCase("application/octet-stream")) {
118 MimeTypeMap.getSingleton().getMimeTypeFromExtension(
119 MimeTypeMap.getFileExtensionFromUrl(url));
120 if (newMimeType != null) {
121 mValues.put(Downloads.MIMETYPE, newMimeType);
124 String filename = URLUtil.guessFileName(url,
126 mValues.put(Downloads.FILENAME_HINT, filename);
129 // Start the download
130 final Uri contentUri =
131 mActivity.getContentResolver().insert(Downloads.CONTENT_URI, mValues);
132 mActivity.viewDownloads(contentUri);