fdroidclient / F-Droid / src / org / fdroid / fdroid / net / AsyncDownloadWrapper.java @ 0a9941d9
History | View | Annotate | Download (3.06 KB)
1 |
package org.fdroid.fdroid.net; |
---|---|
2 |
|
3 |
import android.os.Bundle; |
4 |
import android.os.Handler; |
5 |
import android.os.Message; |
6 |
import android.util.Log; |
7 |
|
8 |
import java.io.IOException; |
9 |
|
10 |
class AsyncDownloadWrapper extends Handler implements AsyncDownloader { |
11 |
|
12 |
private static final String TAG = "AsyncDownloadWrapper"; |
13 |
|
14 |
private static final int MSG_DOWNLOAD_COMPLETE = 2; |
15 |
private static final int MSG_DOWNLOAD_CANCELLED = 3; |
16 |
private static final int MSG_ERROR = 4; |
17 |
private static final String MSG_DATA = "data"; |
18 |
|
19 |
private final Downloader downloader; |
20 |
private DownloadThread downloadThread = null; |
21 |
|
22 |
private final Listener listener; |
23 |
|
24 |
/**
|
25 |
* Normally the listener would be provided using a setListener method.
|
26 |
* However for the purposes of this async downloader, it doesn't make
|
27 |
* sense to have an async task without any way to notify the outside
|
28 |
* world about completion. Therefore, we require the listener as a
|
29 |
* parameter to the constructor.
|
30 |
*/
|
31 |
public AsyncDownloadWrapper(Downloader downloader, Listener listener) {
|
32 |
this.downloader = downloader;
|
33 |
this.listener = listener;
|
34 |
} |
35 |
|
36 |
public int getBytesRead() { |
37 |
return downloader.getBytesRead();
|
38 |
} |
39 |
|
40 |
public int getTotalBytes() { |
41 |
return downloader.getTotalBytes();
|
42 |
} |
43 |
|
44 |
public void download() { |
45 |
downloadThread = new DownloadThread();
|
46 |
downloadThread.start(); |
47 |
} |
48 |
|
49 |
public void attemptCancel(boolean userRequested) { |
50 |
if (downloadThread != null) { |
51 |
downloadThread.interrupt(); |
52 |
} |
53 |
} |
54 |
|
55 |
/**
|
56 |
* Receives "messages" from the download thread, and passes them onto the
|
57 |
* relevant {@link AsyncDownloader.Listener}
|
58 |
*/
|
59 |
public void handleMessage(Message message) { |
60 |
switch (message.arg1) {
|
61 |
case MSG_DOWNLOAD_COMPLETE:
|
62 |
listener.onDownloadComplete(); |
63 |
break;
|
64 |
case MSG_DOWNLOAD_CANCELLED:
|
65 |
listener.onDownloadCancelled(); |
66 |
break;
|
67 |
case MSG_ERROR:
|
68 |
listener.onErrorDownloading(message.getData().getString(MSG_DATA)); |
69 |
break;
|
70 |
} |
71 |
} |
72 |
|
73 |
private class DownloadThread extends Thread { |
74 |
|
75 |
public void run() { |
76 |
try {
|
77 |
downloader.download(); |
78 |
sendMessage(MSG_DOWNLOAD_COMPLETE); |
79 |
} catch (InterruptedException e) { |
80 |
sendMessage(MSG_DOWNLOAD_CANCELLED); |
81 |
} catch (IOException e) { |
82 |
Log.e(TAG, "I/O exception in download thread", e);
|
83 |
Bundle data = new Bundle(1); |
84 |
data.putString(MSG_DATA, e.getLocalizedMessage()); |
85 |
Message message = new Message();
|
86 |
message.arg1 = MSG_ERROR; |
87 |
message.setData(data); |
88 |
AsyncDownloadWrapper.this.sendMessage(message); |
89 |
} |
90 |
} |
91 |
|
92 |
private void sendMessage(int messageType) { |
93 |
Message message = new Message();
|
94 |
message.arg1 = messageType; |
95 |
AsyncDownloadWrapper.this.sendMessage(message); |
96 |
} |
97 |
} |
98 |
} |