Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import android.app.Notification;
      4 import android.app.NotificationManager;
      5 import android.app.Service;
      6 import android.content.Context;
      7 import org.robolectric.RuntimeEnvironment;
      8 import org.robolectric.annotation.Implementation;
      9 import org.robolectric.annotation.Implements;
     10 import org.robolectric.annotation.RealObject;
     11 
     12 @SuppressWarnings({"UnusedDeclaration"})
     13 @Implements(Service.class)
     14 public class ShadowService extends ShadowContextWrapper {
     15   @RealObject Service realService;
     16 
     17   private int lastForegroundNotificationId;
     18   private Notification lastForegroundNotification;
     19   private boolean selfStopped = false;
     20   private boolean foregroundStopped;
     21   private boolean notificationShouldRemoved;
     22 
     23   @Implementation
     24   public void onDestroy() {
     25     removeForegroundNotification();
     26   }
     27 
     28   @Implementation
     29   public void stopSelf() {
     30     selfStopped = true;
     31   }
     32 
     33   @Implementation
     34   public void stopSelf(int id) {
     35     selfStopped = true;
     36   }
     37 
     38   @Implementation
     39   public final void startForeground(int id, Notification notification) {
     40     foregroundStopped = false;
     41 	  lastForegroundNotificationId = id;
     42     lastForegroundNotification = notification;
     43     notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
     44     NotificationManager nm = (NotificationManager)RuntimeEnvironment.application.getSystemService(Context.NOTIFICATION_SERVICE);
     45     nm.notify(id, notification);
     46   }
     47 
     48   @Implementation
     49   public void stopForeground(boolean removeNotification) {
     50     foregroundStopped = true;
     51     notificationShouldRemoved = removeNotification;
     52     if (removeNotification) {
     53       removeForegroundNotification();
     54     }
     55   }
     56 
     57   private void removeForegroundNotification() {
     58     NotificationManager nm = (NotificationManager)RuntimeEnvironment.application.getSystemService(Context.NOTIFICATION_SERVICE);
     59     nm.cancel(lastForegroundNotificationId);
     60     lastForegroundNotification = null;
     61   }
     62 
     63   public int getLastForegroundNotificationId() {
     64     return lastForegroundNotificationId;
     65   }
     66 
     67   public Notification getLastForegroundNotification() {
     68     return lastForegroundNotification;
     69   }
     70 
     71   /**
     72    * @return Is this service stopped by self.
     73    */
     74   public boolean isStoppedBySelf() {
     75     return selfStopped;
     76   }
     77 
     78   public boolean isForegroundStopped() {
     79     return foregroundStopped;
     80   }
     81 
     82   public boolean getNotificationShouldRemoved() {
     83     return notificationShouldRemoved;
     84   }
     85 }
     86