Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import android.os.Binder;
      4 import android.os.Parcel;
      5 import android.os.RemoteException;
      6 import org.robolectric.annotation.Implementation;
      7 import org.robolectric.annotation.Implements;
      8 import org.robolectric.annotation.RealObject;
      9 import org.robolectric.annotation.Resetter;
     10 
     11 @Implements(Binder.class)
     12 public class ShadowBinder {
     13   @RealObject
     14   Binder realObject;
     15 
     16   private static Integer callingUid;
     17   private static Integer callingPid;
     18 
     19   @Implementation
     20   protected boolean transact(int code, Parcel data, Parcel reply, int flags)
     21       throws RemoteException {
     22    if (data != null) {
     23      data.setDataPosition(0);
     24    }
     25 
     26    boolean result;
     27    try {
     28      result = new ShadowBinderBridge(realObject).onTransact(code, data, reply, flags);
     29    } catch (Exception e) {
     30      result = true;
     31      if (reply != null) {
     32        reply.writeException(e);
     33      }
     34    }
     35 
     36    if (reply != null) {
     37      reply.setDataPosition(0);
     38    }
     39    return result;
     40   }
     41 
     42   @Implementation
     43   protected static final int getCallingPid() {
     44     if (callingPid != null) {
     45       return callingPid;
     46     }
     47     return android.os.Process.myPid();
     48   }
     49 
     50   @Implementation
     51   protected static final int getCallingUid() {
     52     if (callingUid != null) {
     53       return callingUid;
     54     }
     55     return android.os.Process.myUid();
     56   }
     57 
     58   public static void setCallingPid(int pid) {
     59     ShadowBinder.callingPid = pid;
     60   }
     61 
     62   public static void setCallingUid(int uid) {
     63     ShadowBinder.callingUid = uid;
     64   }
     65 
     66   @Resetter
     67   public static void reset() {
     68     ShadowBinder.callingPid = null;
     69     ShadowBinder.callingUid = null;
     70   }
     71 }
     72