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 | 39 |
1 files changed, 28 insertions, 11 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 38bf6e4..9d731a5 100644 --- a/app/src/main/java/org/pacien/tincapp/context/AppNotificationManager.kt +++ b/app/src/main/java/org/pacien/tincapp/context/AppNotificationManager.kt | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | 2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon |
3 | * Copyright (C) 2017-2019 Pacien TRAN-GIRARD | 3 | * Copyright (C) 2017-2020 Pacien TRAN-GIRARD |
4 | * | 4 | * |
5 | * This program is free software: you can redistribute it and/or modify | 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 | 6 | * it under the terms of the GNU General Public License as published by |
@@ -18,6 +18,7 @@ | |||
18 | 18 | ||
19 | package org.pacien.tincapp.context | 19 | package org.pacien.tincapp.context |
20 | 20 | ||
21 | import android.app.Notification | ||
21 | import android.app.NotificationChannel | 22 | import android.app.NotificationChannel |
22 | import android.app.NotificationManager | 23 | import android.app.NotificationManager |
23 | import android.app.PendingIntent | 24 | import android.app.PendingIntent |
@@ -35,16 +36,19 @@ import org.pacien.tincapp.R | |||
35 | */ | 36 | */ |
36 | class AppNotificationManager(private val context: Context) { | 37 | class AppNotificationManager(private val context: Context) { |
37 | companion object { | 38 | companion object { |
38 | private const val CHANNEL_ID = "org.pacien.tincapp.notification.channels.error" | 39 | private const val ERROR_CHANNEL_ID = "org.pacien.tincapp.notification.channels.error" |
39 | private const val ERROR_NOTIFICATION_ID = 0 | 40 | private const val CONFIG_ACCESS_CHANNEL_ID = "org.pacien.tincapp.notification.channels.configuration" |
41 | |||
42 | const val ERROR_NOTIFICATION_ID = 0 | ||
43 | const val CONFIG_ACCESS_NOTIFICATION_ID = 1 | ||
40 | } | 44 | } |
41 | 45 | ||
42 | init { | 46 | init { |
43 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) registerChannel() | 47 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) registerChannels() |
44 | } | 48 | } |
45 | 49 | ||
46 | fun notifyError(title: String, message: String, manualLink: String? = null) { | 50 | fun notifyError(title: String, message: String, manualLink: String? = null) { |
47 | val notification = NotificationCompat.Builder(context, CHANNEL_ID) | 51 | val notification = NotificationCompat.Builder(context, ERROR_CHANNEL_ID) |
48 | .setSmallIcon(R.drawable.ic_warning_primary_24dp) | 52 | .setSmallIcon(R.drawable.ic_warning_primary_24dp) |
49 | .setContentTitle(title) | 53 | .setContentTitle(title) |
50 | .setContentText(message) | 54 | .setContentText(message) |
@@ -62,13 +66,26 @@ class AppNotificationManager(private val context: Context) { | |||
62 | NotificationManagerCompat.from(context).cancelAll() | 66 | NotificationManagerCompat.from(context).cancelAll() |
63 | } | 67 | } |
64 | 68 | ||
69 | fun newConfigurationAccessNotificationBuilder() = | ||
70 | NotificationCompat.Builder(context, CONFIG_ACCESS_CHANNEL_ID) | ||
71 | |||
65 | @RequiresApi(Build.VERSION_CODES.O) | 72 | @RequiresApi(Build.VERSION_CODES.O) |
66 | private fun registerChannel() { | 73 | private fun registerChannels() { |
67 | val name = context.getString(R.string.notification_error_channel_name) | 74 | context.getSystemService(NotificationManager::class.java) |
68 | val importance = NotificationManager.IMPORTANCE_HIGH | 75 | .apply { |
69 | val channel = NotificationChannel(CHANNEL_ID, name, importance) | 76 | createNotificationChannel(NotificationChannel( |
70 | val notificationManager = context.getSystemService(NotificationManager::class.java) | 77 | ERROR_CHANNEL_ID, |
71 | notificationManager.createNotificationChannel(channel) | 78 | context.getString(R.string.notification_error_channel_name), |
79 | NotificationManager.IMPORTANCE_HIGH | ||
80 | )) | ||
81 | } | ||
82 | .apply { | ||
83 | createNotificationChannel(NotificationChannel( | ||
84 | CONFIG_ACCESS_CHANNEL_ID, | ||
85 | context.getString(R.string.notification_config_access_channel_name), | ||
86 | NotificationManager.IMPORTANCE_MIN | ||
87 | )) | ||
88 | } | ||
72 | } | 89 | } |
73 | 90 | ||
74 | private fun NotificationCompat.Builder.setHighPriority() = apply { | 91 | private fun NotificationCompat.Builder.setHighPriority() = apply { |