aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/context/AppNotificationManager.kt
diff options
context:
space:
mode:
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.kt84
1 files changed, 84 insertions, 0 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
new file mode 100644
index 0000000..d543210
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/context/AppNotificationManager.kt
@@ -0,0 +1,84 @@
1/*
2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
3 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19package org.pacien.tincapp.context
20
21import android.app.NotificationChannel
22import android.app.NotificationManager
23import android.app.PendingIntent
24import android.content.Context
25import android.content.Intent
26import android.net.Uri
27import android.os.Build
28import android.support.annotation.RequiresApi
29import android.support.v4.app.NotificationCompat
30import android.support.v4.app.NotificationManagerCompat
31import org.pacien.tincapp.R
32
33/**
34 * @author pacien
35 */
36class AppNotificationManager(private val context: Context) {
37 companion object {
38 private const val CHANNEL_ID = "org.pacien.tincapp.notification.channels.error"
39 private const val ERROR_NOTIFICATION_ID = 0
40 }
41
42 init {
43 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) registerChannel()
44 }
45
46 fun notifyError(title: String, message: String, manualLink: String? = null) {
47 val notification = NotificationCompat.Builder(context, CHANNEL_ID)
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
57 NotificationManagerCompat.from(context)
58 .notify(ERROR_NOTIFICATION_ID, notification)
59 }
60
61 fun dismissAll() {
62 NotificationManagerCompat.from(context).cancelAll()
63 }
64
65 @RequiresApi(Build.VERSION_CODES.O)
66 private fun registerChannel() {
67 val name = context.getString(R.string.notification_channel_error_name)
68 val importance = NotificationManager.IMPORTANCE_HIGH
69 val channel = NotificationChannel(CHANNEL_ID, name, importance)
70 val notificationManager = context.getSystemService(NotificationManager::class.java)
71 notificationManager.createNotificationChannel(channel)
72 }
73
74 private fun NotificationCompat.Builder.setHighPriority() = apply {
75 priority = NotificationCompat.PRIORITY_MAX
76 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) setDefaults(NotificationCompat.DEFAULT_SOUND) // force heads-up notification
77 }
78
79 private fun NotificationCompat.Builder.setManualLink(manualLink: String) = apply {
80 val intent = Intent(Intent.ACTION_VIEW, Uri.parse(manualLink))
81 val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
82 addAction(R.drawable.ic_help_primary_24dp, context.getString(R.string.action_open_manual), pendingIntent)
83 }
84}