• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import android.content.BroadcastReceiver;
4 import android.content.Context;
5 import android.content.Intent;
6 import java.util.concurrent.atomic.AtomicBoolean;
7 import org.robolectric.annotation.Implementation;
8 import org.robolectric.annotation.Implements;
9 import org.robolectric.annotation.RealObject;
10 
11 @Implements(BroadcastReceiver.class)
12 public class ShadowBroadcastReceiver {
13   @RealObject BroadcastReceiver receiver;
14 
15   private AtomicBoolean abort; // The abort state of the currently processed broadcast
16 
17   @Implementation
abortBroadcast()18   protected void abortBroadcast() {
19     // TODO probably needs a check to prevent calling this method from ordinary Broadcasts
20     abort.set(true);
21   }
22 
23   @Implementation
onReceive(Context context, Intent intent)24   protected void onReceive(Context context, Intent intent) {
25     if (abort == null || !abort.get()) {
26       receiver.onReceive(context, intent);
27     }
28   }
29 
onReceive(Context context, Intent intent, AtomicBoolean abort)30   public void onReceive(Context context, Intent intent, AtomicBoolean abort) {
31     this.abort = abort;
32     onReceive(context, intent);
33     // If the underlying receiver has called goAsync(), we should not finish the pending result yet - they'll do that
34     // for us.
35     if (receiver.getPendingResult() != null) {
36       receiver.getPendingResult().finish();
37     }
38   }
39 }
40