2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import android.accounts.AccountManager;
20 import android.accounts.IAccountManager;
21 import android.app.admin.DevicePolicyManager;
22 import android.app.admin.IDevicePolicyManager;
23 import android.app.job.IJobScheduler;
24 import android.app.job.JobScheduler;
25 import android.app.timezone.RulesManager;
26 import android.app.trust.TrustManager;
27 import android.app.usage.IStorageStatsManager;
28 import android.app.usage.IUsageStatsManager;
29 import android.app.usage.NetworkStatsManager;
30 import android.app.usage.StorageStatsManager;
31 import android.app.usage.UsageStatsManager;
32 import android.appwidget.AppWidgetManager;
33 import android.bluetooth.BluetoothManager;
34 import android.companion.CompanionDeviceManager;
35 import android.companion.ICompanionDeviceManager;
36 import android.content.ClipboardManager;
37 import android.content.Context;
38 import android.content.IRestrictionsManager;
39 import android.content.RestrictionsManager;
40 import android.content.pm.IShortcutService;
41 import android.content.pm.LauncherApps;
42 import android.content.pm.ShortcutManager;
43 import android.content.res.Resources;
44 import android.hardware.ConsumerIrManager;
45 import android.hardware.ISerialManager;
46 import android.hardware.SensorManager;
47 import android.hardware.SerialManager;
48 import android.hardware.SystemSensorManager;
49 import android.hardware.camera2.CameraManager;
50 import android.hardware.display.DisplayManager;
51 import android.hardware.fingerprint.FingerprintManager;
52 import android.hardware.fingerprint.IFingerprintService;
53 import android.hardware.hdmi.HdmiControlManager;
54 import android.hardware.hdmi.IHdmiControlService;
55 import android.hardware.input.InputManager;
56 import android.hardware.location.ContextHubManager;
57 import android.hardware.radio.RadioManager;
58 import android.hardware.usb.IUsbManager;
59 import android.hardware.usb.UsbManager;
60 import android.location.CountryDetector;
61 import android.location.ICountryDetector;
62 import android.location.ILocationManager;
63 import android.location.LocationManager;
64 import android.media.AudioManager;
65 import android.media.MediaRouter;
66 import android.media.midi.IMidiManager;
67 import android.media.midi.MidiManager;
68 import android.media.projection.MediaProjectionManager;
69 import android.media.session.MediaSessionManager;
70 import android.media.soundtrigger.SoundTriggerManager;
71 import android.media.tv.ITvInputManager;
72 import android.media.tv.TvInputManager;
73 import android.net.ConnectivityManager;
74 import android.net.ConnectivityThread;
75 import android.net.EthernetManager;
76 import android.net.IConnectivityManager;
77 import android.net.IEthernetManager;
78 import android.net.IIpSecService;
79 import android.net.INetworkPolicyManager;
80 import android.net.IpSecManager;
81 import android.net.NetworkPolicyManager;
82 import android.net.NetworkScoreManager;
83 import android.net.nsd.INsdManager;
84 import android.net.nsd.NsdManager;
85 import android.net.wifi.IRttManager;
86 import android.net.wifi.IWifiManager;
87 import android.net.wifi.IWifiScanner;
88 import android.net.wifi.RttManager;
89 import android.net.wifi.WifiManager;
90 import android.net.wifi.WifiScanner;
91 import android.net.wifi.aware.IWifiAwareManager;
92 import android.net.wifi.aware.WifiAwareManager;
93 import android.net.wifi.p2p.IWifiP2pManager;
94 import android.net.wifi.p2p.WifiP2pManager;
95 import android.nfc.NfcManager;
96 import android.os.BatteryManager;
97 import android.os.BatteryStats;
98 import android.os.Build;
99 import android.os.DropBoxManager;
100 import android.os.HardwarePropertiesManager;
101 import android.os.IBatteryPropertiesRegistrar;
102 import android.os.IBinder;
103 import android.os.IHardwarePropertiesManager;
104 import android.os.IPowerManager;
105 import android.os.IRecoverySystem;
106 import android.os.IUserManager;
107 import android.os.IncidentManager;
108 import android.os.PowerManager;
109 import android.os.Process;
110 import android.os.RecoverySystem;
111 import android.os.ServiceManager;
112 import android.os.ServiceManager.ServiceNotFoundException;
113 import android.os.SystemVibrator;
114 import android.os.UserHandle;
115 import android.os.UserManager;
116 import android.os.Vibrator;
117 import android.os.health.SystemHealthManager;
118 import android.os.storage.StorageManager;
119 import android.print.IPrintManager;
120 import android.print.PrintManager;
121 import android.service.oemlock.IOemLockService;
122 import android.service.oemlock.OemLockManager;
123 import android.service.persistentdata.IPersistentDataBlockService;
124 import android.service.persistentdata.PersistentDataBlockManager;
125 import android.service.vr.IVrManager;
126 import android.telecom.TelecomManager;
127 import android.telephony.CarrierConfigManager;
128 import android.telephony.SubscriptionManager;
129 import android.telephony.TelephonyManager;
130 import android.util.Log;
131 import android.view.ContextThemeWrapper;
132 import android.view.LayoutInflater;
133 import android.view.WindowManager;
134 import android.view.WindowManagerImpl;
135 import android.view.accessibility.AccessibilityManager;
136 import android.view.accessibility.CaptioningManager;
137 import android.view.autofill.AutofillManager;
138 import android.view.autofill.IAutoFillManager;
139 import android.view.inputmethod.InputMethodManager;
140 import android.view.textclassifier.TextClassificationManager;
141 import android.view.textservice.TextServicesManager;
143 import com.android.internal.app.IAppOpsService;
144 import com.android.internal.app.IBatteryStats;
145 import com.android.internal.app.ISoundTriggerService;
146 import com.android.internal.appwidget.IAppWidgetService;
147 import com.android.internal.os.IDropBoxManagerService;
148 import com.android.internal.policy.PhoneLayoutInflater;
150 import java.util.HashMap;
153 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
154 * Used by {@link ContextImpl}.
156 final class SystemServiceRegistry {
157 private static final String TAG = "SystemServiceRegistry";
159 // Service registry information.
160 // This information is never changed once static initialization has completed.
161 private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
162 new HashMap<Class<?>, String>();
163 private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
164 new HashMap<String, ServiceFetcher<?>>();
165 private static int sServiceCacheSize;
168 private SystemServiceRegistry() { }
171 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
172 new CachedServiceFetcher<AccessibilityManager>() {
174 public AccessibilityManager createService(ContextImpl ctx) {
175 return AccessibilityManager.getInstance(ctx);
178 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
179 new CachedServiceFetcher<CaptioningManager>() {
181 public CaptioningManager createService(ContextImpl ctx) {
182 return new CaptioningManager(ctx);
185 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
186 new CachedServiceFetcher<AccountManager>() {
188 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
189 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
190 IAccountManager service = IAccountManager.Stub.asInterface(b);
191 return new AccountManager(ctx, service);
194 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
195 new CachedServiceFetcher<ActivityManager>() {
197 public ActivityManager createService(ContextImpl ctx) {
198 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
201 registerService(Context.ALARM_SERVICE, AlarmManager.class,
202 new CachedServiceFetcher<AlarmManager>() {
204 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
205 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
206 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
207 return new AlarmManager(service, ctx);
210 registerService(Context.AUDIO_SERVICE, AudioManager.class,
211 new CachedServiceFetcher<AudioManager>() {
213 public AudioManager createService(ContextImpl ctx) {
214 return new AudioManager(ctx);
217 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
218 new CachedServiceFetcher<MediaRouter>() {
220 public MediaRouter createService(ContextImpl ctx) {
221 return new MediaRouter(ctx);
224 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
225 new CachedServiceFetcher<BluetoothManager>() {
227 public BluetoothManager createService(ContextImpl ctx) {
228 return new BluetoothManager(ctx);
231 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
232 new StaticServiceFetcher<HdmiControlManager>() {
234 public HdmiControlManager createService() throws ServiceNotFoundException {
235 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
236 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
239 registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
240 new CachedServiceFetcher<TextClassificationManager>() {
242 public TextClassificationManager createService(ContextImpl ctx) {
243 return new TextClassificationManager(ctx);
246 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
247 new CachedServiceFetcher<ClipboardManager>() {
249 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
250 return new ClipboardManager(ctx.getOuterContext(),
251 ctx.mMainThread.getHandler());
254 // The clipboard service moved to a new package. If someone asks for the old
255 // interface by class then we want to redirect over to the new interface instead
256 // (which extends it).
257 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
259 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
260 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
262 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
263 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
264 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
265 return new ConnectivityManager(context, service);
268 registerService(Context.IPSEC_SERVICE, IpSecManager.class,
269 new StaticServiceFetcher<IpSecManager>() {
271 public IpSecManager createService() {
272 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
273 IIpSecService service = IIpSecService.Stub.asInterface(b);
274 return new IpSecManager(service);
277 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
278 new StaticServiceFetcher<CountryDetector>() {
280 public CountryDetector createService() throws ServiceNotFoundException {
281 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
282 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
285 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
286 new CachedServiceFetcher<DevicePolicyManager>() {
288 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
289 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
290 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
293 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
294 new CachedServiceFetcher<DownloadManager>() {
296 public DownloadManager createService(ContextImpl ctx) {
297 return new DownloadManager(ctx);
300 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
301 new StaticServiceFetcher<BatteryManager>() {
303 public BatteryManager createService() throws ServiceNotFoundException {
304 IBatteryStats stats = IBatteryStats.Stub.asInterface(
305 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
306 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
307 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
308 return new BatteryManager(stats, registrar);
311 registerService(Context.NFC_SERVICE, NfcManager.class,
312 new CachedServiceFetcher<NfcManager>() {
314 public NfcManager createService(ContextImpl ctx) {
315 return new NfcManager(ctx);
318 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
319 new CachedServiceFetcher<DropBoxManager>() {
321 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
322 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
323 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
324 return new DropBoxManager(ctx, service);
327 registerService(Context.INPUT_SERVICE, InputManager.class,
328 new StaticServiceFetcher<InputManager>() {
330 public InputManager createService() {
331 return InputManager.getInstance();
334 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
335 new CachedServiceFetcher<DisplayManager>() {
337 public DisplayManager createService(ContextImpl ctx) {
338 return new DisplayManager(ctx.getOuterContext());
341 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
342 new StaticServiceFetcher<InputMethodManager>() {
344 public InputMethodManager createService() {
345 return InputMethodManager.getInstance();
348 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
349 new StaticServiceFetcher<TextServicesManager>() {
351 public TextServicesManager createService() {
352 return TextServicesManager.getInstance();
355 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
356 new CachedServiceFetcher<KeyguardManager>() {
358 public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
359 return new KeyguardManager(ctx);
362 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
363 new CachedServiceFetcher<LayoutInflater>() {
365 public LayoutInflater createService(ContextImpl ctx) {
366 return new PhoneLayoutInflater(ctx.getOuterContext());
369 registerService(Context.LOCATION_SERVICE, LocationManager.class,
370 new CachedServiceFetcher<LocationManager>() {
372 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
373 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
374 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
377 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
378 new CachedServiceFetcher<NetworkPolicyManager>() {
380 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
381 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
382 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
385 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
386 new CachedServiceFetcher<NotificationManager>() {
388 public NotificationManager createService(ContextImpl ctx) {
389 final Context outerContext = ctx.getOuterContext();
390 return new NotificationManager(
391 new ContextThemeWrapper(outerContext,
392 Resources.selectSystemTheme(0,
393 outerContext.getApplicationInfo().targetSdkVersion,
394 com.android.internal.R.style.Theme_Dialog,
395 com.android.internal.R.style.Theme_Holo_Dialog,
396 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
397 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
398 ctx.mMainThread.getHandler());
401 registerService(Context.NSD_SERVICE, NsdManager.class,
402 new CachedServiceFetcher<NsdManager>() {
404 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
405 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
406 INsdManager service = INsdManager.Stub.asInterface(b);
407 return new NsdManager(ctx.getOuterContext(), service);
410 registerService(Context.POWER_SERVICE, PowerManager.class,
411 new CachedServiceFetcher<PowerManager>() {
413 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
414 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
415 IPowerManager service = IPowerManager.Stub.asInterface(b);
416 return new PowerManager(ctx.getOuterContext(),
417 service, ctx.mMainThread.getHandler());
420 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
421 new CachedServiceFetcher<RecoverySystem>() {
423 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
424 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
425 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
426 return new RecoverySystem(service);
429 registerService(Context.SEARCH_SERVICE, SearchManager.class,
430 new CachedServiceFetcher<SearchManager>() {
432 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
433 return new SearchManager(ctx.getOuterContext(),
434 ctx.mMainThread.getHandler());
437 registerService(Context.SENSOR_SERVICE, SensorManager.class,
438 new CachedServiceFetcher<SensorManager>() {
440 public SensorManager createService(ContextImpl ctx) {
441 return new SystemSensorManager(ctx.getOuterContext(),
442 ctx.mMainThread.getHandler().getLooper());
445 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
446 new CachedServiceFetcher<StatusBarManager>() {
448 public StatusBarManager createService(ContextImpl ctx) {
449 return new StatusBarManager(ctx.getOuterContext());
452 registerService(Context.STORAGE_SERVICE, StorageManager.class,
453 new CachedServiceFetcher<StorageManager>() {
455 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
456 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
459 registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
460 new CachedServiceFetcher<StorageStatsManager>() {
462 public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
463 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
464 ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
465 return new StorageStatsManager(ctx, service);
468 registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
469 new CachedServiceFetcher<TelephonyManager>() {
471 public TelephonyManager createService(ContextImpl ctx) {
472 return new TelephonyManager(ctx.getOuterContext());
475 registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
476 new CachedServiceFetcher<SubscriptionManager>() {
478 public SubscriptionManager createService(ContextImpl ctx) {
479 return new SubscriptionManager(ctx.getOuterContext());
482 registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
483 new CachedServiceFetcher<CarrierConfigManager>() {
485 public CarrierConfigManager createService(ContextImpl ctx) {
486 return new CarrierConfigManager();
489 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
490 new CachedServiceFetcher<TelecomManager>() {
492 public TelecomManager createService(ContextImpl ctx) {
493 return new TelecomManager(ctx.getOuterContext());
496 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
497 new CachedServiceFetcher<UiModeManager>() {
499 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
500 return new UiModeManager();
503 registerService(Context.USB_SERVICE, UsbManager.class,
504 new CachedServiceFetcher<UsbManager>() {
506 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
507 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
508 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
511 registerService(Context.SERIAL_SERVICE, SerialManager.class,
512 new CachedServiceFetcher<SerialManager>() {
514 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
515 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
516 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
519 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
520 new CachedServiceFetcher<Vibrator>() {
522 public Vibrator createService(ContextImpl ctx) {
523 return new SystemVibrator(ctx);
526 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
527 new CachedServiceFetcher<WallpaperManager>() {
529 public WallpaperManager createService(ContextImpl ctx) {
530 return new WallpaperManager(ctx.getOuterContext(),
531 ctx.mMainThread.getHandler());
534 registerService(Context.WIFI_SERVICE, WifiManager.class,
535 new CachedServiceFetcher<WifiManager>() {
537 public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
538 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE);
539 IWifiManager service = IWifiManager.Stub.asInterface(b);
540 return new WifiManager(ctx.getOuterContext(), service,
541 ConnectivityThread.getInstanceLooper());
544 registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
545 new StaticServiceFetcher<WifiP2pManager>() {
547 public WifiP2pManager createService() throws ServiceNotFoundException {
548 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE);
549 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
550 return new WifiP2pManager(service);
553 registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class,
554 new CachedServiceFetcher<WifiAwareManager>() {
556 public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException {
557 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE);
558 IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b);
559 if (service == null) {
562 return new WifiAwareManager(ctx.getOuterContext(), service);
565 registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
566 new CachedServiceFetcher<WifiScanner>() {
568 public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException {
569 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE);
570 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
571 return new WifiScanner(ctx.getOuterContext(), service,
572 ConnectivityThread.getInstanceLooper());
575 registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
576 new CachedServiceFetcher<RttManager>() {
578 public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException {
579 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_SERVICE);
580 IRttManager service = IRttManager.Stub.asInterface(b);
581 return new RttManager(ctx.getOuterContext(), service,
582 ConnectivityThread.getInstanceLooper());
585 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
586 new CachedServiceFetcher<EthernetManager>() {
588 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
589 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
590 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
591 return new EthernetManager(ctx.getOuterContext(), service);
594 registerService(Context.WINDOW_SERVICE, WindowManager.class,
595 new CachedServiceFetcher<WindowManager>() {
597 public WindowManager createService(ContextImpl ctx) {
598 return new WindowManagerImpl(ctx);
601 registerService(Context.USER_SERVICE, UserManager.class,
602 new CachedServiceFetcher<UserManager>() {
604 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
605 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
606 IUserManager service = IUserManager.Stub.asInterface(b);
607 return new UserManager(ctx, service);
610 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
611 new CachedServiceFetcher<AppOpsManager>() {
613 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
614 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
615 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
616 return new AppOpsManager(ctx, service);
619 registerService(Context.CAMERA_SERVICE, CameraManager.class,
620 new CachedServiceFetcher<CameraManager>() {
622 public CameraManager createService(ContextImpl ctx) {
623 return new CameraManager(ctx);
626 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
627 new CachedServiceFetcher<LauncherApps>() {
629 public LauncherApps createService(ContextImpl ctx) {
630 return new LauncherApps(ctx);
633 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
634 new CachedServiceFetcher<RestrictionsManager>() {
636 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
637 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
638 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
639 return new RestrictionsManager(ctx, service);
642 registerService(Context.PRINT_SERVICE, PrintManager.class,
643 new CachedServiceFetcher<PrintManager>() {
645 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
646 // Get the services without throwing as this is an optional feature
647 IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE);
648 IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
649 return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
650 UserHandle.getAppId(Process.myUid()));
653 registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
654 new CachedServiceFetcher<CompanionDeviceManager>() {
656 public CompanionDeviceManager createService(ContextImpl ctx)
657 throws ServiceNotFoundException {
658 // Get the services without throwing as this is an optional feature
660 ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE);
661 ICompanionDeviceManager service =
662 ICompanionDeviceManager.Stub.asInterface(iBinder);
663 return new CompanionDeviceManager(service, ctx.getOuterContext());
666 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
667 new CachedServiceFetcher<ConsumerIrManager>() {
669 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
670 return new ConsumerIrManager(ctx);
673 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
674 new CachedServiceFetcher<MediaSessionManager>() {
676 public MediaSessionManager createService(ContextImpl ctx) {
677 return new MediaSessionManager(ctx);
680 registerService(Context.TRUST_SERVICE, TrustManager.class,
681 new StaticServiceFetcher<TrustManager>() {
683 public TrustManager createService() throws ServiceNotFoundException {
684 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
685 return new TrustManager(b);
688 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
689 new CachedServiceFetcher<FingerprintManager>() {
691 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
692 final IBinder binder;
693 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
694 binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
696 binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
698 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
699 return new FingerprintManager(ctx.getOuterContext(), service);
702 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
703 new StaticServiceFetcher<TvInputManager>() {
705 public TvInputManager createService() throws ServiceNotFoundException {
706 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
707 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
708 return new TvInputManager(service, UserHandle.myUserId());
711 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
712 new CachedServiceFetcher<NetworkScoreManager>() {
714 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
715 return new NetworkScoreManager(ctx);
718 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
719 new CachedServiceFetcher<UsageStatsManager>() {
721 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
722 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
723 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
724 return new UsageStatsManager(ctx.getOuterContext(), service);
727 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
728 new CachedServiceFetcher<NetworkStatsManager>() {
730 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
731 return new NetworkStatsManager(ctx.getOuterContext());
734 registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
735 new StaticServiceFetcher<JobScheduler>() {
737 public JobScheduler createService() throws ServiceNotFoundException {
738 IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE);
739 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
742 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
743 new StaticServiceFetcher<PersistentDataBlockManager>() {
745 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
746 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
747 IPersistentDataBlockService persistentDataBlockService =
748 IPersistentDataBlockService.Stub.asInterface(b);
749 if (persistentDataBlockService != null) {
750 return new PersistentDataBlockManager(persistentDataBlockService);
757 registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
758 new StaticServiceFetcher<OemLockManager>() {
760 public OemLockManager createService() throws ServiceNotFoundException {
761 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
762 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
763 if (oemLockService != null) {
764 return new OemLockManager(oemLockService);
771 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
772 new CachedServiceFetcher<MediaProjectionManager>() {
774 public MediaProjectionManager createService(ContextImpl ctx) {
775 return new MediaProjectionManager(ctx);
778 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
779 new CachedServiceFetcher<AppWidgetManager>() {
781 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
782 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
783 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
786 registerService(Context.MIDI_SERVICE, MidiManager.class,
787 new CachedServiceFetcher<MidiManager>() {
789 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
790 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
791 return new MidiManager(IMidiManager.Stub.asInterface(b));
794 registerService(Context.RADIO_SERVICE, RadioManager.class,
795 new CachedServiceFetcher<RadioManager>() {
797 public RadioManager createService(ContextImpl ctx) {
798 return new RadioManager(ctx);
801 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
802 new CachedServiceFetcher<HardwarePropertiesManager>() {
804 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
805 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
806 IHardwarePropertiesManager service =
807 IHardwarePropertiesManager.Stub.asInterface(b);
808 return new HardwarePropertiesManager(ctx, service);
811 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
812 new CachedServiceFetcher<SoundTriggerManager>() {
814 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
815 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
816 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
819 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
820 new CachedServiceFetcher<ShortcutManager>() {
822 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
823 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
824 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
827 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
828 new CachedServiceFetcher<SystemHealthManager>() {
830 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
831 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
832 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
835 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
836 new CachedServiceFetcher<ContextHubManager>() {
838 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
839 return new ContextHubManager(ctx.getOuterContext(),
840 ctx.mMainThread.getHandler().getLooper());
843 registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
844 new CachedServiceFetcher<IncidentManager>() {
846 public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
847 return new IncidentManager(ctx);
850 registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
851 new CachedServiceFetcher<AutofillManager>() {
853 public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
854 // Get the services without throwing as this is an optional feature
855 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
856 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
857 return new AutofillManager(ctx.getOuterContext(), service);
860 registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
862 public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
863 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
864 return new VrManager(IVrManager.Stub.asInterface(b));
868 registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
869 new CachedServiceFetcher<RulesManager>() {
871 public RulesManager createService(ContextImpl ctx) {
872 return new RulesManager(ctx.getOuterContext());
877 * Creates an array which is used to cache per-Context service instances.
879 public static Object[] createServiceCache() {
880 return new Object[sServiceCacheSize];
884 * Gets a system service from a given context.
886 public static Object getSystemService(ContextImpl ctx, String name) {
887 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
888 return fetcher != null ? fetcher.getService(ctx) : null;
892 * Gets the name of the system-level service that is represented by the specified class.
894 public static String getSystemServiceName(Class<?> serviceClass) {
895 return SYSTEM_SERVICE_NAMES.get(serviceClass);
899 * Statically registers a system service with the context.
900 * This method must be called during static initialization only.
902 private static <T> void registerService(String serviceName, Class<T> serviceClass,
903 ServiceFetcher<T> serviceFetcher) {
904 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
905 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
909 * Base interface for classes that fetch services.
910 * These objects must only be created during static initialization.
912 static abstract interface ServiceFetcher<T> {
913 T getService(ContextImpl ctx);
917 * Override this class when the system service constructor needs a
918 * ContextImpl and should be cached and retained by that context.
920 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
921 private final int mCacheIndex;
923 public CachedServiceFetcher() {
924 mCacheIndex = sServiceCacheSize++;
928 @SuppressWarnings("unchecked")
929 public final T getService(ContextImpl ctx) {
930 final Object[] cache = ctx.mServiceCache;
931 synchronized (cache) {
932 // Fetch or create the service.
933 Object service = cache[mCacheIndex];
934 if (service == null) {
936 service = createService(ctx);
937 cache[mCacheIndex] = service;
938 } catch (ServiceNotFoundException e) {
939 onServiceNotFound(e);
946 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
950 * Override this class when the system service does not need a ContextImpl
951 * and should be cached and retained process-wide.
953 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
954 private T mCachedInstance;
957 public final T getService(ContextImpl ctx) {
958 synchronized (StaticServiceFetcher.this) {
959 if (mCachedInstance == null) {
961 mCachedInstance = createService();
962 } catch (ServiceNotFoundException e) {
963 onServiceNotFound(e);
966 return mCachedInstance;
970 public abstract T createService() throws ServiceNotFoundException;
974 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
975 * creating the service for the first time, passes it the application context of the creating
978 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
979 * case where multiple application components each have their own ConnectivityManager object.
981 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
982 private T mCachedInstance;
985 public final T getService(ContextImpl ctx) {
986 synchronized (StaticApplicationContextServiceFetcher.this) {
987 if (mCachedInstance == null) {
988 Context appContext = ctx.getApplicationContext();
989 // If the application context is null, we're either in the system process or
990 // it's the application context very early in app initialization. In both these
991 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
992 // to the service. http://b/27532714 .
994 mCachedInstance = createService(appContext != null ? appContext : ctx);
995 } catch (ServiceNotFoundException e) {
996 onServiceNotFound(e);
999 return mCachedInstance;
1003 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
1006 public static void onServiceNotFound(ServiceNotFoundException e) {
1007 // We're mostly interested in tracking down long-lived core system
1008 // components that might stumble if they obtain bad references; just
1009 // emit a tidy log message for normal apps
1010 if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1011 Log.wtf(TAG, e.getMessage(), e);
1013 Log.w(TAG, e.getMessage());