1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1;
4 import static org.robolectric.RuntimeEnvironment.getApiLevel;
5 
6 import android.content.BroadcastReceiver;
7 import android.content.Intent;
8 import android.os.Bundle;
9 import android.os.IBinder;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.SettableFuture;
13 import org.robolectric.annotation.Implementation;
14 import org.robolectric.annotation.Implements;
15 import org.robolectric.annotation.RealObject;
16 
17 @Implements(BroadcastReceiver.PendingResult.class)
18 public final class ShadowBroadcastPendingResult {
19   @RealObject BroadcastReceiver.PendingResult pendingResult;
20 
create( int resultCode, String resultData, Bundle resultExtras, boolean ordered)21   static BroadcastReceiver.PendingResult create(
22       int resultCode, String resultData, Bundle resultExtras, boolean ordered) {
23     try {
24       if (getApiLevel() <= LOLLIPOP_MR1) {
25         return BroadcastReceiver.PendingResult.class
26             .getConstructor(
27                 int.class,
28                 String.class,
29                 Bundle.class,
30                 int.class,
31                 boolean.class,
32                 boolean.class,
33                 IBinder.class,
34                 int.class)
35             .newInstance(
36                 resultCode,
37                 resultData,
38                 resultExtras,
39                 0 /* type */,
40                 ordered,
41                 false /*sticky*/,
42                 null /* ibinder token */,
43                 0 /* userid */);
44 
45       } else {
46         return new BroadcastReceiver.PendingResult(
47             resultCode,
48             resultData,
49             resultExtras,
50             0 /* type */,
51             ordered,
52             false /*sticky*/,
53             null /* ibinder token */,
54             0 /* userid */,
55             0 /* flags */);
56       }
57     } catch (Exception e) {
58       throw new RuntimeException(e);
59     }
60   }
61 
createSticky(Intent intent)62   static BroadcastReceiver.PendingResult createSticky(Intent intent) {
63     try {
64       if (getApiLevel() <= LOLLIPOP_MR1) {
65         return BroadcastReceiver.PendingResult.class
66             .getConstructor(
67                 int.class,
68                 String.class,
69                 Bundle.class,
70                 int.class,
71                 boolean.class,
72                 boolean.class,
73                 IBinder.class,
74                 int.class)
75             .newInstance(
76                 0 /*resultCode*/,
77                 intent.getDataString(),
78                 intent.getExtras(),
79                 0 /* type */,
80                 false /*ordered*/,
81                 true /*sticky*/,
82                 null /* ibinder token */,
83                 0 /* userid */);
84 
85       } else {
86         return new BroadcastReceiver.PendingResult(
87             0 /*resultCode*/,
88             intent.getDataString(),
89             intent.getExtras(),
90             0 /* type */,
91             false /*ordered*/,
92             true /*sticky*/,
93             null /* ibinder token */,
94             0 /* userid */,
95             0 /* flags */);
96       }
97     } catch (Exception e) {
98       throw new RuntimeException(e);
99     }
100   }
101 
102   private final SettableFuture<BroadcastReceiver.PendingResult> finished = SettableFuture.create();
103 
104   @Implementation
finish()105   protected void finish() {
106     Preconditions.checkState(finished.set(pendingResult), "Broadcast already finished");
107   }
108 
getFuture()109   public ListenableFuture<BroadcastReceiver.PendingResult> getFuture() {
110     return finished;
111   }
112 }
113