/WebVox/src/com/marvin/webvox/FetchUrlMimeType.java

http://eyes-free.googlecode.com/ · Java · 138 lines · 25 code · 11 blank · 102 comment · 0 complexity · f338375980416076f5a6f0363b3eb951 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 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. package com.marvin.webvox;
  17. import android.content.ContentValues;
  18. import android.net.Uri;
  19. //import android.net.http.AndroidHttpClient;
  20. import org.apache.http.HttpResponse;
  21. import org.apache.http.Header;
  22. import org.apache.http.client.methods.HttpHead;
  23. import java.io.IOException;
  24. import android.os.AsyncTask;
  25. //import android.provider.Downloads;
  26. import android.webkit.MimeTypeMap;
  27. import android.webkit.URLUtil;
  28. /**
  29. * This class is used to pull down the http headers of a given URL so that
  30. * we can analyse the mimetype and make any correction needed before we give
  31. * the URL to the download manager. The ContentValues class holds the
  32. * content that would be provided to the download manager, so that on
  33. * completion of checking the mimetype, we can issue the download to
  34. * the download manager.
  35. * This operation is needed when the user long-clicks on a link or image and
  36. * we don't know the mimetype. If the user just clicks on the link, we will
  37. * do the same steps of correcting the mimetype down in
  38. * android.os.webkit.LoadListener rather than handling it here.
  39. *
  40. */
  41. class FetchUrlMimeType extends AsyncTask<ContentValues, String, String> {
  42. BrowserActivity mActivity;
  43. ContentValues mValues;
  44. public FetchUrlMimeType(BrowserActivity activity) {
  45. mActivity = activity;
  46. }
  47. @Override
  48. public String doInBackground(ContentValues... values) {
  49. mValues = values[0];
  50. /*
  51. // Check to make sure we have a URI to download
  52. String uri = mValues.getAsString(Downloads.COLUMN_URI);
  53. if (uri == null || uri.length() == 0) {
  54. return null;
  55. }
  56. // User agent is likely to be null, though the AndroidHttpClient
  57. // seems ok with that.
  58. AndroidHttpClient client = AndroidHttpClient.newInstance(
  59. mValues.getAsString(Downloads.COLUMN_USER_AGENT));
  60. HttpHead request = new HttpHead(uri);
  61. String cookie = mValues.getAsString(Downloads.COLUMN_COOKIE_DATA);
  62. if (cookie != null && cookie.length() > 0) {
  63. request.addHeader("Cookie", cookie);
  64. }
  65. String referer = mValues.getAsString(Downloads.COLUMN_REFERER);
  66. if (referer != null && referer.length() > 0) {
  67. request.addHeader("Referer", referer);
  68. }
  69. HttpResponse response;
  70. String mimeType = null;
  71. try {
  72. response = client.execute(request);
  73. // We could get a redirect here, but if we do lets let
  74. // the download manager take care of it, and thus trust that
  75. // the server sends the right mimetype
  76. if (response.getStatusLine().getStatusCode() == 200) {
  77. Header header = response.getFirstHeader("Content-Type");
  78. if (header != null) {
  79. mimeType = header.getValue();
  80. final int semicolonIndex = mimeType.indexOf(';');
  81. if (semicolonIndex != -1) {
  82. mimeType = mimeType.substring(0, semicolonIndex);
  83. }
  84. }
  85. }
  86. } catch (IllegalArgumentException ex) {
  87. request.abort();
  88. } catch (IOException ex) {
  89. request.abort();
  90. } finally {
  91. client.close();
  92. }
  93. return mimeType;
  94. */
  95. return "";
  96. }
  97. @Override
  98. public void onPostExecute(String mimeType) {
  99. /*
  100. if (mimeType != null) {
  101. String url = mValues.getAsString(Downloads.COLUMN_URI);
  102. if (mimeType.equalsIgnoreCase("text/plain") ||
  103. mimeType.equalsIgnoreCase("application/octet-stream")) {
  104. String newMimeType =
  105. MimeTypeMap.getSingleton().getMimeTypeFromExtension(
  106. MimeTypeMap.getFileExtensionFromUrl(url));
  107. if (newMimeType != null) {
  108. mValues.put(Downloads.COLUMN_MIME_TYPE, newMimeType);
  109. }
  110. }
  111. String filename = URLUtil.guessFileName(url,
  112. null, mimeType);
  113. mValues.put(Downloads.COLUMN_FILE_NAME_HINT, filename);
  114. }
  115. // Start the download
  116. final Uri contentUri =
  117. mActivity.getContentResolver().insert(Downloads.CONTENT_URI, mValues);
  118. mActivity.viewDownloads(contentUri);
  119. */
  120. }
  121. }