Skip to content

Commit 03a37b9

Browse files
committed
fix(Android): Make sure the notification channel is created before opening the notification channel settings #1110
1 parent d6c41b1 commit 03a37b9

2 files changed

Lines changed: 53 additions & 30 deletions

File tree

common/src/androidMain/kotlin/com/artemchep/keyguard/android/clipboard/KeyguardClipboardService.kt

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,40 @@ class KeyguardClipboardService : Service(), DIAware {
102102
) = Intent(context, KeyguardClipboardService::class.java).apply {
103103
putExtra(KEY_ARGUMENTS, args)
104104
}
105+
106+
//
107+
// Notification channel
108+
//
109+
110+
fun createNotificationChannel(
111+
context: Context,
112+
clipboardService: ClipboardService,
113+
): String {
114+
val channelImportance = if (clipboardService.hasCopyNotification()) {
115+
NotificationManager.IMPORTANCE_DEFAULT
116+
} else {
117+
NotificationManager.IMPORTANCE_HIGH
118+
}
119+
val channel = kotlin.run {
120+
val id = context.getString(R.string.notification_clipboard_channel_id)
121+
val name = context.getString(R.string.notification_clipboard_channel_name)
122+
NotificationChannel(id, name, channelImportance)
123+
}
124+
channel.enableVibration(false)
125+
val audioUri = getAudioUri(context)
126+
val audioAttributes = AudioAttributes.Builder()
127+
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
128+
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
129+
.build()
130+
channel.setSound(audioUri, audioAttributes)
131+
val nm = context.getSystemService<NotificationManager>()!!
132+
nm.createNotificationChannel(channel)
133+
return channel.id
134+
}
135+
136+
private fun getAudioUri(context: Context) =
137+
"${ContentResolver.SCHEME_ANDROID_RESOURCE}://${context.packageName}/${R.raw.silence}"
138+
.toUri()
105139
}
106140

107141
sealed interface Args : Parcelable {
@@ -394,7 +428,7 @@ class KeyguardClipboardService : Service(), DIAware {
394428
else -> R.drawable.ic_number_9_plus
395429
}
396430

397-
val contentTitle = if (autoCopy) {
431+
val contentTitle = if (autoCopy) {
398432
when (type) {
399433
CopyValueEvent.Type.TOTP -> org.jetbrains.compose.resources.getString(Res.string.copied_otp_code)
400434
CopyValueEvent.Type.VALUE -> org.jetbrains.compose.resources.getString(Res.string.copied_value)
@@ -452,7 +486,7 @@ class KeyguardClipboardService : Service(), DIAware {
452486
}
453487

454488
val channelId = createNotificationChannel()
455-
val audioUri = getAudioUri()
489+
val audioUri = getAudioUri(this)
456490
return NotificationCompat.Builder(this, channelId)
457491
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
458492
.setContentTitle(contentTitle)
@@ -468,30 +502,8 @@ class KeyguardClipboardService : Service(), DIAware {
468502
.build()
469503
}
470504

471-
private fun createNotificationChannel(): String {
472-
val channelImportance = if (clipboardService.hasCopyNotification()) {
473-
NotificationManager.IMPORTANCE_DEFAULT
474-
} else {
475-
NotificationManager.IMPORTANCE_HIGH
476-
}
477-
val channel = kotlin.run {
478-
val id = getString(R.string.notification_clipboard_channel_id)
479-
val name = getString(R.string.notification_clipboard_channel_name)
480-
NotificationChannel(id, name, channelImportance)
481-
}
482-
channel.enableVibration(false)
483-
val audioUri = getAudioUri()
484-
val audioAttributes = AudioAttributes.Builder()
485-
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
486-
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
487-
.build()
488-
channel.setSound(audioUri, audioAttributes)
489-
val nm = getSystemService<NotificationManager>()!!
490-
nm.createNotificationChannel(channel)
491-
return channel.id
492-
}
493-
494-
private fun getAudioUri() =
495-
"${ContentResolver.SCHEME_ANDROID_RESOURCE}://${applicationContext.packageName}/${R.raw.silence}"
496-
.toUri()
505+
private fun createNotificationChannel(): String = createNotificationChannel(
506+
context = this,
507+
clipboardService = clipboardService,
508+
)
497509
}

common/src/androidMain/kotlin/com/artemchep/keyguard/feature/home/settings/component/SettingClipboardNotificationSettings.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import androidx.compose.runtime.Composable
88
import androidx.compose.runtime.getValue
99
import androidx.compose.runtime.rememberUpdatedState
1010
import androidx.compose.ui.platform.LocalContext
11+
import com.artemchep.keyguard.android.clipboard.KeyguardClipboardService
1112
import com.artemchep.keyguard.android.util.launchNotificationChannelSettingsOrThrow
1213
import com.artemchep.keyguard.common.R
1314
import com.artemchep.keyguard.common.model.ToastMessage
15+
import com.artemchep.keyguard.common.service.clipboard.ClipboardService
1416
import com.artemchep.keyguard.common.usecase.ShowMessage
1517
import com.artemchep.keyguard.feature.home.settings.LocalSettingItemShape
1618
import com.artemchep.keyguard.feature.home.vault.component.FlatItemSimpleExpressive
@@ -40,6 +42,8 @@ fun settingClipboardNotificationSettingsProvider(
4042
fun SettingClipboardNotificationSettings(
4143
) {
4244
val showMessage by rememberInstance<ShowMessage>()
45+
val clipboardService by rememberInstance<ClipboardService>()
46+
4347
val updatedContext by rememberUpdatedState(LocalContext.current)
4448
FlatItemSimpleExpressive(
4549
shapeState = LocalSettingItemShape.current,
@@ -53,9 +57,16 @@ fun SettingClipboardNotificationSettings(
5357
ChevronIcon()
5458
},
5559
onClick = {
60+
// Make sure that the notification channel is created before
61+
// attempting to launch the notification channel settings.
62+
val channelId = updatedContext
63+
.getString(R.string.notification_clipboard_channel_id)
64+
KeyguardClipboardService.createNotificationChannel(
65+
context = updatedContext,
66+
clipboardService = clipboardService,
67+
)
68+
5669
try {
57-
val channelId = updatedContext
58-
.getString(R.string.notification_clipboard_channel_id)
5970
updatedContext.launchNotificationChannelSettingsOrThrow(channelId)
6071
} catch (e: Exception) {
6172
val msg = ToastMessage(

0 commit comments

Comments
 (0)