blob: 0404e80df9e2bd90c6f2ba13648062cb7273e60d [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
Jeff Brown6e539312015-02-24 18:53:21 -080019import android.accounts.AccountManager;
20import android.accounts.IAccountManager;
Makoto Onukid67070e2018-03-30 12:39:14 -070021import android.app.ContextImpl.ServiceInitializationState;
Jeff Brown6e539312015-02-24 18:53:21 -080022import android.app.admin.DevicePolicyManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060023import android.app.admin.IDevicePolicyManager;
Jeff Brown6e539312015-02-24 18:53:21 -080024import android.app.job.IJobScheduler;
25import android.app.job.JobScheduler;
Hai Zhang4ef21d02018-11-09 14:43:51 -080026import android.app.role.RoleManager;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040027import android.app.slice.SliceManager;
Neil Fullerfeeee682018-05-30 14:35:24 +010028import android.app.timedetector.TimeDetector;
Neil Fuller328532a2017-03-16 18:32:21 +000029import android.app.timezone.RulesManager;
Neil Fuller7fb88c32018-06-19 12:53:47 +010030import android.app.timezonedetector.TimeZoneDetector;
Jeff Brown6e539312015-02-24 18:53:21 -080031import android.app.trust.TrustManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070032import android.app.usage.IStorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080033import android.app.usage.IUsageStatsManager;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000034import android.app.usage.NetworkStatsManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070035import android.app.usage.StorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080036import android.app.usage.UsageStatsManager;
37import android.appwidget.AppWidgetManager;
38import android.bluetooth.BluetoothManager;
Eugene Susla6ed45d82017-01-22 13:52:51 -080039import android.companion.CompanionDeviceManager;
40import android.companion.ICompanionDeviceManager;
Jeff Brown6e539312015-02-24 18:53:21 -080041import android.content.ClipboardManager;
42import android.content.Context;
43import android.content.IRestrictionsManager;
44import android.content.RestrictionsManager;
Tony Makb0d22622018-01-18 12:49:49 +000045import android.content.pm.CrossProfileApps;
46import android.content.pm.ICrossProfileApps;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080047import android.content.pm.IShortcutService;
Jeff Brown6e539312015-02-24 18:53:21 -080048import android.content.pm.LauncherApps;
Svet Ganovec7daa52017-06-30 12:02:59 -070049import android.content.pm.PackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080050import android.content.pm.ShortcutManager;
Jeff Brown6e539312015-02-24 18:53:21 -080051import android.content.res.Resources;
Kenny Rootf74bfde2018-01-18 15:42:48 -080052import android.debug.AdbManager;
53import android.debug.IAdbManager;
Jeff Brown6e539312015-02-24 18:53:21 -080054import android.hardware.ConsumerIrManager;
55import android.hardware.ISerialManager;
56import android.hardware.SensorManager;
57import android.hardware.SerialManager;
58import android.hardware.SystemSensorManager;
Kevin Chyn05c21502018-09-18 13:07:19 -070059import android.hardware.biometrics.BiometricManager;
60import android.hardware.biometrics.IBiometricService;
Jeff Brown6e539312015-02-24 18:53:21 -080061import android.hardware.camera2.CameraManager;
Christine Franks39b03112018-07-03 14:46:07 -070062import android.hardware.display.ColorDisplayManager;
Jeff Brown6e539312015-02-24 18:53:21 -080063import android.hardware.display.DisplayManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020064import android.hardware.face.FaceManager;
65import android.hardware.face.IFaceService;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060066import android.hardware.fingerprint.FingerprintManager;
67import android.hardware.fingerprint.IFingerprintService;
Jeff Brown6e539312015-02-24 18:53:21 -080068import android.hardware.hdmi.HdmiControlManager;
69import android.hardware.hdmi.IHdmiControlService;
70import android.hardware.input.InputManager;
Kevin Chyn51676d22018-11-05 18:00:43 -080071import android.hardware.iris.IIrisService;
Felipe Lemee348dc32018-11-05 12:35:29 -080072import android.hardware.iris.IrisManager;
Peng Xu9ff7d222016-02-11 13:02:05 -080073import android.hardware.location.ContextHubManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060074import android.hardware.radio.RadioManager;
Jeff Brown6e539312015-02-24 18:53:21 -080075import android.hardware.usb.IUsbManager;
76import android.hardware.usb.UsbManager;
77import android.location.CountryDetector;
78import android.location.ICountryDetector;
79import android.location.ILocationManager;
80import android.location.LocationManager;
81import android.media.AudioManager;
82import android.media.MediaRouter;
83import android.media.midi.IMidiManager;
84import android.media.midi.MidiManager;
85import android.media.projection.MediaProjectionManager;
86import android.media.session.MediaSessionManager;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080087import android.media.soundtrigger.SoundTriggerManager;
Jeff Brown6e539312015-02-24 18:53:21 -080088import android.media.tv.ITvInputManager;
89import android.media.tv.TvInputManager;
90import android.net.ConnectivityManager;
Paul Stewartbf8cbb052016-03-17 10:11:54 -070091import android.net.ConnectivityThread;
Jeff Brown6e539312015-02-24 18:53:21 -080092import android.net.EthernetManager;
93import android.net.IConnectivityManager;
94import android.net.IEthernetManager;
Nathan Harold28084d82017-03-01 18:55:06 -080095import android.net.IIpSecService;
Jeff Brown6e539312015-02-24 18:53:21 -080096import android.net.INetworkPolicyManager;
Nathan Harold28084d82017-03-01 18:55:06 -080097import android.net.IpSecManager;
Jeff Brown6e539312015-02-24 18:53:21 -080098import android.net.NetworkPolicyManager;
99import android.net.NetworkScoreManager;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100100import android.net.NetworkWatchlistManager;
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700101import android.net.lowpan.ILowpanManager;
102import android.net.lowpan.LowpanManager;
Etan Cohen17ba4722017-08-21 10:52:17 -0700103import android.net.nsd.INsdManager;
104import android.net.nsd.NsdManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800105import android.net.wifi.IWifiManager;
106import android.net.wifi.IWifiScanner;
107import android.net.wifi.RttManager;
108import android.net.wifi.WifiManager;
109import android.net.wifi.WifiScanner;
Etan Cohen04133272016-10-26 11:22:06 -0700110import android.net.wifi.aware.IWifiAwareManager;
111import android.net.wifi.aware.WifiAwareManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800112import android.net.wifi.p2p.IWifiP2pManager;
113import android.net.wifi.p2p.WifiP2pManager;
Etan Cohen17ba4722017-08-21 10:52:17 -0700114import android.net.wifi.rtt.IWifiRttManager;
115import android.net.wifi.rtt.WifiRttManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800116import android.nfc.NfcManager;
117import android.os.BatteryManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600118import android.os.BatteryStats;
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600119import android.os.Build;
Philip P. Moltmannf80809f2018-04-04 11:20:44 -0700120import android.os.DeviceIdleManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800121import android.os.DropBoxManager;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100122import android.os.HardwarePropertiesManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600123import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6e539312015-02-24 18:53:21 -0800124import android.os.IBinder;
Philip P. Moltmannf80809f2018-04-04 11:20:44 -0700125import android.os.IDeviceIdleController;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100126import android.os.IHardwarePropertiesManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800127import android.os.IPowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800128import android.os.IRecoverySystem;
Tao Bao07342dc2017-01-24 15:08:21 -0800129import android.os.ISystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800130import android.os.IUserManager;
Joe Onorato1754d742016-11-21 17:51:35 -0800131import android.os.IncidentManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800132import android.os.PowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800133import android.os.RecoverySystem;
Jeff Brown6e539312015-02-24 18:53:21 -0800134import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600135import android.os.ServiceManager.ServiceNotFoundException;
Tao Bao07342dc2017-01-24 15:08:21 -0800136import android.os.SystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800137import android.os.SystemVibrator;
138import android.os.UserHandle;
139import android.os.UserManager;
140import android.os.Vibrator;
Joe Onorato713fec82016-03-04 10:34:02 -0800141import android.os.health.SystemHealthManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800142import android.os.storage.StorageManager;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700143import android.permission.PermissionManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800144import android.print.IPrintManager;
145import android.print.PrintManager;
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000146import android.service.oemlock.IOemLockService;
147import android.service.oemlock.OemLockManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800148import android.service.persistentdata.IPersistentDataBlockService;
149import android.service.persistentdata.PersistentDataBlockManager;
Zak Cohen56345f42017-01-26 13:54:28 -0800150import android.service.vr.IVrManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800151import android.telecom.TelecomManager;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800152import android.telephony.CarrierConfigManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800153import android.telephony.SubscriptionManager;
154import android.telephony.TelephonyManager;
Holly Jiuyu Sun5c110242017-12-21 18:44:59 -0800155import android.telephony.euicc.EuiccCardManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100156import android.telephony.euicc.EuiccManager;
Sahin Caliskan9458ebc2018-10-31 13:23:29 -0700157import android.telephony.rcs.RcsManager;
Robert Benea1901a5b2018-05-15 16:36:07 -0700158import android.util.ArrayMap;
Jeff Brown6e539312015-02-24 18:53:21 -0800159import android.util.Log;
160import android.view.ContextThemeWrapper;
161import android.view.LayoutInflater;
Jeff Brown6e539312015-02-24 18:53:21 -0800162import android.view.WindowManager;
163import android.view.WindowManagerImpl;
164import android.view.accessibility.AccessibilityManager;
165import android.view.accessibility.CaptioningManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100166import android.view.autofill.AutofillManager;
167import android.view.autofill.IAutoFillManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800168import android.view.inputmethod.InputMethodManager;
Felipe Lemeecb08be2018-11-27 15:48:47 -0800169import android.view.intelligence.ContentCaptureManager;
Felipe Lemee348dc32018-11-05 12:35:29 -0800170import android.view.intelligence.IIntelligenceManager;
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000171import android.view.textclassifier.TextClassificationManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800172import android.view.textservice.TextServicesManager;
173
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600174import com.android.internal.app.IAppOpsService;
175import com.android.internal.app.IBatteryStats;
176import com.android.internal.app.ISoundTriggerService;
177import com.android.internal.appwidget.IAppWidgetService;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100178import com.android.internal.net.INetworkWatchlistManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600179import com.android.internal.os.IDropBoxManagerService;
180import com.android.internal.policy.PhoneLayoutInflater;
181
Robert Benea1901a5b2018-05-15 16:36:07 -0700182import java.util.Map;
Jeff Brown6e539312015-02-24 18:53:21 -0800183
184/**
185 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
186 * Used by {@link ContextImpl}.
187 */
188final class SystemServiceRegistry {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600189 private static final String TAG = "SystemServiceRegistry";
190
Jeff Brown6e539312015-02-24 18:53:21 -0800191 // Service registry information.
192 // This information is never changed once static initialization has completed.
Robert Benea1901a5b2018-05-15 16:36:07 -0700193 private static final Map<Class<?>, String> SYSTEM_SERVICE_NAMES =
194 new ArrayMap<Class<?>, String>();
195 private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
196 new ArrayMap<String, ServiceFetcher<?>>();
Jeff Brown6e539312015-02-24 18:53:21 -0800197 private static int sServiceCacheSize;
198
199 // Not instantiable.
200 private SystemServiceRegistry() { }
201
202 static {
Felipe Lemeecb08be2018-11-27 15:48:47 -0800203 //CHECKSTYLE:OFF IndentationCheck
Jeff Brown6e539312015-02-24 18:53:21 -0800204 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
205 new CachedServiceFetcher<AccessibilityManager>() {
206 @Override
207 public AccessibilityManager createService(ContextImpl ctx) {
208 return AccessibilityManager.getInstance(ctx);
209 }});
210
211 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
212 new CachedServiceFetcher<CaptioningManager>() {
213 @Override
214 public CaptioningManager createService(ContextImpl ctx) {
215 return new CaptioningManager(ctx);
216 }});
217
218 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
219 new CachedServiceFetcher<AccountManager>() {
220 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600221 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
222 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800223 IAccountManager service = IAccountManager.Stub.asInterface(b);
224 return new AccountManager(ctx, service);
225 }});
226
227 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
228 new CachedServiceFetcher<ActivityManager>() {
229 @Override
230 public ActivityManager createService(ContextImpl ctx) {
231 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
232 }});
233
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700234 registerService(Context.ACTIVITY_TASK_SERVICE, ActivityTaskManager.class,
235 new CachedServiceFetcher<ActivityTaskManager>() {
236 @Override
237 public ActivityTaskManager createService(ContextImpl ctx) {
238 return new ActivityTaskManager(
239 ctx.getOuterContext(), ctx.mMainThread.getHandler());
240 }});
241
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700242 registerService(Context.URI_GRANTS_SERVICE, UriGrantsManager.class,
243 new CachedServiceFetcher<UriGrantsManager>() {
244 @Override
245 public UriGrantsManager createService(ContextImpl ctx) {
246 return new UriGrantsManager(
247 ctx.getOuterContext(), ctx.mMainThread.getHandler());
248 }});
249
Jeff Brown6e539312015-02-24 18:53:21 -0800250 registerService(Context.ALARM_SERVICE, AlarmManager.class,
251 new CachedServiceFetcher<AlarmManager>() {
252 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600253 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
254 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800255 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
256 return new AlarmManager(service, ctx);
257 }});
258
259 registerService(Context.AUDIO_SERVICE, AudioManager.class,
260 new CachedServiceFetcher<AudioManager>() {
261 @Override
262 public AudioManager createService(ContextImpl ctx) {
263 return new AudioManager(ctx);
264 }});
265
266 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
267 new CachedServiceFetcher<MediaRouter>() {
268 @Override
269 public MediaRouter createService(ContextImpl ctx) {
270 return new MediaRouter(ctx);
271 }});
272
273 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
274 new CachedServiceFetcher<BluetoothManager>() {
275 @Override
276 public BluetoothManager createService(ContextImpl ctx) {
277 return new BluetoothManager(ctx);
278 }});
279
280 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
281 new StaticServiceFetcher<HdmiControlManager>() {
282 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600283 public HdmiControlManager createService() throws ServiceNotFoundException {
284 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800285 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
286 }});
287
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000288 registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000289 new CachedServiceFetcher<TextClassificationManager>() {
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000290 @Override
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000291 public TextClassificationManager createService(ContextImpl ctx) {
292 return new TextClassificationManager(ctx);
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000293 }});
294
Jeff Brown6e539312015-02-24 18:53:21 -0800295 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
296 new CachedServiceFetcher<ClipboardManager>() {
297 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600298 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800299 return new ClipboardManager(ctx.getOuterContext(),
300 ctx.mMainThread.getHandler());
301 }});
302
303 // The clipboard service moved to a new package. If someone asks for the old
304 // interface by class then we want to redirect over to the new interface instead
305 // (which extends it).
306 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
307
308 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
Lorenzo Colitticf959772016-02-29 16:07:37 +0900309 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800310 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600311 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
312 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900313 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
314 return new ConnectivityManager(context, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800315 }});
316
Nathan Harold28084d82017-03-01 18:55:06 -0800317 registerService(Context.IPSEC_SERVICE, IpSecManager.class,
Nathan Harold592dadb2018-03-15 18:06:06 -0700318 new CachedServiceFetcher<IpSecManager>() {
Nathan Harold28084d82017-03-01 18:55:06 -0800319 @Override
Nathan Harold592dadb2018-03-15 18:06:06 -0700320 public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nathan Harold28084d82017-03-01 18:55:06 -0800321 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
322 IIpSecService service = IIpSecService.Stub.asInterface(b);
Nathan Harold592dadb2018-03-15 18:06:06 -0700323 return new IpSecManager(ctx, service);
Nathan Harold28084d82017-03-01 18:55:06 -0800324 }});
325
Jeff Brown6e539312015-02-24 18:53:21 -0800326 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
327 new StaticServiceFetcher<CountryDetector>() {
328 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600329 public CountryDetector createService() throws ServiceNotFoundException {
330 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
Jeff Brown6e539312015-02-24 18:53:21 -0800331 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
332 }});
333
334 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
335 new CachedServiceFetcher<DevicePolicyManager>() {
336 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600337 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
338 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
339 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800340 }});
341
342 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
343 new CachedServiceFetcher<DownloadManager>() {
344 @Override
345 public DownloadManager createService(ContextImpl ctx) {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700346 return new DownloadManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800347 }});
348
349 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
Yifan Hongb0f13522017-10-31 15:48:50 -0700350 new CachedServiceFetcher<BatteryManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800351 @Override
Yifan Hongb0f13522017-10-31 15:48:50 -0700352 public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600353 IBatteryStats stats = IBatteryStats.Stub.asInterface(
354 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
355 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
356 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
Yifan Hongb0f13522017-10-31 15:48:50 -0700357 return new BatteryManager(ctx, stats, registrar);
Jeff Brown6e539312015-02-24 18:53:21 -0800358 }});
359
360 registerService(Context.NFC_SERVICE, NfcManager.class,
361 new CachedServiceFetcher<NfcManager>() {
362 @Override
363 public NfcManager createService(ContextImpl ctx) {
364 return new NfcManager(ctx);
365 }});
366
367 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700368 new CachedServiceFetcher<DropBoxManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800369 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600370 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
371 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800372 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700373 return new DropBoxManager(ctx, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800374 }});
375
376 registerService(Context.INPUT_SERVICE, InputManager.class,
377 new StaticServiceFetcher<InputManager>() {
378 @Override
379 public InputManager createService() {
380 return InputManager.getInstance();
381 }});
382
383 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
384 new CachedServiceFetcher<DisplayManager>() {
385 @Override
386 public DisplayManager createService(ContextImpl ctx) {
387 return new DisplayManager(ctx.getOuterContext());
388 }});
389
Christine Franks39b03112018-07-03 14:46:07 -0700390 registerService(Context.COLOR_DISPLAY_SERVICE, ColorDisplayManager.class,
391 new CachedServiceFetcher<ColorDisplayManager>() {
392 @Override
393 public ColorDisplayManager createService(ContextImpl ctx) {
394 return new ColorDisplayManager();
395 }
396 });
397
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800398 // InputMethodManager has its own cache strategy based on display id to support apps that
399 // still assume InputMethodManager is a per-process singleton and it's safe to directly
400 // access internal fields via reflection. Hence directly use ServiceFetcher instead of
401 // StaticServiceFetcher/CachedServiceFetcher.
Jeff Brown6e539312015-02-24 18:53:21 -0800402 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800403 new ServiceFetcher<InputMethodManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800404 @Override
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800405 public InputMethodManager getService(ContextImpl ctx) {
406 return InputMethodManager.forContext(ctx.getOuterContext());
Jeff Brown6e539312015-02-24 18:53:21 -0800407 }});
408
409 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
410 new StaticServiceFetcher<TextServicesManager>() {
411 @Override
412 public TextServicesManager createService() {
413 return TextServicesManager.getInstance();
414 }});
415
416 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
Nancy Zheng43f166d2016-11-18 18:15:09 -0800417 new CachedServiceFetcher<KeyguardManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800418 @Override
Nancy Zheng40cd8e42016-11-21 12:58:16 -0800419 public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nancy Zheng43f166d2016-11-18 18:15:09 -0800420 return new KeyguardManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800421 }});
422
423 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
424 new CachedServiceFetcher<LayoutInflater>() {
425 @Override
426 public LayoutInflater createService(ContextImpl ctx) {
427 return new PhoneLayoutInflater(ctx.getOuterContext());
428 }});
429
430 registerService(Context.LOCATION_SERVICE, LocationManager.class,
431 new CachedServiceFetcher<LocationManager>() {
432 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600433 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
434 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800435 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
436 }});
437
438 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
Svet Ganov16a16892015-04-16 10:32:04 -0700439 new CachedServiceFetcher<NetworkPolicyManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800440 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600441 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov16a16892015-04-16 10:32:04 -0700442 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600443 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
Jeff Brown6e539312015-02-24 18:53:21 -0800444 }});
445
446 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
447 new CachedServiceFetcher<NotificationManager>() {
448 @Override
449 public NotificationManager createService(ContextImpl ctx) {
450 final Context outerContext = ctx.getOuterContext();
451 return new NotificationManager(
452 new ContextThemeWrapper(outerContext,
453 Resources.selectSystemTheme(0,
454 outerContext.getApplicationInfo().targetSdkVersion,
455 com.android.internal.R.style.Theme_Dialog,
456 com.android.internal.R.style.Theme_Holo_Dialog,
457 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
458 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
459 ctx.mMainThread.getHandler());
460 }});
461
462 registerService(Context.NSD_SERVICE, NsdManager.class,
463 new CachedServiceFetcher<NsdManager>() {
464 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600465 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
466 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800467 INsdManager service = INsdManager.Stub.asInterface(b);
468 return new NsdManager(ctx.getOuterContext(), service);
469 }});
470
471 registerService(Context.POWER_SERVICE, PowerManager.class,
472 new CachedServiceFetcher<PowerManager>() {
473 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600474 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
475 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800476 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown6e539312015-02-24 18:53:21 -0800477 return new PowerManager(ctx.getOuterContext(),
478 service, ctx.mMainThread.getHandler());
479 }});
480
Tao Baoe8a403d2015-12-31 07:44:55 -0800481 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
482 new CachedServiceFetcher<RecoverySystem>() {
483 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600484 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
485 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
Tao Baoe8a403d2015-12-31 07:44:55 -0800486 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
Tao Baoe8a403d2015-12-31 07:44:55 -0800487 return new RecoverySystem(service);
488 }});
489
Jeff Brown6e539312015-02-24 18:53:21 -0800490 registerService(Context.SEARCH_SERVICE, SearchManager.class,
491 new CachedServiceFetcher<SearchManager>() {
492 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600493 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800494 return new SearchManager(ctx.getOuterContext(),
495 ctx.mMainThread.getHandler());
496 }});
497
498 registerService(Context.SENSOR_SERVICE, SensorManager.class,
499 new CachedServiceFetcher<SensorManager>() {
500 @Override
501 public SensorManager createService(ContextImpl ctx) {
502 return new SystemSensorManager(ctx.getOuterContext(),
503 ctx.mMainThread.getHandler().getLooper());
504 }});
505
David Chenadaf8b32017-11-03 15:42:08 -0700506 registerService(Context.STATS_MANAGER, StatsManager.class,
Jeff Sharkey6b649252018-04-16 09:50:22 -0600507 new CachedServiceFetcher<StatsManager>() {
508 @Override
509 public StatsManager createService(ContextImpl ctx) {
510 return new StatsManager(ctx.getOuterContext());
511 }});
David Chenadaf8b32017-11-03 15:42:08 -0700512
Jeff Brown6e539312015-02-24 18:53:21 -0800513 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
514 new CachedServiceFetcher<StatusBarManager>() {
515 @Override
516 public StatusBarManager createService(ContextImpl ctx) {
517 return new StatusBarManager(ctx.getOuterContext());
518 }});
519
520 registerService(Context.STORAGE_SERVICE, StorageManager.class,
521 new CachedServiceFetcher<StorageManager>() {
522 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600523 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey48877892015-03-18 11:27:19 -0700524 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800525 }});
526
Jeff Sharkeye8cece92017-01-04 11:33:33 -0700527 registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
528 new CachedServiceFetcher<StorageStatsManager>() {
529 @Override
530 public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
531 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
532 ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
533 return new StorageStatsManager(ctx, service);
534 }});
535
Tao Bao07342dc2017-01-24 15:08:21 -0800536 registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class,
537 new CachedServiceFetcher<SystemUpdateManager>() {
538 @Override
539 public SystemUpdateManager createService(ContextImpl ctx)
540 throws ServiceNotFoundException {
541 IBinder b = ServiceManager.getServiceOrThrow(
542 Context.SYSTEM_UPDATE_SERVICE);
543 ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b);
544 return new SystemUpdateManager(service);
545 }});
546
Jeff Brown6e539312015-02-24 18:53:21 -0800547 registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
548 new CachedServiceFetcher<TelephonyManager>() {
549 @Override
550 public TelephonyManager createService(ContextImpl ctx) {
551 return new TelephonyManager(ctx.getOuterContext());
552 }});
553
554 registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
555 new CachedServiceFetcher<SubscriptionManager>() {
556 @Override
Jeff Sharkey717f52f2018-01-04 16:04:11 -0700557 public SubscriptionManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800558 return new SubscriptionManager(ctx.getOuterContext());
559 }});
560
Sahin Caliskan9458ebc2018-10-31 13:23:29 -0700561 registerService(Context.TELEPHONY_RCS_SERVICE, RcsManager.class,
562 new CachedServiceFetcher<RcsManager>() {
563 @Override
564 public RcsManager createService(ContextImpl ctx) {
565 return new RcsManager();
566 }
567 });
568
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800569 registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
570 new CachedServiceFetcher<CarrierConfigManager>() {
571 @Override
572 public CarrierConfigManager createService(ContextImpl ctx) {
Malcolm Chend1272d12018-02-28 15:20:45 -0800573 return new CarrierConfigManager(ctx.getOuterContext());
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800574 }});
575
Jeff Brown6e539312015-02-24 18:53:21 -0800576 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
577 new CachedServiceFetcher<TelecomManager>() {
578 @Override
579 public TelecomManager createService(ContextImpl ctx) {
580 return new TelecomManager(ctx.getOuterContext());
581 }});
582
Jeff Davidson35cda392017-02-27 09:46:00 -0800583 registerService(Context.EUICC_SERVICE, EuiccManager.class,
584 new CachedServiceFetcher<EuiccManager>() {
585 @Override
586 public EuiccManager createService(ContextImpl ctx) {
587 return new EuiccManager(ctx.getOuterContext());
588 }});
589
Holly Jiuyu Sun5c110242017-12-21 18:44:59 -0800590 registerService(Context.EUICC_CARD_SERVICE, EuiccCardManager.class,
591 new CachedServiceFetcher<EuiccCardManager>() {
592 @Override
593 public EuiccCardManager createService(ContextImpl ctx) {
594 return new EuiccCardManager(ctx.getOuterContext());
595 }});
596
Jeff Brown6e539312015-02-24 18:53:21 -0800597 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
598 new CachedServiceFetcher<UiModeManager>() {
599 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600600 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800601 return new UiModeManager();
602 }});
603
604 registerService(Context.USB_SERVICE, UsbManager.class,
605 new CachedServiceFetcher<UsbManager>() {
606 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600607 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
608 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800609 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
610 }});
611
Kenny Rootf74bfde2018-01-18 15:42:48 -0800612 registerService(Context.ADB_SERVICE, AdbManager.class,
613 new CachedServiceFetcher<AdbManager>() {
614 @Override
615 public AdbManager createService(ContextImpl ctx)
616 throws ServiceNotFoundException {
617 IBinder b = ServiceManager.getServiceOrThrow(Context.ADB_SERVICE);
618 return new AdbManager(ctx, IAdbManager.Stub.asInterface(b));
619 }});
620
Jeff Brown6e539312015-02-24 18:53:21 -0800621 registerService(Context.SERIAL_SERVICE, SerialManager.class,
622 new CachedServiceFetcher<SerialManager>() {
623 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600624 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
625 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800626 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
627 }});
628
629 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
630 new CachedServiceFetcher<Vibrator>() {
631 @Override
632 public Vibrator createService(ContextImpl ctx) {
633 return new SystemVibrator(ctx);
634 }});
635
636 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
637 new CachedServiceFetcher<WallpaperManager>() {
638 @Override
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800639 public WallpaperManager createService(ContextImpl ctx)
640 throws ServiceNotFoundException {
641 final IBinder b;
642 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
643 b = ServiceManager.getServiceOrThrow(Context.WALLPAPER_SERVICE);
644 } else {
645 b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
646 }
647 IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
648 return new WallpaperManager(service, ctx.getOuterContext(),
Jeff Brown6e539312015-02-24 18:53:21 -0800649 ctx.mMainThread.getHandler());
650 }});
651
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700652 registerService(Context.LOWPAN_SERVICE, LowpanManager.class,
653 new CachedServiceFetcher<LowpanManager>() {
654 @Override
655 public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
656 IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE);
657 ILowpanManager service = ILowpanManager.Stub.asInterface(b);
658 return new LowpanManager(ctx.getOuterContext(), service,
659 ConnectivityThread.getInstanceLooper());
660 }});
661
Jeff Brown6e539312015-02-24 18:53:21 -0800662 registerService(Context.WIFI_SERVICE, WifiManager.class,
663 new CachedServiceFetcher<WifiManager>() {
664 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600665 public WifiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
666 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800667 IWifiManager service = IWifiManager.Stub.asInterface(b);
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700668 return new WifiManager(ctx.getOuterContext(), service,
669 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800670 }});
671
Jeff Brown6e539312015-02-24 18:53:21 -0800672 registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
673 new StaticServiceFetcher<WifiP2pManager>() {
674 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600675 public WifiP2pManager createService() throws ServiceNotFoundException {
676 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800677 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
678 return new WifiP2pManager(service);
679 }});
680
Etan Cohen04133272016-10-26 11:22:06 -0700681 registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class,
682 new CachedServiceFetcher<WifiAwareManager>() {
Etan Cohen20d329b2015-09-29 13:49:02 -0700683 @Override
Etan Cohen04133272016-10-26 11:22:06 -0700684 public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey589f3092016-11-29 12:54:39 -0700685 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE);
Etan Cohen04133272016-10-26 11:22:06 -0700686 IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b);
Etan Cohen20d329b2015-09-29 13:49:02 -0700687 if (service == null) {
688 return null;
689 }
Etan Cohen04133272016-10-26 11:22:06 -0700690 return new WifiAwareManager(ctx.getOuterContext(), service);
Etan Cohen20d329b2015-09-29 13:49:02 -0700691 }});
692
Jeff Brown6e539312015-02-24 18:53:21 -0800693 registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
694 new CachedServiceFetcher<WifiScanner>() {
695 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600696 public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException {
697 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800698 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
Mitchell Wills91312c72016-04-06 17:03:35 -0700699 return new WifiScanner(ctx.getOuterContext(), service,
700 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800701 }});
702
703 registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
704 new CachedServiceFetcher<RttManager>() {
Etan Cohen9accbba2018-01-30 16:41:00 -0800705 @Override
706 public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException {
707 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_RANGING_SERVICE);
708 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
709 return new RttManager(ctx.getOuterContext(),
710 new WifiRttManager(ctx.getOuterContext(), service));
711 }});
Jeff Brown6e539312015-02-24 18:53:21 -0800712
Etan Cohen46efb482017-12-07 13:50:57 -0800713 registerService(Context.WIFI_RTT_RANGING_SERVICE, WifiRttManager.class,
Etan Cohen17ba4722017-08-21 10:52:17 -0700714 new CachedServiceFetcher<WifiRttManager>() {
715 @Override
716 public WifiRttManager createService(ContextImpl ctx)
717 throws ServiceNotFoundException {
Etan Cohen46efb482017-12-07 13:50:57 -0800718 IBinder b = ServiceManager.getServiceOrThrow(
719 Context.WIFI_RTT_RANGING_SERVICE);
Etan Cohen17ba4722017-08-21 10:52:17 -0700720 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
721 return new WifiRttManager(ctx.getOuterContext(), service);
722 }});
723
Jeff Brown6e539312015-02-24 18:53:21 -0800724 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
725 new CachedServiceFetcher<EthernetManager>() {
726 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600727 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
728 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800729 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
730 return new EthernetManager(ctx.getOuterContext(), service);
731 }});
732
733 registerService(Context.WINDOW_SERVICE, WindowManager.class,
734 new CachedServiceFetcher<WindowManager>() {
735 @Override
736 public WindowManager createService(ContextImpl ctx) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700737 return new WindowManagerImpl(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800738 }});
739
740 registerService(Context.USER_SERVICE, UserManager.class,
741 new CachedServiceFetcher<UserManager>() {
742 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600743 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
744 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800745 IUserManager service = IUserManager.Stub.asInterface(b);
746 return new UserManager(ctx, service);
747 }});
748
749 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
750 new CachedServiceFetcher<AppOpsManager>() {
751 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600752 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
753 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800754 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
755 return new AppOpsManager(ctx, service);
756 }});
757
758 registerService(Context.CAMERA_SERVICE, CameraManager.class,
759 new CachedServiceFetcher<CameraManager>() {
760 @Override
761 public CameraManager createService(ContextImpl ctx) {
762 return new CameraManager(ctx);
763 }});
764
765 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
766 new CachedServiceFetcher<LauncherApps>() {
767 @Override
768 public LauncherApps createService(ContextImpl ctx) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700769 return new LauncherApps(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800770 }});
771
772 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
773 new CachedServiceFetcher<RestrictionsManager>() {
774 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600775 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
776 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800777 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
778 return new RestrictionsManager(ctx, service);
779 }});
780
781 registerService(Context.PRINT_SERVICE, PrintManager.class,
782 new CachedServiceFetcher<PrintManager>() {
783 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600784 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganovec7daa52017-06-30 12:02:59 -0700785 IPrintManager service = null;
786 // If the feature not present, don't try to look up every time
787 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
788 service = IPrintManager.Stub.asInterface(ServiceManager
789 .getServiceOrThrow(Context.PRINT_SERVICE));
790 }
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700791 final int userId = ctx.getUserId();
792 final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid);
793 return new PrintManager(ctx.getOuterContext(), service, userId, appId);
Jeff Brown6e539312015-02-24 18:53:21 -0800794 }});
795
Eugene Susla6ed45d82017-01-22 13:52:51 -0800796 registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
797 new CachedServiceFetcher<CompanionDeviceManager>() {
Svet Ganovec7daa52017-06-30 12:02:59 -0700798 @Override
799 public CompanionDeviceManager createService(ContextImpl ctx)
800 throws ServiceNotFoundException {
801 ICompanionDeviceManager service = null;
802 // If the feature not present, don't try to look up every time
803 if (ctx.getPackageManager().hasSystemFeature(
804 PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) {
805 service = ICompanionDeviceManager.Stub.asInterface(
806 ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE));
807 }
808 return new CompanionDeviceManager(service, ctx.getOuterContext());
809 }});
Eugene Susla6ed45d82017-01-22 13:52:51 -0800810
Jeff Brown6e539312015-02-24 18:53:21 -0800811 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
812 new CachedServiceFetcher<ConsumerIrManager>() {
813 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600814 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800815 return new ConsumerIrManager(ctx);
816 }});
817
818 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
819 new CachedServiceFetcher<MediaSessionManager>() {
820 @Override
821 public MediaSessionManager createService(ContextImpl ctx) {
822 return new MediaSessionManager(ctx);
823 }});
824
825 registerService(Context.TRUST_SERVICE, TrustManager.class,
826 new StaticServiceFetcher<TrustManager>() {
827 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600828 public TrustManager createService() throws ServiceNotFoundException {
829 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800830 return new TrustManager(b);
831 }});
832
833 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
834 new CachedServiceFetcher<FingerprintManager>() {
835 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600836 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600837 final IBinder binder;
838 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
839 binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
840 } else {
841 binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
842 }
Jeff Brown6e539312015-02-24 18:53:21 -0800843 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
844 return new FingerprintManager(ctx.getOuterContext(), service);
845 }});
846
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200847 registerService(Context.FACE_SERVICE, FaceManager.class,
848 new CachedServiceFetcher<FaceManager>() {
849 @Override
850 public FaceManager createService(ContextImpl ctx)
851 throws ServiceNotFoundException {
852 final IBinder binder;
853 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
854 binder = ServiceManager.getServiceOrThrow(Context.FACE_SERVICE);
855 } else {
856 binder = ServiceManager.getService(Context.FACE_SERVICE);
857 }
858 IFaceService service = IFaceService.Stub.asInterface(binder);
859 return new FaceManager(ctx.getOuterContext(), service);
860 }
861 });
862
Kevin Chyn51676d22018-11-05 18:00:43 -0800863 registerService(Context.IRIS_SERVICE, IrisManager.class,
864 new CachedServiceFetcher<IrisManager>() {
865 @Override
866 public IrisManager createService(ContextImpl ctx)
867 throws ServiceNotFoundException {
868 final IBinder binder =
869 ServiceManager.getServiceOrThrow(Context.IRIS_SERVICE);
870 IIrisService service = IIrisService.Stub.asInterface(binder);
871 return new IrisManager(ctx.getOuterContext(), service);
872 }
873 });
874
Kevin Chyn05c21502018-09-18 13:07:19 -0700875 registerService(Context.BIOMETRIC_SERVICE, BiometricManager.class,
876 new CachedServiceFetcher<BiometricManager>() {
877 @Override
878 public BiometricManager createService(ContextImpl ctx)
879 throws ServiceNotFoundException {
Kevin Chyne739daf2018-11-06 17:11:06 -0800880 if (BiometricManager.hasBiometrics(ctx)) {
881 final IBinder binder =
882 ServiceManager.getServiceOrThrow(Context.BIOMETRIC_SERVICE);
883 final IBiometricService service =
884 IBiometricService.Stub.asInterface(binder);
885 return new BiometricManager(ctx.getOuterContext(), service);
886 } else {
887 // Allow access to the manager when service is null. This saves memory
888 // on devices without biometric hardware.
889 return new BiometricManager(ctx.getOuterContext(), null);
890 }
Kevin Chyn05c21502018-09-18 13:07:19 -0700891 }
892 });
893
Jeff Brown6e539312015-02-24 18:53:21 -0800894 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700895 new CachedServiceFetcher<TvInputManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800896 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700897 public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600898 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800899 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700900 return new TvInputManager(service, ctx.getUserId());
Jeff Brown6e539312015-02-24 18:53:21 -0800901 }});
902
903 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
904 new CachedServiceFetcher<NetworkScoreManager>() {
905 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600906 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800907 return new NetworkScoreManager(ctx);
908 }});
909
910 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
911 new CachedServiceFetcher<UsageStatsManager>() {
912 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600913 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
914 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800915 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
916 return new UsageStatsManager(ctx.getOuterContext(), service);
917 }});
918
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000919 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
920 new CachedServiceFetcher<NetworkStatsManager>() {
921 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600922 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000923 return new NetworkStatsManager(ctx.getOuterContext());
924 }});
925
Jeff Brown6e539312015-02-24 18:53:21 -0800926 registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
927 new StaticServiceFetcher<JobScheduler>() {
928 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600929 public JobScheduler createService() throws ServiceNotFoundException {
930 IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800931 return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
932 }});
933
934 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
935 new StaticServiceFetcher<PersistentDataBlockManager>() {
936 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600937 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
938 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800939 IPersistentDataBlockService persistentDataBlockService =
940 IPersistentDataBlockService.Stub.asInterface(b);
941 if (persistentDataBlockService != null) {
942 return new PersistentDataBlockManager(persistentDataBlockService);
943 } else {
944 // not supported
945 return null;
946 }
947 }});
948
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000949 registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
950 new StaticServiceFetcher<OemLockManager>() {
951 @Override
952 public OemLockManager createService() throws ServiceNotFoundException {
953 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
954 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
955 if (oemLockService != null) {
956 return new OemLockManager(oemLockService);
957 } else {
958 // not supported
959 return null;
960 }
961 }});
962
Jeff Brown6e539312015-02-24 18:53:21 -0800963 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
964 new CachedServiceFetcher<MediaProjectionManager>() {
965 @Override
966 public MediaProjectionManager createService(ContextImpl ctx) {
967 return new MediaProjectionManager(ctx);
968 }});
969
970 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
971 new CachedServiceFetcher<AppWidgetManager>() {
972 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600973 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
974 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800975 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
976 }});
977
978 registerService(Context.MIDI_SERVICE, MidiManager.class,
979 new CachedServiceFetcher<MidiManager>() {
980 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600981 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
982 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
Mike Lockwoode0a6ca62015-06-04 13:43:56 -0700983 return new MidiManager(IMidiManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800984 }});
Eric Laurent2035ac82015-03-05 15:18:44 -0800985
986 registerService(Context.RADIO_SERVICE, RadioManager.class,
987 new CachedServiceFetcher<RadioManager>() {
988 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -0700989 public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Eric Laurent2035ac82015-03-05 15:18:44 -0800990 return new RadioManager(ctx);
991 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800992
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100993 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
994 new CachedServiceFetcher<HardwarePropertiesManager>() {
995 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600996 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
997 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
998 IHardwarePropertiesManager service =
999 IHardwarePropertiesManager.Stub.asInterface(b);
1000 return new HardwarePropertiesManager(ctx, service);
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001001 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001002
1003 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
1004 new CachedServiceFetcher<SoundTriggerManager>() {
1005 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001006 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1007 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001008 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
1009 }});
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001010
1011 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
1012 new CachedServiceFetcher<ShortcutManager>() {
Makoto Onuki55046222016-03-08 10:49:47 -08001013 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001014 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1015 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
1016 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
Makoto Onuki55046222016-03-08 10:49:47 -08001017 }});
Joe Onorato713fec82016-03-04 10:34:02 -08001018
Ricky Wai1a6e6672017-10-27 14:46:01 +01001019 registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class,
1020 new CachedServiceFetcher<NetworkWatchlistManager>() {
1021 @Override
1022 public NetworkWatchlistManager createService(ContextImpl ctx)
1023 throws ServiceNotFoundException {
1024 IBinder b =
1025 ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE);
1026 return new NetworkWatchlistManager(ctx,
1027 INetworkWatchlistManager.Stub.asInterface(b));
1028 }});
1029
Joe Onorato713fec82016-03-04 10:34:02 -08001030 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
1031 new CachedServiceFetcher<SystemHealthManager>() {
1032 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001033 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1034 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
1035 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
Joe Onorato713fec82016-03-04 10:34:02 -08001036 }});
Peng Xu9ff7d222016-02-11 13:02:05 -08001037
1038 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
1039 new CachedServiceFetcher<ContextHubManager>() {
1040 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001041 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Peng Xu9ff7d222016-02-11 13:02:05 -08001042 return new ContextHubManager(ctx.getOuterContext(),
1043 ctx.mMainThread.getHandler().getLooper());
1044 }});
Joe Onorato1754d742016-11-21 17:51:35 -08001045
1046 registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
1047 new CachedServiceFetcher<IncidentManager>() {
1048 @Override
1049 public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1050 return new IncidentManager(ctx);
1051 }});
Clara Bayarri04d72ab2017-01-10 09:31:51 -08001052
Felipe Leme640f30a2017-03-06 15:44:06 -08001053 registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
1054 new CachedServiceFetcher<AutofillManager>() {
Felipe Leme3461d3c2017-01-19 08:54:55 -08001055 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -08001056 public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov43574b02017-04-12 09:25:20 -07001057 // Get the services without throwing as this is an optional feature
1058 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
Svet Ganov782043c2017-02-11 00:52:02 +00001059 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
Felipe Leme640f30a2017-03-06 15:44:06 -08001060 return new AutofillManager(ctx.getOuterContext(), service);
Felipe Leme3461d3c2017-01-19 08:54:55 -08001061 }});
Zak Cohen56345f42017-01-26 13:54:28 -08001062
Felipe Lemeecb08be2018-11-27 15:48:47 -08001063 registerService(Context.CONTENT_CAPTURE_MANAGER_SERVICE, ContentCaptureManager.class,
1064 new CachedServiceFetcher<ContentCaptureManager>() {
Felipe Lemee348dc32018-11-05 12:35:29 -08001065 @Override
Felipe Lemeecb08be2018-11-27 15:48:47 -08001066 public ContentCaptureManager createService(ContextImpl ctx)
Felipe Lemee348dc32018-11-05 12:35:29 -08001067 throws ServiceNotFoundException {
1068 // Get the services without throwing as this is an optional feature
Felipe Lemeecb08be2018-11-27 15:48:47 -08001069 Context outerContext = ctx.getOuterContext();
1070 if (outerContext.isContentCaptureSupported()) {
1071 IBinder b = ServiceManager
1072 .getService(Context.CONTENT_CAPTURE_MANAGER_SERVICE);
1073 IIntelligenceManager service = IIntelligenceManager.Stub.asInterface(b);
1074 return new ContentCaptureManager(outerContext, service);
1075 }
1076 return null;
Felipe Lemee348dc32018-11-05 12:35:29 -08001077 }});
1078
Zak Cohen56345f42017-01-26 13:54:28 -08001079 registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
1080 @Override
1081 public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1082 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
1083 return new VrManager(IVrManager.Stub.asInterface(b));
1084 }
1085 });
Neil Fuller328532a2017-03-16 18:32:21 +00001086
1087 registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
1088 new CachedServiceFetcher<RulesManager>() {
1089 @Override
1090 public RulesManager createService(ContextImpl ctx) {
1091 return new RulesManager(ctx.getOuterContext());
1092 }});
Tony Mak1b708e62017-10-12 10:59:11 +01001093
1094 registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class,
1095 new CachedServiceFetcher<CrossProfileApps>() {
1096 @Override
1097 public CrossProfileApps createService(ContextImpl ctx)
1098 throws ServiceNotFoundException {
1099 IBinder b = ServiceManager.getServiceOrThrow(
1100 Context.CROSS_PROFILE_APPS_SERVICE);
1101 return new CrossProfileApps(ctx.getOuterContext(),
1102 ICrossProfileApps.Stub.asInterface(b));
1103 }
1104 });
Jason Monk8f5f7ff2017-10-17 14:12:42 -04001105
1106 registerService(Context.SLICE_SERVICE, SliceManager.class,
1107 new CachedServiceFetcher<SliceManager>() {
1108 @Override
1109 public SliceManager createService(ContextImpl ctx)
1110 throws ServiceNotFoundException {
1111 return new SliceManager(ctx.getOuterContext(),
1112 ctx.mMainThread.getHandler());
1113 }
1114 });
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001115
1116 registerService(Context.DEVICE_IDLE_CONTROLLER, DeviceIdleManager.class,
1117 new CachedServiceFetcher<DeviceIdleManager>() {
1118 @Override
1119 public DeviceIdleManager createService(ContextImpl ctx)
1120 throws ServiceNotFoundException {
1121 IDeviceIdleController service = IDeviceIdleController.Stub.asInterface(
1122 ServiceManager.getServiceOrThrow(
1123 Context.DEVICE_IDLE_CONTROLLER));
1124 return new DeviceIdleManager(ctx.getOuterContext(), service);
1125 }});
Neil Fullerfeeee682018-05-30 14:35:24 +01001126
1127 registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class,
1128 new CachedServiceFetcher<TimeDetector>() {
1129 @Override
1130 public TimeDetector createService(ContextImpl ctx)
1131 throws ServiceNotFoundException {
1132 return new TimeDetector();
1133 }});
Neil Fuller7fb88c32018-06-19 12:53:47 +01001134 registerService(Context.TIME_ZONE_DETECTOR_SERVICE, TimeZoneDetector.class,
1135 new CachedServiceFetcher<TimeZoneDetector>() {
1136 @Override
1137 public TimeZoneDetector createService(ContextImpl ctx)
1138 throws ServiceNotFoundException {
1139 return new TimeZoneDetector();
1140 }});
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001141
1142 registerService(Context.PERMISSION_SERVICE, PermissionManager.class,
1143 new CachedServiceFetcher<PermissionManager>() {
1144 @Override
1145 public PermissionManager createService(ContextImpl ctx) {
1146 return new PermissionManager(ctx.getOuterContext());
1147 }});
Hai Zhang4ef21d02018-11-09 14:43:51 -08001148
1149 registerService(Context.ROLE_SERVICE, RoleManager.class,
1150 new CachedServiceFetcher<RoleManager>() {
1151 @Override
1152 public RoleManager createService(ContextImpl ctx)
1153 throws ServiceNotFoundException {
1154 return new RoleManager(ctx.getOuterContext());
1155 }});
Felipe Lemeecb08be2018-11-27 15:48:47 -08001156 //CHECKSTYLE:ON IndentationCheck
Jeff Brown6e539312015-02-24 18:53:21 -08001157 }
1158
1159 /**
1160 * Creates an array which is used to cache per-Context service instances.
1161 */
1162 public static Object[] createServiceCache() {
1163 return new Object[sServiceCacheSize];
1164 }
1165
1166 /**
1167 * Gets a system service from a given context.
1168 */
1169 public static Object getSystemService(ContextImpl ctx, String name) {
1170 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
1171 return fetcher != null ? fetcher.getService(ctx) : null;
1172 }
1173
1174 /**
Paul McLeane3383cc2015-05-08 11:41:20 -07001175 * Gets the name of the system-level service that is represented by the specified class.
Jeff Brown6e539312015-02-24 18:53:21 -08001176 */
1177 public static String getSystemServiceName(Class<?> serviceClass) {
1178 return SYSTEM_SERVICE_NAMES.get(serviceClass);
1179 }
1180
1181 /**
1182 * Statically registers a system service with the context.
1183 * This method must be called during static initialization only.
1184 */
1185 private static <T> void registerService(String serviceName, Class<T> serviceClass,
1186 ServiceFetcher<T> serviceFetcher) {
1187 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
1188 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
1189 }
1190
1191 /**
1192 * Base interface for classes that fetch services.
1193 * These objects must only be created during static initialization.
1194 */
1195 static abstract interface ServiceFetcher<T> {
1196 T getService(ContextImpl ctx);
1197 }
1198
1199 /**
1200 * Override this class when the system service constructor needs a
1201 * ContextImpl and should be cached and retained by that context.
1202 */
1203 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
1204 private final int mCacheIndex;
1205
Makoto Onukid67070e2018-03-30 12:39:14 -07001206 CachedServiceFetcher() {
1207 // Note this class must be instantiated only by the static initializer of the
1208 // outer class (SystemServiceRegistry), which already does the synchronization,
1209 // so bare access to sServiceCacheSize is okay here.
Jeff Brown6e539312015-02-24 18:53:21 -08001210 mCacheIndex = sServiceCacheSize++;
1211 }
1212
1213 @Override
1214 @SuppressWarnings("unchecked")
1215 public final T getService(ContextImpl ctx) {
1216 final Object[] cache = ctx.mServiceCache;
Makoto Onukid67070e2018-03-30 12:39:14 -07001217 final int[] gates = ctx.mServiceInitializationStateArray;
Makoto Onukif896f122018-01-25 09:50:24 -08001218
Makoto Onukid67070e2018-03-30 12:39:14 -07001219 for (;;) {
1220 boolean doInitialize = false;
1221 synchronized (cache) {
1222 // Return it if we already have a cached instance.
1223 T service = (T) cache[mCacheIndex];
1224 if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) {
1225 return service;
1226 }
Makoto Onukif896f122018-01-25 09:50:24 -08001227
Makoto Onukid67070e2018-03-30 12:39:14 -07001228 // If we get here, there's no cached instance.
Makoto Onukif896f122018-01-25 09:50:24 -08001229
Makoto Onukid67070e2018-03-30 12:39:14 -07001230 // Grr... if gate is STATE_READY, then this means we initialized the service
1231 // once but someone cleared it.
1232 // We start over from STATE_UNINITIALIZED.
1233 if (gates[mCacheIndex] == ContextImpl.STATE_READY) {
1234 gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED;
1235 }
1236
1237 // It's possible for multiple threads to get here at the same time, so
1238 // use the "gate" to make sure only the first thread will call createService().
1239
1240 // At this point, the gate must be either UNINITIALIZED or INITIALIZING.
1241 if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) {
1242 doInitialize = true;
1243 gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING;
1244 }
Makoto Onukif896f122018-01-25 09:50:24 -08001245 }
1246
Makoto Onukid67070e2018-03-30 12:39:14 -07001247 if (doInitialize) {
1248 // Only the first thread gets here.
Makoto Onukif896f122018-01-25 09:50:24 -08001249
Makoto Onukid67070e2018-03-30 12:39:14 -07001250 T service = null;
1251 @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND;
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001252 try {
Makoto Onukid67070e2018-03-30 12:39:14 -07001253 // This thread is the first one to get here. Instantiate the service
1254 // *without* the cache lock held.
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001255 service = createService(ctx);
Makoto Onukid67070e2018-03-30 12:39:14 -07001256 newState = ContextImpl.STATE_READY;
Makoto Onukif896f122018-01-25 09:50:24 -08001257
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001258 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001259 onServiceNotFound(e);
Makoto Onukif896f122018-01-25 09:50:24 -08001260
Makoto Onukid67070e2018-03-30 12:39:14 -07001261 } finally {
1262 synchronized (cache) {
1263 cache[mCacheIndex] = service;
1264 gates[mCacheIndex] = newState;
1265 cache.notifyAll();
1266 }
1267 }
1268 return service;
1269 }
1270 // The other threads will wait for the first thread to call notifyAll(),
1271 // and go back to the top and retry.
1272 synchronized (cache) {
1273 while (gates[mCacheIndex] < ContextImpl.STATE_READY) {
1274 try {
1275 cache.wait();
1276 } catch (InterruptedException e) {
1277 Log.w(TAG, "getService() interrupted");
1278 Thread.currentThread().interrupt();
1279 return null;
1280 }
Makoto Onukif896f122018-01-25 09:50:24 -08001281 }
1282 }
Makoto Onukif896f122018-01-25 09:50:24 -08001283 }
Jeff Brown6e539312015-02-24 18:53:21 -08001284 }
1285
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001286 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001287 }
1288
1289 /**
1290 * Override this class when the system service does not need a ContextImpl
1291 * and should be cached and retained process-wide.
1292 */
1293 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
1294 private T mCachedInstance;
1295
1296 @Override
Jeff Sharkey589f3092016-11-29 12:54:39 -07001297 public final T getService(ContextImpl ctx) {
Jeff Brown6e539312015-02-24 18:53:21 -08001298 synchronized (StaticServiceFetcher.this) {
1299 if (mCachedInstance == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001300 try {
1301 mCachedInstance = createService();
1302 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001303 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001304 }
Jeff Brown6e539312015-02-24 18:53:21 -08001305 }
1306 return mCachedInstance;
1307 }
1308 }
1309
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001310 public abstract T createService() throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001311 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001312
1313 /**
Lorenzo Colitticf959772016-02-29 16:07:37 +09001314 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
1315 * creating the service for the first time, passes it the application context of the creating
1316 * application.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001317 *
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001318 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
1319 * case where multiple application components each have their own ConnectivityManager object.
1320 */
Lorenzo Colitticf959772016-02-29 16:07:37 +09001321 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001322 private T mCachedInstance;
1323
1324 @Override
1325 public final T getService(ContextImpl ctx) {
Lorenzo Colitticf959772016-02-29 16:07:37 +09001326 synchronized (StaticApplicationContextServiceFetcher.this) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001327 if (mCachedInstance == null) {
Lorenzo Colitti0ad9ab02016-03-25 17:40:11 +09001328 Context appContext = ctx.getApplicationContext();
1329 // If the application context is null, we're either in the system process or
1330 // it's the application context very early in app initialization. In both these
1331 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
1332 // to the service. http://b/27532714 .
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001333 try {
1334 mCachedInstance = createService(appContext != null ? appContext : ctx);
1335 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001336 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001337 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001338 }
1339 return mCachedInstance;
1340 }
1341 }
1342
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001343 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001344 }
Jeff Sharkey589f3092016-11-29 12:54:39 -07001345
1346 public static void onServiceNotFound(ServiceNotFoundException e) {
1347 // We're mostly interested in tracking down long-lived core system
1348 // components that might stumble if they obtain bad references; just
1349 // emit a tidy log message for normal apps
1350 if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1351 Log.wtf(TAG, e.getMessage(), e);
1352 } else {
1353 Log.w(TAG, e.getMessage());
1354 }
1355 }
Jeff Brown6e539312015-02-24 18:53:21 -08001356}