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