]> iEval git - fonbot.git/commitdiff
Fix the service notification
authorMarius Gavrilescu <marius@ieval.ro>
Wed, 27 Mar 2013 09:03:51 +0000 (11:03 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Wed, 27 Mar 2013 09:03:51 +0000 (11:03 +0200)
The service notification is no longer updated on each service call, and the InboxStyle finally works.

src/ro/ieval/fonbot/FonBotMainService.java

index 47fdafc38ee290e07b07662f947314f75efa7f82..dba53894b38f28e8295d3152c798da4e6a35845d 100644 (file)
@@ -104,6 +104,9 @@ public final class FonBotMainService extends Service {
         */
        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.
         *
@@ -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;
        }
 }
This page took 0.027731 seconds and 4 git commands to generate.