Statistics
| Branch: | Tag: | Revision:

fdroidclient / F-Droid / src / org / fdroid / fdroid / net / DownloaderFactory.java @ 0a9941d9

History | View | Annotate | Download (2.96 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.URL;
9

    
10
public class DownloaderFactory {
11

    
12
    /**
13
     * Downloads to a temporary file, which *you must delete yourself when
14
     * you are done.  It is stored in {@link Context#getCacheDir()} and starts
15
     * with the prefix {@code dl-}.
16
     */
17
    public static Downloader create(Context context, String urlString)
18
            throws IOException {
19
        return create(context, new URL(urlString));
20
    }
21

    
22
    /**
23
     * Downloads to a temporary file, which *you must delete yourself when
24
     * you are done.  It is stored in {@link Context#getCacheDir()} and starts
25
     * with the prefix {@code dl-}.
26
     */
27
    public static Downloader create(Context context, URL url)
28
            throws IOException {
29
        File destFile = File.createTempFile("dl-", "", context.getCacheDir());
30
        destFile.deleteOnExit(); // this probably does nothing, but maybe...
31
        return create(context, url, destFile);
32
    }
33

    
34
    public static Downloader create(Context context, String urlString, File destFile)
35
            throws IOException {
36
        return create(context, new URL(urlString), destFile);
37
    }
38

    
39
    public static Downloader create(Context context, URL url, File destFile)
40
            throws IOException {
41
        if (isBluetoothAddress(url)) {
42
            String macAddress = url.getHost().replace("-", ":");
43
            return new BluetoothDownloader(context, macAddress, url, destFile);
44
        } else if (isOnionAddress(url)) {
45
            return new TorHttpDownloader(context, url, destFile);
46
        } else {
47
            return new HttpDownloader(context, url, destFile);
48
        }
49
    }
50

    
51
    private static boolean isBluetoothAddress(URL url) {
52
        return "bluetooth".equalsIgnoreCase(url.getProtocol());
53
    }
54

    
55
    public static AsyncDownloader createAsync(Context context, String urlString, File destFile, String title, String id, AsyncDownloader.Listener listener) throws IOException {
56
        return createAsync(context, new URL(urlString), destFile, title, id, listener);
57
    }
58

    
59
    public static AsyncDownloader createAsync(Context context, URL url, File destFile, String title, String id, AsyncDownloader.Listener listener)
60
            throws IOException {
61
        if (canUseDownloadManager(url)) {
62
            return new AsyncDownloaderFromAndroid(context, listener, title, id, url.toString(), destFile);
63
        } else {
64
            return new AsyncDownloadWrapper(create(context, url, destFile), listener);
65
        }
66
    }
67

    
68
    static boolean isOnionAddress(URL url) {
69
        return url.getHost().endsWith(".onion");
70
    }
71

    
72
    /**
73
     * Tests to see if we can use Android's DownloadManager to download the APK, instead of
74
     * a downloader returned from DownloadFactory.
75
     */
76
    private static boolean canUseDownloadManager(URL url) {
77
        return Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO && !isOnionAddress(url);
78
    }
79

    
80
}