FetchUrlMimeTypepublic class FetchUrlMimeType extends android.os.AsyncTask This class is used to pull down the http headers of a given URL so that
we can analyse the mimetype and make any correction needed before we give
the URL to the download manager. The ContentValues class holds the
content that would be provided to the download manager, so that on
completion of checking the mimetype, we can issue the download to
the download manager.
This operation is needed when the user long-clicks on a link or image and
we don't know the mimetype. If the user just clicks on the link, we will
do the same steps of correcting the mimetype down in
android.os.webkit.LoadListener rather than handling it here. |
Fields Summary |
---|
BrowserActivity | mActivity | android.content.ContentValues | mValues |
Constructors Summary |
---|
public FetchUrlMimeType(BrowserActivity activity)
mActivity = activity;
|
Methods Summary |
---|
public java.lang.String | doInBackground(android.content.ContentValues values)
mValues = values[0];
// Check to make sure we have a URI to download
String uri = mValues.getAsString(Downloads.URI);
if (uri == null || uri.length() == 0) {
return null;
}
// User agent is likely to be null, though the AndroidHttpClient
// seems ok with that.
AndroidHttpClient client = AndroidHttpClient.newInstance(
mValues.getAsString(Downloads.USER_AGENT));
HttpHead request = new HttpHead(uri);
String cookie = mValues.getAsString(Downloads.COOKIE_DATA);
if (cookie != null && cookie.length() > 0) {
request.addHeader("Cookie", cookie);
}
String referer = mValues.getAsString(Downloads.REFERER);
if (referer != null && referer.length() > 0) {
request.addHeader("Referer", referer);
}
HttpResponse response;
Boolean succeeded = true;
String mimeType = null;
try {
response = client.execute(request);
// We could get a redirect here, but if we do lets let
// the download manager take care of it, and thus trust that
// the server sends the right mimetype
if (response.getStatusLine().getStatusCode() == 200) {
Header header = response.getFirstHeader("Content-Type");
if (header != null) {
mimeType = header.getValue();
final int semicolonIndex = mimeType.indexOf(';");
if (semicolonIndex != -1) {
mimeType = mimeType.substring(0, semicolonIndex);
}
}
}
} catch (IllegalArgumentException ex) {
request.abort();
} catch (IOException ex) {
request.abort();
} finally {
client.close();
}
return mimeType;
| public void | onPostExecute(java.lang.String mimeType)
if (mimeType != null) {
String url = mValues.getAsString(Downloads.URI);
if (mimeType.equalsIgnoreCase("text/plain") ||
mimeType.equalsIgnoreCase("application/octet-stream")) {
String newMimeType =
MimeTypeMap.getSingleton().getMimeTypeFromExtension(
MimeTypeMap.getFileExtensionFromUrl(url));
if (newMimeType != null) {
mValues.put(Downloads.MIMETYPE, newMimeType);
}
}
String filename = URLUtil.guessFileName(url,
null, mimeType);
mValues.put(Downloads.FILENAME_HINT, filename);
}
// Start the download
final Uri contentUri =
mActivity.getContentResolver().insert(Downloads.CONTENT_URI, mValues);
mActivity.viewDownloads(contentUri);
|
|