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 private int stopSelfId; 23 private int stopSelfResultId; 24 25 @Implementation onDestroy()26 protected void onDestroy() { 27 removeForegroundNotification(); 28 } 29 30 @Implementation stopSelf()31 protected void stopSelf() { 32 selfStopped = true; 33 } 34 35 @Implementation stopSelf(int id)36 protected void stopSelf(int id) { 37 selfStopped = true; 38 stopSelfId = id; 39 } 40 41 @Implementation stopSelfResult(int id)42 protected boolean stopSelfResult(int id) { 43 selfStopped = true; 44 stopSelfResultId = id; 45 return true; 46 } 47 48 @Implementation startForeground(int id, Notification notification)49 protected final void startForeground(int id, Notification notification) { 50 foregroundStopped = false; 51 lastForegroundNotificationId = id; 52 lastForegroundNotification = notification; 53 notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 54 NotificationManager nm = (NotificationManager)RuntimeEnvironment.application.getSystemService(Context.NOTIFICATION_SERVICE); 55 nm.notify(id, notification); 56 } 57 58 @Implementation stopForeground(boolean removeNotification)59 protected void stopForeground(boolean removeNotification) { 60 foregroundStopped = true; 61 notificationShouldRemoved = removeNotification; 62 if (removeNotification) { 63 removeForegroundNotification(); 64 } 65 } 66 removeForegroundNotification()67 private void removeForegroundNotification() { 68 NotificationManager nm = (NotificationManager)RuntimeEnvironment.application.getSystemService(Context.NOTIFICATION_SERVICE); 69 nm.cancel(lastForegroundNotificationId); 70 lastForegroundNotification = null; 71 } 72 getLastForegroundNotificationId()73 public int getLastForegroundNotificationId() { 74 return lastForegroundNotificationId; 75 } 76 getLastForegroundNotification()77 public Notification getLastForegroundNotification() { 78 return lastForegroundNotification; 79 } 80 81 /** 82 * @return Is this service stopped by self. 83 */ isStoppedBySelf()84 public boolean isStoppedBySelf() { 85 return selfStopped; 86 } 87 isForegroundStopped()88 public boolean isForegroundStopped() { 89 return foregroundStopped; 90 } 91 getNotificationShouldRemoved()92 public boolean getNotificationShouldRemoved() { 93 return notificationShouldRemoved; 94 } 95 96 /** 97 * Returns id passed to {@link #stopSelf(int)} method. Make sure to check result of {@link 98 * #isStoppedBySelf()} first. 99 */ getStopSelfId()100 public int getStopSelfId() { 101 return stopSelfId; 102 } 103 104 /** 105 * Returns id passed to {@link #stopSelfResult(int)} method. Make sure to check result of {@link 106 * #isStoppedBySelf()} first. 107 */ getStopSelfResultId()108 public int getStopSelfResultId() { 109 return stopSelfResultId; 110 } 111 } 112