/
ActivityReceiver.kt
130 lines (126 loc) · 5.05 KB
/
ActivityReceiver.kt
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright © 2020 - present | Handwashing reminder by Javinator9889
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*
* Created by Javinator9889 on 10/04/20 - Handwashing reminder.
*/
package com.javinator9889.handwashingreminder.gms.activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import androidx.annotation.StringRes
import androidx.emoji.text.EmojiCompat
import com.google.android.gms.location.ActivityTransition
import com.google.android.gms.location.ActivityTransitionResult
import com.google.android.gms.location.DetectedActivity
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.emoji.EmojiLoader
import com.javinator9889.handwashingreminder.notifications.NotificationsHandler
import com.javinator9889.handwashingreminder.utils.ACTIVITY_CHANNEL_ID
import com.javinator9889.handwashingreminder.utils.goAsync
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class ActivityReceiver : BroadcastReceiver() {
/**
* {@inheritDoc}
*/
override fun onReceive(context: Context, intent: Intent) {
if (ActivityTransitionResult.hasResult(intent)
&& TextUtils.equals(TRANSITIONS_RECEIVER_ACTION, intent.action)) {
val emojiLoader = EmojiLoader.loadAsync(context)
val result = ActivityTransitionResult.extractResult(intent)!!
for (event in result.transitionEvents) {
if (event.transitionType !=
ActivityTransition.ACTIVITY_TRANSITION_EXIT ||
event.activityType == DetectedActivity.UNKNOWN
)
continue
val notificationHandler = NotificationsHandler(
context,
ACTIVITY_CHANNEL_ID,
context.getString(
R.string.activity_notification_channel_name
),
context.getString(
R.string.activity_notification_channel_desc
)
)
goAsync {
putNotification(
notificationHandler,
emojiLoader,
event.activityType,
context
)
}
break
}
}
}
private suspend fun putNotification(
notificationsHandler: NotificationsHandler,
emojiLoader: Deferred<EmojiCompat>,
detectedActivity: Int,
context: Context
) {
val notificationContent = when (detectedActivity) {
DetectedActivity.WALKING ->
NotificationContent(
R.string.activity_notification_walk,
R.string.activity_notification_walk_content
)
DetectedActivity.RUNNING ->
NotificationContent(
R.string.activity_notification_run,
R.string.activity_notifications_run_content
)
DetectedActivity.ON_BICYCLE ->
NotificationContent(
R.string.activity_notification_cycling,
R.string.activity_notification_cycling_content
)
DetectedActivity.IN_VEHICLE ->
NotificationContent(
R.string.activity_notification_vehicle,
R.string.activity_notification_vehicle_content
)
else -> throw IllegalArgumentException(
"Activity not recognized - $detectedActivity"
)
}
var title = context.getText(notificationContent.title)
var content = context.getText(notificationContent.content)
try {
val emojiCompat = emojiLoader.await()
title = emojiCompat.process(title)
content = emojiCompat.process(content)
} catch (_: IllegalStateException) { }
withContext(Dispatchers.Main) {
notificationsHandler.createNotification(
iconDrawable = R.drawable.ic_stat_handwashing,
largeIcon = R.drawable.handwashing_app_logo,
title = title,
content = content,
longContent = content
)
}
}
}
private data class NotificationContent(
@StringRes val title: Int,
@StringRes val content: Int
)