blob: 6c0b69c8afdb3f5876fef2a1b7ede6db333e0855 [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;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080040import android.content.pm.IShortcutService;
Jeff Brown6e539312015-02-24 18:53:21 -080041import android.content.pm.LauncherApps;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080042import android.content.pm.ShortcutManager;
Jeff Brown6e539312015-02-24 18:53:21 -080043import android.content.res.Resources;
44import android.hardware.ConsumerIrManager;
45import android.hardware.ISerialManager;
46import android.hardware.SensorManager;
47import android.hardware.SerialManager;
48import android.hardware.SystemSensorManager;
49import android.hardware.camera2.CameraManager;
50import android.hardware.display.DisplayManager;
51import android.hardware.hdmi.HdmiControlManager;
52import android.hardware.hdmi.IHdmiControlService;
53import android.hardware.input.InputManager;
54import android.hardware.usb.IUsbManager;
55import android.hardware.usb.UsbManager;
Eric Laurent2035ac82015-03-05 15:18:44 -080056import android.hardware.radio.RadioManager;
Jeff Brown6e539312015-02-24 18:53:21 -080057import android.location.CountryDetector;
58import android.location.ICountryDetector;
59import android.location.ILocationManager;
60import android.location.LocationManager;
61import android.media.AudioManager;
62import android.media.MediaRouter;
63import android.media.midi.IMidiManager;
64import android.media.midi.MidiManager;
65import android.media.projection.MediaProjectionManager;
66import android.media.session.MediaSessionManager;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080067import android.media.soundtrigger.SoundTriggerManager;
Jeff Brown6e539312015-02-24 18:53:21 -080068import android.media.tv.ITvInputManager;
69import android.media.tv.TvInputManager;
70import android.net.ConnectivityManager;
71import android.net.EthernetManager;
72import android.net.IConnectivityManager;
73import android.net.IEthernetManager;
74import android.net.INetworkPolicyManager;
75import android.net.NetworkPolicyManager;
76import android.net.NetworkScoreManager;
77import android.net.nsd.INsdManager;
78import android.net.nsd.NsdManager;
79import android.net.wifi.IRttManager;
80import android.net.wifi.IWifiManager;
81import android.net.wifi.IWifiScanner;
82import android.net.wifi.RttManager;
83import android.net.wifi.WifiManager;
84import android.net.wifi.WifiScanner;
Etan Cohen20d329b2015-09-29 13:49:02 -070085import android.net.wifi.nan.IWifiNanManager;
86import android.net.wifi.nan.WifiNanManager;
Jeff Brown6e539312015-02-24 18:53:21 -080087import android.net.wifi.p2p.IWifiP2pManager;
88import android.net.wifi.p2p.WifiP2pManager;
Jeff Brown6e539312015-02-24 18:53:21 -080089import android.nfc.NfcManager;
90import android.os.BatteryManager;
91import android.os.DropBoxManager;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010092import android.os.HardwarePropertiesManager;
Jeff Brown6e539312015-02-24 18:53:21 -080093import android.os.IBinder;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010094import android.os.IHardwarePropertiesManager;
Jeff Brown6e539312015-02-24 18:53:21 -080095import android.os.IPowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -080096import android.os.IRecoverySystem;
Jeff Brown6e539312015-02-24 18:53:21 -080097import android.os.IUserManager;
98import android.os.PowerManager;
99import android.os.Process;
Tao Baoe8a403d2015-12-31 07:44:55 -0800100import android.os.RecoverySystem;
Jeff Brown6e539312015-02-24 18:53:21 -0800101import android.os.ServiceManager;
102import android.os.SystemVibrator;
103import android.os.UserHandle;
104import android.os.UserManager;
105import android.os.Vibrator;
Joe Onorato713fec82016-03-04 10:34:02 -0800106import android.os.health.SystemHealthManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800107import android.os.storage.StorageManager;
108import android.print.IPrintManager;
109import android.print.PrintManager;
Jim Millerebbf2052015-03-31 17:24:34 -0700110import android.hardware.fingerprint.FingerprintManager;
111import android.hardware.fingerprint.IFingerprintService;
Jeff Brown6e539312015-02-24 18:53:21 -0800112import android.service.persistentdata.IPersistentDataBlockService;
113import android.service.persistentdata.PersistentDataBlockManager;
114import android.telecom.TelecomManager;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800115import android.telephony.CarrierConfigManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800116import android.telephony.SubscriptionManager;
117import android.telephony.TelephonyManager;
118import android.util.Log;
119import android.view.ContextThemeWrapper;
120import android.view.LayoutInflater;
Adam Powell6711f3b2015-05-06 15:57:09 -0700121import com.android.internal.policy.PhoneLayoutInflater;
Jeff Brown6e539312015-02-24 18:53:21 -0800122import android.view.WindowManager;
123import android.view.WindowManagerImpl;
124import android.view.accessibility.AccessibilityManager;
125import android.view.accessibility.CaptioningManager;
126import android.view.inputmethod.InputMethodManager;
127import android.view.textservice.TextServicesManager;
128
129import java.util.HashMap;
130
131/**
132 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
133 * Used by {@link ContextImpl}.
134 */
135final class SystemServiceRegistry {
136 private final static String TAG = "SystemServiceRegistry";
137
138 // Service registry information.
139 // This information is never changed once static initialization has completed.
140 private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
141 new HashMap<Class<?>, String>();
142 private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
143 new HashMap<String, ServiceFetcher<?>>();
144 private static int sServiceCacheSize;
145
146 // Not instantiable.
147 private SystemServiceRegistry() { }
148
149 static {
150 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
151 new CachedServiceFetcher<AccessibilityManager>() {
152 @Override
153 public AccessibilityManager createService(ContextImpl ctx) {
154 return AccessibilityManager.getInstance(ctx);
155 }});
156
157 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
158 new CachedServiceFetcher<CaptioningManager>() {
159 @Override
160 public CaptioningManager createService(ContextImpl ctx) {
161 return new CaptioningManager(ctx);
162 }});
163
164 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
165 new CachedServiceFetcher<AccountManager>() {
166 @Override
167 public AccountManager createService(ContextImpl ctx) {
168 IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE);
169 IAccountManager service = IAccountManager.Stub.asInterface(b);
170 return new AccountManager(ctx, service);
171 }});
172
173 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
174 new CachedServiceFetcher<ActivityManager>() {
175 @Override
176 public ActivityManager createService(ContextImpl ctx) {
177 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
178 }});
179
180 registerService(Context.ALARM_SERVICE, AlarmManager.class,
181 new CachedServiceFetcher<AlarmManager>() {
182 @Override
183 public AlarmManager createService(ContextImpl ctx) {
184 IBinder b = ServiceManager.getService(Context.ALARM_SERVICE);
185 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
186 return new AlarmManager(service, ctx);
187 }});
188
189 registerService(Context.AUDIO_SERVICE, AudioManager.class,
190 new CachedServiceFetcher<AudioManager>() {
191 @Override
192 public AudioManager createService(ContextImpl ctx) {
193 return new AudioManager(ctx);
194 }});
195
196 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
197 new CachedServiceFetcher<MediaRouter>() {
198 @Override
199 public MediaRouter createService(ContextImpl ctx) {
200 return new MediaRouter(ctx);
201 }});
202
203 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
204 new CachedServiceFetcher<BluetoothManager>() {
205 @Override
206 public BluetoothManager createService(ContextImpl ctx) {
207 return new BluetoothManager(ctx);
208 }});
209
210 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
211 new StaticServiceFetcher<HdmiControlManager>() {
212 @Override
213 public HdmiControlManager createService() {
214 IBinder b = ServiceManager.getService(Context.HDMI_CONTROL_SERVICE);
215 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
216 }});
217
218 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
219 new CachedServiceFetcher<ClipboardManager>() {
220 @Override
221 public ClipboardManager createService(ContextImpl ctx) {
222 return new ClipboardManager(ctx.getOuterContext(),
223 ctx.mMainThread.getHandler());
224 }});
225
226 // The clipboard service moved to a new package. If someone asks for the old
227 // interface by class then we want to redirect over to the new interface instead
228 // (which extends it).
229 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
230
231 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
Lorenzo Colitticf959772016-02-29 16:07:37 +0900232 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800233 @Override
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900234 public ConnectivityManager createService(Context context) {
Jeff Brown6e539312015-02-24 18:53:21 -0800235 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900236 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
237 return new ConnectivityManager(context, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800238 }});
239
240 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
241 new StaticServiceFetcher<CountryDetector>() {
242 @Override
243 public CountryDetector createService() {
244 IBinder b = ServiceManager.getService(Context.COUNTRY_DETECTOR);
245 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
246 }});
247
248 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
249 new CachedServiceFetcher<DevicePolicyManager>() {
250 @Override
251 public DevicePolicyManager createService(ContextImpl ctx) {
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700252 return DevicePolicyManager.create(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800253 }});
254
255 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
256 new CachedServiceFetcher<DownloadManager>() {
257 @Override
258 public DownloadManager createService(ContextImpl ctx) {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700259 return new DownloadManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800260 }});
261
262 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
263 new StaticServiceFetcher<BatteryManager>() {
264 @Override
265 public BatteryManager createService() {
266 return new BatteryManager();
267 }});
268
269 registerService(Context.NFC_SERVICE, NfcManager.class,
270 new CachedServiceFetcher<NfcManager>() {
271 @Override
272 public NfcManager createService(ContextImpl ctx) {
273 return new NfcManager(ctx);
274 }});
275
276 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700277 new CachedServiceFetcher<DropBoxManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800278 @Override
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700279 public DropBoxManager createService(ContextImpl ctx) {
Jeff Brown6e539312015-02-24 18:53:21 -0800280 IBinder b = ServiceManager.getService(Context.DROPBOX_SERVICE);
281 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
282 if (service == null) {
283 // Don't return a DropBoxManager that will NPE upon use.
284 // This also avoids caching a broken DropBoxManager in
285 // getDropBoxManager during early boot, before the
286 // DROPBOX_SERVICE is registered.
287 return null;
288 }
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700289 return new DropBoxManager(ctx, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800290 }});
291
292 registerService(Context.INPUT_SERVICE, InputManager.class,
293 new StaticServiceFetcher<InputManager>() {
294 @Override
295 public InputManager createService() {
296 return InputManager.getInstance();
297 }});
298
299 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
300 new CachedServiceFetcher<DisplayManager>() {
301 @Override
302 public DisplayManager createService(ContextImpl ctx) {
303 return new DisplayManager(ctx.getOuterContext());
304 }});
305
306 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
307 new StaticServiceFetcher<InputMethodManager>() {
308 @Override
309 public InputMethodManager createService() {
310 return InputMethodManager.getInstance();
311 }});
312
313 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
314 new StaticServiceFetcher<TextServicesManager>() {
315 @Override
316 public TextServicesManager createService() {
317 return TextServicesManager.getInstance();
318 }});
319
320 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
321 new StaticServiceFetcher<KeyguardManager>() {
322 @Override
323 public KeyguardManager createService() {
324 return new KeyguardManager();
325 }});
326
327 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
328 new CachedServiceFetcher<LayoutInflater>() {
329 @Override
330 public LayoutInflater createService(ContextImpl ctx) {
331 return new PhoneLayoutInflater(ctx.getOuterContext());
332 }});
333
334 registerService(Context.LOCATION_SERVICE, LocationManager.class,
335 new CachedServiceFetcher<LocationManager>() {
336 @Override
337 public LocationManager createService(ContextImpl ctx) {
338 IBinder b = ServiceManager.getService(Context.LOCATION_SERVICE);
339 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
340 }});
341
342 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
Svet Ganov16a16892015-04-16 10:32:04 -0700343 new CachedServiceFetcher<NetworkPolicyManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800344 @Override
Svet Ganov16a16892015-04-16 10:32:04 -0700345 public NetworkPolicyManager createService(ContextImpl ctx) {
346 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
Jeff Brown6e539312015-02-24 18:53:21 -0800347 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE)));
348 }});
349
350 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
351 new CachedServiceFetcher<NotificationManager>() {
352 @Override
353 public NotificationManager createService(ContextImpl ctx) {
354 final Context outerContext = ctx.getOuterContext();
355 return new NotificationManager(
356 new ContextThemeWrapper(outerContext,
357 Resources.selectSystemTheme(0,
358 outerContext.getApplicationInfo().targetSdkVersion,
359 com.android.internal.R.style.Theme_Dialog,
360 com.android.internal.R.style.Theme_Holo_Dialog,
361 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
362 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
363 ctx.mMainThread.getHandler());
364 }});
365
366 registerService(Context.NSD_SERVICE, NsdManager.class,
367 new CachedServiceFetcher<NsdManager>() {
368 @Override
369 public NsdManager createService(ContextImpl ctx) {
370 IBinder b = ServiceManager.getService(Context.NSD_SERVICE);
371 INsdManager service = INsdManager.Stub.asInterface(b);
372 return new NsdManager(ctx.getOuterContext(), service);
373 }});
374
375 registerService(Context.POWER_SERVICE, PowerManager.class,
376 new CachedServiceFetcher<PowerManager>() {
377 @Override
378 public PowerManager createService(ContextImpl ctx) {
379 IBinder b = ServiceManager.getService(Context.POWER_SERVICE);
380 IPowerManager service = IPowerManager.Stub.asInterface(b);
381 if (service == null) {
382 Log.wtf(TAG, "Failed to get power manager service.");
383 }
384 return new PowerManager(ctx.getOuterContext(),
385 service, ctx.mMainThread.getHandler());
386 }});
387
Tao Baoe8a403d2015-12-31 07:44:55 -0800388 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
389 new CachedServiceFetcher<RecoverySystem>() {
390 @Override
391 public RecoverySystem createService(ContextImpl ctx) {
392 IBinder b = ServiceManager.getService(Context.RECOVERY_SERVICE);
393 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
394 if (service == null) {
395 Log.wtf(TAG, "Failed to get recovery service.");
396 }
397 return new RecoverySystem(service);
398 }});
399
Jeff Brown6e539312015-02-24 18:53:21 -0800400 registerService(Context.SEARCH_SERVICE, SearchManager.class,
401 new CachedServiceFetcher<SearchManager>() {
402 @Override
403 public SearchManager createService(ContextImpl ctx) {
404 return new SearchManager(ctx.getOuterContext(),
405 ctx.mMainThread.getHandler());
406 }});
407
408 registerService(Context.SENSOR_SERVICE, SensorManager.class,
409 new CachedServiceFetcher<SensorManager>() {
410 @Override
411 public SensorManager createService(ContextImpl ctx) {
412 return new SystemSensorManager(ctx.getOuterContext(),
413 ctx.mMainThread.getHandler().getLooper());
414 }});
415
416 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
417 new CachedServiceFetcher<StatusBarManager>() {
418 @Override
419 public StatusBarManager createService(ContextImpl ctx) {
420 return new StatusBarManager(ctx.getOuterContext());
421 }});
422
423 registerService(Context.STORAGE_SERVICE, StorageManager.class,
424 new CachedServiceFetcher<StorageManager>() {
425 @Override
426 public StorageManager createService(ContextImpl ctx) {
Jeff Sharkey48877892015-03-18 11:27:19 -0700427 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800428 }});
429
430 registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
431 new CachedServiceFetcher<TelephonyManager>() {
432 @Override
433 public TelephonyManager createService(ContextImpl ctx) {
434 return new TelephonyManager(ctx.getOuterContext());
435 }});
436
437 registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
438 new CachedServiceFetcher<SubscriptionManager>() {
439 @Override
440 public SubscriptionManager createService(ContextImpl ctx) {
441 return new SubscriptionManager(ctx.getOuterContext());
442 }});
443
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800444 registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
445 new CachedServiceFetcher<CarrierConfigManager>() {
446 @Override
447 public CarrierConfigManager createService(ContextImpl ctx) {
448 return new CarrierConfigManager();
449 }});
450
Jeff Brown6e539312015-02-24 18:53:21 -0800451 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
452 new CachedServiceFetcher<TelecomManager>() {
453 @Override
454 public TelecomManager createService(ContextImpl ctx) {
455 return new TelecomManager(ctx.getOuterContext());
456 }});
457
458 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
459 new CachedServiceFetcher<UiModeManager>() {
460 @Override
461 public UiModeManager createService(ContextImpl ctx) {
462 return new UiModeManager();
463 }});
464
465 registerService(Context.USB_SERVICE, UsbManager.class,
466 new CachedServiceFetcher<UsbManager>() {
467 @Override
468 public UsbManager createService(ContextImpl ctx) {
469 IBinder b = ServiceManager.getService(Context.USB_SERVICE);
470 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
471 }});
472
473 registerService(Context.SERIAL_SERVICE, SerialManager.class,
474 new CachedServiceFetcher<SerialManager>() {
475 @Override
476 public SerialManager createService(ContextImpl ctx) {
477 IBinder b = ServiceManager.getService(Context.SERIAL_SERVICE);
478 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
479 }});
480
481 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
482 new CachedServiceFetcher<Vibrator>() {
483 @Override
484 public Vibrator createService(ContextImpl ctx) {
485 return new SystemVibrator(ctx);
486 }});
487
488 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
489 new CachedServiceFetcher<WallpaperManager>() {
490 @Override
491 public WallpaperManager createService(ContextImpl ctx) {
492 return new WallpaperManager(ctx.getOuterContext(),
493 ctx.mMainThread.getHandler());
494 }});
495
496 registerService(Context.WIFI_SERVICE, WifiManager.class,
497 new CachedServiceFetcher<WifiManager>() {
498 @Override
499 public WifiManager createService(ContextImpl ctx) {
500 IBinder b = ServiceManager.getService(Context.WIFI_SERVICE);
501 IWifiManager service = IWifiManager.Stub.asInterface(b);
502 return new WifiManager(ctx.getOuterContext(), service);
503 }});
504
Jeff Brown6e539312015-02-24 18:53:21 -0800505 registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
506 new StaticServiceFetcher<WifiP2pManager>() {
507 @Override
508 public WifiP2pManager createService() {
509 IBinder b = ServiceManager.getService(Context.WIFI_P2P_SERVICE);
510 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
511 return new WifiP2pManager(service);
512 }});
513
Etan Cohen20d329b2015-09-29 13:49:02 -0700514 registerService(Context.WIFI_NAN_SERVICE, WifiNanManager.class,
515 new StaticServiceFetcher<WifiNanManager>() {
516 @Override
517 public WifiNanManager createService() {
518 IBinder b = ServiceManager.getService(Context.WIFI_NAN_SERVICE);
519 IWifiNanManager service = IWifiNanManager.Stub.asInterface(b);
520 if (service == null) {
521 return null;
522 }
523 return new WifiNanManager(service);
524 }});
525
Jeff Brown6e539312015-02-24 18:53:21 -0800526 registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
527 new CachedServiceFetcher<WifiScanner>() {
528 @Override
529 public WifiScanner createService(ContextImpl ctx) {
530 IBinder b = ServiceManager.getService(Context.WIFI_SCANNING_SERVICE);
531 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
532 return new WifiScanner(ctx.getOuterContext(), service);
533 }});
534
535 registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
536 new CachedServiceFetcher<RttManager>() {
537 @Override
538 public RttManager createService(ContextImpl ctx) {
539 IBinder b = ServiceManager.getService(Context.WIFI_RTT_SERVICE);
540 IRttManager service = IRttManager.Stub.asInterface(b);
541 return new RttManager(ctx.getOuterContext(), service);
542 }});
543
544 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
545 new CachedServiceFetcher<EthernetManager>() {
546 @Override
547 public EthernetManager createService(ContextImpl ctx) {
548 IBinder b = ServiceManager.getService(Context.ETHERNET_SERVICE);
549 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
550 return new EthernetManager(ctx.getOuterContext(), service);
551 }});
552
553 registerService(Context.WINDOW_SERVICE, WindowManager.class,
554 new CachedServiceFetcher<WindowManager>() {
555 @Override
556 public WindowManager createService(ContextImpl ctx) {
557 return new WindowManagerImpl(ctx.getDisplay());
558 }});
559
560 registerService(Context.USER_SERVICE, UserManager.class,
561 new CachedServiceFetcher<UserManager>() {
562 @Override
563 public UserManager createService(ContextImpl ctx) {
564 IBinder b = ServiceManager.getService(Context.USER_SERVICE);
565 IUserManager service = IUserManager.Stub.asInterface(b);
566 return new UserManager(ctx, service);
567 }});
568
569 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
570 new CachedServiceFetcher<AppOpsManager>() {
571 @Override
572 public AppOpsManager createService(ContextImpl ctx) {
573 IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE);
574 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
575 return new AppOpsManager(ctx, service);
576 }});
577
578 registerService(Context.CAMERA_SERVICE, CameraManager.class,
579 new CachedServiceFetcher<CameraManager>() {
580 @Override
581 public CameraManager createService(ContextImpl ctx) {
582 return new CameraManager(ctx);
583 }});
584
585 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
586 new CachedServiceFetcher<LauncherApps>() {
587 @Override
588 public LauncherApps createService(ContextImpl ctx) {
589 IBinder b = ServiceManager.getService(Context.LAUNCHER_APPS_SERVICE);
590 ILauncherApps service = ILauncherApps.Stub.asInterface(b);
591 return new LauncherApps(ctx, service);
592 }});
593
594 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
595 new CachedServiceFetcher<RestrictionsManager>() {
596 @Override
597 public RestrictionsManager createService(ContextImpl ctx) {
598 IBinder b = ServiceManager.getService(Context.RESTRICTIONS_SERVICE);
599 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
600 return new RestrictionsManager(ctx, service);
601 }});
602
603 registerService(Context.PRINT_SERVICE, PrintManager.class,
604 new CachedServiceFetcher<PrintManager>() {
605 @Override
606 public PrintManager createService(ContextImpl ctx) {
607 IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE);
608 IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
609 return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
610 UserHandle.getAppId(Process.myUid()));
611 }});
612
613 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
614 new CachedServiceFetcher<ConsumerIrManager>() {
615 @Override
616 public ConsumerIrManager createService(ContextImpl ctx) {
617 return new ConsumerIrManager(ctx);
618 }});
619
620 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
621 new CachedServiceFetcher<MediaSessionManager>() {
622 @Override
623 public MediaSessionManager createService(ContextImpl ctx) {
624 return new MediaSessionManager(ctx);
625 }});
626
627 registerService(Context.TRUST_SERVICE, TrustManager.class,
628 new StaticServiceFetcher<TrustManager>() {
629 @Override
630 public TrustManager createService() {
631 IBinder b = ServiceManager.getService(Context.TRUST_SERVICE);
632 return new TrustManager(b);
633 }});
634
635 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
636 new CachedServiceFetcher<FingerprintManager>() {
637 @Override
638 public FingerprintManager createService(ContextImpl ctx) {
639 IBinder binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
640 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
641 return new FingerprintManager(ctx.getOuterContext(), service);
642 }});
643
644 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
645 new StaticServiceFetcher<TvInputManager>() {
646 @Override
647 public TvInputManager createService() {
648 IBinder iBinder = ServiceManager.getService(Context.TV_INPUT_SERVICE);
649 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
650 return new TvInputManager(service, UserHandle.myUserId());
651 }});
652
653 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
654 new CachedServiceFetcher<NetworkScoreManager>() {
655 @Override
656 public NetworkScoreManager createService(ContextImpl ctx) {
657 return new NetworkScoreManager(ctx);
658 }});
659
660 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
661 new CachedServiceFetcher<UsageStatsManager>() {
662 @Override
663 public UsageStatsManager createService(ContextImpl ctx) {
664 IBinder iBinder = ServiceManager.getService(Context.USAGE_STATS_SERVICE);
665 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
666 return new UsageStatsManager(ctx.getOuterContext(), service);
667 }});
668
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000669 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
670 new CachedServiceFetcher<NetworkStatsManager>() {
671 @Override
672 public NetworkStatsManager createService(ContextImpl ctx) {
673 return new NetworkStatsManager(ctx.getOuterContext());
674 }});
675
Jeff Brown6e539312015-02-24 18:53:21 -0800676 registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
677 new StaticServiceFetcher<JobScheduler>() {
678 @Override
679 public JobScheduler createService() {
680 IBinder b = ServiceManager.getService(Context.JOB_SCHEDULER_SERVICE);
681 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
682 }});
683
684 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
685 new StaticServiceFetcher<PersistentDataBlockManager>() {
686 @Override
687 public PersistentDataBlockManager createService() {
688 IBinder b = ServiceManager.getService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
689 IPersistentDataBlockService persistentDataBlockService =
690 IPersistentDataBlockService.Stub.asInterface(b);
691 if (persistentDataBlockService != null) {
692 return new PersistentDataBlockManager(persistentDataBlockService);
693 } else {
694 // not supported
695 return null;
696 }
697 }});
698
699 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
700 new CachedServiceFetcher<MediaProjectionManager>() {
701 @Override
702 public MediaProjectionManager createService(ContextImpl ctx) {
703 return new MediaProjectionManager(ctx);
704 }});
705
706 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
707 new CachedServiceFetcher<AppWidgetManager>() {
708 @Override
709 public AppWidgetManager createService(ContextImpl ctx) {
710 IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE);
711 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
712 }});
713
714 registerService(Context.MIDI_SERVICE, MidiManager.class,
715 new CachedServiceFetcher<MidiManager>() {
716 @Override
717 public MidiManager createService(ContextImpl ctx) {
718 IBinder b = ServiceManager.getService(Context.MIDI_SERVICE);
Ricardo Garcia47abda52015-07-16 18:20:46 -0700719 if (b == null) {
720 return null;
721 }
Mike Lockwoode0a6ca62015-06-04 13:43:56 -0700722 return new MidiManager(IMidiManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800723 }});
Eric Laurent2035ac82015-03-05 15:18:44 -0800724
725 registerService(Context.RADIO_SERVICE, RadioManager.class,
726 new CachedServiceFetcher<RadioManager>() {
727 @Override
728 public RadioManager createService(ContextImpl ctx) {
729 return new RadioManager(ctx);
730 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800731
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100732 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
733 new CachedServiceFetcher<HardwarePropertiesManager>() {
734 @Override
735 public HardwarePropertiesManager createService(ContextImpl ctx) {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100736 IBinder b = ServiceManager.getService(Context.HARDWARE_PROPERTIES_SERVICE);
737 IHardwarePropertiesManager service =
738 IHardwarePropertiesManager.Stub.asInterface(b);
739 if (service == null) {
740 Log.wtf(TAG, "Failed to get hardwareproperties service.");
741 return null;
742 }
743 return new HardwarePropertiesManager(ctx, service);
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100744 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800745
746 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
747 new CachedServiceFetcher<SoundTriggerManager>() {
748 @Override
749 public SoundTriggerManager createService(ContextImpl ctx) {
750 IBinder b = ServiceManager.getService(Context.SOUND_TRIGGER_SERVICE);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800751 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
752 }});
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800753
754 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
755 new CachedServiceFetcher<ShortcutManager>() {
Makoto Onuki55046222016-03-08 10:49:47 -0800756 @Override
757 public ShortcutManager createService(ContextImpl ctx) {
758 IBinder b = ServiceManager.getService(Context.SHORTCUT_SERVICE);
759 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
760 }});
Joe Onorato713fec82016-03-04 10:34:02 -0800761
762 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
763 new CachedServiceFetcher<SystemHealthManager>() {
764 @Override
765 public SystemHealthManager createService(ContextImpl ctx) {
766 return new SystemHealthManager();
767 }});
Jeff Brown6e539312015-02-24 18:53:21 -0800768 }
769
770 /**
771 * Creates an array which is used to cache per-Context service instances.
772 */
773 public static Object[] createServiceCache() {
774 return new Object[sServiceCacheSize];
775 }
776
777 /**
778 * Gets a system service from a given context.
779 */
780 public static Object getSystemService(ContextImpl ctx, String name) {
781 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
782 return fetcher != null ? fetcher.getService(ctx) : null;
783 }
784
785 /**
Paul McLeane3383cc2015-05-08 11:41:20 -0700786 * Gets the name of the system-level service that is represented by the specified class.
Jeff Brown6e539312015-02-24 18:53:21 -0800787 */
788 public static String getSystemServiceName(Class<?> serviceClass) {
789 return SYSTEM_SERVICE_NAMES.get(serviceClass);
790 }
791
792 /**
793 * Statically registers a system service with the context.
794 * This method must be called during static initialization only.
795 */
796 private static <T> void registerService(String serviceName, Class<T> serviceClass,
797 ServiceFetcher<T> serviceFetcher) {
798 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
799 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
800 }
801
802 /**
803 * Base interface for classes that fetch services.
804 * These objects must only be created during static initialization.
805 */
806 static abstract interface ServiceFetcher<T> {
807 T getService(ContextImpl ctx);
808 }
809
810 /**
811 * Override this class when the system service constructor needs a
812 * ContextImpl and should be cached and retained by that context.
813 */
814 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
815 private final int mCacheIndex;
816
817 public CachedServiceFetcher() {
818 mCacheIndex = sServiceCacheSize++;
819 }
820
821 @Override
822 @SuppressWarnings("unchecked")
823 public final T getService(ContextImpl ctx) {
824 final Object[] cache = ctx.mServiceCache;
825 synchronized (cache) {
826 // Fetch or create the service.
827 Object service = cache[mCacheIndex];
828 if (service == null) {
829 service = createService(ctx);
830 cache[mCacheIndex] = service;
831 }
832 return (T)service;
833 }
834 }
835
836 public abstract T createService(ContextImpl ctx);
837 }
838
839 /**
840 * Override this class when the system service does not need a ContextImpl
841 * and should be cached and retained process-wide.
842 */
843 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
844 private T mCachedInstance;
845
846 @Override
847 public final T getService(ContextImpl unused) {
848 synchronized (StaticServiceFetcher.this) {
849 if (mCachedInstance == null) {
850 mCachedInstance = createService();
851 }
852 return mCachedInstance;
853 }
854 }
855
856 public abstract T createService();
857 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900858
859 /**
Lorenzo Colitticf959772016-02-29 16:07:37 +0900860 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
861 * creating the service for the first time, passes it the application context of the creating
862 * application.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900863 *
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900864 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
865 * case where multiple application components each have their own ConnectivityManager object.
866 */
Lorenzo Colitticf959772016-02-29 16:07:37 +0900867 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900868 private T mCachedInstance;
869
870 @Override
871 public final T getService(ContextImpl ctx) {
Lorenzo Colitticf959772016-02-29 16:07:37 +0900872 synchronized (StaticApplicationContextServiceFetcher.this) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900873 if (mCachedInstance == null) {
Lorenzo Colitticf959772016-02-29 16:07:37 +0900874 mCachedInstance = createService(ctx.getApplicationContext());
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900875 }
876 return mCachedInstance;
877 }
878 }
879
880 public abstract T createService(Context applicationContext);
881 }
882
Jeff Brown6e539312015-02-24 18:53:21 -0800883}