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   public boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
     21    if (data != null) {
     22      data.setDataPosition(0);
     23    }
     24 
     25    boolean result;
     26    try {
     27      result = new ShadowBinderBridge(realObject).onTransact(code, data, reply, flags);
     28    } catch (Exception e) {
     29      result = true;
     30      if (reply != null) {
     31        reply.writeException(e);
     32      }
     33    }
     34 
     35    if (reply != null) {
     36      reply.setDataPosition(0);
     37    }
     38    return result;
     39   }
     40 
     41   @Implementation
     42   public static final int getCallingPid() {
     43     if (callingPid != null) {
     44       return callingPid;
     45     }
     46     return android.os.Process.myPid();
     47   }
     48 
     49   @Implementation
     50   public static final int getCallingUid() {
     51     if (callingUid != null) {
     52       return callingUid;
     53     }
     54     return android.os.Process.myUid();
     55   }
     56 
     57   public static void setCallingPid(int pid) {
     58     ShadowBinder.callingPid = pid;
     59   }
     60 
     61   public static void setCallingUid(int uid) {
     62     ShadowBinder.callingUid = uid;
     63   }
     64 
     65   @Resetter
     66   public static void reset() {
     67     ShadowBinder.callingPid = null;
     68     ShadowBinder.callingUid = null;
     69   }
     70 }
     71