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