Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import android.app.ActivityThread;
      4 import android.content.pm.ApplicationInfo;
      5 import android.content.pm.PackageManager;
      6 import android.os.RemoteException;
      7 import java.lang.reflect.InvocationHandler;
      8 import java.lang.reflect.Method;
      9 import java.lang.reflect.Proxy;
     10 import javax.annotation.Nonnull;
     11 import org.robolectric.RuntimeEnvironment;
     12 import org.robolectric.annotation.Implementation;
     13 import org.robolectric.annotation.Implements;
     14 
     15 @Implements(value = ActivityThread.class, isInAndroidSdk = false, looseSignatures = true)
     16 public class ShadowActivityThread {
     17   private static ApplicationInfo applicationInfo;
     18 
     19   @Implementation
     20   public static Object getPackageManager() {
     21     ClassLoader classLoader = ShadowActivityThread.class.getClassLoader();
     22     Class<?> iPackageManagerClass;
     23     try {
     24       iPackageManagerClass = classLoader.loadClass("android.content.pm.IPackageManager");
     25     } catch (ClassNotFoundException e) {
     26       throw new RuntimeException(e);
     27     }
     28     return Proxy.newProxyInstance(
     29         classLoader,
     30         new Class[] {iPackageManagerClass},
     31         new InvocationHandler() {
     32           @Override
     33           public Object invoke(Object proxy, @Nonnull Method method, Object[] args)
     34               throws Exception {
     35             if (method.getName().equals("getApplicationInfo")) {
     36               String packageName = (String) args[0];
     37               int flags = (Integer) args[1];
     38 
     39               if (packageName.equals(ShadowActivityThread.applicationInfo.packageName)) {
     40                 return ShadowActivityThread.applicationInfo;
     41               }
     42 
     43               try {
     44                 return RuntimeEnvironment.application
     45                     .getPackageManager()
     46                     .getApplicationInfo(packageName, flags);
     47               } catch (PackageManager.NameNotFoundException e) {
     48                 throw new RemoteException(e.getMessage());
     49               }
     50             } else if (method.getName().equals("notifyPackageUse")) {
     51               return null;
     52             } else if (method.getName().equals("getPackageInstaller")) {
     53               return null;
     54             }
     55             throw new UnsupportedOperationException("sorry, not supporting " + method + " yet!");
     56           }
     57         });
     58   }
     59 
     60   @Implementation
     61   public static Object currentActivityThread() {
     62     return RuntimeEnvironment.getActivityThread();
     63   }
     64 
     65   /**
     66    * Internal use only.
     67    *
     68    * @deprecated do not use
     69    */
     70   @Deprecated
     71   public static void setApplicationInfo(ApplicationInfo applicationInfo) {
     72     ShadowActivityThread.applicationInfo = applicationInfo;
     73   }
     74 }
     75