-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
NotificationManagerImpl.kt
332 lines (283 loc) · 13.8 KB
/
NotificationManagerImpl.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS 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.
*
* QKSMS 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 QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package common.util
import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ContentUris
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat
import android.support.v4.app.RemoteInput
import android.support.v4.app.TaskStackBuilder
import android.telephony.PhoneNumberUtils
import com.moez.QKSMS.R
import common.util.extensions.dpToPx
import feature.compose.ComposeActivity
import feature.qkreply.QkReplyActivity
import mapper.CursorToPartImpl
import receiver.MarkReadReceiver
import receiver.MarkSeenReceiver
import receiver.RemoteMessagingReceiver
import repository.MessageRepository
import util.Preferences
import util.extensions.isImage
import util.tryOrNull
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class NotificationManagerImpl @Inject constructor(
private val context: Context,
private val prefs: Preferences,
private val colors: Colors,
private val messageRepo: MessageRepository
) : manager.NotificationManager {
companion object {
const val DEFAULT_CHANNEL_ID = "notifications_default"
val VIBRATE_PATTERN = longArrayOf(0, 200, 0, 200)
}
private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
init {
@SuppressLint("NewApi")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "Default"
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(DEFAULT_CHANNEL_ID, name, importance).apply {
enableLights(true)
lightColor = Color.WHITE
enableVibration(true)
vibrationPattern = VIBRATE_PATTERN
}
notificationManager.createNotificationChannel(channel)
}
}
/**
* Updates the notification for a particular conversation
*/
override fun update(threadId: Long) {
// If notifications are disabled, don't do anything
if (!prefs.notifications(threadId).get()) {
return
}
val messages = messageRepo.getUnreadUnseenMessages(threadId)
// If there are no messages to be displayed, make sure that the notification is dismissed
if (messages.isEmpty()) {
notificationManager.cancel(threadId.toInt())
return
}
val conversation = messageRepo.getConversation(threadId) ?: return
val contentIntent = Intent(context, ComposeActivity::class.java).putExtra("threadId", threadId)
val taskStackBuilder = TaskStackBuilder.create(context)
taskStackBuilder.addParentStack(ComposeActivity::class.java)
taskStackBuilder.addNextIntent(contentIntent)
val contentPI = taskStackBuilder.getPendingIntent(threadId.toInt() + 10000, PendingIntent.FLAG_UPDATE_CURRENT)
val seenIntent = Intent(context, MarkSeenReceiver::class.java).putExtra("threadId", threadId)
val seenPI = PendingIntent.getBroadcast(context, threadId.toInt() + 20000, seenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val readIntent = Intent(context, MarkReadReceiver::class.java).putExtra("threadId", threadId)
val readPI = PendingIntent.getBroadcast(context, threadId.toInt() + 30000, readIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val readAction = NotificationCompat.Action(R.drawable.ic_check_white_24dp, context.getString(R.string.notification_read), readPI)
// We can't store a null preference, so map it to a null Uri if the pref string is empty
val ringtone = prefs.ringtone(threadId).get()
.takeIf { it.isNotEmpty() }
?.let(Uri::parse)
val notification = NotificationCompat.Builder(context, getChannelIdForNotification(threadId))
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setColor(colors.theme(threadId).theme)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_notification)
.setNumber(messages.size)
.setAutoCancel(true)
.setContentIntent(contentPI)
.setDeleteIntent(seenPI)
.addAction(readAction)
.setSound(ringtone)
.setLights(Color.WHITE, 500, 2000)
.setVibrate(if (prefs.vibration(threadId).get()) VIBRATE_PATTERN else longArrayOf(0))
val messagingStyle = NotificationCompat.MessagingStyle("Me")
if (conversation.recipients.size >= 2) {
messagingStyle.conversationTitle = conversation.getTitle()
}
messages.forEach { message ->
val name = when {
message.isMe() -> null
conversation.recipients.size < 2 -> conversation.getTitle()
else -> conversation.recipients
.firstOrNull { PhoneNumberUtils.compare(it.address, message.address) }
?.getDisplayName()
?: message.address
}
NotificationCompat.MessagingStyle.Message(message.getSummary(), message.date, name).apply {
message.parts.firstOrNull { it.isImage() }?.let { part ->
setData(part.type, ContentUris.withAppendedId(CursorToPartImpl.CONTENT_URI, part.id))
}
messagingStyle.addMessage(this)
}
}
val avatar = conversation.recipients.takeIf { it.size == 1 }
?.first()?.address
?.let { address ->
GlideApp.with(context)
.asBitmap()
.circleCrop()
.load(PhoneNumberUtils.stripSeparators(address))
.submit(64.dpToPx(context), 64.dpToPx(context))
}
?.let { futureGet -> tryOrNull { futureGet.get() } }
when (prefs.notificationPreviews(threadId).get()) {
Preferences.NOTIFICATION_PREVIEWS_ALL -> {
notification
.setLargeIcon(avatar)
.setStyle(messagingStyle)
}
Preferences.NOTIFICATION_PREVIEWS_NAME -> {
notification
.setLargeIcon(avatar)
.setContentTitle(conversation.getTitle())
.setContentText(context.resources.getQuantityString(R.plurals.notification_new_messages, messages.size, messages.size))
}
Preferences.NOTIFICATION_PREVIEWS_NONE -> {
notification
.setContentTitle(context.getString(R.string.app_name))
.setContentText(context.resources.getQuantityString(R.plurals.notification_new_messages, messages.size, messages.size))
}
}
// Add all of the people from this conversation to the notification, so that the system can
// appropriately bypass DND mode
conversation.recipients
.mapNotNull { recipient -> recipient.contact?.lookupKey }
.forEach { uri -> notification.addPerson(uri) }
if (Build.VERSION.SDK_INT >= 24) {
notification.addAction(getReplyAction(threadId))
} else {
val replyIntent = Intent(context, QkReplyActivity::class.java).putExtra("threadId", threadId)
val replyPI = PendingIntent.getActivity(context, threadId.toInt() + 40000, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val replyAction = NotificationCompat.Action(R.drawable.ic_reply_white_24dp, context.getString(R.string.notification_reply), replyPI)
notification.addAction(replyAction)
}
if (prefs.qkreply.get()) {
notification.priority = NotificationCompat.PRIORITY_DEFAULT
val intent = Intent(context, QkReplyActivity::class.java)
.putExtra("threadId", threadId)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
notificationManager.notify(threadId.toInt(), notification.build())
}
override fun notifyFailed(msgId: Long) {
val message = messageRepo.getMessage(msgId)
if (message == null || !message.isFailedMessage()) {
return
}
val conversation = messageRepo.getConversation(message.threadId) ?: return
val threadId = conversation.id
val contentIntent = Intent(context, ComposeActivity::class.java).putExtra("threadId", threadId)
val taskStackBuilder = TaskStackBuilder.create(context)
taskStackBuilder.addParentStack(ComposeActivity::class.java)
taskStackBuilder.addNextIntent(contentIntent)
val contentPI = taskStackBuilder.getPendingIntent(threadId.toInt() + 40000, PendingIntent.FLAG_UPDATE_CURRENT)
val notification = NotificationCompat.Builder(context, getChannelIdForNotification(threadId))
.setContentTitle(context.getString(R.string.notification_message_failed_title))
.setContentText(context.getString(R.string.notification_message_failed_text, conversation.getTitle()))
.setColor(colors.theme(threadId).theme)
.setPriority(NotificationManagerCompat.IMPORTANCE_MAX)
.setSmallIcon(R.drawable.ic_notification_failed)
.setAutoCancel(true)
.setContentIntent(contentPI)
.setSound(Uri.parse(prefs.ringtone(threadId).get()))
.setLights(Color.WHITE, 500, 2000)
.setVibrate(if (prefs.vibration(threadId).get()) VIBRATE_PATTERN else longArrayOf(0))
notificationManager.notify(threadId.toInt() + 50000, notification.build())
}
private fun getReplyAction(threadId: Long): NotificationCompat.Action {
val replyIntent = Intent(context, RemoteMessagingReceiver::class.java).putExtra("threadId", threadId)
val replyPI = PendingIntent.getBroadcast(context, threadId.toInt() + 40000, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val responseSet = context.resources.getStringArray(R.array.qk_responses)
val remoteInput = RemoteInput.Builder("body")
.setLabel(context.getString(R.string.notification_reply))
.setChoices(responseSet)
.build()
return NotificationCompat.Action.Builder(
R.drawable.ic_reply_white_24dp,
context.getString(R.string.notification_reply), replyPI)
.addRemoteInput(remoteInput)
.build()
}
/**
* Creates a notification channel for the given conversation
*/
override fun createNotificationChannel(threadId: Long) {
// Only proceed if the android version supports notification channels
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
messageRepo.getConversation(threadId)?.let { conversation ->
val channelId = buildNotificationChannelId(threadId)
val name = conversation.getTitle()
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(channelId, name, importance).apply {
enableLights(true)
lightColor = Color.WHITE
enableVibration(true)
vibrationPattern = VIBRATE_PATTERN
}
notificationManager.createNotificationChannel(channel)
}
}
/**
* Returns the notification channel for the given conversation, or null if it doesn't exist
*/
private fun getNotificationChannel(threadId: Long): NotificationChannel? {
val channelId = buildNotificationChannelId(threadId)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return notificationManager
.notificationChannels
.firstOrNull { channel -> channel.id == channelId }
}
return null
}
/**
* Returns the channel id that should be used for a notification based on the threadId
*
* If a notification channel for the conversation exists, use the id for that. Otherwise return
* the default channel id
*/
private fun getChannelIdForNotification(threadId: Long): String {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = buildNotificationChannelId(threadId)
return notificationManager
.notificationChannels
.map { channel -> channel.id }
.firstOrNull { id -> id == channelId }
?: DEFAULT_CHANNEL_ID
}
return DEFAULT_CHANNEL_ID
}
/**
* Formats a notification channel id for a given thread id, whether the channel exists or not
*/
override fun buildNotificationChannelId(threadId: Long): String {
return when (threadId) {
0L -> DEFAULT_CHANNEL_ID
else -> "notifications_$threadId"
}
}
}