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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package de.danoeh.antennapod.adapter;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.widget.ImageButton;
import org.apache.commons.lang3.Validate;
import java.lang.ref.WeakReference;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.LongList;
/**
* Utility methods for the action button that is displayed on the right hand side
* of a listitem.
*/
public class ActionButtonUtils {
private final int[] labels;
private final TypedArray drawables;
private final Context context;
public ActionButtonUtils(Context context) {
Validate.notNull(context);
this.context = context.getApplicationContext();
drawables = context.obtainStyledAttributes(new int[] {
R.attr.av_play,
R.attr.navigation_cancel,
R.attr.av_download,
R.attr.av_pause,
R.attr.navigation_accept,
R.attr.content_new
});
labels = new int[] {
R.string.play_label,
R.string.cancel_download_label,
R.string.download_label,
R.string.mark_read_label,
R.string.add_to_queue_label
};
}
/**
* Sets the displayed bitmap and content description of the given
* action button so that it matches the state of the FeedItem.
*/
public void configureActionButton(ImageButton butSecondary, FeedItem item) {
Validate.isTrue(butSecondary != null && item != null, "butSecondary or item was null");
final FeedMedia media = item.getMedia();
if (media != null) {
final boolean isDownloadingMedia = DownloadRequester.getInstance().isDownloadingFile(media);
if (!media.isDownloaded()) {
if (isDownloadingMedia) {
// item is being downloaded
butSecondary.setVisibility(View.VISIBLE);
butSecondary.setImageDrawable(drawables
.getDrawable(1));
butSecondary.setContentDescription(context.getString(labels[1]));
} else {
// item is not downloaded and not being downloaded
LongList queueIds = DBReader.getQueueIDList();
if(DefaultActionButtonCallback.userAllowedMobileDownloads() ||
!DefaultActionButtonCallback.userChoseAddToQueue() || queueIds.contains(item.getId())) {
butSecondary.setVisibility(View.VISIBLE);
butSecondary.setImageDrawable(drawables.getDrawable(2));
butSecondary.setContentDescription(context.getString(labels[2]));
} else {
// mobile download not allowed yet, item is not in queue and user chose add to queue
butSecondary.setVisibility(View.VISIBLE);
butSecondary.setImageDrawable(drawables.getDrawable(5));
butSecondary.setContentDescription(context.getString(labels[4]));
}
}
} else {
// item is downloaded
butSecondary.setVisibility(View.VISIBLE);
if (media.isCurrentlyPlaying()) {
butSecondary.setImageDrawable(drawables.getDrawable(3));
} else {
butSecondary.setImageDrawable(drawables.getDrawable(0));
}
butSecondary.setContentDescription(context.getString(labels[0]));
}
} else {
if (item.isPlayed()) {
butSecondary.setVisibility(View.INVISIBLE);
} else {
butSecondary.setVisibility(View.VISIBLE);
butSecondary.setImageDrawable(drawables.getDrawable(4));
butSecondary.setContentDescription(context.getString(labels[3]));
}
}
}
}
|