*/
private final Set<OngoingEvent> ongoing=EnumSet.noneOf(OngoingEvent.class);
+ /** true if running in foreground, false otherwise */
+ private boolean isForeground = false;
+
/**
* Get the set of ongoing events.
*
@Override
public int onStartCommand(final @Nullable Intent intent, final int flags, final int startId) {
+ boolean updateNotification = false;
if(intent!=null && intent.getAction()==ACTION_PUT_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID)){
ongoing.add(OngoingEvent.values()[intent.getIntExtra(EXTRA_ONGOING_ID, 0)]);
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE));
+ updateNotification=true;
}
if(intent!=null && intent.getAction()==ACTION_DELETE_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID)){
ongoing.remove(OngoingEvent.values()[intent.getIntExtra(EXTRA_ONGOING_ID, 0)]);
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE));
+ updateNotification=true;
}
if(intent!=null && intent.getAction()==ACTION_TRIGGER_POLL)
if(!runForeground)
stopForeground(true);
- if(runForeground||!ongoing.isEmpty()){
+ final NotificationManager man=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+ final boolean shouldNotify=runForeground||!ongoing.isEmpty();
+ if(shouldNotify && (updateNotification || runForeground != isForeground)){
final Intent mainIntent=new Intent(this, FonBotMainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final NotificationCompat.Builder builder=new NotificationCompat.Builder(this).
setContentIntent(PendingIntent.getActivity(this, 0, mainIntent, 0)).
setOngoing(true);
- final Notification notification;
- if(ongoing.isEmpty())
- notification=builder.build();
- else {
+ if(!ongoing.isEmpty()) {
final NotificationCompat.InboxStyle inboxBuilder=new NotificationCompat.InboxStyle(builder);
for(final OngoingEvent event : ongoing)
inboxBuilder.addLine(getString(event.resource));
- notification=inboxBuilder.build();
}
+ final Notification notification=builder.build();
if(runForeground)
- startForeground(1337, builder.build());
- else{
- final NotificationManager man=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+ startForeground(1337, notification);
+ else
man.notify(1337, notification);
- }
}
+ if(!shouldNotify)
+ man.cancel(1337);
+ isForeground=runForeground;
return START_STICKY;
}
}