/
TimerActivityListener.groovy
60 lines (54 loc) · 2.16 KB
/
TimerActivityListener.groovy
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
package me.champeau.speakertime
import android.app.PendingIntent
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat
import com.google.android.gms.wearable.MessageEvent
import com.google.android.gms.wearable.WearableListenerService
import groovy.transform.CompileStatic
import me.champeau.speakertime.support.MessageConstants
@CompileStatic
class TimerActivityListener extends WearableListenerService {
private int NOTIFICATION_ID = 0
private int lastElapsedVibrate = -1
private @Lazy Bitmap cachedBitmap = BitmapFactory.decodeResource(resources, R.drawable.speaker)
@Override
void onMessageReceived(MessageEvent messageEvent) {
switch (messageEvent.path) {
case MessageConstants.MSG_TIME_LEFT:
updateNotification(messageEvent)
break
case MessageConstants.STOP_WEAR_ACTIVITY:
cancelNotification()
break
}
}
private void cancelNotification() {
compatNotificationManager.cancel(NOTIFICATION_ID)
lastElapsedVibrate = -1
}
private void updateNotification(MessageEvent messageEvent) {
String[] data = new String(messageEvent.data).split(';')
String timeLeft = data[0]
double elapsed = Double.valueOf(data[1])
int rounded = (int) elapsed
if (lastElapsedVibrate==-1 || timeLeft.endsWith(':0')) {
// Build the notification and issues it with notification manager.
notify(NOTIFICATION_ID) {
smallIcon = R.drawable.ic_action_alarms
largeIcon = cachedBitmap
contentTitle = 'Time left'
contentText = "$timeLeft (Elapsed: ${rounded}%)"
contentIntent = pendingActivityIntent(0, intent(WearPresentationActivity), PendingIntent.FLAG_UPDATE_CURRENT)
ongoing = true
style = bigTextStyle {
bigText """Time left for your presentation: $timeLeft
Elapsed time: ${rounded}%)
"""
}
}
}
}
}