|
3 | 3 | import android.app.NotificationChannel; |
4 | 4 | import android.app.NotificationManager; |
5 | 5 | import android.content.Context; |
| 6 | +import android.content.Intent; |
| 7 | +import android.media.AudioAttributes; |
6 | 8 | import android.os.Build; |
| 9 | +import android.provider.Settings; |
| 10 | +import android.widget.Toast; |
7 | 11 |
|
8 | 12 | public class NotificationChannelCreator { |
9 | | - public static final String CHANNEL_ID = "IrssiNotifierDefaultChannel"; |
| 13 | + public static final String CHANNEL_DEFAULT_ID = "IrssiNotifierDefaultChannel"; |
| 14 | + public static final String CHANNEL_LOWPRIO_ID = "IrssiNotifierLowPriorityChannel"; |
10 | 15 |
|
11 | | - public static void createNotificationChannel(Context ctx) { |
| 16 | + public static void createNotificationChannels(Context ctx) { |
12 | 17 | // Create the NotificationChannel, but only on API 26+ because |
13 | 18 | // the NotificationChannel class is new and not in the support library |
14 | 19 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
15 | | - CharSequence name = "IrssiNotifier default"; |
16 | | - String description = "Default notifications from Irssi hilights."; |
17 | | - int importance = NotificationManager.IMPORTANCE_HIGH; |
18 | | - NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); |
19 | | - channel.setDescription(description); |
20 | | - |
21 | | - Preferences prefs = new Preferences(ctx); |
22 | | - channel.enableLights(prefs.isLightsEnabled()); |
23 | | - channel.setLightColor(prefs.getCustomLightColor()); |
24 | | - channel.enableVibration(prefs.isVibrationEnabled()); |
25 | | - |
26 | | - // Register the channel with the system; you can't change the importance |
27 | | - // or other notification behaviors after this |
28 | | - NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class); |
29 | | - notificationManager.createNotificationChannel(channel); |
| 20 | + { |
| 21 | + CharSequence name = "IrssiNotifier default"; |
| 22 | + String description = "Default notifications for Irssi hilights."; |
| 23 | + int importance = NotificationManager.IMPORTANCE_DEFAULT; |
| 24 | + NotificationChannel channel = new NotificationChannel(CHANNEL_DEFAULT_ID, name, importance); |
| 25 | + channel.setDescription(description); |
| 26 | + |
| 27 | + Preferences prefs = new Preferences(ctx); |
| 28 | + channel.enableLights(prefs.isLightsEnabled()); |
| 29 | + channel.setLightColor(prefs.getCustomLightColor()); |
| 30 | + channel.enableVibration(prefs.isVibrationEnabled()); |
| 31 | + |
| 32 | + if (prefs.isSoundEnabled()) { |
| 33 | + AudioAttributes att = new AudioAttributes.Builder() |
| 34 | + .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT) |
| 35 | + .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) |
| 36 | + .build(); |
| 37 | + channel.setSound(prefs.getNotificationSound(), att); |
| 38 | + } else { |
| 39 | + channel.setSound(null, null); |
| 40 | + } |
| 41 | + |
| 42 | + // Register the channel with the system; you can't change the importance |
| 43 | + // or other notification behaviors after this |
| 44 | + NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class); |
| 45 | + notificationManager.createNotificationChannel(channel); |
| 46 | + } |
| 47 | + |
| 48 | + { |
| 49 | + CharSequence name = "IrssiNotifier low priority"; |
| 50 | + String description = "Repeated hilights, you can set spam filter duration in app settings."; |
| 51 | + int importance = NotificationManager.IMPORTANCE_LOW; |
| 52 | + NotificationChannel channel = new NotificationChannel(CHANNEL_LOWPRIO_ID, name, importance); |
| 53 | + channel.setDescription(description); |
| 54 | + |
| 55 | + NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class); |
| 56 | + notificationManager.createNotificationChannel(channel); |
| 57 | + } |
30 | 58 | } |
31 | 59 | } |
32 | 60 |
|
33 | | - private static void deleteNotificationChannel(Context ctx) { |
| 61 | + private static void deleteNotificationChannels(Context ctx) { |
34 | 62 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
35 | 63 | NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class); |
36 | | - notificationManager.deleteNotificationChannel(CHANNEL_ID); |
| 64 | + notificationManager.deleteNotificationChannel(CHANNEL_DEFAULT_ID); |
| 65 | + notificationManager.deleteNotificationChannel(CHANNEL_LOWPRIO_ID); |
37 | 66 | } |
38 | 67 | } |
39 | 68 |
|
40 | 69 | public static void recreateNotificationChannel(Context ctx) { |
41 | | - deleteNotificationChannel(ctx); |
42 | | - createNotificationChannel(ctx); |
| 70 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 71 | + deleteNotificationChannels(ctx); |
| 72 | + createNotificationChannels(ctx); |
| 73 | + |
| 74 | + Toast.makeText(ctx, "Notification Channel settings applied", Toast.LENGTH_SHORT).show(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static void openNotificationChannelSettings(Context ctx) { |
| 79 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 80 | + Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); |
| 81 | + intent.putExtra(Settings.EXTRA_APP_PACKAGE, ctx.getPackageName()); |
| 82 | + intent.putExtra(Settings.EXTRA_CHANNEL_ID, CHANNEL_DEFAULT_ID); |
| 83 | + ctx.startActivity(intent); |
| 84 | + } |
43 | 85 | } |
44 | 86 | } |
0 commit comments