-
Notifications
You must be signed in to change notification settings - Fork 524
Expand file tree
/
Copy pathChannelManager.kt
More file actions
151 lines (134 loc) · 4.69 KB
/
ChannelManager.kt
File metadata and controls
151 lines (134 loc) · 4.69 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
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
package app.tauri.notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.ContentResolver
import android.content.Context
import android.graphics.Color
import android.media.AudioAttributes
import android.net.Uri
import android.os.Build
import app.tauri.Logger
import app.tauri.annotation.InvokeArg
import app.tauri.plugin.Invoke
import com.fasterxml.jackson.annotation.JsonValue
enum class Importance(@JsonValue val value: Int) {
None(0),
Min(1),
Low(2),
Default(3),
High(4);
}
enum class Visibility(@JsonValue val value: Int) {
Secret(-1),
Private(0),
Public(1);
}
@InvokeArg
class Channel {
lateinit var id: String
lateinit var name: String
var description: String? = null
var sound: String? = null
var lights: Boolean? = null
var lightsColor: String? = null
var vibration: Boolean? = null
var importance: Importance? = null
var visibility: Visibility? = null
}
@InvokeArg
class DeleteChannelArgs {
lateinit var id: String
}
class ChannelManager(private var context: Context) {
private var notificationManager: NotificationManager? = null
init {
notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
}
fun createChannel(invoke: Invoke) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = invoke.parseArgs(Channel::class.java)
createChannel(channel)
invoke.resolve()
} else {
invoke.reject("channel not available")
}
}
private fun createChannel(channel: Channel) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(
channel.id,
channel.name,
(channel.importance ?: Importance.Default).value
)
notificationChannel.description = channel.description
notificationChannel.lockscreenVisibility = (channel.visibility ?: Visibility.Private).value
notificationChannel.enableVibration(channel.vibration ?: false)
notificationChannel.enableLights(channel.lights ?: false)
val lightColor = channel.lightsColor ?: ""
if (lightColor.isNotEmpty()) {
try {
notificationChannel.lightColor = Color.parseColor(lightColor)
} catch (ex: IllegalArgumentException) {
Logger.error(
NOTIFICATION_CHANNEL_LOG_TAGS,
"Invalid color provided for light color.",
null
)
}
}
var sound = channel.sound ?: ""
if (sound.isNotEmpty()) {
if (sound.contains(".")) {
sound = sound.substring(0, sound.lastIndexOf('.'))
}
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
val soundUri =
Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.packageName + "/raw/" + sound)
notificationChannel.setSound(soundUri, audioAttributes)
}
notificationManager?.createNotificationChannel(notificationChannel)
}
}
fun deleteChannel(invoke: Invoke) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val args = invoke.parseArgs(DeleteChannelArgs::class.java)
notificationManager?.deleteNotificationChannel(args.id)
invoke.resolve()
} else {
invoke.reject("channel not available")
}
}
fun listChannels(invoke: Invoke) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannels: List<NotificationChannel> =
notificationManager?.notificationChannels ?: listOf()
val channels = mutableListOf<Channel>()
for (notificationChannel in notificationChannels) {
val channel = Channel()
channel.id = notificationChannel.id
channel.name = notificationChannel.name.toString()
channel.description = notificationChannel.description
channel.sound = notificationChannel.sound.toString()
channel.lights = notificationChannel.shouldShowLights()
String.format(
"#%06X",
0xFFFFFF and notificationChannel.lightColor
)
channel.vibration = notificationChannel.shouldVibrate()
channel.importance = Importance.values().firstOrNull { it.value == notificationChannel.importance }
channel.visibility = Visibility.values().firstOrNull { it.value == notificationChannel.lockscreenVisibility }
channels.add(channel)
}
invoke.resolveObject(channels)
} else {
invoke.reject("channel not available")
}
}
}