package eu.friedcutlet.yokan.services.notifications import eu.friedcutlet.yokan.AndroidAppContext import eu.friedcutlet.yokan.AndroidForegroundActivity import android.Manifest import android.app.Notification import android.app.NotificationChannel as AndroidChannel import android.app.NotificationManager import android.content.Context import android.content.pm.PackageManager import android.os.Build import eu.friedcutlet.yokan.domain.ProfileId actual fun createPlatformNotificationService(profileId: ProfileId): NotificationService = AndroidNotificationService(profileId) private val ChannelDefinitions = mapOf( NotificationChannel.Downloads to ChannelInfo("yokan_downloads", "Downloads"), NotificationChannel.LibraryUpdates to ChannelInfo("yokan_library_updates", "Library updates"), NotificationChannel.Backup to ChannelInfo("yokan_backup", "Backup"), NotificationChannel.Sync to ChannelInfo("yokan_sync", "Sync"), ) private data class ChannelInfo(val id: String, val label: String) /** The Android id of [channel], created on first use; the download foreground service posts on it too. */ internal fun NotificationManager.ensureChannel(channel: NotificationChannel): String? { val info = ChannelDefinitions[channel] ?: return null if (getNotificationChannel(info.id) == null) { createNotificationChannel(AndroidChannel(info.id, info.label, NotificationManager.IMPORTANCE_LOW)) } return info.id } private class AndroidNotificationService(profileId: ProfileId) : NotificationService { private val namespace = ProfileNotificationNamespace(profileId) override fun supported(): Boolean = AndroidAppContext.appContext != null override fun requestPermission() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return val activity = AndroidForegroundActivity.activity ?: return if (activity.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { activity.requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1001) } } override fun notify(payload: NotificationPayload) { val context = AndroidAppContext.appContext ?: return if (!hasNotificationPermission(context)) return val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager ?: return val channelId = manager.ensureChannel(payload.channel) ?: return val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Notification.Builder(context, channelId) } else { @Suppress("DEPRECATION") Notification.Builder(context) } builder .setContentTitle(payload.title) .setContentText(payload.message) .setSmallIcon(context.applicationInfo.icon) .setAutoCancel(true) runCatching { manager.notify(namespace.androidTag(payload.channel, payload.tag), namespace.androidId(channelId, payload.tag), builder.build()) } } override fun cancel(channel: NotificationChannel, tag: String?) { val context = AndroidAppContext.appContext ?: return val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager ?: return val info = ChannelDefinitions[channel] ?: return manager.cancel(namespace.androidTag(channel, tag), namespace.androidId(info.id, tag)) } private fun hasNotificationPermission(context: Context): Boolean = Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || context.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED }