|
| 1 | +package com.aliucord.manager.ui.util |
| 2 | + |
| 3 | +import android.Manifest |
| 4 | +import android.app.* |
| 5 | +import android.content.Context |
| 6 | +import android.content.Intent |
| 7 | +import android.content.pm.PackageManager |
| 8 | +import android.os.Build |
| 9 | +import android.util.Log |
| 10 | +import androidx.annotation.StringRes |
| 11 | +import androidx.core.app.* |
| 12 | +import androidx.core.content.ContextCompat |
| 13 | +import com.aliucord.manager.* |
| 14 | +import com.aliucord.manager.util.findActivity |
| 15 | + |
| 16 | +object InstallNotifications { |
| 17 | + private const val CHANNEL_ID = "installation" |
| 18 | + |
| 19 | + /** |
| 20 | + * Creates or replaces a notification with id [id] that brings |
| 21 | + * up the existing [MainActivity] when clicked upon. |
| 22 | + * |
| 23 | + * @param id A unique notification ID for different notifications |
| 24 | + * @param title Main notification title |
| 25 | + * @param description Notification description |
| 26 | + */ |
| 27 | + fun createNotification( |
| 28 | + context: Context, |
| 29 | + id: Int, |
| 30 | + @StringRes title: Int, |
| 31 | + @StringRes description: Int, |
| 32 | + ) { |
| 33 | + val manager = NotificationManagerCompat.from(context) |
| 34 | + |
| 35 | + // Create the target notification channel |
| 36 | + if (Build.VERSION.SDK_INT >= 26 && manager.getNotificationChannel(CHANNEL_ID) == null) { |
| 37 | + val channel = NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManager.IMPORTANCE_HIGH) |
| 38 | + .setName(context.getString(R.string.notif_group_install_title)) |
| 39 | + .setDescription(context.getString(R.string.notif_group_install_desc)) |
| 40 | + .build() |
| 41 | + |
| 42 | + manager.createNotificationChannel(channel) |
| 43 | + } |
| 44 | + |
| 45 | + val notification = NotificationCompat.Builder(context, CHANNEL_ID) |
| 46 | + .setDefaults(Notification.DEFAULT_LIGHTS or Notification.DEFAULT_SOUND) |
| 47 | + .setAutoCancel(true) |
| 48 | + .setSmallIcon(R.drawable.ic_aliucord_logo) |
| 49 | + .setContentTitle(context.getString(title)) |
| 50 | + .setContentText(context.getString(description)) |
| 51 | + .setContentIntent( |
| 52 | + PendingIntent.getActivity( |
| 53 | + /* context = */ context, |
| 54 | + /* requestCode = */ 0, |
| 55 | + /* intent = */ |
| 56 | + Intent(context, MainActivity::class.java) |
| 57 | + .addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT), |
| 58 | + /* flags = */ PendingIntent.FLAG_IMMUTABLE, |
| 59 | + ) |
| 60 | + ) |
| 61 | + .build() |
| 62 | + |
| 63 | + try { |
| 64 | + manager.notify(id, notification) |
| 65 | + } catch (e: SecurityException) { |
| 66 | + Log.w(BuildConfig.TAG, "Failed to send install notification", e) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Request the `POST_NOTIFICATIONS` permission if needed. |
| 72 | + */ |
| 73 | + fun requestPermissions(context: Context) { |
| 74 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return |
| 75 | + |
| 76 | + val granted = ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) |
| 77 | + val activity = context.findActivity() ?: return |
| 78 | + |
| 79 | + if (granted != PackageManager.PERMISSION_GRANTED) { |
| 80 | + ActivityCompat.requestPermissions( |
| 81 | + activity, |
| 82 | + arrayOf(Manifest.permission.POST_NOTIFICATIONS), |
| 83 | + 0, |
| 84 | + ) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments