blob: 5effa1b9981337c281edea86ab5d81fb839e8c0d [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;
Terry Wangecc0d1b2019-10-17 17:05:18 -070025import android.app.appsearch.AppSearchManagerFrameworkInitializer;
Sudheer Shankaf5b36962019-10-04 16:16:13 -070026import android.app.blob.BlobStoreManagerFrameworkInitializer;
Winson Chung3fb0f252019-01-08 17:41:55 -080027import android.app.contentsuggestions.ContentSuggestionsManager;
28import android.app.contentsuggestions.IContentSuggestionsManager;
Makoto Onuki54c4e032019-10-30 12:05:20 -070029import android.app.job.JobSchedulerFrameworkInitializer;
Sunny Goyal54e91342018-11-14 11:59:02 -080030import android.app.prediction.AppPredictionManager;
Hai Zhanga4959e52019-03-06 12:21:07 -080031import android.app.role.RoleControllerManager;
Hai Zhang4ef21d02018-11-09 14:43:51 -080032import android.app.role.RoleManager;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040033import android.app.slice.SliceManager;
Neil Fullerfeeee682018-05-30 14:35:24 +010034import android.app.timedetector.TimeDetector;
Neil Fuller328532a2017-03-16 18:32:21 +000035import android.app.timezone.RulesManager;
Jeff Brown6e539312015-02-24 18:53:21 -080036import android.app.trust.TrustManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070037import android.app.usage.IStorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080038import android.app.usage.IUsageStatsManager;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000039import android.app.usage.NetworkStatsManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070040import android.app.usage.StorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080041import android.app.usage.UsageStatsManager;
42import android.appwidget.AppWidgetManager;
43import android.bluetooth.BluetoothManager;
Eugene Susla6ed45d82017-01-22 13:52:51 -080044import android.companion.CompanionDeviceManager;
45import android.companion.ICompanionDeviceManager;
Jeff Brown6e539312015-02-24 18:53:21 -080046import android.content.ClipboardManager;
Felipe Leme326f15a2019-02-19 09:42:24 -080047import android.content.ContentCaptureOptions;
Jeff Brown6e539312015-02-24 18:53:21 -080048import android.content.Context;
49import android.content.IRestrictionsManager;
50import android.content.RestrictionsManager;
Hyunyoung Song880a9512018-12-20 11:24:48 -080051import android.content.om.IOverlayManager;
52import android.content.om.OverlayManager;
Tony Makb0d22622018-01-18 12:49:49 +000053import android.content.pm.CrossProfileApps;
54import android.content.pm.ICrossProfileApps;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070055import android.content.pm.IPackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080056import android.content.pm.IShortcutService;
Jeff Brown6e539312015-02-24 18:53:21 -080057import android.content.pm.LauncherApps;
Svet Ganovec7daa52017-06-30 12:02:59 -070058import android.content.pm.PackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080059import android.content.pm.ShortcutManager;
Jeff Brown6e539312015-02-24 18:53:21 -080060import android.content.res.Resources;
Richard Uhlerb29f1452018-09-12 16:38:15 +010061import android.content.rollback.IRollbackManager;
62import android.content.rollback.RollbackManager;
Kenny Rootf74bfde2018-01-18 15:42:48 -080063import android.debug.AdbManager;
64import android.debug.IAdbManager;
Jeff Brown6e539312015-02-24 18:53:21 -080065import android.hardware.ConsumerIrManager;
66import android.hardware.ISerialManager;
67import android.hardware.SensorManager;
Chad Brubaker90f391f2018-10-19 10:26:19 -070068import android.hardware.SensorPrivacyManager;
Jeff Brown6e539312015-02-24 18:53:21 -080069import android.hardware.SerialManager;
70import android.hardware.SystemSensorManager;
Kevin Chyn05c21502018-09-18 13:07:19 -070071import android.hardware.biometrics.BiometricManager;
Ilya Matyukhine4675b32019-11-07 16:07:19 -080072import android.hardware.biometrics.IAuthService;
Jeff Brown6e539312015-02-24 18:53:21 -080073import android.hardware.camera2.CameraManager;
Christine Franks39b03112018-07-03 14:46:07 -070074import android.hardware.display.ColorDisplayManager;
Jeff Brown6e539312015-02-24 18:53:21 -080075import android.hardware.display.DisplayManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020076import android.hardware.face.FaceManager;
77import android.hardware.face.IFaceService;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060078import android.hardware.fingerprint.FingerprintManager;
79import android.hardware.fingerprint.IFingerprintService;
Jeff Brown6e539312015-02-24 18:53:21 -080080import android.hardware.hdmi.HdmiControlManager;
81import android.hardware.hdmi.IHdmiControlService;
82import android.hardware.input.InputManager;
Kevin Chyn51676d22018-11-05 18:00:43 -080083import android.hardware.iris.IIrisService;
Felipe Lemee348dc32018-11-05 12:35:29 -080084import android.hardware.iris.IrisManager;
Peng Xu9ff7d222016-02-11 13:02:05 -080085import android.hardware.location.ContextHubManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060086import android.hardware.radio.RadioManager;
Jeff Brown6e539312015-02-24 18:53:21 -080087import android.hardware.usb.IUsbManager;
88import android.hardware.usb.UsbManager;
89import android.location.CountryDetector;
90import android.location.ICountryDetector;
91import android.location.ILocationManager;
92import android.location.LocationManager;
93import android.media.AudioManager;
94import android.media.MediaRouter;
95import android.media.midi.IMidiManager;
96import android.media.midi.MidiManager;
97import android.media.projection.MediaProjectionManager;
98import android.media.session.MediaSessionManager;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080099import android.media.soundtrigger.SoundTriggerManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800100import android.media.tv.ITvInputManager;
101import android.media.tv.TvInputManager;
102import android.net.ConnectivityManager;
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700103import android.net.ConnectivityThread;
Jeff Brown6e539312015-02-24 18:53:21 -0800104import android.net.EthernetManager;
105import android.net.IConnectivityManager;
106import android.net.IEthernetManager;
Nathan Harold28084d82017-03-01 18:55:06 -0800107import android.net.IIpSecService;
Jeff Brown6e539312015-02-24 18:53:21 -0800108import android.net.INetworkPolicyManager;
Benedict Wong99a48412018-11-09 14:45:34 -0800109import android.net.ITestNetworkManager;
Nathan Harold28084d82017-03-01 18:55:06 -0800110import android.net.IpSecManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800111import android.net.NetworkPolicyManager;
112import android.net.NetworkScoreManager;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100113import android.net.NetworkWatchlistManager;
Benedict Wong99a48412018-11-09 14:45:34 -0800114import android.net.TestNetworkManager;
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700115import android.net.lowpan.ILowpanManager;
116import android.net.lowpan.LowpanManager;
Etan Cohen17ba4722017-08-21 10:52:17 -0700117import android.net.nsd.INsdManager;
118import android.net.nsd.NsdManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800119import android.net.wifi.IWifiScanner;
120import android.net.wifi.RttManager;
121import android.net.wifi.WifiManager;
122import android.net.wifi.WifiScanner;
Etan Cohen04133272016-10-26 11:22:06 -0700123import android.net.wifi.aware.IWifiAwareManager;
124import android.net.wifi.aware.WifiAwareManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800125import android.net.wifi.p2p.IWifiP2pManager;
126import android.net.wifi.p2p.WifiP2pManager;
Etan Cohen17ba4722017-08-21 10:52:17 -0700127import android.net.wifi.rtt.IWifiRttManager;
128import android.net.wifi.rtt.WifiRttManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800129import android.nfc.NfcManager;
130import android.os.BatteryManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600131import android.os.BatteryStats;
Roshan Pius848513e2019-10-11 13:44:00 -0700132import android.os.BatteryStatsManager;
Nandana Duttd11850c2018-12-12 17:26:57 +0000133import android.os.BugreportManager;
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600134import android.os.Build;
Jeff Brown6e539312015-02-24 18:53:21 -0800135import android.os.DropBoxManager;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100136import android.os.HardwarePropertiesManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600137import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6e539312015-02-24 18:53:21 -0800138import android.os.IBinder;
Nandana Duttd11850c2018-12-12 17:26:57 +0000139import android.os.IDumpstate;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100140import android.os.IHardwarePropertiesManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800141import android.os.IPowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800142import android.os.IRecoverySystem;
Tao Bao07342dc2017-01-24 15:08:21 -0800143import android.os.ISystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800144import android.os.IUserManager;
Joe Onorato1754d742016-11-21 17:51:35 -0800145import android.os.IncidentManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800146import android.os.PowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800147import android.os.RecoverySystem;
Benedict Wong99a48412018-11-09 14:45:34 -0800148import android.os.RemoteException;
Jeff Brown6e539312015-02-24 18:53:21 -0800149import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600150import android.os.ServiceManager.ServiceNotFoundException;
Tao Bao07342dc2017-01-24 15:08:21 -0800151import android.os.SystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800152import android.os.SystemVibrator;
153import android.os.UserHandle;
154import android.os.UserManager;
155import android.os.Vibrator;
Joe Onorato713fec82016-03-04 10:34:02 -0800156import android.os.health.SystemHealthManager;
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +0800157import android.os.image.DynamicSystemManager;
158import android.os.image.IDynamicSystemService;
Jeff Brown6e539312015-02-24 18:53:21 -0800159import android.os.storage.StorageManager;
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800160import android.permission.PermissionControllerManager;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700161import android.permission.PermissionManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800162import android.print.IPrintManager;
163import android.print.PrintManager;
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000164import android.service.oemlock.IOemLockService;
165import android.service.oemlock.OemLockManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800166import android.service.persistentdata.IPersistentDataBlockService;
167import android.service.persistentdata.PersistentDataBlockManager;
Zak Cohen56345f42017-01-26 13:54:28 -0800168import android.service.vr.IVrManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800169import android.telecom.TelecomManager;
Jayachandran C58fd3eb2019-11-20 19:01:56 -0800170import android.telephony.TelephonyFrameworkInitializer;
Makoto Onuki54c4e032019-10-30 12:05:20 -0700171import android.telephony.TelephonyRegistryManager;
Robert Benea1901a5b2018-05-15 16:36:07 -0700172import android.util.ArrayMap;
Jeff Brown6e539312015-02-24 18:53:21 -0800173import android.util.Log;
174import android.view.ContextThemeWrapper;
175import android.view.LayoutInflater;
Jeff Brown6e539312015-02-24 18:53:21 -0800176import android.view.WindowManager;
177import android.view.WindowManagerImpl;
178import android.view.accessibility.AccessibilityManager;
179import android.view.accessibility.CaptioningManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100180import android.view.autofill.AutofillManager;
181import android.view.autofill.IAutoFillManager;
Felipe Leme749b8892018-12-03 16:30:30 -0800182import android.view.contentcapture.ContentCaptureManager;
183import android.view.contentcapture.IContentCaptureManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800184import android.view.inputmethod.InputMethodManager;
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000185import android.view.textclassifier.TextClassificationManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800186import android.view.textservice.TextServicesManager;
187
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600188import com.android.internal.app.IAppOpsService;
189import com.android.internal.app.IBatteryStats;
190import com.android.internal.app.ISoundTriggerService;
191import com.android.internal.appwidget.IAppWidgetService;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100192import com.android.internal.net.INetworkWatchlistManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600193import com.android.internal.os.IDropBoxManagerService;
194import com.android.internal.policy.PhoneLayoutInflater;
Makoto Onuki05b14f52019-11-05 12:36:07 -0800195import com.android.internal.util.Preconditions;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600196
Robert Benea1901a5b2018-05-15 16:36:07 -0700197import java.util.Map;
Jeff Brown6e539312015-02-24 18:53:21 -0800198
199/**
200 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
201 * Used by {@link ContextImpl}.
Makoto Onuki792de8d2019-07-16 16:19:01 -0700202 *
203 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -0800204 */
Makoto Onuki792de8d2019-07-16 16:19:01 -0700205public final class SystemServiceRegistry {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600206 private static final String TAG = "SystemServiceRegistry";
207
Jeff Brown6e539312015-02-24 18:53:21 -0800208 // Service registry information.
209 // This information is never changed once static initialization has completed.
Robert Benea1901a5b2018-05-15 16:36:07 -0700210 private static final Map<Class<?>, String> SYSTEM_SERVICE_NAMES =
211 new ArrayMap<Class<?>, String>();
212 private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
213 new ArrayMap<String, ServiceFetcher<?>>();
Jeff Brown6e539312015-02-24 18:53:21 -0800214 private static int sServiceCacheSize;
215
Makoto Onuki05b14f52019-11-05 12:36:07 -0800216 private static volatile boolean sInitializing;
217
Jeff Brown6e539312015-02-24 18:53:21 -0800218 // Not instantiable.
219 private SystemServiceRegistry() { }
220
221 static {
Felipe Lemeecb08be2018-11-27 15:48:47 -0800222 //CHECKSTYLE:OFF IndentationCheck
Jeff Brown6e539312015-02-24 18:53:21 -0800223 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
224 new CachedServiceFetcher<AccessibilityManager>() {
225 @Override
226 public AccessibilityManager createService(ContextImpl ctx) {
227 return AccessibilityManager.getInstance(ctx);
228 }});
229
230 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
231 new CachedServiceFetcher<CaptioningManager>() {
232 @Override
233 public CaptioningManager createService(ContextImpl ctx) {
234 return new CaptioningManager(ctx);
235 }});
236
237 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
238 new CachedServiceFetcher<AccountManager>() {
239 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600240 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
241 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800242 IAccountManager service = IAccountManager.Stub.asInterface(b);
243 return new AccountManager(ctx, service);
244 }});
245
246 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
247 new CachedServiceFetcher<ActivityManager>() {
248 @Override
249 public ActivityManager createService(ContextImpl ctx) {
250 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
251 }});
252
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700253 registerService(Context.ACTIVITY_TASK_SERVICE, ActivityTaskManager.class,
254 new CachedServiceFetcher<ActivityTaskManager>() {
255 @Override
256 public ActivityTaskManager createService(ContextImpl ctx) {
257 return new ActivityTaskManager(
258 ctx.getOuterContext(), ctx.mMainThread.getHandler());
259 }});
260
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700261 registerService(Context.URI_GRANTS_SERVICE, UriGrantsManager.class,
262 new CachedServiceFetcher<UriGrantsManager>() {
263 @Override
264 public UriGrantsManager createService(ContextImpl ctx) {
265 return new UriGrantsManager(
266 ctx.getOuterContext(), ctx.mMainThread.getHandler());
267 }});
268
Jeff Brown6e539312015-02-24 18:53:21 -0800269 registerService(Context.ALARM_SERVICE, AlarmManager.class,
270 new CachedServiceFetcher<AlarmManager>() {
271 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600272 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
273 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800274 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
275 return new AlarmManager(service, ctx);
276 }});
277
278 registerService(Context.AUDIO_SERVICE, AudioManager.class,
279 new CachedServiceFetcher<AudioManager>() {
280 @Override
281 public AudioManager createService(ContextImpl ctx) {
282 return new AudioManager(ctx);
283 }});
284
285 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
286 new CachedServiceFetcher<MediaRouter>() {
287 @Override
288 public MediaRouter createService(ContextImpl ctx) {
289 return new MediaRouter(ctx);
290 }});
291
292 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
293 new CachedServiceFetcher<BluetoothManager>() {
294 @Override
295 public BluetoothManager createService(ContextImpl ctx) {
296 return new BluetoothManager(ctx);
297 }});
298
299 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
300 new StaticServiceFetcher<HdmiControlManager>() {
301 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600302 public HdmiControlManager createService() throws ServiceNotFoundException {
303 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800304 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
305 }});
306
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000307 registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000308 new CachedServiceFetcher<TextClassificationManager>() {
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000309 @Override
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000310 public TextClassificationManager createService(ContextImpl ctx) {
311 return new TextClassificationManager(ctx);
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000312 }});
313
Jeff Brown6e539312015-02-24 18:53:21 -0800314 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
315 new CachedServiceFetcher<ClipboardManager>() {
316 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600317 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800318 return new ClipboardManager(ctx.getOuterContext(),
319 ctx.mMainThread.getHandler());
320 }});
321
322 // The clipboard service moved to a new package. If someone asks for the old
323 // interface by class then we want to redirect over to the new interface instead
324 // (which extends it).
325 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
326
327 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
Lorenzo Colitticf959772016-02-29 16:07:37 +0900328 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800329 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600330 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
331 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900332 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
333 return new ConnectivityManager(context, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800334 }});
335
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +0900336 registerService(Context.NETD_SERVICE, IBinder.class, new StaticServiceFetcher<IBinder>() {
Remi NGUYEN VAN31935982019-01-28 11:40:08 +0900337 @Override
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +0900338 public IBinder createService() throws ServiceNotFoundException {
339 return ServiceManager.getServiceOrThrow(Context.NETD_SERVICE);
Remi NGUYEN VAN31935982019-01-28 11:40:08 +0900340 }
341 });
342
Nathan Harold28084d82017-03-01 18:55:06 -0800343 registerService(Context.IPSEC_SERVICE, IpSecManager.class,
Nathan Harold592dadb2018-03-15 18:06:06 -0700344 new CachedServiceFetcher<IpSecManager>() {
Nathan Harold28084d82017-03-01 18:55:06 -0800345 @Override
Nathan Harold592dadb2018-03-15 18:06:06 -0700346 public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nathan Harold28084d82017-03-01 18:55:06 -0800347 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
348 IIpSecService service = IIpSecService.Stub.asInterface(b);
Nathan Harold592dadb2018-03-15 18:06:06 -0700349 return new IpSecManager(ctx, service);
Nathan Harold28084d82017-03-01 18:55:06 -0800350 }});
351
Benedict Wong99a48412018-11-09 14:45:34 -0800352 registerService(
353 Context.TEST_NETWORK_SERVICE,
354 TestNetworkManager.class,
355 new StaticApplicationContextServiceFetcher<TestNetworkManager>() {
356 @Override
357 public TestNetworkManager createService(Context context)
358 throws ServiceNotFoundException {
359 IBinder csBinder =
360 ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
361 IConnectivityManager csMgr =
362 IConnectivityManager.Stub.asInterface(csBinder);
363
364 final IBinder tnBinder;
365 try {
366 tnBinder = csMgr.startOrGetTestNetworkService();
367 } catch (RemoteException e) {
368 throw new ServiceNotFoundException(Context.TEST_NETWORK_SERVICE);
369 }
370 ITestNetworkManager tnMgr = ITestNetworkManager.Stub.asInterface(tnBinder);
Benedict Wongb4e98f32019-03-12 21:54:16 -0700371 return new TestNetworkManager(tnMgr);
Benedict Wong99a48412018-11-09 14:45:34 -0800372 }
373 });
374
Jeff Brown6e539312015-02-24 18:53:21 -0800375 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
376 new StaticServiceFetcher<CountryDetector>() {
377 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600378 public CountryDetector createService() throws ServiceNotFoundException {
379 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
Jeff Brown6e539312015-02-24 18:53:21 -0800380 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
381 }});
382
383 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
384 new CachedServiceFetcher<DevicePolicyManager>() {
385 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600386 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
387 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
388 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800389 }});
390
391 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
392 new CachedServiceFetcher<DownloadManager>() {
393 @Override
394 public DownloadManager createService(ContextImpl ctx) {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700395 return new DownloadManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800396 }});
397
398 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
Yifan Hongb0f13522017-10-31 15:48:50 -0700399 new CachedServiceFetcher<BatteryManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800400 @Override
Yifan Hongb0f13522017-10-31 15:48:50 -0700401 public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600402 IBatteryStats stats = IBatteryStats.Stub.asInterface(
403 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
404 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
405 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
Yifan Hongb0f13522017-10-31 15:48:50 -0700406 return new BatteryManager(ctx, stats, registrar);
Jeff Brown6e539312015-02-24 18:53:21 -0800407 }});
408
409 registerService(Context.NFC_SERVICE, NfcManager.class,
410 new CachedServiceFetcher<NfcManager>() {
411 @Override
412 public NfcManager createService(ContextImpl ctx) {
413 return new NfcManager(ctx);
414 }});
415
416 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700417 new CachedServiceFetcher<DropBoxManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800418 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600419 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
420 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800421 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700422 return new DropBoxManager(ctx, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800423 }});
424
425 registerService(Context.INPUT_SERVICE, InputManager.class,
426 new StaticServiceFetcher<InputManager>() {
427 @Override
428 public InputManager createService() {
429 return InputManager.getInstance();
430 }});
431
432 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
433 new CachedServiceFetcher<DisplayManager>() {
434 @Override
435 public DisplayManager createService(ContextImpl ctx) {
436 return new DisplayManager(ctx.getOuterContext());
437 }});
438
Christine Franks39b03112018-07-03 14:46:07 -0700439 registerService(Context.COLOR_DISPLAY_SERVICE, ColorDisplayManager.class,
440 new CachedServiceFetcher<ColorDisplayManager>() {
441 @Override
442 public ColorDisplayManager createService(ContextImpl ctx) {
443 return new ColorDisplayManager();
444 }
445 });
446
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800447 // InputMethodManager has its own cache strategy based on display id to support apps that
448 // still assume InputMethodManager is a per-process singleton and it's safe to directly
449 // access internal fields via reflection. Hence directly use ServiceFetcher instead of
450 // StaticServiceFetcher/CachedServiceFetcher.
Jeff Brown6e539312015-02-24 18:53:21 -0800451 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800452 new ServiceFetcher<InputMethodManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800453 @Override
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800454 public InputMethodManager getService(ContextImpl ctx) {
455 return InputMethodManager.forContext(ctx.getOuterContext());
Jeff Brown6e539312015-02-24 18:53:21 -0800456 }});
457
458 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
Yohei Yukawa9f141ee2019-01-18 09:17:16 -0800459 new CachedServiceFetcher<TextServicesManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800460 @Override
Yohei Yukawa9f141ee2019-01-18 09:17:16 -0800461 public TextServicesManager createService(ContextImpl ctx)
462 throws ServiceNotFoundException {
463 return TextServicesManager.createInstance(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800464 }});
465
466 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
Nancy Zheng43f166d2016-11-18 18:15:09 -0800467 new CachedServiceFetcher<KeyguardManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800468 @Override
Nancy Zheng40cd8e42016-11-21 12:58:16 -0800469 public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nancy Zheng43f166d2016-11-18 18:15:09 -0800470 return new KeyguardManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800471 }});
472
473 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
474 new CachedServiceFetcher<LayoutInflater>() {
475 @Override
476 public LayoutInflater createService(ContextImpl ctx) {
477 return new PhoneLayoutInflater(ctx.getOuterContext());
478 }});
479
480 registerService(Context.LOCATION_SERVICE, LocationManager.class,
481 new CachedServiceFetcher<LocationManager>() {
482 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600483 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
484 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800485 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
486 }});
487
488 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
Svet Ganov16a16892015-04-16 10:32:04 -0700489 new CachedServiceFetcher<NetworkPolicyManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800490 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600491 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov16a16892015-04-16 10:32:04 -0700492 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600493 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
Jeff Brown6e539312015-02-24 18:53:21 -0800494 }});
495
496 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
497 new CachedServiceFetcher<NotificationManager>() {
498 @Override
499 public NotificationManager createService(ContextImpl ctx) {
500 final Context outerContext = ctx.getOuterContext();
501 return new NotificationManager(
502 new ContextThemeWrapper(outerContext,
503 Resources.selectSystemTheme(0,
504 outerContext.getApplicationInfo().targetSdkVersion,
505 com.android.internal.R.style.Theme_Dialog,
506 com.android.internal.R.style.Theme_Holo_Dialog,
507 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
508 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
509 ctx.mMainThread.getHandler());
510 }});
511
512 registerService(Context.NSD_SERVICE, NsdManager.class,
513 new CachedServiceFetcher<NsdManager>() {
514 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600515 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
516 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800517 INsdManager service = INsdManager.Stub.asInterface(b);
518 return new NsdManager(ctx.getOuterContext(), service);
519 }});
520
521 registerService(Context.POWER_SERVICE, PowerManager.class,
522 new CachedServiceFetcher<PowerManager>() {
523 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600524 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
525 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800526 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown6e539312015-02-24 18:53:21 -0800527 return new PowerManager(ctx.getOuterContext(),
528 service, ctx.mMainThread.getHandler());
529 }});
530
Tao Baoe8a403d2015-12-31 07:44:55 -0800531 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
532 new CachedServiceFetcher<RecoverySystem>() {
533 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600534 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
535 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
Tao Baoe8a403d2015-12-31 07:44:55 -0800536 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
Tao Baoe8a403d2015-12-31 07:44:55 -0800537 return new RecoverySystem(service);
538 }});
539
Jeff Brown6e539312015-02-24 18:53:21 -0800540 registerService(Context.SEARCH_SERVICE, SearchManager.class,
541 new CachedServiceFetcher<SearchManager>() {
542 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600543 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800544 return new SearchManager(ctx.getOuterContext(),
545 ctx.mMainThread.getHandler());
546 }});
547
548 registerService(Context.SENSOR_SERVICE, SensorManager.class,
549 new CachedServiceFetcher<SensorManager>() {
550 @Override
551 public SensorManager createService(ContextImpl ctx) {
552 return new SystemSensorManager(ctx.getOuterContext(),
553 ctx.mMainThread.getHandler().getLooper());
554 }});
555
Chad Brubaker90f391f2018-10-19 10:26:19 -0700556 registerService(Context.SENSOR_PRIVACY_SERVICE, SensorPrivacyManager.class,
557 new CachedServiceFetcher<SensorPrivacyManager>() {
558 @Override
559 public SensorPrivacyManager createService(ContextImpl ctx) {
560 return SensorPrivacyManager.getInstance(ctx);
561 }});
562
David Chenadaf8b32017-11-03 15:42:08 -0700563 registerService(Context.STATS_MANAGER, StatsManager.class,
Jeff Sharkey6b649252018-04-16 09:50:22 -0600564 new CachedServiceFetcher<StatsManager>() {
565 @Override
566 public StatsManager createService(ContextImpl ctx) {
567 return new StatsManager(ctx.getOuterContext());
568 }});
David Chenadaf8b32017-11-03 15:42:08 -0700569
Jeff Brown6e539312015-02-24 18:53:21 -0800570 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
571 new CachedServiceFetcher<StatusBarManager>() {
572 @Override
573 public StatusBarManager createService(ContextImpl ctx) {
574 return new StatusBarManager(ctx.getOuterContext());
575 }});
576
577 registerService(Context.STORAGE_SERVICE, StorageManager.class,
578 new CachedServiceFetcher<StorageManager>() {
579 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600580 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey48877892015-03-18 11:27:19 -0700581 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800582 }});
583
Jeff Sharkeye8cece92017-01-04 11:33:33 -0700584 registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
585 new CachedServiceFetcher<StorageStatsManager>() {
586 @Override
587 public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
588 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
589 ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
590 return new StorageStatsManager(ctx, service);
591 }});
592
Tao Bao07342dc2017-01-24 15:08:21 -0800593 registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class,
594 new CachedServiceFetcher<SystemUpdateManager>() {
595 @Override
596 public SystemUpdateManager createService(ContextImpl ctx)
597 throws ServiceNotFoundException {
598 IBinder b = ServiceManager.getServiceOrThrow(
599 Context.SYSTEM_UPDATE_SERVICE);
600 ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b);
601 return new SystemUpdateManager(service);
602 }});
603
Chen Xu288b71c2019-09-15 18:28:21 -0700604 registerService(Context.TELEPHONY_REGISTRY_SERVICE, TelephonyRegistryManager.class,
605 new CachedServiceFetcher<TelephonyRegistryManager>() {
606 @Override
607 public TelephonyRegistryManager createService(ContextImpl ctx) {
Chen Xu1f6cfa52019-10-13 17:30:32 -0700608 return new TelephonyRegistryManager(ctx);
Chen Xu288b71c2019-09-15 18:28:21 -0700609 }});
610
Jeff Brown6e539312015-02-24 18:53:21 -0800611 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
612 new CachedServiceFetcher<TelecomManager>() {
613 @Override
614 public TelecomManager createService(ContextImpl ctx) {
615 return new TelecomManager(ctx.getOuterContext());
616 }});
617
618 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
619 new CachedServiceFetcher<UiModeManager>() {
620 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600621 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Tyler Gunndaef6682019-09-30 15:12:24 -0700622 return new UiModeManager(ctx.getOuterContext());
Jeff Brown6e539312015-02-24 18:53:21 -0800623 }});
624
625 registerService(Context.USB_SERVICE, UsbManager.class,
626 new CachedServiceFetcher<UsbManager>() {
627 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600628 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
629 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800630 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
631 }});
632
Kenny Rootf74bfde2018-01-18 15:42:48 -0800633 registerService(Context.ADB_SERVICE, AdbManager.class,
634 new CachedServiceFetcher<AdbManager>() {
635 @Override
636 public AdbManager createService(ContextImpl ctx)
637 throws ServiceNotFoundException {
638 IBinder b = ServiceManager.getServiceOrThrow(Context.ADB_SERVICE);
639 return new AdbManager(ctx, IAdbManager.Stub.asInterface(b));
640 }});
641
Jeff Brown6e539312015-02-24 18:53:21 -0800642 registerService(Context.SERIAL_SERVICE, SerialManager.class,
643 new CachedServiceFetcher<SerialManager>() {
644 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600645 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
646 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800647 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
648 }});
649
650 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
651 new CachedServiceFetcher<Vibrator>() {
652 @Override
653 public Vibrator createService(ContextImpl ctx) {
654 return new SystemVibrator(ctx);
655 }});
656
657 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
658 new CachedServiceFetcher<WallpaperManager>() {
659 @Override
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800660 public WallpaperManager createService(ContextImpl ctx)
661 throws ServiceNotFoundException {
Felipe Leme34a861a2019-08-05 16:00:12 -0700662 final IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
663 if (b == null) {
664 // There are 2 reason service can be null:
665 // 1.Device doesn't support it - that's fine
666 // 2.App is running on instant mode - should fail
667 final boolean enabled = Resources.getSystem()
668 .getBoolean(com.android.internal.R.bool.config_enableWallpaperService);
669 if (!enabled) {
670 // Life moves on...
671 return DisabledWallpaperManager.getInstance();
672 }
673 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
674 // Instant app
675 throw new ServiceNotFoundException(Context.WALLPAPER_SERVICE);
676 }
677 // Bad state - WallpaperManager methods will throw exception
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800678 }
679 IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
680 return new WallpaperManager(service, ctx.getOuterContext(),
Jeff Brown6e539312015-02-24 18:53:21 -0800681 ctx.mMainThread.getHandler());
682 }});
683
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700684 registerService(Context.LOWPAN_SERVICE, LowpanManager.class,
685 new CachedServiceFetcher<LowpanManager>() {
686 @Override
687 public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
688 IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE);
689 ILowpanManager service = ILowpanManager.Stub.asInterface(b);
690 return new LowpanManager(ctx.getOuterContext(), service,
691 ConnectivityThread.getInstanceLooper());
692 }});
693
Jeff Brown6e539312015-02-24 18:53:21 -0800694 registerService(Context.WIFI_SERVICE, WifiManager.class,
695 new CachedServiceFetcher<WifiManager>() {
696 @Override
Roshan Pius2161b392019-07-12 11:06:16 -0700697 public WifiManager createService(ContextImpl ctx) {
698 return new WifiManager(ctx.getOuterContext(),
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700699 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800700 }});
701
Jeff Brown6e539312015-02-24 18:53:21 -0800702 registerService(Context.WIFI_P2P_SERVICE, WifiP2pManager.class,
703 new StaticServiceFetcher<WifiP2pManager>() {
704 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600705 public WifiP2pManager createService() throws ServiceNotFoundException {
706 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_P2P_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800707 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
708 return new WifiP2pManager(service);
709 }});
710
Etan Cohen04133272016-10-26 11:22:06 -0700711 registerService(Context.WIFI_AWARE_SERVICE, WifiAwareManager.class,
712 new CachedServiceFetcher<WifiAwareManager>() {
Etan Cohen20d329b2015-09-29 13:49:02 -0700713 @Override
Etan Cohen04133272016-10-26 11:22:06 -0700714 public WifiAwareManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey589f3092016-11-29 12:54:39 -0700715 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_AWARE_SERVICE);
Etan Cohen04133272016-10-26 11:22:06 -0700716 IWifiAwareManager service = IWifiAwareManager.Stub.asInterface(b);
Etan Cohen20d329b2015-09-29 13:49:02 -0700717 if (service == null) {
718 return null;
719 }
Etan Cohen04133272016-10-26 11:22:06 -0700720 return new WifiAwareManager(ctx.getOuterContext(), service);
Etan Cohen20d329b2015-09-29 13:49:02 -0700721 }});
722
Jeff Brown6e539312015-02-24 18:53:21 -0800723 registerService(Context.WIFI_SCANNING_SERVICE, WifiScanner.class,
724 new CachedServiceFetcher<WifiScanner>() {
725 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600726 public WifiScanner createService(ContextImpl ctx) throws ServiceNotFoundException {
727 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_SCANNING_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800728 IWifiScanner service = IWifiScanner.Stub.asInterface(b);
Mitchell Wills91312c72016-04-06 17:03:35 -0700729 return new WifiScanner(ctx.getOuterContext(), service,
730 ConnectivityThread.getInstanceLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800731 }});
732
733 registerService(Context.WIFI_RTT_SERVICE, RttManager.class,
734 new CachedServiceFetcher<RttManager>() {
Etan Cohen9accbba2018-01-30 16:41:00 -0800735 @Override
736 public RttManager createService(ContextImpl ctx) throws ServiceNotFoundException {
737 IBinder b = ServiceManager.getServiceOrThrow(Context.WIFI_RTT_RANGING_SERVICE);
738 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
739 return new RttManager(ctx.getOuterContext(),
740 new WifiRttManager(ctx.getOuterContext(), service));
741 }});
Jeff Brown6e539312015-02-24 18:53:21 -0800742
Etan Cohen46efb482017-12-07 13:50:57 -0800743 registerService(Context.WIFI_RTT_RANGING_SERVICE, WifiRttManager.class,
Etan Cohen17ba4722017-08-21 10:52:17 -0700744 new CachedServiceFetcher<WifiRttManager>() {
745 @Override
746 public WifiRttManager createService(ContextImpl ctx)
747 throws ServiceNotFoundException {
Etan Cohen46efb482017-12-07 13:50:57 -0800748 IBinder b = ServiceManager.getServiceOrThrow(
749 Context.WIFI_RTT_RANGING_SERVICE);
Etan Cohen17ba4722017-08-21 10:52:17 -0700750 IWifiRttManager service = IWifiRttManager.Stub.asInterface(b);
751 return new WifiRttManager(ctx.getOuterContext(), service);
752 }});
753
Jeff Brown6e539312015-02-24 18:53:21 -0800754 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
755 new CachedServiceFetcher<EthernetManager>() {
756 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600757 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
758 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800759 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
760 return new EthernetManager(ctx.getOuterContext(), service);
761 }});
762
763 registerService(Context.WINDOW_SERVICE, WindowManager.class,
764 new CachedServiceFetcher<WindowManager>() {
765 @Override
766 public WindowManager createService(ContextImpl ctx) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700767 return new WindowManagerImpl(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800768 }});
769
770 registerService(Context.USER_SERVICE, UserManager.class,
771 new CachedServiceFetcher<UserManager>() {
772 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600773 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
774 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800775 IUserManager service = IUserManager.Stub.asInterface(b);
776 return new UserManager(ctx, service);
777 }});
778
779 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
780 new CachedServiceFetcher<AppOpsManager>() {
781 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600782 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
783 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800784 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
785 return new AppOpsManager(ctx, service);
786 }});
787
788 registerService(Context.CAMERA_SERVICE, CameraManager.class,
789 new CachedServiceFetcher<CameraManager>() {
790 @Override
791 public CameraManager createService(ContextImpl ctx) {
792 return new CameraManager(ctx);
793 }});
794
795 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
796 new CachedServiceFetcher<LauncherApps>() {
797 @Override
798 public LauncherApps createService(ContextImpl ctx) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700799 return new LauncherApps(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800800 }});
801
802 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
803 new CachedServiceFetcher<RestrictionsManager>() {
804 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600805 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
806 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800807 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
808 return new RestrictionsManager(ctx, service);
809 }});
810
811 registerService(Context.PRINT_SERVICE, PrintManager.class,
812 new CachedServiceFetcher<PrintManager>() {
813 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600814 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganovec7daa52017-06-30 12:02:59 -0700815 IPrintManager service = null;
816 // If the feature not present, don't try to look up every time
817 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
818 service = IPrintManager.Stub.asInterface(ServiceManager
819 .getServiceOrThrow(Context.PRINT_SERVICE));
820 }
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700821 final int userId = ctx.getUserId();
822 final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid);
823 return new PrintManager(ctx.getOuterContext(), service, userId, appId);
Jeff Brown6e539312015-02-24 18:53:21 -0800824 }});
825
Eugene Susla6ed45d82017-01-22 13:52:51 -0800826 registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
827 new CachedServiceFetcher<CompanionDeviceManager>() {
Svet Ganovec7daa52017-06-30 12:02:59 -0700828 @Override
829 public CompanionDeviceManager createService(ContextImpl ctx)
830 throws ServiceNotFoundException {
831 ICompanionDeviceManager service = null;
832 // If the feature not present, don't try to look up every time
833 if (ctx.getPackageManager().hasSystemFeature(
834 PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) {
835 service = ICompanionDeviceManager.Stub.asInterface(
836 ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE));
837 }
838 return new CompanionDeviceManager(service, ctx.getOuterContext());
839 }});
Eugene Susla6ed45d82017-01-22 13:52:51 -0800840
Jeff Brown6e539312015-02-24 18:53:21 -0800841 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
842 new CachedServiceFetcher<ConsumerIrManager>() {
843 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600844 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800845 return new ConsumerIrManager(ctx);
846 }});
847
848 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
849 new CachedServiceFetcher<MediaSessionManager>() {
850 @Override
851 public MediaSessionManager createService(ContextImpl ctx) {
852 return new MediaSessionManager(ctx);
853 }});
854
855 registerService(Context.TRUST_SERVICE, TrustManager.class,
856 new StaticServiceFetcher<TrustManager>() {
857 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600858 public TrustManager createService() throws ServiceNotFoundException {
859 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800860 return new TrustManager(b);
861 }});
862
863 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
864 new CachedServiceFetcher<FingerprintManager>() {
865 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600866 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600867 final IBinder binder;
868 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
869 binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
870 } else {
871 binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
872 }
Jeff Brown6e539312015-02-24 18:53:21 -0800873 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
874 return new FingerprintManager(ctx.getOuterContext(), service);
875 }});
876
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200877 registerService(Context.FACE_SERVICE, FaceManager.class,
878 new CachedServiceFetcher<FaceManager>() {
879 @Override
880 public FaceManager createService(ContextImpl ctx)
881 throws ServiceNotFoundException {
882 final IBinder binder;
883 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
884 binder = ServiceManager.getServiceOrThrow(Context.FACE_SERVICE);
885 } else {
886 binder = ServiceManager.getService(Context.FACE_SERVICE);
887 }
888 IFaceService service = IFaceService.Stub.asInterface(binder);
889 return new FaceManager(ctx.getOuterContext(), service);
890 }
891 });
892
Kevin Chyn51676d22018-11-05 18:00:43 -0800893 registerService(Context.IRIS_SERVICE, IrisManager.class,
894 new CachedServiceFetcher<IrisManager>() {
895 @Override
896 public IrisManager createService(ContextImpl ctx)
897 throws ServiceNotFoundException {
898 final IBinder binder =
899 ServiceManager.getServiceOrThrow(Context.IRIS_SERVICE);
900 IIrisService service = IIrisService.Stub.asInterface(binder);
901 return new IrisManager(ctx.getOuterContext(), service);
902 }
903 });
904
Kevin Chyn05c21502018-09-18 13:07:19 -0700905 registerService(Context.BIOMETRIC_SERVICE, BiometricManager.class,
906 new CachedServiceFetcher<BiometricManager>() {
907 @Override
908 public BiometricManager createService(ContextImpl ctx)
909 throws ServiceNotFoundException {
Kevin Chyne739daf2018-11-06 17:11:06 -0800910 if (BiometricManager.hasBiometrics(ctx)) {
911 final IBinder binder =
Ilya Matyukhine4675b32019-11-07 16:07:19 -0800912 ServiceManager.getServiceOrThrow(Context.AUTH_SERVICE);
913 final IAuthService service =
914 IAuthService.Stub.asInterface(binder);
Kevin Chyne739daf2018-11-06 17:11:06 -0800915 return new BiometricManager(ctx.getOuterContext(), service);
916 } else {
917 // Allow access to the manager when service is null. This saves memory
918 // on devices without biometric hardware.
919 return new BiometricManager(ctx.getOuterContext(), null);
920 }
Kevin Chyn05c21502018-09-18 13:07:19 -0700921 }
922 });
923
Jeff Brown6e539312015-02-24 18:53:21 -0800924 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700925 new CachedServiceFetcher<TvInputManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800926 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700927 public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600928 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800929 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700930 return new TvInputManager(service, ctx.getUserId());
Jeff Brown6e539312015-02-24 18:53:21 -0800931 }});
932
933 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
934 new CachedServiceFetcher<NetworkScoreManager>() {
935 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600936 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800937 return new NetworkScoreManager(ctx);
938 }});
939
940 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
941 new CachedServiceFetcher<UsageStatsManager>() {
942 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600943 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
944 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800945 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
946 return new UsageStatsManager(ctx.getOuterContext(), service);
947 }});
948
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000949 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
950 new CachedServiceFetcher<NetworkStatsManager>() {
951 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600952 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000953 return new NetworkStatsManager(ctx.getOuterContext());
954 }});
955
Jeff Brown6e539312015-02-24 18:53:21 -0800956 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
957 new StaticServiceFetcher<PersistentDataBlockManager>() {
958 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600959 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
960 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800961 IPersistentDataBlockService persistentDataBlockService =
962 IPersistentDataBlockService.Stub.asInterface(b);
963 if (persistentDataBlockService != null) {
964 return new PersistentDataBlockManager(persistentDataBlockService);
965 } else {
966 // not supported
967 return null;
968 }
969 }});
970
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000971 registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
972 new StaticServiceFetcher<OemLockManager>() {
973 @Override
974 public OemLockManager createService() throws ServiceNotFoundException {
975 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
976 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
977 if (oemLockService != null) {
978 return new OemLockManager(oemLockService);
979 } else {
980 // not supported
981 return null;
982 }
983 }});
984
Jeff Brown6e539312015-02-24 18:53:21 -0800985 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
986 new CachedServiceFetcher<MediaProjectionManager>() {
987 @Override
988 public MediaProjectionManager createService(ContextImpl ctx) {
989 return new MediaProjectionManager(ctx);
990 }});
991
992 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
993 new CachedServiceFetcher<AppWidgetManager>() {
994 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600995 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
996 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800997 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
998 }});
999
1000 registerService(Context.MIDI_SERVICE, MidiManager.class,
1001 new CachedServiceFetcher<MidiManager>() {
1002 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001003 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1004 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
Mike Lockwoode0a6ca62015-06-04 13:43:56 -07001005 return new MidiManager(IMidiManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -08001006 }});
Eric Laurent2035ac82015-03-05 15:18:44 -08001007
1008 registerService(Context.RADIO_SERVICE, RadioManager.class,
1009 new CachedServiceFetcher<RadioManager>() {
1010 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -07001011 public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Eric Laurent2035ac82015-03-05 15:18:44 -08001012 return new RadioManager(ctx);
1013 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001014
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001015 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
1016 new CachedServiceFetcher<HardwarePropertiesManager>() {
1017 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001018 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1019 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
1020 IHardwarePropertiesManager service =
1021 IHardwarePropertiesManager.Stub.asInterface(b);
1022 return new HardwarePropertiesManager(ctx, service);
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001023 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001024
1025 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
1026 new CachedServiceFetcher<SoundTriggerManager>() {
1027 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001028 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1029 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001030 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
1031 }});
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001032
1033 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
1034 new CachedServiceFetcher<ShortcutManager>() {
Makoto Onuki55046222016-03-08 10:49:47 -08001035 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001036 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1037 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
1038 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
Makoto Onuki55046222016-03-08 10:49:47 -08001039 }});
Joe Onorato713fec82016-03-04 10:34:02 -08001040
Hyunyoung Song880a9512018-12-20 11:24:48 -08001041 registerService(Context.OVERLAY_SERVICE, OverlayManager.class,
1042 new CachedServiceFetcher<OverlayManager>() {
1043 @Override
1044 public OverlayManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1045 IBinder b = ServiceManager.getServiceOrThrow(Context.OVERLAY_SERVICE);
1046 return new OverlayManager(ctx, IOverlayManager.Stub.asInterface(b));
1047 }});
1048
Ricky Wai1a6e6672017-10-27 14:46:01 +01001049 registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class,
1050 new CachedServiceFetcher<NetworkWatchlistManager>() {
1051 @Override
1052 public NetworkWatchlistManager createService(ContextImpl ctx)
1053 throws ServiceNotFoundException {
1054 IBinder b =
1055 ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE);
1056 return new NetworkWatchlistManager(ctx,
1057 INetworkWatchlistManager.Stub.asInterface(b));
1058 }});
1059
Joe Onorato713fec82016-03-04 10:34:02 -08001060 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
1061 new CachedServiceFetcher<SystemHealthManager>() {
1062 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001063 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1064 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
1065 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
Joe Onorato713fec82016-03-04 10:34:02 -08001066 }});
Peng Xu9ff7d222016-02-11 13:02:05 -08001067
1068 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
1069 new CachedServiceFetcher<ContextHubManager>() {
1070 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001071 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Peng Xu9ff7d222016-02-11 13:02:05 -08001072 return new ContextHubManager(ctx.getOuterContext(),
1073 ctx.mMainThread.getHandler().getLooper());
1074 }});
Joe Onorato1754d742016-11-21 17:51:35 -08001075
1076 registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
1077 new CachedServiceFetcher<IncidentManager>() {
1078 @Override
1079 public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1080 return new IncidentManager(ctx);
1081 }});
Clara Bayarri04d72ab2017-01-10 09:31:51 -08001082
Nandana Duttd11850c2018-12-12 17:26:57 +00001083 registerService(Context.BUGREPORT_SERVICE, BugreportManager.class,
1084 new CachedServiceFetcher<BugreportManager>() {
1085 @Override
1086 public BugreportManager createService(ContextImpl ctx)
1087 throws ServiceNotFoundException {
1088 IBinder b = ServiceManager.getServiceOrThrow(Context.BUGREPORT_SERVICE);
1089 return new BugreportManager(ctx.getOuterContext(),
1090 IDumpstate.Stub.asInterface(b));
1091 }});
1092
Felipe Leme640f30a2017-03-06 15:44:06 -08001093 registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
1094 new CachedServiceFetcher<AutofillManager>() {
Felipe Leme3461d3c2017-01-19 08:54:55 -08001095 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -08001096 public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov43574b02017-04-12 09:25:20 -07001097 // Get the services without throwing as this is an optional feature
1098 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
Svet Ganov782043c2017-02-11 00:52:02 +00001099 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
Felipe Leme640f30a2017-03-06 15:44:06 -08001100 return new AutofillManager(ctx.getOuterContext(), service);
Felipe Leme3461d3c2017-01-19 08:54:55 -08001101 }});
Zak Cohen56345f42017-01-26 13:54:28 -08001102
Felipe Lemeecb08be2018-11-27 15:48:47 -08001103 registerService(Context.CONTENT_CAPTURE_MANAGER_SERVICE, ContentCaptureManager.class,
1104 new CachedServiceFetcher<ContentCaptureManager>() {
Felipe Lemee348dc32018-11-05 12:35:29 -08001105 @Override
Felipe Lemeecb08be2018-11-27 15:48:47 -08001106 public ContentCaptureManager createService(ContextImpl ctx)
Felipe Lemee348dc32018-11-05 12:35:29 -08001107 throws ServiceNotFoundException {
1108 // Get the services without throwing as this is an optional feature
Felipe Lemeecb08be2018-11-27 15:48:47 -08001109 Context outerContext = ctx.getOuterContext();
Felipe Leme326f15a2019-02-19 09:42:24 -08001110 ContentCaptureOptions options = outerContext.getContentCaptureOptions();
1111 // Options is null when the service didn't whitelist the activity or package
Felipe Lemecbf7f262019-04-17 13:57:59 -07001112 if (options != null && (options.lite || options.isWhitelisted(outerContext))) {
Felipe Lemeecb08be2018-11-27 15:48:47 -08001113 IBinder b = ServiceManager
1114 .getService(Context.CONTENT_CAPTURE_MANAGER_SERVICE);
Felipe Leme749b8892018-12-03 16:30:30 -08001115 IContentCaptureManager service = IContentCaptureManager.Stub.asInterface(b);
Felipe Leme326f15a2019-02-19 09:42:24 -08001116 // Service is null when not provided by OEM or disabled by kill-switch.
Felipe Lemed49d52c2019-02-15 09:48:20 -08001117 if (service != null) {
Felipe Leme326f15a2019-02-19 09:42:24 -08001118 return new ContentCaptureManager(outerContext, service, options);
Felipe Lemed49d52c2019-02-15 09:48:20 -08001119 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001120 }
Felipe Leme326f15a2019-02-19 09:42:24 -08001121 // When feature is disabled or app / package not whitelisted, we return a null
1122 // manager to apps so the performance impact is practically zero
Felipe Lemeecb08be2018-11-27 15:48:47 -08001123 return null;
Felipe Lemee348dc32018-11-05 12:35:29 -08001124 }});
1125
Sunny Goyal54e91342018-11-14 11:59:02 -08001126 registerService(Context.APP_PREDICTION_SERVICE, AppPredictionManager.class,
1127 new CachedServiceFetcher<AppPredictionManager>() {
1128 @Override
1129 public AppPredictionManager createService(ContextImpl ctx)
1130 throws ServiceNotFoundException {
Felipe Leme6378bd42019-08-14 18:14:55 -07001131 IBinder b = ServiceManager.getService(Context.APP_PREDICTION_SERVICE);
1132 return b == null ? null : new AppPredictionManager(ctx);
Sunny Goyal54e91342018-11-14 11:59:02 -08001133 }
1134 });
1135
Winson Chung3fb0f252019-01-08 17:41:55 -08001136 registerService(Context.CONTENT_SUGGESTIONS_SERVICE,
1137 ContentSuggestionsManager.class,
1138 new CachedServiceFetcher<ContentSuggestionsManager>() {
1139 @Override
1140 public ContentSuggestionsManager createService(ContextImpl ctx) {
1141 // No throw as this is an optional service
1142 IBinder b = ServiceManager.getService(
1143 Context.CONTENT_SUGGESTIONS_SERVICE);
1144 IContentSuggestionsManager service =
1145 IContentSuggestionsManager.Stub.asInterface(b);
Zak Cohen4834e9f2019-03-08 12:59:01 -08001146 return new ContentSuggestionsManager(ctx.getUserId(), service);
Winson Chung3fb0f252019-01-08 17:41:55 -08001147 }
1148 });
1149
Zak Cohen56345f42017-01-26 13:54:28 -08001150 registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
1151 @Override
1152 public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1153 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
1154 return new VrManager(IVrManager.Stub.asInterface(b));
1155 }
1156 });
Neil Fuller328532a2017-03-16 18:32:21 +00001157
1158 registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
1159 new CachedServiceFetcher<RulesManager>() {
1160 @Override
1161 public RulesManager createService(ContextImpl ctx) {
1162 return new RulesManager(ctx.getOuterContext());
1163 }});
Tony Mak1b708e62017-10-12 10:59:11 +01001164
1165 registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class,
1166 new CachedServiceFetcher<CrossProfileApps>() {
1167 @Override
1168 public CrossProfileApps createService(ContextImpl ctx)
1169 throws ServiceNotFoundException {
1170 IBinder b = ServiceManager.getServiceOrThrow(
1171 Context.CROSS_PROFILE_APPS_SERVICE);
1172 return new CrossProfileApps(ctx.getOuterContext(),
1173 ICrossProfileApps.Stub.asInterface(b));
1174 }
1175 });
Jason Monk8f5f7ff2017-10-17 14:12:42 -04001176
1177 registerService(Context.SLICE_SERVICE, SliceManager.class,
1178 new CachedServiceFetcher<SliceManager>() {
1179 @Override
1180 public SliceManager createService(ContextImpl ctx)
1181 throws ServiceNotFoundException {
1182 return new SliceManager(ctx.getOuterContext(),
1183 ctx.mMainThread.getHandler());
1184 }
1185 });
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001186
Neil Fullerfeeee682018-05-30 14:35:24 +01001187 registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class,
1188 new CachedServiceFetcher<TimeDetector>() {
1189 @Override
1190 public TimeDetector createService(ContextImpl ctx)
1191 throws ServiceNotFoundException {
1192 return new TimeDetector();
1193 }});
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001194
1195 registerService(Context.PERMISSION_SERVICE, PermissionManager.class,
1196 new CachedServiceFetcher<PermissionManager>() {
1197 @Override
1198 public PermissionManager createService(ContextImpl ctx) {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07001199 IPackageManager packageManager = AppGlobals.getPackageManager();
1200 return new PermissionManager(ctx.getOuterContext(), packageManager);
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001201 }});
Hai Zhang4ef21d02018-11-09 14:43:51 -08001202
Philip P. Moltmannbc054d82018-12-21 09:41:58 -08001203 registerService(Context.PERMISSION_CONTROLLER_SERVICE, PermissionControllerManager.class,
1204 new CachedServiceFetcher<PermissionControllerManager>() {
1205 @Override
1206 public PermissionControllerManager createService(ContextImpl ctx) {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07001207 return new PermissionControllerManager(ctx.getOuterContext(),
1208 ctx.getMainThreadHandler());
Philip P. Moltmannbc054d82018-12-21 09:41:58 -08001209 }});
1210
Hai Zhang4ef21d02018-11-09 14:43:51 -08001211 registerService(Context.ROLE_SERVICE, RoleManager.class,
1212 new CachedServiceFetcher<RoleManager>() {
1213 @Override
1214 public RoleManager createService(ContextImpl ctx)
1215 throws ServiceNotFoundException {
1216 return new RoleManager(ctx.getOuterContext());
1217 }});
Richard Uhlerb29f1452018-09-12 16:38:15 +01001218
Hai Zhanga4959e52019-03-06 12:21:07 -08001219 registerService(Context.ROLE_CONTROLLER_SERVICE, RoleControllerManager.class,
1220 new CachedServiceFetcher<RoleControllerManager>() {
1221 @Override
1222 public RoleControllerManager createService(ContextImpl ctx)
1223 throws ServiceNotFoundException {
1224 return new RoleControllerManager(ctx.getOuterContext());
1225 }});
1226
Richard Uhlerb29f1452018-09-12 16:38:15 +01001227 registerService(Context.ROLLBACK_SERVICE, RollbackManager.class,
1228 new CachedServiceFetcher<RollbackManager>() {
1229 @Override
1230 public RollbackManager createService(ContextImpl ctx)
1231 throws ServiceNotFoundException {
1232 IBinder b = ServiceManager.getServiceOrThrow(Context.ROLLBACK_SERVICE);
1233 return new RollbackManager(ctx.getOuterContext(),
1234 IRollbackManager.Stub.asInterface(b));
1235 }});
Howard Chen0a947642019-01-07 14:10:44 +08001236
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001237 registerService(Context.DYNAMIC_SYSTEM_SERVICE, DynamicSystemManager.class,
1238 new CachedServiceFetcher<DynamicSystemManager>() {
Howard Chen0a947642019-01-07 14:10:44 +08001239 @Override
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001240 public DynamicSystemManager createService(ContextImpl ctx)
Howard Chen0a947642019-01-07 14:10:44 +08001241 throws ServiceNotFoundException {
1242 IBinder b = ServiceManager.getServiceOrThrow(
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001243 Context.DYNAMIC_SYSTEM_SERVICE);
1244 return new DynamicSystemManager(
1245 IDynamicSystemService.Stub.asInterface(b));
Howard Chen0a947642019-01-07 14:10:44 +08001246 }});
Roshan Pius848513e2019-10-11 13:44:00 -07001247 registerService(Context.BATTERY_STATS_SERVICE, BatteryStatsManager.class,
1248 new CachedServiceFetcher<BatteryStatsManager>() {
1249 @Override
1250 public BatteryStatsManager createService(ContextImpl ctx)
1251 throws ServiceNotFoundException {
1252 IBinder b = ServiceManager.getServiceOrThrow(
1253 Context.BATTERY_STATS_SERVICE);
1254 return new BatteryStatsManager(
1255 IBatteryStats.Stub.asInterface(b));
1256 }});
Felipe Lemeecb08be2018-11-27 15:48:47 -08001257 //CHECKSTYLE:ON IndentationCheck
Makoto Onuki54c4e032019-10-30 12:05:20 -07001258
Makoto Onuki05b14f52019-11-05 12:36:07 -08001259 sInitializing = true;
1260 try {
1261 // Note: the following functions need to be @SystemApis, once they become mainline
1262 // modules.
Sudheer Shankaf5b36962019-10-04 16:16:13 -07001263
Makoto Onuki05b14f52019-11-05 12:36:07 -08001264 JobSchedulerFrameworkInitializer.registerServiceWrappers();
1265 BlobStoreManagerFrameworkInitializer.initialize();
Jayachandran C58fd3eb2019-11-20 19:01:56 -08001266 TelephonyFrameworkInitializer.registerServiceWrappers();
Terry Wangecc0d1b2019-10-17 17:05:18 -07001267 AppSearchManagerFrameworkInitializer.initialize();
Makoto Onuki05b14f52019-11-05 12:36:07 -08001268 } finally {
1269 // If any of the above code throws, we're in a pretty bad shape and the process
1270 // will likely crash, but we'll reset it just in case there's an exception handler...
1271 sInitializing = false;
1272 }
Jeff Brown6e539312015-02-24 18:53:21 -08001273 }
1274
Makoto Onuki05b14f52019-11-05 12:36:07 -08001275 /** Throws {@link IllegalStateException} if not during a static initialization. */
1276 private static void ensureInitializing(String methodName) {
1277 Preconditions.checkState(sInitializing, "Internal error: " + methodName
1278 + " can only be called during class initialization.");
1279 }
Jeff Brown6e539312015-02-24 18:53:21 -08001280 /**
1281 * Creates an array which is used to cache per-Context service instances.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001282 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001283 */
1284 public static Object[] createServiceCache() {
1285 return new Object[sServiceCacheSize];
1286 }
1287
1288 /**
1289 * Gets a system service from a given context.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001290 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001291 */
1292 public static Object getSystemService(ContextImpl ctx, String name) {
1293 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
1294 return fetcher != null ? fetcher.getService(ctx) : null;
1295 }
1296
1297 /**
Paul McLeane3383cc2015-05-08 11:41:20 -07001298 * Gets the name of the system-level service that is represented by the specified class.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001299 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001300 */
1301 public static String getSystemServiceName(Class<?> serviceClass) {
1302 return SYSTEM_SERVICE_NAMES.get(serviceClass);
1303 }
1304
1305 /**
1306 * Statically registers a system service with the context.
1307 * This method must be called during static initialization only.
1308 */
Makoto Onuki05b14f52019-11-05 12:36:07 -08001309 private static <T> void registerService(@NonNull String serviceName,
1310 @NonNull Class<T> serviceClass, @NonNull ServiceFetcher<T> serviceFetcher) {
Jeff Brown6e539312015-02-24 18:53:21 -08001311 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
1312 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
1313 }
1314
1315 /**
Makoto Onuki05b14f52019-11-05 12:36:07 -08001316 * Callback interface used as a parameter to {@link #registerStaticService(
1317 * String, Class, StaticServiceProducerNoBinder)}, which generates a service wrapper instance
1318 * that's not tied to any context and does not take a service binder object in the constructor.
1319 *
1320 * @param <TServiceClass> type of the service wrapper class.
Makoto Onuki792de8d2019-07-16 16:19:01 -07001321 *
1322 * @hide
1323 */
Makoto Onuki05b14f52019-11-05 12:36:07 -08001324 //@SystemApi TODO Make it a system API.
1325 public interface StaticServiceProducerNoBinder<TServiceClass> {
1326 /**
1327 * Return a new service wrapper of type {@code TServiceClass}.
1328 */
1329 TServiceClass createService();
1330 }
1331
1332 /**
1333 * Callback interface used as a parameter to {@link #registerStaticService(
1334 * String, Class, StaticServiceProducerWithBinder)}, which generates a service wrapper instance
1335 * that's not tied to any context and takes a service binder object in the constructor.
1336 *
1337 * @param <TServiceClass> type of the service wrapper class.
1338 *
1339 * @hide
1340 */
1341 //@SystemApi TODO Make it a system API.
1342 public interface StaticServiceProducerWithBinder<TServiceClass> {
1343 /**
1344 * Return a new service wrapper of type {@code TServiceClass} backed by a given
1345 * service binder object.
1346 */
1347 TServiceClass createService(IBinder serviceBinder);
1348 }
1349
1350 /**
1351 * Callback interface used as a parameter to {@link #registerContextAwareService(
1352 * String, Class, ContextAwareServiceProducerNoBinder)},
1353 * which generates a service wrapper instance
1354 * that's tied to a specific context and does not take a service binder object in the
1355 * constructor.
1356 *
1357 * @param <TServiceClass> type of the service wrapper class.
1358 *
1359 * @hide
1360 */
1361 //@SystemApi TODO Make it a system API.
1362 public interface ContextAwareServiceProducerNoBinder<TServiceClass> {
1363 /**
1364 * Return a new service wrapper of type {@code TServiceClass} tied to a given
1365 * {@code context}.
1366 *
1367 * TODO Do we need to pass the "base context" too?
1368 */
1369 TServiceClass createService(Context context);
1370 }
1371
1372 /**
1373 * Callback interface used as a parameter to {@link #registerContextAwareService(
1374 * String, Class, ContextAwareServiceProducerWithBinder)},
1375 * which generates a service wrapper instance
1376 * that's tied to a specific context and takes a service binder object in the constructor.
1377 *
1378 * @param <TServiceClass> type of the service wrapper class.
1379 *
1380 * @hide
1381 */
1382 //@SystemApi TODO Make it a system API.
1383 public interface ContextAwareServiceProducerWithBinder<TServiceClass> {
1384 /**
1385 * Return a new service wrapper of type {@code TServiceClass} backed by a given
1386 * service binder object that's tied to a given {@code context}.
1387 *
1388 * TODO Do we need to pass the "base context" too?
1389 */
1390 TServiceClass createService(Context context, IBinder serviceBinder);
1391 }
1392
1393 /**
1394 * Used by apex modules to register a "service wrapper" that is not tied to any {@link Context}.
1395 *
1396 * <p>This can only be called from the methods called by the static initializer of
1397 * {@link SystemServiceRegistry}. (Otherwise it throws a {@link IllegalStateException}.)
1398 *
1399 * @param serviceName the name of the binder object, such as
1400 * {@link Context#JOB_SCHEDULER_SERVICE}.
1401 * @param serviceWrapperClass the wrapper class, such as the class of
1402 * {@link android.app.job.JobScheduler}.
1403 * @param serviceProducer Callback that takes the service binder object with the name
1404 * {@code serviceName} and returns an actual service wrapper instance.
1405 *
1406 * @hide
1407 */
1408 //@SystemApi TODO Make it a system API.
1409 public static <TServiceClass> void registerStaticService(
1410 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1411 @NonNull StaticServiceProducerWithBinder<TServiceClass> serviceProducer) {
1412 ensureInitializing("registerStaticService");
1413 Preconditions.checkStringNotEmpty(serviceName);
1414 Preconditions.checkNotNull(serviceWrapperClass);
1415 Preconditions.checkNotNull(serviceProducer);
1416
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001417 registerService(serviceName, serviceWrapperClass,
Makoto Onuki05b14f52019-11-05 12:36:07 -08001418 new StaticServiceFetcher<TServiceClass>() {
Makoto Onuki792de8d2019-07-16 16:19:01 -07001419 @Override
Makoto Onuki05b14f52019-11-05 12:36:07 -08001420 public TServiceClass createService() throws ServiceNotFoundException {
1421 return serviceProducer.createService(
1422 ServiceManager.getServiceOrThrow(serviceName));
Makoto Onuki792de8d2019-07-16 16:19:01 -07001423 }});
1424 }
1425
1426 /**
Makoto Onuki05b14f52019-11-05 12:36:07 -08001427 * Similar to {@link #registerStaticService(String, Class, StaticServiceProducerWithBinder)},
1428 * but used for a "service wrapper" that doesn't take a service binder in its constructor.
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001429 *
1430 * @hide
1431 */
Makoto Onuki05b14f52019-11-05 12:36:07 -08001432 //@SystemApi TODO Make it a system API.
1433 public static <TServiceClass> void registerStaticService(
1434 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1435 @NonNull StaticServiceProducerNoBinder<TServiceClass> serviceProducer) {
1436 ensureInitializing("registerStaticService");
1437 Preconditions.checkStringNotEmpty(serviceName);
1438 Preconditions.checkNotNull(serviceWrapperClass);
1439 Preconditions.checkNotNull(serviceProducer);
1440
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001441 registerService(serviceName, serviceWrapperClass,
Makoto Onuki05b14f52019-11-05 12:36:07 -08001442 new StaticServiceFetcher<TServiceClass>() {
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001443 @Override
Makoto Onuki05b14f52019-11-05 12:36:07 -08001444 public TServiceClass createService() {
1445 return serviceProducer.createService();
1446 }});
1447 }
1448
1449 /**
1450 * Used by apex modules to register a "service wrapper" that is tied to a specific
1451 * {@link Context}.
1452 *
1453 * <p>This can only be called from the methods called by the static initializer of
1454 * {@link SystemServiceRegistry}. (Otherwise it throws a {@link IllegalStateException}.)
1455 *
1456 * @param serviceName the name of the binder object, such as
1457 * {@link Context#JOB_SCHEDULER_SERVICE}.
1458 * @param serviceWrapperClass the wrapper class, such as the class of
1459 * {@link android.app.job.JobScheduler}.
1460 * @param serviceProducer lambda that takes the service binder object with the name
1461 * {@code serviceName}, a {@link Context} and returns an actual service wrapper instance.
1462 *
1463 * @hide
1464 */
1465 //@SystemApi TODO Make it a system API.
1466 public static <TServiceClass> void registerContextAwareService(
1467 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1468 @NonNull ContextAwareServiceProducerWithBinder<TServiceClass> serviceProducer) {
1469 ensureInitializing("registerContextAwareService");
1470 Preconditions.checkStringNotEmpty(serviceName);
1471 Preconditions.checkNotNull(serviceWrapperClass);
1472 Preconditions.checkNotNull(serviceProducer);
1473
1474 registerService(serviceName, serviceWrapperClass,
1475 new CachedServiceFetcher<TServiceClass>() {
1476 @Override
1477 public TServiceClass createService(ContextImpl ctx)
1478 throws ServiceNotFoundException {
1479 return serviceProducer.createService(
1480 ctx.getOuterContext(),
1481 ServiceManager.getServiceOrThrow(serviceName));
1482 }});
1483 }
1484
1485
1486 /**
1487 * Similar to {@link #registerContextAwareService(String, Class,
1488 * ContextAwareServiceProducerWithBinder)},
1489 * but used for a "service wrapper" that doesn't take a service binder in its constructor.
1490 *
1491 * @hide
1492 */
1493 //@SystemApi TODO Make it a system API.
1494 public static <TServiceClass> void registerContextAwareService(
1495 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1496 @NonNull ContextAwareServiceProducerNoBinder<TServiceClass> serviceProducer) {
1497 ensureInitializing("registerContextAwareService");
1498 Preconditions.checkStringNotEmpty(serviceName);
1499 Preconditions.checkNotNull(serviceWrapperClass);
1500 Preconditions.checkNotNull(serviceProducer);
1501
1502 registerService(serviceName, serviceWrapperClass,
1503 new CachedServiceFetcher<TServiceClass>() {
1504 @Override
1505 public TServiceClass createService(ContextImpl ctx) {
1506 return serviceProducer.createService(ctx.getOuterContext());
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001507 }});
1508 }
1509
1510 /**
Jeff Brown6e539312015-02-24 18:53:21 -08001511 * Base interface for classes that fetch services.
1512 * These objects must only be created during static initialization.
1513 */
1514 static abstract interface ServiceFetcher<T> {
1515 T getService(ContextImpl ctx);
1516 }
1517
1518 /**
1519 * Override this class when the system service constructor needs a
1520 * ContextImpl and should be cached and retained by that context.
1521 */
1522 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
1523 private final int mCacheIndex;
1524
Makoto Onukid67070e2018-03-30 12:39:14 -07001525 CachedServiceFetcher() {
1526 // Note this class must be instantiated only by the static initializer of the
1527 // outer class (SystemServiceRegistry), which already does the synchronization,
1528 // so bare access to sServiceCacheSize is okay here.
Jeff Brown6e539312015-02-24 18:53:21 -08001529 mCacheIndex = sServiceCacheSize++;
1530 }
1531
1532 @Override
1533 @SuppressWarnings("unchecked")
1534 public final T getService(ContextImpl ctx) {
1535 final Object[] cache = ctx.mServiceCache;
Makoto Onukid67070e2018-03-30 12:39:14 -07001536 final int[] gates = ctx.mServiceInitializationStateArray;
Makoto Onukif896f122018-01-25 09:50:24 -08001537
Makoto Onukid67070e2018-03-30 12:39:14 -07001538 for (;;) {
1539 boolean doInitialize = false;
1540 synchronized (cache) {
1541 // Return it if we already have a cached instance.
1542 T service = (T) cache[mCacheIndex];
1543 if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) {
1544 return service;
1545 }
Makoto Onukif896f122018-01-25 09:50:24 -08001546
Makoto Onukid67070e2018-03-30 12:39:14 -07001547 // If we get here, there's no cached instance.
Makoto Onukif896f122018-01-25 09:50:24 -08001548
Makoto Onukid67070e2018-03-30 12:39:14 -07001549 // Grr... if gate is STATE_READY, then this means we initialized the service
1550 // once but someone cleared it.
1551 // We start over from STATE_UNINITIALIZED.
1552 if (gates[mCacheIndex] == ContextImpl.STATE_READY) {
1553 gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED;
1554 }
1555
1556 // It's possible for multiple threads to get here at the same time, so
1557 // use the "gate" to make sure only the first thread will call createService().
1558
1559 // At this point, the gate must be either UNINITIALIZED or INITIALIZING.
1560 if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) {
1561 doInitialize = true;
1562 gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING;
1563 }
Makoto Onukif896f122018-01-25 09:50:24 -08001564 }
1565
Makoto Onukid67070e2018-03-30 12:39:14 -07001566 if (doInitialize) {
1567 // Only the first thread gets here.
Makoto Onukif896f122018-01-25 09:50:24 -08001568
Makoto Onukid67070e2018-03-30 12:39:14 -07001569 T service = null;
1570 @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND;
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001571 try {
Makoto Onukid67070e2018-03-30 12:39:14 -07001572 // This thread is the first one to get here. Instantiate the service
1573 // *without* the cache lock held.
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001574 service = createService(ctx);
Makoto Onukid67070e2018-03-30 12:39:14 -07001575 newState = ContextImpl.STATE_READY;
Makoto Onukif896f122018-01-25 09:50:24 -08001576
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001577 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001578 onServiceNotFound(e);
Makoto Onukif896f122018-01-25 09:50:24 -08001579
Makoto Onukid67070e2018-03-30 12:39:14 -07001580 } finally {
1581 synchronized (cache) {
1582 cache[mCacheIndex] = service;
1583 gates[mCacheIndex] = newState;
1584 cache.notifyAll();
1585 }
1586 }
1587 return service;
1588 }
1589 // The other threads will wait for the first thread to call notifyAll(),
1590 // and go back to the top and retry.
1591 synchronized (cache) {
1592 while (gates[mCacheIndex] < ContextImpl.STATE_READY) {
1593 try {
1594 cache.wait();
1595 } catch (InterruptedException e) {
1596 Log.w(TAG, "getService() interrupted");
1597 Thread.currentThread().interrupt();
1598 return null;
1599 }
Makoto Onukif896f122018-01-25 09:50:24 -08001600 }
1601 }
Makoto Onukif896f122018-01-25 09:50:24 -08001602 }
Jeff Brown6e539312015-02-24 18:53:21 -08001603 }
1604
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001605 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001606 }
1607
1608 /**
1609 * Override this class when the system service does not need a ContextImpl
1610 * and should be cached and retained process-wide.
1611 */
1612 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
1613 private T mCachedInstance;
1614
1615 @Override
Jeff Sharkey589f3092016-11-29 12:54:39 -07001616 public final T getService(ContextImpl ctx) {
Jeff Brown6e539312015-02-24 18:53:21 -08001617 synchronized (StaticServiceFetcher.this) {
1618 if (mCachedInstance == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001619 try {
1620 mCachedInstance = createService();
1621 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001622 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001623 }
Jeff Brown6e539312015-02-24 18:53:21 -08001624 }
1625 return mCachedInstance;
1626 }
1627 }
1628
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001629 public abstract T createService() throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001630 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001631
1632 /**
Lorenzo Colitticf959772016-02-29 16:07:37 +09001633 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
1634 * creating the service for the first time, passes it the application context of the creating
1635 * application.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001636 *
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001637 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
1638 * case where multiple application components each have their own ConnectivityManager object.
1639 */
Lorenzo Colitticf959772016-02-29 16:07:37 +09001640 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001641 private T mCachedInstance;
1642
1643 @Override
1644 public final T getService(ContextImpl ctx) {
Lorenzo Colitticf959772016-02-29 16:07:37 +09001645 synchronized (StaticApplicationContextServiceFetcher.this) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001646 if (mCachedInstance == null) {
Lorenzo Colitti0ad9ab02016-03-25 17:40:11 +09001647 Context appContext = ctx.getApplicationContext();
1648 // If the application context is null, we're either in the system process or
1649 // it's the application context very early in app initialization. In both these
1650 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
1651 // to the service. http://b/27532714 .
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001652 try {
1653 mCachedInstance = createService(appContext != null ? appContext : ctx);
1654 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001655 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001656 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001657 }
1658 return mCachedInstance;
1659 }
1660 }
1661
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001662 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001663 }
Jeff Sharkey589f3092016-11-29 12:54:39 -07001664
Makoto Onuki05b14f52019-11-05 12:36:07 -08001665 /** @hide */
Jeff Sharkey589f3092016-11-29 12:54:39 -07001666 public static void onServiceNotFound(ServiceNotFoundException e) {
1667 // We're mostly interested in tracking down long-lived core system
1668 // components that might stumble if they obtain bad references; just
1669 // emit a tidy log message for normal apps
1670 if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1671 Log.wtf(TAG, e.getMessage(), e);
1672 } else {
1673 Log.w(TAG, e.getMessage());
1674 }
1675 }
Jeff Brown6e539312015-02-24 18:53:21 -08001676}