blob: f170b5dfbc27c768a433a73c197a6dcb2ff05c84 [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 Onuki0ceb3e02019-11-18 10:37:10 -080022import android.annotation.SystemApi;
Makoto Onukid67070e2018-03-30 12:39:14 -070023import android.app.ContextImpl.ServiceInitializationState;
Jeff Brown6e539312015-02-24 18:53:21 -080024import android.app.admin.DevicePolicyManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060025import android.app.admin.IDevicePolicyManager;
Terry Wangecc0d1b2019-10-17 17:05:18 -070026import android.app.appsearch.AppSearchManagerFrameworkInitializer;
Sudheer Shankaf5b36962019-10-04 16:16:13 -070027import android.app.blob.BlobStoreManagerFrameworkInitializer;
Winson Chung3fb0f252019-01-08 17:41:55 -080028import android.app.contentsuggestions.ContentSuggestionsManager;
29import android.app.contentsuggestions.IContentSuggestionsManager;
Makoto Onuki54c4e032019-10-30 12:05:20 -070030import android.app.job.JobSchedulerFrameworkInitializer;
Sunny Goyal54e91342018-11-14 11:59:02 -080031import android.app.prediction.AppPredictionManager;
Hai Zhanga4959e52019-03-06 12:21:07 -080032import android.app.role.RoleControllerManager;
Hai Zhang4ef21d02018-11-09 14:43:51 -080033import android.app.role.RoleManager;
Jason Monk8f5f7ff2017-10-17 14:12:42 -040034import android.app.slice.SliceManager;
Neil Fullerfeeee682018-05-30 14:35:24 +010035import android.app.timedetector.TimeDetector;
Neil Fuller328532a2017-03-16 18:32:21 +000036import android.app.timezone.RulesManager;
Neil Fuller3e3b5402019-11-07 15:35:05 +000037import android.app.timezonedetector.TimeZoneDetector;
Jeff Brown6e539312015-02-24 18:53:21 -080038import android.app.trust.TrustManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070039import android.app.usage.IStorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080040import android.app.usage.IUsageStatsManager;
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +000041import android.app.usage.NetworkStatsManager;
Jeff Sharkeye8cece92017-01-04 11:33:33 -070042import android.app.usage.StorageStatsManager;
Jeff Brown6e539312015-02-24 18:53:21 -080043import android.app.usage.UsageStatsManager;
44import android.appwidget.AppWidgetManager;
45import android.bluetooth.BluetoothManager;
Eugene Susla6ed45d82017-01-22 13:52:51 -080046import android.companion.CompanionDeviceManager;
47import android.companion.ICompanionDeviceManager;
Jeff Brown6e539312015-02-24 18:53:21 -080048import android.content.ClipboardManager;
Felipe Leme326f15a2019-02-19 09:42:24 -080049import android.content.ContentCaptureOptions;
Jeff Brown6e539312015-02-24 18:53:21 -080050import android.content.Context;
51import android.content.IRestrictionsManager;
52import android.content.RestrictionsManager;
Song Pan6e3677c2019-10-29 14:19:26 +000053import android.content.integrity.AppIntegrityManager;
54import android.content.integrity.IAppIntegrityManager;
Hyunyoung Song880a9512018-12-20 11:24:48 -080055import android.content.om.IOverlayManager;
56import android.content.om.OverlayManager;
Felipe Leme8ac0d0d2020-01-14 13:37:41 -080057import android.content.pm.ApplicationInfo;
Tony Makb0d22622018-01-18 12:49:49 +000058import android.content.pm.CrossProfileApps;
Songchun Fan6cde9d92019-12-10 10:23:54 -080059import android.content.pm.DataLoaderManager;
Tony Makb0d22622018-01-18 12:49:49 +000060import android.content.pm.ICrossProfileApps;
Songchun Fan6cde9d92019-12-10 10:23:54 -080061import android.content.pm.IDataLoaderManager;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070062import android.content.pm.IPackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080063import android.content.pm.IShortcutService;
Jeff Brown6e539312015-02-24 18:53:21 -080064import android.content.pm.LauncherApps;
Svet Ganovec7daa52017-06-30 12:02:59 -070065import android.content.pm.PackageManager;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080066import android.content.pm.ShortcutManager;
Jeff Brown6e539312015-02-24 18:53:21 -080067import android.content.res.Resources;
Richard Uhlerb29f1452018-09-12 16:38:15 +010068import android.content.rollback.IRollbackManager;
69import android.content.rollback.RollbackManager;
Kenny Rootf74bfde2018-01-18 15:42:48 -080070import android.debug.AdbManager;
71import android.debug.IAdbManager;
Jeff Brown6e539312015-02-24 18:53:21 -080072import android.hardware.ConsumerIrManager;
73import android.hardware.ISerialManager;
74import android.hardware.SensorManager;
Chad Brubaker90f391f2018-10-19 10:26:19 -070075import android.hardware.SensorPrivacyManager;
Jeff Brown6e539312015-02-24 18:53:21 -080076import android.hardware.SerialManager;
77import android.hardware.SystemSensorManager;
Kevin Chyn05c21502018-09-18 13:07:19 -070078import android.hardware.biometrics.BiometricManager;
Ilya Matyukhine4675b32019-11-07 16:07:19 -080079import android.hardware.biometrics.IAuthService;
Jeff Brown6e539312015-02-24 18:53:21 -080080import android.hardware.camera2.CameraManager;
Christine Franks39b03112018-07-03 14:46:07 -070081import android.hardware.display.ColorDisplayManager;
Jeff Brown6e539312015-02-24 18:53:21 -080082import android.hardware.display.DisplayManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020083import android.hardware.face.FaceManager;
84import android.hardware.face.IFaceService;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060085import android.hardware.fingerprint.FingerprintManager;
86import android.hardware.fingerprint.IFingerprintService;
Jeff Brown6e539312015-02-24 18:53:21 -080087import android.hardware.hdmi.HdmiControlManager;
88import android.hardware.hdmi.IHdmiControlService;
89import android.hardware.input.InputManager;
Kevin Chyn51676d22018-11-05 18:00:43 -080090import android.hardware.iris.IIrisService;
Felipe Lemee348dc32018-11-05 12:35:29 -080091import android.hardware.iris.IrisManager;
Ivailo Karamanolev090d02c2020-01-09 17:02:49 +010092import android.hardware.lights.LightsManager;
Peng Xu9ff7d222016-02-11 13:02:05 -080093import android.hardware.location.ContextHubManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060094import android.hardware.radio.RadioManager;
Jeff Brown6e539312015-02-24 18:53:21 -080095import android.hardware.usb.IUsbManager;
96import android.hardware.usb.UsbManager;
97import android.location.CountryDetector;
98import android.location.ICountryDetector;
99import android.location.ILocationManager;
100import android.location.LocationManager;
101import android.media.AudioManager;
102import android.media.MediaRouter;
103import android.media.midi.IMidiManager;
104import android.media.midi.MidiManager;
105import android.media.projection.MediaProjectionManager;
106import android.media.session.MediaSessionManager;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800107import android.media.soundtrigger.SoundTriggerManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800108import android.media.tv.ITvInputManager;
109import android.media.tv.TvInputManager;
Cody Kestingb125776002019-12-17 17:21:40 -0800110import android.net.ConnectivityDiagnosticsManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800111import android.net.ConnectivityManager;
Paul Stewartbf8cbb052016-03-17 10:11:54 -0700112import android.net.ConnectivityThread;
Jeff Brown6e539312015-02-24 18:53:21 -0800113import android.net.EthernetManager;
114import android.net.IConnectivityManager;
115import android.net.IEthernetManager;
Nathan Harold28084d82017-03-01 18:55:06 -0800116import android.net.IIpSecService;
Jeff Brown6e539312015-02-24 18:53:21 -0800117import android.net.INetworkPolicyManager;
Benedict Wong99a48412018-11-09 14:45:34 -0800118import android.net.ITestNetworkManager;
Nathan Harold28084d82017-03-01 18:55:06 -0800119import android.net.IpSecManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800120import android.net.NetworkPolicyManager;
121import android.net.NetworkScoreManager;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100122import android.net.NetworkWatchlistManager;
Benedict Wong99a48412018-11-09 14:45:34 -0800123import android.net.TestNetworkManager;
markchienae8aa642019-12-16 20:15:20 +0800124import android.net.TetheringManager;
Benedict Wong50b44432019-11-01 16:46:28 -0700125import android.net.VpnManager;
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700126import android.net.lowpan.ILowpanManager;
127import android.net.lowpan.LowpanManager;
Etan Cohen17ba4722017-08-21 10:52:17 -0700128import android.net.nsd.INsdManager;
129import android.net.nsd.NsdManager;
David Su409f3712019-10-24 11:18:41 -0700130import android.net.wifi.WifiFrameworkInitializer;
Etan Cohen5abc6d92019-12-18 10:54:28 -0800131import android.net.wifi.wificond.WifiCondManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800132import android.nfc.NfcManager;
133import android.os.BatteryManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600134import android.os.BatteryStats;
Roshan Pius848513e2019-10-11 13:44:00 -0700135import android.os.BatteryStatsManager;
Nandana Duttd11850c2018-12-12 17:26:57 +0000136import android.os.BugreportManager;
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600137import android.os.Build;
Jeff Brown6e539312015-02-24 18:53:21 -0800138import android.os.DropBoxManager;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100139import android.os.HardwarePropertiesManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600140import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6e539312015-02-24 18:53:21 -0800141import android.os.IBinder;
Nandana Duttd11850c2018-12-12 17:26:57 +0000142import android.os.IDumpstate;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100143import android.os.IHardwarePropertiesManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800144import android.os.IPowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800145import android.os.IRecoverySystem;
Tao Bao07342dc2017-01-24 15:08:21 -0800146import android.os.ISystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800147import android.os.IUserManager;
Joe Onorato1754d742016-11-21 17:51:35 -0800148import android.os.IncidentManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800149import android.os.PowerManager;
Tao Baoe8a403d2015-12-31 07:44:55 -0800150import android.os.RecoverySystem;
Benedict Wong99a48412018-11-09 14:45:34 -0800151import android.os.RemoteException;
Jeff Brown6e539312015-02-24 18:53:21 -0800152import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600153import android.os.ServiceManager.ServiceNotFoundException;
Jeffrey Huangf58800b2020-01-23 13:22:10 -0800154import android.os.StatsFrameworkInitializer;
Hall Liub38ad5f2019-12-23 18:11:10 -0800155import android.os.SystemConfigManager;
Tao Bao07342dc2017-01-24 15:08:21 -0800156import android.os.SystemUpdateManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800157import android.os.SystemVibrator;
158import android.os.UserHandle;
159import android.os.UserManager;
160import android.os.Vibrator;
Joe Onorato713fec82016-03-04 10:34:02 -0800161import android.os.health.SystemHealthManager;
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +0800162import android.os.image.DynamicSystemManager;
163import android.os.image.IDynamicSystemService;
Songchun Fan43f18ea2019-12-11 17:54:38 -0800164import android.os.incremental.IIncrementalManagerNative;
165import android.os.incremental.IncrementalManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800166import android.os.storage.StorageManager;
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800167import android.permission.PermissionControllerManager;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700168import android.permission.PermissionManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800169import android.print.IPrintManager;
170import android.print.PrintManager;
Victor Hsieh20fe1f62019-09-30 13:36:21 -0700171import android.security.FileIntegrityManager;
172import android.security.IFileIntegrityService;
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000173import android.service.oemlock.IOemLockService;
174import android.service.oemlock.OemLockManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800175import android.service.persistentdata.IPersistentDataBlockService;
176import android.service.persistentdata.PersistentDataBlockManager;
Zak Cohen56345f42017-01-26 13:54:28 -0800177import android.service.vr.IVrManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800178import android.telecom.TelecomManager;
Sarah Chin4affb512020-01-10 16:09:11 -0800179import android.telephony.MmsManager;
Jayachandran C58fd3eb2019-11-20 19:01:56 -0800180import android.telephony.TelephonyFrameworkInitializer;
Makoto Onuki54c4e032019-10-30 12:05:20 -0700181import android.telephony.TelephonyRegistryManager;
Robert Benea1901a5b2018-05-15 16:36:07 -0700182import android.util.ArrayMap;
Jeff Brown6e539312015-02-24 18:53:21 -0800183import android.util.Log;
184import android.view.ContextThemeWrapper;
185import android.view.LayoutInflater;
Jeff Brown6e539312015-02-24 18:53:21 -0800186import android.view.WindowManager;
187import android.view.WindowManagerImpl;
188import android.view.accessibility.AccessibilityManager;
189import android.view.accessibility.CaptioningManager;
Neil Fuller009e77a2017-05-04 16:53:41 +0100190import android.view.autofill.AutofillManager;
191import android.view.autofill.IAutoFillManager;
Felipe Leme749b8892018-12-03 16:30:30 -0800192import android.view.contentcapture.ContentCaptureManager;
193import android.view.contentcapture.IContentCaptureManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800194import android.view.inputmethod.InputMethodManager;
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000195import android.view.textclassifier.TextClassificationManager;
Jeff Brown6e539312015-02-24 18:53:21 -0800196import android.view.textservice.TextServicesManager;
197
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600198import com.android.internal.app.IAppOpsService;
199import com.android.internal.app.IBatteryStats;
200import com.android.internal.app.ISoundTriggerService;
201import com.android.internal.appwidget.IAppWidgetService;
Ricky Wai1a6e6672017-10-27 14:46:01 +0100202import com.android.internal.net.INetworkWatchlistManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600203import com.android.internal.os.IDropBoxManagerService;
204import com.android.internal.policy.PhoneLayoutInflater;
Makoto Onuki05b14f52019-11-05 12:36:07 -0800205import com.android.internal.util.Preconditions;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600206
Robert Benea1901a5b2018-05-15 16:36:07 -0700207import java.util.Map;
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +0000208import java.util.Objects;
Jeff Brown6e539312015-02-24 18:53:21 -0800209
210/**
211 * Manages all of the system services that can be returned by {@link Context#getSystemService}.
212 * Used by {@link ContextImpl}.
Makoto Onuki792de8d2019-07-16 16:19:01 -0700213 *
214 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -0800215 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -0800216@SystemApi
Makoto Onuki792de8d2019-07-16 16:19:01 -0700217public final class SystemServiceRegistry {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600218 private static final String TAG = "SystemServiceRegistry";
219
Jeff Brown6e539312015-02-24 18:53:21 -0800220 // Service registry information.
221 // This information is never changed once static initialization has completed.
Robert Benea1901a5b2018-05-15 16:36:07 -0700222 private static final Map<Class<?>, String> SYSTEM_SERVICE_NAMES =
223 new ArrayMap<Class<?>, String>();
224 private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
225 new ArrayMap<String, ServiceFetcher<?>>();
Jeff Brown6e539312015-02-24 18:53:21 -0800226 private static int sServiceCacheSize;
227
Makoto Onuki05b14f52019-11-05 12:36:07 -0800228 private static volatile boolean sInitializing;
229
Jeff Brown6e539312015-02-24 18:53:21 -0800230 // Not instantiable.
231 private SystemServiceRegistry() { }
232
233 static {
Felipe Lemeecb08be2018-11-27 15:48:47 -0800234 //CHECKSTYLE:OFF IndentationCheck
Jeff Brown6e539312015-02-24 18:53:21 -0800235 registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
236 new CachedServiceFetcher<AccessibilityManager>() {
237 @Override
238 public AccessibilityManager createService(ContextImpl ctx) {
239 return AccessibilityManager.getInstance(ctx);
240 }});
241
242 registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
243 new CachedServiceFetcher<CaptioningManager>() {
244 @Override
245 public CaptioningManager createService(ContextImpl ctx) {
246 return new CaptioningManager(ctx);
247 }});
248
249 registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
250 new CachedServiceFetcher<AccountManager>() {
251 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600252 public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
253 IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800254 IAccountManager service = IAccountManager.Stub.asInterface(b);
255 return new AccountManager(ctx, service);
256 }});
257
258 registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
259 new CachedServiceFetcher<ActivityManager>() {
260 @Override
261 public ActivityManager createService(ContextImpl ctx) {
262 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
263 }});
264
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700265 registerService(Context.ACTIVITY_TASK_SERVICE, ActivityTaskManager.class,
266 new CachedServiceFetcher<ActivityTaskManager>() {
267 @Override
268 public ActivityTaskManager createService(ContextImpl ctx) {
269 return new ActivityTaskManager(
270 ctx.getOuterContext(), ctx.mMainThread.getHandler());
271 }});
272
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700273 registerService(Context.URI_GRANTS_SERVICE, UriGrantsManager.class,
274 new CachedServiceFetcher<UriGrantsManager>() {
275 @Override
276 public UriGrantsManager createService(ContextImpl ctx) {
277 return new UriGrantsManager(
278 ctx.getOuterContext(), ctx.mMainThread.getHandler());
279 }});
280
Jeff Brown6e539312015-02-24 18:53:21 -0800281 registerService(Context.ALARM_SERVICE, AlarmManager.class,
282 new CachedServiceFetcher<AlarmManager>() {
283 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600284 public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
285 IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800286 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
287 return new AlarmManager(service, ctx);
288 }});
289
290 registerService(Context.AUDIO_SERVICE, AudioManager.class,
291 new CachedServiceFetcher<AudioManager>() {
292 @Override
293 public AudioManager createService(ContextImpl ctx) {
294 return new AudioManager(ctx);
295 }});
296
297 registerService(Context.MEDIA_ROUTER_SERVICE, MediaRouter.class,
298 new CachedServiceFetcher<MediaRouter>() {
299 @Override
300 public MediaRouter createService(ContextImpl ctx) {
301 return new MediaRouter(ctx);
302 }});
303
304 registerService(Context.BLUETOOTH_SERVICE, BluetoothManager.class,
305 new CachedServiceFetcher<BluetoothManager>() {
306 @Override
307 public BluetoothManager createService(ContextImpl ctx) {
308 return new BluetoothManager(ctx);
309 }});
310
311 registerService(Context.HDMI_CONTROL_SERVICE, HdmiControlManager.class,
312 new StaticServiceFetcher<HdmiControlManager>() {
313 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600314 public HdmiControlManager createService() throws ServiceNotFoundException {
315 IBinder b = ServiceManager.getServiceOrThrow(Context.HDMI_CONTROL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800316 return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b));
317 }});
318
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000319 registerService(Context.TEXT_CLASSIFICATION_SERVICE, TextClassificationManager.class,
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000320 new CachedServiceFetcher<TextClassificationManager>() {
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000321 @Override
Abodunrinwa Tokif001fef2017-01-04 23:51:42 +0000322 public TextClassificationManager createService(ContextImpl ctx) {
323 return new TextClassificationManager(ctx);
Abodunrinwa Toki8158af52016-11-23 20:41:09 +0000324 }});
325
Jeff Brown6e539312015-02-24 18:53:21 -0800326 registerService(Context.CLIPBOARD_SERVICE, ClipboardManager.class,
327 new CachedServiceFetcher<ClipboardManager>() {
328 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600329 public ClipboardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800330 return new ClipboardManager(ctx.getOuterContext(),
331 ctx.mMainThread.getHandler());
332 }});
333
334 // The clipboard service moved to a new package. If someone asks for the old
335 // interface by class then we want to redirect over to the new interface instead
336 // (which extends it).
337 SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
338
339 registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
Lorenzo Colitticf959772016-02-29 16:07:37 +0900340 new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800341 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600342 public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
343 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900344 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
345 return new ConnectivityManager(context, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800346 }});
347
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +0900348 registerService(Context.NETD_SERVICE, IBinder.class, new StaticServiceFetcher<IBinder>() {
Remi NGUYEN VAN31935982019-01-28 11:40:08 +0900349 @Override
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +0900350 public IBinder createService() throws ServiceNotFoundException {
351 return ServiceManager.getServiceOrThrow(Context.NETD_SERVICE);
Remi NGUYEN VAN31935982019-01-28 11:40:08 +0900352 }
353 });
354
Remi NGUYEN VAN05f7b5c2020-01-07 11:44:14 +0900355 registerService(Context.NETWORK_STACK_SERVICE, IBinder.class,
356 new StaticServiceFetcher<IBinder>() {
357 @Override
358 public IBinder createService() {
359 return ServiceManager.getService(Context.NETWORK_STACK_SERVICE);
360 }
361 });
362
markchienae8aa642019-12-16 20:15:20 +0800363 registerService(Context.TETHERING_SERVICE, TetheringManager.class,
364 new CachedServiceFetcher<TetheringManager>() {
365 @Override
366 public TetheringManager createService(ContextImpl ctx) throws ServiceNotFoundException {
367 IBinder b = ServiceManager.getService(Context.TETHERING_SERVICE);
368 if (b == null) return null;
369
370 return new TetheringManager(ctx, b);
371 }});
372
373
Nathan Harold28084d82017-03-01 18:55:06 -0800374 registerService(Context.IPSEC_SERVICE, IpSecManager.class,
Nathan Harold592dadb2018-03-15 18:06:06 -0700375 new CachedServiceFetcher<IpSecManager>() {
Nathan Harold28084d82017-03-01 18:55:06 -0800376 @Override
Nathan Harold592dadb2018-03-15 18:06:06 -0700377 public IpSecManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nathan Harold28084d82017-03-01 18:55:06 -0800378 IBinder b = ServiceManager.getService(Context.IPSEC_SERVICE);
379 IIpSecService service = IIpSecService.Stub.asInterface(b);
Nathan Harold592dadb2018-03-15 18:06:06 -0700380 return new IpSecManager(ctx, service);
Nathan Harold28084d82017-03-01 18:55:06 -0800381 }});
382
Benedict Wong50b44432019-11-01 16:46:28 -0700383 registerService(Context.VPN_MANAGEMENT_SERVICE, VpnManager.class,
384 new CachedServiceFetcher<VpnManager>() {
385 @Override
386 public VpnManager createService(ContextImpl ctx) throws ServiceNotFoundException {
387 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
388 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
389 return new VpnManager(ctx, service);
390 }});
391
Cody Kestingb125776002019-12-17 17:21:40 -0800392 registerService(Context.CONNECTIVITY_DIAGNOSTICS_SERVICE,
393 ConnectivityDiagnosticsManager.class,
394 new CachedServiceFetcher<ConnectivityDiagnosticsManager>() {
395 @Override
396 public ConnectivityDiagnosticsManager createService(ContextImpl ctx)
397 throws ServiceNotFoundException {
398 // ConnectivityDiagnosticsManager is backed by ConnectivityService
399 IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
400 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
401 return new ConnectivityDiagnosticsManager(ctx, service);
402 }});
403
Benedict Wong99a48412018-11-09 14:45:34 -0800404 registerService(
405 Context.TEST_NETWORK_SERVICE,
406 TestNetworkManager.class,
407 new StaticApplicationContextServiceFetcher<TestNetworkManager>() {
408 @Override
409 public TestNetworkManager createService(Context context)
410 throws ServiceNotFoundException {
411 IBinder csBinder =
412 ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
413 IConnectivityManager csMgr =
414 IConnectivityManager.Stub.asInterface(csBinder);
415
416 final IBinder tnBinder;
417 try {
418 tnBinder = csMgr.startOrGetTestNetworkService();
419 } catch (RemoteException e) {
420 throw new ServiceNotFoundException(Context.TEST_NETWORK_SERVICE);
421 }
422 ITestNetworkManager tnMgr = ITestNetworkManager.Stub.asInterface(tnBinder);
Benedict Wongb4e98f32019-03-12 21:54:16 -0700423 return new TestNetworkManager(tnMgr);
Benedict Wong99a48412018-11-09 14:45:34 -0800424 }
425 });
426
Jeff Brown6e539312015-02-24 18:53:21 -0800427 registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
428 new StaticServiceFetcher<CountryDetector>() {
429 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600430 public CountryDetector createService() throws ServiceNotFoundException {
431 IBinder b = ServiceManager.getServiceOrThrow(Context.COUNTRY_DETECTOR);
Jeff Brown6e539312015-02-24 18:53:21 -0800432 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
433 }});
434
435 registerService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class,
436 new CachedServiceFetcher<DevicePolicyManager>() {
437 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600438 public DevicePolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
439 IBinder b = ServiceManager.getServiceOrThrow(Context.DEVICE_POLICY_SERVICE);
440 return new DevicePolicyManager(ctx, IDevicePolicyManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -0800441 }});
442
443 registerService(Context.DOWNLOAD_SERVICE, DownloadManager.class,
444 new CachedServiceFetcher<DownloadManager>() {
445 @Override
446 public DownloadManager createService(ContextImpl ctx) {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700447 return new DownloadManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800448 }});
449
450 registerService(Context.BATTERY_SERVICE, BatteryManager.class,
Yifan Hongb0f13522017-10-31 15:48:50 -0700451 new CachedServiceFetcher<BatteryManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800452 @Override
Yifan Hongb0f13522017-10-31 15:48:50 -0700453 public BatteryManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600454 IBatteryStats stats = IBatteryStats.Stub.asInterface(
455 ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME));
456 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub
457 .asInterface(ServiceManager.getServiceOrThrow("batteryproperties"));
Yifan Hongb0f13522017-10-31 15:48:50 -0700458 return new BatteryManager(ctx, stats, registrar);
Jeff Brown6e539312015-02-24 18:53:21 -0800459 }});
460
461 registerService(Context.NFC_SERVICE, NfcManager.class,
462 new CachedServiceFetcher<NfcManager>() {
463 @Override
464 public NfcManager createService(ContextImpl ctx) {
465 return new NfcManager(ctx);
466 }});
467
468 registerService(Context.DROPBOX_SERVICE, DropBoxManager.class,
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700469 new CachedServiceFetcher<DropBoxManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800470 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600471 public DropBoxManager createService(ContextImpl ctx) throws ServiceNotFoundException {
472 IBinder b = ServiceManager.getServiceOrThrow(Context.DROPBOX_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800473 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700474 return new DropBoxManager(ctx, service);
Jeff Brown6e539312015-02-24 18:53:21 -0800475 }});
476
477 registerService(Context.INPUT_SERVICE, InputManager.class,
478 new StaticServiceFetcher<InputManager>() {
479 @Override
480 public InputManager createService() {
481 return InputManager.getInstance();
482 }});
483
484 registerService(Context.DISPLAY_SERVICE, DisplayManager.class,
485 new CachedServiceFetcher<DisplayManager>() {
486 @Override
487 public DisplayManager createService(ContextImpl ctx) {
488 return new DisplayManager(ctx.getOuterContext());
489 }});
490
Christine Franks39b03112018-07-03 14:46:07 -0700491 registerService(Context.COLOR_DISPLAY_SERVICE, ColorDisplayManager.class,
492 new CachedServiceFetcher<ColorDisplayManager>() {
493 @Override
494 public ColorDisplayManager createService(ContextImpl ctx) {
495 return new ColorDisplayManager();
496 }
497 });
498
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800499 // InputMethodManager has its own cache strategy based on display id to support apps that
500 // still assume InputMethodManager is a per-process singleton and it's safe to directly
501 // access internal fields via reflection. Hence directly use ServiceFetcher instead of
502 // StaticServiceFetcher/CachedServiceFetcher.
Jeff Brown6e539312015-02-24 18:53:21 -0800503 registerService(Context.INPUT_METHOD_SERVICE, InputMethodManager.class,
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800504 new ServiceFetcher<InputMethodManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800505 @Override
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800506 public InputMethodManager getService(ContextImpl ctx) {
507 return InputMethodManager.forContext(ctx.getOuterContext());
Jeff Brown6e539312015-02-24 18:53:21 -0800508 }});
509
510 registerService(Context.TEXT_SERVICES_MANAGER_SERVICE, TextServicesManager.class,
Yohei Yukawa9f141ee2019-01-18 09:17:16 -0800511 new CachedServiceFetcher<TextServicesManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800512 @Override
Yohei Yukawa9f141ee2019-01-18 09:17:16 -0800513 public TextServicesManager createService(ContextImpl ctx)
514 throws ServiceNotFoundException {
515 return TextServicesManager.createInstance(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800516 }});
517
518 registerService(Context.KEYGUARD_SERVICE, KeyguardManager.class,
Nancy Zheng43f166d2016-11-18 18:15:09 -0800519 new CachedServiceFetcher<KeyguardManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800520 @Override
Nancy Zheng40cd8e42016-11-21 12:58:16 -0800521 public KeyguardManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Nancy Zheng43f166d2016-11-18 18:15:09 -0800522 return new KeyguardManager(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800523 }});
524
525 registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,
526 new CachedServiceFetcher<LayoutInflater>() {
527 @Override
528 public LayoutInflater createService(ContextImpl ctx) {
529 return new PhoneLayoutInflater(ctx.getOuterContext());
530 }});
531
532 registerService(Context.LOCATION_SERVICE, LocationManager.class,
533 new CachedServiceFetcher<LocationManager>() {
534 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600535 public LocationManager createService(ContextImpl ctx) throws ServiceNotFoundException {
536 IBinder b = ServiceManager.getServiceOrThrow(Context.LOCATION_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800537 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
538 }});
539
540 registerService(Context.NETWORK_POLICY_SERVICE, NetworkPolicyManager.class,
Svet Ganov16a16892015-04-16 10:32:04 -0700541 new CachedServiceFetcher<NetworkPolicyManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800542 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600543 public NetworkPolicyManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov16a16892015-04-16 10:32:04 -0700544 return new NetworkPolicyManager(ctx, INetworkPolicyManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600545 ServiceManager.getServiceOrThrow(Context.NETWORK_POLICY_SERVICE)));
Jeff Brown6e539312015-02-24 18:53:21 -0800546 }});
547
548 registerService(Context.NOTIFICATION_SERVICE, NotificationManager.class,
549 new CachedServiceFetcher<NotificationManager>() {
550 @Override
551 public NotificationManager createService(ContextImpl ctx) {
552 final Context outerContext = ctx.getOuterContext();
553 return new NotificationManager(
554 new ContextThemeWrapper(outerContext,
555 Resources.selectSystemTheme(0,
556 outerContext.getApplicationInfo().targetSdkVersion,
557 com.android.internal.R.style.Theme_Dialog,
558 com.android.internal.R.style.Theme_Holo_Dialog,
559 com.android.internal.R.style.Theme_DeviceDefault_Dialog,
560 com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)),
561 ctx.mMainThread.getHandler());
562 }});
563
564 registerService(Context.NSD_SERVICE, NsdManager.class,
565 new CachedServiceFetcher<NsdManager>() {
566 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600567 public NsdManager createService(ContextImpl ctx) throws ServiceNotFoundException {
568 IBinder b = ServiceManager.getServiceOrThrow(Context.NSD_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800569 INsdManager service = INsdManager.Stub.asInterface(b);
570 return new NsdManager(ctx.getOuterContext(), service);
571 }});
572
573 registerService(Context.POWER_SERVICE, PowerManager.class,
574 new CachedServiceFetcher<PowerManager>() {
575 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600576 public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
577 IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800578 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown6e539312015-02-24 18:53:21 -0800579 return new PowerManager(ctx.getOuterContext(),
580 service, ctx.mMainThread.getHandler());
581 }});
582
Tao Baoe8a403d2015-12-31 07:44:55 -0800583 registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
584 new CachedServiceFetcher<RecoverySystem>() {
585 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600586 public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
587 IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
Tao Baoe8a403d2015-12-31 07:44:55 -0800588 IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
Tao Baoe8a403d2015-12-31 07:44:55 -0800589 return new RecoverySystem(service);
590 }});
591
Jeff Brown6e539312015-02-24 18:53:21 -0800592 registerService(Context.SEARCH_SERVICE, SearchManager.class,
593 new CachedServiceFetcher<SearchManager>() {
594 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600595 public SearchManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800596 return new SearchManager(ctx.getOuterContext(),
597 ctx.mMainThread.getHandler());
598 }});
599
600 registerService(Context.SENSOR_SERVICE, SensorManager.class,
601 new CachedServiceFetcher<SensorManager>() {
602 @Override
603 public SensorManager createService(ContextImpl ctx) {
604 return new SystemSensorManager(ctx.getOuterContext(),
605 ctx.mMainThread.getHandler().getLooper());
606 }});
607
Chad Brubaker90f391f2018-10-19 10:26:19 -0700608 registerService(Context.SENSOR_PRIVACY_SERVICE, SensorPrivacyManager.class,
609 new CachedServiceFetcher<SensorPrivacyManager>() {
610 @Override
611 public SensorPrivacyManager createService(ContextImpl ctx) {
612 return SensorPrivacyManager.getInstance(ctx);
613 }});
614
Jeff Brown6e539312015-02-24 18:53:21 -0800615 registerService(Context.STATUS_BAR_SERVICE, StatusBarManager.class,
616 new CachedServiceFetcher<StatusBarManager>() {
617 @Override
618 public StatusBarManager createService(ContextImpl ctx) {
619 return new StatusBarManager(ctx.getOuterContext());
620 }});
621
622 registerService(Context.STORAGE_SERVICE, StorageManager.class,
623 new CachedServiceFetcher<StorageManager>() {
624 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600625 public StorageManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey48877892015-03-18 11:27:19 -0700626 return new StorageManager(ctx, ctx.mMainThread.getHandler().getLooper());
Jeff Brown6e539312015-02-24 18:53:21 -0800627 }});
628
Jeff Sharkeye8cece92017-01-04 11:33:33 -0700629 registerService(Context.STORAGE_STATS_SERVICE, StorageStatsManager.class,
630 new CachedServiceFetcher<StorageStatsManager>() {
631 @Override
632 public StorageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
633 IStorageStatsManager service = IStorageStatsManager.Stub.asInterface(
634 ServiceManager.getServiceOrThrow(Context.STORAGE_STATS_SERVICE));
635 return new StorageStatsManager(ctx, service);
636 }});
637
Tao Bao07342dc2017-01-24 15:08:21 -0800638 registerService(Context.SYSTEM_UPDATE_SERVICE, SystemUpdateManager.class,
639 new CachedServiceFetcher<SystemUpdateManager>() {
640 @Override
641 public SystemUpdateManager createService(ContextImpl ctx)
642 throws ServiceNotFoundException {
643 IBinder b = ServiceManager.getServiceOrThrow(
644 Context.SYSTEM_UPDATE_SERVICE);
645 ISystemUpdateManager service = ISystemUpdateManager.Stub.asInterface(b);
646 return new SystemUpdateManager(service);
647 }});
648
Hall Liub38ad5f2019-12-23 18:11:10 -0800649 registerService(Context.SYSTEM_CONFIG_SERVICE, SystemConfigManager.class,
650 new CachedServiceFetcher<SystemConfigManager>() {
651 @Override
652 public SystemConfigManager createService(ContextImpl ctx) {
653 return new SystemConfigManager();
654 }});
655
Chen Xu288b71c2019-09-15 18:28:21 -0700656 registerService(Context.TELEPHONY_REGISTRY_SERVICE, TelephonyRegistryManager.class,
657 new CachedServiceFetcher<TelephonyRegistryManager>() {
658 @Override
659 public TelephonyRegistryManager createService(ContextImpl ctx) {
Chen Xu1f6cfa52019-10-13 17:30:32 -0700660 return new TelephonyRegistryManager(ctx);
Chen Xu288b71c2019-09-15 18:28:21 -0700661 }});
662
Jeff Brown6e539312015-02-24 18:53:21 -0800663 registerService(Context.TELECOM_SERVICE, TelecomManager.class,
664 new CachedServiceFetcher<TelecomManager>() {
665 @Override
666 public TelecomManager createService(ContextImpl ctx) {
667 return new TelecomManager(ctx.getOuterContext());
668 }});
669
Sarah Chin4affb512020-01-10 16:09:11 -0800670 registerService(Context.MMS_SERVICE, MmsManager.class,
671 new CachedServiceFetcher<MmsManager>() {
672 @Override
673 public MmsManager createService(ContextImpl ctx) {
674 return new MmsManager(ctx.getOuterContext());
675 }});
676
Jeff Brown6e539312015-02-24 18:53:21 -0800677 registerService(Context.UI_MODE_SERVICE, UiModeManager.class,
678 new CachedServiceFetcher<UiModeManager>() {
679 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600680 public UiModeManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Tyler Gunndaef6682019-09-30 15:12:24 -0700681 return new UiModeManager(ctx.getOuterContext());
Jeff Brown6e539312015-02-24 18:53:21 -0800682 }});
683
684 registerService(Context.USB_SERVICE, UsbManager.class,
685 new CachedServiceFetcher<UsbManager>() {
686 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600687 public UsbManager createService(ContextImpl ctx) throws ServiceNotFoundException {
688 IBinder b = ServiceManager.getServiceOrThrow(Context.USB_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800689 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
690 }});
691
Kenny Rootf74bfde2018-01-18 15:42:48 -0800692 registerService(Context.ADB_SERVICE, AdbManager.class,
693 new CachedServiceFetcher<AdbManager>() {
694 @Override
695 public AdbManager createService(ContextImpl ctx)
696 throws ServiceNotFoundException {
697 IBinder b = ServiceManager.getServiceOrThrow(Context.ADB_SERVICE);
698 return new AdbManager(ctx, IAdbManager.Stub.asInterface(b));
699 }});
700
Jeff Brown6e539312015-02-24 18:53:21 -0800701 registerService(Context.SERIAL_SERVICE, SerialManager.class,
702 new CachedServiceFetcher<SerialManager>() {
703 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600704 public SerialManager createService(ContextImpl ctx) throws ServiceNotFoundException {
705 IBinder b = ServiceManager.getServiceOrThrow(Context.SERIAL_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800706 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
707 }});
708
709 registerService(Context.VIBRATOR_SERVICE, Vibrator.class,
710 new CachedServiceFetcher<Vibrator>() {
711 @Override
712 public Vibrator createService(ContextImpl ctx) {
713 return new SystemVibrator(ctx);
714 }});
715
716 registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
717 new CachedServiceFetcher<WallpaperManager>() {
718 @Override
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800719 public WallpaperManager createService(ContextImpl ctx)
720 throws ServiceNotFoundException {
Felipe Leme34a861a2019-08-05 16:00:12 -0700721 final IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
722 if (b == null) {
Felipe Leme8ac0d0d2020-01-14 13:37:41 -0800723 ApplicationInfo appInfo = ctx.getApplicationInfo();
724 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.P
725 && appInfo.isInstantApp()) {
Felipe Leme34a861a2019-08-05 16:00:12 -0700726 // Instant app
727 throw new ServiceNotFoundException(Context.WALLPAPER_SERVICE);
728 }
Felipe Leme8ac0d0d2020-01-14 13:37:41 -0800729 final boolean enabled = Resources.getSystem()
730 .getBoolean(com.android.internal.R.bool.config_enableWallpaperService);
731 if (!enabled) {
732 // Device doesn't support wallpaper, return a limited manager
733 return DisabledWallpaperManager.getInstance();
734 }
Felipe Leme34a861a2019-08-05 16:00:12 -0700735 // Bad state - WallpaperManager methods will throw exception
Felipe Leme8ac0d0d2020-01-14 13:37:41 -0800736 Log.e(TAG, "No wallpaper service");
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800737 }
738 IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
739 return new WallpaperManager(service, ctx.getOuterContext(),
Jeff Brown6e539312015-02-24 18:53:21 -0800740 ctx.mMainThread.getHandler());
741 }});
742
Robert Quattlebaum87a71042017-05-15 15:45:20 -0700743 registerService(Context.LOWPAN_SERVICE, LowpanManager.class,
744 new CachedServiceFetcher<LowpanManager>() {
745 @Override
746 public LowpanManager createService(ContextImpl ctx) throws ServiceNotFoundException {
747 IBinder b = ServiceManager.getServiceOrThrow(Context.LOWPAN_SERVICE);
748 ILowpanManager service = ILowpanManager.Stub.asInterface(b);
749 return new LowpanManager(ctx.getOuterContext(), service,
750 ConnectivityThread.getInstanceLooper());
751 }});
752
Jeff Brown6e539312015-02-24 18:53:21 -0800753 registerService(Context.ETHERNET_SERVICE, EthernetManager.class,
754 new CachedServiceFetcher<EthernetManager>() {
755 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600756 public EthernetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
757 IBinder b = ServiceManager.getServiceOrThrow(Context.ETHERNET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800758 IEthernetManager service = IEthernetManager.Stub.asInterface(b);
759 return new EthernetManager(ctx.getOuterContext(), service);
760 }});
761
Etan Cohen73450f22019-12-12 23:29:25 -0800762 registerService(Context.WIFI_COND_SERVICE, WifiCondManager.class,
763 new CachedServiceFetcher<WifiCondManager>() {
764 @Override
765 public WifiCondManager createService(ContextImpl ctx) {
766 return new WifiCondManager(ctx.getOuterContext());
767 }
768 });
769
Jeff Brown6e539312015-02-24 18:53:21 -0800770 registerService(Context.WINDOW_SERVICE, WindowManager.class,
771 new CachedServiceFetcher<WindowManager>() {
772 @Override
773 public WindowManager createService(ContextImpl ctx) {
Adam Lesinski4ece3d62016-06-16 18:05:41 -0700774 return new WindowManagerImpl(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800775 }});
776
777 registerService(Context.USER_SERVICE, UserManager.class,
778 new CachedServiceFetcher<UserManager>() {
779 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600780 public UserManager createService(ContextImpl ctx) throws ServiceNotFoundException {
781 IBinder b = ServiceManager.getServiceOrThrow(Context.USER_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800782 IUserManager service = IUserManager.Stub.asInterface(b);
783 return new UserManager(ctx, service);
784 }});
785
786 registerService(Context.APP_OPS_SERVICE, AppOpsManager.class,
787 new CachedServiceFetcher<AppOpsManager>() {
788 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600789 public AppOpsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
790 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_OPS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800791 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
792 return new AppOpsManager(ctx, service);
793 }});
794
795 registerService(Context.CAMERA_SERVICE, CameraManager.class,
796 new CachedServiceFetcher<CameraManager>() {
797 @Override
798 public CameraManager createService(ContextImpl ctx) {
799 return new CameraManager(ctx);
800 }});
801
802 registerService(Context.LAUNCHER_APPS_SERVICE, LauncherApps.class,
803 new CachedServiceFetcher<LauncherApps>() {
804 @Override
805 public LauncherApps createService(ContextImpl ctx) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700806 return new LauncherApps(ctx);
Jeff Brown6e539312015-02-24 18:53:21 -0800807 }});
808
809 registerService(Context.RESTRICTIONS_SERVICE, RestrictionsManager.class,
810 new CachedServiceFetcher<RestrictionsManager>() {
811 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600812 public RestrictionsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
813 IBinder b = ServiceManager.getServiceOrThrow(Context.RESTRICTIONS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800814 IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b);
815 return new RestrictionsManager(ctx, service);
816 }});
817
818 registerService(Context.PRINT_SERVICE, PrintManager.class,
819 new CachedServiceFetcher<PrintManager>() {
820 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600821 public PrintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganovec7daa52017-06-30 12:02:59 -0700822 IPrintManager service = null;
823 // If the feature not present, don't try to look up every time
824 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
825 service = IPrintManager.Stub.asInterface(ServiceManager
826 .getServiceOrThrow(Context.PRINT_SERVICE));
827 }
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700828 final int userId = ctx.getUserId();
829 final int appId = UserHandle.getAppId(ctx.getApplicationInfo().uid);
830 return new PrintManager(ctx.getOuterContext(), service, userId, appId);
Jeff Brown6e539312015-02-24 18:53:21 -0800831 }});
832
Eugene Susla6ed45d82017-01-22 13:52:51 -0800833 registerService(Context.COMPANION_DEVICE_SERVICE, CompanionDeviceManager.class,
834 new CachedServiceFetcher<CompanionDeviceManager>() {
Svet Ganovec7daa52017-06-30 12:02:59 -0700835 @Override
836 public CompanionDeviceManager createService(ContextImpl ctx)
837 throws ServiceNotFoundException {
838 ICompanionDeviceManager service = null;
839 // If the feature not present, don't try to look up every time
840 if (ctx.getPackageManager().hasSystemFeature(
841 PackageManager.FEATURE_COMPANION_DEVICE_SETUP)) {
842 service = ICompanionDeviceManager.Stub.asInterface(
843 ServiceManager.getServiceOrThrow(Context.COMPANION_DEVICE_SERVICE));
844 }
845 return new CompanionDeviceManager(service, ctx.getOuterContext());
846 }});
Eugene Susla6ed45d82017-01-22 13:52:51 -0800847
Jeff Brown6e539312015-02-24 18:53:21 -0800848 registerService(Context.CONSUMER_IR_SERVICE, ConsumerIrManager.class,
849 new CachedServiceFetcher<ConsumerIrManager>() {
850 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600851 public ConsumerIrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800852 return new ConsumerIrManager(ctx);
853 }});
854
855 registerService(Context.MEDIA_SESSION_SERVICE, MediaSessionManager.class,
856 new CachedServiceFetcher<MediaSessionManager>() {
857 @Override
858 public MediaSessionManager createService(ContextImpl ctx) {
859 return new MediaSessionManager(ctx);
860 }});
861
862 registerService(Context.TRUST_SERVICE, TrustManager.class,
863 new StaticServiceFetcher<TrustManager>() {
864 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600865 public TrustManager createService() throws ServiceNotFoundException {
866 IBinder b = ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800867 return new TrustManager(b);
868 }});
869
870 registerService(Context.FINGERPRINT_SERVICE, FingerprintManager.class,
871 new CachedServiceFetcher<FingerprintManager>() {
872 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600873 public FingerprintManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey351c2c22017-03-18 16:48:02 -0600874 final IBinder binder;
875 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
876 binder = ServiceManager.getServiceOrThrow(Context.FINGERPRINT_SERVICE);
877 } else {
878 binder = ServiceManager.getService(Context.FINGERPRINT_SERVICE);
879 }
Jeff Brown6e539312015-02-24 18:53:21 -0800880 IFingerprintService service = IFingerprintService.Stub.asInterface(binder);
881 return new FingerprintManager(ctx.getOuterContext(), service);
882 }});
883
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200884 registerService(Context.FACE_SERVICE, FaceManager.class,
885 new CachedServiceFetcher<FaceManager>() {
886 @Override
887 public FaceManager createService(ContextImpl ctx)
888 throws ServiceNotFoundException {
889 final IBinder binder;
890 if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
891 binder = ServiceManager.getServiceOrThrow(Context.FACE_SERVICE);
892 } else {
893 binder = ServiceManager.getService(Context.FACE_SERVICE);
894 }
895 IFaceService service = IFaceService.Stub.asInterface(binder);
896 return new FaceManager(ctx.getOuterContext(), service);
897 }
898 });
899
Kevin Chyn51676d22018-11-05 18:00:43 -0800900 registerService(Context.IRIS_SERVICE, IrisManager.class,
901 new CachedServiceFetcher<IrisManager>() {
902 @Override
903 public IrisManager createService(ContextImpl ctx)
904 throws ServiceNotFoundException {
905 final IBinder binder =
906 ServiceManager.getServiceOrThrow(Context.IRIS_SERVICE);
907 IIrisService service = IIrisService.Stub.asInterface(binder);
908 return new IrisManager(ctx.getOuterContext(), service);
909 }
910 });
911
Kevin Chyn05c21502018-09-18 13:07:19 -0700912 registerService(Context.BIOMETRIC_SERVICE, BiometricManager.class,
913 new CachedServiceFetcher<BiometricManager>() {
914 @Override
915 public BiometricManager createService(ContextImpl ctx)
916 throws ServiceNotFoundException {
Kevin Chyne739daf2018-11-06 17:11:06 -0800917 if (BiometricManager.hasBiometrics(ctx)) {
918 final IBinder binder =
Ilya Matyukhine4675b32019-11-07 16:07:19 -0800919 ServiceManager.getServiceOrThrow(Context.AUTH_SERVICE);
920 final IAuthService service =
921 IAuthService.Stub.asInterface(binder);
Kevin Chyne739daf2018-11-06 17:11:06 -0800922 return new BiometricManager(ctx.getOuterContext(), service);
923 } else {
924 // Allow access to the manager when service is null. This saves memory
925 // on devices without biometric hardware.
926 return new BiometricManager(ctx.getOuterContext(), null);
927 }
Kevin Chyn05c21502018-09-18 13:07:19 -0700928 }
929 });
930
Jeff Brown6e539312015-02-24 18:53:21 -0800931 registerService(Context.TV_INPUT_SERVICE, TvInputManager.class,
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700932 new CachedServiceFetcher<TvInputManager>() {
Jeff Brown6e539312015-02-24 18:53:21 -0800933 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700934 public TvInputManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600935 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.TV_INPUT_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800936 ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder);
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700937 return new TvInputManager(service, ctx.getUserId());
Jeff Brown6e539312015-02-24 18:53:21 -0800938 }});
939
940 registerService(Context.NETWORK_SCORE_SERVICE, NetworkScoreManager.class,
941 new CachedServiceFetcher<NetworkScoreManager>() {
942 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600943 public NetworkScoreManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Jeff Brown6e539312015-02-24 18:53:21 -0800944 return new NetworkScoreManager(ctx);
945 }});
946
947 registerService(Context.USAGE_STATS_SERVICE, UsageStatsManager.class,
948 new CachedServiceFetcher<UsageStatsManager>() {
949 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600950 public UsageStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
951 IBinder iBinder = ServiceManager.getServiceOrThrow(Context.USAGE_STATS_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800952 IUsageStatsManager service = IUsageStatsManager.Stub.asInterface(iBinder);
953 return new UsageStatsManager(ctx.getOuterContext(), service);
954 }});
955
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000956 registerService(Context.NETWORK_STATS_SERVICE, NetworkStatsManager.class,
957 new CachedServiceFetcher<NetworkStatsManager>() {
958 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600959 public NetworkStatsManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Zoltan Szatmary-Ban9c5dfa52015-02-23 17:20:20 +0000960 return new NetworkStatsManager(ctx.getOuterContext());
961 }});
962
Jeff Brown6e539312015-02-24 18:53:21 -0800963 registerService(Context.PERSISTENT_DATA_BLOCK_SERVICE, PersistentDataBlockManager.class,
964 new StaticServiceFetcher<PersistentDataBlockManager>() {
965 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600966 public PersistentDataBlockManager createService() throws ServiceNotFoundException {
967 IBinder b = ServiceManager.getServiceOrThrow(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -0800968 IPersistentDataBlockService persistentDataBlockService =
969 IPersistentDataBlockService.Stub.asInterface(b);
970 if (persistentDataBlockService != null) {
971 return new PersistentDataBlockManager(persistentDataBlockService);
972 } else {
973 // not supported
974 return null;
975 }
976 }});
977
Andrew Scull3b8b46f2017-02-13 18:12:15 +0000978 registerService(Context.OEM_LOCK_SERVICE, OemLockManager.class,
979 new StaticServiceFetcher<OemLockManager>() {
980 @Override
981 public OemLockManager createService() throws ServiceNotFoundException {
982 IBinder b = ServiceManager.getServiceOrThrow(Context.OEM_LOCK_SERVICE);
983 IOemLockService oemLockService = IOemLockService.Stub.asInterface(b);
984 if (oemLockService != null) {
985 return new OemLockManager(oemLockService);
986 } else {
987 // not supported
988 return null;
989 }
990 }});
991
Jeff Brown6e539312015-02-24 18:53:21 -0800992 registerService(Context.MEDIA_PROJECTION_SERVICE, MediaProjectionManager.class,
993 new CachedServiceFetcher<MediaProjectionManager>() {
994 @Override
995 public MediaProjectionManager createService(ContextImpl ctx) {
996 return new MediaProjectionManager(ctx);
997 }});
998
999 registerService(Context.APPWIDGET_SERVICE, AppWidgetManager.class,
1000 new CachedServiceFetcher<AppWidgetManager>() {
1001 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001002 public AppWidgetManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1003 IBinder b = ServiceManager.getServiceOrThrow(Context.APPWIDGET_SERVICE);
Jeff Brown6e539312015-02-24 18:53:21 -08001004 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
1005 }});
1006
1007 registerService(Context.MIDI_SERVICE, MidiManager.class,
1008 new CachedServiceFetcher<MidiManager>() {
1009 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001010 public MidiManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1011 IBinder b = ServiceManager.getServiceOrThrow(Context.MIDI_SERVICE);
Mike Lockwoode0a6ca62015-06-04 13:43:56 -07001012 return new MidiManager(IMidiManager.Stub.asInterface(b));
Jeff Brown6e539312015-02-24 18:53:21 -08001013 }});
Eric Laurent2035ac82015-03-05 15:18:44 -08001014
1015 registerService(Context.RADIO_SERVICE, RadioManager.class,
1016 new CachedServiceFetcher<RadioManager>() {
1017 @Override
Tomasz Wasilczyk347192e2017-04-04 11:13:44 -07001018 public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Eric Laurent2035ac82015-03-05 15:18:44 -08001019 return new RadioManager(ctx);
1020 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001021
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001022 registerService(Context.HARDWARE_PROPERTIES_SERVICE, HardwarePropertiesManager.class,
1023 new CachedServiceFetcher<HardwarePropertiesManager>() {
1024 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001025 public HardwarePropertiesManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1026 IBinder b = ServiceManager.getServiceOrThrow(Context.HARDWARE_PROPERTIES_SERVICE);
1027 IHardwarePropertiesManager service =
1028 IHardwarePropertiesManager.Stub.asInterface(b);
1029 return new HardwarePropertiesManager(ctx, service);
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001030 }});
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001031
1032 registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
1033 new CachedServiceFetcher<SoundTriggerManager>() {
1034 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001035 public SoundTriggerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1036 IBinder b = ServiceManager.getServiceOrThrow(Context.SOUND_TRIGGER_SERVICE);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001037 return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
1038 }});
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001039
1040 registerService(Context.SHORTCUT_SERVICE, ShortcutManager.class,
1041 new CachedServiceFetcher<ShortcutManager>() {
Makoto Onuki55046222016-03-08 10:49:47 -08001042 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001043 public ShortcutManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1044 IBinder b = ServiceManager.getServiceOrThrow(Context.SHORTCUT_SERVICE);
1045 return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
Makoto Onuki55046222016-03-08 10:49:47 -08001046 }});
Joe Onorato713fec82016-03-04 10:34:02 -08001047
Hyunyoung Song880a9512018-12-20 11:24:48 -08001048 registerService(Context.OVERLAY_SERVICE, OverlayManager.class,
1049 new CachedServiceFetcher<OverlayManager>() {
1050 @Override
1051 public OverlayManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1052 IBinder b = ServiceManager.getServiceOrThrow(Context.OVERLAY_SERVICE);
1053 return new OverlayManager(ctx, IOverlayManager.Stub.asInterface(b));
1054 }});
1055
Ricky Wai1a6e6672017-10-27 14:46:01 +01001056 registerService(Context.NETWORK_WATCHLIST_SERVICE, NetworkWatchlistManager.class,
1057 new CachedServiceFetcher<NetworkWatchlistManager>() {
1058 @Override
1059 public NetworkWatchlistManager createService(ContextImpl ctx)
1060 throws ServiceNotFoundException {
1061 IBinder b =
1062 ServiceManager.getServiceOrThrow(Context.NETWORK_WATCHLIST_SERVICE);
1063 return new NetworkWatchlistManager(ctx,
1064 INetworkWatchlistManager.Stub.asInterface(b));
1065 }});
1066
Joe Onorato713fec82016-03-04 10:34:02 -08001067 registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
1068 new CachedServiceFetcher<SystemHealthManager>() {
1069 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001070 public SystemHealthManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1071 IBinder b = ServiceManager.getServiceOrThrow(BatteryStats.SERVICE_NAME);
1072 return new SystemHealthManager(IBatteryStats.Stub.asInterface(b));
Joe Onorato713fec82016-03-04 10:34:02 -08001073 }});
Peng Xu9ff7d222016-02-11 13:02:05 -08001074
1075 registerService(Context.CONTEXTHUB_SERVICE, ContextHubManager.class,
1076 new CachedServiceFetcher<ContextHubManager>() {
1077 @Override
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001078 public ContextHubManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Peng Xu9ff7d222016-02-11 13:02:05 -08001079 return new ContextHubManager(ctx.getOuterContext(),
1080 ctx.mMainThread.getHandler().getLooper());
1081 }});
Joe Onorato1754d742016-11-21 17:51:35 -08001082
1083 registerService(Context.INCIDENT_SERVICE, IncidentManager.class,
1084 new CachedServiceFetcher<IncidentManager>() {
1085 @Override
1086 public IncidentManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1087 return new IncidentManager(ctx);
1088 }});
Clara Bayarri04d72ab2017-01-10 09:31:51 -08001089
Nandana Duttd11850c2018-12-12 17:26:57 +00001090 registerService(Context.BUGREPORT_SERVICE, BugreportManager.class,
1091 new CachedServiceFetcher<BugreportManager>() {
1092 @Override
1093 public BugreportManager createService(ContextImpl ctx)
1094 throws ServiceNotFoundException {
1095 IBinder b = ServiceManager.getServiceOrThrow(Context.BUGREPORT_SERVICE);
1096 return new BugreportManager(ctx.getOuterContext(),
1097 IDumpstate.Stub.asInterface(b));
1098 }});
1099
Felipe Leme640f30a2017-03-06 15:44:06 -08001100 registerService(Context.AUTOFILL_MANAGER_SERVICE, AutofillManager.class,
1101 new CachedServiceFetcher<AutofillManager>() {
Felipe Leme3461d3c2017-01-19 08:54:55 -08001102 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -08001103 public AutofillManager createService(ContextImpl ctx) throws ServiceNotFoundException {
Svet Ganov43574b02017-04-12 09:25:20 -07001104 // Get the services without throwing as this is an optional feature
1105 IBinder b = ServiceManager.getService(Context.AUTOFILL_MANAGER_SERVICE);
Svet Ganov782043c2017-02-11 00:52:02 +00001106 IAutoFillManager service = IAutoFillManager.Stub.asInterface(b);
Felipe Leme640f30a2017-03-06 15:44:06 -08001107 return new AutofillManager(ctx.getOuterContext(), service);
Felipe Leme3461d3c2017-01-19 08:54:55 -08001108 }});
Zak Cohen56345f42017-01-26 13:54:28 -08001109
Felipe Lemeecb08be2018-11-27 15:48:47 -08001110 registerService(Context.CONTENT_CAPTURE_MANAGER_SERVICE, ContentCaptureManager.class,
1111 new CachedServiceFetcher<ContentCaptureManager>() {
Felipe Lemee348dc32018-11-05 12:35:29 -08001112 @Override
Felipe Lemeecb08be2018-11-27 15:48:47 -08001113 public ContentCaptureManager createService(ContextImpl ctx)
Felipe Lemee348dc32018-11-05 12:35:29 -08001114 throws ServiceNotFoundException {
1115 // Get the services without throwing as this is an optional feature
Felipe Lemeecb08be2018-11-27 15:48:47 -08001116 Context outerContext = ctx.getOuterContext();
Felipe Leme326f15a2019-02-19 09:42:24 -08001117 ContentCaptureOptions options = outerContext.getContentCaptureOptions();
1118 // Options is null when the service didn't whitelist the activity or package
Felipe Lemecbf7f262019-04-17 13:57:59 -07001119 if (options != null && (options.lite || options.isWhitelisted(outerContext))) {
Felipe Lemeecb08be2018-11-27 15:48:47 -08001120 IBinder b = ServiceManager
1121 .getService(Context.CONTENT_CAPTURE_MANAGER_SERVICE);
Felipe Leme749b8892018-12-03 16:30:30 -08001122 IContentCaptureManager service = IContentCaptureManager.Stub.asInterface(b);
Felipe Leme326f15a2019-02-19 09:42:24 -08001123 // Service is null when not provided by OEM or disabled by kill-switch.
Felipe Lemed49d52c2019-02-15 09:48:20 -08001124 if (service != null) {
Felipe Leme326f15a2019-02-19 09:42:24 -08001125 return new ContentCaptureManager(outerContext, service, options);
Felipe Lemed49d52c2019-02-15 09:48:20 -08001126 }
Felipe Lemeecb08be2018-11-27 15:48:47 -08001127 }
Felipe Leme326f15a2019-02-19 09:42:24 -08001128 // When feature is disabled or app / package not whitelisted, we return a null
1129 // manager to apps so the performance impact is practically zero
Felipe Lemeecb08be2018-11-27 15:48:47 -08001130 return null;
Felipe Lemee348dc32018-11-05 12:35:29 -08001131 }});
1132
Sunny Goyal54e91342018-11-14 11:59:02 -08001133 registerService(Context.APP_PREDICTION_SERVICE, AppPredictionManager.class,
1134 new CachedServiceFetcher<AppPredictionManager>() {
1135 @Override
1136 public AppPredictionManager createService(ContextImpl ctx)
1137 throws ServiceNotFoundException {
Felipe Leme6378bd42019-08-14 18:14:55 -07001138 IBinder b = ServiceManager.getService(Context.APP_PREDICTION_SERVICE);
1139 return b == null ? null : new AppPredictionManager(ctx);
Sunny Goyal54e91342018-11-14 11:59:02 -08001140 }
1141 });
1142
Winson Chung3fb0f252019-01-08 17:41:55 -08001143 registerService(Context.CONTENT_SUGGESTIONS_SERVICE,
1144 ContentSuggestionsManager.class,
1145 new CachedServiceFetcher<ContentSuggestionsManager>() {
1146 @Override
1147 public ContentSuggestionsManager createService(ContextImpl ctx) {
1148 // No throw as this is an optional service
1149 IBinder b = ServiceManager.getService(
1150 Context.CONTENT_SUGGESTIONS_SERVICE);
1151 IContentSuggestionsManager service =
1152 IContentSuggestionsManager.Stub.asInterface(b);
Zak Cohen4834e9f2019-03-08 12:59:01 -08001153 return new ContentSuggestionsManager(ctx.getUserId(), service);
Winson Chung3fb0f252019-01-08 17:41:55 -08001154 }
1155 });
1156
Zak Cohen56345f42017-01-26 13:54:28 -08001157 registerService(Context.VR_SERVICE, VrManager.class, new CachedServiceFetcher<VrManager>() {
1158 @Override
1159 public VrManager createService(ContextImpl ctx) throws ServiceNotFoundException {
1160 IBinder b = ServiceManager.getServiceOrThrow(Context.VR_SERVICE);
1161 return new VrManager(IVrManager.Stub.asInterface(b));
1162 }
1163 });
Neil Fuller328532a2017-03-16 18:32:21 +00001164
1165 registerService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, RulesManager.class,
1166 new CachedServiceFetcher<RulesManager>() {
1167 @Override
1168 public RulesManager createService(ContextImpl ctx) {
1169 return new RulesManager(ctx.getOuterContext());
1170 }});
Tony Mak1b708e62017-10-12 10:59:11 +01001171
1172 registerService(Context.CROSS_PROFILE_APPS_SERVICE, CrossProfileApps.class,
1173 new CachedServiceFetcher<CrossProfileApps>() {
1174 @Override
1175 public CrossProfileApps createService(ContextImpl ctx)
1176 throws ServiceNotFoundException {
1177 IBinder b = ServiceManager.getServiceOrThrow(
1178 Context.CROSS_PROFILE_APPS_SERVICE);
1179 return new CrossProfileApps(ctx.getOuterContext(),
1180 ICrossProfileApps.Stub.asInterface(b));
1181 }
1182 });
Jason Monk8f5f7ff2017-10-17 14:12:42 -04001183
1184 registerService(Context.SLICE_SERVICE, SliceManager.class,
1185 new CachedServiceFetcher<SliceManager>() {
1186 @Override
1187 public SliceManager createService(ContextImpl ctx)
1188 throws ServiceNotFoundException {
1189 return new SliceManager(ctx.getOuterContext(),
1190 ctx.mMainThread.getHandler());
1191 }
1192 });
Philip P. Moltmannf80809f2018-04-04 11:20:44 -07001193
Neil Fullerfeeee682018-05-30 14:35:24 +01001194 registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class,
1195 new CachedServiceFetcher<TimeDetector>() {
1196 @Override
1197 public TimeDetector createService(ContextImpl ctx)
1198 throws ServiceNotFoundException {
1199 return new TimeDetector();
1200 }});
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001201
Neil Fuller3e3b5402019-11-07 15:35:05 +00001202 registerService(Context.TIME_ZONE_DETECTOR_SERVICE, TimeZoneDetector.class,
1203 new CachedServiceFetcher<TimeZoneDetector>() {
1204 @Override
1205 public TimeZoneDetector createService(ContextImpl ctx)
1206 throws ServiceNotFoundException {
1207 return new TimeZoneDetector();
1208 }});
1209
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001210 registerService(Context.PERMISSION_SERVICE, PermissionManager.class,
1211 new CachedServiceFetcher<PermissionManager>() {
1212 @Override
Peter Wang56dec3f2019-10-25 11:24:33 -07001213 public PermissionManager createService(ContextImpl ctx)
1214 throws ServiceNotFoundException {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07001215 IPackageManager packageManager = AppGlobals.getPackageManager();
1216 return new PermissionManager(ctx.getOuterContext(), packageManager);
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001217 }});
Hai Zhang4ef21d02018-11-09 14:43:51 -08001218
Philip P. Moltmannbc054d82018-12-21 09:41:58 -08001219 registerService(Context.PERMISSION_CONTROLLER_SERVICE, PermissionControllerManager.class,
1220 new CachedServiceFetcher<PermissionControllerManager>() {
1221 @Override
1222 public PermissionControllerManager createService(ContextImpl ctx) {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07001223 return new PermissionControllerManager(ctx.getOuterContext(),
1224 ctx.getMainThreadHandler());
Philip P. Moltmannbc054d82018-12-21 09:41:58 -08001225 }});
1226
Hai Zhang4ef21d02018-11-09 14:43:51 -08001227 registerService(Context.ROLE_SERVICE, RoleManager.class,
1228 new CachedServiceFetcher<RoleManager>() {
1229 @Override
1230 public RoleManager createService(ContextImpl ctx)
1231 throws ServiceNotFoundException {
1232 return new RoleManager(ctx.getOuterContext());
1233 }});
Richard Uhlerb29f1452018-09-12 16:38:15 +01001234
Hai Zhanga4959e52019-03-06 12:21:07 -08001235 registerService(Context.ROLE_CONTROLLER_SERVICE, RoleControllerManager.class,
1236 new CachedServiceFetcher<RoleControllerManager>() {
1237 @Override
1238 public RoleControllerManager createService(ContextImpl ctx)
1239 throws ServiceNotFoundException {
1240 return new RoleControllerManager(ctx.getOuterContext());
1241 }});
1242
Richard Uhlerb29f1452018-09-12 16:38:15 +01001243 registerService(Context.ROLLBACK_SERVICE, RollbackManager.class,
1244 new CachedServiceFetcher<RollbackManager>() {
1245 @Override
1246 public RollbackManager createService(ContextImpl ctx)
1247 throws ServiceNotFoundException {
1248 IBinder b = ServiceManager.getServiceOrThrow(Context.ROLLBACK_SERVICE);
1249 return new RollbackManager(ctx.getOuterContext(),
1250 IRollbackManager.Stub.asInterface(b));
1251 }});
Howard Chen0a947642019-01-07 14:10:44 +08001252
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001253 registerService(Context.DYNAMIC_SYSTEM_SERVICE, DynamicSystemManager.class,
1254 new CachedServiceFetcher<DynamicSystemManager>() {
Howard Chen0a947642019-01-07 14:10:44 +08001255 @Override
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001256 public DynamicSystemManager createService(ContextImpl ctx)
Howard Chen0a947642019-01-07 14:10:44 +08001257 throws ServiceNotFoundException {
1258 IBinder b = ServiceManager.getServiceOrThrow(
Po-Chien Hsueh4e908c22019-03-07 11:57:17 +08001259 Context.DYNAMIC_SYSTEM_SERVICE);
1260 return new DynamicSystemManager(
1261 IDynamicSystemService.Stub.asInterface(b));
Howard Chen0a947642019-01-07 14:10:44 +08001262 }});
Victor Hsieh20fe1f62019-09-30 13:36:21 -07001263
Roshan Pius848513e2019-10-11 13:44:00 -07001264 registerService(Context.BATTERY_STATS_SERVICE, BatteryStatsManager.class,
1265 new CachedServiceFetcher<BatteryStatsManager>() {
1266 @Override
1267 public BatteryStatsManager createService(ContextImpl ctx)
1268 throws ServiceNotFoundException {
1269 IBinder b = ServiceManager.getServiceOrThrow(
1270 Context.BATTERY_STATS_SERVICE);
1271 return new BatteryStatsManager(
1272 IBatteryStats.Stub.asInterface(b));
1273 }});
Songchun Fan6cde9d92019-12-10 10:23:54 -08001274 registerService(Context.DATA_LOADER_MANAGER_SERVICE, DataLoaderManager.class,
1275 new CachedServiceFetcher<DataLoaderManager>() {
1276 @Override
1277 public DataLoaderManager createService(ContextImpl ctx)
1278 throws ServiceNotFoundException {
1279 IBinder b = ServiceManager.getServiceOrThrow(
1280 Context.DATA_LOADER_MANAGER_SERVICE);
1281 return new DataLoaderManager(IDataLoaderManager.Stub.asInterface(b));
1282 }});
Ivailo Karamanolev090d02c2020-01-09 17:02:49 +01001283 registerService(Context.LIGHTS_SERVICE, LightsManager.class,
1284 new CachedServiceFetcher<LightsManager>() {
1285 @Override
1286 public LightsManager createService(ContextImpl ctx)
1287 throws ServiceNotFoundException {
1288 return new LightsManager(ctx);
1289 }});
Songchun Fan43f18ea2019-12-11 17:54:38 -08001290 //TODO(b/136132412): refactor this: 1) merge IIncrementalManager.aidl and
1291 //IIncrementalManagerNative.aidl, 2) implement the binder interface in
1292 //IncrementalManagerService.java, 3) use JNI to call native functions
1293 registerService(Context.INCREMENTAL_SERVICE, IncrementalManager.class,
1294 new CachedServiceFetcher<IncrementalManager>() {
1295 @Override
1296 public IncrementalManager createService(ContextImpl ctx) {
1297 IBinder b = ServiceManager.getService(Context.INCREMENTAL_SERVICE);
1298 if (b == null) {
1299 return null;
1300 }
1301 return new IncrementalManager(
1302 IIncrementalManagerNative.Stub.asInterface(b));
1303 }});
Victor Hsieh20fe1f62019-09-30 13:36:21 -07001304
1305 registerService(Context.FILE_INTEGRITY_SERVICE, FileIntegrityManager.class,
1306 new CachedServiceFetcher<FileIntegrityManager>() {
1307 @Override
1308 public FileIntegrityManager createService(ContextImpl ctx)
1309 throws ServiceNotFoundException {
1310 IBinder b = ServiceManager.getServiceOrThrow(
1311 Context.FILE_INTEGRITY_SERVICE);
1312 return new FileIntegrityManager(
1313 IFileIntegrityService.Stub.asInterface(b));
1314 }});
Felipe Lemeecb08be2018-11-27 15:48:47 -08001315 //CHECKSTYLE:ON IndentationCheck
Song Pan6e3677c2019-10-29 14:19:26 +00001316 registerService(Context.APP_INTEGRITY_SERVICE, AppIntegrityManager.class,
1317 new CachedServiceFetcher<AppIntegrityManager>() {
1318 @Override
1319 public AppIntegrityManager createService(ContextImpl ctx)
1320 throws ServiceNotFoundException {
1321 IBinder b = ServiceManager.getServiceOrThrow(Context.APP_INTEGRITY_SERVICE);
1322 return new AppIntegrityManager(IAppIntegrityManager.Stub.asInterface(b));
1323 }});
Makoto Onuki54c4e032019-10-30 12:05:20 -07001324
Makoto Onuki05b14f52019-11-05 12:36:07 -08001325 sInitializing = true;
1326 try {
1327 // Note: the following functions need to be @SystemApis, once they become mainline
1328 // modules.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001329 JobSchedulerFrameworkInitializer.registerServiceWrappers();
1330 BlobStoreManagerFrameworkInitializer.initialize();
Jayachandran C58fd3eb2019-11-20 19:01:56 -08001331 TelephonyFrameworkInitializer.registerServiceWrappers();
Terry Wangecc0d1b2019-10-17 17:05:18 -07001332 AppSearchManagerFrameworkInitializer.initialize();
David Su409f3712019-10-24 11:18:41 -07001333 WifiFrameworkInitializer.registerServiceWrappers();
Jeffrey Huangf58800b2020-01-23 13:22:10 -08001334 StatsFrameworkInitializer.registerServiceWrappers();
Makoto Onuki05b14f52019-11-05 12:36:07 -08001335 } finally {
1336 // If any of the above code throws, we're in a pretty bad shape and the process
1337 // will likely crash, but we'll reset it just in case there's an exception handler...
1338 sInitializing = false;
1339 }
Jeff Brown6e539312015-02-24 18:53:21 -08001340 }
1341
Makoto Onuki05b14f52019-11-05 12:36:07 -08001342 /** Throws {@link IllegalStateException} if not during a static initialization. */
1343 private static void ensureInitializing(String methodName) {
1344 Preconditions.checkState(sInitializing, "Internal error: " + methodName
1345 + " can only be called during class initialization.");
1346 }
Jeff Brown6e539312015-02-24 18:53:21 -08001347 /**
1348 * Creates an array which is used to cache per-Context service instances.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001349 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001350 */
1351 public static Object[] createServiceCache() {
1352 return new Object[sServiceCacheSize];
1353 }
1354
1355 /**
1356 * Gets a system service from a given context.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001357 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001358 */
1359 public static Object getSystemService(ContextImpl ctx, String name) {
1360 ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
1361 return fetcher != null ? fetcher.getService(ctx) : null;
1362 }
1363
1364 /**
Paul McLeane3383cc2015-05-08 11:41:20 -07001365 * Gets the name of the system-level service that is represented by the specified class.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001366 * @hide
Jeff Brown6e539312015-02-24 18:53:21 -08001367 */
1368 public static String getSystemServiceName(Class<?> serviceClass) {
1369 return SYSTEM_SERVICE_NAMES.get(serviceClass);
1370 }
1371
1372 /**
1373 * Statically registers a system service with the context.
1374 * This method must be called during static initialization only.
1375 */
Makoto Onuki05b14f52019-11-05 12:36:07 -08001376 private static <T> void registerService(@NonNull String serviceName,
1377 @NonNull Class<T> serviceClass, @NonNull ServiceFetcher<T> serviceFetcher) {
Jeff Brown6e539312015-02-24 18:53:21 -08001378 SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
1379 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
1380 }
1381
1382 /**
Makoto Onuki05b14f52019-11-05 12:36:07 -08001383 * Callback interface used as a parameter to {@link #registerStaticService(
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001384 * String, Class, StaticServiceProducerWithoutBinder)}, which generates a service wrapper
1385 * instance that's not tied to any context and does not take a service binder object in the
1386 * constructor.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001387 *
1388 * @param <TServiceClass> type of the service wrapper class.
Makoto Onuki792de8d2019-07-16 16:19:01 -07001389 *
1390 * @hide
1391 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001392 @SystemApi
1393 public interface StaticServiceProducerWithoutBinder<TServiceClass> {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001394 /**
1395 * Return a new service wrapper of type {@code TServiceClass}.
1396 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001397 @NonNull
Makoto Onuki05b14f52019-11-05 12:36:07 -08001398 TServiceClass createService();
1399 }
1400
1401 /**
1402 * Callback interface used as a parameter to {@link #registerStaticService(
1403 * String, Class, StaticServiceProducerWithBinder)}, which generates a service wrapper instance
1404 * that's not tied to any context and takes a service binder object in the constructor.
1405 *
1406 * @param <TServiceClass> type of the service wrapper class.
1407 *
1408 * @hide
1409 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001410 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001411 public interface StaticServiceProducerWithBinder<TServiceClass> {
1412 /**
1413 * Return a new service wrapper of type {@code TServiceClass} backed by a given
1414 * service binder object.
1415 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001416 @NonNull
1417 TServiceClass createService(@NonNull IBinder serviceBinder);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001418 }
1419
1420 /**
1421 * Callback interface used as a parameter to {@link #registerContextAwareService(
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001422 * String, Class, ContextAwareServiceProducerWithoutBinder)},
Makoto Onuki05b14f52019-11-05 12:36:07 -08001423 * which generates a service wrapper instance
1424 * that's tied to a specific context and does not take a service binder object in the
1425 * constructor.
1426 *
1427 * @param <TServiceClass> type of the service wrapper class.
1428 *
1429 * @hide
1430 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001431 @SystemApi
1432 public interface ContextAwareServiceProducerWithoutBinder<TServiceClass> {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001433 /**
1434 * Return a new service wrapper of type {@code TServiceClass} tied to a given
1435 * {@code context}.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001436 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001437 @NonNull
1438 //TODO Do we need to pass the "base context" too?
1439 TServiceClass createService(@NonNull Context context);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001440 }
1441
1442 /**
1443 * Callback interface used as a parameter to {@link #registerContextAwareService(
1444 * String, Class, ContextAwareServiceProducerWithBinder)},
1445 * which generates a service wrapper instance
1446 * that's tied to a specific context and takes a service binder object in the constructor.
1447 *
1448 * @param <TServiceClass> type of the service wrapper class.
1449 *
1450 * @hide
1451 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001452 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001453 public interface ContextAwareServiceProducerWithBinder<TServiceClass> {
1454 /**
1455 * Return a new service wrapper of type {@code TServiceClass} backed by a given
1456 * service binder object that's tied to a given {@code context}.
Makoto Onuki05b14f52019-11-05 12:36:07 -08001457 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001458 @NonNull
1459 //TODO Do we need to pass the "base context" too?
1460 TServiceClass createService(@NonNull Context context, @NonNull IBinder serviceBinder);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001461 }
1462
1463 /**
1464 * Used by apex modules to register a "service wrapper" that is not tied to any {@link Context}.
1465 *
1466 * <p>This can only be called from the methods called by the static initializer of
1467 * {@link SystemServiceRegistry}. (Otherwise it throws a {@link IllegalStateException}.)
1468 *
1469 * @param serviceName the name of the binder object, such as
1470 * {@link Context#JOB_SCHEDULER_SERVICE}.
1471 * @param serviceWrapperClass the wrapper class, such as the class of
1472 * {@link android.app.job.JobScheduler}.
1473 * @param serviceProducer Callback that takes the service binder object with the name
1474 * {@code serviceName} and returns an actual service wrapper instance.
1475 *
1476 * @hide
1477 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001478 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001479 public static <TServiceClass> void registerStaticService(
1480 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1481 @NonNull StaticServiceProducerWithBinder<TServiceClass> serviceProducer) {
1482 ensureInitializing("registerStaticService");
1483 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001484 Objects.requireNonNull(serviceWrapperClass);
1485 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001486
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001487 registerService(serviceName, serviceWrapperClass,
Makoto Onuki05b14f52019-11-05 12:36:07 -08001488 new StaticServiceFetcher<TServiceClass>() {
Makoto Onuki792de8d2019-07-16 16:19:01 -07001489 @Override
Makoto Onuki05b14f52019-11-05 12:36:07 -08001490 public TServiceClass createService() throws ServiceNotFoundException {
1491 return serviceProducer.createService(
1492 ServiceManager.getServiceOrThrow(serviceName));
Makoto Onuki792de8d2019-07-16 16:19:01 -07001493 }});
1494 }
1495
1496 /**
Makoto Onuki05b14f52019-11-05 12:36:07 -08001497 * Similar to {@link #registerStaticService(String, Class, StaticServiceProducerWithBinder)},
1498 * but used for a "service wrapper" that doesn't take a service binder in its constructor.
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001499 *
1500 * @hide
1501 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001502 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001503 public static <TServiceClass> void registerStaticService(
1504 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001505 @NonNull StaticServiceProducerWithoutBinder<TServiceClass> serviceProducer) {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001506 ensureInitializing("registerStaticService");
1507 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001508 Objects.requireNonNull(serviceWrapperClass);
1509 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001510
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001511 registerService(serviceName, serviceWrapperClass,
Makoto Onuki05b14f52019-11-05 12:36:07 -08001512 new StaticServiceFetcher<TServiceClass>() {
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001513 @Override
Makoto Onuki05b14f52019-11-05 12:36:07 -08001514 public TServiceClass createService() {
1515 return serviceProducer.createService();
1516 }});
1517 }
1518
1519 /**
1520 * Used by apex modules to register a "service wrapper" that is tied to a specific
1521 * {@link Context}.
1522 *
1523 * <p>This can only be called from the methods called by the static initializer of
1524 * {@link SystemServiceRegistry}. (Otherwise it throws a {@link IllegalStateException}.)
1525 *
1526 * @param serviceName the name of the binder object, such as
1527 * {@link Context#JOB_SCHEDULER_SERVICE}.
1528 * @param serviceWrapperClass the wrapper class, such as the class of
1529 * {@link android.app.job.JobScheduler}.
1530 * @param serviceProducer lambda that takes the service binder object with the name
1531 * {@code serviceName}, a {@link Context} and returns an actual service wrapper instance.
1532 *
1533 * @hide
1534 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001535 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001536 public static <TServiceClass> void registerContextAwareService(
1537 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
1538 @NonNull ContextAwareServiceProducerWithBinder<TServiceClass> serviceProducer) {
1539 ensureInitializing("registerContextAwareService");
1540 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001541 Objects.requireNonNull(serviceWrapperClass);
1542 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001543
1544 registerService(serviceName, serviceWrapperClass,
1545 new CachedServiceFetcher<TServiceClass>() {
1546 @Override
1547 public TServiceClass createService(ContextImpl ctx)
1548 throws ServiceNotFoundException {
1549 return serviceProducer.createService(
1550 ctx.getOuterContext(),
1551 ServiceManager.getServiceOrThrow(serviceName));
1552 }});
1553 }
1554
1555
1556 /**
1557 * Similar to {@link #registerContextAwareService(String, Class,
1558 * ContextAwareServiceProducerWithBinder)},
1559 * but used for a "service wrapper" that doesn't take a service binder in its constructor.
1560 *
1561 * @hide
1562 */
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001563 @SystemApi
Makoto Onuki05b14f52019-11-05 12:36:07 -08001564 public static <TServiceClass> void registerContextAwareService(
1565 @NonNull String serviceName, @NonNull Class<TServiceClass> serviceWrapperClass,
Makoto Onuki0ceb3e02019-11-18 10:37:10 -08001566 @NonNull ContextAwareServiceProducerWithoutBinder<TServiceClass> serviceProducer) {
Makoto Onuki05b14f52019-11-05 12:36:07 -08001567 ensureInitializing("registerContextAwareService");
1568 Preconditions.checkStringNotEmpty(serviceName);
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00001569 Objects.requireNonNull(serviceWrapperClass);
1570 Objects.requireNonNull(serviceProducer);
Makoto Onuki05b14f52019-11-05 12:36:07 -08001571
1572 registerService(serviceName, serviceWrapperClass,
1573 new CachedServiceFetcher<TServiceClass>() {
1574 @Override
1575 public TServiceClass createService(ContextImpl ctx) {
1576 return serviceProducer.createService(ctx.getOuterContext());
Makoto Onuki7d6d9ca2019-08-13 11:45:45 -07001577 }});
1578 }
1579
1580 /**
Jeff Brown6e539312015-02-24 18:53:21 -08001581 * Base interface for classes that fetch services.
1582 * These objects must only be created during static initialization.
1583 */
1584 static abstract interface ServiceFetcher<T> {
1585 T getService(ContextImpl ctx);
1586 }
1587
1588 /**
1589 * Override this class when the system service constructor needs a
1590 * ContextImpl and should be cached and retained by that context.
1591 */
1592 static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
1593 private final int mCacheIndex;
1594
Makoto Onukid67070e2018-03-30 12:39:14 -07001595 CachedServiceFetcher() {
1596 // Note this class must be instantiated only by the static initializer of the
1597 // outer class (SystemServiceRegistry), which already does the synchronization,
1598 // so bare access to sServiceCacheSize is okay here.
Jeff Brown6e539312015-02-24 18:53:21 -08001599 mCacheIndex = sServiceCacheSize++;
1600 }
1601
1602 @Override
1603 @SuppressWarnings("unchecked")
1604 public final T getService(ContextImpl ctx) {
1605 final Object[] cache = ctx.mServiceCache;
Makoto Onukid67070e2018-03-30 12:39:14 -07001606 final int[] gates = ctx.mServiceInitializationStateArray;
Makoto Onukif896f122018-01-25 09:50:24 -08001607
Makoto Onukid67070e2018-03-30 12:39:14 -07001608 for (;;) {
1609 boolean doInitialize = false;
1610 synchronized (cache) {
1611 // Return it if we already have a cached instance.
1612 T service = (T) cache[mCacheIndex];
1613 if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) {
1614 return service;
1615 }
Makoto Onukif896f122018-01-25 09:50:24 -08001616
Makoto Onukid67070e2018-03-30 12:39:14 -07001617 // If we get here, there's no cached instance.
Makoto Onukif896f122018-01-25 09:50:24 -08001618
Makoto Onukid67070e2018-03-30 12:39:14 -07001619 // Grr... if gate is STATE_READY, then this means we initialized the service
1620 // once but someone cleared it.
1621 // We start over from STATE_UNINITIALIZED.
1622 if (gates[mCacheIndex] == ContextImpl.STATE_READY) {
1623 gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED;
1624 }
1625
1626 // It's possible for multiple threads to get here at the same time, so
1627 // use the "gate" to make sure only the first thread will call createService().
1628
1629 // At this point, the gate must be either UNINITIALIZED or INITIALIZING.
1630 if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) {
1631 doInitialize = true;
1632 gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING;
1633 }
Makoto Onukif896f122018-01-25 09:50:24 -08001634 }
1635
Makoto Onukid67070e2018-03-30 12:39:14 -07001636 if (doInitialize) {
1637 // Only the first thread gets here.
Makoto Onukif896f122018-01-25 09:50:24 -08001638
Makoto Onukid67070e2018-03-30 12:39:14 -07001639 T service = null;
1640 @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND;
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001641 try {
Makoto Onukid67070e2018-03-30 12:39:14 -07001642 // This thread is the first one to get here. Instantiate the service
1643 // *without* the cache lock held.
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001644 service = createService(ctx);
Makoto Onukid67070e2018-03-30 12:39:14 -07001645 newState = ContextImpl.STATE_READY;
Makoto Onukif896f122018-01-25 09:50:24 -08001646
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001647 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001648 onServiceNotFound(e);
Makoto Onukif896f122018-01-25 09:50:24 -08001649
Makoto Onukid67070e2018-03-30 12:39:14 -07001650 } finally {
1651 synchronized (cache) {
1652 cache[mCacheIndex] = service;
1653 gates[mCacheIndex] = newState;
1654 cache.notifyAll();
1655 }
1656 }
1657 return service;
1658 }
1659 // The other threads will wait for the first thread to call notifyAll(),
1660 // and go back to the top and retry.
1661 synchronized (cache) {
1662 while (gates[mCacheIndex] < ContextImpl.STATE_READY) {
1663 try {
1664 cache.wait();
1665 } catch (InterruptedException e) {
1666 Log.w(TAG, "getService() interrupted");
1667 Thread.currentThread().interrupt();
1668 return null;
1669 }
Makoto Onukif896f122018-01-25 09:50:24 -08001670 }
1671 }
Makoto Onukif896f122018-01-25 09:50:24 -08001672 }
Jeff Brown6e539312015-02-24 18:53:21 -08001673 }
1674
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001675 public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001676 }
1677
1678 /**
1679 * Override this class when the system service does not need a ContextImpl
1680 * and should be cached and retained process-wide.
1681 */
1682 static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
1683 private T mCachedInstance;
1684
1685 @Override
Jeff Sharkey589f3092016-11-29 12:54:39 -07001686 public final T getService(ContextImpl ctx) {
Jeff Brown6e539312015-02-24 18:53:21 -08001687 synchronized (StaticServiceFetcher.this) {
1688 if (mCachedInstance == null) {
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001689 try {
1690 mCachedInstance = createService();
1691 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001692 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001693 }
Jeff Brown6e539312015-02-24 18:53:21 -08001694 }
1695 return mCachedInstance;
1696 }
1697 }
1698
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001699 public abstract T createService() throws ServiceNotFoundException;
Jeff Brown6e539312015-02-24 18:53:21 -08001700 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001701
1702 /**
Lorenzo Colitticf959772016-02-29 16:07:37 +09001703 * Like StaticServiceFetcher, creates only one instance of the service per application, but when
1704 * creating the service for the first time, passes it the application context of the creating
1705 * application.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001706 *
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001707 * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
1708 * case where multiple application components each have their own ConnectivityManager object.
1709 */
Lorenzo Colitticf959772016-02-29 16:07:37 +09001710 static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T> {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001711 private T mCachedInstance;
1712
1713 @Override
1714 public final T getService(ContextImpl ctx) {
Lorenzo Colitticf959772016-02-29 16:07:37 +09001715 synchronized (StaticApplicationContextServiceFetcher.this) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001716 if (mCachedInstance == null) {
Lorenzo Colitti0ad9ab02016-03-25 17:40:11 +09001717 Context appContext = ctx.getApplicationContext();
1718 // If the application context is null, we're either in the system process or
1719 // it's the application context very early in app initialization. In both these
1720 // cases, the passed-in ContextImpl will not be freed, so it's safe to pass it
1721 // to the service. http://b/27532714 .
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001722 try {
1723 mCachedInstance = createService(appContext != null ? appContext : ctx);
1724 } catch (ServiceNotFoundException e) {
Jeff Sharkey589f3092016-11-29 12:54:39 -07001725 onServiceNotFound(e);
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001726 }
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001727 }
1728 return mCachedInstance;
1729 }
1730 }
1731
Jeff Sharkey49ca5292016-05-10 12:54:45 -06001732 public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001733 }
Jeff Sharkey589f3092016-11-29 12:54:39 -07001734
Makoto Onuki05b14f52019-11-05 12:36:07 -08001735 /** @hide */
Jeff Sharkey589f3092016-11-29 12:54:39 -07001736 public static void onServiceNotFound(ServiceNotFoundException e) {
1737 // We're mostly interested in tracking down long-lived core system
1738 // components that might stumble if they obtain bad references; just
1739 // emit a tidy log message for normal apps
1740 if (android.os.Process.myUid() < android.os.Process.FIRST_APPLICATION_UID) {
1741 Log.wtf(TAG, e.getMessage(), e);
1742 } else {
1743 Log.w(TAG, e.getMessage());
1744 }
1745 }
Jeff Brown6e539312015-02-24 18:53:21 -08001746}