|
|
@ -5,15 +5,14 @@ package acr.browser.lightning; |
|
|
|
|
|
|
|
|
|
|
|
import android.app.DownloadManager; |
|
|
|
import android.app.DownloadManager; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.Context; |
|
|
|
import android.net.http.AndroidHttpClient; |
|
|
|
|
|
|
|
import android.os.Environment; |
|
|
|
import android.os.Environment; |
|
|
|
import android.webkit.MimeTypeMap; |
|
|
|
import android.webkit.MimeTypeMap; |
|
|
|
import android.webkit.URLUtil; |
|
|
|
import android.webkit.URLUtil; |
|
|
|
import org.apache.http.Header; |
|
|
|
import android.widget.Toast; |
|
|
|
import org.apache.http.HttpResponse; |
|
|
|
|
|
|
|
import org.apache.http.client.methods.HttpHead; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.net.HttpURLConnection; |
|
|
|
|
|
|
|
import java.net.URL; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* This class is used to pull down the http headers of a given URL so that we |
|
|
|
* This class is used to pull down the http headers of a given URL so that we |
|
|
@ -42,48 +41,50 @@ public class FetchUrlMimeType extends Thread { |
|
|
|
mUri = uri; |
|
|
|
mUri = uri; |
|
|
|
mCookies = cookies; |
|
|
|
mCookies = cookies; |
|
|
|
mUserAgent = userAgent; |
|
|
|
mUserAgent = userAgent; |
|
|
|
|
|
|
|
Toast.makeText(mContext, R.string.download_pending, Toast.LENGTH_SHORT).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
|
// User agent is likely to be null, though the AndroidHttpClient
|
|
|
|
// User agent is likely to be null, though the AndroidHttpClient
|
|
|
|
// seems ok with that.
|
|
|
|
// seems ok with that.
|
|
|
|
AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpHead request = new HttpHead(mUri); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mCookies != null && mCookies.length() > 0) { |
|
|
|
|
|
|
|
request.addHeader("Cookie", mCookies); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpResponse response; |
|
|
|
|
|
|
|
String mimeType = null; |
|
|
|
String mimeType = null; |
|
|
|
String contentDisposition = null; |
|
|
|
String contentDisposition = null; |
|
|
|
|
|
|
|
HttpURLConnection connection = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
response = client.execute(request); |
|
|
|
URL url = new URL(mUri); |
|
|
|
|
|
|
|
connection = (HttpURLConnection) url.openConnection(); |
|
|
|
|
|
|
|
if (mCookies != null && mCookies.length() > 0) { |
|
|
|
|
|
|
|
connection.addRequestProperty("Cookie", mCookies); |
|
|
|
|
|
|
|
connection.setRequestProperty("User-Agent", mUserAgent); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
connection.connect(); |
|
|
|
// We could get a redirect here, but if we do lets let
|
|
|
|
// We could get a redirect here, but if we do lets let
|
|
|
|
// the download manager take care of it, and thus trust that
|
|
|
|
// the download manager take care of it, and thus trust that
|
|
|
|
// the server sends the right mimetype
|
|
|
|
// the server sends the right mimetype
|
|
|
|
if (response.getStatusLine().getStatusCode() == 200) { |
|
|
|
if (connection.getResponseCode() == 200) { |
|
|
|
Header header = response.getFirstHeader("Content-Type"); |
|
|
|
String header = connection.getHeaderField("Content-Type"); |
|
|
|
if (header != null) { |
|
|
|
if (header != null) { |
|
|
|
mimeType = header.getValue(); |
|
|
|
mimeType = header; |
|
|
|
final int semicolonIndex = mimeType.indexOf(';'); |
|
|
|
final int semicolonIndex = mimeType.indexOf(';'); |
|
|
|
if (semicolonIndex != -1) { |
|
|
|
if (semicolonIndex != -1) { |
|
|
|
mimeType = mimeType.substring(0, semicolonIndex); |
|
|
|
mimeType = mimeType.substring(0, semicolonIndex); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Header contentDispositionHeader = response.getFirstHeader("Content-Disposition"); |
|
|
|
String contentDispositionHeader = connection.getHeaderField("Content-Disposition"); |
|
|
|
if (contentDispositionHeader != null) { |
|
|
|
if (contentDispositionHeader != null) { |
|
|
|
contentDisposition = contentDispositionHeader.getValue(); |
|
|
|
contentDisposition = contentDispositionHeader; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (IllegalArgumentException ex) { |
|
|
|
} catch (IllegalArgumentException ex) { |
|
|
|
request.abort(); |
|
|
|
if (connection != null) |
|
|
|
|
|
|
|
connection.disconnect(); |
|
|
|
} catch (IOException ex) { |
|
|
|
} catch (IOException ex) { |
|
|
|
request.abort(); |
|
|
|
if (connection != null) |
|
|
|
|
|
|
|
connection.disconnect(); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
client.close(); |
|
|
|
if (connection != null) |
|
|
|
|
|
|
|
connection.disconnect(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (mimeType != null) { |
|
|
|
if (mimeType != null) { |
|
|
|