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.ContextImpl.ServiceInitializationState;
22 import android.app.admin.DevicePolicyManager;
23 import android.app.admin.IDevicePolicyManager;
24 import android.app.contentsuggestions.ContentSuggestionsManager;
25 import android.app.contentsuggestions.IContentSuggestionsManager;
26 import android.app.job.IJobScheduler;
27 import android.app.job.JobScheduler;
28 import android.app.prediction.AppPredictionManager;
29 import android.app.role.RoleControllerManager;
30 import android.app.role.RoleManager;
31 import android.app.slice.SliceManager;
32 import android.app.timedetector.TimeDetector;
33 import android.app.timedetector.TimeDetectorImpl;
34 import android.app.timezone.RulesManager;
35 import android.app.timezonedetector.TimeZoneDetector;
36 import android.app.timezonedetector.TimeZoneDetectorImpl;
37 import android.app.trust.TrustManager;
38 import android.app.usage.IStorageStatsManager;
39 import android.app.usage.IUsageStatsManager;
40 import android.app.usage.NetworkStatsManager;
41 import android.app.usage.StorageStatsManager;
42 import android.app.usage.UsageStatsManager;
43 import android.appwidget.AppWidgetManager;
44 import android.bluetooth.BluetoothManager;
45 import android.companion.CompanionDeviceManager;
46 import android.companion.ICompanionDeviceManager;
47 import android.content.ClipboardManager;
48 import android.content.ContentCaptureOptions;
49 import android.content.Context;
50 import android.content.IRestrictionsManager;
51 import android.content.RestrictionsManager;
52 import android.content.om.IOverlayManager;
53 import android.content.om.OverlayManager;
54 import android.content.pm.CrossProfileApps;
55 import android.content.pm.ICrossProfileApps;
56 import android.content.pm.IPackageManager;
57 import android.content.pm.IShortcutService;
58 import android.content.pm.LauncherApps;
59 import android.content.pm.PackageManager;
60 import android.content.pm.ShortcutManager;
61 import android.content.res.Resources;
62 import android.content.rollback.IRollbackManager;
63 import android.content.rollback.RollbackManager;
64 import android.debug.AdbManager;
65 import android.debug.IAdbManager;
66 import android.hardware.ConsumerIrManager;
67 import android.hardware.ISerialManager;
68 import android.hardware.SensorManager;
69 import android.hardware.SensorPrivacyManager;
70 import android.hardware.SerialManager;
71 import android.hardware.SystemSensorManager;
72 import android.hardware.biometrics.BiometricManager;
73 import android.hardware.biometrics.IBiometricService;
74 import android.hardware.camera2.CameraManager;
75 import android.hardware.display.ColorDisplayManager;
76 import android.hardware.display.DisplayManager;
77 import android.hardware.face.FaceManager;
78 import android.hardware.face.IFaceService;
79 import android.hardware.fingerprint.FingerprintManager;
80 import android.hardware.fingerprint.IFingerprintService;
81 import android.hardware.hdmi.HdmiControlManager;
82 import android.hardware.hdmi.IHdmiControlService;
83 import android.hardware.input.InputManager;
84 import android.hardware.iris.IIrisService;
85 import android.hardware.iris.IrisManager;
86 import android.hardware.lights.LightsManager;
87 import android.hardware.location.ContextHubManager;
88 import android.hardware.radio.RadioManager;
89 import android.hardware.usb.IUsbManager;
90 import android.hardware.usb.UsbManager;
91 import android.location.CountryDetector;
92 import android.location.ICountryDetector;
93 import android.location.ILocationManager;
94 import android.location.LocationManager;
95 import android.media.AudioManager;
96 import android.media.MediaRouter;
97 import android.media.midi.IMidiManager;
98 import android.media.midi.MidiManager;
99 import android.media.projection.MediaProjectionManager;
100 import android.media.session.MediaSessionManager;
101 import android.media.soundtrigger.SoundTriggerManager;
102 import android.media.tv.ITvInputManager;
103 import android.media.tv.TvInputManager;
104 import android.net.ConnectivityDiagnosticsManager;
105 import android.net.ConnectivityManager;
106 import android.net.ConnectivityThread;
107 import android.net.EthernetManager;
108 import android.net.IConnectivityManager;
109 import android.net.IEthernetManager;
110 import android.net.IIpSecService;
111 import android.net.INetworkPolicyManager;
112 import android.net.ITestNetworkManager;
113 import android.net.IpSecManager;
114 import android.net.NetworkPolicyManager;
115 import android.net.NetworkScoreManager;
116 import android.net.NetworkWatchlistManager;
117 import android.net.TestNetworkManager;
118 import android.net.TetheringManager;
119 import android.net.VpnManager;
120 import android.net.lowpan.ILowpanManager;
121 import android.net.lowpan.LowpanManager;
122 import android.net.nsd.INsdManager;
123 import android.net.nsd.NsdManager;
124 import android.net.wifi.IWifiManager;
125 import android.net.wifi.IWifiScanner;
126 import android.net.wifi.RttManager;
127 import android.net.wifi.WifiManager;
128 import android.net.wifi.WifiScanner;
129 import android.net.wifi.aware.IWifiAwareManager;
130 import android.net.wifi.aware.WifiAwareManager;
131 import android.net.wifi.p2p.IWifiP2pManager;
132 import android.net.wifi.p2p.WifiP2pManager;
133 import android.net.wifi.rtt.IWifiRttManager;
134 import android.net.wifi.rtt.WifiRttManager;
135 import android.nfc.NfcManager;
136 import android.os.BatteryManager;
137 import android.os.BatteryStats;
138 import android.os.BugreportManager;
139 import android.os.Build;
140 import android.os.DeviceIdleManager;
141 import android.os.DropBoxManager;
142 import android.os.HardwarePropertiesManager;
143 import android.os.IBatteryPropertiesRegistrar;
144 import android.os.IBinder;
145 import android.os.IDeviceIdleController;
146 import android.os.IDumpstate;
147 import android.os.IHardwarePropertiesManager;
148 import android.os.IPowerManager;
149 import android.os.IRecoverySystem;
150 import android.os.ISystemUpdateManager;
151 import android.os.IUserManager;
152 import android.os.IncidentManager;
153 import android.os.PowerManager;
154 import android.os.RecoverySystem;
155 import android.os.RemoteException;
156 import android.os.ServiceManager;
157 import android.os.ServiceManager.ServiceNotFoundException;
158 import android.os.SystemUpdateManager;
159 import android.os.SystemVibrator;
160 import android.os.UserHandle;
161 import android.os.UserManager;
162 import android.os.Vibrator;
163 import android.os.health.SystemHealthManager;
164 import android.os.image.DynamicSystemManager;
165 import android.os.image.IDynamicSystemService;
166 import android.os.storage.StorageManager;
167 import android.permission.PermissionControllerManager;
168 import android.permission.PermissionManager;
169 import android.print.IPrintManager;
170 import android.print.PrintManager;
171 import android.service.oemlock.IOemLockService;
172 import android.service.oemlock.OemLockManager;
173 import android.service.persistentdata.IPersistentDataBlockService;
174 import android.service.persistentdata.PersistentDataBlockManager;
175 import android.service.vr.IVrManager;
176 import android.telecom.TelecomManager;
177 import android.telephony.CarrierConfigManager;
178 import android.telephony.MmsManager;
179 import android.telephony.SubscriptionManager;
180 import android.telephony.TelephonyManager;
181 import android.telephony.TelephonyRegistryManager;
182 import android.telephony.euicc.EuiccCardManager;
183 import android.telephony.euicc.EuiccManager;
184 import android.util.ArrayMap;
185 import android.util.Log;
186 import android.view.ContextThemeWrapper;
187 import android.view.LayoutInflater;
188 import android.view.WindowManager;
189 import android.view.WindowManagerImpl;
190 import android.view.accessibility.AccessibilityManager;
191 import android.view.accessibility.CaptioningManager;
192 import android.view.autofill.AutofillManager;
193 import android.view.autofill.IAutoFillManager;
194 import android.view.contentcapture.ContentCaptureManager;
195 import android.view.contentcapture.IContentCaptureManager;
196 import android.view.inputmethod.InputMethodManager;
197 import android.view.textclassifier.TextClassificationManager;
198 import android.view.textservice.TextServicesManager;
199 
200 import com.android.internal.app.IAppOpsService;
201 import com.android.internal.app.IBatteryStats;
202 import com.android.internal.app.ISoundTriggerService;
203 import com.android.internal.appwidget.IAppWidgetService;
204 import com.android.internal.net.INetworkWatchlistManager;
205 import com.android.internal.os.IDropBoxManagerService;
206 import com.android.internal.policy.PhoneLayoutInflater;
207 
208 import java.util.Map;
209 
210 /**
211  * Manages all of the system services that can be returned by {@link Context#getSystemService}.
212  * Used by {@link ContextImpl}.
213  */
214 final class SystemServiceRegistry {
215     private static final String TAG = "SystemServiceRegistry";
216 
217     // Service registry information.
218     // This information is never changed once static initialization has completed.
219     private static final Map<Class<?>, String> SYSTEM_SERVICE_NAMES =
220             new ArrayMap<Class<?>, String>();
221     private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
222             new ArrayMap<String, ServiceFetcher<?>>();
223     private static int sServiceCacheSize;
224 
225     // Not instantiable.
SystemServiceRegistry()226     private SystemServiceRegistry() { }
227 
228     static {
229         //CHECKSTYLE:OFF IndentationCheck
registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class, new CachedServiceFetcher<AccessibilityManager>() { @Override public AccessibilityManager createService(ContextImpl ctx) { return AccessibilityManager.getInstance(ctx); }})230         registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
231                 new CachedServiceFetcher<AccessibilityManager>() {
232             @Override
233             public AccessibilityManager createService(ContextImpl ctx) {
234                 return AccessibilityManager.getInstance(ctx);
235             }});
236 
registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class, new CachedServiceFetcher<CaptioningManager>() { @Override public CaptioningManager createService(ContextImpl ctx) { return new CaptioningManager(ctx); }})237         registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
238                 new CachedServiceFetcher<CaptioningManager>() {
239             @Override
240             public CaptioningManager createService(ContextImpl ctx) {
241                 return new CaptioningManager(ctx);
242             }});
243 
registerService(Context.ACCOUNT_SERVICE, AccountManager.class, new CachedServiceFetcher<AccountManager>() { @Override public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE); IAccountManager service = IAccountManager.Stub.asInterface(b); return new AccountManager(ctx, service); }})244         registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
245                 new CachedServiceFetcher<AccountManager>() {
246             @Override
247             public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
248                 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
249                 IAccountManager service = IAccountManager.Stub.asInterface(b);
250                 return new AccountManager(ctx, service);
251             }});
252 
registerService(Context.ACTIVITY_SERVICE, ActivityManager.class, new CachedServiceFetcher<ActivityManager>() { @Override public ActivityManager createService(ContextImpl ctx) { return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); }})253         registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
254                 new CachedServiceFetcher<ActivityManager>() {
255             @Override
256             public ActivityManager createService(ContextImpl ctx) {
257                 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
258             }});
259 
registerService(Context.ACTIVITY_TASK_SERVICE, ActivityTaskManager.class, new CachedServiceFetcher<ActivityTaskManager>() { @Override public ActivityTaskManager createService(ContextImpl ctx) { return new ActivityTaskManager( ctx.getOuterContext(), ctx.mMainThread.getHandler()); }})260         registerService(Context.ACTIVITY_TASK_SERVICE, ActivityTaskManager.class,
261                 new CachedServiceFetcher<ActivityTaskManager>() {
262             @Override
263             public ActivityTaskManager createService(ContextImpl ctx) {
264                 return new ActivityTaskManager(
265                         ctx.getOuterContext(), ctx.mMainThread.getHandler());
266             }});
267 
registerService(Context.URI_GRANTS_SERVICE, UriGrantsManager.class, new CachedServiceFetcher<UriGrantsManager>() { @Override public UriGrantsManager createService(ContextImpl ctx) { return new UriGrantsManager( ctx.getOuterContext(), ctx.mMainThread.getHandler()); }})268         registerService(Context.URI_GRANTS_SERVICE, UriGrantsManager.class,
269                 new CachedServiceFetcher<UriGrantsManager>() {
270             @Override
271             public UriGrantsManager createService(ContextImpl ctx) {
272                 return new UriGrantsManager(
273                         ctx.getOuterContext(), ctx.mMainThread.getHandler());
274             }});
275 
registerService(Context.ALARM_SERVICE, AlarmManager.class, new CachedServiceFetcher<AlarmManager>() { @Override public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE); IAlarmManager service = IAlarmManager.Stub.asInterface(b); return new AlarmManager(service, ctx); }})276         registerService(Context.ALARM_SERVICE, AlarmManager.class,
277                 new CachedServiceFetcher<AlarmManager>() {
278             @Override
279             public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
280                 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
281                 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
282                 return new AlarmManager(service, ctx);
283             }});
284 
registerService(Context.AUDIO_SERVICE, AudioManager.class, new CachedServiceFetcher<AudioManager>() { @Override public AudioManager createService(ContextImpl ctx) { return new AudioManager(ctx); }})285         registerService(Context.AUDIO_SERVICE, AudioManager.class,
286                 new CachedServiceFetcher<AudioManager>() {
287             @Override
288             public AudioManager createService(ContextImpl ctx) {
289                 return new AudioManager(ctx);
290             }});
291 
registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class, new CachedServiceFetcher<MediaRouter>() { @Override public MediaRouter createService(ContextImpl ctx) { return new MediaRouter(ctx); }})292         registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
293                 new CachedServiceFetcher<MediaRouter>() {
294             @Override
295             public MediaRouter createService(ContextImpl ctx) {
296                 return new MediaRouter(ctx);
297             }});
298 
registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class, new CachedServiceFetcher<BluetoothManager>() { @Override public BluetoothManager createService(ContextImpl ctx) { return new BluetoothManager(ctx); }})299         registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
300                 new CachedServiceFetcher<BluetoothManager>() {
301             @Override
302             public BluetoothManager createService(ContextImpl ctx) {
303                 return new BluetoothManager(ctx);
304             }});
305 
registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class, new StaticServiceFetcher<HdmiControlManager>() { @Override public HdmiControlManager createService() throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE); return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b)); }})306         registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
307                 new StaticServiceFetcher<HdmiControlManager>() {
308             @Override
309             public HdmiControlManager createService() throws ServiceNotFoundException {
310                 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
311                 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
312             }});
313 
registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class, new CachedServiceFetcher<TextClassificationManager>() { @Override public TextClassificationManager createService(ContextImpl ctx) { return new TextClassificationManager(ctx); }})314         registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
315                 new CachedServiceFetcher<TextClassificationManager>() {
316             @Override
317             public TextClassificationManager createService(ContextImpl ctx) {
318                 return new TextClassificationManager(ctx);
319             }});
320 
registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class, new CachedServiceFetcher<ClipboardManager>() { @Override public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new ClipboardManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); }})321         registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
322                 new CachedServiceFetcher<ClipboardManager>() {
323             @Override
324             public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
325                 return new ClipboardManager(ctx.getOuterContext(),
326                         ctx.mMainThread.getHandler());
327             }});
328 
329         // The clipboard service moved to a new package.  If someone asks for the old
330         // interface by class then we want to redirect over to the new interface instead
331         // (which extends it).
SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE)332         SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
333 
registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class, new StaticApplicationContextServiceFetcher<ConnectivityManager>() { @Override public ConnectivityManager createService(Context context) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE); IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); return new ConnectivityManager(context, service); }})334         registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
335                 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
336             @Override
337             public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
338                 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
339                 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
340                 return new ConnectivityManager(context, service);
341             }});
342 
registerService(Context.NETD_SERVICE, IBinder.class, new StaticServiceFetcher<IBinder>() { @Override public IBinder createService() throws ServiceNotFoundException { return ServiceManager.getServiceOrThrow(Context.NETD_SERVICE); } })343         registerService(Context.NETD_SERVICE, IBinder.class, new StaticServiceFetcher<IBinder>() {
344             @Override
345             public IBinder createService() throws ServiceNotFoundException {
346                 return ServiceManager.getServiceOrThrow(Context.NETD_SERVICE);
347             }
348         });
349 
registerService(Context.TETHERING_SERVICE, TetheringManager.class, new CachedServiceFetcher<TetheringManager>() { @Override public TetheringManager createService(ContextImpl ctx) { return new TetheringManager( ctx, () -> ServiceManager.getService(Context.TETHERING_SERVICE)); }})350         registerService(Context.TETHERING_SERVICE, TetheringManager.class,
351                 new CachedServiceFetcher<TetheringManager>() {
352             @Override
353             public TetheringManager createService(ContextImpl ctx) {
354                 return new TetheringManager(
355                         ctx, () -> ServiceManager.getService(Context.TETHERING_SERVICE));
356             }});
357 
358 
registerService(Context.IPSEC_SERVICE, IpSecManager.class, new CachedServiceFetcher<IpSecManager>() { @Override public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE); IIpSecService service = IIpSecService.Stub.asInterface(b); return new IpSecManager(ctx, service); }})359         registerService(Context.IPSEC_SERVICE, IpSecManager.class,
360                 new CachedServiceFetcher<IpSecManager>() {
361             @Override
362             public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException {
363                 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
364                 IIpSecService service = IIpSecService.Stub.asInterface(b);
365                 return new IpSecManager(ctx, service);
366             }});
367 
registerService(Context.VPN_MANAGEMENT_SERVICE, VpnManager.class, new CachedServiceFetcher<VpnManager>() { @Override public VpnManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); return new VpnManager(ctx, service); }})368         registerService(Context.VPN_MANAGEMENT_SERVICE, VpnManager.class,
369                 new CachedServiceFetcher<VpnManager>() {
370             @Override
371             public VpnManager createService(ContextImpl ctx) throws ServiceNotFoundException {
372                 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
373                 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
374                 return new VpnManager(ctx, service);
375             }});
376 
registerService(Context.CONNECTIVITY_DIAGNOSTICS_SERVICE, ConnectivityDiagnosticsManager.class, new CachedServiceFetcher<ConnectivityDiagnosticsManager>() { @Override public ConnectivityDiagnosticsManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE); IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); return new ConnectivityDiagnosticsManager(ctx, service); }})377         registerService(Context.CONNECTIVITY_DIAGNOSTICS_SERVICE,
378                 ConnectivityDiagnosticsManager.class,
379                 new CachedServiceFetcher<ConnectivityDiagnosticsManager>() {
380             @Override
381             public ConnectivityDiagnosticsManager createService(ContextImpl ctx)
382                     throws ServiceNotFoundException {
383                 // ConnectivityDiagnosticsManager is backed by ConnectivityService
384                 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
385                 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
386                 return new ConnectivityDiagnosticsManager(ctx, service);
387             }});
388 
registerService( Context.TEST_NETWORK_SERVICE, TestNetworkManager.class, new StaticApplicationContextServiceFetcher<TestNetworkManager>() { @Override public TestNetworkManager createService(Context context) throws ServiceNotFoundException { IBinder csBinder = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE); IConnectivityManager csMgr = IConnectivityManager.Stub.asInterface(csBinder); final IBinder tnBinder; try { tnBinder = csMgr.startOrGetTestNetworkService(); } catch (RemoteException e) { throw new ServiceNotFoundException(Context.TEST_NETWORK_SERVICE); } ITestNetworkManager tnMgr = ITestNetworkManager.Stub.asInterface(tnBinder); return new TestNetworkManager(tnMgr); } })389         registerService(
390                 Context.TEST_NETWORK_SERVICE,
391                 TestNetworkManager.class,
392                 new StaticApplicationContextServiceFetcher<TestNetworkManager>() {
393                     @Override
394                     public TestNetworkManager createService(Context context)
395                             throws ServiceNotFoundException {
396                         IBinder csBinder =
397                                 ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
398                         IConnectivityManager csMgr =
399                                 IConnectivityManager.Stub.asInterface(csBinder);
400 
401                         final IBinder tnBinder;
402                         try {
403                             tnBinder = csMgr.startOrGetTestNetworkService();
404                         } catch (RemoteException e) {
405                             throw new ServiceNotFoundException(Context.TEST_NETWORK_SERVICE);
406                         }
407                         ITestNetworkManager tnMgr = ITestNetworkManager.Stub.asInterface(tnBinder);
408                         return new TestNetworkManager(tnMgr);
409                     }
410                 });
411 
registerService(Context.COUNTRY_DETECTOR, CountryDetector.class, new StaticServiceFetcher<CountryDetector>() { @Override public CountryDetector createService() throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR); return new CountryDetector(ICountryDetector.Stub.asInterface(b)); }})412         registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
413                 new StaticServiceFetcher<CountryDetector>() {
414             @Override
415             public CountryDetector createService() throws ServiceNotFoundException {
416                 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
417                 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
418             }});
419 
registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class, new CachedServiceFetcher<DevicePolicyManager>() { @Override public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE); return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b)); }})420         registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
421                 new CachedServiceFetcher<DevicePolicyManager>() {
422             @Override
423             public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
424                 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
425                 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
426             }});
427 
registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class, new CachedServiceFetcher<DownloadManager>() { @Override public DownloadManager createService(ContextImpl ctx) { return new DownloadManager(ctx); }})428         registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
429                 new CachedServiceFetcher<DownloadManager>() {
430             @Override
431             public DownloadManager createService(ContextImpl ctx) {
432                 return new DownloadManager(ctx);
433             }});
434 
registerService(Context.BATTERY_SERVICE, BatteryManager.class, new CachedServiceFetcher<BatteryManager>() { @Override public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBatteryStats stats = IBatteryStats.Stub.asInterface( ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME)); IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub .asInterface(ServiceManager.getServiceOrThrow("batteryproperties")); return new BatteryManager(ctx, stats, registrar); }})435         registerService(Context.BATTERY_SERVICE, BatteryManager.class,
436                 new CachedServiceFetcher<BatteryManager>() {
437             @Override
438             public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
439                 IBatteryStats stats = IBatteryStats.Stub.asInterface(
440                         ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
441                 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
442                         .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
443                 return new BatteryManager(ctx, stats, registrar);
444             }});
445 
registerService(Context.NFC_SERVICE, NfcManager.class, new CachedServiceFetcher<NfcManager>() { @Override public NfcManager createService(ContextImpl ctx) { return new NfcManager(ctx); }})446         registerService(Context.NFC_SERVICE, NfcManager.class,
447                 new CachedServiceFetcher<NfcManager>() {
448             @Override
449             public NfcManager createService(ContextImpl ctx) {
450                 return new NfcManager(ctx);
451             }});
452 
registerService(Context.DROPBOX_SERVICE, DropBoxManager.class, new CachedServiceFetcher<DropBoxManager>() { @Override public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE); IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b); return new DropBoxManager(ctx, service); }})453         registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
454                 new CachedServiceFetcher<DropBoxManager>() {
455             @Override
456             public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
457                 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
458                 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
459                 return new DropBoxManager(ctx, service);
460             }});
461 
registerService(Context.INPUT_SERVICE, InputManager.class, new StaticServiceFetcher<InputManager>() { @Override public InputManager createService() { return InputManager.getInstance(); }})462         registerService(Context.INPUT_SERVICE, InputManager.class,
463                 new StaticServiceFetcher<InputManager>() {
464             @Override
465             public InputManager createService() {
466                 return InputManager.getInstance();
467             }});
468 
registerService(Context.DISPLAY_SERVICE, DisplayManager.class, new CachedServiceFetcher<DisplayManager>() { @Override public DisplayManager createService(ContextImpl ctx) { return new DisplayManager(ctx.getOuterContext()); }})469         registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
470                 new CachedServiceFetcher<DisplayManager>() {
471             @Override
472             public DisplayManager createService(ContextImpl ctx) {
473                 return new DisplayManager(ctx.getOuterContext());
474             }});
475 
registerService(Context.COLOR_DISPLAY_SERVICE, ColorDisplayManager.class, new CachedServiceFetcher<ColorDisplayManager>() { @Override public ColorDisplayManager createService(ContextImpl ctx) { return new ColorDisplayManager(); } })476         registerService(Context.COLOR_DISPLAY_SERVICE, ColorDisplayManager.class,
477                 new CachedServiceFetcher<ColorDisplayManager>() {
478                     @Override
479                     public ColorDisplayManager createService(ContextImpl ctx) {
480                         return new ColorDisplayManager();
481                     }
482                 });
483 
484         // InputMethodManager has its own cache strategy based on display id to support apps that
485         // still assume InputMethodManager is a per-process singleton and it's safe to directly
486         // access internal fields via reflection.  Hence directly use ServiceFetcher instead of
487         // StaticServiceFetcher/CachedServiceFetcher.
registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class, new ServiceFetcher<InputMethodManager>() { @Override public InputMethodManager getService(ContextImpl ctx) { return InputMethodManager.forContext(ctx.getOuterContext()); }})488         registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
489                 new ServiceFetcher<InputMethodManager>() {
490             @Override
491             public InputMethodManager getService(ContextImpl ctx) {
492                 return InputMethodManager.forContext(ctx.getOuterContext());
493             }});
494 
registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class, new CachedServiceFetcher<TextServicesManager>() { @Override public TextServicesManager createService(ContextImpl ctx) throws ServiceNotFoundException { return TextServicesManager.createInstance(ctx); }})495         registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
496                 new CachedServiceFetcher<TextServicesManager>() {
497             @Override
498             public TextServicesManager createService(ContextImpl ctx)
499                     throws ServiceNotFoundException {
500                 return TextServicesManager.createInstance(ctx);
501             }});
502 
registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class, new CachedServiceFetcher<KeyguardManager>() { @Override public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new KeyguardManager(ctx); }})503         registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
504                 new CachedServiceFetcher<KeyguardManager>() {
505             @Override
506             public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
507                 return new KeyguardManager(ctx);
508             }});
509 
registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class, new CachedServiceFetcher<LayoutInflater>() { @Override public LayoutInflater createService(ContextImpl ctx) { return new PhoneLayoutInflater(ctx.getOuterContext()); }})510         registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
511                 new CachedServiceFetcher<LayoutInflater>() {
512             @Override
513             public LayoutInflater createService(ContextImpl ctx) {
514                 return new PhoneLayoutInflater(ctx.getOuterContext());
515             }});
516 
registerService(Context.LOCATION_SERVICE, LocationManager.class, new CachedServiceFetcher<LocationManager>() { @Override public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE); return new LocationManager(ctx, ILocationManager.Stub.asInterface(b)); }})517         registerService(Context.LOCATION_SERVICE, LocationManager.class,
518                 new CachedServiceFetcher<LocationManager>() {
519             @Override
520             public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
521                 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
522                 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
523             }});
524 
registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class, new CachedServiceFetcher<NetworkPolicyManager>() { @Override public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface( ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE))); }})525         registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
526                 new CachedServiceFetcher<NetworkPolicyManager>() {
527             @Override
528             public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
529                 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
530                         ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
531             }});
532 
registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class, new CachedServiceFetcher<NotificationManager>() { @Override public NotificationManager createService(ContextImpl ctx) { final Context outerContext = ctx.getOuterContext(); return new NotificationManager( new ContextThemeWrapper(outerContext, Resources.selectSystemTheme(0, outerContext.getApplicationInfo().targetSdkVersion, com.android.internal.R.style.Theme_Dialog, com.android.internal.R.style.Theme_Holo_Dialog, com.android.internal.R.style.Theme_DeviceDefault_Dialog, com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)), ctx.mMainThread.getHandler()); }})533         registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
534                 new CachedServiceFetcher<NotificationManager>() {
535             @Override
536             public NotificationManager createService(ContextImpl ctx) {
537                 final Context outerContext = ctx.getOuterContext();
538                 return new NotificationManager(
539                     new ContextThemeWrapper(outerContext,
540                             Resources.selectSystemTheme(0,
541                                     outerContext.getApplicationInfo().targetSdkVersion,
542                                     com.android.internal.R.style.Theme_Dialog,
543                                     com.android.internal.R.style.Theme_Holo_Dialog,
544                                     com.android.internal.R.style.Theme_DeviceDefault_Dialog,
545                                     com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
546                     ctx.mMainThread.getHandler());
547             }});
548 
registerService(Context.NSD_SERVICE, NsdManager.class, new CachedServiceFetcher<NsdManager>() { @Override public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE); INsdManager service = INsdManager.Stub.asInterface(b); return new NsdManager(ctx.getOuterContext(), service); }})549         registerService(Context.NSD_SERVICE, NsdManager.class,
550                 new CachedServiceFetcher<NsdManager>() {
551             @Override
552             public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
553                 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
554                 INsdManager service = INsdManager.Stub.asInterface(b);
555                 return new NsdManager(ctx.getOuterContext(), service);
556             }});
557 
registerService(Context.POWER_SERVICE, PowerManager.class, new CachedServiceFetcher<PowerManager>() { @Override public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE); IPowerManager service = IPowerManager.Stub.asInterface(b); return new PowerManager(ctx.getOuterContext(), service, ctx.mMainThread.getHandler()); }})558         registerService(Context.POWER_SERVICE, PowerManager.class,
559                 new CachedServiceFetcher<PowerManager>() {
560             @Override
561             public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
562                 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
563                 IPowerManager service = IPowerManager.Stub.asInterface(b);
564                 return new PowerManager(ctx.getOuterContext(),
565                         service, ctx.mMainThread.getHandler());
566             }});
567 
registerService(Context.RECOVERY_SERVICE, RecoverySystem.class, new CachedServiceFetcher<RecoverySystem>() { @Override public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE); IRecoverySystem service = IRecoverySystem.Stub.asInterface(b); return new RecoverySystem(service); }})568         registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
569                 new CachedServiceFetcher<RecoverySystem>() {
570             @Override
571             public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
572                 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
573                 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
574                 return new RecoverySystem(service);
575             }});
576 
registerService(Context.SEARCH_SERVICE, SearchManager.class, new CachedServiceFetcher<SearchManager>() { @Override public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new SearchManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); }})577         registerService(Context.SEARCH_SERVICE, SearchManager.class,
578                 new CachedServiceFetcher<SearchManager>() {
579             @Override
580             public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
581                 return new SearchManager(ctx.getOuterContext(),
582                         ctx.mMainThread.getHandler());
583             }});
584 
registerService(Context.SENSOR_SERVICE, SensorManager.class, new CachedServiceFetcher<SensorManager>() { @Override public SensorManager createService(ContextImpl ctx) { return new SystemSensorManager(ctx.getOuterContext(), ctx.mMainThread.getHandler().getLooper()); }})585         registerService(Context.SENSOR_SERVICE, SensorManager.class,
586                 new CachedServiceFetcher<SensorManager>() {
587             @Override
588             public SensorManager createService(ContextImpl ctx) {
589                 return new SystemSensorManager(ctx.getOuterContext(),
590                   ctx.mMainThread.getHandler().getLooper());
591             }});
592 
registerService(Context.SENSOR_PRIVACY_SERVICE, SensorPrivacyManager.class, new CachedServiceFetcher<SensorPrivacyManager>() { @Override public SensorPrivacyManager createService(ContextImpl ctx) { return SensorPrivacyManager.getInstance(ctx); }})593         registerService(Context.SENSOR_PRIVACY_SERVICE, SensorPrivacyManager.class,
594                 new CachedServiceFetcher<SensorPrivacyManager>() {
595                     @Override
596                     public SensorPrivacyManager createService(ContextImpl ctx) {
597                         return SensorPrivacyManager.getInstance(ctx);
598                     }});
599 
registerService(Context.STATS_MANAGER, StatsManager.class, new CachedServiceFetcher<StatsManager>() { @Override public StatsManager createService(ContextImpl ctx) { return new StatsManager(ctx.getOuterContext()); }})600         registerService(Context.STATS_MANAGER, StatsManager.class,
601                 new CachedServiceFetcher<StatsManager>() {
602             @Override
603             public StatsManager createService(ContextImpl ctx) {
604                 return new StatsManager(ctx.getOuterContext());
605             }});
606 
registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class, new CachedServiceFetcher<StatusBarManager>() { @Override public StatusBarManager createService(ContextImpl ctx) { return new StatusBarManager(ctx.getOuterContext()); }})607         registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
608                 new CachedServiceFetcher<StatusBarManager>() {
609             @Override
610             public StatusBarManager createService(ContextImpl ctx) {
611                 return new StatusBarManager(ctx.getOuterContext());
612             }});
613 
registerService(Context.STORAGE_SERVICE, StorageManager.class, new CachedServiceFetcher<StorageManager>() { @Override public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper()); }})614         registerService(Context.STORAGE_SERVICE, StorageManager.class,
615                 new CachedServiceFetcher<StorageManager>() {
616             @Override
617             public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
618                 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
619             }});
620 
registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class, new CachedServiceFetcher<StorageStatsManager>() { @Override public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException { IStorageStatsManager service = IStorageStatsManager.Stub.asInterface( ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE)); return new StorageStatsManager(ctx, service); }})621         registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
622                 new CachedServiceFetcher<StorageStatsManager>() {
623             @Override
624             public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
625                 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
626                         ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
627                 return new StorageStatsManager(ctx, service);
628             }});
629 
registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class, new CachedServiceFetcher<SystemUpdateManager>() { @Override public SystemUpdateManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow( Context.SYSTEM_UPDATE_SERVICE); ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b); return new SystemUpdateManager(service); }})630         registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class,
631                 new CachedServiceFetcher<SystemUpdateManager>() {
632                     @Override
633                     public SystemUpdateManager createService(ContextImpl ctx)
634                             throws ServiceNotFoundException {
635                         IBinder b = ServiceManager.getServiceOrThrow(
636                                 Context.SYSTEM_UPDATE_SERVICE);
637                         ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b);
638                         return new SystemUpdateManager(service);
639                     }});
640 
registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class, new CachedServiceFetcher<TelephonyManager>() { @Override public TelephonyManager createService(ContextImpl ctx) { return new TelephonyManager(ctx.getOuterContext()); }})641         registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
642                 new CachedServiceFetcher<TelephonyManager>() {
643             @Override
644             public TelephonyManager createService(ContextImpl ctx) {
645                 return new TelephonyManager(ctx.getOuterContext());
646             }});
647 
registerService(Context.TELEPHONY_REGISTRY_SERVICE, TelephonyRegistryManager.class, new CachedServiceFetcher<TelephonyRegistryManager>() { @Override public TelephonyRegistryManager createService(ContextImpl ctx) { return new TelephonyRegistryManager(ctx); }})648         registerService(Context.TELEPHONY_REGISTRY_SERVICE, TelephonyRegistryManager.class,
649             new CachedServiceFetcher<TelephonyRegistryManager>() {
650                 @Override
651                 public TelephonyRegistryManager createService(ContextImpl ctx) {
652                     return new TelephonyRegistryManager(ctx);
653                 }});
654 
registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class, new CachedServiceFetcher<SubscriptionManager>() { @Override public SubscriptionManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new SubscriptionManager(ctx.getOuterContext()); }})655         registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
656                 new CachedServiceFetcher<SubscriptionManager>() {
657             @Override
658             public SubscriptionManager createService(ContextImpl ctx) throws ServiceNotFoundException {
659                 return new SubscriptionManager(ctx.getOuterContext());
660             }});
661 
registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class, new CachedServiceFetcher<CarrierConfigManager>() { @Override public CarrierConfigManager createService(ContextImpl ctx) { return new CarrierConfigManager(ctx.getOuterContext()); }})662         registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
663                 new CachedServiceFetcher<CarrierConfigManager>() {
664             @Override
665             public CarrierConfigManager createService(ContextImpl ctx) {
666                 return new CarrierConfigManager(ctx.getOuterContext());
667             }});
668 
registerService(Context.TELECOM_SERVICE, TelecomManager.class, new CachedServiceFetcher<TelecomManager>() { @Override public TelecomManager createService(ContextImpl ctx) { return new TelecomManager(ctx.getOuterContext()); }})669         registerService(Context.TELECOM_SERVICE, TelecomManager.class,
670                 new CachedServiceFetcher<TelecomManager>() {
671             @Override
672             public TelecomManager createService(ContextImpl ctx) {
673                 return new TelecomManager(ctx.getOuterContext());
674             }});
675 
registerService(Context.EUICC_SERVICE, EuiccManager.class, new CachedServiceFetcher<EuiccManager>() { @Override public EuiccManager createService(ContextImpl ctx) { return new EuiccManager(ctx.getOuterContext()); }})676         registerService(Context.EUICC_SERVICE, EuiccManager.class,
677                 new CachedServiceFetcher<EuiccManager>() {
678             @Override
679             public EuiccManager createService(ContextImpl ctx) {
680                 return new EuiccManager(ctx.getOuterContext());
681             }});
682 
registerService(Context.EUICC_CARD_SERVICE, EuiccCardManager.class, new CachedServiceFetcher<EuiccCardManager>() { @Override public EuiccCardManager createService(ContextImpl ctx) { return new EuiccCardManager(ctx.getOuterContext()); }})683         registerService(Context.EUICC_CARD_SERVICE, EuiccCardManager.class,
684                 new CachedServiceFetcher<EuiccCardManager>() {
685                     @Override
686                     public EuiccCardManager createService(ContextImpl ctx) {
687                         return new EuiccCardManager(ctx.getOuterContext());
688                     }});
689 
registerService(Context.MMS_SERVICE, MmsManager.class, new CachedServiceFetcher<MmsManager>() { @Override public MmsManager createService(ContextImpl ctx) { return new MmsManager(ctx.getOuterContext()); }})690         registerService(Context.MMS_SERVICE, MmsManager.class,
691                 new CachedServiceFetcher<MmsManager>() {
692                     @Override
693                     public MmsManager createService(ContextImpl ctx) {
694                         return new MmsManager(ctx.getOuterContext());
695                     }});
696 
registerService(Context.UI_MODE_SERVICE, UiModeManager.class, new CachedServiceFetcher<UiModeManager>() { @Override public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new UiModeManager(ctx.getOuterContext()); }})697         registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
698                 new CachedServiceFetcher<UiModeManager>() {
699             @Override
700             public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
701                 return new UiModeManager(ctx.getOuterContext());
702             }});
703 
registerService(Context.USB_SERVICE, UsbManager.class, new CachedServiceFetcher<UsbManager>() { @Override public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE); return new UsbManager(ctx, IUsbManager.Stub.asInterface(b)); }})704         registerService(Context.USB_SERVICE, UsbManager.class,
705                 new CachedServiceFetcher<UsbManager>() {
706             @Override
707             public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
708                 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
709                 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
710             }});
711 
registerService(Context.ADB_SERVICE, AdbManager.class, new CachedServiceFetcher<AdbManager>() { @Override public AdbManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.ADB_SERVICE); return new AdbManager(ctx, IAdbManager.Stub.asInterface(b)); }})712         registerService(Context.ADB_SERVICE, AdbManager.class,
713                 new CachedServiceFetcher<AdbManager>() {
714                     @Override
715                     public AdbManager createService(ContextImpl ctx)
716                                 throws ServiceNotFoundException {
717                         IBinder b = ServiceManager.getServiceOrThrow(Context.ADB_SERVICE);
718                         return new AdbManager(ctx, IAdbManager.Stub.asInterface(b));
719                     }});
720 
registerService(Context.SERIAL_SERVICE, SerialManager.class, new CachedServiceFetcher<SerialManager>() { @Override public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE); return new SerialManager(ctx, ISerialManager.Stub.asInterface(b)); }})721         registerService(Context.SERIAL_SERVICE, SerialManager.class,
722                 new CachedServiceFetcher<SerialManager>() {
723             @Override
724             public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
725                 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
726                 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
727             }});
728 
registerService(Context.VIBRATOR_SERVICE, Vibrator.class, new CachedServiceFetcher<Vibrator>() { @Override public Vibrator createService(ContextImpl ctx) { return new SystemVibrator(ctx); }})729         registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
730                 new CachedServiceFetcher<Vibrator>() {
731             @Override
732             public Vibrator createService(ContextImpl ctx) {
733                 return new SystemVibrator(ctx);
734             }});
735 
registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class, new CachedServiceFetcher<WallpaperManager>() { @Override public WallpaperManager createService(ContextImpl ctx) throws ServiceNotFoundException { final IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE); if (b == null) { final boolean enabled = Resources.getSystem() .getBoolean(com.android.internal.R.bool.config_enableWallpaperService); if (!enabled) { return DisabledWallpaperManager.getInstance(); } if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) { throw new ServiceNotFoundException(Context.WALLPAPER_SERVICE); } } IWallpaperManager service = IWallpaperManager.Stub.asInterface(b); return new WallpaperManager(service, ctx.getOuterContext(), ctx.mMainThread.getHandler()); }})736         registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
737                 new CachedServiceFetcher<WallpaperManager>() {
738             @Override
739             public WallpaperManager createService(ContextImpl ctx)
740                     throws ServiceNotFoundException {
741                 final IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
742                 if (b == null) {
743                     // There are 2 reason service can be null:
744                     // 1.Device doesn't support it - that's fine
745                     // 2.App is running on instant mode - should fail
746                     final boolean enabled = Resources.getSystem()
747                             .getBoolean(com.android.internal.R.bool.config_enableWallpaperService);
748                     if (!enabled) {
749                         // Life moves on...
750                         return DisabledWallpaperManager.getInstance();
751                     }
752                     if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
753                         // Instant app
754                         throw new ServiceNotFoundException(Context.WALLPAPER_SERVICE);
755                     }
756                     // Bad state - WallpaperManager methods will throw exception
757                 }
758                 IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
759                 return new WallpaperManager(service, ctx.getOuterContext(),
760                         ctx.mMainThread.getHandler());
761             }});
762 
registerService(Context.LOWPAN_SERVICE, LowpanManager.class, new CachedServiceFetcher<LowpanManager>() { @Override public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE); ILowpanManager service = ILowpanManager.Stub.asInterface(b); return new LowpanManager(ctx.getOuterContext(), service, ConnectivityThread.getInstanceLooper()); }})763         registerService(Context.LOWPAN_SERVICE, LowpanManager.class,
764                 new CachedServiceFetcher<LowpanManager>() {
765             @Override
766             public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
767                 IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE);
768                 ILowpanManager service = ILowpanManager.Stub.asInterface(b);
769                 return new LowpanManager(ctx.getOuterContext(), service,
770                         ConnectivityThread.getInstanceLooper());
771             }});
772 
registerService(Context.WIFI_SERVICE, WifiManager.class, new CachedServiceFetcher<WifiManager>() { @Override public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE); IWifiManager service = IWifiManager.Stub.asInterface(b); return new WifiManager(ctx.getOuterContext(), service, ConnectivityThread.getInstanceLooper()); }})773         registerService(Context.WIFI_SERVICE, WifiManager.class,
774                 new CachedServiceFetcher<WifiManager>() {
775             @Override
776             public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
777                 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE);
778                 IWifiManager service = IWifiManager.Stub.asInterface(b);
779                 return new WifiManager(ctx.getOuterContext(), service,
780                         ConnectivityThread.getInstanceLooper());
781             }});
782 
registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class, new StaticServiceFetcher<WifiP2pManager>() { @Override public WifiP2pManager createService() throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE); IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b); return new WifiP2pManager(service); }})783         registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
784                 new StaticServiceFetcher<WifiP2pManager>() {
785             @Override
786             public WifiP2pManager createService() throws ServiceNotFoundException {
787                 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE);
788                 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
789                 return new WifiP2pManager(service);
790             }});
791 
registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class, new CachedServiceFetcher<WifiAwareManager>() { @Override public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE); IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b); if (service == null) { return null; } return new WifiAwareManager(ctx.getOuterContext(), service); }})792         registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class,
793                 new CachedServiceFetcher<WifiAwareManager>() {
794             @Override
795             public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException {
796                 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE);
797                 IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b);
798                 if (service == null) {
799                     return null;
800                 }
801                 return new WifiAwareManager(ctx.getOuterContext(), service);
802             }});
803 
registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class, new CachedServiceFetcher<WifiScanner>() { @Override public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE); IWifiScanner service = IWifiScanner.Stub.asInterface(b); return new WifiScanner(ctx.getOuterContext(), service, ConnectivityThread.getInstanceLooper()); }})804         registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
805                 new CachedServiceFetcher<WifiScanner>() {
806             @Override
807             public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException {
808                 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE);
809                 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
810                 return new WifiScanner(ctx.getOuterContext(), service,
811                         ConnectivityThread.getInstanceLooper());
812             }});
813 
registerService(Context.WIFI_RTT_SERVICE, RttManager.class, new CachedServiceFetcher<RttManager>() { @Override public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_RANGING_SERVICE); IWifiRttManager service = IWifiRttManager.Stub.asInterface(b); return new RttManager(ctx.getOuterContext(), new WifiRttManager(ctx.getOuterContext(), service)); }})814         registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
815                 new CachedServiceFetcher<RttManager>() {
816                 @Override
817                 public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException {
818                     IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_RANGING_SERVICE);
819                     IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
820                     return new RttManager(ctx.getOuterContext(),
821                             new WifiRttManager(ctx.getOuterContext(), service));
822                 }});
823 
registerService(Context.WIFI_RTT_RANGING_SERVICE, WifiRttManager.class, new CachedServiceFetcher<WifiRttManager>() { @Override public WifiRttManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow( Context.WIFI_RTT_RANGING_SERVICE); IWifiRttManager service = IWifiRttManager.Stub.asInterface(b); return new WifiRttManager(ctx.getOuterContext(), service); }})824         registerService(Context.WIFI_RTT_RANGING_SERVICE, WifiRttManager.class,
825                 new CachedServiceFetcher<WifiRttManager>() {
826                     @Override
827                     public WifiRttManager createService(ContextImpl ctx)
828                             throws ServiceNotFoundException {
829                         IBinder b = ServiceManager.getServiceOrThrow(
830                                 Context.WIFI_RTT_RANGING_SERVICE);
831                         IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
832                         return new WifiRttManager(ctx.getOuterContext(), service);
833                     }});
834 
registerService(Context.ETHERNET_SERVICE, EthernetManager.class, new CachedServiceFetcher<EthernetManager>() { @Override public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE); IEthernetManager service = IEthernetManager.Stub.asInterface(b); return new EthernetManager(ctx.getOuterContext(), service); }})835         registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
836                 new CachedServiceFetcher<EthernetManager>() {
837             @Override
838             public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
839                 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
840                 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
841                 return new EthernetManager(ctx.getOuterContext(), service);
842             }});
843 
registerService(Context.WINDOW_SERVICE, WindowManager.class, new CachedServiceFetcher<WindowManager>() { @Override public WindowManager createService(ContextImpl ctx) { return new WindowManagerImpl(ctx); }})844         registerService(Context.WINDOW_SERVICE, WindowManager.class,
845                 new CachedServiceFetcher<WindowManager>() {
846             @Override
847             public WindowManager createService(ContextImpl ctx) {
848                 return new WindowManagerImpl(ctx);
849             }});
850 
registerService(Context.USER_SERVICE, UserManager.class, new CachedServiceFetcher<UserManager>() { @Override public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE); IUserManager service = IUserManager.Stub.asInterface(b); return new UserManager(ctx, service); }})851         registerService(Context.USER_SERVICE, UserManager.class,
852                 new CachedServiceFetcher<UserManager>() {
853             @Override
854             public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
855                 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
856                 IUserManager service = IUserManager.Stub.asInterface(b);
857                 return new UserManager(ctx, service);
858             }});
859 
registerService(Context.APP_OPS_SERVICE, AppOpsManager.class, new CachedServiceFetcher<AppOpsManager>() { @Override public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE); IAppOpsService service = IAppOpsService.Stub.asInterface(b); return new AppOpsManager(ctx, service); }})860         registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
861                 new CachedServiceFetcher<AppOpsManager>() {
862             @Override
863             public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
864                 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
865                 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
866                 return new AppOpsManager(ctx, service);
867             }});
868 
registerService(Context.CAMERA_SERVICE, CameraManager.class, new CachedServiceFetcher<CameraManager>() { @Override public CameraManager createService(ContextImpl ctx) { return new CameraManager(ctx); }})869         registerService(Context.CAMERA_SERVICE, CameraManager.class,
870                 new CachedServiceFetcher<CameraManager>() {
871             @Override
872             public CameraManager createService(ContextImpl ctx) {
873                 return new CameraManager(ctx);
874             }});
875 
registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class, new CachedServiceFetcher<LauncherApps>() { @Override public LauncherApps createService(ContextImpl ctx) { return new LauncherApps(ctx); }})876         registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
877                 new CachedServiceFetcher<LauncherApps>() {
878             @Override
879             public LauncherApps createService(ContextImpl ctx) {
880                 return new LauncherApps(ctx);
881             }});
882 
registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class, new CachedServiceFetcher<RestrictionsManager>() { @Override public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE); IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b); return new RestrictionsManager(ctx, service); }})883         registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
884                 new CachedServiceFetcher<RestrictionsManager>() {
885             @Override
886             public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
887                 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
888                 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
889                 return new RestrictionsManager(ctx, service);
890             }});
891 
registerService(Context.PRINT_SERVICE, PrintManager.class, new CachedServiceFetcher<PrintManager>() { @Override public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException { IPrintManager service = null; if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) { service = IPrintManager.Stub.asInterface(ServiceManager .getServiceOrThrow(Context.PRINT_SERVICE)); } final int userId = ctx.getUserId(); final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid); return new PrintManager(ctx.getOuterContext(), service, userId, appId); }})892         registerService(Context.PRINT_SERVICE, PrintManager.class,
893                 new CachedServiceFetcher<PrintManager>() {
894             @Override
895             public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
896                 IPrintManager service = null;
897                 // If the feature not present, don't try to look up every time
898                 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
899                     service = IPrintManager.Stub.asInterface(ServiceManager
900                             .getServiceOrThrow(Context.PRINT_SERVICE));
901                 }
902                 final int userId = ctx.getUserId();
903                 final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid);
904                 return new PrintManager(ctx.getOuterContext(), service, userId, appId);
905             }});
906 
registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class, new CachedServiceFetcher<CompanionDeviceManager>() { @Override public CompanionDeviceManager createService(ContextImpl ctx) throws ServiceNotFoundException { ICompanionDeviceManager service = null; if (ctx.getPackageManager().hasSystemFeature( PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) { service = ICompanionDeviceManager.Stub.asInterface( ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE)); } return new CompanionDeviceManager(service, ctx.getOuterContext()); }})907         registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
908                 new CachedServiceFetcher<CompanionDeviceManager>() {
909             @Override
910             public CompanionDeviceManager createService(ContextImpl ctx)
911                     throws ServiceNotFoundException {
912                 ICompanionDeviceManager service = null;
913                 // If the feature not present, don't try to look up every time
914                 if (ctx.getPackageManager().hasSystemFeature(
915                         PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) {
916                     service = ICompanionDeviceManager.Stub.asInterface(
917                             ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE));
918                 }
919                 return new CompanionDeviceManager(service, ctx.getOuterContext());
920             }});
921 
registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class, new CachedServiceFetcher<ConsumerIrManager>() { @Override public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new ConsumerIrManager(ctx); }})922         registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
923                 new CachedServiceFetcher<ConsumerIrManager>() {
924             @Override
925             public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
926                 return new ConsumerIrManager(ctx);
927             }});
928 
registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class, new CachedServiceFetcher<MediaSessionManager>() { @Override public MediaSessionManager createService(ContextImpl ctx) { return new MediaSessionManager(ctx); }})929         registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
930                 new CachedServiceFetcher<MediaSessionManager>() {
931             @Override
932             public MediaSessionManager createService(ContextImpl ctx) {
933                 return new MediaSessionManager(ctx);
934             }});
935 
registerService(Context.TRUST_SERVICE, TrustManager.class, new StaticServiceFetcher<TrustManager>() { @Override public TrustManager createService() throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE); return new TrustManager(b); }})936         registerService(Context.TRUST_SERVICE, TrustManager.class,
937                 new StaticServiceFetcher<TrustManager>() {
938             @Override
939             public TrustManager createService() throws ServiceNotFoundException {
940                 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
941                 return new TrustManager(b);
942             }});
943 
registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class, new CachedServiceFetcher<FingerprintManager>() { @Override public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException { final IBinder binder; if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) { binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE); } else { binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE); } IFingerprintService service = IFingerprintService.Stub.asInterface(binder); return new FingerprintManager(ctx.getOuterContext(), service); }})944         registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
945                 new CachedServiceFetcher<FingerprintManager>() {
946             @Override
947             public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
948                 final IBinder binder;
949                 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
950                     binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
951                 } else {
952                     binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
953                 }
954                 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
955                 return new FingerprintManager(ctx.getOuterContext(), service);
956             }});
957 
registerService(Context.FACE_SERVICE, FaceManager.class, new CachedServiceFetcher<FaceManager>() { @Override public FaceManager createService(ContextImpl ctx) throws ServiceNotFoundException { final IBinder binder; if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) { binder = ServiceManager.getServiceOrThrow(Context.FACE_SERVICE); } else { binder = ServiceManager.getService(Context.FACE_SERVICE); } IFaceService service = IFaceService.Stub.asInterface(binder); return new FaceManager(ctx.getOuterContext(), service); } })958         registerService(Context.FACE_SERVICE, FaceManager.class,
959                 new CachedServiceFetcher<FaceManager>() {
960                     @Override
961                     public FaceManager createService(ContextImpl ctx)
962                             throws ServiceNotFoundException {
963                         final IBinder binder;
964                         if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
965                             binder = ServiceManager.getServiceOrThrow(Context.FACE_SERVICE);
966                         } else {
967                             binder = ServiceManager.getService(Context.FACE_SERVICE);
968                         }
969                         IFaceService service = IFaceService.Stub.asInterface(binder);
970                         return new FaceManager(ctx.getOuterContext(), service);
971                     }
972                 });
973 
registerService(Context.IRIS_SERVICE, IrisManager.class, new CachedServiceFetcher<IrisManager>() { @Override public IrisManager createService(ContextImpl ctx) throws ServiceNotFoundException { final IBinder binder = ServiceManager.getServiceOrThrow(Context.IRIS_SERVICE); IIrisService service = IIrisService.Stub.asInterface(binder); return new IrisManager(ctx.getOuterContext(), service); } })974         registerService(Context.IRIS_SERVICE, IrisManager.class,
975                 new CachedServiceFetcher<IrisManager>() {
976                     @Override
977                     public IrisManager createService(ContextImpl ctx)
978                         throws ServiceNotFoundException {
979                         final IBinder binder =
980                                 ServiceManager.getServiceOrThrow(Context.IRIS_SERVICE);
981                         IIrisService service = IIrisService.Stub.asInterface(binder);
982                         return new IrisManager(ctx.getOuterContext(), service);
983                     }
984                 });
985 
registerService(Context.BIOMETRIC_SERVICE, BiometricManager.class, new CachedServiceFetcher<BiometricManager>() { @Override public BiometricManager createService(ContextImpl ctx) throws ServiceNotFoundException { if (BiometricManager.hasBiometrics(ctx)) { final IBinder binder = ServiceManager.getServiceOrThrow(Context.BIOMETRIC_SERVICE); final IBiometricService service = IBiometricService.Stub.asInterface(binder); return new BiometricManager(ctx.getOuterContext(), service); } else { return new BiometricManager(ctx.getOuterContext(), null); } } })986         registerService(Context.BIOMETRIC_SERVICE, BiometricManager.class,
987                 new CachedServiceFetcher<BiometricManager>() {
988                     @Override
989                     public BiometricManager createService(ContextImpl ctx)
990                             throws ServiceNotFoundException {
991                         if (BiometricManager.hasBiometrics(ctx)) {
992                             final IBinder binder =
993                                     ServiceManager.getServiceOrThrow(Context.BIOMETRIC_SERVICE);
994                             final IBiometricService service =
995                                     IBiometricService.Stub.asInterface(binder);
996                             return new BiometricManager(ctx.getOuterContext(), service);
997                         } else {
998                             // Allow access to the manager when service is null. This saves memory
999                             // on devices without biometric hardware.
1000                             return new BiometricManager(ctx.getOuterContext(), null);
1001                         }
1002                     }
1003                 });
1004 
registerService(Context.TV_INPUT_SERVICE, TvInputManager.class, new CachedServiceFetcher<TvInputManager>() { @Override public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE); ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder); return new TvInputManager(service, ctx.getUserId()); }})1005         registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
1006                 new CachedServiceFetcher<TvInputManager>() {
1007             @Override
1008             public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1009                 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
1010                 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
1011                 return new TvInputManager(service, ctx.getUserId());
1012             }});
1013 
registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class, new CachedServiceFetcher<NetworkScoreManager>() { @Override public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new NetworkScoreManager(ctx); }})1014         registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
1015                 new CachedServiceFetcher<NetworkScoreManager>() {
1016             @Override
1017             public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1018                 return new NetworkScoreManager(ctx);
1019             }});
1020 
registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class, new CachedServiceFetcher<UsageStatsManager>() { @Override public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE); IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder); return new UsageStatsManager(ctx.getOuterContext(), service); }})1021         registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
1022                 new CachedServiceFetcher<UsageStatsManager>() {
1023             @Override
1024             public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1025                 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
1026                 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
1027                 return new UsageStatsManager(ctx.getOuterContext(), service);
1028             }});
1029 
registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class, new CachedServiceFetcher<NetworkStatsManager>() { @Override public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new NetworkStatsManager(ctx.getOuterContext()); }})1030         registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
1031                 new CachedServiceFetcher<NetworkStatsManager>() {
1032             @Override
1033             public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1034                 return new NetworkStatsManager(ctx.getOuterContext());
1035             }});
1036 
registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class, new StaticServiceFetcher<JobScheduler>() { @Override public JobScheduler createService() throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE); return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b)); }})1037         registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
1038                 new StaticServiceFetcher<JobScheduler>() {
1039             @Override
1040             public JobScheduler createService() throws ServiceNotFoundException {
1041                 IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE);
1042                 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
1043             }});
1044 
registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class, new StaticServiceFetcher<PersistentDataBlockManager>() { @Override public PersistentDataBlockManager createService() throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE); IPersistentDataBlockService persistentDataBlockService = IPersistentDataBlockService.Stub.asInterface(b); if (persistentDataBlockService != null) { return new PersistentDataBlockManager(persistentDataBlockService); } else { return null; } }})1045         registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
1046                 new StaticServiceFetcher<PersistentDataBlockManager>() {
1047             @Override
1048             public PersistentDataBlockManager createService() throws ServiceNotFoundException {
1049                 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
1050                 IPersistentDataBlockService persistentDataBlockService =
1051                         IPersistentDataBlockService.Stub.asInterface(b);
1052                 if (persistentDataBlockService != null) {
1053                     return new PersistentDataBlockManager(persistentDataBlockService);
1054                 } else {
1055                     // not supported
1056                     return null;
1057                 }
1058             }});
1059 
registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class, new StaticServiceFetcher<OemLockManager>() { @Override public OemLockManager createService() throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE); IOemLockService oemLockService = IOemLockService.Stub.asInterface(b); if (oemLockService != null) { return new OemLockManager(oemLockService); } else { return null; } }})1060         registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
1061                 new StaticServiceFetcher<OemLockManager>() {
1062             @Override
1063             public OemLockManager createService() throws ServiceNotFoundException {
1064                 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
1065                 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
1066                 if (oemLockService != null) {
1067                     return new OemLockManager(oemLockService);
1068                 } else {
1069                     // not supported
1070                     return null;
1071                 }
1072             }});
1073 
registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class, new CachedServiceFetcher<MediaProjectionManager>() { @Override public MediaProjectionManager createService(ContextImpl ctx) { return new MediaProjectionManager(ctx); }})1074         registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
1075                 new CachedServiceFetcher<MediaProjectionManager>() {
1076             @Override
1077             public MediaProjectionManager createService(ContextImpl ctx) {
1078                 return new MediaProjectionManager(ctx);
1079             }});
1080 
registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class, new CachedServiceFetcher<AppWidgetManager>() { @Override public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE); return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b)); }})1081         registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
1082                 new CachedServiceFetcher<AppWidgetManager>() {
1083             @Override
1084             public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1085                 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
1086                 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
1087             }});
1088 
registerService(Context.MIDI_SERVICE, MidiManager.class, new CachedServiceFetcher<MidiManager>() { @Override public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE); return new MidiManager(IMidiManager.Stub.asInterface(b)); }})1089         registerService(Context.MIDI_SERVICE, MidiManager.class,
1090                 new CachedServiceFetcher<MidiManager>() {
1091             @Override
1092             public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1093                 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
1094                 return new MidiManager(IMidiManager.Stub.asInterface(b));
1095             }});
1096 
registerService(Context.RADIO_SERVICE, RadioManager.class, new CachedServiceFetcher<RadioManager>() { @Override public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new RadioManager(ctx); }})1097         registerService(Context.RADIO_SERVICE, RadioManager.class,
1098                 new CachedServiceFetcher<RadioManager>() {
1099             @Override
1100             public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1101                 return new RadioManager(ctx);
1102             }});
1103 
registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class, new CachedServiceFetcher<HardwarePropertiesManager>() { @Override public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE); IHardwarePropertiesManager service = IHardwarePropertiesManager.Stub.asInterface(b); return new HardwarePropertiesManager(ctx, service); }})1104         registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
1105                 new CachedServiceFetcher<HardwarePropertiesManager>() {
1106             @Override
1107             public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1108                 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
1109                 IHardwarePropertiesManager service =
1110                         IHardwarePropertiesManager.Stub.asInterface(b);
1111                 return new HardwarePropertiesManager(ctx, service);
1112             }});
1113 
registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class, new CachedServiceFetcher<SoundTriggerManager>() { @Override public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE); return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b)); }})1114         registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
1115                 new CachedServiceFetcher<SoundTriggerManager>() {
1116             @Override
1117             public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1118                 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
1119                 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
1120             }});
1121 
registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class, new CachedServiceFetcher<ShortcutManager>() { @Override public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE); return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b)); }})1122         registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
1123                 new CachedServiceFetcher<ShortcutManager>() {
1124             @Override
1125             public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1126                 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
1127                 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
1128             }});
1129 
registerService(Context.OVERLAY_SERVICE, OverlayManager.class, new CachedServiceFetcher<OverlayManager>() { @Override public OverlayManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.OVERLAY_SERVICE); return new OverlayManager(ctx, IOverlayManager.Stub.asInterface(b)); }})1130         registerService(Context.OVERLAY_SERVICE, OverlayManager.class,
1131                 new CachedServiceFetcher<OverlayManager>() {
1132             @Override
1133             public OverlayManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1134                 IBinder b = ServiceManager.getServiceOrThrow(Context.OVERLAY_SERVICE);
1135                 return new OverlayManager(ctx, IOverlayManager.Stub.asInterface(b));
1136             }});
1137 
registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class, new CachedServiceFetcher<NetworkWatchlistManager>() { @Override public NetworkWatchlistManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE); return new NetworkWatchlistManager(ctx, INetworkWatchlistManager.Stub.asInterface(b)); }})1138         registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class,
1139                 new CachedServiceFetcher<NetworkWatchlistManager>() {
1140                     @Override
1141                     public NetworkWatchlistManager createService(ContextImpl ctx)
1142                             throws ServiceNotFoundException {
1143                         IBinder b =
1144                                 ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE);
1145                         return new NetworkWatchlistManager(ctx,
1146                                 INetworkWatchlistManager.Stub.asInterface(b));
1147                     }});
1148 
registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class, new CachedServiceFetcher<SystemHealthManager>() { @Override public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME); return new SystemHealthManager(IBatteryStats.Stub.asInterface(b)); }})1149         registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
1150                 new CachedServiceFetcher<SystemHealthManager>() {
1151             @Override
1152             public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1153                 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
1154                 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
1155             }});
1156 
registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class, new CachedServiceFetcher<ContextHubManager>() { @Override public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new ContextHubManager(ctx.getOuterContext(), ctx.mMainThread.getHandler().getLooper()); }})1157         registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
1158                 new CachedServiceFetcher<ContextHubManager>() {
1159             @Override
1160             public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1161                 return new ContextHubManager(ctx.getOuterContext(),
1162                   ctx.mMainThread.getHandler().getLooper());
1163             }});
1164 
registerService(Context.INCIDENT_SERVICE, IncidentManager.class, new CachedServiceFetcher<IncidentManager>() { @Override public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new IncidentManager(ctx); }})1165         registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
1166                 new CachedServiceFetcher<IncidentManager>() {
1167             @Override
1168             public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1169                 return new IncidentManager(ctx);
1170             }});
1171 
registerService(Context.BUGREPORT_SERVICE, BugreportManager.class, new CachedServiceFetcher<BugreportManager>() { @Override public BugreportManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.BUGREPORT_SERVICE); return new BugreportManager(ctx.getOuterContext(), IDumpstate.Stub.asInterface(b)); }})1172         registerService(Context.BUGREPORT_SERVICE, BugreportManager.class,
1173                 new CachedServiceFetcher<BugreportManager>() {
1174                     @Override
1175                     public BugreportManager createService(ContextImpl ctx)
1176                             throws ServiceNotFoundException {
1177                         IBinder b = ServiceManager.getServiceOrThrow(Context.BUGREPORT_SERVICE);
1178                         return new BugreportManager(ctx.getOuterContext(),
1179                                 IDumpstate.Stub.asInterface(b));
1180                     }});
1181 
registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class, new CachedServiceFetcher<AutofillManager>() { @Override public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE); IAutoFillManager service = IAutoFillManager.Stub.asInterface(b); return new AutofillManager(ctx.getOuterContext(), service); }})1182         registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
1183                 new CachedServiceFetcher<AutofillManager>() {
1184             @Override
1185             public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1186                 // Get the services without throwing as this is an optional feature
1187                 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
1188                 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
1189                 return new AutofillManager(ctx.getOuterContext(), service);
1190             }});
1191 
registerService(Context.CONTENT_CAPTURE_MANAGER_SERVICE, ContentCaptureManager.class, new CachedServiceFetcher<ContentCaptureManager>() { @Override public ContentCaptureManager createService(ContextImpl ctx) throws ServiceNotFoundException { Context outerContext = ctx.getOuterContext(); ContentCaptureOptions options = outerContext.getContentCaptureOptions(); if (options != null && (options.lite || options.isWhitelisted(outerContext))) { IBinder b = ServiceManager .getService(Context.CONTENT_CAPTURE_MANAGER_SERVICE); IContentCaptureManager service = IContentCaptureManager.Stub.asInterface(b); if (service != null) { return new ContentCaptureManager(outerContext, service, options); } } return null; }})1192         registerService(Context.CONTENT_CAPTURE_MANAGER_SERVICE, ContentCaptureManager.class,
1193                 new CachedServiceFetcher<ContentCaptureManager>() {
1194             @Override
1195             public ContentCaptureManager createService(ContextImpl ctx)
1196                     throws ServiceNotFoundException {
1197                 // Get the services without throwing as this is an optional feature
1198                 Context outerContext = ctx.getOuterContext();
1199                 ContentCaptureOptions options = outerContext.getContentCaptureOptions();
1200                 // Options is null when the service didn't whitelist the activity or package
1201                 if (options != null && (options.lite || options.isWhitelisted(outerContext))) {
1202                     IBinder b = ServiceManager
1203                             .getService(Context.CONTENT_CAPTURE_MANAGER_SERVICE);
1204                     IContentCaptureManager service = IContentCaptureManager.Stub.asInterface(b);
1205                     // Service is null when not provided by OEM or disabled by kill-switch.
1206                     if (service != null) {
1207                         return new ContentCaptureManager(outerContext, service, options);
1208                     }
1209                 }
1210                 // When feature is disabled or app / package not whitelisted, we return a null
1211                 // manager to apps so the performance impact is practically zero
1212                 return null;
1213             }});
1214 
registerService(Context.APP_PREDICTION_SERVICE, AppPredictionManager.class, new CachedServiceFetcher<AppPredictionManager>() { @Override public AppPredictionManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getService(Context.APP_PREDICTION_SERVICE); return b == null ? null : new AppPredictionManager(ctx); } })1215         registerService(Context.APP_PREDICTION_SERVICE, AppPredictionManager.class,
1216                 new CachedServiceFetcher<AppPredictionManager>() {
1217             @Override
1218             public AppPredictionManager createService(ContextImpl ctx)
1219                     throws ServiceNotFoundException {
1220                 IBinder b = ServiceManager.getService(Context.APP_PREDICTION_SERVICE);
1221                 return b == null ? null : new AppPredictionManager(ctx);
1222             }
1223         });
1224 
registerService(Context.CONTENT_SUGGESTIONS_SERVICE, ContentSuggestionsManager.class, new CachedServiceFetcher<ContentSuggestionsManager>() { @Override public ContentSuggestionsManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService( Context.CONTENT_SUGGESTIONS_SERVICE); IContentSuggestionsManager service = IContentSuggestionsManager.Stub.asInterface(b); return new ContentSuggestionsManager(ctx.getUserId(), service); } })1225         registerService(Context.CONTENT_SUGGESTIONS_SERVICE,
1226                 ContentSuggestionsManager.class,
1227                 new CachedServiceFetcher<ContentSuggestionsManager>() {
1228                     @Override
1229                     public ContentSuggestionsManager createService(ContextImpl ctx) {
1230                         // No throw as this is an optional service
1231                         IBinder b = ServiceManager.getService(
1232                                 Context.CONTENT_SUGGESTIONS_SERVICE);
1233                         IContentSuggestionsManager service =
1234                                 IContentSuggestionsManager.Stub.asInterface(b);
1235                         return new ContentSuggestionsManager(ctx.getUserId(), service);
1236                     }
1237                 });
1238 
registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() { @Override public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE); return new VrManager(IVrManager.Stub.asInterface(b)); } })1239         registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
1240             @Override
1241             public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1242                 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
1243                 return new VrManager(IVrManager.Stub.asInterface(b));
1244             }
1245         });
1246 
registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class, new CachedServiceFetcher<RulesManager>() { @Override public RulesManager createService(ContextImpl ctx) { return new RulesManager(ctx.getOuterContext()); }})1247         registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
1248                 new CachedServiceFetcher<RulesManager>() {
1249             @Override
1250             public RulesManager createService(ContextImpl ctx) {
1251                 return new RulesManager(ctx.getOuterContext());
1252             }});
1253 
registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class, new CachedServiceFetcher<CrossProfileApps>() { @Override public CrossProfileApps createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow( Context.CROSS_PROFILE_APPS_SERVICE); return new CrossProfileApps(ctx.getOuterContext(), ICrossProfileApps.Stub.asInterface(b)); } })1254         registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class,
1255                 new CachedServiceFetcher<CrossProfileApps>() {
1256                     @Override
1257                     public CrossProfileApps createService(ContextImpl ctx)
1258                             throws ServiceNotFoundException {
1259                         IBinder b = ServiceManager.getServiceOrThrow(
1260                                 Context.CROSS_PROFILE_APPS_SERVICE);
1261                         return new CrossProfileApps(ctx.getOuterContext(),
1262                                 ICrossProfileApps.Stub.asInterface(b));
1263                     }
1264                 });
1265 
registerService(Context.SLICE_SERVICE, SliceManager.class, new CachedServiceFetcher<SliceManager>() { @Override public SliceManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new SliceManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); } })1266         registerService(Context.SLICE_SERVICE, SliceManager.class,
1267                 new CachedServiceFetcher<SliceManager>() {
1268                     @Override
1269                     public SliceManager createService(ContextImpl ctx)
1270                             throws ServiceNotFoundException {
1271                         return new SliceManager(ctx.getOuterContext(),
1272                                 ctx.mMainThread.getHandler());
1273                     }
1274             });
1275 
registerService(Context.DEVICE_IDLE_CONTROLLER, DeviceIdleManager.class, new CachedServiceFetcher<DeviceIdleManager>() { @Override public DeviceIdleManager createService(ContextImpl ctx) throws ServiceNotFoundException { IDeviceIdleController service = IDeviceIdleController.Stub.asInterface( ServiceManager.getServiceOrThrow( Context.DEVICE_IDLE_CONTROLLER)); return new DeviceIdleManager(ctx.getOuterContext(), service); }})1276         registerService(Context.DEVICE_IDLE_CONTROLLER, DeviceIdleManager.class,
1277                 new CachedServiceFetcher<DeviceIdleManager>() {
1278                     @Override
1279                     public DeviceIdleManager createService(ContextImpl ctx)
1280                             throws ServiceNotFoundException {
1281                         IDeviceIdleController service = IDeviceIdleController.Stub.asInterface(
1282                                 ServiceManager.getServiceOrThrow(
1283                                         Context.DEVICE_IDLE_CONTROLLER));
1284                         return new DeviceIdleManager(ctx.getOuterContext(), service);
1285                     }});
1286 
registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class, new CachedServiceFetcher<TimeDetector>() { @Override public TimeDetector createService(ContextImpl ctx) throws ServiceNotFoundException { return new TimeDetectorImpl(); }})1287         registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class,
1288                 new CachedServiceFetcher<TimeDetector>() {
1289                     @Override
1290                     public TimeDetector createService(ContextImpl ctx)
1291                             throws ServiceNotFoundException {
1292                         return new TimeDetectorImpl();
1293                     }});
1294 
registerService(Context.TIME_ZONE_DETECTOR_SERVICE, TimeZoneDetector.class, new CachedServiceFetcher<TimeZoneDetector>() { @Override public TimeZoneDetector createService(ContextImpl ctx) throws ServiceNotFoundException { return new TimeZoneDetectorImpl(); }})1295         registerService(Context.TIME_ZONE_DETECTOR_SERVICE, TimeZoneDetector.class,
1296                 new CachedServiceFetcher<TimeZoneDetector>() {
1297                     @Override
1298                     public TimeZoneDetector createService(ContextImpl ctx)
1299                             throws ServiceNotFoundException {
1300                         return new TimeZoneDetectorImpl();
1301                     }});
1302 
registerService(Context.TELEPHONY_IMS_SERVICE, android.telephony.ims.ImsManager.class, new CachedServiceFetcher<android.telephony.ims.ImsManager>() { @Override public android.telephony.ims.ImsManager createService(ContextImpl ctx) { return new android.telephony.ims.ImsManager(ctx.getOuterContext()); } })1303         registerService(Context.TELEPHONY_IMS_SERVICE, android.telephony.ims.ImsManager.class,
1304                 new CachedServiceFetcher<android.telephony.ims.ImsManager>() {
1305                     @Override
1306                     public android.telephony.ims.ImsManager createService(ContextImpl ctx) {
1307                         return new android.telephony.ims.ImsManager(ctx.getOuterContext());
1308                     }
1309                 });
1310 
registerService(Context.PERMISSION_SERVICE, PermissionManager.class, new CachedServiceFetcher<PermissionManager>() { @Override public PermissionManager createService(ContextImpl ctx) { IPackageManager packageManager = AppGlobals.getPackageManager(); return new PermissionManager(ctx.getOuterContext(), packageManager); }})1311         registerService(Context.PERMISSION_SERVICE, PermissionManager.class,
1312                 new CachedServiceFetcher<PermissionManager>() {
1313                     @Override
1314                     public PermissionManager createService(ContextImpl ctx) {
1315                         IPackageManager packageManager = AppGlobals.getPackageManager();
1316                         return new PermissionManager(ctx.getOuterContext(), packageManager);
1317                     }});
1318 
registerService(Context.PERMISSION_CONTROLLER_SERVICE, PermissionControllerManager.class, new CachedServiceFetcher<PermissionControllerManager>() { @Override public PermissionControllerManager createService(ContextImpl ctx) { return new PermissionControllerManager(ctx.getOuterContext(), ctx.getMainThreadHandler()); }})1319         registerService(Context.PERMISSION_CONTROLLER_SERVICE, PermissionControllerManager.class,
1320                 new CachedServiceFetcher<PermissionControllerManager>() {
1321                     @Override
1322                     public PermissionControllerManager createService(ContextImpl ctx) {
1323                         return new PermissionControllerManager(ctx.getOuterContext(),
1324                                 ctx.getMainThreadHandler());
1325                     }});
1326 
registerService(Context.ROLE_SERVICE, RoleManager.class, new CachedServiceFetcher<RoleManager>() { @Override public RoleManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new RoleManager(ctx.getOuterContext()); }})1327         registerService(Context.ROLE_SERVICE, RoleManager.class,
1328                 new CachedServiceFetcher<RoleManager>() {
1329                     @Override
1330                     public RoleManager createService(ContextImpl ctx)
1331                             throws ServiceNotFoundException {
1332                         return new RoleManager(ctx.getOuterContext());
1333                     }});
1334 
registerService(Context.ROLE_CONTROLLER_SERVICE, RoleControllerManager.class, new CachedServiceFetcher<RoleControllerManager>() { @Override public RoleControllerManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new RoleControllerManager(ctx.getOuterContext()); }})1335         registerService(Context.ROLE_CONTROLLER_SERVICE, RoleControllerManager.class,
1336                 new CachedServiceFetcher<RoleControllerManager>() {
1337                     @Override
1338                     public RoleControllerManager createService(ContextImpl ctx)
1339                             throws ServiceNotFoundException {
1340                         return new RoleControllerManager(ctx.getOuterContext());
1341                     }});
1342 
registerService(Context.ROLLBACK_SERVICE, RollbackManager.class, new CachedServiceFetcher<RollbackManager>() { @Override public RollbackManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow(Context.ROLLBACK_SERVICE); return new RollbackManager(ctx.getOuterContext(), IRollbackManager.Stub.asInterface(b)); }})1343         registerService(Context.ROLLBACK_SERVICE, RollbackManager.class,
1344                 new CachedServiceFetcher<RollbackManager>() {
1345                     @Override
1346                     public RollbackManager createService(ContextImpl ctx)
1347                             throws ServiceNotFoundException {
1348                         IBinder b = ServiceManager.getServiceOrThrow(Context.ROLLBACK_SERVICE);
1349                         return new RollbackManager(ctx.getOuterContext(),
1350                                 IRollbackManager.Stub.asInterface(b));
1351                     }});
1352 
registerService(Context.DYNAMIC_SYSTEM_SERVICE, DynamicSystemManager.class, new CachedServiceFetcher<DynamicSystemManager>() { @Override public DynamicSystemManager createService(ContextImpl ctx) throws ServiceNotFoundException { IBinder b = ServiceManager.getServiceOrThrow( Context.DYNAMIC_SYSTEM_SERVICE); return new DynamicSystemManager( IDynamicSystemService.Stub.asInterface(b)); }})1353         registerService(Context.DYNAMIC_SYSTEM_SERVICE, DynamicSystemManager.class,
1354                 new CachedServiceFetcher<DynamicSystemManager>() {
1355                     @Override
1356                     public DynamicSystemManager createService(ContextImpl ctx)
1357                             throws ServiceNotFoundException {
1358                         IBinder b = ServiceManager.getServiceOrThrow(
1359                                 Context.DYNAMIC_SYSTEM_SERVICE);
1360                         return new DynamicSystemManager(
1361                                 IDynamicSystemService.Stub.asInterface(b));
1362                     }});
registerService(Context.LIGHTS_SERVICE, LightsManager.class, new CachedServiceFetcher<LightsManager>() { @Override public LightsManager createService(ContextImpl ctx) throws ServiceNotFoundException { return new LightsManager(ctx); }})1363         registerService(Context.LIGHTS_SERVICE, LightsManager.class,
1364             new CachedServiceFetcher<LightsManager>() {
1365                 @Override
1366                 public LightsManager createService(ContextImpl ctx)
1367                     throws ServiceNotFoundException {
1368                     return new LightsManager(ctx);
1369                 }});
1370         //CHECKSTYLE:ON IndentationCheck
1371     }
1372 
1373     /**
1374      * Creates an array which is used to cache per-Context service instances.
1375      */
createServiceCache()1376     public static Object[] createServiceCache() {
1377         return new Object[sServiceCacheSize];
1378     }
1379 
1380     /**
1381      * Gets a system service from a given context.
1382      */
getSystemService(ContextImpl ctx, String name)1383     public static Object getSystemService(ContextImpl ctx, String name) {
1384         ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
1385         return fetcher != null ? fetcher.getService(ctx) : null;
1386     }
1387 
1388     /**
1389      * Gets the name of the system-level service that is represented by the specified class.
1390      */
getSystemServiceName(Class<?> serviceClass)1391     public static String getSystemServiceName(Class<?> serviceClass) {
1392         return SYSTEM_SERVICE_NAMES.get(serviceClass);
1393     }
1394 
1395     /**
1396      * Statically registers a system service with the context.
1397      * This method must be called during static initialization only.
1398      */
registerService(String serviceName, Class<T> serviceClass, ServiceFetcher<T> serviceFetcher)1399     private static <T> void registerService(String serviceName, Class<T> serviceClass,
1400             ServiceFetcher<T> serviceFetcher) {
1401         SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
1402         SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
1403     }
1404 
1405     /**
1406      * Base interface for classes that fetch services.
1407      * These objects must only be created during static initialization.
1408      */
1409     static abstract interface ServiceFetcher<T> {
getService(ContextImpl ctx)1410         T getService(ContextImpl ctx);
1411     }
1412 
1413     /**
1414      * Override this class when the system service constructor needs a
1415      * ContextImpl and should be cached and retained by that context.
1416      */
1417     static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
1418         private final int mCacheIndex;
1419 
CachedServiceFetcher()1420         CachedServiceFetcher() {
1421             // Note this class must be instantiated only by the static initializer of the
1422             // outer class (SystemServiceRegistry), which already does the synchronization,
1423             // so bare access to sServiceCacheSize is okay here.
1424             mCacheIndex = sServiceCacheSize++;
1425         }
1426 
1427         @Override
1428         @SuppressWarnings("unchecked")
getService(ContextImpl ctx)1429         public final T getService(ContextImpl ctx) {
1430             final Object[] cache = ctx.mServiceCache;
1431             final int[] gates = ctx.mServiceInitializationStateArray;
1432 
1433             for (;;) {
1434                 boolean doInitialize = false;
1435                 synchronized (cache) {
1436                     // Return it if we already have a cached instance.
1437                     T service = (T) cache[mCacheIndex];
1438                     if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) {
1439                         return service;
1440                     }
1441 
1442                     // If we get here, there's no cached instance.
1443 
1444                     // Grr... if gate is STATE_READY, then this means we initialized the service
1445                     // once but someone cleared it.
1446                     // We start over from STATE_UNINITIALIZED.
1447                     if (gates[mCacheIndex] == ContextImpl.STATE_READY) {
1448                         gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED;
1449                     }
1450 
1451                     // It's possible for multiple threads to get here at the same time, so
1452                     // use the "gate" to make sure only the first thread will call createService().
1453 
1454                     // At this point, the gate must be either UNINITIALIZED or INITIALIZING.
1455                     if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) {
1456                         doInitialize = true;
1457                         gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING;
1458                     }
1459                 }
1460 
1461                 if (doInitialize) {
1462                     // Only the first thread gets here.
1463 
1464                     T service = null;
1465                     @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND;
1466                     try {
1467                         // This thread is the first one to get here. Instantiate the service
1468                         // *without* the cache lock held.
1469                         service = createService(ctx);
1470                         newState = ContextImpl.STATE_READY;
1471 
1472                     } catch (ServiceNotFoundException e) {
1473                         onServiceNotFound(e);
1474 
1475                     } finally {
1476                         synchronized (cache) {
1477                             cache[mCacheIndex] = service;
1478                             gates[mCacheIndex] = newState;
1479                             cache.notifyAll();
1480                         }
1481                     }
1482                     return service;
1483                 }
1484                 // The other threads will wait for the first thread to call notifyAll(),
1485                 // and go back to the top and retry.
1486                 synchronized (cache) {
1487                     while (gates[mCacheIndex] < ContextImpl.STATE_READY) {
1488                         try {
1489                             cache.wait();
1490                         } catch (InterruptedException e) {
1491                             Log.w(TAG, "getService() interrupted");
1492                             Thread.currentThread().interrupt();
1493                             return null;
1494                         }
1495                     }
1496                 }
1497             }
1498         }
1499 
createService(ContextImpl ctx)1500         public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
1501     }
1502 
1503     /**
1504      * Override this class when the system service does not need a ContextImpl
1505      * and should be cached and retained process-wide.
1506      */
1507     static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
1508         private T mCachedInstance;
1509 
1510         @Override
getService(ContextImpl ctx)1511         public final T getService(ContextImpl ctx) {
1512             synchronized (StaticServiceFetcher.this) {
1513                 if (mCachedInstance == null) {
1514                     try {
1515                         mCachedInstance = createService();
1516                     } catch (ServiceNotFoundException e) {
1517                         onServiceNotFound(e);
1518                     }
1519                 }
1520                 return mCachedInstance;
1521             }
1522         }
1523 
createService()1524         public abstract T createService() throws ServiceNotFoundException;
1525     }
1526 
1527     /**
1528      * Like StaticServiceFetcher, creates only one instance of the service per application, but when
1529      * creating the service for the first time, passes it the application context of the creating
1530      * application.
1531      *
1532      * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
1533      * case where multiple application components each have their own ConnectivityManager object.
1534      */
1535     static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
1536         private T mCachedInstance;
1537 
1538         @Override
getService(ContextImpl ctx)1539         public final T getService(ContextImpl ctx) {
1540             synchronized (StaticApplicationContextServiceFetcher.this) {
1541                 if (mCachedInstance == null) {
1542                     Context appContext = ctx.getApplicationContext();
1543                     // If the application context is null, we're either in the system process or
1544                     // it's the application context very early in app initialization. In both these
1545                     // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
1546                     // to the service. http://b/27532714 .
1547                     try {
1548                         mCachedInstance = createService(appContext != null ? appContext : ctx);
1549                     } catch (ServiceNotFoundException e) {
1550                         onServiceNotFound(e);
1551                     }
1552                 }
1553                 return mCachedInstance;
1554             }
1555         }
1556 
createService(Context applicationContext)1557         public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
1558     }
1559 
onServiceNotFound(ServiceNotFoundException e)1560     public static void onServiceNotFound(ServiceNotFoundException e) {
1561         // We're mostly interested in tracking down long-lived core system
1562         // components that might stumble if they obtain bad references; just
1563         // emit a tidy log message for normal apps
1564         if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1565             Log.wtf(TAG, e.getMessage(), e);
1566         } else {
1567             Log.w(TAG, e.getMessage());
1568         }
1569     }
1570 }
1571