-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathNotifications.kt
More file actions
256 lines (236 loc) · 10.2 KB
/
Notifications.kt
File metadata and controls
256 lines (236 loc) · 10.2 KB
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
package com.afkanerd.deku.DefaultSMS.Models
import android.app.Activity.RESULT_OK
import android.app.Notification
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.LocusId
import android.content.pm.PackageManager
import android.content.pm.ShortcutInfo
import android.graphics.drawable.Icon
import android.os.Build
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.unit.dp
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.Person
import androidx.core.app.RemoteInput
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getString
import androidx.core.content.LocusIdCompat
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import androidx.core.graphics.drawable.RoundedBitmapDrawable
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import androidx.core.graphics.drawable.toBitmap
import androidx.preference.PreferenceManager
import coil3.compose.AsyncImage
import com.afkanerd.deku.DefaultSMS.Commons.Helpers
import com.afkanerd.deku.DefaultSMS.R
import com.afkanerd.deku.DefaultSMS.ui.Components.ConvenientMethods
import com.google.android.material.color.MaterialColors
object Notifications {
const val KEY_TEXT_REPLY = "KEY_TEXT_REPLY"
fun createNotification(
context: Context,
title: String,
text: String,
address: String,
requestCode: Int,
contentIntent: Intent,
replyIntent: Intent? = null,
muteIntent: Intent? = null,
markAsRead: Intent? = null,
) : NotificationCompat.Builder {
val channelId = getString(context, R.string.incoming_messages_channel_id)
var replyLabel = getString(context, R.string.notifications_reply_label)
var remoteInput = RemoteInput.Builder(KEY_TEXT_REPLY).run {
setLabel(replyLabel)
build()
}
var pendingIntent = PendingIntent
.getActivity(
context,
requestCode,
contentIntent,
PendingIntent.FLAG_MUTABLE
)
// Build a PendingIntent for the reply action to trigger.
var replyPendingIntent = if(replyIntent == null) null else
PendingIntent.getBroadcast(
context,
requestCode,
replyIntent,
PendingIntent.FLAG_MUTABLE
)
var mutePendingIntent = if(muteIntent == null) null else
PendingIntent.getBroadcast(
context,
requestCode,
muteIntent,
PendingIntent.FLAG_MUTABLE
)
var markAsReadPendingIntent = if(markAsRead == null) null else
PendingIntent.getBroadcast(
context,
requestCode,
markAsRead,
PendingIntent.FLAG_MUTABLE
)
var replyAction: NotificationCompat.Action? =
if(replyPendingIntent == null || Helpers.isShortCode(address)) null else
NotificationCompat.Action.Builder(
null,
getString(context, R.string.notifications_reply_label),
replyPendingIntent
)
.addRemoteInput(RemoteInput.Builder("extra_remote_reply").setLabel("").build())
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
.setShowsUserInterface(false)
.build()
// .addRemoteInput(remoteInput)
// .setAllowGeneratedReplies(true)
// .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
// .setShowsUserInterface(false)
// .build()
var muteAction: NotificationCompat.Action? = if(mutePendingIntent == null) null else
NotificationCompat.Action.Builder(
null,
getString(context, R.string.conversation_menu_mute),
mutePendingIntent
)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MUTE)
.build()
var markAsReadAction: NotificationCompat.Action? = if(markAsReadPendingIntent == null) null else
NotificationCompat.Action.Builder(
null,
getString(context, R.string.notifications_mark_as_read_label),
markAsReadPendingIntent
)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
.build()
val bitmap = Contacts.getContactBitmapPhoto(context, address)
val icon = if(bitmap != null) {
IconCompat.createWithBitmap(
ConvenientMethods.getRoundedCornerImageBitmap(bitmap.asImageBitmap(), 100))
} else {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM)
IconCompat.createWithResource(context, R.drawable.baseline_account_circle_24)
else null
}
val user = Person.Builder()
.setIcon(icon)
.setName(title)
.setKey(address)
.setBot(false)
.build()
val bubbleMetadata = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
NotificationCompat.BubbleMetadata.Builder(
pendingIntent,
icon ?:
IconCompat.createWithResource(context, R.drawable.baseline_account_circle_24)
)
.setDesiredHeight(600)
.build()
else null
val shortcut = ShortcutInfoCompat.Builder(context, address)
.setIntent(contentIntent.apply {
setAction(Intent.ACTION_DEFAULT)
})
.setCategories(setOf(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION))
.setShortLabel(user.name!!)
.setLongLived(true)
.setPerson(user)
.build()
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut);
return NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_stat_name)
.setColor(context.getColor(R.color.md_theme_primary))
.setContentTitle(title)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setAllowSystemGeneratedContextualActions(true)
.addAction(muteAction)
.addAction(markAsReadAction)
.addAction(replyAction)
.setShortcutId(address)
.setBubbleMetadata(bubbleMetadata)
.setLocusId(
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
LocusIdCompat.toLocusIdCompat(LocusId(address))
else null
)
.setStyle(
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
NotificationCompat.MessagingStyle(title)
.addMessage(text, System.currentTimeMillis(), address)
} else {
// NotificationCompat.MessagingStyle(user)
// .addMessage(text, System.currentTimeMillis(), user)
NotificationCompat.MessagingStyle(user).addMessage(
NotificationCompat.MessagingStyle.Message(
text,
System.currentTimeMillis(),
user
)
)
}
)
}
fun notify(
context: Context,
builder: NotificationCompat.Builder,
notificationId: Int
) {
with(NotificationManagerCompat.from(context)) {
if (ActivityCompat.checkSelfPermission(
context,
android.Manifest.permission.POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED
) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
// grantResults: IntArray)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return@with
}
// notificationId is a unique int for each notification that you must define.
notify(notificationId, builder.build())
}
}
fun getPreviousNotifications(context: Context):
NotificationCompat.MessagingStyle? {
var messagingStyle: NotificationCompat.MessagingStyle? = null
with(NotificationManagerCompat.from(context)) {
val title = activeNotifications.first().notification.extras
.getCharSequence(Notification.EXTRA_TITLE)
val text = activeNotifications.first().notification.extras
.getCharSequence(Notification.EXTRA_TEXT)
val person = Person.Builder().setName(title).build()
messagingStyle = NotificationCompat.MessagingStyle(person)
.setConversationTitle(title)
.addMessage(text, System.currentTimeMillis(), person)
}
return messagingStyle
}
}