OSDN Git Service

Merge "Avoid activity leak via Companion callback" into oc-dev
[android-x86/frameworks-base.git] / core / java / android / app / SystemServiceRegistry.java
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package android.app;
18
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;
142
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;
149
150 import java.util.HashMap;
151
152 /**
153  * Manages all of the system services that can be returned by {@link Context#getSystemService}.
154  * Used by {@link ContextImpl}.
155  */
156 final class SystemServiceRegistry {
157     private static final String TAG = "SystemServiceRegistry";
158
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;
166
167     // Not instantiable.
168     private SystemServiceRegistry() { }
169
170     static {
171         registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
172                 new CachedServiceFetcher<AccessibilityManager>() {
173             @Override
174             public AccessibilityManager createService(ContextImpl ctx) {
175                 return AccessibilityManager.getInstance(ctx);
176             }});
177
178         registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
179                 new CachedServiceFetcher<CaptioningManager>() {
180             @Override
181             public CaptioningManager createService(ContextImpl ctx) {
182                 return new CaptioningManager(ctx);
183             }});
184
185         registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
186                 new CachedServiceFetcher<AccountManager>() {
187             @Override
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);
192             }});
193
194         registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
195                 new CachedServiceFetcher<ActivityManager>() {
196             @Override
197             public ActivityManager createService(ContextImpl ctx) {
198                 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
199             }});
200
201         registerService(Context.ALARM_SERVICE, AlarmManager.class,
202                 new CachedServiceFetcher<AlarmManager>() {
203             @Override
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);
208             }});
209
210         registerService(Context.AUDIO_SERVICE, AudioManager.class,
211                 new CachedServiceFetcher<AudioManager>() {
212             @Override
213             public AudioManager createService(ContextImpl ctx) {
214                 return new AudioManager(ctx);
215             }});
216
217         registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
218                 new CachedServiceFetcher<MediaRouter>() {
219             @Override
220             public MediaRouter createService(ContextImpl ctx) {
221                 return new MediaRouter(ctx);
222             }});
223
224         registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
225                 new CachedServiceFetcher<BluetoothManager>() {
226             @Override
227             public BluetoothManager createService(ContextImpl ctx) {
228                 return new BluetoothManager(ctx);
229             }});
230
231         registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
232                 new StaticServiceFetcher<HdmiControlManager>() {
233             @Override
234             public HdmiControlManager createService() throws ServiceNotFoundException {
235                 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
236                 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
237             }});
238
239         registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
240                 new CachedServiceFetcher<TextClassificationManager>() {
241             @Override
242             public TextClassificationManager createService(ContextImpl ctx) {
243                 return new TextClassificationManager(ctx);
244             }});
245
246         registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
247                 new CachedServiceFetcher<ClipboardManager>() {
248             @Override
249             public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
250                 return new ClipboardManager(ctx.getOuterContext(),
251                         ctx.mMainThread.getHandler());
252             }});
253
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);
258
259         registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
260                 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
261             @Override
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);
266             }});
267
268         registerService(Context.IPSEC_SERVICE, IpSecManager.class,
269                 new StaticServiceFetcher<IpSecManager>() {
270             @Override
271             public IpSecManager createService() {
272                 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
273                 IIpSecService service = IIpSecService.Stub.asInterface(b);
274                 return new IpSecManager(service);
275             }});
276
277         registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
278                 new StaticServiceFetcher<CountryDetector>() {
279             @Override
280             public CountryDetector createService() throws ServiceNotFoundException {
281                 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
282                 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
283             }});
284
285         registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
286                 new CachedServiceFetcher<DevicePolicyManager>() {
287             @Override
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));
291             }});
292
293         registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
294                 new CachedServiceFetcher<DownloadManager>() {
295             @Override
296             public DownloadManager createService(ContextImpl ctx) {
297                 return new DownloadManager(ctx);
298             }});
299
300         registerService(Context.BATTERY_SERVICE, BatteryManager.class,
301                 new StaticServiceFetcher<BatteryManager>() {
302             @Override
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);
309             }});
310
311         registerService(Context.NFC_SERVICE, NfcManager.class,
312                 new CachedServiceFetcher<NfcManager>() {
313             @Override
314             public NfcManager createService(ContextImpl ctx) {
315                 return new NfcManager(ctx);
316             }});
317
318         registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
319                 new CachedServiceFetcher<DropBoxManager>() {
320             @Override
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);
325             }});
326
327         registerService(Context.INPUT_SERVICE, InputManager.class,
328                 new StaticServiceFetcher<InputManager>() {
329             @Override
330             public InputManager createService() {
331                 return InputManager.getInstance();
332             }});
333
334         registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
335                 new CachedServiceFetcher<DisplayManager>() {
336             @Override
337             public DisplayManager createService(ContextImpl ctx) {
338                 return new DisplayManager(ctx.getOuterContext());
339             }});
340
341         registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
342                 new StaticServiceFetcher<InputMethodManager>() {
343             @Override
344             public InputMethodManager createService() {
345                 return InputMethodManager.getInstance();
346             }});
347
348         registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
349                 new StaticServiceFetcher<TextServicesManager>() {
350             @Override
351             public TextServicesManager createService() {
352                 return TextServicesManager.getInstance();
353             }});
354
355         registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
356                 new CachedServiceFetcher<KeyguardManager>() {
357             @Override
358             public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
359                 return new KeyguardManager(ctx);
360             }});
361
362         registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
363                 new CachedServiceFetcher<LayoutInflater>() {
364             @Override
365             public LayoutInflater createService(ContextImpl ctx) {
366                 return new PhoneLayoutInflater(ctx.getOuterContext());
367             }});
368
369         registerService(Context.LOCATION_SERVICE, LocationManager.class,
370                 new CachedServiceFetcher<LocationManager>() {
371             @Override
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));
375             }});
376
377         registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
378                 new CachedServiceFetcher<NetworkPolicyManager>() {
379             @Override
380             public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
381                 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
382                         ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
383             }});
384
385         registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
386                 new CachedServiceFetcher<NotificationManager>() {
387             @Override
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());
399             }});
400
401         registerService(Context.NSD_SERVICE, NsdManager.class,
402                 new CachedServiceFetcher<NsdManager>() {
403             @Override
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);
408             }});
409
410         registerService(Context.POWER_SERVICE, PowerManager.class,
411                 new CachedServiceFetcher<PowerManager>() {
412             @Override
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());
418             }});
419
420         registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
421                 new CachedServiceFetcher<RecoverySystem>() {
422             @Override
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);
427             }});
428
429         registerService(Context.SEARCH_SERVICE, SearchManager.class,
430                 new CachedServiceFetcher<SearchManager>() {
431             @Override
432             public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
433                 return new SearchManager(ctx.getOuterContext(),
434                         ctx.mMainThread.getHandler());
435             }});
436
437         registerService(Context.SENSOR_SERVICE, SensorManager.class,
438                 new CachedServiceFetcher<SensorManager>() {
439             @Override
440             public SensorManager createService(ContextImpl ctx) {
441                 return new SystemSensorManager(ctx.getOuterContext(),
442                   ctx.mMainThread.getHandler().getLooper());
443             }});
444
445         registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
446                 new CachedServiceFetcher<StatusBarManager>() {
447             @Override
448             public StatusBarManager createService(ContextImpl ctx) {
449                 return new StatusBarManager(ctx.getOuterContext());
450             }});
451
452         registerService(Context.STORAGE_SERVICE, StorageManager.class,
453                 new CachedServiceFetcher<StorageManager>() {
454             @Override
455             public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
456                 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
457             }});
458
459         registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
460                 new CachedServiceFetcher<StorageStatsManager>() {
461             @Override
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);
466             }});
467
468         registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
469                 new CachedServiceFetcher<TelephonyManager>() {
470             @Override
471             public TelephonyManager createService(ContextImpl ctx) {
472                 return new TelephonyManager(ctx.getOuterContext());
473             }});
474
475         registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
476                 new CachedServiceFetcher<SubscriptionManager>() {
477             @Override
478             public SubscriptionManager createService(ContextImpl ctx) {
479                 return new SubscriptionManager(ctx.getOuterContext());
480             }});
481
482         registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
483                 new CachedServiceFetcher<CarrierConfigManager>() {
484             @Override
485             public CarrierConfigManager createService(ContextImpl ctx) {
486                 return new CarrierConfigManager();
487             }});
488
489         registerService(Context.TELECOM_SERVICE, TelecomManager.class,
490                 new CachedServiceFetcher<TelecomManager>() {
491             @Override
492             public TelecomManager createService(ContextImpl ctx) {
493                 return new TelecomManager(ctx.getOuterContext());
494             }});
495
496         registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
497                 new CachedServiceFetcher<UiModeManager>() {
498             @Override
499             public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
500                 return new UiModeManager();
501             }});
502
503         registerService(Context.USB_SERVICE, UsbManager.class,
504                 new CachedServiceFetcher<UsbManager>() {
505             @Override
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));
509             }});
510
511         registerService(Context.SERIAL_SERVICE, SerialManager.class,
512                 new CachedServiceFetcher<SerialManager>() {
513             @Override
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));
517             }});
518
519         registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
520                 new CachedServiceFetcher<Vibrator>() {
521             @Override
522             public Vibrator createService(ContextImpl ctx) {
523                 return new SystemVibrator(ctx);
524             }});
525
526         registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
527                 new CachedServiceFetcher<WallpaperManager>() {
528             @Override
529             public WallpaperManager createService(ContextImpl ctx) {
530                 return new WallpaperManager(ctx.getOuterContext(),
531                         ctx.mMainThread.getHandler());
532             }});
533
534         registerService(Context.WIFI_SERVICE, WifiManager.class,
535                 new CachedServiceFetcher<WifiManager>() {
536             @Override
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());
542             }});
543
544         registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
545                 new StaticServiceFetcher<WifiP2pManager>() {
546             @Override
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);
551             }});
552
553         registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class,
554                 new CachedServiceFetcher<WifiAwareManager>() {
555             @Override
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) {
560                     return null;
561                 }
562                 return new WifiAwareManager(ctx.getOuterContext(), service);
563             }});
564
565         registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
566                 new CachedServiceFetcher<WifiScanner>() {
567             @Override
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());
573             }});
574
575         registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
576                 new CachedServiceFetcher<RttManager>() {
577             @Override
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());
583             }});
584
585         registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
586                 new CachedServiceFetcher<EthernetManager>() {
587             @Override
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);
592             }});
593
594         registerService(Context.WINDOW_SERVICE, WindowManager.class,
595                 new CachedServiceFetcher<WindowManager>() {
596             @Override
597             public WindowManager createService(ContextImpl ctx) {
598                 return new WindowManagerImpl(ctx);
599             }});
600
601         registerService(Context.USER_SERVICE, UserManager.class,
602                 new CachedServiceFetcher<UserManager>() {
603             @Override
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);
608             }});
609
610         registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
611                 new CachedServiceFetcher<AppOpsManager>() {
612             @Override
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);
617             }});
618
619         registerService(Context.CAMERA_SERVICE, CameraManager.class,
620                 new CachedServiceFetcher<CameraManager>() {
621             @Override
622             public CameraManager createService(ContextImpl ctx) {
623                 return new CameraManager(ctx);
624             }});
625
626         registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
627                 new CachedServiceFetcher<LauncherApps>() {
628             @Override
629             public LauncherApps createService(ContextImpl ctx) {
630                 return new LauncherApps(ctx);
631             }});
632
633         registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
634                 new CachedServiceFetcher<RestrictionsManager>() {
635             @Override
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);
640             }});
641
642         registerService(Context.PRINT_SERVICE, PrintManager.class,
643                 new CachedServiceFetcher<PrintManager>() {
644             @Override
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()));
651             }});
652
653         registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
654                 new CachedServiceFetcher<CompanionDeviceManager>() {
655                     @Override
656                     public CompanionDeviceManager createService(ContextImpl ctx)
657                             throws ServiceNotFoundException {
658                         // Get the services without throwing as this is an optional feature
659                         IBinder iBinder =
660                                 ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE);
661                         ICompanionDeviceManager service =
662                                 ICompanionDeviceManager.Stub.asInterface(iBinder);
663                         return new CompanionDeviceManager(service, ctx.getOuterContext());
664                     }});
665
666         registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
667                 new CachedServiceFetcher<ConsumerIrManager>() {
668             @Override
669             public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
670                 return new ConsumerIrManager(ctx);
671             }});
672
673         registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
674                 new CachedServiceFetcher<MediaSessionManager>() {
675             @Override
676             public MediaSessionManager createService(ContextImpl ctx) {
677                 return new MediaSessionManager(ctx);
678             }});
679
680         registerService(Context.TRUST_SERVICE, TrustManager.class,
681                 new StaticServiceFetcher<TrustManager>() {
682             @Override
683             public TrustManager createService() throws ServiceNotFoundException {
684                 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
685                 return new TrustManager(b);
686             }});
687
688         registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
689                 new CachedServiceFetcher<FingerprintManager>() {
690             @Override
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);
695                 } else {
696                     binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
697                 }
698                 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
699                 return new FingerprintManager(ctx.getOuterContext(), service);
700             }});
701
702         registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
703                 new StaticServiceFetcher<TvInputManager>() {
704             @Override
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());
709             }});
710
711         registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
712                 new CachedServiceFetcher<NetworkScoreManager>() {
713             @Override
714             public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
715                 return new NetworkScoreManager(ctx);
716             }});
717
718         registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
719                 new CachedServiceFetcher<UsageStatsManager>() {
720             @Override
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);
725             }});
726
727         registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
728                 new CachedServiceFetcher<NetworkStatsManager>() {
729             @Override
730             public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
731                 return new NetworkStatsManager(ctx.getOuterContext());
732             }});
733
734         registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
735                 new StaticServiceFetcher<JobScheduler>() {
736             @Override
737             public JobScheduler createService() throws ServiceNotFoundException {
738                 IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE);
739                 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
740             }});
741
742         registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
743                 new StaticServiceFetcher<PersistentDataBlockManager>() {
744             @Override
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);
751                 } else {
752                     // not supported
753                     return null;
754                 }
755             }});
756
757         registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
758                 new StaticServiceFetcher<OemLockManager>() {
759             @Override
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);
765                 } else {
766                     // not supported
767                     return null;
768                 }
769             }});
770
771         registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
772                 new CachedServiceFetcher<MediaProjectionManager>() {
773             @Override
774             public MediaProjectionManager createService(ContextImpl ctx) {
775                 return new MediaProjectionManager(ctx);
776             }});
777
778         registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
779                 new CachedServiceFetcher<AppWidgetManager>() {
780             @Override
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));
784             }});
785
786         registerService(Context.MIDI_SERVICE, MidiManager.class,
787                 new CachedServiceFetcher<MidiManager>() {
788             @Override
789             public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
790                 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
791                 return new MidiManager(IMidiManager.Stub.asInterface(b));
792             }});
793
794         registerService(Context.RADIO_SERVICE, RadioManager.class,
795                 new CachedServiceFetcher<RadioManager>() {
796             @Override
797             public RadioManager createService(ContextImpl ctx) {
798                 return new RadioManager(ctx);
799             }});
800
801         registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
802                 new CachedServiceFetcher<HardwarePropertiesManager>() {
803             @Override
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);
809             }});
810
811         registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
812                 new CachedServiceFetcher<SoundTriggerManager>() {
813             @Override
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));
817             }});
818
819         registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
820                 new CachedServiceFetcher<ShortcutManager>() {
821             @Override
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));
825             }});
826
827         registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
828                 new CachedServiceFetcher<SystemHealthManager>() {
829             @Override
830             public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
831                 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
832                 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
833             }});
834
835         registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
836                 new CachedServiceFetcher<ContextHubManager>() {
837             @Override
838             public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
839                 return new ContextHubManager(ctx.getOuterContext(),
840                   ctx.mMainThread.getHandler().getLooper());
841             }});
842
843         registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
844                 new CachedServiceFetcher<IncidentManager>() {
845             @Override
846             public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
847                 return new IncidentManager(ctx);
848             }});
849
850         registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
851                 new CachedServiceFetcher<AutofillManager>() {
852             @Override
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);
858             }});
859
860         registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
861             @Override
862             public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
863                 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
864                 return new VrManager(IVrManager.Stub.asInterface(b));
865             }
866         });
867
868         registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
869                 new CachedServiceFetcher<RulesManager>() {
870             @Override
871             public RulesManager createService(ContextImpl ctx) {
872                 return new RulesManager(ctx.getOuterContext());
873             }});
874     }
875
876     /**
877      * Creates an array which is used to cache per-Context service instances.
878      */
879     public static Object[] createServiceCache() {
880         return new Object[sServiceCacheSize];
881     }
882
883     /**
884      * Gets a system service from a given context.
885      */
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;
889     }
890
891     /**
892      * Gets the name of the system-level service that is represented by the specified class.
893      */
894     public static String getSystemServiceName(Class<?> serviceClass) {
895         return SYSTEM_SERVICE_NAMES.get(serviceClass);
896     }
897
898     /**
899      * Statically registers a system service with the context.
900      * This method must be called during static initialization only.
901      */
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);
906     }
907
908     /**
909      * Base interface for classes that fetch services.
910      * These objects must only be created during static initialization.
911      */
912     static abstract interface ServiceFetcher<T> {
913         T getService(ContextImpl ctx);
914     }
915
916     /**
917      * Override this class when the system service constructor needs a
918      * ContextImpl and should be cached and retained by that context.
919      */
920     static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
921         private final int mCacheIndex;
922
923         public CachedServiceFetcher() {
924             mCacheIndex = sServiceCacheSize++;
925         }
926
927         @Override
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) {
935                     try {
936                         service = createService(ctx);
937                         cache[mCacheIndex] = service;
938                     } catch (ServiceNotFoundException e) {
939                         onServiceNotFound(e);
940                     }
941                 }
942                 return (T)service;
943             }
944         }
945
946         public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
947     }
948
949     /**
950      * Override this class when the system service does not need a ContextImpl
951      * and should be cached and retained process-wide.
952      */
953     static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
954         private T mCachedInstance;
955
956         @Override
957         public final T getService(ContextImpl ctx) {
958             synchronized (StaticServiceFetcher.this) {
959                 if (mCachedInstance == null) {
960                     try {
961                         mCachedInstance = createService();
962                     } catch (ServiceNotFoundException e) {
963                         onServiceNotFound(e);
964                     }
965                 }
966                 return mCachedInstance;
967             }
968         }
969
970         public abstract T createService() throws ServiceNotFoundException;
971     }
972
973     /**
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
976      * application.
977      *
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.
980      */
981     static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
982         private T mCachedInstance;
983
984         @Override
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 .
993                     try {
994                         mCachedInstance = createService(appContext != null ? appContext : ctx);
995                     } catch (ServiceNotFoundException e) {
996                         onServiceNotFound(e);
997                     }
998                 }
999                 return mCachedInstance;
1000             }
1001         }
1002
1003         public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
1004     }
1005
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);
1012         } else {
1013             Log.w(TAG, e.getMessage());
1014         }
1015     }
1016 }