2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 21:55:10 +00:00

Separate persistent notification channel based on whether devices are connected

This allows users to selectively disable the persistent notification when no devices are connected.

BUG: 460927
This commit is contained in:
Yannik Sembritzki
2022-10-31 21:03:27 +00:00
committed by Philip Cohn-Cort
parent 70d70dcb33
commit 9f9b565ce6
3 changed files with 21 additions and 9 deletions

View File

@@ -347,7 +347,8 @@
<string name="pref_plugin_mprisreceiver_desc">Control your phone\'s media players from another device</string>
<string name="notification_channel_default">Other notifications</string>
<string name="notification_channel_persistent">Persistent indicator</string>
<string name="notification_channel_persistent_no_devices">Persistent indicator (no devices)</string>
<string name="notification_channel_persistent_with_devices">Persistent indicator (with devices)</string>
<string name="notification_channel_media_control">Media control</string>
<string name="notification_channel_filetransfer">File transfer</string>
<string name="notification_channel_high_priority">High priority</string>

View File

@@ -335,7 +335,13 @@ public class BackgroundService extends Service {
}
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NotificationHelper.Channels.PERSISTENT);
NotificationCompat.Builder notification;
if (connectedDevices.isEmpty()) {
notification = new NotificationCompat.Builder(this, NotificationHelper.Channels.PERSISTENT_NO_DEVICES);
} else {
notification = new NotificationCompat.Builder(this, NotificationHelper.Channels.PERSISTENT_WITH_DEVICES);
}
notification
.setSmallIcon(R.drawable.ic_notification)
.setOngoing(true)

View File

@@ -18,7 +18,8 @@ import java.util.List;
public class NotificationHelper {
public static class Channels {
public final static String PERSISTENT = "persistent";
public final static String PERSISTENT_NO_DEVICES = "persistent_no_devices";
public final static String PERSISTENT_WITH_DEVICES = "persistent_with_devices";
public final static String DEFAULT = "default";
public final static String MEDIA_CONTROL = "media_control";
public final static String FILETRANSFER = "filetransfer";
@@ -46,9 +47,13 @@ public class NotificationHelper {
}
public static void initializeChannels(Context context) {
final NotificationChannelCompat persistentChannel = new NotificationChannelCompat
.Builder(Channels.PERSISTENT, NotificationManagerCompat.IMPORTANCE_MIN)
.setName(context.getString(R.string.notification_channel_persistent))
final NotificationChannelCompat persistentChannelNoDevices = new NotificationChannelCompat
.Builder(Channels.PERSISTENT_NO_DEVICES, NotificationManagerCompat.IMPORTANCE_MIN)
.setName(context.getString(R.string.notification_channel_persistent_no_devices))
.build();
final NotificationChannelCompat persistentChannelWithDevices = new NotificationChannelCompat
.Builder(Channels.PERSISTENT_WITH_DEVICES, NotificationManagerCompat.IMPORTANCE_MIN)
.setName(context.getString(R.string.notification_channel_persistent_with_devices))
.build();
final NotificationChannelCompat defaultChannel = new NotificationChannelCompat
.Builder(Channels.DEFAULT, NotificationManagerCompat.IMPORTANCE_DEFAULT)
@@ -76,9 +81,9 @@ public class NotificationHelper {
.setName(context.getString(R.string.notification_channel_high_priority))
.build();
final List<NotificationChannelCompat> channels = Arrays.asList(persistentChannel,
defaultChannel, mediaChannel, fileTransferChannel, receiveNotificationChannel,
smsMmsChannel, highPriorityChannel);
final List<NotificationChannelCompat> channels = Arrays.asList(persistentChannelNoDevices,
persistentChannelWithDevices, defaultChannel, mediaChannel, fileTransferChannel,
receiveNotificationChannel, smsMmsChannel, highPriorityChannel);
NotificationManagerCompat.from(context).createNotificationChannelsCompat(channels);
}