forked from douglascraigschmidt/LiveLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloadWithRunnable.java
68 lines (60 loc) · 2.26 KB
/
DownloadWithRunnable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package edu.vuum.mocca;
import android.graphics.Bitmap;
/**
* @class DownloadWithRunnable
*
* @brief Use the Runnables and Handlers defined by the HaMeR
* framework to implement the ConcurrencyStrategy interface and
* download a bitmap image in a background Thread. This class
* plays the role of the "Concrete Strategy" in the Strategy
* pattern.
*/
public class DownloadWithRunnable implements ButtonStrategy {
/**
* Thread object that's used for the download.
*/
private Thread mThread = null;
/**
* Creates and starts a new Thread to download an image in the
* background via a Runnable. The downloaded image is then
* diplayed in the UI Thread by posting another Runnable via the
* Activity's runOnUiThread() method, which uses an internal
* Handler.
*/
@Override
public void downloadAndDisplayImage(final DownloadContext downloadContext) {
Runnable downloadRunnable = new Runnable() {
/**
* Download a bitmap image in a background Thread and then
* post a Runnable to the UI Thread to set the image to
* the ImageView.
*/
@Override
public void run() {
// Download the image.
final Bitmap image =
downloadContext.downloadImage(downloadContext.getUrlString());
// Display the downloaded image to the user.
downloadContext.displayImage(image);
}
};
// Inform the user that the download is starting with
// this particular concurrency strategy.
downloadContext.showToast
("downloading via Handlers and Runnables");
// Create and Start a new Thread to perform the download and
// display the results to the user.
mThread = new Thread(downloadRunnable);
mThread.start();
}
/**
* Cancel the download.
*/
@Override
public void cancelDownload(DownloadContext downloadContext) {
// Let the user know this download is being canceled.
downloadContext.showToast("Canceling DownloadWithRunnable in progress");
// Interrupt the Thread so it will stop the download.
mThread.interrupt();
}
}