fdroidclient / F-Droid / src / org / fdroid / fdroid / net / DownloaderFactory.java @ 69ecaf02
History | View | Annotate | Download (2.99 KB)
1 |
package org.fdroid.fdroid.net; |
---|---|
2 |
|
3 |
import android.content.Context; |
4 |
import android.os.Build; |
5 |
|
6 |
import java.io.File; |
7 |
import java.io.IOException; |
8 |
import java.net.MalformedURLException; |
9 |
import java.net.URL; |
10 |
|
11 |
public class DownloaderFactory { |
12 |
|
13 |
/**
|
14 |
* Downloads to a temporary file, which *you must delete yourself when
|
15 |
* you are done. It is stored in {@link Context#getCacheDir()} and starts
|
16 |
* with the prefix {@code dl-}.
|
17 |
*/
|
18 |
public static Downloader create(Context context, String urlString) |
19 |
throws IOException { |
20 |
return create(context, new URL(urlString)); |
21 |
} |
22 |
|
23 |
/**
|
24 |
* Downloads to a temporary file, which *you must delete yourself when
|
25 |
* you are done. It is stored in {@link Context#getCacheDir()} and starts
|
26 |
* with the prefix {@code dl-}.
|
27 |
*/
|
28 |
public static Downloader create(Context context, URL url) |
29 |
throws IOException { |
30 |
File destFile = File.createTempFile("dl-", "", context.getCacheDir()); |
31 |
destFile.deleteOnExit(); // this probably does nothing, but maybe...
|
32 |
return create(context, url, destFile);
|
33 |
} |
34 |
|
35 |
public static Downloader create(Context context, String urlString, File destFile) |
36 |
throws IOException { |
37 |
return create(context, new URL(urlString), destFile); |
38 |
} |
39 |
|
40 |
public static Downloader create(Context context, URL url, File destFile) |
41 |
throws IOException { |
42 |
if (isBluetoothAddress(url)) {
|
43 |
String macAddress = url.getHost().replace("-", ":"); |
44 |
return new BluetoothDownloader(context, macAddress, url, destFile); |
45 |
} else if (isOnionAddress(url)) { |
46 |
return new TorHttpDownloader(context, url, destFile); |
47 |
} else {
|
48 |
return new HttpDownloader(context, url, destFile); |
49 |
} |
50 |
} |
51 |
|
52 |
private static boolean isBluetoothAddress(URL url) { |
53 |
return "bluetooth".equalsIgnoreCase(url.getProtocol()); |
54 |
} |
55 |
|
56 |
public static AsyncDownloader createAsync(Context context, String urlString, File destFile, String title, String id, AsyncDownloader.Listener listener) throws IOException { |
57 |
return createAsync(context, new URL(urlString), destFile, title, id, listener); |
58 |
} |
59 |
|
60 |
public static AsyncDownloader createAsync(Context context, URL url, File destFile, String title, String id, AsyncDownloader.Listener listener) |
61 |
throws IOException { |
62 |
if (canUseDownloadManager(url)) {
|
63 |
return new AsyncDownloaderFromAndroid(context, listener, title, id, url.toString(), destFile); |
64 |
} else {
|
65 |
return new AsyncDownloader(create(context, url, destFile), listener); |
66 |
} |
67 |
} |
68 |
|
69 |
static boolean isOnionAddress(URL url) { |
70 |
return url.getHost().endsWith(".onion"); |
71 |
} |
72 |
|
73 |
/**
|
74 |
* Tests to see if we can use Android's DownloadManager to download the APK, instead of
|
75 |
* a downloader returned from DownloadFactory.
|
76 |
*/
|
77 |
private static boolean canUseDownloadManager(URL url) { |
78 |
return Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO && !isOnionAddress(url);
|
79 |
} |
80 |
|
81 |
} |