/
AbstractNotificationsWorker.kt
170 lines (160 loc) · 6.36 KB
/
AbstractNotificationsWorker.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
165
166
167
168
169
170
/*
* 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 22/04/20 - Handwashing reminder.
*/
package com.javinator9889.handwashingreminder.jobs.workers
import android.content.Context
import androidx.annotation.ArrayRes
import androidx.annotation.IntRange
import androidx.annotation.StringRes
import androidx.preference.PreferenceManager
import androidx.work.*
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.application.HandwashingApplication
import com.javinator9889.handwashingreminder.emoji.EmojiLoader
import com.javinator9889.handwashingreminder.notifications.NotificationsHandler
import com.javinator9889.handwashingreminder.utils.TIME_CHANNEL_ID
import com.javinator9889.handwashingreminder.utils.runAt
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeoutOrNull
import timber.log.Timber
import java.util.concurrent.TimeUnit
data class WorkScheduleParams(
@IntRange(from = 0, to = 23) val hour: Int,
@IntRange(from = 0, to = 59) val minute: Int
)
abstract class AbstractNotificationsWorker(
context: Context,
params: WorkerParameters
) : CoroutineWorker(context, params) {
protected var maxRetries = 5
protected var shouldScheduleNext = true
protected var workConstraints = with(Constraints.Builder()) {
setRequiredNetworkType(NetworkType.NOT_REQUIRED)
setRequiresBatteryNotLow(false)
setRequiresCharging(false)
setRequiresDeviceIdle(false)
setRequiresStorageNotLow(false)
build()
}
protected abstract val clazz: Class<out ListenableWorker>
protected abstract val workUniqueName: String
protected abstract val preferencesKey: String
protected abstract val titleRes: Int
protected abstract val commentsRes: Int
protected val workParams: WorkScheduleParams
get() {
val preferences =
PreferenceManager.getDefaultSharedPreferences(applicationContext)
val time = preferences.getString(preferencesKey, "")
if (time == "" || time == null)
throw IllegalStateException("Time value cannot be null")
val splitTime = time.split(":")
val hour = Integer.parseInt(splitTime[0])
val minute = Integer.parseInt(splitTime[1])
return WorkScheduleParams(hour, minute)
}
override suspend fun doWork(): Result = coroutineScope {
with(HandwashingApplication.getInstance()) {
withTimeoutOrNull(10_000L) {
firebaseInitDeferred.await()
}
}
shouldScheduleNext = true
var data: Data? = null
try {
data = work()
Result.success()
} catch (e: Exception) {
catchBlock(e)
} finally {
if (shouldScheduleNext) {
scheduleNext(data)
}
}
}
protected open suspend fun work(): Data? {
val emojiLoader = EmojiLoader.get(applicationContext)
val notificationsHandler = NotificationsHandler(
context = applicationContext,
channelId = TIME_CHANNEL_ID,
channelName = getString(R.string.time_notification_channel_name),
channelDesc = getString(R.string.time_notification_channel_desc)
)
val emojiCompat = emojiLoader.await()
var title: CharSequence
var content: CharSequence
try {
title = emojiCompat.process(getText(titleRes))
content = emojiCompat.process(
getStringArray(commentsRes).toList().random()
)
} catch (_: IllegalStateException) {
title = getText(titleRes)
content = getStringArray(commentsRes).toList().random()
}
withContext(Dispatchers.Main) {
notificationsHandler.createNotification(
iconDrawable = R.drawable.ic_stat_handwashing,
largeIcon = R.drawable.handwashing_app_logo,
title = title,
content = content,
longContent = content
)
}
return null
}
protected fun catchBlock(e: Exception): Result {
if (runAttemptCount >= maxRetries) {
Timber.d("Exceeded max attempts: $maxRetries")
return Result.failure()
}
return when (e.cause) {
is IllegalStateException -> {
Timber.w(e, "IllegalStateException on worker class")
shouldScheduleNext = false
Result.retry()
}
else -> {
Timber.e(e, "Uncaught exception on worker class")
Result.failure()
}
}
}
protected fun scheduleNext(data: Data?) {
val workManager = WorkManager.getInstance(applicationContext)
val nextExecutionDelay = runAt(workParams.hour, workParams.minute)
Timber.d("Executing $workUniqueName in ${nextExecutionDelay / 1000} s")
val jobRequest = with(OneTimeWorkRequest.Builder(clazz)) {
data?.let { setInputData(it) }
setInitialDelay(nextExecutionDelay, TimeUnit.MILLISECONDS)
setConstraints(workConstraints)
build()
}
workManager.enqueueUniqueWork(
workUniqueName, ExistingWorkPolicy.REPLACE, jobRequest
)
}
private fun getString(@StringRes resId: Int): String =
applicationContext.getString(resId)
private fun getText(@StringRes resId: Int): CharSequence =
applicationContext.getText(resId)
private fun getStringArray(@ArrayRes resId: Int): Array<out String> =
applicationContext.resources.getStringArray(resId)
}