X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=src%2Fro%2Fieval%2Ffonbot%2FFonBotMainService.java;h=dba53894b38f28e8295d3152c798da4e6a35845d;hb=72de2947b5234e3b8495065e358530d9fddf3fb8;hp=753425d18081daa9a54c67eb1f2c2ac8248f5612;hpb=6b9507db27500572e4366b3f23eefeea079a9d8f;p=fonbot.git diff --git a/src/ro/ieval/fonbot/FonBotMainService.java b/src/ro/ieval/fonbot/FonBotMainService.java index 753425d..dba5389 100644 --- a/src/ro/ieval/fonbot/FonBotMainService.java +++ b/src/ro/ieval/fonbot/FonBotMainService.java @@ -4,7 +4,7 @@ import static ro.ieval.fonbot.R.string.*; import static ro.ieval.fonbot.Utils.toNonNull; import java.util.Collections; -import java.util.HashSet; +import java.util.EnumSet; import java.util.Set; import org.eclipse.jdt.annotation.Nullable; @@ -102,7 +102,10 @@ public final class FonBotMainService extends Service { /** * Set of ongoing events. */ - private final Set ongoing=new HashSet(10); + private final Set ongoing=EnumSet.noneOf(OngoingEvent.class); + + /** true if running in foreground, false otherwise */ + private boolean isForeground = false; /** * Get the set of ongoing events. @@ -132,13 +135,16 @@ public final class FonBotMainService extends Service { @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) @@ -148,7 +154,9 @@ public final class FonBotMainService extends Service { 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). @@ -159,26 +167,24 @@ public final class FonBotMainService extends Service { 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; } }