X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=src%2Fro%2Fieval%2Ffonbot%2FFonBotMainService.java;h=dba53894b38f28e8295d3152c798da4e6a35845d;hb=72de2947b5234e3b8495065e358530d9fddf3fb8;hp=a751d231c434173188b16dcd6a6037273eb83430;hpb=8dfb76c9431dbf8401412cb92c7512e7dc3081a2;p=fonbot.git diff --git a/src/ro/ieval/fonbot/FonBotMainService.java b/src/ro/ieval/fonbot/FonBotMainService.java index a751d23..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; @@ -70,6 +70,8 @@ public final class FonBotMainService extends Service { * Broadcast action: remove an ongoing event */ public static final String ACTION_DELETE_ONGOING="ro.ieval.fonbot.FonBotMainService.ACTION_DELETE_ONGOING"; + /** Broadcast action: trigger a server poll */ + public static final String ACTION_TRIGGER_POLL="ro.ieval.fonbot.FonBotMainService.ACTION_TRIGGER_POLL"; /** * Extra: ongoing event id * @@ -100,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. @@ -130,20 +135,28 @@ 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) + Utils.pollServer(this); + final boolean runForeground=PreferenceManager.getDefaultSharedPreferences(this).getBoolean("foreground", false); 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). @@ -154,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; } }