blob: e446700317ab4be825a769b795cc47bc75573baf [file] [log] [blame]
Jeff Brown6e539312015-02-24 18:53:21 -08001/*
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 com.android.internal.app.IAppOpsService;
20import com.android.internal.appwidget.IAppWidgetService;
21import com.android.internal.os.IDropBoxManagerService;
22
23import android.accounts.AccountManager;
24import android.accounts.IAccountManager;
25import android.app.admin.DevicePolicyManager;
26import android.app.job.IJobScheduler;
27import android.app.job.JobScheduler;
28import android.app.trust.TrustManager;
29import android.app.usage.IUsageStatsManager;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000030import android.app.usage.NetworkStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080031import android.app.usage.UsageStatsManager;
32import android.appwidget.AppWidgetManager;
33import android.bluetooth.BluetoothManager;
34import android.content.ClipboardManager;
35import android.content.Context;
36import android.content.IRestrictionsManager;
37import android.content.RestrictionsManager;
38import android.content.pm.ILauncherApps;
39import android.content.pm.LauncherApps;
40import android.content.res.Resources;
41import android.hardware.ConsumerIrManager;
42import android.hardware.ISerialManager;
43import android.hardware.SensorManager;
44import android.hardware.SerialManager;
45import android.hardware.SystemSensorManager;
46import android.hardware.camera2.CameraManager;
47import android.hardware.display.DisplayManager;
48import android.hardware.hdmi.HdmiControlManager;
49import android.hardware.hdmi.IHdmiControlService;
50import android.hardware.input.InputManager;
51import android.hardware.usb.IUsbManager;
52import android.hardware.usb.UsbManager;
Eric Laurent2035ac82015-03-05 15:18:44 -080053import android.hardware.radio.RadioManager;
Jeff Brown6e539312015-02-24 18:53:21 -080054import android.location.CountryDetector;
55import android.location.ICountryDetector;
56import android.location.ILocationManager;
57import android.location.LocationManager;
Paul McLeana33be212015-02-20 07:52:45 -080058import android.media.AudioDevicesManager;
Jeff Brown6e539312015-02-24 18:53:21 -080059import android.media.AudioManager;
60import android.media.MediaRouter;
61import android.media.midi.IMidiManager;
62import android.media.midi.MidiManager;
63import android.media.projection.MediaProjectionManager;
64import android.media.session.MediaSessionManager;
65import android.media.tv.ITvInputManager;
66import android.media.tv.TvInputManager;
67import android.net.ConnectivityManager;
68import android.net.EthernetManager;
69import android.net.IConnectivityManager;
70import android.net.IEthernetManager;
71import android.net.INetworkPolicyManager;
72import android.net.NetworkPolicyManager;
73import android.net.NetworkScoreManager;
74import android.net.nsd.INsdManager;
75import android.net.nsd.NsdManager;
76import android.net.wifi.IRttManager;
77import android.net.wifi.IWifiManager;
78import android.net.wifi.IWifiScanner;
79import android.net.wifi.RttManager;
80import android.net.wifi.WifiManager;
81import android.net.wifi.WifiScanner;
82import android.net.wifi.p2p.IWifiP2pManager;
83import android.net.wifi.p2p.WifiP2pManager;
84import android.net.wifi.passpoint.IWifiPasspointManager;
85import android.net.wifi.passpoint.WifiPasspointManager;
86import android.nfc.NfcManager;
87import android.os.BatteryManager;
88import android.os.DropBoxManager;
89import android.os.IBinder;
90import android.os.IPowerManager;
91import android.os.IUserManager;
92import android.os.PowerManager;
93import android.os.Process;
94import android.os.RemoteException;
95import android.os.ServiceManager;
96import android.os.SystemVibrator;
97import android.os.UserHandle;
98import android.os.UserManager;
99import android.os.Vibrator;
100import android.os.storage.StorageManager;
101import android.print.IPrintManager;
102import android.print.PrintManager;
Jim Millerebbf2052015-03-31 17:24:34 -0700103import android.hardware.fingerprint.FingerprintManager;
104import android.hardware.fingerprint.IFingerprintService;
Jeff Brown6e539312015-02-24 18:53:21 -0800105import android.service.persistentdata.IPersistentDataBlockService;
106import android.service.persistentdata.PersistentDataBlockManager;
107import android.telecom.TelecomManager;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800108import android.telephony.CarrierConfigManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800109import android.telephony.SubscriptionManager;
110import android.telephony.TelephonyManager;
111import android.util.Log;
112import android.view.ContextThemeWrapper;
113import android.view.LayoutInflater;
114import android.view.PhoneLayoutInflater;
115import android.view.WindowManager;
116import android.view.WindowManagerImpl;
117import android.view.accessibility.AccessibilityManager;
118import android.view.accessibility.CaptioningManager;
119import android.view.inputmethod.InputMethodManager;
120import android.view.textservice.TextServicesManager;
121
122import java.util.HashMap;
123
124/**
125 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
126 * Used by {@link ContextImpl}.
127 */
128final class SystemServiceRegistry {
129 private final static String TAG = "SystemServiceRegistry";
130
131 // Service registry information.
132 // This information is never changed once static initialization has completed.
133 private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
134 new HashMap<Class<?>, String>();
135 private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
136 new HashMap<String, ServiceFetcher<?>>();
137 private static int sServiceCacheSize;
138
139 // Not instantiable.
140 private SystemServiceRegistry() { }
141
142 static {
143 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
144 new CachedServiceFetcher<AccessibilityManager>() {
145 @Override
146 public AccessibilityManager createService(ContextImpl ctx) {
147 return AccessibilityManager.getInstance(ctx);
148 }});
149
150 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
151 new CachedServiceFetcher<CaptioningManager>() {
152 @Override
153 public CaptioningManager createService(ContextImpl ctx) {
154 return new CaptioningManager(ctx);
155 }});
156
157 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
158 new CachedServiceFetcher<AccountManager>() {
159 @Override
160 public AccountManager createService(ContextImpl ctx) {
161 IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE);
162 IAccountManager service = IAccountManager.Stub.asInterface(b);
163 return new AccountManager(ctx, service);
164 }});
165
166 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
167 new CachedServiceFetcher<ActivityManager>() {
168 @Override
169 public ActivityManager createService(ContextImpl ctx) {
170 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
171 }});
172
173 registerService(Context.ALARM_SERVICE, AlarmManager.class,
174 new CachedServiceFetcher<AlarmManager>() {
175 @Override
176 public AlarmManager createService(ContextImpl ctx) {
177 IBinder b = ServiceManager.getService(Context.ALARM_SERVICE);
178 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
179 return new AlarmManager(service, ctx);
180 }});
181
182 registerService(Context.AUDIO_SERVICE, AudioManager.class,
183 new CachedServiceFetcher<AudioManager>() {
184 @Override
185 public AudioManager createService(ContextImpl ctx) {
186 return new AudioManager(ctx);
187 }});
188
189 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
190 new CachedServiceFetcher<MediaRouter>() {
191 @Override
192 public MediaRouter createService(ContextImpl ctx) {
193 return new MediaRouter(ctx);
194 }});
195
196 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
197 new CachedServiceFetcher<BluetoothManager>() {
198 @Override
199 public BluetoothManager createService(ContextImpl ctx) {
200 return new BluetoothManager(ctx);
201 }});
202
203 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
204 new StaticServiceFetcher<HdmiControlManager>() {
205 @Override
206 public HdmiControlManager createService() {
207 IBinder b = ServiceManager.getService(Context.HDMI_CONTROL_SERVICE);
208 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
209 }});
210
211 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
212 new CachedServiceFetcher<ClipboardManager>() {
213 @Override
214 public ClipboardManager createService(ContextImpl ctx) {
215 return new ClipboardManager(ctx.getOuterContext(),
216 ctx.mMainThread.getHandler());
217 }});
218
219 // The clipboard service moved to a new package. If someone asks for the old
220 // interface by class then we want to redirect over to the new interface instead
221 // (which extends it).
222 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
223
224 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
225 new StaticServiceFetcher<ConnectivityManager>() {
226 @Override
227 public ConnectivityManager createService() {
228 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
229 return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b));
230 }});
231
232 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
233 new StaticServiceFetcher<CountryDetector>() {
234 @Override
235 public CountryDetector createService() {
236 IBinder b = ServiceManager.getService(Context.COUNTRY_DETECTOR);
237 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
238 }});
239
240 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
241 new CachedServiceFetcher<DevicePolicyManager>() {
242 @Override
243 public DevicePolicyManager createService(ContextImpl ctx) {
244 return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler());
245 }});
246
247 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
248 new CachedServiceFetcher<DownloadManager>() {
249 @Override
250 public DownloadManager createService(ContextImpl ctx) {
251 return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
252 }});
253
254 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
255 new StaticServiceFetcher<BatteryManager>() {
256 @Override
257 public BatteryManager createService() {
258 return new BatteryManager();
259 }});
260
261 registerService(Context.NFC_SERVICE, NfcManager.class,
262 new CachedServiceFetcher<NfcManager>() {
263 @Override
264 public NfcManager createService(ContextImpl ctx) {
265 return new NfcManager(ctx);
266 }});
267
268 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
269 new StaticServiceFetcher<DropBoxManager>() {
270 @Override
271 public DropBoxManager createService() {
272 IBinder b = ServiceManager.getService(Context.DROPBOX_SERVICE);
273 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
274 if (service == null) {
275 // Don't return a DropBoxManager that will NPE upon use.
276 // This also avoids caching a broken DropBoxManager in
277 // getDropBoxManager during early boot, before the
278 // DROPBOX_SERVICE is registered.
279 return null;
280 }
281 return new DropBoxManager(service);
282 }});
283
284 registerService(Context.INPUT_SERVICE, InputManager.class,
285 new StaticServiceFetcher<InputManager>() {
286 @Override
287 public InputManager createService() {
288 return InputManager.getInstance();
289 }});
290
291 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
292 new CachedServiceFetcher<DisplayManager>() {
293 @Override
294 public DisplayManager createService(ContextImpl ctx) {
295 return new DisplayManager(ctx.getOuterContext());
296 }});
297
298 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
299 new StaticServiceFetcher<InputMethodManager>() {
300 @Override
301 public InputMethodManager createService() {
302 return InputMethodManager.getInstance();
303 }});
304
305 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
306 new StaticServiceFetcher<TextServicesManager>() {
307 @Override
308 public TextServicesManager createService() {
309 return TextServicesManager.getInstance();
310 }});
311
312 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
313 new StaticServiceFetcher<KeyguardManager>() {
314 @Override
315 public KeyguardManager createService() {
316 return new KeyguardManager();
317 }});
318
319 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
320 new CachedServiceFetcher<LayoutInflater>() {
321 @Override
322 public LayoutInflater createService(ContextImpl ctx) {
323 return new PhoneLayoutInflater(ctx.getOuterContext());
324 }});
325
326 registerService(Context.LOCATION_SERVICE, LocationManager.class,
327 new CachedServiceFetcher<LocationManager>() {
328 @Override
329 public LocationManager createService(ContextImpl ctx) {
330 IBinder b = ServiceManager.getService(Context.LOCATION_SERVICE);
331 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
332 }});
333
334 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
335 new StaticServiceFetcher<NetworkPolicyManager>() {
336 @Override
337 public NetworkPolicyManager createService() {
338 return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface(
339 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE)));
340 }});
341
342 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
343 new CachedServiceFetcher<NotificationManager>() {
344 @Override
345 public NotificationManager createService(ContextImpl ctx) {
346 final Context outerContext = ctx.getOuterContext();
347 return new NotificationManager(
348 new ContextThemeWrapper(outerContext,
349 Resources.selectSystemTheme(0,
350 outerContext.getApplicationInfo().targetSdkVersion,
351 com.android.internal.R.style.Theme_Dialog,
352 com.android.internal.R.style.Theme_Holo_Dialog,
353 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
354 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
355 ctx.mMainThread.getHandler());
356 }});
357
358 registerService(Context.NSD_SERVICE, NsdManager.class,
359 new CachedServiceFetcher<NsdManager>() {
360 @Override
361 public NsdManager createService(ContextImpl ctx) {
362 IBinder b = ServiceManager.getService(Context.NSD_SERVICE);
363 INsdManager service = INsdManager.Stub.asInterface(b);
364 return new NsdManager(ctx.getOuterContext(), service);
365 }});
366
367 registerService(Context.POWER_SERVICE, PowerManager.class,
368 new CachedServiceFetcher<PowerManager>() {
369 @Override
370 public PowerManager createService(ContextImpl ctx) {
371 IBinder b = ServiceManager.getService(Context.POWER_SERVICE);
372 IPowerManager service = IPowerManager.Stub.asInterface(b);
373 if (service == null) {
374 Log.wtf(TAG, "Failed to get power manager service.");
375 }
376 return new PowerManager(ctx.getOuterContext(),
377 service, ctx.mMainThread.getHandler());
378 }});
379
380 registerService(Context.SEARCH_SERVICE, SearchManager.class,
381 new CachedServiceFetcher<SearchManager>() {
382 @Override
383 public SearchManager createService(ContextImpl ctx) {
384 return new SearchManager(ctx.getOuterContext(),
385 ctx.mMainThread.getHandler());
386 }});
387
388 registerService(Context.SENSOR_SERVICE, SensorManager.class,
389 new CachedServiceFetcher<SensorManager>() {
390 @Override
391 public SensorManager createService(ContextImpl ctx) {
392 return new SystemSensorManager(ctx.getOuterContext(),
393 ctx.mMainThread.getHandler().getLooper());
394 }});
395
396 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
397 new CachedServiceFetcher<StatusBarManager>() {
398 @Override
399 public StatusBarManager createService(ContextImpl ctx) {
400 return new StatusBarManager(ctx.getOuterContext());
401 }});
402
403 registerService(Context.STORAGE_SERVICE, StorageManager.class,
404 new CachedServiceFetcher<StorageManager>() {
405 @Override
406 public StorageManager createService(ContextImpl ctx) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700407 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800408 }});
409
410 registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
411 new CachedServiceFetcher<TelephonyManager>() {
412 @Override
413 public TelephonyManager createService(ContextImpl ctx) {
414 return new TelephonyManager(ctx.getOuterContext());
415 }});
416
417 registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
418 new CachedServiceFetcher<SubscriptionManager>() {
419 @Override
420 public SubscriptionManager createService(ContextImpl ctx) {
421 return new SubscriptionManager(ctx.getOuterContext());
422 }});
423
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800424 registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
425 new CachedServiceFetcher<CarrierConfigManager>() {
426 @Override
427 public CarrierConfigManager createService(ContextImpl ctx) {
428 return new CarrierConfigManager();
429 }});
430
Jeff Brown6e539312015-02-24 18:53:21 -0800431 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
432 new CachedServiceFetcher<TelecomManager>() {
433 @Override
434 public TelecomManager createService(ContextImpl ctx) {
435 return new TelecomManager(ctx.getOuterContext());
436 }});
437
438 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
439 new CachedServiceFetcher<UiModeManager>() {
440 @Override
441 public UiModeManager createService(ContextImpl ctx) {
442 return new UiModeManager();
443 }});
444
445 registerService(Context.USB_SERVICE, UsbManager.class,
446 new CachedServiceFetcher<UsbManager>() {
447 @Override
448 public UsbManager createService(ContextImpl ctx) {
449 IBinder b = ServiceManager.getService(Context.USB_SERVICE);
450 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
451 }});
452
453 registerService(Context.SERIAL_SERVICE, SerialManager.class,
454 new CachedServiceFetcher<SerialManager>() {
455 @Override
456 public SerialManager createService(ContextImpl ctx) {
457 IBinder b = ServiceManager.getService(Context.SERIAL_SERVICE);
458 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
459 }});
460
461 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
462 new CachedServiceFetcher<Vibrator>() {
463 @Override
464 public Vibrator createService(ContextImpl ctx) {
465 return new SystemVibrator(ctx);
466 }});
467
468 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
469 new CachedServiceFetcher<WallpaperManager>() {
470 @Override
471 public WallpaperManager createService(ContextImpl ctx) {
472 return new WallpaperManager(ctx.getOuterContext(),
473 ctx.mMainThread.getHandler());
474 }});
475
476 registerService(Context.WIFI_SERVICE, WifiManager.class,
477 new CachedServiceFetcher<WifiManager>() {
478 @Override
479 public WifiManager createService(ContextImpl ctx) {
480 IBinder b = ServiceManager.getService(Context.WIFI_SERVICE);
481 IWifiManager service = IWifiManager.Stub.asInterface(b);
482 return new WifiManager(ctx.getOuterContext(), service);
483 }});
484
485 registerService(Context.WIFI_PASSPOINT_SERVICE, WifiPasspointManager.class,
486 new CachedServiceFetcher<WifiPasspointManager>() {
487 @Override
488 public WifiPasspointManager createService(ContextImpl ctx) {
489 IBinder b = ServiceManager.getService(Context.WIFI_PASSPOINT_SERVICE);
490 IWifiPasspointManager service = IWifiPasspointManager.Stub.asInterface(b);
491 return new WifiPasspointManager(ctx.getOuterContext(), service);
492 }});
493
494 registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
495 new StaticServiceFetcher<WifiP2pManager>() {
496 @Override
497 public WifiP2pManager createService() {
498 IBinder b = ServiceManager.getService(Context.WIFI_P2P_SERVICE);
499 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
500 return new WifiP2pManager(service);
501 }});
502
503 registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
504 new CachedServiceFetcher<WifiScanner>() {
505 @Override
506 public WifiScanner createService(ContextImpl ctx) {
507 IBinder b = ServiceManager.getService(Context.WIFI_SCANNING_SERVICE);
508 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
509 return new WifiScanner(ctx.getOuterContext(), service);
510 }});
511
512 registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
513 new CachedServiceFetcher<RttManager>() {
514 @Override
515 public RttManager createService(ContextImpl ctx) {
516 IBinder b = ServiceManager.getService(Context.WIFI_RTT_SERVICE);
517 IRttManager service = IRttManager.Stub.asInterface(b);
518 return new RttManager(ctx.getOuterContext(), service);
519 }});
520
521 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
522 new CachedServiceFetcher<EthernetManager>() {
523 @Override
524 public EthernetManager createService(ContextImpl ctx) {
525 IBinder b = ServiceManager.getService(Context.ETHERNET_SERVICE);
526 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
527 return new EthernetManager(ctx.getOuterContext(), service);
528 }});
529
530 registerService(Context.WINDOW_SERVICE, WindowManager.class,
531 new CachedServiceFetcher<WindowManager>() {
532 @Override
533 public WindowManager createService(ContextImpl ctx) {
534 return new WindowManagerImpl(ctx.getDisplay());
535 }});
536
537 registerService(Context.USER_SERVICE, UserManager.class,
538 new CachedServiceFetcher<UserManager>() {
539 @Override
540 public UserManager createService(ContextImpl ctx) {
541 IBinder b = ServiceManager.getService(Context.USER_SERVICE);
542 IUserManager service = IUserManager.Stub.asInterface(b);
543 return new UserManager(ctx, service);
544 }});
545
546 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
547 new CachedServiceFetcher<AppOpsManager>() {
548 @Override
549 public AppOpsManager createService(ContextImpl ctx) {
550 IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE);
551 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
552 return new AppOpsManager(ctx, service);
553 }});
554
555 registerService(Context.CAMERA_SERVICE, CameraManager.class,
556 new CachedServiceFetcher<CameraManager>() {
557 @Override
558 public CameraManager createService(ContextImpl ctx) {
559 return new CameraManager(ctx);
560 }});
561
562 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
563 new CachedServiceFetcher<LauncherApps>() {
564 @Override
565 public LauncherApps createService(ContextImpl ctx) {
566 IBinder b = ServiceManager.getService(Context.LAUNCHER_APPS_SERVICE);
567 ILauncherApps service = ILauncherApps.Stub.asInterface(b);
568 return new LauncherApps(ctx, service);
569 }});
570
571 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
572 new CachedServiceFetcher<RestrictionsManager>() {
573 @Override
574 public RestrictionsManager createService(ContextImpl ctx) {
575 IBinder b = ServiceManager.getService(Context.RESTRICTIONS_SERVICE);
576 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
577 return new RestrictionsManager(ctx, service);
578 }});
579
580 registerService(Context.PRINT_SERVICE, PrintManager.class,
581 new CachedServiceFetcher<PrintManager>() {
582 @Override
583 public PrintManager createService(ContextImpl ctx) {
584 IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE);
585 IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
586 return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
587 UserHandle.getAppId(Process.myUid()));
588 }});
589
590 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
591 new CachedServiceFetcher<ConsumerIrManager>() {
592 @Override
593 public ConsumerIrManager createService(ContextImpl ctx) {
594 return new ConsumerIrManager(ctx);
595 }});
596
597 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
598 new CachedServiceFetcher<MediaSessionManager>() {
599 @Override
600 public MediaSessionManager createService(ContextImpl ctx) {
601 return new MediaSessionManager(ctx);
602 }});
603
604 registerService(Context.TRUST_SERVICE, TrustManager.class,
605 new StaticServiceFetcher<TrustManager>() {
606 @Override
607 public TrustManager createService() {
608 IBinder b = ServiceManager.getService(Context.TRUST_SERVICE);
609 return new TrustManager(b);
610 }});
611
612 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
613 new CachedServiceFetcher<FingerprintManager>() {
614 @Override
615 public FingerprintManager createService(ContextImpl ctx) {
616 IBinder binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
617 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
618 return new FingerprintManager(ctx.getOuterContext(), service);
619 }});
620
621 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
622 new StaticServiceFetcher<TvInputManager>() {
623 @Override
624 public TvInputManager createService() {
625 IBinder iBinder = ServiceManager.getService(Context.TV_INPUT_SERVICE);
626 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
627 return new TvInputManager(service, UserHandle.myUserId());
628 }});
629
630 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
631 new CachedServiceFetcher<NetworkScoreManager>() {
632 @Override
633 public NetworkScoreManager createService(ContextImpl ctx) {
634 return new NetworkScoreManager(ctx);
635 }});
636
637 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
638 new CachedServiceFetcher<UsageStatsManager>() {
639 @Override
640 public UsageStatsManager createService(ContextImpl ctx) {
641 IBinder iBinder = ServiceManager.getService(Context.USAGE_STATS_SERVICE);
642 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
643 return new UsageStatsManager(ctx.getOuterContext(), service);
644 }});
645
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000646 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
647 new CachedServiceFetcher<NetworkStatsManager>() {
648 @Override
649 public NetworkStatsManager createService(ContextImpl ctx) {
650 return new NetworkStatsManager(ctx.getOuterContext());
651 }});
652
Jeff Brown6e539312015-02-24 18:53:21 -0800653 registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
654 new StaticServiceFetcher<JobScheduler>() {
655 @Override
656 public JobScheduler createService() {
657 IBinder b = ServiceManager.getService(Context.JOB_SCHEDULER_SERVICE);
658 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
659 }});
660
661 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
662 new StaticServiceFetcher<PersistentDataBlockManager>() {
663 @Override
664 public PersistentDataBlockManager createService() {
665 IBinder b = ServiceManager.getService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
666 IPersistentDataBlockService persistentDataBlockService =
667 IPersistentDataBlockService.Stub.asInterface(b);
668 if (persistentDataBlockService != null) {
669 return new PersistentDataBlockManager(persistentDataBlockService);
670 } else {
671 // not supported
672 return null;
673 }
674 }});
675
676 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
677 new CachedServiceFetcher<MediaProjectionManager>() {
678 @Override
679 public MediaProjectionManager createService(ContextImpl ctx) {
680 return new MediaProjectionManager(ctx);
681 }});
682
683 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
684 new CachedServiceFetcher<AppWidgetManager>() {
685 @Override
686 public AppWidgetManager createService(ContextImpl ctx) {
687 IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE);
688 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
689 }});
690
691 registerService(Context.MIDI_SERVICE, MidiManager.class,
692 new CachedServiceFetcher<MidiManager>() {
693 @Override
694 public MidiManager createService(ContextImpl ctx) {
695 IBinder b = ServiceManager.getService(Context.MIDI_SERVICE);
696 return new MidiManager(ctx, IMidiManager.Stub.asInterface(b));
697 }});
Eric Laurent2035ac82015-03-05 15:18:44 -0800698
699 registerService(Context.RADIO_SERVICE, RadioManager.class,
700 new CachedServiceFetcher<RadioManager>() {
701 @Override
702 public RadioManager createService(ContextImpl ctx) {
703 return new RadioManager(ctx);
704 }});
Paul McLeana33be212015-02-20 07:52:45 -0800705
706 registerService(Context.AUDIO_DEVICES_SERVICE, AudioDevicesManager.class,
707 new CachedServiceFetcher<AudioDevicesManager>() {
708 @Override
709 public AudioDevicesManager createService(ContextImpl ctx) {
710 return new AudioDevicesManager(ctx);
711 }});
Jeff Brown6e539312015-02-24 18:53:21 -0800712 }
713
714 /**
715 * Creates an array which is used to cache per-Context service instances.
716 */
717 public static Object[] createServiceCache() {
718 return new Object[sServiceCacheSize];
719 }
720
721 /**
722 * Gets a system service from a given context.
723 */
724 public static Object getSystemService(ContextImpl ctx, String name) {
725 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
726 return fetcher != null ? fetcher.getService(ctx) : null;
727 }
728
729 /**
730 * Gets the name of the system-level service that is represented by the specified class.
731 */
732 public static String getSystemServiceName(Class<?> serviceClass) {
733 return SYSTEM_SERVICE_NAMES.get(serviceClass);
734 }
735
736 /**
737 * Statically registers a system service with the context.
738 * This method must be called during static initialization only.
739 */
740 private static <T> void registerService(String serviceName, Class<T> serviceClass,
741 ServiceFetcher<T> serviceFetcher) {
742 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
743 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
744 }
745
746 /**
747 * Base interface for classes that fetch services.
748 * These objects must only be created during static initialization.
749 */
750 static abstract interface ServiceFetcher<T> {
751 T getService(ContextImpl ctx);
752 }
753
754 /**
755 * Override this class when the system service constructor needs a
756 * ContextImpl and should be cached and retained by that context.
757 */
758 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
759 private final int mCacheIndex;
760
761 public CachedServiceFetcher() {
762 mCacheIndex = sServiceCacheSize++;
763 }
764
765 @Override
766 @SuppressWarnings("unchecked")
767 public final T getService(ContextImpl ctx) {
768 final Object[] cache = ctx.mServiceCache;
769 synchronized (cache) {
770 // Fetch or create the service.
771 Object service = cache[mCacheIndex];
772 if (service == null) {
773 service = createService(ctx);
774 cache[mCacheIndex] = service;
775 }
776 return (T)service;
777 }
778 }
779
780 public abstract T createService(ContextImpl ctx);
781 }
782
783 /**
784 * Override this class when the system service does not need a ContextImpl
785 * and should be cached and retained process-wide.
786 */
787 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
788 private T mCachedInstance;
789
790 @Override
791 public final T getService(ContextImpl unused) {
792 synchronized (StaticServiceFetcher.this) {
793 if (mCachedInstance == null) {
794 mCachedInstance = createService();
795 }
796 return mCachedInstance;
797 }
798 }
799
800 public abstract T createService();
801 }
802}