1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.LOLLIPOP; 4 5 import android.app.ISearchManager; 6 import android.app.trust.ITrustManager; 7 import android.content.Context; 8 import android.net.INetworkPolicyManager; 9 import android.os.Binder; 10 import android.os.Build; 11 import android.os.IBinder; 12 import android.os.IInterface; 13 import android.os.RemoteException; 14 import android.os.ServiceManager; 15 import java.util.HashMap; 16 import java.util.Map; 17 import org.robolectric.RuntimeEnvironment; 18 import org.robolectric.annotation.Implementation; 19 import org.robolectric.annotation.Implements; 20 import org.robolectric.util.ReflectionHelpers; 21 22 @Implements(value = ServiceManager.class, isInAndroidSdk = false) 23 public class ShadowServiceManager { 24 25 private static Map<String, IBinder> SERVICES = 26 new HashMap<String, IBinder>() { 27 { 28 put( 29 Context.SEARCH_SERVICE, 30 createBinder(ISearchManager.class, "android.app.ISearchManager")); 31 put( 32 Context.UI_MODE_SERVICE, 33 createBinder(ISearchManager.class, "android.app.IUiModeManager")); 34 if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP) { 35 put( 36 Context.TRUST_SERVICE, 37 createBinder(ITrustManager.class, "android.app.trust.ITrustManager")); 38 } 39 // BEGIN-INTERNAL 40 if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.P) { 41 put( 42 Context.NETWORK_POLICY_SERVICE, 43 createBinder(INetworkPolicyManager.class, "android.net.INetworkPolicyManager")); 44 } 45 // END-INTERNAL 46 } 47 }; 48 49 @Implementation getService(String name)50 public static IBinder getService(String name) { 51 return SERVICES.get(name); 52 } 53 createBinder(Class<? extends IInterface> clazz, String descriptor)54 private static Binder createBinder(Class<? extends IInterface> clazz, String descriptor) { 55 Binder binder = new Binder(); 56 binder.attachInterface(ReflectionHelpers.createNullProxy(clazz), descriptor); 57 return binder; 58 } 59 60 @Implementation addService(String name, IBinder service)61 public static void addService(String name, IBinder service) { 62 } 63 64 @Implementation checkService(String name)65 public static IBinder checkService(String name) { 66 return null; 67 } 68 69 @Implementation listServices()70 public static String[] listServices() throws RemoteException { 71 return null; 72 } 73 74 @Implementation initServiceCache(Map<String, IBinder> cache)75 public static void initServiceCache(Map<String, IBinder> cache) { 76 } 77 } 78