summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/FetchUrlMimeType.java
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2009-03-03 19:32:16 -0800
committerThe Android Open Source Project <initial-contribution@android.com>2009-03-03 19:32:16 -0800
commit0c90888c75eed12f6e2e14a9044faf50bd4af8ed (patch)
tree266c059a147232a297b2d956e04397c0f6683bc4 /src/com/android/browser/FetchUrlMimeType.java
parent8611831e36b71c844a14788998728f3cd625b833 (diff)
downloadpackages_apps_browser-0c90888c75eed12f6e2e14a9044faf50bd4af8ed.zip
packages_apps_browser-0c90888c75eed12f6e2e14a9044faf50bd4af8ed.tar.gz
packages_apps_browser-0c90888c75eed12f6e2e14a9044faf50bd4af8ed.tar.bz2
auto import from //depot/cupcake/@135843
Diffstat (limited to 'src/com/android/browser/FetchUrlMimeType.java')
-rw-r--r--src/com/android/browser/FetchUrlMimeType.java135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/com/android/browser/FetchUrlMimeType.java b/src/com/android/browser/FetchUrlMimeType.java
new file mode 100644
index 0000000..8578643
--- /dev/null
+++ b/src/com/android/browser/FetchUrlMimeType.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.browser;
+
+import android.content.ContentValues;
+import android.net.Uri;
+import android.net.http.AndroidHttpClient;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.Header;
+import org.apache.http.client.methods.HttpHead;
+
+import java.io.IOException;
+
+import android.os.AsyncTask;
+import android.provider.Downloads;
+import android.webkit.MimeTypeMap;
+import android.webkit.URLUtil;
+
+/**
+ * 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.
+ *
+ */
+class FetchUrlMimeType extends AsyncTask<ContentValues, String, String> {
+
+ BrowserActivity mActivity;
+ ContentValues mValues;
+
+ public FetchUrlMimeType(BrowserActivity activity) {
+ mActivity = activity;
+ }
+
+ @Override
+ public String doInBackground(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;
+ }
+
+ @Override
+ public void onPostExecute(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);
+ }
+
+}