• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
4 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
5 import static android.os.Build.VERSION_CODES.KITKAT;
6 import static android.os.Build.VERSION_CODES.LOLLIPOP;
7 import static android.os.Build.VERSION_CODES.M;
8 import static android.os.Build.VERSION_CODES.N;
9 import static android.os.Build.VERSION_CODES.N_MR1;
10 import static android.os.Build.VERSION_CODES.O;
11 import static android.os.Build.VERSION_CODES.P;
12 import static android.os.Build.VERSION_CODES.Q;
13 import static android.os.Build.VERSION_CODES.R;
14 import static android.os.Build.VERSION_CODES.S;
15 import static android.os.Build.VERSION_CODES.TIRAMISU;
16 
17 import android.accounts.IAccountManager;
18 import android.app.IAlarmManager;
19 import android.app.ILocaleManager;
20 import android.app.INotificationManager;
21 import android.app.ISearchManager;
22 import android.app.IUiModeManager;
23 import android.app.IWallpaperManager;
24 import android.app.admin.IDevicePolicyManager;
25 import android.app.ambientcontext.IAmbientContextManager;
26 import android.app.job.IJobScheduler;
27 import android.app.role.IRoleManager;
28 import android.app.slice.ISliceManager;
29 import android.app.timedetector.ITimeDetectorService;
30 import android.app.timezonedetector.ITimeZoneDetectorService;
31 import android.app.trust.ITrustManager;
32 import android.app.usage.IStorageStatsManager;
33 import android.app.usage.IUsageStatsManager;
34 import android.bluetooth.BluetoothAdapter;
35 import android.bluetooth.IBluetooth;
36 import android.bluetooth.IBluetoothManager;
37 import android.content.Context;
38 import android.content.IClipboard;
39 import android.content.IRestrictionsManager;
40 import android.content.integrity.IAppIntegrityManager;
41 import android.content.pm.ICrossProfileApps;
42 import android.content.pm.IShortcutService;
43 import android.content.rollback.IRollbackManager;
44 import android.hardware.biometrics.IAuthService;
45 import android.hardware.biometrics.IBiometricService;
46 import android.hardware.fingerprint.IFingerprintService;
47 import android.hardware.input.IInputManager;
48 import android.hardware.location.IContextHubService;
49 import android.hardware.usb.IUsbManager;
50 import android.location.ICountryDetector;
51 import android.location.ILocationManager;
52 import android.media.IAudioService;
53 import android.media.IMediaRouterService;
54 import android.media.session.ISessionManager;
55 import android.net.IConnectivityManager;
56 import android.net.IIpSecService;
57 import android.net.INetworkPolicyManager;
58 import android.net.INetworkScoreService;
59 import android.net.ITetheringConnector;
60 import android.net.nsd.INsdManager;
61 import android.net.vcn.IVcnManagementService;
62 import android.net.wifi.IWifiManager;
63 import android.net.wifi.aware.IWifiAwareManager;
64 import android.net.wifi.p2p.IWifiP2pManager;
65 import android.net.wifi.rtt.IWifiRttManager;
66 import android.nfc.INfcAdapter;
67 import android.os.BatteryStats;
68 import android.os.Binder;
69 import android.os.IBatteryPropertiesRegistrar;
70 import android.os.IBinder;
71 import android.os.IDumpstate;
72 import android.os.IInterface;
73 import android.os.IPowerManager;
74 import android.os.IThermalService;
75 import android.os.IUserManager;
76 import android.os.RemoteException;
77 import android.os.ServiceManager;
78 import android.os.storage.IStorageManager;
79 import android.permission.ILegacyPermissionManager;
80 import android.permission.IPermissionManager;
81 import android.safetycenter.ISafetyCenterManager;
82 import android.speech.IRecognitionServiceManager;
83 import android.uwb.IUwbAdapter;
84 import android.view.IWindowManager;
85 import android.view.contentcapture.IContentCaptureManager;
86 import android.view.translation.ITranslationManager;
87 import com.android.internal.app.IAppOpsService;
88 import com.android.internal.app.IBatteryStats;
89 import com.android.internal.appwidget.IAppWidgetService;
90 import com.android.internal.os.IDropBoxManagerService;
91 import com.android.internal.statusbar.IStatusBar;
92 import com.android.internal.telephony.ITelephony;
93 import com.android.internal.telephony.ITelephonyRegistry;
94 import com.android.internal.view.IInputMethodManager;
95 import java.util.HashMap;
96 import java.util.HashSet;
97 import java.util.Map;
98 import java.util.Set;
99 import org.robolectric.RuntimeEnvironment;
100 import org.robolectric.annotation.Implementation;
101 import org.robolectric.annotation.Implements;
102 import org.robolectric.annotation.Resetter;
103 import org.robolectric.util.ReflectionHelpers;
104 import android.companion.virtual.IVirtualDeviceManager;
105 
106 /** Shadow for {@link ServiceManager}. */
107 @SuppressWarnings("NewApi")
108 @Implements(value = ServiceManager.class, isInAndroidSdk = false)
109 public class ShadowServiceManager {
110 
111   private static final Map<String, BinderService> binderServices = new HashMap<>();
112   private static final Set<String> unavailableServices = new HashSet<>();
113 
114   static {
addBinderService(Context.CLIPBOARD_SERVICE, IClipboard.class)115     addBinderService(Context.CLIPBOARD_SERVICE, IClipboard.class);
addBinderService(Context.WIFI_P2P_SERVICE, IWifiP2pManager.class)116     addBinderService(Context.WIFI_P2P_SERVICE, IWifiP2pManager.class);
addBinderService(Context.ACCOUNT_SERVICE, IAccountManager.class)117     addBinderService(Context.ACCOUNT_SERVICE, IAccountManager.class);
addBinderService(Context.USB_SERVICE, IUsbManager.class)118     addBinderService(Context.USB_SERVICE, IUsbManager.class);
addBinderService(Context.LOCATION_SERVICE, ILocationManager.class)119     addBinderService(Context.LOCATION_SERVICE, ILocationManager.class);
addBinderService(Context.INPUT_METHOD_SERVICE, IInputMethodManager.class)120     addBinderService(Context.INPUT_METHOD_SERVICE, IInputMethodManager.class);
addBinderService(Context.ALARM_SERVICE, IAlarmManager.class)121     addBinderService(Context.ALARM_SERVICE, IAlarmManager.class);
addBinderService(Context.POWER_SERVICE, IPowerManager.class)122     addBinderService(Context.POWER_SERVICE, IPowerManager.class);
addBinderService(BatteryStats.SERVICE_NAME, IBatteryStats.class)123     addBinderService(BatteryStats.SERVICE_NAME, IBatteryStats.class);
addBinderService(Context.DROPBOX_SERVICE, IDropBoxManagerService.class)124     addBinderService(Context.DROPBOX_SERVICE, IDropBoxManagerService.class);
addBinderService(Context.DEVICE_POLICY_SERVICE, IDevicePolicyManager.class)125     addBinderService(Context.DEVICE_POLICY_SERVICE, IDevicePolicyManager.class);
addBinderService(Context.TELEPHONY_SERVICE, ITelephony.class)126     addBinderService(Context.TELEPHONY_SERVICE, ITelephony.class);
addBinderService(Context.CONNECTIVITY_SERVICE, IConnectivityManager.class)127     addBinderService(Context.CONNECTIVITY_SERVICE, IConnectivityManager.class);
addBinderService(Context.WIFI_SERVICE, IWifiManager.class)128     addBinderService(Context.WIFI_SERVICE, IWifiManager.class);
addBinderService(Context.SEARCH_SERVICE, ISearchManager.class)129     addBinderService(Context.SEARCH_SERVICE, ISearchManager.class);
addBinderService(Context.UI_MODE_SERVICE, IUiModeManager.class)130     addBinderService(Context.UI_MODE_SERVICE, IUiModeManager.class);
addBinderService(Context.NETWORK_POLICY_SERVICE, INetworkPolicyManager.class)131     addBinderService(Context.NETWORK_POLICY_SERVICE, INetworkPolicyManager.class);
addBinderService(Context.INPUT_SERVICE, IInputManager.class)132     addBinderService(Context.INPUT_SERVICE, IInputManager.class);
addBinderService(Context.COUNTRY_DETECTOR, ICountryDetector.class)133     addBinderService(Context.COUNTRY_DETECTOR, ICountryDetector.class);
addBinderService(Context.NSD_SERVICE, INsdManager.class)134     addBinderService(Context.NSD_SERVICE, INsdManager.class);
addBinderService(Context.AUDIO_SERVICE, IAudioService.class)135     addBinderService(Context.AUDIO_SERVICE, IAudioService.class);
addBinderService(Context.APPWIDGET_SERVICE, IAppWidgetService.class)136     addBinderService(Context.APPWIDGET_SERVICE, IAppWidgetService.class);
addBinderService(Context.NOTIFICATION_SERVICE, INotificationManager.class)137     addBinderService(Context.NOTIFICATION_SERVICE, INotificationManager.class);
addBinderService(Context.WALLPAPER_SERVICE, IWallpaperManager.class)138     addBinderService(Context.WALLPAPER_SERVICE, IWallpaperManager.class);
addBinderService(Context.BLUETOOTH_SERVICE, IBluetooth.class)139     addBinderService(Context.BLUETOOTH_SERVICE, IBluetooth.class);
addBinderService(Context.WINDOW_SERVICE, IWindowManager.class)140     addBinderService(Context.WINDOW_SERVICE, IWindowManager.class);
addBinderService(Context.NFC_SERVICE, INfcAdapter.class, true)141     addBinderService(Context.NFC_SERVICE, INfcAdapter.class, true);
addBinderService(Context.VIRTUAL_DEVICE_SERVICE, IVirtualDeviceManager.class)142     addBinderService(Context.VIRTUAL_DEVICE_SERVICE, IVirtualDeviceManager.class);
143 
144     if (RuntimeEnvironment.getApiLevel() >= JELLY_BEAN_MR1) {
addBinderService(Context.USER_SERVICE, IUserManager.class)145       addBinderService(Context.USER_SERVICE, IUserManager.class);
addBinderService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE, IBluetoothManager.class)146       addBinderService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE, IBluetoothManager.class);
147     }
148     if (RuntimeEnvironment.getApiLevel() >= JELLY_BEAN_MR2) {
addBinderService(Context.APP_OPS_SERVICE, IAppOpsService.class)149       addBinderService(Context.APP_OPS_SERVICE, IAppOpsService.class);
150     }
151     if (RuntimeEnvironment.getApiLevel() >= KITKAT) {
152       addBinderService("batteryproperties", IBatteryPropertiesRegistrar.class);
153     }
154     if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP) {
addBinderService(Context.RESTRICTIONS_SERVICE, IRestrictionsManager.class)155       addBinderService(Context.RESTRICTIONS_SERVICE, IRestrictionsManager.class);
addBinderService(Context.TRUST_SERVICE, ITrustManager.class)156       addBinderService(Context.TRUST_SERVICE, ITrustManager.class);
addBinderService(Context.JOB_SCHEDULER_SERVICE, IJobScheduler.class)157       addBinderService(Context.JOB_SCHEDULER_SERVICE, IJobScheduler.class);
addBinderService(Context.NETWORK_SCORE_SERVICE, INetworkScoreService.class)158       addBinderService(Context.NETWORK_SCORE_SERVICE, INetworkScoreService.class);
addBinderService(Context.USAGE_STATS_SERVICE, IUsageStatsManager.class)159       addBinderService(Context.USAGE_STATS_SERVICE, IUsageStatsManager.class);
addBinderService(Context.MEDIA_ROUTER_SERVICE, IMediaRouterService.class)160       addBinderService(Context.MEDIA_ROUTER_SERVICE, IMediaRouterService.class);
addBinderService(Context.MEDIA_SESSION_SERVICE, ISessionManager.class, true)161       addBinderService(Context.MEDIA_SESSION_SERVICE, ISessionManager.class, true);
162     }
163     if (RuntimeEnvironment.getApiLevel() >= M) {
addBinderService(Context.FINGERPRINT_SERVICE, IFingerprintService.class)164       addBinderService(Context.FINGERPRINT_SERVICE, IFingerprintService.class);
165     }
166     if (RuntimeEnvironment.getApiLevel() >= N) {
addBinderService(Context.CONTEXTHUB_SERVICE, IContextHubService.class)167       addBinderService(Context.CONTEXTHUB_SERVICE, IContextHubService.class);
168     }
169     if (RuntimeEnvironment.getApiLevel() >= N_MR1) {
addBinderService(Context.SHORTCUT_SERVICE, IShortcutService.class)170       addBinderService(Context.SHORTCUT_SERVICE, IShortcutService.class);
171     }
172     if (RuntimeEnvironment.getApiLevel() >= O) {
173       addBinderService("mount", IStorageManager.class);
addBinderService(Context.WIFI_AWARE_SERVICE, IWifiAwareManager.class)174       addBinderService(Context.WIFI_AWARE_SERVICE, IWifiAwareManager.class);
addBinderService(Context.STORAGE_STATS_SERVICE, IStorageStatsManager.class)175       addBinderService(Context.STORAGE_STATS_SERVICE, IStorageStatsManager.class);
176     } else {
177       addBinderService("mount", "android.os.storage.IMountService");
178     }
179     if (RuntimeEnvironment.getApiLevel() >= P) {
addBinderService(Context.SLICE_SERVICE, ISliceManager.class)180       addBinderService(Context.SLICE_SERVICE, ISliceManager.class);
addBinderService(Context.CROSS_PROFILE_APPS_SERVICE, ICrossProfileApps.class)181       addBinderService(Context.CROSS_PROFILE_APPS_SERVICE, ICrossProfileApps.class);
addBinderService(Context.WIFI_RTT_RANGING_SERVICE, IWifiRttManager.class)182       addBinderService(Context.WIFI_RTT_RANGING_SERVICE, IWifiRttManager.class);
addBinderService(Context.IPSEC_SERVICE, IIpSecService.class)183       addBinderService(Context.IPSEC_SERVICE, IIpSecService.class);
184     }
185     if (RuntimeEnvironment.getApiLevel() >= Q) {
addBinderService(Context.BIOMETRIC_SERVICE, IBiometricService.class)186       addBinderService(Context.BIOMETRIC_SERVICE, IBiometricService.class);
addBinderService(Context.CONTENT_CAPTURE_MANAGER_SERVICE, IContentCaptureManager.class)187       addBinderService(Context.CONTENT_CAPTURE_MANAGER_SERVICE, IContentCaptureManager.class);
addBinderService(Context.ROLE_SERVICE, IRoleManager.class)188       addBinderService(Context.ROLE_SERVICE, IRoleManager.class);
addBinderService(Context.ROLLBACK_SERVICE, IRollbackManager.class)189       addBinderService(Context.ROLLBACK_SERVICE, IRollbackManager.class);
addBinderService(Context.THERMAL_SERVICE, IThermalService.class)190       addBinderService(Context.THERMAL_SERVICE, IThermalService.class);
addBinderService(Context.BUGREPORT_SERVICE, IDumpstate.class)191       addBinderService(Context.BUGREPORT_SERVICE, IDumpstate.class);
192     }
193     if (RuntimeEnvironment.getApiLevel() >= R) {
addBinderService(Context.APP_INTEGRITY_SERVICE, IAppIntegrityManager.class)194       addBinderService(Context.APP_INTEGRITY_SERVICE, IAppIntegrityManager.class);
addBinderService(Context.AUTH_SERVICE, IAuthService.class)195       addBinderService(Context.AUTH_SERVICE, IAuthService.class);
addBinderService(Context.TETHERING_SERVICE, ITetheringConnector.class)196       addBinderService(Context.TETHERING_SERVICE, ITetheringConnector.class);
197       addBinderService("telephony.registry", ITelephonyRegistry.class);
198     }
199     if (RuntimeEnvironment.getApiLevel() >= S) {
200       addBinderService("permissionmgr", IPermissionManager.class);
addBinderService(Context.TIME_ZONE_DETECTOR_SERVICE, ITimeZoneDetectorService.class)201       addBinderService(Context.TIME_ZONE_DETECTOR_SERVICE, ITimeZoneDetectorService.class);
addBinderService(Context.TIME_DETECTOR_SERVICE, ITimeDetectorService.class)202       addBinderService(Context.TIME_DETECTOR_SERVICE, ITimeDetectorService.class);
addBinderService(Context.SPEECH_RECOGNITION_SERVICE, IRecognitionServiceManager.class)203       addBinderService(Context.SPEECH_RECOGNITION_SERVICE, IRecognitionServiceManager.class);
addBinderService(Context.LEGACY_PERMISSION_SERVICE, ILegacyPermissionManager.class)204       addBinderService(Context.LEGACY_PERMISSION_SERVICE, ILegacyPermissionManager.class);
addBinderService(Context.UWB_SERVICE, IUwbAdapter.class)205       addBinderService(Context.UWB_SERVICE, IUwbAdapter.class);
addBinderService(Context.VCN_MANAGEMENT_SERVICE, IVcnManagementService.class)206       addBinderService(Context.VCN_MANAGEMENT_SERVICE, IVcnManagementService.class);
addBinderService(Context.TRANSLATION_MANAGER_SERVICE, ITranslationManager.class)207       addBinderService(Context.TRANSLATION_MANAGER_SERVICE, ITranslationManager.class);
208     }
209     if (RuntimeEnvironment.getApiLevel() >= TIRAMISU) {
addBinderService(Context.AMBIENT_CONTEXT_SERVICE, IAmbientContextManager.class)210       addBinderService(Context.AMBIENT_CONTEXT_SERVICE, IAmbientContextManager.class);
addBinderService(Context.LOCALE_SERVICE, ILocaleManager.class)211       addBinderService(Context.LOCALE_SERVICE, ILocaleManager.class);
addBinderService(Context.SAFETY_CENTER_SERVICE, ISafetyCenterManager.class)212       addBinderService(Context.SAFETY_CENTER_SERVICE, ISafetyCenterManager.class);
addBinderService(Context.STATUS_BAR_SERVICE, IStatusBar.class)213       addBinderService(Context.STATUS_BAR_SERVICE, IStatusBar.class);
214     }
215   }
216 
217   /**
218    * A data class that holds descriptor information about binder services. It also holds the cached
219    * binder object if it is requested by {@link #getService(String)}.
220    */
221   private static class BinderService {
222 
223     private final Class<? extends IInterface> clazz;
224     private final String className;
225     private final boolean useDeepBinder;
226     private Binder cachedBinder;
227 
BinderService(Class<? extends IInterface> clazz, String className, boolean useDeepBinder)228     BinderService(Class<? extends IInterface> clazz, String className, boolean useDeepBinder) {
229       this.clazz = clazz;
230       this.className = className;
231       this.useDeepBinder = useDeepBinder;
232     }
233 
234     // Needs to be synchronized in case multiple threads call ServiceManager.getService
235     // concurrently.
getBinder()236     synchronized IBinder getBinder() {
237       if (cachedBinder == null) {
238         cachedBinder = new Binder();
239         cachedBinder.attachInterface(
240             useDeepBinder
241                 ? ReflectionHelpers.createDeepProxy(clazz)
242                 : ReflectionHelpers.createNullProxy(clazz),
243             className);
244       }
245       return cachedBinder;
246     }
247   }
248 
addBinderService(String name, Class<? extends IInterface> clazz)249   protected static void addBinderService(String name, Class<? extends IInterface> clazz) {
250     addBinderService(name, clazz, clazz.getCanonicalName(), false);
251   }
252 
addBinderService( String name, Class<? extends IInterface> clazz, boolean useDeepBinder)253   protected static void addBinderService(
254       String name, Class<? extends IInterface> clazz, boolean useDeepBinder) {
255     addBinderService(name, clazz, clazz.getCanonicalName(), useDeepBinder);
256   }
257 
addBinderService(String name, String className)258   protected static void addBinderService(String name, String className) {
259     Class<? extends IInterface> clazz;
260     try {
261       clazz = Class.forName(className).asSubclass(IInterface.class);
262     } catch (ClassNotFoundException e) {
263       throw new RuntimeException(e);
264     }
265     addBinderService(name, clazz, className, false);
266   }
267 
addBinderService( String name, Class<? extends IInterface> clazz, String className, boolean useDeepBinder)268   protected static void addBinderService(
269       String name, Class<? extends IInterface> clazz, String className, boolean useDeepBinder) {
270     binderServices.put(name, new BinderService(clazz, className, useDeepBinder));
271   }
272   /**
273    * Returns the binder associated with the given system service. If the given service is set to
274    * unavailable in {@link #setServiceAvailability}, {@code null} will be returned.
275    */
276   @Implementation
getService(String name)277   protected static IBinder getService(String name) {
278     if (unavailableServices.contains(name)) {
279       return null;
280     }
281     BinderService binderService = binderServices.get(name);
282     if (binderService == null) {
283       return null;
284     }
285 
286     return binderService.getBinder();
287   }
288 
289   @Implementation
addService(String name, IBinder service)290   protected static void addService(String name, IBinder service) {}
291 
292   @Implementation
checkService(String name)293   protected static IBinder checkService(String name) {
294     return null;
295   }
296 
297   @Implementation
listServices()298   protected static String[] listServices() throws RemoteException {
299     return null;
300   }
301 
302   @Implementation
initServiceCache(Map<String, IBinder> cache)303   protected static void initServiceCache(Map<String, IBinder> cache) {}
304 
305   /**
306    * Sets the availability of the given system service. If the service is set as unavailable,
307    * subsequent calls to {@link Context#getSystemService} for that service will return {@code null}.
308    */
setServiceAvailability(String service, boolean available)309   public static void setServiceAvailability(String service, boolean available) {
310     if (available) {
311       unavailableServices.remove(service);
312     } else {
313       unavailableServices.add(service);
314     }
315   }
316 
317   @Resetter
reset()318   public static void reset() {
319     unavailableServices.clear();
320   }
321 }
322