diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/context/AppNotificationManager.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/context/AppNotificationManager.kt | 81 |
1 files changed, 35 insertions, 46 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/context/AppNotificationManager.kt b/app/src/main/java/org/pacien/tincapp/context/AppNotificationManager.kt index d6e21f5..29f72de 100644 --- a/app/src/main/java/org/pacien/tincapp/context/AppNotificationManager.kt +++ b/app/src/main/java/org/pacien/tincapp/context/AppNotificationManager.kt | |||
@@ -18,70 +18,59 @@ | |||
18 | 18 | ||
19 | package org.pacien.tincapp.context | 19 | package org.pacien.tincapp.context |
20 | 20 | ||
21 | import android.app.NotificationChannel | ||
22 | import android.app.NotificationManager | ||
23 | import android.content.Context | 21 | import android.content.Context |
24 | import android.content.Intent | 22 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener |
25 | import android.net.Uri | ||
26 | import android.os.Build | ||
27 | import androidx.annotation.RequiresApi | ||
28 | import androidx.core.app.NotificationCompat | ||
29 | import androidx.core.app.NotificationManagerCompat | ||
30 | import org.pacien.tincapp.R | ||
31 | import org.pacien.tincapp.utils.PendingIntentUtils | ||
32 | 23 | ||
33 | /** | 24 | /** |
34 | * @author pacien | 25 | * @author pacien |
35 | */ | 26 | */ |
36 | class AppNotificationManager(private val context: Context) { | 27 | class AppNotificationManager(private val context: Context) { |
28 | data class ErrorNotification( | ||
29 | val title: String, | ||
30 | val message: String, | ||
31 | val manualLink: String? | ||
32 | ) | ||
33 | |||
37 | companion object { | 34 | companion object { |
38 | private const val ERROR_CHANNEL_ID = "org.pacien.tincapp.notification.channels.error" | 35 | private val STORE_NAME = this::class.java.`package`!!.name |
39 | const val ERROR_NOTIFICATION_ID = 0 | 36 | private const val STORE_KEY_TITLE = "title" |
37 | private const val STORE_KEY_MESSAGE = "message" | ||
38 | private const val STORE_KEY_MANUAL_LINK = "manual_link" | ||
40 | } | 39 | } |
41 | 40 | ||
42 | init { | 41 | private val store by lazy { context.getSharedPreferences(STORE_NAME, Context.MODE_PRIVATE)!! } |
43 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) registerChannels() | ||
44 | } | ||
45 | 42 | ||
46 | fun notifyError(title: String, message: String, manualLink: String? = null) { | 43 | fun getError(): ErrorNotification? { |
47 | val notification = NotificationCompat.Builder(context, ERROR_CHANNEL_ID) | 44 | if (!store.contains(STORE_KEY_TITLE)) return null; |
48 | .setSmallIcon(R.drawable.ic_warning_primary_24dp) | ||
49 | .setContentTitle(title) | ||
50 | .setContentText(message) | ||
51 | .setStyle(NotificationCompat.BigTextStyle().bigText(message)) | ||
52 | .setHighPriority() | ||
53 | .setAutoCancel(true) | ||
54 | .apply { if (manualLink != null) setManualLink(manualLink) } | ||
55 | .build() | ||
56 | 45 | ||
57 | NotificationManagerCompat.from(context) | 46 | return ErrorNotification( |
58 | .notify(ERROR_NOTIFICATION_ID, notification) | 47 | store.getString(STORE_KEY_TITLE, null)!!, |
48 | store.getString(STORE_KEY_MESSAGE, null)!!, | ||
49 | store.getString(STORE_KEY_MANUAL_LINK, null) | ||
50 | ) | ||
59 | } | 51 | } |
60 | 52 | ||
61 | fun dismissAll() { | 53 | fun notifyError(title: String, message: String, manualLink: String? = null) { |
62 | NotificationManagerCompat.from(context).cancelAll() | 54 | store |
55 | .edit() | ||
56 | .putString(STORE_KEY_TITLE, title) | ||
57 | .putString(STORE_KEY_MESSAGE, message) | ||
58 | .putString(STORE_KEY_MANUAL_LINK, manualLink) | ||
59 | .apply() | ||
63 | } | 60 | } |
64 | 61 | ||
65 | @RequiresApi(Build.VERSION_CODES.O) | 62 | fun dismissAll() { |
66 | private fun registerChannels() { | 63 | store |
67 | context.getSystemService(NotificationManager::class.java) | 64 | .edit() |
68 | .apply { | 65 | .clear() |
69 | createNotificationChannel(NotificationChannel( | 66 | .apply() |
70 | ERROR_CHANNEL_ID, | ||
71 | context.getString(R.string.notification_error_channel_name), | ||
72 | NotificationManager.IMPORTANCE_HIGH | ||
73 | )) | ||
74 | } | ||
75 | } | 67 | } |
76 | 68 | ||
77 | private fun NotificationCompat.Builder.setHighPriority() = apply { | 69 | fun registerListener(listener: OnSharedPreferenceChangeListener) { |
78 | priority = NotificationCompat.PRIORITY_MAX | 70 | store.registerOnSharedPreferenceChangeListener(listener) |
79 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) setDefaults(NotificationCompat.DEFAULT_SOUND) // force heads-up notification | ||
80 | } | 71 | } |
81 | 72 | ||
82 | private fun NotificationCompat.Builder.setManualLink(manualLink: String) = apply { | 73 | fun unregisterListener(listener: OnSharedPreferenceChangeListener) { |
83 | val intent = Intent(Intent.ACTION_VIEW, Uri.parse(manualLink)) | 74 | store.unregisterOnSharedPreferenceChangeListener(listener) |
84 | val pendingIntent = PendingIntentUtils.getActivity(context, 0, intent, 0) | ||
85 | addAction(R.drawable.ic_help_primary_24dp, context.getString(R.string.notification_error_action_open_manual), pendingIntent) | ||
86 | } | 75 | } |
87 | } | 76 | } |