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