blob: d0ba879bd10e4a9f5c536953488e0aa836643795 [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 Onuki05b14f52019-11-05 12:36:07 -080021import android.annotation.NonNull;
Makoto Onukid67070e2018-03-30 12:39:14 -070022import android.app.ContextImpl.ServiceInitializationState;
Jeff Brown6e539312015-02-24 18:53:21 -080023import android.app.admin.DevicePolicyManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060024import android.app.admin.IDevicePolicyManager;
Sudheer Shankaf5b36962019-10-04 16:16:13 -070025import android.app.blob.BlobStoreManagerFrameworkInitializer;
Winson Chung3fb0f252019-01-08 17:41:55 -080026import android.app.contentsuggestions.ContentSuggestionsManager;
27import android.app.contentsuggestions.IContentSuggestionsManager;
Makoto Onuki54c4e032019-10-30 12:05:20 -070028import android.app.job.JobSchedulerFrameworkInitializer;
Sunny Goyal54e91342018-11-14 11:59:02 -080029import android.app.prediction.AppPredictionManager;
Hai Zhanga4959e52019-03-06 12:21:07 -080030import android.app.role.RoleControllerManager;
Hai Zhang4ef21d02018-11-09 14:43:51 -080031import android.app.role.RoleManager;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040032import android.app.slice.SliceManager;
Neil Fullerfeeee682018-05-30 14:35:24 +010033import android.app.timedetector.TimeDetector;
Neil Fuller328532a2017-03-16 18:32:21 +000034import android.app.timezone.RulesManager;
Jeff Brown6e539312015-02-24 18:53:21 -080035import android.app.trust.TrustManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070036import android.app.usage.IStorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080037import android.app.usage.IUsageStatsManager;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000038import android.app.usage.NetworkStatsManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070039import android.app.usage.StorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080040import android.app.usage.UsageStatsManager;
41import android.appwidget.AppWidgetManager;
42import android.bluetooth.BluetoothManager;
Eugene Susla6ed45d82017-01-22 13:52:51 -080043import android.companion.CompanionDeviceManager;
44import android.companion.ICompanionDeviceManager;
Jeff Brown6e539312015-02-24 18:53:21 -080045import android.content.ClipboardManager;
Felipe Leme326f15a2019-02-19 09:42:24 -080046import android.content.ContentCaptureOptions;
Jeff Brown6e539312015-02-24 18:53:21 -080047import android.content.Context;
48import android.content.IRestrictionsManager;
49import android.content.RestrictionsManager;
Hyunyoung Song880a9512018-12-20 11:24:48 -080050import android.content.om.IOverlayManager;
51import android.content.om.OverlayManager;
Tony Makb0d22622018-01-18 12:49:49 +000052import android.content.pm.CrossProfileApps;
53import android.content.pm.ICrossProfileApps;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070054import android.content.pm.IPackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080055import android.content.pm.IShortcutService;
Jeff Brown6e539312015-02-24 18:53:21 -080056import android.content.pm.LauncherApps;
Svet Ganovec7daa52017-06-30 12:02:59 -070057import android.content.pm.PackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080058import android.content.pm.ShortcutManager;
Jeff Brown6e539312015-02-24 18:53:21 -080059import android.content.res.Resources;
Richard Uhlerb29f1452018-09-12 16:38:15 +010060import android.content.rollback.IRollbackManager;
61import android.content.rollback.RollbackManager;
Kenny Rootf74bfde2018-01-18 15:42:48 -080062import android.debug.AdbManager;
63import android.debug.IAdbManager;
Jeff Brown6e539312015-02-24 18:53:21 -080064import android.hardware.ConsumerIrManager;
65import android.hardware.ISerialManager;
66import android.hardware.SensorManager;
Chad Brubaker90f391f2018-10-19 10:26:19 -070067import android.hardware.SensorPrivacyManager;
Jeff Brown6e539312015-02-24 18:53:21 -080068import android.hardware.SerialManager;
69import android.hardware.SystemSensorManager;
Kevin Chyn05c21502018-09-18 13:07:19 -070070import android.hardware.biometrics.BiometricManager;
Ilya Matyukhine4675b32019-11-07 16:07:19 -080071import android.hardware.biometrics.IAuthService;
Jeff Brown6e539312015-02-24 18:53:21 -080072import android.hardware.camera2.CameraManager;
Christine Franks39b03112018-07-03 14:46:07 -070073import android.hardware.display.ColorDisplayManager;
Jeff Brown6e539312015-02-24 18:53:21 -080074import android.hardware.display.DisplayManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020075import android.hardware.face.FaceManager;
76import android.hardware.face.IFaceService;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060077import android.hardware.fingerprint.FingerprintManager;
78import android.hardware.fingerprint.IFingerprintService;
Jeff Brown6e539312015-02-24 18:53:21 -080079import android.hardware.hdmi.HdmiControlManager;
80import android.hardware.hdmi.IHdmiControlService;
81import android.hardware.input.InputManager;
Kevin Chyn51676d22018-11-05 18:00:43 -080082import android.hardware.iris.IIrisService;
Felipe Lemee348dc32018-11-05 12:35:29 -080083import android.hardware.iris.IrisManager;
Peng Xu9ff7d222016-02-11 13:02:05 -080084import android.hardware.location.ContextHubManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060085import android.hardware.radio.RadioManager;
Jeff Brown6e539312015-02-24 18:53:21 -080086import android.hardware.usb.IUsbManager;
87import android.hardware.usb.UsbManager;
88import android.location.CountryDetector;
89import android.location.ICountryDetector;
90import android.location.ILocationManager;
91import android.location.LocationManager;
92import android.media.AudioManager;
93import android.media.MediaRouter;
94import android.media.midi.IMidiManager;
95import android.media.midi.MidiManager;
96import android.media.projection.MediaProjectionManager;
97import android.media.session.MediaSessionManager;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080098import android.media.soundtrigger.SoundTriggerManager;
Jeff Brown6e539312015-02-24 18:53:21 -080099import android.media.tv.ITvInputManager;
100import android.media.tv.TvInputManager;
101import android.net.ConnectivityManager;
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700102import android.net.ConnectivityThread;
Jeff Brown6e539312015-02-24 18:53:21 -0800103import android.net.EthernetManager;
104import android.net.IConnectivityManager;
105import android.net.IEthernetManager;
Nathan Harold28084d82017-03-01 18:55:06 -0800106import android.net.IIpSecService;
Jeff Brown6e539312015-02-24 18:53:21 -0800107import android.net.INetworkPolicyManager;
Benedict Wong99a48412018-11-09 14:45:34 -0800108import android.net.ITestNetworkManager;
Nathan Harold28084d82017-03-01 18:55:06 -0800109import android.net.IpSecManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800110import android.net.NetworkPolicyManager;
111import android.net.NetworkScoreManager;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100112import android.net.NetworkWatchlistManager;
Benedict Wong99a48412018-11-09 14:45:34 -0800113import android.net.TestNetworkManager;
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700114import android.net.lowpan.ILowpanManager;
115import android.net.lowpan.LowpanManager;
Etan Cohen17ba4722017-08-21 10:52:17 -0700116import android.net.nsd.INsdManager;
117import android.net.nsd.NsdManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800118import android.net.wifi.IWifiScanner;
119import android.net.wifi.RttManager;
120import android.net.wifi.WifiManager;
121import android.net.wifi.WifiScanner;
Etan Cohen04133272016-10-26 11:22:06 -0700122import android.net.wifi.aware.IWifiAwareManager;
123import android.net.wifi.aware.WifiAwareManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800124import android.net.wifi.p2p.IWifiP2pManager;
125import android.net.wifi.p2p.WifiP2pManager;
Etan Cohen17ba4722017-08-21 10:52:17 -0700126import android.net.wifi.rtt.IWifiRttManager;
127import android.net.wifi.rtt.WifiRttManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800128import android.nfc.NfcManager;
129import android.os.BatteryManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600130import android.os.BatteryStats;
Roshan Pius848513e2019-10-11 13:44:00 -0700131import android.os.BatteryStatsManager;
Nandana Duttd11850c2018-12-12 17:26:57 +0000132import android.os.BugreportManager;
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600133import android.os.Build;
Jeff Brown6e539312015-02-24 18:53:21 -0800134import android.os.DropBoxManager;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100135import android.os.HardwarePropertiesManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600136import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6e539312015-02-24 18:53:21 -0800137import android.os.IBinder;
Nandana Duttd11850c2018-12-12 17:26:57 +0000138import android.os.IDumpstate;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100139import android.os.IHardwarePropertiesManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800140import android.os.IPowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800141import android.os.IRecoverySystem;
Tao Bao07342dc2017-01-24 15:08:21 -0800142import android.os.ISystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800143import android.os.IUserManager;
Joe Onorato1754d742016-11-21 17:51:35 -0800144import android.os.IncidentManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800145import android.os.PowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800146import android.os.RecoverySystem;
Benedict Wong99a48412018-11-09 14:45:34 -0800147import android.os.RemoteException;
Jeff Brown6e539312015-02-24 18:53:21 -0800148import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600149import android.os.ServiceManager.ServiceNotFoundException;
Tao Bao07342dc2017-01-24 15:08:21 -0800150import android.os.SystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800151import android.os.SystemVibrator;
152import android.os.UserHandle;
153import android.os.UserManager;
154import android.os.Vibrator;
Joe Onorato713fec82016-03-04 10:34:02 -0800155import android.os.health.SystemHealthManager;
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +0800156import android.os.image.DynamicSystemManager;
157import android.os.image.IDynamicSystemService;
Jeff Brown6e539312015-02-24 18:53:21 -0800158import android.os.storage.StorageManager;
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800159import android.permission.PermissionControllerManager;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700160import android.permission.PermissionManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800161import android.print.IPrintManager;
162import android.print.PrintManager;
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000163import android.service.oemlock.IOemLockService;
164import android.service.oemlock.OemLockManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800165import android.service.persistentdata.IPersistentDataBlockService;
166import android.service.persistentdata.PersistentDataBlockManager;
Zak Cohen56345f42017-01-26 13:54:28 -0800167import android.service.vr.IVrManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800168import android.telecom.TelecomManager;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800169import android.telephony.CarrierConfigManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800170import android.telephony.SubscriptionManager;
171import android.telephony.TelephonyManager;
Makoto Onuki54c4e032019-10-30 12:05:20 -0700172import android.telephony.TelephonyRegistryManager;
Holly Jiuyu Sun5c110242017-12-21 18:44:59 -0800173import android.telephony.euicc.EuiccCardManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100174import android.telephony.euicc.EuiccManager;
Robert Benea1901a5b2018-05-15 16:36:07 -0700175import android.util.ArrayMap;
Jeff Brown6e539312015-02-24 18:53:21 -0800176import android.util.Log;
177import android.view.ContextThemeWrapper;
178import android.view.LayoutInflater;
Jeff Brown6e539312015-02-24 18:53:21 -0800179import android.view.WindowManager;
180import android.view.WindowManagerImpl;
181import android.view.accessibility.AccessibilityManager;
182import android.view.accessibility.CaptioningManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100183import android.view.autofill.AutofillManager;
184import android.view.autofill.IAutoFillManager;
Felipe Leme749b8892018-12-03 16:30:30 -0800185import android.view.contentcapture.ContentCaptureManager;
186import android.view.contentcapture.IContentCaptureManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800187import android.view.inputmethod.InputMethodManager;
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000188import android.view.textclassifier.TextClassificationManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800189import android.view.textservice.TextServicesManager;
190
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600191import com.android.internal.app.IAppOpsService;
192import com.android.internal.app.IBatteryStats;
193import com.android.internal.app.ISoundTriggerService;
194import com.android.internal.appwidget.IAppWidgetService;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100195import com.android.internal.net.INetworkWatchlistManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600196import com.android.internal.os.IDropBoxManagerService;
197import com.android.internal.policy.PhoneLayoutInflater;
Makoto Onuki05b14f52019-11-05 12:36:07 -0800198import com.android.internal.util.Preconditions;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600199
Robert Benea1901a5b2018-05-15 16:36:07 -0700200import java.util.Map;
Jeff Brown6e539312015-02-24 18:53:21 -0800201
202/**
203 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
204 * Used by {@link ContextImpl}.
Makoto Onuki792de8d2019-07-16 16:19:01 -0700205 *
206 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -0800207 */
Makoto Onuki792de8d2019-07-16 16:19:01 -0700208public final class SystemServiceRegistry {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600209 private static final String TAG = "SystemServiceRegistry";
210
Jeff Brown6e539312015-02-24 18:53:21 -0800211 // Service registry information.
212 // This information is never changed once static initialization has completed.
Robert Benea1901a5b2018-05-15 16:36:07 -0700213 private static final Map<Class<?>, String> SYSTEM_SERVICE_NAMES =
214 new ArrayMap<Class<?>, String>();
215 private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
216 new ArrayMap<String, ServiceFetcher<?>>();
Jeff Brown6e539312015-02-24 18:53:21 -0800217 private static int sServiceCacheSize;
218
Makoto Onuki05b14f52019-11-05 12:36:07 -0800219 private static volatile boolean sInitializing;
220
Jeff Brown6e539312015-02-24 18:53:21 -0800221 // Not instantiable.
222 private SystemServiceRegistry() { }
223
224 static {
Felipe Lemeecb08be2018-11-27 15:48:47 -0800225 //CHECKSTYLE:OFF IndentationCheck
Jeff Brown6e539312015-02-24 18:53:21 -0800226 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
227 new CachedServiceFetcher<AccessibilityManager>() {
228 @Override
229 public AccessibilityManager createService(ContextImpl ctx) {
230 return AccessibilityManager.getInstance(ctx);
231 }});
232
233 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
234 new CachedServiceFetcher<CaptioningManager>() {
235 @Override
236 public CaptioningManager createService(ContextImpl ctx) {
237 return new CaptioningManager(ctx);
238 }});
239
240 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
241 new CachedServiceFetcher<AccountManager>() {
242 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600243 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
244 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800245 IAccountManager service = IAccountManager.Stub.asInterface(b);
246 return new AccountManager(ctx, service);
247 }});
248
249 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
250 new CachedServiceFetcher<ActivityManager>() {
251 @Override
252 public ActivityManager createService(ContextImpl ctx) {
253 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
254 }});
255
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700256 registerService(Context.ACTIVITY_TASK_SERVICE, ActivityTaskManager.class,
257 new CachedServiceFetcher<ActivityTaskManager>() {
258 @Override
259 public ActivityTaskManager createService(ContextImpl ctx) {
260 return new ActivityTaskManager(
261 ctx.getOuterContext(), ctx.mMainThread.getHandler());
262 }});
263
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700264 registerService(Context.URI_GRANTS_SERVICE, UriGrantsManager.class,
265 new CachedServiceFetcher<UriGrantsManager>() {
266 @Override
267 public UriGrantsManager createService(ContextImpl ctx) {
268 return new UriGrantsManager(
269 ctx.getOuterContext(), ctx.mMainThread.getHandler());
270 }});
271
Jeff Brown6e539312015-02-24 18:53:21 -0800272 registerService(Context.ALARM_SERVICE, AlarmManager.class,
273 new CachedServiceFetcher<AlarmManager>() {
274 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600275 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
276 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800277 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
278 return new AlarmManager(service, ctx);
279 }});
280
281 registerService(Context.AUDIO_SERVICE, AudioManager.class,
282 new CachedServiceFetcher<AudioManager>() {
283 @Override
284 public AudioManager createService(ContextImpl ctx) {
285 return new AudioManager(ctx);
286 }});
287
288 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
289 new CachedServiceFetcher<MediaRouter>() {
290 @Override
291 public MediaRouter createService(ContextImpl ctx) {
292 return new MediaRouter(ctx);
293 }});
294
295 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
296 new CachedServiceFetcher<BluetoothManager>() {
297 @Override
298 public BluetoothManager createService(ContextImpl ctx) {
299 return new BluetoothManager(ctx);
300 }});
301
302 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
303 new StaticServiceFetcher<HdmiControlManager>() {
304 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600305 public HdmiControlManager createService() throws ServiceNotFoundException {
306 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800307 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
308 }});
309
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000310 registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000311 new CachedServiceFetcher<TextClassificationManager>() {
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000312 @Override
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000313 public TextClassificationManager createService(ContextImpl ctx) {
314 return new TextClassificationManager(ctx);
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000315 }});
316
Jeff Brown6e539312015-02-24 18:53:21 -0800317 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
318 new CachedServiceFetcher<ClipboardManager>() {
319 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600320 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800321 return new ClipboardManager(ctx.getOuterContext(),
322 ctx.mMainThread.getHandler());
323 }});
324
325 // The clipboard service moved to a new package. If someone asks for the old
326 // interface by class then we want to redirect over to the new interface instead
327 // (which extends it).
328 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
329
330 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
Lorenzo Colitticf959772016-02-29 16:07:37 +0900331 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800332 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600333 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
334 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900335 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
336 return new ConnectivityManager(context, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800337 }});
338
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +0900339 registerService(Context.NETD_SERVICE, IBinder.class, new StaticServiceFetcher<IBinder>() {
Remi NGUYEN VAN31935982019-01-28 11:40:08 +0900340 @Override
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +0900341 public IBinder createService() throws ServiceNotFoundException {
342 return ServiceManager.getServiceOrThrow(Context.NETD_SERVICE);
Remi NGUYEN VAN31935982019-01-28 11:40:08 +0900343 }
344 });
345
Nathan Harold28084d82017-03-01 18:55:06 -0800346 registerService(Context.IPSEC_SERVICE, IpSecManager.class,
Nathan Harold592dadb2018-03-15 18:06:06 -0700347 new CachedServiceFetcher<IpSecManager>() {
Nathan Harold28084d82017-03-01 18:55:06 -0800348 @Override
Nathan Harold592dadb2018-03-15 18:06:06 -0700349 public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nathan Harold28084d82017-03-01 18:55:06 -0800350 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
351 IIpSecService service = IIpSecService.Stub.asInterface(b);
Nathan Harold592dadb2018-03-15 18:06:06 -0700352 return new IpSecManager(ctx, service);
Nathan Harold28084d82017-03-01 18:55:06 -0800353 }});
354
Benedict Wong99a48412018-11-09 14:45:34 -0800355 registerService(
356 Context.TEST_NETWORK_SERVICE,
357 TestNetworkManager.class,
358 new StaticApplicationContextServiceFetcher<TestNetworkManager>() {
359 @Override
360 public TestNetworkManager createService(Context context)
361 throws ServiceNotFoundException {
362 IBinder csBinder =
363 ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
364 IConnectivityManager csMgr =
365 IConnectivityManager.Stub.asInterface(csBinder);
366
367 final IBinder tnBinder;
368 try {
369 tnBinder = csMgr.startOrGetTestNetworkService();
370 } catch (RemoteException e) {
371 throw new ServiceNotFoundException(Context.TEST_NETWORK_SERVICE);
372 }
373 ITestNetworkManager tnMgr = ITestNetworkManager.Stub.asInterface(tnBinder);
Benedict Wongb4e98f32019-03-12 21:54:16 -0700374 return new TestNetworkManager(tnMgr);
Benedict Wong99a48412018-11-09 14:45:34 -0800375 }
376 });
377
Jeff Brown6e539312015-02-24 18:53:21 -0800378 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
379 new StaticServiceFetcher<CountryDetector>() {
380 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600381 public CountryDetector createService() throws ServiceNotFoundException {
382 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
Jeff Brown6e539312015-02-24 18:53:21 -0800383 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
384 }});
385
386 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
387 new CachedServiceFetcher<DevicePolicyManager>() {
388 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600389 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
390 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
391 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800392 }});
393
394 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
395 new CachedServiceFetcher<DownloadManager>() {
396 @Override
397 public DownloadManager createService(ContextImpl ctx) {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700398 return new DownloadManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800399 }});
400
401 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
Yifan Hongb0f13522017-10-31 15:48:50 -0700402 new CachedServiceFetcher<BatteryManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800403 @Override
Yifan Hongb0f13522017-10-31 15:48:50 -0700404 public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600405 IBatteryStats stats = IBatteryStats.Stub.asInterface(
406 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
407 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
408 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
Yifan Hongb0f13522017-10-31 15:48:50 -0700409 return new BatteryManager(ctx, stats, registrar);
Jeff Brown6e539312015-02-24 18:53:21 -0800410 }});
411
412 registerService(Context.NFC_SERVICE, NfcManager.class,
413 new CachedServiceFetcher<NfcManager>() {
414 @Override
415 public NfcManager createService(ContextImpl ctx) {
416 return new NfcManager(ctx);
417 }});
418
419 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700420 new CachedServiceFetcher<DropBoxManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800421 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600422 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
423 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800424 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700425 return new DropBoxManager(ctx, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800426 }});
427
428 registerService(Context.INPUT_SERVICE, InputManager.class,
429 new StaticServiceFetcher<InputManager>() {
430 @Override
431 public InputManager createService() {
432 return InputManager.getInstance();
433 }});
434
435 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
436 new CachedServiceFetcher<DisplayManager>() {
437 @Override
438 public DisplayManager createService(ContextImpl ctx) {
439 return new DisplayManager(ctx.getOuterContext());
440 }});
441
Christine Franks39b03112018-07-03 14:46:07 -0700442 registerService(Context.COLOR_DISPLAY_SERVICE, ColorDisplayManager.class,
443 new CachedServiceFetcher<ColorDisplayManager>() {
444 @Override
445 public ColorDisplayManager createService(ContextImpl ctx) {
446 return new ColorDisplayManager();
447 }
448 });
449
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800450 // InputMethodManager has its own cache strategy based on display id to support apps that
451 // still assume InputMethodManager is a per-process singleton and it's safe to directly
452 // access internal fields via reflection. Hence directly use ServiceFetcher instead of
453 // StaticServiceFetcher/CachedServiceFetcher.
Jeff Brown6e539312015-02-24 18:53:21 -0800454 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800455 new ServiceFetcher<InputMethodManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800456 @Override
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800457 public InputMethodManager getService(ContextImpl ctx) {
458 return InputMethodManager.forContext(ctx.getOuterContext());
Jeff Brown6e539312015-02-24 18:53:21 -0800459 }});
460
461 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
Yohei Yukawa9f141ee2019-01-18 09:17:16 -0800462 new CachedServiceFetcher<TextServicesManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800463 @Override
Yohei Yukawa9f141ee2019-01-18 09:17:16 -0800464 public TextServicesManager createService(ContextImpl ctx)
465 throws ServiceNotFoundException {
466 return TextServicesManager.createInstance(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800467 }});
468
469 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
Nancy Zheng43f166d2016-11-18 18:15:09 -0800470 new CachedServiceFetcher<KeyguardManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800471 @Override
Nancy Zheng40cd8e42016-11-21 12:58:16 -0800472 public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nancy Zheng43f166d2016-11-18 18:15:09 -0800473 return new KeyguardManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800474 }});
475
476 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
477 new CachedServiceFetcher<LayoutInflater>() {
478 @Override
479 public LayoutInflater createService(ContextImpl ctx) {
480 return new PhoneLayoutInflater(ctx.getOuterContext());
481 }});
482
483 registerService(Context.LOCATION_SERVICE, LocationManager.class,
484 new CachedServiceFetcher<LocationManager>() {
485 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600486 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
487 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800488 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
489 }});
490
491 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
Svet Ganov16a16892015-04-16 10:32:04 -0700492 new CachedServiceFetcher<NetworkPolicyManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800493 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600494 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov16a16892015-04-16 10:32:04 -0700495 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600496 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
Jeff Brown6e539312015-02-24 18:53:21 -0800497 }});
498
499 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
500 new CachedServiceFetcher<NotificationManager>() {
501 @Override
502 public NotificationManager createService(ContextImpl ctx) {
503 final Context outerContext = ctx.getOuterContext();
504 return new NotificationManager(
505 new ContextThemeWrapper(outerContext,
506 Resources.selectSystemTheme(0,
507 outerContext.getApplicationInfo().targetSdkVersion,
508 com.android.internal.R.style.Theme_Dialog,
509 com.android.internal.R.style.Theme_Holo_Dialog,
510 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
511 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
512 ctx.mMainThread.getHandler());
513 }});
514
515 registerService(Context.NSD_SERVICE, NsdManager.class,
516 new CachedServiceFetcher<NsdManager>() {
517 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600518 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
519 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800520 INsdManager service = INsdManager.Stub.asInterface(b);
521 return new NsdManager(ctx.getOuterContext(), service);
522 }});
523
524 registerService(Context.POWER_SERVICE, PowerManager.class,
525 new CachedServiceFetcher<PowerManager>() {
526 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600527 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
528 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800529 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown6e539312015-02-24 18:53:21 -0800530 return new PowerManager(ctx.getOuterContext(),
531 service, ctx.mMainThread.getHandler());
532 }});
533
Tao Baoe8a403d2015-12-31 07:44:55 -0800534 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
535 new CachedServiceFetcher<RecoverySystem>() {
536 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600537 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
538 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
Tao Baoe8a403d2015-12-31 07:44:55 -0800539 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
Tao Baoe8a403d2015-12-31 07:44:55 -0800540 return new RecoverySystem(service);
541 }});
542
Jeff Brown6e539312015-02-24 18:53:21 -0800543 registerService(Context.SEARCH_SERVICE, SearchManager.class,
544 new CachedServiceFetcher<SearchManager>() {
545 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600546 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800547 return new SearchManager(ctx.getOuterContext(),
548 ctx.mMainThread.getHandler());
549 }});
550
551 registerService(Context.SENSOR_SERVICE, SensorManager.class,
552 new CachedServiceFetcher<SensorManager>() {
553 @Override
554 public SensorManager createService(ContextImpl ctx) {
555 return new SystemSensorManager(ctx.getOuterContext(),
556 ctx.mMainThread.getHandler().getLooper());
557 }});
558
Chad Brubaker90f391f2018-10-19 10:26:19 -0700559 registerService(Context.SENSOR_PRIVACY_SERVICE, SensorPrivacyManager.class,
560 new CachedServiceFetcher<SensorPrivacyManager>() {
561 @Override
562 public SensorPrivacyManager createService(ContextImpl ctx) {
563 return SensorPrivacyManager.getInstance(ctx);
564 }});
565
David Chenadaf8b32017-11-03 15:42:08 -0700566 registerService(Context.STATS_MANAGER, StatsManager.class,
Jeff Sharkey6b649252018-04-16 09:50:22 -0600567 new CachedServiceFetcher<StatsManager>() {
568 @Override
569 public StatsManager createService(ContextImpl ctx) {
570 return new StatsManager(ctx.getOuterContext());
571 }});
David Chenadaf8b32017-11-03 15:42:08 -0700572
Jeff Brown6e539312015-02-24 18:53:21 -0800573 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
574 new CachedServiceFetcher<StatusBarManager>() {
575 @Override
576 public StatusBarManager createService(ContextImpl ctx) {
577 return new StatusBarManager(ctx.getOuterContext());
578 }});
579
580 registerService(Context.STORAGE_SERVICE, StorageManager.class,
581 new CachedServiceFetcher<StorageManager>() {
582 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600583 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey48877892015-03-18 11:27:19 -0700584 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800585 }});
586
Jeff Sharkeye8cece92017-01-04 11:33:33 -0700587 registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
588 new CachedServiceFetcher<StorageStatsManager>() {
589 @Override
590 public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
591 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
592 ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
593 return new StorageStatsManager(ctx, service);
594 }});
595
Tao Bao07342dc2017-01-24 15:08:21 -0800596 registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class,
597 new CachedServiceFetcher<SystemUpdateManager>() {
598 @Override
599 public SystemUpdateManager createService(ContextImpl ctx)
600 throws ServiceNotFoundException {
601 IBinder b = ServiceManager.getServiceOrThrow(
602 Context.SYSTEM_UPDATE_SERVICE);
603 ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b);
604 return new SystemUpdateManager(service);
605 }});
606
Jeff Brown6e539312015-02-24 18:53:21 -0800607 registerService(Context.TELEPHONY_SERVICE, TelephonyManager.class,
608 new CachedServiceFetcher<TelephonyManager>() {
609 @Override
610 public TelephonyManager createService(ContextImpl ctx) {
611 return new TelephonyManager(ctx.getOuterContext());
612 }});
613
Chen Xu288b71c2019-09-15 18:28:21 -0700614 registerService(Context.TELEPHONY_REGISTRY_SERVICE, TelephonyRegistryManager.class,
615 new CachedServiceFetcher<TelephonyRegistryManager>() {
616 @Override
617 public TelephonyRegistryManager createService(ContextImpl ctx) {
Chen Xu1f6cfa52019-10-13 17:30:32 -0700618 return new TelephonyRegistryManager(ctx);
Chen Xu288b71c2019-09-15 18:28:21 -0700619 }});
620
Jeff Brown6e539312015-02-24 18:53:21 -0800621 registerService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
622 new CachedServiceFetcher<SubscriptionManager>() {
623 @Override
Jeff Sharkey717f52f2018-01-04 16:04:11 -0700624 public SubscriptionManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800625 return new SubscriptionManager(ctx.getOuterContext());
626 }});
627
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800628 registerService(Context.CARRIER_CONFIG_SERVICE, CarrierConfigManager.class,
629 new CachedServiceFetcher<CarrierConfigManager>() {
630 @Override
631 public CarrierConfigManager createService(ContextImpl ctx) {
Malcolm Chend1272d12018-02-28 15:20:45 -0800632 return new CarrierConfigManager(ctx.getOuterContext());
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800633 }});
634
Jeff Brown6e539312015-02-24 18:53:21 -0800635 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
636 new CachedServiceFetcher<TelecomManager>() {
637 @Override
638 public TelecomManager createService(ContextImpl ctx) {
639 return new TelecomManager(ctx.getOuterContext());
640 }});
641
Jeff Davidson35cda392017-02-27 09:46:00 -0800642 registerService(Context.EUICC_SERVICE, EuiccManager.class,
643 new CachedServiceFetcher<EuiccManager>() {
644 @Override
645 public EuiccManager createService(ContextImpl ctx) {
646 return new EuiccManager(ctx.getOuterContext());
647 }});
648
Holly Jiuyu Sun5c110242017-12-21 18:44:59 -0800649 registerService(Context.EUICC_CARD_SERVICE, EuiccCardManager.class,
650 new CachedServiceFetcher<EuiccCardManager>() {
651 @Override
652 public EuiccCardManager createService(ContextImpl ctx) {
653 return new EuiccCardManager(ctx.getOuterContext());
654 }});
655
Jeff Brown6e539312015-02-24 18:53:21 -0800656 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
657 new CachedServiceFetcher<UiModeManager>() {
658 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600659 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Tyler Gunndaef6682019-09-30 15:12:24 -0700660 return new UiModeManager(ctx.getOuterContext());
Jeff Brown6e539312015-02-24 18:53:21 -0800661 }});
662
663 registerService(Context.USB_SERVICE, UsbManager.class,
664 new CachedServiceFetcher<UsbManager>() {
665 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600666 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
667 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800668 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
669 }});
670
Kenny Rootf74bfde2018-01-18 15:42:48 -0800671 registerService(Context.ADB_SERVICE, AdbManager.class,
672 new CachedServiceFetcher<AdbManager>() {
673 @Override
674 public AdbManager createService(ContextImpl ctx)
675 throws ServiceNotFoundException {
676 IBinder b = ServiceManager.getServiceOrThrow(Context.ADB_SERVICE);
677 return new AdbManager(ctx, IAdbManager.Stub.asInterface(b));
678 }});
679
Jeff Brown6e539312015-02-24 18:53:21 -0800680 registerService(Context.SERIAL_SERVICE, SerialManager.class,
681 new CachedServiceFetcher<SerialManager>() {
682 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600683 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
684 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800685 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
686 }});
687
688 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
689 new CachedServiceFetcher<Vibrator>() {
690 @Override
691 public Vibrator createService(ContextImpl ctx) {
692 return new SystemVibrator(ctx);
693 }});
694
695 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
696 new CachedServiceFetcher<WallpaperManager>() {
697 @Override
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800698 public WallpaperManager createService(ContextImpl ctx)
699 throws ServiceNotFoundException {
Felipe Leme34a861a2019-08-05 16:00:12 -0700700 final IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
701 if (b == null) {
702 // There are 2 reason service can be null:
703 // 1.Device doesn't support it - that's fine
704 // 2.App is running on instant mode - should fail
705 final boolean enabled = Resources.getSystem()
706 .getBoolean(com.android.internal.R.bool.config_enableWallpaperService);
707 if (!enabled) {
708 // Life moves on...
709 return DisabledWallpaperManager.getInstance();
710 }
711 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
712 // Instant app
713 throw new ServiceNotFoundException(Context.WALLPAPER_SERVICE);
714 }
715 // Bad state - WallpaperManager methods will throw exception
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800716 }
717 IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
718 return new WallpaperManager(service, ctx.getOuterContext(),
Jeff Brown6e539312015-02-24 18:53:21 -0800719 ctx.mMainThread.getHandler());
720 }});
721
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700722 registerService(Context.LOWPAN_SERVICE, LowpanManager.class,
723 new CachedServiceFetcher<LowpanManager>() {
724 @Override
725 public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
726 IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE);
727 ILowpanManager service = ILowpanManager.Stub.asInterface(b);
728 return new LowpanManager(ctx.getOuterContext(), service,
729 ConnectivityThread.getInstanceLooper());
730 }});
731
Jeff Brown6e539312015-02-24 18:53:21 -0800732 registerService(Context.WIFI_SERVICE, WifiManager.class,
733 new CachedServiceFetcher<WifiManager>() {
734 @Override
Roshan Pius2161b392019-07-12 11:06:16 -0700735 public WifiManager createService(ContextImpl ctx) {
736 return new WifiManager(ctx.getOuterContext(),
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700737 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800738 }});
739
Jeff Brown6e539312015-02-24 18:53:21 -0800740 registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
741 new StaticServiceFetcher<WifiP2pManager>() {
742 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600743 public WifiP2pManager createService() throws ServiceNotFoundException {
744 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800745 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
746 return new WifiP2pManager(service);
747 }});
748
Etan Cohen04133272016-10-26 11:22:06 -0700749 registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class,
750 new CachedServiceFetcher<WifiAwareManager>() {
Etan Cohen20d329b2015-09-29 13:49:02 -0700751 @Override
Etan Cohen04133272016-10-26 11:22:06 -0700752 public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey589f3092016-11-29 12:54:39 -0700753 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE);
Etan Cohen04133272016-10-26 11:22:06 -0700754 IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b);
Etan Cohen20d329b2015-09-29 13:49:02 -0700755 if (service == null) {
756 return null;
757 }
Etan Cohen04133272016-10-26 11:22:06 -0700758 return new WifiAwareManager(ctx.getOuterContext(), service);
Etan Cohen20d329b2015-09-29 13:49:02 -0700759 }});
760
Jeff Brown6e539312015-02-24 18:53:21 -0800761 registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
762 new CachedServiceFetcher<WifiScanner>() {
763 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600764 public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException {
765 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800766 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
Mitchell Wills91312c72016-04-06 17:03:35 -0700767 return new WifiScanner(ctx.getOuterContext(), service,
768 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800769 }});
770
771 registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
772 new CachedServiceFetcher<RttManager>() {
Etan Cohen9accbba2018-01-30 16:41:00 -0800773 @Override
774 public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException {
775 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_RANGING_SERVICE);
776 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
777 return new RttManager(ctx.getOuterContext(),
778 new WifiRttManager(ctx.getOuterContext(), service));
779 }});
Jeff Brown6e539312015-02-24 18:53:21 -0800780
Etan Cohen46efb482017-12-07 13:50:57 -0800781 registerService(Context.WIFI_RTT_RANGING_SERVICE, WifiRttManager.class,
Etan Cohen17ba4722017-08-21 10:52:17 -0700782 new CachedServiceFetcher<WifiRttManager>() {
783 @Override
784 public WifiRttManager createService(ContextImpl ctx)
785 throws ServiceNotFoundException {
Etan Cohen46efb482017-12-07 13:50:57 -0800786 IBinder b = ServiceManager.getServiceOrThrow(
787 Context.WIFI_RTT_RANGING_SERVICE);
Etan Cohen17ba4722017-08-21 10:52:17 -0700788 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
789 return new WifiRttManager(ctx.getOuterContext(), service);
790 }});
791
Jeff Brown6e539312015-02-24 18:53:21 -0800792 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
793 new CachedServiceFetcher<EthernetManager>() {
794 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600795 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
796 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800797 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
798 return new EthernetManager(ctx.getOuterContext(), service);
799 }});
800
801 registerService(Context.WINDOW_SERVICE, WindowManager.class,
802 new CachedServiceFetcher<WindowManager>() {
803 @Override
804 public WindowManager createService(ContextImpl ctx) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700805 return new WindowManagerImpl(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800806 }});
807
808 registerService(Context.USER_SERVICE, UserManager.class,
809 new CachedServiceFetcher<UserManager>() {
810 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600811 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
812 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800813 IUserManager service = IUserManager.Stub.asInterface(b);
814 return new UserManager(ctx, service);
815 }});
816
817 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
818 new CachedServiceFetcher<AppOpsManager>() {
819 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600820 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
821 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800822 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
823 return new AppOpsManager(ctx, service);
824 }});
825
826 registerService(Context.CAMERA_SERVICE, CameraManager.class,
827 new CachedServiceFetcher<CameraManager>() {
828 @Override
829 public CameraManager createService(ContextImpl ctx) {
830 return new CameraManager(ctx);
831 }});
832
833 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
834 new CachedServiceFetcher<LauncherApps>() {
835 @Override
836 public LauncherApps createService(ContextImpl ctx) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700837 return new LauncherApps(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800838 }});
839
840 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
841 new CachedServiceFetcher<RestrictionsManager>() {
842 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600843 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
844 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800845 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
846 return new RestrictionsManager(ctx, service);
847 }});
848
849 registerService(Context.PRINT_SERVICE, PrintManager.class,
850 new CachedServiceFetcher<PrintManager>() {
851 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600852 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganovec7daa52017-06-30 12:02:59 -0700853 IPrintManager service = null;
854 // If the feature not present, don't try to look up every time
855 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
856 service = IPrintManager.Stub.asInterface(ServiceManager
857 .getServiceOrThrow(Context.PRINT_SERVICE));
858 }
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700859 final int userId = ctx.getUserId();
860 final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid);
861 return new PrintManager(ctx.getOuterContext(), service, userId, appId);
Jeff Brown6e539312015-02-24 18:53:21 -0800862 }});
863
Eugene Susla6ed45d82017-01-22 13:52:51 -0800864 registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
865 new CachedServiceFetcher<CompanionDeviceManager>() {
Svet Ganovec7daa52017-06-30 12:02:59 -0700866 @Override
867 public CompanionDeviceManager createService(ContextImpl ctx)
868 throws ServiceNotFoundException {
869 ICompanionDeviceManager service = null;
870 // If the feature not present, don't try to look up every time
871 if (ctx.getPackageManager().hasSystemFeature(
872 PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) {
873 service = ICompanionDeviceManager.Stub.asInterface(
874 ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE));
875 }
876 return new CompanionDeviceManager(service, ctx.getOuterContext());
877 }});
Eugene Susla6ed45d82017-01-22 13:52:51 -0800878
Jeff Brown6e539312015-02-24 18:53:21 -0800879 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
880 new CachedServiceFetcher<ConsumerIrManager>() {
881 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600882 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800883 return new ConsumerIrManager(ctx);
884 }});
885
886 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
887 new CachedServiceFetcher<MediaSessionManager>() {
888 @Override
889 public MediaSessionManager createService(ContextImpl ctx) {
890 return new MediaSessionManager(ctx);
891 }});
892
893 registerService(Context.TRUST_SERVICE, TrustManager.class,
894 new StaticServiceFetcher<TrustManager>() {
895 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600896 public TrustManager createService() throws ServiceNotFoundException {
897 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800898 return new TrustManager(b);
899 }});
900
901 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
902 new CachedServiceFetcher<FingerprintManager>() {
903 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600904 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600905 final IBinder binder;
906 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
907 binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
908 } else {
909 binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
910 }
Jeff Brown6e539312015-02-24 18:53:21 -0800911 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
912 return new FingerprintManager(ctx.getOuterContext(), service);
913 }});
914
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200915 registerService(Context.FACE_SERVICE, FaceManager.class,
916 new CachedServiceFetcher<FaceManager>() {
917 @Override
918 public FaceManager createService(ContextImpl ctx)
919 throws ServiceNotFoundException {
920 final IBinder binder;
921 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
922 binder = ServiceManager.getServiceOrThrow(Context.FACE_SERVICE);
923 } else {
924 binder = ServiceManager.getService(Context.FACE_SERVICE);
925 }
926 IFaceService service = IFaceService.Stub.asInterface(binder);
927 return new FaceManager(ctx.getOuterContext(), service);
928 }
929 });
930
Kevin Chyn51676d22018-11-05 18:00:43 -0800931 registerService(Context.IRIS_SERVICE, IrisManager.class,
932 new CachedServiceFetcher<IrisManager>() {
933 @Override
934 public IrisManager createService(ContextImpl ctx)
935 throws ServiceNotFoundException {
936 final IBinder binder =
937 ServiceManager.getServiceOrThrow(Context.IRIS_SERVICE);
938 IIrisService service = IIrisService.Stub.asInterface(binder);
939 return new IrisManager(ctx.getOuterContext(), service);
940 }
941 });
942
Kevin Chyn05c21502018-09-18 13:07:19 -0700943 registerService(Context.BIOMETRIC_SERVICE, BiometricManager.class,
944 new CachedServiceFetcher<BiometricManager>() {
945 @Override
946 public BiometricManager createService(ContextImpl ctx)
947 throws ServiceNotFoundException {
Kevin Chyne739daf2018-11-06 17:11:06 -0800948 if (BiometricManager.hasBiometrics(ctx)) {
949 final IBinder binder =
Ilya Matyukhine4675b32019-11-07 16:07:19 -0800950 ServiceManager.getServiceOrThrow(Context.AUTH_SERVICE);
951 final IAuthService service =
952 IAuthService.Stub.asInterface(binder);
Kevin Chyne739daf2018-11-06 17:11:06 -0800953 return new BiometricManager(ctx.getOuterContext(), service);
954 } else {
955 // Allow access to the manager when service is null. This saves memory
956 // on devices without biometric hardware.
957 return new BiometricManager(ctx.getOuterContext(), null);
958 }
Kevin Chyn05c21502018-09-18 13:07:19 -0700959 }
960 });
961
Jeff Brown6e539312015-02-24 18:53:21 -0800962 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700963 new CachedServiceFetcher<TvInputManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800964 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700965 public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600966 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800967 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700968 return new TvInputManager(service, ctx.getUserId());
Jeff Brown6e539312015-02-24 18:53:21 -0800969 }});
970
971 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
972 new CachedServiceFetcher<NetworkScoreManager>() {
973 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600974 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800975 return new NetworkScoreManager(ctx);
976 }});
977
978 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
979 new CachedServiceFetcher<UsageStatsManager>() {
980 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600981 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
982 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800983 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
984 return new UsageStatsManager(ctx.getOuterContext(), service);
985 }});
986
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000987 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
988 new CachedServiceFetcher<NetworkStatsManager>() {
989 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600990 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000991 return new NetworkStatsManager(ctx.getOuterContext());
992 }});
993
Jeff Brown6e539312015-02-24 18:53:21 -0800994 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
995 new StaticServiceFetcher<PersistentDataBlockManager>() {
996 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600997 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
998 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800999 IPersistentDataBlockService persistentDataBlockService =
1000 IPersistentDataBlockService.Stub.asInterface(b);
1001 if (persistentDataBlockService != null) {
1002 return new PersistentDataBlockManager(persistentDataBlockService);
1003 } else {
1004 // not supported
1005 return null;
1006 }
1007 }});
1008
Andrew Scull3b8b46f2017-02-13 18:12:15 +00001009 registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
1010 new StaticServiceFetcher<OemLockManager>() {
1011 @Override
1012 public OemLockManager createService() throws ServiceNotFoundException {
1013 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
1014 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
1015 if (oemLockService != null) {
1016 return new OemLockManager(oemLockService);
1017 } else {
1018 // not supported
1019 return null;
1020 }
1021 }});
1022
Jeff Brown6e539312015-02-24 18:53:21 -08001023 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
1024 new CachedServiceFetcher<MediaProjectionManager>() {
1025 @Override
1026 public MediaProjectionManager createService(ContextImpl ctx) {
1027 return new MediaProjectionManager(ctx);
1028 }});
1029
1030 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
1031 new CachedServiceFetcher<AppWidgetManager>() {
1032 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001033 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1034 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -08001035 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
1036 }});
1037
1038 registerService(Context.MIDI_SERVICE, MidiManager.class,
1039 new CachedServiceFetcher<MidiManager>() {
1040 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001041 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1042 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
Mike Lockwoode0a6ca62015-06-04 13:43:56 -07001043 return new MidiManager(IMidiManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -08001044 }});
Eric Laurent2035ac82015-03-05 15:18:44 -08001045
1046 registerService(Context.RADIO_SERVICE, RadioManager.class,
1047 new CachedServiceFetcher<RadioManager>() {
1048 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -07001049 public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Eric Laurent2035ac82015-03-05 15:18:44 -08001050 return new RadioManager(ctx);
1051 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001052
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001053 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
1054 new CachedServiceFetcher<HardwarePropertiesManager>() {
1055 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001056 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1057 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
1058 IHardwarePropertiesManager service =
1059 IHardwarePropertiesManager.Stub.asInterface(b);
1060 return new HardwarePropertiesManager(ctx, service);
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001061 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001062
1063 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
1064 new CachedServiceFetcher<SoundTriggerManager>() {
1065 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001066 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1067 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001068 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
1069 }});
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001070
1071 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
1072 new CachedServiceFetcher<ShortcutManager>() {
Makoto Onuki55046222016-03-08 10:49:47 -08001073 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001074 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1075 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
1076 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
Makoto Onuki55046222016-03-08 10:49:47 -08001077 }});
Joe Onorato713fec82016-03-04 10:34:02 -08001078
Hyunyoung Song880a9512018-12-20 11:24:48 -08001079 registerService(Context.OVERLAY_SERVICE, OverlayManager.class,
1080 new CachedServiceFetcher<OverlayManager>() {
1081 @Override
1082 public OverlayManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1083 IBinder b = ServiceManager.getServiceOrThrow(Context.OVERLAY_SERVICE);
1084 return new OverlayManager(ctx, IOverlayManager.Stub.asInterface(b));
1085 }});
1086
Ricky Wai1a6e6672017-10-27 14:46:01 +01001087 registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class,
1088 new CachedServiceFetcher<NetworkWatchlistManager>() {
1089 @Override
1090 public NetworkWatchlistManager createService(ContextImpl ctx)
1091 throws ServiceNotFoundException {
1092 IBinder b =
1093 ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE);
1094 return new NetworkWatchlistManager(ctx,
1095 INetworkWatchlistManager.Stub.asInterface(b));
1096 }});
1097
Joe Onorato713fec82016-03-04 10:34:02 -08001098 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
1099 new CachedServiceFetcher<SystemHealthManager>() {
1100 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001101 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1102 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
1103 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
Joe Onorato713fec82016-03-04 10:34:02 -08001104 }});
Peng Xu9ff7d222016-02-11 13:02:05 -08001105
1106 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
1107 new CachedServiceFetcher<ContextHubManager>() {
1108 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001109 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Peng Xu9ff7d222016-02-11 13:02:05 -08001110 return new ContextHubManager(ctx.getOuterContext(),
1111 ctx.mMainThread.getHandler().getLooper());
1112 }});
Joe Onorato1754d742016-11-21 17:51:35 -08001113
1114 registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
1115 new CachedServiceFetcher<IncidentManager>() {
1116 @Override
1117 public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1118 return new IncidentManager(ctx);
1119 }});
Clara Bayarri04d72ab2017-01-10 09:31:51 -08001120
Nandana Duttd11850c2018-12-12 17:26:57 +00001121 registerService(Context.BUGREPORT_SERVICE, BugreportManager.class,
1122 new CachedServiceFetcher<BugreportManager>() {
1123 @Override
1124 public BugreportManager createService(ContextImpl ctx)
1125 throws ServiceNotFoundException {
1126 IBinder b = ServiceManager.getServiceOrThrow(Context.BUGREPORT_SERVICE);
1127 return new BugreportManager(ctx.getOuterContext(),
1128 IDumpstate.Stub.asInterface(b));
1129 }});
1130
Felipe Leme640f30a2017-03-06 15:44:06 -08001131 registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
1132 new CachedServiceFetcher<AutofillManager>() {
Felipe Leme3461d3c2017-01-19 08:54:55 -08001133 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -08001134 public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov43574b02017-04-12 09:25:20 -07001135 // Get the services without throwing as this is an optional feature
1136 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
Svet Ganov782043c2017-02-11 00:52:02 +00001137 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
Felipe Leme640f30a2017-03-06 15:44:06 -08001138 return new AutofillManager(ctx.getOuterContext(), service);
Felipe Leme3461d3c2017-01-19 08:54:55 -08001139 }});
Zak Cohen56345f42017-01-26 13:54:28 -08001140
Felipe Lemeecb08be2018-11-27 15:48:47 -08001141 registerService(Context.CONTENT_CAPTURE_MANAGER_SERVICE, ContentCaptureManager.class,
1142 new CachedServiceFetcher<ContentCaptureManager>() {
Felipe Lemee348dc32018-11-05 12:35:29 -08001143 @Override
Felipe Lemeecb08be2018-11-27 15:48:47 -08001144 public ContentCaptureManager createService(ContextImpl ctx)
Felipe Lemee348dc32018-11-05 12:35:29 -08001145 throws ServiceNotFoundException {
1146 // Get the services without throwing as this is an optional feature
Felipe Lemeecb08be2018-11-27 15:48:47 -08001147 Context outerContext = ctx.getOuterContext();
Felipe Leme326f15a2019-02-19 09:42:24 -08001148 ContentCaptureOptions options = outerContext.getContentCaptureOptions();
1149 // Options is null when the service didn't whitelist the activity or package
Felipe Lemecbf7f262019-04-17 13:57:59 -07001150 if (options != null && (options.lite || options.isWhitelisted(outerContext))) {
Felipe Lemeecb08be2018-11-27 15:48:47 -08001151 IBinder b = ServiceManager
1152 .getService(Context.CONTENT_CAPTURE_MANAGER_SERVICE);
Felipe Leme749b8892018-12-03 16:30:30 -08001153 IContentCaptureManager service = IContentCaptureManager.Stub.asInterface(b);
Felipe Leme326f15a2019-02-19 09:42:24 -08001154 // Service is null when not provided by OEM or disabled by kill-switch.
Felipe Lemed49d52c2019-02-15 09:48:20 -08001155 if (service != null) {
Felipe Leme326f15a2019-02-19 09:42:24 -08001156 return new ContentCaptureManager(outerContext, service, options);
Felipe Lemed49d52c2019-02-15 09:48:20 -08001157 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001158 }
Felipe Leme326f15a2019-02-19 09:42:24 -08001159 // When feature is disabled or app / package not whitelisted, we return a null
1160 // manager to apps so the performance impact is practically zero
Felipe Lemeecb08be2018-11-27 15:48:47 -08001161 return null;
Felipe Lemee348dc32018-11-05 12:35:29 -08001162 }});
1163
Sunny Goyal54e91342018-11-14 11:59:02 -08001164 registerService(Context.APP_PREDICTION_SERVICE, AppPredictionManager.class,
1165 new CachedServiceFetcher<AppPredictionManager>() {
1166 @Override
1167 public AppPredictionManager createService(ContextImpl ctx)
1168 throws ServiceNotFoundException {
Felipe Leme6378bd42019-08-14 18:14:55 -07001169 IBinder b = ServiceManager.getService(Context.APP_PREDICTION_SERVICE);
1170 return b == null ? null : new AppPredictionManager(ctx);
Sunny Goyal54e91342018-11-14 11:59:02 -08001171 }
1172 });
1173
Winson Chung3fb0f252019-01-08 17:41:55 -08001174 registerService(Context.CONTENT_SUGGESTIONS_SERVICE,
1175 ContentSuggestionsManager.class,
1176 new CachedServiceFetcher<ContentSuggestionsManager>() {
1177 @Override
1178 public ContentSuggestionsManager createService(ContextImpl ctx) {
1179 // No throw as this is an optional service
1180 IBinder b = ServiceManager.getService(
1181 Context.CONTENT_SUGGESTIONS_SERVICE);
1182 IContentSuggestionsManager service =
1183 IContentSuggestionsManager.Stub.asInterface(b);
Zak Cohen4834e9f2019-03-08 12:59:01 -08001184 return new ContentSuggestionsManager(ctx.getUserId(), service);
Winson Chung3fb0f252019-01-08 17:41:55 -08001185 }
1186 });
1187
Zak Cohen56345f42017-01-26 13:54:28 -08001188 registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
1189 @Override
1190 public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1191 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
1192 return new VrManager(IVrManager.Stub.asInterface(b));
1193 }
1194 });
Neil Fuller328532a2017-03-16 18:32:21 +00001195
1196 registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
1197 new CachedServiceFetcher<RulesManager>() {
1198 @Override
1199 public RulesManager createService(ContextImpl ctx) {
1200 return new RulesManager(ctx.getOuterContext());
1201 }});
Tony Mak1b708e62017-10-12 10:59:11 +01001202
1203 registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class,
1204 new CachedServiceFetcher<CrossProfileApps>() {
1205 @Override
1206 public CrossProfileApps createService(ContextImpl ctx)
1207 throws ServiceNotFoundException {
1208 IBinder b = ServiceManager.getServiceOrThrow(
1209 Context.CROSS_PROFILE_APPS_SERVICE);
1210 return new CrossProfileApps(ctx.getOuterContext(),
1211 ICrossProfileApps.Stub.asInterface(b));
1212 }
1213 });
Jason Monk8f5f7ff2017-10-17 14:12:42 -04001214
1215 registerService(Context.SLICE_SERVICE, SliceManager.class,
1216 new CachedServiceFetcher<SliceManager>() {
1217 @Override
1218 public SliceManager createService(ContextImpl ctx)
1219 throws ServiceNotFoundException {
1220 return new SliceManager(ctx.getOuterContext(),
1221 ctx.mMainThread.getHandler());
1222 }
1223 });
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001224
Neil Fullerfeeee682018-05-30 14:35:24 +01001225 registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class,
1226 new CachedServiceFetcher<TimeDetector>() {
1227 @Override
1228 public TimeDetector createService(ContextImpl ctx)
1229 throws ServiceNotFoundException {
1230 return new TimeDetector();
1231 }});
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001232
1233 registerService(Context.PERMISSION_SERVICE, PermissionManager.class,
1234 new CachedServiceFetcher<PermissionManager>() {
1235 @Override
1236 public PermissionManager createService(ContextImpl ctx) {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07001237 IPackageManager packageManager = AppGlobals.getPackageManager();
1238 return new PermissionManager(ctx.getOuterContext(), packageManager);
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001239 }});
Hai Zhang4ef21d02018-11-09 14:43:51 -08001240
Philip P. Moltmannbc054d82018-12-21 09:41:58 -08001241 registerService(Context.PERMISSION_CONTROLLER_SERVICE, PermissionControllerManager.class,
1242 new CachedServiceFetcher<PermissionControllerManager>() {
1243 @Override
1244 public PermissionControllerManager createService(ContextImpl ctx) {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07001245 return new PermissionControllerManager(ctx.getOuterContext(),
1246 ctx.getMainThreadHandler());
Philip P. Moltmannbc054d82018-12-21 09:41:58 -08001247 }});
1248
Hai Zhang4ef21d02018-11-09 14:43:51 -08001249 registerService(Context.ROLE_SERVICE, RoleManager.class,
1250 new CachedServiceFetcher<RoleManager>() {
1251 @Override
1252 public RoleManager createService(ContextImpl ctx)
1253 throws ServiceNotFoundException {
1254 return new RoleManager(ctx.getOuterContext());
1255 }});
Richard Uhlerb29f1452018-09-12 16:38:15 +01001256
Hai Zhanga4959e52019-03-06 12:21:07 -08001257 registerService(Context.ROLE_CONTROLLER_SERVICE, RoleControllerManager.class,
1258 new CachedServiceFetcher<RoleControllerManager>() {
1259 @Override
1260 public RoleControllerManager createService(ContextImpl ctx)
1261 throws ServiceNotFoundException {
1262 return new RoleControllerManager(ctx.getOuterContext());
1263 }});
1264
Richard Uhlerb29f1452018-09-12 16:38:15 +01001265 registerService(Context.ROLLBACK_SERVICE, RollbackManager.class,
1266 new CachedServiceFetcher<RollbackManager>() {
1267 @Override
1268 public RollbackManager createService(ContextImpl ctx)
1269 throws ServiceNotFoundException {
1270 IBinder b = ServiceManager.getServiceOrThrow(Context.ROLLBACK_SERVICE);
1271 return new RollbackManager(ctx.getOuterContext(),
1272 IRollbackManager.Stub.asInterface(b));
1273 }});
Howard Chen0a947642019-01-07 14:10:44 +08001274
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001275 registerService(Context.DYNAMIC_SYSTEM_SERVICE, DynamicSystemManager.class,
1276 new CachedServiceFetcher<DynamicSystemManager>() {
Howard Chen0a947642019-01-07 14:10:44 +08001277 @Override
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001278 public DynamicSystemManager createService(ContextImpl ctx)
Howard Chen0a947642019-01-07 14:10:44 +08001279 throws ServiceNotFoundException {
1280 IBinder b = ServiceManager.getServiceOrThrow(
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001281 Context.DYNAMIC_SYSTEM_SERVICE);
1282 return new DynamicSystemManager(
1283 IDynamicSystemService.Stub.asInterface(b));
Howard Chen0a947642019-01-07 14:10:44 +08001284 }});
Roshan Pius848513e2019-10-11 13:44:00 -07001285 registerService(Context.BATTERY_STATS_SERVICE, BatteryStatsManager.class,
1286 new CachedServiceFetcher<BatteryStatsManager>() {
1287 @Override
1288 public BatteryStatsManager createService(ContextImpl ctx)
1289 throws ServiceNotFoundException {
1290 IBinder b = ServiceManager.getServiceOrThrow(
1291 Context.BATTERY_STATS_SERVICE);
1292 return new BatteryStatsManager(
1293 IBatteryStats.Stub.asInterface(b));
1294 }});
Felipe Lemeecb08be2018-11-27 15:48:47 -08001295 //CHECKSTYLE:ON IndentationCheck
Makoto Onuki54c4e032019-10-30 12:05:20 -07001296
Makoto Onuki05b14f52019-11-05 12:36:07 -08001297 sInitializing = true;
1298 try {
1299 // Note: the following functions need to be @SystemApis, once they become mainline
1300 // modules.
Sudheer Shankaf5b36962019-10-04 16:16:13 -07001301
Makoto Onuki05b14f52019-11-05 12:36:07 -08001302 JobSchedulerFrameworkInitializer.registerServiceWrappers();
1303 BlobStoreManagerFrameworkInitializer.initialize();
1304 } finally {
1305 // If any of the above code throws, we're in a pretty bad shape and the process
1306 // will likely crash, but we'll reset it just in case there's an exception handler...
1307 sInitializing = false;
1308 }
Jeff Brown6e539312015-02-24 18:53:21 -08001309 }
1310
Makoto Onuki05b14f52019-11-05 12:36:07 -08001311 /** Throws {@link IllegalStateException} if not during a static initialization. */
1312 private static void ensureInitializing(String methodName) {
1313 Preconditions.checkState(sInitializing, "Internal error: " + methodName
1314 + " can only be called during class initialization.");
1315 }
Jeff Brown6e539312015-02-24 18:53:21 -08001316 /**
1317 * Creates an array which is used to cache per-Context service instances.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001318 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001319 */
1320 public static Object[] createServiceCache() {
1321 return new Object[sServiceCacheSize];
1322 }
1323
1324 /**
1325 * Gets a system service from a given context.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001326 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001327 */
1328 public static Object getSystemService(ContextImpl ctx, String name) {
1329 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
1330 return fetcher != null ? fetcher.getService(ctx) : null;
1331 }
1332
1333 /**
Paul McLeane3383cc2015-05-08 11:41:20 -07001334 * Gets the name of the system-level service that is represented by the specified class.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001335 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001336 */
1337 public static String getSystemServiceName(Class<?> serviceClass) {
1338 return SYSTEM_SERVICE_NAMES.get(serviceClass);
1339 }
1340
1341 /**
1342 * Statically registers a system service with the context.
1343 * This method must be called during static initialization only.
1344 */
Makoto Onuki05b14f52019-11-05 12:36:07 -08001345 private static <T> void registerService(@NonNull String serviceName,
1346 @NonNull Class<T> serviceClass, @NonNull ServiceFetcher<T> serviceFetcher) {
Jeff Brown6e539312015-02-24 18:53:21 -08001347 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
1348 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
1349 }
1350
1351 /**
Makoto Onuki05b14f52019-11-05 12:36:07 -08001352 * Callback interface used as a parameter to {@link #registerStaticService(
1353 * String, Class, StaticServiceProducerNoBinder)}, which generates a service wrapper instance
1354 * that's not tied to any context and does not take a service binder object in the constructor.
1355 *
1356 * @param <TServiceClass> type of the service wrapper class.
Makoto Onuki792de8d2019-07-16 16:19:01 -07001357 *
1358 * @hide
1359 */
Makoto Onuki05b14f52019-11-05 12:36:07 -08001360 //@SystemApi TODO Make it a system API.
1361 public interface StaticServiceProducerNoBinder<TServiceClass> {
1362 /**
1363 * Return a new service wrapper of type {@code TServiceClass}.
1364 */
1365 TServiceClass createService();
1366 }
1367
1368 /**
1369 * Callback interface used as a parameter to {@link #registerStaticService(
1370 * String, Class, StaticServiceProducerWithBinder)}, which generates a service wrapper instance
1371 * that's not tied to any context and takes a service binder object in the constructor.
1372 *
1373 * @param <TServiceClass> type of the service wrapper class.
1374 *
1375 * @hide
1376 */
1377 //@SystemApi TODO Make it a system API.
1378 public interface StaticServiceProducerWithBinder<TServiceClass> {
1379 /**
1380 * Return a new service wrapper of type {@code TServiceClass} backed by a given
1381 * service binder object.
1382 */
1383 TServiceClass createService(IBinder serviceBinder);
1384 }
1385
1386 /**
1387 * Callback interface used as a parameter to {@link #registerContextAwareService(
1388 * String, Class, ContextAwareServiceProducerNoBinder)},
1389 * which generates a service wrapper instance
1390 * that's tied to a specific context and does not take a service binder object in the
1391 * constructor.
1392 *
1393 * @param <TServiceClass> type of the service wrapper class.
1394 *
1395 * @hide
1396 */
1397 //@SystemApi TODO Make it a system API.
1398 public interface ContextAwareServiceProducerNoBinder<TServiceClass> {
1399 /**
1400 * Return a new service wrapper of type {@code TServiceClass} tied to a given
1401 * {@code context}.
1402 *
1403 * TODO Do we need to pass the "base context" too?
1404 */
1405 TServiceClass createService(Context context);
1406 }
1407
1408 /**
1409 * Callback interface used as a parameter to {@link #registerContextAwareService(
1410 * String, Class, ContextAwareServiceProducerWithBinder)},
1411 * which generates a service wrapper instance
1412 * that's tied to a specific context and takes a service binder object in the constructor.
1413 *
1414 * @param <TServiceClass> type of the service wrapper class.
1415 *
1416 * @hide
1417 */
1418 //@SystemApi TODO Make it a system API.
1419 public interface ContextAwareServiceProducerWithBinder<TServiceClass> {
1420 /**
1421 * Return a new service wrapper of type {@code TServiceClass} backed by a given
1422 * service binder object that's tied to a given {@code context}.
1423 *
1424 * TODO Do we need to pass the "base context" too?
1425 */
1426 TServiceClass createService(Context context, IBinder serviceBinder);
1427 }
1428
1429 /**
1430 * Used by apex modules to register a "service wrapper" that is not tied to any {@link Context}.
1431 *
1432 * <p>This can only be called from the methods called by the static initializer of
1433 * {@link SystemServiceRegistry}. (Otherwise it throws a {@link IllegalStateException}.)
1434 *
1435 * @param serviceName the name of the binder object, such as
1436 * {@link Context#JOB_SCHEDULER_SERVICE}.
1437 * @param serviceWrapperClass the wrapper class, such as the class of
1438 * {@link android.app.job.JobScheduler}.
1439 * @param serviceProducer Callback that takes the service binder object with the name
1440 * {@code serviceName} and returns an actual service wrapper instance.
1441 *
1442 * @hide
1443 */
1444 //@SystemApi TODO Make it a system API.
1445 public static <TServiceClass> void registerStaticService(
1446 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1447 @NonNull StaticServiceProducerWithBinder<TServiceClass> serviceProducer) {
1448 ensureInitializing("registerStaticService");
1449 Preconditions.checkStringNotEmpty(serviceName);
1450 Preconditions.checkNotNull(serviceWrapperClass);
1451 Preconditions.checkNotNull(serviceProducer);
1452
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001453 registerService(serviceName, serviceWrapperClass,
Makoto Onuki05b14f52019-11-05 12:36:07 -08001454 new StaticServiceFetcher<TServiceClass>() {
Makoto Onuki792de8d2019-07-16 16:19:01 -07001455 @Override
Makoto Onuki05b14f52019-11-05 12:36:07 -08001456 public TServiceClass createService() throws ServiceNotFoundException {
1457 return serviceProducer.createService(
1458 ServiceManager.getServiceOrThrow(serviceName));
Makoto Onuki792de8d2019-07-16 16:19:01 -07001459 }});
1460 }
1461
1462 /**
Makoto Onuki05b14f52019-11-05 12:36:07 -08001463 * Similar to {@link #registerStaticService(String, Class, StaticServiceProducerWithBinder)},
1464 * but used for a "service wrapper" that doesn't take a service binder in its constructor.
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001465 *
1466 * @hide
1467 */
Makoto Onuki05b14f52019-11-05 12:36:07 -08001468 //@SystemApi TODO Make it a system API.
1469 public static <TServiceClass> void registerStaticService(
1470 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1471 @NonNull StaticServiceProducerNoBinder<TServiceClass> serviceProducer) {
1472 ensureInitializing("registerStaticService");
1473 Preconditions.checkStringNotEmpty(serviceName);
1474 Preconditions.checkNotNull(serviceWrapperClass);
1475 Preconditions.checkNotNull(serviceProducer);
1476
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001477 registerService(serviceName, serviceWrapperClass,
Makoto Onuki05b14f52019-11-05 12:36:07 -08001478 new StaticServiceFetcher<TServiceClass>() {
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001479 @Override
Makoto Onuki05b14f52019-11-05 12:36:07 -08001480 public TServiceClass createService() {
1481 return serviceProducer.createService();
1482 }});
1483 }
1484
1485 /**
1486 * Used by apex modules to register a "service wrapper" that is tied to a specific
1487 * {@link Context}.
1488 *
1489 * <p>This can only be called from the methods called by the static initializer of
1490 * {@link SystemServiceRegistry}. (Otherwise it throws a {@link IllegalStateException}.)
1491 *
1492 * @param serviceName the name of the binder object, such as
1493 * {@link Context#JOB_SCHEDULER_SERVICE}.
1494 * @param serviceWrapperClass the wrapper class, such as the class of
1495 * {@link android.app.job.JobScheduler}.
1496 * @param serviceProducer lambda that takes the service binder object with the name
1497 * {@code serviceName}, a {@link Context} and returns an actual service wrapper instance.
1498 *
1499 * @hide
1500 */
1501 //@SystemApi TODO Make it a system API.
1502 public static <TServiceClass> void registerContextAwareService(
1503 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1504 @NonNull ContextAwareServiceProducerWithBinder<TServiceClass> serviceProducer) {
1505 ensureInitializing("registerContextAwareService");
1506 Preconditions.checkStringNotEmpty(serviceName);
1507 Preconditions.checkNotNull(serviceWrapperClass);
1508 Preconditions.checkNotNull(serviceProducer);
1509
1510 registerService(serviceName, serviceWrapperClass,
1511 new CachedServiceFetcher<TServiceClass>() {
1512 @Override
1513 public TServiceClass createService(ContextImpl ctx)
1514 throws ServiceNotFoundException {
1515 return serviceProducer.createService(
1516 ctx.getOuterContext(),
1517 ServiceManager.getServiceOrThrow(serviceName));
1518 }});
1519 }
1520
1521
1522 /**
1523 * Similar to {@link #registerContextAwareService(String, Class,
1524 * ContextAwareServiceProducerWithBinder)},
1525 * but used for a "service wrapper" that doesn't take a service binder in its constructor.
1526 *
1527 * @hide
1528 */
1529 //@SystemApi TODO Make it a system API.
1530 public static <TServiceClass> void registerContextAwareService(
1531 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1532 @NonNull ContextAwareServiceProducerNoBinder<TServiceClass> serviceProducer) {
1533 ensureInitializing("registerContextAwareService");
1534 Preconditions.checkStringNotEmpty(serviceName);
1535 Preconditions.checkNotNull(serviceWrapperClass);
1536 Preconditions.checkNotNull(serviceProducer);
1537
1538 registerService(serviceName, serviceWrapperClass,
1539 new CachedServiceFetcher<TServiceClass>() {
1540 @Override
1541 public TServiceClass createService(ContextImpl ctx) {
1542 return serviceProducer.createService(ctx.getOuterContext());
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001543 }});
1544 }
1545
1546 /**
Jeff Brown6e539312015-02-24 18:53:21 -08001547 * Base interface for classes that fetch services.
1548 * These objects must only be created during static initialization.
1549 */
1550 static abstract interface ServiceFetcher<T> {
1551 T getService(ContextImpl ctx);
1552 }
1553
1554 /**
1555 * Override this class when the system service constructor needs a
1556 * ContextImpl and should be cached and retained by that context.
1557 */
1558 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
1559 private final int mCacheIndex;
1560
Makoto Onukid67070e2018-03-30 12:39:14 -07001561 CachedServiceFetcher() {
1562 // Note this class must be instantiated only by the static initializer of the
1563 // outer class (SystemServiceRegistry), which already does the synchronization,
1564 // so bare access to sServiceCacheSize is okay here.
Jeff Brown6e539312015-02-24 18:53:21 -08001565 mCacheIndex = sServiceCacheSize++;
1566 }
1567
1568 @Override
1569 @SuppressWarnings("unchecked")
1570 public final T getService(ContextImpl ctx) {
1571 final Object[] cache = ctx.mServiceCache;
Makoto Onukid67070e2018-03-30 12:39:14 -07001572 final int[] gates = ctx.mServiceInitializationStateArray;
Makoto Onukif896f122018-01-25 09:50:24 -08001573
Makoto Onukid67070e2018-03-30 12:39:14 -07001574 for (;;) {
1575 boolean doInitialize = false;
1576 synchronized (cache) {
1577 // Return it if we already have a cached instance.
1578 T service = (T) cache[mCacheIndex];
1579 if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) {
1580 return service;
1581 }
Makoto Onukif896f122018-01-25 09:50:24 -08001582
Makoto Onukid67070e2018-03-30 12:39:14 -07001583 // If we get here, there's no cached instance.
Makoto Onukif896f122018-01-25 09:50:24 -08001584
Makoto Onukid67070e2018-03-30 12:39:14 -07001585 // Grr... if gate is STATE_READY, then this means we initialized the service
1586 // once but someone cleared it.
1587 // We start over from STATE_UNINITIALIZED.
1588 if (gates[mCacheIndex] == ContextImpl.STATE_READY) {
1589 gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED;
1590 }
1591
1592 // It's possible for multiple threads to get here at the same time, so
1593 // use the "gate" to make sure only the first thread will call createService().
1594
1595 // At this point, the gate must be either UNINITIALIZED or INITIALIZING.
1596 if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) {
1597 doInitialize = true;
1598 gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING;
1599 }
Makoto Onukif896f122018-01-25 09:50:24 -08001600 }
1601
Makoto Onukid67070e2018-03-30 12:39:14 -07001602 if (doInitialize) {
1603 // Only the first thread gets here.
Makoto Onukif896f122018-01-25 09:50:24 -08001604
Makoto Onukid67070e2018-03-30 12:39:14 -07001605 T service = null;
1606 @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND;
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001607 try {
Makoto Onukid67070e2018-03-30 12:39:14 -07001608 // This thread is the first one to get here. Instantiate the service
1609 // *without* the cache lock held.
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001610 service = createService(ctx);
Makoto Onukid67070e2018-03-30 12:39:14 -07001611 newState = ContextImpl.STATE_READY;
Makoto Onukif896f122018-01-25 09:50:24 -08001612
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001613 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001614 onServiceNotFound(e);
Makoto Onukif896f122018-01-25 09:50:24 -08001615
Makoto Onukid67070e2018-03-30 12:39:14 -07001616 } finally {
1617 synchronized (cache) {
1618 cache[mCacheIndex] = service;
1619 gates[mCacheIndex] = newState;
1620 cache.notifyAll();
1621 }
1622 }
1623 return service;
1624 }
1625 // The other threads will wait for the first thread to call notifyAll(),
1626 // and go back to the top and retry.
1627 synchronized (cache) {
1628 while (gates[mCacheIndex] < ContextImpl.STATE_READY) {
1629 try {
1630 cache.wait();
1631 } catch (InterruptedException e) {
1632 Log.w(TAG, "getService() interrupted");
1633 Thread.currentThread().interrupt();
1634 return null;
1635 }
Makoto Onukif896f122018-01-25 09:50:24 -08001636 }
1637 }
Makoto Onukif896f122018-01-25 09:50:24 -08001638 }
Jeff Brown6e539312015-02-24 18:53:21 -08001639 }
1640
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001641 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001642 }
1643
1644 /**
1645 * Override this class when the system service does not need a ContextImpl
1646 * and should be cached and retained process-wide.
1647 */
1648 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
1649 private T mCachedInstance;
1650
1651 @Override
Jeff Sharkey589f3092016-11-29 12:54:39 -07001652 public final T getService(ContextImpl ctx) {
Jeff Brown6e539312015-02-24 18:53:21 -08001653 synchronized (StaticServiceFetcher.this) {
1654 if (mCachedInstance == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001655 try {
1656 mCachedInstance = createService();
1657 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001658 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001659 }
Jeff Brown6e539312015-02-24 18:53:21 -08001660 }
1661 return mCachedInstance;
1662 }
1663 }
1664
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001665 public abstract T createService() throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001666 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001667
1668 /**
Lorenzo Colitticf959772016-02-29 16:07:37 +09001669 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
1670 * creating the service for the first time, passes it the application context of the creating
1671 * application.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001672 *
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001673 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
1674 * case where multiple application components each have their own ConnectivityManager object.
1675 */
Lorenzo Colitticf959772016-02-29 16:07:37 +09001676 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001677 private T mCachedInstance;
1678
1679 @Override
1680 public final T getService(ContextImpl ctx) {
Lorenzo Colitticf959772016-02-29 16:07:37 +09001681 synchronized (StaticApplicationContextServiceFetcher.this) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001682 if (mCachedInstance == null) {
Lorenzo Colitti0ad9ab02016-03-25 17:40:11 +09001683 Context appContext = ctx.getApplicationContext();
1684 // If the application context is null, we're either in the system process or
1685 // it's the application context very early in app initialization. In both these
1686 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
1687 // to the service. http://b/27532714 .
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001688 try {
1689 mCachedInstance = createService(appContext != null ? appContext : ctx);
1690 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001691 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001692 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001693 }
1694 return mCachedInstance;
1695 }
1696 }
1697
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001698 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001699 }
Jeff Sharkey589f3092016-11-29 12:54:39 -07001700
Makoto Onuki05b14f52019-11-05 12:36:07 -08001701 /** @hide */
Jeff Sharkey589f3092016-11-29 12:54:39 -07001702 public static void onServiceNotFound(ServiceNotFoundException e) {
1703 // We're mostly interested in tracking down long-lived core system
1704 // components that might stumble if they obtain bad references; just
1705 // emit a tidy log message for normal apps
1706 if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1707 Log.wtf(TAG, e.getMessage(), e);
1708 } else {
1709 Log.w(TAG, e.getMessage());
1710 }
1711 }
Jeff Brown6e539312015-02-24 18:53:21 -08001712}