blob: 246d4a379eb4acec980676dac96709678b641b62 [file] [log] [blame]
Justin Klaassen10d07c82017-09-15 17:58:39 -04001/*
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
17package android.app;
18
19import android.accounts.AccountManager;
20import android.accounts.IAccountManager;
Justin Klaassenb8042fc2018-04-15 00:41:15 -040021import android.app.ContextImpl.ServiceInitializationState;
Justin Klaassen10d07c82017-09-15 17:58:39 -040022import android.app.admin.DevicePolicyManager;
23import android.app.admin.IDevicePolicyManager;
24import android.app.job.IJobScheduler;
25import android.app.job.JobScheduler;
Justin Klaassen98fe7812018-01-03 13:39:41 -050026import android.app.slice.SliceManager;
Justin Klaassen10d07c82017-09-15 17:58:39 -040027import android.app.timezone.RulesManager;
28import android.app.trust.TrustManager;
29import android.app.usage.IStorageStatsManager;
30import android.app.usage.IUsageStatsManager;
31import android.app.usage.NetworkStatsManager;
32import android.app.usage.StorageStatsManager;
33import android.app.usage.UsageStatsManager;
34import android.appwidget.AppWidgetManager;
35import android.bluetooth.BluetoothManager;
36import android.companion.CompanionDeviceManager;
37import android.companion.ICompanionDeviceManager;
38import android.content.ClipboardManager;
39import android.content.Context;
40import android.content.IRestrictionsManager;
41import android.content.RestrictionsManager;
Jeff Davidsona192cc22018-02-08 15:30:06 -080042import android.content.pm.CrossProfileApps;
43import android.content.pm.ICrossProfileApps;
Justin Klaassen10d07c82017-09-15 17:58:39 -040044import android.content.pm.IShortcutService;
45import android.content.pm.LauncherApps;
46import android.content.pm.PackageManager;
47import android.content.pm.ShortcutManager;
48import android.content.res.Resources;
49import android.hardware.ConsumerIrManager;
50import android.hardware.ISerialManager;
51import android.hardware.SensorManager;
52import android.hardware.SerialManager;
53import android.hardware.SystemSensorManager;
54import android.hardware.camera2.CameraManager;
55import android.hardware.display.DisplayManager;
56import android.hardware.fingerprint.FingerprintManager;
57import android.hardware.fingerprint.IFingerprintService;
58import android.hardware.hdmi.HdmiControlManager;
59import android.hardware.hdmi.IHdmiControlService;
60import android.hardware.input.InputManager;
61import android.hardware.location.ContextHubManager;
62import android.hardware.radio.RadioManager;
63import android.hardware.usb.IUsbManager;
64import android.hardware.usb.UsbManager;
65import android.location.CountryDetector;
66import android.location.ICountryDetector;
67import android.location.ILocationManager;
68import android.location.LocationManager;
69import android.media.AudioManager;
70import android.media.MediaRouter;
71import android.media.midi.IMidiManager;
72import android.media.midi.MidiManager;
73import android.media.projection.MediaProjectionManager;
74import android.media.session.MediaSessionManager;
75import android.media.soundtrigger.SoundTriggerManager;
76import android.media.tv.ITvInputManager;
77import android.media.tv.TvInputManager;
78import android.net.ConnectivityManager;
79import android.net.ConnectivityThread;
80import android.net.EthernetManager;
81import android.net.IConnectivityManager;
82import android.net.IEthernetManager;
83import android.net.IIpSecService;
84import android.net.INetworkPolicyManager;
85import android.net.IpSecManager;
86import android.net.NetworkPolicyManager;
87import android.net.NetworkScoreManager;
Justin Klaassen6a65f2d2017-11-17 16:38:15 -050088import android.net.NetworkWatchlistManager;
Justin Klaassen10d07c82017-09-15 17:58:39 -040089import android.net.lowpan.ILowpanManager;
90import android.net.lowpan.LowpanManager;
Justin Klaassen93b7ee42017-10-10 15:20:13 -040091import android.net.nsd.INsdManager;
92import android.net.nsd.NsdManager;
Justin Klaassen10d07c82017-09-15 17:58:39 -040093import android.net.wifi.IWifiManager;
94import android.net.wifi.IWifiScanner;
95import android.net.wifi.RttManager;
96import android.net.wifi.WifiManager;
97import android.net.wifi.WifiScanner;
98import android.net.wifi.aware.IWifiAwareManager;
99import android.net.wifi.aware.WifiAwareManager;
100import android.net.wifi.p2p.IWifiP2pManager;
101import android.net.wifi.p2p.WifiP2pManager;
Justin Klaassen93b7ee42017-10-10 15:20:13 -0400102import android.net.wifi.rtt.IWifiRttManager;
103import android.net.wifi.rtt.WifiRttManager;
Justin Klaassen10d07c82017-09-15 17:58:39 -0400104import android.nfc.NfcManager;
105import android.os.BatteryManager;
106import android.os.BatteryStats;
107import android.os.Build;
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400108import android.os.DeviceIdleManager;
Justin Klaassen10d07c82017-09-15 17:58:39 -0400109import android.os.DropBoxManager;
110import android.os.HardwarePropertiesManager;
111import android.os.IBatteryPropertiesRegistrar;
112import android.os.IBinder;
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400113import android.os.IDeviceIdleController;
Justin Klaassen10d07c82017-09-15 17:58:39 -0400114import android.os.IHardwarePropertiesManager;
115import android.os.IPowerManager;
116import android.os.IRecoverySystem;
Jeff Davidsona192cc22018-02-08 15:30:06 -0800117import android.os.ISystemUpdateManager;
Justin Klaassen10d07c82017-09-15 17:58:39 -0400118import android.os.IUserManager;
119import android.os.IncidentManager;
120import android.os.PowerManager;
Justin Klaassen10d07c82017-09-15 17:58:39 -0400121import android.os.RecoverySystem;
122import android.os.ServiceManager;
123import android.os.ServiceManager.ServiceNotFoundException;
Jeff Davidsona192cc22018-02-08 15:30:06 -0800124import android.os.SystemUpdateManager;
Justin Klaassen10d07c82017-09-15 17:58:39 -0400125import android.os.SystemVibrator;
126import android.os.UserHandle;
127import android.os.UserManager;
128import android.os.Vibrator;
129import android.os.health.SystemHealthManager;
130import android.os.storage.StorageManager;
131import android.print.IPrintManager;
132import android.print.PrintManager;
133import android.service.oemlock.IOemLockService;
134import android.service.oemlock.OemLockManager;
135import android.service.persistentdata.IPersistentDataBlockService;
136import android.service.persistentdata.PersistentDataBlockManager;
137import android.service.vr.IVrManager;
138import android.telecom.TelecomManager;
139import android.telephony.CarrierConfigManager;
140import android.telephony.SubscriptionManager;
141import android.telephony.TelephonyManager;
Jeff Davidsona192cc22018-02-08 15:30:06 -0800142import android.telephony.euicc.EuiccCardManager;
Justin Klaassen10d07c82017-09-15 17:58:39 -0400143import android.telephony.euicc.EuiccManager;
144import android.util.Log;
145import android.view.ContextThemeWrapper;
146import android.view.LayoutInflater;
147import android.view.WindowManager;
148import android.view.WindowManagerImpl;
149import android.view.accessibility.AccessibilityManager;
150import android.view.accessibility.CaptioningManager;
151import android.view.autofill.AutofillManager;
152import android.view.autofill.IAutoFillManager;
153import android.view.inputmethod.InputMethodManager;
154import android.view.textclassifier.TextClassificationManager;
155import android.view.textservice.TextServicesManager;
156
157import com.android.internal.app.IAppOpsService;
158import com.android.internal.app.IBatteryStats;
159import com.android.internal.app.ISoundTriggerService;
160import com.android.internal.appwidget.IAppWidgetService;
Justin Klaassen6a65f2d2017-11-17 16:38:15 -0500161import com.android.internal.net.INetworkWatchlistManager;
Justin Klaassen10d07c82017-09-15 17:58:39 -0400162import com.android.internal.os.IDropBoxManagerService;
163import com.android.internal.policy.PhoneLayoutInflater;
164
165import java.util.HashMap;
166
167/**
168 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
169 * Used by {@link ContextImpl}.
170 */
171final class SystemServiceRegistry {
172 private static final String TAG = "SystemServiceRegistry";
173
174 // Service registry information.
175 // This information is never changed once static initialization has completed.
176 private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
177 new HashMap<Class<?>, String>();
178 private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
179 new HashMap<String, ServiceFetcher<?>>();
180 private static int sServiceCacheSize;
181
182 // Not instantiable.
183 private SystemServiceRegistry() { }
184
185 static {
186 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
187 new CachedServiceFetcher<AccessibilityManager>() {
188 @Override
189 public AccessibilityManager createService(ContextImpl ctx) {
190 return AccessibilityManager.getInstance(ctx);
191 }});
192
193 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
194 new CachedServiceFetcher<CaptioningManager>() {
195 @Override
196 public CaptioningManager createService(ContextImpl ctx) {
197 return new CaptioningManager(ctx);
198 }});
199
200 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
201 new CachedServiceFetcher<AccountManager>() {
202 @Override
203 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
204 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
205 IAccountManager service = IAccountManager.Stub.asInterface(b);
206 return new AccountManager(ctx, service);
207 }});
208
209 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
210 new CachedServiceFetcher<ActivityManager>() {
211 @Override
212 public ActivityManager createService(ContextImpl ctx) {
213 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
214 }});
215
216 registerService(Context.ALARM_SERVICE, AlarmManager.class,
217 new CachedServiceFetcher<AlarmManager>() {
218 @Override
219 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
220 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
221 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
222 return new AlarmManager(service, ctx);
223 }});
224
225 registerService(Context.AUDIO_SERVICE, AudioManager.class,
226 new CachedServiceFetcher<AudioManager>() {
227 @Override
228 public AudioManager createService(ContextImpl ctx) {
229 return new AudioManager(ctx);
230 }});
231
232 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
233 new CachedServiceFetcher<MediaRouter>() {
234 @Override
235 public MediaRouter createService(ContextImpl ctx) {
236 return new MediaRouter(ctx);
237 }});
238
239 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
240 new CachedServiceFetcher<BluetoothManager>() {
241 @Override
242 public BluetoothManager createService(ContextImpl ctx) {
243 return new BluetoothManager(ctx);
244 }});
245
246 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
247 new StaticServiceFetcher<HdmiControlManager>() {
248 @Override
249 public HdmiControlManager createService() throws ServiceNotFoundException {
250 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
251 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
252 }});
253
254 registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
255 new CachedServiceFetcher<TextClassificationManager>() {
256 @Override
257 public TextClassificationManager createService(ContextImpl ctx) {
258 return new TextClassificationManager(ctx);
259 }});
260
261 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
262 new CachedServiceFetcher<ClipboardManager>() {
263 @Override
264 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
265 return new ClipboardManager(ctx.getOuterContext(),
266 ctx.mMainThread.getHandler());
267 }});
268
269 // The clipboard service moved to a new package. If someone asks for the old
270 // interface by class then we want to redirect over to the new interface instead
271 // (which extends it).
272 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
273
274 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
275 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
276 @Override
277 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
278 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
279 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
280 return new ConnectivityManager(context, service);
281 }});
282
283 registerService(Context.IPSEC_SERVICE, IpSecManager.class,
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400284 new CachedServiceFetcher<IpSecManager>() {
Justin Klaassen10d07c82017-09-15 17:58:39 -0400285 @Override
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400286 public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Justin Klaassen10d07c82017-09-15 17:58:39 -0400287 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
288 IIpSecService service = IIpSecService.Stub.asInterface(b);
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400289 return new IpSecManager(ctx, service);
Justin Klaassen10d07c82017-09-15 17:58:39 -0400290 }});
291
292 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
293 new StaticServiceFetcher<CountryDetector>() {
294 @Override
295 public CountryDetector createService() throws ServiceNotFoundException {
296 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
297 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
298 }});
299
300 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
301 new CachedServiceFetcher<DevicePolicyManager>() {
302 @Override
303 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
304 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
305 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
306 }});
307
308 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
309 new CachedServiceFetcher<DownloadManager>() {
310 @Override
311 public DownloadManager createService(ContextImpl ctx) {
312 return new DownloadManager(ctx);
313 }});
314
315 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
Justin Klaassen6a65f2d2017-11-17 16:38:15 -0500316 new CachedServiceFetcher<BatteryManager>() {
Justin Klaassen10d07c82017-09-15 17:58:39 -0400317 @Override
Justin Klaassen6a65f2d2017-11-17 16:38:15 -0500318 public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Justin Klaassen10d07c82017-09-15 17:58:39 -0400319 IBatteryStats stats = IBatteryStats.Stub.asInterface(
320 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
321 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
322 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
Justin Klaassen6a65f2d2017-11-17 16:38:15 -0500323 return new BatteryManager(ctx, stats, registrar);
Justin Klaassen10d07c82017-09-15 17:58:39 -0400324 }});
325
326 registerService(Context.NFC_SERVICE, NfcManager.class,
327 new CachedServiceFetcher<NfcManager>() {
328 @Override
329 public NfcManager createService(ContextImpl ctx) {
330 return new NfcManager(ctx);
331 }});
332
333 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
334 new CachedServiceFetcher<DropBoxManager>() {
335 @Override
336 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
337 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
338 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
339 return new DropBoxManager(ctx, service);
340 }});
341
342 registerService(Context.INPUT_SERVICE, InputManager.class,
343 new StaticServiceFetcher<InputManager>() {
344 @Override
345 public InputManager createService() {
346 return InputManager.getInstance();
347 }});
348
349 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
350 new CachedServiceFetcher<DisplayManager>() {
351 @Override
352 public DisplayManager createService(ContextImpl ctx) {
353 return new DisplayManager(ctx.getOuterContext());
354 }});
355
356 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
357 new StaticServiceFetcher<InputMethodManager>() {
358 @Override
359 public InputMethodManager createService() {
360 return InputMethodManager.getInstance();
361 }});
362
363 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
364 new StaticServiceFetcher<TextServicesManager>() {
365 @Override
366 public TextServicesManager createService() {
367 return TextServicesManager.getInstance();
368 }});
369
370 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
371 new CachedServiceFetcher<KeyguardManager>() {
372 @Override
373 public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
374 return new KeyguardManager(ctx);
375 }});
376
377 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
378 new CachedServiceFetcher<LayoutInflater>() {
379 @Override
380 public LayoutInflater createService(ContextImpl ctx) {
381 return new PhoneLayoutInflater(ctx.getOuterContext());
382 }});
383
384 registerService(Context.LOCATION_SERVICE, LocationManager.class,
385 new CachedServiceFetcher<LocationManager>() {
386 @Override
387 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
388 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
389 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
390 }});
391
392 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
393 new CachedServiceFetcher<NetworkPolicyManager>() {
394 @Override
395 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
396 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
397 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
398 }});
399
400 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
401 new CachedServiceFetcher<NotificationManager>() {
402 @Override
403 public NotificationManager createService(ContextImpl ctx) {
404 final Context outerContext = ctx.getOuterContext();
405 return new NotificationManager(
406 new ContextThemeWrapper(outerContext,
407 Resources.selectSystemTheme(0,
408 outerContext.getApplicationInfo().targetSdkVersion,
409 com.android.internal.R.style.Theme_Dialog,
410 com.android.internal.R.style.Theme_Holo_Dialog,
411 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
412 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
413 ctx.mMainThread.getHandler());
414 }});
415
416 registerService(Context.NSD_SERVICE, NsdManager.class,
417 new CachedServiceFetcher<NsdManager>() {
418 @Override
419 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
420 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
421 INsdManager service = INsdManager.Stub.asInterface(b);
422 return new NsdManager(ctx.getOuterContext(), service);
423 }});
424
425 registerService(Context.POWER_SERVICE, PowerManager.class,
426 new CachedServiceFetcher<PowerManager>() {
427 @Override
428 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
429 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
430 IPowerManager service = IPowerManager.Stub.asInterface(b);
431 return new PowerManager(ctx.getOuterContext(),
432 service, ctx.mMainThread.getHandler());
433 }});
434
435 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
436 new CachedServiceFetcher<RecoverySystem>() {
437 @Override
438 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
439 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
440 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
441 return new RecoverySystem(service);
442 }});
443
444 registerService(Context.SEARCH_SERVICE, SearchManager.class,
445 new CachedServiceFetcher<SearchManager>() {
446 @Override
447 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
448 return new SearchManager(ctx.getOuterContext(),
449 ctx.mMainThread.getHandler());
450 }});
451
452 registerService(Context.SENSOR_SERVICE, SensorManager.class,
453 new CachedServiceFetcher<SensorManager>() {
454 @Override
455 public SensorManager createService(ContextImpl ctx) {
456 return new SystemSensorManager(ctx.getOuterContext(),
457 ctx.mMainThread.getHandler().getLooper());
458 }});
459
Justin Klaassen6a65f2d2017-11-17 16:38:15 -0500460 registerService(Context.STATS_MANAGER, StatsManager.class,
461 new StaticServiceFetcher<StatsManager>() {
462 @Override
463 public StatsManager createService() throws ServiceNotFoundException {
464 return new StatsManager();
465 }});
466
Justin Klaassen10d07c82017-09-15 17:58:39 -0400467 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
468 new CachedServiceFetcher<StatusBarManager>() {
469 @Override
470 public StatusBarManager createService(ContextImpl ctx) {
471 return new StatusBarManager(ctx.getOuterContext());
472 }});
473
474 registerService(Context.STORAGE_SERVICE, StorageManager.class,
475 new CachedServiceFetcher<StorageManager>() {
476 @Override
477 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
478 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
479 }});
480
481 registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
482 new CachedServiceFetcher<StorageStatsManager>() {
483 @Override
484 public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
485 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
486 ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
487 return new StorageStatsManager(ctx, service);
488 }});
489
Jeff Davidsona192cc22018-02-08 15:30:06 -0800490 registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class,
491 new CachedServiceFetcher<SystemUpdateManager>() {
492 @Override
493 public SystemUpdateManager createService(ContextImpl ctx)
494 throws ServiceNotFoundException {
495 IBinder b = ServiceManager.getServiceOrThrow(
496 Context.SYSTEM_UPDATE_SERVICE);
497 ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b);
498 return new SystemUpdateManager(service);
499 }});
500
Justin Klaassen10d07c82017-09-15 17:58:39 -0400501 registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
502 new CachedServiceFetcher<TelephonyManager>() {
503 @Override
504 public TelephonyManager createService(ContextImpl ctx) {
505 return new TelephonyManager(ctx.getOuterContext());
506 }});
507
508 registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
509 new CachedServiceFetcher<SubscriptionManager>() {
510 @Override
Jeff Davidsona192cc22018-02-08 15:30:06 -0800511 public SubscriptionManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Justin Klaassen10d07c82017-09-15 17:58:39 -0400512 return new SubscriptionManager(ctx.getOuterContext());
513 }});
514
515 registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
516 new CachedServiceFetcher<CarrierConfigManager>() {
517 @Override
518 public CarrierConfigManager createService(ContextImpl ctx) {
519 return new CarrierConfigManager();
520 }});
521
522 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
523 new CachedServiceFetcher<TelecomManager>() {
524 @Override
525 public TelecomManager createService(ContextImpl ctx) {
526 return new TelecomManager(ctx.getOuterContext());
527 }});
528
529 registerService(Context.EUICC_SERVICE, EuiccManager.class,
530 new CachedServiceFetcher<EuiccManager>() {
531 @Override
532 public EuiccManager createService(ContextImpl ctx) {
533 return new EuiccManager(ctx.getOuterContext());
534 }});
535
Jeff Davidsona192cc22018-02-08 15:30:06 -0800536 registerService(Context.EUICC_CARD_SERVICE, EuiccCardManager.class,
537 new CachedServiceFetcher<EuiccCardManager>() {
538 @Override
539 public EuiccCardManager createService(ContextImpl ctx) {
540 return new EuiccCardManager(ctx.getOuterContext());
541 }});
542
Justin Klaassen10d07c82017-09-15 17:58:39 -0400543 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
544 new CachedServiceFetcher<UiModeManager>() {
545 @Override
546 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
547 return new UiModeManager();
548 }});
549
550 registerService(Context.USB_SERVICE, UsbManager.class,
551 new CachedServiceFetcher<UsbManager>() {
552 @Override
553 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
554 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
555 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
556 }});
557
558 registerService(Context.SERIAL_SERVICE, SerialManager.class,
559 new CachedServiceFetcher<SerialManager>() {
560 @Override
561 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
562 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
563 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
564 }});
565
566 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
567 new CachedServiceFetcher<Vibrator>() {
568 @Override
569 public Vibrator createService(ContextImpl ctx) {
570 return new SystemVibrator(ctx);
571 }});
572
573 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
574 new CachedServiceFetcher<WallpaperManager>() {
575 @Override
Justin Klaassen98fe7812018-01-03 13:39:41 -0500576 public WallpaperManager createService(ContextImpl ctx)
577 throws ServiceNotFoundException {
578 final IBinder b;
579 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
580 b = ServiceManager.getServiceOrThrow(Context.WALLPAPER_SERVICE);
581 } else {
582 b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
583 }
584 IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
585 return new WallpaperManager(service, ctx.getOuterContext(),
Justin Klaassen10d07c82017-09-15 17:58:39 -0400586 ctx.mMainThread.getHandler());
587 }});
588
589 registerService(Context.LOWPAN_SERVICE, LowpanManager.class,
590 new CachedServiceFetcher<LowpanManager>() {
591 @Override
592 public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
593 IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE);
594 ILowpanManager service = ILowpanManager.Stub.asInterface(b);
595 return new LowpanManager(ctx.getOuterContext(), service,
596 ConnectivityThread.getInstanceLooper());
597 }});
598
599 registerService(Context.WIFI_SERVICE, WifiManager.class,
600 new CachedServiceFetcher<WifiManager>() {
601 @Override
602 public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
603 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE);
604 IWifiManager service = IWifiManager.Stub.asInterface(b);
605 return new WifiManager(ctx.getOuterContext(), service,
606 ConnectivityThread.getInstanceLooper());
607 }});
608
609 registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
610 new StaticServiceFetcher<WifiP2pManager>() {
611 @Override
612 public WifiP2pManager createService() throws ServiceNotFoundException {
613 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE);
614 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
615 return new WifiP2pManager(service);
616 }});
617
618 registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class,
619 new CachedServiceFetcher<WifiAwareManager>() {
620 @Override
621 public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException {
622 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE);
623 IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b);
624 if (service == null) {
625 return null;
626 }
627 return new WifiAwareManager(ctx.getOuterContext(), service);
628 }});
629
630 registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
631 new CachedServiceFetcher<WifiScanner>() {
632 @Override
633 public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException {
634 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE);
635 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
636 return new WifiScanner(ctx.getOuterContext(), service,
637 ConnectivityThread.getInstanceLooper());
638 }});
639
640 registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
641 new CachedServiceFetcher<RttManager>() {
Justin Klaassen4d01eea2018-04-03 23:21:57 -0400642 @Override
643 public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException {
644 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_RANGING_SERVICE);
645 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
646 return new RttManager(ctx.getOuterContext(),
647 new WifiRttManager(ctx.getOuterContext(), service));
648 }});
Justin Klaassen10d07c82017-09-15 17:58:39 -0400649
Justin Klaassen98fe7812018-01-03 13:39:41 -0500650 registerService(Context.WIFI_RTT_RANGING_SERVICE, WifiRttManager.class,
Justin Klaassen93b7ee42017-10-10 15:20:13 -0400651 new CachedServiceFetcher<WifiRttManager>() {
652 @Override
653 public WifiRttManager createService(ContextImpl ctx)
654 throws ServiceNotFoundException {
Justin Klaassen98fe7812018-01-03 13:39:41 -0500655 IBinder b = ServiceManager.getServiceOrThrow(
656 Context.WIFI_RTT_RANGING_SERVICE);
Justin Klaassen93b7ee42017-10-10 15:20:13 -0400657 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
658 return new WifiRttManager(ctx.getOuterContext(), service);
659 }});
660
Justin Klaassen10d07c82017-09-15 17:58:39 -0400661 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
662 new CachedServiceFetcher<EthernetManager>() {
663 @Override
664 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
665 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
666 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
667 return new EthernetManager(ctx.getOuterContext(), service);
668 }});
669
670 registerService(Context.WINDOW_SERVICE, WindowManager.class,
671 new CachedServiceFetcher<WindowManager>() {
672 @Override
673 public WindowManager createService(ContextImpl ctx) {
674 return new WindowManagerImpl(ctx);
675 }});
676
677 registerService(Context.USER_SERVICE, UserManager.class,
678 new CachedServiceFetcher<UserManager>() {
679 @Override
680 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
681 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
682 IUserManager service = IUserManager.Stub.asInterface(b);
683 return new UserManager(ctx, service);
684 }});
685
686 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
687 new CachedServiceFetcher<AppOpsManager>() {
688 @Override
689 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
690 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
691 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
692 return new AppOpsManager(ctx, service);
693 }});
694
695 registerService(Context.CAMERA_SERVICE, CameraManager.class,
696 new CachedServiceFetcher<CameraManager>() {
697 @Override
698 public CameraManager createService(ContextImpl ctx) {
699 return new CameraManager(ctx);
700 }});
701
702 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
703 new CachedServiceFetcher<LauncherApps>() {
704 @Override
705 public LauncherApps createService(ContextImpl ctx) {
706 return new LauncherApps(ctx);
707 }});
708
709 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
710 new CachedServiceFetcher<RestrictionsManager>() {
711 @Override
712 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
713 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
714 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
715 return new RestrictionsManager(ctx, service);
716 }});
717
718 registerService(Context.PRINT_SERVICE, PrintManager.class,
719 new CachedServiceFetcher<PrintManager>() {
720 @Override
721 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
722 IPrintManager service = null;
723 // If the feature not present, don't try to look up every time
724 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
725 service = IPrintManager.Stub.asInterface(ServiceManager
726 .getServiceOrThrow(Context.PRINT_SERVICE));
727 }
Justin Klaassen4d01eea2018-04-03 23:21:57 -0400728 final int userId = ctx.getUserId();
729 final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid);
730 return new PrintManager(ctx.getOuterContext(), service, userId, appId);
Justin Klaassen10d07c82017-09-15 17:58:39 -0400731 }});
732
733 registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
734 new CachedServiceFetcher<CompanionDeviceManager>() {
735 @Override
736 public CompanionDeviceManager createService(ContextImpl ctx)
737 throws ServiceNotFoundException {
738 ICompanionDeviceManager service = null;
739 // If the feature not present, don't try to look up every time
740 if (ctx.getPackageManager().hasSystemFeature(
741 PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) {
742 service = ICompanionDeviceManager.Stub.asInterface(
743 ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE));
744 }
745 return new CompanionDeviceManager(service, ctx.getOuterContext());
746 }});
747
748 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
749 new CachedServiceFetcher<ConsumerIrManager>() {
750 @Override
751 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
752 return new ConsumerIrManager(ctx);
753 }});
754
755 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
756 new CachedServiceFetcher<MediaSessionManager>() {
757 @Override
758 public MediaSessionManager createService(ContextImpl ctx) {
759 return new MediaSessionManager(ctx);
760 }});
761
762 registerService(Context.TRUST_SERVICE, TrustManager.class,
763 new StaticServiceFetcher<TrustManager>() {
764 @Override
765 public TrustManager createService() throws ServiceNotFoundException {
766 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
767 return new TrustManager(b);
768 }});
769
770 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
771 new CachedServiceFetcher<FingerprintManager>() {
772 @Override
773 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
774 final IBinder binder;
775 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
776 binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
777 } else {
778 binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
779 }
780 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
781 return new FingerprintManager(ctx.getOuterContext(), service);
782 }});
783
784 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
Justin Klaassen4d01eea2018-04-03 23:21:57 -0400785 new CachedServiceFetcher<TvInputManager>() {
Justin Klaassen10d07c82017-09-15 17:58:39 -0400786 @Override
Justin Klaassen4d01eea2018-04-03 23:21:57 -0400787 public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Justin Klaassen10d07c82017-09-15 17:58:39 -0400788 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
789 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
Justin Klaassen4d01eea2018-04-03 23:21:57 -0400790 return new TvInputManager(service, ctx.getUserId());
Justin Klaassen10d07c82017-09-15 17:58:39 -0400791 }});
792
793 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
794 new CachedServiceFetcher<NetworkScoreManager>() {
795 @Override
796 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
797 return new NetworkScoreManager(ctx);
798 }});
799
800 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
801 new CachedServiceFetcher<UsageStatsManager>() {
802 @Override
803 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
804 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
805 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
806 return new UsageStatsManager(ctx.getOuterContext(), service);
807 }});
808
809 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
810 new CachedServiceFetcher<NetworkStatsManager>() {
811 @Override
812 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
813 return new NetworkStatsManager(ctx.getOuterContext());
814 }});
815
816 registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
817 new StaticServiceFetcher<JobScheduler>() {
818 @Override
819 public JobScheduler createService() throws ServiceNotFoundException {
820 IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE);
821 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
822 }});
823
824 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
825 new StaticServiceFetcher<PersistentDataBlockManager>() {
826 @Override
827 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
828 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
829 IPersistentDataBlockService persistentDataBlockService =
830 IPersistentDataBlockService.Stub.asInterface(b);
831 if (persistentDataBlockService != null) {
832 return new PersistentDataBlockManager(persistentDataBlockService);
833 } else {
834 // not supported
835 return null;
836 }
837 }});
838
839 registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
840 new StaticServiceFetcher<OemLockManager>() {
841 @Override
842 public OemLockManager createService() throws ServiceNotFoundException {
843 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
844 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
845 if (oemLockService != null) {
846 return new OemLockManager(oemLockService);
847 } else {
848 // not supported
849 return null;
850 }
851 }});
852
853 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
854 new CachedServiceFetcher<MediaProjectionManager>() {
855 @Override
856 public MediaProjectionManager createService(ContextImpl ctx) {
857 return new MediaProjectionManager(ctx);
858 }});
859
860 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
861 new CachedServiceFetcher<AppWidgetManager>() {
862 @Override
863 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
864 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
865 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
866 }});
867
868 registerService(Context.MIDI_SERVICE, MidiManager.class,
869 new CachedServiceFetcher<MidiManager>() {
870 @Override
871 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
872 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
873 return new MidiManager(IMidiManager.Stub.asInterface(b));
874 }});
875
876 registerService(Context.RADIO_SERVICE, RadioManager.class,
877 new CachedServiceFetcher<RadioManager>() {
878 @Override
879 public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
880 return new RadioManager(ctx);
881 }});
882
883 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
884 new CachedServiceFetcher<HardwarePropertiesManager>() {
885 @Override
886 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
887 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
888 IHardwarePropertiesManager service =
889 IHardwarePropertiesManager.Stub.asInterface(b);
890 return new HardwarePropertiesManager(ctx, service);
891 }});
892
893 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
894 new CachedServiceFetcher<SoundTriggerManager>() {
895 @Override
896 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
897 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
898 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
899 }});
900
901 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
902 new CachedServiceFetcher<ShortcutManager>() {
903 @Override
904 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
905 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
906 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
907 }});
908
Justin Klaassen6a65f2d2017-11-17 16:38:15 -0500909 registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class,
910 new CachedServiceFetcher<NetworkWatchlistManager>() {
911 @Override
912 public NetworkWatchlistManager createService(ContextImpl ctx)
913 throws ServiceNotFoundException {
914 IBinder b =
915 ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE);
916 return new NetworkWatchlistManager(ctx,
917 INetworkWatchlistManager.Stub.asInterface(b));
918 }});
919
Justin Klaassen10d07c82017-09-15 17:58:39 -0400920 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
921 new CachedServiceFetcher<SystemHealthManager>() {
922 @Override
923 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
924 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
925 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
926 }});
927
928 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
929 new CachedServiceFetcher<ContextHubManager>() {
930 @Override
931 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
932 return new ContextHubManager(ctx.getOuterContext(),
933 ctx.mMainThread.getHandler().getLooper());
934 }});
935
936 registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
937 new CachedServiceFetcher<IncidentManager>() {
938 @Override
939 public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
940 return new IncidentManager(ctx);
941 }});
942
943 registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
944 new CachedServiceFetcher<AutofillManager>() {
945 @Override
946 public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
947 // Get the services without throwing as this is an optional feature
948 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
949 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
950 return new AutofillManager(ctx.getOuterContext(), service);
951 }});
952
953 registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
954 @Override
955 public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
956 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
957 return new VrManager(IVrManager.Stub.asInterface(b));
958 }
959 });
960
961 registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
962 new CachedServiceFetcher<RulesManager>() {
963 @Override
964 public RulesManager createService(ContextImpl ctx) {
965 return new RulesManager(ctx.getOuterContext());
966 }});
Justin Klaassen6a65f2d2017-11-17 16:38:15 -0500967
968 registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class,
969 new CachedServiceFetcher<CrossProfileApps>() {
970 @Override
971 public CrossProfileApps createService(ContextImpl ctx)
972 throws ServiceNotFoundException {
973 IBinder b = ServiceManager.getServiceOrThrow(
974 Context.CROSS_PROFILE_APPS_SERVICE);
975 return new CrossProfileApps(ctx.getOuterContext(),
976 ICrossProfileApps.Stub.asInterface(b));
977 }
978 });
Justin Klaassen98fe7812018-01-03 13:39:41 -0500979
980 registerService(Context.SLICE_SERVICE, SliceManager.class,
981 new CachedServiceFetcher<SliceManager>() {
982 @Override
983 public SliceManager createService(ContextImpl ctx)
984 throws ServiceNotFoundException {
985 return new SliceManager(ctx.getOuterContext(),
986 ctx.mMainThread.getHandler());
987 }
988 });
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400989
990 registerService(Context.DEVICE_IDLE_CONTROLLER, DeviceIdleManager.class,
991 new CachedServiceFetcher<DeviceIdleManager>() {
992 @Override
993 public DeviceIdleManager createService(ContextImpl ctx)
994 throws ServiceNotFoundException {
995 IDeviceIdleController service = IDeviceIdleController.Stub.asInterface(
996 ServiceManager.getServiceOrThrow(
997 Context.DEVICE_IDLE_CONTROLLER));
998 return new DeviceIdleManager(ctx.getOuterContext(), service);
999 }});
Justin Klaassen10d07c82017-09-15 17:58:39 -04001000 }
1001
1002 /**
1003 * Creates an array which is used to cache per-Context service instances.
1004 */
1005 public static Object[] createServiceCache() {
1006 return new Object[sServiceCacheSize];
1007 }
1008
1009 /**
1010 * Gets a system service from a given context.
1011 */
1012 public static Object getSystemService(ContextImpl ctx, String name) {
1013 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
1014 return fetcher != null ? fetcher.getService(ctx) : null;
1015 }
1016
1017 /**
1018 * Gets the name of the system-level service that is represented by the specified class.
1019 */
1020 public static String getSystemServiceName(Class<?> serviceClass) {
1021 return SYSTEM_SERVICE_NAMES.get(serviceClass);
1022 }
1023
1024 /**
1025 * Statically registers a system service with the context.
1026 * This method must be called during static initialization only.
1027 */
1028 private static <T> void registerService(String serviceName, Class<T> serviceClass,
1029 ServiceFetcher<T> serviceFetcher) {
1030 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
1031 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
1032 }
1033
1034 /**
1035 * Base interface for classes that fetch services.
1036 * These objects must only be created during static initialization.
1037 */
1038 static abstract interface ServiceFetcher<T> {
1039 T getService(ContextImpl ctx);
1040 }
1041
1042 /**
1043 * Override this class when the system service constructor needs a
1044 * ContextImpl and should be cached and retained by that context.
1045 */
1046 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
1047 private final int mCacheIndex;
1048
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001049 CachedServiceFetcher() {
1050 // Note this class must be instantiated only by the static initializer of the
1051 // outer class (SystemServiceRegistry), which already does the synchronization,
1052 // so bare access to sServiceCacheSize is okay here.
Justin Klaassen10d07c82017-09-15 17:58:39 -04001053 mCacheIndex = sServiceCacheSize++;
1054 }
1055
1056 @Override
1057 @SuppressWarnings("unchecked")
1058 public final T getService(ContextImpl ctx) {
1059 final Object[] cache = ctx.mServiceCache;
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001060 final int[] gates = ctx.mServiceInitializationStateArray;
Justin Klaassen4d01eea2018-04-03 23:21:57 -04001061
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001062 for (;;) {
1063 boolean doInitialize = false;
1064 synchronized (cache) {
1065 // Return it if we already have a cached instance.
1066 T service = (T) cache[mCacheIndex];
1067 if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) {
1068 return service;
1069 }
Justin Klaassen4d01eea2018-04-03 23:21:57 -04001070
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001071 // If we get here, there's no cached instance.
Justin Klaassen4d01eea2018-04-03 23:21:57 -04001072
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001073 // Grr... if gate is STATE_READY, then this means we initialized the service
1074 // once but someone cleared it.
1075 // We start over from STATE_UNINITIALIZED.
1076 if (gates[mCacheIndex] == ContextImpl.STATE_READY) {
1077 gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED;
1078 }
1079
1080 // It's possible for multiple threads to get here at the same time, so
1081 // use the "gate" to make sure only the first thread will call createService().
1082
1083 // At this point, the gate must be either UNINITIALIZED or INITIALIZING.
1084 if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) {
1085 doInitialize = true;
1086 gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING;
1087 }
Justin Klaassen4d01eea2018-04-03 23:21:57 -04001088 }
1089
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001090 if (doInitialize) {
1091 // Only the first thread gets here.
Justin Klaassen4d01eea2018-04-03 23:21:57 -04001092
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001093 T service = null;
1094 @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND;
Justin Klaassen10d07c82017-09-15 17:58:39 -04001095 try {
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001096 // This thread is the first one to get here. Instantiate the service
1097 // *without* the cache lock held.
Justin Klaassen10d07c82017-09-15 17:58:39 -04001098 service = createService(ctx);
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001099 newState = ContextImpl.STATE_READY;
Justin Klaassen4d01eea2018-04-03 23:21:57 -04001100
Justin Klaassen10d07c82017-09-15 17:58:39 -04001101 } catch (ServiceNotFoundException e) {
1102 onServiceNotFound(e);
Justin Klaassen4d01eea2018-04-03 23:21:57 -04001103
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001104 } finally {
1105 synchronized (cache) {
1106 cache[mCacheIndex] = service;
1107 gates[mCacheIndex] = newState;
1108 cache.notifyAll();
1109 }
1110 }
1111 return service;
1112 }
1113 // The other threads will wait for the first thread to call notifyAll(),
1114 // and go back to the top and retry.
1115 synchronized (cache) {
1116 while (gates[mCacheIndex] < ContextImpl.STATE_READY) {
1117 try {
1118 cache.wait();
1119 } catch (InterruptedException e) {
1120 Log.w(TAG, "getService() interrupted");
1121 Thread.currentThread().interrupt();
1122 return null;
1123 }
Justin Klaassen4d01eea2018-04-03 23:21:57 -04001124 }
1125 }
Justin Klaassen4d01eea2018-04-03 23:21:57 -04001126 }
Justin Klaassen10d07c82017-09-15 17:58:39 -04001127 }
1128
1129 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
1130 }
1131
1132 /**
1133 * Override this class when the system service does not need a ContextImpl
1134 * and should be cached and retained process-wide.
1135 */
1136 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
1137 private T mCachedInstance;
1138
1139 @Override
1140 public final T getService(ContextImpl ctx) {
1141 synchronized (StaticServiceFetcher.this) {
1142 if (mCachedInstance == null) {
1143 try {
1144 mCachedInstance = createService();
1145 } catch (ServiceNotFoundException e) {
1146 onServiceNotFound(e);
1147 }
1148 }
1149 return mCachedInstance;
1150 }
1151 }
1152
1153 public abstract T createService() throws ServiceNotFoundException;
1154 }
1155
1156 /**
1157 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
1158 * creating the service for the first time, passes it the application context of the creating
1159 * application.
1160 *
1161 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
1162 * case where multiple application components each have their own ConnectivityManager object.
1163 */
1164 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
1165 private T mCachedInstance;
1166
1167 @Override
1168 public final T getService(ContextImpl ctx) {
1169 synchronized (StaticApplicationContextServiceFetcher.this) {
1170 if (mCachedInstance == null) {
1171 Context appContext = ctx.getApplicationContext();
1172 // If the application context is null, we're either in the system process or
1173 // it's the application context very early in app initialization. In both these
1174 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
1175 // to the service. http://b/27532714 .
1176 try {
1177 mCachedInstance = createService(appContext != null ? appContext : ctx);
1178 } catch (ServiceNotFoundException e) {
1179 onServiceNotFound(e);
1180 }
1181 }
1182 return mCachedInstance;
1183 }
1184 }
1185
1186 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
1187 }
1188
1189 public static void onServiceNotFound(ServiceNotFoundException e) {
1190 // We're mostly interested in tracking down long-lived core system
1191 // components that might stumble if they obtain bad references; just
1192 // emit a tidy log message for normal apps
1193 if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1194 Log.wtf(TAG, e.getMessage(), e);
1195 } else {
1196 Log.w(TAG, e.getMessage());
1197 }
1198 }
1199}