1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 11:21:29 +00:00

Use separate notification channel while monitoring run conditions (#1046)

This commit is contained in:
Martin Carpella 2018-03-28 23:52:36 +02:00 committed by Audrius Butkevicius
parent 7ab45a9d88
commit 7194e25a5c
2 changed files with 25 additions and 4 deletions

View file

@ -22,16 +22,19 @@ import javax.inject.Inject;
public class NotificationHandler { public class NotificationHandler {
private static final int ID_PERSISTENT = 1; private static final int ID_PERSISTENT = 1;
private static final int ID_PERSISTENT_WAITING = 4;
private static final int ID_RESTART = 2; private static final int ID_RESTART = 2;
private static final int ID_STOP_BACKGROUND_WARNING = 3; private static final int ID_STOP_BACKGROUND_WARNING = 3;
private static final int ID_CRASH = 9; private static final int ID_CRASH = 9;
private static final String CHANNEL_PERSISTENT = "01_syncthing_persistent"; private static final String CHANNEL_PERSISTENT = "01_syncthing_persistent";
private static final String CHANNEL_INFO = "02_syncthing_notifications"; private static final String CHANNEL_INFO = "02_syncthing_notifications";
private static final String CHANNEL_PERSISTENT_WAITING = "03_syncthing_persistent_waiting";
private final Context mContext; private final Context mContext;
@Inject SharedPreferences mPreferences; @Inject SharedPreferences mPreferences;
private final NotificationManager mNotificationManager; private final NotificationManager mNotificationManager;
private final NotificationChannel mPersistentChannel; private final NotificationChannel mPersistentChannel;
private final NotificationChannel mPersistentChannelWaiting;
private final NotificationChannel mInfoChannel; private final NotificationChannel mInfoChannel;
public NotificationHandler(Context context) { public NotificationHandler(Context context) {
@ -47,6 +50,15 @@ public class NotificationHandler {
mPersistentChannel.enableVibration(false); mPersistentChannel.enableVibration(false);
mPersistentChannel.setSound(null, null); mPersistentChannel.setSound(null, null);
mNotificationManager.createNotificationChannel(mPersistentChannel); mNotificationManager.createNotificationChannel(mPersistentChannel);
mPersistentChannelWaiting = new NotificationChannel(
CHANNEL_PERSISTENT_WAITING, mContext.getString(R.string.notification_persistent_waiting_channel),
NotificationManager.IMPORTANCE_MIN);
mPersistentChannelWaiting.enableLights(false);
mPersistentChannelWaiting.enableVibration(false);
mPersistentChannelWaiting.setSound(null, null);
mNotificationManager.createNotificationChannel(mPersistentChannelWaiting);
mInfoChannel = new NotificationChannel( mInfoChannel = new NotificationChannel(
CHANNEL_INFO, mContext.getString(R.string.notifications_other_channel), CHANNEL_INFO, mContext.getString(R.string.notifications_other_channel),
NotificationManager.IMPORTANCE_LOW); NotificationManager.IMPORTANCE_LOW);
@ -55,6 +67,7 @@ public class NotificationHandler {
mNotificationManager.createNotificationChannel(mInfoChannel); mNotificationManager.createNotificationChannel(mInfoChannel);
} else { } else {
mPersistentChannel = null; mPersistentChannel = null;
mPersistentChannelWaiting = null;
mInfoChannel = null; mInfoChannel = null;
} }
} }
@ -95,7 +108,12 @@ public class NotificationHandler {
PendingIntent pi = PendingIntent.getActivity(mContext, 0, PendingIntent pi = PendingIntent.getActivity(mContext, 0,
new Intent(mContext, FirstStartActivity.class), 0); new Intent(mContext, FirstStartActivity.class), 0);
int title = syncthingRunning ? R.string.syncthing_active : R.string.syncthing_disabled; int title = syncthingRunning ? R.string.syncthing_active : R.string.syncthing_disabled;
NotificationCompat.Builder builder = getNotificationBuilder(mPersistentChannel) // Reason for two separate IDs: if one of the notification channels is hidden then
// the startForeground() below won't update the notification but use the old one
int idToShow = syncthingRunning ? ID_PERSISTENT : ID_PERSISTENT_WAITING;
int idToCancel = syncthingRunning ? ID_PERSISTENT_WAITING : ID_PERSISTENT;
NotificationChannel channel = syncthingRunning ? mPersistentChannel : mPersistentChannelWaiting;
NotificationCompat.Builder builder = getNotificationBuilder(channel)
.setContentTitle(mContext.getString(title)) .setContentTitle(mContext.getString(title))
.setSmallIcon(R.drawable.ic_stat_notify) .setSmallIcon(R.drawable.ic_stat_notify)
.setOngoing(true) .setOngoing(true)
@ -105,11 +123,12 @@ public class NotificationHandler {
builder.setPriority(NotificationCompat.PRIORITY_MIN); builder.setPriority(NotificationCompat.PRIORITY_MIN);
if (foreground) { if (foreground) {
service.startForeground(ID_PERSISTENT, builder.build()); service.startForeground(idToShow, builder.build());
} else { } else {
service.stopForeground(false); // ensure no longer running with foreground priority service.stopForeground(false); // ensure no longer running with foreground priority
mNotificationManager.notify(ID_PERSISTENT, builder.build()); mNotificationManager.notify(idToShow, builder.build());
} }
mNotificationManager.cancel(idToCancel);
} else { } else {
// ensure no longer running with foreground priority // ensure no longer running with foreground priority
cancelPersistentNotification(service); cancelPersistentNotification(service);
@ -122,6 +141,7 @@ public class NotificationHandler {
service.stopForeground(false); service.stopForeground(false);
mNotificationManager.cancel(ID_PERSISTENT); mNotificationManager.cancel(ID_PERSISTENT);
mNotificationManager.cancel(ID_PERSISTENT_WAITING);
} }
public void showCrashedNotification(@StringRes int title, boolean force) { public void showCrashedNotification(@StringRes int title, boolean force) {

View file

@ -541,7 +541,8 @@ Please report any problems you encounter via Github.</string>
<string name="notification_crash_text">Click to view logs</string> <string name="notification_crash_text">Click to view logs</string>
<string name="notifications_persistent_channel">Persistent notification</string> <string name="notifications_persistent_channel">Syncthing active</string>
<string name="notification_persistent_waiting_channel">Monitoring run conditions</string>
<string name="notifications_other_channel">Other notifications</string> <string name="notifications_other_channel">Other notifications</string>
<!-- RestApi --> <!-- RestApi -->