/
ActivityReceiver.kt
164 lines (160 loc) · 6.49 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* 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 androidx.annotation.StringRes
import androidx.emoji.text.EmojiCompat
import androidx.preference.PreferenceManager
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.Preferences
import com.javinator9889.handwashingreminder.utils.calendar.CalendarUtils
import com.javinator9889.handwashingreminder.utils.goAsync
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.io.*
import java.util.*
import java.util.concurrent.TimeUnit
class ActivityReceiver : BroadcastReceiver() {
/**
* {@inheritDoc}
*/
override fun onReceive(context: Context, intent: Intent) {
Timber.d("Detected user activity - ${intent.action}")
if (ActivityTransitionResult.hasResult(intent)) {
val emojiLoader = EmojiLoader.loadAsync(context)
val result = ActivityTransitionResult.extractResult(intent)
result?.let {
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
}
} ?: Timber.w("Received unmatched activity - $intent")
}
}
private suspend fun putNotification(
notificationsHandler: NotificationsHandler,
emojiLoader: Deferred<EmojiCompat>,
detectedActivity: Int,
context: Context
) {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val timeInBetweenNotifications =
prefs.getString(Preferences.ACTIVITY_MINIMUM_TIME, "15")!!.toInt()
val timeFile = File(context.cacheDir, "activity.time")
var latestNotificationTime = 0L
withContext(Dispatchers.IO) {
if (timeFile.exists()) {
DataInputStream(FileInputStream(timeFile)).use {
latestNotificationTime = it.readLong()
}
}
}
val timeDifference = CalendarUtils.timeBetweenIn(
TimeUnit.MINUTES,
latestNotificationTime
)
Timber.d("$timeDifference - $timeInBetweenNotifications")
if (timeDifference <= timeInBetweenNotifications)
return
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,
notificationId = 2
)
}
withContext(Dispatchers.IO) {
DataOutputStream(FileOutputStream(timeFile)).use {
it.writeLong(Calendar.getInstance().timeInMillis)
}
}
}
}
private data class NotificationContent(
@StringRes val title: Int,
@StringRes val content: Int
)