PageRenderTime 16ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.marvin.webvox;
 18
 19import android.content.ContentValues;
 20import android.net.Uri;
 21//import android.net.http.AndroidHttpClient;
 22
 23import org.apache.http.HttpResponse;
 24import org.apache.http.Header;
 25import org.apache.http.client.methods.HttpHead;
 26
 27import java.io.IOException;
 28
 29import android.os.AsyncTask;
 30//import android.provider.Downloads;
 31import android.webkit.MimeTypeMap;
 32import android.webkit.URLUtil;
 33
 34/**
 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.
 45 *
 46 */
 47class FetchUrlMimeType extends AsyncTask<ContentValues, String, String> {
 48
 49    BrowserActivity mActivity;
 50    ContentValues mValues;
 51
 52    public FetchUrlMimeType(BrowserActivity activity) {
 53        mActivity = activity;
 54    }
 55
 56    @Override
 57    public String doInBackground(ContentValues... values) {
 58        mValues = values[0];
 59/*
 60        // Check to make sure we have a URI to download
 61        String uri = mValues.getAsString(Downloads.COLUMN_URI);
 62        if (uri == null || uri.length() == 0) {
 63            return null;
 64        }
 65
 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.COLUMN_USER_AGENT));
 70        HttpHead request = new HttpHead(uri);
 71
 72        String cookie = mValues.getAsString(Downloads.COLUMN_COOKIE_DATA);
 73        if (cookie != null && cookie.length() > 0) {
 74            request.addHeader("Cookie", cookie);
 75        }
 76
 77        String referer = mValues.getAsString(Downloads.COLUMN_REFERER);
 78        if (referer != null && referer.length() > 0) {
 79            request.addHeader("Referer", referer);
 80        }
 81
 82        HttpResponse response;
 83        String mimeType = null;
 84        try {
 85            response = client.execute(request);
 86            // We could get a redirect here, but if we do lets let
 87            // the download manager take care of it, and thus trust that
 88            // the server sends the right mimetype
 89            if (response.getStatusLine().getStatusCode() == 200) {
 90                Header header = response.getFirstHeader("Content-Type");
 91                if (header != null) {
 92                    mimeType = header.getValue();
 93                    final int semicolonIndex = mimeType.indexOf(';');
 94                    if (semicolonIndex != -1) {
 95                        mimeType = mimeType.substring(0, semicolonIndex);
 96                    }
 97                }
 98            }
 99        } catch (IllegalArgumentException ex) {
100            request.abort();
101        } catch (IOException ex) {
102            request.abort();
103        } finally {
104            client.close();
105        }
106
107        return mimeType;
108*/ 
109        return "";
110    }
111
112   @Override
113    public void onPostExecute(String mimeType) {
114/*       
115       if (mimeType != null) {
116           String url = mValues.getAsString(Downloads.COLUMN_URI);
117           if (mimeType.equalsIgnoreCase("text/plain") ||
118                   mimeType.equalsIgnoreCase("application/octet-stream")) {
119               String newMimeType =
120                       MimeTypeMap.getSingleton().getMimeTypeFromExtension(
121                           MimeTypeMap.getFileExtensionFromUrl(url));
122               if (newMimeType != null) {
123                   mValues.put(Downloads.COLUMN_MIME_TYPE, newMimeType);
124               }
125           }
126           String filename = URLUtil.guessFileName(url,
127                   null, mimeType);
128           mValues.put(Downloads.COLUMN_FILE_NAME_HINT, filename);
129       }
130
131       // Start the download
132       final Uri contentUri =
133           mActivity.getContentResolver().insert(Downloads.CONTENT_URI, mValues);
134       mActivity.viewDownloads(contentUri);
135*/       
136    }
137
138}