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