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