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
103
104
105
106
|
package de.luhmer.owncloudnewsreader.view;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import de.greenrobot.event.EventBus;
import de.luhmer.owncloudnewsreader.NewsReaderListActivity;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.events.podcast.UpdatePodcastStatusEvent;
import de.luhmer.owncloudnewsreader.events.podcast.broadcastreceiver.PodcastNotificationToggle;
public class PodcastNotification {
private Context context;
private NotificationManager notificationManager;
private EventBus eventBus;
private NotificationCompat.Builder notificationBuilder;
private PendingIntent resultPendingIntent;
private final static int NOTIFICATION_ID = 1111;
public PodcastNotification(Context context) {
this.context = context;
this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
eventBus = EventBus.getDefault();
eventBus.register(this);
}
private void createNewNotificationBuilder() {
// Creates an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(context, NewsReaderListActivity.class);
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
resultPendingIntent =
PendingIntent.getActivity(
context,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
// Create the final Notification object.
notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent);
}
int lastDrawableId;
public void onEvent(UpdatePodcastStatusEvent podcast) {
if(!podcast.isFileLoaded())
return;
int drawableId = podcast.isPlaying() ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play;
String actionText = podcast.isPlaying() ? "Pause" : "Play";
if(lastDrawableId != drawableId) {
lastDrawableId = drawableId;
createNewNotificationBuilder();
notificationBuilder.setContentTitle(podcast.getTitle());
notificationBuilder.addAction(drawableId, actionText, PendingIntent.getBroadcast(context, 0, new Intent(context,
PodcastNotificationToggle.class),
PendingIntent.FLAG_ONE_SHOT));
}
int hours = (int)( podcast.getCurrent() / (1000*60*60));
int minutes = (int)(podcast.getCurrent() % (1000*60*60)) / (1000*60);
int seconds = (int) ((podcast.getCurrent() % (1000*60*60)) % (1000*60) / 1000);
minutes += hours * 60;
String fromText = (String.format("%02d:%02d", minutes, seconds));
hours = (int)( podcast.getMax() / (1000*60*60));
minutes = (int)(podcast.getMax() % (1000*60*60)) / (1000*60);
seconds = (int) ((podcast.getMax() % (1000*60*60)) % (1000*60) / 1000);
minutes += hours * 60;
String toText = (String.format("%02d:%02d", minutes, seconds));
double progressDouble = ((double)podcast.getCurrent() / (double)podcast.getMax()) * 100d;
int progress = ((int) progressDouble);
notificationBuilder
.setContentText(fromText + " - " + toText)
.setProgress(100, progress, podcast.isPreparingFile());
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
//.setLargeIcon(R.drawable.ic_launcher)
//.addAction(android.R.drawable.ic_media_pause, "More", resultPendingIntent)
}
public void cancel()
{
notificationManager.cancel(NOTIFICATION_ID);
}
}
|